[ox/std] Cleanup

This commit is contained in:
Gary Talent 2024-05-02 23:14:03 -05:00
parent 0a0a6e306d
commit 7163947efd
2 changed files with 46 additions and 34 deletions

View File

@ -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<std::size_t buffLen>
template<std::size_t StrCap>
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<size> IString<size>::operator+(Integer_c auto i) const noexcep
return this->operator+(str);
}
template<std::size_t buffLen>
constexpr bool IString<buffLen>::operator==(const char *other) const noexcept {
template<std::size_t StrCap>
constexpr bool IString<StrCap>::operator==(const char *other) const noexcept {
return ox::StringView(*this) == other;
}
template<std::size_t buffLen>
constexpr bool IString<buffLen>::operator==(const OxString_c auto &other) const noexcept {
template<std::size_t StrCap>
constexpr bool IString<StrCap>::operator==(const OxString_c auto &other) const noexcept {
return ox::StringView(*this) == ox::StringView(other);
}
template<std::size_t buffLen>
constexpr bool IString<buffLen>::operator!=(const char *other) const noexcept {
template<std::size_t StrCap>
constexpr bool IString<StrCap>::operator!=(const char *other) const noexcept {
return !operator==(other);
}
template<std::size_t buffLen>
constexpr bool IString<buffLen>::operator!=(const OxString_c auto &other) noexcept {
template<std::size_t StrCap>
constexpr bool IString<StrCap>::operator!=(const OxString_c auto &other) noexcept {
return !operator==(other);
}
template<std::size_t buffLen>
constexpr char IString<buffLen>::operator[](std::size_t i) const noexcept {
template<std::size_t StrCap>
constexpr char IString<StrCap>::operator[](std::size_t i) const noexcept {
return m_buff[i];
}
template<std::size_t buffLen>
constexpr char &IString<buffLen>::operator[](std::size_t i) noexcept {
template<std::size_t StrCap>
constexpr char &IString<StrCap>::operator[](std::size_t i) noexcept {
return m_buff[i];
}
template<std::size_t buffLen>
constexpr Error IString<buffLen>::append(const char *str, std::size_t strLen) noexcept {
template<std::size_t StrCap>
constexpr Error IString<StrCap>::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<buffLen>::append(const char *str, std::size_t strLen) no
return err;
}
template<std::size_t buffLen>
constexpr const char *IString<buffLen>::data() const noexcept {
template<std::size_t StrCap>
constexpr const char *IString<StrCap>::data() const noexcept {
return static_cast<const char*>(m_buff);
}
template<std::size_t buffLen>
constexpr char *IString<buffLen>::data() noexcept {
template<std::size_t StrCap>
constexpr char *IString<StrCap>::data() noexcept {
return static_cast<char*>(m_buff);
}
template<std::size_t buffLen>
constexpr const char *IString<buffLen>::c_str() const noexcept {
template<std::size_t StrCap>
constexpr const char *IString<StrCap>::c_str() const noexcept {
return static_cast<const char*>(m_buff);
}
template<std::size_t buffLen>
constexpr std::size_t IString<buffLen>::len() const noexcept {
template<std::size_t StrCap>
constexpr std::size_t IString<StrCap>::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<uint8_t>(m_buff[i]);
if (b) {
const auto asciiChar = (b & 128) == 0;
@ -270,16 +272,25 @@ constexpr std::size_t IString<buffLen>::len() const noexcept {
return length;
}
template<std::size_t buffLen>
constexpr std::size_t IString<buffLen>::bytes() const noexcept {
template<std::size_t StrCap>
constexpr std::size_t IString<StrCap>::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<std::size_t buffLen>
constexpr std::size_t IString<buffLen>::cap() const noexcept {
return buffLen;
template<std::size_t StrCap>
constexpr ox::Error IString<StrCap>::resize(size_t sz) noexcept {
if (sz > StrCap) {
return OxError(1, "Trying to extend IString beyond its cap");
}
m_size = sz;
return {};
}
template<std::size_t StrCap>
constexpr std::size_t IString<StrCap>::cap() const noexcept {
return StrCap;
}
template<size_t sz>

View File

@ -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<char, 3> out;
ox::IString<2> out;
std::ignore = out.resize(2);
out[0] = valMap[static_cast<unsigned>((v & 0xf0) / 16)];
out[1] = valMap[static_cast<unsigned>(v & 0x0f)];
out[2] = 0;