19#if defined(BSONCXX_POLY_USE_MNMLSTC)
21#include <core/string.hpp>
27using ::core::basic_string_view;
28using ::core::string_view;
34#elif defined(BSONCXX_POLY_USE_BOOST)
36#include <boost/version.hpp>
38#if BOOST_VERSION >= 106100
40#include <boost/utility/string_view.hpp>
46using ::boost::basic_string_view;
47using ::boost::string_view;
55#include <boost/utility/string_ref.hpp>
61template <
typename charT,
typename traits = std::
char_traits<
charT>>
62using basic_string_view = ::boost::basic_string_ref<charT, traits>;
63using string_view = ::boost::string_ref;
71#elif defined(BSONCXX_POLY_USE_STD_EXPERIMENTAL)
73#include <experimental/string_view>
79using ::std::experimental::basic_string_view;
80using ::std::experimental::string_view;
86#elif defined(BSONCXX_POLY_USE_STD)
94using ::std::basic_string_view;
95using ::std::string_view;
101#elif defined(BSONCXX_POLY_USE_IMPLS)
118template <
typename Char,
typename Traits = std::
char_traits<Char>>
119class basic_string_view : bsoncxx::detail::equality_operators, bsoncxx::detail::ordering_operators {
121 using pointer = Char*;
122 using const_pointer =
const Char*;
123 using size_type = std::size_t;
124 using difference_type = std::ptrdiff_t;
125 using value_type = Char;
128 static constexpr size_type npos =
static_cast<size_type
>(-1);
132 const_pointer _begin =
nullptr;
137 using traits_type = Traits;
138 using reference = Char&;
139 using const_reference =
const Char&;
140 using const_iterator = const_pointer;
141 using iterator = const_iterator;
142 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
143 using reverse_iterator = const_reverse_iterator;
145 constexpr basic_string_view() noexcept = default;
146 constexpr basic_string_view(const basic_string_view&) noexcept = default;
147 bsoncxx_cxx14_constexpr basic_string_view& operator=(const basic_string_view&) noexcept =
150 constexpr basic_string_view(const_pointer s, size_type count) : _begin(s), _size(count) {}
152 constexpr basic_string_view(const_pointer s) : _begin(s), _size(traits_type::length(s)) {}
154 template <
typename Alloc>
155 constexpr basic_string_view(
156 const std::basic_string<value_type, traits_type, Alloc>& str) noexcept
157 : _begin(str.data()), _size(str.size()) {}
159#if defined(__cpp_lib_string_view)
160 constexpr basic_string_view(std::basic_string_view<value_type, traits_type> sv) noexcept
161 : _begin(sv.data()), _size(sv.size()) {}
164 basic_string_view(std::nullptr_t) =
delete;
166 constexpr const_iterator begin() const noexcept {
167 return const_iterator(_begin);
169 constexpr const_iterator end() const noexcept {
170 return begin() + size();
172 constexpr const_iterator cbegin() const noexcept {
175 constexpr const_iterator cend() const noexcept {
179 constexpr const_reverse_iterator rbegin() const noexcept {
180 return const_reverse_iterator{end()};
183 constexpr const_reverse_iterator rend() const noexcept {
184 return const_reverse_iterator{begin()};
187 constexpr const_reverse_iterator crbegin() const noexcept {
188 return const_reverse_iterator{cend()};
191 constexpr const_reverse_iterator crend() const noexcept {
192 return const_reverse_iterator{crbegin()};
195 constexpr const_reference operator[](size_type offset)
const {
196 return _begin[offset];
199 bsoncxx_cxx14_constexpr const_reference at(size_type pos)
const {
201 throw std::out_of_range{
"bsoncxx::stdx::basic_string_view::at()"};
206 constexpr const_reference front()
const {
210 constexpr const_reference back()
const {
211 return (*
this)[size() - 1];
214 constexpr const_pointer data() const noexcept {
218 constexpr size_type size() const noexcept {
222 constexpr size_type length() const noexcept {
226 constexpr bool empty() const noexcept {
230 constexpr size_type max_size() const noexcept {
231 return static_cast<size_type
>(std::numeric_limits<difference_type>::max());
234 bsoncxx_cxx14_constexpr
void remove_prefix(size_type n) {
239 bsoncxx_cxx14_constexpr
void remove_suffix(size_type n) {
243 bsoncxx_cxx14_constexpr
void swap(basic_string_view& other) {
244 std::swap(_begin, other._begin);
245 std::swap(_size, other._size);
248 size_type copy(pointer dest, size_type count, size_type pos = 0)
const {
250 throw std::out_of_range{
"bsoncxx::stdx::basic_string_view::substr()"};
252 count = (std::min)(count, size() - pos);
253 Traits::copy(dest, data() + pos, count);
257 bsoncxx_cxx14_constexpr basic_string_view substr(size_type pos = 0,
258 size_type count = npos)
const {
260 throw std::out_of_range{
"bsoncxx::stdx::basic_string_view::substr()"};
262 return basic_string_view(_begin + pos, (std::min)(count, size() - pos));
265 constexpr int compare(basic_string_view other)
const noexcept {
267 return _compare2(Traits::compare(data(), other.data(), (std::min)(size(), other.size())),
271 constexpr int compare(const_pointer cstr)
const {
272 return compare(basic_string_view(cstr));
275 constexpr int compare(size_type pos1, size_type count1, basic_string_view other)
const {
276 return substr(pos1, count1).compare(other);
279 constexpr int compare(size_type pos1, size_type count1, const_pointer cstr)
const {
280 return compare(pos1, count1, basic_string_view(cstr));
283 constexpr int compare(size_type pos1,
285 basic_string_view other,
287 size_type count2)
const {
288 return substr(pos1, count1).compare(other.substr(pos2, count2));
291 constexpr int compare(size_type pos1,
294 size_type count2)
const {
295 return substr(pos1, count1).compare(basic_string_view(str, count2));
298 bsoncxx_cxx14_constexpr size_type find(basic_string_view infix, size_type pos = 0) const
303 basic_string_view sub = this->substr(pos);
308 const_iterator found = std::search(sub.begin(), sub.end(), infix.begin(), infix.end());
309 if (found == sub.end()) {
312 return static_cast<size_type
>(found - begin());
315 bsoncxx_cxx14_constexpr size_type rfind(basic_string_view infix, size_type pos = npos)
const
318 const size_type substr_size = pos != npos ? pos + infix.size() : pos;
320 return (std::min)(pos, size());
322 basic_string_view searched = this->substr(0, substr_size);
323 auto f = std::search(searched.rbegin(), searched.rend(), infix.rbegin(), infix.rend());
324 if (f == searched.rend()) {
327 return static_cast<size_type
>(rend() - f) - infix.size();
330 constexpr size_type find_first_of(basic_string_view set, size_type pos = 0) const noexcept {
331 return _find_if(pos, [&](value_type chr) {
return set.find(chr) != npos; });
334 constexpr size_type find_last_of(basic_string_view set, size_type pos = npos)
const noexcept {
335 return _rfind_if(pos, [&](value_type chr) {
return set.find(chr) != npos; });
338 constexpr size_type find_first_not_of(basic_string_view set, size_type pos = 0) const noexcept {
339 return _find_if(pos, [&](value_type chr) {
return set.find(chr) == npos; });
342 constexpr size_type find_last_not_of(basic_string_view set, size_type pos = npos)
const
344 return _rfind_if(pos, [&](value_type chr) {
return set.find(chr) == npos; });
347#pragma push_macro("DECL_FINDERS")
349#define DECL_FINDERS(Name, DefaultPos) \
350 constexpr size_type Name(value_type chr, size_type pos = DefaultPos) const noexcept { \
351 return Name(basic_string_view(&chr, 1), pos); \
353 constexpr size_type Name(const_pointer cstr, size_type pos, size_type count) const { \
354 return Name(basic_string_view(cstr, count), pos); \
356 constexpr size_type Name(const_pointer cstr, size_type pos = DefaultPos) const { \
357 return Name(basic_string_view(cstr), pos); \
359 BSONCXX_FORCE_SEMICOLON
361 DECL_FINDERS(find, 0);
362 DECL_FINDERS(rfind, npos);
363 DECL_FINDERS(find_first_of, 0);
364 DECL_FINDERS(find_last_of, npos);
365 DECL_FINDERS(find_first_not_of, 0);
366 DECL_FINDERS(find_last_not_of, npos);
367#pragma pop_macro("DECL_FINDERS")
370 template <
typename Allocator>
371 explicit operator std::basic_string<Char, Traits, Allocator>()
const {
372 return std::basic_string<Char, Traits, Allocator>(data(), size());
375#if defined(__cpp_lib_string_view)
376 explicit operator std::basic_string_view<value_type, traits_type>() const noexcept {
377 return std::basic_string_view<value_type, traits_type>(data(), size());
383 constexpr int _compare2(
int diff, basic_string_view other)
const noexcept {
385 return diff ? diff :
static_cast<int>(size() - other.size());
389 constexpr friend bool tag_invoke(bsoncxx::detail::equal_to,
390 basic_string_view left,
391 basic_string_view right)
noexcept {
392 return left.size() == right.size() && left.compare(right) == 0;
396 constexpr friend bsoncxx::detail::strong_ordering tag_invoke(
397 bsoncxx::detail::compare_three_way cmp,
398 basic_string_view left,
399 basic_string_view right)
noexcept {
400 return cmp(left.compare(right), 0);
403 friend std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& out,
404 basic_string_view self) {
405 out << std::basic_string<Char, Traits>(self);
411 template <
typename F>
412 bsoncxx_cxx14_constexpr size_type _find_if(size_type pos, F pred)
const noexcept {
413 const auto sub = substr(pos);
414 const iterator found = std::find_if(sub.begin(), sub.end(), pred);
415 if (found == end()) {
418 return static_cast<size_type
>(found - begin());
423 template <
typename F>
424 bsoncxx_cxx14_constexpr size_type _rfind_if(size_type pos, F pred)
const noexcept {
426 const auto rpos = pos == npos ? npos : pos + 1;
428 const auto prefix = substr(0, rpos);
429 const const_reverse_iterator found = std::find_if(prefix.rbegin(), prefix.rend(), pred);
430 if (found == rend()) {
434 return static_cast<size_type
>(rend() - found) - 1u;
440template <
typename C,
typename Tr>
441constexpr std::size_t basic_string_view<C, Tr>::npos;
443using string_view = basic_string_view<char>;
451template <
typename CharT,
typename Traits>
452struct hash<
bsoncxx::v_noabi::stdx::basic_string_view<CharT, Traits>>
453 :
private std::hash<std::basic_string<CharT, Traits>> {
454 std::size_t operator()(
455 const bsoncxx::v_noabi::stdx::basic_string_view<CharT, Traits>& str)
const {
456 return std::hash<std::basic_string<CharT, Traits>>::operator()(
457 std::basic_string<CharT, Traits>(str.data(), str.size()));
464#error "Cannot find a valid polyfill for string_view"
472using ::bsoncxx::v_noabi::stdx::basic_string_view;
473using ::bsoncxx::v_noabi::stdx::string_view;
490#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:503
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.