From 4cf4fa68e12556c7dd5df898dcefeb9496765433 Mon Sep 17 00:00:00 2001 From: artpaul Date: Sat, 10 Dec 2016 03:13:33 +0500 Subject: [PATCH] parametrize StringView --- dbms/include/DB/Common/StringView.h | 61 +++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/dbms/include/DB/Common/StringView.h b/dbms/include/DB/Common/StringView.h index ca2ed71c530..3cfed0831ac 100644 --- a/dbms/include/DB/Common/StringView.h +++ b/dbms/include/DB/Common/StringView.h @@ -5,33 +5,46 @@ #include /// A lightweight non-owning read-only view into a subsequence of a string. -class StringView +template < + typename TChar, + typename TTraits = std::char_traits +> +class StringViewImpl { public: using size_type = size_t; + using traits_type = TTraits; + using value_type = typename TTraits::char_type; static constexpr size_type npos = size_type(-1); public: - inline StringView() noexcept + inline StringViewImpl() noexcept : str(nullptr) , len(0) { } - constexpr inline StringView(const char* data_, size_t len_) noexcept + constexpr inline StringViewImpl(const TChar* data_, size_t len_) noexcept : str(data_) , len(len_) { } - inline StringView(const std::string& str) noexcept + inline StringViewImpl(const std::basic_string& str) noexcept : str(str.data()) , len(str.size()) { } - inline const char* data() const noexcept + inline TChar at(size_type pos) const + { + if (pos >= len) + throw std::out_of_range("pos must be less than len"); + return str[pos]; + } + + inline const TChar* data() const noexcept { return str; } @@ -58,14 +71,14 @@ public: * If the requested substring extends past the end of the string, * or if count == npos, the returned substring is [pos, size()). */ - StringView substr(size_type pos, size_type count = npos) const + StringViewImpl substr(size_type pos, size_type count = npos) const { if (pos >= len) throw std::out_of_range("pos must be less than len"); if (pos + count >= len || count == npos) - return StringView(str + pos, len - pos); + return StringViewImpl(str + pos, len - pos); else - return StringView(str + pos, count); + return StringViewImpl(str + pos, count); } public: @@ -74,15 +87,39 @@ public: return !empty(); } + inline TChar operator [] (size_type pos) const noexcept + { + return str[pos]; + } + + inline bool operator < (const StringViewImpl& other) const noexcept + { + if (len < other.len) + return true; + if (len > other.len) + return false; + return TTraits::compare(str, other.str, len) < 0; + } + + inline bool operator == (const StringViewImpl& other) const noexcept + { + if (len == other.len) + return TTraits::compare(str, other.str, len) == 0; + return false; + } + private: - const char* str; + const TChar* str; size_t len; }; /// It creates StringView from literal constant at compile time. -template -constexpr inline StringView MakeStringView(const char (&str)[size]) +template +constexpr inline StringViewImpl MakeStringView(const TChar (&str)[size]) { - return StringView(str, size - 1); + return StringViewImpl(str, size - 1); } + + +using StringView = StringViewImpl;