From 041f824448084b6e63d26bc3ef2bd7255646cad3 Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 1 Nov 2021 19:10:12 +0100 Subject: [PATCH] Erase, substr for String, and fixed comparisons. --- core/string.cpp | 112 +++++++++++++++++++++++++++--------------------- core/string.h | 4 ++ 2 files changed, 66 insertions(+), 50 deletions(-) diff --git a/core/string.cpp b/core/string.cpp index 11e975b..83669d7 100644 --- a/core/string.cpp +++ b/core/string.cpp @@ -38,6 +38,26 @@ void String::erase(const char element) { } } +void String::erase(const int start_index, const int length) { + int sil = start_index + length; + + if (sil >= _size) { + _size = 0; + _data[_size] = '\0'; + return; + } + + int j = start_index; + for (int i = sil; i < _size; ++i) { + _data[j] = _data[i]; + ++j; + } + + _size -= length; + + _data[_size] = '\0'; +} + void String::clear() { _size = 0; } @@ -125,6 +145,42 @@ void String::get_substr_nt(char *into_buf, const int start_index, const int len) into_buf[len + 1] = '\0'; } +String String::substr(const int start_index, const int len) { + ERR_FAIL_INDEX_V(start_index, _size, String()); + + int sil = start_index + len; + + ERR_FAIL_INDEX_V(sil, _size, String()); + + String str; + str.ensure_capacity(len + 1); + for (int i = start_index; i < sil; ++i) { + str._data[str._size++] = _data[i]; + } + + str._data[str._size] = '\0'; + + return str; +} + +int String::compare(const String &other) const { + if (size() < other.size()) { + return 1; + } else if (size() > other.size()) { + return 2; + } else { + for (int i = 0; i < _size; ++i) { + if (_data[i] < other._data[i]) { + return 1; + } else if (_data[i] > other._data[i]) { + return 2; + } + } + + return 0; + } +} + uint8_t String::read_uint8_bytes_at(int &index, bool advance_index) { ERR_FAIL_INDEX_V(index, _size, 0); @@ -609,64 +665,20 @@ bool operator!=(std::string &b, const String &a) { } bool operator<(const String &a, const String &b) { - if (a.size() < b.size()) { - return true; - } else if (a.size() > b.size()) { - return false; - } else { - for (int i = 0; i < a._size; ++i) { - if (a[i] < b[i]) { - return true; - } - } - - return false; - } + return a.compare(b) == 1; } bool operator>(const String &a, const String &b) { - if (a.size() > b.size()) { - return true; - } else if (a.size() < b.size()) { - return false; - } else { - for (int i = 0; i < a._size; ++i) { - if (a[i] > b[i]) { - return true; - } - } - - return false; - } + return a.compare(b) == 2; } bool operator<=(const String &a, const String &b) { - if (a.size() < b.size()) { - return true; - } else if (a.size() > b.size()) { - return false; - } else { - for (int i = 0; i < a._size; ++i) { - if (a[i] <= b[i]) { - return true; - } - } + int c = a.compare(b); - return false; - } + return c == 0 || c == 1; } bool operator>=(const String &a, const String &b) { - if (a.size() > b.size()) { - return true; - } else if (a.size() < b.size()) { - return false; - } else { - for (int i = 0; i < a._size; ++i) { - if (a[i] >= b[i]) { - return true; - } - } + int c = a.compare(b); - return false; - } + return c == 0 || c == 2; } String &String::operator=(const String &other) { diff --git a/core/string.h b/core/string.h index c5f229d..cda9cc1 100644 --- a/core/string.h +++ b/core/string.h @@ -11,6 +11,7 @@ public: void pop_back(); void remove(const int index); void erase(const char element); + void erase(const int start_index, const int length); void clear(); bool empty() const; char get(const int index); @@ -24,6 +25,9 @@ public: int find(const char val) const; void get_substr(char *into_buf, const int start_index, const int len); void get_substr_nt(char *into_buf, const int start_index, const int len); + String substr(const int start_index, const int len); + + int compare(const String &other) const; uint8_t read_uint8_bytes_at(int &index, bool advance_index = true); uint16_t read_uint16_bytes_at(int &index, bool advance_index = true);