From fb59d8033bd40bad2bc81c9be7213308acad6c21 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 6 May 2021 11:50:58 -0500 Subject: [PATCH] [ox/std] Add append to String and cleanup BString::operator+= --- deps/ox/src/ox/std/bstring.hpp | 8 +------- deps/ox/src/ox/std/string.cpp | 23 +---------------------- deps/ox/src/ox/std/string.hpp | 27 ++++++++++++++++++++++++++- deps/ox/src/ox/std/vector.hpp | 24 ++++++++++++------------ 4 files changed, 40 insertions(+), 42 deletions(-) diff --git a/deps/ox/src/ox/std/bstring.hpp b/deps/ox/src/ox/std/bstring.hpp index 4713508b..46840f76 100644 --- a/deps/ox/src/ox/std/bstring.hpp +++ b/deps/ox/src/ox/std/bstring.hpp @@ -104,13 +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; - auto currentLen = len(); - if (cap() < currentLen + strLen) { - strLen = cap() - currentLen; - } - ox_memcpy(m_buff + currentLen, str, strLen); - // make sure last element is a null terminator - m_buff[currentLen + strLen] = 0; + append(str, strLen); return *this; } diff --git a/deps/ox/src/ox/std/string.cpp b/deps/ox/src/ox/std/string.cpp index 82635eb5..a3e61d49 100644 --- a/deps/ox/src/ox/std/string.cpp +++ b/deps/ox/src/ox/std/string.cpp @@ -86,11 +86,7 @@ String &String::operator=(String &&src) noexcept { String &String::operator+=(const char *str) noexcept { std::size_t strLen = ox_strlen(str); - auto currentLen = len(); - m_buff.resize(m_buff.size() + strLen); - memcpy(&m_buff[currentLen], str, strLen); - // make sure last element is a null terminator - m_buff[currentLen + strLen] = 0; + append(str, strLen); return *this; } @@ -191,23 +187,6 @@ bool String::endsWith(const String &ending) const noexcept { return len() >= endingLen && ox_strcmp(data() + (len() - endingLen), ending.c_str()) == 0; } -std::size_t String::len() const noexcept { - std::size_t length = 0; - for (std::size_t i = 0; i < m_buff.size(); i++) { - uint8_t b = static_cast(m_buff[i]); - if (b) { - if ((b & 128) == 0) { // normal ASCII character - length++; - } else if ((b & (256 << 6)) == (256 << 6)) { // start of UTF-8 character - length++; - } - } else { - break; - } - } - return length; -} - std::size_t String::bytes() const noexcept { std::size_t i; for (i = 0; i < m_buff.size() && m_buff[i]; i++); diff --git a/deps/ox/src/ox/std/string.hpp b/deps/ox/src/ox/std/string.hpp index 19a92c64..22701f33 100644 --- a/deps/ox/src/ox/std/string.hpp +++ b/deps/ox/src/ox/std/string.hpp @@ -83,6 +83,14 @@ class String { char &operator[](std::size_t i) noexcept; + constexpr void 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; + } + [[nodiscard]] String substr(std::size_t pos) const noexcept; @@ -118,7 +126,7 @@ class String { * Returns the number of characters in this string. */ [[nodiscard]] - std::size_t len() const noexcept; + constexpr std::size_t len() const noexcept; /** * Returns the number of bytes used for this string. @@ -128,4 +136,21 @@ class String { }; +constexpr std::size_t String::len() const noexcept { + std::size_t length = 0; + for (std::size_t i = 0; i < m_buff.size(); i++) { + uint8_t b = static_cast(m_buff[i]); + if (b) { + if ((b & 128) == 0) { // normal ASCII character + length++; + } else if ((b & (256 << 6)) == (256 << 6)) { // start of UTF-8 character + length++; + } + } else { + break; + } + } + return length; +} + } diff --git a/deps/ox/src/ox/std/vector.hpp b/deps/ox/src/ox/std/vector.hpp index b0073bb9..a0b7d35e 100644 --- a/deps/ox/src/ox/std/vector.hpp +++ b/deps/ox/src/ox/std/vector.hpp @@ -40,13 +40,13 @@ class Vector { bool operator==(const Vector &other) const; - Vector &operator=(const Vector &other); + constexpr Vector &operator=(const Vector &other); - Vector &operator=(Vector &&other) noexcept; + constexpr Vector &operator=(Vector &&other) noexcept; - T &operator[](std::size_t i) noexcept; + constexpr T &operator[](std::size_t i) noexcept; - const T &operator[](std::size_t i) const noexcept; + constexpr const T &operator[](std::size_t i) const noexcept; Result front() noexcept; @@ -57,14 +57,14 @@ class Vector { Result back() const noexcept; [[nodiscard]] - std::size_t size() const noexcept; + constexpr std::size_t size() const noexcept; [[nodiscard]] bool empty() const noexcept; void clear(); - void resize(std::size_t size); + constexpr void resize(std::size_t size); [[nodiscard]] constexpr T *data() noexcept { @@ -159,7 +159,7 @@ bool Vector::operator==(const Vector &other) const { } template -Vector &Vector::operator=(const Vector &other) { +constexpr Vector &Vector::operator=(const Vector &other) { if (this != &other) { clear(); delete[] bit_cast*>(m_items); @@ -174,7 +174,7 @@ Vector &Vector::operator=(const Vector &other) { } template -Vector &Vector::operator=(Vector &&other) noexcept { +constexpr Vector &Vector::operator=(Vector &&other) noexcept { if (this != &other) { clear(); delete[] bit_cast*>(m_items); @@ -189,12 +189,12 @@ Vector &Vector::operator=(Vector &&other) noexcept { } template -T &Vector::operator[](std::size_t i) noexcept { +constexpr T &Vector::operator[](std::size_t i) noexcept { return m_items[i]; } template -const T &Vector::operator[](std::size_t i) const noexcept { +constexpr const T &Vector::operator[](std::size_t i) const noexcept { return m_items[i]; } @@ -235,7 +235,7 @@ Result Vector::back() const noexcept { } template -std::size_t Vector::size() const noexcept { +constexpr std::size_t Vector::size() const noexcept { return m_size; } @@ -255,7 +255,7 @@ void Vector::clear() { } template -void Vector::resize(std::size_t size) { +constexpr void Vector::resize(std::size_t size) { if (m_cap < size) { expandCap(size); }