Add utf8_utf16_length.

GitOrigin-RevId: d5e713df1f3e0cdf70004d0898c5b55246dd014e
This commit is contained in:
levlam 2019-10-07 03:41:04 +03:00
parent c91efe472b
commit 4d68487c12
2 changed files with 10 additions and 4 deletions

View File

@ -21,10 +21,7 @@ Result<std::wstring> to_wstring(CSlice slice) {
return Status::Error("Wrong encoding");
}
size_t wstring_len = 0;
for (auto c : slice) {
wstring_len += ((c & 0xc0) != 0x80) + ((c & 0xf8) == 0xf0);
}
size_t wstring_len = utf8_utf16_length(slice);
std::wstring result(wstring_len, static_cast<wchar_t>(0));
if (wstring_len) {

View File

@ -28,6 +28,15 @@ inline size_t utf8_length(Slice str) {
return result;
}
/// returns length of UTF-8 string in UTF-16 code units
inline size_t utf8_utf16_length(Slice str) {
size_t result = 0;
for (auto c : str) {
result += is_utf8_character_first_code_unit(c) + ((c & 0xf8) == 0xf0);
}
return result;
}
/// appends a Unicode character using UTF-8 encoding
void append_utf8_character(string &str, uint32 ch);