QoL improvements

This commit is contained in:
Alex
2023-03-27 20:11:32 +03:00
parent 3eb6923374
commit 93afcd2210
59 changed files with 612 additions and 424 deletions

View File

@ -25,85 +25,85 @@ namespace std
class string
{
private:
char *m_Data;
size_t m_Length;
size_t m_Capacity;
char *Data;
size_t Length;
size_t Capacity;
public:
static const size_t npos = -1;
string(const char *Str = "")
{
this->m_Length = strlen(Str);
this->m_Capacity = this->m_Length + 1;
this->m_Data = new char[m_Capacity];
strcpy(m_Data, Str);
strdbg("New string created: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
this->Length = strlen(Str);
this->Capacity = this->Length + 1;
this->Data = new char[this->Capacity];
strcpy(this->Data, Str);
strdbg("New string created: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
}
~string()
{
strdbg("String deleted: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
delete[] this->m_Data, this->m_Data = nullptr;
strdbg("String deleted: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
delete[] this->Data, this->Data = nullptr;
}
int length() const
size_t length() const
{
strdbg("String length: %d", this->m_Length);
return this->m_Length;
strdbg("String length: %d", this->Length);
return this->Length;
}
int capacity() const
size_t capacity() const
{
strdbg("String capacity: %d", this->m_Capacity);
return this->m_Capacity;
strdbg("String capacity: %d", this->Capacity);
return this->Capacity;
}
const char *c_str() const
{
strdbg("String data: \"%s\"", this->m_Data);
return this->m_Data;
strdbg("String data: \"%s\"", this->Data);
return this->Data;
}
void resize(size_t NewLength)
{
strdbg("String resize: %d", NewLength);
if (NewLength > this->m_Capacity)
if (NewLength > this->Capacity)
{
size_t newCapacity = NewLength + 1;
char *newData = new char[newCapacity];
strcpy(newData, this->m_Data);
strcpy(newData, this->Data);
strdbg("old: %#lx, new: %#lx", this->m_Data, newData);
delete[] this->m_Data;
this->m_Data = newData;
this->m_Capacity = newCapacity;
strdbg("old: %#lx, new: %#lx", this->Data, newData);
delete[] this->Data;
this->Data = newData;
this->Capacity = newCapacity;
}
this->m_Length = NewLength;
this->m_Data[m_Length] = '\0';
strdbg("String resized: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
this->Length = NewLength;
this->Data[this->Length] = '\0';
strdbg("String resized: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
}
void concat(const string &Other)
{
int NewLength = this->m_Length + Other.m_Length;
size_t NewLength = this->Length + Other.Length;
this->resize(NewLength);
strcat(m_Data, Other.m_Data);
strdbg("String concatenated: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
strcat(this->Data, Other.Data);
strdbg("String concatenated: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
}
bool empty() const
{
strdbg("String empty: %d", this->m_Length == 0);
return this->m_Length == 0;
strdbg("String empty: %d", this->Length == 0);
return this->Length == 0;
}
size_t size() const
{
strdbg("String size: %d", this->m_Length);
return this->m_Length;
strdbg("String size: %d", this->Length);
return this->Length;
}
void clear()
@ -115,15 +115,15 @@ namespace std
size_t find(const char *Str, size_t Pos = 0) const
{
strdbg("String find: \"%s\", %d", Str, Pos);
if (Pos >= this->m_Length)
if (Pos >= this->Length)
return npos;
for (size_t i = Pos; i < this->m_Length; i++)
for (size_t i = Pos; i < this->Length; i++)
{
bool found = true;
for (size_t j = 0; Str[j] != '\0'; j++)
{
if (this->m_Data[i + j] != Str[j])
if (this->Data[i + j] != Str[j])
{
found = false;
break;
@ -144,35 +144,35 @@ namespace std
void erase(int Index, int Count = 1)
{
strdbg("String erase: %d, %d", Index, Count);
if (Index < 0 || (size_t)Index >= this->m_Length)
if (Index < 0 || (size_t)Index >= this->Length)
return;
if (Count < 0)
return;
if ((size_t)(Index + Count) > this->m_Length)
Count = this->m_Length - Index;
if ((size_t)(Index + Count) > this->Length)
Count = (int)this->Length - Index;
for (size_t i = Index; i < this->m_Length - Count; i++)
this->m_Data[i] = this->m_Data[i + Count];
for (size_t i = Index; i < this->Length - Count; i++)
this->Data[i] = this->Data[i + Count];
this->m_Length -= Count;
this->m_Data[m_Length] = '\0';
strdbg("String erased: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
this->Length -= Count;
this->Data[this->Length] = '\0';
strdbg("String erased: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
}
size_t find_last_not_of(const char *Str, size_t Pos = npos) const
{
strdbg("String find_last_not_of: \"%s\", %d", Str, Pos);
if (Pos == npos)
Pos = this->m_Length - 1;
Pos = this->Length - 1;
for (int i = (int)Pos; i >= 0; i--)
{
bool found = false;
for (size_t j = 0; Str[j] != '\0'; j++)
{
if (this->m_Data[i] == Str[j])
if (this->Data[i] == Str[j])
{
found = true;
break;
@ -187,15 +187,15 @@ namespace std
size_t find_first_not_of(const char *Str, size_t Pos = 0) const
{
strdbg("String find_first_not_of: \"%s\", %d", Str, Pos);
if (Pos >= this->m_Length)
if (Pos >= this->Length)
return npos;
for (size_t i = Pos; i < this->m_Length; i++)
for (size_t i = Pos; i < this->Length; i++)
{
bool found = false;
for (size_t j = 0; Str[j] != '\0'; j++)
{
if (this->m_Data[i] == Str[j])
if (this->Data[i] == Str[j])
{
found = true;
break;
@ -210,15 +210,15 @@ namespace std
size_t find_first_of(const char *Str, size_t Pos = 0) const
{
strdbg("String find_first_of: \"%s\", %d", Str, Pos);
if (Pos >= this->m_Length)
if (Pos >= this->Length)
return npos;
for (size_t i = Pos; i < this->m_Length; i++)
for (size_t i = Pos; i < this->Length; i++)
{
bool found = false;
for (size_t j = 0; Str[j] != '\0'; j++)
{
if (this->m_Data[i] == Str[j])
if (this->Data[i] == Str[j])
{
found = true;
break;
@ -234,14 +234,14 @@ namespace std
{
strdbg("String find_last_of: \"%s\", %d", Str, Pos);
if (Pos == npos)
Pos = this->m_Length - 1;
Pos = this->Length - 1;
for (int i = (int)Pos; i >= 0; i--)
{
bool found = false;
for (int j = 0; Str[j] != '\0'; j++)
{
if (this->m_Data[i] == Str[j])
if (this->Data[i] == Str[j])
{
found = true;
break;
@ -256,12 +256,12 @@ namespace std
size_t find_first_of(char C, size_t Pos = 0) const
{
strdbg("String find_first_of: '%c', %d", C, Pos);
if (Pos >= this->m_Length)
if (Pos >= this->Length)
return npos;
for (size_t i = Pos; i < this->m_Length; i++)
for (size_t i = Pos; i < this->Length; i++)
{
if (this->m_Data[i] == C)
if (this->Data[i] == C)
return i;
}
return npos;
@ -271,11 +271,11 @@ namespace std
{
strdbg("String find_last_of: '%c', %d", C, Pos);
if (Pos == npos)
Pos = this->m_Length - 1;
Pos = this->Length - 1;
for (int i = (int)Pos; i >= 0; i--)
{
if (this->m_Data[i] == C)
if (this->Data[i] == C)
return i;
}
return npos;
@ -284,15 +284,15 @@ namespace std
size_t substr(const char *Str, size_t Pos = 0) const
{
strdbg("String substr: \"%s\", %d", Str, Pos);
if (Pos >= this->m_Length)
if (Pos >= this->Length)
return npos;
for (size_t i = Pos; i < this->m_Length; i++)
for (size_t i = Pos; i < this->Length; i++)
{
bool found = true;
for (size_t j = 0; Str[j] != '\0'; j++)
{
if (this->m_Data[i + j] != Str[j])
if (this->Data[i + j] != Str[j])
{
found = false;
break;
@ -313,76 +313,76 @@ namespace std
string substr(size_t Pos = 0, size_t Count = npos) const
{
strdbg("String substr: %d, %d", Pos, Count);
if (Pos >= this->m_Length)
if (Pos >= this->Length)
return string();
if (Count == npos)
Count = this->m_Length - Pos;
Count = this->Length - Pos;
if (Pos + Count > this->m_Length)
Count = this->m_Length - Pos;
if (Pos + Count > this->Length)
Count = this->Length - Pos;
string ret;
ret.resize(Count);
for (size_t i = 0; i < Count; i++)
ret.m_Data[i] = this->m_Data[Pos + i];
ret.m_Data[Count] = '\0';
ret.Data[i] = this->Data[Pos + i];
ret.Data[Count] = '\0';
return ret;
}
void replace(size_t Pos, size_t Count, const char *Str)
{
strdbg("String replace: %d, %d, \"%s\"", Pos, Count, Str);
if (Pos >= this->m_Length)
if (Pos >= this->Length)
return;
if ((int64_t)Count < 0)
return;
if (Pos + Count > this->m_Length)
Count = this->m_Length - Pos;
if (Pos + Count > this->Length)
Count = this->Length - Pos;
int NewLength = this->m_Length - Count + strlen(Str);
size_t NewLength = this->Length - Count + strlen(Str);
this->resize(NewLength);
for (size_t i = this->m_Length - 1; i >= Pos + strlen(Str); i--)
this->m_Data[i] = this->m_Data[i - strlen(Str) + Count];
for (size_t i = this->Length - 1; i >= Pos + strlen(Str); i--)
this->Data[i] = this->Data[i - strlen(Str) + Count];
for (unsigned long i = 0; i < strlen(Str); i++)
this->m_Data[Pos + i] = Str[i];
this->Data[Pos + i] = Str[i];
strdbg("String replaced: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
strdbg("String replaced: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
}
void replace(size_t Pos, size_t Count, const string &Str)
{
strdbg("String replace: %d, %d, \"%s\"", Pos, Count, Str.m_Data);
if (Pos >= this->m_Length)
strdbg("String replace: %d, %d, \"%s\"", Pos, Count, Str.Data);
if (Pos >= this->Length)
return;
if ((int64_t)Count < 0)
return;
if (Pos + Count > this->m_Length)
Count = this->m_Length - Pos;
if (Pos + Count > this->Length)
Count = this->Length - Pos;
int NewLength = this->m_Length - Count + Str.m_Length;
size_t NewLength = this->Length - Count + Str.Length;
this->resize(NewLength);
for (size_t i = this->m_Length - 1; i >= Pos + Str.m_Length; i--)
this->m_Data[i] = this->m_Data[i - Str.m_Length + Count];
for (size_t i = this->Length - 1; i >= Pos + Str.Length; i--)
this->Data[i] = this->Data[i - Str.Length + Count];
for (size_t i = 0; i < Str.m_Length; i++)
this->m_Data[Pos + i] = Str.m_Data[i];
for (size_t i = 0; i < Str.Length; i++)
this->Data[Pos + i] = Str.Data[i];
strdbg("String replaced: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
strdbg("String replaced: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
}
string operator+(const string &Other) const
{
string result = *this;
result.concat(Other);
strdbg("String added: \"%s\" (data: %#lx, length: %d, capacity: %d)", result.m_Data, result.m_Data, result.m_Length, result.m_Capacity);
strdbg("String added: \"%s\" (data: %#lx, length: %d, capacity: %d)", result.Data, result.Data, result.Length, result.Capacity);
return result;
}
@ -390,21 +390,21 @@ namespace std
{
string result = *this;
result.concat(Other);
strdbg("String added: \"%s\" (data: %#lx, length: %d, capacity: %d)", result.m_Data, result.m_Data, result.m_Length, result.m_Capacity);
strdbg("String added: \"%s\" (data: %#lx, length: %d, capacity: %d)", result.Data, result.Data, result.Length, result.Capacity);
return result;
}
string &operator+=(const string &Other)
{
this->concat(Other);
strdbg("String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
strdbg("String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
return *this;
}
string &operator+=(const char *Other)
{
this->concat(Other);
strdbg("String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
strdbg("String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
return *this;
}
@ -415,120 +415,120 @@ namespace std
// {
// if (this != &Other)
// {
// delete[] this->m_Data;
// this->m_Data = Other.m_Data;
// this->m_Length = Other.m_Length;
// this->m_Capacity = Other.m_Capacity;
// strdbg("String assigned: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
// delete[] this->Data;
// this->Data = Other.Data;
// this->Length = Other.Length;
// this->Capacity = Other.Capacity;
// strdbg("String assigned: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
// }
// return *this;
// }
string &operator=(const char *Other)
{
this->m_Length = strlen(Other);
this->m_Capacity = this->m_Length + 1;
delete[] this->m_Data;
this->m_Data = new char[m_Capacity];
strcpy(m_Data, Other);
strdbg("String assigned: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
this->Length = strlen(Other);
this->Capacity = this->Length + 1;
delete[] this->Data;
this->Data = new char[this->Capacity];
strcpy(this->Data, Other);
strdbg("String assigned: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
return *this;
}
string &operator<<(const string &Other)
{
this->concat(Other);
strdbg("String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
strdbg("String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
return *this;
}
string &operator<<(const char *Other)
{
this->concat(Other);
strdbg("String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
strdbg("String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
return *this;
}
char &operator[](int Index)
{
strdbg("String index: %d", Index);
return this->m_Data[Index];
return this->Data[Index];
}
const char &operator[](int Index) const
{
strdbg("String index: %d", Index);
return this->m_Data[Index];
return this->Data[Index];
}
bool operator==(const string &Other) const
{
strdbg("String compared: \"%s\" == \"%s\"", this->m_Data, Other.m_Data);
return strcmp(this->m_Data, Other.m_Data) == 0;
strdbg("String compared: \"%s\" == \"%s\"", this->Data, Other.Data);
return strcmp(this->Data, Other.Data) == 0;
}
bool operator!=(const char *Other) const
{
strdbg("String compared: \"%s\" != \"%s\"", this->m_Data, Other);
return strcmp(this->m_Data, Other) != 0;
strdbg("String compared: \"%s\" != \"%s\"", this->Data, Other);
return strcmp(this->Data, Other) != 0;
}
bool operator!=(const string &Other) const
{
strdbg("String compared: \"%s\" != \"%s\"", this->m_Data, Other.m_Data);
return strcmp(this->m_Data, Other.m_Data) != 0;
strdbg("String compared: \"%s\" != \"%s\"", this->Data, Other.Data);
return strcmp(this->Data, Other.Data) != 0;
}
bool operator==(const char *Other) const
{
strdbg("String compared: \"%s\" == \"%s\"", this->m_Data, Other);
return strcmp(this->m_Data, Other) == 0;
strdbg("String compared: \"%s\" == \"%s\"", this->Data, Other);
return strcmp(this->Data, Other) == 0;
}
class iterator
{
private:
char *m_Pointer;
char *Pointer;
public:
iterator(char *Pointer) : m_Pointer(Pointer) {}
iterator(char *Pointer) : Pointer(Pointer) {}
iterator &operator++()
{
++this->m_Pointer;
strdbg("String iterator incremented: %#lx", this->m_Pointer);
++this->Pointer;
strdbg("String iterator incremented: %#lx", this->Pointer);
return *this;
}
char &operator*()
{
strdbg("String iterator dereferenced: %#lx", this->m_Pointer);
return *this->m_Pointer;
strdbg("String iterator dereferenced: %#lx", this->Pointer);
return *this->Pointer;
}
bool operator!=(const iterator &Other) const
{
strdbg("String iterator compared: %#lx != %#lx", this->m_Pointer, Other.m_Pointer);
return this->m_Pointer != Other.m_Pointer;
strdbg("String iterator compared: %#lx != %#lx", this->Pointer, Other.Pointer);
return this->Pointer != Other.Pointer;
}
bool operator==(const iterator &Other) const
{
strdbg("String iterator compared: %#lx == %#lx", this->m_Pointer, Other.m_Pointer);
return this->m_Pointer == Other.m_Pointer;
strdbg("String iterator compared: %#lx == %#lx", this->Pointer, Other.Pointer);
return this->Pointer == Other.Pointer;
}
};
iterator begin()
{
strdbg("String iterator begin: %#lx", this->m_Data);
return iterator(this->m_Data);
strdbg("String iterator begin: %#lx", this->Data);
return iterator(this->Data);
}
iterator end()
{
strdbg("String iterator end: %#lx", this->m_Data + this->m_Length);
return iterator(this->m_Data + this->m_Length);
strdbg("String iterator end: %#lx", this->Data + this->Length);
return iterator(this->Data + this->Length);
}
};
}