[ox/std] Remove some unnecessary/breaking checks in BasicString::set functions

This commit is contained in:
Gary Talent 2022-12-29 02:12:11 -06:00
parent b08559b3f3
commit 887d3b3d13

View File

@ -179,16 +179,10 @@ class BasicString {
constexpr BasicString substr(std::size_t begin, std::size_t end) const noexcept;
[[nodiscard]]
constexpr bool beginsWith(const char *ending) const noexcept;
constexpr bool beginsWith(CRStringView ending) const noexcept;
[[nodiscard]]
constexpr bool beginsWith(const BasicString &ending) const noexcept;
[[nodiscard]]
constexpr bool endsWith(const char *ending) const noexcept;
[[nodiscard]]
constexpr bool endsWith(const BasicString &ending) const noexcept;
constexpr bool endsWith(CRStringView ending) const noexcept;
[[nodiscard]]
constexpr const char *data() const noexcept {
@ -238,7 +232,7 @@ class BasicString {
template<std::size_t OtherSize>
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;
@ -291,10 +285,10 @@ constexpr BasicString<SmallStringSize_v>::BasicString(const char *str, std::size
template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v>::BasicString(CRStringView str) noexcept {
if (m_buff.size()) {
m_buff[0] = 0;
} else {
if (m_buff.empty()) {
m_buff.push_back(0);
} else {
m_buff[0] = 0;
}
set(str);
}
@ -521,27 +515,15 @@ constexpr BasicString<SmallStringSize_v> BasicString<SmallStringSize_v>::substr(
}
template<std::size_t SmallStringSize_v>
constexpr bool BasicString<SmallStringSize_v>::beginsWith(const char *beginning) const noexcept {
const auto beginningLen = ox::min(ox_strlen(beginning), len());
constexpr bool BasicString<SmallStringSize_v>::beginsWith(CRStringView beginning) const noexcept {
const auto beginningLen = ox::min(beginning.len(), len());
return ox_strncmp(data(), beginning, beginningLen) == 0;
}
template<std::size_t SmallStringSize_v>
constexpr bool BasicString<SmallStringSize_v>::beginsWith(const BasicString &beginning) 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 {
constexpr bool BasicString<SmallStringSize_v>::endsWith(CRStringView ending) const noexcept {
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>
@ -555,11 +537,10 @@ template<std::size_t SmallStringSize_v>
constexpr std::size_t BasicString<SmallStringSize_v>::len() const noexcept {
std::size_t length = 0;
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 & 128) == 0) { // normal ASCII character
++length;
} else if ((b & (256 << 6)) == (256 << 6)) { // start of UTF-8 character
// normal ASCII character or start of UTF-8 character
if ((b & 128) == 0 || (b & (256 << 6)) == (256 << 6)) {
++length;
}
} else {
@ -573,41 +554,33 @@ template<std::size_t SmallStringSize_v>
template<std::size_t OtherSize>
constexpr void BasicString<SmallStringSize_v>::set(const BasicString<OtherSize> &src) noexcept {
std::size_t strBytes = src.bytes();
if (strBytes > 1) {
m_buff.resize(strBytes);
copy_n(src.begin(), strBytes, m_buff.data());
m_buff.back().value = 0;
}
m_buff.resize(strBytes);
copy_n(src.begin(), strBytes, m_buff.data());
m_buff.back().value = 0;
}
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();
if (strBytes > 1) {
m_buff.resize(strBytes + 1);
copy_n(str.data(), strBytes, m_buff.data());
m_buff.back().value = 0;
}
m_buff.resize(strBytes + 1);
copy_n(str.data(), strBytes, m_buff.data());
m_buff.back().value = 0;
}
template<std::size_t SmallStringSize_v>
constexpr void BasicString<SmallStringSize_v>::set(const char *str) noexcept {
std::size_t strBytes = ox_strlen(str) + 1;
if (strBytes > 1) {
m_buff.resize(strBytes);
copy_n(str, strBytes, m_buff.data());
m_buff.back().value = 0;
}
m_buff.resize(strBytes);
copy_n(str, strBytes, m_buff.data());
m_buff.back().value = 0;
}
template<std::size_t SmallStringSize_v>
constexpr void BasicString<SmallStringSize_v>::set(const char8_t *str) noexcept {
std::size_t strBytes = ox_strlen(str) + 1;
if (strBytes > 1) {
m_buff.resize(strBytes);
memcpy(m_buff.data(), str, strBytes);
m_buff.back().value = 0;
}
m_buff.resize(strBytes);
memcpy(m_buff.data(), str, strBytes);
m_buff.back().value = 0;
}
extern template class BasicString<8>;