UCS2 conversion for unreal
This commit is contained in:
@@ -194,6 +194,30 @@ std::u32string from_utf8(std::string_view s, int *consumed) {
|
||||
return result.substr(0, len);
|
||||
}
|
||||
|
||||
std::u16string utf8_to_ucs2(std::string_view s, int *consumed) {
|
||||
std::string_view rest = s;
|
||||
std::u16string result(s.size(), 0);
|
||||
int len = 0;
|
||||
while (true) {
|
||||
int32_t c = read_codepoint_utf8(rest);
|
||||
if (c == -1) {
|
||||
break; // EOF reached;
|
||||
} else if (c < 0) {
|
||||
rest.remove_prefix(1);
|
||||
} else if ((c >= 0xD800) && (c <= 0xDFFF)) {
|
||||
result[len++] = 0x2610;
|
||||
} else if (c > 0xFFFF) {
|
||||
result[len++] = 0x2610;
|
||||
} else {
|
||||
result[len++] = (char16_t)c;
|
||||
}
|
||||
}
|
||||
if (consumed != nullptr) {
|
||||
*consumed = s.size() - rest.size();
|
||||
}
|
||||
return result.substr(0, len);
|
||||
}
|
||||
|
||||
static std::vector<std::string> parse_control_lst(std::string_view ctrl) {
|
||||
std::vector<std::string> result;
|
||||
while (!ctrl.empty()) {
|
||||
|
||||
@@ -63,6 +63,10 @@ std::string to_utf8(const std::u32string &cps);
|
||||
//
|
||||
std::u32string from_utf8(std::string_view source, int *consumed);
|
||||
|
||||
// Convert a UTF8 string to a UCS-2 string.
|
||||
//
|
||||
std::u16string utf8_to_ucs2(std::string_view source, int *consumed);
|
||||
|
||||
// Get a system error message, in an OS-independent manner.
|
||||
//
|
||||
// These versions of strerror is thread-safe, and it never fails
|
||||
|
||||
Reference in New Issue
Block a user