[ox/std] Add beginsWith and endsWith variants that that cingle chars

This commit is contained in:
2025-07-26 18:31:03 -05:00
parent ee9a3e1152
commit 934f0c9232

View File

@ -21,7 +21,7 @@ OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
namespace ox { namespace ox {
[[nodiscard]] [[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) { if (str.size() >= pos) {
return {&str[pos], 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]] [[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) { if (str.size() >= start && end >= start) {
return {&str[start], end - start}; return {&str[start], end - start};
} }
return {}; 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]] [[nodiscard]]
constexpr bool beginsWith(StringViewCR base, StringViewCR beginning) noexcept { constexpr bool beginsWith(StringViewCR base, StringViewCR beginning) noexcept {
const auto beginningLen = ox::min(beginning.size(), base.size()); const auto beginningLen = ox::min(beginning.size(), base.size());
@ -49,7 +59,7 @@ constexpr bool endsWith(StringViewCR base, StringViewCR ending) noexcept {
} }
[[nodiscard]] [[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; std::size_t i = 0;
for (; i < str.size(); ++i) { for (; i < str.size(); ++i) {
if (str[i] == search) { if (str[i] == search) {
@ -72,7 +82,7 @@ constexpr std::size_t find(StringViewCR str, StringViewCR search) noexcept {
template<std::size_t smallSz = 0> template<std::size_t smallSz = 0>
[[nodiscard]] [[nodiscard]]
constexpr ox::Vector<ox::StringView, smallSz> split(StringViewCR str, char del) noexcept { constexpr ox::Vector<ox::StringView, smallSz> split(StringViewCR str, char const del) noexcept {
ox::Vector<ox::StringView, smallSz> out; ox::Vector<ox::StringView, smallSz> out;
constexpr auto nextSeg = [](StringViewCR current, char del) { constexpr auto nextSeg = [](StringViewCR current, char del) {
return substr(current, find(current, del) + 1); return substr(current, find(current, del) + 1);
@ -105,7 +115,7 @@ constexpr ox::Vector<ox::StringView, smallSz> split(StringViewCR str, StringView
} }
[[nodiscard]] [[nodiscard]]
constexpr ox::Result<std::size_t> lastIndexOf(ox::StringViewCR str, int character) noexcept { constexpr ox::Result<std::size_t> lastIndexOf(ox::StringViewCR str, int const character) noexcept {
ox::Result<std::size_t> retval = ox::Error(1, "Character not found"); ox::Result<std::size_t> retval = ox::Error(1, "Character not found");
for (auto i = static_cast<int>(str.bytes() - 1); i >= 0; --i) { for (auto i = static_cast<int>(str.bytes() - 1); i >= 0; --i) {
if (str[static_cast<std::size_t>(i)] == character) { if (str[static_cast<std::size_t>(i)] == character) {