[ox/std] Add Error return value to {,B}String::append

This commit is contained in:
Gary Talent 2021-05-07 16:04:08 -05:00
parent 800e04b3b9
commit 9a31e898d0
6 changed files with 31 additions and 20 deletions

View File

@ -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<size> &BString<size>::operator=(char *str) noexcept {
template<std::size_t size>
constexpr const BString<size> &BString<size>::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<buffLen>::operator[](std::size_t i) noexcept {
}
template<std::size_t buffLen>
void BString<buffLen>::append(const char *str, std::size_t strLen) noexcept {
Error BString<buffLen>::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<std::size_t buffLen>

View File

@ -39,12 +39,18 @@ constexpr const char *stringify(const String &s) noexcept {
return s.c_str();
}
#if __has_include(<string>) && __cplusplus >= 202002L
#if __has_include(<string>)
constexpr const char *stringify(const std::string &s) noexcept {
return s.c_str();
}
#endif
#if __has_include(<QString>)
constexpr const char *stringify(const QString &s) noexcept {
return s.toUtf8();
}
#endif
class FmtArg {
private:
@ -155,17 +161,17 @@ constexpr Fmt<segementCnt> fmtSegments(const char *fmt) noexcept {
}
template<typename StringType = String, typename ...Args>
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<sizeof...(args)+1>(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);
}

View File

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

View File

@ -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]]

View File

@ -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<std::size_t fmtSegmentCnt, typename ...Args>
constexpr OutStream(const char *file, int line, const char *ch, detail::Fmt<fmtSegmentCnt> fmtSegments, Args... args) {
constexpr OutStream(const char *file, int line, const char *ch, detail::Fmt<fmtSegmentCnt> 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

View File

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