From f232a295e22eb94b4512addd2c8c48d7d2878f9b Mon Sep 17 00:00:00 2001 From: Thaddeus Crews Date: Tue, 22 Oct 2024 17:08:05 -0500 Subject: [PATCH] Core: Integrate `CharStringT` --- core/string/ustring.cpp | 134 +++++++++++---------------------------- core/string/ustring.h | 110 +++++++++++--------------------- core/templates/cowdata.h | 8 +-- 3 files changed, 80 insertions(+), 172 deletions(-) diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index e6f7492a189..12fedd42b59 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -59,8 +59,6 @@ static _FORCE_INLINE_ char32_t lower_case(char32_t c) { return (is_ascii_upper_case(c) ? (c + ('a' - 'A')) : c); } -const char CharString::_null = 0; -const char16_t Char16String::_null = 0; const char32_t String::_null = 0; const char32_t String::_replacement_char = 0xfffd; @@ -92,48 +90,58 @@ bool select_word(const String &p_s, int p_col, int &r_beg, int &r_end) { } } -/*************************************************************************/ -/* Char16String */ -/*************************************************************************/ - -bool Char16String::operator<(const Char16String &p_right) const { +template +bool CharStringT::operator<(const CharStringT &p_other) const { if (length() == 0) { - return p_right.length() != 0; + return p_other.length() != 0; } - return is_str_less(get_data(), p_right.get_data()); + return is_str_less(get_data(), p_other.get_data()); } -Char16String &Char16String::operator+=(char16_t p_char) { +template +bool CharStringT::operator==(const CharStringT &p_other) const { + if (length() != p_other.length()) { + return false; + } + if (length() == 0) { + return true; + } + + size_t len = length(); + const T *src = get_data(); + const T *dst = p_other.get_data(); + + /* Compare char by char */ + for (size_t i = 0; i < len; i++) { + if (src[i] != dst[i]) { + return false; + } + } + + return true; +} + +template +CharStringT &CharStringT::operator+=(T p_char) { const int lhs_len = length(); resize(lhs_len + 2); - char16_t *dst = ptrw(); + T *dst = ptrw(); dst[lhs_len] = p_char; dst[lhs_len + 1] = 0; return *this; } -void Char16String::operator=(const char16_t *p_cstr) { - copy_from(p_cstr); -} - -const char16_t *Char16String::get_data() const { - if (size()) { - return &operator[](0); - } else { - return u""; - } -} - -void Char16String::copy_from(const char16_t *p_cstr) { +template +void CharStringT::copy_from(const T *p_cstr) { if (!p_cstr) { resize(0); return; } - const char16_t *s = p_cstr; + const T *s = p_cstr; for (; *s; s++) { } size_t len = s - p_cstr; @@ -145,81 +153,15 @@ void Char16String::copy_from(const char16_t *p_cstr) { Error err = resize(++len); // include terminating null char - ERR_FAIL_COND_MSG(err != OK, "Failed to copy char16_t string."); - - memcpy(ptrw(), p_cstr, len * sizeof(char16_t)); -} - -/*************************************************************************/ -/* CharString */ -/*************************************************************************/ - -bool CharString::operator<(const CharString &p_right) const { - if (length() == 0) { - return p_right.length() != 0; - } - - return is_str_less(get_data(), p_right.get_data()); -} - -bool CharString::operator==(const CharString &p_right) const { - if (length() == 0) { - // True if both have length 0, false if only p_right has a length - return p_right.length() == 0; - } else if (p_right.length() == 0) { - // False due to unequal length - return false; - } - - return strcmp(ptr(), p_right.ptr()) == 0; -} - -CharString &CharString::operator+=(char p_char) { - const int lhs_len = length(); - resize(lhs_len + 2); - - char *dst = ptrw(); - dst[lhs_len] = p_char; - dst[lhs_len + 1] = 0; - - return *this; -} - -void CharString::operator=(const char *p_cstr) { - copy_from(p_cstr); -} - -const char *CharString::get_data() const { - if (size()) { - return &operator[](0); - } else { - return ""; - } -} - -void CharString::copy_from(const char *p_cstr) { - if (!p_cstr) { - resize(0); - return; - } - - size_t len = strlen(p_cstr); - - if (len == 0) { - resize(0); - return; - } - - Error err = resize(++len); // include terminating null char - ERR_FAIL_COND_MSG(err != OK, "Failed to copy C-string."); - memcpy(ptrw(), p_cstr, len); + memcpy(ptrw(), p_cstr, len * sizeof(T)); } -/*************************************************************************/ -/* String */ -/*************************************************************************/ +template class CharStringT; +template class CharStringT; +template class CharStringT; +template class CharStringT; Error String::parse_url(String &r_scheme, String &r_host, int &r_port, String &r_path, String &r_fragment) const { // Splits the URL into scheme, host, port, path, fragment. Strip credentials when present. diff --git a/core/string/ustring.h b/core/string/ustring.h index aa62c9cb188..0acc3e57dc7 100644 --- a/core/string/ustring.h +++ b/core/string/ustring.h @@ -39,19 +39,19 @@ #include "core/typedefs.h" #include "core/variant/array.h" -/*************************************************************************/ -/* CharProxy */ -/*************************************************************************/ +class String; +template +class CharStringT; template class CharProxy { - friend class Char16String; - friend class CharString; friend class String; + template + friend class CharStringT; const int _index; CowData &_cowdata; - static const T _null = 0; + static constexpr T _null = 0; _FORCE_INLINE_ CharProxy(const int &p_index, CowData &p_cowdata) : _index(p_index), @@ -83,92 +83,58 @@ public: } }; -/*************************************************************************/ -/* Char16String */ -/*************************************************************************/ +template +class CharStringT { + friend class String; -class Char16String { - CowData _cowdata; - static const char16_t _null; + CowData _cowdata; + static constexpr T _null = 0; public: - _FORCE_INLINE_ char16_t *ptrw() { return _cowdata.ptrw(); } - _FORCE_INLINE_ const char16_t *ptr() const { return _cowdata.ptr(); } + _FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); } + _FORCE_INLINE_ const T *ptr() const { return _cowdata.ptr(); } _FORCE_INLINE_ int size() const { return _cowdata.size(); } - Error resize(int p_size) { return _cowdata.resize(p_size); } + _FORCE_INLINE_ Error resize(int p_size) { return _cowdata.resize(p_size); } - _FORCE_INLINE_ char16_t get(int p_index) const { return _cowdata.get(p_index); } - _FORCE_INLINE_ void set(int p_index, const char16_t &p_elem) { _cowdata.set(p_index, p_elem); } - _FORCE_INLINE_ const char16_t &operator[](int p_index) const { + _FORCE_INLINE_ T get(int p_index) const { return _cowdata.get(p_index); } + _FORCE_INLINE_ void set(int p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); } + _FORCE_INLINE_ const T &operator[](int p_index) const { if (unlikely(p_index == _cowdata.size())) { return _null; } return _cowdata.get(p_index); } - _FORCE_INLINE_ CharProxy operator[](int p_index) { return CharProxy(p_index, _cowdata); } + _FORCE_INLINE_ CharProxy operator[](int p_index) { return CharProxy(p_index, _cowdata); } - _FORCE_INLINE_ Char16String() {} - _FORCE_INLINE_ Char16String(const Char16String &p_str) { _cowdata._ref(p_str._cowdata); } - _FORCE_INLINE_ void operator=(const Char16String &p_str) { _cowdata._ref(p_str._cowdata); } - _FORCE_INLINE_ Char16String(const char16_t *p_cstr) { copy_from(p_cstr); } + _FORCE_INLINE_ CharStringT() {} + _FORCE_INLINE_ CharStringT(const CharStringT &p_str) { _cowdata._ref(p_str._cowdata); } + _FORCE_INLINE_ void operator=(const CharStringT &p_str) { _cowdata._ref(p_str._cowdata); } + _FORCE_INLINE_ CharStringT(const T *p_cstr) { copy_from(p_cstr); } + _FORCE_INLINE_ void operator=(const T *p_cstr) { copy_from(p_cstr); } - void operator=(const char16_t *p_cstr); - bool operator<(const Char16String &p_right) const; - Char16String &operator+=(char16_t p_char); - int length() const { return size() ? size() - 1 : 0; } - const char16_t *get_data() const; - operator const char16_t *() const { return get_data(); }; + bool operator==(const CharStringT &p_other) const; + _FORCE_INLINE_ bool operator!=(const CharStringT &p_other) const { return !(*this == p_other); } + bool operator<(const CharStringT &p_other) const; + CharStringT &operator+=(T p_char); -protected: - void copy_from(const char16_t *p_cstr); -}; - -/*************************************************************************/ -/* CharString */ -/*************************************************************************/ - -class CharString { - CowData _cowdata; - static const char _null; - -public: - _FORCE_INLINE_ char *ptrw() { return _cowdata.ptrw(); } - _FORCE_INLINE_ const char *ptr() const { return _cowdata.ptr(); } - _FORCE_INLINE_ int size() const { return _cowdata.size(); } - Error resize(int p_size) { return _cowdata.resize(p_size); } - - _FORCE_INLINE_ char get(int p_index) const { return _cowdata.get(p_index); } - _FORCE_INLINE_ void set(int p_index, const char &p_elem) { _cowdata.set(p_index, p_elem); } - _FORCE_INLINE_ const char &operator[](int p_index) const { - if (unlikely(p_index == _cowdata.size())) { - return _null; + _FORCE_INLINE_ int length() const { return size() ? size() - 1 : 0; } + _FORCE_INLINE_ const T *get_data() const { + if (size()) { + return &operator[](0); } - - return _cowdata.get(p_index); + return &_null; } - _FORCE_INLINE_ CharProxy operator[](int p_index) { return CharProxy(p_index, _cowdata); } - - _FORCE_INLINE_ CharString() {} - _FORCE_INLINE_ CharString(const CharString &p_str) { _cowdata._ref(p_str._cowdata); } - _FORCE_INLINE_ void operator=(const CharString &p_str) { _cowdata._ref(p_str._cowdata); } - _FORCE_INLINE_ CharString(const char *p_cstr) { copy_from(p_cstr); } - - void operator=(const char *p_cstr); - bool operator<(const CharString &p_right) const; - bool operator==(const CharString &p_right) const; - CharString &operator+=(char p_char); - int length() const { return size() ? size() - 1 : 0; } - const char *get_data() const; - operator const char *() const { return get_data(); }; + _FORCE_INLINE_ operator const T *() const { return get_data(); }; protected: - void copy_from(const char *p_cstr); + void copy_from(const T *p_cstr); }; -/*************************************************************************/ -/* String */ -/*************************************************************************/ +using CharString = CharStringT; +using Char16String = CharStringT; +using Char32String = CharStringT; +using CharWideString = CharStringT; struct StrRange { const char32_t *c_str; diff --git a/core/templates/cowdata.h b/core/templates/cowdata.h index fedcfaec3b9..fa484befc33 100644 --- a/core/templates/cowdata.h +++ b/core/templates/cowdata.h @@ -41,8 +41,8 @@ template class Vector; class String; -class Char16String; -class CharString; +template +class CharStringT; template class VMap; @@ -59,8 +59,8 @@ class CowData { template friend class Vector; friend class String; - friend class Char16String; - friend class CharString; + template + friend class CharStringT; template friend class VMap;