[ox/std] Make String::beginsWtih and endsWith functions that take StringViews

This commit is contained in:
Gary Talent 2023-01-03 03:30:33 -06:00
parent 03378ebe43
commit 5508dc5dc0
8 changed files with 50 additions and 47 deletions

View File

@ -33,6 +33,7 @@ add_library(
substitutes.cpp
stacktrace.cpp
string.cpp
stringview.cpp
strops.cpp
trace.cpp
typetraits.cpp

View File

@ -143,6 +143,7 @@ template<std::size_t size>
constexpr BString<size> &BString<size>::operator+=(StringView s) noexcept {
std::size_t strLen = s.bytes();
oxIgnoreError(append(s.data(), strLen));
return *this;
}
template<std::size_t size>

View File

@ -178,12 +178,6 @@ class BasicString {
[[nodiscard]]
constexpr BasicString substr(std::size_t begin, std::size_t end) const noexcept;
[[nodiscard]]
constexpr bool beginsWith(CRStringView ending) const noexcept;
[[nodiscard]]
constexpr bool endsWith(CRStringView ending) const noexcept;
[[nodiscard]]
constexpr const char *data() const noexcept {
return m_buff.data();
@ -514,18 +508,6 @@ constexpr BasicString<SmallStringSize_v> BasicString<SmallStringSize_v>::substr(
return out;
}
template<std::size_t SmallStringSize_v>
constexpr bool BasicString<SmallStringSize_v>::beginsWith(CRStringView beginning) const noexcept {
const auto beginningLen = ox::min(beginning.len(), len());
return ox_strncmp(data(), beginning, beginningLen) == 0;
}
template<std::size_t SmallStringSize_v>
constexpr bool BasicString<SmallStringSize_v>::endsWith(CRStringView ending) const noexcept {
const auto endingLen = ending.len();
return len() >= endingLen && ox_strcmp(data() + (len() - endingLen), ending) == 0;
}
template<std::size_t SmallStringSize_v>
constexpr std::size_t BasicString<SmallStringSize_v>::bytes() const noexcept {
std::size_t i;

20
deps/ox/src/ox/std/stringview.cpp vendored Normal file
View File

@ -0,0 +1,20 @@
/*
* Copyright 2015 - 2022 gary@drinkingtea.net
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "stringview.hpp"
static_assert(ox::StringView("Read").bytes() == 4);
static_assert(ox::StringView("Read") == ox::StringView("Read"));
static_assert(ox::StringView("Read") != ox::StringView("Write"));
static_assert(ox::StringView("Write") != ox::StringView("Read"));
static_assert(ox::StringView("Write") != ox::StringView(""));
static_assert(ox::StringView("") != ox::StringView("Read"));
static_assert(ox::StringView("") == ox::StringView(""));
static_assert(ox::StringView("") == "");
static_assert("Read" == ox::StringView("Read"));
static_assert(ox::StringView("Read") == ox::StringView("Read"));

View File

@ -137,8 +137,11 @@ class StringView {
public:
constexpr StringView() noexcept = default;
constexpr StringView(const StringView &sv) noexcept: m_str(sv.m_str), m_len(sv.m_len) {
}
constexpr StringView(const StringView &sv) noexcept = default;
#ifdef OX_USE_STDLIB
constexpr StringView(const std::string_view &sv) noexcept: m_str(sv.data()), m_len(sv.size()) {}
#endif
template<std::size_t SmallStrSz>
constexpr StringView(const BasicString<SmallStrSz> &str) noexcept: m_str(str.c_str()), m_len(str.len()) {}
@ -263,6 +266,13 @@ class StringView {
using CRStringView = const StringView&;
[[nodiscard]]
constexpr bool beginsWith(CRStringView base, CRStringView beginning) noexcept {
const auto beginningLen = ox::min(beginning.len(), base.len());
return ox_strncmp(base.data(), beginning, beginningLen) == 0;
}
[[nodiscard]]
constexpr bool endsWith(CRStringView base, CRStringView ending) noexcept {
const auto endingLen = ending.len();
return base.len() >= endingLen && ox_strcmp(base.data() + (base.len() - endingLen), ending) == 0;
@ -274,16 +284,4 @@ constexpr auto toStdStringView(CRStringView sv) noexcept {
}
#endif
static_assert(StringView("Read").bytes() == 4);
static_assert(StringView("Read") == StringView("Read"));
static_assert(StringView("Read") != StringView("Write"));
static_assert(StringView("Write") != StringView("Read"));
static_assert(StringView("Write") != StringView(""));
static_assert(StringView("") != StringView("Read"));
static_assert(StringView("") == StringView(""));
static_assert(StringView("") == "");
static_assert("Read" == StringView("Read"));
static_assert(StringView("Read") == StringView("Read"));
}

View File

@ -75,17 +75,17 @@ static std::map<ox::String, ox::Error(*)()> tests = {
oxAssert(s == "asdf", "String assign broken");
s += "aoeu";
oxAssert(s == "asdfaoeu", "String append broken");
ox::String ending = "asdf";
oxAssert(ending.beginsWith("as"), "String::beginsWith is broken");
oxAssert(ending.beginsWith("asd"), "String::beginsWith is broken");
oxAssert(ending.beginsWith("asdf"), "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");
const ox::StringView str = "asdf";
oxAssert(beginsWith(str, "as"), "String beginsWith is broken");
oxAssert(beginsWith(str, "asd"), "String beginsWith is broken");
oxAssert(beginsWith(str, "asdf"), "String beginsWith is broken");
oxAssert(!beginsWith(str, "aa"), "String beginsWith is broken");
oxAssert(!beginsWith(str, "aaaaaaa"), "String beginsWith is broken");
oxAssert(!beginsWith(str, "li"), "String beginsWith is broken");
oxAssert(!beginsWith(str, "afoif"), "String beginsWith is broken");
oxAssert(endsWith(str, "df"), "String endsWith is broken");
oxAssert(!endsWith(str, "awefawe"), "String endsWith is broken");
oxAssert(!endsWith(str, "eu"), "String endsWith is broken");
return OxError(0);
}
},

View File

@ -59,11 +59,11 @@ class OutStream {
TraceMsg m_msg;
public:
constexpr OutStream(const char *file, int line, const char *ch, const String &msg) noexcept {
constexpr OutStream(const char *file, int line, const char *ch, const StringView &msg) noexcept {
m_msg.file = file;
m_msg.line = line;
m_msg.ch = ch;
m_msg.msg = msg.c_str();
m_msg.msg = msg;
}
constexpr OutStream(const char *file, int line, const char *ch, const char *msg = "") noexcept {
@ -182,7 +182,7 @@ constexpr OutStream &OutStream::operator<<(Integer_c auto v) noexcept {
class NullStream {
public:
constexpr NullStream(const char*, int, const char*, const String&) noexcept {
constexpr NullStream(const char*, int, const char*, const StringView&) noexcept {
}
constexpr NullStream(const char*, int, const char*, const char* = "") noexcept {

View File

@ -77,7 +77,8 @@ void oxTraceHook([[maybe_unused]] const char *file, [[maybe_unused]] int line,
} else if (ox_strcmp(ch, "stderr") == 0) {
printf("%s", msg);
} else if (ox_strcmp(ch, "error") == 0) {
std::cerr << "\033[31;1;1mERROR:\033[0m (" << file << ':' << line << "): " << msg << '\n';
//std::cerr << "\033[31;1;1mERROR:\033[0m (" << file << ':' << line << "): " << msg << '\n';
fprintf(stderr, "\033[31;1;1mERROR:\033[0m (%s:%d): %s\n", file, line, msg);
}
#else
if (ox_strcmp(ch, "info") == 0) {