MongoDB C++ Driver 4.1.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
20
21#if defined(BSONCXX_POLY_USE_STD)
22
23#include <string_view>
24
25namespace bsoncxx {
26namespace v1 {
27namespace stdx {
28
29using std::basic_string_view;
30using std::string_view;
31
32} // namespace stdx
33} // namespace v1
34} // namespace bsoncxx
35
36#elif defined(BSONCXX_POLY_USE_IMPLS)
37
41
42#include <algorithm>
43#include <cstddef>
44#include <ios>
45#include <limits>
46#include <stdexcept>
47#include <string>
48#include <utility>
49
50namespace bsoncxx {
51namespace v1 {
52namespace stdx {
53
54template <typename Char, typename Traits = std::char_traits<Char>>
55class basic_string_view : bsoncxx::detail::equality_operators, bsoncxx::detail::ordering_operators {
56 public:
57 using pointer = Char*;
58 using const_pointer = Char const*;
59 using size_type = std::size_t;
60 using difference_type = std::ptrdiff_t;
61 using value_type = Char;
62
63 // Constant sentinel value to represent an impossible/invalid string position.
64 static constexpr size_type npos = static_cast<size_type>(-1);
65
66 private:
67 // Pointer to the beginning of the string being viewed.
68 const_pointer _begin = nullptr;
69 // The size of the array that is being viewed via `_begin`.
70 size_type _size = 0;
71
72 public:
73 using traits_type = Traits;
74 using reference = Char&;
75 using const_reference = Char const&;
76 using const_iterator = const_pointer;
77 using iterator = const_iterator;
78 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
79 using reverse_iterator = const_reverse_iterator;
80
81 constexpr basic_string_view() noexcept = default;
82 constexpr basic_string_view(basic_string_view const&) noexcept = default;
83 BSONCXX_PRIVATE_CONSTEXPR_CXX14 basic_string_view& operator=(basic_string_view const&) noexcept = default;
84
85 constexpr basic_string_view(const_pointer s, size_type count) : _begin(s), _size(count) {}
86
87 constexpr basic_string_view(const_pointer s) : _begin(s), _size(traits_type::length(s)) {}
88
89 template <typename Alloc>
90 constexpr basic_string_view(std::basic_string<value_type, traits_type, Alloc> const& str) noexcept
91 : _begin(str.data()), _size(str.size()) {}
92
93#if defined(__cpp_lib_string_view)
94 constexpr basic_string_view(std::basic_string_view<value_type, traits_type> sv) noexcept
95 : _begin(sv.data()), _size(sv.size()) {}
96#endif
97
98 basic_string_view(std::nullptr_t) = delete;
99
100 constexpr const_iterator begin() const noexcept {
101 return const_iterator(_begin);
102 }
103 constexpr const_iterator end() const noexcept {
104 return begin() + size();
105 }
106 constexpr const_iterator cbegin() const noexcept {
107 return begin();
108 }
109 constexpr const_iterator cend() const noexcept {
110 return end();
111 }
112
113 constexpr const_reverse_iterator rbegin() const noexcept {
114 return const_reverse_iterator{end()};
115 }
116
117 constexpr const_reverse_iterator rend() const noexcept {
118 return const_reverse_iterator{begin()};
119 }
120
121 constexpr const_reverse_iterator crbegin() const noexcept {
122 return const_reverse_iterator{cend()};
123 }
124
125 constexpr const_reverse_iterator crend() const noexcept {
126 return const_reverse_iterator{crbegin()};
127 }
128
129 constexpr const_reference operator[](size_type offset) const {
130 return _begin[offset];
131 }
132
133 BSONCXX_PRIVATE_CONSTEXPR_CXX14 const_reference at(size_type pos) const {
134 if (pos >= size()) {
135 throw std::out_of_range{"bsoncxx::stdx::basic_string_view::at()"};
136 }
137 return _begin[pos];
138 }
139
140 constexpr const_reference front() const {
141 return (*this)[0];
142 }
143
144 constexpr const_reference back() const {
145 return (*this)[size() - 1];
146 }
147
148 constexpr const_pointer data() const noexcept {
149 return _begin;
150 }
151
152 constexpr size_type size() const noexcept {
153 return _size;
154 }
155
156 constexpr size_type length() const noexcept {
157 return size();
158 }
159
160 constexpr bool empty() const noexcept {
161 return size() == 0;
162 }
163
164 constexpr size_type max_size() const noexcept {
165 return static_cast<size_type>(std::numeric_limits<difference_type>::max());
166 }
167
168 BSONCXX_PRIVATE_CONSTEXPR_CXX14 void remove_prefix(size_type n) {
169 _begin += n;
170 _size -= n;
171 }
172
173 BSONCXX_PRIVATE_CONSTEXPR_CXX14 void remove_suffix(size_type n) {
174 _size -= n;
175 }
176
177 BSONCXX_PRIVATE_CONSTEXPR_CXX14 void swap(basic_string_view& other) {
178 std::swap(_begin, other._begin);
179 std::swap(_size, other._size);
180 }
181
182 size_type copy(pointer dest, size_type count, size_type pos = 0) const {
183 if (pos > size()) {
184 throw std::out_of_range{"bsoncxx::stdx::basic_string_view::substr()"};
185 }
186 count = (std::min)(count, size() - pos);
187 Traits::copy(dest, data() + pos, count);
188 return count;
189 }
190
191 BSONCXX_PRIVATE_CONSTEXPR_CXX14 basic_string_view substr(size_type pos = 0, size_type count = npos) const {
192 if (pos > size()) {
193 throw std::out_of_range{"bsoncxx::stdx::basic_string_view::substr()"};
194 }
195 return basic_string_view(_begin + pos, (std::min)(count, size() - pos));
196 }
197
198 constexpr int compare(basic_string_view other) const noexcept {
199 // Another level of indirection to support restricted C++11 constexpr.
200 return _compare2(Traits::compare(data(), other.data(), (std::min)(size(), other.size())), other);
201 }
202
203 constexpr int compare(const_pointer cstr) const {
204 return compare(basic_string_view(cstr));
205 }
206
207 constexpr int compare(size_type pos1, size_type count1, basic_string_view other) const {
208 return substr(pos1, count1).compare(other);
209 }
210
211 constexpr int compare(size_type pos1, size_type count1, const_pointer cstr) const {
212 return compare(pos1, count1, basic_string_view(cstr));
213 }
214
215 constexpr int compare(size_type pos1, size_type count1, basic_string_view other, size_type pos2, size_type count2)
216 const {
217 return substr(pos1, count1).compare(other.substr(pos2, count2));
218 }
219
220 constexpr int compare(size_type pos1, size_type count1, const_pointer str, size_type count2) const {
221 return substr(pos1, count1).compare(basic_string_view(str, count2));
222 }
223
224 BSONCXX_PRIVATE_CONSTEXPR_CXX14 size_type find(basic_string_view infix, size_type pos = 0) const noexcept {
225 if (pos > size()) {
226 return npos;
227 }
228 basic_string_view sub = this->substr(pos);
229 if (infix.empty()) {
230 // The empty string is always "present" at the beginning of any string.
231 return pos;
232 }
233 const_iterator found = std::search(sub.begin(), sub.end(), infix.begin(), infix.end());
234 if (found == sub.end()) {
235 return npos;
236 }
237 return static_cast<size_type>(found - begin());
238 }
239
240 BSONCXX_PRIVATE_CONSTEXPR_CXX14 size_type rfind(basic_string_view infix, size_type pos = npos) const noexcept {
241 // Calc the endpos where searching should begin, which includes the infix size.
242 size_type const substr_size = pos != npos ? pos + infix.size() : pos;
243 if (infix.empty()) {
244 return (std::min)(pos, size());
245 }
246 basic_string_view searched = this->substr(0, substr_size);
247 auto f = std::search(searched.rbegin(), searched.rend(), infix.rbegin(), infix.rend());
248 if (f == searched.rend()) {
249 return npos;
250 }
251 return static_cast<size_type>(rend() - f) - infix.size();
252 }
253
254 constexpr size_type find_first_of(basic_string_view set, size_type pos = 0) const noexcept {
255 return _find_if(pos, [&](value_type chr) { return set.find(chr) != npos; });
256 }
257
258 constexpr size_type find_last_of(basic_string_view set, size_type pos = npos) const noexcept {
259 return _rfind_if(pos, [&](value_type chr) { return set.find(chr) != npos; });
260 }
261
262 constexpr size_type find_first_not_of(basic_string_view set, size_type pos = 0) const noexcept {
263 return _find_if(pos, [&](value_type chr) { return set.find(chr) == npos; });
264 }
265
266 constexpr size_type find_last_not_of(basic_string_view set, size_type pos = npos) const noexcept {
267 return _rfind_if(pos, [&](value_type chr) { return set.find(chr) == npos; });
268 }
269
270#pragma push_macro("DECL_FINDERS")
271#undef DECL_FINDERS
272#define DECL_FINDERS(Name, DefaultPos) \
273 constexpr size_type Name(value_type chr, size_type pos = DefaultPos) const noexcept { \
274 return Name(basic_string_view(&chr, 1), pos); \
275 } \
276 constexpr size_type Name(const_pointer cstr, size_type pos, size_type count) const { \
277 return Name(basic_string_view(cstr, count), pos); \
278 } \
279 constexpr size_type Name(const_pointer cstr, size_type pos = DefaultPos) const { \
280 return Name(basic_string_view(cstr), pos); \
281 } \
282 BSONCXX_PRIVATE_FORCE_SEMICOLON
283
284 DECL_FINDERS(find, 0);
285 DECL_FINDERS(rfind, npos);
286 DECL_FINDERS(find_first_of, 0);
287 DECL_FINDERS(find_last_of, npos);
288 DECL_FINDERS(find_first_not_of, 0);
289 DECL_FINDERS(find_last_not_of, npos);
290#pragma pop_macro("DECL_FINDERS")
291
292 // Explicit-conversion to a std::basic_string.
293 template <typename Allocator>
294 explicit operator std::basic_string<Char, Traits, Allocator>() const {
295 return std::basic_string<Char, Traits, Allocator>(data(), size());
296 }
297
298#if defined(__cpp_lib_string_view)
299 explicit operator std::basic_string_view<value_type, traits_type>() const noexcept {
300 return std::basic_string_view<value_type, traits_type>(data(), size());
301 }
302#endif
303
304 private:
305 // Additional level-of-indirection for constexpr compare().
306 constexpr int _compare2(int diff, basic_string_view other) const noexcept {
307 // "diff" is the diff according to Traits::cmp
308 return diff ? diff : static_cast<int>(size() - other.size());
309 }
310
311 // Implementation of equality comparison.
312 constexpr friend bool
313 tag_invoke(bsoncxx::detail::equal_to, basic_string_view left, basic_string_view right) noexcept {
314 return left.size() == right.size() && left.compare(right) == 0;
315 }
316
317 // Implementation of a three-way-comparison.
318 constexpr friend bsoncxx::detail::strong_ordering
319 tag_invoke(bsoncxx::detail::compare_three_way cmp, basic_string_view left, basic_string_view right) noexcept {
320 return cmp(left.compare(right), 0);
321 }
322
323 friend std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& out, basic_string_view self) {
324 out << std::basic_string<Char, Traits>(self);
325 return out;
326 }
327
328 // Find the first in-bounds index I in [pos, size()) where the given predicate
329 // returns true for substr(I). If no index exists, returns npos.
330 template <typename F>
331 BSONCXX_PRIVATE_CONSTEXPR_CXX14 size_type _find_if(size_type pos, F pred) const noexcept {
332 auto const sub = substr(pos);
333 iterator const found = std::find_if(sub.begin(), sub.end(), pred);
334 if (found == end()) {
335 return npos;
336 }
337 return static_cast<size_type>(found - begin());
338 }
339
340 // Find the LAST index I in [0, pos] where the given predicate returns true for
341 // substr(0, I). If no such index exists, returns npos.
342 template <typename F>
343 BSONCXX_PRIVATE_CONSTEXPR_CXX14 size_type _rfind_if(size_type pos, F pred) const noexcept {
344 // Adjust 'pos' for an inclusive range in substr()
345 auto const rpos = pos == npos ? npos : pos + 1;
346 // The substring that will be searched:
347 auto const prefix = substr(0, rpos);
348 const_reverse_iterator const found = std::find_if(prefix.rbegin(), prefix.rend(), pred);
349 if (found == rend()) {
350 return npos;
351 }
352 // Adjust by 1 to account for reversed-ness
353 return static_cast<size_type>(rend() - found) - 1u;
354 }
355};
356
357// Required to define this here for compatibility with C++14 and older. Can be removed in C++17 or
358// newer.
359template <typename C, typename Tr>
360constexpr std::size_t basic_string_view<C, Tr>::npos;
361
362using string_view = basic_string_view<char>;
363
364} // namespace stdx
365} // namespace v1
366} // namespace bsoncxx
367
368namespace std {
369
370template <typename CharT, typename Traits>
371struct hash<bsoncxx::v1::stdx::basic_string_view<CharT, Traits>> : private std::hash<std::basic_string<CharT, Traits>> {
372 std::size_t operator()(bsoncxx::v1::stdx::basic_string_view<CharT, Traits> const& str) const {
373 return std::hash<std::basic_string<CharT, Traits>>::operator()(
374 std::basic_string<CharT, Traits>(str.data(), str.size()));
375 }
376};
377
378} // namespace std
379
380#else
381#error "Cannot find a valid polyfill for string_view"
382#endif
383
385
397
398#if defined(BSONCXX_PRIVATE_DOXYGEN_PREPROCESSOR)
399
400namespace bsoncxx {
401namespace v1 {
402namespace stdx {
403
411class string_view {};
412
413} // namespace stdx
414} // namespace v1
415} // namespace bsoncxx
416
417#endif // defined(BSONCXX_PRIVATE_DOXYGEN_PREPROCESSOR)
Provides macros describing the bsoncxx library configuration.
For internal use only!
The bsoncxx v1 macro guard postlude header.
The bsoncxx v1 macro guard prelude header.
A polyfill for std::string_view.
Definition string_view.hpp:411
For internal use only!
Declares C++17 standard library polyfills.
Declares entities whose ABI stability is guaranteed for documented symbols.
The top-level namespace within which all bsoncxx library entities are declared.
For internal use only!