]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/ext/vstring.h
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / vstring.h
1 // Versatile string -*- C++ -*-
2
3 // Copyright (C) 2005-2018 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /** @file ext/vstring.h
26 * This file is a GNU extension to the Standard C++ Library.
27 */
28
29 #ifndef _VSTRING_H
30 #define _VSTRING_H 1
31
32 #pragma GCC system_header
33
34 #if __cplusplus >= 201103L
35 #include <initializer_list>
36 #endif
37
38 #include <ext/vstring_util.h>
39 #include <ext/rc_string_base.h>
40 #include <ext/sso_string_base.h>
41
42 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
43 {
44 _GLIBCXX_BEGIN_NAMESPACE_VERSION
45
46 /**
47 * @class __versa_string vstring.h
48 * @brief Template class __versa_string.
49 * @ingroup extensions
50 *
51 * Data structure managing sequences of characters and
52 * character-like objects.
53 */
54 template<typename _CharT, typename _Traits, typename _Alloc,
55 template <typename, typename, typename> class _Base>
56 class __versa_string
57 : private _Base<_CharT, _Traits, _Alloc>
58 {
59 typedef _Base<_CharT, _Traits, _Alloc> __vstring_base;
60 typedef typename __vstring_base::_CharT_alloc_type _CharT_alloc_type;
61
62 // Types:
63 public:
64 typedef _Traits traits_type;
65 typedef typename _Traits::char_type value_type;
66 typedef _Alloc allocator_type;
67 typedef typename _CharT_alloc_type::size_type size_type;
68 typedef typename _CharT_alloc_type::difference_type difference_type;
69 typedef value_type& reference;
70 typedef const value_type& const_reference;
71 typedef typename _CharT_alloc_type::pointer pointer;
72 typedef typename _CharT_alloc_type::const_pointer const_pointer;
73 typedef __gnu_cxx::__normal_iterator<pointer, __versa_string> iterator;
74 typedef __gnu_cxx::__normal_iterator<const_pointer, __versa_string>
75 const_iterator;
76 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
77 typedef std::reverse_iterator<iterator> reverse_iterator;
78
79 // Data Member (public):
80 /// Value returned by various member functions when they fail.
81 static const size_type npos = static_cast<size_type>(-1);
82
83 private:
84 size_type
85 _M_check(size_type __pos, const char* __s) const
86 {
87 if (__pos > this->size())
88 std::__throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
89 "this->size() (which is %zu)"),
90 __s, __pos, this->size());
91 return __pos;
92 }
93
94 void
95 _M_check_length(size_type __n1, size_type __n2, const char* __s) const
96 {
97 if (this->max_size() - (this->size() - __n1) < __n2)
98 std::__throw_length_error(__N(__s));
99 }
100
101 // NB: _M_limit doesn't check for a bad __pos value.
102 size_type
103 _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
104 {
105 const bool __testoff = __off < this->size() - __pos;
106 return __testoff ? __off : this->size() - __pos;
107 }
108
109 // True if _Rep and source do not overlap.
110 bool
111 _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
112 {
113 return (std::less<const _CharT*>()(__s, this->_M_data())
114 || std::less<const _CharT*>()(this->_M_data()
115 + this->size(), __s));
116 }
117
118 // For the internal use we have functions similar to `begin'/`end'
119 // but they do not call _M_leak.
120 iterator
121 _M_ibegin() const _GLIBCXX_NOEXCEPT
122 { return iterator(this->_M_data()); }
123
124 iterator
125 _M_iend() const _GLIBCXX_NOEXCEPT
126 { return iterator(this->_M_data() + this->_M_length()); }
127
128 public:
129 // Construct/copy/destroy:
130 // NB: We overload ctors in some cases instead of using default
131 // arguments, per 17.4.4.4 para. 2 item 2.
132
133 /**
134 * @brief Construct an empty string using allocator @a a.
135 */
136 explicit
137 __versa_string(const _Alloc& __a = _Alloc()) _GLIBCXX_NOEXCEPT
138 : __vstring_base(__a) { }
139
140 // NB: per LWG issue 42, semantics different from IS:
141 /**
142 * @brief Construct string with copy of value of @a __str.
143 * @param __str Source string.
144 */
145 __versa_string(const __versa_string& __str)
146 : __vstring_base(__str) { }
147
148 #if __cplusplus >= 201103L
149 /**
150 * @brief String move constructor.
151 * @param __str Source string.
152 *
153 * The newly-constructed %string contains the exact contents of
154 * @a __str. The contents of @a __str are a valid, but unspecified
155 * string.
156 */
157 __versa_string(__versa_string&& __str) noexcept
158 : __vstring_base(std::move(__str)) { }
159
160 /**
161 * @brief Construct string from an initializer list.
162 * @param __l std::initializer_list of characters.
163 * @param __a Allocator to use (default is default allocator).
164 */
165 __versa_string(std::initializer_list<_CharT> __l,
166 const _Alloc& __a = _Alloc())
167 : __vstring_base(__l.begin(), __l.end(), __a) { }
168 #endif
169
170 /**
171 * @brief Construct string as copy of a substring.
172 * @param __str Source string.
173 * @param __pos Index of first character to copy from.
174 * @param __n Number of characters to copy (default remainder).
175 */
176 __versa_string(const __versa_string& __str, size_type __pos,
177 size_type __n = npos)
178 : __vstring_base(__str._M_data()
179 + __str._M_check(__pos,
180 "__versa_string::__versa_string"),
181 __str._M_data() + __str._M_limit(__pos, __n)
182 + __pos, _Alloc()) { }
183
184 /**
185 * @brief Construct string as copy of a substring.
186 * @param __str Source string.
187 * @param __pos Index of first character to copy from.
188 * @param __n Number of characters to copy.
189 * @param __a Allocator to use.
190 */
191 __versa_string(const __versa_string& __str, size_type __pos,
192 size_type __n, const _Alloc& __a)
193 : __vstring_base(__str._M_data()
194 + __str._M_check(__pos,
195 "__versa_string::__versa_string"),
196 __str._M_data() + __str._M_limit(__pos, __n)
197 + __pos, __a) { }
198
199 /**
200 * @brief Construct string initialized by a character array.
201 * @param __s Source character array.
202 * @param __n Number of characters to copy.
203 * @param __a Allocator to use (default is default allocator).
204 *
205 * NB: @a __s must have at least @a __n characters, '\\0' has no special
206 * meaning.
207 */
208 __versa_string(const _CharT* __s, size_type __n,
209 const _Alloc& __a = _Alloc())
210 : __vstring_base(__s, __s + __n, __a) { }
211
212 /**
213 * @brief Construct string as copy of a C string.
214 * @param __s Source C string.
215 * @param __a Allocator to use (default is default allocator).
216 */
217 __versa_string(const _CharT* __s, const _Alloc& __a = _Alloc())
218 : __vstring_base(__s, __s ? __s + traits_type::length(__s) :
219 __s + npos, __a) { }
220
221 /**
222 * @brief Construct string as multiple characters.
223 * @param __n Number of characters.
224 * @param __c Character to use.
225 * @param __a Allocator to use (default is default allocator).
226 */
227 __versa_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
228 : __vstring_base(__n, __c, __a) { }
229
230 /**
231 * @brief Construct string as copy of a range.
232 * @param __beg Start of range.
233 * @param __end End of range.
234 * @param __a Allocator to use (default is default allocator).
235 */
236 #if __cplusplus >= 201103L
237 template<class _InputIterator,
238 typename = std::_RequireInputIter<_InputIterator>>
239 #else
240 template<class _InputIterator>
241 #endif
242 __versa_string(_InputIterator __beg, _InputIterator __end,
243 const _Alloc& __a = _Alloc())
244 : __vstring_base(__beg, __end, __a) { }
245
246 /**
247 * @brief Destroy the string instance.
248 */
249 ~__versa_string() _GLIBCXX_NOEXCEPT { }
250
251 /**
252 * @brief Assign the value of @a str to this string.
253 * @param __str Source string.
254 */
255 __versa_string&
256 operator=(const __versa_string& __str)
257 { return this->assign(__str); }
258
259 #if __cplusplus >= 201103L
260 /**
261 * @brief String move assignment operator.
262 * @param __str Source string.
263 *
264 * The contents of @a __str are moved into this string (without
265 * copying). @a __str is a valid, but unspecified string.
266 */
267 __versa_string&
268 operator=(__versa_string&& __str) noexcept
269 {
270 // NB: DR 1204.
271 this->swap(__str);
272 return *this;
273 }
274
275 /**
276 * @brief Set value to string constructed from initializer list.
277 * @param __l std::initializer_list.
278 */
279 __versa_string&
280 operator=(std::initializer_list<_CharT> __l)
281 {
282 this->assign(__l.begin(), __l.end());
283 return *this;
284 }
285 #endif
286
287 /**
288 * @brief Copy contents of @a __s into this string.
289 * @param __s Source null-terminated string.
290 */
291 __versa_string&
292 operator=(const _CharT* __s)
293 { return this->assign(__s); }
294
295 /**
296 * @brief Set value to string of length 1.
297 * @param __c Source character.
298 *
299 * Assigning to a character makes this string length 1 and
300 * (*this)[0] == @a __c.
301 */
302 __versa_string&
303 operator=(_CharT __c)
304 {
305 this->assign(1, __c);
306 return *this;
307 }
308
309 // Iterators:
310 /**
311 * Returns a read/write iterator that points to the first character in
312 * the %string. Unshares the string.
313 */
314 iterator
315 begin() _GLIBCXX_NOEXCEPT
316 {
317 this->_M_leak();
318 return iterator(this->_M_data());
319 }
320
321 /**
322 * Returns a read-only (constant) iterator that points to the first
323 * character in the %string.
324 */
325 const_iterator
326 begin() const _GLIBCXX_NOEXCEPT
327 { return const_iterator(this->_M_data()); }
328
329 /**
330 * Returns a read/write iterator that points one past the last
331 * character in the %string. Unshares the string.
332 */
333 iterator
334 end() _GLIBCXX_NOEXCEPT
335 {
336 this->_M_leak();
337 return iterator(this->_M_data() + this->size());
338 }
339
340 /**
341 * Returns a read-only (constant) iterator that points one past the
342 * last character in the %string.
343 */
344 const_iterator
345 end() const _GLIBCXX_NOEXCEPT
346 { return const_iterator(this->_M_data() + this->size()); }
347
348 /**
349 * Returns a read/write reverse iterator that points to the last
350 * character in the %string. Iteration is done in reverse element
351 * order. Unshares the string.
352 */
353 reverse_iterator
354 rbegin() _GLIBCXX_NOEXCEPT
355 { return reverse_iterator(this->end()); }
356
357 /**
358 * Returns a read-only (constant) reverse iterator that points
359 * to the last character in the %string. Iteration is done in
360 * reverse element order.
361 */
362 const_reverse_iterator
363 rbegin() const _GLIBCXX_NOEXCEPT
364 { return const_reverse_iterator(this->end()); }
365
366 /**
367 * Returns a read/write reverse iterator that points to one before the
368 * first character in the %string. Iteration is done in reverse
369 * element order. Unshares the string.
370 */
371 reverse_iterator
372 rend() _GLIBCXX_NOEXCEPT
373 { return reverse_iterator(this->begin()); }
374
375 /**
376 * Returns a read-only (constant) reverse iterator that points
377 * to one before the first character in the %string. Iteration
378 * is done in reverse element order.
379 */
380 const_reverse_iterator
381 rend() const _GLIBCXX_NOEXCEPT
382 { return const_reverse_iterator(this->begin()); }
383
384 #if __cplusplus >= 201103L
385 /**
386 * Returns a read-only (constant) iterator that points to the first
387 * character in the %string.
388 */
389 const_iterator
390 cbegin() const noexcept
391 { return const_iterator(this->_M_data()); }
392
393 /**
394 * Returns a read-only (constant) iterator that points one past the
395 * last character in the %string.
396 */
397 const_iterator
398 cend() const noexcept
399 { return const_iterator(this->_M_data() + this->size()); }
400
401 /**
402 * Returns a read-only (constant) reverse iterator that points
403 * to the last character in the %string. Iteration is done in
404 * reverse element order.
405 */
406 const_reverse_iterator
407 crbegin() const noexcept
408 { return const_reverse_iterator(this->end()); }
409
410 /**
411 * Returns a read-only (constant) reverse iterator that points
412 * to one before the first character in the %string. Iteration
413 * is done in reverse element order.
414 */
415 const_reverse_iterator
416 crend() const noexcept
417 { return const_reverse_iterator(this->begin()); }
418 #endif
419
420 public:
421 // Capacity:
422 /// Returns the number of characters in the string, not including any
423 /// null-termination.
424 size_type
425 size() const _GLIBCXX_NOEXCEPT
426 { return this->_M_length(); }
427
428 /// Returns the number of characters in the string, not including any
429 /// null-termination.
430 size_type
431 length() const _GLIBCXX_NOEXCEPT
432 { return this->_M_length(); }
433
434 /// Returns the size() of the largest possible %string.
435 size_type
436 max_size() const _GLIBCXX_NOEXCEPT
437 { return this->_M_max_size(); }
438
439 /**
440 * @brief Resizes the %string to the specified number of characters.
441 * @param __n Number of characters the %string should contain.
442 * @param __c Character to fill any new elements.
443 *
444 * This function will %resize the %string to the specified
445 * number of characters. If the number is smaller than the
446 * %string's current size the %string is truncated, otherwise
447 * the %string is extended and new elements are set to @a __c.
448 */
449 void
450 resize(size_type __n, _CharT __c);
451
452 /**
453 * @brief Resizes the %string to the specified number of characters.
454 * @param __n Number of characters the %string should contain.
455 *
456 * This function will resize the %string to the specified
457 * length. If the new size is smaller than the %string's
458 * current size the %string is truncated, otherwise the %string
459 * is extended and new characters are default-constructed. For
460 * basic types such as char, this means setting them to 0.
461 */
462 void
463 resize(size_type __n)
464 { this->resize(__n, _CharT()); }
465
466 #if __cplusplus >= 201103L
467 /// A non-binding request to reduce capacity() to size().
468 void
469 shrink_to_fit() noexcept
470 {
471 if (capacity() > size())
472 {
473 __try
474 { this->reserve(0); }
475 __catch(...)
476 { }
477 }
478 }
479 #endif
480
481 /**
482 * Returns the total number of characters that the %string can
483 * hold before needing to allocate more memory.
484 */
485 size_type
486 capacity() const _GLIBCXX_NOEXCEPT
487 { return this->_M_capacity(); }
488
489 /**
490 * @brief Attempt to preallocate enough memory for specified number of
491 * characters.
492 * @param __res_arg Number of characters required.
493 * @throw std::length_error If @a __res_arg exceeds @c max_size().
494 *
495 * This function attempts to reserve enough memory for the
496 * %string to hold the specified number of characters. If the
497 * number requested is more than max_size(), length_error is
498 * thrown.
499 *
500 * The advantage of this function is that if optimal code is a
501 * necessity and the user can determine the string length that
502 * will be required, the user can reserve the memory in
503 * %advance, and thus prevent a possible reallocation of memory
504 * and copying of %string data.
505 */
506 void
507 reserve(size_type __res_arg = 0)
508 { this->_M_reserve(__res_arg); }
509
510 /**
511 * Erases the string, making it empty.
512 */
513 void
514 clear() _GLIBCXX_NOEXCEPT
515 { this->_M_clear(); }
516
517 /**
518 * Returns true if the %string is empty. Equivalent to
519 * <code>*this == ""</code>.
520 */
521 bool
522 empty() const _GLIBCXX_NOEXCEPT
523 { return this->size() == 0; }
524
525 // Element access:
526 /**
527 * @brief Subscript access to the data contained in the %string.
528 * @param __pos The index of the character to access.
529 * @return Read-only (constant) reference to the character.
530 *
531 * This operator allows for easy, array-style, data access.
532 * Note that data access with this operator is unchecked and
533 * out_of_range lookups are not defined. (For checked lookups
534 * see at().)
535 */
536 const_reference
537 operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
538 {
539 __glibcxx_assert(__pos <= this->size());
540 return this->_M_data()[__pos];
541 }
542
543 /**
544 * @brief Subscript access to the data contained in the %string.
545 * @param __pos The index of the character to access.
546 * @return Read/write reference to the character.
547 *
548 * This operator allows for easy, array-style, data access.
549 * Note that data access with this operator is unchecked and
550 * out_of_range lookups are not defined. (For checked lookups
551 * see at().) Unshares the string.
552 */
553 reference
554 operator[](size_type __pos) _GLIBCXX_NOEXCEPT
555 {
556 // Allow pos == size() both in C++98 mode, as v3 extension,
557 // and in C++11 mode.
558 __glibcxx_assert(__pos <= this->size());
559 // In pedantic mode be strict in C++98 mode.
560 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L
561 || __pos < this->size());
562 this->_M_leak();
563 return this->_M_data()[__pos];
564 }
565
566 /**
567 * @brief Provides access to the data contained in the %string.
568 * @param __n The index of the character to access.
569 * @return Read-only (const) reference to the character.
570 * @throw std::out_of_range If @a __n is an invalid index.
571 *
572 * This function provides for safer data access. The parameter
573 * is first checked that it is in the range of the string. The
574 * function throws out_of_range if the check fails.
575 */
576 const_reference
577 at(size_type __n) const
578 {
579 if (__n >= this->size())
580 std::__throw_out_of_range_fmt(__N("__versa_string::at: __n "
581 "(which is %zu) >= this->size() "
582 "(which is %zu)"),
583 __n, this->size());
584 return this->_M_data()[__n];
585 }
586
587 /**
588 * @brief Provides access to the data contained in the %string.
589 * @param __n The index of the character to access.
590 * @return Read/write reference to the character.
591 * @throw std::out_of_range If @a __n is an invalid index.
592 *
593 * This function provides for safer data access. The parameter
594 * is first checked that it is in the range of the string. The
595 * function throws out_of_range if the check fails. Success
596 * results in unsharing the string.
597 */
598 reference
599 at(size_type __n)
600 {
601 if (__n >= this->size())
602 std::__throw_out_of_range_fmt(__N("__versa_string::at: __n "
603 "(which is %zu) >= this->size() "
604 "(which is %zu)"),
605 __n, this->size());
606 this->_M_leak();
607 return this->_M_data()[__n];
608 }
609
610 #if __cplusplus >= 201103L
611 /**
612 * Returns a read/write reference to the data at the first
613 * element of the %string.
614 */
615 reference
616 front() noexcept
617 { return operator[](0); }
618
619 /**
620 * Returns a read-only (constant) reference to the data at the first
621 * element of the %string.
622 */
623 const_reference
624 front() const noexcept
625 { return operator[](0); }
626
627 /**
628 * Returns a read/write reference to the data at the last
629 * element of the %string.
630 */
631 reference
632 back() noexcept
633 { return operator[](this->size() - 1); }
634
635 /**
636 * Returns a read-only (constant) reference to the data at the
637 * last element of the %string.
638 */
639 const_reference
640 back() const noexcept
641 { return operator[](this->size() - 1); }
642 #endif
643
644 // Modifiers:
645 /**
646 * @brief Append a string to this string.
647 * @param __str The string to append.
648 * @return Reference to this string.
649 */
650 __versa_string&
651 operator+=(const __versa_string& __str)
652 { return this->append(__str); }
653
654 /**
655 * @brief Append a C string.
656 * @param __s The C string to append.
657 * @return Reference to this string.
658 */
659 __versa_string&
660 operator+=(const _CharT* __s)
661 { return this->append(__s); }
662
663 /**
664 * @brief Append a character.
665 * @param __c The character to append.
666 * @return Reference to this string.
667 */
668 __versa_string&
669 operator+=(_CharT __c)
670 {
671 this->push_back(__c);
672 return *this;
673 }
674
675 #if __cplusplus >= 201103L
676 /**
677 * @brief Append an initializer_list of characters.
678 * @param __l The initializer_list of characters to be appended.
679 * @return Reference to this string.
680 */
681 __versa_string&
682 operator+=(std::initializer_list<_CharT> __l)
683 { return this->append(__l.begin(), __l.end()); }
684 #endif // C++11
685
686 /**
687 * @brief Append a string to this string.
688 * @param __str The string to append.
689 * @return Reference to this string.
690 */
691 __versa_string&
692 append(const __versa_string& __str)
693 { return _M_append(__str._M_data(), __str.size()); }
694
695 /**
696 * @brief Append a substring.
697 * @param __str The string to append.
698 * @param __pos Index of the first character of str to append.
699 * @param __n The number of characters to append.
700 * @return Reference to this string.
701 * @throw std::out_of_range if @a pos is not a valid index.
702 *
703 * This function appends @a __n characters from @a __str
704 * starting at @a __pos to this string. If @a __n is is larger
705 * than the number of available characters in @a __str, the
706 * remainder of @a __str is appended.
707 */
708 __versa_string&
709 append(const __versa_string& __str, size_type __pos, size_type __n)
710 { return _M_append(__str._M_data()
711 + __str._M_check(__pos, "__versa_string::append"),
712 __str._M_limit(__pos, __n)); }
713
714 /**
715 * @brief Append a C substring.
716 * @param __s The C string to append.
717 * @param __n The number of characters to append.
718 * @return Reference to this string.
719 */
720 __versa_string&
721 append(const _CharT* __s, size_type __n)
722 {
723 __glibcxx_requires_string_len(__s, __n);
724 _M_check_length(size_type(0), __n, "__versa_string::append");
725 return _M_append(__s, __n);
726 }
727
728 /**
729 * @brief Append a C string.
730 * @param __s The C string to append.
731 * @return Reference to this string.
732 */
733 __versa_string&
734 append(const _CharT* __s)
735 {
736 __glibcxx_requires_string(__s);
737 const size_type __n = traits_type::length(__s);
738 _M_check_length(size_type(0), __n, "__versa_string::append");
739 return _M_append(__s, __n);
740 }
741
742 /**
743 * @brief Append multiple characters.
744 * @param __n The number of characters to append.
745 * @param __c The character to use.
746 * @return Reference to this string.
747 *
748 * Appends n copies of c to this string.
749 */
750 __versa_string&
751 append(size_type __n, _CharT __c)
752 { return _M_replace_aux(this->size(), size_type(0), __n, __c); }
753
754 #if __cplusplus >= 201103L
755 /**
756 * @brief Append an initializer_list of characters.
757 * @param __l The initializer_list of characters to append.
758 * @return Reference to this string.
759 */
760 __versa_string&
761 append(std::initializer_list<_CharT> __l)
762 { return this->append(__l.begin(), __l.end()); }
763 #endif // C++11
764
765 /**
766 * @brief Append a range of characters.
767 * @param __first Iterator referencing the first character to append.
768 * @param __last Iterator marking the end of the range.
769 * @return Reference to this string.
770 *
771 * Appends characters in the range [first,last) to this string.
772 */
773 #if __cplusplus >= 201103L
774 template<class _InputIterator,
775 typename = std::_RequireInputIter<_InputIterator>>
776 #else
777 template<class _InputIterator>
778 #endif
779 __versa_string&
780 append(_InputIterator __first, _InputIterator __last)
781 { return this->replace(_M_iend(), _M_iend(), __first, __last); }
782
783 /**
784 * @brief Append a single character.
785 * @param __c Character to append.
786 */
787 void
788 push_back(_CharT __c)
789 {
790 const size_type __size = this->size();
791 if (__size + 1 > this->capacity() || this->_M_is_shared())
792 this->_M_mutate(__size, size_type(0), 0, size_type(1));
793 traits_type::assign(this->_M_data()[__size], __c);
794 this->_M_set_length(__size + 1);
795 }
796
797 /**
798 * @brief Set value to contents of another string.
799 * @param __str Source string to use.
800 * @return Reference to this string.
801 */
802 __versa_string&
803 assign(const __versa_string& __str)
804 {
805 this->_M_assign(__str);
806 return *this;
807 }
808
809 #if __cplusplus >= 201103L
810 /**
811 * @brief Set value to contents of another string.
812 * @param __str Source string to use.
813 * @return Reference to this string.
814 *
815 * This function sets this string to the exact contents of @a __str.
816 * @a __str is a valid, but unspecified string.
817 */
818 __versa_string&
819 assign(__versa_string&& __str) noexcept
820 {
821 this->swap(__str);
822 return *this;
823 }
824 #endif // C++11
825
826 /**
827 * @brief Set value to a substring of a string.
828 * @param __str The string to use.
829 * @param __pos Index of the first character of str.
830 * @param __n Number of characters to use.
831 * @return Reference to this string.
832 * @throw std::out_of_range if @a __pos is not a valid index.
833 *
834 * This function sets this string to the substring of @a __str
835 * consisting of @a __n characters at @a __pos. If @a __n is
836 * is larger than the number of available characters in @a
837 * __str, the remainder of @a __str is used.
838 */
839 __versa_string&
840 assign(const __versa_string& __str, size_type __pos, size_type __n)
841 { return _M_replace(size_type(0), this->size(), __str._M_data()
842 + __str._M_check(__pos, "__versa_string::assign"),
843 __str._M_limit(__pos, __n)); }
844
845 /**
846 * @brief Set value to a C substring.
847 * @param __s The C string to use.
848 * @param __n Number of characters to use.
849 * @return Reference to this string.
850 *
851 * This function sets the value of this string to the first @a
852 * __n characters of @a __s. If @a __n is is larger than the
853 * number of available characters in @a __s, the remainder of
854 * @a __s is used.
855 */
856 __versa_string&
857 assign(const _CharT* __s, size_type __n)
858 {
859 __glibcxx_requires_string_len(__s, __n);
860 return _M_replace(size_type(0), this->size(), __s, __n);
861 }
862
863 /**
864 * @brief Set value to contents of a C string.
865 * @param __s The C string to use.
866 * @return Reference to this string.
867 *
868 * This function sets the value of this string to the value of
869 * @a __s. The data is copied, so there is no dependence on @a
870 * __s once the function returns.
871 */
872 __versa_string&
873 assign(const _CharT* __s)
874 {
875 __glibcxx_requires_string(__s);
876 return _M_replace(size_type(0), this->size(), __s,
877 traits_type::length(__s));
878 }
879
880 /**
881 * @brief Set value to multiple characters.
882 * @param __n Length of the resulting string.
883 * @param __c The character to use.
884 * @return Reference to this string.
885 *
886 * This function sets the value of this string to @a __n copies of
887 * character @a __c.
888 */
889 __versa_string&
890 assign(size_type __n, _CharT __c)
891 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
892
893 /**
894 * @brief Set value to a range of characters.
895 * @param __first Iterator referencing the first character to append.
896 * @param __last Iterator marking the end of the range.
897 * @return Reference to this string.
898 *
899 * Sets value of string to characters in the range
900 * [first,last).
901 */
902 #if __cplusplus >= 201103L
903 template<class _InputIterator,
904 typename = std::_RequireInputIter<_InputIterator>>
905 #else
906 template<class _InputIterator>
907 #endif
908 __versa_string&
909 assign(_InputIterator __first, _InputIterator __last)
910 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
911
912 #if __cplusplus >= 201103L
913 /**
914 * @brief Set value to an initializer_list of characters.
915 * @param __l The initializer_list of characters to assign.
916 * @return Reference to this string.
917 */
918 __versa_string&
919 assign(std::initializer_list<_CharT> __l)
920 { return this->assign(__l.begin(), __l.end()); }
921 #endif // C++11
922
923 #if __cplusplus >= 201103L
924 /**
925 * @brief Insert multiple characters.
926 * @param __p Const_iterator referencing location in string to
927 * insert at.
928 * @param __n Number of characters to insert
929 * @param __c The character to insert.
930 * @return Iterator referencing the first inserted char.
931 * @throw std::length_error If new length exceeds @c max_size().
932 *
933 * Inserts @a __n copies of character @a __c starting at the
934 * position referenced by iterator @a __p. If adding
935 * characters causes the length to exceed max_size(),
936 * length_error is thrown. The value of the string doesn't
937 * change if an error is thrown.
938 */
939 iterator
940 insert(const_iterator __p, size_type __n, _CharT __c)
941 {
942 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
943 const size_type __pos = __p - _M_ibegin();
944 this->replace(__p, __p, __n, __c);
945 return iterator(this->_M_data() + __pos);
946 }
947 #else
948 /**
949 * @brief Insert multiple characters.
950 * @param __p Iterator referencing location in string to insert at.
951 * @param __n Number of characters to insert
952 * @param __c The character to insert.
953 * @throw std::length_error If new length exceeds @c max_size().
954 *
955 * Inserts @a __n copies of character @a __c starting at the
956 * position referenced by iterator @a __p. If adding
957 * characters causes the length to exceed max_size(),
958 * length_error is thrown. The value of the string doesn't
959 * change if an error is thrown.
960 */
961 void
962 insert(iterator __p, size_type __n, _CharT __c)
963 { this->replace(__p, __p, __n, __c); }
964 #endif
965
966 #if __cplusplus >= 201103L
967 /**
968 * @brief Insert a range of characters.
969 * @param __p Const_iterator referencing location in string to
970 * insert at.
971 * @param __beg Start of range.
972 * @param __end End of range.
973 * @return Iterator referencing the first inserted char.
974 * @throw std::length_error If new length exceeds @c max_size().
975 *
976 * Inserts characters in range [beg,end). If adding characters
977 * causes the length to exceed max_size(), length_error is
978 * thrown. The value of the string doesn't change if an error
979 * is thrown.
980 */
981 template<class _InputIterator,
982 typename = std::_RequireInputIter<_InputIterator>>
983 iterator
984 insert(const_iterator __p, _InputIterator __beg, _InputIterator __end)
985 {
986 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
987 const size_type __pos = __p - _M_ibegin();
988 this->replace(__p, __p, __beg, __end);
989 return iterator(this->_M_data() + __pos);
990 }
991 #else
992 /**
993 * @brief Insert a range of characters.
994 * @param __p Iterator referencing location in string to insert at.
995 * @param __beg Start of range.
996 * @param __end End of range.
997 * @throw std::length_error If new length exceeds @c max_size().
998 *
999 * Inserts characters in range [beg,end). If adding characters
1000 * causes the length to exceed max_size(), length_error is
1001 * thrown. The value of the string doesn't change if an error
1002 * is thrown.
1003 */
1004 template<class _InputIterator>
1005 void
1006 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
1007 { this->replace(__p, __p, __beg, __end); }
1008 #endif
1009
1010 #if __cplusplus >= 201103L
1011 /**
1012 * @brief Insert an initializer_list of characters.
1013 * @param __p Const_iterator referencing location in string to
1014 * insert at.
1015 * @param __l The initializer_list of characters to insert.
1016 * @return Iterator referencing the first inserted char.
1017 * @throw std::length_error If new length exceeds @c max_size().
1018 */
1019 iterator
1020 insert(const_iterator __p, std::initializer_list<_CharT> __l)
1021 { return this->insert(__p, __l.begin(), __l.end()); }
1022 #endif // C++11
1023
1024 /**
1025 * @brief Insert value of a string.
1026 * @param __pos1 Iterator referencing location in string to insert at.
1027 * @param __str The string to insert.
1028 * @return Reference to this string.
1029 * @throw std::length_error If new length exceeds @c max_size().
1030 *
1031 * Inserts value of @a __str starting at @a __pos1. If adding
1032 * characters causes the length to exceed max_size(),
1033 * length_error is thrown. The value of the string doesn't
1034 * change if an error is thrown.
1035 */
1036 __versa_string&
1037 insert(size_type __pos1, const __versa_string& __str)
1038 { return this->replace(__pos1, size_type(0),
1039 __str._M_data(), __str.size()); }
1040
1041 /**
1042 * @brief Insert a substring.
1043 * @param __pos1 Iterator referencing location in string to insert at.
1044 * @param __str The string to insert.
1045 * @param __pos2 Start of characters in str to insert.
1046 * @param __n Number of characters to insert.
1047 * @return Reference to this string.
1048 * @throw std::length_error If new length exceeds @c max_size().
1049 * @throw std::out_of_range If @a __pos1 > size() or
1050 * @a __pos2 > @a __str.size().
1051 *
1052 * Starting at @a __pos1, insert @a __n character of @a __str
1053 * beginning with @a __pos2. If adding characters causes the
1054 * length to exceed max_size(), length_error is thrown. If @a
1055 * __pos1 is beyond the end of this string or @a __pos2 is
1056 * beyond the end of @a __str, out_of_range is thrown. The
1057 * value of the string doesn't change if an error is thrown.
1058 */
1059 __versa_string&
1060 insert(size_type __pos1, const __versa_string& __str,
1061 size_type __pos2, size_type __n)
1062 { return this->replace(__pos1, size_type(0), __str._M_data()
1063 + __str._M_check(__pos2, "__versa_string::insert"),
1064 __str._M_limit(__pos2, __n)); }
1065
1066 /**
1067 * @brief Insert a C substring.
1068 * @param __pos Iterator referencing location in string to insert at.
1069 * @param __s The C string to insert.
1070 * @param __n The number of characters to insert.
1071 * @return Reference to this string.
1072 * @throw std::length_error If new length exceeds @c max_size().
1073 * @throw std::out_of_range If @a __pos is beyond the end of this
1074 * string.
1075 *
1076 * Inserts the first @a __n characters of @a __s starting at @a
1077 * __pos. If adding characters causes the length to exceed
1078 * max_size(), length_error is thrown. If @a __pos is beyond
1079 * end(), out_of_range is thrown. The value of the string
1080 * doesn't change if an error is thrown.
1081 */
1082 __versa_string&
1083 insert(size_type __pos, const _CharT* __s, size_type __n)
1084 { return this->replace(__pos, size_type(0), __s, __n); }
1085
1086 /**
1087 * @brief Insert a C string.
1088 * @param __pos Iterator referencing location in string to insert at.
1089 * @param __s The C string to insert.
1090 * @return Reference to this string.
1091 * @throw std::length_error If new length exceeds @c max_size().
1092 * @throw std::out_of_range If @a __pos is beyond the end of this
1093 * string.
1094 *
1095 * Inserts the first @a __n characters of @a __s starting at @a
1096 * __pos. If adding characters causes the length to exceed
1097 * max_size(), length_error is thrown. If @a __pos is beyond
1098 * end(), out_of_range is thrown. The value of the string
1099 * doesn't change if an error is thrown.
1100 */
1101 __versa_string&
1102 insert(size_type __pos, const _CharT* __s)
1103 {
1104 __glibcxx_requires_string(__s);
1105 return this->replace(__pos, size_type(0), __s,
1106 traits_type::length(__s));
1107 }
1108
1109 /**
1110 * @brief Insert multiple characters.
1111 * @param __pos Index in string to insert at.
1112 * @param __n Number of characters to insert
1113 * @param __c The character to insert.
1114 * @return Reference to this string.
1115 * @throw std::length_error If new length exceeds @c max_size().
1116 * @throw std::out_of_range If @a __pos is beyond the end of this
1117 * string.
1118 *
1119 * Inserts @a __n copies of character @a __c starting at index
1120 * @a __pos. If adding characters causes the length to exceed
1121 * max_size(), length_error is thrown. If @a __pos > length(),
1122 * out_of_range is thrown. The value of the string doesn't
1123 * change if an error is thrown.
1124 */
1125 __versa_string&
1126 insert(size_type __pos, size_type __n, _CharT __c)
1127 { return _M_replace_aux(_M_check(__pos, "__versa_string::insert"),
1128 size_type(0), __n, __c); }
1129
1130 /**
1131 * @brief Insert one character.
1132 * @param __p Iterator referencing position in string to insert at.
1133 * @param __c The character to insert.
1134 * @return Iterator referencing newly inserted char.
1135 * @throw std::length_error If new length exceeds @c max_size().
1136 *
1137 * Inserts character @a __c at position referenced by @a __p.
1138 * If adding character causes the length to exceed max_size(),
1139 * length_error is thrown. If @a __p is beyond end of string,
1140 * out_of_range is thrown. The value of the string doesn't
1141 * change if an error is thrown.
1142 */
1143 iterator
1144 #if __cplusplus >= 201103L
1145 insert(const_iterator __p, _CharT __c)
1146 #else
1147 insert(iterator __p, _CharT __c)
1148 #endif
1149 {
1150 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1151 const size_type __pos = __p - _M_ibegin();
1152 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1153 this->_M_set_leaked();
1154 return iterator(this->_M_data() + __pos);
1155 }
1156
1157 /**
1158 * @brief Remove characters.
1159 * @param __pos Index of first character to remove (default 0).
1160 * @param __n Number of characters to remove (default remainder).
1161 * @return Reference to this string.
1162 * @throw std::out_of_range If @a __pos is beyond the end of this
1163 * string.
1164 *
1165 * Removes @a __n characters from this string starting at @a
1166 * __pos. The length of the string is reduced by @a __n. If
1167 * there are < @a __n characters to remove, the remainder of
1168 * the string is truncated. If @a __p is beyond end of string,
1169 * out_of_range is thrown. The value of the string doesn't
1170 * change if an error is thrown.
1171 */
1172 __versa_string&
1173 erase(size_type __pos = 0, size_type __n = npos)
1174 {
1175 this->_M_erase(_M_check(__pos, "__versa_string::erase"),
1176 _M_limit(__pos, __n));
1177 return *this;
1178 }
1179
1180 /**
1181 * @brief Remove one character.
1182 * @param __position Iterator referencing the character to remove.
1183 * @return iterator referencing same location after removal.
1184 *
1185 * Removes the character at @a __position from this string. The
1186 * value of the string doesn't change if an error is thrown.
1187 */
1188 iterator
1189 #if __cplusplus >= 201103L
1190 erase(const_iterator __position)
1191 #else
1192 erase(iterator __position)
1193 #endif
1194 {
1195 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
1196 && __position < _M_iend());
1197 const size_type __pos = __position - _M_ibegin();
1198 this->_M_erase(__pos, size_type(1));
1199 this->_M_set_leaked();
1200 return iterator(this->_M_data() + __pos);
1201 }
1202
1203 /**
1204 * @brief Remove a range of characters.
1205 * @param __first Iterator referencing the first character to remove.
1206 * @param __last Iterator referencing the end of the range.
1207 * @return Iterator referencing location of first after removal.
1208 *
1209 * Removes the characters in the range [first,last) from this
1210 * string. The value of the string doesn't change if an error
1211 * is thrown.
1212 */
1213 iterator
1214 #if __cplusplus >= 201103L
1215 erase(const_iterator __first, const_iterator __last)
1216 #else
1217 erase(iterator __first, iterator __last)
1218 #endif
1219 {
1220 _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
1221 && __last <= _M_iend());
1222 const size_type __pos = __first - _M_ibegin();
1223 this->_M_erase(__pos, __last - __first);
1224 this->_M_set_leaked();
1225 return iterator(this->_M_data() + __pos);
1226 }
1227
1228 #if __cplusplus >= 201103L
1229 /**
1230 * @brief Remove the last character.
1231 *
1232 * The string must be non-empty.
1233 */
1234 void
1235 pop_back()
1236 { this->_M_erase(size()-1, 1); }
1237 #endif // C++11
1238
1239 /**
1240 * @brief Replace characters with value from another string.
1241 * @param __pos Index of first character to replace.
1242 * @param __n Number of characters to be replaced.
1243 * @param __str String to insert.
1244 * @return Reference to this string.
1245 * @throw std::out_of_range If @a __pos is beyond the end of this
1246 * string.
1247 * @throw std::length_error If new length exceeds @c max_size().
1248 *
1249 * Removes the characters in the range [pos,pos+n) from this
1250 * string. In place, the value of @a __str is inserted. If @a
1251 * __pos is beyond end of string, out_of_range is thrown. If
1252 * the length of the result exceeds max_size(), length_error is
1253 * thrown. The value of the string doesn't change if an error
1254 * is thrown.
1255 */
1256 __versa_string&
1257 replace(size_type __pos, size_type __n, const __versa_string& __str)
1258 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1259
1260 /**
1261 * @brief Replace characters with value from another string.
1262 * @param __pos1 Index of first character to replace.
1263 * @param __n1 Number of characters to be replaced.
1264 * @param __str String to insert.
1265 * @param __pos2 Index of first character of str to use.
1266 * @param __n2 Number of characters from str to use.
1267 * @return Reference to this string.
1268 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
1269 * str.size().
1270 * @throw std::length_error If new length exceeds @c max_size().
1271 *
1272 * Removes the characters in the range [pos1,pos1 + n) from
1273 * this string. In place, the value of @a __str is inserted.
1274 * If @a __pos is beyond end of string, out_of_range is thrown.
1275 * If the length of the result exceeds max_size(), length_error
1276 * is thrown. The value of the string doesn't change if an
1277 * error is thrown.
1278 */
1279 __versa_string&
1280 replace(size_type __pos1, size_type __n1, const __versa_string& __str,
1281 size_type __pos2, size_type __n2)
1282 {
1283 return this->replace(__pos1, __n1, __str._M_data()
1284 + __str._M_check(__pos2,
1285 "__versa_string::replace"),
1286 __str._M_limit(__pos2, __n2));
1287 }
1288
1289 /**
1290 * @brief Replace characters with value of a C substring.
1291 * @param __pos Index of first character to replace.
1292 * @param __n1 Number of characters to be replaced.
1293 * @param __s C string to insert.
1294 * @param __n2 Number of characters from @a __s to use.
1295 * @return Reference to this string.
1296 * @throw std::out_of_range If @a __pos1 > size().
1297 * @throw std::length_error If new length exceeds @c max_size().
1298 *
1299 * Removes the characters in the range [pos,pos + n1) from this
1300 * string. In place, the first @a __n2 characters of @a __s
1301 * are inserted, or all of @a __s if @a __n2 is too large. If
1302 * @a __pos is beyond end of string, out_of_range is thrown.
1303 * If the length of result exceeds max_size(), length_error is
1304 * thrown. The value of the string doesn't change if an error
1305 * is thrown.
1306 */
1307 __versa_string&
1308 replace(size_type __pos, size_type __n1, const _CharT* __s,
1309 size_type __n2)
1310 {
1311 __glibcxx_requires_string_len(__s, __n2);
1312 return _M_replace(_M_check(__pos, "__versa_string::replace"),
1313 _M_limit(__pos, __n1), __s, __n2);
1314 }
1315
1316 /**
1317 * @brief Replace characters with value of a C string.
1318 * @param __pos Index of first character to replace.
1319 * @param __n1 Number of characters to be replaced.
1320 * @param __s C string to insert.
1321 * @return Reference to this string.
1322 * @throw std::out_of_range If @a __pos > size().
1323 * @throw std::length_error If new length exceeds @c max_size().
1324 *
1325 * Removes the characters in the range [pos,pos + n1) from this
1326 * string. In place, the characters of @a __s are inserted. If
1327 * @a pos is beyond end of string, out_of_range is thrown. If
1328 * the length of result exceeds max_size(), length_error is thrown.
1329 * The value of the string doesn't change if an error is thrown.
1330 */
1331 __versa_string&
1332 replace(size_type __pos, size_type __n1, const _CharT* __s)
1333 {
1334 __glibcxx_requires_string(__s);
1335 return this->replace(__pos, __n1, __s, traits_type::length(__s));
1336 }
1337
1338 /**
1339 * @brief Replace characters with multiple characters.
1340 * @param __pos Index of first character to replace.
1341 * @param __n1 Number of characters to be replaced.
1342 * @param __n2 Number of characters to insert.
1343 * @param __c Character to insert.
1344 * @return Reference to this string.
1345 * @throw std::out_of_range If @a __pos > size().
1346 * @throw std::length_error If new length exceeds @c max_size().
1347 *
1348 * Removes the characters in the range [pos,pos + n1) from this
1349 * string. In place, @a __n2 copies of @a __c are inserted.
1350 * If @a __pos is beyond end of string, out_of_range is thrown.
1351 * If the length of result exceeds max_size(), length_error is
1352 * thrown. The value of the string doesn't change if an error
1353 * is thrown.
1354 */
1355 __versa_string&
1356 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1357 { return _M_replace_aux(_M_check(__pos, "__versa_string::replace"),
1358 _M_limit(__pos, __n1), __n2, __c); }
1359
1360 /**
1361 * @brief Replace range of characters with string.
1362 * @param __i1 Iterator referencing start of range to replace.
1363 * @param __i2 Iterator referencing end of range to replace.
1364 * @param __str String value to insert.
1365 * @return Reference to this string.
1366 * @throw std::length_error If new length exceeds @c max_size().
1367 *
1368 * Removes the characters in the range [i1,i2). In place, the
1369 * value of @a __str is inserted. If the length of result
1370 * exceeds max_size(), length_error is thrown. The value of
1371 * the string doesn't change if an error is thrown.
1372 */
1373 __versa_string&
1374 #if __cplusplus >= 201103L
1375 replace(const_iterator __i1, const_iterator __i2,
1376 const __versa_string& __str)
1377 #else
1378 replace(iterator __i1, iterator __i2, const __versa_string& __str)
1379 #endif
1380 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
1381
1382 /**
1383 * @brief Replace range of characters with C substring.
1384 * @param __i1 Iterator referencing start of range to replace.
1385 * @param __i2 Iterator referencing end of range to replace.
1386 * @param __s C string value to insert.
1387 * @param __n Number of characters from s to insert.
1388 * @return Reference to this string.
1389 * @throw std::length_error If new length exceeds @c max_size().
1390 *
1391 * Removes the characters in the range [i1,i2). In place, the
1392 * first @a n characters of @a __s are inserted. If the length
1393 * of result exceeds max_size(), length_error is thrown. The
1394 * value of the string doesn't change if an error is thrown.
1395 */
1396 __versa_string&
1397 #if __cplusplus >= 201103L
1398 replace(const_iterator __i1, const_iterator __i2,
1399 const _CharT* __s, size_type __n)
1400 #else
1401 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
1402 #endif
1403 {
1404 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1405 && __i2 <= _M_iend());
1406 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
1407 }
1408
1409 /**
1410 * @brief Replace range of characters with C string.
1411 * @param __i1 Iterator referencing start of range to replace.
1412 * @param __i2 Iterator referencing end of range to replace.
1413 * @param __s C string value to insert.
1414 * @return Reference to this string.
1415 * @throw std::length_error If new length exceeds @c max_size().
1416 *
1417 * Removes the characters in the range [i1,i2). In place, the
1418 * characters of @a __s are inserted. If the length of result
1419 * exceeds max_size(), length_error is thrown. The value of
1420 * the string doesn't change if an error is thrown.
1421 */
1422 __versa_string&
1423 #if __cplusplus >= 201103L
1424 replace(const_iterator __i1, const_iterator __i2, const _CharT* __s)
1425 #else
1426 replace(iterator __i1, iterator __i2, const _CharT* __s)
1427 #endif
1428 {
1429 __glibcxx_requires_string(__s);
1430 return this->replace(__i1, __i2, __s, traits_type::length(__s));
1431 }
1432
1433 /**
1434 * @brief Replace range of characters with multiple characters
1435 * @param __i1 Iterator referencing start of range to replace.
1436 * @param __i2 Iterator referencing end of range to replace.
1437 * @param __n Number of characters to insert.
1438 * @param __c Character to insert.
1439 * @return Reference to this string.
1440 * @throw std::length_error If new length exceeds @c max_size().
1441 *
1442 * Removes the characters in the range [i1,i2). In place, @a
1443 * __n copies of @a __c are inserted. If the length of result
1444 * exceeds max_size(), length_error is thrown. The value of
1445 * the string doesn't change if an error is thrown.
1446 */
1447 __versa_string&
1448 #if __cplusplus >= 201103L
1449 replace(const_iterator __i1, const_iterator __i2, size_type __n,
1450 _CharT __c)
1451 #else
1452 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
1453 #endif
1454 {
1455 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1456 && __i2 <= _M_iend());
1457 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
1458 }
1459
1460 /**
1461 * @brief Replace range of characters with range.
1462 * @param __i1 Iterator referencing start of range to replace.
1463 * @param __i2 Iterator referencing end of range to replace.
1464 * @param __k1 Iterator referencing start of range to insert.
1465 * @param __k2 Iterator referencing end of range to insert.
1466 * @return Reference to this string.
1467 * @throw std::length_error If new length exceeds @c max_size().
1468 *
1469 * Removes the characters in the range [i1,i2). In place,
1470 * characters in the range [k1,k2) are inserted. If the length
1471 * of result exceeds max_size(), length_error is thrown. The
1472 * value of the string doesn't change if an error is thrown.
1473 */
1474 #if __cplusplus >= 201103L
1475 template<class _InputIterator,
1476 typename = std::_RequireInputIter<_InputIterator>>
1477 __versa_string&
1478 replace(const_iterator __i1, const_iterator __i2,
1479 _InputIterator __k1, _InputIterator __k2)
1480 {
1481 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1482 && __i2 <= _M_iend());
1483 __glibcxx_requires_valid_range(__k1, __k2);
1484 return this->_M_replace_dispatch(__i1, __i2, __k1, __k2,
1485 std::__false_type());
1486 }
1487 #else
1488 template<class _InputIterator>
1489 __versa_string&
1490 replace(iterator __i1, iterator __i2,
1491 _InputIterator __k1, _InputIterator __k2)
1492 {
1493 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1494 && __i2 <= _M_iend());
1495 __glibcxx_requires_valid_range(__k1, __k2);
1496 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1497 return this->_M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
1498 }
1499 #endif
1500
1501 // Specializations for the common case of pointer and iterator:
1502 // useful to avoid the overhead of temporary buffering in _M_replace.
1503 __versa_string&
1504 #if __cplusplus >= 201103L
1505 replace(const_iterator __i1, const_iterator __i2,
1506 _CharT* __k1, _CharT* __k2)
1507 #else
1508 replace(iterator __i1, iterator __i2,
1509 _CharT* __k1, _CharT* __k2)
1510 #endif
1511 {
1512 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1513 && __i2 <= _M_iend());
1514 __glibcxx_requires_valid_range(__k1, __k2);
1515 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1516 __k1, __k2 - __k1);
1517 }
1518
1519 __versa_string&
1520 #if __cplusplus >= 201103L
1521 replace(const_iterator __i1, const_iterator __i2,
1522 const _CharT* __k1, const _CharT* __k2)
1523 #else
1524 replace(iterator __i1, iterator __i2,
1525 const _CharT* __k1, const _CharT* __k2)
1526 #endif
1527 {
1528 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1529 && __i2 <= _M_iend());
1530 __glibcxx_requires_valid_range(__k1, __k2);
1531 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1532 __k1, __k2 - __k1);
1533 }
1534
1535 __versa_string&
1536 #if __cplusplus >= 201103L
1537 replace(const_iterator __i1, const_iterator __i2,
1538 iterator __k1, iterator __k2)
1539 #else
1540 replace(iterator __i1, iterator __i2,
1541 iterator __k1, iterator __k2)
1542 #endif
1543 {
1544 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1545 && __i2 <= _M_iend());
1546 __glibcxx_requires_valid_range(__k1, __k2);
1547 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1548 __k1.base(), __k2 - __k1);
1549 }
1550
1551 __versa_string&
1552 #if __cplusplus >= 201103L
1553 replace(const_iterator __i1, const_iterator __i2,
1554 const_iterator __k1, const_iterator __k2)
1555 #else
1556 replace(iterator __i1, iterator __i2,
1557 const_iterator __k1, const_iterator __k2)
1558 #endif
1559 {
1560 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1561 && __i2 <= _M_iend());
1562 __glibcxx_requires_valid_range(__k1, __k2);
1563 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1564 __k1.base(), __k2 - __k1);
1565 }
1566
1567 #if __cplusplus >= 201103L
1568 /**
1569 * @brief Replace range of characters with initializer_list.
1570 * @param __i1 Iterator referencing start of range to replace.
1571 * @param __i2 Iterator referencing end of range to replace.
1572 * @param __l The initializer_list of characters to insert.
1573 * @return Reference to this string.
1574 * @throw std::length_error If new length exceeds @c max_size().
1575 *
1576 * Removes the characters in the range [i1,i2). In place,
1577 * characters in the range [k1,k2) are inserted. If the length
1578 * of result exceeds max_size(), length_error is thrown. The
1579 * value of the string doesn't change if an error is thrown.
1580 */
1581 __versa_string&
1582 replace(const_iterator __i1, const_iterator __i2,
1583 std::initializer_list<_CharT> __l)
1584 { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
1585 #endif // C++11
1586
1587 private:
1588 template<class _Integer>
1589 __versa_string&
1590 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
1591 _Integer __n, _Integer __val, std::__true_type)
1592 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
1593
1594 template<class _InputIterator>
1595 __versa_string&
1596 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
1597 _InputIterator __k1, _InputIterator __k2,
1598 std::__false_type);
1599
1600 __versa_string&
1601 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
1602 _CharT __c);
1603
1604 __versa_string&
1605 _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
1606 const size_type __len2);
1607
1608 __versa_string&
1609 _M_append(const _CharT* __s, size_type __n);
1610
1611 public:
1612
1613 /**
1614 * @brief Copy substring into C string.
1615 * @param __s C string to copy value into.
1616 * @param __n Number of characters to copy.
1617 * @param __pos Index of first character to copy.
1618 * @return Number of characters actually copied
1619 * @throw std::out_of_range If pos > size().
1620 *
1621 * Copies up to @a __n characters starting at @a __pos into the
1622 * C string @a s. If @a __pos is greater than size(),
1623 * out_of_range is thrown.
1624 */
1625 size_type
1626 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
1627
1628 /**
1629 * @brief Swap contents with another string.
1630 * @param __s String to swap with.
1631 *
1632 * Exchanges the contents of this string with that of @a __s in
1633 * constant time.
1634 */
1635 void
1636 swap(__versa_string& __s) _GLIBCXX_NOEXCEPT
1637 { this->_M_swap(__s); }
1638
1639 // String operations:
1640 /**
1641 * @brief Return const pointer to null-terminated contents.
1642 *
1643 * This is a handle to internal data. Do not modify or dire things may
1644 * happen.
1645 */
1646 const _CharT*
1647 c_str() const _GLIBCXX_NOEXCEPT
1648 { return this->_M_data(); }
1649
1650 /**
1651 * @brief Return const pointer to contents.
1652 *
1653 * This is a handle to internal data. Do not modify or dire things may
1654 * happen.
1655 */
1656 const _CharT*
1657 data() const _GLIBCXX_NOEXCEPT
1658 { return this->_M_data(); }
1659
1660 /**
1661 * @brief Return copy of allocator used to construct this string.
1662 */
1663 allocator_type
1664 get_allocator() const _GLIBCXX_NOEXCEPT
1665 { return allocator_type(this->_M_get_allocator()); }
1666
1667 /**
1668 * @brief Find position of a C substring.
1669 * @param __s C string to locate.
1670 * @param __pos Index of character to search from.
1671 * @param __n Number of characters from @a __s to search for.
1672 * @return Index of start of first occurrence.
1673 *
1674 * Starting from @a __pos, searches forward for the first @a
1675 * __n characters in @a __s within this string. If found,
1676 * returns the index where it begins. If not found, returns
1677 * npos.
1678 */
1679 size_type
1680 find(const _CharT* __s, size_type __pos, size_type __n) const;
1681
1682 /**
1683 * @brief Find position of a string.
1684 * @param __str String to locate.
1685 * @param __pos Index of character to search from (default 0).
1686 * @return Index of start of first occurrence.
1687 *
1688 * Starting from @a __pos, searches forward for value of @a
1689 * __str within this string. If found, returns the index where
1690 * it begins. If not found, returns npos.
1691 */
1692 size_type
1693 find(const __versa_string& __str, size_type __pos = 0) const
1694 _GLIBCXX_NOEXCEPT
1695 { return this->find(__str.data(), __pos, __str.size()); }
1696
1697 /**
1698 * @brief Find position of a C string.
1699 * @param __s C string to locate.
1700 * @param __pos Index of character to search from (default 0).
1701 * @return Index of start of first occurrence.
1702 *
1703 * Starting from @a __pos, searches forward for the value of @a
1704 * __s within this string. If found, returns the index where
1705 * it begins. If not found, returns npos.
1706 */
1707 size_type
1708 find(const _CharT* __s, size_type __pos = 0) const
1709 {
1710 __glibcxx_requires_string(__s);
1711 return this->find(__s, __pos, traits_type::length(__s));
1712 }
1713
1714 /**
1715 * @brief Find position of a character.
1716 * @param __c Character to locate.
1717 * @param __pos Index of character to search from (default 0).
1718 * @return Index of first occurrence.
1719 *
1720 * Starting from @a __pos, searches forward for @a __c within
1721 * this string. If found, returns the index where it was
1722 * found. If not found, returns npos.
1723 */
1724 size_type
1725 find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
1726
1727 /**
1728 * @brief Find last position of a string.
1729 * @param __str String to locate.
1730 * @param __pos Index of character to search back from (default end).
1731 * @return Index of start of last occurrence.
1732 *
1733 * Starting from @a __pos, searches backward for value of @a
1734 * __str within this string. If found, returns the index where
1735 * it begins. If not found, returns npos.
1736 */
1737 size_type
1738 rfind(const __versa_string& __str, size_type __pos = npos) const
1739 _GLIBCXX_NOEXCEPT
1740 { return this->rfind(__str.data(), __pos, __str.size()); }
1741
1742 /**
1743 * @brief Find last position of a C substring.
1744 * @param __s C string to locate.
1745 * @param __pos Index of character to search back from.
1746 * @param __n Number of characters from s to search for.
1747 * @return Index of start of last occurrence.
1748 *
1749 * Starting from @a __pos, searches backward for the first @a
1750 * __n characters in @a __s within this string. If found,
1751 * returns the index where it begins. If not found, returns
1752 * npos.
1753 */
1754 size_type
1755 rfind(const _CharT* __s, size_type __pos, size_type __n) const;
1756
1757 /**
1758 * @brief Find last position of a C string.
1759 * @param __s C string to locate.
1760 * @param __pos Index of character to start search at (default end).
1761 * @return Index of start of last occurrence.
1762 *
1763 * Starting from @a __pos, searches backward for the value of
1764 * @a __s within this string. If found, returns the index
1765 * where it begins. If not found, returns npos.
1766 */
1767 size_type
1768 rfind(const _CharT* __s, size_type __pos = npos) const
1769 {
1770 __glibcxx_requires_string(__s);
1771 return this->rfind(__s, __pos, traits_type::length(__s));
1772 }
1773
1774 /**
1775 * @brief Find last position of a character.
1776 * @param __c Character to locate.
1777 * @param __pos Index of character to search back from (default end).
1778 * @return Index of last occurrence.
1779 *
1780 * Starting from @a __pos, searches backward for @a __c within
1781 * this string. If found, returns the index where it was
1782 * found. If not found, returns npos.
1783 */
1784 size_type
1785 rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
1786
1787 /**
1788 * @brief Find position of a character of string.
1789 * @param __str String containing characters to locate.
1790 * @param __pos Index of character to search from (default 0).
1791 * @return Index of first occurrence.
1792 *
1793 * Starting from @a __pos, searches forward for one of the characters of
1794 * @a __str within this string. If found, returns the index where it was
1795 * found. If not found, returns npos.
1796 */
1797 size_type
1798 find_first_of(const __versa_string& __str, size_type __pos = 0) const
1799 _GLIBCXX_NOEXCEPT
1800 { return this->find_first_of(__str.data(), __pos, __str.size()); }
1801
1802 /**
1803 * @brief Find position of a character of C substring.
1804 * @param __s String containing characters to locate.
1805 * @param __pos Index of character to search from.
1806 * @param __n Number of characters from s to search for.
1807 * @return Index of first occurrence.
1808 *
1809 * Starting from @a __pos, searches forward for one of the
1810 * first @a __n characters of @a __s within this string. If
1811 * found, returns the index where it was found. If not found,
1812 * returns npos.
1813 */
1814 size_type
1815 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
1816
1817 /**
1818 * @brief Find position of a character of C string.
1819 * @param __s String containing characters to locate.
1820 * @param __pos Index of character to search from (default 0).
1821 * @return Index of first occurrence.
1822 *
1823 * Starting from @a __pos, searches forward for one of the
1824 * characters of @a __s within this string. If found, returns
1825 * the index where it was found. If not found, returns npos.
1826 */
1827 size_type
1828 find_first_of(const _CharT* __s, size_type __pos = 0) const
1829 {
1830 __glibcxx_requires_string(__s);
1831 return this->find_first_of(__s, __pos, traits_type::length(__s));
1832 }
1833
1834 /**
1835 * @brief Find position of a character.
1836 * @param __c Character to locate.
1837 * @param __pos Index of character to search from (default 0).
1838 * @return Index of first occurrence.
1839 *
1840 * Starting from @a __pos, searches forward for the character
1841 * @a __c within this string. If found, returns the index
1842 * where it was found. If not found, returns npos.
1843 *
1844 * Note: equivalent to find(c, pos).
1845 */
1846 size_type
1847 find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
1848 { return this->find(__c, __pos); }
1849
1850 /**
1851 * @brief Find last position of a character of string.
1852 * @param __str String containing characters to locate.
1853 * @param __pos Index of character to search back from (default end).
1854 * @return Index of last occurrence.
1855 *
1856 * Starting from @a __pos, searches backward for one of the
1857 * characters of @a __str within this string. If found,
1858 * returns the index where it was found. If not found, returns
1859 * npos.
1860 */
1861 size_type
1862 find_last_of(const __versa_string& __str, size_type __pos = npos) const
1863 _GLIBCXX_NOEXCEPT
1864 { return this->find_last_of(__str.data(), __pos, __str.size()); }
1865
1866 /**
1867 * @brief Find last position of a character of C substring.
1868 * @param __s C string containing characters to locate.
1869 * @param __pos Index of character to search back from.
1870 * @param __n Number of characters from s to search for.
1871 * @return Index of last occurrence.
1872 *
1873 * Starting from @a __pos, searches backward for one of the
1874 * first @a __n characters of @a __s within this string. If
1875 * found, returns the index where it was found. If not found,
1876 * returns npos.
1877 */
1878 size_type
1879 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
1880
1881 /**
1882 * @brief Find last position of a character of C string.
1883 * @param __s C string containing characters to locate.
1884 * @param __pos Index of character to search back from (default end).
1885 * @return Index of last occurrence.
1886 *
1887 * Starting from @a __pos, searches backward for one of the
1888 * characters of @a __s within this string. If found, returns
1889 * the index where it was found. If not found, returns npos.
1890 */
1891 size_type
1892 find_last_of(const _CharT* __s, size_type __pos = npos) const
1893 {
1894 __glibcxx_requires_string(__s);
1895 return this->find_last_of(__s, __pos, traits_type::length(__s));
1896 }
1897
1898 /**
1899 * @brief Find last position of a character.
1900 * @param __c Character to locate.
1901 * @param __pos Index of character to search back from (default end).
1902 * @return Index of last occurrence.
1903 *
1904 * Starting from @a __pos, searches backward for @a __c within
1905 * this string. If found, returns the index where it was
1906 * found. If not found, returns npos.
1907 *
1908 * Note: equivalent to rfind(c, pos).
1909 */
1910 size_type
1911 find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
1912 { return this->rfind(__c, __pos); }
1913
1914 /**
1915 * @brief Find position of a character not in string.
1916 * @param __str String containing characters to avoid.
1917 * @param __pos Index of character to search from (default 0).
1918 * @return Index of first occurrence.
1919 *
1920 * Starting from @a __pos, searches forward for a character not
1921 * contained in @a __str within this string. If found, returns
1922 * the index where it was found. If not found, returns npos.
1923 */
1924 size_type
1925 find_first_not_of(const __versa_string& __str, size_type __pos = 0) const
1926 _GLIBCXX_NOEXCEPT
1927 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
1928
1929 /**
1930 * @brief Find position of a character not in C substring.
1931 * @param __s C string containing characters to avoid.
1932 * @param __pos Index of character to search from.
1933 * @param __n Number of characters from s to consider.
1934 * @return Index of first occurrence.
1935 *
1936 * Starting from @a __pos, searches forward for a character not
1937 * contained in the first @a __n characters of @a __s within
1938 * this string. If found, returns the index where it was
1939 * found. If not found, returns npos.
1940 */
1941 size_type
1942 find_first_not_of(const _CharT* __s, size_type __pos,
1943 size_type __n) const;
1944
1945 /**
1946 * @brief Find position of a character not in C string.
1947 * @param __s C string containing characters to avoid.
1948 * @param __pos Index of character to search from (default 0).
1949 * @return Index of first occurrence.
1950 *
1951 * Starting from @a __pos, searches forward for a character not
1952 * contained in @a __s within this string. If found, returns
1953 * the index where it was found. If not found, returns npos.
1954 */
1955 size_type
1956 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
1957 {
1958 __glibcxx_requires_string(__s);
1959 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
1960 }
1961
1962 /**
1963 * @brief Find position of a different character.
1964 * @param __c Character to avoid.
1965 * @param __pos Index of character to search from (default 0).
1966 * @return Index of first occurrence.
1967 *
1968 * Starting from @a __pos, searches forward for a character
1969 * other than @a __c within this string. If found, returns the
1970 * index where it was found. If not found, returns npos.
1971 */
1972 size_type
1973 find_first_not_of(_CharT __c, size_type __pos = 0) const
1974 _GLIBCXX_NOEXCEPT;
1975
1976 /**
1977 * @brief Find last position of a character not in string.
1978 * @param __str String containing characters to avoid.
1979 * @param __pos Index of character to search back from (default end).
1980 * @return Index of last occurrence.
1981 *
1982 * Starting from @a __pos, searches backward for a character
1983 * not contained in @a __str within this string. If found,
1984 * returns the index where it was found. If not found, returns
1985 * npos.
1986 */
1987 size_type
1988 find_last_not_of(const __versa_string& __str,
1989 size_type __pos = npos) const _GLIBCXX_NOEXCEPT
1990 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
1991
1992 /**
1993 * @brief Find last position of a character not in C substring.
1994 * @param __s C string containing characters to avoid.
1995 * @param __pos Index of character to search back from.
1996 * @param __n Number of characters from s to consider.
1997 * @return Index of last occurrence.
1998 *
1999 * Starting from @a __pos, searches backward for a character
2000 * not contained in the first @a __n characters of @a __s
2001 * within this string. If found, returns the index where it
2002 * was found. If not found, returns npos.
2003 */
2004 size_type
2005 find_last_not_of(const _CharT* __s, size_type __pos,
2006 size_type __n) const;
2007 /**
2008 * @brief Find last position of a character not in C string.
2009 * @param __s C string containing characters to avoid.
2010 * @param __pos Index of character to search back from (default end).
2011 * @return Index of last occurrence.
2012 *
2013 * Starting from @a __pos, searches backward for a character
2014 * not contained in @a __s within this string. If found,
2015 * returns the index where it was found. If not found, returns
2016 * npos.
2017 */
2018 size_type
2019 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
2020 {
2021 __glibcxx_requires_string(__s);
2022 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
2023 }
2024
2025 /**
2026 * @brief Find last position of a different character.
2027 * @param __c Character to avoid.
2028 * @param __pos Index of character to search back from (default end).
2029 * @return Index of last occurrence.
2030 *
2031 * Starting from @a __pos, searches backward for a character
2032 * other than @a __c within this string. If found, returns the
2033 * index where it was found. If not found, returns npos.
2034 */
2035 size_type
2036 find_last_not_of(_CharT __c, size_type __pos = npos) const
2037 _GLIBCXX_NOEXCEPT;
2038
2039 /**
2040 * @brief Get a substring.
2041 * @param __pos Index of first character (default 0).
2042 * @param __n Number of characters in substring (default remainder).
2043 * @return The new string.
2044 * @throw std::out_of_range If pos > size().
2045 *
2046 * Construct and return a new string using the @a __n
2047 * characters starting at @a __pos. If the string is too
2048 * short, use the remainder of the characters. If @a __pos is
2049 * beyond the end of the string, out_of_range is thrown.
2050 */
2051 __versa_string
2052 substr(size_type __pos = 0, size_type __n = npos) const
2053 {
2054 return __versa_string(*this, _M_check(__pos, "__versa_string::substr"),
2055 __n);
2056 }
2057
2058 /**
2059 * @brief Compare to a string.
2060 * @param __str String to compare against.
2061 * @return Integer < 0, 0, or > 0.
2062 *
2063 * Returns an integer < 0 if this string is ordered before @a
2064 * __str, 0 if their values are equivalent, or > 0 if this
2065 * string is ordered after @a __str. Determines the effective
2066 * length rlen of the strings to compare as the smallest of
2067 * size() and str.size(). The function then compares the two
2068 * strings by calling traits::compare(data(), str.data(),rlen).
2069 * If the result of the comparison is nonzero returns it,
2070 * otherwise the shorter one is ordered first.
2071 */
2072 int
2073 compare(const __versa_string& __str) const
2074 {
2075 if (this->_M_compare(__str))
2076 return 0;
2077
2078 const size_type __size = this->size();
2079 const size_type __osize = __str.size();
2080 const size_type __len = std::min(__size, __osize);
2081
2082 int __r = traits_type::compare(this->_M_data(), __str.data(), __len);
2083 if (!__r)
2084 __r = this->_S_compare(__size, __osize);
2085 return __r;
2086 }
2087
2088 /**
2089 * @brief Compare substring to a string.
2090 * @param __pos Index of first character of substring.
2091 * @param __n Number of characters in substring.
2092 * @param __str String to compare against.
2093 * @return Integer < 0, 0, or > 0.
2094 *
2095 * Form the substring of this string from the @a __n characters
2096 * starting at @a __pos. Returns an integer < 0 if the
2097 * substring is ordered before @a __str, 0 if their values are
2098 * equivalent, or > 0 if the substring is ordered after @a
2099 * __str. Determines the effective length rlen of the strings
2100 * to compare as the smallest of the length of the substring
2101 * and @a __str.size(). The function then compares the two
2102 * strings by calling
2103 * traits::compare(substring.data(),str.data(),rlen). If the
2104 * result of the comparison is nonzero returns it, otherwise
2105 * the shorter one is ordered first.
2106 */
2107 int
2108 compare(size_type __pos, size_type __n,
2109 const __versa_string& __str) const;
2110
2111 /**
2112 * @brief Compare substring to a substring.
2113 * @param __pos1 Index of first character of substring.
2114 * @param __n1 Number of characters in substring.
2115 * @param __str String to compare against.
2116 * @param __pos2 Index of first character of substring of str.
2117 * @param __n2 Number of characters in substring of str.
2118 * @return Integer < 0, 0, or > 0.
2119 *
2120 * Form the substring of this string from the @a __n1
2121 * characters starting at @a __pos1. Form the substring of @a
2122 * __str from the @a __n2 characters starting at @a __pos2.
2123 * Returns an integer < 0 if this substring is ordered before
2124 * the substring of @a __str, 0 if their values are equivalent,
2125 * or > 0 if this substring is ordered after the substring of
2126 * @a __str. Determines the effective length rlen of the
2127 * strings to compare as the smallest of the lengths of the
2128 * substrings. The function then compares the two strings by
2129 * calling
2130 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
2131 * If the result of the comparison is nonzero returns it,
2132 * otherwise the shorter one is ordered first.
2133 */
2134 int
2135 compare(size_type __pos1, size_type __n1, const __versa_string& __str,
2136 size_type __pos2, size_type __n2) const;
2137
2138 /**
2139 * @brief Compare to a C string.
2140 * @param __s C string to compare against.
2141 * @return Integer < 0, 0, or > 0.
2142 *
2143 * Returns an integer < 0 if this string is ordered before @a
2144 * __s, 0 if their values are equivalent, or > 0 if this string
2145 * is ordered after @a __s. Determines the effective length
2146 * rlen of the strings to compare as the smallest of size() and
2147 * the length of a string constructed from @a __s. The
2148 * function then compares the two strings by calling
2149 * traits::compare(data(),s,rlen). If the result of the
2150 * comparison is nonzero returns it, otherwise the shorter one
2151 * is ordered first.
2152 */
2153 int
2154 compare(const _CharT* __s) const;
2155
2156 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2157 // 5 String::compare specification questionable
2158 /**
2159 * @brief Compare substring to a C string.
2160 * @param __pos Index of first character of substring.
2161 * @param __n1 Number of characters in substring.
2162 * @param __s C string to compare against.
2163 * @return Integer < 0, 0, or > 0.
2164 *
2165 * Form the substring of this string from the @a __n1
2166 * characters starting at @a __pos. Returns an integer < 0 if
2167 * the substring is ordered before @a __s, 0 if their values
2168 * are equivalent, or > 0 if the substring is ordered after @a
2169 * __s. Determines the effective length rlen of the strings to
2170 * compare as the smallest of the length of the substring and
2171 * the length of a string constructed from @a __s. The
2172 * function then compares the two string by calling
2173 * traits::compare(substring.data(),s,rlen). If the result of
2174 * the comparison is nonzero returns it, otherwise the shorter
2175 * one is ordered first.
2176 */
2177 int
2178 compare(size_type __pos, size_type __n1, const _CharT* __s) const;
2179
2180 /**
2181 * @brief Compare substring against a character array.
2182 * @param __pos Index of first character of substring.
2183 * @param __n1 Number of characters in substring.
2184 * @param __s character array to compare against.
2185 * @param __n2 Number of characters of s.
2186 * @return Integer < 0, 0, or > 0.
2187 *
2188 * Form the substring of this string from the @a __n1
2189 * characters starting at @a __pos. Form a string from the
2190 * first @a __n2 characters of @a __s. Returns an integer < 0
2191 * if this substring is ordered before the string from @a __s,
2192 * 0 if their values are equivalent, or > 0 if this substring
2193 * is ordered after the string from @a __s. Determines the
2194 * effective length rlen of the strings to compare as the
2195 * smallest of the length of the substring and @a __n2. The
2196 * function then compares the two strings by calling
2197 * traits::compare(substring.data(),__s,rlen). If the result of
2198 * the comparison is nonzero returns it, otherwise the shorter
2199 * one is ordered first.
2200 *
2201 * NB: __s must have at least n2 characters, <em>\\0</em> has no special
2202 * meaning.
2203 */
2204 int
2205 compare(size_type __pos, size_type __n1, const _CharT* __s,
2206 size_type __n2) const;
2207 };
2208
2209 // operator+
2210 /**
2211 * @brief Concatenate two strings.
2212 * @param __lhs First string.
2213 * @param __rhs Last string.
2214 * @return New string with value of @a __lhs followed by @a __rhs.
2215 */
2216 template<typename _CharT, typename _Traits, typename _Alloc,
2217 template <typename, typename, typename> class _Base>
2218 __versa_string<_CharT, _Traits, _Alloc, _Base>
2219 operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2220 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs);
2221
2222 /**
2223 * @brief Concatenate C string and string.
2224 * @param __lhs First string.
2225 * @param __rhs Last string.
2226 * @return New string with value of @a __lhs followed by @a __rhs.
2227 */
2228 template<typename _CharT, typename _Traits, typename _Alloc,
2229 template <typename, typename, typename> class _Base>
2230 __versa_string<_CharT, _Traits, _Alloc, _Base>
2231 operator+(const _CharT* __lhs,
2232 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs);
2233
2234 /**
2235 * @brief Concatenate character and string.
2236 * @param __lhs First string.
2237 * @param __rhs Last string.
2238 * @return New string with @a __lhs followed by @a __rhs.
2239 */
2240 template<typename _CharT, typename _Traits, typename _Alloc,
2241 template <typename, typename, typename> class _Base>
2242 __versa_string<_CharT, _Traits, _Alloc, _Base>
2243 operator+(_CharT __lhs,
2244 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs);
2245
2246 /**
2247 * @brief Concatenate string and C string.
2248 * @param __lhs First string.
2249 * @param __rhs Last string.
2250 * @return New string with @a __lhs followed by @a __rhs.
2251 */
2252 template<typename _CharT, typename _Traits, typename _Alloc,
2253 template <typename, typename, typename> class _Base>
2254 __versa_string<_CharT, _Traits, _Alloc, _Base>
2255 operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2256 const _CharT* __rhs);
2257
2258 /**
2259 * @brief Concatenate string and character.
2260 * @param __lhs First string.
2261 * @param __rhs Last string.
2262 * @return New string with @a __lhs followed by @a __rhs.
2263 */
2264 template<typename _CharT, typename _Traits, typename _Alloc,
2265 template <typename, typename, typename> class _Base>
2266 __versa_string<_CharT, _Traits, _Alloc, _Base>
2267 operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2268 _CharT __rhs);
2269
2270 #if __cplusplus >= 201103L
2271 template<typename _CharT, typename _Traits, typename _Alloc,
2272 template <typename, typename, typename> class _Base>
2273 inline __versa_string<_CharT, _Traits, _Alloc, _Base>
2274 operator+(__versa_string<_CharT, _Traits, _Alloc, _Base>&& __lhs,
2275 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2276 { return std::move(__lhs.append(__rhs)); }
2277
2278 template<typename _CharT, typename _Traits, typename _Alloc,
2279 template <typename, typename, typename> class _Base>
2280 inline __versa_string<_CharT, _Traits, _Alloc, _Base>
2281 operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2282 __versa_string<_CharT, _Traits, _Alloc, _Base>&& __rhs)
2283 { return std::move(__rhs.insert(0, __lhs)); }
2284
2285 template<typename _CharT, typename _Traits, typename _Alloc,
2286 template <typename, typename, typename> class _Base>
2287 inline __versa_string<_CharT, _Traits, _Alloc, _Base>
2288 operator+(__versa_string<_CharT, _Traits, _Alloc, _Base>&& __lhs,
2289 __versa_string<_CharT, _Traits, _Alloc, _Base>&& __rhs)
2290 {
2291 const auto __size = __lhs.size() + __rhs.size();
2292 const bool __cond = (__size > __lhs.capacity()
2293 && __size <= __rhs.capacity());
2294 return __cond ? std::move(__rhs.insert(0, __lhs))
2295 : std::move(__lhs.append(__rhs));
2296 }
2297
2298 template<typename _CharT, typename _Traits, typename _Alloc,
2299 template <typename, typename, typename> class _Base>
2300 inline __versa_string<_CharT, _Traits, _Alloc, _Base>
2301 operator+(const _CharT* __lhs,
2302 __versa_string<_CharT, _Traits, _Alloc, _Base>&& __rhs)
2303 { return std::move(__rhs.insert(0, __lhs)); }
2304
2305 template<typename _CharT, typename _Traits, typename _Alloc,
2306 template <typename, typename, typename> class _Base>
2307 inline __versa_string<_CharT, _Traits, _Alloc, _Base>
2308 operator+(_CharT __lhs,
2309 __versa_string<_CharT, _Traits, _Alloc, _Base>&& __rhs)
2310 { return std::move(__rhs.insert(0, 1, __lhs)); }
2311
2312 template<typename _CharT, typename _Traits, typename _Alloc,
2313 template <typename, typename, typename> class _Base>
2314 inline __versa_string<_CharT, _Traits, _Alloc, _Base>
2315 operator+(__versa_string<_CharT, _Traits, _Alloc, _Base>&& __lhs,
2316 const _CharT* __rhs)
2317 { return std::move(__lhs.append(__rhs)); }
2318
2319 template<typename _CharT, typename _Traits, typename _Alloc,
2320 template <typename, typename, typename> class _Base>
2321 inline __versa_string<_CharT, _Traits, _Alloc, _Base>
2322 operator+(__versa_string<_CharT, _Traits, _Alloc, _Base>&& __lhs,
2323 _CharT __rhs)
2324 { return std::move(__lhs.append(1, __rhs)); }
2325 #endif
2326
2327 // operator ==
2328 /**
2329 * @brief Test equivalence of two strings.
2330 * @param __lhs First string.
2331 * @param __rhs Second string.
2332 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
2333 */
2334 template<typename _CharT, typename _Traits, typename _Alloc,
2335 template <typename, typename, typename> class _Base>
2336 inline bool
2337 operator==(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2338 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2339 { return __lhs.compare(__rhs) == 0; }
2340
2341 template<typename _CharT,
2342 template <typename, typename, typename> class _Base>
2343 inline typename __enable_if<std::__is_char<_CharT>::__value, bool>::__type
2344 operator==(const __versa_string<_CharT, std::char_traits<_CharT>,
2345 std::allocator<_CharT>, _Base>& __lhs,
2346 const __versa_string<_CharT, std::char_traits<_CharT>,
2347 std::allocator<_CharT>, _Base>& __rhs)
2348 { return (__lhs.size() == __rhs.size()
2349 && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
2350 __lhs.size())); }
2351
2352 /**
2353 * @brief Test equivalence of C string and string.
2354 * @param __lhs C string.
2355 * @param __rhs String.
2356 * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
2357 */
2358 template<typename _CharT, typename _Traits, typename _Alloc,
2359 template <typename, typename, typename> class _Base>
2360 inline bool
2361 operator==(const _CharT* __lhs,
2362 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2363 { return __rhs.compare(__lhs) == 0; }
2364
2365 /**
2366 * @brief Test equivalence of string and C string.
2367 * @param __lhs String.
2368 * @param __rhs C string.
2369 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
2370 */
2371 template<typename _CharT, typename _Traits, typename _Alloc,
2372 template <typename, typename, typename> class _Base>
2373 inline bool
2374 operator==(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2375 const _CharT* __rhs)
2376 { return __lhs.compare(__rhs) == 0; }
2377
2378 // operator !=
2379 /**
2380 * @brief Test difference of two strings.
2381 * @param __lhs First string.
2382 * @param __rhs Second string.
2383 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
2384 */
2385 template<typename _CharT, typename _Traits, typename _Alloc,
2386 template <typename, typename, typename> class _Base>
2387 inline bool
2388 operator!=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2389 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2390 { return !(__lhs == __rhs); }
2391
2392 /**
2393 * @brief Test difference of C string and string.
2394 * @param __lhs C string.
2395 * @param __rhs String.
2396 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
2397 */
2398 template<typename _CharT, typename _Traits, typename _Alloc,
2399 template <typename, typename, typename> class _Base>
2400 inline bool
2401 operator!=(const _CharT* __lhs,
2402 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2403 { return !(__lhs == __rhs); }
2404
2405 /**
2406 * @brief Test difference of string and C string.
2407 * @param __lhs String.
2408 * @param __rhs C string.
2409 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
2410 */
2411 template<typename _CharT, typename _Traits, typename _Alloc,
2412 template <typename, typename, typename> class _Base>
2413 inline bool
2414 operator!=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2415 const _CharT* __rhs)
2416 { return !(__lhs == __rhs); }
2417
2418 // operator <
2419 /**
2420 * @brief Test if string precedes string.
2421 * @param __lhs First string.
2422 * @param __rhs Second string.
2423 * @return True if @a __lhs precedes @a __rhs. False otherwise.
2424 */
2425 template<typename _CharT, typename _Traits, typename _Alloc,
2426 template <typename, typename, typename> class _Base>
2427 inline bool
2428 operator<(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2429 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2430 { return __lhs.compare(__rhs) < 0; }
2431
2432 /**
2433 * @brief Test if string precedes C string.
2434 * @param __lhs String.
2435 * @param __rhs C string.
2436 * @return True if @a __lhs precedes @a __rhs. False otherwise.
2437 */
2438 template<typename _CharT, typename _Traits, typename _Alloc,
2439 template <typename, typename, typename> class _Base>
2440 inline bool
2441 operator<(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2442 const _CharT* __rhs)
2443 { return __lhs.compare(__rhs) < 0; }
2444
2445 /**
2446 * @brief Test if C string precedes string.
2447 * @param __lhs C string.
2448 * @param __rhs String.
2449 * @return True if @a __lhs precedes @a __rhs. False otherwise.
2450 */
2451 template<typename _CharT, typename _Traits, typename _Alloc,
2452 template <typename, typename, typename> class _Base>
2453 inline bool
2454 operator<(const _CharT* __lhs,
2455 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2456 { return __rhs.compare(__lhs) > 0; }
2457
2458 // operator >
2459 /**
2460 * @brief Test if string follows string.
2461 * @param __lhs First string.
2462 * @param __rhs Second string.
2463 * @return True if @a __lhs follows @a __rhs. False otherwise.
2464 */
2465 template<typename _CharT, typename _Traits, typename _Alloc,
2466 template <typename, typename, typename> class _Base>
2467 inline bool
2468 operator>(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2469 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2470 { return __lhs.compare(__rhs) > 0; }
2471
2472 /**
2473 * @brief Test if string follows C string.
2474 * @param __lhs String.
2475 * @param __rhs C string.
2476 * @return True if @a __lhs follows @a __rhs. False otherwise.
2477 */
2478 template<typename _CharT, typename _Traits, typename _Alloc,
2479 template <typename, typename, typename> class _Base>
2480 inline bool
2481 operator>(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2482 const _CharT* __rhs)
2483 { return __lhs.compare(__rhs) > 0; }
2484
2485 /**
2486 * @brief Test if C string follows string.
2487 * @param __lhs C string.
2488 * @param __rhs String.
2489 * @return True if @a __lhs follows @a __rhs. False otherwise.
2490 */
2491 template<typename _CharT, typename _Traits, typename _Alloc,
2492 template <typename, typename, typename> class _Base>
2493 inline bool
2494 operator>(const _CharT* __lhs,
2495 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2496 { return __rhs.compare(__lhs) < 0; }
2497
2498 // operator <=
2499 /**
2500 * @brief Test if string doesn't follow string.
2501 * @param __lhs First string.
2502 * @param __rhs Second string.
2503 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
2504 */
2505 template<typename _CharT, typename _Traits, typename _Alloc,
2506 template <typename, typename, typename> class _Base>
2507 inline bool
2508 operator<=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2509 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2510 { return __lhs.compare(__rhs) <= 0; }
2511
2512 /**
2513 * @brief Test if string doesn't follow C string.
2514 * @param __lhs String.
2515 * @param __rhs C string.
2516 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
2517 */
2518 template<typename _CharT, typename _Traits, typename _Alloc,
2519 template <typename, typename, typename> class _Base>
2520 inline bool
2521 operator<=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2522 const _CharT* __rhs)
2523 { return __lhs.compare(__rhs) <= 0; }
2524
2525 /**
2526 * @brief Test if C string doesn't follow string.
2527 * @param __lhs C string.
2528 * @param __rhs String.
2529 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
2530 */
2531 template<typename _CharT, typename _Traits, typename _Alloc,
2532 template <typename, typename, typename> class _Base>
2533 inline bool
2534 operator<=(const _CharT* __lhs,
2535 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2536 { return __rhs.compare(__lhs) >= 0; }
2537
2538 // operator >=
2539 /**
2540 * @brief Test if string doesn't precede string.
2541 * @param __lhs First string.
2542 * @param __rhs Second string.
2543 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
2544 */
2545 template<typename _CharT, typename _Traits, typename _Alloc,
2546 template <typename, typename, typename> class _Base>
2547 inline bool
2548 operator>=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2549 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2550 { return __lhs.compare(__rhs) >= 0; }
2551
2552 /**
2553 * @brief Test if string doesn't precede C string.
2554 * @param __lhs String.
2555 * @param __rhs C string.
2556 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
2557 */
2558 template<typename _CharT, typename _Traits, typename _Alloc,
2559 template <typename, typename, typename> class _Base>
2560 inline bool
2561 operator>=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2562 const _CharT* __rhs)
2563 { return __lhs.compare(__rhs) >= 0; }
2564
2565 /**
2566 * @brief Test if C string doesn't precede string.
2567 * @param __lhs C string.
2568 * @param __rhs String.
2569 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
2570 */
2571 template<typename _CharT, typename _Traits, typename _Alloc,
2572 template <typename, typename, typename> class _Base>
2573 inline bool
2574 operator>=(const _CharT* __lhs,
2575 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2576 { return __rhs.compare(__lhs) <= 0; }
2577
2578 /**
2579 * @brief Swap contents of two strings.
2580 * @param __lhs First string.
2581 * @param __rhs Second string.
2582 *
2583 * Exchanges the contents of @a __lhs and @a __rhs in constant time.
2584 */
2585 template<typename _CharT, typename _Traits, typename _Alloc,
2586 template <typename, typename, typename> class _Base>
2587 inline void
2588 swap(__versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2589 __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2590 { __lhs.swap(__rhs); }
2591
2592 _GLIBCXX_END_NAMESPACE_VERSION
2593 } // namespace
2594
2595 namespace std _GLIBCXX_VISIBILITY(default)
2596 {
2597 _GLIBCXX_BEGIN_NAMESPACE_VERSION
2598
2599 /**
2600 * @brief Read stream into a string.
2601 * @param __is Input stream.
2602 * @param __str Buffer to store into.
2603 * @return Reference to the input stream.
2604 *
2605 * Stores characters from @a __is into @a __str until whitespace is
2606 * found, the end of the stream is encountered, or str.max_size()
2607 * is reached. If is.width() is non-zero, that is the limit on the
2608 * number of characters stored into @a __str. Any previous
2609 * contents of @a __str are erased.
2610 */
2611 template<typename _CharT, typename _Traits, typename _Alloc,
2612 template <typename, typename, typename> class _Base>
2613 basic_istream<_CharT, _Traits>&
2614 operator>>(basic_istream<_CharT, _Traits>& __is,
2615 __gnu_cxx::__versa_string<_CharT, _Traits,
2616 _Alloc, _Base>& __str);
2617
2618 /**
2619 * @brief Write string to a stream.
2620 * @param __os Output stream.
2621 * @param __str String to write out.
2622 * @return Reference to the output stream.
2623 *
2624 * Output characters of @a __str into os following the same rules as for
2625 * writing a C string.
2626 */
2627 template<typename _CharT, typename _Traits, typename _Alloc,
2628 template <typename, typename, typename> class _Base>
2629 inline basic_ostream<_CharT, _Traits>&
2630 operator<<(basic_ostream<_CharT, _Traits>& __os,
2631 const __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc,
2632 _Base>& __str)
2633 {
2634 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2635 // 586. string inserter not a formatted function
2636 return __ostream_insert(__os, __str.data(), __str.size());
2637 }
2638
2639 /**
2640 * @brief Read a line from stream into a string.
2641 * @param __is Input stream.
2642 * @param __str Buffer to store into.
2643 * @param __delim Character marking end of line.
2644 * @return Reference to the input stream.
2645 *
2646 * Stores characters from @a __is into @a __str until @a __delim is
2647 * found, the end of the stream is encountered, or str.max_size()
2648 * is reached. If is.width() is non-zero, that is the limit on the
2649 * number of characters stored into @a __str. Any previous
2650 * contents of @a __str are erased. If @a delim was encountered,
2651 * it is extracted but not stored into @a __str.
2652 */
2653 template<typename _CharT, typename _Traits, typename _Alloc,
2654 template <typename, typename, typename> class _Base>
2655 basic_istream<_CharT, _Traits>&
2656 getline(basic_istream<_CharT, _Traits>& __is,
2657 __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base>& __str,
2658 _CharT __delim);
2659
2660 /**
2661 * @brief Read a line from stream into a string.
2662 * @param __is Input stream.
2663 * @param __str Buffer to store into.
2664 * @return Reference to the input stream.
2665 *
2666 * Stores characters from is into @a __str until &apos;\n&apos; is
2667 * found, the end of the stream is encountered, or str.max_size()
2668 * is reached. If is.width() is non-zero, that is the limit on the
2669 * number of characters stored into @a __str. Any previous
2670 * contents of @a __str are erased. If end of line was
2671 * encountered, it is extracted but not stored into @a __str.
2672 */
2673 template<typename _CharT, typename _Traits, typename _Alloc,
2674 template <typename, typename, typename> class _Base>
2675 inline basic_istream<_CharT, _Traits>&
2676 getline(basic_istream<_CharT, _Traits>& __is,
2677 __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base>& __str)
2678 { return getline(__is, __str, __is.widen('\n')); }
2679
2680 _GLIBCXX_END_NAMESPACE_VERSION
2681 } // namespace
2682
2683 #if __cplusplus >= 201103L
2684
2685 #include <ext/string_conversions.h>
2686
2687 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
2688 {
2689 _GLIBCXX_BEGIN_NAMESPACE_VERSION
2690
2691 #if _GLIBCXX_USE_C99_STDLIB
2692 // 21.4 Numeric Conversions [string.conversions].
2693 inline int
2694 stoi(const __vstring& __str, std::size_t* __idx = 0, int __base = 10)
2695 { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
2696 __idx, __base); }
2697
2698 inline long
2699 stol(const __vstring& __str, std::size_t* __idx = 0, int __base = 10)
2700 { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
2701 __idx, __base); }
2702
2703 inline unsigned long
2704 stoul(const __vstring& __str, std::size_t* __idx = 0, int __base = 10)
2705 { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
2706 __idx, __base); }
2707
2708 inline long long
2709 stoll(const __vstring& __str, std::size_t* __idx = 0, int __base = 10)
2710 { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
2711 __idx, __base); }
2712
2713 inline unsigned long long
2714 stoull(const __vstring& __str, std::size_t* __idx, int __base = 10)
2715 { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
2716 __idx, __base); }
2717
2718 // NB: strtof vs strtod.
2719 inline float
2720 stof(const __vstring& __str, std::size_t* __idx = 0)
2721 { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
2722
2723 inline double
2724 stod(const __vstring& __str, std::size_t* __idx = 0)
2725 { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
2726
2727 inline long double
2728 stold(const __vstring& __str, std::size_t* __idx = 0)
2729 { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
2730 #endif // _GLIBCXX_USE_C99_STDLIB
2731
2732 #if _GLIBCXX_USE_C99_STDIO
2733 // NB: (v)snprintf vs sprintf.
2734
2735 // DR 1261.
2736 inline __vstring
2737 to_string(int __val)
2738 { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf, 4 * sizeof(int),
2739 "%d", __val); }
2740
2741 inline __vstring
2742 to_string(unsigned __val)
2743 { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf,
2744 4 * sizeof(unsigned),
2745 "%u", __val); }
2746
2747 inline __vstring
2748 to_string(long __val)
2749 { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf,
2750 4 * sizeof(long),
2751 "%ld", __val); }
2752
2753 inline __vstring
2754 to_string(unsigned long __val)
2755 { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf,
2756 4 * sizeof(unsigned long),
2757 "%lu", __val); }
2758
2759
2760 inline __vstring
2761 to_string(long long __val)
2762 { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf,
2763 4 * sizeof(long long),
2764 "%lld", __val); }
2765
2766 inline __vstring
2767 to_string(unsigned long long __val)
2768 { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf,
2769 4 * sizeof(unsigned long long),
2770 "%llu", __val); }
2771
2772 inline __vstring
2773 to_string(float __val)
2774 {
2775 const int __n = __numeric_traits<float>::__max_exponent10 + 20;
2776 return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf, __n,
2777 "%f", __val);
2778 }
2779
2780 inline __vstring
2781 to_string(double __val)
2782 {
2783 const int __n = __numeric_traits<double>::__max_exponent10 + 20;
2784 return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf, __n,
2785 "%f", __val);
2786 }
2787
2788 inline __vstring
2789 to_string(long double __val)
2790 {
2791 const int __n = __numeric_traits<long double>::__max_exponent10 + 20;
2792 return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf, __n,
2793 "%Lf", __val);
2794 }
2795 #endif // _GLIBCXX_USE_C99_STDIO
2796
2797 #if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
2798 inline int
2799 stoi(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10)
2800 { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
2801 __idx, __base); }
2802
2803 inline long
2804 stol(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10)
2805 { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
2806 __idx, __base); }
2807
2808 inline unsigned long
2809 stoul(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10)
2810 { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
2811 __idx, __base); }
2812
2813 inline long long
2814 stoll(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10)
2815 { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
2816 __idx, __base); }
2817
2818 inline unsigned long long
2819 stoull(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10)
2820 { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
2821 __idx, __base); }
2822
2823 // NB: wcstof vs wcstod.
2824 inline float
2825 stof(const __wvstring& __str, std::size_t* __idx = 0)
2826 { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
2827
2828 inline double
2829 stod(const __wvstring& __str, std::size_t* __idx = 0)
2830 { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
2831
2832 inline long double
2833 stold(const __wvstring& __str, std::size_t* __idx = 0)
2834 { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
2835
2836 #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
2837 // DR 1261.
2838 inline __wvstring
2839 to_wstring(int __val)
2840 { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
2841 4 * sizeof(int),
2842 L"%d", __val); }
2843
2844 inline __wvstring
2845 to_wstring(unsigned __val)
2846 { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
2847 4 * sizeof(unsigned),
2848 L"%u", __val); }
2849
2850 inline __wvstring
2851 to_wstring(long __val)
2852 { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
2853 4 * sizeof(long),
2854 L"%ld", __val); }
2855
2856 inline __wvstring
2857 to_wstring(unsigned long __val)
2858 { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
2859 4 * sizeof(unsigned long),
2860 L"%lu", __val); }
2861
2862 inline __wvstring
2863 to_wstring(long long __val)
2864 { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
2865 4 * sizeof(long long),
2866 L"%lld", __val); }
2867
2868 inline __wvstring
2869 to_wstring(unsigned long long __val)
2870 { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
2871 4 * sizeof(unsigned long long),
2872 L"%llu", __val); }
2873
2874 inline __wvstring
2875 to_wstring(float __val)
2876 {
2877 const int __n = __numeric_traits<float>::__max_exponent10 + 20;
2878 return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf, __n,
2879 L"%f", __val);
2880 }
2881
2882 inline __wvstring
2883 to_wstring(double __val)
2884 {
2885 const int __n = __numeric_traits<double>::__max_exponent10 + 20;
2886 return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf, __n,
2887 L"%f", __val);
2888 }
2889
2890 inline __wvstring
2891 to_wstring(long double __val)
2892 {
2893 const int __n = __numeric_traits<long double>::__max_exponent10 + 20;
2894 return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf, __n,
2895 L"%Lf", __val);
2896 }
2897 #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
2898 #endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR
2899
2900 _GLIBCXX_END_NAMESPACE_VERSION
2901 } // namespace
2902
2903 #endif
2904
2905 #if __cplusplus >= 201103L
2906
2907 #include <bits/functional_hash.h>
2908
2909 namespace std _GLIBCXX_VISIBILITY(default)
2910 {
2911 _GLIBCXX_BEGIN_NAMESPACE_VERSION
2912
2913 /// std::hash specialization for __vstring.
2914 template<>
2915 struct hash<__gnu_cxx::__vstring>
2916 : public __hash_base<size_t, __gnu_cxx::__vstring>
2917 {
2918 size_t
2919 operator()(const __gnu_cxx::__vstring& __s) const noexcept
2920 { return std::_Hash_impl::hash(__s.data(), __s.length()); }
2921 };
2922
2923 #ifdef _GLIBCXX_USE_WCHAR_T
2924 /// std::hash specialization for __wvstring.
2925 template<>
2926 struct hash<__gnu_cxx::__wvstring>
2927 : public __hash_base<size_t, __gnu_cxx::__wvstring>
2928 {
2929 size_t
2930 operator()(const __gnu_cxx::__wvstring& __s) const noexcept
2931 { return std::_Hash_impl::hash(__s.data(),
2932 __s.length() * sizeof(wchar_t)); }
2933 };
2934 #endif
2935
2936 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
2937 /// std::hash specialization for __u16vstring.
2938 template<>
2939 struct hash<__gnu_cxx::__u16vstring>
2940 : public __hash_base<size_t, __gnu_cxx::__u16vstring>
2941 {
2942 size_t
2943 operator()(const __gnu_cxx::__u16vstring& __s) const noexcept
2944 { return std::_Hash_impl::hash(__s.data(),
2945 __s.length() * sizeof(char16_t)); }
2946 };
2947
2948 /// std::hash specialization for __u32vstring.
2949 template<>
2950 struct hash<__gnu_cxx::__u32vstring>
2951 : public __hash_base<size_t, __gnu_cxx::__u32vstring>
2952 {
2953 size_t
2954 operator()(const __gnu_cxx::__u32vstring& __s) const noexcept
2955 { return std::_Hash_impl::hash(__s.data(),
2956 __s.length() * sizeof(char32_t)); }
2957 };
2958 #endif
2959
2960 _GLIBCXX_END_NAMESPACE_VERSION
2961 } // namespace
2962
2963 #endif // C++11
2964
2965 #include "vstring.tcc"
2966
2967 #endif /* _VSTRING_H */