From 9a31e898d01f1aab3590a9c04cc585a0479a9704 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 7 May 2021 16:04:08 -0500 Subject: [PATCH] [ox/std] Add Error return value to {,B}String::append --- deps/ox/src/ox/std/bstring.hpp | 9 ++++++--- deps/ox/src/ox/std/fmt.hpp | 14 ++++++++++---- deps/ox/src/ox/std/string.cpp | 12 ++++++------ deps/ox/src/ox/std/string.hpp | 4 +++- deps/ox/src/ox/std/trace.hpp | 10 +++++----- deps/ox/src/ox/std/vector.hpp | 2 +- 6 files changed, 31 insertions(+), 20 deletions(-) diff --git a/deps/ox/src/ox/std/bstring.hpp b/deps/ox/src/ox/std/bstring.hpp index 46840f76..69770956 100644 --- a/deps/ox/src/ox/std/bstring.hpp +++ b/deps/ox/src/ox/std/bstring.hpp @@ -45,7 +45,7 @@ class BString { constexpr char &operator[](std::size_t i) noexcept; - void append(const char *str, std::size_t sz) noexcept; + Error append(const char *str, std::size_t sz) noexcept; constexpr char *data() noexcept; @@ -104,7 +104,7 @@ constexpr const BString &BString::operator=(char *str) noexcept { template constexpr const BString &BString::operator+=(const char *str) noexcept { std::size_t strLen = ox_strlen(str) + 1; - append(str, strLen); + oxIgnoreError(append(str, strLen)); return *this; } @@ -150,14 +150,17 @@ constexpr char &BString::operator[](std::size_t i) noexcept { } template -void BString::append(const char *str, std::size_t strLen) noexcept { +Error BString::append(const char *str, std::size_t strLen) noexcept { + Error err; auto currentLen = len(); if (cap() < currentLen + strLen + 1) { strLen = cap() - currentLen; + err = OxError(1, "Insufficient space for full string"); } ox_memcpy(m_buff + currentLen, str, strLen); // make sure last element is a null terminator m_buff[currentLen + strLen] = 0; + return err; } template diff --git a/deps/ox/src/ox/std/fmt.hpp b/deps/ox/src/ox/std/fmt.hpp index 147dfbf8..dabf42fb 100644 --- a/deps/ox/src/ox/std/fmt.hpp +++ b/deps/ox/src/ox/std/fmt.hpp @@ -39,12 +39,18 @@ constexpr const char *stringify(const String &s) noexcept { return s.c_str(); } -#if __has_include() && __cplusplus >= 202002L +#if __has_include() constexpr const char *stringify(const std::string &s) noexcept { return s.c_str(); } #endif +#if __has_include() +constexpr const char *stringify(const QString &s) noexcept { + return s.toUtf8(); +} +#endif + class FmtArg { private: @@ -155,17 +161,17 @@ constexpr Fmt fmtSegments(const char *fmt) noexcept { } template -auto sfmt(const char *fmt, Args... args) noexcept { +StringType sfmt(const char *fmt, Args... args) noexcept { oxAssert(ox::detail::argCount(fmt) == sizeof...(args), "Argument count mismatch."); StringType out; const auto fmtSegments = ox::detail::fmtSegments(fmt); const auto &firstSegment = fmtSegments.segments[0]; - out.append(firstSegment.str, firstSegment.length); + oxIgnoreError(out.append(firstSegment.str, firstSegment.length)); const detail::FmtArg elements[sizeof...(args)] = {args...}; for (auto i = 0u; i < fmtSegments.size - 1; ++i) { out += elements[i].out; const auto &s = fmtSegments.segments[i + 1]; - out.append(s.str, s.length); + oxIgnoreError(out.append(s.str, s.length)); } return move(out); } diff --git a/deps/ox/src/ox/std/string.cpp b/deps/ox/src/ox/std/string.cpp index a3e61d49..0fcbee1f 100644 --- a/deps/ox/src/ox/std/string.cpp +++ b/deps/ox/src/ox/std/string.cpp @@ -86,7 +86,7 @@ String &String::operator=(String &&src) noexcept { String &String::operator+=(const char *str) noexcept { std::size_t strLen = ox_strlen(str); - append(str, strLen); + oxIgnoreError(append(str, strLen)); return *this; } @@ -95,7 +95,7 @@ String &String::operator+=(char *str) noexcept { } String &String::operator+=(char c) noexcept { - char str[] = {c, 0}; + const char str[] = {c, 0}; return this->operator+=(str); } @@ -114,15 +114,15 @@ String &String::operator+=(const String &src) noexcept { } String String::operator+(const char *str) const noexcept { - std::size_t strLen = ox_strlen(str); - auto currentLen = len(); + const std::size_t strLen = ox_strlen(str); + const auto currentLen = len(); String cpy(currentLen + strLen); cpy.m_buff.resize(m_buff.size() + strLen); memcpy(&cpy.m_buff[0], m_buff.data(), currentLen); memcpy(&cpy.m_buff[currentLen], str, strLen); // make sure last element is a null terminator cpy.m_buff[currentLen + strLen] = 0; - return cpy; + return move(cpy); } String String::operator+(char *str) const noexcept { @@ -130,7 +130,7 @@ String String::operator+(char *str) const noexcept { } String String::operator+(char c) const noexcept { - char str[] = {c, 0}; + const char str[] = {c, 0}; return *this + str; } diff --git a/deps/ox/src/ox/std/string.hpp b/deps/ox/src/ox/std/string.hpp index 22701f33..54bdf10f 100644 --- a/deps/ox/src/ox/std/string.hpp +++ b/deps/ox/src/ox/std/string.hpp @@ -83,12 +83,14 @@ class String { char &operator[](std::size_t i) noexcept; - constexpr void append(const char *str, std::size_t strLen) noexcept { + constexpr Error append(const char *str, std::size_t strLen) noexcept { auto currentLen = len(); m_buff.resize(m_buff.size() + strLen); ox_memcpy(&m_buff[currentLen], str, strLen); // make sure last element is a null terminator m_buff[currentLen + strLen] = 0; + // this can't fail, but it returns an Error to match BString::append + return OxError(0); } [[nodiscard]] diff --git a/deps/ox/src/ox/std/trace.hpp b/deps/ox/src/ox/std/trace.hpp index be36007e..cf18e9a8 100644 --- a/deps/ox/src/ox/std/trace.hpp +++ b/deps/ox/src/ox/std/trace.hpp @@ -71,28 +71,28 @@ class OutStream { m_msg.line = line; m_msg.ch = ch; const auto &firstSegment = fmtSegments.segments[0]; - m_msg.msg.append(firstSegment.str, firstSegment.length); + oxIgnoreError(m_msg.msg.append(firstSegment.str, firstSegment.length)); //const detail::FmtArg elements[sizeof...(args)] = {args...}; for (auto i = 0u; i < fmtSegments.size - 1; ++i) { m_msg.msg += elements[i].out; const auto &s = fmtSegments.segments[i + 1]; - m_msg.msg.append(s.str, s.length); + oxIgnoreError(m_msg.msg.append(s.str, s.length)); } } #else template - constexpr OutStream(const char *file, int line, const char *ch, detail::Fmt fmtSegments, Args... args) { + constexpr OutStream(const char *file, int line, const char *ch, detail::Fmt fmtSegments, Args... args) noexcept { //static_assert(sizeof...(args) == fmtSegmentCnt - 1, "Wrong number of trace arguments for format."); m_msg.file = file; m_msg.line = line; m_msg.ch = ch; const auto &firstSegment = fmtSegments.segments[0]; - m_msg.msg.append(firstSegment.str, firstSegment.length); + oxIgnoreError(m_msg.msg.append(firstSegment.str, firstSegment.length)); const detail::FmtArg elements[sizeof...(args)] = {args...}; for (auto i = 0u; i < fmtSegments.size - 1; ++i) { m_msg.msg += elements[i].out; const auto &s = fmtSegments.segments[i + 1]; - m_msg.msg.append(s.str, s.length); + oxIgnoreError(m_msg.msg.append(s.str, s.length)); } } #endif diff --git a/deps/ox/src/ox/std/vector.hpp b/deps/ox/src/ox/std/vector.hpp index a0b7d35e..b396878f 100644 --- a/deps/ox/src/ox/std/vector.hpp +++ b/deps/ox/src/ox/std/vector.hpp @@ -28,7 +28,7 @@ class Vector { T *m_items = nullptr; public: - Vector() noexcept = default; + constexpr Vector() noexcept = default; explicit Vector(std::size_t size) noexcept;