From 95a69b72b535ea4b619a8a0504a94333dd276da1 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 29 May 2024 20:51:15 -0500 Subject: [PATCH] [ox/std] Fix String::c_str to always retrun a valid C str --- deps/ox/src/ox/std/string.cpp | 1 + deps/ox/src/ox/std/string.hpp | 31 ++++++++++--------------------- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/deps/ox/src/ox/std/string.cpp b/deps/ox/src/ox/std/string.cpp index dca59e06..a5c63ede 100644 --- a/deps/ox/src/ox/std/string.cpp +++ b/deps/ox/src/ox/std/string.cpp @@ -13,6 +13,7 @@ namespace ox { template class BasicString<8>; static_assert(StringView("Write") != String("")); +static_assert(ox::strcmp(String{}.c_str(), "\0") == 0); static_assert(String("Write") != StringView("")); static_assert(String("Write") == StringView("Write")); static_assert(String(StringView("Write")) == StringView("Write")); diff --git a/deps/ox/src/ox/std/string.hpp b/deps/ox/src/ox/std/string.hpp index e05e3e14..324e243f 100644 --- a/deps/ox/src/ox/std/string.hpp +++ b/deps/ox/src/ox/std/string.hpp @@ -30,7 +30,7 @@ constexpr ox::IString<21> itoa(Integer v) noexcept; template class BasicString { private: - Vector m_buff{1}; + Vector m_buff; public: static constexpr auto SmallStringSize = SmallStringSize_v; @@ -193,6 +193,7 @@ class BasicString { constexpr BasicString substr(std::size_t begin, std::size_t end) const noexcept; constexpr void resize(size_t sz) noexcept { + ++sz; m_buff.resize(sz); m_buff[sz - 1] = 0; } @@ -248,28 +249,20 @@ class BasicString { }; template -constexpr BasicString::BasicString() noexcept = default; +constexpr BasicString::BasicString() noexcept { + m_buff.resize(1); +} template constexpr BasicString::BasicString(std::size_t cap) noexcept: m_buff(cap + 1) {} template constexpr BasicString::BasicString(const char *str) noexcept { - if (!m_buff.empty()) { - m_buff[0] = 0; - } else { - m_buff.push_back(0); - } set(str); } template constexpr BasicString::BasicString(const char8_t *str) noexcept { - if (!m_buff.empty()) { - m_buff[0] = 0; - } else { - m_buff.push_back(0); - } set(str); } @@ -277,21 +270,15 @@ template constexpr BasicString::BasicString(const char *str, std::size_t size) noexcept { m_buff.resize(size + 1); ox::listcpy(m_buff.data(), str, size); - m_buff[size] = 0; } template constexpr BasicString::BasicString(StringLiteral const&str) noexcept: - BasicString(StringView{str.data(), str.bytes()}) { + BasicString(StringView{str.data(), str.len()}) { } template constexpr BasicString::BasicString(CRStringView str) noexcept { - if (m_buff.empty()) { - m_buff.push_back(0); - } else { - m_buff[0] = 0; - } set(str); } @@ -302,7 +289,8 @@ constexpr BasicString::BasicString(const BasicString &other) template constexpr BasicString::BasicString(BasicString &&other) noexcept: m_buff(std::move(other.m_buff)) { - other.m_buff.push_back(0); + other.m_buff.resize(1); + other.m_buff[0] = 0; } template @@ -348,7 +336,8 @@ template constexpr BasicString &BasicString::operator=(BasicString &&src) noexcept { if (this != &src) { m_buff = std::move(src.m_buff); - src.m_buff.push_back(0); + src.m_buff.resize(1); + src.m_buff[0] = 0; } return *this; }