[ox/std] Add implicit String constructor for str literals
All checks were successful
Build / build (push) Successful in 2m20s

Also make String more constexpr friendly
This commit is contained in:
Gary Talent 2024-04-18 23:59:47 -05:00
parent 6bfe184261
commit dfd27afd67

View File

@ -32,6 +32,9 @@ class BasicString {
constexpr explicit BasicString(std::size_t cap) noexcept; constexpr explicit BasicString(std::size_t cap) noexcept;
template<size_t Sz>
constexpr BasicString(char const (&str)[Sz]) noexcept;
constexpr explicit BasicString(const char *str) noexcept; constexpr explicit BasicString(const char *str) noexcept;
constexpr explicit BasicString(const char8_t *str) noexcept; constexpr explicit BasicString(const char8_t *str) noexcept;
@ -167,7 +170,7 @@ class BasicString {
constexpr Error append(const char *str, std::size_t strLen) noexcept { constexpr Error append(const char *str, std::size_t strLen) noexcept {
auto currentLen = len(); auto currentLen = len();
m_buff.resize(m_buff.size() + strLen); m_buff.resize(m_buff.size() + strLen);
ox::memcpy(&m_buff[currentLen], str, strLen); ox::listcpy(&m_buff[currentLen], str, strLen);
// make sure last element is a null terminator // make sure last element is a null terminator
m_buff[currentLen + strLen] = 0; m_buff[currentLen + strLen] = 0;
// this can't fail, but it returns an Error to match BString::append // this can't fail, but it returns an Error to match BString::append
@ -247,6 +250,14 @@ constexpr BasicString<SmallStringSize_v>::BasicString(std::size_t cap) noexcept:
} }
} }
template<std::size_t SmallStringSize_v>
template<size_t Sz>
constexpr BasicString<SmallStringSize_v>::BasicString(char const (&str)[Sz]) noexcept {
m_buff.resize(Sz + 1);
ox::listcpy(m_buff.data(), str, Sz);
m_buff[Sz] = 0;
}
template<std::size_t SmallStringSize_v> template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v>::BasicString(const char *str) noexcept { constexpr BasicString<SmallStringSize_v>::BasicString(const char *str) noexcept {
if (!m_buff.empty()) { if (!m_buff.empty()) {
@ -270,7 +281,7 @@ constexpr BasicString<SmallStringSize_v>::BasicString(const char8_t *str) noexce
template<std::size_t SmallStringSize_v> template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v>::BasicString(const char *str, std::size_t size) noexcept { constexpr BasicString<SmallStringSize_v>::BasicString(const char *str, std::size_t size) noexcept {
m_buff.resize(size + 1); m_buff.resize(size + 1);
memcpy(m_buff.data(), str, size); ox::listcpy(m_buff.data(), str, size);
m_buff[size] = 0; m_buff[size] = 0;
} }
@ -394,8 +405,8 @@ constexpr BasicString<SmallStringSize_v> BasicString<SmallStringSize_v>::operato
const auto currentLen = len(); const auto currentLen = len();
BasicString<SmallStringSize_v> cpy(currentLen + strLen); BasicString<SmallStringSize_v> cpy(currentLen + strLen);
cpy.m_buff.resize(m_buff.size() + strLen); cpy.m_buff.resize(m_buff.size() + strLen);
memcpy(&cpy.m_buff[0], m_buff.data(), currentLen); ox::listcpy(&cpy.m_buff[0], m_buff.data(), currentLen);
memcpy(&cpy.m_buff[currentLen], str, strLen); ox::listcpy(&cpy.m_buff[currentLen], str, strLen);
// make sure last element is a null terminator // make sure last element is a null terminator
cpy.m_buff[currentLen + strLen] = 0; cpy.m_buff[currentLen + strLen] = 0;
return cpy; return cpy;
@ -425,8 +436,8 @@ constexpr BasicString<SmallStringSize_v> BasicString<SmallStringSize_v>::operato
const auto currentLen = len(); const auto currentLen = len();
BasicString<SmallStringSize_v> cpy(currentLen + strLen); BasicString<SmallStringSize_v> cpy(currentLen + strLen);
cpy.m_buff.resize(m_buff.size() + strLen); cpy.m_buff.resize(m_buff.size() + strLen);
memcpy(&cpy.m_buff[0], m_buff.data(), currentLen); ox::listcpy(&cpy.m_buff[0], m_buff.data(), currentLen);
memcpy(&cpy.m_buff[currentLen], src.data(), strLen + 1); ox::listcpy(&cpy.m_buff[currentLen], src.data(), strLen + 1);
return cpy; return cpy;
} }
@ -436,8 +447,8 @@ constexpr BasicString<SmallStringSize_v> BasicString<SmallStringSize_v>::operato
const auto currentLen = len(); const auto currentLen = len();
BasicString<SmallStringSize_v> cpy(currentLen + strLen); BasicString<SmallStringSize_v> cpy(currentLen + strLen);
cpy.m_buff.resize(m_buff.size() + strLen); cpy.m_buff.resize(m_buff.size() + strLen);
memcpy(&cpy.m_buff[0], m_buff.data(), currentLen); ox::listcpy(&cpy.m_buff[0], m_buff.data(), currentLen);
memcpy(&cpy.m_buff[currentLen], src.data(), strLen + 1); ox::listcpy(&cpy.m_buff[currentLen], src.data(), strLen + 1);
return cpy; return cpy;
} }
@ -509,7 +520,7 @@ constexpr BasicString<SmallStringSize_v> BasicString<SmallStringSize_v>::substr(
const auto size = end - begin; const auto size = end - begin;
BasicString<SmallStringSize_v> out(size); BasicString<SmallStringSize_v> out(size);
const auto buff = out.data(); const auto buff = out.data();
memcpy(buff, src, size); ox::listcpy(buff, src, size);
buff[size] = 0; buff[size] = 0;
return out; return out;
} }
@ -550,7 +561,7 @@ template<std::size_t SmallStringSize_v>
constexpr void BasicString<SmallStringSize_v>::set(const char8_t *str) noexcept { constexpr void BasicString<SmallStringSize_v>::set(const char8_t *str) noexcept {
std::size_t strBytes = ox::strlen(str) + 1; std::size_t strBytes = ox::strlen(str) + 1;
m_buff.resize(strBytes); m_buff.resize(strBytes);
memcpy(m_buff.data(), str, strBytes); ox::listcpy(m_buff.data(), str, strBytes);
*m_buff.back().value = 0; *m_buff.back().value = 0;
} }