[ox/std] Make BString::operator== const
This commit is contained in:
parent
6bee2d12d7
commit
84da41ffc9
45
deps/ox/src/ox/std/bstring.hpp
vendored
45
deps/ox/src/ox/std/bstring.hpp
vendored
@ -25,19 +25,19 @@ class BString {
|
||||
|
||||
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;
|
||||
|
||||
@ -45,47 +45,50 @@ class BString {
|
||||
|
||||
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;
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr const char *c_str() const noexcept;
|
||||
|
||||
/**
|
||||
* Returns the number of characters in this string.
|
||||
*/
|
||||
[[nodiscard]]
|
||||
constexpr std::size_t len() const noexcept;
|
||||
|
||||
/**
|
||||
* Returns the number of bytes used for this string.
|
||||
*/
|
||||
[[nodiscard]]
|
||||
constexpr std::size_t bytes() const noexcept;
|
||||
|
||||
/**
|
||||
* Returns the capacity of bytes for this string.
|
||||
*/
|
||||
[[nodiscard]]
|
||||
constexpr std::size_t cap() const noexcept;
|
||||
};
|
||||
|
||||
template<std::size_t size>
|
||||
constexpr BString<size>::BString() noexcept {
|
||||
m_buff[0] = 0;
|
||||
constexpr BString<size>::BString() noexcept: m_buff{{0}} {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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] = {};
|
||||
ox_itoa(i, str);
|
||||
return this->operator=(str);
|
||||
}
|
||||
|
||||
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;
|
||||
if (cap() < strLen) {
|
||||
strLen = cap();
|
||||
@ -97,31 +100,31 @@ constexpr const BString<size> &BString<size>::operator=(const char *str) noexcep
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
oxIgnoreError(append(str, strLen));
|
||||
return *this;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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] = {};
|
||||
ox_itoa(i, str);
|
||||
return this->operator+=(str);
|
||||
}
|
||||
|
||||
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;
|
||||
std::size_t i = 0;
|
||||
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++) {
|
||||
uint8_t b = static_cast<uint8_t>(m_buff[i]);
|
||||
if (b) {
|
||||
if ((b & 128) == 0) { // normal ASCII character
|
||||
length++;
|
||||
} else if ((b & (256 << 6)) == (256 << 6)) { // start of UTF-8 character
|
||||
const auto asciiChar = (b & 128) == 0;
|
||||
const auto utf8Char = (b & (256 << 6)) == (256 << 6);
|
||||
if (asciiChar || utf8Char) {
|
||||
length++;
|
||||
}
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user