diff options
| author | Yuqian Yang <crupest@crupest.life> | 2025-11-18 21:25:44 +0800 |
|---|---|---|
| committer | Yuqian Yang <crupest@crupest.life> | 2025-11-18 21:25:44 +0800 |
| commit | df2dadbd0f0ace6e02281c84218537ec2ce5c47f (patch) | |
| tree | 56a360c16c4ba46658572305d3a444fb30d32272 | |
| parent | 3648f669cb42cdd9d232d60c8b9715dfbbe5b31a (diff) | |
| download | cru-df2dadbd0f0ace6e02281c84218537ec2ce5c47f.tar.gz cru-df2dadbd0f0ace6e02281c84218537ec2ce5c47f.tar.bz2 cru-df2dadbd0f0ace6e02281c84218537ec2ce5c47f.zip | |
Add overload of string_view for string utils. clean up codes.
| -rw-r--r-- | include/cru/base/Base.h | 2 | ||||
| -rw-r--r-- | include/cru/base/Guard.h | 8 | ||||
| -rw-r--r-- | include/cru/base/StringUtil.h | 231 | ||||
| -rw-r--r-- | include/cru/base/io/Stream.h | 2 | ||||
| -rw-r--r-- | include/cru/platform/Base.h | 8 | ||||
| -rw-r--r-- | src/base/StringUtil.cpp | 4 | ||||
| -rw-r--r-- | src/base/log/StdioLogTarget.cpp | 2 | ||||
| -rw-r--r-- | src/base/platform/win/DebugLogTarget.cpp | 2 | ||||
| -rw-r--r-- | src/base/platform/win/Stream.cpp | 4 | ||||
| -rw-r--r-- | src/base/platform/win/Win32SubProcess.cpp | 8 | ||||
| -rw-r--r-- | src/platform/graphics/cairo/PangoTextLayout.cpp | 3 | ||||
| -rw-r--r-- | src/platform/graphics/direct2d/Font.cpp | 2 | ||||
| -rw-r--r-- | src/platform/graphics/direct2d/TextLayout.cpp | 26 | ||||
| -rw-r--r-- | src/platform/graphics/quartz/TextLayout.cpp | 28 | ||||
| -rw-r--r-- | src/platform/gui/osx/Window.mm | 19 | ||||
| -rw-r--r-- | src/platform/gui/win/Clipboard.cpp | 4 | ||||
| -rw-r--r-- | src/platform/gui/win/InputMethod.cpp | 26 | ||||
| -rw-r--r-- | src/platform/gui/win/Window.cpp | 4 | ||||
| -rw-r--r-- | src/ui/controls/TextHostControlService.cpp | 34 | ||||
| -rw-r--r-- | test/base/platform/win/StreamTest.cpp | 4 |
20 files changed, 309 insertions, 112 deletions
diff --git a/include/cru/base/Base.h b/include/cru/base/Base.h index 88682c96..fda45f66 100644 --- a/include/cru/base/Base.h +++ b/include/cru/base/Base.h @@ -121,7 +121,7 @@ class CRU_BASE_API Exception : public std::exception { ~Exception() override; public: - std::string GetUtf8Message() const { return this->message_; } + std::string GetMessage() const { return this->message_; } std::exception* GetInner() const noexcept { return inner_.get(); } diff --git a/include/cru/base/Guard.h b/include/cru/base/Guard.h index c05bc668..77e48d79 100644 --- a/include/cru/base/Guard.h +++ b/include/cru/base/Guard.h @@ -14,10 +14,10 @@ struct Guard { Guard() = default; explicit Guard(const ExitFunc& f) : on_exit(f) {} explicit Guard(ExitFunc&& f) : on_exit(std::move(f)) {} - Guard(const Guard&) = delete; - Guard(Guard&&) = default; - Guard& operator=(const Guard&) = delete; - Guard& operator=(Guard&&) = default; + + CRU_DELETE_COPY(Guard) + CRU_DEFAULT_MOVE(Guard) + ~Guard() { if (on_exit) { on_exit(); diff --git a/include/cru/base/StringUtil.h b/include/cru/base/StringUtil.h index d3afda1d..7748f0d3 100644 --- a/include/cru/base/StringUtil.h +++ b/include/cru/base/StringUtil.h @@ -1,6 +1,7 @@ #pragma once #include "Base.h" #include "Bitmask.h" +#include "cru/base/StringUtil.h" #include <algorithm> #include <cctype> @@ -209,7 +210,9 @@ struct ImplementFormatterByToString { using CodePoint = std::int32_t; using Utf8CodeUnit = char; +using Utf8StringView = std::string_view; using Utf16CodeUnit = char16_t; +using Utf16StringView = std::u16string_view; constexpr CodePoint k_invalid_code_point = -1; inline bool IsUtf8LeadingByte(Utf8CodeUnit c) { @@ -232,13 +235,23 @@ inline bool IsUtf16SurrogatePairTrailing(Utf16CodeUnit c) { return c >= 0xDC00 && c <= 0xDFFF; } -CodePoint CRU_BASE_API Utf8NextCodePoint(const char* ptr, Index size, +CodePoint CRU_BASE_API Utf8NextCodePoint(const Utf8CodeUnit* ptr, Index size, Index current, Index* next_position); -CodePoint CRU_BASE_API Utf8PreviousCodePoint(const char* ptr, Index size, - Index current, +inline CodePoint Utf8NextCodePoint(Utf8StringView str, Index current, + Index* next_position) { + return Utf8NextCodePoint(str.data(), str.size(), next_position); +} + +CodePoint CRU_BASE_API Utf8PreviousCodePoint(const Utf8CodeUnit* ptr, + Index size, Index current, Index* previous_position); +inline CodePoint Utf8PreviousCodePoint(Utf8StringView str, Index current, + Index* next_position) { + return Utf8PreviousCodePoint(str.data(), str.size(), next_position); +} + namespace details { template <typename Integer, int number_of_bit, typename ReturnType> inline ReturnType ExtractBits(Integer n) { @@ -297,28 +310,92 @@ Utf8EncodeCodePointAppend(CodePoint code_point, CharWriter&& writer) { bool CRU_BASE_API Utf8IsValidInsertPosition(const Utf8CodeUnit* ptr, Index size, Index position); +inline bool Utf8IsValidInsertPosition(Utf8StringView str, Index position) { + return Utf8IsValidInsertPosition(str.data(), str.size(), position); +} + // Return position after the character making predicate returns true or 0 if no // character doing so. Index CRU_BASE_API Utf8BackwardUntil(const Utf8CodeUnit* ptr, Index size, Index position, const std::function<bool(CodePoint)>& predicate); + +inline Index Utf8BackwardUntil( + Utf8StringView str, Index position, + const std::function<bool(CodePoint)>& predicate) { + return Utf8BackwardUntil(str.data(), str.size(), position, predicate); +} + // Return position before the character making predicate returns true or // str.size() if no character doing so. Index CRU_BASE_API Utf8ForwardUntil(const Utf8CodeUnit* ptr, Index size, Index position, const std::function<bool(CodePoint)>& predicate); +inline Index Utf8ForwardUntil(Utf8StringView str, Index position, + const std::function<bool(CodePoint)>& predicate) { + return Utf8ForwardUntil(str.data(), str.size(), position, predicate); +} + Index CRU_BASE_API Utf8PreviousWord(const Utf8CodeUnit* ptr, Index size, Index position, bool* is_space = nullptr); + +inline Index Utf8PreviousWord(Utf8StringView str, Index position, + bool* is_space = nullptr) { + return Utf8PreviousWord(str.data(), str.size(), position, is_space); +} + Index CRU_BASE_API Utf8NextWord(const Utf8CodeUnit* ptr, Index size, Index position, bool* is_space = nullptr); +inline Index Utf8NextWord(Utf8StringView str, Index position, + bool* is_space = nullptr) { + return Utf8NextWord(str.data(), str.size(), position, is_space); +} + CodePoint CRU_BASE_API Utf16NextCodePoint(const Utf16CodeUnit* ptr, Index size, Index current, Index* next_position); + +inline CodePoint Utf16NextCodePoint(Utf16StringView str, Index current, + Index* next_position) { + return Utf16NextCodePoint(str.data(), str.size(), current, next_position); +} + +#ifdef _WIN32 +inline CodePoint Utf16NextCodePoint(const wchar_t* ptr, Index size, + Index current, Index* next_position) { + return Utf16NextCodePoint(reinterpret_cast<const Utf16CodeUnit*>(ptr), size, + current, next_position); +} + +inline CodePoint Utf16NextCodePoint(std::wstring_view str, Index current, + Index* next_position) { + return Utf16NextCodePoint(str.data(), str.size(), current, next_position); +} +#endif + CodePoint CRU_BASE_API Utf16PreviousCodePoint(const Utf16CodeUnit* ptr, Index size, Index current, Index* previous_position); +inline CodePoint Utf16PreviousCodePoint(Utf16StringView str, Index current, + Index* next_position) { + return Utf16PreviousCodePoint(str.data(), str.size(), current, next_position); +} + +#ifdef _WIN32 +inline CodePoint Utf16PreviousCodePoint(const wchar_t* ptr, Index size, + Index current, Index* next_position) { + return Utf16PreviousCodePoint(reinterpret_cast<const Utf16CodeUnit*>(ptr), + size, current, next_position); +} + +inline CodePoint Utf16PreviousCodePoint(std::wstring_view str, Index current, + Index* next_position) { + return Utf16PreviousCodePoint(str.data(), str.size(), current, next_position); +} +#endif + template <typename CharWriter> std::enable_if_t<std::is_invocable_v<CharWriter, Utf16CodeUnit>, bool> Utf16EncodeCodePointAppend(CodePoint code_point, CharWriter&& writer) { @@ -343,22 +420,119 @@ Utf16EncodeCodePointAppend(CodePoint code_point, CharWriter&& writer) { bool CRU_BASE_API Utf16IsValidInsertPosition(const Utf16CodeUnit* ptr, Index size, Index position); +inline bool Utf16IsValidInsertPosition(Utf16StringView str, Index position) { + return Utf16IsValidInsertPosition(str.data(), str.size(), position); +} + +#ifdef _WIN32 +inline bool Utf16IsValidInsertPosition(const wchar_t* ptr, Index size, + Index position) { + return Utf16IsValidInsertPosition(reinterpret_cast<const Utf16CodeUnit*>(ptr), + size, position); +} + +inline CodePoint Utf16IsValidInsertPosition(std::wstring_view str, + Index position) { + return Utf16IsValidInsertPosition(str.data(), str.size(), position); +} +#endif + // Return position after the character making predicate returns true or 0 if no // character doing so. Index CRU_BASE_API Utf16BackwardUntil(const Utf16CodeUnit* ptr, Index size, Index position, const std::function<bool(CodePoint)>& predicate); + +inline Index Utf16BackwardUntil( + Utf16StringView str, Index position, + const std::function<bool(CodePoint)>& predicate) { + return Utf16BackwardUntil(str.data(), str.size(), position, predicate); +} + +#ifdef _WIN32 +inline CodePoint Utf16BackwardUntil( + const wchar_t* ptr, Index size, Index position, + const std::function<bool(CodePoint)>& predicate) { + return Utf16BackwardUntil(reinterpret_cast<const Utf16CodeUnit*>(ptr), size, + position, predicate); +} + +inline CodePoint Utf16BackwardUntil( + std::wstring_view str, Index position, + const std::function<bool(CodePoint)>& predicate) { + return Utf16BackwardUntil(str.data(), str.size(), position, predicate); +} +#endif + // Return position before the character making predicate returns true or // str.size() if no character doing so. Index CRU_BASE_API Utf16ForwardUntil(const Utf16CodeUnit* ptr, Index size, Index position, const std::function<bool(CodePoint)>& predicate); +inline Index Utf16ForwardUntil( + Utf16StringView str, Index position, + const std::function<bool(CodePoint)>& predicate) { + return Utf16ForwardUntil(str.data(), str.size(), position, predicate); +} + +#ifdef _WIN32 +inline CodePoint Utf16ForwardUntil( + const wchar_t* ptr, Index size, Index position, + const std::function<bool(CodePoint)>& predicate) { + return Utf16ForwardUntil(reinterpret_cast<const Utf16CodeUnit*>(ptr), size, + position, predicate); +} + +inline CodePoint Utf16ForwardUntil( + std::wstring_view str, Index position, + const std::function<bool(CodePoint)>& predicate) { + return Utf16ForwardUntil(str.data(), str.size(), position, predicate); +} +#endif + Index CRU_BASE_API Utf16PreviousWord(const Utf16CodeUnit* ptr, Index size, Index position, bool* is_space = nullptr); + +inline Index Utf16PreviousWord(Utf16StringView str, Index position, + bool* is_space = nullptr) { + return Utf16PreviousWord(str.data(), str.size(), position, is_space); +} + +#ifdef _WIN32 +inline CodePoint Utf16PreviousWord(const wchar_t* ptr, Index size, + Index position, bool* is_space = nullptr) { + return Utf16PreviousWord(reinterpret_cast<const Utf16CodeUnit*>(ptr), size, + position, is_space); +} + +inline CodePoint Utf16PreviousWord(std::wstring_view str, Index position, + bool* is_space = nullptr) { + return Utf16PreviousWord(str.data(), str.size(), position, is_space); +} +#endif + Index CRU_BASE_API Utf16NextWord(const Utf16CodeUnit* ptr, Index size, Index position, bool* is_space = nullptr); +inline Index Utf16NextWord(Utf16StringView str, Index position, + bool* is_space = nullptr) { + return Utf16NextWord(str.data(), str.size(), position, is_space); +} + +#ifdef _WIN32 +inline CodePoint Utf16NextWord(const wchar_t* ptr, Index size, Index position, + bool* is_space = nullptr) { + return Utf16NextWord(reinterpret_cast<const Utf16CodeUnit*>(ptr), size, + position, is_space); +} + +inline CodePoint Utf16NextWord(std::wstring_view str, Index position, + bool* is_space = nullptr) { + return Utf16NextWord(str.data(), str.size(), position, is_space); +} +#endif + template <typename CharType> using NextCodePointFunctionType = CodePoint (*)(const CharType*, Index, Index, Index*); @@ -448,15 +622,62 @@ using Utf16CodePointIterator = Index CRU_BASE_API Utf8IndexCodeUnitToCodePoint(const Utf8CodeUnit* ptr, Index size, Index position); + +inline Index Utf8IndexCodeUnitToCodePoint(Utf8StringView str, Index position) { + return Utf8IndexCodeUnitToCodePoint(str.data(), str.size(), position); +} + Index CRU_BASE_API Utf8IndexCodePointToCodeUnit(const Utf8CodeUnit* ptr, Index size, Index position); + +inline Index Utf8IndexCodePointToCodeUnit(Utf8StringView str, Index position) { + return Utf8IndexCodePointToCodeUnit(str.data(), str.size(), position); +} + Index CRU_BASE_API Utf16IndexCodeUnitToCodePoint(const Utf16CodeUnit* ptr, Index size, Index position); + +inline Index Utf16IndexCodeUnitToCodePoint(Utf16StringView str, + Index position) { + return Utf16IndexCodeUnitToCodePoint(str.data(), str.size(), position); +} + +#ifdef _WIN32 +inline Index Utf16IndexCodeUnitToCodePoint(const wchar_t* ptr, Index size, + Index position) { + return Utf16IndexCodeUnitToCodePoint(reinterpret_cast<const char16_t*>(ptr), + size, position); +} + +inline Index Utf16IndexCodeUnitToCodePoint(std::wstring_view str, + Index position) { + return Utf16IndexCodeUnitToCodePoint(str.data(), str.size(), position); +} +#endif + Index CRU_BASE_API Utf16IndexCodePointToCodeUnit(const Utf16CodeUnit* ptr, Index size, Index position); +inline Index Utf16IndexCodePointToCodeUnit(Utf16StringView str, + Index position) { + return Utf16IndexCodePointToCodeUnit(str.data(), str.size(), position); +} + +#ifdef _WIN32 +inline Index Utf16IndexCodePointToCodeUnit(const wchar_t* ptr, Index size, + Index position) { + return Utf16IndexCodePointToCodeUnit(reinterpret_cast<const char16_t*>(ptr), + size, position); +} + +inline Index Utf16IndexCodePointToCodeUnit(std::wstring_view str, + Index position) { + return Utf16IndexCodePointToCodeUnit(str.data(), str.size(), position); +} +#endif + #ifdef _WIN32 -std::wstring CRU_BASE_API ToUtf16(std::string_view str); -std::string CRU_BASE_API ToUtf8(std::wstring_view str); +std::wstring CRU_BASE_API ToUtf16WString(std::string_view str); +std::string CRU_BASE_API ToUtf8String(std::wstring_view str); #endif } // namespace cru::string diff --git a/include/cru/base/io/Stream.h b/include/cru/base/io/Stream.h index cbcb3ced..8c0d3669 100644 --- a/include/cru/base/io/Stream.h +++ b/include/cru/base/io/Stream.h @@ -11,7 +11,7 @@ class CRU_BASE_API StreamOperationNotSupportedException : public Exception { explicit StreamOperationNotSupportedException(std::string operation); public: - std::string GetOperationUtf8() const { return operation_; } + std::string GetOperation() const { return operation_; } public: static void CheckSeek(bool seekable); diff --git a/include/cru/platform/Base.h b/include/cru/platform/Base.h index b910cde1..7009c145 100644 --- a/include/cru/platform/Base.h +++ b/include/cru/platform/Base.h @@ -35,8 +35,8 @@ class CRU_PLATFORM_API PlatformNotMatchException : public PlatformException { ~PlatformNotMatchException() override; - std::string GetResourcePlatformUtf8() const { return resource_platform_; } - std::string GetTargetPlatformUtf8() const { return target_platform_; } + std::string GetResourcePlatform() const { return resource_platform_; } + std::string GetTargetPlatform() const { return target_platform_; } private: std::string resource_platform_; @@ -62,8 +62,8 @@ class CRU_PLATFORM_API PlatformUnsupportedException : public PlatformException { ~PlatformUnsupportedException() override; - std::string GetPlatformUtf8() const { return platform_; } - std::string GetOperationUtf8() const { return operation_; } + std::string GetPlatform() const { return platform_; } + std::string GetOperation() const { return operation_; } private: std::string platform_; diff --git a/src/base/StringUtil.cpp b/src/base/StringUtil.cpp index a5cc3122..eaa3f58e 100644 --- a/src/base/StringUtil.cpp +++ b/src/base/StringUtil.cpp @@ -404,7 +404,7 @@ Index Utf16IndexCodePointToCodeUnit(const Utf16CodeUnit* ptr, Index size, } #ifdef _WIN32 -std::wstring ToUtf16(std::string_view str) { +std::wstring ToUtf16WString(std::string_view str) { Utf8CodePointIterator iter(str.data(),str.size()); std::wstring result; for (auto c : iter) { @@ -413,7 +413,7 @@ std::wstring ToUtf16(std::string_view str) { return result; } -std::string ToUtf8(std::wstring_view str) { +std::string ToUtf8String(std::wstring_view str) { Utf16CodePointIterator iter(reinterpret_cast<const char16_t*>( str.data()),str.size()); std::string result; for (auto c : iter) { diff --git a/src/base/log/StdioLogTarget.cpp b/src/base/log/StdioLogTarget.cpp index bdcdd783..4b77f755 100644 --- a/src/base/log/StdioLogTarget.cpp +++ b/src/base/log/StdioLogTarget.cpp @@ -10,7 +10,7 @@ StdioLogTarget::~StdioLogTarget() {} void StdioLogTarget::Write(log::LogLevel level, std::string message) { #ifdef _WIN32 - auto s = string::ToUtf16(message); + auto s = string::ToUtf16WString(message); if (level == log::LogLevel::Error) { std::wcerr << s << std::endl; } else { diff --git a/src/base/platform/win/DebugLogTarget.cpp b/src/base/platform/win/DebugLogTarget.cpp index 3b73b332..d6dc1d95 100644 --- a/src/base/platform/win/DebugLogTarget.cpp +++ b/src/base/platform/win/DebugLogTarget.cpp @@ -6,7 +6,7 @@ namespace cru::platform::win { void WinDebugLogTarget::Write(::cru::log::LogLevel level, std::string s) { CRU_UNUSED(level) - std::wstring m = string::ToUtf16(s); + std::wstring m = string::ToUtf16WString(s); ::OutputDebugStringW(reinterpret_cast<const wchar_t*>(m.c_str())); } } // namespace cru::platform::win diff --git a/src/base/platform/win/Stream.cpp b/src/base/platform/win/Stream.cpp index d4b24d64..72466905 100644 --- a/src/base/platform/win/Stream.cpp +++ b/src/base/platform/win/Stream.cpp @@ -43,7 +43,7 @@ HANDLE OpenHandle(std::string_view path, OpenFileFlag flags) { IStream* stream; auto handle = - ::CreateFileW(cru::string::ToUtf16(path).c_str(), access, 0, nullptr, + ::CreateFileW(cru::string::ToUtf16WString(path).c_str(), access, 0, nullptr, creation_disposition, FILE_ATTRIBUTE_NORMAL, nullptr); if (handle == INVALID_HANDLE_VALUE) { @@ -163,7 +163,7 @@ IStream* OpenComStream(std::string_view path, OpenFileFlag flags) { IStream* stream; CheckHResult(SHCreateStreamOnFileEx( - cru::string::ToUtf16(path).c_str(), grfMode, FILE_ATTRIBUTE_NORMAL, + cru::string::ToUtf16WString(path).c_str(), grfMode, FILE_ATTRIBUTE_NORMAL, flags & io::OpenFileFlags::Create ? TRUE : FALSE, NULL, &stream)); return stream; diff --git a/src/base/platform/win/Win32SubProcess.cpp b/src/base/platform/win/Win32SubProcess.cpp index c97f3d66..aed3937c 100644 --- a/src/base/platform/win/Win32SubProcess.cpp +++ b/src/base/platform/win/Win32SubProcess.cpp @@ -8,7 +8,7 @@ #include <string_view> namespace cru::platform::win { -using cru::string::ToUtf16; +using cru::string::ToUtf16WString; Win32SubProcessImpl::Win32SubProcessImpl() : exit_code_(0) {} @@ -23,14 +23,14 @@ void Win32SubProcessImpl::PlatformCreateProcess( std::move(inner)); }; - auto app = ToUtf16(start_info.program); + auto app = ToUtf16WString(start_info.program); // TODO: Space and quoting problem. auto command_line = - app + L" " + ToUtf16(cru::string::Join(" ", start_info.arguments)); + app + L" " + ToUtf16WString(cru::string::Join(" ", start_info.arguments)); std::wstring env_str; for (const auto& [key, value] : start_info.environments) { - env_str += ToUtf16(key) + L"=" + ToUtf16(value) + L"\0"; + env_str += ToUtf16WString(key) + L"=" + ToUtf16WString(value) + L"\0"; } env_str += L"\0"; diff --git a/src/platform/graphics/cairo/PangoTextLayout.cpp b/src/platform/graphics/cairo/PangoTextLayout.cpp index 25d9f85d..98ad4f45 100644 --- a/src/platform/graphics/cairo/PangoTextLayout.cpp +++ b/src/platform/graphics/cairo/PangoTextLayout.cpp @@ -165,8 +165,7 @@ TextHitTestResult PangoTextLayout::HitTest(const Point& point) { if (result.trailing) { Index position_with_trailing; - string::Utf8NextCodePoint(text_.data(), text_.size(), result.position, - &position_with_trailing); + string::Utf8NextCodePoint(text_, result.position, &position_with_trailing); result.position_with_trailing = position_with_trailing; } else { result.position_with_trailing = result.position; diff --git a/src/platform/graphics/direct2d/Font.cpp b/src/platform/graphics/direct2d/Font.cpp index 18a4a2c7..4e0a5e49 100644 --- a/src/platform/graphics/direct2d/Font.cpp +++ b/src/platform/graphics/direct2d/Font.cpp @@ -17,7 +17,7 @@ DWriteFont::DWriteFont(DirectGraphicsFactory* factory, std::string font_family, ::GetLastError(), "Failed to get locale when create dwrite font."); CheckHResult(factory->GetDWriteFactory()->CreateTextFormat( - string::ToUtf16(font_family_).c_str(), nullptr, DWRITE_FONT_WEIGHT_NORMAL, + string::ToUtf16WString(font_family_).c_str(), nullptr, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, font_size, buffer.data(), &text_format_)); diff --git a/src/platform/graphics/direct2d/TextLayout.cpp b/src/platform/graphics/direct2d/TextLayout.cpp index 3fc91616..18784ccb 100644 --- a/src/platform/graphics/direct2d/TextLayout.cpp +++ b/src/platform/graphics/direct2d/TextLayout.cpp @@ -13,7 +13,7 @@ DWriteTextLayout::DWriteTextLayout(DirectGraphicsFactory* factory, Expects(font); font_ = CheckPlatform<DWriteFont>(font, GetPlatformId()); - utf16_text_ = string::ToUtf16(text_); + utf16_text_ = string::ToUtf16WString(text_); CheckHResult(factory->GetDWriteFactory()->CreateTextLayout( utf16_text_.c_str(), static_cast<UINT32>(utf16_text_.size()), font_->GetComInterface(), max_width_, max_height_, &text_layout_)); @@ -25,7 +25,7 @@ std::string DWriteTextLayout::GetText() { return text_; } void DWriteTextLayout::SetText(std::string new_text) { text_ = std::move(new_text); - utf16_text_ = string::ToUtf16(text_); + utf16_text_ = string::ToUtf16WString(text_); CheckHResult(GetDirectFactory()->GetDWriteFactory()->CreateTextLayout( utf16_text_.c_str(), static_cast<UINT32>(utf16_text_.size()), font_->GetComInterface(), max_width_, max_height_, &text_layout_)); @@ -108,15 +108,11 @@ std::vector<Rect> DWriteTextLayout::TextRangeRect( const auto text_range = Range::FromTwoSides( string::Utf16IndexCodePointToCodeUnit( - reinterpret_cast<const char16_t*>(utf16_text_.data()), - utf16_text_.size(), - string::Utf8IndexCodeUnitToCodePoint(text_.data(), text_.size(), - text_range_arg.GetStart())), + utf16_text_, string::Utf8IndexCodeUnitToCodePoint( + text_, text_range_arg.GetStart())), string::Utf16IndexCodePointToCodeUnit( - reinterpret_cast<const char16_t*>(utf16_text_.data()), - utf16_text_.size(), - string::Utf8IndexCodeUnitToCodePoint(text_.data(), text_.size(), - text_range_arg.GetEnd()))) + utf16_text_, string::Utf8IndexCodeUnitToCodePoint( + text_, text_range_arg.GetEnd()))) .Normalize(); DWRITE_TEXT_METRICS text_metrics; @@ -147,9 +143,7 @@ std::vector<Rect> DWriteTextLayout::TextRangeRect( Rect DWriteTextLayout::TextSinglePoint(Index position, bool trailing) { position = string::Utf16IndexCodePointToCodeUnit( - reinterpret_cast<const char16_t*>(utf16_text_.data()), utf16_text_.size(), - string::Utf8IndexCodeUnitToCodePoint(text_.data(), text_.size(), - position)); + utf16_text_, string::Utf8IndexCodeUnitToCodePoint(text_, position)); DWRITE_HIT_TEST_METRICS metrics; FLOAT left; FLOAT top; @@ -169,10 +163,8 @@ TextHitTestResult DWriteTextLayout::HitTest(const Point& point) { TextHitTestResult result; result.position = string::Utf8IndexCodePointToCodeUnit( - text_.data(), text_.size(), - string::Utf16IndexCodeUnitToCodePoint( - reinterpret_cast<const char16_t*>(utf16_text_.data()), - utf16_text_.size(), metrics.textPosition)); + text_, + string::Utf16IndexCodeUnitToCodePoint(utf16_text_, metrics.textPosition)); result.trailing = trailing != 0; if (result.trailing) { diff --git a/src/platform/graphics/quartz/TextLayout.cpp b/src/platform/graphics/quartz/TextLayout.cpp index 4793a19c..80cb8a3f 100644 --- a/src/platform/graphics/quartz/TextLayout.cpp +++ b/src/platform/graphics/quartz/TextLayout.cpp @@ -238,28 +238,25 @@ TextHitTestResult OsxCTTextLayout::HitTest(const Point& point) { bool inside_text; if (pp.x < bounds.origin.x) { - po = cru::string::Utf8IndexCodePointToCodeUnit( - actual_text_.data(), actual_text_.size(), range.location); + po = cru::string::Utf8IndexCodePointToCodeUnit(actual_text_, + range.location); inside_text = false; } else if (pp.x > bounds.origin.x + bounds.size.width) { po = cru::string::Utf8IndexCodePointToCodeUnit( - actual_text_.data(), actual_text_.size(), - range.location + range.length); + actual_text_, range.location + range.length); inside_text = false; } else { int position = CTLineGetStringIndexForPosition( line, CGPointMake(pp.x - line_origins_[i].x, pp.y - line_origins_[i].y)); - po = cru::string::Utf8IndexCodePointToCodeUnit( - actual_text_.data(), actual_text_.size(), position); + po = cru::string::Utf8IndexCodePointToCodeUnit(actual_text_, position); inside_text = true; } if (po != 0 && po == cru::string::Utf8IndexCodePointToCodeUnit( - actual_text_.data(), actual_text_.size(), - range.location + range.length) && + actual_text_, range.location + range.length) && actual_text_[po - 1] == u'\n') { --po; } @@ -405,13 +402,11 @@ CGRect OsxCTTextLayout::DoGetTextBoundsIncludingEmptyLines( std::vector<CGRect> OsxCTTextLayout::DoTextRangeRect( const TextRange& text_range) { - const auto r = - Range::FromTwoSides( - cru::string::Utf8IndexCodeUnitToCodePoint( - actual_text_.data(), actual_text_.size(), text_range.position), - cru::string::Utf8IndexCodeUnitToCodePoint( - actual_text_.data(), actual_text_.size(), text_range.GetEnd())) - .Normalize(); + const auto r = Range::FromTwoSides(cru::string::Utf8IndexCodeUnitToCodePoint( + actual_text_, text_range.position), + cru::string::Utf8IndexCodeUnitToCodePoint( + actual_text_, text_range.GetEnd())) + .Normalize(); std::vector<CGRect> results; @@ -443,8 +438,7 @@ CGRect OsxCTTextLayout::DoTextSinglePoint(Index position, bool trailing) { if (actual_text_.empty()) return CGRectMake(0, 0, 0, font_->GetFontSize()); - position = cru::string::Utf8IndexCodeUnitToCodePoint( - actual_text_.data(), actual_text_.size(), position); + position = cru::string::Utf8IndexCodeUnitToCodePoint(actual_text_, position); for (int i = 0; i < line_count_; i++) { auto line = lines_[i]; diff --git a/src/platform/gui/osx/Window.mm b/src/platform/gui/osx/Window.mm index 167adfc5..2d30724f 100644 --- a/src/platform/gui/osx/Window.mm +++ b/src/platform/gui/osx/Window.mm @@ -3,9 +3,9 @@ #include "CursorPrivate.h" #include "InputMethodPrivate.h" -#include "cru/base/platform/osx/Base.h" #include "cru/base/Range.h" #include "cru/base/log/Logger.h" +#include "cru/base/platform/osx/Base.h" #include "cru/platform/graphics/NullPainter.h" #include "cru/platform/graphics/quartz/Painter.h" #include "cru/platform/gui/Input.h" @@ -384,9 +384,15 @@ IEvent<MouseEnterLeaveType>* OsxWindow::MouseEnterLeaveEvent() { return &p_->mouse_enter_leave_event_; } IEvent<const Point&>* OsxWindow::MouseMoveEvent() { return &p_->mouse_move_event_; } -IEvent<const NativeMouseButtonEventArgs&>* OsxWindow::MouseDownEvent() { return &p_->mouse_down_event_; } -IEvent<const NativeMouseButtonEventArgs&>* OsxWindow::MouseUpEvent() { return &p_->mouse_up_event_; } -IEvent<const NativeMouseWheelEventArgs&>* OsxWindow::MouseWheelEvent() { return &p_->mouse_wheel_event_; } +IEvent<const NativeMouseButtonEventArgs&>* OsxWindow::MouseDownEvent() { + return &p_->mouse_down_event_; +} +IEvent<const NativeMouseButtonEventArgs&>* OsxWindow::MouseUpEvent() { + return &p_->mouse_up_event_; +} +IEvent<const NativeMouseWheelEventArgs&>* OsxWindow::MouseWheelEvent() { + return &p_->mouse_wheel_event_; +} IEvent<const NativeKeyEventArgs&>* OsxWindow::KeyDownEvent() { return &p_->key_down_event_; } IEvent<const NativeKeyEventArgs&>* OsxWindow::KeyUpEvent() { return &p_->key_up_event_; } @@ -685,10 +691,9 @@ const std::unordered_set<KeyCode> input_context_handle_codes_when_has_text{ cru::platform::gui::CompositionText composition_text; composition_text.text = FromCFStringRef((CFStringRef)[_input_context_text string]); composition_text.selection.position = - cru::string::Utf8IndexCodePointToCodeUnit(ss.data(), ss.size(), selectedRange.location); + cru::string::Utf8IndexCodePointToCodeUnit(ss, selectedRange.location); composition_text.selection.count = - cru::string::Utf8IndexCodePointToCodeUnit(ss.data(), ss.size(), - selectedRange.location + selectedRange.length) - + cru::string::Utf8IndexCodePointToCodeUnit(ss, selectedRange.location + selectedRange.length) - composition_text.selection.position; _input_context_p->SetCompositionText(composition_text); _input_context_p->RaiseCompositionEvent(); diff --git a/src/platform/gui/win/Clipboard.cpp b/src/platform/gui/win/Clipboard.cpp index 7062f160..2a41a1eb 100644 --- a/src/platform/gui/win/Clipboard.cpp +++ b/src/platform/gui/win/Clipboard.cpp @@ -39,11 +39,11 @@ std::string WinClipboard::GetText() { ::GlobalUnlock(handle); ::CloseClipboard(); - return string::ToUtf8(result); + return string::ToUtf8String(result); } void WinClipboard::SetText(std::string utf8_text) { - auto text = string::ToUtf16(utf8_text); + auto text = string::ToUtf16WString(utf8_text); if (!::OpenClipboard(nullptr)) { CRU_LOG_TAG_WARN("Failed to open clipboard."); diff --git a/src/platform/gui/win/InputMethod.cpp b/src/platform/gui/win/InputMethod.cpp index c5aca657..812670e1 100644 --- a/src/platform/gui/win/InputMethod.cpp +++ b/src/platform/gui/win/InputMethod.cpp @@ -123,7 +123,7 @@ CompositionText GetCompositionInfo(HIMC imm_context) { // convert them into underlines and selection range respectively. auto utf16_text = GetString(imm_context); - auto text = string::ToUtf8(utf16_text); + auto text = string::ToUtf8String(utf16_text); int length = static_cast<int>(utf16_text.length()); // Find out the range selected by the user. @@ -134,23 +134,15 @@ CompositionText GetCompositionInfo(HIMC imm_context) { auto clauses = GetCompositionClauses(imm_context, target_start, target_end); for (auto& clause : clauses) { clause.start = string::Utf8IndexCodePointToCodeUnit( - text.data(), text.size(), - string::Utf16IndexCodeUnitToCodePoint( - reinterpret_cast<const char16_t*>(utf16_text.data()), - utf16_text.size(), clause.start)); + text, string::Utf16IndexCodeUnitToCodePoint(utf16_text, clause.start)); clause.end = string::Utf8IndexCodePointToCodeUnit( - text.data(), text.size(), - string::Utf16IndexCodeUnitToCodePoint( - reinterpret_cast<const char16_t*>(utf16_text.data()), - utf16_text.size(), clause.end)); + text, string::Utf16IndexCodeUnitToCodePoint(utf16_text, clause.end)); } int cursor = string::Utf8IndexCodePointToCodeUnit( - text.data(), text.size(), - string::Utf16IndexCodeUnitToCodePoint( - reinterpret_cast<const char16_t*>(utf16_text.data()), - utf16_text.size(), - ::ImmGetCompositionString(imm_context, GCS_CURSORPOS, NULL, 0))); + text, string::Utf16IndexCodeUnitToCodePoint( + utf16_text, ::ImmGetCompositionString(imm_context, + GCS_CURSORPOS, NULL, 0))); return CompositionText{std::move(text), std::move(clauses), TextRange{cursor}}; @@ -229,7 +221,9 @@ IEvent<std::nullptr_t>* WinInputMethodContext::CompositionEvent() { return &composition_event_; } -IEvent<const std::string&>* WinInputMethodContext::TextEvent() { return &text_event_; } +IEvent<const std::string&>* WinInputMethodContext::TextEvent() { + return &text_event_; +} void WinInputMethodContext::OnWindowNativeMessage( WindowNativeMessageEventArgs& args) { @@ -286,7 +280,7 @@ void WinInputMethodContext::OnWindowNativeMessage( std::string WinInputMethodContext::GetResultString() { auto himc = GetHIMC(); auto result = win::GetResultString(himc.Get()); - return string::ToUtf8(result); + return string::ToUtf8String(result); } AutoHIMC WinInputMethodContext::GetHIMC() { diff --git a/src/platform/gui/win/Window.cpp b/src/platform/gui/win/Window.cpp index e24bb08e..02357336 100644 --- a/src/platform/gui/win/Window.cpp +++ b/src/platform/gui/win/Window.cpp @@ -96,7 +96,7 @@ void WinNativeWindow::SetTitle(std::string title) { title_ = title; if (hwnd_) { - auto utf16_text = string::ToUtf16(title); + auto utf16_text = string::ToUtf16WString(title); ::SetWindowTextW(hwnd_, utf16_text.c_str()); } } @@ -498,7 +498,7 @@ void WinNativeWindow::RecreateWindow() { SetCursor(application_->GetCursorManager()->GetSystemCursor( cru::platform::gui::SystemCursorType::Arrow)); - auto utf16_title = string::ToUtf16(title_); + auto utf16_title = string::ToUtf16WString(title_); ::SetWindowTextW(hwnd_, utf16_title.c_str()); window_render_target_ = diff --git a/src/ui/controls/TextHostControlService.cpp b/src/ui/controls/TextHostControlService.cpp index 5908852a..5780b6d7 100644 --- a/src/ui/controls/TextHostControlService.cpp +++ b/src/ui/controls/TextHostControlService.cpp @@ -28,8 +28,7 @@ TextControlMovePattern TextControlMovePattern::kLeft( [](TextHostControlService* service, std::string_view text, Index current_position) { CRU_UNUSED(service) - Utf8PreviousCodePoint(text.data(), text.size(), current_position, - ¤t_position); + Utf8PreviousCodePoint(text, current_position, ¤t_position); return current_position; }); TextControlMovePattern TextControlMovePattern::kRight( @@ -37,8 +36,7 @@ TextControlMovePattern TextControlMovePattern::kRight( [](TextHostControlService* service, std::string_view text, Index current_position) { CRU_UNUSED(service) - Utf8NextCodePoint(text.data(), text.size(), current_position, - ¤t_position); + Utf8NextCodePoint(text, current_position, ¤t_position); return current_position; }); TextControlMovePattern TextControlMovePattern::kCtrlLeft( @@ -48,7 +46,7 @@ TextControlMovePattern TextControlMovePattern::kCtrlLeft( [](TextHostControlService* service, std::string_view text, Index current_position) { CRU_UNUSED(service) - return Utf8PreviousWord(text.data(), text.size(), current_position); + return Utf8PreviousWord(text, current_position); }); TextControlMovePattern TextControlMovePattern::kCtrlRight( "Ctrl+Right(Next Word)", @@ -57,7 +55,7 @@ TextControlMovePattern TextControlMovePattern::kCtrlRight( [](TextHostControlService* service, std::string_view text, Index current_position) { CRU_UNUSED(service) - return Utf8NextWord(text.data(), text.size(), current_position); + return Utf8NextWord(text, current_position); }); TextControlMovePattern TextControlMovePattern::kUp( "Up", helper::ShortcutKeyBind(platform::gui::KeyCode::Up), @@ -86,7 +84,7 @@ TextControlMovePattern TextControlMovePattern::kHome( [](TextHostControlService* service, std::string_view text, Index current_position) { CRU_UNUSED(service) - return Utf8BackwardUntil(text.data(), text.size(), current_position, + return Utf8BackwardUntil(text, current_position, [](CodePoint c) { return c == u'\n'; }); }); TextControlMovePattern TextControlMovePattern::kEnd( @@ -94,7 +92,7 @@ TextControlMovePattern TextControlMovePattern::kEnd( [](TextHostControlService* service, std::string_view text, Index current_position) { CRU_UNUSED(service) - return Utf8ForwardUntil(text.data(), text.size(), current_position, + return Utf8ForwardUntil(text, current_position, [](CodePoint c) { return c == u'\n'; }); }); TextControlMovePattern TextControlMovePattern::kCtrlHome( @@ -223,8 +221,7 @@ void TextHostControlService::SetText(std::string text, bool stop_composition) { void TextHostControlService::InsertText(Index position, std::string_view text, bool stop_composition) { - if (!Utf8IsValidInsertPosition(this->text_.data(), this->text_.size(), - position)) { + if (!Utf8IsValidInsertPosition(this->text_, position)) { CRU_LOG_TAG_ERROR("Invalid text insert position."); return; } @@ -238,29 +235,26 @@ void TextHostControlService::InsertText(Index position, std::string_view text, } void TextHostControlService::DeleteChar(Index position, bool stop_composition) { - if (!Utf8IsValidInsertPosition(this->text_.data(), this->text_.size(), - position)) { + if (!Utf8IsValidInsertPosition(this->text_, position)) { CRU_LOG_TAG_ERROR("Invalid text delete position {}.", position); return; } if (position == static_cast<Index>(this->text_.size())) return; Index next; - Utf8NextCodePoint(this->text_.data(), this->text_.size(), position, &next); + Utf8NextCodePoint(this->text_, position, &next); this->DeleteText(TextRange::FromTwoSides(position, next), stop_composition); } // Return the position of deleted character. Index TextHostControlService::DeleteCharPrevious(Index position, bool stop_composition) { - if (!Utf8IsValidInsertPosition(this->text_.data(), this->text_.size(), - position)) { + if (!Utf8IsValidInsertPosition(this->text_, position)) { CRU_LOG_TAG_ERROR("Invalid text delete position {}.", position); return 0; } if (position == 0) return 0; Index previous; - Utf8PreviousCodePoint(this->text_.data(), this->text_.size(), position, - &previous); + Utf8PreviousCodePoint(this->text_, position, &previous); this->DeleteText(TextRange::FromTwoSides(previous, position), stop_composition); return previous; @@ -270,14 +264,12 @@ void TextHostControlService::DeleteText(TextRange range, bool stop_composition) { if (range.count == 0) return; range = range.Normalize(); - if (!Utf8IsValidInsertPosition(this->text_.data(), this->text_.size(), - range.GetStart())) { + if (!Utf8IsValidInsertPosition(this->text_, range.GetStart())) { CRU_LOG_TAG_ERROR("Invalid text delete start position {}.", range.GetStart()); return; } - if (!Utf8IsValidInsertPosition(this->text_.data(), this->text_.size(), - range.GetEnd())) { + if (!Utf8IsValidInsertPosition(this->text_, range.GetEnd())) { CRU_LOG_TAG_ERROR("Invalid text delete end position {}.", range.GetEnd()); return; } diff --git a/test/base/platform/win/StreamTest.cpp b/test/base/platform/win/StreamTest.cpp index e1a6e4fe..f42fc5c2 100644 --- a/test/base/platform/win/StreamTest.cpp +++ b/test/base/platform/win/StreamTest.cpp @@ -16,7 +16,7 @@ TEST_CASE("StreamConvert FileStreamWork", "[stream]") { .native(); _wmktemp(temp_file_path.data()); - std::string path = string::ToUtf8(temp_file_path); + std::string path = string::ToUtf8String(temp_file_path); ComStream file(path, OpenFileFlags::Write | OpenFileFlags::Create); file.Write("abc", 3); @@ -46,7 +46,7 @@ TEST_CASE("ComStream Work", "[stream]") { .native(); _wmktemp(temp_file_path.data()); - std::string path = string::ToUtf8(temp_file_path); + std::string path = string::ToUtf8String(temp_file_path); ComStream file(path, OpenFileFlags::Write | OpenFileFlags::Create); auto write_count = file.Write("abc", 3); |
