Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Demonese/painful-cpp-string-conversion

Open more actions menu

Repository files navigation

Painful C++ string conversion

English 中文

CMake on multiple platforms

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>

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

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

You can customize namespace and function name:

#include <iostream>
#define PAINFUL_CPP_STRING_CONVERSION_NAMESPACE utf
#define PAINFUL_CPP_STRING_CONVERSION_FUNCTION  as
#include <ext/convert.hpp>

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

int main() {
  std::cout << utf::as<std::string>(U"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 ⚠️
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 span or span-like types as parameter
  • 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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.