|
@@ -3,6 +3,8 @@
|
|
|
|
|
|
#include "core/math/Math.hpp"
|
|
#include "core/math/Math.hpp"
|
|
#include "core/utils/Check.hpp"
|
|
#include "core/utils/Check.hpp"
|
|
|
|
+#include "core/utils/Error.hpp"
|
|
|
|
+#include "core/utils/Meta.hpp"
|
|
#include "core/utils/Types.hpp"
|
|
#include "core/utils/Types.hpp"
|
|
#include "core/utils/Utility.hpp"
|
|
#include "core/utils/Utility.hpp"
|
|
|
|
|
|
@@ -19,18 +21,27 @@ namespace Core {
|
|
template<int N>
|
|
template<int N>
|
|
class Char32String;
|
|
class Char32String;
|
|
|
|
|
|
- template<int N>
|
|
+ class CharString {
|
|
- class CharString final {
|
|
+ protected:
|
|
- int length;
|
|
+ i32 length;
|
|
|
|
+ i32 capacity;
|
|
u32 hash;
|
|
u32 hash;
|
|
- static_assert(N > 0, "size of array string must be positive");
|
|
+ char* data;
|
|
- char data[static_cast<unsigned int>(N)];
|
|
|
|
|
|
|
|
public:
|
|
public:
|
|
- CharString() : length(0), hash(0) {
|
|
+ CharString(char* buffer, i32 bufferSize)
|
|
|
|
+ : length(0), capacity(bufferSize), hash(0), data(buffer) {
|
|
data[0] = '\0';
|
|
data[0] = '\0';
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ CharString(const CharString&) = delete;
|
|
|
|
+ CharString& operator=(const CharString&) = delete;
|
|
|
|
+
|
|
|
|
+ check_return Error copyFrom(const CharString& s) {
|
|
|
|
+ clear();
|
|
|
|
+ return s.toString(*this);
|
|
|
|
+ }
|
|
|
|
+
|
|
bool operator==(const char* s) const {
|
|
bool operator==(const char* s) const {
|
|
const char* p = data;
|
|
const char* p = data;
|
|
while(*s == *p && *s != '\0') {
|
|
while(*s == *p && *s != '\0') {
|
|
@@ -40,8 +51,7 @@ namespace Core {
|
|
return *s == *p;
|
|
return *s == *p;
|
|
}
|
|
}
|
|
|
|
|
|
- template<int L>
|
|
+ bool operator==(const CharString& other) const {
|
|
- bool operator==(const CharString<L>& other) const {
|
|
|
|
if(length != other.getLength()) {
|
|
if(length != other.getLength()) {
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
@@ -57,8 +67,7 @@ namespace Core {
|
|
return !((*this) == s);
|
|
return !((*this) == s);
|
|
}
|
|
}
|
|
|
|
|
|
- template<int L>
|
|
+ bool operator!=(const CharString& other) const {
|
|
- bool operator!=(const CharString<L>& other) const {
|
|
|
|
return !((*this) == other);
|
|
return !((*this) == other);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -71,7 +80,7 @@ namespace Core {
|
|
}
|
|
}
|
|
|
|
|
|
constexpr int getCapacity() const {
|
|
constexpr int getCapacity() const {
|
|
- return N - 1;
|
|
+ return capacity - 1;
|
|
}
|
|
}
|
|
|
|
|
|
check_return Error append(char c) {
|
|
check_return Error append(char c) {
|
|
@@ -177,8 +186,7 @@ namespace Core {
|
|
return t.toString(*this);
|
|
return t.toString(*this);
|
|
}
|
|
}
|
|
|
|
|
|
- template<int L>
|
|
+ check_return Error toString(CharString& s) const {
|
|
- check_return Error toString(CharString<L>& s) const {
|
|
|
|
int l = length; // length changes if &s == this
|
|
int l = length; // length changes if &s == this
|
|
for(int i = 0; i < l; i++) {
|
|
for(int i = 0; i < l; i++) {
|
|
CORE_RETURN_ERROR(s.append(data[i]));
|
|
CORE_RETURN_ERROR(s.append(data[i]));
|
|
@@ -188,7 +196,7 @@ namespace Core {
|
|
|
|
|
|
template<int L>
|
|
template<int L>
|
|
check_return Error toString(Char32String<L>& s) const {
|
|
check_return Error toString(Char32String<L>& s) const {
|
|
- return s.append(data);
|
|
+ return s.append(static_cast<const char*>(data));
|
|
}
|
|
}
|
|
|
|
|
|
void clear() {
|
|
void clear() {
|
|
@@ -215,17 +223,17 @@ namespace Core {
|
|
}
|
|
}
|
|
|
|
|
|
template<typename... Args>
|
|
template<typename... Args>
|
|
- check_return Error format(Args&&... args) {
|
|
+ check_return Error format(CharString& s, Args&&... args) {
|
|
- CharString s;
|
|
|
|
Error e = formatBuffer(s, 0, Core::forward<Args>(args)...);
|
|
Error e = formatBuffer(s, 0, Core::forward<Args>(args)...);
|
|
- if(e == Error::NONE || e == Error::CAPACITY_REACHED) {
|
|
+ if(e == Error::NONE) {
|
|
- *this = s;
|
|
+ return copyFrom(s);
|
|
|
|
+ } else if(e == Error::CAPACITY_REACHED) {
|
|
|
|
+ (void)copyFrom(s);
|
|
}
|
|
}
|
|
return e;
|
|
return e;
|
|
}
|
|
}
|
|
|
|
|
|
- template<int L>
|
|
+ bool startsWidth(const CharString& other, int from = 0) const {
|
|
- bool startsWidth(const CharString<L>& other, int from = 0) const {
|
|
|
|
if(from > length - other.getLength()) {
|
|
if(from > length - other.getLength()) {
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
@@ -237,8 +245,7 @@ namespace Core {
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
- template<int L>
|
|
+ int search(const CharString& other, int from = 0) const {
|
|
- int search(const CharString<L>& other, int from = 0) const {
|
|
|
|
for(int i = from; i < length; i++) {
|
|
for(int i = from; i < length; i++) {
|
|
if(startsWidth(other, i)) {
|
|
if(startsWidth(other, i)) {
|
|
return i;
|
|
return i;
|
|
@@ -247,8 +254,7 @@ namespace Core {
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
|
|
- template<int L>
|
|
+ bool contains(const CharString& other, int from = 0) const {
|
|
- bool contains(const CharString<L>& other, int from = 0) const {
|
|
|
|
return search(other, from) >= 0;
|
|
return search(other, from) >= 0;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -265,24 +271,22 @@ namespace Core {
|
|
return search(u, from) >= 0;
|
|
return search(u, from) >= 0;
|
|
}
|
|
}
|
|
|
|
|
|
- CharString substring(int from, int to) const {
|
|
+ check_return Error substring(CharString& s, int from, int to) const {
|
|
|
|
+ s.clear();
|
|
from = Math::max(from, 0);
|
|
from = Math::max(from, 0);
|
|
to = Math::min(to, length - 1);
|
|
to = Math::min(to, length - 1);
|
|
- CharString s;
|
|
|
|
for(int i = from; i <= to; i++) {
|
|
for(int i = from; i <= to; i++) {
|
|
- (void)s.append(data[i]);
|
|
+ CORE_RETURN_ERROR(s.append(data[i]));
|
|
}
|
|
}
|
|
- return s;
|
|
+ return Error::NONE;
|
|
}
|
|
}
|
|
|
|
|
|
- CharString substring(int from = 0) const {
|
|
+ check_return Error substring(CharString& s, int from = 0) const {
|
|
- return substring(from, length - 1);
|
|
+ return substring(s, from, length - 1);
|
|
}
|
|
}
|
|
|
|
|
|
- template<int L1, int L2>
|
|
+ check_return Error replace(CharString& s, const CharString& search,
|
|
- check_return Error replace(const CharString<L1>& search,
|
|
+ const CharString& replace) {
|
|
- const CharString<L2>& replace) {
|
|
|
|
- CharString<N> s;
|
|
|
|
int i = 0;
|
|
int i = 0;
|
|
while(i < length) {
|
|
while(i < length) {
|
|
if(startsWidth(search, i)) {
|
|
if(startsWidth(search, i)) {
|
|
@@ -293,8 +297,7 @@ namespace Core {
|
|
i++;
|
|
i++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- *this = s;
|
|
+ return copyFrom(s);
|
|
- return Error::NONE;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
void replace(char search, char replace) {
|
|
void replace(char search, char replace) {
|
|
@@ -313,7 +316,7 @@ namespace Core {
|
|
|
|
|
|
private:
|
|
private:
|
|
Error add(char c) {
|
|
Error add(char c) {
|
|
- if(length >= N - 1) {
|
|
+ if(length >= capacity - 1) {
|
|
return Error::CAPACITY_REACHED;
|
|
return Error::CAPACITY_REACHED;
|
|
}
|
|
}
|
|
data[length++] = c;
|
|
data[length++] = c;
|
|
@@ -388,6 +391,45 @@ namespace Core {
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+ template<int N>
|
|
|
|
+ class ArrayCharString final : public CharString {
|
|
|
|
+ static_assert(N > 0, "size of array string must be positive");
|
|
|
|
+ char data[static_cast<unsigned int>(N)];
|
|
|
|
+
|
|
|
|
+ public:
|
|
|
|
+ ArrayCharString() : CharString(data, N) {
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ArrayCharString(const ArrayCharString& other) : CharString(data, N) {
|
|
|
|
+ Core::memoryCopy(data, other.data, N);
|
|
|
|
+ length = other.length;
|
|
|
|
+ hash = other.hash;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ArrayCharString& operator=(const ArrayCharString& other) {
|
|
|
|
+ if(this != &other) {
|
|
|
|
+ Core::memoryCopy(data, other.data, N);
|
|
|
|
+ length = other.length;
|
|
|
|
+ hash = other.hash;
|
|
|
|
+ }
|
|
|
|
+ return *this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ template<typename... Args>
|
|
|
|
+ check_return Error format(Args&&... args) {
|
|
|
|
+ ArrayCharString s;
|
|
|
|
+ return CharString::format(s, Core::forward<Args>(args)...);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ check_return Error replace(const CharString& search,
|
|
|
|
+ const CharString& replace) {
|
|
|
|
+ ArrayCharString s;
|
|
|
|
+ return CharString::replace(s, search, replace);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ using CharString::replace;
|
|
|
|
+ };
|
|
|
|
+
|
|
template<int N>
|
|
template<int N>
|
|
class Char32String final {
|
|
class Char32String final {
|
|
int length;
|
|
int length;
|
|
@@ -470,21 +512,13 @@ namespace Core {
|
|
}
|
|
}
|
|
|
|
|
|
check_return Error append(const char* s) {
|
|
check_return Error append(const char* s) {
|
|
- if constexpr(IsSame<c32, char>) {
|
|
+ while(true) {
|
|
- // stringLength as s could be some part of data
|
|
+ c32 u = 0;
|
|
- for(int i = stringLength(s); i > 0; i--) {
|
|
+ CORE_RETURN_ERROR(readUnicode(u, s));
|
|
- CORE_RETURN_ERROR(append(*(s++)));
|
|
+ if(u == 0) {
|
|
- }
|
|
+ return Error::NONE;
|
|
- return Error::NONE;
|
|
|
|
- } else {
|
|
|
|
- while(true) {
|
|
|
|
- c32 u = 0;
|
|
|
|
- CORE_RETURN_ERROR(readUnicode(u, s));
|
|
|
|
- if(u == 0) {
|
|
|
|
- return Error::NONE;
|
|
|
|
- }
|
|
|
|
- CORE_RETURN_ERROR(append(u));
|
|
|
|
}
|
|
}
|
|
|
|
+ CORE_RETURN_ERROR(append(u));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -561,8 +595,7 @@ namespace Core {
|
|
return t.toString(*this);
|
|
return t.toString(*this);
|
|
}
|
|
}
|
|
|
|
|
|
- template<int L>
|
|
+ check_return Error toString(CharString& s) const {
|
|
- check_return Error toString(CharString<L>& s) const {
|
|
|
|
int l = length; // length changes if &s == this
|
|
int l = length; // length changes if &s == this
|
|
for(int i = 0; i < l; i++) {
|
|
for(int i = 0; i < l; i++) {
|
|
CORE_RETURN_ERROR(s.append(data[i]));
|
|
CORE_RETURN_ERROR(s.append(data[i]));
|
|
@@ -814,7 +847,7 @@ namespace Core {
|
|
}
|
|
}
|
|
|
|
|
|
template<int N>
|
|
template<int N>
|
|
- using String8 = CharString<N>;
|
|
+ using String8 = ArrayCharString<N>;
|
|
|
|
|
|
template<int N>
|
|
template<int N>
|
|
using String32 = Char32String<N>;
|
|
using String32 = Char32String<N>;
|
|
@@ -830,13 +863,11 @@ bool operator!=(const c32* cs, const Core::Char32String<N>& s) {
|
|
return s != cs;
|
|
return s != cs;
|
|
}
|
|
}
|
|
|
|
|
|
-template<int N>
|
|
+inline bool operator==(const char* cs, const Core::CharString& s) {
|
|
-bool operator==(const char* cs, const Core::CharString<N>& s) {
|
|
|
|
return s == cs;
|
|
return s == cs;
|
|
}
|
|
}
|
|
|
|
|
|
-template<int N>
|
|
+inline bool operator!=(const char* cs, const Core::CharString& s) {
|
|
-bool operator!=(const char* cs, const Core::CharString<N>& s) {
|
|
|
|
return s != cs;
|
|
return s != cs;
|
|
}
|
|
}
|
|
|
|
|