[ox/std] Fix String::c_str to always retrun a valid C str
All checks were successful
Build / build (push) Successful in 2m36s
All checks were successful
Build / build (push) Successful in 2m36s
This commit is contained in:
parent
e4c3866017
commit
95a69b72b5
1
deps/ox/src/ox/std/string.cpp
vendored
1
deps/ox/src/ox/std/string.cpp
vendored
@ -13,6 +13,7 @@ namespace ox {
|
|||||||
template class BasicString<8>;
|
template class BasicString<8>;
|
||||||
|
|
||||||
static_assert(StringView("Write") != String(""));
|
static_assert(StringView("Write") != String(""));
|
||||||
|
static_assert(ox::strcmp(String{}.c_str(), "\0") == 0);
|
||||||
static_assert(String("Write") != StringView(""));
|
static_assert(String("Write") != StringView(""));
|
||||||
static_assert(String("Write") == StringView("Write"));
|
static_assert(String("Write") == StringView("Write"));
|
||||||
static_assert(String(StringView("Write")) == StringView("Write"));
|
static_assert(String(StringView("Write")) == StringView("Write"));
|
||||||
|
31
deps/ox/src/ox/std/string.hpp
vendored
31
deps/ox/src/ox/std/string.hpp
vendored
@ -30,7 +30,7 @@ constexpr ox::IString<21> itoa(Integer v) noexcept;
|
|||||||
template<std::size_t SmallStringSize_v>
|
template<std::size_t SmallStringSize_v>
|
||||||
class BasicString {
|
class BasicString {
|
||||||
private:
|
private:
|
||||||
Vector<char, SmallStringSize_v> m_buff{1};
|
Vector<char, SmallStringSize_v> m_buff;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static constexpr auto SmallStringSize = SmallStringSize_v;
|
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 BasicString substr(std::size_t begin, std::size_t end) const noexcept;
|
||||||
|
|
||||||
constexpr void resize(size_t sz) noexcept {
|
constexpr void resize(size_t sz) noexcept {
|
||||||
|
++sz;
|
||||||
m_buff.resize(sz);
|
m_buff.resize(sz);
|
||||||
m_buff[sz - 1] = 0;
|
m_buff[sz - 1] = 0;
|
||||||
}
|
}
|
||||||
@ -248,28 +249,20 @@ class BasicString {
|
|||||||
};
|
};
|
||||||
|
|
||||||
template<std::size_t SmallStringSize_v>
|
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>
|
template<std::size_t SmallStringSize_v>
|
||||||
constexpr BasicString<SmallStringSize_v>::BasicString(std::size_t cap) noexcept: m_buff(cap + 1) {}
|
constexpr BasicString<SmallStringSize_v>::BasicString(std::size_t cap) noexcept: m_buff(cap + 1) {}
|
||||||
|
|
||||||
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()) {
|
|
||||||
m_buff[0] = 0;
|
|
||||||
} else {
|
|
||||||
m_buff.push_back(0);
|
|
||||||
}
|
|
||||||
set(str);
|
set(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<std::size_t SmallStringSize_v>
|
template<std::size_t SmallStringSize_v>
|
||||||
constexpr BasicString<SmallStringSize_v>::BasicString(const char8_t *str) noexcept {
|
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);
|
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 {
|
constexpr BasicString<SmallStringSize_v>::BasicString(const char *str, std::size_t size) noexcept {
|
||||||
m_buff.resize(size + 1);
|
m_buff.resize(size + 1);
|
||||||
ox::listcpy(m_buff.data(), str, size);
|
ox::listcpy(m_buff.data(), str, size);
|
||||||
m_buff[size] = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<std::size_t SmallStringSize_v>
|
template<std::size_t SmallStringSize_v>
|
||||||
constexpr BasicString<SmallStringSize_v>::BasicString(StringLiteral const&str) noexcept:
|
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>
|
template<std::size_t SmallStringSize_v>
|
||||||
constexpr BasicString<SmallStringSize_v>::BasicString(CRStringView str) noexcept {
|
constexpr BasicString<SmallStringSize_v>::BasicString(CRStringView str) noexcept {
|
||||||
if (m_buff.empty()) {
|
|
||||||
m_buff.push_back(0);
|
|
||||||
} else {
|
|
||||||
m_buff[0] = 0;
|
|
||||||
}
|
|
||||||
set(str);
|
set(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -302,7 +289,8 @@ constexpr BasicString<SmallStringSize_v>::BasicString(const BasicString &other)
|
|||||||
|
|
||||||
template<std::size_t SmallStringSize_v>
|
template<std::size_t SmallStringSize_v>
|
||||||
constexpr BasicString<SmallStringSize_v>::BasicString(BasicString &&other) noexcept: m_buff(std::move(other.m_buff)) {
|
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>
|
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 {
|
constexpr BasicString<SmallStringSize_v> &BasicString<SmallStringSize_v>::operator=(BasicString &&src) noexcept {
|
||||||
if (this != &src) {
|
if (this != &src) {
|
||||||
m_buff = std::move(src.m_buff);
|
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;
|
return *this;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user