From 887d3b3d13418320faa9d72bbf93f8d45f5b5901 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 29 Dec 2022 02:12:11 -0600 Subject: [PATCH] [ox/std] Remove some unnecessary/breaking checks in BasicString::set functions --- deps/ox/src/ox/std/string.hpp | 79 ++++++++++++----------------------- 1 file changed, 26 insertions(+), 53 deletions(-) diff --git a/deps/ox/src/ox/std/string.hpp b/deps/ox/src/ox/std/string.hpp index f8863c3a..620cb6e6 100644 --- a/deps/ox/src/ox/std/string.hpp +++ b/deps/ox/src/ox/std/string.hpp @@ -179,16 +179,10 @@ class BasicString { constexpr BasicString substr(std::size_t begin, std::size_t end) const noexcept; [[nodiscard]] - constexpr bool beginsWith(const char *ending) const noexcept; + constexpr bool beginsWith(CRStringView ending) const noexcept; [[nodiscard]] - constexpr bool beginsWith(const BasicString &ending) const noexcept; - - [[nodiscard]] - constexpr bool endsWith(const char *ending) const noexcept; - - [[nodiscard]] - constexpr bool endsWith(const BasicString &ending) const noexcept; + constexpr bool endsWith(CRStringView ending) const noexcept; [[nodiscard]] constexpr const char *data() const noexcept { @@ -238,7 +232,7 @@ class BasicString { template constexpr void set(const BasicString &src) noexcept; - constexpr void set(StringView str) noexcept; + constexpr void set(CRStringView str) noexcept; constexpr void set(const char *str) noexcept; @@ -291,10 +285,10 @@ constexpr BasicString::BasicString(const char *str, std::size template constexpr BasicString::BasicString(CRStringView str) noexcept { - if (m_buff.size()) { - m_buff[0] = 0; - } else { + if (m_buff.empty()) { m_buff.push_back(0); + } else { + m_buff[0] = 0; } set(str); } @@ -521,27 +515,15 @@ constexpr BasicString BasicString::substr( } template -constexpr bool BasicString::beginsWith(const char *beginning) const noexcept { - const auto beginningLen = ox::min(ox_strlen(beginning), len()); +constexpr bool BasicString::beginsWith(CRStringView beginning) const noexcept { + const auto beginningLen = ox::min(beginning.len(), len()); return ox_strncmp(data(), beginning, beginningLen) == 0; } template -constexpr bool BasicString::beginsWith(const BasicString &beginning) const noexcept { - const auto sz = ox::min(beginning.len(), len());; - return ox_strncmp(data(), beginning.c_str(), sz) == 0; -} - -template -constexpr bool BasicString::endsWith(const char *ending) const noexcept { - const auto endingLen = ox_strlen(ending); - return len() >= endingLen && ox_strcmp(data() + (len() - endingLen), ending) == 0; -} - -template -constexpr bool BasicString::endsWith(const BasicString &ending) const noexcept { +constexpr bool BasicString::endsWith(CRStringView ending) const noexcept { 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) == 0; } template @@ -555,11 +537,10 @@ template constexpr std::size_t BasicString::len() const noexcept { std::size_t length = 0; for (const auto c : m_buff) { - auto b = static_cast(c); + const auto b = static_cast(c); if (b) { - if ((b & 128) == 0) { // normal ASCII character - ++length; - } else if ((b & (256 << 6)) == (256 << 6)) { // start of UTF-8 character + // normal ASCII character or start of UTF-8 character + if ((b & 128) == 0 || (b & (256 << 6)) == (256 << 6)) { ++length; } } else { @@ -573,41 +554,33 @@ template template constexpr void BasicString::set(const BasicString &src) noexcept { std::size_t strBytes = src.bytes(); - if (strBytes > 1) { - m_buff.resize(strBytes); - copy_n(src.begin(), strBytes, m_buff.data()); - m_buff.back().value = 0; - } + m_buff.resize(strBytes); + copy_n(src.begin(), strBytes, m_buff.data()); + m_buff.back().value = 0; } template -constexpr void BasicString::set(StringView str) noexcept { +constexpr void BasicString::set(CRStringView str) noexcept { std::size_t strBytes = str.bytes(); - if (strBytes > 1) { - m_buff.resize(strBytes + 1); - copy_n(str.data(), strBytes, m_buff.data()); - m_buff.back().value = 0; - } + m_buff.resize(strBytes + 1); + copy_n(str.data(), strBytes, m_buff.data()); + m_buff.back().value = 0; } template constexpr void BasicString::set(const char *str) noexcept { std::size_t strBytes = ox_strlen(str) + 1; - if (strBytes > 1) { - m_buff.resize(strBytes); - copy_n(str, strBytes, m_buff.data()); - m_buff.back().value = 0; - } + m_buff.resize(strBytes); + copy_n(str, strBytes, m_buff.data()); + m_buff.back().value = 0; } template constexpr void BasicString::set(const char8_t *str) noexcept { 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; - } + m_buff.resize(strBytes); + memcpy(m_buff.data(), str, strBytes); + m_buff.back().value = 0; } extern template class BasicString<8>;