From d2a3cfa72e8a6e1ccd4ec0c5560019322af14e4f Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 2 May 2024 23:59:19 -0500 Subject: [PATCH] [ox/std] Remove append operators from IString This is because the append operators cannot report the failure that is possible with IString --- deps/ox/src/ox/std/fmt.hpp | 2 +- deps/ox/src/ox/std/istring.hpp | 79 +++++++------------------------ deps/ox/src/ox/std/test/tests.cpp | 9 ++-- deps/ox/src/ox/std/uuid.hpp | 1 + 4 files changed, 25 insertions(+), 66 deletions(-) diff --git a/deps/ox/src/ox/std/fmt.hpp b/deps/ox/src/ox/std/fmt.hpp index 21493375..2bc21a89 100644 --- a/deps/ox/src/ox/std/fmt.hpp +++ b/deps/ox/src/ox/std/fmt.hpp @@ -195,7 +195,7 @@ constexpr StringType sfmt(StringView fmt, Args&&... args) noexcept { std::ignore = out.append(firstSegment.str, firstSegment.length); const detail::FmtArg elements[sizeof...(args)] = {args...}; for (size_t i = 0; i < fmtSegments.size - 1; ++i) { - out += elements[i].out; + std::ignore = out.append(elements[i].out.data(), elements[i].out.len()); const auto &s = fmtSegments.segments[i + 1]; std::ignore = out.append(s.str, s.length); } diff --git a/deps/ox/src/ox/std/istring.hpp b/deps/ox/src/ox/std/istring.hpp index 0e2f55de..37be9cd6 100644 --- a/deps/ox/src/ox/std/istring.hpp +++ b/deps/ox/src/ox/std/istring.hpp @@ -9,7 +9,6 @@ #pragma once #include "array.hpp" -#include "ignore.hpp" #include "memops.hpp" #include "ox/std/error.hpp" #include "stringview.hpp" @@ -39,19 +38,6 @@ class IString { constexpr IString &operator=(Integer_c auto i) noexcept; - constexpr IString &operator+=(const char *str) noexcept; - - constexpr IString &operator+=(char *str) noexcept; - - constexpr IString &operator+=(Integer_c auto i) noexcept; - - constexpr IString &operator+=(StringView s) noexcept; - - constexpr IString operator+(const char *str) const noexcept; - - constexpr IString operator+(char *str) const noexcept; - - constexpr IString operator+(Integer_c auto i) const noexcept; constexpr bool operator==(const char *other) const noexcept; @@ -67,6 +53,8 @@ class IString { constexpr Error append(const char *str, std::size_t strLen) noexcept; + constexpr Error append(ox::StringView str) noexcept; + [[nodiscard]] constexpr const char *data() const noexcept; @@ -149,52 +137,6 @@ constexpr IString &IString::operator=(char *str) noexcept { return *this = static_cast(str); } -template -constexpr IString &IString::operator+=(const char *str) noexcept { - std::size_t strLen = ox::strlen(str) + 1; - std::ignore = append(str, strLen); - return *this; -} - -template -constexpr IString &IString::operator+=(char *str) noexcept { - return *this += static_cast(str); -} - -template -constexpr IString &IString::operator+=(Integer_c auto i) noexcept { - ox::Array str{}; - ox::itoa(i, str.data()); - return this->operator+=(str.data()); -} - -template -constexpr IString &IString::operator+=(StringView s) noexcept { - std::size_t strLen = s.bytes(); - std::ignore = append(s.data(), strLen); - return *this; -} - -template -constexpr IString IString::operator+(const char *str) const noexcept { - auto out = *this; - std::size_t strLen = ox::strlen(str) + 1; - std::ignore = out.append(str, strLen); - return out; -} - -template -constexpr IString IString::operator+(char *str) const noexcept { - return *this + static_cast(str); -} - -template -constexpr IString IString::operator+(Integer_c auto i) const noexcept { - ox::Array str{}; - ox::itoa(i, str.data()); - return this->operator+(str.data()); -} - template constexpr bool IString::operator==(const char *other) const noexcept { return ox::StringView(*this) == other; @@ -227,7 +169,22 @@ constexpr char &IString::operator[](std::size_t i) noexcept { template constexpr Error IString::append(const char *str, std::size_t strLen) noexcept { - Error err; + Error err{}; + auto currentLen = len(); + if (cap() < currentLen + strLen + 1) { + strLen = cap() - currentLen; + err = OxError(1, "Insufficient space for full string"); + } + ox::strncpy(m_buff.data() + currentLen, str, strLen); + // make sure last element is a null terminator + m_buff[currentLen + strLen] = 0; + return err; +} + +template +constexpr Error IString::append(ox::StringView str) noexcept { + auto strLen = str.len(); + Error err{}; auto currentLen = len(); if (cap() < currentLen + strLen + 1) { strLen = cap() - currentLen; diff --git a/deps/ox/src/ox/std/test/tests.cpp b/deps/ox/src/ox/std/test/tests.cpp index 04a73639..1af6c8f7 100644 --- a/deps/ox/src/ox/std/test/tests.cpp +++ b/deps/ox/src/ox/std/test/tests.cpp @@ -6,6 +6,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +#include "ox/std/def.hpp" #undef NDEBUG #include @@ -69,10 +70,10 @@ static std::map tests = { "BString", []() { ox::IString<5> s; - s += "A"; - s += "B"; - s += 9; - s += "C"; + oxReturnError(s.append("A")); + oxReturnError(s.append("B")); + oxReturnError(s.append("9")); + oxReturnError(s.append("C")); oxAssert(s == "AB9C", "BString append broken"); s = "asdf"; oxAssert(s == "asdf", "String assign broken"); diff --git a/deps/ox/src/ox/std/uuid.hpp b/deps/ox/src/ox/std/uuid.hpp index f4dbdbe9..19e535f7 100644 --- a/deps/ox/src/ox/std/uuid.hpp +++ b/deps/ox/src/ox/std/uuid.hpp @@ -8,6 +8,7 @@ #pragma once +#include "ignore.hpp" #include "istring.hpp" #include "buffer.hpp" #include "random.hpp"