Change istring to use string_view for the majority of its operations

The istring and string_view operators have identical implementations. By
changing the istring operators to cast to string_view and use that
implementation instead, the number of redundant implementations is
reduced. This does incurr a small performance penalty, around 15 cycles
when tested on the MGBA Gameboy Advance emulator (uses an armv7tdmi).
When compared to the time operations take, the performance difference is
negligible. Ex. An insertion with two 8 character strings takes around
450 cycles.
This commit is contained in:
Madeline Busig 2024-03-07 16:28:18 -07:00
parent 98d55c9869
commit 52d91aa752
2 changed files with 7 additions and 28 deletions

View File

@ -33,6 +33,8 @@ public:
istring(char* _str, size_t _capacity, size_t _size); istring(char* _str, size_t _capacity, size_t _size);
operator string_view() const { return string_view(m_str, m_size); }
// Data functions // Data functions
// If the new string does not fit, it will be truncated to the capacity // If the new string does not fit, it will be truncated to the capacity
@ -112,8 +114,6 @@ public:
* *
* Inserting a substring of the destination string is not supported. * Inserting a substring of the destination string is not supported.
*/ */
istring& insert(size_t index, const istring& str);
istring& insert(size_t index, const istring& str, size_t s_index, size_t count = npos);
istring& insert(size_t index, const string_view& str); istring& insert(size_t index, const string_view& str);
istring& insert(size_t index, const string_view& str, size_t str_index, size_t count = npos); istring& insert(size_t index, const string_view& str, size_t str_index, size_t count = npos);
istring& insert(size_t index, size_t count, char ch); istring& insert(size_t index, size_t count, char ch);
@ -130,8 +130,6 @@ public:
void push_back(char ch); void push_back(char ch);
void pop_back(); void pop_back();
istring& append(const istring& str);
istring& append(const istring& str, size_t s_index, size_t count = npos);
istring& append(const string_view& str); istring& append(const string_view& str);
istring& append(const string_view& str, size_t str_index, size_t count = npos); istring& append(const string_view& str, size_t str_index, size_t count = npos);
istring& append(size_t count, char ch); istring& append(size_t count, char ch);
@ -141,14 +139,9 @@ public:
istring& operator+=(char ch); istring& operator+=(char ch);
istring& operator+=(const string_view& str); istring& operator+=(const string_view& str);
istring& operator+=(const istring& str);
istring& replace(size_t pos, size_t count, const istring& str);
istring& replace(const_iterator first, const_iterator last, const istring& str);
istring& replace(size_t pos, size_t count, const istring& str, size_t pos2, size_t count2 = npos);
istring& replace(size_t pos, size_t count, const string_view& str, size_t pos2, size_t count2 = npos);
istring& replace(const_iterator first, const_iterator last, const string_view& str, size_t pos2, size_t count2 = npos);
istring& replace(size_t pos, size_t count, const string_view& str); istring& replace(size_t pos, size_t count, const string_view& str);
istring& replace(size_t pos, size_t count, const string_view& str, size_t pos2, size_t count2 = npos);
istring& replace(const_iterator first, const_iterator last, const string_view& str); istring& replace(const_iterator first, const_iterator last, const string_view& str);
istring& replace(size_t pos, size_t count, size_t count2, char ch); istring& replace(size_t pos, size_t count, size_t count2, char ch);
istring& replace(const_iterator first, const_iterator last, size_t count2, char ch); istring& replace(const_iterator first, const_iterator last, size_t count2, char ch);
@ -169,46 +162,30 @@ public:
// Search // Search
size_t find(const istring& str, size_t pos = 0) const;
size_t find(const string_view& str, size_t pos = 0) const; size_t find(const string_view& str, size_t pos = 0) const;
size_t find(char ch, size_t pos = 0) const; size_t find(char ch, size_t pos = 0) const;
size_t rfind(const istring& str, size_t pos = npos) const;
size_t rfind(const string_view& str, size_t pos = npos) const; size_t rfind(const string_view& str, size_t pos = npos) const;
size_t rfind(char ch, size_t pos = npos) const; size_t rfind(char ch, size_t pos = npos) const;
size_t find_first_of(const istring& str, size_t pos = 0) const;
size_t find_first_of(const string_view& str, size_t pos = 0) const; size_t find_first_of(const string_view& str, size_t pos = 0) const;
size_t find_first_of(char ch, size_t pos = 0) const; size_t find_first_of(char ch, size_t pos = 0) const;
size_t find_first_not_of(const istring& str, size_t pos = 0) const;
size_t find_first_not_of(const string_view& str, size_t pos = 0) const; size_t find_first_not_of(const string_view& str, size_t pos = 0) const;
size_t find_first_not_of(char ch, size_t pos = 0) const; size_t find_first_not_of(char ch, size_t pos = 0) const;
size_t find_last_of(const istring& str, size_t pos = npos) const;
size_t find_last_of(const string_view& str, size_t pos = npos) const; size_t find_last_of(const string_view& str, size_t pos = npos) const;
size_t find_last_of(char ch, size_t pos = npos) const; size_t find_last_of(char ch, size_t pos = npos) const;
size_t find_last_not_of(const istring& str, size_t pos = npos) const;
size_t find_last_not_of(const string_view& str, size_t pos = npos) const; size_t find_last_not_of(const string_view& str, size_t pos = npos) const;
size_t find_last_not_of(char ch, size_t pos = npos) const; size_t find_last_not_of(char ch, size_t pos = npos) const;
// Comparison // Comparison
int32_t compare(const istring& str) const;
int32_t compare(size_t pos, size_t count, const istring& str) const;
int32_t compare(size_t pos1, size_t count1, istring& str, size_t pos2, size_t count2 = npos) const;
int32_t compare(const string_view& str) const; int32_t compare(const string_view& str) const;
int32_t compare(size_t pos, size_t count, const string_view& str) const; int32_t compare(size_t pos, size_t count, const string_view& str) const;
int32_t compare(size_t pos1, size_t count1, const string_view& str, size_t pos2, size_t count2 = npos) const; int32_t compare(size_t pos1, size_t count1, const string_view& str, size_t pos2, size_t count2 = npos) const;
friend bool operator==(const istring& lhs, const istring& rhs);
friend bool operator!=(const istring& lhs, const istring& rhs);
friend bool operator<(const istring& lhs, const istring& rhs);
friend bool operator<=(const istring& lhs, const istring& rhs);
friend bool operator>(const istring& lhs, const istring& rhs);
friend bool operator>=(const istring& lhs, const istring& rhs);
friend bool operator==(const istring& lhs, const string_view& rhs); friend bool operator==(const istring& lhs, const string_view& rhs);
friend bool operator==(const string_view& lhs, const istring& rhs); friend bool operator==(const string_view& lhs, const istring& rhs);
friend bool operator!=(const istring& lhs, const string_view& rhs); friend bool operator!=(const istring& lhs, const string_view& rhs);
@ -237,6 +214,8 @@ public:
mtl::memcpy(m_buf, str.c_str(), m_size); mtl::memcpy(m_buf, str.c_str(), m_size);
m_str[m_size] = '\0'; m_str[m_size] = '\0';
} }
using istring::operator=;
}; };
class string_ext : public istring { class string_ext : public istring {

View File

@ -191,7 +191,7 @@ void istring::clear() {
m_size = 0; m_size = 0;
} }
istring& istring::insert(size_t index, const istring& str) { istring& istring::insert(size_t index, const string_view& str) {
char* substr = m_str + index; char* substr = m_str + index;
size_t space = m_capacity - index; size_t space = m_capacity - index;
@ -213,7 +213,7 @@ istring& istring::insert(size_t index, const istring& str) {
return *this; return *this;
} }
istring& istring::insert(size_t index, const istring& str, size_t str_index, size_t count) { istring& istring::insert(size_t index, const string_view& str, size_t str_index, size_t count) {
if (count == npos) { if (count == npos) {
count = str.m_size - str_index; count = str.m_size - str_index;
} }