Core: Integrate CharStringT
This commit is contained in:
parent
533c616cb8
commit
f232a295e2
3 changed files with 80 additions and 172 deletions
|
@ -59,8 +59,6 @@ static _FORCE_INLINE_ char32_t lower_case(char32_t c) {
|
||||||
return (is_ascii_upper_case(c) ? (c + ('a' - 'A')) : 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::_null = 0;
|
||||||
const char32_t String::_replacement_char = 0xfffd;
|
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) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*************************************************************************/
|
template <typename T>
|
||||||
/* Char16String */
|
bool CharStringT<T>::operator<(const CharStringT<T> &p_other) const {
|
||||||
/*************************************************************************/
|
|
||||||
|
|
||||||
bool Char16String::operator<(const Char16String &p_right) const {
|
|
||||||
if (length() == 0) {
|
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 <typename T>
|
||||||
|
bool CharStringT<T>::operator==(const CharStringT<T> &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 <typename T>
|
||||||
|
CharStringT<T> &CharStringT<T>::operator+=(T p_char) {
|
||||||
const int lhs_len = length();
|
const int lhs_len = length();
|
||||||
resize(lhs_len + 2);
|
resize(lhs_len + 2);
|
||||||
|
|
||||||
char16_t *dst = ptrw();
|
T *dst = ptrw();
|
||||||
dst[lhs_len] = p_char;
|
dst[lhs_len] = p_char;
|
||||||
dst[lhs_len + 1] = 0;
|
dst[lhs_len + 1] = 0;
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Char16String::operator=(const char16_t *p_cstr) {
|
template <typename T>
|
||||||
copy_from(p_cstr);
|
void CharStringT<T>::copy_from(const T *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) {
|
|
||||||
if (!p_cstr) {
|
if (!p_cstr) {
|
||||||
resize(0);
|
resize(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char16_t *s = p_cstr;
|
const T *s = p_cstr;
|
||||||
for (; *s; s++) {
|
for (; *s; s++) {
|
||||||
}
|
}
|
||||||
size_t len = s - p_cstr;
|
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
|
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.");
|
ERR_FAIL_COND_MSG(err != OK, "Failed to copy C-string.");
|
||||||
|
|
||||||
memcpy(ptrw(), p_cstr, len);
|
memcpy(ptrw(), p_cstr, len * sizeof(T));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*************************************************************************/
|
template class CharStringT<char>;
|
||||||
/* String */
|
template class CharStringT<char16_t>;
|
||||||
/*************************************************************************/
|
template class CharStringT<char32_t>;
|
||||||
|
template class CharStringT<wchar_t>;
|
||||||
|
|
||||||
Error String::parse_url(String &r_scheme, String &r_host, int &r_port, String &r_path, String &r_fragment) const {
|
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.
|
// Splits the URL into scheme, host, port, path, fragment. Strip credentials when present.
|
||||||
|
|
|
@ -39,19 +39,19 @@
|
||||||
#include "core/typedefs.h"
|
#include "core/typedefs.h"
|
||||||
#include "core/variant/array.h"
|
#include "core/variant/array.h"
|
||||||
|
|
||||||
/*************************************************************************/
|
class String;
|
||||||
/* CharProxy */
|
template <typename T>
|
||||||
/*************************************************************************/
|
class CharStringT;
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class CharProxy {
|
class CharProxy {
|
||||||
friend class Char16String;
|
|
||||||
friend class CharString;
|
|
||||||
friend class String;
|
friend class String;
|
||||||
|
template <typename TS>
|
||||||
|
friend class CharStringT;
|
||||||
|
|
||||||
const int _index;
|
const int _index;
|
||||||
CowData<T> &_cowdata;
|
CowData<T> &_cowdata;
|
||||||
static const T _null = 0;
|
static constexpr T _null = 0;
|
||||||
|
|
||||||
_FORCE_INLINE_ CharProxy(const int &p_index, CowData<T> &p_cowdata) :
|
_FORCE_INLINE_ CharProxy(const int &p_index, CowData<T> &p_cowdata) :
|
||||||
_index(p_index),
|
_index(p_index),
|
||||||
|
@ -83,92 +83,58 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/*************************************************************************/
|
template <typename T>
|
||||||
/* Char16String */
|
class CharStringT {
|
||||||
/*************************************************************************/
|
friend class String;
|
||||||
|
|
||||||
class Char16String {
|
CowData<T> _cowdata;
|
||||||
CowData<char16_t> _cowdata;
|
static constexpr T _null = 0;
|
||||||
static const char16_t _null;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
_FORCE_INLINE_ char16_t *ptrw() { return _cowdata.ptrw(); }
|
_FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); }
|
||||||
_FORCE_INLINE_ const char16_t *ptr() const { return _cowdata.ptr(); }
|
_FORCE_INLINE_ const T *ptr() const { return _cowdata.ptr(); }
|
||||||
_FORCE_INLINE_ int size() const { return _cowdata.size(); }
|
_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_ 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_ void set(int p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); }
|
||||||
_FORCE_INLINE_ const char16_t &operator[](int p_index) const {
|
_FORCE_INLINE_ const T &operator[](int p_index) const {
|
||||||
if (unlikely(p_index == _cowdata.size())) {
|
if (unlikely(p_index == _cowdata.size())) {
|
||||||
return _null;
|
return _null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return _cowdata.get(p_index);
|
return _cowdata.get(p_index);
|
||||||
}
|
}
|
||||||
_FORCE_INLINE_ CharProxy<char16_t> operator[](int p_index) { return CharProxy<char16_t>(p_index, _cowdata); }
|
_FORCE_INLINE_ CharProxy<T> operator[](int p_index) { return CharProxy<T>(p_index, _cowdata); }
|
||||||
|
|
||||||
_FORCE_INLINE_ Char16String() {}
|
_FORCE_INLINE_ CharStringT() {}
|
||||||
_FORCE_INLINE_ Char16String(const Char16String &p_str) { _cowdata._ref(p_str._cowdata); }
|
_FORCE_INLINE_ CharStringT(const CharStringT<T> &p_str) { _cowdata._ref(p_str._cowdata); }
|
||||||
_FORCE_INLINE_ void operator=(const Char16String &p_str) { _cowdata._ref(p_str._cowdata); }
|
_FORCE_INLINE_ void operator=(const CharStringT<T> &p_str) { _cowdata._ref(p_str._cowdata); }
|
||||||
_FORCE_INLINE_ Char16String(const char16_t *p_cstr) { copy_from(p_cstr); }
|
_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 CharStringT<T> &p_other) const;
|
||||||
bool operator<(const Char16String &p_right) const;
|
_FORCE_INLINE_ bool operator!=(const CharStringT<T> &p_other) const { return !(*this == p_other); }
|
||||||
Char16String &operator+=(char16_t p_char);
|
bool operator<(const CharStringT<T> &p_other) const;
|
||||||
int length() const { return size() ? size() - 1 : 0; }
|
CharStringT<T> &operator+=(T p_char);
|
||||||
const char16_t *get_data() const;
|
|
||||||
operator const char16_t *() const { return get_data(); };
|
|
||||||
|
|
||||||
protected:
|
_FORCE_INLINE_ int length() const { return size() ? size() - 1 : 0; }
|
||||||
void copy_from(const char16_t *p_cstr);
|
_FORCE_INLINE_ const T *get_data() const {
|
||||||
};
|
if (size()) {
|
||||||
|
return &operator[](0);
|
||||||
/*************************************************************************/
|
|
||||||
/* CharString */
|
|
||||||
/*************************************************************************/
|
|
||||||
|
|
||||||
class CharString {
|
|
||||||
CowData<char> _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;
|
|
||||||
}
|
}
|
||||||
|
return &_null;
|
||||||
return _cowdata.get(p_index);
|
|
||||||
}
|
}
|
||||||
_FORCE_INLINE_ CharProxy<char> operator[](int p_index) { return CharProxy<char>(p_index, _cowdata); }
|
_FORCE_INLINE_ operator const T *() const { return get_data(); };
|
||||||
|
|
||||||
_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(); };
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void copy_from(const char *p_cstr);
|
void copy_from(const T *p_cstr);
|
||||||
};
|
};
|
||||||
|
|
||||||
/*************************************************************************/
|
using CharString = CharStringT<char>;
|
||||||
/* String */
|
using Char16String = CharStringT<char16_t>;
|
||||||
/*************************************************************************/
|
using Char32String = CharStringT<char32_t>;
|
||||||
|
using CharWideString = CharStringT<wchar_t>;
|
||||||
|
|
||||||
struct StrRange {
|
struct StrRange {
|
||||||
const char32_t *c_str;
|
const char32_t *c_str;
|
||||||
|
|
|
@ -41,8 +41,8 @@
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class Vector;
|
class Vector;
|
||||||
class String;
|
class String;
|
||||||
class Char16String;
|
template <typename T>
|
||||||
class CharString;
|
class CharStringT;
|
||||||
template <typename T, typename V>
|
template <typename T, typename V>
|
||||||
class VMap;
|
class VMap;
|
||||||
|
|
||||||
|
@ -59,8 +59,8 @@ class CowData {
|
||||||
template <typename TV>
|
template <typename TV>
|
||||||
friend class Vector;
|
friend class Vector;
|
||||||
friend class String;
|
friend class String;
|
||||||
friend class Char16String;
|
template <typename TS>
|
||||||
friend class CharString;
|
friend class CharStringT;
|
||||||
template <typename TV, typename VV>
|
template <typename TV, typename VV>
|
||||||
friend class VMap;
|
friend class VMap;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue