[ox/std] Remove append operators from IString

This is because the append operators cannot report the failure that is possible with IString
This commit is contained in:
Gary Talent 2024-05-02 23:59:19 -05:00
parent 7c4e2a6564
commit d2a3cfa72e
4 changed files with 25 additions and 66 deletions

View File

@ -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);
}

View File

@ -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<size> &IString<size>::operator=(char *str) noexcept {
return *this = static_cast<const char*>(str);
}
template<std::size_t size>
constexpr IString<size> &IString<size>::operator+=(const char *str) noexcept {
std::size_t strLen = ox::strlen(str) + 1;
std::ignore = append(str, strLen);
return *this;
}
template<std::size_t size>
constexpr IString<size> &IString<size>::operator+=(char *str) noexcept {
return *this += static_cast<const char*>(str);
}
template<std::size_t size>
constexpr IString<size> &IString<size>::operator+=(Integer_c auto i) noexcept {
ox::Array<char, 65> str{};
ox::itoa(i, str.data());
return this->operator+=(str.data());
}
template<std::size_t size>
constexpr IString<size> &IString<size>::operator+=(StringView s) noexcept {
std::size_t strLen = s.bytes();
std::ignore = append(s.data(), strLen);
return *this;
}
template<std::size_t size>
constexpr IString<size> IString<size>::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<std::size_t size>
constexpr IString<size> IString<size>::operator+(char *str) const noexcept {
return *this + static_cast<const char*>(str);
}
template<std::size_t size>
constexpr IString<size> IString<size>::operator+(Integer_c auto i) const noexcept {
ox::Array<char, 65> str{};
ox::itoa(i, str.data());
return this->operator+(str.data());
}
template<std::size_t StrCap>
constexpr bool IString<StrCap>::operator==(const char *other) const noexcept {
return ox::StringView(*this) == other;
@ -227,7 +169,22 @@ constexpr char &IString<StrCap>::operator[](std::size_t i) noexcept {
template<std::size_t StrCap>
constexpr Error IString<StrCap>::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<std::size_t StrCap>
constexpr Error IString<StrCap>::append(ox::StringView str) noexcept {
auto strLen = str.len();
Error err{};
auto currentLen = len();
if (cap() < currentLen + strLen + 1) {
strLen = cap() - currentLen;

View File

@ -6,6 +6,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "ox/std/def.hpp"
#undef NDEBUG
#include <map>
@ -69,10 +70,10 @@ static std::map<ox::StringView, ox::Error(*)()> 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");

View File

@ -8,6 +8,7 @@
#pragma once
#include "ignore.hpp"
#include "istring.hpp"
#include "buffer.hpp"
#include "random.hpp"