[ox/std] Cleanup BasicString
This commit is contained in:
parent
8a93e44aba
commit
eea466750c
163
deps/ox/src/ox/std/string.hpp
vendored
163
deps/ox/src/ox/std/string.hpp
vendored
@ -100,19 +100,19 @@ class BasicString {
|
||||
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;
|
||||
|
||||
@ -128,35 +128,35 @@ class BasicString {
|
||||
|
||||
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 {
|
||||
auto currentLen = len();
|
||||
@ -169,16 +169,16 @@ class BasicString {
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
BasicString substr(std::size_t pos) const noexcept;
|
||||
constexpr BasicString substr(std::size_t pos) const noexcept;
|
||||
|
||||
[[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]]
|
||||
bool endsWith(const char *ending) const noexcept;
|
||||
constexpr bool endsWith(const char *ending) const noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
bool endsWith(const BasicString &ending) const noexcept;
|
||||
constexpr bool endsWith(const BasicString &ending) const noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr const char *data() const noexcept {
|
||||
@ -212,9 +212,12 @@ class BasicString {
|
||||
* Returns the number of bytes used for this string.
|
||||
*/
|
||||
[[nodiscard]]
|
||||
std::size_t bytes() const noexcept;
|
||||
constexpr std::size_t bytes() const noexcept;
|
||||
|
||||
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 char8_t *str) noexcept;
|
||||
@ -275,48 +278,48 @@ constexpr BasicString<SmallStringSize>::BasicString(BasicString &&other) noexcep
|
||||
}
|
||||
|
||||
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);
|
||||
return *this;
|
||||
}
|
||||
|
||||
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};
|
||||
this->operator=(str);
|
||||
set(str);
|
||||
return *this;
|
||||
}
|
||||
|
||||
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));
|
||||
return *this;
|
||||
}
|
||||
|
||||
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] = {};
|
||||
ox_itoa(i, str);
|
||||
this->operator=(str);
|
||||
set(str);
|
||||
return *this;
|
||||
}
|
||||
|
||||
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] = {};
|
||||
ox_itoa(i, str);
|
||||
this->operator=(str);
|
||||
set(str);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<std::size_t SmallStringSize>
|
||||
BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator=(const BasicString &src) noexcept {
|
||||
*this = src.c_str();
|
||||
constexpr BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator=(const BasicString &src) noexcept {
|
||||
set(src);
|
||||
return *this;
|
||||
}
|
||||
|
||||
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);
|
||||
return *this;
|
||||
}
|
||||
@ -360,11 +363,12 @@ constexpr BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator+=
|
||||
|
||||
template<std::size_t SmallStringSize>
|
||||
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>
|
||||
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 auto currentLen = len();
|
||||
BasicString<SmallStringSize> cpy(currentLen + strLen);
|
||||
@ -377,42 +381,48 @@ BasicString<SmallStringSize> BasicString<SmallStringSize>::operator+(const char
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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};
|
||||
return *this + str;
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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] = {};
|
||||
ox_itoa(i, str);
|
||||
return *this + str;
|
||||
}
|
||||
|
||||
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] = {};
|
||||
ox_itoa(i, str);
|
||||
return *this + str;
|
||||
}
|
||||
|
||||
template<std::size_t SmallStringSize>
|
||||
BasicString<SmallStringSize> BasicString<SmallStringSize>::operator+(const BasicString &src) const noexcept {
|
||||
return *this + src.c_str();
|
||||
constexpr BasicString<SmallStringSize> BasicString<SmallStringSize>::operator+(const BasicString &src) const noexcept {
|
||||
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>
|
||||
bool BasicString<SmallStringSize>::operator==(const BasicString &other) const noexcept {
|
||||
constexpr bool BasicString<SmallStringSize>::operator==(const BasicString &other) const noexcept {
|
||||
bool retval = true;
|
||||
std::size_t i = 0;
|
||||
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>
|
||||
bool BasicString<SmallStringSize>::operator!=(const BasicString &other) const noexcept {
|
||||
constexpr bool BasicString<SmallStringSize>::operator!=(const BasicString &other) const noexcept {
|
||||
return !operator==(other);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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 size = end - begin;
|
||||
BasicString<SmallStringSize> out(size);
|
||||
@ -477,19 +487,19 @@ BasicString<SmallStringSize> BasicString<SmallStringSize>::substr(std::size_t be
|
||||
}
|
||||
|
||||
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);
|
||||
return len() >= endingLen && ox_strcmp(data() + (len() - endingLen), ending) == 0;
|
||||
}
|
||||
|
||||
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();
|
||||
return len() >= endingLen && ox_strcmp(data() + (len() - endingLen), ending.c_str()) == 0;
|
||||
}
|
||||
|
||||
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;
|
||||
for (i = 0; i < m_buff.size() && m_buff[i]; i++);
|
||||
return i + 1; // add one for null terminator
|
||||
@ -513,22 +523,35 @@ constexpr std::size_t BasicString<SmallStringSize>::len() const noexcept {
|
||||
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>
|
||||
constexpr void BasicString<SmallStringSize>::set(const char *str) noexcept {
|
||||
std::size_t strLen = ox_strlen(str) + 1;
|
||||
m_buff.resize(strLen + 1);
|
||||
memcpy(m_buff.data(), str, strLen);
|
||||
// make sure last element is a null terminator
|
||||
m_buff[m_buff.size() - 1] = 0;
|
||||
std::size_t strBytes = ox_strlen(str) + 1;
|
||||
if (strBytes > 1) {
|
||||
m_buff.resize(strBytes);
|
||||
memcpy(m_buff.data(), str, strBytes);
|
||||
m_buff.back().value = 0;
|
||||
}
|
||||
}
|
||||
|
||||
template<std::size_t SmallStringSize>
|
||||
constexpr void BasicString<SmallStringSize>::set(const char8_t *str) noexcept {
|
||||
std::size_t strLen = ox_strlen(str) + 1;
|
||||
m_buff.resize(strLen + 1);
|
||||
memcpy(m_buff.data(), str, strLen);
|
||||
// make sure last element is a null terminator
|
||||
m_buff[m_buff.size() - 1] = 0;
|
||||
std::size_t strBytes = ox_strlen(str) + 1;
|
||||
if (strBytes > 1) {
|
||||
m_buff.resize(strBytes);
|
||||
memcpy(m_buff.data(), str, strBytes);
|
||||
m_buff.back().value = 0;
|
||||
}
|
||||
}
|
||||
|
||||
extern template class BasicString<8>;
|
||||
|
Loading…
x
Reference in New Issue
Block a user