From 934f0c9232b450db3fca321571f6ced11fd98a16 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 26 Jul 2025 18:31:03 -0500 Subject: [PATCH] [ox/std] Add beginsWith and endsWith variants that that cingle chars --- deps/ox/src/ox/std/strops.hpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/deps/ox/src/ox/std/strops.hpp b/deps/ox/src/ox/std/strops.hpp index b2f3ec99..86251f44 100644 --- a/deps/ox/src/ox/std/strops.hpp +++ b/deps/ox/src/ox/std/strops.hpp @@ -21,7 +21,7 @@ OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) namespace ox { [[nodiscard]] -constexpr ox::StringView substr(ox::StringView const&str, std::size_t pos) noexcept { +constexpr StringView substr(StringViewCR str, std::size_t const pos) noexcept { if (str.size() >= pos) { return {&str[pos], str.size() - pos}; } @@ -29,13 +29,23 @@ constexpr ox::StringView substr(ox::StringView const&str, std::size_t pos) noexc } [[nodiscard]] -constexpr ox::StringView substr(ox::StringView const&str, std::size_t start, std::size_t end) noexcept { +constexpr StringView substr(StringViewCR str, std::size_t const start, std::size_t const end) noexcept { if (str.size() >= start && end >= start) { return {&str[start], end - start}; } return {}; } +[[nodiscard]] +constexpr bool beginsWith(StringViewCR base, char const beginning) noexcept { + return base.size() && base[0] == beginning; +} + +[[nodiscard]] +constexpr bool endsWith(StringViewCR base, char const ending) noexcept { + return base.size() && base[base.size() - 1] == ending; +} + [[nodiscard]] constexpr bool beginsWith(StringViewCR base, StringViewCR beginning) noexcept { const auto beginningLen = ox::min(beginning.size(), base.size()); @@ -49,7 +59,7 @@ constexpr bool endsWith(StringViewCR base, StringViewCR ending) noexcept { } [[nodiscard]] -constexpr std::size_t find(StringViewCR str, char search) noexcept { +constexpr std::size_t find(StringViewCR str, char const search) noexcept { std::size_t i = 0; for (; i < str.size(); ++i) { if (str[i] == search) { @@ -72,7 +82,7 @@ constexpr std::size_t find(StringViewCR str, StringViewCR search) noexcept { template [[nodiscard]] -constexpr ox::Vector split(StringViewCR str, char del) noexcept { +constexpr ox::Vector split(StringViewCR str, char const del) noexcept { ox::Vector out; constexpr auto nextSeg = [](StringViewCR current, char del) { return substr(current, find(current, del) + 1); @@ -105,7 +115,7 @@ constexpr ox::Vector split(StringViewCR str, StringView } [[nodiscard]] -constexpr ox::Result lastIndexOf(ox::StringViewCR str, int character) noexcept { +constexpr ox::Result lastIndexOf(ox::StringViewCR str, int const character) noexcept { ox::Result retval = ox::Error(1, "Character not found"); for (auto i = static_cast(str.bytes() - 1); i >= 0; --i) { if (str[static_cast(i)] == character) {