[ox/std] Remove some unnecessary/breaking checks in BasicString::set functions
This commit is contained in:
parent
b08559b3f3
commit
887d3b3d13
79
deps/ox/src/ox/std/string.hpp
vendored
79
deps/ox/src/ox/std/string.hpp
vendored
@ -179,16 +179,10 @@ 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;
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
constexpr bool beginsWith(const char *ending) const noexcept;
|
constexpr bool beginsWith(CRStringView ending) const noexcept;
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
constexpr bool beginsWith(const BasicString &ending) const noexcept;
|
constexpr bool endsWith(CRStringView ending) const noexcept;
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
constexpr bool endsWith(const char *ending) const noexcept;
|
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
constexpr bool endsWith(const BasicString &ending) const noexcept;
|
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
constexpr const char *data() const noexcept {
|
constexpr const char *data() const noexcept {
|
||||||
@ -238,7 +232,7 @@ class BasicString {
|
|||||||
template<std::size_t OtherSize>
|
template<std::size_t OtherSize>
|
||||||
constexpr void set(const BasicString<OtherSize> &src) noexcept;
|
constexpr void set(const BasicString<OtherSize> &src) noexcept;
|
||||||
|
|
||||||
constexpr void set(StringView str) noexcept;
|
constexpr void set(CRStringView str) noexcept;
|
||||||
|
|
||||||
constexpr void set(const char *str) noexcept;
|
constexpr void set(const char *str) noexcept;
|
||||||
|
|
||||||
@ -291,10 +285,10 @@ constexpr BasicString<SmallStringSize_v>::BasicString(const char *str, std::size
|
|||||||
|
|
||||||
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.size()) {
|
if (m_buff.empty()) {
|
||||||
m_buff[0] = 0;
|
|
||||||
} else {
|
|
||||||
m_buff.push_back(0);
|
m_buff.push_back(0);
|
||||||
|
} else {
|
||||||
|
m_buff[0] = 0;
|
||||||
}
|
}
|
||||||
set(str);
|
set(str);
|
||||||
}
|
}
|
||||||
@ -521,27 +515,15 @@ constexpr BasicString<SmallStringSize_v> BasicString<SmallStringSize_v>::substr(
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<std::size_t SmallStringSize_v>
|
template<std::size_t SmallStringSize_v>
|
||||||
constexpr bool BasicString<SmallStringSize_v>::beginsWith(const char *beginning) const noexcept {
|
constexpr bool BasicString<SmallStringSize_v>::beginsWith(CRStringView beginning) const noexcept {
|
||||||
const auto beginningLen = ox::min(ox_strlen(beginning), len());
|
const auto beginningLen = ox::min(beginning.len(), len());
|
||||||
return ox_strncmp(data(), beginning, beginningLen) == 0;
|
return ox_strncmp(data(), beginning, beginningLen) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<std::size_t SmallStringSize_v>
|
template<std::size_t SmallStringSize_v>
|
||||||
constexpr bool BasicString<SmallStringSize_v>::beginsWith(const BasicString &beginning) const noexcept {
|
constexpr bool BasicString<SmallStringSize_v>::endsWith(CRStringView ending) const noexcept {
|
||||||
const auto sz = ox::min(beginning.len(), len());;
|
|
||||||
return ox_strncmp(data(), beginning.c_str(), sz) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<std::size_t SmallStringSize_v>
|
|
||||||
constexpr bool BasicString<SmallStringSize_v>::endsWith(const char *ending) const noexcept {
|
|
||||||
const auto endingLen = ox_strlen(ending);
|
|
||||||
return len() >= endingLen && ox_strcmp(data() + (len() - endingLen), ending) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<std::size_t SmallStringSize_v>
|
|
||||||
constexpr bool BasicString<SmallStringSize_v>::endsWith(const BasicString &ending) const noexcept {
|
|
||||||
const auto endingLen = ending.len();
|
const auto endingLen = ending.len();
|
||||||
return len() >= endingLen && ox_strcmp(data() + (len() - endingLen), ending.c_str()) == 0;
|
return len() >= endingLen && ox_strcmp(data() + (len() - endingLen), ending) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<std::size_t SmallStringSize_v>
|
template<std::size_t SmallStringSize_v>
|
||||||
@ -555,11 +537,10 @@ template<std::size_t SmallStringSize_v>
|
|||||||
constexpr std::size_t BasicString<SmallStringSize_v>::len() const noexcept {
|
constexpr std::size_t BasicString<SmallStringSize_v>::len() const noexcept {
|
||||||
std::size_t length = 0;
|
std::size_t length = 0;
|
||||||
for (const auto c : m_buff) {
|
for (const auto c : m_buff) {
|
||||||
auto b = static_cast<uint8_t>(c);
|
const auto b = static_cast<uint8_t>(c);
|
||||||
if (b) {
|
if (b) {
|
||||||
if ((b & 128) == 0) { // normal ASCII character
|
// normal ASCII character or start of UTF-8 character
|
||||||
++length;
|
if ((b & 128) == 0 || (b & (256 << 6)) == (256 << 6)) {
|
||||||
} else if ((b & (256 << 6)) == (256 << 6)) { // start of UTF-8 character
|
|
||||||
++length;
|
++length;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -573,41 +554,33 @@ template<std::size_t SmallStringSize_v>
|
|||||||
template<std::size_t OtherSize>
|
template<std::size_t OtherSize>
|
||||||
constexpr void BasicString<SmallStringSize_v>::set(const BasicString<OtherSize> &src) noexcept {
|
constexpr void BasicString<SmallStringSize_v>::set(const BasicString<OtherSize> &src) noexcept {
|
||||||
std::size_t strBytes = src.bytes();
|
std::size_t strBytes = src.bytes();
|
||||||
if (strBytes > 1) {
|
m_buff.resize(strBytes);
|
||||||
m_buff.resize(strBytes);
|
copy_n(src.begin(), strBytes, m_buff.data());
|
||||||
copy_n(src.begin(), strBytes, m_buff.data());
|
m_buff.back().value = 0;
|
||||||
m_buff.back().value = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<std::size_t SmallStringSize_v>
|
template<std::size_t SmallStringSize_v>
|
||||||
constexpr void BasicString<SmallStringSize_v>::set(StringView str) noexcept {
|
constexpr void BasicString<SmallStringSize_v>::set(CRStringView str) noexcept {
|
||||||
std::size_t strBytes = str.bytes();
|
std::size_t strBytes = str.bytes();
|
||||||
if (strBytes > 1) {
|
m_buff.resize(strBytes + 1);
|
||||||
m_buff.resize(strBytes + 1);
|
copy_n(str.data(), strBytes, m_buff.data());
|
||||||
copy_n(str.data(), strBytes, m_buff.data());
|
m_buff.back().value = 0;
|
||||||
m_buff.back().value = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<std::size_t SmallStringSize_v>
|
template<std::size_t SmallStringSize_v>
|
||||||
constexpr void BasicString<SmallStringSize_v>::set(const char *str) noexcept {
|
constexpr void BasicString<SmallStringSize_v>::set(const char *str) noexcept {
|
||||||
std::size_t strBytes = ox_strlen(str) + 1;
|
std::size_t strBytes = ox_strlen(str) + 1;
|
||||||
if (strBytes > 1) {
|
m_buff.resize(strBytes);
|
||||||
m_buff.resize(strBytes);
|
copy_n(str, strBytes, m_buff.data());
|
||||||
copy_n(str, strBytes, m_buff.data());
|
m_buff.back().value = 0;
|
||||||
m_buff.back().value = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<std::size_t SmallStringSize_v>
|
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;
|
||||||
if (strBytes > 1) {
|
m_buff.resize(strBytes);
|
||||||
m_buff.resize(strBytes);
|
memcpy(m_buff.data(), str, strBytes);
|
||||||
memcpy(m_buff.data(), str, strBytes);
|
m_buff.back().value = 0;
|
||||||
m_buff.back().value = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extern template class BasicString<8>;
|
extern template class BasicString<8>;
|
||||||
|
Loading…
Reference in New Issue
Block a user