[ox/std] Make BString::operator== const

This commit is contained in:
Gary Talent 2021-11-08 02:23:32 -06:00
parent 6bee2d12d7
commit 84da41ffc9

View File

@ -25,19 +25,19 @@ class BString {
constexpr BString(const char *str) noexcept; constexpr BString(const char *str) noexcept;
constexpr const BString &operator=(const char *str) noexcept; constexpr BString &operator=(const char *str) noexcept;
constexpr const BString &operator=(char *str) noexcept; constexpr BString &operator=(char *str) noexcept;
constexpr const BString &operator=(int64_t i) noexcept; constexpr BString &operator=(int64_t i) noexcept;
constexpr const BString &operator+=(const char *str) noexcept; constexpr BString &operator+=(const char *str) noexcept;
constexpr const BString &operator+=(char *str) noexcept; constexpr BString &operator+=(char *str) noexcept;
constexpr const BString &operator+=(int64_t i) noexcept; constexpr BString &operator+=(int64_t i) noexcept;
constexpr bool operator==(const BString &other) noexcept; constexpr bool operator==(const BString &other) const noexcept;
constexpr bool operator!=(const BString &other) noexcept; constexpr bool operator!=(const BString &other) noexcept;
@ -45,47 +45,50 @@ class BString {
constexpr char &operator[](std::size_t i) noexcept; constexpr char &operator[](std::size_t i) noexcept;
Error append(const char *str, std::size_t sz) noexcept; Error append(const char *str, std::size_t strLen) noexcept;
constexpr char *data() noexcept; constexpr char *data() noexcept;
[[nodiscard]]
constexpr const char *c_str() const noexcept; constexpr const char *c_str() const noexcept;
/** /**
* Returns the number of characters in this string. * Returns the number of characters in this string.
*/ */
[[nodiscard]]
constexpr std::size_t len() const noexcept; constexpr std::size_t len() const noexcept;
/** /**
* Returns the number of bytes used for this string. * Returns the number of bytes used for this string.
*/ */
[[nodiscard]]
constexpr std::size_t bytes() const noexcept; constexpr std::size_t bytes() const noexcept;
/** /**
* Returns the capacity of bytes for this string. * Returns the capacity of bytes for this string.
*/ */
[[nodiscard]]
constexpr std::size_t cap() const noexcept; constexpr std::size_t cap() const noexcept;
}; };
template<std::size_t size> template<std::size_t size>
constexpr BString<size>::BString() noexcept { constexpr BString<size>::BString() noexcept: m_buff{{0}} {
m_buff[0] = 0;
} }
template<std::size_t size> template<std::size_t size>
constexpr BString<size>::BString(const char *str) noexcept { constexpr BString<size>::BString(const char *str) noexcept: m_buff{{0}} {
*this = str; *this = str;
} }
template<std::size_t size> template<std::size_t size>
constexpr const BString<size> &BString<size>::operator=(int64_t i) noexcept { constexpr BString<size> &BString<size>::operator=(int64_t i) noexcept {
char str[65] = {}; char str[65] = {};
ox_itoa(i, str); ox_itoa(i, str);
return this->operator=(str); return this->operator=(str);
} }
template<std::size_t size> template<std::size_t size>
constexpr const BString<size> &BString<size>::operator=(const char *str) noexcept { constexpr BString<size> &BString<size>::operator=(const char *str) noexcept {
std::size_t strLen = ox_strlen(str) + 1; std::size_t strLen = ox_strlen(str) + 1;
if (cap() < strLen) { if (cap() < strLen) {
strLen = cap(); strLen = cap();
@ -97,31 +100,31 @@ constexpr const BString<size> &BString<size>::operator=(const char *str) noexcep
} }
template<std::size_t size> template<std::size_t size>
constexpr const BString<size> &BString<size>::operator=(char *str) noexcept { constexpr BString<size> &BString<size>::operator=(char *str) noexcept {
return *this = static_cast<const char*>(str); return *this = static_cast<const char*>(str);
} }
template<std::size_t size> template<std::size_t size>
constexpr const BString<size> &BString<size>::operator+=(const char *str) noexcept { constexpr BString<size> &BString<size>::operator+=(const char *str) noexcept {
std::size_t strLen = ox_strlen(str) + 1; std::size_t strLen = ox_strlen(str) + 1;
oxIgnoreError(append(str, strLen)); oxIgnoreError(append(str, strLen));
return *this; return *this;
} }
template<std::size_t size> template<std::size_t size>
constexpr const BString<size> &BString<size>::operator+=(char *str) noexcept { constexpr BString<size> &BString<size>::operator+=(char *str) noexcept {
return *this += static_cast<const char*>(str); return *this += static_cast<const char*>(str);
} }
template<std::size_t size> template<std::size_t size>
constexpr const BString<size> &BString<size>::operator+=(int64_t i) noexcept { constexpr BString<size> &BString<size>::operator+=(int64_t i) noexcept {
char str[65] = {}; char str[65] = {};
ox_itoa(i, str); ox_itoa(i, str);
return this->operator+=(str); return this->operator+=(str);
} }
template<std::size_t buffLen> template<std::size_t buffLen>
constexpr bool BString<buffLen>::operator==(const BString<buffLen> &other) noexcept { constexpr bool BString<buffLen>::operator==(const BString<buffLen> &other) const noexcept {
bool retval = true; bool retval = true;
std::size_t i = 0; std::size_t i = 0;
while (i < buffLen && (m_buff[i] || other.m_buff[i])) { while (i < buffLen && (m_buff[i] || other.m_buff[i])) {
@ -180,9 +183,9 @@ constexpr std::size_t BString<buffLen>::len() const noexcept {
for (std::size_t i = 0; i < buffLen; i++) { for (std::size_t i = 0; i < buffLen; i++) {
uint8_t b = static_cast<uint8_t>(m_buff[i]); uint8_t b = static_cast<uint8_t>(m_buff[i]);
if (b) { if (b) {
if ((b & 128) == 0) { // normal ASCII character const auto asciiChar = (b & 128) == 0;
length++; const auto utf8Char = (b & (256 << 6)) == (256 << 6);
} else if ((b & (256 << 6)) == (256 << 6)) { // start of UTF-8 character if (asciiChar || utf8Char) {
length++; length++;
} }
} else { } else {