From 105e4ad880a810300bf4b3a0a0752ae58924667b Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 15 Sep 2021 17:12:51 +0800 Subject: ... --- src/common/String.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'src/common/String.cpp') diff --git a/src/common/String.cpp b/src/common/String.cpp index 6d958688..602bcc51 100644 --- a/src/common/String.cpp +++ b/src/common/String.cpp @@ -1,8 +1,11 @@ #include "cru/common/String.hpp" #include "cru/common/StringUtil.hpp" +#include + #include #include +#include #include namespace cru { @@ -302,4 +305,42 @@ void FormatAppendFromFormatTokenList( } } // namespace details +StringView::StringView(const std::uint16_t* ptr) + : ptr_(ptr), size_(GetStrSize(ptr)) {} + +int StringView::Compare(const StringView& other) const { + const_iterator i1 = cbegin(); + const_iterator i2 = other.cbegin(); + + const_iterator end1 = cend(); + const_iterator end2 = other.cend(); + + while (i1 != end1 && i2 != end2) { + int r = cru::Compare(*i1, *i2); + if (r != 0) return r; + i1++; + i2++; + } + + if (i1 == end1) { + if (i2 == end2) { + return 0; + } else { + return -1; + } + } else { + return 1; + } +} + +StringView StringView::substr(Index pos) { + Expects(pos >= 0 && pos < size_); + return StringView(ptr_ + pos, size_ - pos); +} + +StringView StringView::substr(Index pos, Index size) { + Expects(pos >= 0 && pos < size_); + return StringView(ptr_ + pos, std::min(size, size_ - pos)); +} + } // namespace cru -- cgit v1.2.3