From 7163947efd697788b254d8a25b0652f36728c685 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 2 May 2024 23:14:03 -0500 Subject: [PATCH] [ox/std] Cleanup --- deps/ox/src/ox/std/istring.hpp | 75 +++++++++++++++++++--------------- deps/ox/src/ox/std/uuid.hpp | 5 ++- 2 files changed, 46 insertions(+), 34 deletions(-) diff --git a/deps/ox/src/ox/std/istring.hpp b/deps/ox/src/ox/std/istring.hpp index b7eef79a..5c97cf49 100644 --- a/deps/ox/src/ox/std/istring.hpp +++ b/deps/ox/src/ox/std/istring.hpp @@ -10,17 +10,17 @@ #include "ignore.hpp" #include "memops.hpp" +#include "ox/std/error.hpp" #include "stringview.hpp" -#include "strops.hpp" #include "typetraits.hpp" namespace ox { // Inline String -template +template class IString { private: - char m_buff[buffLen + 1]; + char m_buff[StrCap + 1]; size_t m_size{}; public: @@ -87,6 +87,8 @@ class IString { [[nodiscard]] constexpr std::size_t bytes() const noexcept; + constexpr ox::Error resize(size_t sz) noexcept; + /** * Returns the capacity of bytes for this string. */ @@ -192,38 +194,38 @@ constexpr IString IString::operator+(Integer_c auto i) const noexcep return this->operator+(str); } -template -constexpr bool IString::operator==(const char *other) const noexcept { +template +constexpr bool IString::operator==(const char *other) const noexcept { return ox::StringView(*this) == other; } -template -constexpr bool IString::operator==(const OxString_c auto &other) const noexcept { +template +constexpr bool IString::operator==(const OxString_c auto &other) const noexcept { return ox::StringView(*this) == ox::StringView(other); } -template -constexpr bool IString::operator!=(const char *other) const noexcept { +template +constexpr bool IString::operator!=(const char *other) const noexcept { return !operator==(other); } -template -constexpr bool IString::operator!=(const OxString_c auto &other) noexcept { +template +constexpr bool IString::operator!=(const OxString_c auto &other) noexcept { return !operator==(other); } -template -constexpr char IString::operator[](std::size_t i) const noexcept { +template +constexpr char IString::operator[](std::size_t i) const noexcept { return m_buff[i]; } -template -constexpr char &IString::operator[](std::size_t i) noexcept { +template +constexpr char &IString::operator[](std::size_t i) noexcept { return m_buff[i]; } -template -constexpr Error IString::append(const char *str, std::size_t strLen) noexcept { +template +constexpr Error IString::append(const char *str, std::size_t strLen) noexcept { Error err; auto currentLen = len(); if (cap() < currentLen + strLen + 1) { @@ -236,26 +238,26 @@ constexpr Error IString::append(const char *str, std::size_t strLen) no return err; } -template -constexpr const char *IString::data() const noexcept { +template +constexpr const char *IString::data() const noexcept { return static_cast(m_buff); } -template -constexpr char *IString::data() noexcept { +template +constexpr char *IString::data() noexcept { return static_cast(m_buff); } -template -constexpr const char *IString::c_str() const noexcept { +template +constexpr const char *IString::c_str() const noexcept { return static_cast(m_buff); } -template -constexpr std::size_t IString::len() const noexcept { +template +constexpr std::size_t IString::len() const noexcept { std::size_t length = 0; - for (std::size_t i = 0; i < buffLen; i++) { + for (std::size_t i = 0; i < StrCap; i++) { uint8_t b = static_cast(m_buff[i]); if (b) { const auto asciiChar = (b & 128) == 0; @@ -270,16 +272,25 @@ constexpr std::size_t IString::len() const noexcept { return length; } -template -constexpr std::size_t IString::bytes() const noexcept { +template +constexpr std::size_t IString::bytes() const noexcept { std::size_t i = 0; - for (i = 0; i < buffLen && m_buff[i]; i++); + for (i = 0; i < StrCap && m_buff[i]; i++); return i + 1; // add one for null terminator } -template -constexpr std::size_t IString::cap() const noexcept { - return buffLen; +template +constexpr ox::Error IString::resize(size_t sz) noexcept { + if (sz > StrCap) { + return OxError(1, "Trying to extend IString beyond its cap"); + } + m_size = sz; + return {}; +} + +template +constexpr std::size_t IString::cap() const noexcept { + return StrCap; } template diff --git a/deps/ox/src/ox/std/uuid.hpp b/deps/ox/src/ox/std/uuid.hpp index 1d448ba1..f4dbdbe9 100644 --- a/deps/ox/src/ox/std/uuid.hpp +++ b/deps/ox/src/ox/std/uuid.hpp @@ -8,12 +8,12 @@ #pragma once -#include "array.hpp" #include "istring.hpp" #include "buffer.hpp" #include "random.hpp" #include "ranges.hpp" #include "stringview.hpp" +#include "strops.hpp" namespace ox { @@ -80,7 +80,8 @@ constexpr ox::IString<2> toHex(uint8_t v) noexcept { 'e', 'f', }; - ox::Array out; + ox::IString<2> out; + std::ignore = out.resize(2); out[0] = valMap[static_cast((v & 0xf0) / 16)]; out[1] = valMap[static_cast(v & 0x0f)]; out[2] = 0;