[ox/std] Fix String::c_str to always retrun a valid C str
All checks were successful
Build / build (push) Successful in 2m36s

This commit is contained in:
Gary Talent 2024-05-29 20:51:15 -05:00
parent e4c3866017
commit 95a69b72b5
2 changed files with 11 additions and 21 deletions

View File

@ -13,6 +13,7 @@ namespace ox {
template class BasicString<8>;
static_assert(StringView("Write") != String(""));
static_assert(ox::strcmp(String{}.c_str(), "\0") == 0);
static_assert(String("Write") != StringView(""));
static_assert(String("Write") == StringView("Write"));
static_assert(String(StringView("Write")) == StringView("Write"));

View File

@ -30,7 +30,7 @@ constexpr ox::IString<21> itoa(Integer v) noexcept;
template<std::size_t SmallStringSize_v>
class BasicString {
private:
Vector<char, SmallStringSize_v> m_buff{1};
Vector<char, SmallStringSize_v> m_buff;
public:
static constexpr auto SmallStringSize = SmallStringSize_v;
@ -193,6 +193,7 @@ class BasicString {
constexpr BasicString substr(std::size_t begin, std::size_t end) const noexcept;
constexpr void resize(size_t sz) noexcept {
++sz;
m_buff.resize(sz);
m_buff[sz - 1] = 0;
}
@ -248,28 +249,20 @@ class BasicString {
};
template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v>::BasicString() noexcept = default;
constexpr BasicString<SmallStringSize_v>::BasicString() noexcept {
m_buff.resize(1);
}
template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v>::BasicString(std::size_t cap) noexcept: m_buff(cap + 1) {}
template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v>::BasicString(const char *str) noexcept {
if (!m_buff.empty()) {
m_buff[0] = 0;
} else {
m_buff.push_back(0);
}
set(str);
}
template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v>::BasicString(const char8_t *str) noexcept {
if (!m_buff.empty()) {
m_buff[0] = 0;
} else {
m_buff.push_back(0);
}
set(str);
}
@ -277,21 +270,15 @@ template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v>::BasicString(const char *str, std::size_t size) noexcept {
m_buff.resize(size + 1);
ox::listcpy(m_buff.data(), str, size);
m_buff[size] = 0;
}
template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v>::BasicString(StringLiteral const&str) noexcept:
BasicString(StringView{str.data(), str.bytes()}) {
BasicString(StringView{str.data(), str.len()}) {
}
template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v>::BasicString(CRStringView str) noexcept {
if (m_buff.empty()) {
m_buff.push_back(0);
} else {
m_buff[0] = 0;
}
set(str);
}
@ -302,7 +289,8 @@ constexpr BasicString<SmallStringSize_v>::BasicString(const BasicString &other)
template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v>::BasicString(BasicString &&other) noexcept: m_buff(std::move(other.m_buff)) {
other.m_buff.push_back(0);
other.m_buff.resize(1);
other.m_buff[0] = 0;
}
template<std::size_t SmallStringSize_v>
@ -348,7 +336,8 @@ template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operator=(BasicString &&src) noexcept {
if (this != &src) {
m_buff = std::move(src.m_buff);
src.m_buff.push_back(0);
src.m_buff.resize(1);
src.m_buff[0] = 0;
}
return *this;
}