[ox/std] Add append to String and cleanup BString::operator+=
This commit is contained in:
parent
a5bb1eeed0
commit
fb59d8033b
8
deps/ox/src/ox/std/bstring.hpp
vendored
8
deps/ox/src/ox/std/bstring.hpp
vendored
@ -104,13 +104,7 @@ constexpr const BString<size> &BString<size>::operator=(char *str) noexcept {
|
|||||||
template<std::size_t size>
|
template<std::size_t size>
|
||||||
constexpr const BString<size> &BString<size>::operator+=(const char *str) noexcept {
|
constexpr const BString<size> &BString<size>::operator+=(const char *str) noexcept {
|
||||||
std::size_t strLen = ox_strlen(str) + 1;
|
std::size_t strLen = ox_strlen(str) + 1;
|
||||||
auto currentLen = len();
|
append(str, strLen);
|
||||||
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;
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
23
deps/ox/src/ox/std/string.cpp
vendored
23
deps/ox/src/ox/std/string.cpp
vendored
@ -86,11 +86,7 @@ String &String::operator=(String &&src) noexcept {
|
|||||||
|
|
||||||
String &String::operator+=(const char *str) noexcept {
|
String &String::operator+=(const char *str) noexcept {
|
||||||
std::size_t strLen = ox_strlen(str);
|
std::size_t strLen = ox_strlen(str);
|
||||||
auto currentLen = len();
|
append(str, strLen);
|
||||||
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;
|
|
||||||
return *this;
|
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;
|
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<uint8_t>(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 String::bytes() const noexcept {
|
||||||
std::size_t i;
|
std::size_t i;
|
||||||
for (i = 0; i < m_buff.size() && m_buff[i]; i++);
|
for (i = 0; i < m_buff.size() && m_buff[i]; i++);
|
||||||
|
27
deps/ox/src/ox/std/string.hpp
vendored
27
deps/ox/src/ox/std/string.hpp
vendored
@ -83,6 +83,14 @@ class String {
|
|||||||
|
|
||||||
char &operator[](std::size_t i) noexcept;
|
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]]
|
[[nodiscard]]
|
||||||
String substr(std::size_t pos) const noexcept;
|
String substr(std::size_t pos) const noexcept;
|
||||||
|
|
||||||
@ -118,7 +126,7 @@ class String {
|
|||||||
* Returns the number of characters in this string.
|
* Returns the number of characters in this string.
|
||||||
*/
|
*/
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
std::size_t len() const noexcept;
|
constexpr std::size_t len() const noexcept;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the number of bytes used for this string.
|
* 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<uint8_t>(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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
24
deps/ox/src/ox/std/vector.hpp
vendored
24
deps/ox/src/ox/std/vector.hpp
vendored
@ -40,13 +40,13 @@ class Vector {
|
|||||||
|
|
||||||
bool operator==(const Vector &other) const;
|
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<T&> front() noexcept;
|
Result<T&> front() noexcept;
|
||||||
|
|
||||||
@ -57,14 +57,14 @@ class Vector {
|
|||||||
Result<const T&> back() const noexcept;
|
Result<const T&> back() const noexcept;
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
std::size_t size() const noexcept;
|
constexpr std::size_t size() const noexcept;
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
bool empty() const noexcept;
|
bool empty() const noexcept;
|
||||||
|
|
||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
void resize(std::size_t size);
|
constexpr void resize(std::size_t size);
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
constexpr T *data() noexcept {
|
constexpr T *data() noexcept {
|
||||||
@ -159,7 +159,7 @@ bool Vector<T>::operator==(const Vector<T> &other) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Vector<T> &Vector<T>::operator=(const Vector<T> &other) {
|
constexpr Vector<T> &Vector<T>::operator=(const Vector<T> &other) {
|
||||||
if (this != &other) {
|
if (this != &other) {
|
||||||
clear();
|
clear();
|
||||||
delete[] bit_cast<AllocAlias<T>*>(m_items);
|
delete[] bit_cast<AllocAlias<T>*>(m_items);
|
||||||
@ -174,7 +174,7 @@ Vector<T> &Vector<T>::operator=(const Vector<T> &other) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Vector<T> &Vector<T>::operator=(Vector<T> &&other) noexcept {
|
constexpr Vector<T> &Vector<T>::operator=(Vector<T> &&other) noexcept {
|
||||||
if (this != &other) {
|
if (this != &other) {
|
||||||
clear();
|
clear();
|
||||||
delete[] bit_cast<AllocAlias<T>*>(m_items);
|
delete[] bit_cast<AllocAlias<T>*>(m_items);
|
||||||
@ -189,12 +189,12 @@ Vector<T> &Vector<T>::operator=(Vector<T> &&other) noexcept {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T &Vector<T>::operator[](std::size_t i) noexcept {
|
constexpr T &Vector<T>::operator[](std::size_t i) noexcept {
|
||||||
return m_items[i];
|
return m_items[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
const T &Vector<T>::operator[](std::size_t i) const noexcept {
|
constexpr const T &Vector<T>::operator[](std::size_t i) const noexcept {
|
||||||
return m_items[i];
|
return m_items[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -235,7 +235,7 @@ Result<const T&> Vector<T>::back() const noexcept {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
std::size_t Vector<T>::size() const noexcept {
|
constexpr std::size_t Vector<T>::size() const noexcept {
|
||||||
return m_size;
|
return m_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -255,7 +255,7 @@ void Vector<T>::clear() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Vector<T>::resize(std::size_t size) {
|
constexpr void Vector<T>::resize(std::size_t size) {
|
||||||
if (m_cap < size) {
|
if (m_cap < size) {
|
||||||
expandCap(size);
|
expandCap(size);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user