From f7bdd5042d74bfe9e814263f289ab8b0f2607bb3 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 15 Mar 2020 02:17:11 -0500 Subject: [PATCH] [ox/std] Add String::operator+ methods --- deps/ox/src/ox/std/string.cpp | 35 +++++++++++++++++++++++++++++++++++ deps/ox/src/ox/std/string.hpp | 12 ++++++++++++ 2 files changed, 47 insertions(+) diff --git a/deps/ox/src/ox/std/string.cpp b/deps/ox/src/ox/std/string.cpp index c341fec6..59ef281e 100644 --- a/deps/ox/src/ox/std/string.cpp +++ b/deps/ox/src/ox/std/string.cpp @@ -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(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; diff --git a/deps/ox/src/ox/std/string.hpp b/deps/ox/src/ox/std/string.hpp index af4934e3..43f791d0 100644 --- a/deps/ox/src/ox/std/string.hpp +++ b/deps/ox/src/ox/std/string.hpp @@ -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;