diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2022-08-22 19:06:49 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2022-08-22 19:06:49 +0100 |
commit | 1e1aa58aafc40c52a0d4dd64611f7bdba19e5ff3 (patch) | |
tree | fa3648092f172b14e5356066aeac0c21019f0d4e /lib/unicode.h | |
parent | Create a string_view like thing for utf8 strings (diff) | |
download | ilt-1e1aa58aafc40c52a0d4dd64611f7bdba19e5ff3.tar.bz2 ilt-1e1aa58aafc40c52a0d4dd64611f7bdba19e5ff3.tar.xz ilt-1e1aa58aafc40c52a0d4dd64611f7bdba19e5ff3.zip |
Make utf8_string_view work on any contiguous collection of chars
const char * const or container with .data() and .length()
Diffstat (limited to 'lib/unicode.h')
-rw-r--r-- | lib/unicode.h | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/lib/unicode.h b/lib/unicode.h index 2945650..f69c43c 100644 --- a/lib/unicode.h +++ b/lib/unicode.h @@ -45,25 +45,30 @@ struct utf8_string_view { const char * pos; }; + template<typename Str> // cppcheck-suppress noExplicitConstructor; NOLINTNEXTLINE(hicpp-explicit-conversions) - template<typename... Args> constexpr utf8_string_view(Args &&... args) : str {std::forward<Args>(args)...} { } + constexpr utf8_string_view(const Str & str) : begin_ {str.data()}, end_ {str.data() + str.length()} + { + } + // cppcheck-suppress noExplicitConstructor; NOLINTNEXTLINE(hicpp-explicit-conversions) + constexpr utf8_string_view(const char * const str) : utf8_string_view {std::string_view {str}} { } [[nodiscard]] auto begin() const { - return iter {str.cbegin()}; + return iter {begin_}; } [[nodiscard]] auto end() const { - return iter {str.cend()}; + return iter {end_}; } [[nodiscard]] size_t length() const; private: - std::string_view str; + const char *begin_, *end_; }; template<> struct std::iterator_traits<utf8_string_view::iter> { using difference_type = size_t; |