[ox/std] Add String::operator+ methods

This commit is contained in:
Gary Talent 2020-03-15 02:17:11 -05:00
parent 412b187c4f
commit f7bdd5042d
2 changed files with 47 additions and 0 deletions

View File

@ -7,6 +7,7 @@
*/
#include "string.hpp"
#include "types.hpp"
namespace ox {
@ -14,6 +15,10 @@ String::String() noexcept {
m_buff.push_back(0);
}
String::String(std::size_t cap) noexcept {
m_buff.resize(cap + 1);
}
String::String(const char *str) noexcept {
m_buff.push_back(0);
*this = str;
@ -58,6 +63,36 @@ const String &String::operator+=(int64_t i) noexcept {
return this->operator+=(str);
}
const String &String::operator+=(const String &src) noexcept {
return *this += src.c_str();
}
const String String::operator+(const char *str) const noexcept {
std::size_t strLen = ox_strlen(str);
auto currentLen = len();
String cpy(currentLen + strLen);
cpy.m_buff.resize(m_buff.size() + strLen);
memcpy(&cpy.m_buff[0], m_buff.data(), currentLen);
memcpy(&cpy.m_buff[currentLen], str, strLen);
// make sure last element is a null terminator
cpy.m_buff[currentLen + strLen] = 0;
return cpy;
}
const String String::operator+(char *str) const noexcept {
return *this + static_cast<const char*>(str);
}
const String String::operator+(int64_t i) const noexcept {
char str[65] = {};
ox_itoa(i, str);
return *this + str;
}
const String String::operator+(const String &src) const noexcept {
return *this + src.c_str();
}
bool String::operator==(const String &other) noexcept {
bool retval = true;
std::size_t i = 0;

View File

@ -24,6 +24,8 @@ class String {
public:
String() noexcept;
String(std::size_t cap) noexcept;
String(const char *str) noexcept;
const String &operator=(const char *str) noexcept;
@ -38,6 +40,16 @@ class String {
const String &operator+=(int64_t i) noexcept;
const String &operator+=(const String &src) noexcept;
const String operator+(const char *str) const noexcept;
const String operator+(char *str) const noexcept;
const String operator+(int64_t i) const noexcept;
const String operator+(const String &src) const noexcept;
bool operator==(const String &other) noexcept;
bool operator!=(const String &other) noexcept;