[ox/std] Cleanup BasicString

This commit is contained in:
Gary Talent 2022-02-21 02:12:12 -06:00
parent 8a93e44aba
commit eea466750c

View File

@ -100,19 +100,19 @@ class BasicString {
return m_buff.rend(); return m_buff.rend();
} }
BasicString &operator=(const char *str) noexcept; constexpr BasicString &operator=(const char *str) noexcept;
BasicString &operator=(char c) noexcept; constexpr BasicString &operator=(char c) noexcept;
BasicString &operator=(int i) noexcept; constexpr BasicString &operator=(int i) noexcept;
BasicString &operator=(int64_t i) noexcept; constexpr BasicString &operator=(int64_t i) noexcept;
BasicString &operator=(uint64_t i) noexcept; constexpr BasicString &operator=(uint64_t i) noexcept;
BasicString &operator=(const BasicString &src) noexcept; constexpr BasicString &operator=(const BasicString &src) noexcept;
BasicString &operator=(BasicString &&src) noexcept; constexpr BasicString &operator=(BasicString &&src) noexcept;
constexpr BasicString &operator+=(const char *str) noexcept; constexpr BasicString &operator+=(const char *str) noexcept;
@ -128,35 +128,35 @@ class BasicString {
constexpr BasicString &operator+=(const BasicString &src) noexcept; constexpr BasicString &operator+=(const BasicString &src) noexcept;
BasicString operator+(const char *str) const noexcept; constexpr BasicString operator+(const char *str) const noexcept;
BasicString operator+(char *str) const noexcept; constexpr BasicString operator+(char *str) const noexcept;
BasicString operator+(char c) const noexcept; constexpr BasicString operator+(char c) const noexcept;
BasicString operator+(int i) const noexcept; constexpr BasicString operator+(int i) const noexcept;
BasicString operator+(int64_t i) const noexcept; constexpr BasicString operator+(int64_t i) const noexcept;
BasicString operator+(uint64_t i) const noexcept; constexpr BasicString operator+(uint64_t i) const noexcept;
BasicString operator+(const BasicString &src) const noexcept; constexpr BasicString operator+(const BasicString &src) const noexcept;
bool operator==(const BasicString &other) const noexcept; constexpr bool operator==(const BasicString &other) const noexcept;
bool operator!=(const BasicString &other) const noexcept; constexpr bool operator!=(const BasicString &other) const noexcept;
bool operator<(const BasicString &other) const noexcept; constexpr bool operator<(const BasicString &other) const noexcept;
bool operator>(const BasicString &other) const noexcept; constexpr bool operator>(const BasicString &other) const noexcept;
bool operator<=(const BasicString &other) const noexcept; constexpr bool operator<=(const BasicString &other) const noexcept;
bool operator>=(const BasicString &other) const noexcept; constexpr bool operator>=(const BasicString &other) const noexcept;
char operator[](std::size_t i) const noexcept; constexpr char operator[](std::size_t i) const noexcept;
char &operator[](std::size_t i) noexcept; constexpr char &operator[](std::size_t i) noexcept;
constexpr Error append(const char *str, std::size_t strLen) noexcept { constexpr Error append(const char *str, std::size_t strLen) noexcept {
auto currentLen = len(); auto currentLen = len();
@ -169,16 +169,16 @@ class BasicString {
} }
[[nodiscard]] [[nodiscard]]
BasicString substr(std::size_t pos) const noexcept; constexpr BasicString substr(std::size_t pos) const noexcept;
[[nodiscard]] [[nodiscard]]
BasicString substr(std::size_t begin, std::size_t end) const noexcept; constexpr BasicString substr(std::size_t begin, std::size_t end) const noexcept;
[[nodiscard]] [[nodiscard]]
bool endsWith(const char *ending) const noexcept; constexpr bool endsWith(const char *ending) const noexcept;
[[nodiscard]] [[nodiscard]]
bool endsWith(const BasicString &ending) const noexcept; constexpr bool endsWith(const BasicString &ending) const noexcept;
[[nodiscard]] [[nodiscard]]
constexpr const char *data() const noexcept { constexpr const char *data() const noexcept {
@ -212,9 +212,12 @@ class BasicString {
* Returns the number of bytes used for this string. * Returns the number of bytes used for this string.
*/ */
[[nodiscard]] [[nodiscard]]
std::size_t bytes() const noexcept; constexpr std::size_t bytes() const noexcept;
private: private:
template<std::size_t OtherSize>
constexpr void set(const BasicString<OtherSize> &src) noexcept;
constexpr void set(const char *str) noexcept; constexpr void set(const char *str) noexcept;
constexpr void set(const char8_t *str) noexcept; constexpr void set(const char8_t *str) noexcept;
@ -275,48 +278,48 @@ constexpr BasicString<SmallStringSize>::BasicString(BasicString &&other) noexcep
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator=(const char *str) noexcept { constexpr BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator=(const char *str) noexcept {
set(str); set(str);
return *this; return *this;
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator=(char c) noexcept { constexpr BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator=(char c) noexcept {
char str[] = {c, 0}; char str[] = {c, 0};
this->operator=(str); set(str);
return *this; return *this;
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator=(int i) noexcept { constexpr BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator=(int i) noexcept {
this->operator=(static_cast<int64_t>(i)); this->operator=(static_cast<int64_t>(i));
return *this; return *this;
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator=(int64_t i) noexcept { constexpr BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator=(int64_t i) noexcept {
char str[65] = {}; char str[65] = {};
ox_itoa(i, str); ox_itoa(i, str);
this->operator=(str); set(str);
return *this; return *this;
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator=(uint64_t i) noexcept { constexpr BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator=(uint64_t i) noexcept {
char str[65] = {}; char str[65] = {};
ox_itoa(i, str); ox_itoa(i, str);
this->operator=(str); set(str);
return *this; return *this;
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator=(const BasicString &src) noexcept { constexpr BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator=(const BasicString &src) noexcept {
*this = src.c_str(); set(src);
return *this; return *this;
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator=(BasicString &&src) noexcept { constexpr BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator=(BasicString &&src) noexcept {
m_buff = std::move(src.m_buff); m_buff = std::move(src.m_buff);
return *this; return *this;
} }
@ -360,11 +363,12 @@ constexpr BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator+=
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
constexpr BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator+=(const BasicString &src) noexcept { constexpr BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator+=(const BasicString &src) noexcept {
return *this += src.c_str(); oxIgnoreError(append(src.c_str(), src.len()));
return *this;
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
BasicString<SmallStringSize> BasicString<SmallStringSize>::operator+(const char *str) const noexcept { constexpr BasicString<SmallStringSize> BasicString<SmallStringSize>::operator+(const char *str) const noexcept {
const std::size_t strLen = ox_strlen(str); const std::size_t strLen = ox_strlen(str);
const auto currentLen = len(); const auto currentLen = len();
BasicString<SmallStringSize> cpy(currentLen + strLen); BasicString<SmallStringSize> cpy(currentLen + strLen);
@ -377,42 +381,48 @@ BasicString<SmallStringSize> BasicString<SmallStringSize>::operator+(const char
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
BasicString<SmallStringSize> BasicString<SmallStringSize>::operator+(char *str) const noexcept { constexpr BasicString<SmallStringSize> BasicString<SmallStringSize>::operator+(char *str) const noexcept {
return *this + static_cast<const char*>(str); return *this + static_cast<const char*>(str);
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
BasicString<SmallStringSize> BasicString<SmallStringSize>::operator+(char c) const noexcept { constexpr BasicString<SmallStringSize> BasicString<SmallStringSize>::operator+(char c) const noexcept {
const char str[] = {c, 0}; const char str[] = {c, 0};
return *this + str; return *this + str;
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
BasicString<SmallStringSize> BasicString<SmallStringSize>::operator+(int i) const noexcept { constexpr BasicString<SmallStringSize> BasicString<SmallStringSize>::operator+(int i) const noexcept {
return this->operator+(static_cast<int64_t>(i)); return this->operator+(static_cast<int64_t>(i));
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
BasicString<SmallStringSize> BasicString<SmallStringSize>::operator+(int64_t i) const noexcept { constexpr BasicString<SmallStringSize> BasicString<SmallStringSize>::operator+(int64_t i) const noexcept {
char str[65] = {}; char str[65] = {};
ox_itoa(i, str); ox_itoa(i, str);
return *this + str; return *this + str;
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
BasicString<SmallStringSize> BasicString<SmallStringSize>::operator+(uint64_t i) const noexcept { constexpr BasicString<SmallStringSize> BasicString<SmallStringSize>::operator+(uint64_t i) const noexcept {
char str[65] = {}; char str[65] = {};
ox_itoa(i, str); ox_itoa(i, str);
return *this + str; return *this + str;
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
BasicString<SmallStringSize> BasicString<SmallStringSize>::operator+(const BasicString &src) const noexcept { constexpr BasicString<SmallStringSize> BasicString<SmallStringSize>::operator+(const BasicString &src) const noexcept {
return *this + src.c_str(); const std::size_t strLen = src.len();
const auto currentLen = len();
BasicString<SmallStringSize> cpy(currentLen + strLen);
cpy.m_buff.resize(m_buff.size() + strLen);
memcpy(&cpy.m_buff[0], m_buff.data(), currentLen);
memcpy(&cpy.m_buff[currentLen], src.data(), strLen + 1);
return cpy;
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
bool BasicString<SmallStringSize>::operator==(const BasicString &other) const noexcept { constexpr bool BasicString<SmallStringSize>::operator==(const BasicString &other) const noexcept {
bool retval = true; bool retval = true;
std::size_t i = 0; std::size_t i = 0;
while (i < m_buff.size() && (m_buff[i] || other.m_buff[i])) { while (i < m_buff.size() && (m_buff[i] || other.m_buff[i])) {
@ -426,47 +436,47 @@ bool BasicString<SmallStringSize>::operator==(const BasicString &other) const no
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
bool BasicString<SmallStringSize>::operator!=(const BasicString &other) const noexcept { constexpr bool BasicString<SmallStringSize>::operator!=(const BasicString &other) const noexcept {
return !operator==(other); return !operator==(other);
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
bool BasicString<SmallStringSize>::operator<(const BasicString &other) const noexcept { constexpr bool BasicString<SmallStringSize>::operator<(const BasicString &other) const noexcept {
return ox_strcmp(c_str(), other.c_str()) < 0; return ox_strcmp(c_str(), other.c_str()) < 0;
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
bool BasicString<SmallStringSize>::operator>(const BasicString &other) const noexcept { constexpr bool BasicString<SmallStringSize>::operator>(const BasicString &other) const noexcept {
return ox_strcmp(c_str(), other.c_str()) > 0; return ox_strcmp(c_str(), other.c_str()) > 0;
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
bool BasicString<SmallStringSize>::operator<=(const BasicString &other) const noexcept { constexpr bool BasicString<SmallStringSize>::operator<=(const BasicString &other) const noexcept {
return ox_strcmp(c_str(), other.c_str()) < 1; return ox_strcmp(c_str(), other.c_str()) < 1;
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
bool BasicString<SmallStringSize>::operator>=(const BasicString &other) const noexcept { constexpr bool BasicString<SmallStringSize>::operator>=(const BasicString &other) const noexcept {
return ox_strcmp(c_str(), other.c_str()) > -1; return ox_strcmp(c_str(), other.c_str()) > -1;
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
char BasicString<SmallStringSize>::operator[](std::size_t i) const noexcept { constexpr char BasicString<SmallStringSize>::operator[](std::size_t i) const noexcept {
return m_buff[i]; return m_buff[i];
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
char &BasicString<SmallStringSize>::operator[](std::size_t i) noexcept { constexpr char &BasicString<SmallStringSize>::operator[](std::size_t i) noexcept {
return m_buff[i]; return m_buff[i];
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
BasicString<SmallStringSize> BasicString<SmallStringSize>::substr(std::size_t pos) const noexcept { constexpr BasicString<SmallStringSize> BasicString<SmallStringSize>::substr(std::size_t pos) const noexcept {
return m_buff.data() + pos; return m_buff.data() + pos;
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
BasicString<SmallStringSize> BasicString<SmallStringSize>::substr(std::size_t begin, std::size_t end) const noexcept { constexpr BasicString<SmallStringSize> BasicString<SmallStringSize>::substr(std::size_t begin, std::size_t end) const noexcept {
const auto src = m_buff.data() + begin; const auto src = m_buff.data() + begin;
const auto size = end - begin; const auto size = end - begin;
BasicString<SmallStringSize> out(size); BasicString<SmallStringSize> out(size);
@ -477,19 +487,19 @@ BasicString<SmallStringSize> BasicString<SmallStringSize>::substr(std::size_t be
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
bool BasicString<SmallStringSize>::endsWith(const char *ending) const noexcept { constexpr bool BasicString<SmallStringSize>::endsWith(const char *ending) const noexcept {
const auto endingLen = ox_strlen(ending); const auto endingLen = ox_strlen(ending);
return len() >= endingLen && ox_strcmp(data() + (len() - endingLen), ending) == 0; return len() >= endingLen && ox_strcmp(data() + (len() - endingLen), ending) == 0;
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
bool BasicString<SmallStringSize>::endsWith(const BasicString &ending) const noexcept { constexpr bool BasicString<SmallStringSize>::endsWith(const BasicString &ending) const noexcept {
const auto endingLen = ending.len(); const auto endingLen = ending.len();
return len() >= endingLen && ox_strcmp(data() + (len() - endingLen), ending.c_str()) == 0; return len() >= endingLen && ox_strcmp(data() + (len() - endingLen), ending.c_str()) == 0;
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
std::size_t BasicString<SmallStringSize>::bytes() const noexcept { constexpr std::size_t BasicString<SmallStringSize>::bytes() const noexcept {
std::size_t i; std::size_t i;
for (i = 0; i < m_buff.size() && m_buff[i]; i++); for (i = 0; i < m_buff.size() && m_buff[i]; i++);
return i + 1; // add one for null terminator return i + 1; // add one for null terminator
@ -513,22 +523,35 @@ constexpr std::size_t BasicString<SmallStringSize>::len() const noexcept {
return length; return length;
} }
template<std::size_t SmallStringSize>
template<std::size_t OtherSize>
constexpr void BasicString<SmallStringSize>::set(const BasicString<OtherSize> &src) noexcept {
std::size_t strBytes = src.bytes();
if (strBytes > 1) {
m_buff.resize(strBytes);
memcpy(m_buff.data(), src.data(), strBytes);
m_buff.back().value = 0;
}
}
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
constexpr void BasicString<SmallStringSize>::set(const char *str) noexcept { constexpr void BasicString<SmallStringSize>::set(const char *str) noexcept {
std::size_t strLen = ox_strlen(str) + 1; std::size_t strBytes = ox_strlen(str) + 1;
m_buff.resize(strLen + 1); if (strBytes > 1) {
memcpy(m_buff.data(), str, strLen); m_buff.resize(strBytes);
// make sure last element is a null terminator memcpy(m_buff.data(), str, strBytes);
m_buff[m_buff.size() - 1] = 0; m_buff.back().value = 0;
}
} }
template<std::size_t SmallStringSize> template<std::size_t SmallStringSize>
constexpr void BasicString<SmallStringSize>::set(const char8_t *str) noexcept { constexpr void BasicString<SmallStringSize>::set(const char8_t *str) noexcept {
std::size_t strLen = ox_strlen(str) + 1; std::size_t strBytes = ox_strlen(str) + 1;
m_buff.resize(strLen + 1); if (strBytes > 1) {
memcpy(m_buff.data(), str, strLen); m_buff.resize(strBytes);
// make sure last element is a null terminator memcpy(m_buff.data(), str, strBytes);
m_buff[m_buff.size() - 1] = 0; m_buff.back().value = 0;
}
} }
extern template class BasicString<8>; extern template class BasicString<8>;