2016-12-09 17:38:15 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cassert>
|
2016-12-09 19:31:16 +00:00
|
|
|
#include <stdexcept>
|
2016-12-09 17:38:15 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
/// A lightweight non-owning read-only view into a subsequence of a string.
|
|
|
|
class StringView
|
|
|
|
{
|
2016-12-09 19:31:16 +00:00
|
|
|
public:
|
|
|
|
using size_type = size_t;
|
|
|
|
|
|
|
|
static constexpr size_type npos = size_type(-1);
|
|
|
|
|
2016-12-09 17:38:15 +00:00
|
|
|
public:
|
|
|
|
inline StringView() noexcept
|
|
|
|
: str(nullptr)
|
|
|
|
, len(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr inline StringView(const char* data_, size_t len_) noexcept
|
|
|
|
: str(data_)
|
|
|
|
, len(len_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
inline StringView(const std::string& str) noexcept
|
|
|
|
: str(str.data())
|
|
|
|
, len(str.size())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const char* data() const noexcept
|
|
|
|
{
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool empty() const noexcept
|
|
|
|
{
|
|
|
|
return len == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool null() const noexcept
|
|
|
|
{
|
|
|
|
assert(len == 0);
|
|
|
|
return str == nullptr;
|
|
|
|
}
|
|
|
|
|
2016-12-09 19:31:16 +00:00
|
|
|
inline size_type size() const noexcept
|
2016-12-09 17:38:15 +00:00
|
|
|
{
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2016-12-09 19:31:16 +00:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Returns a substring [pos, pos + count).
|
|
|
|
* 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
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
else
|
|
|
|
return StringView(str + pos, count);
|
|
|
|
}
|
|
|
|
|
2016-12-09 17:38:15 +00:00
|
|
|
public:
|
|
|
|
inline operator bool () const noexcept
|
|
|
|
{
|
|
|
|
return !empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const char* str;
|
2016-12-09 19:31:16 +00:00
|
|
|
size_t len;
|
2016-12-09 17:38:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// It creates StringView from literal constant at compile time.
|
|
|
|
template <size_t size>
|
|
|
|
constexpr inline StringView MakeStringView(const char (&str)[size])
|
|
|
|
{
|
|
|
|
return StringView(str, size - 1);
|
|
|
|
}
|