From 9ce177e13ebd506b467f69fc1135dc339d19ca2a Mon Sep 17 00:00:00 2001 From: ChUrl Date: Mon, 1 Aug 2022 20:43:38 +0200 Subject: [PATCH] add stringview.cc --- c_os/user/lib/StringView.cc | 12 ++++++++++++ c_os/user/lib/StringView.h | 30 ++++-------------------------- 2 files changed, 16 insertions(+), 26 deletions(-) create mode 100644 c_os/user/lib/StringView.cc diff --git a/c_os/user/lib/StringView.cc b/c_os/user/lib/StringView.cc new file mode 100644 index 0000000..7cdc9b5 --- /dev/null +++ b/c_os/user/lib/StringView.cc @@ -0,0 +1,12 @@ +#include "user/lib/StringView.h" + +bse::string_view bse::string_view::substring(std::size_t first, std::size_t last) const { + if (first < 0 || first > len || last <= first || last > len) { + return nullptr; + } + + string_view new_view; + new_view.len = last - first; + new_view.buf = &buf[first]; + return new_view; +} \ No newline at end of file diff --git a/c_os/user/lib/StringView.h b/c_os/user/lib/StringView.h index 30b2fd8..42164a6 100644 --- a/c_os/user/lib/StringView.h +++ b/c_os/user/lib/StringView.h @@ -22,39 +22,17 @@ namespace bse { string_view(const char* str) : len(strlen(str)), buf(str) {} string_view(const string& str) : len(str.size()), buf(static_cast(str)) {} -// iterator begin() { return iterator(buf); } iterator begin() const { return iterator(buf); } -// iterator end() { return iterator(&buf[len]); } iterator end() const { return iterator(&buf[len]); } -// explicit operator const char*() { return buf; } explicit operator const char*() const { return buf; } - -// char operator[](std::size_t pos) { return buf[pos]; } char operator[](std::size_t pos) const { return buf[pos]; } + bool operator==(const string_view& other) const { return buf == other.buf; } + bool operator!=(const string_view& other) const { return buf != other.buf; } - bool operator==(const string_view& other) const { - return buf == other.buf; - } + std::size_t size() const { return len; } - bool operator!=(const string_view& other) const { - return buf != other.buf; - } - - string_view substring(std::size_t first, std::size_t last) const { - if (first < 0 || first > len || last <= first || last > len) { - return nullptr; - } - - string_view new_view; - new_view.len = last - first; - new_view.buf = &buf[first]; - return new_view; - } - - std::size_t size() const { - return len; - } + string_view substring(std::size_t first, std::size_t last) const; }; } // namespace bse