From 9161a1a8ef9962ac7dd8f73d59ad2768160fa00e Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 23 Mar 2022 03:08:04 -0500 Subject: [PATCH] [ox/std] Add BasicString::beginsWith --- deps/ox/src/ox/std/string.hpp | 18 ++++++++++++++++++ deps/ox/src/ox/std/test/tests.cpp | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/deps/ox/src/ox/std/string.hpp b/deps/ox/src/ox/std/string.hpp index 04a1266b..6714eec4 100644 --- a/deps/ox/src/ox/std/string.hpp +++ b/deps/ox/src/ox/std/string.hpp @@ -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 BasicString::substr(std: return out; } +template +constexpr bool BasicString::beginsWith(const char *beginning) const noexcept { + const auto beginningLen = ox::min(ox_strlen(beginning), len()); + return ox_strncmp(data(), beginning, beginningLen) == 0; +} + +template +constexpr bool BasicString::beginsWith(const BasicString &beginning) const noexcept { + const auto sz = ox::min(beginning.len(), len());; + return ox_strncmp(data(), beginning.c_str(), sz) == 0; +} + template constexpr bool BasicString::endsWith(const char *ending) const noexcept { const auto endingLen = ox_strlen(ending); diff --git a/deps/ox/src/ox/std/test/tests.cpp b/deps/ox/src/ox/std/test/tests.cpp index d5a6e7bb..a2a4a96c 100644 --- a/deps/ox/src/ox/std/test/tests.cpp +++ b/deps/ox/src/ox/std/test/tests.cpp @@ -78,6 +78,11 @@ std::map> 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");