[ox/std] Add String::endsWith

This commit is contained in:
Gary Talent 2021-04-17 16:35:14 -05:00
parent 49fb4d0f0b
commit 8f7504c1c8
3 changed files with 32 additions and 0 deletions

View File

@ -177,6 +177,20 @@ char &String::operator[](std::size_t i) noexcept {
return m_buff[i];
}
String String::substr(std::size_t pos) const noexcept {
return m_buff.data() + pos;
}
bool String::endsWith(const char *ending) const noexcept {
const auto endingLen = ox_strlen(ending);
return len() >= endingLen && ox_strcmp(data() + (len() - endingLen), ending) == 0;
}
bool String::endsWith(const String &ending) const noexcept {
const auto endingLen = ending.len();
return len() >= endingLen && ox_strcmp(data() + (len() - endingLen), ending.c_str()) == 0;
}
std::size_t String::len() const noexcept {
std::size_t length = 0;
for (std::size_t i = 0; i < m_buff.size(); i++) {

View File

@ -82,6 +82,20 @@ class String {
char &operator[](std::size_t i) noexcept;
[[nodiscard]]
String substr(std::size_t pos) const noexcept;
[[nodiscard]]
bool endsWith(const char *other) const noexcept;
[[nodiscard]]
bool endsWith(const String &other) const noexcept;
[[nodiscard]]
constexpr const char *data() const noexcept {
return m_buff.data();
}
[[nodiscard]]
constexpr char *data() noexcept {
return m_buff.data();

View File

@ -75,6 +75,10 @@ std::map<std::string, std::function<ox::Error()>> tests = {
oxAssert(s == "asdf", "String assign broken");
s += "aoeu";
oxAssert(s == "asdfaoeu", "String append broken");
ox::String ending = "asdf";
oxAssert(ending.endsWith("df"), "String::endsWith is broken");
oxAssert(!ending.endsWith("awefawe"), "String::endsWith is broken");
oxAssert(!ending.endsWith("eu"), "String::endsWith is broken");
return OxError(0);
}
},