From 52d91aa752baa22e5277651af707c0fd2a9e2659 Mon Sep 17 00:00:00 2001 From: Madeline Busig Date: Thu, 7 Mar 2024 16:28:18 -0700 Subject: [PATCH] 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. --- include/mtl/string.hpp | 31 +++++-------------------------- src/string.cpp | 4 ++-- 2 files changed, 7 insertions(+), 28 deletions(-) diff --git a/include/mtl/string.hpp b/include/mtl/string.hpp index f6cd944..92e2220 100644 --- a/include/mtl/string.hpp +++ b/include/mtl/string.hpp @@ -33,6 +33,8 @@ public: istring(char* _str, size_t _capacity, size_t _size); + operator string_view() const { return string_view(m_str, m_size); } + // Data functions // 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. */ - 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, size_t str_index, size_t count = npos); istring& insert(size_t index, size_t count, char ch); @@ -130,8 +130,6 @@ public: void push_back(char ch); 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, size_t str_index, size_t count = npos); istring& append(size_t count, char ch); @@ -141,14 +139,9 @@ public: istring& operator+=(char ch); 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, size_t pos2, size_t count2 = npos); 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(const_iterator first, const_iterator last, size_t count2, char ch); @@ -169,46 +162,30 @@ public: // 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(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(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(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(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(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(char ch, size_t pos = npos) const; // 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(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; - 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 string_view& lhs, const istring& 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); m_str[m_size] = '\0'; } + + using istring::operator=; }; class string_ext : public istring { diff --git a/src/string.cpp b/src/string.cpp index 15c5610..150f350 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -191,7 +191,7 @@ void istring::clear() { 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; size_t space = m_capacity - index; @@ -213,7 +213,7 @@ istring& istring::insert(size_t index, const istring& str) { 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) { count = str.m_size - str_index; }