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