From 7302284095446b37cdf7739f69e40ac2e5ff8dfd Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 11 May 2021 22:09:26 -0500 Subject: [PATCH] [ox/std] Make String constructors constexpr --- deps/ox/src/ox/std/string.cpp | 54 ++++++++++++----------------------- deps/ox/src/ox/std/string.hpp | 54 +++++++++++++++++++++++++++++++---- 2 files changed, 66 insertions(+), 42 deletions(-) diff --git a/deps/ox/src/ox/std/string.cpp b/deps/ox/src/ox/std/string.cpp index 0fcbee1f..cb15f9b1 100644 --- a/deps/ox/src/ox/std/string.cpp +++ b/deps/ox/src/ox/std/string.cpp @@ -11,42 +11,6 @@ namespace ox { -String::String() noexcept { - if (m_buff.size()) { - m_buff[0] = 0; - } else { - m_buff.push_back(0); - } -} - -String::String(std::size_t cap) noexcept { - m_buff.resize(cap + 1); - m_buff[0] = 0; -} - -String::String(const char *str) noexcept { - if (m_buff.size()) { - m_buff[0] = 0; - } else { - m_buff.push_back(0); - } - *this = str; -} - -String::String(const char *str, std::size_t size) noexcept { - m_buff.resize(size + 1); - memcpy(m_buff.data(), str, size); - m_buff[size] = 0; -} - -String::String(const String &other) noexcept { - m_buff = other.m_buff; -} - -String::String(String &&other) noexcept { - m_buff = move(other.m_buff); -} - String &String::operator=(const char *str) noexcept { std::size_t strLen = ox_strlen(str) + 1; m_buff.resize(strLen + 1); @@ -75,6 +39,12 @@ String &String::operator=(int64_t i) noexcept { return this->operator=(str); } +String &String::operator=(uint64_t i) noexcept { + char str[65] = {}; + ox_itoa(i, str); + return this->operator=(str); +} + String &String::operator=(const String &src) noexcept { return *this = src.c_str(); } @@ -109,6 +79,12 @@ String &String::operator+=(int64_t i) noexcept { return this->operator+=(str); } +String &String::operator+=(uint64_t i) noexcept { + char str[65] = {}; + ox_itoa(i, str); + return this->operator+=(str); +} + String &String::operator+=(const String &src) noexcept { return *this += src.c_str(); } @@ -144,6 +120,12 @@ String String::operator+(int64_t i) const noexcept { return *this + str; } +String String::operator+(uint64_t i) const noexcept { + char str[65] = {}; + ox_itoa(i, str); + return *this + str; +} + String String::operator+(const String &src) const noexcept { return *this + src.c_str(); } diff --git a/deps/ox/src/ox/std/string.hpp b/deps/ox/src/ox/std/string.hpp index 54bdf10f..1aa9e2c8 100644 --- a/deps/ox/src/ox/std/string.hpp +++ b/deps/ox/src/ox/std/string.hpp @@ -25,17 +25,17 @@ class String { Buffer m_buff; public: - String() noexcept; + constexpr String() noexcept; - explicit String(std::size_t cap) noexcept; + constexpr explicit String(std::size_t cap) noexcept; - String(const char *str) noexcept; + constexpr String(const char *str) noexcept; - String(const char *str, std::size_t size) noexcept; + constexpr String(const char *str, std::size_t size) noexcept; - String(const String&) noexcept; + constexpr String(const String&) noexcept; - String(String&&) noexcept; + constexpr String(String&&) noexcept; String &operator=(const char *str) noexcept; @@ -47,6 +47,8 @@ class String { String &operator=(int64_t i) noexcept; + String &operator=(uint64_t i) noexcept; + String &operator=(const String &src) noexcept; String &operator=(String &&src) noexcept; @@ -61,6 +63,8 @@ class String { String &operator+=(int64_t i) noexcept; + String &operator+=(uint64_t i) noexcept; + String &operator+=(const String &src) noexcept; String operator+(const char *str) const noexcept; @@ -73,6 +77,8 @@ class String { String operator+(int64_t i) const noexcept; + String operator+(uint64_t i) const noexcept; + String operator+(const String &src) const noexcept; bool operator==(const String &other) const noexcept; @@ -138,6 +144,42 @@ class String { }; +constexpr String::String() noexcept { + if (m_buff.size()) { + m_buff[0] = 0; + } else { + m_buff.push_back(0); + } +} + +constexpr String::String(std::size_t cap) noexcept { + m_buff.resize(cap + 1); + m_buff[0] = 0; +} + +constexpr String::String(const char *str) noexcept { + if (m_buff.size()) { + m_buff[0] = 0; + } else { + m_buff.push_back(0); + } + *this = str; +} + +constexpr String::String(const char *str, std::size_t size) noexcept { + m_buff.resize(size + 1); + memcpy(m_buff.data(), str, size); + m_buff[size] = 0; +} + +constexpr String::String(const String &other) noexcept { + m_buff = other.m_buff; +} + +constexpr String::String(String &&other) noexcept { + m_buff = move(other.m_buff); +} + constexpr std::size_t String::len() const noexcept { std::size_t length = 0; for (std::size_t i = 0; i < m_buff.size(); i++) {