Rename string class to basic_string

This commit is contained in:
EnderIce2 2024-03-19 02:33:57 +02:00
parent be80b52543
commit 90b9e7301d
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD

View File

@ -83,10 +83,10 @@ namespace std
} }
/** /**
* @brief String class * @brief Basic string class
* String class that can be used to store strings. * String class that can be used to store strings.
*/ */
class string class basic_string
{ {
private: private:
char *Data{}; char *Data{};
@ -96,17 +96,17 @@ namespace std
public: public:
static const size_t npos = -1; static const size_t npos = -1;
string(const char *Str = "") basic_string(const char *Str = "")
{ {
this->Length = strlen(Str); this->Length = strlen(Str);
this->Capacity = this->Length + 1; this->Capacity = this->Length + 1;
this->Data = new char[this->Capacity]; this->Data = new char[this->Capacity];
strcpy(this->Data, Str); strcpy(this->Data, Str);
strdbg("%#lx: New string created: \"%s\" (data: %#lx, length: %d, capacity: %d)", strdbg("%#lx: New basic_string created: \"%s\" (data: %#lx, length: %d, capacity: %d)",
this, this->Data, this->Data, this->Length, this->Capacity); this, this->Data, this->Data, this->Length, this->Capacity);
} }
~string() ~basic_string()
{ {
strdbg("%#lx: String deleted: \"%s\" (data: %#lx, length: %d, capacity: %d)", strdbg("%#lx: String deleted: \"%s\" (data: %#lx, length: %d, capacity: %d)",
this, this->Data, this->Data, this->Length, this->Capacity); this, this->Data, this->Data, this->Length, this->Capacity);
@ -164,7 +164,7 @@ namespace std
this, this->Data, this->Data, this->Length, this->Capacity); this, this->Data, this->Data, this->Length, this->Capacity);
} }
void concat(const string &Other) void concat(const basic_string &Other)
{ {
size_t NewLength = this->Length + Other.Length; size_t NewLength = this->Length + Other.Length;
this->resize(NewLength); this->resize(NewLength);
@ -218,7 +218,7 @@ namespace std
return npos; return npos;
} }
size_t find(const string &Str, size_t Pos = 0) const size_t find(const basic_string &Str, size_t Pos = 0) const
{ {
strdbg("%#lx: String find: \"%s\", %d", strdbg("%#lx: String find: \"%s\", %d",
this, Str.c_str(), Pos); this, Str.c_str(), Pos);
@ -397,19 +397,19 @@ namespace std
return npos; return npos;
} }
size_t substr(const string &Str, size_t Pos = 0) const size_t substr(const basic_string &Str, size_t Pos = 0) const
{ {
strdbg("%#lx: String substr: \"%s\", %d", strdbg("%#lx: String substr: \"%s\", %d",
this, Str.c_str(), Pos); this, Str.c_str(), Pos);
return this->substr(Str.c_str(), Pos); return this->substr(Str.c_str(), Pos);
} }
string substr(size_t Pos = 0, size_t Count = npos) const basic_string substr(size_t Pos = 0, size_t Count = npos) const
{ {
strdbg("%#lx: String substr: %d, %d", strdbg("%#lx: String substr: %d, %d",
this, Pos, Count); this, Pos, Count);
if (Pos >= this->Length) if (Pos >= this->Length)
return string(); return basic_string();
if (Count == npos) if (Count == npos)
Count = this->Length - Pos; Count = this->Length - Pos;
@ -417,7 +417,7 @@ namespace std
if (Pos + Count > this->Length) if (Pos + Count > this->Length)
Count = this->Length - Pos; Count = this->Length - Pos;
string ret; basic_string ret;
ret.resize(Count); ret.resize(Count);
for (size_t i = 0; i < Count; i++) for (size_t i = 0; i < Count; i++)
ret.Data[i] = this->Data[Pos + i]; ret.Data[i] = this->Data[Pos + i];
@ -451,7 +451,7 @@ namespace std
this, this->Data, this->Data, this->Length, this->Capacity); this, this->Data, this->Data, this->Length, this->Capacity);
} }
void replace(size_t Pos, size_t Count, const string &Str) void replace(size_t Pos, size_t Count, const basic_string &Str)
{ {
strdbg("%#lx: String replace: %d, %d, \"%s\"", strdbg("%#lx: String replace: %d, %d, \"%s\"",
this, Pos, Count, Str.Data); this, Pos, Count, Str.Data);
@ -507,26 +507,25 @@ namespace std
this, this->Data, this->Data, this->Length, this->Capacity); this, this->Data, this->Data, this->Length, this->Capacity);
} }
basic_string operator+(const basic_string &Other) const
string operator+(const string &Other) const
{ {
string result = *this; basic_string result = *this;
result.concat(Other); result.concat(Other);
strdbg("%#lx: String added: \"%s\" (data: %#lx, length: %d, capacity: %d)", strdbg("%#lx: String added: \"%s\" (data: %#lx, length: %d, capacity: %d)",
this, result.Data, result.Data, result.Length, result.Capacity); this, result.Data, result.Data, result.Length, result.Capacity);
return result; return result;
} }
string operator+(const char *Other) const basic_string operator+(const char *Other) const
{ {
string result = *this; basic_string result = *this;
result.concat(Other); result.concat(Other);
strdbg("%#lx: String added: \"%s\" (data: %#lx, length: %d, capacity: %d)", strdbg("%#lx: String added: \"%s\" (data: %#lx, length: %d, capacity: %d)",
this, result.Data, result.Data, result.Length, result.Capacity); this, result.Data, result.Data, result.Length, result.Capacity);
return result; return result;
} }
string &operator+=(const string &Other) basic_string &operator+=(const basic_string &Other)
{ {
this->concat(Other); this->concat(Other);
strdbg("%#lx: String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", strdbg("%#lx: String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)",
@ -534,7 +533,7 @@ namespace std
return *this; return *this;
} }
string &operator+=(const char *Other) basic_string &operator+=(const char *Other)
{ {
this->concat(Other); this->concat(Other);
strdbg("%#lx: String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", strdbg("%#lx: String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)",
@ -542,7 +541,7 @@ namespace std
return *this; return *this;
} }
string &operator+=(char Other) basic_string &operator+=(char Other)
{ {
const char str[2] = {Other, '\0'}; const char str[2] = {Other, '\0'};
this->concat(str); this->concat(str);
@ -552,9 +551,9 @@ namespace std
} }
/* warning: implicitly-declared 'constexpr String::String(const String&)' is deprecated [-Wdeprecated-copy] */ /* warning: implicitly-declared 'constexpr String::String(const String&)' is deprecated [-Wdeprecated-copy] */
string &operator=(const string &Other) = default; basic_string &operator=(const basic_string &Other) = default;
// string &operator=(const string &Other) // basic_string &operator=(const basic_string &Other)
// { // {
// if (this != &Other) // if (this != &Other)
// { // {
@ -568,7 +567,7 @@ namespace std
// return *this; // return *this;
// } // }
string &operator=(const char *Other) basic_string &operator=(const char *Other)
{ {
this->Length = strlen(Other); this->Length = strlen(Other);
this->Capacity = this->Length + 1; this->Capacity = this->Length + 1;
@ -580,7 +579,7 @@ namespace std
return *this; return *this;
} }
string &operator<<(const string &Other) basic_string &operator<<(const basic_string &Other)
{ {
this->concat(Other); this->concat(Other);
strdbg("%#lx: String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", strdbg("%#lx: String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)",
@ -588,7 +587,7 @@ namespace std
return *this; return *this;
} }
string &operator<<(const char *Other) basic_string &operator<<(const char *Other)
{ {
this->concat(Other); this->concat(Other);
strdbg("%#lx: String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", strdbg("%#lx: String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)",
@ -620,7 +619,7 @@ namespace std
return this->Data[Index]; return this->Data[Index];
} }
bool operator==(const string &Other) const bool operator==(const basic_string &Other) const
{ {
strdbg("%#lx: String compared: \"%s\" == \"%s\"", strdbg("%#lx: String compared: \"%s\" == \"%s\"",
this, this->Data, Other.Data); this, this->Data, Other.Data);
@ -634,7 +633,7 @@ namespace std
return strcmp(this->Data, Other) != 0; return strcmp(this->Data, Other) != 0;
} }
bool operator!=(const string &Other) const bool operator!=(const basic_string &Other) const
{ {
strdbg("%#lx: String compared: \"%s\" != \"%s\"", strdbg("%#lx: String compared: \"%s\" != \"%s\"",
this, this->Data, Other.Data); this, this->Data, Other.Data);
@ -701,6 +700,8 @@ namespace std
} }
}; };
typedef basic_string string;
template <class CharT> template <class CharT>
class char_traits class char_traits
{ {