[ox/std] Ensure ox::String always has at least a null terminator

This commit is contained in:
Gary Talent 2024-05-29 02:15:30 -05:00
parent 2761f23d31
commit 67cf3ae837

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;
Vector<char, SmallStringSize_v> m_buff{1};
public:
static constexpr auto SmallStringSize = SmallStringSize_v;
@ -248,21 +248,10 @@ class BasicString {
};
template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v>::BasicString() noexcept {
if (!m_buff.empty()) {
m_buff[0] = 0;
} else {
m_buff.push_back(0);
}
}
constexpr BasicString<SmallStringSize_v>::BasicString() noexcept = default;
template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v>::BasicString(std::size_t cap) noexcept: m_buff(cap + 1) {
// GCC complains if you don't do this pretty unnecessary size check
if (!m_buff.empty()) {
m_buff[0] = 0;
}
}
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 {
@ -313,6 +302,7 @@ 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);
}
template<std::size_t SmallStringSize_v>
@ -358,6 +348,7 @@ 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);
}
return *this;
}