aboutsummaryrefslogtreecommitdiff
path: root/src/win/String.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-07-05 19:48:49 +0800
committercrupest <crupest@outlook.com>2020-07-05 19:48:49 +0800
commitbbec59718bf8a824583869126762013112f8e568 (patch)
tree1c0917e7c12a7af9ff3482f452a9da684a74ed81 /src/win/String.cpp
parentce56ab59a6d68c220fcc47c6977c618eaa43de7a (diff)
downloadcru-bbec59718bf8a824583869126762013112f8e568.tar.gz
cru-bbec59718bf8a824583869126762013112f8e568.tar.bz2
cru-bbec59718bf8a824583869126762013112f8e568.zip
...
Diffstat (limited to 'src/win/String.cpp')
-rw-r--r--src/win/String.cpp89
1 files changed, 15 insertions, 74 deletions
diff --git a/src/win/String.cpp b/src/win/String.cpp
index 65a280f2..eb585523 100644
--- a/src/win/String.cpp
+++ b/src/win/String.cpp
@@ -54,74 +54,16 @@ inline std::enable_if_t<std::is_unsigned_v<UInt>, CodePoint> ExtractBits(
return static_cast<CodePoint>(n & ((1u << number_of_bit) - 1));
}
-CodePoint Utf8Iterator::Next() {
- if (position_ == static_cast<int>(string_.length())) return k_code_point_end;
-
- const auto cu0 = static_cast<std::uint8_t>(string_[position_++]);
-
- auto read_next_folowing_code = [this]() -> CodePoint {
- if (this->position_ == static_cast<int>(string_.length()))
- throw TextEncodeException(
- "Unexpected end when read continuing byte of multi-byte code point.");
-
-#ifdef CRU_DEBUG
- const auto u = static_cast<std::uint8_t>(string_[position_]);
- if (!(u & (1u << 7)) || (u & (1u << 6))) {
- throw TextEncodeException(
- "Unexpected bad-format (not 0b10xxxxxx) continuing byte of "
- "multi-byte code point.");
- }
-#endif
-
- return ExtractBits<std::uint8_t, 6>(string_[position_++]);
- };
-
- if ((1u << 7) & cu0) {
- if ((1u << 6) & cu0) { // 2~4-length code point
- if ((1u << 5) & cu0) { // 3~4-length code point
- if ((1u << 4) & cu0) { // 4-length code point
-#ifdef CRU_DEBUG
- if (cu0 & (1u << 3)) {
- throw TextEncodeException(
- "Unexpected bad-format begin byte (not 0b10xxxxxx) of 4-byte "
- "code point.");
- }
-#endif
-
- const CodePoint s0 = ExtractBits<std::uint8_t, 3>(cu0) << (6 * 3);
- const CodePoint s1 = read_next_folowing_code() << (6 * 2);
- const CodePoint s2 = read_next_folowing_code() << 6;
- const CodePoint s3 = read_next_folowing_code();
- return s0 + s1 + s2 + s3;
- } else { // 3-length code point
- const CodePoint s0 = ExtractBits<std::uint8_t, 4>(cu0) << (6 * 2);
- const CodePoint s1 = read_next_folowing_code() << 6;
- const CodePoint s2 = read_next_folowing_code();
- return s0 + s1 + s2;
- }
- } else { // 2-length code point
- const CodePoint s0 = ExtractBits<std::uint8_t, 5>(cu0) << 6;
- const CodePoint s1 = read_next_folowing_code();
- return s0 + s1;
- }
- } else {
- throw TextEncodeException(
- "Unexpected bad-format (0b10xxxxxx) begin byte of a code point.");
- }
- } else {
- return static_cast<CodePoint>(cu0);
- }
-}
-
CodePoint Utf16Iterator::Next() {
- if (position_ == static_cast<int>(string_.length())) return k_code_point_end;
+ if (position_ == static_cast<Index>(string_.length()))
+ return k_code_point_end;
const auto cu0 = static_cast<std::uint16_t>(string_[position_++]);
if (cu0 < 0xd800u || cu0 >= 0xe000u) { // 1-length code point
return static_cast<CodePoint>(cu0);
} else if (cu0 <= 0xdbffu) { // 2-length code point
- if (position_ == static_cast<int>(string_.length())) {
+ if (position_ == static_cast<Index>(string_.length())) {
throw TextEncodeException(
"Unexpected end when reading second code unit of surrogate pair.");
}
@@ -145,12 +87,12 @@ 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());
+Index IndexUtf8ToUtf16(const std::string_view& utf8_string, Index utf8_index,
+ const std::wstring_view& utf16_string) {
+ if (utf8_index >= static_cast<Index>(utf8_string.length()))
+ return utf16_string.length();
- int cp_index = 0;
+ Index cp_index = 0;
Utf8Iterator iter{utf8_string};
while (iter.CurrentPosition() <= utf8_index) {
iter.Next();
@@ -158,19 +100,19 @@ int IndexUtf8ToUtf16(const std::string_view& utf8_string, int utf8_index,
}
Utf16Iterator result_iter{utf16_string};
- for (int i = 0; i < cp_index - 1; i++) {
+ for (Index 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());
+Index IndexUtf16ToUtf8(const std::wstring_view& utf16_string, Index utf16_index,
+ const std::string_view& utf8_string) {
+ if (utf16_index >= static_cast<Index>(utf16_string.length()))
+ return utf8_string.length();
- int cp_index = 0;
+ Index cp_index = 0;
Utf16Iterator iter{utf16_string};
while (iter.CurrentPosition() <= utf16_index) {
iter.Next();
@@ -178,11 +120,10 @@ int IndexUtf16ToUtf8(const std::wstring_view& utf16_string, int utf16_index,
}
Utf8Iterator result_iter{utf8_string};
- for (int i = 0; i < cp_index - 1; i++) {
+ for (Index i = 0; i < cp_index - 1; i++) {
if (result_iter.Next() == k_code_point_end) break;
}
return result_iter.CurrentPosition();
}
-
} // namespace cru::platform::win