diff options
author | crupest <crupest@outlook.com> | 2019-12-31 23:24:01 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2019-12-31 23:24:01 +0800 |
commit | 13d84d93e6695184a92cc0b7f85230e7b78d7a96 (patch) | |
tree | e60323b65ada0edac2e309235c16e4ae241b92ab /src | |
parent | ed6bab6393d768bd46f0072113edc80b9f33ac75 (diff) | |
download | cru-13d84d93e6695184a92cc0b7f85230e7b78d7a96.tar.gz cru-13d84d93e6695184a92cc0b7f85230e7b78d7a96.tar.bz2 cru-13d84d93e6695184a92cc0b7f85230e7b78d7a96.zip |
...
Diffstat (limited to 'src')
-rw-r--r-- | src/win/string.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/win/string.cpp b/src/win/string.cpp index c8b0ca87..fb9811eb 100644 --- a/src/win/string.cpp +++ b/src/win/string.cpp @@ -143,4 +143,44 @@ CodePoint Utf16Iterator::Next() { } } +int IndexUtf8ToUtf16(const std::string_view& utf8_string, int utf8_index, + const std::wstring_view& utf16_string) { + if (utf8_index >= static_cast<int>(utf8_string.length())) + return static_cast<int>(utf16_string.length()); + + int cp_index = 0; + Utf8Iterator iter{utf8_string}; + while (iter.CurrentPosition() <= utf8_index) { + iter.Next(); + cp_index++; + } + + Utf16Iterator result_iter{utf16_string}; + for (int i = 0; i < cp_index - 1; i++) { + if (result_iter.Next() == k_code_point_end) break; + } + + return result_iter.CurrentPosition(); +} + +int IndexUtf16ToUtf8(const std::wstring_view& utf16_string, int utf16_index, + const std::string_view& utf8_string) { + if (utf16_index >= static_cast<int>(utf16_string.length())) + return static_cast<int>(utf8_string.length()); + + int cp_index = 0; + Utf16Iterator iter{utf16_string}; + while (iter.CurrentPosition() <= utf16_index) { + iter.Next(); + cp_index++; + } + + Utf8Iterator result_iter{utf8_string}; + for (int i = 0; i < cp_index - 1; i++) { + if (result_iter.Next() == k_code_point_end) break; + } + + return result_iter.CurrentPosition(); +} + } // namespace cru::platform::win |