[ox/std] Add append to String and cleanup BString::operator+=

This commit is contained in:
Gary Talent 2021-05-06 11:50:58 -05:00
parent a5bb1eeed0
commit fb59d8033b
4 changed files with 40 additions and 42 deletions

View File

@ -104,13 +104,7 @@ constexpr const BString<size> &BString<size>::operator=(char *str) noexcept {
template<std::size_t size>
constexpr const BString<size> &BString<size>::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;
}

View File

@ -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<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 i;
for (i = 0; i < m_buff.size() && m_buff[i]; i++);

View File

@ -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<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;
}
}

View File

@ -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<T&> front() noexcept;
@ -57,14 +57,14 @@ class Vector {
Result<const T&> 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<T>::operator==(const Vector<T> &other) const {
}
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) {
clear();
delete[] bit_cast<AllocAlias<T>*>(m_items);
@ -174,7 +174,7 @@ Vector<T> &Vector<T>::operator=(const Vector<T> &other) {
}
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) {
clear();
delete[] bit_cast<AllocAlias<T>*>(m_items);
@ -189,12 +189,12 @@ Vector<T> &Vector<T>::operator=(Vector<T> &&other) noexcept {
}
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];
}
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];
}
@ -235,7 +235,7 @@ Result<const T&> Vector<T>::back() const noexcept {
}
template<typename T>
std::size_t Vector<T>::size() const noexcept {
constexpr std::size_t Vector<T>::size() const noexcept {
return m_size;
}
@ -255,7 +255,7 @@ void Vector<T>::clear() {
}
template<typename T>
void Vector<T>::resize(std::size_t size) {
constexpr void Vector<T>::resize(std::size_t size) {
if (m_cap < size) {
expandCap(size);
}