diff --git a/src/openrct2/core/CodepointView.hpp b/src/openrct2/core/CodepointView.hpp new file mode 100644 index 0000000000..dc2ec04d26 --- /dev/null +++ b/src/openrct2/core/CodepointView.hpp @@ -0,0 +1,95 @@ +/***************************************************************************** + * Copyright (c) 2014-2024 OpenRCT2 developers + * + * For a complete list of all authors, please refer to contributors.md + * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is licensed under the GNU General Public License version 3. + *****************************************************************************/ + +#pragma once + +#include "UTF8.h" + +namespace OpenRCT2 +{ + class CodepointView + { + private: + std::string_view _str; + + public: + class iterator + { + private: + std::string_view _str; + size_t _index; + + public: + iterator(std::string_view str, size_t index) + : _str(str) + , _index(index) + { + } + + bool operator==(const iterator& rhs) const + { + return _index == rhs._index; + } + bool operator!=(const iterator& rhs) const + { + return _index != rhs._index; + } + char32_t operator*() const + { + return GetNextCodepoint(&_str[_index], nullptr); + } + iterator& operator++() + { + if (_index < _str.size()) + { + const utf8* nextch; + GetNextCodepoint(&_str[_index], &nextch); + _index = std::min(nextch - _str.data(), _str.size()); + } + return *this; + } + iterator operator++(int) + { + auto result = *this; + if (_index < _str.size()) + { + const utf8* nextch; + GetNextCodepoint(&_str[_index], &nextch); + _index = std::min(nextch - _str.data(), _str.size()); + } + return result; + } + + size_t GetIndex() const + { + return _index; + } + + static char32_t GetNextCodepoint(const char* ch, const char** next) + { + return UTF8GetNext(ch, next); + } + }; + + CodepointView(std::string_view str) + : _str(String::UTF8Truncate(str, str.size())) + { + } + + iterator begin() const + { + return iterator(_str, 0); + } + + iterator end() const + { + return iterator(_str, _str.size()); + } + }; +} // namespace OpenRCT2 diff --git a/src/openrct2/core/String.cpp b/src/openrct2/core/String.cpp index 06a68935af..630ca6b165 100644 --- a/src/openrct2/core/String.cpp +++ b/src/openrct2/core/String.cpp @@ -726,8 +726,3 @@ namespace OpenRCT2::String return escaped.str(); } } // namespace OpenRCT2::String - -char32_t CodepointView::iterator::GetNextCodepoint(const char* ch, const char** next) -{ - return UTF8GetNext(ch, next); -} diff --git a/src/openrct2/core/String.hpp b/src/openrct2/core/String.hpp index d8a1322bc0..9bb260121c 100644 --- a/src/openrct2/core/String.hpp +++ b/src/openrct2/core/String.hpp @@ -205,80 +205,3 @@ namespace OpenRCT2::String // Escapes special characters in a string to the percentage equivalent that can be used in URLs. std::string URLEncode(std::string_view value); } // namespace OpenRCT2::String - -class CodepointView -{ -private: - std::string_view _str; - -public: - class iterator - { - private: - std::string_view _str; - size_t _index; - - public: - iterator(std::string_view str, size_t index) - : _str(str) - , _index(index) - { - } - - bool operator==(const iterator& rhs) const - { - return _index == rhs._index; - } - bool operator!=(const iterator& rhs) const - { - return _index != rhs._index; - } - char32_t operator*() const - { - return GetNextCodepoint(&_str[_index], nullptr); - } - iterator& operator++() - { - if (_index < _str.size()) - { - const utf8* nextch; - GetNextCodepoint(&_str[_index], &nextch); - _index = std::min(nextch - _str.data(), _str.size()); - } - return *this; - } - iterator operator++(int) - { - auto result = *this; - if (_index < _str.size()) - { - const utf8* nextch; - GetNextCodepoint(&_str[_index], &nextch); - _index = std::min(nextch - _str.data(), _str.size()); - } - return result; - } - - size_t GetIndex() const - { - return _index; - } - - static char32_t GetNextCodepoint(const char* ch, const char** next); - }; - - CodepointView(std::string_view str) - : _str(OpenRCT2::String::UTF8Truncate(str, str.size())) - { - } - - iterator begin() const - { - return iterator(_str, 0); - } - - iterator end() const - { - return iterator(_str, _str.size()); - } -}; diff --git a/src/openrct2/drawing/Drawing.String.cpp b/src/openrct2/drawing/Drawing.String.cpp index cc6b38e8ed..4ad0215b06 100644 --- a/src/openrct2/drawing/Drawing.String.cpp +++ b/src/openrct2/drawing/Drawing.String.cpp @@ -9,6 +9,7 @@ #include "../Context.h" #include "../config/Config.h" +#include "../core/CodepointView.hpp" #include "../core/String.hpp" #include "../core/UTF8.h" #include "../core/UnicodeChar.h" diff --git a/src/openrct2/drawing/ScrollingText.cpp b/src/openrct2/drawing/ScrollingText.cpp index bde223f19a..b1552f3f69 100644 --- a/src/openrct2/drawing/ScrollingText.cpp +++ b/src/openrct2/drawing/ScrollingText.cpp @@ -10,6 +10,7 @@ #include "ScrollingText.h" #include "../config/Config.h" +#include "../core/CodepointView.hpp" #include "../core/String.hpp" #include "../interface/Colour.h" #include "../localisation/Formatter.h" diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj index 01f1989dea..2be5b8a854 100644 --- a/src/openrct2/libopenrct2.vcxproj +++ b/src/openrct2/libopenrct2.vcxproj @@ -184,6 +184,7 @@ + diff --git a/src/openrct2/paint/tile_element/Paint.LargeScenery.cpp b/src/openrct2/paint/tile_element/Paint.LargeScenery.cpp index c0370ea6b0..1626c1fc64 100644 --- a/src/openrct2/paint/tile_element/Paint.LargeScenery.cpp +++ b/src/openrct2/paint/tile_element/Paint.LargeScenery.cpp @@ -12,8 +12,8 @@ #include "../../Game.h" #include "../../GameState.h" #include "../../config/Config.h" +#include "../../core/CodepointView.hpp" #include "../../core/Numerics.hpp" -#include "../../core/String.hpp" #include "../../core/UTF8.h" #include "../../interface/Viewport.h" #include "../../localisation/Formatting.h" diff --git a/src/openrct2/rct12/RCT12.cpp b/src/openrct2/rct12/RCT12.cpp index f72aaa8f96..d4552f839e 100644 --- a/src/openrct2/rct12/RCT12.cpp +++ b/src/openrct2/rct12/RCT12.cpp @@ -9,6 +9,7 @@ #include "RCT12.h" +#include "../core/CodepointView.hpp" #include "../core/String.hpp" #include "../localisation/Formatting.h" #include "../object/ObjectList.h" diff --git a/src/openrct2/world/Scenery.cpp b/src/openrct2/world/Scenery.cpp index 6d97773c45..5d7ba79544 100644 --- a/src/openrct2/world/Scenery.cpp +++ b/src/openrct2/world/Scenery.cpp @@ -19,7 +19,7 @@ #include "../actions/LargeSceneryRemoveAction.h" #include "../actions/SmallSceneryRemoveAction.h" #include "../actions/WallRemoveAction.h" -#include "../core/String.hpp" +#include "../core/CodepointView.hpp" #include "../entity/Fountain.h" #include "../network/network.h" #include "../object/BannerSceneryEntry.h" diff --git a/test/tests/StringTest.cpp b/test/tests/StringTest.cpp index 49e9bb74e6..733a86366b 100644 --- a/test/tests/StringTest.cpp +++ b/test/tests/StringTest.cpp @@ -11,6 +11,7 @@ #include "helpers/StringHelpers.hpp" #include +#include #include #include #include