diff options
Diffstat (limited to 'src/base')
-rw-r--r-- | src/base/Osx.cpp | 13 | ||||
-rw-r--r-- | src/base/StringUtil.cpp | 47 | ||||
-rw-r--r-- | src/base/io/Stream.cpp | 8 |
3 files changed, 62 insertions, 6 deletions
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<CFStringRef> ToCFString(StringView string) { +CFWrapper<CFStringRef> ToCFString(std::string_view string) { return CFWrapper<CFStringRef>(CFStringCreateWithBytes( nullptr, reinterpret_cast<const UInt8*>(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 <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 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 <algorithm> #include <format> +#include <iterator> #include <utility> 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<char>(c); }); + return result; } } // namespace cru::io |