Change strings to use string_view instead of const char*

Because the length of a string_view is computed at compile time, it is
faster for simple copies.
This commit is contained in:
Madeline Busig 2024-03-07 14:25:39 -07:00
parent 913e54252a
commit ecac9d2087
2 changed files with 46 additions and 49 deletions

View File

@ -5,6 +5,7 @@
#include <iterator> #include <iterator>
#include "utility.hpp" #include "utility.hpp"
#include "string_view.hpp"
namespace mtl { namespace mtl {
@ -37,8 +38,8 @@ public:
// 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
istring& assign(const istring& str); istring& assign(const istring& str);
istring& assign(const istring& str, size_t pos, size_t count = npos); istring& assign(const istring& str, size_t pos, size_t count = npos);
istring& assign(const char* str, size_t count); istring& assign(const string_view& str);
istring& assign(const char* str); istring& assign(const string_view& str, size_t pos, size_t count = npos);
istring& assign(size_t count, char ch); istring& assign(size_t count, char ch);
template <typename T> template <typename T>
istring& assign(T first, T last) { istring& assign(T first, T last) {
@ -54,7 +55,7 @@ public:
} }
istring& operator=(const istring& rhs); istring& operator=(const istring& rhs);
istring& operator=(const char* rhs); istring& operator=(const string_view& rhs);
istring& operator=(char rhs); istring& operator=(char rhs);
// Element access functions // Element access functions
@ -109,8 +110,8 @@ public:
// Insert from self only works if index is after input range // Insert from self only works if index is after input range
istring& insert(size_t index, const istring& str); 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 istring& str, size_t s_index, size_t count = npos);
istring& insert(size_t index, const char* str); istring& insert(size_t index, const string_view& str);
istring& insert(size_t index, const char* str, size_t count); 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);
iterator insert(const_iterator pos, char ch); iterator insert(const_iterator pos, char ch);
iterator insert(const_iterator pos, size_t count, char ch); iterator insert(const_iterator pos, size_t count, char ch);
@ -127,24 +128,24 @@ public:
istring& append(const istring& str); istring& append(const istring& str);
istring& append(const istring& str, size_t s_index, size_t count = npos); istring& append(const istring& str, size_t s_index, size_t count = npos);
istring& append(const char* str); istring& append(const string_view& str);
istring& append(const char* str, size_t count); 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);
template <typename T> template <typename T>
istring& append(T first, T last) { istring& append(T first, T last) {
} }
istring& operator+=(char ch); istring& operator+=(char ch);
istring& operator+=(const char* str); istring& operator+=(const string_view& str);
istring& operator+=(const istring& str); istring& operator+=(const istring& str);
istring& replace(size_t pos, size_t count, 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(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 istring& str, size_t pos2, size_t count2 = npos);
istring& replace(size_t pos, size_t count, const char* str, size_t count2); 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 char* str, size_t count2); 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 char* str); istring& replace(size_t pos, size_t count, const string_view& str);
istring& replace(const_iterator first, const_iterator last, const char* 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);
template <typename T> template <typename T>
@ -158,33 +159,27 @@ public:
// Search // Search
size_t find(const istring& str, size_t pos = 0) const; size_t find(const istring& str, size_t pos = 0) const;
size_t find(const char* str, size_t pos, size_t count) const; size_t find(const string_view& str, size_t pos = 0) const;
size_t find(const char* 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 istring& str, size_t pos = npos) const;
size_t rfind(const char* str, size_t pos, size_t count) const; size_t rfind(const string_view& str, size_t pos = npos) const;
size_t rfind(const char* 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 istring& str, size_t pos = 0) const;
size_t find_first_of(const char* str, size_t pos, size_t count) const; size_t find_first_of(const string_view& str, size_t pos = 0) const;
size_t find_first_of(const char* 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 istring& str, size_t pos = 0) const;
size_t find_first_not_of(const char* str, size_t pos, size_t count) const; size_t find_first_not_of(const string_view& str, size_t pos = 0) const;
size_t find_first_not_of(const char* 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 istring& str, size_t pos = npos) const;
size_t find_last_of(const char* str, size_t pos, size_t count) const; size_t find_last_of(const string_view& str, size_t pos = npos) const;
size_t find_last_of(const char* 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 istring& str, size_t pos = npos) const;
size_t find_last_not_of(const char* str, size_t pos, size_t count) const; size_t find_last_not_of(const string_view& str, size_t pos = npos) const;
size_t find_last_not_of(const char* 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
@ -192,9 +187,9 @@ public:
int32_t compare(const istring& str) const; 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 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(size_t pos1, size_t count1, istring& str, size_t pos2, size_t count2 = npos) const;
int32_t compare(const char* str) const; int32_t compare(const string_view& str) const;
int32_t compare(size_t pos, size_t count, const char* 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 char* str, size_t count2) 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);
@ -203,18 +198,18 @@ public:
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 char* rhs); friend bool operator==(const istring& lhs, const string_view& rhs);
friend bool operator==(const char* lhs, const istring& rhs); friend bool operator==(const string_view& lhs, const istring& rhs);
friend bool operator!=(const istring& lhs, const char* rhs); friend bool operator!=(const istring& lhs, const string_view& rhs);
friend bool operator!=(const char* lhs, const istring& rhs); friend bool operator!=(const string_view& lhs, const istring& rhs);
friend bool operator<(const istring& lhs, const char* rhs); friend bool operator<(const istring& lhs, const string_view& rhs);
friend bool operator<(const char* lhs, const istring& rhs); friend bool operator<(const string_view& lhs, const istring& rhs);
friend bool operator<=(const istring& lhs, const char* rhs); friend bool operator<=(const istring& lhs, const string_view& rhs);
friend bool operator<=(const char* lhs, const istring& rhs); friend bool operator<=(const string_view& lhs, const istring& rhs);
friend bool operator>(const istring& lhs, const char* rhs); friend bool operator>(const istring& lhs, const string_view& rhs);
friend bool operator>(const char* lhs, const istring& rhs); friend bool operator>(const string_view& lhs, const istring& rhs);
friend bool operator>=(const istring& lhs, const char* rhs); friend bool operator>=(const istring& lhs, const string_view& rhs);
friend bool operator>=(const char* lhs, const istring& rhs); friend bool operator>=(const string_view& lhs, const istring& rhs);
}; };
template <size_t C> template <size_t C>
@ -224,8 +219,8 @@ private:
public: public:
string() : istring(m_buf, C, 0) {} string() : istring(m_buf, C, 0) {}
string(const char* str) : istring(m_buf, C, 0) { string(const string_view& str) : istring(m_buf, C, 0) {
m_size = std::strlen(str); m_size = str.length();
if (m_size > C) { m_size = m_capacity; } if (m_size > C) { m_size = m_capacity; }
mtl::memcpy(m_buf, str, m_size); mtl::memcpy(m_buf, str, m_size);

View File

@ -4,6 +4,7 @@
#include <cstring> #include <cstring>
#include "mtl/utility.hpp" #include "mtl/utility.hpp"
#include "mtl/string_view.hpp"
namespace mtl { namespace mtl {
istring::istring(char* _str, size_t _capacity, size_t _size) istring::istring(char* _str, size_t _capacity, size_t _size)
@ -36,22 +37,24 @@ istring& istring::assign(const istring& str, size_t pos, size_t count) {
return *this; return *this;
} }
istring& istring::assign(const char* str, size_t count) { istring& istring::assign(const string_view& str) {
m_size = count; m_size = str.length();
if (m_size > m_capacity) { m_size = m_capacity; } if (m_size > m_capacity) { m_size = m_capacity; }
// str may overlap // str may overlap
std::memmove(m_str, str, m_size); mtl::memcpy(m_str, str.c_str(), m_size);
m_str[m_size] = '\0'; m_str[m_size] = '\0';
return *this; return *this;
} }
istring& istring::assign(const char* str) { istring& istring::assign(const string_view& str, size_t pos, size_t count) {
m_size = std::strlen(str); if (pos + count > str.length()) { m_size = str.length() - pos; }
else { m_size = count; }
if (m_size > m_capacity) { m_size = m_capacity; } if (m_size > m_capacity) { m_size = m_capacity; }
// str may overlap // str may overlap
mtl::memcpy(m_str, str, m_size); mtl::memcpy(m_str, str.c_str() + pos, m_size);
m_str[m_size] = '\0'; m_str[m_size] = '\0';
return *this; return *this;
@ -72,7 +75,7 @@ istring& istring::assign(size_t count, char ch) {
istring& istring::operator=(const istring& rhs) { istring& istring::operator=(const istring& rhs) {
return assign(rhs); return assign(rhs);
} }
istring& istring::operator=(const char* rhs) { istring& istring::operator=(const string_view& rhs) {
return assign(rhs); return assign(rhs);
} }
istring& istring::operator=(char rhs) { istring& istring::operator=(char rhs) {
@ -210,7 +213,6 @@ istring& istring::insert(size_t index, const istring& str) {
return *this; return *this;
} }
string_ext::string_ext(char* buf, size_t buf_size) : istring(buf, buf_size - 1, 0) {} string_ext::string_ext(char* buf, size_t buf_size) : istring(buf, buf_size - 1, 0) {}