MongoDB C++ Driver mongocxx-3.11.0
Loading...
Searching...
No Matches
string_view.hpp
Go to the documentation of this file.
1// Copyright 2009-present MongoDB, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
18
19#if defined(BSONCXX_POLY_USE_MNMLSTC)
20
21#include <core/string.hpp>
22
23namespace bsoncxx {
24namespace v_noabi {
25namespace stdx {
26
27using ::core::basic_string_view;
28using ::core::string_view;
29
30} // namespace stdx
31} // namespace v_noabi
32} // namespace bsoncxx
33
34#elif defined(BSONCXX_POLY_USE_BOOST)
35
36#include <boost/version.hpp>
37
38#if BOOST_VERSION >= 106100
39
40#include <boost/utility/string_view.hpp>
41
42namespace bsoncxx {
43namespace v_noabi {
44namespace stdx {
45
46using ::boost::basic_string_view;
47using ::boost::string_view;
48
49} // namespace stdx
50} // namespace v_noabi
51} // namespace bsoncxx
52
53#else
54
55#include <boost/utility/string_ref.hpp>
56
57namespace bsoncxx {
58namespace v_noabi {
59namespace stdx {
60
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;
64
65} // namespace stdx
66} // namespace v_noabi
67} // namespace bsoncxx
68
69#endif
70
71#elif defined(BSONCXX_POLY_USE_STD_EXPERIMENTAL)
72
73#include <experimental/string_view>
74
75namespace bsoncxx {
76namespace v_noabi {
77namespace stdx {
78
79using ::std::experimental::basic_string_view;
80using ::std::experimental::string_view;
81
82} // namespace stdx
83} // namespace v_noabi
84} // namespace bsoncxx
85
86#elif defined(BSONCXX_POLY_USE_STD)
87
88#include <string_view>
89
90namespace bsoncxx {
91namespace v_noabi {
92namespace stdx {
93
94using ::std::basic_string_view;
95using ::std::string_view;
96
97} // namespace stdx
98} // namespace v_noabi
99} // namespace bsoncxx
100
101#elif defined(BSONCXX_POLY_USE_IMPLS)
102
103#include <algorithm>
104#include <cstddef>
105#include <ios>
106#include <limits>
107#include <stdexcept>
108#include <string>
109#include <utility>
110
113
114namespace bsoncxx {
115namespace v_noabi {
116namespace stdx {
117
118template <typename Char, typename Traits = std::char_traits<Char>>
119class basic_string_view : bsoncxx::detail::equality_operators, bsoncxx::detail::ordering_operators {
120 public:
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;
126
127 // Constant sentinel value to represent an impossible/invalid string position.
128 static constexpr size_type npos = static_cast<size_type>(-1);
129
130 private:
131 // Pointer to the beginning of the string being viewed.
132 const_pointer _begin = nullptr;
133 // The size of the array that is being viewed via `_begin`.
134 size_type _size = 0;
135
136 public:
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;
144
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 =
148 default;
149
150 constexpr basic_string_view(const_pointer s, size_type count) : _begin(s), _size(count) {}
151
152 constexpr basic_string_view(const_pointer s) : _begin(s), _size(traits_type::length(s)) {}
153
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()) {}
158
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()) {}
162#endif
163
164 basic_string_view(std::nullptr_t) = delete;
165
166 constexpr const_iterator begin() const noexcept {
167 return const_iterator(_begin);
168 }
169 constexpr const_iterator end() const noexcept {
170 return begin() + size();
171 }
172 constexpr const_iterator cbegin() const noexcept {
173 return begin();
174 }
175 constexpr const_iterator cend() const noexcept {
176 return end();
177 }
178
179 constexpr const_reverse_iterator rbegin() const noexcept {
180 return const_reverse_iterator{end()};
181 }
182
183 constexpr const_reverse_iterator rend() const noexcept {
184 return const_reverse_iterator{begin()};
185 }
186
187 constexpr const_reverse_iterator crbegin() const noexcept {
188 return const_reverse_iterator{cend()};
189 }
190
191 constexpr const_reverse_iterator crend() const noexcept {
192 return const_reverse_iterator{crbegin()};
193 }
194
195 constexpr const_reference operator[](size_type offset) const {
196 return _begin[offset];
197 }
198
199 bsoncxx_cxx14_constexpr const_reference at(size_type pos) const {
200 if (pos >= size()) {
201 throw std::out_of_range{"bsoncxx::stdx::basic_string_view::at()"};
202 }
203 return _begin[pos];
204 }
205
206 constexpr const_reference front() const {
207 return (*this)[0];
208 }
209
210 constexpr const_reference back() const {
211 return (*this)[size() - 1];
212 }
213
214 constexpr const_pointer data() const noexcept {
215 return _begin;
216 }
217
218 constexpr size_type size() const noexcept {
219 return _size;
220 }
221
222 constexpr size_type length() const noexcept {
223 return size();
224 }
225
226 constexpr bool empty() const noexcept {
227 return size() == 0;
228 }
229
230 constexpr size_type max_size() const noexcept {
231 return static_cast<size_type>(std::numeric_limits<difference_type>::max());
232 }
233
234 bsoncxx_cxx14_constexpr void remove_prefix(size_type n) {
235 _begin += n;
236 _size -= n;
237 }
238
239 bsoncxx_cxx14_constexpr void remove_suffix(size_type n) {
240 _size -= n;
241 }
242
243 bsoncxx_cxx14_constexpr void swap(basic_string_view& other) {
244 std::swap(_begin, other._begin);
245 std::swap(_size, other._size);
246 }
247
248 size_type copy(pointer dest, size_type count, size_type pos = 0) const {
249 if (pos > size()) {
250 throw std::out_of_range{"bsoncxx::stdx::basic_string_view::substr()"};
251 }
252 count = (std::min)(count, size() - pos);
253 Traits::copy(dest, data() + pos, count);
254 return count;
255 }
256
257 bsoncxx_cxx14_constexpr basic_string_view substr(size_type pos = 0,
258 size_type count = npos) const {
259 if (pos > size()) {
260 throw std::out_of_range{"bsoncxx::stdx::basic_string_view::substr()"};
261 }
262 return basic_string_view(_begin + pos, (std::min)(count, size() - pos));
263 }
264
265 constexpr int compare(basic_string_view other) const noexcept {
266 // Another level of indirection to support restricted C++11 constexpr.
267 return _compare2(Traits::compare(data(), other.data(), (std::min)(size(), other.size())),
268 other);
269 }
270
271 constexpr int compare(const_pointer cstr) const {
272 return compare(basic_string_view(cstr));
273 }
274
275 constexpr int compare(size_type pos1, size_type count1, basic_string_view other) const {
276 return substr(pos1, count1).compare(other);
277 }
278
279 constexpr int compare(size_type pos1, size_type count1, const_pointer cstr) const {
280 return compare(pos1, count1, basic_string_view(cstr));
281 }
282
283 constexpr int compare(size_type pos1,
284 size_type count1,
285 basic_string_view other,
286 size_type pos2,
287 size_type count2) const {
288 return substr(pos1, count1).compare(other.substr(pos2, count2));
289 }
290
291 constexpr int compare(size_type pos1,
292 size_type count1,
293 const_pointer str,
294 size_type count2) const {
295 return substr(pos1, count1).compare(basic_string_view(str, count2));
296 }
297
298 bsoncxx_cxx14_constexpr size_type find(basic_string_view infix, size_type pos = 0) const
299 noexcept {
300 if (pos > size()) {
301 return npos;
302 }
303 basic_string_view sub = this->substr(pos);
304 if (infix.empty()) {
305 // The empty string is always "present" at the beginning of any string.
306 return pos;
307 }
308 const_iterator found = std::search(sub.begin(), sub.end(), infix.begin(), infix.end());
309 if (found == sub.end()) {
310 return npos;
311 }
312 return static_cast<size_type>(found - begin());
313 }
314
315 bsoncxx_cxx14_constexpr size_type rfind(basic_string_view infix, size_type pos = npos) const
316 noexcept {
317 // Calc the endpos where searching should begin, which includes the infix size.
318 const size_type substr_size = pos != npos ? pos + infix.size() : pos;
319 if (infix.empty()) {
320 return (std::min)(pos, size());
321 }
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()) {
325 return npos;
326 }
327 return static_cast<size_type>(rend() - f) - infix.size();
328 }
329
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; });
332 }
333
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; });
336 }
337
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; });
340 }
341
342 constexpr size_type find_last_not_of(basic_string_view set, size_type pos = npos) const
343 noexcept {
344 return _rfind_if(pos, [&](value_type chr) { return set.find(chr) == npos; });
345 }
346
347#pragma push_macro("DECL_FINDERS")
348#undef 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); \
352 } \
353 constexpr size_type Name(const_pointer cstr, size_type pos, size_type count) const { \
354 return Name(basic_string_view(cstr, count), pos); \
355 } \
356 constexpr size_type Name(const_pointer cstr, size_type pos = DefaultPos) const { \
357 return Name(basic_string_view(cstr), pos); \
358 } \
359 BSONCXX_FORCE_SEMICOLON
360
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")
368
369 // Explicit-conversion to a std::basic_string.
370 template <typename Allocator>
371 explicit operator std::basic_string<Char, Traits, Allocator>() const {
372 return std::basic_string<Char, Traits, Allocator>(data(), size());
373 }
374
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());
378 }
379#endif
380
381 private:
382 // Additional level-of-indirection for constexpr compare().
383 constexpr int _compare2(int diff, basic_string_view other) const noexcept {
384 // "diff" is the diff according to Traits::cmp
385 return diff ? diff : static_cast<int>(size() - other.size());
386 }
387
388 // Implementation of equality comparison.
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;
393 }
394
395 // Implementation of a three-way-comparison.
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);
401 }
402
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);
406 return out;
407 }
408
409 // Find the first in-bounds index I in [pos, size()) where the given predicate
410 // returns true for substr(I). If no index exists, returns npos.
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()) {
416 return npos;
417 }
418 return static_cast<size_type>(found - begin());
419 }
420
421 // Find the LAST index I in [0, pos] where the given predicate returns true for
422 // substr(0, I). If no such index exists, returns npos.
423 template <typename F>
424 bsoncxx_cxx14_constexpr size_type _rfind_if(size_type pos, F pred) const noexcept {
425 // Adjust 'pos' for an inclusive range in substr()
426 const auto rpos = pos == npos ? npos : pos + 1;
427 // The substring that will be searched:
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()) {
431 return npos;
432 }
433 // Adjust by 1 to account for reversed-ness
434 return static_cast<size_type>(rend() - found) - 1u;
435 }
436};
437
438// Required to define this here for compatibility with C++14 and older. Can be removed in C++17 or
439// newer.
440template <typename C, typename Tr>
441constexpr std::size_t basic_string_view<C, Tr>::npos;
442
443using string_view = basic_string_view<char>;
444
445} // namespace stdx
446} // namespace v_noabi
447} // namespace bsoncxx
448
449namespace std {
450
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()));
458 }
459};
460
461} // namespace std
462
463#else
464#error "Cannot find a valid polyfill for string_view"
465#endif
466
468
469namespace bsoncxx {
470namespace stdx {
471
472using ::bsoncxx::v_noabi::stdx::basic_string_view;
473using ::bsoncxx::v_noabi::stdx::string_view;
474
475} // namespace stdx
476} // namespace bsoncxx
477
489
490#if defined(BSONCXX_PRIVATE_DOXYGEN_PREPROCESSOR)
491
492namespace bsoncxx {
493namespace v_noabi {
494namespace stdx {
495
503class string_view {};
504
505} // namespace stdx
506} // namespace v_noabi
507} // namespace bsoncxx
508
509#endif // 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.