aboutsummaryrefslogtreecommitdiff
path: root/src/base/StringUtil.cpp
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-10-17 21:50:24 +0800
committerYuqian Yang <crupest@crupest.life>2025-10-17 21:50:24 +0800
commit728d592f4075ae78b67dab6911ada05875a470a3 (patch)
tree57a7232ea0a984b0344bc63a593c1dee65072d2f /src/base/StringUtil.cpp
parent045462a6aed2796976a2f5cf0042f9a0ac1493f7 (diff)
downloadcru-728d592f4075ae78b67dab6911ada05875a470a3.tar.gz
cru-728d592f4075ae78b67dab6911ada05875a470a3.tar.bz2
cru-728d592f4075ae78b67dab6911ada05875a470a3.zip
Fix macOS build.
Diffstat (limited to 'src/base/StringUtil.cpp')
-rw-r--r--src/base/StringUtil.cpp47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/base/StringUtil.cpp b/src/base/StringUtil.cpp
index 581ebcab..4cd662d1 100644
--- a/src/base/StringUtil.cpp
+++ b/src/base/StringUtil.cpp
@@ -357,4 +357,51 @@ Index Utf16NextWord(const Utf16CodeUnit* ptr, Index size, Index position,
ptr, size, position, is_space);
}
+template <typename CharType,
+ NextCodePointFunctionType<CharType> NextCodePointFunction>
+static Index IndexCodeUnitToCodePoint(const CharType* ptr, Index size,
+ Index position) {
+ CodePointIterator<CharType, NextCodePointFunction> iter(ptr, size);
+ Index result = 0;
+ while (!iter.IsPastEnd() && iter.GetPosition() < position) {
+ ++iter;
+ ++result;
+ }
+ return result;
+}
+
+template <typename CharType,
+ NextCodePointFunctionType<CharType> NextCodePointFunction>
+static Index IndexCodePointToCodeUnit(const CharType* ptr, Index size,
+ Index position) {
+ CodePointIterator<CharType, NextCodePointFunction> iter(ptr, size);
+ for (Index i = 0; i < position; i++) {
+ ++iter;
+ }
+ return iter.GetPosition();
+}
+
+Index Utf8IndexCodeUnitToCodePoint(const Utf8CodeUnit* ptr, Index size,
+ Index position) {
+ return IndexCodeUnitToCodePoint<Utf8CodeUnit, Utf8NextCodePoint>(ptr, size,
+ position);
+}
+
+Index Utf8IndexCodePointToCodeUnit(const Utf8CodeUnit* ptr, Index size,
+ Index position) {
+ return IndexCodePointToCodeUnit<Utf8CodeUnit, Utf8NextCodePoint>(ptr, size,
+ position);
+}
+
+Index Utf16IndexCodeUnitToCodePoint(const Utf16CodeUnit* ptr, Index size,
+ Index position) {
+ return IndexCodeUnitToCodePoint<Utf16CodeUnit, Utf16NextCodePoint>(ptr, size,
+ position);
+}
+Index Utf16IndexCodePointToCodeUnit(const Utf16CodeUnit* ptr, Index size,
+ Index position) {
+ return IndexCodePointToCodeUnit<Utf16CodeUnit, Utf16NextCodePoint>(ptr, size,
+ position);
+}
+
} // namespace cru::string