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
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 basic_string.h
28 * This is an internal header file, included by other library headers.
29 * You should not attempt to use it directly.
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 _GLIBCXX_BEGIN_NAMESPACE(std)
48 * @class basic_string basic_string.h <string>
49 * @brief Managing sequences of characters and character-like objects.
53 * Meets the requirements of a <a href="tables.html#65">container</a>, a
54 * <a href="tables.html#66">reversible container</a>, and a
55 * <a href="tables.html#67">sequence</a>. Of the
56 * <a href="tables.html#68">optional sequence requirements</a>, only
57 * @c push_back, @c at, and array access are supported.
62 * Documentation? What's that?
63 * Nathan Myers <ncm@cantrip.org>.
65 * A string looks like this:
70 * [basic_string<char_type>] _M_capacity
71 * _M_dataplus _M_refcount
72 * _M_p ----------------> unnamed array of char_type
75 * Where the _M_p points to the first character in the string, and
76 * you cast it to a pointer-to-_Rep and subtract 1 to get a
77 * pointer to the header.
79 * This approach has the enormous advantage that a string object
80 * requires only one allocation. All the ugliness is confined
81 * within a single pair of inline functions, which each compile to
82 * a single @a add instruction: _Rep::_M_data(), and
83 * string::_M_rep(); and the allocation function which gets a
84 * block of raw bytes and with room enough and constructs a _Rep
85 * object at the front.
87 * The reason you want _M_data pointing to the character array and
88 * not the _Rep is so that the debugger can see the string
89 * contents. (Probably we should add a non-inline member to get
90 * the _Rep for the debugger to use, so users can check the actual
93 * Note that the _Rep object is a POD so that you can have a
94 * static <em>empty string</em> _Rep object already @a constructed before
95 * static constructors have run. The reference-count encoding is
96 * chosen so that a 0 indicates one reference, so you never try to
97 * destroy the empty-string _Rep object.
99 * All but the last paragraph is considered pretty conventional
100 * for a C++ string implementation.
102 // 21.3 Template class basic_string
103 template<typename _CharT, typename _Traits, typename _Alloc>
106 typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
110 typedef _Traits traits_type;
111 typedef typename _Traits::char_type value_type;
112 typedef _Alloc allocator_type;
113 typedef typename _CharT_alloc_type::size_type size_type;
114 typedef typename _CharT_alloc_type::difference_type difference_type;
115 typedef typename _CharT_alloc_type::reference reference;
116 typedef typename _CharT_alloc_type::const_reference const_reference;
117 typedef typename _CharT_alloc_type::pointer pointer;
118 typedef typename _CharT_alloc_type::const_pointer const_pointer;
119 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
120 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
122 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
123 typedef std::reverse_iterator<iterator> reverse_iterator;
126 // _Rep: string representation
128 // 1. String really contains _M_length + 1 characters: due to 21.3.4
129 // must be kept null-terminated.
130 // 2. _M_capacity >= _M_length
131 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
132 // 3. _M_refcount has three states:
133 // -1: leaked, one reference, no ref-copies allowed, non-const.
134 // 0: one reference, non-const.
135 // n>0: n + 1 references, operations require a lock, const.
136 // 4. All fields==0 is an empty string, given the extra storage
137 // beyond-the-end for a null terminator; thus, the shared
138 // empty string representation needs no constructor.
143 size_type _M_capacity;
144 _Atomic_word _M_refcount;
147 struct _Rep : _Rep_base
150 typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc;
152 // (Public) Data members:
154 // The maximum number of individual char_type elements of an
155 // individual string is determined by _S_max_size. This is the
156 // value that will be returned by max_size(). (Whereas npos
157 // is the maximum number of bytes the allocator can allocate.)
158 // If one was to divvy up the theoretical largest size string,
159 // with a terminating character and m _CharT elements, it'd
161 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
163 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
164 // In addition, this implementation quarters this amount.
165 static const size_type _S_max_size;
166 static const _CharT _S_terminal;
168 // The following storage is init'd to 0 by the linker, resulting
169 // (carefully) in an empty string with one reference.
170 static size_type _S_empty_rep_storage[];
175 // NB: Mild hack to avoid strict-aliasing warnings. Note that
176 // _S_empty_rep_storage is never modified and the punning should
177 // be reasonably safe in this case.
178 void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage);
179 return *reinterpret_cast<_Rep*>(__p);
184 { return this->_M_refcount < 0; }
188 { return this->_M_refcount > 0; }
192 { this->_M_refcount = -1; }
196 { this->_M_refcount = 0; }
199 _M_set_length_and_sharable(size_type __n)
201 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
202 if (__builtin_expect(this != &_S_empty_rep(), false))
205 this->_M_set_sharable(); // One reference.
206 this->_M_length = __n;
207 traits_type::assign(this->_M_refdata()[__n], _S_terminal);
208 // grrr. (per 21.3.4)
209 // You cannot leave those LWG people alone for a second.
215 { return reinterpret_cast<_CharT*>(this + 1); }
218 _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
220 return (!_M_is_leaked() && __alloc1 == __alloc2)
221 ? _M_refcopy() : _M_clone(__alloc1);
226 _S_create(size_type, size_type, const _Alloc&);
229 _M_dispose(const _Alloc& __a)
231 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
232 if (__builtin_expect(this != &_S_empty_rep(), false))
234 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
240 _M_destroy(const _Alloc&) throw();
245 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
246 if (__builtin_expect(this != &_S_empty_rep(), false))
248 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
253 _M_clone(const _Alloc&, size_type __res = 0);
256 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
257 struct _Alloc_hider : _Alloc
259 _Alloc_hider(_CharT* __dat, const _Alloc& __a)
260 : _Alloc(__a), _M_p(__dat) { }
262 _CharT* _M_p; // The actual data.
266 // Data Members (public):
267 // NB: This is an unsigned type, and thus represents the maximum
268 // size that the allocator can hold.
269 /// Value returned by various member functions when they fail.
270 static const size_type npos = static_cast<size_type>(-1);
273 // Data Members (private):
274 mutable _Alloc_hider _M_dataplus;
278 { return _M_dataplus._M_p; }
282 { return (_M_dataplus._M_p = __p); }
286 { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
288 // For the internal use we have functions similar to `begin'/`end'
289 // but they do not call _M_leak.
292 { return iterator(_M_data()); }
296 { return iterator(_M_data() + this->size()); }
299 _M_leak() // for use in begin() & non-const op[]
301 if (!_M_rep()->_M_is_leaked())
306 _M_check(size_type __pos, const char* __s) const
308 if (__pos > this->size())
309 __throw_out_of_range(__N(__s));
314 _M_check_length(size_type __n1, size_type __n2, const char* __s) const
316 if (this->max_size() - (this->size() - __n1) < __n2)
317 __throw_length_error(__N(__s));
320 // NB: _M_limit doesn't check for a bad __pos value.
322 _M_limit(size_type __pos, size_type __off) const
324 const bool __testoff = __off < this->size() - __pos;
325 return __testoff ? __off : this->size() - __pos;
328 // True if _Rep and source do not overlap.
330 _M_disjunct(const _CharT* __s) const
332 return (less<const _CharT*>()(__s, _M_data())
333 || less<const _CharT*>()(_M_data() + this->size(), __s));
336 // When __n = 1 way faster than the general multichar
337 // traits_type::copy/move/assign.
339 _M_copy(_CharT* __d, const _CharT* __s, size_type __n)
342 traits_type::assign(*__d, *__s);
344 traits_type::copy(__d, __s, __n);
348 _M_move(_CharT* __d, const _CharT* __s, size_type __n)
351 traits_type::assign(*__d, *__s);
353 traits_type::move(__d, __s, __n);
357 _M_assign(_CharT* __d, size_type __n, _CharT __c)
360 traits_type::assign(*__d, __c);
362 traits_type::assign(__d, __n, __c);
365 // _S_copy_chars is a separate template to permit specialization
366 // to optimize for the common case of pointers as iterators.
367 template<class _Iterator>
369 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
371 for (; __k1 != __k2; ++__k1, ++__p)
372 traits_type::assign(*__p, *__k1); // These types are off.
376 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2)
377 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
380 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
381 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
384 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
385 { _M_copy(__p, __k1, __k2 - __k1); }
388 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
389 { _M_copy(__p, __k1, __k2 - __k1); }
392 _S_compare(size_type __n1, size_type __n2)
394 const difference_type __d = difference_type(__n1 - __n2);
396 if (__d > __gnu_cxx::__numeric_traits<int>::__max)
397 return __gnu_cxx::__numeric_traits<int>::__max;
398 else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
399 return __gnu_cxx::__numeric_traits<int>::__min;
405 _M_mutate(size_type __pos, size_type __len1, size_type __len2);
412 { return _Rep::_S_empty_rep(); }
415 // Construct/copy/destroy:
416 // NB: We overload ctors in some cases instead of using default
417 // arguments, per 17.4.4.4 para. 2 item 2.
420 * @brief Default constructor creates an empty string.
423 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
424 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
426 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()){ }
430 * @brief Construct an empty string using allocator @a a.
433 basic_string(const _Alloc& __a);
435 // NB: per LWG issue 42, semantics different from IS:
437 * @brief Construct string with copy of value of @a str.
438 * @param str Source string.
440 basic_string(const basic_string& __str);
442 * @brief Construct string as copy of a substring.
443 * @param str Source string.
444 * @param pos Index of first character to copy from.
445 * @param n Number of characters to copy (default remainder).
447 basic_string(const basic_string& __str, size_type __pos,
448 size_type __n = npos);
450 * @brief Construct string as copy of a substring.
451 * @param str Source string.
452 * @param pos Index of first character to copy from.
453 * @param n Number of characters to copy.
454 * @param a Allocator to use.
456 basic_string(const basic_string& __str, size_type __pos,
457 size_type __n, const _Alloc& __a);
460 * @brief Construct string initialized by a character array.
461 * @param s Source character array.
462 * @param n Number of characters to copy.
463 * @param a Allocator to use (default is default allocator).
465 * NB: @a s must have at least @a n characters, '\\0'
466 * has no special meaning.
468 basic_string(const _CharT* __s, size_type __n,
469 const _Alloc& __a = _Alloc());
471 * @brief Construct string as copy of a C string.
472 * @param s Source C string.
473 * @param a Allocator to use (default is default allocator).
475 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
477 * @brief Construct string as multiple characters.
478 * @param n Number of characters.
479 * @param c Character to use.
480 * @param a Allocator to use (default is default allocator).
482 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
484 #ifdef __GXX_EXPERIMENTAL_CXX0X__
486 * @brief Move construct string.
487 * @param str Source string.
489 * The newly-created string contains the exact contents of @a str.
490 * @a str is a valid, but unspecified string.
492 basic_string(basic_string&& __str)
493 : _M_dataplus(__str._M_dataplus)
495 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
496 __str._M_data(_S_empty_rep()._M_refdata());
498 __str._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
503 * @brief Construct string from an initializer list.
504 * @param l std::initializer_list of characters.
505 * @param a Allocator to use (default is default allocator).
507 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc());
508 #endif // __GXX_EXPERIMENTAL_CXX0X__
511 * @brief Construct string as copy of a range.
512 * @param beg Start of range.
513 * @param end End of range.
514 * @param a Allocator to use (default is default allocator).
516 template<class _InputIterator>
517 basic_string(_InputIterator __beg, _InputIterator __end,
518 const _Alloc& __a = _Alloc());
521 * @brief Destroy the string instance.
524 { _M_rep()->_M_dispose(this->get_allocator()); }
527 * @brief Assign the value of @a str to this string.
528 * @param str Source string.
531 operator=(const basic_string& __str)
532 { return this->assign(__str); }
535 * @brief Copy contents of @a s into this string.
536 * @param s Source null-terminated string.
539 operator=(const _CharT* __s)
540 { return this->assign(__s); }
543 * @brief Set value to string of length 1.
544 * @param c Source character.
546 * Assigning to a character makes this string length 1 and
547 * (*this)[0] == @a c.
550 operator=(_CharT __c)
552 this->assign(1, __c);
556 #ifdef __GXX_EXPERIMENTAL_CXX0X__
558 * @brief Move assign the value of @a str to this string.
559 * @param str Source string.
561 * The contents of @a str are moved into this string (without copying).
562 * @a str is a valid, but unspecified string.
565 operator=(basic_string&& __str)
573 * @brief Set value to string constructed from initializer list.
574 * @param l std::initializer_list.
577 operator=(initializer_list<_CharT> __l)
579 this->assign(__l.begin(), __l.size());
582 #endif // __GXX_EXPERIMENTAL_CXX0X__
586 * Returns a read/write iterator that points to the first character in
587 * the %string. Unshares the string.
593 return iterator(_M_data());
597 * Returns a read-only (constant) iterator that points to the first
598 * character in the %string.
602 { return const_iterator(_M_data()); }
605 * Returns a read/write iterator that points one past the last
606 * character in the %string. Unshares the string.
612 return iterator(_M_data() + this->size());
616 * Returns a read-only (constant) iterator that points one past the
617 * last character in the %string.
621 { return const_iterator(_M_data() + this->size()); }
624 * Returns a read/write reverse iterator that points to the last
625 * character in the %string. Iteration is done in reverse element
626 * order. Unshares the string.
630 { return reverse_iterator(this->end()); }
633 * Returns a read-only (constant) reverse iterator that points
634 * to the last character in the %string. Iteration is done in
635 * reverse element order.
637 const_reverse_iterator
639 { return const_reverse_iterator(this->end()); }
642 * Returns a read/write reverse iterator that points to one before the
643 * first character in the %string. Iteration is done in reverse
644 * element order. Unshares the string.
648 { return reverse_iterator(this->begin()); }
651 * Returns a read-only (constant) reverse iterator that points
652 * to one before the first character in the %string. Iteration
653 * is done in reverse element order.
655 const_reverse_iterator
657 { return const_reverse_iterator(this->begin()); }
659 #ifdef __GXX_EXPERIMENTAL_CXX0X__
661 * Returns a read-only (constant) iterator that points to the first
662 * character in the %string.
666 { return const_iterator(this->_M_data()); }
669 * Returns a read-only (constant) iterator that points one past the
670 * last character in the %string.
674 { return const_iterator(this->_M_data() + this->size()); }
677 * Returns a read-only (constant) reverse iterator that points
678 * to the last character in the %string. Iteration is done in
679 * reverse element order.
681 const_reverse_iterator
683 { return const_reverse_iterator(this->end()); }
686 * Returns a read-only (constant) reverse iterator that points
687 * to one before the first character in the %string. Iteration
688 * is done in reverse element order.
690 const_reverse_iterator
692 { return const_reverse_iterator(this->begin()); }
697 /// Returns the number of characters in the string, not including any
698 /// null-termination.
701 { return _M_rep()->_M_length; }
703 /// Returns the number of characters in the string, not including any
704 /// null-termination.
707 { return _M_rep()->_M_length; }
709 /// Returns the size() of the largest possible %string.
712 { return _Rep::_S_max_size; }
715 * @brief Resizes the %string to the specified number of characters.
716 * @param n Number of characters the %string should contain.
717 * @param c Character to fill any new elements.
719 * This function will %resize the %string to the specified
720 * number of characters. If the number is smaller than the
721 * %string's current size the %string is truncated, otherwise
722 * the %string is extended and new elements are set to @a c.
725 resize(size_type __n, _CharT __c);
728 * @brief Resizes the %string to the specified number of characters.
729 * @param n Number of characters the %string should contain.
731 * This function will resize the %string to the specified length. If
732 * the new size is smaller than the %string's current size the %string
733 * is truncated, otherwise the %string is extended and new characters
734 * are default-constructed. For basic types such as char, this means
738 resize(size_type __n)
739 { this->resize(__n, _CharT()); }
741 #ifdef __GXX_EXPERIMENTAL_CXX0X__
742 /// A non-binding request to reduce capacity() to size().
754 * Returns the total number of characters that the %string can hold
755 * before needing to allocate more memory.
759 { return _M_rep()->_M_capacity; }
762 * @brief Attempt to preallocate enough memory for specified number of
764 * @param res_arg Number of characters required.
765 * @throw std::length_error If @a res_arg exceeds @c max_size().
767 * This function attempts to reserve enough memory for the
768 * %string to hold the specified number of characters. If the
769 * number requested is more than max_size(), length_error is
772 * The advantage of this function is that if optimal code is a
773 * necessity and the user can determine the string length that will be
774 * required, the user can reserve the memory in %advance, and thus
775 * prevent a possible reallocation of memory and copying of %string
779 reserve(size_type __res_arg = 0);
782 * Erases the string, making it empty.
786 { _M_mutate(0, this->size(), 0); }
789 * Returns true if the %string is empty. Equivalent to
790 * <code>*this == ""</code>.
794 { return this->size() == 0; }
798 * @brief Subscript access to the data contained in the %string.
799 * @param pos The index of the character to access.
800 * @return Read-only (constant) reference to the character.
802 * This operator allows for easy, array-style, data access.
803 * Note that data access with this operator is unchecked and
804 * out_of_range lookups are not defined. (For checked lookups
808 operator[] (size_type __pos) const
810 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
811 return _M_data()[__pos];
815 * @brief Subscript access to the data contained in the %string.
816 * @param pos The index of the character to access.
817 * @return Read/write reference to the character.
819 * This operator allows for easy, array-style, data access.
820 * Note that data access with this operator is unchecked and
821 * out_of_range lookups are not defined. (For checked lookups
822 * see at().) Unshares the string.
825 operator[](size_type __pos)
827 // allow pos == size() as v3 extension:
828 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
829 // but be strict in pedantic mode:
830 _GLIBCXX_DEBUG_PEDASSERT(__pos < size());
832 return _M_data()[__pos];
836 * @brief Provides access to the data contained in the %string.
837 * @param n The index of the character to access.
838 * @return Read-only (const) reference to the character.
839 * @throw std::out_of_range If @a n is an invalid index.
841 * This function provides for safer data access. The parameter is
842 * first checked that it is in the range of the string. The function
843 * throws out_of_range if the check fails.
846 at(size_type __n) const
848 if (__n >= this->size())
849 __throw_out_of_range(__N("basic_string::at"));
850 return _M_data()[__n];
854 * @brief Provides access to the data contained in the %string.
855 * @param n The index of the character to access.
856 * @return Read/write reference to the character.
857 * @throw std::out_of_range If @a n is an invalid index.
859 * This function provides for safer data access. The parameter is
860 * first checked that it is in the range of the string. The function
861 * throws out_of_range if the check fails. Success results in
862 * unsharing the string.
868 __throw_out_of_range(__N("basic_string::at"));
870 return _M_data()[__n];
875 * @brief Append a string to this string.
876 * @param str The string to append.
877 * @return Reference to this string.
880 operator+=(const basic_string& __str)
881 { return this->append(__str); }
884 * @brief Append a C string.
885 * @param s The C string to append.
886 * @return Reference to this string.
889 operator+=(const _CharT* __s)
890 { return this->append(__s); }
893 * @brief Append a character.
894 * @param c The character to append.
895 * @return Reference to this string.
898 operator+=(_CharT __c)
900 this->push_back(__c);
904 #ifdef __GXX_EXPERIMENTAL_CXX0X__
906 * @brief Append an initializer_list of characters.
907 * @param l The initializer_list of characters to be appended.
908 * @return Reference to this string.
911 operator+=(initializer_list<_CharT> __l)
912 { return this->append(__l.begin(), __l.size()); }
913 #endif // __GXX_EXPERIMENTAL_CXX0X__
916 * @brief Append a string to this string.
917 * @param str The string to append.
918 * @return Reference to this string.
921 append(const basic_string& __str);
924 * @brief Append a substring.
925 * @param str The string to append.
926 * @param pos Index of the first character of str to append.
927 * @param n The number of characters to append.
928 * @return Reference to this string.
929 * @throw std::out_of_range if @a pos is not a valid index.
931 * This function appends @a n characters from @a str starting at @a pos
932 * to this string. If @a n is is larger than the number of available
933 * characters in @a str, the remainder of @a str is appended.
936 append(const basic_string& __str, size_type __pos, size_type __n);
939 * @brief Append a C substring.
940 * @param s The C string to append.
941 * @param n The number of characters to append.
942 * @return Reference to this string.
945 append(const _CharT* __s, size_type __n);
948 * @brief Append a C string.
949 * @param s The C string to append.
950 * @return Reference to this string.
953 append(const _CharT* __s)
955 __glibcxx_requires_string(__s);
956 return this->append(__s, traits_type::length(__s));
960 * @brief Append multiple characters.
961 * @param n The number of characters to append.
962 * @param c The character to use.
963 * @return Reference to this string.
965 * Appends n copies of c to this string.
968 append(size_type __n, _CharT __c);
970 #ifdef __GXX_EXPERIMENTAL_CXX0X__
972 * @brief Append an initializer_list of characters.
973 * @param l The initializer_list of characters to append.
974 * @return Reference to this string.
977 append(initializer_list<_CharT> __l)
978 { return this->append(__l.begin(), __l.size()); }
979 #endif // __GXX_EXPERIMENTAL_CXX0X__
982 * @brief Append a range of characters.
983 * @param first Iterator referencing the first character to append.
984 * @param last Iterator marking the end of the range.
985 * @return Reference to this string.
987 * Appends characters in the range [first,last) to this string.
989 template<class _InputIterator>
991 append(_InputIterator __first, _InputIterator __last)
992 { return this->replace(_M_iend(), _M_iend(), __first, __last); }
995 * @brief Append a single character.
996 * @param c Character to append.
999 push_back(_CharT __c)
1001 const size_type __len = 1 + this->size();
1002 if (__len > this->capacity() || _M_rep()->_M_is_shared())
1003 this->reserve(__len);
1004 traits_type::assign(_M_data()[this->size()], __c);
1005 _M_rep()->_M_set_length_and_sharable(__len);
1009 * @brief Set value to contents of another string.
1010 * @param str Source string to use.
1011 * @return Reference to this string.
1014 assign(const basic_string& __str);
1016 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1018 * @brief Set value to contents of another string.
1019 * @param str Source string to use.
1020 * @return Reference to this string.
1022 * This function sets this string to the exact contents of @a str.
1023 * @a str is a valid, but unspecified string.
1026 assign(basic_string&& __str)
1031 #endif // __GXX_EXPERIMENTAL_CXX0X__
1034 * @brief Set value to a substring of a string.
1035 * @param str The string to use.
1036 * @param pos Index of the first character of str.
1037 * @param n Number of characters to use.
1038 * @return Reference to this string.
1039 * @throw std::out_of_range if @a pos is not a valid index.
1041 * This function sets this string to the substring of @a str consisting
1042 * of @a n characters at @a pos. If @a n is is larger than the number
1043 * of available characters in @a str, the remainder of @a str is used.
1046 assign(const basic_string& __str, size_type __pos, size_type __n)
1047 { return this->assign(__str._M_data()
1048 + __str._M_check(__pos, "basic_string::assign"),
1049 __str._M_limit(__pos, __n)); }
1052 * @brief Set value to a C substring.
1053 * @param s The C string to use.
1054 * @param n Number of characters to use.
1055 * @return Reference to this string.
1057 * This function sets the value of this string to the first @a n
1058 * characters of @a s. If @a n is is larger than the number of
1059 * available characters in @a s, the remainder of @a s is used.
1062 assign(const _CharT* __s, size_type __n);
1065 * @brief Set value to contents of a C string.
1066 * @param s The C string to use.
1067 * @return Reference to this string.
1069 * This function sets the value of this string to the value of @a s.
1070 * The data is copied, so there is no dependence on @a s once the
1074 assign(const _CharT* __s)
1076 __glibcxx_requires_string(__s);
1077 return this->assign(__s, traits_type::length(__s));
1081 * @brief Set value to multiple characters.
1082 * @param n Length of the resulting string.
1083 * @param c The character to use.
1084 * @return Reference to this string.
1086 * This function sets the value of this string to @a n copies of
1090 assign(size_type __n, _CharT __c)
1091 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
1094 * @brief Set value to a range of characters.
1095 * @param first Iterator referencing the first character to append.
1096 * @param last Iterator marking the end of the range.
1097 * @return Reference to this string.
1099 * Sets value of string to characters in the range [first,last).
1101 template<class _InputIterator>
1103 assign(_InputIterator __first, _InputIterator __last)
1104 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
1106 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1108 * @brief Set value to an initializer_list of characters.
1109 * @param l The initializer_list of characters to assign.
1110 * @return Reference to this string.
1113 assign(initializer_list<_CharT> __l)
1114 { return this->assign(__l.begin(), __l.size()); }
1115 #endif // __GXX_EXPERIMENTAL_CXX0X__
1118 * @brief Insert multiple characters.
1119 * @param p Iterator referencing location in string to insert at.
1120 * @param n Number of characters to insert
1121 * @param c The character to insert.
1122 * @throw std::length_error If new length exceeds @c max_size().
1124 * Inserts @a n copies of character @a c starting at the position
1125 * referenced by iterator @a p. If adding characters causes the length
1126 * to exceed max_size(), length_error is thrown. The value of the
1127 * string doesn't change if an error is thrown.
1130 insert(iterator __p, size_type __n, _CharT __c)
1131 { this->replace(__p, __p, __n, __c); }
1134 * @brief Insert a range of characters.
1135 * @param p Iterator referencing location in string to insert at.
1136 * @param beg Start of range.
1137 * @param end End of range.
1138 * @throw std::length_error If new length exceeds @c max_size().
1140 * Inserts characters in range [beg,end). If adding characters causes
1141 * the length to exceed max_size(), length_error is thrown. The value
1142 * of the string doesn't change if an error is thrown.
1144 template<class _InputIterator>
1146 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
1147 { this->replace(__p, __p, __beg, __end); }
1149 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1151 * @brief Insert an initializer_list of characters.
1152 * @param p Iterator referencing location in string to insert at.
1153 * @param l The initializer_list of characters to insert.
1154 * @throw std::length_error If new length exceeds @c max_size().
1157 insert(iterator __p, initializer_list<_CharT> __l)
1159 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1160 this->insert(__p - _M_ibegin(), __l.begin(), __l.size());
1162 #endif // __GXX_EXPERIMENTAL_CXX0X__
1165 * @brief Insert value of a string.
1166 * @param pos1 Iterator referencing location in string to insert at.
1167 * @param str The string to insert.
1168 * @return Reference to this string.
1169 * @throw std::length_error If new length exceeds @c max_size().
1171 * Inserts value of @a str starting at @a pos1. If adding characters
1172 * causes the length to exceed max_size(), length_error is thrown. The
1173 * value of the string doesn't change if an error is thrown.
1176 insert(size_type __pos1, const basic_string& __str)
1177 { return this->insert(__pos1, __str, size_type(0), __str.size()); }
1180 * @brief Insert a substring.
1181 * @param pos1 Iterator referencing location in string to insert at.
1182 * @param str The string to insert.
1183 * @param pos2 Start of characters in str to insert.
1184 * @param n Number of characters to insert.
1185 * @return Reference to this string.
1186 * @throw std::length_error If new length exceeds @c max_size().
1187 * @throw std::out_of_range If @a pos1 > size() or
1188 * @a pos2 > @a str.size().
1190 * Starting at @a pos1, insert @a n character of @a str beginning with
1191 * @a pos2. If adding characters causes the length to exceed
1192 * max_size(), length_error is thrown. If @a pos1 is beyond the end of
1193 * this string or @a pos2 is beyond the end of @a str, out_of_range is
1194 * thrown. The value of the string doesn't change if an error is
1198 insert(size_type __pos1, const basic_string& __str,
1199 size_type __pos2, size_type __n)
1200 { return this->insert(__pos1, __str._M_data()
1201 + __str._M_check(__pos2, "basic_string::insert"),
1202 __str._M_limit(__pos2, __n)); }
1205 * @brief Insert a C substring.
1206 * @param pos Iterator referencing location in string to insert at.
1207 * @param s The C string to insert.
1208 * @param n The number of characters to insert.
1209 * @return Reference to this string.
1210 * @throw std::length_error If new length exceeds @c max_size().
1211 * @throw std::out_of_range If @a pos is beyond the end of this
1214 * Inserts the first @a n characters of @a s starting at @a pos. If
1215 * adding characters causes the length to exceed max_size(),
1216 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1217 * thrown. The value of the string doesn't change if an error is
1221 insert(size_type __pos, const _CharT* __s, size_type __n);
1224 * @brief Insert a C string.
1225 * @param pos Iterator referencing location in string to insert at.
1226 * @param s The C string to insert.
1227 * @return Reference to this string.
1228 * @throw std::length_error If new length exceeds @c max_size().
1229 * @throw std::out_of_range If @a pos is beyond the end of this
1232 * Inserts the first @a n characters of @a s starting at @a pos. If
1233 * adding characters causes the length to exceed max_size(),
1234 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1235 * thrown. The value of the string doesn't change if an error is
1239 insert(size_type __pos, const _CharT* __s)
1241 __glibcxx_requires_string(__s);
1242 return this->insert(__pos, __s, traits_type::length(__s));
1246 * @brief Insert multiple characters.
1247 * @param pos Index in string to insert at.
1248 * @param n Number of characters to insert
1249 * @param c The character to insert.
1250 * @return Reference to this string.
1251 * @throw std::length_error If new length exceeds @c max_size().
1252 * @throw std::out_of_range If @a pos is beyond the end of this
1255 * Inserts @a n copies of character @a c starting at index @a pos. If
1256 * adding characters causes the length to exceed max_size(),
1257 * length_error is thrown. If @a pos > length(), out_of_range is
1258 * thrown. The value of the string doesn't change if an error is
1262 insert(size_type __pos, size_type __n, _CharT __c)
1263 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
1264 size_type(0), __n, __c); }
1267 * @brief Insert one character.
1268 * @param p Iterator referencing position in string to insert at.
1269 * @param c The character to insert.
1270 * @return Iterator referencing newly inserted char.
1271 * @throw std::length_error If new length exceeds @c max_size().
1273 * Inserts character @a c at position referenced by @a p. If adding
1274 * character causes the length to exceed max_size(), length_error is
1275 * thrown. If @a p is beyond end of string, out_of_range is thrown.
1276 * The value of the string doesn't change if an error is thrown.
1279 insert(iterator __p, _CharT __c)
1281 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1282 const size_type __pos = __p - _M_ibegin();
1283 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1284 _M_rep()->_M_set_leaked();
1285 return iterator(_M_data() + __pos);
1289 * @brief Remove characters.
1290 * @param pos Index of first character to remove (default 0).
1291 * @param n Number of characters to remove (default remainder).
1292 * @return Reference to this string.
1293 * @throw std::out_of_range If @a pos is beyond the end of this
1296 * Removes @a n characters from this string starting at @a pos. The
1297 * length of the string is reduced by @a n. If there are < @a n
1298 * characters to remove, the remainder of the string is truncated. If
1299 * @a p is beyond end of string, out_of_range is thrown. The value of
1300 * the string doesn't change if an error is thrown.
1303 erase(size_type __pos = 0, size_type __n = npos)
1305 _M_mutate(_M_check(__pos, "basic_string::erase"),
1306 _M_limit(__pos, __n), size_type(0));
1311 * @brief Remove one character.
1312 * @param position Iterator referencing the character to remove.
1313 * @return iterator referencing same location after removal.
1315 * Removes the character at @a position from this string. The value
1316 * of the string doesn't change if an error is thrown.
1319 erase(iterator __position)
1321 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
1322 && __position < _M_iend());
1323 const size_type __pos = __position - _M_ibegin();
1324 _M_mutate(__pos, size_type(1), size_type(0));
1325 _M_rep()->_M_set_leaked();
1326 return iterator(_M_data() + __pos);
1330 * @brief Remove a range of characters.
1331 * @param first Iterator referencing the first character to remove.
1332 * @param last Iterator referencing the end of the range.
1333 * @return Iterator referencing location of first after removal.
1335 * Removes the characters in the range [first,last) from this string.
1336 * The value of the string doesn't change if an error is thrown.
1339 erase(iterator __first, iterator __last);
1342 * @brief Replace characters with value from another string.
1343 * @param pos Index of first character to replace.
1344 * @param n Number of characters to be replaced.
1345 * @param str String to insert.
1346 * @return Reference to this string.
1347 * @throw std::out_of_range If @a pos is beyond the end of this
1349 * @throw std::length_error If new length exceeds @c max_size().
1351 * Removes the characters in the range [pos,pos+n) from this string.
1352 * In place, the value of @a str is inserted. If @a pos is beyond end
1353 * of string, out_of_range is thrown. If the length of the result
1354 * exceeds max_size(), length_error is thrown. The value of the string
1355 * doesn't change if an error is thrown.
1358 replace(size_type __pos, size_type __n, const basic_string& __str)
1359 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1362 * @brief Replace characters with value from another string.
1363 * @param pos1 Index of first character to replace.
1364 * @param n1 Number of characters to be replaced.
1365 * @param str String to insert.
1366 * @param pos2 Index of first character of str to use.
1367 * @param n2 Number of characters from str to use.
1368 * @return Reference to this string.
1369 * @throw std::out_of_range If @a pos1 > size() or @a pos2 >
1371 * @throw std::length_error If new length exceeds @c max_size().
1373 * Removes the characters in the range [pos1,pos1 + n) from this
1374 * string. In place, the value of @a str is inserted. If @a pos is
1375 * beyond end of string, out_of_range is thrown. If the length of the
1376 * result exceeds max_size(), length_error is thrown. The value of the
1377 * string doesn't change if an error is thrown.
1380 replace(size_type __pos1, size_type __n1, const basic_string& __str,
1381 size_type __pos2, size_type __n2)
1382 { return this->replace(__pos1, __n1, __str._M_data()
1383 + __str._M_check(__pos2, "basic_string::replace"),
1384 __str._M_limit(__pos2, __n2)); }
1387 * @brief Replace characters with value of a C substring.
1388 * @param pos Index of first character to replace.
1389 * @param n1 Number of characters to be replaced.
1390 * @param s C string to insert.
1391 * @param n2 Number of characters from @a s to use.
1392 * @return Reference to this string.
1393 * @throw std::out_of_range If @a pos1 > size().
1394 * @throw std::length_error If new length exceeds @c max_size().
1396 * Removes the characters in the range [pos,pos + n1) from this string.
1397 * In place, the first @a n2 characters of @a s are inserted, or all
1398 * of @a s if @a n2 is too large. If @a pos is beyond end of string,
1399 * out_of_range is thrown. If the length of result exceeds max_size(),
1400 * length_error is thrown. The value of the string doesn't change if
1401 * an error is thrown.
1404 replace(size_type __pos, size_type __n1, const _CharT* __s,
1408 * @brief Replace characters with value of a C string.
1409 * @param pos Index of first character to replace.
1410 * @param n1 Number of characters to be replaced.
1411 * @param s C string to insert.
1412 * @return Reference to this string.
1413 * @throw std::out_of_range If @a pos > size().
1414 * @throw std::length_error If new length exceeds @c max_size().
1416 * Removes the characters in the range [pos,pos + n1) from this string.
1417 * In place, the first @a n characters of @a s are inserted. If @a
1418 * pos is beyond end of string, out_of_range is thrown. If the length
1419 * of result exceeds max_size(), length_error is thrown. The value of
1420 * the string doesn't change if an error is thrown.
1423 replace(size_type __pos, size_type __n1, const _CharT* __s)
1425 __glibcxx_requires_string(__s);
1426 return this->replace(__pos, __n1, __s, traits_type::length(__s));
1430 * @brief Replace characters with multiple characters.
1431 * @param pos Index of first character to replace.
1432 * @param n1 Number of characters to be replaced.
1433 * @param n2 Number of characters to insert.
1434 * @param c Character to insert.
1435 * @return Reference to this string.
1436 * @throw std::out_of_range If @a pos > size().
1437 * @throw std::length_error If new length exceeds @c max_size().
1439 * Removes the characters in the range [pos,pos + n1) from this string.
1440 * In place, @a n2 copies of @a c are inserted. If @a pos is beyond
1441 * end of string, out_of_range is thrown. If the length of result
1442 * exceeds max_size(), length_error is thrown. The value of the string
1443 * doesn't change if an error is thrown.
1446 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1447 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
1448 _M_limit(__pos, __n1), __n2, __c); }
1451 * @brief Replace range of characters with string.
1452 * @param i1 Iterator referencing start of range to replace.
1453 * @param i2 Iterator referencing end of range to replace.
1454 * @param str String value to insert.
1455 * @return Reference to this string.
1456 * @throw std::length_error If new length exceeds @c max_size().
1458 * Removes the characters in the range [i1,i2). In place, the value of
1459 * @a str is inserted. If the length of result exceeds max_size(),
1460 * length_error is thrown. The value of the string doesn't change if
1461 * an error is thrown.
1464 replace(iterator __i1, iterator __i2, const basic_string& __str)
1465 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
1468 * @brief Replace range of characters with C substring.
1469 * @param i1 Iterator referencing start of range to replace.
1470 * @param i2 Iterator referencing end of range to replace.
1471 * @param s C string value to insert.
1472 * @param n Number of characters from s to insert.
1473 * @return Reference to this string.
1474 * @throw std::length_error If new length exceeds @c max_size().
1476 * Removes the characters in the range [i1,i2). In place, the first @a
1477 * n characters of @a s are inserted. If the length of result exceeds
1478 * max_size(), length_error is thrown. The value of the string doesn't
1479 * change if an error is thrown.
1482 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
1484 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1485 && __i2 <= _M_iend());
1486 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
1490 * @brief Replace range of characters with C string.
1491 * @param i1 Iterator referencing start of range to replace.
1492 * @param i2 Iterator referencing end of range to replace.
1493 * @param s C string value to insert.
1494 * @return Reference to this string.
1495 * @throw std::length_error If new length exceeds @c max_size().
1497 * Removes the characters in the range [i1,i2). In place, the
1498 * characters of @a s are inserted. If the length of result exceeds
1499 * max_size(), length_error is thrown. The value of the string doesn't
1500 * change if an error is thrown.
1503 replace(iterator __i1, iterator __i2, const _CharT* __s)
1505 __glibcxx_requires_string(__s);
1506 return this->replace(__i1, __i2, __s, traits_type::length(__s));
1510 * @brief Replace range of characters with multiple characters
1511 * @param i1 Iterator referencing start of range to replace.
1512 * @param i2 Iterator referencing end of range to replace.
1513 * @param n Number of characters to insert.
1514 * @param c Character to insert.
1515 * @return Reference to this string.
1516 * @throw std::length_error If new length exceeds @c max_size().
1518 * Removes the characters in the range [i1,i2). In place, @a n copies
1519 * of @a c are inserted. If the length of result exceeds max_size(),
1520 * length_error is thrown. The value of the string doesn't change if
1521 * an error is thrown.
1524 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
1526 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1527 && __i2 <= _M_iend());
1528 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
1532 * @brief Replace range of characters with range.
1533 * @param i1 Iterator referencing start of range to replace.
1534 * @param i2 Iterator referencing end of range to replace.
1535 * @param k1 Iterator referencing start of range to insert.
1536 * @param k2 Iterator referencing end of range to insert.
1537 * @return Reference to this string.
1538 * @throw std::length_error If new length exceeds @c max_size().
1540 * Removes the characters in the range [i1,i2). In place, characters
1541 * in the range [k1,k2) are inserted. If the length of result exceeds
1542 * max_size(), length_error is thrown. The value of the string doesn't
1543 * change if an error is thrown.
1545 template<class _InputIterator>
1547 replace(iterator __i1, iterator __i2,
1548 _InputIterator __k1, _InputIterator __k2)
1550 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1551 && __i2 <= _M_iend());
1552 __glibcxx_requires_valid_range(__k1, __k2);
1553 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1554 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
1557 // Specializations for the common case of pointer and iterator:
1558 // useful to avoid the overhead of temporary buffering in _M_replace.
1560 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
1562 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1563 && __i2 <= _M_iend());
1564 __glibcxx_requires_valid_range(__k1, __k2);
1565 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1570 replace(iterator __i1, iterator __i2,
1571 const _CharT* __k1, const _CharT* __k2)
1573 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1574 && __i2 <= _M_iend());
1575 __glibcxx_requires_valid_range(__k1, __k2);
1576 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1581 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
1583 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1584 && __i2 <= _M_iend());
1585 __glibcxx_requires_valid_range(__k1, __k2);
1586 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1587 __k1.base(), __k2 - __k1);
1591 replace(iterator __i1, iterator __i2,
1592 const_iterator __k1, const_iterator __k2)
1594 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1595 && __i2 <= _M_iend());
1596 __glibcxx_requires_valid_range(__k1, __k2);
1597 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1598 __k1.base(), __k2 - __k1);
1601 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1603 * @brief Replace range of characters with initializer_list.
1604 * @param i1 Iterator referencing start of range to replace.
1605 * @param i2 Iterator referencing end of range to replace.
1606 * @param l The initializer_list of characters to insert.
1607 * @return Reference to this string.
1608 * @throw std::length_error If new length exceeds @c max_size().
1610 * Removes the characters in the range [i1,i2). In place, characters
1611 * in the range [k1,k2) are inserted. If the length of result exceeds
1612 * max_size(), length_error is thrown. The value of the string doesn't
1613 * change if an error is thrown.
1615 basic_string& replace(iterator __i1, iterator __i2,
1616 initializer_list<_CharT> __l)
1617 { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
1618 #endif // __GXX_EXPERIMENTAL_CXX0X__
1621 template<class _Integer>
1623 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
1624 _Integer __val, __true_type)
1625 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
1627 template<class _InputIterator>
1629 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
1630 _InputIterator __k2, __false_type);
1633 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
1637 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
1640 // _S_construct_aux is used to implement the 21.3.1 para 15 which
1641 // requires special behaviour if _InIter is an integral type
1642 template<class _InIterator>
1644 _S_construct_aux(_InIterator __beg, _InIterator __end,
1645 const _Alloc& __a, __false_type)
1647 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
1648 return _S_construct(__beg, __end, __a, _Tag());
1651 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1652 // 438. Ambiguity in the "do the right thing" clause
1653 template<class _Integer>
1655 _S_construct_aux(_Integer __beg, _Integer __end,
1656 const _Alloc& __a, __true_type)
1657 { return _S_construct_aux_2(static_cast<size_type>(__beg),
1661 _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
1662 { return _S_construct(__req, __c, __a); }
1664 template<class _InIterator>
1666 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
1668 typedef typename std::__is_integer<_InIterator>::__type _Integral;
1669 return _S_construct_aux(__beg, __end, __a, _Integral());
1672 // For Input Iterators, used in istreambuf_iterators, etc.
1673 template<class _InIterator>
1675 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1676 input_iterator_tag);
1678 // For forward_iterators up to random_access_iterators, used for
1679 // string::iterator, _CharT*, etc.
1680 template<class _FwdIterator>
1682 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
1683 forward_iterator_tag);
1686 _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
1691 * @brief Copy substring into C string.
1692 * @param s C string to copy value into.
1693 * @param n Number of characters to copy.
1694 * @param pos Index of first character to copy.
1695 * @return Number of characters actually copied
1696 * @throw std::out_of_range If pos > size().
1698 * Copies up to @a n characters starting at @a pos into the C string @a
1699 * s. If @a pos is greater than size(), out_of_range is thrown.
1702 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
1705 * @brief Swap contents with another string.
1706 * @param s String to swap with.
1708 * Exchanges the contents of this string with that of @a s in constant
1712 swap(basic_string& __s);
1714 // String operations:
1716 * @brief Return const pointer to null-terminated contents.
1718 * This is a handle to internal data. Do not modify or dire things may
1723 { return _M_data(); }
1726 * @brief Return const pointer to contents.
1728 * This is a handle to internal data. Do not modify or dire things may
1733 { return _M_data(); }
1736 * @brief Return copy of allocator used to construct this string.
1739 get_allocator() const
1740 { return _M_dataplus; }
1743 * @brief Find position of a C substring.
1744 * @param s C string to locate.
1745 * @param pos Index of character to search from.
1746 * @param n Number of characters from @a s to search for.
1747 * @return Index of start of first occurrence.
1749 * Starting from @a pos, searches forward for the first @a n characters
1750 * in @a s within this string. If found, returns the index where it
1751 * begins. If not found, returns npos.
1754 find(const _CharT* __s, size_type __pos, size_type __n) const;
1757 * @brief Find position of a string.
1758 * @param str String to locate.
1759 * @param pos Index of character to search from (default 0).
1760 * @return Index of start of first occurrence.
1762 * Starting from @a pos, searches forward for value of @a str within
1763 * this string. If found, returns the index where it begins. If not
1764 * found, returns npos.
1767 find(const basic_string& __str, size_type __pos = 0) const
1768 { return this->find(__str.data(), __pos, __str.size()); }
1771 * @brief Find position of a C string.
1772 * @param s C string to locate.
1773 * @param pos Index of character to search from (default 0).
1774 * @return Index of start of first occurrence.
1776 * Starting from @a pos, searches forward for the value of @a s within
1777 * this string. If found, returns the index where it begins. If not
1778 * found, returns npos.
1781 find(const _CharT* __s, size_type __pos = 0) const
1783 __glibcxx_requires_string(__s);
1784 return this->find(__s, __pos, traits_type::length(__s));
1788 * @brief Find position of a character.
1789 * @param c Character to locate.
1790 * @param pos Index of character to search from (default 0).
1791 * @return Index of first occurrence.
1793 * Starting from @a pos, searches forward for @a c within this string.
1794 * If found, returns the index where it was found. If not found,
1798 find(_CharT __c, size_type __pos = 0) const;
1801 * @brief Find last position of a string.
1802 * @param str String to locate.
1803 * @param pos Index of character to search back from (default end).
1804 * @return Index of start of last occurrence.
1806 * Starting from @a pos, searches backward for value of @a str within
1807 * this string. If found, returns the index where it begins. If not
1808 * found, returns npos.
1811 rfind(const basic_string& __str, size_type __pos = npos) const
1812 { return this->rfind(__str.data(), __pos, __str.size()); }
1815 * @brief Find last position of a C substring.
1816 * @param s C string to locate.
1817 * @param pos Index of character to search back from.
1818 * @param n Number of characters from s to search for.
1819 * @return Index of start of last occurrence.
1821 * Starting from @a pos, searches backward for the first @a n
1822 * characters in @a s within this string. If found, returns the index
1823 * where it begins. If not found, returns npos.
1826 rfind(const _CharT* __s, size_type __pos, size_type __n) const;
1829 * @brief Find last position of a C string.
1830 * @param s C string to locate.
1831 * @param pos Index of character to start search at (default end).
1832 * @return Index of start of last occurrence.
1834 * Starting from @a pos, searches backward for the value of @a s within
1835 * this string. If found, returns the index where it begins. If not
1836 * found, returns npos.
1839 rfind(const _CharT* __s, size_type __pos = npos) const
1841 __glibcxx_requires_string(__s);
1842 return this->rfind(__s, __pos, traits_type::length(__s));
1846 * @brief Find last position of a character.
1847 * @param c Character to locate.
1848 * @param pos Index of character to search back from (default end).
1849 * @return Index of last occurrence.
1851 * Starting from @a pos, searches backward for @a c within this string.
1852 * If found, returns the index where it was found. If not found,
1856 rfind(_CharT __c, size_type __pos = npos) const;
1859 * @brief Find position of a character of string.
1860 * @param str String containing characters to locate.
1861 * @param pos Index of character to search from (default 0).
1862 * @return Index of first occurrence.
1864 * Starting from @a pos, searches forward for one of the characters of
1865 * @a str within this string. If found, returns the index where it was
1866 * found. If not found, returns npos.
1869 find_first_of(const basic_string& __str, size_type __pos = 0) const
1870 { return this->find_first_of(__str.data(), __pos, __str.size()); }
1873 * @brief Find position of a character of C substring.
1874 * @param s String containing characters to locate.
1875 * @param pos Index of character to search from.
1876 * @param n Number of characters from s to search for.
1877 * @return Index of first occurrence.
1879 * Starting from @a pos, searches forward for one of the first @a n
1880 * characters of @a s within this string. If found, returns the index
1881 * where it was found. If not found, returns npos.
1884 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
1887 * @brief Find position of a character of C string.
1888 * @param s String containing characters to locate.
1889 * @param pos Index of character to search from (default 0).
1890 * @return Index of first occurrence.
1892 * Starting from @a pos, searches forward for one of the characters of
1893 * @a s within this string. If found, returns the index where it was
1894 * found. If not found, returns npos.
1897 find_first_of(const _CharT* __s, size_type __pos = 0) const
1899 __glibcxx_requires_string(__s);
1900 return this->find_first_of(__s, __pos, traits_type::length(__s));
1904 * @brief Find position of a character.
1905 * @param c Character to locate.
1906 * @param pos Index of character to search from (default 0).
1907 * @return Index of first occurrence.
1909 * Starting from @a pos, searches forward for the character @a c within
1910 * this string. If found, returns the index where it was found. If
1911 * not found, returns npos.
1913 * Note: equivalent to find(c, pos).
1916 find_first_of(_CharT __c, size_type __pos = 0) const
1917 { return this->find(__c, __pos); }
1920 * @brief Find last position of a character of string.
1921 * @param str String containing characters to locate.
1922 * @param pos Index of character to search back from (default end).
1923 * @return Index of last occurrence.
1925 * Starting from @a pos, searches backward for one of the characters of
1926 * @a str within this string. If found, returns the index where it was
1927 * found. If not found, returns npos.
1930 find_last_of(const basic_string& __str, size_type __pos = npos) const
1931 { return this->find_last_of(__str.data(), __pos, __str.size()); }
1934 * @brief Find last position of a character of C substring.
1935 * @param s C string containing characters to locate.
1936 * @param pos Index of character to search back from.
1937 * @param n Number of characters from s to search for.
1938 * @return Index of last occurrence.
1940 * Starting from @a pos, searches backward for one of the first @a n
1941 * characters of @a s within this string. If found, returns the index
1942 * where it was found. If not found, returns npos.
1945 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
1948 * @brief Find last position of a character of C string.
1949 * @param s C string containing characters to locate.
1950 * @param pos Index of character to search back from (default end).
1951 * @return Index of last occurrence.
1953 * Starting from @a pos, searches backward for one of the characters of
1954 * @a s within this string. If found, returns the index where it was
1955 * found. If not found, returns npos.
1958 find_last_of(const _CharT* __s, size_type __pos = npos) const
1960 __glibcxx_requires_string(__s);
1961 return this->find_last_of(__s, __pos, traits_type::length(__s));
1965 * @brief Find last position of a character.
1966 * @param c Character to locate.
1967 * @param pos Index of character to search back from (default end).
1968 * @return Index of last occurrence.
1970 * Starting from @a pos, searches backward for @a c within this string.
1971 * If found, returns the index where it was found. If not found,
1974 * Note: equivalent to rfind(c, pos).
1977 find_last_of(_CharT __c, size_type __pos = npos) const
1978 { return this->rfind(__c, __pos); }
1981 * @brief Find position of a character not in string.
1982 * @param str String containing characters to avoid.
1983 * @param pos Index of character to search from (default 0).
1984 * @return Index of first occurrence.
1986 * Starting from @a pos, searches forward for a character not contained
1987 * in @a str within this string. If found, returns the index where it
1988 * was found. If not found, returns npos.
1991 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
1992 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
1995 * @brief Find position of a character not in C substring.
1996 * @param s C string containing characters to avoid.
1997 * @param pos Index of character to search from.
1998 * @param n Number of characters from s to consider.
1999 * @return Index of first occurrence.
2001 * Starting from @a pos, searches forward for a character not contained
2002 * in the first @a n characters of @a s within this string. If found,
2003 * returns the index where it was found. If not found, returns npos.
2006 find_first_not_of(const _CharT* __s, size_type __pos,
2007 size_type __n) const;
2010 * @brief Find position of a character not in C string.
2011 * @param s C string containing characters to avoid.
2012 * @param pos Index of character to search from (default 0).
2013 * @return Index of first occurrence.
2015 * Starting from @a pos, searches forward for a character not contained
2016 * in @a s within this string. If found, returns the index where it
2017 * was found. If not found, returns npos.
2020 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
2022 __glibcxx_requires_string(__s);
2023 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
2027 * @brief Find position of a different character.
2028 * @param c Character to avoid.
2029 * @param pos Index of character to search from (default 0).
2030 * @return Index of first occurrence.
2032 * Starting from @a pos, searches forward for a character other than @a c
2033 * within this string. If found, returns the index where it was found.
2034 * If not found, returns npos.
2037 find_first_not_of(_CharT __c, size_type __pos = 0) const;
2040 * @brief Find last position of a character not in string.
2041 * @param str String containing characters to avoid.
2042 * @param pos Index of character to search back from (default end).
2043 * @return Index of last occurrence.
2045 * Starting from @a pos, searches backward for a character not
2046 * contained in @a str within this string. If found, returns the index
2047 * where it was found. If not found, returns npos.
2050 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
2051 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
2054 * @brief Find last position of a character not in C substring.
2055 * @param s C string containing characters to avoid.
2056 * @param pos Index of character to search back from.
2057 * @param n Number of characters from s to consider.
2058 * @return Index of last occurrence.
2060 * Starting from @a pos, searches backward for a character not
2061 * contained in the first @a n characters of @a s within this string.
2062 * If found, returns the index where it was found. If not found,
2066 find_last_not_of(const _CharT* __s, size_type __pos,
2067 size_type __n) const;
2069 * @brief Find last position of a character not in C string.
2070 * @param s C string containing characters to avoid.
2071 * @param pos Index of character to search back from (default end).
2072 * @return Index of last occurrence.
2074 * Starting from @a pos, searches backward for a character not
2075 * contained in @a s within this string. If found, returns the index
2076 * where it was found. If not found, returns npos.
2079 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
2081 __glibcxx_requires_string(__s);
2082 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
2086 * @brief Find last position of a different character.
2087 * @param c Character to avoid.
2088 * @param pos Index of character to search back from (default end).
2089 * @return Index of last occurrence.
2091 * Starting from @a pos, searches backward for a character other than
2092 * @a c within this string. If found, returns the index where it was
2093 * found. If not found, returns npos.
2096 find_last_not_of(_CharT __c, size_type __pos = npos) const;
2099 * @brief Get a substring.
2100 * @param pos Index of first character (default 0).
2101 * @param n Number of characters in substring (default remainder).
2102 * @return The new string.
2103 * @throw std::out_of_range If pos > size().
2105 * Construct and return a new string using the @a n characters starting
2106 * at @a pos. If the string is too short, use the remainder of the
2107 * characters. If @a pos is beyond the end of the string, out_of_range
2111 substr(size_type __pos = 0, size_type __n = npos) const
2112 { return basic_string(*this,
2113 _M_check(__pos, "basic_string::substr"), __n); }
2116 * @brief Compare to a string.
2117 * @param str String to compare against.
2118 * @return Integer < 0, 0, or > 0.
2120 * Returns an integer < 0 if this string is ordered before @a str, 0 if
2121 * their values are equivalent, or > 0 if this string is ordered after
2122 * @a str. Determines the effective length rlen of the strings to
2123 * compare as the smallest of size() and str.size(). The function
2124 * then compares the two strings by calling traits::compare(data(),
2125 * str.data(),rlen). If the result of the comparison is nonzero returns
2126 * it, otherwise the shorter one is ordered first.
2129 compare(const basic_string& __str) const
2131 const size_type __size = this->size();
2132 const size_type __osize = __str.size();
2133 const size_type __len = std::min(__size, __osize);
2135 int __r = traits_type::compare(_M_data(), __str.data(), __len);
2137 __r = _S_compare(__size, __osize);
2142 * @brief Compare substring to a string.
2143 * @param pos Index of first character of substring.
2144 * @param n Number of characters in substring.
2145 * @param str String to compare against.
2146 * @return Integer < 0, 0, or > 0.
2148 * Form the substring of this string from the @a n characters starting
2149 * at @a pos. Returns an integer < 0 if the substring is ordered
2150 * before @a str, 0 if their values are equivalent, or > 0 if the
2151 * substring is ordered after @a str. Determines the effective length
2152 * rlen of the strings to compare as the smallest of the length of the
2153 * substring and @a str.size(). The function then compares the two
2154 * strings by calling traits::compare(substring.data(),str.data(),rlen).
2155 * If the result of the comparison is nonzero returns it, otherwise the
2156 * shorter one is ordered first.
2159 compare(size_type __pos, size_type __n, const basic_string& __str) const;
2162 * @brief Compare substring to a substring.
2163 * @param pos1 Index of first character of substring.
2164 * @param n1 Number of characters in substring.
2165 * @param str String to compare against.
2166 * @param pos2 Index of first character of substring of str.
2167 * @param n2 Number of characters in substring of str.
2168 * @return Integer < 0, 0, or > 0.
2170 * Form the substring of this string from the @a n1 characters starting
2171 * at @a pos1. Form the substring of @a str from the @a n2 characters
2172 * starting at @a pos2. Returns an integer < 0 if this substring is
2173 * ordered before the substring of @a str, 0 if their values are
2174 * equivalent, or > 0 if this substring is ordered after the substring
2175 * of @a str. Determines the effective length rlen of the strings
2176 * to compare as the smallest of the lengths of the substrings. The
2177 * function then compares the two strings by calling
2178 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
2179 * If the result of the comparison is nonzero returns it, otherwise the
2180 * shorter one is ordered first.
2183 compare(size_type __pos1, size_type __n1, const basic_string& __str,
2184 size_type __pos2, size_type __n2) const;
2187 * @brief Compare to a C string.
2188 * @param s C string to compare against.
2189 * @return Integer < 0, 0, or > 0.
2191 * Returns an integer < 0 if this string is ordered before @a s, 0 if
2192 * their values are equivalent, or > 0 if this string is ordered after
2193 * @a s. Determines the effective length rlen of the strings to
2194 * compare as the smallest of size() and the length of a string
2195 * constructed from @a s. The function then compares the two strings
2196 * by calling traits::compare(data(),s,rlen). If the result of the
2197 * comparison is nonzero returns it, otherwise the shorter one is
2201 compare(const _CharT* __s) const;
2203 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2204 // 5 String::compare specification questionable
2206 * @brief Compare substring to a C string.
2207 * @param pos Index of first character of substring.
2208 * @param n1 Number of characters in substring.
2209 * @param s C string to compare against.
2210 * @return Integer < 0, 0, or > 0.
2212 * Form the substring of this string from the @a n1 characters starting
2213 * at @a pos. Returns an integer < 0 if the substring is ordered
2214 * before @a s, 0 if their values are equivalent, or > 0 if the
2215 * substring is ordered after @a s. Determines the effective length
2216 * rlen of the strings to compare as the smallest of the length of the
2217 * substring and the length of a string constructed from @a s. The
2218 * function then compares the two string by calling
2219 * traits::compare(substring.data(),s,rlen). If the result of the
2220 * comparison is nonzero returns it, otherwise the shorter one is
2224 compare(size_type __pos, size_type __n1, const _CharT* __s) const;
2227 * @brief Compare substring against a character array.
2228 * @param pos1 Index of first character of substring.
2229 * @param n1 Number of characters in substring.
2230 * @param s character array to compare against.
2231 * @param n2 Number of characters of s.
2232 * @return Integer < 0, 0, or > 0.
2234 * Form the substring of this string from the @a n1 characters starting
2235 * at @a pos1. Form a string from the first @a n2 characters of @a s.
2236 * Returns an integer < 0 if this substring is ordered before the string
2237 * from @a s, 0 if their values are equivalent, or > 0 if this substring
2238 * is ordered after the string from @a s. Determines the effective
2239 * length rlen of the strings to compare as the smallest of the length
2240 * of the substring and @a n2. The function then compares the two
2241 * strings by calling traits::compare(substring.data(),s,rlen). If the
2242 * result of the comparison is nonzero returns it, otherwise the shorter
2243 * one is ordered first.
2245 * NB: s must have at least n2 characters, '\\0' has
2246 * no special meaning.
2249 compare(size_type __pos, size_type __n1, const _CharT* __s,
2250 size_type __n2) const;
2255 * @brief Concatenate two strings.
2256 * @param lhs First string.
2257 * @param rhs Last string.
2258 * @return New string with value of @a lhs followed by @a rhs.
2260 template<typename _CharT, typename _Traits, typename _Alloc>
2261 basic_string<_CharT, _Traits, _Alloc>
2262 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2263 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2265 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2266 __str.append(__rhs);
2271 * @brief Concatenate C string and string.
2272 * @param lhs First string.
2273 * @param rhs Last string.
2274 * @return New string with value of @a lhs followed by @a rhs.
2276 template<typename _CharT, typename _Traits, typename _Alloc>
2277 basic_string<_CharT,_Traits,_Alloc>
2278 operator+(const _CharT* __lhs,
2279 const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2282 * @brief Concatenate character and string.
2283 * @param lhs First string.
2284 * @param rhs Last string.
2285 * @return New string with @a lhs followed by @a rhs.
2287 template<typename _CharT, typename _Traits, typename _Alloc>
2288 basic_string<_CharT,_Traits,_Alloc>
2289 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2292 * @brief Concatenate string and C string.
2293 * @param lhs First string.
2294 * @param rhs Last string.
2295 * @return New string with @a lhs followed by @a rhs.
2297 template<typename _CharT, typename _Traits, typename _Alloc>
2298 inline basic_string<_CharT, _Traits, _Alloc>
2299 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2300 const _CharT* __rhs)
2302 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2303 __str.append(__rhs);
2308 * @brief Concatenate string and character.
2309 * @param lhs First string.
2310 * @param rhs Last string.
2311 * @return New string with @a lhs followed by @a rhs.
2313 template<typename _CharT, typename _Traits, typename _Alloc>
2314 inline basic_string<_CharT, _Traits, _Alloc>
2315 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
2317 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
2318 typedef typename __string_type::size_type __size_type;
2319 __string_type __str(__lhs);
2320 __str.append(__size_type(1), __rhs);
2326 * @brief Test equivalence of two strings.
2327 * @param lhs First string.
2328 * @param rhs Second string.
2329 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2331 template<typename _CharT, typename _Traits, typename _Alloc>
2333 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2334 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2335 { return __lhs.compare(__rhs) == 0; }
2337 template<typename _CharT>
2339 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type
2340 operator==(const basic_string<_CharT>& __lhs,
2341 const basic_string<_CharT>& __rhs)
2342 { return (__lhs.size() == __rhs.size()
2343 && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
2347 * @brief Test equivalence of C string and string.
2348 * @param lhs C string.
2349 * @param rhs String.
2350 * @return True if @a rhs.compare(@a lhs) == 0. False otherwise.
2352 template<typename _CharT, typename _Traits, typename _Alloc>
2354 operator==(const _CharT* __lhs,
2355 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2356 { return __rhs.compare(__lhs) == 0; }
2359 * @brief Test equivalence of string and C string.
2360 * @param lhs String.
2361 * @param rhs C string.
2362 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2364 template<typename _CharT, typename _Traits, typename _Alloc>
2366 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2367 const _CharT* __rhs)
2368 { return __lhs.compare(__rhs) == 0; }
2372 * @brief Test difference of two strings.
2373 * @param lhs First string.
2374 * @param rhs Second string.
2375 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2377 template<typename _CharT, typename _Traits, typename _Alloc>
2379 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2380 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2381 { return !(__lhs == __rhs); }
2384 * @brief Test difference of C string and string.
2385 * @param lhs C string.
2386 * @param rhs String.
2387 * @return True if @a rhs.compare(@a lhs) != 0. False otherwise.
2389 template<typename _CharT, typename _Traits, typename _Alloc>
2391 operator!=(const _CharT* __lhs,
2392 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2393 { return !(__lhs == __rhs); }
2396 * @brief Test difference of string and C string.
2397 * @param lhs String.
2398 * @param rhs C string.
2399 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2401 template<typename _CharT, typename _Traits, typename _Alloc>
2403 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2404 const _CharT* __rhs)
2405 { return !(__lhs == __rhs); }
2409 * @brief Test if string precedes string.
2410 * @param lhs First string.
2411 * @param rhs Second string.
2412 * @return True if @a lhs precedes @a rhs. False otherwise.
2414 template<typename _CharT, typename _Traits, typename _Alloc>
2416 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2417 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2418 { return __lhs.compare(__rhs) < 0; }
2421 * @brief Test if string precedes C string.
2422 * @param lhs String.
2423 * @param rhs C string.
2424 * @return True if @a lhs precedes @a rhs. False otherwise.
2426 template<typename _CharT, typename _Traits, typename _Alloc>
2428 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2429 const _CharT* __rhs)
2430 { return __lhs.compare(__rhs) < 0; }
2433 * @brief Test if C string precedes string.
2434 * @param lhs C string.
2435 * @param rhs String.
2436 * @return True if @a lhs precedes @a rhs. False otherwise.
2438 template<typename _CharT, typename _Traits, typename _Alloc>
2440 operator<(const _CharT* __lhs,
2441 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2442 { return __rhs.compare(__lhs) > 0; }
2446 * @brief Test if string follows string.
2447 * @param lhs First string.
2448 * @param rhs Second string.
2449 * @return True if @a lhs follows @a rhs. False otherwise.
2451 template<typename _CharT, typename _Traits, typename _Alloc>
2453 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2454 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2455 { return __lhs.compare(__rhs) > 0; }
2458 * @brief Test if string follows C string.
2459 * @param lhs String.
2460 * @param rhs C string.
2461 * @return True if @a lhs follows @a rhs. False otherwise.
2463 template<typename _CharT, typename _Traits, typename _Alloc>
2465 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2466 const _CharT* __rhs)
2467 { return __lhs.compare(__rhs) > 0; }
2470 * @brief Test if C string follows string.
2471 * @param lhs C string.
2472 * @param rhs String.
2473 * @return True if @a lhs follows @a rhs. False otherwise.
2475 template<typename _CharT, typename _Traits, typename _Alloc>
2477 operator>(const _CharT* __lhs,
2478 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2479 { return __rhs.compare(__lhs) < 0; }
2483 * @brief Test if string doesn't follow string.
2484 * @param lhs First string.
2485 * @param rhs Second string.
2486 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2488 template<typename _CharT, typename _Traits, typename _Alloc>
2490 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2491 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2492 { return __lhs.compare(__rhs) <= 0; }
2495 * @brief Test if string doesn't follow C string.
2496 * @param lhs String.
2497 * @param rhs C string.
2498 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2500 template<typename _CharT, typename _Traits, typename _Alloc>
2502 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2503 const _CharT* __rhs)
2504 { return __lhs.compare(__rhs) <= 0; }
2507 * @brief Test if C string doesn't follow string.
2508 * @param lhs C string.
2509 * @param rhs String.
2510 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2512 template<typename _CharT, typename _Traits, typename _Alloc>
2514 operator<=(const _CharT* __lhs,
2515 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2516 { return __rhs.compare(__lhs) >= 0; }
2520 * @brief Test if string doesn't precede string.
2521 * @param lhs First string.
2522 * @param rhs Second string.
2523 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2525 template<typename _CharT, typename _Traits, typename _Alloc>
2527 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2528 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2529 { return __lhs.compare(__rhs) >= 0; }
2532 * @brief Test if string doesn't precede C string.
2533 * @param lhs String.
2534 * @param rhs C string.
2535 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2537 template<typename _CharT, typename _Traits, typename _Alloc>
2539 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2540 const _CharT* __rhs)
2541 { return __lhs.compare(__rhs) >= 0; }
2544 * @brief Test if C string doesn't precede string.
2545 * @param lhs C string.
2546 * @param rhs String.
2547 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2549 template<typename _CharT, typename _Traits, typename _Alloc>
2551 operator>=(const _CharT* __lhs,
2552 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2553 { return __rhs.compare(__lhs) <= 0; }
2556 * @brief Swap contents of two strings.
2557 * @param lhs First string.
2558 * @param rhs Second string.
2560 * Exchanges the contents of @a lhs and @a rhs in constant time.
2562 template<typename _CharT, typename _Traits, typename _Alloc>
2564 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
2565 basic_string<_CharT, _Traits, _Alloc>& __rhs)
2566 { __lhs.swap(__rhs); }
2569 * @brief Read stream into a string.
2570 * @param is Input stream.
2571 * @param str Buffer to store into.
2572 * @return Reference to the input stream.
2574 * Stores characters from @a is into @a str until whitespace is found, the
2575 * end of the stream is encountered, or str.max_size() is reached. If
2576 * is.width() is non-zero, that is the limit on the number of characters
2577 * stored into @a str. Any previous contents of @a str are erased.
2579 template<typename _CharT, typename _Traits, typename _Alloc>
2580 basic_istream<_CharT, _Traits>&
2581 operator>>(basic_istream<_CharT, _Traits>& __is,
2582 basic_string<_CharT, _Traits, _Alloc>& __str);
2585 basic_istream<char>&
2586 operator>>(basic_istream<char>& __is, basic_string<char>& __str);
2589 * @brief Write string to a stream.
2590 * @param os Output stream.
2591 * @param str String to write out.
2592 * @return Reference to the output stream.
2594 * Output characters of @a str into os following the same rules as for
2595 * writing a C string.
2597 template<typename _CharT, typename _Traits, typename _Alloc>
2598 inline basic_ostream<_CharT, _Traits>&
2599 operator<<(basic_ostream<_CharT, _Traits>& __os,
2600 const basic_string<_CharT, _Traits, _Alloc>& __str)
2602 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2603 // 586. string inserter not a formatted function
2604 return __ostream_insert(__os, __str.data(), __str.size());
2608 * @brief Read a line from stream into a string.
2609 * @param is Input stream.
2610 * @param str Buffer to store into.
2611 * @param delim Character marking end of line.
2612 * @return Reference to the input stream.
2614 * Stores characters from @a is into @a str until @a delim is found, the
2615 * end of the stream is encountered, or str.max_size() is reached. If
2616 * is.width() is non-zero, that is the limit on the number of characters
2617 * stored into @a str. Any previous contents of @a str are erased. If @a
2618 * delim was encountered, it is extracted but not stored into @a str.
2620 template<typename _CharT, typename _Traits, typename _Alloc>
2621 basic_istream<_CharT, _Traits>&
2622 getline(basic_istream<_CharT, _Traits>& __is,
2623 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
2626 * @brief Read a line from stream into a string.
2627 * @param is Input stream.
2628 * @param str Buffer to store into.
2629 * @return Reference to the input stream.
2631 * Stores characters from is into @a str until '\n' is
2632 * found, the end of the stream is encountered, or str.max_size()
2633 * is reached. If is.width() is non-zero, that is the limit on the
2634 * number of characters stored into @a str. Any previous contents
2635 * of @a str are erased. If end of line was encountered, it is
2636 * extracted but not stored into @a str.
2638 template<typename _CharT, typename _Traits, typename _Alloc>
2639 inline basic_istream<_CharT, _Traits>&
2640 getline(basic_istream<_CharT, _Traits>& __is,
2641 basic_string<_CharT, _Traits, _Alloc>& __str)
2642 { return getline(__is, __str, __is.widen('\n')); }
2645 basic_istream<char>&
2646 getline(basic_istream<char>& __in, basic_string<char>& __str,
2649 #ifdef _GLIBCXX_USE_WCHAR_T
2651 basic_istream<wchar_t>&
2652 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
2656 _GLIBCXX_END_NAMESPACE
2658 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) && defined(_GLIBCXX_USE_C99) \
2659 && !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))
2661 #include <ext/string_conversions.h>
2663 _GLIBCXX_BEGIN_NAMESPACE(std)
2665 // 21.4 Numeric Conversions [string.conversions].
2667 stoi(const string& __str, size_t* __idx = 0, int __base = 10)
2668 { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
2672 stol(const string& __str, size_t* __idx = 0, int __base = 10)
2673 { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
2676 inline unsigned long
2677 stoul(const string& __str, size_t* __idx = 0, int __base = 10)
2678 { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
2682 stoll(const string& __str, size_t* __idx = 0, int __base = 10)
2683 { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
2686 inline unsigned long long
2687 stoull(const string& __str, size_t* __idx = 0, int __base = 10)
2688 { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
2691 // NB: strtof vs strtod.
2693 stof(const string& __str, size_t* __idx = 0)
2694 { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
2697 stod(const string& __str, size_t* __idx = 0)
2698 { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
2701 stold(const string& __str, size_t* __idx = 0)
2702 { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
2704 // NB: (v)snprintf vs sprintf.
2708 to_string(int __val)
2709 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(int),
2713 to_string(unsigned __val)
2714 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
2715 4 * sizeof(unsigned),
2719 to_string(long __val)
2720 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(long),
2724 to_string(unsigned long __val)
2725 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
2726 4 * sizeof(unsigned long),
2730 to_string(long long __val)
2731 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
2732 4 * sizeof(long long),
2736 to_string(unsigned long long __val)
2737 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
2738 4 * sizeof(unsigned long long),
2742 to_string(float __val)
2745 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
2746 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
2751 to_string(double __val)
2754 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
2755 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
2760 to_string(long double __val)
2763 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
2764 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
2768 #ifdef _GLIBCXX_USE_WCHAR_T
2770 stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
2771 { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
2775 stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
2776 { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
2779 inline unsigned long
2780 stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
2781 { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
2785 stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
2786 { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
2789 inline unsigned long long
2790 stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
2791 { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
2794 // NB: wcstof vs wcstod.
2796 stof(const wstring& __str, size_t* __idx = 0)
2797 { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
2800 stod(const wstring& __str, size_t* __idx = 0)
2801 { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
2804 stold(const wstring& __str, size_t* __idx = 0)
2805 { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
2809 to_wstring(int __val)
2810 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int),
2814 to_wstring(unsigned __val)
2815 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
2816 4 * sizeof(unsigned),
2820 to_wstring(long __val)
2821 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long),
2825 to_wstring(unsigned long __val)
2826 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
2827 4 * sizeof(unsigned long),
2831 to_wstring(long long __val)
2832 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
2833 4 * sizeof(long long),
2837 to_wstring(unsigned long long __val)
2838 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
2839 4 * sizeof(unsigned long long),
2843 to_wstring(float __val)
2846 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
2847 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
2852 to_wstring(double __val)
2855 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
2856 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
2861 to_wstring(long double __val)
2864 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
2865 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
2870 _GLIBCXX_END_NAMESPACE
2872 #endif /* __GXX_EXPERIMENTAL_CXX0X__ && _GLIBCXX_USE_C99 ... */
2874 #ifdef __GXX_EXPERIMENTAL_CXX0X__
2876 #include <bits/functional_hash.h>
2878 _GLIBCXX_BEGIN_NAMESPACE(std)
2882 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
2883 /// std::hash specialization for string.
2886 : public std::unary_function<string, size_t>
2889 operator()(const string& __s) const
2890 { return _Fnv_hash<>::hash(__s.data(), __s.length()); }
2893 #ifdef _GLIBCXX_USE_WCHAR_T
2894 /// std::hash specialization for wstring.
2896 struct hash<wstring>
2897 : public std::unary_function<wstring, size_t>
2900 operator()(const wstring& __s) const
2902 const char* __p = reinterpret_cast<const char*>(__s.data());
2903 return _Fnv_hash<>::hash(__p, __s.length() * sizeof(wchar_t));
2907 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
2909 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
2910 /// std::hash specialization for u16string.
2912 struct hash<u16string>
2913 : public std::unary_function<u16string, size_t>
2916 operator()(const u16string& __s) const
2918 const char* __p = reinterpret_cast<const char*>(__s.data());
2919 return _Fnv_hash<>::hash(__p, __s.length() * sizeof(char16_t));
2923 /// std::hash specialization for u32string.
2925 struct hash<u32string>
2926 : public std::unary_function<u32string, size_t>
2929 operator()(const u32string& __s) const
2931 const char* __p = reinterpret_cast<const char*>(__s.data());
2932 return _Fnv_hash<>::hash(__p, __s.length() * sizeof(char32_t));
2937 _GLIBCXX_END_NAMESPACE
2939 #endif /* __GXX_EXPERIMENTAL_CXX0X__ */
2941 #endif /* _BASIC_STRING_H */