24 #ifndef PSTORE_ADT_SSTRING_VIEW_HPP 25 #define PSTORE_ADT_SSTRING_VIEW_HPP 29 #include "pstore/support/fnv.hpp" 40 template <
typename StringType>
45 static std::size_t length (std::string
const & s) noexcept {
return s.length (); }
46 static gsl::czstring data (std::string
const & s) noexcept {
return s.data (); }
50 static std::size_t length (gsl::czstring
const s) noexcept {
return std::strlen (s); }
51 static gsl::czstring data (gsl::czstring
const s) noexcept {
return s; }
53 template <std::
size_t Size>
55 static std::size_t length (gsl::czstring
const s) noexcept {
return std::strlen (s); }
56 static gsl::czstring data (gsl::czstring
const s) noexcept {
return s; }
58 template <std::
size_t Size>
60 static std::size_t length (gsl::czstring
const s) noexcept {
return std::strlen (s); }
61 static gsl::czstring data (gsl::czstring
const s) noexcept {
return s; }
69 template <
typename Po
interType>
71 static constexpr
bool is_pointer =
false;
75 static constexpr
bool is_pointer =
true;
76 using value_type =
char const;
77 static char const * as_raw (
char const *
const p) noexcept {
return p; }
83 static constexpr
bool is_pointer =
true;
84 using value_type =
typename T::element_type;
85 static_assert (std::is_same<value_type, char>::value ||
86 std::is_same<value_type, char const>::value,
87 "pointer element type must be char or char const");
88 static char const * as_raw (T
const & p) noexcept {
return p.get (); }
112 template <
typename Po
interType>
116 "PointerType is not a known pointer type!");
118 using value_type =
char const;
119 using traits = std::char_traits<value_type>;
120 using pointer = value_type *;
121 using const_pointer = value_type
const *;
122 using reference = value_type &;
123 using const_reference = value_type
const &;
124 using const_iterator = value_type
const *;
126 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
128 using reverse_iterator = const_reverse_iterator;
129 using size_type = std::size_t;
130 using difference_type = std::ptrdiff_t;
131 static constexpr
auto const npos =
static_cast<size_type
> (-1);
135 sstring_view (PointerType ptr, size_type const size) noexcept
136 : ptr_{std::move (ptr)}
145 const_iterator
begin ()
const noexcept {
return data (); }
146 const_iterator end ()
const noexcept {
return begin () + size_; }
147 const_iterator cbegin ()
const noexcept {
return begin (); }
148 const_iterator cend ()
const noexcept {
return end (); }
149 const_reverse_iterator rbegin ()
const noexcept {
return const_reverse_iterator (end ()); }
150 const_reverse_iterator rend ()
const noexcept {
return const_reverse_iterator (
begin ()); }
151 const_reverse_iterator crbegin ()
const noexcept {
return const_reverse_iterator (end ()); }
152 const_reverse_iterator crend ()
const noexcept {
return const_reverse_iterator (
begin ()); }
155 size_type size ()
const noexcept {
return size_; }
156 size_type length ()
const noexcept {
return size_; }
157 size_type max_size ()
const noexcept {
return std::numeric_limits<size_t>::max (); }
158 bool empty ()
const noexcept {
return size_ == 0; }
161 const_reference operator[] (size_type
const pos)
const {
162 PSTORE_ASSERT (pos < size_);
163 return (this->data ())[pos];
165 const_reference at (size_type
const pos)
const {
166 #ifdef PSTORE_EXCEPTIONS 168 throw std::out_of_range (
"sstring_view access out of range");
173 const_reference front ()
const {
174 PSTORE_ASSERT (size_ > 0);
177 const_reference back ()
const {
178 PSTORE_ASSERT (size_ > 0);
179 return (*
this)[size_ - 1];
184 void clear () noexcept { size_ = 0; }
187 std::swap (ptr_, s.ptr_);
188 std::swap (size_, s.size_);
192 explicit operator std::string ()
const {
return this->to_string (); }
194 template <
class Allocator = std::allocator<
char>>
195 std::basic_string<char, std::char_traits<char>, Allocator>
196 to_string (Allocator
const & a = Allocator ())
const {
197 return {data (), size_, a};
205 pos = std::min (pos, size_);
206 return {data () + pos, std::min (n, size_ - pos)};
209 template <
typename StringType>
210 int compare (StringType
const & s)
const;
213 size_type
find (value_type ch, size_type pos = 0) const noexcept {
214 for (; pos < size_; ++pos) {
215 if (ptr_[pos] == ch) {
223 PointerType ptr_ =
nullptr;
224 size_type size_ = size_type{0};
227 template <
typename Po
interType>
237 template <
typename Po
interType>
242 template <
typename Po
interType>
243 template <
typename StringType>
246 size_type
const common_len = std::min (size (), slen);
249 result = (size () == slen) ? 0 : (size () < slen ? -1 : 1);
256 template <
typename Po
interType1,
typename Po
interType2>
259 if (lhs.size () != rhs.size ()) {
262 return lhs.compare (rhs) == 0;
264 template <
typename Po
interType,
typename StringType>
266 StringType
const & rhs) noexcept {
271 return lhs.compare (rhs) == 0;
273 template <
typename StringType,
typename Po
interType>
274 inline bool operator== (StringType
const & lhs,
280 return rhs.compare (lhs) == 0;
285 template <
typename Po
interType1,
typename Po
interType2>
288 return !operator== (lhs, rhs);
290 template <
typename Po
interType,
typename StringType>
292 StringType
const & rhs) noexcept {
293 return !operator== (lhs, rhs);
295 template <
typename StringType,
typename Po
interType>
296 inline bool operator!= (StringType
const & lhs,
298 return !operator== (lhs, rhs);
303 template <
typename Po
interType1,
typename Po
interType2>
306 return lhs.compare (rhs) >= 0;
308 template <
typename Po
interType,
typename StringType>
310 StringType
const & rhs) noexcept {
311 return lhs.compare (rhs) >= 0;
313 template <
typename StringType,
typename Po
interType>
314 inline bool operator>= (StringType
const & lhs,
316 return rhs.compare (lhs) <= 0;
321 template <
typename Po
interType1,
typename Po
interType2>
324 return lhs.compare (rhs) > 0;
326 template <
typename Po
interType,
typename StringType>
328 return lhs.compare (rhs) > 0;
330 template <
typename StringType,
typename Po
interType>
332 return rhs.compare (lhs) < 0;
337 template <
typename Po
interType1,
typename Po
interType2>
338 inline bool operator<= (sstring_view<PointerType1>
const & lhs,
340 return lhs.compare (rhs) <= 0;
342 template <
typename Po
interType,
typename StringType>
343 inline bool operator<= (sstring_view<PointerType>
const & lhs,
344 StringType
const & rhs) noexcept {
345 return lhs.compare (rhs) <= 0;
347 template <
typename StringType,
typename Po
interType>
348 inline bool operator<= (StringType
const & lhs,
350 return rhs.compare (lhs) >= 0;
355 template <
typename Po
interType1,
typename Po
interType2>
356 inline bool operator< (sstring_view<PointerType1>
const & lhs,
358 return lhs.compare (rhs) < 0;
360 template <
typename Po
interType,
typename StringType>
361 inline bool operator< (sstring_view<PointerType>
const & lhs, StringType
const & rhs) noexcept {
362 return lhs.compare (rhs) < 0;
364 template <
typename StringType,
typename Po
interType>
365 inline bool operator< (StringType const & lhs, sstring_view<PointerType>
const & rhs) noexcept {
366 return rhs.compare (lhs) > 0;
371 template <
typename OStream,
typename Po
interType>
372 inline OStream & operator<< (OStream & os, sstring_view<PointerType>
const & str) {
373 using ustreamsize = std::make_unsigned<std::streamsize>::type;
375 static_cast<ustreamsize
> (std::numeric_limits<std::streamsize>::max ());
376 auto const size = std::min (str.length (), max);
377 os.write (str.data (),
static_cast<std::streamsize
> (size));
391 template <
typename ValueType>
393 make_shared_sstring_view (std::shared_ptr<ValueType>
const & ptr, std::size_t length) {
394 return {ptr, length};
400 template <
typename ValueType>
402 make_unique_sstring_view (std::unique_ptr<ValueType> ptr, std::size_t length) {
403 return {std::move (ptr), length};
406 template <std::ptrdiff_t Extent>
408 PSTORE_ASSERT (span.size () >= 0);
409 return {span.data (), unsigned_cast (span.size ())};
411 inline raw_sstring_view make_sstring_view (
char const *
const ptr, std::size_t
const length) {
412 return {ptr, length};
416 return {str.data (), str.length ()};
423 template <
typename StringType>
425 template <
typename S1,
typename S2>
426 bool operator() (S1
const & x, S2
const & y)
const {
431 template <
typename StringType>
434 return static_cast<size_t> (
435 ::pstore::fnv_64a_buf (::pstore::gsl::make_span (str.begin (), str.end ())));
441 #endif // PSTORE_ADT_SSTRING_VIEW_HPP Definition: sstring_view.hpp:41
Definition: chunked_sequence.hpp:607
unsigned_cast() (and its runtime-checked version) allow for simple integral unsigned casts...
transaction< transaction_lock > begin(database &db, transaction_lock &lock)
Creates a new transaction. Every operation performed on a transaction instance can be potentially und...
Definition: transaction.hpp:311
Definition: sstring_view.hpp:82
Definition: sstring_view.hpp:70
sstring_view< gsl::czstring > substr(size_type pos=0, size_type const n=npos) const
Returns a view of the substring [pos, pos + rcount), where rcount is the smaller of n and size() - po...
Definition: sstring_view.hpp:204
const_iterator iterator
Because sstring_view refers to a constant sequence, iterator and const_iterator are the same type...
Definition: sstring_view.hpp:126
Definition: nonpod2.cpp:40
size_type find(value_type ch, size_type pos=0) const noexcept
Finds the first occurrence of ch in this view, starting at position pos.
Definition: sstring_view.hpp:213
Definition: sstring_view.hpp:113
Implements a prefix-style variable-length integer.