[ox/std] Cleanup IString

This commit is contained in:
Gary Talent 2024-05-02 23:32:02 -05:00
parent e30ebce4c0
commit 7c4e2a6564

View File

@ -8,6 +8,7 @@
#pragma once #pragma once
#include "array.hpp"
#include "ignore.hpp" #include "ignore.hpp"
#include "memops.hpp" #include "memops.hpp"
#include "ox/std/error.hpp" #include "ox/std/error.hpp"
@ -20,7 +21,7 @@ namespace ox {
template<std::size_t StrCap> template<std::size_t StrCap>
class IString { class IString {
private: private:
char m_buff[StrCap + 1]; ox::Array<char, StrCap + 1> m_buff;
size_t m_size{}; size_t m_size{};
public: public:
@ -112,9 +113,9 @@ constexpr IString<size>::IString(const char *str) noexcept: m_buff{{0}} {
template<std::size_t size> template<std::size_t size>
constexpr IString<size> &IString<size>::operator=(Integer_c auto i) noexcept { constexpr IString<size> &IString<size>::operator=(Integer_c auto i) noexcept {
char str[65] = {}; ox::Array<char, 65> str{};
ox::itoa(i, str); ox::itoa(i, str.data());
return this->operator=(str); return this->operator=(str.data());
} }
template<std::size_t size> template<std::size_t size>
@ -124,7 +125,7 @@ constexpr IString<size> &IString<size>::operator=(ox::CRStringView str) noexcept
strLen = cap(); strLen = cap();
} }
m_size = strLen; m_size = strLen;
ox::listcpy(m_buff, str.data(), strLen); ox::listcpy(m_buff.data(), str.data(), strLen);
// make sure last element is a null terminator // make sure last element is a null terminator
m_buff[strLen] = 0; m_buff[strLen] = 0;
return *this; return *this;
@ -137,7 +138,7 @@ constexpr IString<size> &IString<size>::operator=(const char *str) noexcept {
strLen = cap(); strLen = cap();
} }
m_size = strLen; m_size = strLen;
ox::listcpy(m_buff, str, strLen); ox::listcpy(m_buff.data(), str, strLen);
// make sure last element is a null terminator // make sure last element is a null terminator
m_buff[cap()] = 0; m_buff[cap()] = 0;
return *this; return *this;
@ -162,9 +163,9 @@ constexpr IString<size> &IString<size>::operator+=(char *str) noexcept {
template<std::size_t size> template<std::size_t size>
constexpr IString<size> &IString<size>::operator+=(Integer_c auto i) noexcept { constexpr IString<size> &IString<size>::operator+=(Integer_c auto i) noexcept {
char str[65] = {}; ox::Array<char, 65> str{};
ox::itoa(i, str); ox::itoa(i, str.data());
return this->operator+=(str); return this->operator+=(str.data());
} }
template<std::size_t size> template<std::size_t size>
@ -189,9 +190,9 @@ constexpr IString<size> IString<size>::operator+(char *str) const noexcept {
template<std::size_t size> template<std::size_t size>
constexpr IString<size> IString<size>::operator+(Integer_c auto i) const noexcept { constexpr IString<size> IString<size>::operator+(Integer_c auto i) const noexcept {
char str[65] = {}; ox::Array<char, 65> str{};
ox::itoa(i, str); ox::itoa(i, str.data());
return this->operator+(str); return this->operator+(str.data());
} }
template<std::size_t StrCap> template<std::size_t StrCap>
@ -232,7 +233,7 @@ constexpr Error IString<StrCap>::append(const char *str, std::size_t strLen) noe
strLen = cap() - currentLen; strLen = cap() - currentLen;
err = OxError(1, "Insufficient space for full string"); err = OxError(1, "Insufficient space for full string");
} }
ox::strncpy(m_buff + currentLen, str, strLen); ox::strncpy(m_buff.data() + currentLen, str, strLen);
// make sure last element is a null terminator // make sure last element is a null terminator
m_buff[currentLen + strLen] = 0; m_buff[currentLen + strLen] = 0;
return err; return err;
@ -240,17 +241,17 @@ constexpr Error IString<StrCap>::append(const char *str, std::size_t strLen) noe
template<std::size_t StrCap> template<std::size_t StrCap>
constexpr const char *IString<StrCap>::data() const noexcept { constexpr const char *IString<StrCap>::data() const noexcept {
return static_cast<const char*>(m_buff); return static_cast<const char*>(m_buff.data());
} }
template<std::size_t StrCap> template<std::size_t StrCap>
constexpr char *IString<StrCap>::data() noexcept { constexpr char *IString<StrCap>::data() noexcept {
return static_cast<char*>(m_buff); return static_cast<char*>(m_buff.data());
} }
template<std::size_t StrCap> template<std::size_t StrCap>
constexpr const char *IString<StrCap>::c_str() const noexcept { constexpr const char *IString<StrCap>::c_str() const noexcept {
return static_cast<const char*>(m_buff); return static_cast<const char*>(m_buff.data());
} }
@ -258,7 +259,7 @@ template<std::size_t StrCap>
constexpr std::size_t IString<StrCap>::len() const noexcept { constexpr std::size_t IString<StrCap>::len() const noexcept {
std::size_t length = 0; std::size_t length = 0;
for (std::size_t i = 0; i < StrCap; i++) { for (std::size_t i = 0; i < StrCap; i++) {
uint8_t b = static_cast<uint8_t>(m_buff[i]); auto const b = static_cast<uint8_t>(m_buff[i]);
if (b) { if (b) {
const auto asciiChar = (b & 128) == 0; const auto asciiChar = (b & 128) == 0;
const auto utf8Char = (b & (256 << 6)) == (256 << 6); const auto utf8Char = (b & (256 << 6)) == (256 << 6);