refactor: bring char8_t to this library.

- add yycc_char8_t and yycc_u8string in code to indicate explicit utf8 char type and string. it also has a polyfill if compiler and library do not support utf8 char type.
- refactor the whole encoding helper. allow converting string with embedded NUL. but not tested.
This commit is contained in:
2024-06-26 21:04:56 +08:00
parent bb17bb6a1f
commit c15b57d055
4 changed files with 286 additions and 117 deletions

View File

@ -24,14 +24,23 @@
#endif
//// Decide the char type we used
//#include <string>
//namespace YYCC {
//#if defined(__cpp_char8_t)
// using u8char = char8_t;
// using u8string = std::std::string
//#else
// using u8char = char;
// using u8string = std::string;
//#endif
//}
// Define the UTF8 char type we used.
// Also define an universal macro to create UTF8 string literal.
// And do a polyfill if no embedded char8_t type.
#include <string>
namespace YYCC {
#if defined(__cpp_char8_t)
using yycc_char8_t = char8_t;
using yycc_u8string = std::u8string;
#define _YYCC_U8(strl) u8 ## strl
#define YYCC_U8(strl) (_YYCC_U8(strl))
#else
using yycc_char8_t = unsigned char;
using yycc_u8string = std::basic_string<yycc_char8_t>;
#define _YYCC_U8(strl) u8 ## strl
#define YYCC_U8(strl) (reinterpret_cast<const yycc_char8_t*>(_YYCC_U8(strl)))
#endif
}