mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-09-23 10:10:50 +00:00
parametrize StringView
This commit is contained in:
parent
228b43bc95
commit
4cf4fa68e1
@ -5,33 +5,46 @@
|
||||
#include <string>
|
||||
|
||||
/// A lightweight non-owning read-only view into a subsequence of a string.
|
||||
class StringView
|
||||
template <
|
||||
typename TChar,
|
||||
typename TTraits = std::char_traits<TChar>
|
||||
>
|
||||
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<TChar>& 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 <size_t size>
|
||||
constexpr inline StringView MakeStringView(const char (&str)[size])
|
||||
template <typename TChar, size_t size>
|
||||
constexpr inline StringViewImpl<TChar> MakeStringView(const TChar (&str)[size])
|
||||
{
|
||||
return StringView(str, size - 1);
|
||||
return StringViewImpl<TChar>(str, size - 1);
|
||||
}
|
||||
|
||||
|
||||
using StringView = StringViewImpl<char>;
|
||||
|
Loading…
Reference in New Issue
Block a user