1 // Components for manipulating sequences of characters -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009, 2010, 2011
5 // Free Software Foundation, Inc.
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)
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.
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.
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/>.
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}
33 // ISO C++ 14882: 21 Strings library
36 #ifndef _BASIC_STRING_H
37 #define _BASIC_STRING_H 1
39 #pragma GCC system_header
41 #include <ext/atomicity.h>
42 #include <debug/debug.h>
43 #include <initializer_list>
45 namespace std _GLIBCXX_VISIBILITY(default)
47 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 * @class basic_string basic_string.h <string>
51 * @brief Managing sequences of characters and character-like objects.
56 * Meets the requirements of a <a href="tables.html#65">container</a>, a
57 * <a href="tables.html#66">reversible container</a>, and a
58 * <a href="tables.html#67">sequence</a>. Of the
59 * <a href="tables.html#68">optional sequence requirements</a>, only
60 * @c push_back, @c at, and @c %array access are supported.
65 * Documentation? What's that?
66 * Nathan Myers <ncm@cantrip.org>.
68 * A string looks like this:
73 * [basic_string<char_type>] _M_capacity
74 * _M_dataplus _M_refcount
75 * _M_p ----------------> unnamed array of char_type
78 * Where the _M_p points to the first character in the string, and
79 * you cast it to a pointer-to-_Rep and subtract 1 to get a
80 * pointer to the header.
82 * This approach has the enormous advantage that a string object
83 * requires only one allocation. All the ugliness is confined
84 * within a single %pair of inline functions, which each compile to
85 * a single @a add instruction: _Rep::_M_data(), and
86 * string::_M_rep(); and the allocation function which gets a
87 * block of raw bytes and with room enough and constructs a _Rep
88 * object at the front.
90 * The reason you want _M_data pointing to the character %array and
91 * not the _Rep is so that the debugger can see the string
92 * contents. (Probably we should add a non-inline member to get
93 * the _Rep for the debugger to use, so users can check the actual
96 * Note that the _Rep object is a POD so that you can have a
97 * static <em>empty string</em> _Rep object already @a constructed before
98 * static constructors have run. The reference-count encoding is
99 * chosen so that a 0 indicates one reference, so you never try to
100 * destroy the empty-string _Rep object.
102 * All but the last paragraph is considered pretty conventional
103 * for a C++ string implementation.
105 // 21.3 Template class basic_string
106 template<typename _CharT, typename _Traits, typename _Alloc>
109 typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
113 typedef _Traits traits_type;
114 typedef typename _Traits::char_type value_type;
115 typedef _Alloc allocator_type;
116 typedef typename _CharT_alloc_type::size_type size_type;
117 typedef typename _CharT_alloc_type::difference_type difference_type;
118 typedef typename _CharT_alloc_type::reference reference;
119 typedef typename _CharT_alloc_type::const_reference const_reference;
120 typedef typename _CharT_alloc_type::pointer pointer;
121 typedef typename _CharT_alloc_type::const_pointer const_pointer;
122 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
123 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
125 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
126 typedef std::reverse_iterator<iterator> reverse_iterator;
129 // _Rep: string representation
131 // 1. String really contains _M_length + 1 characters: due to 21.3.4
132 // must be kept null-terminated.
133 // 2. _M_capacity >= _M_length
134 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
135 // 3. _M_refcount has three states:
136 // -1: leaked, one reference, no ref-copies allowed, non-const.
137 // 0: one reference, non-const.
138 // n>0: n + 1 references, operations require a lock, const.
139 // 4. All fields==0 is an empty string, given the extra storage
140 // beyond-the-end for a null terminator; thus, the shared
141 // empty string representation needs no constructor.
146 size_type _M_capacity;
147 _Atomic_word _M_refcount;
150 struct _Rep : _Rep_base
153 typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc;
155 // (Public) Data members:
157 // The maximum number of individual char_type elements of an
158 // individual string is determined by _S_max_size. This is the
159 // value that will be returned by max_size(). (Whereas npos
160 // is the maximum number of bytes the allocator can allocate.)
161 // If one was to divvy up the theoretical largest size string,
162 // with a terminating character and m _CharT elements, it'd
164 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
166 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
167 // In addition, this implementation quarters this amount.
168 static const size_type _S_max_size;
169 static const _CharT _S_terminal;
171 // The following storage is init'd to 0 by the linker, resulting
172 // (carefully) in an empty string with one reference.
173 static size_type _S_empty_rep_storage[];
178 // NB: Mild hack to avoid strict-aliasing warnings. Note that
179 // _S_empty_rep_storage is never modified and the punning should
180 // be reasonably safe in this case.
181 void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage);
182 return *reinterpret_cast<_Rep*>(__p);
187 { return this->_M_refcount < 0; }
191 { return this->_M_refcount > 0; }
195 { this->_M_refcount = -1; }
199 { this->_M_refcount = 0; }
202 _M_set_length_and_sharable(size_type __n)
204 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
205 if (__builtin_expect(this != &_S_empty_rep(), false))
208 this->_M_set_sharable(); // One reference.
209 this->_M_length = __n;
210 traits_type::assign(this->_M_refdata()[__n], _S_terminal);
211 // grrr. (per 21.3.4)
212 // You cannot leave those LWG people alone for a second.
218 { return reinterpret_cast<_CharT*>(this + 1); }
221 _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
223 return (!_M_is_leaked() && __alloc1 == __alloc2)
224 ? _M_refcopy() : _M_clone(__alloc1);
229 _S_create(size_type, size_type, const _Alloc&);
232 _M_dispose(const _Alloc& __a)
234 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
235 if (__builtin_expect(this != &_S_empty_rep(), false))
238 // Be race-detector-friendly. For more info see bits/c++config.
239 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount);
240 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
243 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount);
250 _M_destroy(const _Alloc&) throw();
255 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
256 if (__builtin_expect(this != &_S_empty_rep(), false))
258 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
263 _M_clone(const _Alloc&, size_type __res = 0);
266 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
267 struct _Alloc_hider : _Alloc
269 _Alloc_hider(_CharT* __dat, const _Alloc& __a)
270 : _Alloc(__a), _M_p(__dat) { }
272 _CharT* _M_p; // The actual data.
276 // Data Members (public):
277 // NB: This is an unsigned type, and thus represents the maximum
278 // size that the allocator can hold.
279 /// Value returned by various member functions when they fail.
280 static const size_type npos = static_cast<size_type>(-1);
283 // Data Members (private):
284 mutable _Alloc_hider _M_dataplus;
288 { return _M_dataplus._M_p; }
292 { return (_M_dataplus._M_p = __p); }
296 { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
298 // For the internal use we have functions similar to `begin'/`end'
299 // but they do not call _M_leak.
302 { return iterator(_M_data()); }
306 { return iterator(_M_data() + this->size()); }
309 _M_leak() // for use in begin() & non-const op[]
311 if (!_M_rep()->_M_is_leaked())
316 _M_check(size_type __pos, const char* __s) const
318 if (__pos > this->size())
319 __throw_out_of_range(__N(__s));
324 _M_check_length(size_type __n1, size_type __n2, const char* __s) const
326 if (this->max_size() - (this->size() - __n1) < __n2)
327 __throw_length_error(__N(__s));
330 // NB: _M_limit doesn't check for a bad __pos value.
332 _M_limit(size_type __pos, size_type __off) const
334 const bool __testoff = __off < this->size() - __pos;
335 return __testoff ? __off : this->size() - __pos;
338 // True if _Rep and source do not overlap.
340 _M_disjunct(const _CharT* __s) const
342 return (less<const _CharT*>()(__s, _M_data())
343 || less<const _CharT*>()(_M_data() + this->size(), __s));
346 // When __n = 1 way faster than the general multichar
347 // traits_type::copy/move/assign.
349 _M_copy(_CharT* __d, const _CharT* __s, size_type __n)
352 traits_type::assign(*__d, *__s);
354 traits_type::copy(__d, __s, __n);
358 _M_move(_CharT* __d, const _CharT* __s, size_type __n)
361 traits_type::assign(*__d, *__s);
363 traits_type::move(__d, __s, __n);
367 _M_assign(_CharT* __d, size_type __n, _CharT __c)
370 traits_type::assign(*__d, __c);
372 traits_type::assign(__d, __n, __c);
375 // _S_copy_chars is a separate template to permit specialization
376 // to optimize for the common case of pointers as iterators.
377 template<class _Iterator>
379 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
381 for (; __k1 != __k2; ++__k1, ++__p)
382 traits_type::assign(*__p, *__k1); // These types are off.
386 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2)
387 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
390 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
391 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
394 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
395 { _M_copy(__p, __k1, __k2 - __k1); }
398 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
399 { _M_copy(__p, __k1, __k2 - __k1); }
402 _S_compare(size_type __n1, size_type __n2)
404 const difference_type __d = difference_type(__n1 - __n2);
406 if (__d > __gnu_cxx::__numeric_traits<int>::__max)
407 return __gnu_cxx::__numeric_traits<int>::__max;
408 else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
409 return __gnu_cxx::__numeric_traits<int>::__min;
415 _M_mutate(size_type __pos, size_type __len1, size_type __len2);
422 { return _Rep::_S_empty_rep(); }
425 // Construct/copy/destroy:
426 // NB: We overload ctors in some cases instead of using default
427 // arguments, per 17.4.4.4 para. 2 item 2.
430 * @brief Default constructor creates an empty string.
433 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
434 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
436 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()){ }
440 * @brief Construct an empty string using allocator @a a.
443 basic_string(const _Alloc& __a);
445 // NB: per LWG issue 42, semantics different from IS:
447 * @brief Construct string with copy of value of @a str.
448 * @param str Source string.
450 basic_string(const basic_string& __str);
452 * @brief Construct string as copy of a substring.
453 * @param str Source string.
454 * @param pos Index of first character to copy from.
455 * @param n Number of characters to copy (default remainder).
457 basic_string(const basic_string& __str, size_type __pos,
458 size_type __n = npos);
460 * @brief Construct string as copy of a substring.
461 * @param str Source string.
462 * @param pos Index of first character to copy from.
463 * @param n Number of characters to copy.
464 * @param a Allocator to use.
466 basic_string(const basic_string& __str, size_type __pos,
467 size_type __n, const _Alloc& __a);
470 * @brief Construct string initialized by a character %array.
471 * @param s Source character %array.
472 * @param n Number of characters to copy.
473 * @param a Allocator to use (default is default allocator).
475 * NB: @a s must have at least @a n characters, '\\0'
476 * has no special meaning.
478 basic_string(const _CharT* __s, size_type __n,
479 const _Alloc& __a = _Alloc());
481 * @brief Construct string as copy of a C string.
482 * @param s Source C string.
483 * @param a Allocator to use (default is default allocator).
485 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
487 * @brief Construct string as multiple characters.
488 * @param n Number of characters.
489 * @param c Character to use.
490 * @param a Allocator to use (default is default allocator).
492 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
494 #ifdef __GXX_EXPERIMENTAL_CXX0X__
496 * @brief Move construct string.
497 * @param str Source string.
499 * The newly-created string contains the exact contents of @a str.
500 * @a str is a valid, but unspecified string.
502 basic_string(basic_string&& __str) noexcept
503 : _M_dataplus(__str._M_dataplus)
505 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
506 __str._M_data(_S_empty_rep()._M_refdata());
508 __str._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
513 * @brief Construct string from an initializer %list.
514 * @param l std::initializer_list of characters.
515 * @param a Allocator to use (default is default allocator).
517 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc());
518 #endif // __GXX_EXPERIMENTAL_CXX0X__
521 * @brief Construct string as copy of a range.
522 * @param beg Start of range.
523 * @param end End of range.
524 * @param a Allocator to use (default is default allocator).
526 template<class _InputIterator>
527 basic_string(_InputIterator __beg, _InputIterator __end,
528 const _Alloc& __a = _Alloc());
531 * @brief Destroy the string instance.
533 ~basic_string() _GLIBCXX_NOEXCEPT
534 { _M_rep()->_M_dispose(this->get_allocator()); }
537 * @brief Assign the value of @a str to this string.
538 * @param str Source string.
541 operator=(const basic_string& __str)
542 { return this->assign(__str); }
545 * @brief Copy contents of @a s into this string.
546 * @param s Source null-terminated string.
549 operator=(const _CharT* __s)
550 { return this->assign(__s); }
553 * @brief Set value to string of length 1.
554 * @param c Source character.
556 * Assigning to a character makes this string length 1 and
557 * (*this)[0] == @a c.
560 operator=(_CharT __c)
562 this->assign(1, __c);
566 #ifdef __GXX_EXPERIMENTAL_CXX0X__
568 * @brief Move assign the value of @a str to this string.
569 * @param str Source string.
571 * The contents of @a str are moved into this string (without copying).
572 * @a str is a valid, but unspecified string.
575 operator=(basic_string&& __str)
583 * @brief Set value to string constructed from initializer %list.
584 * @param l std::initializer_list.
587 operator=(initializer_list<_CharT> __l)
589 this->assign(__l.begin(), __l.size());
592 #endif // __GXX_EXPERIMENTAL_CXX0X__
596 * Returns a read/write iterator that points to the first character in
597 * the %string. Unshares the string.
600 begin() _GLIBCXX_NOEXCEPT
603 return iterator(_M_data());
607 * Returns a read-only (constant) iterator that points to the first
608 * character in the %string.
611 begin() const _GLIBCXX_NOEXCEPT
612 { return const_iterator(_M_data()); }
615 * Returns a read/write iterator that points one past the last
616 * character in the %string. Unshares the string.
619 end() _GLIBCXX_NOEXCEPT
622 return iterator(_M_data() + this->size());
626 * Returns a read-only (constant) iterator that points one past the
627 * last character in the %string.
630 end() const _GLIBCXX_NOEXCEPT
631 { return const_iterator(_M_data() + this->size()); }
634 * Returns a read/write reverse iterator that points to the last
635 * character in the %string. Iteration is done in reverse element
636 * order. Unshares the string.
639 rbegin() _GLIBCXX_NOEXCEPT
640 { return reverse_iterator(this->end()); }
643 * Returns a read-only (constant) reverse iterator that points
644 * to the last character in the %string. Iteration is done in
645 * reverse element order.
647 const_reverse_iterator
648 rbegin() const _GLIBCXX_NOEXCEPT
649 { return const_reverse_iterator(this->end()); }
652 * Returns a read/write reverse iterator that points to one before the
653 * first character in the %string. Iteration is done in reverse
654 * element order. Unshares the string.
657 rend() _GLIBCXX_NOEXCEPT
658 { return reverse_iterator(this->begin()); }
661 * Returns a read-only (constant) reverse iterator that points
662 * to one before the first character in the %string. Iteration
663 * is done in reverse element order.
665 const_reverse_iterator
666 rend() const _GLIBCXX_NOEXCEPT
667 { return const_reverse_iterator(this->begin()); }
669 #ifdef __GXX_EXPERIMENTAL_CXX0X__
671 * Returns a read-only (constant) iterator that points to the first
672 * character in the %string.
675 cbegin() const noexcept
676 { return const_iterator(this->_M_data()); }
679 * Returns a read-only (constant) iterator that points one past the
680 * last character in the %string.
683 cend() const noexcept
684 { return const_iterator(this->_M_data() + this->size()); }
687 * Returns a read-only (constant) reverse iterator that points
688 * to the last character in the %string. Iteration is done in
689 * reverse element order.
691 const_reverse_iterator
692 crbegin() const noexcept
693 { return const_reverse_iterator(this->end()); }
696 * Returns a read-only (constant) reverse iterator that points
697 * to one before the first character in the %string. Iteration
698 * is done in reverse element order.
700 const_reverse_iterator
701 crend() const noexcept
702 { return const_reverse_iterator(this->begin()); }
707 /// Returns the number of characters in the string, not including any
708 /// null-termination.
710 size() const _GLIBCXX_NOEXCEPT
711 { return _M_rep()->_M_length; }
713 /// Returns the number of characters in the string, not including any
714 /// null-termination.
716 length() const _GLIBCXX_NOEXCEPT
717 { return _M_rep()->_M_length; }
719 /// Returns the size() of the largest possible %string.
721 max_size() const _GLIBCXX_NOEXCEPT
722 { return _Rep::_S_max_size; }
725 * @brief Resizes the %string to the specified number of characters.
726 * @param n Number of characters the %string should contain.
727 * @param c Character to fill any new elements.
729 * This function will %resize the %string to the specified
730 * number of characters. If the number is smaller than the
731 * %string's current size the %string is truncated, otherwise
732 * the %string is extended and new elements are %set to @a c.
735 resize(size_type __n, _CharT __c);
738 * @brief Resizes the %string to the specified number of characters.
739 * @param n Number of characters the %string should contain.
741 * This function will resize the %string to the specified length. If
742 * the new size is smaller than the %string's current size the %string
743 * is truncated, otherwise the %string is extended and new characters
744 * are default-constructed. For basic types such as char, this means
748 resize(size_type __n)
749 { this->resize(__n, _CharT()); }
751 #ifdef __GXX_EXPERIMENTAL_CXX0X__
752 /// A non-binding request to reduce capacity() to size().
756 if (capacity() > size())
767 * Returns the total number of characters that the %string can hold
768 * before needing to allocate more memory.
771 capacity() const _GLIBCXX_NOEXCEPT
772 { return _M_rep()->_M_capacity; }
775 * @brief Attempt to preallocate enough memory for specified number of
777 * @param res_arg Number of characters required.
778 * @throw std::length_error If @a res_arg exceeds @c max_size().
780 * This function attempts to reserve enough memory for the
781 * %string to hold the specified number of characters. If the
782 * number requested is more than max_size(), length_error is
785 * The advantage of this function is that if optimal code is a
786 * necessity and the user can determine the string length that will be
787 * required, the user can reserve the memory in %advance, and thus
788 * prevent a possible reallocation of memory and copying of %string
792 reserve(size_type __res_arg = 0);
795 * Erases the string, making it empty.
798 clear() _GLIBCXX_NOEXCEPT
799 { _M_mutate(0, this->size(), 0); }
802 * Returns true if the %string is empty. Equivalent to
803 * <code>*this == ""</code>.
806 empty() const _GLIBCXX_NOEXCEPT
807 { return this->size() == 0; }
811 * @brief Subscript access to the data contained in the %string.
812 * @param pos The index of the character to access.
813 * @return Read-only (constant) reference to the character.
815 * This operator allows for easy, array-style, data access.
816 * Note that data access with this operator is unchecked and
817 * out_of_range lookups are not defined. (For checked lookups
821 operator[] (size_type __pos) const
823 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
824 return _M_data()[__pos];
828 * @brief Subscript access to the data contained in the %string.
829 * @param pos The index of the character to access.
830 * @return Read/write reference to the character.
832 * This operator allows for easy, array-style, data access.
833 * Note that data access with this operator is unchecked and
834 * out_of_range lookups are not defined. (For checked lookups
835 * see at().) Unshares the string.
838 operator[](size_type __pos)
840 // allow pos == size() as v3 extension:
841 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
842 // but be strict in pedantic mode:
843 _GLIBCXX_DEBUG_PEDASSERT(__pos < size());
845 return _M_data()[__pos];
849 * @brief Provides access to the data contained in the %string.
850 * @param n The index of the character to access.
851 * @return Read-only (const) reference to the character.
852 * @throw std::out_of_range If @a n is an invalid index.
854 * This function provides for safer data access. The parameter is
855 * first checked that it is in the range of the string. The function
856 * throws out_of_range if the check fails.
859 at(size_type __n) const
861 if (__n >= this->size())
862 __throw_out_of_range(__N("basic_string::at"));
863 return _M_data()[__n];
866 #ifdef __GXX_EXPERIMENTAL_CXX0X__
868 * Returns a read/write reference to the data at the first
869 * element of the %string.
873 { return operator[](0); }
876 * Returns a read-only (constant) reference to the data at the first
877 * element of the %string.
881 { return operator[](0); }
884 * Returns a read/write reference to the data at the last
885 * element of the %string.
889 { return operator[](this->size() - 1); }
892 * Returns a read-only (constant) reference to the data at the
893 * last element of the %string.
897 { return operator[](this->size() - 1); }
901 * @brief Provides access to the data contained in the %string.
902 * @param n The index of the character to access.
903 * @return Read/write reference to the character.
904 * @throw std::out_of_range If @a n is an invalid index.
906 * This function provides for safer data access. The parameter is
907 * first checked that it is in the range of the string. The function
908 * throws out_of_range if the check fails. Success results in
909 * unsharing the string.
915 __throw_out_of_range(__N("basic_string::at"));
917 return _M_data()[__n];
922 * @brief Append a string to this string.
923 * @param str The string to append.
924 * @return Reference to this string.
927 operator+=(const basic_string& __str)
928 { return this->append(__str); }
931 * @brief Append a C string.
932 * @param s The C string to append.
933 * @return Reference to this string.
936 operator+=(const _CharT* __s)
937 { return this->append(__s); }
940 * @brief Append a character.
941 * @param c The character to append.
942 * @return Reference to this string.
945 operator+=(_CharT __c)
947 this->push_back(__c);
951 #ifdef __GXX_EXPERIMENTAL_CXX0X__
953 * @brief Append an initializer_list of characters.
954 * @param l The initializer_list of characters to be appended.
955 * @return Reference to this string.
958 operator+=(initializer_list<_CharT> __l)
959 { return this->append(__l.begin(), __l.size()); }
960 #endif // __GXX_EXPERIMENTAL_CXX0X__
963 * @brief Append a string to this string.
964 * @param str The string to append.
965 * @return Reference to this string.
968 append(const basic_string& __str);
971 * @brief Append a substring.
972 * @param str The string to append.
973 * @param pos Index of the first character of str to append.
974 * @param n The number of characters to append.
975 * @return Reference to this string.
976 * @throw std::out_of_range if @a pos is not a valid index.
978 * This function appends @a n characters from @a str starting at @a pos
979 * to this string. If @a n is is larger than the number of available
980 * characters in @a str, the remainder of @a str is appended.
983 append(const basic_string& __str, size_type __pos, size_type __n);
986 * @brief Append a C substring.
987 * @param s The C string to append.
988 * @param n The number of characters to append.
989 * @return Reference to this string.
992 append(const _CharT* __s, size_type __n);
995 * @brief Append a C string.
996 * @param s The C string to append.
997 * @return Reference to this string.
1000 append(const _CharT* __s)
1002 __glibcxx_requires_string(__s);
1003 return this->append(__s, traits_type::length(__s));
1007 * @brief Append multiple characters.
1008 * @param n The number of characters to append.
1009 * @param c The character to use.
1010 * @return Reference to this string.
1012 * Appends n copies of c to this string.
1015 append(size_type __n, _CharT __c);
1017 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1019 * @brief Append an initializer_list of characters.
1020 * @param l The initializer_list of characters to append.
1021 * @return Reference to this string.
1024 append(initializer_list<_CharT> __l)
1025 { return this->append(__l.begin(), __l.size()); }
1026 #endif // __GXX_EXPERIMENTAL_CXX0X__
1029 * @brief Append a range of characters.
1030 * @param first Iterator referencing the first character to append.
1031 * @param last Iterator marking the end of the range.
1032 * @return Reference to this string.
1034 * Appends characters in the range [first,last) to this string.
1036 template<class _InputIterator>
1038 append(_InputIterator __first, _InputIterator __last)
1039 { return this->replace(_M_iend(), _M_iend(), __first, __last); }
1042 * @brief Append a single character.
1043 * @param c Character to append.
1046 push_back(_CharT __c)
1048 const size_type __len = 1 + this->size();
1049 if (__len > this->capacity() || _M_rep()->_M_is_shared())
1050 this->reserve(__len);
1051 traits_type::assign(_M_data()[this->size()], __c);
1052 _M_rep()->_M_set_length_and_sharable(__len);
1056 * @brief Set value to contents of another string.
1057 * @param str Source string to use.
1058 * @return Reference to this string.
1061 assign(const basic_string& __str);
1063 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1065 * @brief Set value to contents of another string.
1066 * @param str Source string to use.
1067 * @return Reference to this string.
1069 * This function sets this string to the exact contents of @a str.
1070 * @a str is a valid, but unspecified string.
1073 assign(basic_string&& __str)
1078 #endif // __GXX_EXPERIMENTAL_CXX0X__
1081 * @brief Set value to a substring of a string.
1082 * @param str The string to use.
1083 * @param pos Index of the first character of str.
1084 * @param n Number of characters to use.
1085 * @return Reference to this string.
1086 * @throw std::out_of_range if @a pos is not a valid index.
1088 * This function sets this string to the substring of @a str consisting
1089 * of @a n characters at @a pos. If @a n is is larger than the number
1090 * of available characters in @a str, the remainder of @a str is used.
1093 assign(const basic_string& __str, size_type __pos, size_type __n)
1094 { return this->assign(__str._M_data()
1095 + __str._M_check(__pos, "basic_string::assign"),
1096 __str._M_limit(__pos, __n)); }
1099 * @brief Set value to a C substring.
1100 * @param s The C string to use.
1101 * @param n Number of characters to use.
1102 * @return Reference to this string.
1104 * This function sets the value of this string to the first @a n
1105 * characters of @a s. If @a n is is larger than the number of
1106 * available characters in @a s, the remainder of @a s is used.
1109 assign(const _CharT* __s, size_type __n);
1112 * @brief Set value to contents of a C string.
1113 * @param s The C string to use.
1114 * @return Reference to this string.
1116 * This function sets the value of this string to the value of @a s.
1117 * The data is copied, so there is no dependence on @a s once the
1121 assign(const _CharT* __s)
1123 __glibcxx_requires_string(__s);
1124 return this->assign(__s, traits_type::length(__s));
1128 * @brief Set value to multiple characters.
1129 * @param n Length of the resulting string.
1130 * @param c The character to use.
1131 * @return Reference to this string.
1133 * This function sets the value of this string to @a n copies of
1137 assign(size_type __n, _CharT __c)
1138 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
1141 * @brief Set value to a range of characters.
1142 * @param first Iterator referencing the first character to append.
1143 * @param last Iterator marking the end of the range.
1144 * @return Reference to this string.
1146 * Sets value of string to characters in the range [first,last).
1148 template<class _InputIterator>
1150 assign(_InputIterator __first, _InputIterator __last)
1151 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
1153 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1155 * @brief Set value to an initializer_list of characters.
1156 * @param l The initializer_list of characters to assign.
1157 * @return Reference to this string.
1160 assign(initializer_list<_CharT> __l)
1161 { return this->assign(__l.begin(), __l.size()); }
1162 #endif // __GXX_EXPERIMENTAL_CXX0X__
1165 * @brief Insert multiple characters.
1166 * @param p Iterator referencing location in string to insert at.
1167 * @param n Number of characters to insert
1168 * @param c The character to insert.
1169 * @throw std::length_error If new length exceeds @c max_size().
1171 * Inserts @a n copies of character @a c starting at the position
1172 * referenced by iterator @a p. If adding characters causes the length
1173 * to exceed max_size(), length_error is thrown. The value of the
1174 * string doesn't change if an error is thrown.
1177 insert(iterator __p, size_type __n, _CharT __c)
1178 { this->replace(__p, __p, __n, __c); }
1181 * @brief Insert a range of characters.
1182 * @param p Iterator referencing location in string to insert at.
1183 * @param beg Start of range.
1184 * @param end End of range.
1185 * @throw std::length_error If new length exceeds @c max_size().
1187 * Inserts characters in range [beg,end). If adding characters causes
1188 * the length to exceed max_size(), length_error is thrown. The value
1189 * of the string doesn't change if an error is thrown.
1191 template<class _InputIterator>
1193 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
1194 { this->replace(__p, __p, __beg, __end); }
1196 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1198 * @brief Insert an initializer_list of characters.
1199 * @param p Iterator referencing location in string to insert at.
1200 * @param l The initializer_list of characters to insert.
1201 * @throw std::length_error If new length exceeds @c max_size().
1204 insert(iterator __p, initializer_list<_CharT> __l)
1206 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1207 this->insert(__p - _M_ibegin(), __l.begin(), __l.size());
1209 #endif // __GXX_EXPERIMENTAL_CXX0X__
1212 * @brief Insert value of a string.
1213 * @param pos1 Iterator referencing location in string to insert at.
1214 * @param str The string to insert.
1215 * @return Reference to this string.
1216 * @throw std::length_error If new length exceeds @c max_size().
1218 * Inserts value of @a str starting at @a pos1. If adding characters
1219 * causes the length to exceed max_size(), length_error is thrown. The
1220 * value of the string doesn't change if an error is thrown.
1223 insert(size_type __pos1, const basic_string& __str)
1224 { return this->insert(__pos1, __str, size_type(0), __str.size()); }
1227 * @brief Insert a substring.
1228 * @param pos1 Iterator referencing location in string to insert at.
1229 * @param str The string to insert.
1230 * @param pos2 Start of characters in str to insert.
1231 * @param n Number of characters to insert.
1232 * @return Reference to this string.
1233 * @throw std::length_error If new length exceeds @c max_size().
1234 * @throw std::out_of_range If @a pos1 > size() or
1235 * @a pos2 > @a str.size().
1237 * Starting at @a pos1, insert @a n character of @a str beginning with
1238 * @a pos2. If adding characters causes the length to exceed
1239 * max_size(), length_error is thrown. If @a pos1 is beyond the end of
1240 * this string or @a pos2 is beyond the end of @a str, out_of_range is
1241 * thrown. The value of the string doesn't change if an error is
1245 insert(size_type __pos1, const basic_string& __str,
1246 size_type __pos2, size_type __n)
1247 { return this->insert(__pos1, __str._M_data()
1248 + __str._M_check(__pos2, "basic_string::insert"),
1249 __str._M_limit(__pos2, __n)); }
1252 * @brief Insert a C substring.
1253 * @param pos Iterator referencing location in string to insert at.
1254 * @param s The C string to insert.
1255 * @param n The number of characters to insert.
1256 * @return Reference to this string.
1257 * @throw std::length_error If new length exceeds @c max_size().
1258 * @throw std::out_of_range If @a pos is beyond the end of this
1261 * Inserts the first @a n characters of @a s starting at @a pos. If
1262 * adding characters causes the length to exceed max_size(),
1263 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1264 * thrown. The value of the string doesn't change if an error is
1268 insert(size_type __pos, const _CharT* __s, size_type __n);
1271 * @brief Insert a C string.
1272 * @param pos Iterator referencing location in string to insert at.
1273 * @param s The C string to insert.
1274 * @return Reference to this string.
1275 * @throw std::length_error If new length exceeds @c max_size().
1276 * @throw std::out_of_range If @a pos is beyond the end of this
1279 * Inserts the first @a n characters of @a s starting at @a pos. If
1280 * adding characters causes the length to exceed max_size(),
1281 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1282 * thrown. The value of the string doesn't change if an error is
1286 insert(size_type __pos, const _CharT* __s)
1288 __glibcxx_requires_string(__s);
1289 return this->insert(__pos, __s, traits_type::length(__s));
1293 * @brief Insert multiple characters.
1294 * @param pos Index in string to insert at.
1295 * @param n Number of characters to insert
1296 * @param c The character to insert.
1297 * @return Reference to this string.
1298 * @throw std::length_error If new length exceeds @c max_size().
1299 * @throw std::out_of_range If @a pos is beyond the end of this
1302 * Inserts @a n copies of character @a c starting at index @a pos. If
1303 * adding characters causes the length to exceed max_size(),
1304 * length_error is thrown. If @a pos > length(), out_of_range is
1305 * thrown. The value of the string doesn't change if an error is
1309 insert(size_type __pos, size_type __n, _CharT __c)
1310 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
1311 size_type(0), __n, __c); }
1314 * @brief Insert one character.
1315 * @param p Iterator referencing position in string to insert at.
1316 * @param c The character to insert.
1317 * @return Iterator referencing newly inserted char.
1318 * @throw std::length_error If new length exceeds @c max_size().
1320 * Inserts character @a c at position referenced by @a p. If adding
1321 * character causes the length to exceed max_size(), length_error is
1322 * thrown. If @a p is beyond end of string, out_of_range is thrown.
1323 * The value of the string doesn't change if an error is thrown.
1326 insert(iterator __p, _CharT __c)
1328 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1329 const size_type __pos = __p - _M_ibegin();
1330 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1331 _M_rep()->_M_set_leaked();
1332 return iterator(_M_data() + __pos);
1336 * @brief Remove characters.
1337 * @param pos Index of first character to remove (default 0).
1338 * @param n Number of characters to remove (default remainder).
1339 * @return Reference to this string.
1340 * @throw std::out_of_range If @a pos is beyond the end of this
1343 * Removes @a n characters from this string starting at @a pos. The
1344 * length of the string is reduced by @a n. If there are < @a n
1345 * characters to remove, the remainder of the string is truncated. If
1346 * @a p is beyond end of string, out_of_range is thrown. The value of
1347 * the string doesn't change if an error is thrown.
1350 erase(size_type __pos = 0, size_type __n = npos)
1352 _M_mutate(_M_check(__pos, "basic_string::erase"),
1353 _M_limit(__pos, __n), size_type(0));
1358 * @brief Remove one character.
1359 * @param position Iterator referencing the character to remove.
1360 * @return iterator referencing same location after removal.
1362 * Removes the character at @a position from this string. The value
1363 * of the string doesn't change if an error is thrown.
1366 erase(iterator __position)
1368 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
1369 && __position < _M_iend());
1370 const size_type __pos = __position - _M_ibegin();
1371 _M_mutate(__pos, size_type(1), size_type(0));
1372 _M_rep()->_M_set_leaked();
1373 return iterator(_M_data() + __pos);
1377 * @brief Remove a range of characters.
1378 * @param first Iterator referencing the first character to remove.
1379 * @param last Iterator referencing the end of the range.
1380 * @return Iterator referencing location of first after removal.
1382 * Removes the characters in the range [first,last) from this string.
1383 * The value of the string doesn't change if an error is thrown.
1386 erase(iterator __first, iterator __last);
1389 * @brief Replace characters with value from another string.
1390 * @param pos Index of first character to replace.
1391 * @param n Number of characters to be replaced.
1392 * @param str String to insert.
1393 * @return Reference to this string.
1394 * @throw std::out_of_range If @a pos is beyond the end of this
1396 * @throw std::length_error If new length exceeds @c max_size().
1398 * Removes the characters in the range [pos,pos+n) from this string.
1399 * In place, the value of @a str is inserted. If @a pos is beyond end
1400 * of string, out_of_range is thrown. If the length of the result
1401 * exceeds max_size(), length_error is thrown. The value of the string
1402 * doesn't change if an error is thrown.
1405 replace(size_type __pos, size_type __n, const basic_string& __str)
1406 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1409 * @brief Replace characters with value from another string.
1410 * @param pos1 Index of first character to replace.
1411 * @param n1 Number of characters to be replaced.
1412 * @param str String to insert.
1413 * @param pos2 Index of first character of str to use.
1414 * @param n2 Number of characters from str to use.
1415 * @return Reference to this string.
1416 * @throw std::out_of_range If @a pos1 > size() or @a pos2 >
1418 * @throw std::length_error If new length exceeds @c max_size().
1420 * Removes the characters in the range [pos1,pos1 + n) from this
1421 * string. In place, the value of @a str is inserted. If @a pos is
1422 * beyond end of string, out_of_range is thrown. If the length of the
1423 * result exceeds max_size(), length_error is thrown. The value of the
1424 * string doesn't change if an error is thrown.
1427 replace(size_type __pos1, size_type __n1, const basic_string& __str,
1428 size_type __pos2, size_type __n2)
1429 { return this->replace(__pos1, __n1, __str._M_data()
1430 + __str._M_check(__pos2, "basic_string::replace"),
1431 __str._M_limit(__pos2, __n2)); }
1434 * @brief Replace characters with value of a C substring.
1435 * @param pos Index of first character to replace.
1436 * @param n1 Number of characters to be replaced.
1437 * @param s C string to insert.
1438 * @param n2 Number of characters from @a s to use.
1439 * @return Reference to this string.
1440 * @throw std::out_of_range If @a pos1 > size().
1441 * @throw std::length_error If new length exceeds @c max_size().
1443 * Removes the characters in the range [pos,pos + n1) from this string.
1444 * In place, the first @a n2 characters of @a s are inserted, or all
1445 * of @a s if @a n2 is too large. If @a pos is beyond end of string,
1446 * out_of_range is thrown. If the length of result exceeds max_size(),
1447 * length_error is thrown. The value of the string doesn't change if
1448 * an error is thrown.
1451 replace(size_type __pos, size_type __n1, const _CharT* __s,
1455 * @brief Replace characters with value of a C string.
1456 * @param pos Index of first character to replace.
1457 * @param n1 Number of characters to be replaced.
1458 * @param s C string to insert.
1459 * @return Reference to this string.
1460 * @throw std::out_of_range If @a pos > size().
1461 * @throw std::length_error If new length exceeds @c max_size().
1463 * Removes the characters in the range [pos,pos + n1) from this string.
1464 * In place, the characters of @a s are inserted. If @a pos is beyond
1465 * end of string, out_of_range is thrown. If the length of result
1466 * exceeds max_size(), length_error is thrown. The value of the string
1467 * doesn't change if an error is thrown.
1470 replace(size_type __pos, size_type __n1, const _CharT* __s)
1472 __glibcxx_requires_string(__s);
1473 return this->replace(__pos, __n1, __s, traits_type::length(__s));
1477 * @brief Replace characters with multiple characters.
1478 * @param pos Index of first character to replace.
1479 * @param n1 Number of characters to be replaced.
1480 * @param n2 Number of characters to insert.
1481 * @param c Character to insert.
1482 * @return Reference to this string.
1483 * @throw std::out_of_range If @a pos > size().
1484 * @throw std::length_error If new length exceeds @c max_size().
1486 * Removes the characters in the range [pos,pos + n1) from this string.
1487 * In place, @a n2 copies of @a c are inserted. If @a pos is beyond
1488 * end of string, out_of_range is thrown. If the length of result
1489 * exceeds max_size(), length_error is thrown. The value of the string
1490 * doesn't change if an error is thrown.
1493 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1494 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
1495 _M_limit(__pos, __n1), __n2, __c); }
1498 * @brief Replace range of characters with string.
1499 * @param i1 Iterator referencing start of range to replace.
1500 * @param i2 Iterator referencing end of range to replace.
1501 * @param str String value to insert.
1502 * @return Reference to this string.
1503 * @throw std::length_error If new length exceeds @c max_size().
1505 * Removes the characters in the range [i1,i2). In place, the value of
1506 * @a str is inserted. If the length of result exceeds max_size(),
1507 * length_error is thrown. The value of the string doesn't change if
1508 * an error is thrown.
1511 replace(iterator __i1, iterator __i2, const basic_string& __str)
1512 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
1515 * @brief Replace range of characters with C substring.
1516 * @param i1 Iterator referencing start of range to replace.
1517 * @param i2 Iterator referencing end of range to replace.
1518 * @param s C string value to insert.
1519 * @param n Number of characters from s to insert.
1520 * @return Reference to this string.
1521 * @throw std::length_error If new length exceeds @c max_size().
1523 * Removes the characters in the range [i1,i2). In place, the first @a
1524 * n characters of @a s are inserted. If the length of result exceeds
1525 * max_size(), length_error is thrown. The value of the string doesn't
1526 * change if an error is thrown.
1529 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
1531 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1532 && __i2 <= _M_iend());
1533 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
1537 * @brief Replace range of characters with C string.
1538 * @param i1 Iterator referencing start of range to replace.
1539 * @param i2 Iterator referencing end of range to replace.
1540 * @param s C string value to insert.
1541 * @return Reference to this string.
1542 * @throw std::length_error If new length exceeds @c max_size().
1544 * Removes the characters in the range [i1,i2). In place, the
1545 * characters of @a s are inserted. If the length of result exceeds
1546 * max_size(), length_error is thrown. The value of the string doesn't
1547 * change if an error is thrown.
1550 replace(iterator __i1, iterator __i2, const _CharT* __s)
1552 __glibcxx_requires_string(__s);
1553 return this->replace(__i1, __i2, __s, traits_type::length(__s));
1557 * @brief Replace range of characters with multiple characters
1558 * @param i1 Iterator referencing start of range to replace.
1559 * @param i2 Iterator referencing end of range to replace.
1560 * @param n Number of characters to insert.
1561 * @param c Character to insert.
1562 * @return Reference to this string.
1563 * @throw std::length_error If new length exceeds @c max_size().
1565 * Removes the characters in the range [i1,i2). In place, @a n copies
1566 * of @a c are inserted. If the length of result exceeds max_size(),
1567 * length_error is thrown. The value of the string doesn't change if
1568 * an error is thrown.
1571 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
1573 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1574 && __i2 <= _M_iend());
1575 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
1579 * @brief Replace range of characters with range.
1580 * @param i1 Iterator referencing start of range to replace.
1581 * @param i2 Iterator referencing end of range to replace.
1582 * @param k1 Iterator referencing start of range to insert.
1583 * @param k2 Iterator referencing end of range to insert.
1584 * @return Reference to this string.
1585 * @throw std::length_error If new length exceeds @c max_size().
1587 * Removes the characters in the range [i1,i2). In place, characters
1588 * in the range [k1,k2) are inserted. If the length of result exceeds
1589 * max_size(), length_error is thrown. The value of the string doesn't
1590 * change if an error is thrown.
1592 template<class _InputIterator>
1594 replace(iterator __i1, iterator __i2,
1595 _InputIterator __k1, _InputIterator __k2)
1597 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1598 && __i2 <= _M_iend());
1599 __glibcxx_requires_valid_range(__k1, __k2);
1600 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1601 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
1604 // Specializations for the common case of pointer and iterator:
1605 // useful to avoid the overhead of temporary buffering in _M_replace.
1607 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
1609 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1610 && __i2 <= _M_iend());
1611 __glibcxx_requires_valid_range(__k1, __k2);
1612 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1617 replace(iterator __i1, iterator __i2,
1618 const _CharT* __k1, const _CharT* __k2)
1620 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1621 && __i2 <= _M_iend());
1622 __glibcxx_requires_valid_range(__k1, __k2);
1623 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1628 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
1630 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1631 && __i2 <= _M_iend());
1632 __glibcxx_requires_valid_range(__k1, __k2);
1633 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1634 __k1.base(), __k2 - __k1);
1638 replace(iterator __i1, iterator __i2,
1639 const_iterator __k1, const_iterator __k2)
1641 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1642 && __i2 <= _M_iend());
1643 __glibcxx_requires_valid_range(__k1, __k2);
1644 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1645 __k1.base(), __k2 - __k1);
1648 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1650 * @brief Replace range of characters with initializer_list.
1651 * @param i1 Iterator referencing start of range to replace.
1652 * @param i2 Iterator referencing end of range to replace.
1653 * @param l The initializer_list of characters to insert.
1654 * @return Reference to this string.
1655 * @throw std::length_error If new length exceeds @c max_size().
1657 * Removes the characters in the range [i1,i2). In place, characters
1658 * in the range [k1,k2) are inserted. If the length of result exceeds
1659 * max_size(), length_error is thrown. The value of the string doesn't
1660 * change if an error is thrown.
1662 basic_string& replace(iterator __i1, iterator __i2,
1663 initializer_list<_CharT> __l)
1664 { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
1665 #endif // __GXX_EXPERIMENTAL_CXX0X__
1668 template<class _Integer>
1670 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
1671 _Integer __val, __true_type)
1672 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
1674 template<class _InputIterator>
1676 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
1677 _InputIterator __k2, __false_type);
1680 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
1684 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
1687 // _S_construct_aux is used to implement the 21.3.1 para 15 which
1688 // requires special behaviour if _InIter is an integral type
1689 template<class _InIterator>
1691 _S_construct_aux(_InIterator __beg, _InIterator __end,
1692 const _Alloc& __a, __false_type)
1694 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
1695 return _S_construct(__beg, __end, __a, _Tag());
1698 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1699 // 438. Ambiguity in the "do the right thing" clause
1700 template<class _Integer>
1702 _S_construct_aux(_Integer __beg, _Integer __end,
1703 const _Alloc& __a, __true_type)
1704 { return _S_construct_aux_2(static_cast<size_type>(__beg),
1708 _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
1709 { return _S_construct(__req, __c, __a); }
1711 template<class _InIterator>
1713 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
1715 typedef typename std::__is_integer<_InIterator>::__type _Integral;
1716 return _S_construct_aux(__beg, __end, __a, _Integral());
1719 // For Input Iterators, used in istreambuf_iterators, etc.
1720 template<class _InIterator>
1722 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1723 input_iterator_tag);
1725 // For forward_iterators up to random_access_iterators, used for
1726 // string::iterator, _CharT*, etc.
1727 template<class _FwdIterator>
1729 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
1730 forward_iterator_tag);
1733 _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
1738 * @brief Copy substring into C string.
1739 * @param s C string to copy value into.
1740 * @param n Number of characters to copy.
1741 * @param pos Index of first character to copy.
1742 * @return Number of characters actually copied
1743 * @throw std::out_of_range If pos > size().
1745 * Copies up to @a n characters starting at @a pos into the C string @a
1746 * s. If @a pos is %greater than size(), out_of_range is thrown.
1749 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
1752 * @brief Swap contents with another string.
1753 * @param s String to swap with.
1755 * Exchanges the contents of this string with that of @a s in constant
1759 swap(basic_string& __s);
1761 // String operations:
1763 * @brief Return const pointer to null-terminated contents.
1765 * This is a handle to internal data. Do not modify or dire things may
1769 c_str() const _GLIBCXX_NOEXCEPT
1770 { return _M_data(); }
1773 * @brief Return const pointer to contents.
1775 * This is a handle to internal data. Do not modify or dire things may
1779 data() const _GLIBCXX_NOEXCEPT
1780 { return _M_data(); }
1783 * @brief Return copy of allocator used to construct this string.
1786 get_allocator() const _GLIBCXX_NOEXCEPT
1787 { return _M_dataplus; }
1790 * @brief Find position of a C substring.
1791 * @param s C string to locate.
1792 * @param pos Index of character to search from.
1793 * @param n Number of characters from @a s to search for.
1794 * @return Index of start of first occurrence.
1796 * Starting from @a pos, searches forward for the first @a n characters
1797 * in @a s within this string. If found, returns the index where it
1798 * begins. If not found, returns npos.
1801 find(const _CharT* __s, size_type __pos, size_type __n) const;
1804 * @brief Find position of a string.
1805 * @param str String to locate.
1806 * @param pos Index of character to search from (default 0).
1807 * @return Index of start of first occurrence.
1809 * Starting from @a pos, searches forward for value of @a str within
1810 * this string. If found, returns the index where it begins. If not
1811 * found, returns npos.
1814 find(const basic_string& __str, size_type __pos = 0) const
1816 { return this->find(__str.data(), __pos, __str.size()); }
1819 * @brief Find position of a C string.
1820 * @param s C string to locate.
1821 * @param pos Index of character to search from (default 0).
1822 * @return Index of start of first occurrence.
1824 * Starting from @a pos, searches forward for the value of @a s within
1825 * this string. If found, returns the index where it begins. If not
1826 * found, returns npos.
1829 find(const _CharT* __s, size_type __pos = 0) const
1831 __glibcxx_requires_string(__s);
1832 return this->find(__s, __pos, traits_type::length(__s));
1836 * @brief Find position of a character.
1837 * @param c Character to locate.
1838 * @param pos Index of character to search from (default 0).
1839 * @return Index of first occurrence.
1841 * Starting from @a pos, searches forward for @a c within this string.
1842 * If found, returns the index where it was found. If not found,
1846 find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
1849 * @brief Find last position of a string.
1850 * @param str String to locate.
1851 * @param pos Index of character to search back from (default end).
1852 * @return Index of start of last occurrence.
1854 * Starting from @a pos, searches backward for value of @a str within
1855 * this string. If found, returns the index where it begins. If not
1856 * found, returns npos.
1859 rfind(const basic_string& __str, size_type __pos = npos) const
1861 { return this->rfind(__str.data(), __pos, __str.size()); }
1864 * @brief Find last position of a C substring.
1865 * @param s C string to locate.
1866 * @param pos Index of character to search back from.
1867 * @param n Number of characters from s to search for.
1868 * @return Index of start of last occurrence.
1870 * Starting from @a pos, searches backward for the first @a n
1871 * characters in @a s within this string. If found, returns the index
1872 * where it begins. If not found, returns npos.
1875 rfind(const _CharT* __s, size_type __pos, size_type __n) const;
1878 * @brief Find last position of a C string.
1879 * @param s C string to locate.
1880 * @param pos Index of character to start search at (default end).
1881 * @return Index of start of last occurrence.
1883 * Starting from @a pos, searches backward for the value of @a s within
1884 * this string. If found, returns the index where it begins. If not
1885 * found, returns npos.
1888 rfind(const _CharT* __s, size_type __pos = npos) const
1890 __glibcxx_requires_string(__s);
1891 return this->rfind(__s, __pos, traits_type::length(__s));
1895 * @brief Find last position of a character.
1896 * @param c Character to locate.
1897 * @param pos Index of character to search back from (default end).
1898 * @return Index of last occurrence.
1900 * Starting from @a pos, searches backward for @a c within this string.
1901 * If found, returns the index where it was found. If not found,
1905 rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
1908 * @brief Find position of a character of string.
1909 * @param str String containing characters to locate.
1910 * @param pos Index of character to search from (default 0).
1911 * @return Index of first occurrence.
1913 * Starting from @a pos, searches forward for one of the characters of
1914 * @a str within this string. If found, returns the index where it was
1915 * found. If not found, returns npos.
1918 find_first_of(const basic_string& __str, size_type __pos = 0) const
1920 { return this->find_first_of(__str.data(), __pos, __str.size()); }
1923 * @brief Find position of a character of C substring.
1924 * @param s String containing characters to locate.
1925 * @param pos Index of character to search from.
1926 * @param n Number of characters from s to search for.
1927 * @return Index of first occurrence.
1929 * Starting from @a pos, searches forward for one of the first @a n
1930 * characters of @a s within this string. If found, returns the index
1931 * where it was found. If not found, returns npos.
1934 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
1937 * @brief Find position of a character of C string.
1938 * @param s String containing characters to locate.
1939 * @param pos Index of character to search from (default 0).
1940 * @return Index of first occurrence.
1942 * Starting from @a pos, searches forward for one of the characters of
1943 * @a s within this string. If found, returns the index where it was
1944 * found. If not found, returns npos.
1947 find_first_of(const _CharT* __s, size_type __pos = 0) const
1949 __glibcxx_requires_string(__s);
1950 return this->find_first_of(__s, __pos, traits_type::length(__s));
1954 * @brief Find position of a character.
1955 * @param c Character to locate.
1956 * @param pos Index of character to search from (default 0).
1957 * @return Index of first occurrence.
1959 * Starting from @a pos, searches forward for the character @a c within
1960 * this string. If found, returns the index where it was found. If
1961 * not found, returns npos.
1963 * Note: equivalent to find(c, pos).
1966 find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
1967 { return this->find(__c, __pos); }
1970 * @brief Find last position of a character of string.
1971 * @param str String containing characters to locate.
1972 * @param pos Index of character to search back from (default end).
1973 * @return Index of last occurrence.
1975 * Starting from @a pos, searches backward for one of the characters of
1976 * @a str within this string. If found, returns the index where it was
1977 * found. If not found, returns npos.
1980 find_last_of(const basic_string& __str, size_type __pos = npos) const
1982 { return this->find_last_of(__str.data(), __pos, __str.size()); }
1985 * @brief Find last position of a character of C substring.
1986 * @param s C string containing characters to locate.
1987 * @param pos Index of character to search back from.
1988 * @param n Number of characters from s to search for.
1989 * @return Index of last occurrence.
1991 * Starting from @a pos, searches backward for one of the first @a n
1992 * characters of @a s within this string. If found, returns the index
1993 * where it was found. If not found, returns npos.
1996 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
1999 * @brief Find last position of a character of C string.
2000 * @param s C string containing characters to locate.
2001 * @param pos Index of character to search back from (default end).
2002 * @return Index of last occurrence.
2004 * Starting from @a pos, searches backward for one of the characters of
2005 * @a s within this string. If found, returns the index where it was
2006 * found. If not found, returns npos.
2009 find_last_of(const _CharT* __s, size_type __pos = npos) const
2011 __glibcxx_requires_string(__s);
2012 return this->find_last_of(__s, __pos, traits_type::length(__s));
2016 * @brief Find last position of a character.
2017 * @param c Character to locate.
2018 * @param pos Index of character to search back from (default end).
2019 * @return Index of last occurrence.
2021 * Starting from @a pos, searches backward for @a c within this string.
2022 * If found, returns the index where it was found. If not found,
2025 * Note: equivalent to rfind(c, pos).
2028 find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
2029 { return this->rfind(__c, __pos); }
2032 * @brief Find position of a character not in string.
2033 * @param str String containing characters to avoid.
2034 * @param pos Index of character to search from (default 0).
2035 * @return Index of first occurrence.
2037 * Starting from @a pos, searches forward for a character not contained
2038 * in @a str within this string. If found, returns the index where it
2039 * was found. If not found, returns npos.
2042 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
2044 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
2047 * @brief Find position of a character not in C substring.
2048 * @param s C string containing characters to avoid.
2049 * @param pos Index of character to search from.
2050 * @param n Number of characters from s to consider.
2051 * @return Index of first occurrence.
2053 * Starting from @a pos, searches forward for a character not contained
2054 * in the first @a n characters of @a s within this string. If found,
2055 * returns the index where it was found. If not found, returns npos.
2058 find_first_not_of(const _CharT* __s, size_type __pos,
2059 size_type __n) const;
2062 * @brief Find position of a character not in C string.
2063 * @param s C string containing characters to avoid.
2064 * @param pos Index of character to search from (default 0).
2065 * @return Index of first occurrence.
2067 * Starting from @a pos, searches forward for a character not contained
2068 * in @a s within this string. If found, returns the index where it
2069 * was found. If not found, returns npos.
2072 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
2074 __glibcxx_requires_string(__s);
2075 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
2079 * @brief Find position of a different character.
2080 * @param c Character to avoid.
2081 * @param pos Index of character to search from (default 0).
2082 * @return Index of first occurrence.
2084 * Starting from @a pos, searches forward for a character other than @a c
2085 * within this string. If found, returns the index where it was found.
2086 * If not found, returns npos.
2089 find_first_not_of(_CharT __c, size_type __pos = 0) const
2093 * @brief Find last position of a character not in string.
2094 * @param str String containing characters to avoid.
2095 * @param pos Index of character to search back from (default end).
2096 * @return Index of last occurrence.
2098 * Starting from @a pos, searches backward for a character not
2099 * contained in @a str within this string. If found, returns the index
2100 * where it was found. If not found, returns npos.
2103 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
2105 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
2108 * @brief Find last position of a character not in C substring.
2109 * @param s C string containing characters to avoid.
2110 * @param pos Index of character to search back from.
2111 * @param n Number of characters from s to consider.
2112 * @return Index of last occurrence.
2114 * Starting from @a pos, searches backward for a character not
2115 * contained in the first @a n characters of @a s within this string.
2116 * If found, returns the index where it was found. If not found,
2120 find_last_not_of(const _CharT* __s, size_type __pos,
2121 size_type __n) const;
2123 * @brief Find last position of a character not in C string.
2124 * @param s C string containing characters to avoid.
2125 * @param pos Index of character to search back from (default end).
2126 * @return Index of last occurrence.
2128 * Starting from @a pos, searches backward for a character not
2129 * contained in @a s within this string. If found, returns the index
2130 * where it was found. If not found, returns npos.
2133 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
2135 __glibcxx_requires_string(__s);
2136 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
2140 * @brief Find last position of a different character.
2141 * @param c Character to avoid.
2142 * @param pos Index of character to search back from (default end).
2143 * @return Index of last occurrence.
2145 * Starting from @a pos, searches backward for a character other than
2146 * @a c within this string. If found, returns the index where it was
2147 * found. If not found, returns npos.
2150 find_last_not_of(_CharT __c, size_type __pos = npos) const
2154 * @brief Get a substring.
2155 * @param pos Index of first character (default 0).
2156 * @param n Number of characters in substring (default remainder).
2157 * @return The new string.
2158 * @throw std::out_of_range If pos > size().
2160 * Construct and return a new string using the @a n characters starting
2161 * at @a pos. If the string is too short, use the remainder of the
2162 * characters. If @a pos is beyond the end of the string, out_of_range
2166 substr(size_type __pos = 0, size_type __n = npos) const
2167 { return basic_string(*this,
2168 _M_check(__pos, "basic_string::substr"), __n); }
2171 * @brief Compare to a string.
2172 * @param str String to compare against.
2173 * @return Integer < 0, 0, or > 0.
2175 * Returns an integer < 0 if this string is ordered before @a str, 0 if
2176 * their values are equivalent, or > 0 if this string is ordered after
2177 * @a str. Determines the effective length rlen of the strings to
2178 * compare as the smallest of size() and str.size(). The function
2179 * then compares the two strings by calling traits::compare(data(),
2180 * str.data(),rlen). If the result of the comparison is nonzero returns
2181 * it, otherwise the shorter one is ordered first.
2184 compare(const basic_string& __str) const
2186 const size_type __size = this->size();
2187 const size_type __osize = __str.size();
2188 const size_type __len = std::min(__size, __osize);
2190 int __r = traits_type::compare(_M_data(), __str.data(), __len);
2192 __r = _S_compare(__size, __osize);
2197 * @brief Compare substring to a string.
2198 * @param pos Index of first character of substring.
2199 * @param n Number of characters in substring.
2200 * @param str String to compare against.
2201 * @return Integer < 0, 0, or > 0.
2203 * Form the substring of this string from the @a n characters starting
2204 * at @a pos. Returns an integer < 0 if the substring is ordered
2205 * before @a str, 0 if their values are equivalent, or > 0 if the
2206 * substring is ordered after @a str. Determines the effective length
2207 * rlen of the strings to compare as the smallest of the length of the
2208 * substring and @a str.size(). The function then compares the two
2209 * strings by calling traits::compare(substring.data(),str.data(),rlen).
2210 * If the result of the comparison is nonzero returns it, otherwise the
2211 * shorter one is ordered first.
2214 compare(size_type __pos, size_type __n, const basic_string& __str) const;
2217 * @brief Compare substring to a substring.
2218 * @param pos1 Index of first character of substring.
2219 * @param n1 Number of characters in substring.
2220 * @param str String to compare against.
2221 * @param pos2 Index of first character of substring of str.
2222 * @param n2 Number of characters in substring of str.
2223 * @return Integer < 0, 0, or > 0.
2225 * Form the substring of this string from the @a n1 characters starting
2226 * at @a pos1. Form the substring of @a str from the @a n2 characters
2227 * starting at @a pos2. Returns an integer < 0 if this substring is
2228 * ordered before the substring of @a str, 0 if their values are
2229 * equivalent, or > 0 if this substring is ordered after the substring
2230 * of @a str. Determines the effective length rlen of the strings
2231 * to compare as the smallest of the lengths of the substrings. The
2232 * function then compares the two strings by calling
2233 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
2234 * If the result of the comparison is nonzero returns it, otherwise the
2235 * shorter one is ordered first.
2238 compare(size_type __pos1, size_type __n1, const basic_string& __str,
2239 size_type __pos2, size_type __n2) const;
2242 * @brief Compare to a C string.
2243 * @param s C string to compare against.
2244 * @return Integer < 0, 0, or > 0.
2246 * Returns an integer < 0 if this string is ordered before @a s, 0 if
2247 * their values are equivalent, or > 0 if this string is ordered after
2248 * @a s. Determines the effective length rlen of the strings to
2249 * compare as the smallest of size() and the length of a string
2250 * constructed from @a s. The function then compares the two strings
2251 * by calling traits::compare(data(),s,rlen). If the result of the
2252 * comparison is nonzero returns it, otherwise the shorter one is
2256 compare(const _CharT* __s) const;
2258 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2259 // 5 String::compare specification questionable
2261 * @brief Compare substring to a C string.
2262 * @param pos Index of first character of substring.
2263 * @param n1 Number of characters in substring.
2264 * @param s C string to compare against.
2265 * @return Integer < 0, 0, or > 0.
2267 * Form the substring of this string from the @a n1 characters starting
2268 * at @a pos. Returns an integer < 0 if the substring is ordered
2269 * before @a s, 0 if their values are equivalent, or > 0 if the
2270 * substring is ordered after @a s. Determines the effective length
2271 * rlen of the strings to compare as the smallest of the length of the
2272 * substring and the length of a string constructed from @a s. The
2273 * function then compares the two string by calling
2274 * traits::compare(substring.data(),s,rlen). If the result of the
2275 * comparison is nonzero returns it, otherwise the shorter one is
2279 compare(size_type __pos, size_type __n1, const _CharT* __s) const;
2282 * @brief Compare substring against a character %array.
2283 * @param pos1 Index of first character of substring.
2284 * @param n1 Number of characters in substring.
2285 * @param s character %array to compare against.
2286 * @param n2 Number of characters of s.
2287 * @return Integer < 0, 0, or > 0.
2289 * Form the substring of this string from the @a n1 characters starting
2290 * at @a pos1. Form a string from the first @a n2 characters of @a s.
2291 * Returns an integer < 0 if this substring is ordered before the string
2292 * from @a s, 0 if their values are equivalent, or > 0 if this substring
2293 * is ordered after the string from @a s. Determines the effective
2294 * length rlen of the strings to compare as the smallest of the length
2295 * of the substring and @a n2. The function then compares the two
2296 * strings by calling traits::compare(substring.data(),s,rlen). If the
2297 * result of the comparison is nonzero returns it, otherwise the shorter
2298 * one is ordered first.
2300 * NB: s must have at least n2 characters, '\\0' has
2301 * no special meaning.
2304 compare(size_type __pos, size_type __n1, const _CharT* __s,
2305 size_type __n2) const;
2310 * @brief Concatenate two strings.
2311 * @param lhs First string.
2312 * @param rhs Last string.
2313 * @return New string with value of @a lhs followed by @a rhs.
2315 template<typename _CharT, typename _Traits, typename _Alloc>
2316 basic_string<_CharT, _Traits, _Alloc>
2317 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2318 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2320 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2321 __str.append(__rhs);
2326 * @brief Concatenate C string and string.
2327 * @param lhs First string.
2328 * @param rhs Last string.
2329 * @return New string with value of @a lhs followed by @a rhs.
2331 template<typename _CharT, typename _Traits, typename _Alloc>
2332 basic_string<_CharT,_Traits,_Alloc>
2333 operator+(const _CharT* __lhs,
2334 const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2337 * @brief Concatenate character and string.
2338 * @param lhs First string.
2339 * @param rhs Last string.
2340 * @return New string with @a lhs followed by @a rhs.
2342 template<typename _CharT, typename _Traits, typename _Alloc>
2343 basic_string<_CharT,_Traits,_Alloc>
2344 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2347 * @brief Concatenate string and C string.
2348 * @param lhs First string.
2349 * @param rhs Last string.
2350 * @return New string with @a lhs followed by @a rhs.
2352 template<typename _CharT, typename _Traits, typename _Alloc>
2353 inline basic_string<_CharT, _Traits, _Alloc>
2354 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2355 const _CharT* __rhs)
2357 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2358 __str.append(__rhs);
2363 * @brief Concatenate string and character.
2364 * @param lhs First string.
2365 * @param rhs Last string.
2366 * @return New string with @a lhs followed by @a rhs.
2368 template<typename _CharT, typename _Traits, typename _Alloc>
2369 inline basic_string<_CharT, _Traits, _Alloc>
2370 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
2372 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
2373 typedef typename __string_type::size_type __size_type;
2374 __string_type __str(__lhs);
2375 __str.append(__size_type(1), __rhs);
2379 #ifdef __GXX_EXPERIMENTAL_CXX0X__
2380 template<typename _CharT, typename _Traits, typename _Alloc>
2381 inline basic_string<_CharT, _Traits, _Alloc>
2382 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
2383 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2384 { return std::move(__lhs.append(__rhs)); }
2386 template<typename _CharT, typename _Traits, typename _Alloc>
2387 inline basic_string<_CharT, _Traits, _Alloc>
2388 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2389 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
2390 { return std::move(__rhs.insert(0, __lhs)); }
2392 template<typename _CharT, typename _Traits, typename _Alloc>
2393 inline basic_string<_CharT, _Traits, _Alloc>
2394 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
2395 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
2397 const auto __size = __lhs.size() + __rhs.size();
2398 const bool __cond = (__size > __lhs.capacity()
2399 && __size <= __rhs.capacity());
2400 return __cond ? std::move(__rhs.insert(0, __lhs))
2401 : std::move(__lhs.append(__rhs));
2404 template<typename _CharT, typename _Traits, typename _Alloc>
2405 inline basic_string<_CharT, _Traits, _Alloc>
2406 operator+(const _CharT* __lhs,
2407 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
2408 { return std::move(__rhs.insert(0, __lhs)); }
2410 template<typename _CharT, typename _Traits, typename _Alloc>
2411 inline basic_string<_CharT, _Traits, _Alloc>
2412 operator+(_CharT __lhs,
2413 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
2414 { return std::move(__rhs.insert(0, 1, __lhs)); }
2416 template<typename _CharT, typename _Traits, typename _Alloc>
2417 inline basic_string<_CharT, _Traits, _Alloc>
2418 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
2419 const _CharT* __rhs)
2420 { return std::move(__lhs.append(__rhs)); }
2422 template<typename _CharT, typename _Traits, typename _Alloc>
2423 inline basic_string<_CharT, _Traits, _Alloc>
2424 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
2426 { return std::move(__lhs.append(1, __rhs)); }
2431 * @brief Test equivalence of two strings.
2432 * @param lhs First string.
2433 * @param rhs Second string.
2434 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2436 template<typename _CharT, typename _Traits, typename _Alloc>
2438 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2439 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2440 { return __lhs.compare(__rhs) == 0; }
2442 template<typename _CharT>
2444 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type
2445 operator==(const basic_string<_CharT>& __lhs,
2446 const basic_string<_CharT>& __rhs)
2447 { return (__lhs.size() == __rhs.size()
2448 && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
2452 * @brief Test equivalence of C string and string.
2453 * @param lhs C string.
2454 * @param rhs String.
2455 * @return True if @a rhs.compare(@a lhs) == 0. False otherwise.
2457 template<typename _CharT, typename _Traits, typename _Alloc>
2459 operator==(const _CharT* __lhs,
2460 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2461 { return __rhs.compare(__lhs) == 0; }
2464 * @brief Test equivalence of string and C string.
2465 * @param lhs String.
2466 * @param rhs C string.
2467 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2469 template<typename _CharT, typename _Traits, typename _Alloc>
2471 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2472 const _CharT* __rhs)
2473 { return __lhs.compare(__rhs) == 0; }
2477 * @brief Test difference of two strings.
2478 * @param lhs First string.
2479 * @param rhs Second string.
2480 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2482 template<typename _CharT, typename _Traits, typename _Alloc>
2484 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2485 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2486 { return !(__lhs == __rhs); }
2489 * @brief Test difference of C string and string.
2490 * @param lhs C string.
2491 * @param rhs String.
2492 * @return True if @a rhs.compare(@a lhs) != 0. False otherwise.
2494 template<typename _CharT, typename _Traits, typename _Alloc>
2496 operator!=(const _CharT* __lhs,
2497 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2498 { return !(__lhs == __rhs); }
2501 * @brief Test difference of string and C string.
2502 * @param lhs String.
2503 * @param rhs C string.
2504 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2506 template<typename _CharT, typename _Traits, typename _Alloc>
2508 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2509 const _CharT* __rhs)
2510 { return !(__lhs == __rhs); }
2514 * @brief Test if string precedes string.
2515 * @param lhs First string.
2516 * @param rhs Second string.
2517 * @return True if @a lhs precedes @a rhs. False otherwise.
2519 template<typename _CharT, typename _Traits, typename _Alloc>
2521 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2522 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2523 { return __lhs.compare(__rhs) < 0; }
2526 * @brief Test if string precedes C string.
2527 * @param lhs String.
2528 * @param rhs C string.
2529 * @return True if @a lhs precedes @a rhs. False otherwise.
2531 template<typename _CharT, typename _Traits, typename _Alloc>
2533 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2534 const _CharT* __rhs)
2535 { return __lhs.compare(__rhs) < 0; }
2538 * @brief Test if C string precedes string.
2539 * @param lhs C string.
2540 * @param rhs String.
2541 * @return True if @a lhs precedes @a rhs. False otherwise.
2543 template<typename _CharT, typename _Traits, typename _Alloc>
2545 operator<(const _CharT* __lhs,
2546 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2547 { return __rhs.compare(__lhs) > 0; }
2551 * @brief Test if string follows string.
2552 * @param lhs First string.
2553 * @param rhs Second string.
2554 * @return True if @a lhs follows @a rhs. False otherwise.
2556 template<typename _CharT, typename _Traits, typename _Alloc>
2558 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2559 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2560 { return __lhs.compare(__rhs) > 0; }
2563 * @brief Test if string follows C string.
2564 * @param lhs String.
2565 * @param rhs C string.
2566 * @return True if @a lhs follows @a rhs. False otherwise.
2568 template<typename _CharT, typename _Traits, typename _Alloc>
2570 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2571 const _CharT* __rhs)
2572 { return __lhs.compare(__rhs) > 0; }
2575 * @brief Test if C string follows string.
2576 * @param lhs C string.
2577 * @param rhs String.
2578 * @return True if @a lhs follows @a rhs. False otherwise.
2580 template<typename _CharT, typename _Traits, typename _Alloc>
2582 operator>(const _CharT* __lhs,
2583 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2584 { return __rhs.compare(__lhs) < 0; }
2588 * @brief Test if string doesn't follow string.
2589 * @param lhs First string.
2590 * @param rhs Second string.
2591 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2593 template<typename _CharT, typename _Traits, typename _Alloc>
2595 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2596 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2597 { return __lhs.compare(__rhs) <= 0; }
2600 * @brief Test if string doesn't follow C string.
2601 * @param lhs String.
2602 * @param rhs C string.
2603 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2605 template<typename _CharT, typename _Traits, typename _Alloc>
2607 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2608 const _CharT* __rhs)
2609 { return __lhs.compare(__rhs) <= 0; }
2612 * @brief Test if C string doesn't follow string.
2613 * @param lhs C string.
2614 * @param rhs String.
2615 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2617 template<typename _CharT, typename _Traits, typename _Alloc>
2619 operator<=(const _CharT* __lhs,
2620 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2621 { return __rhs.compare(__lhs) >= 0; }
2625 * @brief Test if string doesn't precede string.
2626 * @param lhs First string.
2627 * @param rhs Second string.
2628 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2630 template<typename _CharT, typename _Traits, typename _Alloc>
2632 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2633 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2634 { return __lhs.compare(__rhs) >= 0; }
2637 * @brief Test if string doesn't precede C string.
2638 * @param lhs String.
2639 * @param rhs C string.
2640 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2642 template<typename _CharT, typename _Traits, typename _Alloc>
2644 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2645 const _CharT* __rhs)
2646 { return __lhs.compare(__rhs) >= 0; }
2649 * @brief Test if C string doesn't precede string.
2650 * @param lhs C string.
2651 * @param rhs String.
2652 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2654 template<typename _CharT, typename _Traits, typename _Alloc>
2656 operator>=(const _CharT* __lhs,
2657 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2658 { return __rhs.compare(__lhs) <= 0; }
2661 * @brief Swap contents of two strings.
2662 * @param lhs First string.
2663 * @param rhs Second string.
2665 * Exchanges the contents of @a lhs and @a rhs in constant time.
2667 template<typename _CharT, typename _Traits, typename _Alloc>
2669 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
2670 basic_string<_CharT, _Traits, _Alloc>& __rhs)
2671 { __lhs.swap(__rhs); }
2674 * @brief Read stream into a string.
2675 * @param is Input stream.
2676 * @param str Buffer to store into.
2677 * @return Reference to the input stream.
2679 * Stores characters from @a is into @a str until whitespace is found, the
2680 * end of the stream is encountered, or str.max_size() is reached. If
2681 * is.width() is non-zero, that is the limit on the number of characters
2682 * stored into @a str. Any previous contents of @a str are erased.
2684 template<typename _CharT, typename _Traits, typename _Alloc>
2685 basic_istream<_CharT, _Traits>&
2686 operator>>(basic_istream<_CharT, _Traits>& __is,
2687 basic_string<_CharT, _Traits, _Alloc>& __str);
2690 basic_istream<char>&
2691 operator>>(basic_istream<char>& __is, basic_string<char>& __str);
2694 * @brief Write string to a stream.
2695 * @param os Output stream.
2696 * @param str String to write out.
2697 * @return Reference to the output stream.
2699 * Output characters of @a str into os following the same rules as for
2700 * writing a C string.
2702 template<typename _CharT, typename _Traits, typename _Alloc>
2703 inline basic_ostream<_CharT, _Traits>&
2704 operator<<(basic_ostream<_CharT, _Traits>& __os,
2705 const basic_string<_CharT, _Traits, _Alloc>& __str)
2707 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2708 // 586. string inserter not a formatted function
2709 return __ostream_insert(__os, __str.data(), __str.size());
2713 * @brief Read a line from stream into a string.
2714 * @param is Input stream.
2715 * @param str Buffer to store into.
2716 * @param delim Character marking end of line.
2717 * @return Reference to the input stream.
2719 * Stores characters from @a is into @a str until @a delim is found, the
2720 * end of the stream is encountered, or str.max_size() is reached. If
2721 * is.width() is non-zero, that is the limit on the number of characters
2722 * stored into @a str. Any previous contents of @a str are erased. If @a
2723 * delim was encountered, it is extracted but not stored into @a str.
2725 template<typename _CharT, typename _Traits, typename _Alloc>
2726 basic_istream<_CharT, _Traits>&
2727 getline(basic_istream<_CharT, _Traits>& __is,
2728 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
2731 * @brief Read a line from stream into a string.
2732 * @param is Input stream.
2733 * @param str Buffer to store into.
2734 * @return Reference to the input stream.
2736 * Stores characters from is into @a str until '\n' is
2737 * found, the end of the stream is encountered, or str.max_size()
2738 * is reached. If is.width() is non-zero, that is the limit on the
2739 * number of characters stored into @a str. Any previous contents
2740 * of @a str are erased. If end of line was encountered, it is
2741 * extracted but not stored into @a str.
2743 template<typename _CharT, typename _Traits, typename _Alloc>
2744 inline basic_istream<_CharT, _Traits>&
2745 getline(basic_istream<_CharT, _Traits>& __is,
2746 basic_string<_CharT, _Traits, _Alloc>& __str)
2747 { return getline(__is, __str, __is.widen('\n')); }
2750 basic_istream<char>&
2751 getline(basic_istream<char>& __in, basic_string<char>& __str,
2754 #ifdef _GLIBCXX_USE_WCHAR_T
2756 basic_istream<wchar_t>&
2757 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
2761 _GLIBCXX_END_NAMESPACE_VERSION
2764 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) && defined(_GLIBCXX_USE_C99) \
2765 && !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))
2767 #include <ext/string_conversions.h>
2769 namespace std _GLIBCXX_VISIBILITY(default)
2771 _GLIBCXX_BEGIN_NAMESPACE_VERSION
2773 // 21.4 Numeric Conversions [string.conversions].
2775 stoi(const string& __str, size_t* __idx = 0, int __base = 10)
2776 { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
2780 stol(const string& __str, size_t* __idx = 0, int __base = 10)
2781 { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
2784 inline unsigned long
2785 stoul(const string& __str, size_t* __idx = 0, int __base = 10)
2786 { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
2790 stoll(const string& __str, size_t* __idx = 0, int __base = 10)
2791 { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
2794 inline unsigned long long
2795 stoull(const string& __str, size_t* __idx = 0, int __base = 10)
2796 { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
2799 // NB: strtof vs strtod.
2801 stof(const string& __str, size_t* __idx = 0)
2802 { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
2805 stod(const string& __str, size_t* __idx = 0)
2806 { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
2809 stold(const string& __str, size_t* __idx = 0)
2810 { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
2812 // NB: (v)snprintf vs sprintf.
2816 to_string(int __val)
2817 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(int),
2821 to_string(unsigned __val)
2822 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
2823 4 * sizeof(unsigned),
2827 to_string(long __val)
2828 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(long),
2832 to_string(unsigned long __val)
2833 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
2834 4 * sizeof(unsigned long),
2838 to_string(long long __val)
2839 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
2840 4 * sizeof(long long),
2844 to_string(unsigned long long __val)
2845 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
2846 4 * sizeof(unsigned long long),
2850 to_string(float __val)
2853 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
2854 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
2859 to_string(double __val)
2862 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
2863 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
2868 to_string(long double __val)
2871 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
2872 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
2876 #ifdef _GLIBCXX_USE_WCHAR_T
2878 stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
2879 { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
2883 stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
2884 { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
2887 inline unsigned long
2888 stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
2889 { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
2893 stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
2894 { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
2897 inline unsigned long long
2898 stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
2899 { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
2902 // NB: wcstof vs wcstod.
2904 stof(const wstring& __str, size_t* __idx = 0)
2905 { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
2908 stod(const wstring& __str, size_t* __idx = 0)
2909 { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
2912 stold(const wstring& __str, size_t* __idx = 0)
2913 { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
2917 to_wstring(int __val)
2918 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int),
2922 to_wstring(unsigned __val)
2923 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
2924 4 * sizeof(unsigned),
2928 to_wstring(long __val)
2929 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long),
2933 to_wstring(unsigned long __val)
2934 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
2935 4 * sizeof(unsigned long),
2939 to_wstring(long long __val)
2940 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
2941 4 * sizeof(long long),
2945 to_wstring(unsigned long long __val)
2946 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
2947 4 * sizeof(unsigned long long),
2951 to_wstring(float __val)
2954 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
2955 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
2960 to_wstring(double __val)
2963 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
2964 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
2969 to_wstring(long double __val)
2972 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
2973 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
2978 _GLIBCXX_END_NAMESPACE_VERSION
2981 #endif /* __GXX_EXPERIMENTAL_CXX0X__ && _GLIBCXX_USE_C99 ... */
2983 #ifdef __GXX_EXPERIMENTAL_CXX0X__
2985 #include <bits/functional_hash.h>
2987 namespace std _GLIBCXX_VISIBILITY(default)
2989 _GLIBCXX_BEGIN_NAMESPACE_VERSION
2993 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
2994 /// std::hash specialization for string.
2997 : public __hash_base<size_t, string>
3000 operator()(const string& __s) const
3001 { return std::_Hash_impl::hash(__s.data(), __s.length()); }
3004 #ifdef _GLIBCXX_USE_WCHAR_T
3005 /// std::hash specialization for wstring.
3007 struct hash<wstring>
3008 : public __hash_base<size_t, wstring>
3011 operator()(const wstring& __s) const
3012 { return std::_Hash_impl::hash(__s.data(),
3013 __s.length() * sizeof(wchar_t)); }
3016 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
3018 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
3019 /// std::hash specialization for u16string.
3021 struct hash<u16string>
3022 : public __hash_base<size_t, u16string>
3025 operator()(const u16string& __s) const
3026 { return std::_Hash_impl::hash(__s.data(),
3027 __s.length() * sizeof(char16_t)); }
3030 /// std::hash specialization for u32string.
3032 struct hash<u32string>
3033 : public __hash_base<size_t, u32string>
3036 operator()(const u32string& __s) const
3037 { return std::_Hash_impl::hash(__s.data(),
3038 __s.length() * sizeof(char32_t)); }
3042 _GLIBCXX_END_NAMESPACE_VERSION
3045 #endif /* __GXX_EXPERIMENTAL_CXX0X__ */
3047 #endif /* _BASIC_STRING_H */