[ox/std] Make String constructors constexpr

This commit is contained in:
Gary Talent 2021-05-11 22:09:26 -05:00
parent a0c645f25c
commit 7302284095
2 changed files with 66 additions and 42 deletions

View File

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

View File

@ -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++) {