19#if defined(BSONCXX_POLY_USE_STD)
27using ::std::basic_string_view;
28using ::std::string_view;
34#elif defined(BSONCXX_POLY_USE_IMPLS)
51template <
typename Char,
typename Traits = std::
char_traits<Char>>
52class basic_string_view : bsoncxx::detail::equality_operators, bsoncxx::detail::ordering_operators {
54 using pointer = Char*;
55 using const_pointer =
const Char*;
56 using size_type = std::size_t;
57 using difference_type = std::ptrdiff_t;
58 using value_type = Char;
61 static constexpr size_type npos =
static_cast<size_type
>(-1);
65 const_pointer _begin =
nullptr;
70 using traits_type = Traits;
71 using reference = Char&;
72 using const_reference =
const Char&;
73 using const_iterator = const_pointer;
74 using iterator = const_iterator;
75 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
76 using reverse_iterator = const_reverse_iterator;
78 constexpr basic_string_view() noexcept = default;
79 constexpr basic_string_view(const basic_string_view&) noexcept = default;
80 bsoncxx_cxx14_constexpr basic_string_view& operator=(const basic_string_view&) noexcept =
83 constexpr basic_string_view(const_pointer s, size_type count) : _begin(s), _size(count) {}
85 constexpr basic_string_view(const_pointer s) : _begin(s), _size(traits_type::length(s)) {}
87 template <
typename Alloc>
88 constexpr basic_string_view(
89 const std::basic_string<value_type, traits_type, Alloc>& str) noexcept
90 : _begin(str.data()), _size(str.size()) {}
92#if defined(__cpp_lib_string_view)
93 constexpr basic_string_view(std::basic_string_view<value_type, traits_type> sv) noexcept
94 : _begin(sv.data()), _size(sv.size()) {}
97 basic_string_view(std::nullptr_t) =
delete;
99 constexpr const_iterator begin() const noexcept {
100 return const_iterator(_begin);
102 constexpr const_iterator end() const noexcept {
103 return begin() + size();
105 constexpr const_iterator cbegin() const noexcept {
108 constexpr const_iterator cend() const noexcept {
112 constexpr const_reverse_iterator rbegin() const noexcept {
113 return const_reverse_iterator{end()};
116 constexpr const_reverse_iterator rend() const noexcept {
117 return const_reverse_iterator{begin()};
120 constexpr const_reverse_iterator crbegin() const noexcept {
121 return const_reverse_iterator{cend()};
124 constexpr const_reverse_iterator crend() const noexcept {
125 return const_reverse_iterator{crbegin()};
128 constexpr const_reference operator[](size_type offset)
const {
129 return _begin[offset];
132 bsoncxx_cxx14_constexpr const_reference at(size_type pos)
const {
134 throw std::out_of_range{
"bsoncxx::stdx::basic_string_view::at()"};
139 constexpr const_reference front()
const {
143 constexpr const_reference back()
const {
144 return (*
this)[size() - 1];
147 constexpr const_pointer data() const noexcept {
151 constexpr size_type size() const noexcept {
155 constexpr size_type length() const noexcept {
159 constexpr bool empty() const noexcept {
163 constexpr size_type max_size() const noexcept {
164 return static_cast<size_type
>(std::numeric_limits<difference_type>::max());
167 bsoncxx_cxx14_constexpr
void remove_prefix(size_type n) {
172 bsoncxx_cxx14_constexpr
void remove_suffix(size_type n) {
176 bsoncxx_cxx14_constexpr
void swap(basic_string_view& other) {
177 std::swap(_begin, other._begin);
178 std::swap(_size, other._size);
181 size_type copy(pointer dest, size_type count, size_type pos = 0)
const {
183 throw std::out_of_range{
"bsoncxx::stdx::basic_string_view::substr()"};
185 count = (std::min)(count, size() - pos);
186 Traits::copy(dest, data() + pos, count);
190 bsoncxx_cxx14_constexpr basic_string_view substr(size_type pos = 0,
191 size_type count = npos)
const {
193 throw std::out_of_range{
"bsoncxx::stdx::basic_string_view::substr()"};
195 return basic_string_view(_begin + pos, (std::min)(count, size() - pos));
198 constexpr int compare(basic_string_view other)
const noexcept {
200 return _compare2(Traits::compare(data(), other.data(), (std::min)(size(), other.size())),
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,
218 basic_string_view other,
220 size_type count2)
const {
221 return substr(pos1, count1).compare(other.substr(pos2, count2));
224 constexpr int compare(size_type pos1,
227 size_type count2)
const {
228 return substr(pos1, count1).compare(basic_string_view(str, count2));
231 bsoncxx_cxx14_constexpr size_type find(basic_string_view infix, size_type pos = 0) const
236 basic_string_view sub = this->substr(pos);
241 const_iterator found = std::search(sub.begin(), sub.end(), infix.begin(), infix.end());
242 if (found == sub.end()) {
245 return static_cast<size_type
>(found - begin());
248 bsoncxx_cxx14_constexpr size_type rfind(basic_string_view infix, size_type pos = npos)
const
251 const size_type substr_size = pos != npos ? pos + infix.size() : pos;
253 return (std::min)(pos, size());
255 basic_string_view searched = this->substr(0, substr_size);
256 auto f = std::search(searched.rbegin(), searched.rend(), infix.rbegin(), infix.rend());
257 if (f == searched.rend()) {
260 return static_cast<size_type
>(rend() - f) - infix.size();
263 constexpr size_type find_first_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_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 constexpr size_type find_first_not_of(basic_string_view set, size_type pos = 0) const noexcept {
272 return _find_if(pos, [&](value_type chr) {
return set.find(chr) == npos; });
275 constexpr size_type find_last_not_of(basic_string_view set, size_type pos = npos)
const
277 return _rfind_if(pos, [&](value_type chr) {
return set.find(chr) == npos; });
280#pragma push_macro("DECL_FINDERS")
282#define DECL_FINDERS(Name, DefaultPos) \
283 constexpr size_type Name(value_type chr, size_type pos = DefaultPos) const noexcept { \
284 return Name(basic_string_view(&chr, 1), pos); \
286 constexpr size_type Name(const_pointer cstr, size_type pos, size_type count) const { \
287 return Name(basic_string_view(cstr, count), pos); \
289 constexpr size_type Name(const_pointer cstr, size_type pos = DefaultPos) const { \
290 return Name(basic_string_view(cstr), pos); \
292 BSONCXX_FORCE_SEMICOLON
294 DECL_FINDERS(find, 0);
295 DECL_FINDERS(rfind, npos);
296 DECL_FINDERS(find_first_of, 0);
297 DECL_FINDERS(find_last_of, npos);
298 DECL_FINDERS(find_first_not_of, 0);
299 DECL_FINDERS(find_last_not_of, npos);
300#pragma pop_macro("DECL_FINDERS")
303 template <
typename Allocator>
304 explicit operator std::basic_string<Char, Traits, Allocator>()
const {
305 return std::basic_string<Char, Traits, Allocator>(data(), size());
308#if defined(__cpp_lib_string_view)
309 explicit operator std::basic_string_view<value_type, traits_type>() const noexcept {
310 return std::basic_string_view<value_type, traits_type>(data(), size());
316 constexpr int _compare2(
int diff, basic_string_view other)
const noexcept {
318 return diff ? diff :
static_cast<int>(size() - other.size());
322 constexpr friend bool tag_invoke(bsoncxx::detail::equal_to,
323 basic_string_view left,
324 basic_string_view right)
noexcept {
325 return left.size() == right.size() && left.compare(right) == 0;
329 constexpr friend bsoncxx::detail::strong_ordering tag_invoke(
330 bsoncxx::detail::compare_three_way cmp,
331 basic_string_view left,
332 basic_string_view right)
noexcept {
333 return cmp(left.compare(right), 0);
336 friend std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& out,
337 basic_string_view self) {
338 out << std::basic_string<Char, Traits>(self);
344 template <
typename F>
345 bsoncxx_cxx14_constexpr size_type _find_if(size_type pos, F pred)
const noexcept {
346 const auto sub = substr(pos);
347 const iterator found = std::find_if(sub.begin(), sub.end(), pred);
348 if (found == end()) {
351 return static_cast<size_type
>(found - begin());
356 template <
typename F>
357 bsoncxx_cxx14_constexpr size_type _rfind_if(size_type pos, F pred)
const noexcept {
359 const auto rpos = pos == npos ? npos : pos + 1;
361 const auto prefix = substr(0, rpos);
362 const const_reverse_iterator found = std::find_if(prefix.rbegin(), prefix.rend(), pred);
363 if (found == rend()) {
367 return static_cast<size_type
>(rend() - found) - 1u;
373template <
typename C,
typename Tr>
374constexpr std::size_t basic_string_view<C, Tr>::npos;
376using string_view = basic_string_view<char>;
384template <
typename CharT,
typename Traits>
385struct hash<
bsoncxx::v_noabi::stdx::basic_string_view<CharT, Traits>>
386 :
private std::hash<std::basic_string<CharT, Traits>> {
387 std::size_t operator()(
388 const bsoncxx::v_noabi::stdx::basic_string_view<CharT, Traits>& str)
const {
389 return std::hash<std::basic_string<CharT, Traits>>::operator()(
390 std::basic_string<CharT, Traits>(str.data(), str.size()));
397#error "Cannot find a valid polyfill for string_view"
405using ::bsoncxx::v_noabi::stdx::basic_string_view;
406using ::bsoncxx::v_noabi::stdx::string_view;
423#if defined(BSONCXX_PRIVATE_DOXYGEN_PREPROCESSOR)
The bsoncxx macro guard postlude header.
The bsoncxx macro guard prelude header.
A polyfill for std::string_view.
Definition string_view.hpp:436
The top-level namespace within which all bsoncxx library entities are declared.
The top-level namespace reserved for the C++ standard library.
Provides comparison-related utilities for internal use.
Provides <type_traits>-related polyfills for internal use.