21#if defined(BSONCXX_POLY_USE_STD)
29using std::basic_string_view;
30using std::string_view;
36#elif defined(BSONCXX_POLY_USE_IMPLS)
55template <
typename Char,
typename Traits = std::
char_traits<Char>>
56class basic_string_view : bsoncxx::detail::equality_operators, bsoncxx::detail::ordering_operators {
58 using pointer = Char*;
59 using const_pointer = Char
const*;
60 using size_type = std::size_t;
61 using difference_type = std::ptrdiff_t;
62 using value_type = Char;
65 static constexpr size_type npos =
static_cast<size_type
>(-1);
69 const_pointer _begin =
nullptr;
74 using traits_type = Traits;
75 using reference = Char&;
76 using const_reference = Char
const&;
77 using const_iterator = const_pointer;
78 using iterator = const_iterator;
79 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
80 using reverse_iterator = const_reverse_iterator;
82 constexpr basic_string_view() noexcept = default;
83 constexpr basic_string_view(basic_string_view const&) noexcept = default;
84 BSONCXX_PRIVATE_CONSTEXPR_CXX14 basic_string_view& operator=(basic_string_view const&) noexcept = default;
86 constexpr basic_string_view(const_pointer s, size_type count) : _begin(s), _size(count) {}
88 constexpr basic_string_view(const_pointer s) : _begin(s), _size(traits_type::length(s)) {}
90 template <
typename Alloc>
91 constexpr basic_string_view(std::basic_string<value_type, traits_type, Alloc>
const& str) noexcept
92 : _begin(str.data()), _size(str.size()) {}
94#if defined(__cpp_lib_string_view)
95 constexpr basic_string_view(std::basic_string_view<value_type, traits_type> sv) noexcept
96 : _begin(sv.data()), _size(sv.size()) {}
99 basic_string_view(std::nullptr_t) =
delete;
101 constexpr const_iterator begin() const noexcept {
102 return const_iterator(_begin);
104 constexpr const_iterator end() const noexcept {
105 return begin() + size();
107 constexpr const_iterator cbegin() const noexcept {
110 constexpr const_iterator cend() const noexcept {
114 constexpr const_reverse_iterator rbegin() const noexcept {
115 return const_reverse_iterator{end()};
118 constexpr const_reverse_iterator rend() const noexcept {
119 return const_reverse_iterator{begin()};
122 constexpr const_reverse_iterator crbegin() const noexcept {
123 return const_reverse_iterator{cend()};
126 constexpr const_reverse_iterator crend() const noexcept {
127 return const_reverse_iterator{crbegin()};
130 constexpr const_reference operator[](size_type offset)
const {
131 return _begin[offset];
134 BSONCXX_PRIVATE_CONSTEXPR_CXX14 const_reference at(size_type pos)
const {
136 throw std::out_of_range{
"bsoncxx::stdx::basic_string_view::at()"};
141 constexpr const_reference front()
const {
145 constexpr const_reference back()
const {
146 return (*
this)[size() - 1];
149 constexpr const_pointer data() const noexcept {
153 constexpr size_type size() const noexcept {
157 constexpr size_type length() const noexcept {
161 constexpr bool empty() const noexcept {
165 constexpr size_type max_size() const noexcept {
166 return static_cast<size_type
>(std::numeric_limits<difference_type>::max());
169 BSONCXX_PRIVATE_CONSTEXPR_CXX14
void remove_prefix(size_type n) {
174 BSONCXX_PRIVATE_CONSTEXPR_CXX14
void remove_suffix(size_type n) {
178 BSONCXX_PRIVATE_CONSTEXPR_CXX14
void swap(basic_string_view& other) {
179 std::swap(_begin, other._begin);
180 std::swap(_size, other._size);
183 size_type copy(pointer dest, size_type count, size_type pos = 0)
const {
185 throw std::out_of_range{
"bsoncxx::stdx::basic_string_view::substr()"};
187 count = (std::min)(count, size() - pos);
188 Traits::copy(dest, data() + pos, count);
192 BSONCXX_PRIVATE_CONSTEXPR_CXX14 basic_string_view substr(size_type pos = 0, size_type count = npos)
const {
194 throw std::out_of_range{
"bsoncxx::stdx::basic_string_view::substr()"};
196 return basic_string_view(_begin + pos, (std::min)(count, size() - pos));
199 constexpr int compare(basic_string_view other)
const noexcept {
201 return _compare2(Traits::compare(data(), other.data(), (std::min)(size(), other.size())), other);
204 constexpr int compare(const_pointer cstr)
const {
205 return compare(basic_string_view(cstr));
208 constexpr int compare(size_type pos1, size_type count1, basic_string_view other)
const {
209 return substr(pos1, count1).compare(other);
212 constexpr int compare(size_type pos1, size_type count1, const_pointer cstr)
const {
213 return compare(pos1, count1, basic_string_view(cstr));
216 constexpr int compare(size_type pos1, size_type count1, basic_string_view other, size_type pos2, size_type count2)
218 return substr(pos1, count1).compare(other.substr(pos2, count2));
221 constexpr int compare(size_type pos1, size_type count1, const_pointer str, size_type count2)
const {
222 return substr(pos1, count1).compare(basic_string_view(str, count2));
225 BSONCXX_PRIVATE_CONSTEXPR_CXX14 size_type find(basic_string_view infix, size_type pos = 0) const noexcept {
229 basic_string_view sub = this->substr(pos);
234 const_iterator found = std::search(sub.begin(), sub.end(), infix.begin(), infix.end());
235 if (found == sub.end()) {
238 return static_cast<size_type
>(found - begin());
241 BSONCXX_PRIVATE_CONSTEXPR_CXX14 size_type rfind(basic_string_view infix, size_type pos = npos)
const noexcept {
243 size_type
const substr_size = pos != npos ? pos + infix.size() : pos;
245 return (std::min)(pos, size());
247 basic_string_view searched = this->substr(0, substr_size);
248 auto f = std::search(searched.rbegin(), searched.rend(), infix.rbegin(), infix.rend());
249 if (f == searched.rend()) {
252 return static_cast<size_type
>(rend() - f) - infix.size();
255 constexpr size_type find_first_of(basic_string_view set, size_type pos = 0) const noexcept {
256 return _find_if(pos, [&](value_type chr) {
return set.find(chr) != npos; });
259 constexpr size_type find_last_of(basic_string_view set, size_type pos = npos)
const noexcept {
260 return _rfind_if(pos, [&](value_type chr) {
return set.find(chr) != npos; });
263 constexpr size_type find_first_not_of(basic_string_view set, size_type pos = 0) const noexcept {
264 return _find_if(pos, [&](value_type chr) {
return set.find(chr) == npos; });
267 constexpr size_type find_last_not_of(basic_string_view set, size_type pos = npos)
const noexcept {
268 return _rfind_if(pos, [&](value_type chr) {
return set.find(chr) == npos; });
271#pragma push_macro("DECL_FINDERS")
273#define DECL_FINDERS(Name, DefaultPos) \
274 constexpr size_type Name(value_type chr, size_type pos = DefaultPos) const noexcept { \
275 return Name(basic_string_view(&chr, 1), pos); \
277 constexpr size_type Name(const_pointer cstr, size_type pos, size_type count) const { \
278 return Name(basic_string_view(cstr, count), pos); \
280 constexpr size_type Name(const_pointer cstr, size_type pos = DefaultPos) const { \
281 return Name(basic_string_view(cstr), pos); \
283 BSONCXX_PRIVATE_FORCE_SEMICOLON
285 DECL_FINDERS(find, 0);
286 DECL_FINDERS(rfind, npos);
287 DECL_FINDERS(find_first_of, 0);
288 DECL_FINDERS(find_last_of, npos);
289 DECL_FINDERS(find_first_not_of, 0);
290 DECL_FINDERS(find_last_not_of, npos);
291#pragma pop_macro("DECL_FINDERS")
294 template <
typename Allocator>
295 explicit operator std::basic_string<Char, Traits, Allocator>()
const {
296 return std::basic_string<Char, Traits, Allocator>(data(), size());
299#if defined(__cpp_lib_string_view)
300 explicit operator std::basic_string_view<value_type, traits_type>() const noexcept {
301 return std::basic_string_view<value_type, traits_type>(data(), size());
307 constexpr int _compare2(
int diff, basic_string_view other)
const noexcept {
309 return diff ? diff :
static_cast<int>(size() - other.size());
313 constexpr friend bool
314 tag_invoke(bsoncxx::detail::equal_to, basic_string_view left, basic_string_view right)
noexcept {
315 return left.size() == right.size() && left.compare(right) == 0;
319 constexpr friend bsoncxx::detail::strong_ordering
320 tag_invoke(bsoncxx::detail::compare_three_way cmp, basic_string_view left, basic_string_view right)
noexcept {
321 return cmp(left.compare(right), 0);
324 friend std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& out, basic_string_view self) {
325 out << std::basic_string<Char, Traits>(self);
331 template <
typename F>
332 BSONCXX_PRIVATE_CONSTEXPR_CXX14 size_type _find_if(size_type pos, F pred)
const noexcept {
333 auto const sub = substr(pos);
334 iterator
const found = std::find_if(sub.begin(), sub.end(), pred);
335 if (found == end()) {
338 return static_cast<size_type
>(found - begin());
343 template <
typename F>
344 BSONCXX_PRIVATE_CONSTEXPR_CXX14 size_type _rfind_if(size_type pos, F pred)
const noexcept {
346 auto const rpos = pos == npos ? npos : pos + 1;
348 auto const prefix = substr(0, rpos);
349 const_reverse_iterator
const found = std::find_if(prefix.rbegin(), prefix.rend(), pred);
350 if (found == rend()) {
354 return static_cast<size_type
>(rend() - found) - 1u;
360template <
typename C,
typename Tr>
361constexpr std::size_t basic_string_view<C, Tr>::npos;
371template <
typename CharT,
typename Traits>
372struct hash<
bsoncxx::v1::stdx::basic_string_view<CharT, Traits>> :
private std::hash<std::basic_string<CharT, Traits>> {
373 std::size_t operator()(bsoncxx::v1::stdx::basic_string_view<CharT, Traits>
const& str)
const {
374 return std::hash<std::basic_string<CharT, Traits>>::operator()(
375 std::basic_string<CharT, Traits>(str.data(), str.size()));
382#error "Cannot find a valid polyfill for string_view"
399#if defined(BSONCXX_PRIVATE_DOXYGEN_PREPROCESSOR)
Provides macros describing the bsoncxx library configuration.
The bsoncxx v1 macro guard postlude header.
The bsoncxx v1 macro guard prelude header.
A polyfill for std::string_view.
Definition string_view.hpp:412
Declares C++17 standard library polyfills.
Declares entities whose ABI stability is guaranteed for documented symbols.
@ bsoncxx
From the bsoncxx library.
Definition exception.hpp:39
The top-level namespace within which all bsoncxx library entities are declared.