From 728d592f4075ae78b67dab6911ada05875a470a3 Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Fri, 17 Oct 2025 21:50:24 +0800 Subject: Fix macOS build. --- src/base/Osx.cpp | 13 ++++++++----- src/base/StringUtil.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/base/io/Stream.cpp | 8 +++++++- 3 files changed, 62 insertions(+), 6 deletions(-) (limited to 'src/base') diff --git a/src/base/Osx.cpp b/src/base/Osx.cpp index 46b923fe..d979f542 100644 --- a/src/base/Osx.cpp +++ b/src/base/Osx.cpp @@ -1,19 +1,22 @@ #include "cru/base/Osx.h" +#include "cru/base/StringUtil.h" namespace cru { -CFWrapper ToCFString(StringView string) { +CFWrapper ToCFString(std::string_view string) { return CFWrapper(CFStringCreateWithBytes( nullptr, reinterpret_cast(string.data()), - string.size() * sizeof(std::uint16_t), kCFStringEncodingUTF16, false)); + string.size() * sizeof(char), kCFStringEncodingUTF8, false)); } -String FromCFStringRef(CFStringRef string) { +std::string FromCFStringRef(CFStringRef string) { auto length = CFStringGetLength(string); - String result; + std::string result; for (int i = 0; i < length; i++) { - result.AppendCodePoint(CFStringGetCharacterAtIndex(string, i)); + cru::string::Utf8EncodeCodePointAppend( + CFStringGetCharacterAtIndex(string, i), + [&result](char c) { result += c; }); } return result; 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 NextCodePointFunction> +static Index IndexCodeUnitToCodePoint(const CharType* ptr, Index size, + Index position) { + CodePointIterator iter(ptr, size); + Index result = 0; + while (!iter.IsPastEnd() && iter.GetPosition() < position) { + ++iter; + ++result; + } + return result; +} + +template NextCodePointFunction> +static Index IndexCodePointToCodeUnit(const CharType* ptr, Index size, + Index position) { + CodePointIterator 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(ptr, size, + position); +} + +Index Utf8IndexCodePointToCodeUnit(const Utf8CodeUnit* ptr, Index size, + Index position) { + return IndexCodePointToCodeUnit(ptr, size, + position); +} + +Index Utf16IndexCodeUnitToCodePoint(const Utf16CodeUnit* ptr, Index size, + Index position) { + return IndexCodeUnitToCodePoint(ptr, size, + position); +} +Index Utf16IndexCodePointToCodeUnit(const Utf16CodeUnit* ptr, Index size, + Index position) { + return IndexCodePointToCodeUnit(ptr, size, + position); +} + } // namespace cru::string diff --git a/src/base/io/Stream.cpp b/src/base/io/Stream.cpp index 1aafc839..e07f2899 100644 --- a/src/base/io/Stream.cpp +++ b/src/base/io/Stream.cpp @@ -1,7 +1,9 @@ #include "cru/base/io/Stream.h" #include "cru/base/Exception.h" +#include #include +#include #include namespace cru::io { @@ -193,6 +195,10 @@ Buffer Stream::ReadToEnd(Index grow_size) { std::string Stream::ReadToEndAsUtf8String() { auto buffer = ReadToEnd(); - return std::string(buffer.GetUsedBeginPtr(), buffer.GetUsedEndPtr()); + std::string result; + std::transform(buffer.GetUsedBeginPtr(), buffer.GetUsedEndPtr(), + std::back_inserter(result), + [](std::byte c) { return static_cast(c); }); + return result; } } // namespace cru::io -- cgit v1.2.3