[ox/std] Add BasicString::beginsWith

This commit is contained in:
Gary Talent 2022-03-23 03:08:04 -05:00
parent ccfc7d5405
commit 9161a1a8ef
2 changed files with 23 additions and 0 deletions

View File

@ -166,6 +166,12 @@ class BasicString {
[[nodiscard]]
constexpr BasicString substr(std::size_t begin, std::size_t end) const noexcept;
[[nodiscard]]
constexpr bool beginsWith(const char *ending) const noexcept;
[[nodiscard]]
constexpr bool beginsWith(const BasicString &ending) const noexcept;
[[nodiscard]]
constexpr bool endsWith(const char *ending) const noexcept;
@ -454,6 +460,18 @@ constexpr BasicString<SmallStringSize> BasicString<SmallStringSize>::substr(std:
return out;
}
template<std::size_t SmallStringSize>
constexpr bool BasicString<SmallStringSize>::beginsWith(const char *beginning) const noexcept {
const auto beginningLen = ox::min(ox_strlen(beginning), len());
return ox_strncmp(data(), beginning, beginningLen) == 0;
}
template<std::size_t SmallStringSize>
constexpr bool BasicString<SmallStringSize>::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>
constexpr bool BasicString<SmallStringSize>::endsWith(const char *ending) const noexcept {
const auto endingLen = ox_strlen(ending);

View File

@ -78,6 +78,11 @@ std::map<std::string, std::function<ox::Error()>> tests = {
s += "aoeu";
oxAssert(s == "asdfaoeu", "String append broken");
ox::String ending = "asdf";
oxAssert(ending.beginsWith("as"), "String::beginsWith is broken");
oxAssert(!ending.beginsWith("aa"), "String::beginsWith is broken");
oxAssert(!ending.beginsWith("aaaaaaa"), "String::beginsWith is broken");
oxAssert(!ending.beginsWith("li"), "String::beginsWith is broken");
oxAssert(!ending.beginsWith("afoif"), "String::beginsWith is broken");
oxAssert(ending.endsWith("df"), "String::endsWith is broken");
oxAssert(!ending.endsWith("awefawe"), "String::endsWith is broken");
oxAssert(!ending.endsWith("eu"), "String::endsWith is broken");