[ox/std] Add String::endsWith
This commit is contained in:
parent
49fb4d0f0b
commit
8f7504c1c8
14
deps/ox/src/ox/std/string.cpp
vendored
14
deps/ox/src/ox/std/string.cpp
vendored
@ -177,6 +177,20 @@ char &String::operator[](std::size_t i) noexcept {
|
|||||||
return m_buff[i];
|
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 String::len() const noexcept {
|
||||||
std::size_t length = 0;
|
std::size_t length = 0;
|
||||||
for (std::size_t i = 0; i < m_buff.size(); i++) {
|
for (std::size_t i = 0; i < m_buff.size(); i++) {
|
||||||
|
14
deps/ox/src/ox/std/string.hpp
vendored
14
deps/ox/src/ox/std/string.hpp
vendored
@ -82,6 +82,20 @@ class String {
|
|||||||
|
|
||||||
char &operator[](std::size_t i) noexcept;
|
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]]
|
[[nodiscard]]
|
||||||
constexpr char *data() noexcept {
|
constexpr char *data() noexcept {
|
||||||
return m_buff.data();
|
return m_buff.data();
|
||||||
|
4
deps/ox/src/ox/std/test/tests.cpp
vendored
4
deps/ox/src/ox/std/test/tests.cpp
vendored
@ -75,6 +75,10 @@ std::map<std::string, std::function<ox::Error()>> tests = {
|
|||||||
oxAssert(s == "asdf", "String assign broken");
|
oxAssert(s == "asdf", "String assign broken");
|
||||||
s += "aoeu";
|
s += "aoeu";
|
||||||
oxAssert(s == "asdfaoeu", "String append broken");
|
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);
|
return OxError(0);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user