diff --git a/deps/ox/src/ox/std/CMakeLists.txt b/deps/ox/src/ox/std/CMakeLists.txt index d0e7079a..03a89c85 100644 --- a/deps/ox/src/ox/std/CMakeLists.txt +++ b/deps/ox/src/ox/std/CMakeLists.txt @@ -33,6 +33,7 @@ add_library( substitutes.cpp stacktrace.cpp string.cpp + stringview.cpp strops.cpp trace.cpp typetraits.cpp diff --git a/deps/ox/src/ox/std/bstring.hpp b/deps/ox/src/ox/std/bstring.hpp index dbfeedf8..8d1f8cbe 100644 --- a/deps/ox/src/ox/std/bstring.hpp +++ b/deps/ox/src/ox/std/bstring.hpp @@ -143,6 +143,7 @@ template constexpr BString &BString::operator+=(StringView s) noexcept { std::size_t strLen = s.bytes(); oxIgnoreError(append(s.data(), strLen)); + return *this; } template diff --git a/deps/ox/src/ox/std/string.hpp b/deps/ox/src/ox/std/string.hpp index 620cb6e6..c697f8b3 100644 --- a/deps/ox/src/ox/std/string.hpp +++ b/deps/ox/src/ox/std/string.hpp @@ -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 BasicString::substr( return out; } -template -constexpr bool BasicString::beginsWith(CRStringView beginning) const noexcept { - const auto beginningLen = ox::min(beginning.len(), len()); - return ox_strncmp(data(), beginning, beginningLen) == 0; -} - -template -constexpr bool BasicString::endsWith(CRStringView ending) const noexcept { - const auto endingLen = ending.len(); - return len() >= endingLen && ox_strcmp(data() + (len() - endingLen), ending) == 0; -} - template constexpr std::size_t BasicString::bytes() const noexcept { std::size_t i; diff --git a/deps/ox/src/ox/std/stringview.cpp b/deps/ox/src/ox/std/stringview.cpp new file mode 100644 index 00000000..7d053d4d --- /dev/null +++ b/deps/ox/src/ox/std/stringview.cpp @@ -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")); diff --git a/deps/ox/src/ox/std/stringview.hpp b/deps/ox/src/ox/std/stringview.hpp index 3a2ed0a0..67972216 100644 --- a/deps/ox/src/ox/std/stringview.hpp +++ b/deps/ox/src/ox/std/stringview.hpp @@ -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 constexpr StringView(const BasicString &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")); - } - diff --git a/deps/ox/src/ox/std/test/tests.cpp b/deps/ox/src/ox/std/test/tests.cpp index afbdc492..5ffbf490 100644 --- a/deps/ox/src/ox/std/test/tests.cpp +++ b/deps/ox/src/ox/std/test/tests.cpp @@ -75,17 +75,17 @@ static std::map 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); } }, diff --git a/deps/ox/src/ox/std/trace.hpp b/deps/ox/src/ox/std/trace.hpp index 21d5621b..6504ee33 100644 --- a/deps/ox/src/ox/std/trace.hpp +++ b/deps/ox/src/ox/std/trace.hpp @@ -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 { diff --git a/deps/ox/src/ox/std/tracehook.cpp b/deps/ox/src/ox/std/tracehook.cpp index 0fe550ed..f238845c 100644 --- a/deps/ox/src/ox/std/tracehook.cpp +++ b/deps/ox/src/ox/std/tracehook.cpp @@ -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) {