Skip to content

Why the conversion between C++ string/wstring/u8string/u16string/u32string so painful?

License

Notifications You must be signed in to change notification settings

Demonese/painful-cpp-string-conversion

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Painful C++ string conversion

| English | 中文 |

Why the conversion between C++ string/wstring/u8string/u16string/u32string so painful?

Usage

You can convert strings through the following API:

  • encoding::to_string
  • encoding::to_u8string
  • encoding::to_u16string
  • encoding::to_u32string
  • encoding::to_wstring
#include <iostream>
#include <encoding/string.hpp>

using std::string_view_literals::operator""sv;

int main() {
  std::wcout << encoding::to_wstring("Hello world!"sv) << std::endl;
  return 0;
}

Conversion matrix

std::string std::u8string std::u16string std::u32string std::wstring
std::string ⚠️ ⚠️ ⚠️ ⚠️
std::u8string ⚠️
std::u16string ⚠️
std::u32string ⚠️
std::wstring ⚠️
  • ✅ OK.
  • ⚠️ Assuming that std::string always stores text in UTF-8 encoding.
  • std::wstring can be considered as std::u16string on Windows.

TODO

  • Supports raw character array as parameter
  • Supports span or span-like types as parameter
  • Clean up code and reduce duplication
  • Supports simdutf as a high-speed conversion implementation

About

Why the conversion between C++ string/wstring/u8string/u16string/u32string so painful?

Resources

License

Stars

Watchers

Forks