lego1: Two more operators for MxString (#53)

Followed the hint from @madebr in #31 that the next function in MxString was operator+. The one after that is operator+= and both are at 100%.

Squashed commits:
* Removed unnecessary consts
* Replaced malloc/free with new/delete, which solved swapped regs in operator=
* Use delete[] when freeing char* m_data
This commit is contained in:
MS 2023-06-27 22:57:30 -04:00 committed by GitHub
parent 8da62cc596
commit 2d9af630ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 12 deletions

View file

@ -6,7 +6,7 @@
MxString::MxString() MxString::MxString()
{ {
// Set string to one char in length and set that char to null terminator // Set string to one char in length and set that char to null terminator
this->m_data = (char *)malloc(1); this->m_data = new char[1];
this->m_data[0] = 0; this->m_data[0] = 0;
this->m_length = 0; this->m_length = 0;
} }
@ -15,7 +15,7 @@ MxString::MxString()
MxString::MxString(const MxString &str) MxString::MxString(const MxString &str)
{ {
this->m_length = str.m_length; this->m_length = str.m_length;
this->m_data = (char *)malloc(this->m_length + 1); this->m_data = new char[this->m_length + 1];
strcpy(this->m_data, str.m_data); strcpy(this->m_data, str.m_data);
} }
@ -24,10 +24,10 @@ MxString::MxString(const char *str)
{ {
if (str) { if (str) {
this->m_length = strlen(str); this->m_length = strlen(str);
this->m_data = (char *)malloc(this->m_length + 1); this->m_data = new char[this->m_length + 1];
strcpy(this->m_data, str); strcpy(this->m_data, str);
} else { } else {
this->m_data = (char *)malloc(1); this->m_data = new char[1];
this->m_data[0] = 0; this->m_data[0] = 0;
this->m_length = 0; this->m_length = 0;
} }
@ -36,7 +36,7 @@ MxString::MxString(const char *str)
// OFFSET: LEGO1 0x100ae420 // OFFSET: LEGO1 0x100ae420
MxString::~MxString() MxString::~MxString()
{ {
free(this->m_data); delete[] this->m_data;
} }
// OFFSET: LEGO1 0x100ae490 // OFFSET: LEGO1 0x100ae490
@ -52,30 +52,63 @@ void MxString::ToLowerCase()
} }
// OFFSET: LEGO1 0x100ae4b0 // OFFSET: LEGO1 0x100ae4b0
const MxString &MxString::operator=(MxString *param) MxString &MxString::operator=(MxString *param)
{ {
if (this->m_data != param->m_data) if (this->m_data != param->m_data)
{ {
free(this->m_data); delete[] this->m_data;
this->m_length = param->m_length; this->m_length = param->m_length;
this->m_data = (char *)malloc(this->m_length + 1); this->m_data = new char[this->m_length + 1];
strcpy(this->m_data, param->m_data); strcpy(this->m_data, param->m_data);
} }
return *this; return *this;
} }
// TODO: this *mostly* matches, again weird with the comparison
// OFFSET: LEGO1 0x100ae510 // OFFSET: LEGO1 0x100ae510
const MxString &MxString::operator=(const char *param) const MxString &MxString::operator=(const char *param)
{ {
if (this->m_data != param) if (this->m_data != param)
{ {
free(this->m_data); delete[] this->m_data;
this->m_length = strlen(param); this->m_length = strlen(param);
this->m_data = (char *)malloc(this->m_length + 1); this->m_data = new char[this->m_length + 1];
strcpy(this->m_data, param); strcpy(this->m_data, param);
} }
return *this; return *this;
} }
// Return type is intentionally just MxString, not MxString&.
// This forces MSVC to add $ReturnUdt$ to the stack for 100% match.
// OFFSET: LEGO1 0x100ae580
MxString MxString::operator+(const char *str)
{
// MxString constructor allocates 1 byte for m_data, so free that first
MxString tmp;
delete[] tmp.m_data;
tmp.m_length = strlen(str) + this->m_length;
tmp.m_data = new char[tmp.m_length + 1];
strcpy(tmp.m_data, this->m_data);
strcpy(tmp.m_data + this->m_length, str);
return MxString(tmp);
}
// OFFSET: LEGO1 0x100ae690
MxString& MxString::operator+=(const char *str)
{
int newlen = this->m_length + strlen(str);
char *tmp = new char[newlen + 1];
strcpy(tmp, this->m_data);
strcpy(tmp + this->m_length, str);
delete[] this->m_data;
this->m_length = newlen;
this->m_data = tmp;
return *this;
}

View file

@ -14,7 +14,9 @@ class MxString : public MxCore
MxString(const char *); MxString(const char *);
void ToUpperCase(); void ToUpperCase();
void ToLowerCase(); void ToLowerCase();
const MxString &operator=(MxString *); MxString& operator=(MxString *);
MxString operator+(const char *);
MxString& operator+=(const char *);
inline const char *GetData() const { return m_data; } inline const char *GetData() const { return m_data; }