13 #ifndef __JSONV_DETAIL_STRING_VIEW_HPP_INCLUDED__    14 #define __JSONV_DETAIL_STRING_VIEW_HPP_INCLUDED__    37     using value_type             = char;
    38     using pointer                = 
const value_type*;
    39     using reference              = 
const value_type&;
    40     using const_reference        = 
const value_type&;
    41     using iterator               = pointer;
    42     using const_iterator         = pointer;
    43     using reverse_iterator       = std::reverse_iterator<iterator>;
    44     using const_reverse_iterator = std::reverse_iterator<const_iterator>;
    45     using size_type              = std::size_t;
    46     using difference_type        = std::ptrdiff_t;
    48     static constexpr size_type npos = size_type(~0);
    58             _length(std::strlen(p))
    61     constexpr 
string_view(pointer p, size_type len) noexcept :
    66     template <
typename UAllocator>
    67     string_view(
const std::basic_string<value_type, std::char_traits<value_type>, UAllocator>& src)
    68                 noexcept(noexcept(src.data()) && noexcept(src.length())):
    76     template <
typename UAllocator>
    77     explicit operator std::basic_string<value_type, std::char_traits<value_type>, UAllocator>() 
const    79         return std::basic_string<value_type, std::char_traits<value_type>, UAllocator>(_base, _length);
    84     explicit operator std::string()
 const    86         return std::string(_base, _length);
    90     constexpr size_type size()     
const noexcept { 
return _length; }
    91     constexpr size_type max_size() 
const noexcept { 
return _length; }
    92     constexpr size_type length()   
const noexcept { 
return _length; }
    94     constexpr 
bool empty() 
const noexcept { 
return _length == 0; }
    96     constexpr const_reference operator[](size_type idx)
 const { 
return _base[idx]; }
    97     const_reference at(size_type idx)
 const   102             throw std::out_of_range(
"jsonv::string_view::at");
   105     constexpr const_reference front()
 const { 
return _base[0]; }
   106     constexpr const_reference back()
  const { 
return _base[_length - 1]; }
   108     constexpr pointer data()
 const { 
return _base; }
   110     constexpr const_iterator begin()
  const { 
return _base; }
   111     constexpr const_iterator cbegin()
 const { 
return _base; }
   112     constexpr const_iterator end()
    const { 
return _base + _length; }
   113     constexpr const_iterator cend()
   const { 
return _base + _length; }
   115     const_reverse_iterator rbegin()
  const { 
return const_reverse_iterator(end()); }
   116     const_reverse_iterator crbegin()
 const { 
return const_reverse_iterator(end()); }
   117     const_reverse_iterator rend()
    const { 
return const_reverse_iterator(begin()); }
   118     const_reverse_iterator crend()
   const { 
return const_reverse_iterator(begin()); }
   126     void remove_prefix(size_type n)
   135             throw std::range_error(
"jsonv::string_view::remove_prefix");
   139     void remove_suffix(size_type n)
   147             throw std::range_error(
"jsonv::string_view::remove_prefix");
   151     string_view substr(size_type idx, size_type count = npos)
 const   153         count = count == npos ? (_length - idx) : count;
   154         if (idx + count <= _length)
   157             throw std::range_error(
"jsonv::string_view::substr");
   160     bool starts_with(value_type val)
 const   162         return !empty() && front() == val;
   167         return sub.length() <= length() && std::equal(sub.begin(), sub.end(), begin());
   170     bool ends_with(value_type val)
 const   172         return !empty() && back() == val;
   177         return sub.length() <= length() && std::equal(sub.rbegin(), sub.rend(), rbegin());
   182         auto iter = std::search(begin(), end(), sub.begin(), sub.end());
   183         return iter == end() ? npos : std::distance(begin(), iter);
   186     size_type find(value_type val)
 const   188         auto iter = std::find(begin(), end(), val);
   189         return iter == end() ? npos : std::distance(begin(), iter);
   194         auto iter = std::search(rbegin(), rend(), sub.rbegin(), sub.rend());
   195         return iter == rend() ? npos : std::distance(iter, rend());
   198     size_type rfind(value_type val)
 const   200         auto iter = std::find(rbegin(), rend(), val);
   201         return iter == rend() ? npos : std::distance(iter, rend());
   204     size_type find_first_of(value_type val)
 const   209     size_type find_first_of(
const string_view& chars)
 const   211         auto iter = std::find_first_of(begin(), end(), chars.begin(), chars.end());
   212         return iter == end() ? npos : std::distance(begin(), iter);
   215     size_type find_first_not_of(value_type val)
 const   217         auto iter = std::find_if(begin(), end(), [val] (value_type x) { 
return val != x; });
   218         return iter == end() ? npos : std::distance(begin(), iter);
   221     size_type find_first_not_of(
const string_view& chars)
 const   223         auto iter = std::find_if(begin(), end(),
   224                                  [&chars] (value_type x)
   226                                      return chars.find(x) == npos;
   229         return iter == end() ? npos : std::distance(begin(), iter);
   232     size_type find_last_of(value_type val)
 const   237     size_type find_last_of(
const string_view& chars)
 const   239         auto iter = std::find_first_of(rbegin(), rend(), chars.begin(), chars.end());
   240         return iter == rend() ? npos : std::distance(iter, rend());
   243     size_type find_last_not_of(value_type val)
 const   245         auto iter = std::find_if(rbegin(), rend(), [val] (value_type x) { 
return val != x; });
   246         return iter == rend() ? npos : std::distance(iter, rend());
   249     size_type find_last_not_of(
const string_view& chars)
 const   251         auto iter = std::find_if(rbegin(), rend(),
   252                                  [&chars] (value_type x)
   254                                      return chars.find(x) == npos;
   257         return iter == rend() ? npos : std::distance(iter, rend());
   262         return _length == other._length
   263             && (_base == other._base || std::equal(begin(), end(), other.begin()));
   268         return !operator==(other);
   273         return std::lexicographical_compare(begin(), end(), other.begin(), other.end());
   278         return !(other < *
this);
   283         return other < *
this;
   288         return !(*
this < other);
   291     friend std::ostream& operator<<(std::ostream& os, 
const string_view& 
self)
   293         os.write(
self.begin(), 
self.size());
   302 template <
typename UAllocator>
   303 bool operator==(
const std::basic_string<UAllocator> lhs, 
const string_view& rhs)
   308 template <
typename UAllocator>
   309 bool operator!=(
const std::basic_string<UAllocator> lhs, 
const string_view& rhs)
   314 template <
typename UAllocator>
   315 bool operator<(const std::basic_string<UAllocator> lhs, 
const string_view& rhs)
   320 template <
typename UAllocator>
   321 bool operator<=(const std::basic_string<UAllocator> lhs, 
const string_view& rhs)
   326 template <
typename UAllocator>
   327 bool operator>(
const std::basic_string<UAllocator> lhs, 
const string_view& rhs)
   332 template <
typename UAllocator>
   333 bool operator>=(
const std::basic_string<UAllocator> lhs, 
const string_view& rhs)
 Copyright (c) 2014-2019 by Travis Gockel. 
 
A non-owning reference to an std::string, as proposed to the C++ Standard Committee by Jeffrey Yasski...