diff options
author | crupest <crupest@outlook.com> | 2021-09-15 17:12:51 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-09-15 17:12:51 +0800 |
commit | 105e4ad880a810300bf4b3a0a0752ae58924667b (patch) | |
tree | da9fb12923160312d2d989514b0e4df243907b66 /src/common/String.cpp | |
parent | d6cd1c46889e94b325244e5ba5ccea97cc856952 (diff) | |
download | cru-105e4ad880a810300bf4b3a0a0752ae58924667b.tar.gz cru-105e4ad880a810300bf4b3a0a0752ae58924667b.tar.bz2 cru-105e4ad880a810300bf4b3a0a0752ae58924667b.zip |
...
Diffstat (limited to 'src/common/String.cpp')
-rw-r--r-- | src/common/String.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
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 <gsl/gsl> + #include <algorithm> #include <cstring> +#include <functional> #include <string_view> 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 |