diff options
Diffstat (limited to 'include/cru/common')
-rw-r--r-- | include/cru/common/String.hpp | 144 |
1 files changed, 72 insertions, 72 deletions
diff --git a/include/cru/common/String.hpp b/include/cru/common/String.hpp index 0530c208..13176fc6 100644 --- a/include/cru/common/String.hpp +++ b/include/cru/common/String.hpp @@ -71,7 +71,7 @@ class CRU_BASE_API String { template <Index size> String(const char16_t (&str)[size]) - : String(reinterpret_cast<const_pointer>(str), size) {} + : String(reinterpret_cast<const_pointer>(str), size - 1) {} template <typename Iter> String(Iter start, Iter end) { @@ -164,7 +164,7 @@ class CRU_BASE_API String { inline void append(StringView str); public: - String& operator+=(const String& other); + String& operator+=(StringView other); public: Utf16CodePointIterator CodePointIterator() const { @@ -199,6 +199,76 @@ class CRU_BASE_API String { Index capacity_ = 0; // always 1 smaller than real buffer size }; +class CRU_BASE_API StringView { + public: + using value_type = char16_t; + using size_type = Index; + using difference_type = Index; + using reference = value_type&; + using const_reference = const value_type&; + using pointer = value_type*; + using const_pointer = const value_type*; + using iterator = const value_type*; + using const_iterator = const value_type*; + using reverse_iterator = std::reverse_iterator<iterator>; + using const_reverse_iterator = std::reverse_iterator<const_iterator>; + + StringView() = default; + + constexpr StringView(const_pointer ptr, Index size) + : ptr_(ptr), size_(size) {} + + template <Index size> + constexpr StringView(const value_type (&array)[size]) + : StringView(array, size - 1) {} + + StringView(const String& str) : StringView(str.data(), str.size()) {} + + CRU_DEFAULT_COPY(StringView) + CRU_DEFAULT_MOVE(StringView) + + ~StringView() = default; + + Index size() const { return size_; } + const value_type* data() const { return ptr_; } + + public: + iterator begin() { return this->ptr_; } + const_iterator begin() const { return this->ptr_; } + const_iterator cbegin() const { return this->ptr_; } + + iterator end() { return this->ptr_ + this->size_; } + const_iterator end() const { return this->ptr_ + this->size_; } + const_iterator cend() const { return this->ptr_ + this->size_; } + + reverse_iterator rbegin() { return reverse_iterator{begin()}; } + const_reverse_iterator rbegin() const { + return const_reverse_iterator{begin()}; + } + const_reverse_iterator crbegin() const { + return const_reverse_iterator{cbegin()}; + } + + reverse_iterator rend() { return reverse_iterator{end()}; } + const_reverse_iterator rend() const { return const_reverse_iterator{end()}; } + const_reverse_iterator crend() const { + return const_reverse_iterator{cend()}; + } + + StringView substr(Index pos); + StringView substr(Index pos, Index size); + + int Compare(const StringView& other) const; + + String ToString() const { return String(ptr_, size_); } + + value_type operator[](Index index) const { return ptr_[index]; } + + private: + const char16_t* ptr_; + Index size_; +}; + CRU_DEFINE_COMPARE_OPERATORS(String) inline String operator+(const String& left, const String& right) { @@ -285,76 +355,6 @@ String String::Format(T&&... args) const { return cru::Format(*this, std::forward<T>(args)...); } -class CRU_BASE_API StringView { - public: - using value_type = char16_t; - using size_type = Index; - using difference_type = Index; - using reference = value_type&; - using const_reference = const value_type&; - using pointer = value_type*; - using const_pointer = const value_type*; - using iterator = const value_type*; - using const_iterator = const value_type*; - using reverse_iterator = std::reverse_iterator<iterator>; - using const_reverse_iterator = std::reverse_iterator<const_iterator>; - - StringView() = default; - - constexpr StringView(const_pointer ptr, Index size) - : ptr_(ptr), size_(size) {} - - template <Index size> - constexpr StringView(const value_type (&array)[size]) - : StringView(array, size) {} - - StringView(const String& str) : StringView(str.data(), str.size()) {} - - CRU_DEFAULT_COPY(StringView) - CRU_DEFAULT_MOVE(StringView) - - ~StringView() = default; - - Index size() const { return size_; } - const value_type* data() const { return ptr_; } - - public: - iterator begin() { return this->ptr_; } - const_iterator begin() const { return this->ptr_; } - const_iterator cbegin() const { return this->ptr_; } - - iterator end() { return this->ptr_ + this->size_; } - const_iterator end() const { return this->ptr_ + this->size_; } - const_iterator cend() const { return this->ptr_ + this->size_; } - - reverse_iterator rbegin() { return reverse_iterator{begin()}; } - const_reverse_iterator rbegin() const { - return const_reverse_iterator{begin()}; - } - const_reverse_iterator crbegin() const { - return const_reverse_iterator{cbegin()}; - } - - reverse_iterator rend() { return reverse_iterator{end()}; } - const_reverse_iterator rend() const { return const_reverse_iterator{end()}; } - const_reverse_iterator crend() const { - return const_reverse_iterator{cend()}; - } - - StringView substr(Index pos); - StringView substr(Index pos, Index size); - - int Compare(const StringView& other) const; - - String ToString() const { return String(ptr_, size_); } - - value_type operator[](Index index) const { return ptr_[index]; } - - private: - const char16_t* ptr_; - Index size_; -}; - CRU_DEFINE_COMPARE_OPERATORS(StringView) inline String::iterator String::insert(const_iterator pos, StringView str) { |