]>
Commit | Line | Data |
---|---|---|
20effc67 TL |
1 | // |
2 | // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com) | |
3 | // Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com) | |
4 | // | |
5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
7 | // | |
8 | // Official repository: https://github.com/boostorg/json | |
9 | // | |
10 | ||
11 | #ifndef BOOST_JSON_STRING_HPP | |
12 | #define BOOST_JSON_STRING_HPP | |
13 | ||
14 | #include <boost/json/detail/config.hpp> | |
15 | #include <boost/json/pilfer.hpp> | |
16 | #include <boost/json/storage_ptr.hpp> | |
17 | #include <boost/json/string_view.hpp> | |
18 | #include <boost/json/detail/digest.hpp> | |
19 | #include <boost/json/detail/except.hpp> | |
20 | #include <boost/json/detail/string_impl.hpp> | |
21 | #include <boost/json/detail/value.hpp> | |
22 | #include <algorithm> | |
23 | #include <cstring> | |
24 | #include <initializer_list> | |
25 | #include <iosfwd> | |
26 | #include <iterator> | |
27 | #include <limits> | |
28 | #include <new> | |
29 | #include <type_traits> | |
30 | #include <utility> | |
31 | ||
32 | BOOST_JSON_NS_BEGIN | |
33 | ||
34 | class value; | |
35 | ||
36 | /** The native type of string values. | |
37 | ||
38 | Instances of string store and manipulate sequences | |
39 | of `char` using the UTF-8 encoding. The elements of | |
40 | a string are stored contiguously. A pointer to any | |
41 | character in a string may be passed to functions | |
42 | that expect a pointer to the first element of a | |
43 | null-terminated `char` array. | |
44 | ||
45 | String iterators are regular `char` pointers. | |
46 | ||
47 | @note `string` member functions do not validate | |
1e59de90 | 48 | any UTF-8 byte sequences passed to them. |
20effc67 TL |
49 | |
50 | @par Thread Safety | |
51 | ||
52 | Non-const member functions may not be called | |
53 | concurrently with any other member functions. | |
54 | ||
55 | @par Satisfies | |
56 | <a href="https://en.cppreference.com/w/cpp/named_req/ContiguousContainer"><em>ContiguousContainer</em></a>, | |
57 | <a href="https://en.cppreference.com/w/cpp/named_req/ReversibleContainer"><em>ReversibleContainer</em></a>, and | |
58 | <a href="https://en.cppreference.com/w/cpp/named_req/SequenceContainer"><em>SequenceContainer</em></a>. | |
59 | */ | |
60 | class string | |
61 | { | |
62 | friend class value; | |
63 | #ifndef BOOST_JSON_DOCS | |
64 | // VFALCO doc toolchain shouldn't show this but does | |
65 | friend struct detail::access; | |
66 | #endif | |
67 | ||
68 | using string_impl = detail::string_impl; | |
69 | ||
70 | inline | |
71 | string( | |
72 | detail::key_t const&, | |
73 | string_view s, | |
74 | storage_ptr sp); | |
75 | ||
76 | inline | |
77 | string( | |
78 | detail::key_t const&, | |
79 | string_view s1, | |
80 | string_view s2, | |
81 | storage_ptr sp); | |
82 | ||
83 | public: | |
84 | /** The type of _Allocator_ returned by @ref get_allocator | |
85 | ||
86 | This type is a @ref polymorphic_allocator. | |
87 | */ | |
88 | #ifdef BOOST_JSON_DOCS | |
89 | // VFALCO doc toolchain renders this incorrectly | |
90 | using allocator_type = __see_below__; | |
91 | #else | |
92 | using allocator_type = polymorphic_allocator<value>; | |
93 | #endif | |
94 | ||
95 | /// The type of a character | |
96 | using value_type = char; | |
97 | ||
98 | /// The type used to represent unsigned integers | |
99 | using size_type = std::size_t; | |
100 | ||
101 | /// The type used to represent signed integers | |
102 | using difference_type = std::ptrdiff_t; | |
103 | ||
104 | /// A pointer to an element | |
105 | using pointer = char*; | |
106 | ||
107 | /// A const pointer to an element | |
108 | using const_pointer = char const*; | |
109 | ||
110 | /// A reference to an element | |
111 | using reference = char&; | |
112 | ||
113 | /// A const reference to an element | |
114 | using const_reference = const char&; | |
115 | ||
116 | /// A random access iterator to an element | |
117 | using iterator = char*; | |
118 | ||
119 | /// A random access const iterator to an element | |
120 | using const_iterator = char const*; | |
121 | ||
122 | /// A reverse random access iterator to an element | |
123 | using reverse_iterator = | |
124 | std::reverse_iterator<iterator>; | |
125 | ||
126 | /// A reverse random access const iterator to an element | |
127 | using const_reverse_iterator = | |
128 | std::reverse_iterator<const_iterator>; | |
129 | ||
130 | /// A special index | |
131 | static constexpr std::size_t npos = | |
132 | string_view::npos; | |
133 | ||
134 | private: | |
135 | template<class T> | |
136 | using is_inputit = typename std::enable_if< | |
137 | std::is_convertible<typename | |
138 | std::iterator_traits<T>::value_type, | |
139 | char>::value>::type; | |
140 | ||
141 | storage_ptr sp_; // must come first | |
142 | string_impl impl_; | |
143 | ||
144 | public: | |
145 | /** Destructor. | |
146 | ||
147 | Any dynamically allocated internal storage | |
148 | is freed. | |
149 | ||
150 | @par Complexity | |
151 | Constant. | |
152 | ||
153 | @par Exception Safety | |
154 | No-throw guarantee. | |
155 | */ | |
1e59de90 | 156 | ~string() noexcept |
20effc67 TL |
157 | { |
158 | impl_.destroy(sp_); | |
159 | } | |
160 | ||
161 | //------------------------------------------------------ | |
162 | // | |
163 | // Construction | |
164 | // | |
165 | //------------------------------------------------------ | |
166 | ||
167 | /** Default constructor. | |
168 | ||
169 | The string will have a zero size and a non-zero, | |
170 | unspecified capacity, using the default memory resource. | |
171 | ||
172 | @par Complexity | |
173 | ||
174 | Constant. | |
175 | */ | |
176 | string() = default; | |
177 | ||
178 | /** Pilfer constructor. | |
179 | ||
180 | The string is constructed by acquiring ownership | |
181 | of the contents of `other` using pilfer semantics. | |
182 | This is more efficient than move construction, when | |
183 | it is known that the moved-from object will be | |
184 | immediately destroyed afterwards. | |
1e59de90 | 185 | |
20effc67 TL |
186 | @par Complexity |
187 | Constant. | |
188 | ||
189 | @par Exception Safety | |
190 | No-throw guarantee. | |
191 | ||
192 | @param other The value to pilfer. After pilfer | |
193 | construction, `other` is not in a usable state | |
194 | and may only be destroyed. | |
195 | ||
196 | @see @ref pilfer, | |
197 | <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0308r0.html"> | |
198 | Valueless Variants Considered Harmful</a> | |
199 | */ | |
200 | string(pilfered<string> other) noexcept | |
201 | : sp_(std::move(other.get().sp_)) | |
202 | , impl_(other.get().impl_) | |
203 | { | |
204 | ::new(&other.get().impl_) string_impl(); | |
205 | } | |
206 | ||
207 | /** Constructor. | |
208 | ||
209 | The string will have zero size and a non-zero, | |
210 | unspecified capacity, obtained from the specified | |
211 | memory resource. | |
212 | ||
213 | @par Complexity | |
214 | ||
215 | Constant. | |
216 | ||
217 | @param sp A pointer to the @ref memory_resource | |
218 | to use. The container will acquire shared | |
219 | ownership of the memory resource. | |
220 | */ | |
221 | explicit | |
222 | string(storage_ptr sp) | |
223 | : sp_(std::move(sp)) | |
224 | { | |
225 | } | |
226 | ||
227 | /** Constructor. | |
228 | ||
229 | Construct the contents with `count` copies of | |
230 | character `ch`. | |
231 | ||
232 | @par Complexity | |
233 | ||
234 | Linear in `count`. | |
235 | ||
236 | @par Exception Safety | |
237 | ||
238 | Strong guarantee. | |
239 | Calls to `memory_resource::allocate` may throw. | |
240 | ||
241 | @param count The size of the resulting string. | |
242 | ||
243 | @param ch The value to initialize characters | |
244 | of the string with. | |
245 | ||
246 | @param sp An optional pointer to the @ref memory_resource | |
247 | to use. The container will acquire shared | |
248 | ownership of the memory resource. | |
249 | The default argument for this parameter is `{}`. | |
250 | ||
251 | @throw std::length_error `count > max_size()`. | |
252 | */ | |
253 | BOOST_JSON_DECL | |
254 | explicit | |
255 | string( | |
256 | std::size_t count, | |
257 | char ch, | |
258 | storage_ptr sp = {}); | |
259 | ||
260 | /** Constructor. | |
261 | ||
262 | Construct the contents with those of the null | |
263 | terminated string pointed to by `s`. The length | |
264 | of the string is determined by the first null | |
265 | character. | |
266 | ||
267 | @par Complexity | |
268 | ||
269 | Linear in `strlen(s)`. | |
270 | ||
271 | @par Exception Safety | |
272 | ||
273 | Strong guarantee. | |
274 | Calls to `memory_resource::allocate` may throw. | |
275 | ||
276 | @param s A pointer to a character string used to | |
277 | copy from. | |
278 | ||
279 | @param sp An optional pointer to the @ref memory_resource | |
280 | to use. The container will acquire shared | |
281 | ownership of the memory resource. | |
282 | The default argument for this parameter is `{}`. | |
283 | ||
284 | @throw std::length_error `strlen(s) > max_size()`. | |
285 | */ | |
286 | BOOST_JSON_DECL | |
287 | string( | |
288 | char const* s, | |
289 | storage_ptr sp = {}); | |
290 | ||
291 | /** Constructor. | |
292 | ||
293 | Construct the contents with copies of the | |
294 | characters in the range `{s, s+count)`. | |
295 | This range can contain null characters. | |
296 | ||
297 | @par Complexity | |
298 | ||
299 | Linear in `count`. | |
300 | ||
301 | @par Exception Safety | |
302 | ||
303 | Strong guarantee. | |
304 | Calls to `memory_resource::allocate` may throw. | |
305 | ||
306 | @param count The number of characters to copy. | |
307 | ||
308 | @param s A pointer to a character string used to | |
309 | copy from. | |
310 | ||
311 | @param sp An optional pointer to the @ref memory_resource | |
312 | to use. The container will acquire shared | |
313 | ownership of the memory resource. | |
314 | The default argument for this parameter is `{}`. | |
315 | ||
316 | @throw std::length_error `count > max_size()`. | |
317 | */ | |
318 | BOOST_JSON_DECL | |
319 | explicit | |
320 | string( | |
321 | char const* s, | |
322 | std::size_t count, | |
323 | storage_ptr sp = {}); | |
324 | ||
325 | /** Constructor. | |
326 | ||
327 | Construct the contents with copies of characters | |
328 | in the range `{first, last)`. | |
329 | ||
330 | @par Complexity | |
331 | ||
332 | Linear in `std::distance(first, last)`. | |
333 | ||
334 | @par Exception Safety | |
335 | ||
336 | Strong guarantee. | |
337 | Calls to `memory_resource::allocate` may throw. | |
338 | ||
339 | @tparam InputIt The type of the iterators. | |
340 | ||
341 | @par Constraints | |
342 | ||
343 | `InputIt` satisfies __InputIterator__. | |
344 | ||
345 | @param first An input iterator pointing to the | |
346 | first character to insert, or pointing to the | |
347 | end of the range. | |
348 | ||
349 | @param last An input iterator pointing to the end | |
350 | of the range. | |
351 | ||
352 | @param sp An optional pointer to the @ref memory_resource | |
353 | to use. The container will acquire shared | |
354 | ownership of the memory resource. | |
355 | The default argument for this parameter is `{}`. | |
356 | ||
357 | @throw std::length_error `std::distance(first, last) > max_size()`. | |
358 | */ | |
359 | template<class InputIt | |
360 | #ifndef BOOST_JSON_DOCS | |
361 | ,class = is_inputit<InputIt> | |
362 | #endif | |
363 | > | |
364 | explicit | |
365 | string( | |
366 | InputIt first, | |
367 | InputIt last, | |
368 | storage_ptr sp = {}); | |
369 | ||
370 | /** Copy constructor. | |
371 | ||
372 | Construct the contents with a copy of `other`. | |
373 | ||
374 | @par Complexity | |
375 | ||
376 | Linear in `other.size()`. | |
377 | ||
378 | @par Exception Safety | |
379 | ||
380 | Strong guarantee. | |
381 | Calls to `memory_resource::allocate` may throw. | |
382 | ||
383 | @param other The string to use as a source | |
384 | to copy from. | |
385 | */ | |
386 | BOOST_JSON_DECL | |
387 | string(string const& other); | |
388 | ||
389 | /** Constructor. | |
390 | ||
391 | Construct the contents with a copy of `other`. | |
392 | ||
393 | @par Complexity | |
394 | ||
395 | Linear in `other.size()`. | |
396 | ||
397 | @par Exception Safety | |
398 | ||
399 | Strong guarantee. | |
400 | Calls to `memory_resource::allocate` may throw. | |
401 | ||
402 | @param other The string to use as a source | |
403 | to copy from. | |
404 | ||
405 | @param sp An optional pointer to the @ref memory_resource | |
406 | to use. The container will acquire shared | |
407 | ownership of the memory resource. | |
408 | The default argument for this parameter is `{}`. | |
409 | */ | |
410 | BOOST_JSON_DECL | |
411 | explicit | |
412 | string( | |
413 | string const& other, | |
414 | storage_ptr sp); | |
415 | ||
416 | /** Move constructor. | |
417 | ||
418 | Constructs the string with the contents of `other` | |
419 | using move semantics. Ownership of the underlying | |
420 | memory is transferred. | |
421 | The container acquires shared ownership of the | |
1e59de90 | 422 | @ref memory_resource used by `other`. After construction, |
20effc67 TL |
423 | the moved-from string behaves as if newly |
424 | constructed with its current memory resource. | |
1e59de90 | 425 | |
20effc67 TL |
426 | @par Complexity |
427 | ||
428 | Constant. | |
429 | ||
430 | @param other The string to move | |
431 | */ | |
432 | string(string&& other) noexcept | |
433 | : sp_(other.sp_) | |
434 | , impl_(other.impl_) | |
435 | { | |
436 | ::new(&other.impl_) string_impl(); | |
437 | } | |
438 | ||
439 | /** Constructor. | |
440 | ||
441 | Construct the contents with those of `other` | |
442 | using move semantics. | |
443 | ||
444 | @li If `*other.storage() == *sp`, | |
445 | ownership of the underlying memory is transferred | |
446 | in constant time, with no possibility | |
447 | of exceptions. After construction, the moved-from | |
448 | string behaves as if newly constructed with | |
449 | its current @ref memory_resource. Otherwise, | |
450 | ||
451 | @li If `*other.storage() != *sp`, | |
452 | a copy of the characters in `other` is made. In | |
453 | this case, the moved-from string is not changed. | |
454 | ||
455 | @par Complexity | |
456 | ||
457 | Constant or linear in `other.size()`. | |
458 | ||
459 | @par Exception Safety | |
460 | ||
461 | Strong guarantee. | |
462 | Calls to `memory_resource::allocate` may throw. | |
463 | ||
464 | @param other The string to assign from. | |
465 | ||
466 | @param sp An optional pointer to the @ref memory_resource | |
467 | to use. The container will acquire shared | |
468 | ownership of the memory resource. | |
469 | The default argument for this parameter is `{}`. | |
470 | */ | |
471 | BOOST_JSON_DECL | |
472 | explicit | |
473 | string( | |
474 | string&& other, | |
475 | storage_ptr sp); | |
476 | ||
477 | /** Constructor. | |
1e59de90 | 478 | |
20effc67 TL |
479 | Construct the contents with those of a |
480 | string view. This view can contain | |
481 | null characters. | |
482 | ||
483 | @par Complexity | |
484 | ||
485 | Linear in `s.size()`. | |
486 | ||
487 | @par Exception Safety | |
488 | ||
489 | Strong guarantee. | |
490 | Calls to `memory_resource::allocate` may throw. | |
491 | ||
492 | @param s The string view to copy from. | |
493 | ||
494 | @param sp An optional pointer to the @ref memory_resource | |
495 | to use. The container will acquire shared | |
496 | ownership of the memory resource. | |
497 | The default argument for this parameter is `{}`. | |
498 | ||
499 | @throw std::length_error `s.size() > max_size()`. | |
500 | */ | |
501 | BOOST_JSON_DECL | |
502 | string( | |
503 | string_view s, | |
504 | storage_ptr sp = {}); | |
505 | ||
506 | //------------------------------------------------------ | |
507 | // | |
508 | // Assignment | |
509 | // | |
510 | //------------------------------------------------------ | |
511 | ||
512 | /** Copy assignment. | |
513 | ||
514 | Replace the contents with a copy of `other`. | |
515 | ||
516 | @par Complexity | |
517 | ||
518 | Linear in `other.size()`. | |
519 | ||
520 | @par Exception Safety | |
521 | ||
522 | Strong guarantee. | |
523 | Calls to `memory_resource::allocate` may throw. | |
524 | ||
525 | @return `*this` | |
526 | ||
527 | @param other The string to use as a source | |
528 | to copy from. | |
529 | */ | |
530 | BOOST_JSON_DECL | |
531 | string& | |
532 | operator=(string const& other); | |
533 | ||
534 | /** Move assignment. | |
535 | ||
536 | Replace the contents with those of `other` | |
537 | using move semantics. | |
538 | ||
539 | @li If `*other.storage() == *this->storage()`, | |
540 | ownership of the underlying memory is transferred | |
541 | in constant time, with no possibility | |
542 | of exceptions. After construction, the moved-from | |
543 | string behaves as if newly constructed with its | |
544 | current @ref memory_resource. Otherwise, | |
545 | ||
546 | @li If `*other.storage() != *this->storage()`, | |
547 | a copy of the characters in `other` is made. In | |
548 | this case, the moved-from container is not changed. | |
549 | ||
550 | @par Complexity | |
551 | ||
552 | Constant or linear in `other.size()`. | |
553 | ||
554 | @par Exception Safety | |
555 | ||
556 | Strong guarantee. | |
557 | Calls to `memory_resource::allocate` may throw. | |
558 | ||
559 | @return `*this` | |
560 | ||
561 | @param other The string to use as a source | |
562 | to move from. | |
563 | */ | |
564 | BOOST_JSON_DECL | |
565 | string& | |
566 | operator=(string&& other); | |
567 | ||
568 | /** Assign a value to the string. | |
569 | ||
570 | Replaces the contents with those of the null | |
571 | terminated string pointed to by `s`. The length | |
572 | of the string is determined by the first null | |
573 | character. | |
574 | ||
575 | @par Complexity | |
576 | ||
577 | Linear in `std::strlen(s)`. | |
578 | ||
579 | @par Exception Safety | |
580 | ||
581 | Strong guarantee. | |
582 | Calls to `memory_resource::allocate` may throw. | |
583 | ||
584 | @return `*this` | |
585 | ||
586 | @param s The null-terminated character string. | |
587 | ||
588 | @throw std::length_error `std::strlen(s) > max_size()`. | |
589 | */ | |
590 | BOOST_JSON_DECL | |
591 | string& | |
592 | operator=(char const* s); | |
593 | ||
594 | /** Assign a value to the string. | |
595 | ||
596 | Replaces the contents with those of a | |
597 | string view. This view can contain | |
598 | null characters. | |
599 | ||
600 | @par Complexity | |
601 | ||
602 | Linear in `s.size()`. | |
603 | ||
604 | @par Exception Safety | |
605 | ||
606 | Strong guarantee. | |
607 | Calls to `memory_resource::allocate` may throw. | |
608 | ||
609 | @return `*this` | |
610 | ||
611 | @param s The string view to copy from. | |
612 | ||
613 | @throw std::length_error `s.size() > max_size()`. | |
614 | */ | |
615 | BOOST_JSON_DECL | |
616 | string& | |
617 | operator=(string_view s); | |
618 | ||
619 | //------------------------------------------------------ | |
620 | ||
621 | /** Assign characters to a string. | |
622 | ||
623 | Replace the contents with `count` copies of | |
624 | character `ch`. | |
625 | ||
626 | @par Complexity | |
627 | ||
628 | Linear in `count`. | |
629 | ||
630 | @par Exception Safety | |
631 | ||
632 | Strong guarantee. | |
633 | Calls to `memory_resource::allocate` may throw. | |
634 | ||
635 | @return `*this` | |
636 | ||
637 | @param count The size of the resulting string. | |
638 | ||
639 | @param ch The value to initialize characters | |
640 | of the string with. | |
641 | ||
642 | @throw std::length_error `count > max_size()`. | |
643 | */ | |
644 | BOOST_JSON_DECL | |
645 | string& | |
646 | assign( | |
647 | std::size_t count, | |
648 | char ch); | |
649 | ||
650 | /** Assign characters to a string. | |
651 | ||
652 | Replace the contents with a copy of `other`. | |
653 | ||
654 | @par Complexity | |
655 | ||
656 | Linear in `other.size()`. | |
657 | ||
658 | @par Exception Safety | |
659 | ||
660 | Strong guarantee. | |
661 | Calls to `memory_resource::allocate` may throw. | |
662 | ||
663 | @return `*this` | |
664 | ||
665 | @param other The string to use as a source | |
666 | to copy from. | |
667 | */ | |
668 | BOOST_JSON_DECL | |
669 | string& | |
670 | assign( | |
671 | string const& other); | |
672 | ||
673 | /** Assign characters to a string. | |
674 | ||
675 | Replace the contents with those of `other` | |
676 | using move semantics. | |
677 | ||
678 | @li If `*other.storage() == *this->storage()`, | |
679 | ownership of the underlying memory is transferred | |
680 | in constant time, with no possibility of | |
681 | exceptions. After construction, the moved-from | |
682 | string behaves as if newly constructed with | |
683 | its current @ref memory_resource, otherwise | |
684 | ||
685 | @li If `*other.storage() != *this->storage()`, | |
686 | a copy of the characters in `other` is made. | |
687 | In this case, the moved-from container | |
688 | is not changed. | |
689 | ||
690 | @par Complexity | |
691 | ||
692 | Constant or linear in `other.size()`. | |
693 | ||
694 | @par Exception Safety | |
695 | ||
696 | Strong guarantee. | |
697 | Calls to `memory_resource::allocate` may throw. | |
698 | ||
699 | @return `*this` | |
700 | ||
701 | @param other The string to assign from. | |
702 | */ | |
703 | BOOST_JSON_DECL | |
704 | string& | |
705 | assign(string&& other); | |
706 | ||
707 | /** Assign characters to a string. | |
708 | ||
709 | Replaces the contents with copies of the | |
710 | characters in the range `{s, s+count)`. This | |
711 | range can contain null characters. | |
712 | ||
713 | @par Complexity | |
714 | ||
715 | Linear in `count`. | |
716 | ||
717 | @par Exception Safety | |
718 | ||
719 | Strong guarantee. | |
720 | Calls to `memory_resource::allocate` may throw. | |
721 | ||
722 | @return `*this` | |
723 | ||
724 | @param count The number of characters to copy. | |
725 | ||
726 | @param s A pointer to a character string used to | |
727 | copy from. | |
728 | ||
729 | @throw std::length_error `count > max_size()`. | |
730 | */ | |
731 | BOOST_JSON_DECL | |
732 | string& | |
733 | assign( | |
734 | char const* s, | |
735 | std::size_t count); | |
736 | ||
737 | /** Assign characters to a string. | |
738 | ||
739 | Replaces the contents with those of the null | |
740 | terminated string pointed to by `s`. The length | |
741 | of the string is determined by the first null | |
742 | character. | |
743 | ||
744 | @par Complexity | |
745 | ||
746 | Linear in `strlen(s)`. | |
747 | ||
748 | @par Exception Safety | |
749 | ||
750 | Strong guarantee. | |
751 | ||
752 | @note | |
753 | ||
754 | Calls to `memory_resource::allocate` may throw. | |
755 | ||
756 | @return `*this` | |
757 | ||
758 | @param s A pointer to a character string used to | |
759 | copy from. | |
760 | ||
761 | @throw std::length_error `strlen(s) > max_size()`. | |
762 | */ | |
763 | BOOST_JSON_DECL | |
764 | string& | |
765 | assign( | |
766 | char const* s); | |
767 | ||
768 | /** Assign characters to a string. | |
769 | ||
770 | Replaces the contents with copies of characters | |
771 | in the range `{first, last)`. | |
772 | ||
773 | @par Complexity | |
774 | ||
775 | Linear in `std::distance(first, last)`. | |
776 | ||
777 | @par Exception Safety | |
778 | ||
779 | Strong guarantee. | |
780 | Calls to `memory_resource::allocate` may throw. | |
781 | ||
782 | @tparam InputIt The type of the iterators. | |
783 | ||
784 | @par Constraints | |
785 | ||
786 | `InputIt` satisfies __InputIterator__. | |
787 | ||
788 | @return `*this` | |
789 | ||
790 | @param first An input iterator pointing to the | |
791 | first character to insert, or pointing to the | |
792 | end of the range. | |
793 | ||
794 | @param last An input iterator pointing to the end | |
795 | of the range. | |
796 | ||
797 | @throw std::length_error `std::distance(first, last) > max_size()`. | |
798 | */ | |
799 | template<class InputIt | |
800 | #ifndef BOOST_JSON_DOCS | |
801 | ,class = is_inputit<InputIt> | |
802 | #endif | |
803 | > | |
804 | string& | |
805 | assign( | |
806 | InputIt first, | |
807 | InputIt last); | |
808 | ||
809 | /** Assign characters to a string. | |
810 | ||
811 | Replaces the contents with those of a | |
812 | string view. This view can contain | |
813 | null characters. | |
814 | ||
815 | @par Complexity | |
816 | ||
817 | Linear in `s.size()`. | |
818 | ||
819 | @par Exception Safety | |
820 | ||
821 | Strong guarantee. | |
822 | Calls to `memory_resource::allocate` may throw. | |
823 | ||
824 | @return `*this` | |
825 | ||
826 | @param s The string view to copy from. | |
827 | ||
828 | @throw std::length_error `s.size() > max_size()`. | |
829 | */ | |
830 | string& | |
831 | assign(string_view s) | |
832 | { | |
833 | return assign(s.data(), s.size()); | |
834 | } | |
835 | ||
836 | //------------------------------------------------------ | |
837 | ||
838 | /** Return the associated @ref memory_resource | |
839 | ||
840 | This returns the @ref memory_resource used by | |
841 | the container. | |
842 | ||
843 | @par Complexity | |
844 | ||
845 | Constant. | |
846 | ||
847 | @par Exception Safety | |
848 | ||
849 | No-throw guarantee. | |
850 | */ | |
851 | storage_ptr const& | |
852 | storage() const noexcept | |
853 | { | |
854 | return sp_; | |
855 | } | |
856 | ||
857 | /** Return the associated @ref memory_resource | |
858 | ||
859 | This function returns an instance of | |
860 | @ref polymorphic_allocator constructed from the | |
861 | associated @ref memory_resource. | |
862 | ||
863 | @par Complexity | |
864 | ||
865 | Constant. | |
866 | ||
867 | @par Exception Safety | |
868 | ||
869 | No-throw guarantee. | |
870 | */ | |
871 | allocator_type | |
872 | get_allocator() const noexcept | |
873 | { | |
874 | return sp_.get(); | |
875 | } | |
876 | ||
877 | //------------------------------------------------------ | |
878 | // | |
879 | // Element Access | |
880 | // | |
881 | //------------------------------------------------------ | |
882 | ||
883 | /** Return a character with bounds checking. | |
884 | ||
885 | Returns a reference to the character specified at | |
886 | location `pos`. | |
887 | ||
888 | @par Complexity | |
889 | ||
890 | Constant. | |
891 | ||
892 | @par Exception Safety | |
893 | ||
894 | Strong guarantee. | |
895 | ||
896 | @param pos A zero-based index to access. | |
897 | ||
898 | @throw std::out_of_range `pos >= size()` | |
899 | */ | |
900 | char& | |
901 | at(std::size_t pos) | |
902 | { | |
903 | if(pos >= size()) | |
904 | detail::throw_out_of_range( | |
1e59de90 | 905 | BOOST_JSON_SOURCE_POS); |
20effc67 TL |
906 | return impl_.data()[pos]; |
907 | } | |
908 | ||
909 | /** Return a character with bounds checking. | |
910 | ||
911 | Returns a reference to the character specified at | |
912 | location `pos`. | |
913 | ||
914 | @par Complexity | |
915 | ||
916 | Constant. | |
917 | ||
918 | @par Exception Safety | |
919 | ||
920 | Strong guarantee. | |
921 | ||
922 | @param pos A zero-based index to access. | |
923 | ||
924 | @throw std::out_of_range `pos >= size()` | |
925 | */ | |
926 | char const& | |
927 | at(std::size_t pos) const | |
928 | { | |
929 | if(pos >= size()) | |
930 | detail::throw_out_of_range( | |
1e59de90 | 931 | BOOST_JSON_SOURCE_POS); |
20effc67 TL |
932 | return impl_.data()[pos]; |
933 | } | |
934 | ||
935 | /** Return a character without bounds checking. | |
936 | ||
937 | Returns a reference to the character specified at | |
938 | location `pos`. | |
939 | ||
940 | @par Complexity | |
941 | ||
942 | Constant. | |
943 | ||
944 | @par Precondition | |
1e59de90 | 945 | |
20effc67 TL |
946 | @code |
947 | pos >= size | |
948 | @endcode | |
949 | ||
950 | @param pos A zero-based index to access. | |
951 | */ | |
952 | char& | |
953 | operator[](std::size_t pos) | |
954 | { | |
955 | return impl_.data()[pos]; | |
956 | } | |
957 | ||
958 | /** Return a character without bounds checking. | |
959 | ||
960 | Returns a reference to the character specified at | |
961 | location `pos`. | |
962 | ||
963 | @par Complexity | |
964 | ||
965 | Constant. | |
966 | ||
967 | @par Precondition | |
968 | ||
969 | @code | |
970 | pos >= size | |
971 | @endcode | |
972 | ||
973 | @param pos A zero-based index to access. | |
974 | */ | |
975 | const char& | |
976 | operator[](std::size_t pos) const | |
977 | { | |
978 | return impl_.data()[pos]; | |
979 | } | |
980 | ||
981 | /** Return the first character. | |
982 | ||
983 | Returns a reference to the first character. | |
984 | ||
985 | @par Complexity | |
986 | ||
987 | Constant. | |
988 | ||
989 | @par Precondition | |
990 | ||
991 | @code | |
992 | not empty() | |
993 | @endcode | |
994 | */ | |
995 | char& | |
996 | front() | |
997 | { | |
998 | return impl_.data()[0]; | |
999 | } | |
1000 | ||
1001 | /** Return the first character. | |
1002 | ||
1003 | Returns a reference to the first character. | |
1004 | ||
1005 | @par Complexity | |
1006 | ||
1007 | Constant. | |
1008 | ||
1009 | @par Precondition | |
1010 | ||
1011 | @code | |
1012 | not empty() | |
1013 | @endcode | |
1014 | */ | |
1015 | char const& | |
1016 | front() const | |
1017 | { | |
1018 | return impl_.data()[0]; | |
1019 | } | |
1020 | ||
1021 | /** Return the last character. | |
1022 | ||
1023 | Returns a reference to the last character. | |
1024 | ||
1025 | @par Complexity | |
1026 | ||
1027 | Constant. | |
1028 | ||
1029 | @par Precondition | |
1030 | ||
1031 | @code | |
1032 | not empty() | |
1033 | @endcode | |
1034 | */ | |
1035 | char& | |
1036 | back() | |
1037 | { | |
1038 | return impl_.data()[impl_.size() - 1]; | |
1039 | } | |
1040 | ||
1041 | /** Return the last character. | |
1042 | ||
1043 | Returns a reference to the last character. | |
1044 | ||
1045 | @par Complexity | |
1046 | ||
1047 | Constant. | |
1048 | ||
1049 | @par Precondition | |
1050 | ||
1051 | @code | |
1052 | not empty() | |
1053 | @endcode | |
1054 | */ | |
1055 | char const& | |
1056 | back() const | |
1057 | { | |
1058 | return impl_.data()[impl_.size() - 1]; | |
1059 | } | |
1060 | ||
1061 | /** Return the underlying character array directly. | |
1062 | ||
1063 | Returns a pointer to the underlying array | |
1064 | serving as storage. The value returned is such that | |
1065 | the range `{data(), data()+size())` is always a | |
1066 | valid range, even if the container is empty. | |
1067 | ||
1068 | @par Complexity | |
1069 | ||
1070 | Constant. | |
1071 | ||
1072 | @note The value returned from | |
1073 | this function is never equal to `nullptr`. | |
1074 | */ | |
1075 | char* | |
1076 | data() noexcept | |
1077 | { | |
1078 | return impl_.data(); | |
1079 | } | |
1080 | ||
1081 | /** Return the underlying character array directly. | |
1082 | ||
1083 | Returns a pointer to the underlying array | |
1e59de90 TL |
1084 | serving as storage. |
1085 | ||
20effc67 TL |
1086 | @note The value returned is such that |
1087 | the range `{data(), data() + size())` is always a | |
1e59de90 | 1088 | valid range, even if the container is empty. |
20effc67 TL |
1089 | The value returned from |
1090 | this function is never equal to `nullptr`. | |
1091 | ||
1092 | @par Complexity | |
1093 | ||
1094 | Constant. | |
1095 | */ | |
1096 | char const* | |
1097 | data() const noexcept | |
1098 | { | |
1099 | return impl_.data(); | |
1100 | } | |
1101 | ||
1102 | /** Return the underlying character array directly. | |
1103 | ||
1104 | Returns a pointer to the underlying array | |
1105 | serving as storage. The value returned is such that | |
1106 | the range `{c_str(), c_str() + size()}` is always a | |
1107 | valid range, even if the container is empty. | |
1108 | ||
1109 | @par Complexity | |
1110 | ||
1111 | Constant. | |
1112 | ||
1113 | @note The value returned from | |
1114 | this function is never equal to `nullptr`. | |
1115 | */ | |
1116 | char const* | |
1117 | c_str() const noexcept | |
1118 | { | |
1119 | return impl_.data(); | |
1120 | } | |
1121 | ||
1122 | /** Convert to a `string_view` referring to the string. | |
1123 | ||
1124 | Returns a string view to the | |
1125 | underlying character string. The size of the view | |
1126 | does not include the null terminator. | |
1127 | ||
1128 | @par Complexity | |
1129 | ||
1130 | Constant. | |
1131 | */ | |
1132 | operator string_view() const noexcept | |
1133 | { | |
1134 | return {data(), size()}; | |
1135 | } | |
1136 | ||
1e59de90 TL |
1137 | #if ! defined(BOOST_NO_CXX17_HDR_STRING_VIEW) |
1138 | /** Convert to a `std::string_view` referring to the string. | |
1139 | ||
1140 | Returns a string view to the underlying character string. The size of | |
1141 | the view does not include the null terminator. | |
1142 | ||
1143 | This overload is not defined when `BOOST_NO_CXX17_HDR_STRING_VIEW` | |
1144 | is defined. | |
1145 | ||
1146 | @par Complexity | |
1147 | ||
1148 | Constant. | |
1149 | */ | |
1150 | operator std::string_view() const noexcept | |
1151 | { | |
1152 | return {data(), size()}; | |
1153 | } | |
1154 | #endif | |
1155 | ||
20effc67 TL |
1156 | //------------------------------------------------------ |
1157 | // | |
1158 | // Iterators | |
1159 | // | |
1160 | //------------------------------------------------------ | |
1161 | ||
1162 | /** Return an iterator to the beginning. | |
1163 | ||
1164 | If the container is empty, @ref end() is returned. | |
1165 | ||
1166 | @par Complexity | |
1167 | Constant. | |
1168 | ||
1169 | @par Exception Safety | |
1170 | No-throw guarantee. | |
1171 | */ | |
1172 | iterator | |
1173 | begin() noexcept | |
1174 | { | |
1175 | return impl_.data(); | |
1176 | } | |
1177 | ||
1178 | /** Return an iterator to the beginning. | |
1179 | ||
1180 | If the container is empty, @ref end() is returned. | |
1181 | ||
1182 | @par Complexity | |
1183 | Constant. | |
1184 | ||
1185 | @par Exception Safety | |
1186 | No-throw guarantee. | |
1187 | */ | |
1188 | const_iterator | |
1189 | begin() const noexcept | |
1190 | { | |
1191 | return impl_.data(); | |
1192 | } | |
1193 | ||
1194 | /** Return an iterator to the beginning. | |
1195 | ||
1196 | If the container is empty, @ref cend() is returned. | |
1197 | ||
1198 | @par Complexity | |
1199 | Constant. | |
1200 | ||
1201 | @par Exception Safety | |
1202 | No-throw guarantee. | |
1203 | */ | |
1204 | const_iterator | |
1205 | cbegin() const noexcept | |
1206 | { | |
1207 | return impl_.data(); | |
1208 | } | |
1209 | ||
1210 | /** Return an iterator to the end. | |
1211 | ||
1212 | Returns an iterator to the character | |
1213 | following the last character of the string. | |
1214 | This character acts as a placeholder, attempting | |
1215 | to access it results in undefined behavior. | |
1216 | ||
1217 | @par Complexity | |
1218 | Constant. | |
1219 | ||
1220 | @par Exception Safety | |
1221 | No-throw guarantee. | |
1222 | */ | |
1223 | iterator | |
1224 | end() noexcept | |
1225 | { | |
1226 | return impl_.end(); | |
1227 | } | |
1228 | ||
1229 | /** Return an iterator to the end. | |
1230 | ||
1231 | Returns an iterator to the character following | |
1232 | the last character of the string. | |
1233 | This character acts as a placeholder, attempting | |
1234 | to access it results in undefined behavior. | |
1235 | ||
1236 | @par Complexity | |
1237 | Constant. | |
1238 | ||
1239 | @par Exception Safety | |
1240 | No-throw guarantee. | |
1241 | */ | |
1242 | const_iterator | |
1243 | end() const noexcept | |
1244 | { | |
1245 | return impl_.end(); | |
1246 | } | |
1247 | ||
1248 | /** Return an iterator to the end. | |
1249 | ||
1250 | Returns an iterator to the character following | |
1251 | the last character of the string. | |
1252 | This character acts as a placeholder, attempting | |
1253 | to access it results in undefined behavior. | |
1254 | ||
1255 | @par Complexity | |
1256 | Constant. | |
1257 | ||
1258 | @par Exception Safety | |
1259 | No-throw guarantee. | |
1260 | */ | |
1261 | const_iterator | |
1262 | cend() const noexcept | |
1263 | { | |
1264 | return impl_.end(); | |
1265 | } | |
1266 | ||
1267 | /** Return a reverse iterator to the first character of the reversed container. | |
1268 | ||
1269 | Returns the pointed-to character that | |
1270 | corresponds to the last character of the | |
1271 | non-reversed container. | |
1272 | If the container is empty, @ref rend() is returned. | |
1e59de90 | 1273 | |
20effc67 TL |
1274 | @par Complexity |
1275 | Constant. | |
1276 | ||
1277 | @par Exception Safety | |
1278 | No-throw guarantee. | |
1279 | */ | |
1280 | reverse_iterator | |
1281 | rbegin() noexcept | |
1282 | { | |
1283 | return reverse_iterator(impl_.end()); | |
1284 | } | |
1285 | ||
1286 | /** Return a reverse iterator to the first character of the reversed container. | |
1287 | ||
1288 | Returns the pointed-to character that | |
1289 | corresponds to the last character of the | |
1290 | non-reversed container. | |
1291 | If the container is empty, @ref rend() is returned. | |
1e59de90 | 1292 | |
20effc67 TL |
1293 | @par Complexity |
1294 | Constant. | |
1295 | ||
1296 | @par Exception Safety | |
1297 | No-throw guarantee. | |
1298 | */ | |
1299 | const_reverse_iterator | |
1300 | rbegin() const noexcept | |
1301 | { | |
1302 | return const_reverse_iterator(impl_.end()); | |
1303 | } | |
1304 | ||
1305 | /** Return a reverse iterator to the first character of the reversed container. | |
1306 | ||
1307 | Returns the pointed-to character that | |
1308 | corresponds to the last character of the | |
1309 | non-reversed container. | |
1310 | If the container is empty, @ref crend() is returned. | |
1e59de90 | 1311 | |
20effc67 TL |
1312 | @par Complexity |
1313 | Constant. | |
1314 | ||
1315 | @par Exception Safety | |
1316 | No-throw guarantee. | |
1317 | */ | |
1318 | const_reverse_iterator | |
1319 | crbegin() const noexcept | |
1320 | { | |
1321 | return const_reverse_iterator(impl_.end()); | |
1322 | } | |
1323 | ||
1324 | /** Return a reverse iterator to the character following the last character of the reversed container. | |
1325 | ||
1326 | Returns the pointed-to character that corresponds | |
1327 | to the character preceding the first character of | |
1e59de90 | 1328 | the non-reversed container. |
20effc67 TL |
1329 | This character acts as a placeholder, attempting |
1330 | to access it results in undefined behavior. | |
1e59de90 | 1331 | |
20effc67 TL |
1332 | @par Complexity |
1333 | Constant. | |
1334 | ||
1335 | @par Exception Safety | |
1336 | No-throw guarantee. | |
1337 | */ | |
1338 | reverse_iterator | |
1339 | rend() noexcept | |
1340 | { | |
1341 | return reverse_iterator(begin()); | |
1342 | } | |
1343 | ||
1344 | /** Return a reverse iterator to the character following the last character of the reversed container. | |
1345 | ||
1346 | Returns the pointed-to character that corresponds | |
1347 | to the character preceding the first character of | |
1e59de90 | 1348 | the non-reversed container. |
20effc67 TL |
1349 | This character acts as a placeholder, attempting |
1350 | to access it results in undefined behavior. | |
1351 | ||
1352 | @par Complexity | |
1353 | Constant. | |
1354 | ||
1355 | @par Exception Safety | |
1356 | No-throw guarantee. | |
1357 | */ | |
1358 | const_reverse_iterator | |
1359 | rend() const noexcept | |
1360 | { | |
1361 | return const_reverse_iterator(begin()); | |
1362 | } | |
1363 | ||
1364 | /** Return a reverse iterator to the character following the last character of the reversed container. | |
1365 | ||
1366 | Returns the pointed-to character that corresponds | |
1367 | to the character preceding the first character of | |
1e59de90 | 1368 | the non-reversed container. |
20effc67 TL |
1369 | This character acts as a placeholder, attempting |
1370 | to access it results in undefined behavior. | |
1e59de90 | 1371 | |
20effc67 TL |
1372 | @par Complexity |
1373 | Constant. | |
1374 | ||
1375 | @par Exception Safety | |
1376 | No-throw guarantee. | |
1377 | */ | |
1378 | const_reverse_iterator | |
1379 | crend() const noexcept | |
1380 | { | |
1381 | return const_reverse_iterator(begin()); | |
1382 | } | |
1383 | ||
1384 | //------------------------------------------------------ | |
1385 | // | |
1386 | // Capacity | |
1387 | // | |
1388 | //------------------------------------------------------ | |
1389 | ||
1390 | /** Check if the string has no characters. | |
1391 | ||
1392 | Returns `true` if there are no characters in | |
1393 | the string, i.e. @ref size() returns 0. | |
1394 | ||
1395 | @par Complexity | |
1396 | ||
1397 | Constant. | |
1398 | */ | |
1399 | bool | |
1400 | empty() const noexcept | |
1401 | { | |
1402 | return impl_.size() == 0; | |
1403 | } | |
1404 | ||
1405 | /** Return the number of characters in the string. | |
1406 | ||
1407 | The value returned does not include the | |
1408 | null terminator, which is always present. | |
1409 | ||
1410 | @par Complexity | |
1411 | ||
1412 | Constant. | |
1413 | */ | |
1414 | std::size_t | |
1415 | size() const noexcept | |
1416 | { | |
1417 | return impl_.size(); | |
1418 | } | |
1419 | ||
1420 | /** Return the maximum number of characters any string can hold. | |
1421 | ||
1422 | The maximum is an implementation-defined number. | |
1423 | This value is a theoretical limit; at runtime, | |
1424 | the actual maximum size may be less due to | |
1425 | resource limits. | |
1426 | ||
1427 | @par Complexity | |
1428 | ||
1429 | Constant. | |
1430 | */ | |
1431 | static | |
1432 | constexpr | |
1433 | std::size_t | |
1434 | max_size() noexcept | |
1435 | { | |
1436 | return string_impl::max_size(); | |
1437 | } | |
1438 | ||
1439 | /** Return the number of characters that can be held without a reallocation. | |
1440 | ||
1441 | This number represents the largest number of | |
1442 | characters the currently allocated storage can contain. | |
1443 | This number may be larger than the value returned | |
1444 | by @ref size(). | |
1445 | ||
1446 | @par Complexity | |
1447 | ||
1448 | Constant. | |
1449 | */ | |
1450 | std::size_t | |
1451 | capacity() const noexcept | |
1452 | { | |
1453 | return impl_.capacity(); | |
1454 | } | |
1455 | ||
1456 | /** Increase the capacity to at least a certain amount. | |
1457 | ||
1458 | This increases the capacity of the array to a value | |
1459 | that is greater than or equal to `new_capacity`. If | |
1460 | `new_capacity > capacity()`, new memory is | |
1461 | allocated. Otherwise, the call has no effect. | |
1462 | The number of elements and therefore the | |
1463 | @ref size() of the container is not changed. | |
1464 | ||
1465 | @par Complexity | |
1466 | ||
1467 | At most, linear in @ref size(). | |
1468 | ||
1469 | @par Exception Safety | |
1470 | ||
1471 | Strong guarantee. | |
1472 | Calls to `memory_resource::allocate` may throw. | |
1473 | ||
1474 | @note | |
1475 | ||
1476 | If new memory is allocated, all iterators including | |
1477 | any past-the-end iterators, and all references to | |
1478 | the elements are invalidated. Otherwise, no | |
1479 | iterators or references are invalidated. | |
1480 | ||
1481 | @param new_capacity The new capacity of the array. | |
1482 | ||
1483 | @throw std::length_error `new_capacity > max_size()` | |
1484 | */ | |
1485 | void | |
1486 | reserve(std::size_t new_capacity) | |
1487 | { | |
1488 | if(new_capacity <= capacity()) | |
1489 | return; | |
1490 | reserve_impl(new_capacity); | |
1491 | } | |
1492 | ||
1493 | /** Request the removal of unused capacity. | |
1494 | ||
1495 | This performs a non-binding request to reduce | |
1496 | @ref capacity() to @ref size(). The request may | |
1e59de90 TL |
1497 | or may not be fulfilled. |
1498 | ||
20effc67 TL |
1499 | @par Complexity |
1500 | ||
1501 | At most, linear in @ref size(). | |
1502 | ||
1503 | @note If reallocation occurs, all iterators | |
1504 | including any past-the-end iterators, and all | |
1505 | references to characters are invalidated. | |
1506 | Otherwise, no iterators or references are | |
1507 | invalidated. | |
1508 | */ | |
1509 | BOOST_JSON_DECL | |
1510 | void | |
1511 | shrink_to_fit(); | |
1512 | ||
1513 | //------------------------------------------------------ | |
1514 | // | |
1515 | // Operations | |
1516 | // | |
1517 | //------------------------------------------------------ | |
1518 | ||
1519 | /** Clear the contents. | |
1520 | ||
1521 | Erases all characters from the string. After this | |
1522 | call, @ref size() returns zero but @ref capacity() | |
1e59de90 TL |
1523 | is unchanged. |
1524 | ||
20effc67 TL |
1525 | @par Complexity |
1526 | ||
1527 | Linear in @ref size(). | |
1528 | ||
1529 | @note All references, pointers, or iterators | |
1530 | referring to contained elements are invalidated. | |
1531 | Any past-the-end iterators are also invalidated. | |
1532 | */ | |
1533 | BOOST_JSON_DECL | |
1534 | void | |
1535 | clear() noexcept; | |
1536 | ||
1537 | //------------------------------------------------------ | |
1538 | ||
1539 | /** Insert a string. | |
1540 | ||
1541 | Inserts the `string_view` `sv` at the position `pos`. | |
1542 | ||
1543 | @par Exception Safety | |
1544 | ||
1545 | Strong guarantee. | |
1546 | ||
1547 | @note All references, pointers, or iterators | |
1548 | referring to contained elements are invalidated. | |
1549 | Any past-the-end iterators are also invalidated. | |
1550 | ||
1551 | @return `*this` | |
1552 | ||
1553 | @param pos The index to insert at. | |
1554 | ||
1555 | @param sv The `string_view` to insert. | |
1556 | ||
1557 | @throw std::length_error `size() + s.size() > max_size()` | |
1558 | ||
1559 | @throw std::out_of_range `pos > size()` | |
1560 | */ | |
1561 | BOOST_JSON_DECL | |
1562 | string& | |
1563 | insert( | |
1564 | std::size_t pos, | |
1565 | string_view sv); | |
1566 | ||
1567 | /** Insert a character. | |
1e59de90 | 1568 | |
20effc67 TL |
1569 | Inserts `count` copies of `ch` at the position `pos`. |
1570 | ||
1571 | @par Exception Safety | |
1572 | ||
1573 | Strong guarantee. | |
1574 | ||
1575 | @note All references, pointers, or iterators | |
1576 | referring to contained elements are invalidated. | |
1577 | Any past-the-end iterators are also invalidated. | |
1578 | ||
1579 | @return `*this` | |
1580 | ||
1581 | @param pos The index to insert at. | |
1582 | ||
1583 | @param count The number of characters to insert. | |
1584 | ||
1585 | @param ch The character to insert. | |
1586 | ||
1587 | @throw std::length_error `size() + count > max_size()` | |
1588 | ||
1589 | @throw std::out_of_range `pos > size()` | |
1590 | */ | |
1591 | BOOST_JSON_DECL | |
1592 | string& | |
1593 | insert( | |
1594 | std::size_t pos, | |
1595 | std::size_t count, | |
1596 | char ch); | |
1597 | ||
1598 | /** Insert a character. | |
1599 | ||
1600 | Inserts the character `ch` before the character | |
1601 | at index `pos`. | |
1602 | ||
1603 | @par Exception Safety | |
1604 | ||
1605 | Strong guarantee. | |
1606 | ||
1607 | @note All references, pointers, or iterators | |
1608 | referring to contained elements are invalidated. | |
1609 | Any past-the-end iterators are also invalidated. | |
1610 | ||
1611 | @return `*this` | |
1612 | ||
1613 | @param pos The index to insert at. | |
1614 | ||
1615 | @param ch The character to insert. | |
1616 | ||
1617 | @throw std::length_error `size() + 1 > max_size()` | |
1618 | ||
1619 | @throw std::out_of_range `pos > size()` | |
1620 | */ | |
1621 | string& | |
1622 | insert( | |
1623 | size_type pos, | |
1624 | char ch) | |
1625 | { | |
1626 | return insert(pos, 1, ch); | |
1627 | } | |
1628 | ||
1629 | /** Insert a range of characters. | |
1630 | ||
1631 | Inserts characters from the range `{first, last)` | |
1632 | before the character at index `pos`. | |
1633 | ||
1634 | @par Precondition | |
1635 | ||
1636 | `{first, last)` is a valid range. | |
1637 | ||
1638 | @par Exception Safety | |
1639 | ||
1640 | Strong guarantee. | |
1641 | ||
1642 | @note All references, pointers, or iterators | |
1643 | referring to contained elements are invalidated. | |
1644 | Any past-the-end iterators are also invalidated. | |
1645 | ||
1646 | @tparam InputIt The type of the iterators. | |
1647 | ||
1648 | @par Constraints | |
1649 | ||
1650 | `InputIt` satisfies __InputIterator__. | |
1651 | ||
1652 | @return `*this` | |
1653 | ||
1654 | @param pos The index to insert at. | |
1655 | ||
1656 | @param first The beginning of the character range. | |
1657 | ||
1658 | @param last The end of the character range. | |
1659 | ||
1660 | @throw std::length_error `size() + insert_count > max_size()` | |
1661 | ||
1662 | @throw std::out_of_range `pos > size()` | |
1663 | */ | |
1664 | template<class InputIt | |
1665 | #ifndef BOOST_JSON_DOCS | |
1666 | ,class = is_inputit<InputIt> | |
1667 | #endif | |
1668 | > | |
1669 | string& | |
1670 | insert( | |
1671 | size_type pos, | |
1672 | InputIt first, | |
1673 | InputIt last); | |
1674 | ||
1675 | //------------------------------------------------------ | |
1676 | ||
1677 | /** Erase characters from the string. | |
1e59de90 | 1678 | |
20effc67 TL |
1679 | Erases `num` characters from the string, starting |
1680 | at `pos`. `num` is determined as the smaller of | |
1681 | `count` and `size() - pos`. | |
1682 | ||
1683 | @par Exception Safety | |
1684 | ||
1685 | Strong guarantee. | |
1686 | ||
1687 | @note All references, pointers, or iterators | |
1688 | referring to contained elements are invalidated. | |
1689 | Any past-the-end iterators are also invalidated. | |
1690 | ||
1691 | @return `*this` | |
1692 | ||
1693 | @param pos The index to erase at. | |
1694 | The default argument for this parameter is `0`. | |
1695 | ||
1696 | @param count The number of characters to erase. | |
1697 | The default argument for this parameter | |
1698 | is @ref npos. | |
1e59de90 | 1699 | |
20effc67 TL |
1700 | @throw std::out_of_range `pos > size()` |
1701 | */ | |
1702 | BOOST_JSON_DECL | |
1703 | string& | |
1704 | erase( | |
1705 | std::size_t pos = 0, | |
1706 | std::size_t count = npos); | |
1707 | ||
1708 | /** Erase a character from the string. | |
1e59de90 | 1709 | |
20effc67 TL |
1710 | Erases the character at `pos`. |
1711 | ||
1712 | @par Precondition | |
1713 | ||
1714 | @code | |
1715 | pos >= data() && pos <= data() + size() | |
1716 | @endcode | |
1717 | ||
1718 | @par Exception Safety | |
1719 | ||
1720 | Strong guarantee. | |
1721 | ||
1722 | @note All references, pointers, or iterators | |
1723 | referring to contained elements are invalidated. | |
1724 | Any past-the-end iterators are also invalidated. | |
1725 | ||
1726 | @return An iterator referring to character | |
1727 | immediately following the erased character, or | |
1728 | @ref end() if one does not exist. | |
1729 | ||
1730 | @param pos An iterator referring to the | |
1731 | character to erase. | |
1732 | */ | |
1733 | BOOST_JSON_DECL | |
1734 | iterator | |
1735 | erase(const_iterator pos); | |
1736 | ||
1737 | /** Erase a range from the string. | |
1738 | ||
1739 | Erases the characters in the range `{first, last)`. | |
1740 | ||
1741 | @par Precondition | |
1742 | ||
1743 | `{first, last}` shall be valid within | |
1744 | @code | |
1745 | {data(), data() + size()} | |
1746 | @endcode | |
1747 | ||
1748 | @par Exception Safety | |
1749 | ||
1750 | Strong guarantee. | |
1751 | ||
1752 | @note All references, pointers, or iterators | |
1753 | referring to contained elements are invalidated. | |
1754 | Any past-the-end iterators are also invalidated. | |
1755 | ||
1756 | @return An iterator referring to the character | |
1757 | `last` previously referred to, or @ref end() | |
1758 | if one does not exist. | |
1759 | ||
1760 | @param first An iterator representing the first | |
1761 | character to erase. | |
1762 | ||
1763 | @param last An iterator one past the last | |
1764 | character to erase. | |
1765 | */ | |
1766 | BOOST_JSON_DECL | |
1767 | iterator | |
1768 | erase( | |
1769 | const_iterator first, | |
1770 | const_iterator last); | |
1771 | ||
1772 | //------------------------------------------------------ | |
1773 | ||
1774 | /** Append a character. | |
1e59de90 | 1775 | |
20effc67 TL |
1776 | Appends a character to the end of the string. |
1777 | ||
1778 | @par Exception Safety | |
1779 | ||
1780 | Strong guarantee. | |
1e59de90 | 1781 | |
20effc67 | 1782 | @param ch The character to append. |
1e59de90 | 1783 | |
20effc67 TL |
1784 | @throw std::length_error `size() + 1 > max_size()` |
1785 | */ | |
1786 | BOOST_JSON_DECL | |
1787 | void | |
1788 | push_back(char ch); | |
1789 | ||
1790 | /** Remove the last character. | |
1e59de90 | 1791 | |
20effc67 TL |
1792 | Removes a character from the end of the string. |
1793 | ||
1794 | @par Precondition | |
1795 | ||
1796 | @code | |
1797 | not empty() | |
1798 | @endcode | |
1799 | */ | |
1800 | BOOST_JSON_DECL | |
1801 | void | |
1802 | pop_back(); | |
1803 | ||
1804 | //------------------------------------------------------ | |
1805 | ||
1806 | /** Append characters to the string. | |
1e59de90 | 1807 | |
20effc67 TL |
1808 | Appends `count` copies of `ch` to the end of |
1809 | the string. | |
1810 | ||
1811 | @par Exception Safety | |
1812 | ||
1813 | Strong guarantee. | |
1814 | ||
1815 | @return `*this` | |
1816 | ||
1817 | @param count The number of characters to append. | |
1818 | ||
1819 | @param ch The character to append. | |
1820 | ||
1821 | @throw std::length_error `size() + count > max_size()` | |
1822 | */ | |
1823 | BOOST_JSON_DECL | |
1824 | string& | |
1825 | append( | |
1826 | std::size_t count, | |
1827 | char ch); | |
1828 | ||
1829 | /** Append a string to the string. | |
1830 | ||
1831 | Appends `sv` the end of the string. | |
1832 | ||
1833 | @par Exception Safety | |
1834 | ||
1835 | Strong guarantee. | |
1836 | ||
1837 | @return `*this` | |
1838 | ||
1839 | @param sv The `string_view` to append. | |
1840 | ||
1841 | @throw std::length_error `size() + s.size() > max_size()` | |
1842 | */ | |
1843 | BOOST_JSON_DECL | |
1844 | string& | |
1845 | append(string_view sv); | |
1846 | ||
1847 | /** Append a range of characters. | |
1848 | ||
1849 | Appends characters from the range `{first, last)` | |
1850 | to the end of the string. | |
1851 | ||
1852 | @par Precondition | |
1853 | ||
1854 | `{first, last)` shall be a valid range | |
1855 | ||
1856 | @par Exception Safety | |
1857 | ||
1858 | Strong guarantee. | |
1859 | ||
1860 | @tparam InputIt The type of the iterators. | |
1861 | ||
1862 | @par Constraints | |
1863 | ||
1864 | `InputIt` satisfies __InputIterator__. | |
1865 | ||
1866 | @return `*this` | |
1867 | ||
1868 | @param first An iterator representing the | |
1869 | first character to append. | |
1870 | ||
1871 | @param last An iterator one past the | |
1872 | last character to append. | |
1873 | ||
1874 | @throw std::length_error `size() + insert_count > max_size()` | |
1875 | */ | |
1876 | template<class InputIt | |
1877 | #ifndef BOOST_JSON_DOCS | |
1878 | ,class = is_inputit<InputIt> | |
1879 | #endif | |
1880 | > | |
1881 | string& | |
1882 | append(InputIt first, InputIt last); | |
1883 | ||
1884 | //------------------------------------------------------ | |
1885 | ||
1886 | /** Append characters from a string. | |
1887 | ||
1888 | Appends `{sv.begin(), sv.end())` to the end of | |
1889 | the string. | |
1890 | ||
1891 | @par Exception Safety | |
1892 | ||
1893 | Strong guarantee. | |
1894 | ||
1895 | @return `*this` | |
1896 | ||
1897 | @param sv The `string_view` to append. | |
1898 | ||
1899 | @throw std::length_error `size() + sv.size() > max_size()` | |
1900 | */ | |
1901 | string& | |
1902 | operator+=(string_view sv) | |
1903 | { | |
1904 | return append(sv); | |
1905 | } | |
1906 | ||
1907 | /** Append a character. | |
1908 | ||
1909 | Appends a character to the end of the string. | |
1910 | ||
1911 | @par Exception Safety | |
1912 | ||
1913 | Strong guarantee. | |
1914 | ||
1915 | @param ch The character to append. | |
1916 | ||
1917 | @throw std::length_error `size() + 1 > max_size()` | |
1918 | */ | |
1919 | string& | |
1920 | operator+=(char ch) | |
1921 | { | |
1922 | push_back(ch); | |
1923 | return *this; | |
1924 | } | |
1925 | ||
1926 | //------------------------------------------------------ | |
1927 | ||
1928 | /** Compare a string with the string. | |
1e59de90 | 1929 | |
20effc67 TL |
1930 | Let `comp` be |
1931 | `std::char_traits<char>::compare(data(), sv.data(), std::min(size(), sv.size())`. | |
1932 | If `comp != 0`, then the result is `comp`. Otherwise, | |
1933 | the result is `0` if `size() == sv.size()`, | |
1934 | `-1` if `size() < sv.size()`, and `1` otherwise. | |
1935 | ||
1936 | @par Complexity | |
1937 | ||
1938 | Linear. | |
1939 | ||
1940 | @return The result of lexicographically comparing | |
1941 | the characters of `sv` and the string. | |
1942 | ||
1943 | @param sv The `string_view` to compare. | |
1944 | */ | |
1945 | int | |
1946 | compare(string_view sv) const noexcept | |
1947 | { | |
1948 | return string_view(*this).compare(sv); | |
1949 | } | |
1950 | ||
1951 | //------------------------------------------------------ | |
1952 | ||
1953 | /** Return whether the string begins with a string. | |
1954 | ||
1955 | Returns `true` if the string begins with `s`, | |
1956 | and `false` otherwise. | |
1957 | ||
1958 | @par Complexity | |
1959 | ||
1960 | Linear. | |
1961 | ||
1962 | @param s The `string_view` to check for. | |
1963 | */ | |
1964 | bool | |
1965 | starts_with(string_view s) const noexcept | |
1966 | { | |
1967 | return subview(0, s.size()) == s; | |
1968 | } | |
1969 | ||
1970 | /** Return whether the string begins with a character. | |
1971 | ||
1972 | Returns `true` if the string begins with `ch`, | |
1973 | and `false` otherwise. | |
1974 | ||
1975 | @par Complexity | |
1976 | ||
1977 | Constant. | |
1978 | ||
1979 | @param ch The character to check for. | |
1980 | */ | |
1981 | bool | |
1982 | starts_with(char ch) const noexcept | |
1983 | { | |
1984 | return ! empty() && front() == ch; | |
1985 | } | |
1986 | ||
1987 | /** Return whether the string end with a string. | |
1988 | ||
1989 | Returns `true` if the string end with `s`, | |
1990 | and `false` otherwise. | |
1991 | ||
1992 | @par Complexity | |
1993 | ||
1994 | Linear. | |
1995 | ||
1996 | @param s The string to check for. | |
1997 | */ | |
1998 | bool | |
1999 | ends_with(string_view s) const noexcept | |
2000 | { | |
2001 | return size() >= s.size() && | |
2002 | subview(size() - s.size()) == s; | |
2003 | } | |
2004 | ||
2005 | /** Return whether the string ends with a character. | |
2006 | ||
2007 | Returns `true` if the string ends with `ch`, | |
2008 | and `false` otherwise. | |
2009 | ||
2010 | @par Complexity | |
2011 | ||
2012 | Constant. | |
2013 | ||
2014 | @param ch The character to check for. | |
2015 | */ | |
2016 | bool | |
2017 | ends_with(char ch) const noexcept | |
2018 | { | |
2019 | return ! empty() && back() == ch; | |
2020 | } | |
2021 | ||
2022 | //------------------------------------------------------ | |
2023 | ||
2024 | /** Replace a substring with a string. | |
2025 | ||
2026 | Replaces `rcount` characters starting at index | |
2027 | `pos` with those of `sv`, where `rcount` is | |
2028 | `std::min(count, size() - pos)`. | |
2029 | ||
2030 | @par Exception Safety | |
2031 | ||
2032 | Strong guarantee. | |
2033 | ||
2034 | @note All references, pointers, or iterators | |
1e59de90 | 2035 | referring to contained elements are invalidated. |
20effc67 TL |
2036 | Any past-the-end iterators are also invalidated. |
2037 | ||
2038 | @return `*this` | |
2039 | ||
2040 | @param pos The index to replace at. | |
2041 | ||
2042 | @param count The number of characters to replace. | |
2043 | ||
2044 | @param sv The `string_view` to replace with. | |
2045 | ||
2046 | @throw std::length_error `size() + (sv.size() - rcount) > max_size()` | |
2047 | ||
2048 | @throw std::out_of_range `pos > size()` | |
2049 | */ | |
2050 | BOOST_JSON_DECL | |
2051 | string& | |
2052 | replace( | |
2053 | std::size_t pos, | |
2054 | std::size_t count, | |
2055 | string_view sv); | |
2056 | ||
2057 | /** Replace a range with a string. | |
2058 | ||
2059 | Replaces the characters in the range | |
2060 | `{first, last)` with those of `sv`. | |
2061 | ||
2062 | @par Precondition | |
2063 | ||
2064 | `{first, last)` is a valid range. | |
2065 | ||
2066 | @par Exception Safety | |
2067 | ||
2068 | Strong guarantee. | |
2069 | ||
2070 | @note All references, pointers, or iterators | |
2071 | referring to contained elements are invalidated. | |
2072 | Any past-the-end iterators are also invalidated. | |
2073 | ||
2074 | @return `*this` | |
2075 | ||
2076 | @param first An iterator referring to the first | |
2077 | character to replace. | |
2078 | ||
2079 | @param last An iterator one past the end of | |
2080 | the last character to replace. | |
2081 | ||
2082 | @param sv The `string_view` to replace with. | |
2083 | ||
2084 | @throw std::length_error `size() + (sv.size() - std::distance(first, last)) > max_size()` | |
2085 | */ | |
2086 | string& | |
2087 | replace( | |
2088 | const_iterator first, | |
2089 | const_iterator last, | |
2090 | string_view sv) | |
2091 | { | |
2092 | return replace(first - begin(), last - first, sv); | |
2093 | } | |
2094 | ||
2095 | /** Replace a range with a range. | |
2096 | ||
2097 | Replaces the characters in the range | |
2098 | `{first, last)` with those of `{first2, last2)`. | |
2099 | ||
2100 | @par Precondition | |
2101 | ||
2102 | `{first, last)` is a valid range. | |
2103 | ||
2104 | `{first2, last2)` is a valid range. | |
2105 | ||
2106 | @par Exception Safety | |
2107 | ||
2108 | Strong guarantee. | |
2109 | ||
2110 | @note All references, pointers, or iterators | |
2111 | referring to contained elements are invalidated. | |
2112 | Any past-the-end iterators are also invalidated. | |
2113 | ||
2114 | @tparam InputIt The type of the iterators. | |
2115 | ||
2116 | @par Constraints | |
2117 | ||
2118 | `InputIt` satisfies __InputIterator__. | |
2119 | ||
2120 | @return `*this` | |
2121 | ||
2122 | @param first An iterator referring to the first | |
2123 | character to replace. | |
2124 | ||
2125 | @param last An iterator one past the end of | |
2126 | the last character to replace. | |
2127 | ||
2128 | @param first2 An iterator referring to the first | |
2129 | character to replace with. | |
2130 | ||
2131 | @param last2 An iterator one past the end of | |
2132 | the last character to replace with. | |
2133 | ||
2134 | @throw std::length_error `size() + (inserted - std::distance(first, last)) > max_size()` | |
2135 | */ | |
2136 | template<class InputIt | |
2137 | #ifndef BOOST_JSON_DOCS | |
2138 | ,class = is_inputit<InputIt> | |
2139 | #endif | |
2140 | > | |
2141 | string& | |
2142 | replace( | |
2143 | const_iterator first, | |
2144 | const_iterator last, | |
2145 | InputIt first2, | |
2146 | InputIt last2); | |
2147 | ||
2148 | /** Replace a substring with copies of a character. | |
2149 | ||
2150 | Replaces `rcount` characters starting at index | |
2151 | `pos`with `count2` copies of `ch`, where | |
2152 | `rcount` is `std::min(count, size() - pos)`. | |
2153 | ||
2154 | @par Exception Safety | |
2155 | ||
2156 | Strong guarantee. | |
2157 | ||
2158 | @note All references, pointers, or iterators | |
2159 | referring to contained elements are invalidated. | |
2160 | Any past-the-end iterators are also invalidated. | |
2161 | ||
2162 | @return `*this` | |
2163 | ||
2164 | @param pos The index to replace at. | |
2165 | ||
2166 | @param count The number of characters to replace. | |
2167 | ||
2168 | @param count2 The number of characters to | |
2169 | replace with. | |
2170 | ||
2171 | @param ch The character to replace with. | |
2172 | ||
2173 | @throw std::length_error `size() + (count2 - rcount) > max_size()` | |
2174 | ||
2175 | @throw std::out_of_range `pos > size()` | |
2176 | */ | |
2177 | BOOST_JSON_DECL | |
2178 | string& | |
2179 | replace( | |
2180 | std::size_t pos, | |
2181 | std::size_t count, | |
2182 | std::size_t count2, | |
2183 | char ch); | |
2184 | ||
2185 | /** Replace a range with copies of a character. | |
2186 | ||
2187 | Replaces the characters in the range | |
2188 | `{first, last)` with `count` copies of `ch`. | |
2189 | ||
2190 | @par Precondition | |
2191 | ||
2192 | `{first, last)` is a valid range. | |
2193 | ||
2194 | @par Exception Safety | |
2195 | ||
2196 | Strong guarantee. | |
2197 | ||
2198 | @note All references, pointers, or iterators | |
2199 | referring to contained elements are invalidated. | |
2200 | Any past-the-end iterators are also invalidated. | |
2201 | ||
2202 | @return `*this` | |
2203 | ||
2204 | @param first An iterator referring to the first | |
2205 | character to replace. | |
2206 | ||
2207 | @param last An iterator one past the end of | |
2208 | the last character to replace. | |
2209 | ||
2210 | @param count The number of characters to | |
2211 | replace with. | |
2212 | ||
2213 | @param ch The character to replace with. | |
2214 | ||
2215 | @throw std::length_error `size() + (count - std::distance(first, last)) > max_size()` | |
2216 | */ | |
2217 | string& | |
2218 | replace( | |
2219 | const_iterator first, | |
2220 | const_iterator last, | |
2221 | std::size_t count, | |
2222 | char ch) | |
2223 | { | |
2224 | return replace(first - begin(), last - first, count, ch); | |
2225 | } | |
2226 | ||
2227 | //------------------------------------------------------ | |
2228 | ||
2229 | /** Return a substring. | |
2230 | ||
2231 | Returns a view of a substring. | |
2232 | ||
2233 | @par Exception Safety | |
2234 | ||
2235 | Strong guarantee. | |
2236 | ||
1e59de90 | 2237 | @return A `string_view` object referring |
20effc67 TL |
2238 | to `{data() + pos, std::min(count, size() - pos))`. |
2239 | ||
2240 | @param pos The index to being the substring at. | |
2241 | The default argument for this parameter is `0`. | |
2242 | ||
2243 | @param count The length of the substring. | |
2244 | The default argument for this parameter | |
2245 | is @ref npos. | |
2246 | ||
2247 | @throw std::out_of_range `pos > size()` | |
2248 | */ | |
2249 | string_view | |
2250 | subview( | |
2251 | std::size_t pos = 0, | |
2252 | std::size_t count = npos) const | |
2253 | { | |
2254 | return string_view(*this).substr(pos, count); | |
2255 | } | |
2256 | ||
2257 | //------------------------------------------------------ | |
2258 | ||
2259 | /** Copy a substring to another string. | |
2260 | ||
2261 | Copies `std::min(count, size() - pos)` characters | |
2262 | starting at index `pos` to the string pointed | |
2263 | to by `dest`. | |
2264 | ||
2265 | @note The resulting string is not null terminated. | |
2266 | ||
2267 | @return The number of characters copied. | |
2268 | ||
2269 | @param count The number of characters to copy. | |
2270 | ||
2271 | @param dest The string to copy to. | |
2272 | ||
1e59de90 | 2273 | @param pos The index to begin copying from. The |
20effc67 TL |
2274 | default argument for this parameter is `0`. |
2275 | ||
2276 | @throw std::out_of_range `pos > max_size()` | |
2277 | */ | |
2278 | std::size_t | |
2279 | copy( | |
2280 | char* dest, | |
2281 | std::size_t count, | |
2282 | std::size_t pos = 0) const | |
2283 | { | |
2284 | return string_view(*this).copy(dest, count, pos); | |
2285 | } | |
2286 | ||
2287 | //------------------------------------------------------ | |
2288 | ||
2289 | /** Change the size of the string. | |
1e59de90 | 2290 | |
20effc67 TL |
2291 | Resizes the string to contain `count` characters. |
2292 | If `count > size()`, characters with the value `0` | |
2293 | are appended. Otherwise, `size()` is reduced | |
2294 | to `count`. | |
2295 | ||
2296 | @param count The size to resize the string to. | |
2297 | ||
2298 | @throw std::out_of_range `count > max_size()` | |
2299 | */ | |
2300 | void | |
2301 | resize(std::size_t count) | |
2302 | { | |
2303 | resize(count, 0); | |
2304 | } | |
2305 | ||
2306 | /** Change the size of the string. | |
2307 | ||
2308 | Resizes the string to contain `count` characters. | |
2309 | If `count > size()`, copies of `ch` are | |
2310 | appended. Otherwise, `size()` is reduced | |
2311 | to `count`. | |
2312 | ||
2313 | @param count The size to resize the string to. | |
2314 | ||
2315 | @param ch The characters to append if the size | |
2316 | increases. | |
2317 | ||
2318 | @throw std::out_of_range `count > max_size()` | |
2319 | */ | |
2320 | BOOST_JSON_DECL | |
2321 | void | |
2322 | resize(std::size_t count, char ch); | |
2323 | ||
2324 | /** Increase size without changing capacity. | |
2325 | ||
2326 | This increases the size of the string by `n` | |
2327 | characters, adjusting the position of the | |
2328 | terminating null for the new size. The new | |
2329 | characters remain uninitialized. This function | |
2330 | may be used to append characters directly into | |
2331 | the storage between `end()` and | |
2332 | `data() + capacity()`. | |
2333 | ||
2334 | @par Precondition | |
2335 | ||
2336 | @code | |
2337 | count <= capacity() - size() | |
2338 | @endcode | |
2339 | ||
2340 | @param n The amount to increase the size by. | |
2341 | */ | |
2342 | void | |
2343 | grow(std::size_t n) noexcept | |
2344 | { | |
2345 | BOOST_ASSERT( | |
2346 | n <= impl_.capacity() - impl_.size()); | |
2347 | impl_.term(impl_.size() + n); | |
2348 | } | |
2349 | ||
2350 | //------------------------------------------------------ | |
2351 | ||
2352 | /** Swap the contents. | |
2353 | ||
2354 | Exchanges the contents of this string with another | |
2355 | string. Ownership of the respective @ref memory_resource | |
2356 | objects is not transferred. | |
2357 | ||
2358 | @li If `*other.storage() == *this->storage()`, | |
2359 | ownership of the underlying memory is swapped in | |
2360 | constant time, with no possibility of exceptions. | |
2361 | All iterators and references remain valid. | |
2362 | ||
2363 | @li If `*other.storage() != *this->storage()`, | |
2364 | the contents are logically swapped by making copies, | |
2365 | which can throw. In this case all iterators and | |
2366 | references are invalidated. | |
2367 | ||
2368 | @par Complexity | |
2369 | ||
2370 | Constant or linear in @ref size() plus | |
2371 | `other.size()`. | |
2372 | ||
2373 | @par Precondition | |
2374 | ||
2375 | @code | |
2376 | &other != this | |
2377 | @endcode | |
2378 | ||
2379 | @par Exception Safety | |
2380 | ||
2381 | Strong guarantee. | |
2382 | Calls to `memory_resource::allocate` may throw. | |
2383 | ||
2384 | @param other The string to swap with | |
2385 | If `this == &other`, this function call has no effect. | |
2386 | */ | |
2387 | BOOST_JSON_DECL | |
2388 | void | |
2389 | swap(string& other); | |
2390 | ||
2391 | /** Exchange the given values. | |
2392 | ||
2393 | Exchanges the contents of the string `lhs` with | |
2394 | another string `rhs`. Ownership of the respective | |
2395 | @ref memory_resource objects is not transferred. | |
2396 | ||
2397 | @li If `*lhs.storage() == *rhs.storage()`, | |
2398 | ownership of the underlying memory is swapped in | |
2399 | constant time, with no possibility of exceptions. | |
2400 | All iterators and references remain valid. | |
2401 | ||
2402 | @li If `*lhs.storage() != *rhs.storage()`, | |
2403 | the contents are logically swapped by making a copy, | |
2404 | which can throw. In this case all iterators and | |
2405 | references are invalidated. | |
1e59de90 | 2406 | |
20effc67 TL |
2407 | @par Effects |
2408 | @code | |
2409 | lhs.swap( rhs ); | |
2410 | @endcode | |
2411 | ||
2412 | @par Complexity | |
2413 | Constant or linear in `lhs.size() + rhs.size()`. | |
2414 | ||
2415 | @par Exception Safety | |
2416 | Strong guarantee. | |
2417 | Calls to `memory_resource::allocate` may throw. | |
2418 | ||
2419 | @param lhs The string to exchange. | |
2420 | ||
2421 | @param rhs The string to exchange. | |
2422 | If `&lhs == &rhs`, this function call has no effect. | |
2423 | ||
2424 | @see @ref string::swap | |
2425 | */ | |
2426 | friend | |
2427 | void | |
2428 | swap(string& lhs, string& rhs) | |
2429 | { | |
2430 | lhs.swap(rhs); | |
2431 | } | |
2432 | //------------------------------------------------------ | |
2433 | // | |
2434 | // Search | |
2435 | // | |
2436 | //------------------------------------------------------ | |
2437 | ||
2438 | /** Find the first occurrence of a string within the string. | |
1e59de90 | 2439 | |
20effc67 TL |
2440 | Returns the lowest index `idx` greater than or equal |
2441 | to `pos` where each element of `sv` is equal to | |
2442 | that of `{begin() + idx, begin() + idx + sv.size())` | |
2443 | if one exists, and @ref npos otherwise. | |
2444 | ||
2445 | @par Complexity | |
2446 | ||
2447 | Linear. | |
2448 | ||
2449 | @return The first occurrence of `sv` within the | |
2450 | string starting at the index `pos`, or @ref npos | |
2451 | if none exists. | |
2452 | ||
2453 | @param sv The `string_view` to search for. | |
2454 | ||
2455 | @param pos The index to start searching at. | |
2456 | The default argument for this parameter is `0`. | |
2457 | */ | |
2458 | std::size_t | |
2459 | find( | |
2460 | string_view sv, | |
2461 | std::size_t pos = 0) const noexcept | |
2462 | { | |
2463 | return string_view(*this).find(sv, pos); | |
2464 | } | |
2465 | ||
2466 | /** Find the first occurrence of a character within the string. | |
1e59de90 | 2467 | |
20effc67 TL |
2468 | Returns the index corrosponding to the first |
2469 | occurrence of `ch` within `{begin() + pos, end())` | |
2470 | if it exists, and @ref npos otherwise. | |
2471 | ||
2472 | @par Complexity | |
2473 | ||
2474 | Linear. | |
2475 | ||
1e59de90 | 2476 | @return The first occurrence of `ch` within the |
20effc67 TL |
2477 | string starting at the index `pos`, or @ref npos |
2478 | if none exists. | |
2479 | ||
2480 | @param ch The character to search for. | |
2481 | ||
2482 | @param pos The index to start searching at. | |
2483 | The default argument for this parameter is `0`. | |
2484 | */ | |
2485 | std::size_t | |
2486 | find( | |
2487 | char ch, | |
2488 | std::size_t pos = 0) const noexcept | |
2489 | { | |
2490 | return string_view(*this).find(ch, pos); | |
2491 | } | |
2492 | ||
2493 | //------------------------------------------------------ | |
2494 | ||
2495 | /** Find the last occurrence of a string within the string. | |
2496 | ||
2497 | Returns the highest index `idx` less than or equal | |
2498 | to `pos` where each element of `sv` is equal to that | |
2499 | of `{begin() + idx, begin() + idx + sv.size())` | |
2500 | if one exists, and @ref npos otherwise. | |
2501 | ||
2502 | @par Complexity | |
2503 | ||
2504 | Linear. | |
2505 | ||
2506 | @return The last occurrence of `sv` within the | |
2507 | string starting before or at the index `pos`, | |
2508 | or @ref npos if none exists. | |
2509 | ||
2510 | @param sv The `string_view` to search for. | |
2511 | ||
2512 | @param pos The index to start searching at. | |
2513 | The default argument for this parameter | |
2514 | is @ref npos. | |
2515 | */ | |
2516 | std::size_t | |
2517 | rfind( | |
2518 | string_view sv, | |
2519 | std::size_t pos = npos) const noexcept | |
2520 | { | |
2521 | return string_view(*this).rfind(sv, pos); | |
2522 | } | |
2523 | ||
2524 | /** Find the last occurrence of a character within the string. | |
2525 | ||
2526 | Returns index corrosponding to the last occurrence | |
2527 | of `ch` within `{begin(), begin() + pos}` if it | |
2528 | exists, and @ref npos otherwise. | |
2529 | ||
2530 | @par Complexity | |
2531 | ||
2532 | Linear. | |
2533 | ||
2534 | @return The last occurrence of `ch` within the | |
2535 | string starting before or at the index `pos`, | |
2536 | or @ref npos if none exists. | |
2537 | ||
2538 | @param ch The character to search for. | |
2539 | ||
2540 | @param pos The index to stop searching at. | |
2541 | The default argument for this parameter | |
2542 | is @ref npos. | |
2543 | */ | |
2544 | std::size_t | |
2545 | rfind( | |
2546 | char ch, | |
2547 | std::size_t pos = npos) const noexcept | |
2548 | { | |
2549 | return string_view(*this).rfind(ch, pos); | |
2550 | } | |
2551 | ||
2552 | //------------------------------------------------------ | |
2553 | ||
2554 | /** Find the first occurrence of any of the characters within the string. | |
2555 | ||
2556 | Returns the index corrosponding to the first | |
2557 | occurrence of any of the characters of `sv` | |
2558 | within `{begin() + pos, end())` if it exists, | |
2559 | and @ref npos otherwise. | |
2560 | ||
2561 | @par Complexity | |
2562 | ||
2563 | Linear. | |
2564 | ||
2565 | @return The first occurrence of any of the | |
2566 | characters within `sv` within the string | |
2567 | starting at the index `pos`, or @ref npos | |
2568 | if none exists. | |
2569 | ||
2570 | @param sv The characters to search for. | |
2571 | ||
2572 | @param pos The index to start searching at. | |
2573 | The default argument for this parameter is `0`. | |
2574 | */ | |
2575 | std::size_t | |
2576 | find_first_of( | |
2577 | string_view sv, | |
2578 | std::size_t pos = 0) const noexcept | |
2579 | { | |
2580 | return string_view(*this).find_first_of(sv, pos); | |
2581 | } | |
2582 | ||
2583 | //------------------------------------------------------ | |
2584 | ||
2585 | /** Find the first occurrence of any of the characters not within the string. | |
2586 | ||
2587 | Returns the index corrosponding to the first | |
2588 | character of `{begin() + pos, end())` that is | |
2589 | not within `sv` if it exists, and @ref npos | |
2590 | otherwise. | |
2591 | ||
2592 | @par Complexity | |
2593 | ||
2594 | Linear. | |
2595 | ||
2596 | @return The first occurrence of a character that | |
2597 | is not within `sv` within the string starting at | |
2598 | the index `pos`, or @ref npos if none exists. | |
2599 | ||
2600 | @param sv The characters to ignore. | |
2601 | ||
2602 | @param pos The index to start searching at. | |
2603 | The default argument for this parameter is `0`. | |
2604 | */ | |
2605 | std::size_t | |
2606 | find_first_not_of( | |
2607 | string_view sv, | |
2608 | std::size_t pos = 0) const noexcept | |
2609 | { | |
2610 | return string_view(*this).find_first_not_of(sv, pos); | |
2611 | } | |
2612 | ||
2613 | /** Find the first occurrence of a character not equal to `ch`. | |
2614 | ||
2615 | Returns the index corrosponding to the first | |
2616 | character of `{begin() + pos, end())` that is | |
2617 | not equal to `ch` if it exists, and | |
2618 | @ref npos otherwise. | |
2619 | ||
2620 | @par Complexity | |
2621 | ||
2622 | Linear. | |
2623 | ||
2624 | @return The first occurrence of a character that | |
2625 | is not equal to `ch`, or @ref npos if none exists. | |
2626 | ||
2627 | @param ch The character to ignore. | |
2628 | ||
2629 | @param pos The index to start searching at. | |
2630 | The default argument for this parameter is `0`. | |
2631 | */ | |
2632 | std::size_t | |
2633 | find_first_not_of( | |
2634 | char ch, | |
2635 | std::size_t pos = 0) const noexcept | |
2636 | { | |
2637 | return string_view(*this).find_first_not_of(ch, pos); | |
2638 | } | |
2639 | ||
2640 | //------------------------------------------------------ | |
2641 | ||
2642 | /** Find the last occurrence of any of the characters within the string. | |
2643 | ||
2644 | Returns the index corrosponding to the last | |
2645 | occurrence of any of the characters of `sv` within | |
2646 | `{begin(), begin() + pos}` if it exists, | |
2647 | and @ref npos otherwise. | |
2648 | ||
2649 | @par Complexity | |
2650 | ||
2651 | Linear. | |
2652 | ||
1e59de90 | 2653 | @return The last occurrence of any of the |
20effc67 TL |
2654 | characters within `sv` within the string starting |
2655 | before or at the index `pos`, or @ref npos if | |
2656 | none exists. | |
2657 | ||
2658 | @param sv The characters to search for. | |
2659 | ||
2660 | @param pos The index to stop searching at. | |
2661 | The default argument for this parameter | |
2662 | is @ref npos. | |
2663 | */ | |
2664 | std::size_t | |
2665 | find_last_of( | |
2666 | string_view sv, | |
2667 | std::size_t pos = npos) const noexcept | |
2668 | { | |
2669 | return string_view(*this).find_last_of(sv, pos); | |
2670 | } | |
2671 | ||
2672 | //------------------------------------------------------ | |
2673 | ||
2674 | /** Find the last occurrence of a character not within the string. | |
2675 | ||
2676 | Returns the index corrosponding to the last | |
2677 | character of `{begin(), begin() + pos}` that is not | |
2678 | within `sv` if it exists, and @ref npos otherwise. | |
2679 | ||
2680 | @par Complexity | |
2681 | ||
2682 | Linear. | |
2683 | ||
2684 | @return The last occurrence of a character that is | |
2685 | not within `sv` within the string before or at the | |
2686 | index `pos`, or @ref npos if none exists. | |
2687 | ||
2688 | @param sv The characters to ignore. | |
2689 | ||
2690 | @param pos The index to stop searching at. | |
2691 | The default argument for this parameter | |
2692 | is @ref npos. | |
2693 | */ | |
2694 | std::size_t | |
2695 | find_last_not_of( | |
2696 | string_view sv, | |
2697 | std::size_t pos = npos) const noexcept | |
2698 | { | |
2699 | return string_view(*this).find_last_not_of(sv, pos); | |
2700 | } | |
2701 | ||
2702 | /** Find the last occurrence of a character not equal to `ch`. | |
2703 | ||
2704 | Returns the index corrosponding to the last | |
2705 | character of `{begin(), begin() + pos}` that is | |
2706 | not equal to `ch` if it exists, and @ref npos | |
2707 | otherwise. | |
2708 | ||
2709 | @par Complexity | |
2710 | ||
2711 | Linear. | |
2712 | ||
1e59de90 | 2713 | @return The last occurrence of a character that |
20effc67 TL |
2714 | is not equal to `ch` before or at the index `pos`, |
2715 | or @ref npos if none exists. | |
2716 | ||
2717 | @param ch The character to ignore. | |
2718 | ||
2719 | @param pos The index to start searching at. | |
2720 | The default argument for this parameter | |
2721 | is @ref npos. | |
2722 | */ | |
2723 | std::size_t | |
2724 | find_last_not_of( | |
2725 | char ch, | |
2726 | std::size_t pos = npos) const noexcept | |
2727 | { | |
2728 | return string_view(*this).find_last_not_of(ch, pos); | |
2729 | } | |
2730 | ||
2731 | private: | |
2732 | class undo; | |
2733 | ||
2734 | template<class It> | |
2735 | using iter_cat = typename | |
2736 | std::iterator_traits<It>::iterator_category; | |
2737 | ||
2738 | template<class InputIt> | |
2739 | void | |
2740 | assign(InputIt first, InputIt last, | |
2741 | std::random_access_iterator_tag); | |
2742 | ||
2743 | template<class InputIt> | |
2744 | void | |
2745 | assign(InputIt first, InputIt last, | |
2746 | std::input_iterator_tag); | |
2747 | ||
2748 | template<class InputIt> | |
2749 | void | |
2750 | append(InputIt first, InputIt last, | |
2751 | std::random_access_iterator_tag); | |
2752 | ||
2753 | template<class InputIt> | |
2754 | void | |
2755 | append(InputIt first, InputIt last, | |
2756 | std::input_iterator_tag); | |
2757 | ||
2758 | BOOST_JSON_DECL | |
2759 | void | |
2760 | reserve_impl(std::size_t new_capacity); | |
2761 | }; | |
2762 | ||
2763 | //---------------------------------------------------------- | |
2764 | ||
2765 | /** Return true if lhs equals rhs. | |
2766 | ||
2767 | A lexicographical comparison is used. | |
2768 | */ | |
2769 | #ifdef BOOST_JSON_DOCS | |
2770 | bool | |
2771 | operator==(string const& lhs, string const& rhs) noexcept | |
2772 | #else | |
2773 | template<class T, class U> | |
2774 | typename std::enable_if< | |
2775 | (std::is_same<T, string>::value && | |
2776 | std::is_convertible< | |
2777 | U const&, string_view>::value) || | |
2778 | (std::is_same<U, string>::value && | |
2779 | std::is_convertible< | |
2780 | T const&, string_view>::value), | |
2781 | bool>::type | |
2782 | operator==(T const& lhs, U const& rhs) noexcept | |
2783 | #endif | |
2784 | { | |
2785 | return string_view(lhs) == string_view(rhs); | |
2786 | } | |
2787 | ||
2788 | /** Return true if lhs does not equal rhs. | |
2789 | ||
2790 | A lexicographical comparison is used. | |
2791 | */ | |
2792 | #ifdef BOOST_JSON_DOCS | |
2793 | bool | |
2794 | operator!=(string const& lhs, string const& rhs) noexcept | |
2795 | #else | |
2796 | template<class T, class U> | |
2797 | typename std::enable_if< | |
2798 | (std::is_same<T, string>::value && | |
2799 | std::is_convertible< | |
2800 | U const&, string_view>::value) || | |
2801 | (std::is_same<U, string>::value && | |
2802 | std::is_convertible< | |
2803 | T const&, string_view>::value), | |
2804 | bool>::type | |
2805 | operator!=(T const& lhs, U const& rhs) noexcept | |
2806 | #endif | |
2807 | { | |
2808 | return string_view(lhs) != string_view(rhs); | |
2809 | } | |
2810 | ||
2811 | /** Return true if lhs is less than rhs. | |
2812 | ||
2813 | A lexicographical comparison is used. | |
2814 | */ | |
2815 | #ifdef BOOST_JSON_DOCS | |
2816 | bool | |
2817 | operator<(string const& lhs, string const& rhs) noexcept | |
2818 | #else | |
2819 | template<class T, class U> | |
2820 | typename std::enable_if< | |
2821 | (std::is_same<T, string>::value && | |
2822 | std::is_convertible< | |
2823 | U const&, string_view>::value) || | |
2824 | (std::is_same<U, string>::value && | |
2825 | std::is_convertible< | |
2826 | T const&, string_view>::value), | |
2827 | bool>::type | |
2828 | operator<(T const& lhs, U const& rhs) noexcept | |
2829 | #endif | |
2830 | { | |
2831 | return string_view(lhs) < string_view(rhs); | |
2832 | } | |
2833 | ||
2834 | /** Return true if lhs is less than or equal to rhs. | |
2835 | ||
2836 | A lexicographical comparison is used. | |
2837 | */ | |
2838 | #ifdef BOOST_JSON_DOCS | |
2839 | bool | |
2840 | operator<=(string const& lhs, string const& rhs) noexcept | |
2841 | #else | |
2842 | template<class T, class U> | |
2843 | typename std::enable_if< | |
2844 | (std::is_same<T, string>::value && | |
2845 | std::is_convertible< | |
2846 | U const&, string_view>::value) || | |
2847 | (std::is_same<U, string>::value && | |
2848 | std::is_convertible< | |
2849 | T const&, string_view>::value), | |
2850 | bool>::type | |
2851 | operator<=(T const& lhs, U const& rhs) noexcept | |
2852 | #endif | |
2853 | { | |
2854 | return string_view(lhs) <= string_view(rhs); | |
2855 | } | |
2856 | ||
2857 | #ifdef BOOST_JSON_DOCS | |
2858 | bool | |
2859 | operator>=(string const& lhs, string const& rhs) noexcept | |
2860 | #else | |
2861 | template<class T, class U> | |
2862 | typename std::enable_if< | |
2863 | (std::is_same<T, string>::value && | |
2864 | std::is_convertible< | |
2865 | U const&, string_view>::value) || | |
2866 | (std::is_same<U, string>::value && | |
2867 | std::is_convertible< | |
2868 | T const&, string_view>::value), | |
2869 | bool>::type | |
2870 | operator>=(T const& lhs, U const& rhs) noexcept | |
2871 | #endif | |
2872 | { | |
2873 | return string_view(lhs) >= string_view(rhs); | |
2874 | } | |
2875 | ||
2876 | /** Return true if lhs is greater than rhs. | |
2877 | ||
2878 | A lexicographical comparison is used. | |
2879 | */ | |
2880 | #ifdef BOOST_JSON_DOCS | |
2881 | bool | |
2882 | operator>(string const& lhs, string const& rhs) noexcept | |
2883 | #else | |
2884 | template<class T, class U> | |
2885 | typename std::enable_if< | |
2886 | (std::is_same<T, string>::value && | |
2887 | std::is_convertible< | |
2888 | U const&, string_view>::value) || | |
2889 | (std::is_same<U, string>::value && | |
2890 | std::is_convertible< | |
2891 | T const&, string_view>::value), | |
2892 | bool>::type | |
2893 | operator>(T const& lhs, U const& rhs) noexcept | |
2894 | #endif | |
2895 | { | |
2896 | return string_view(lhs) > string_view(rhs); | |
2897 | } | |
2898 | ||
2899 | BOOST_JSON_NS_END | |
2900 | ||
2901 | // std::hash specialization | |
2902 | #ifndef BOOST_JSON_DOCS | |
2903 | namespace std { | |
2904 | template<> | |
2905 | struct hash< ::boost::json::string > | |
2906 | { | |
2907 | hash() = default; | |
2908 | hash(hash const&) = default; | |
2909 | hash& operator=(hash const&) = default; | |
2910 | ||
2911 | explicit | |
2912 | hash(std::size_t salt) noexcept | |
2913 | : salt_(salt) | |
2914 | { | |
2915 | } | |
2916 | ||
2917 | std::size_t | |
2918 | operator()(::boost::json::string const& js) const noexcept | |
2919 | { | |
2920 | return ::boost::json::detail::digest( | |
1e59de90 | 2921 | js.begin(), js.end(), salt_); |
20effc67 TL |
2922 | } |
2923 | ||
2924 | private: | |
2925 | std::size_t salt_ = 0; | |
2926 | }; | |
2927 | } // std | |
2928 | #endif | |
2929 | ||
2930 | #include <boost/json/impl/string.hpp> | |
2931 | ||
2932 | #endif |