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

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:

  • ext::convert<std::string>
  • ext::convert<std::u8string>
  • ext::convert<std::u16string>
  • ext::convert<std::u32string>
  • ext::convert<std::wstring>
#include <iostream>
#include <ext/convert.hpp>
#include <ext/convert_windows.hpp>

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

int main() {
  std::wcout << ext::convert<std::wstring>("Hello world!"sv) << std::endl;
  return 0;
}

You can customize namespace:

#define PAINFUL_CPP_STRING_CONVERSION_NAMESPACE foo
#include <ext/convert.hpp>
#include <ext/convert_windows.hpp>

Conversion matrix

std::string std::u8string std::u16string std::u32string std::wstring
std::string ⚠️ ⚠️ ⚠️ ⚠️
std::u8string ⚠️
std::u16string ⚠️
std::u32string ⚠️
std::wstring ⚠️
std::string_view std::u8string_view
std::string_view ⚠️
std::u8string_view ⚠️
  • ✅ 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