aboutsummaryrefslogtreecommitdiff
path: root/src/common
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/common
parentce56ab59a6d68c220fcc47c6977c618eaa43de7a (diff)
downloadcru-bbec59718bf8a824583869126762013112f8e568.tar.gz
cru-bbec59718bf8a824583869126762013112f8e568.tar.bz2
cru-bbec59718bf8a824583869126762013112f8e568.zip
...
Diffstat (limited to 'src/common')
-rw-r--r--src/common/CMakeLists.txt2
-rw-r--r--src/common/StringUtil.cpp69
2 files changed, 71 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index d53b9740..6a18ef2b 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -1,6 +1,7 @@
set(CRU_BASE_INCLUDE_DIR ${CRU_INCLUDE_DIR}/cru/common)
add_library(cru_base STATIC
Logger.cpp
+ StringUtil.cpp
)
target_sources(cru_base PUBLIC
${CRU_BASE_INCLUDE_DIR}/Base.hpp
@@ -9,6 +10,7 @@ target_sources(cru_base PUBLIC
${CRU_BASE_INCLUDE_DIR}/Logger.hpp
${CRU_BASE_INCLUDE_DIR}/PreConfig.hpp
${CRU_BASE_INCLUDE_DIR}/SelfResolvable.hpp
+ ${CRU_BASE_INCLUDE_DIR}/StringUtil.hpp
)
target_include_directories(cru_base PUBLIC ${CRU_INCLUDE_DIR})
target_compile_definitions(cru_base PUBLIC $<$<CONFIG:Debug>:CRU_DEBUG>)
diff --git a/src/common/StringUtil.cpp b/src/common/StringUtil.cpp
new file mode 100644
index 00000000..31a5effd
--- /dev/null
+++ b/src/common/StringUtil.cpp
@@ -0,0 +1,69 @@
+#include "cru/common/StringUtil.hpp"
+
+namespace cru {
+template <typename UInt, int number_of_bit>
+inline std::enable_if_t<std::is_unsigned_v<UInt>, CodePoint> ExtractBits(
+ UInt n) {
+ return static_cast<CodePoint>(n & ((1u << number_of_bit) - 1));
+}
+
+CodePoint Utf8Iterator::Next() {
+ if (position_ == static_cast<Index>(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<Index>(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);
+ }
+}
+} // namespace cru