From 84da41ffc9e4a3bd504b1e0c208f88a9c630c13d Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Mon, 8 Nov 2021 02:23:32 -0600 Subject: [PATCH] [ox/std] Make BString::operator== const --- deps/ox/src/ox/std/bstring.hpp | 45 ++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/deps/ox/src/ox/std/bstring.hpp b/deps/ox/src/ox/std/bstring.hpp index 69770956..bc64976c 100644 --- a/deps/ox/src/ox/std/bstring.hpp +++ b/deps/ox/src/ox/std/bstring.hpp @@ -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 -constexpr BString::BString() noexcept { - m_buff[0] = 0; +constexpr BString::BString() noexcept: m_buff{{0}} { } template -constexpr BString::BString(const char *str) noexcept { +constexpr BString::BString(const char *str) noexcept: m_buff{{0}} { *this = str; } template -constexpr const BString &BString::operator=(int64_t i) noexcept { +constexpr BString &BString::operator=(int64_t i) noexcept { char str[65] = {}; ox_itoa(i, str); return this->operator=(str); } template -constexpr const BString &BString::operator=(const char *str) noexcept { +constexpr BString &BString::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 &BString::operator=(const char *str) noexcep } template -constexpr const BString &BString::operator=(char *str) noexcept { +constexpr BString &BString::operator=(char *str) noexcept { return *this = static_cast(str); } template -constexpr const BString &BString::operator+=(const char *str) noexcept { +constexpr BString &BString::operator+=(const char *str) noexcept { std::size_t strLen = ox_strlen(str) + 1; oxIgnoreError(append(str, strLen)); return *this; } template -constexpr const BString &BString::operator+=(char *str) noexcept { +constexpr BString &BString::operator+=(char *str) noexcept { return *this += static_cast(str); } template -constexpr const BString &BString::operator+=(int64_t i) noexcept { +constexpr BString &BString::operator+=(int64_t i) noexcept { char str[65] = {}; ox_itoa(i, str); return this->operator+=(str); } template -constexpr bool BString::operator==(const BString &other) noexcept { +constexpr bool BString::operator==(const BString &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::len() const noexcept { for (std::size_t i = 0; i < buffLen; i++) { uint8_t b = static_cast(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 {