1 // Components for manipulating sequences of characters -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
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 2, 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 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING. If not, write to the Free
20 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction. Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License. This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
32 /** @file basic_string.h
33 * This is an internal header file, included by other library headers.
34 * You should not attempt to use it directly.
38 // ISO C++ 14882: 21 Strings library
41 #ifndef _BASIC_STRING_H
42 #define _BASIC_STRING_H 1
44 #pragma GCC system_header
46 #include <ext/atomicity.h>
47 #include <debug/debug.h>
49 _GLIBCXX_BEGIN_NAMESPACE(std)
52 * @class basic_string basic_string.h <string>
53 * @brief Managing sequences of characters and character-like objects.
58 * Meets the requirements of a <a href="tables.html#65">container</a>, a
59 * <a href="tables.html#66">reversible container</a>, and a
60 * <a href="tables.html#67">sequence</a>. Of the
61 * <a href="tables.html#68">optional sequence requirements</a>, only
62 * @c push_back, @c at, and array access are supported.
68 * Documentation? What's that?
69 * Nathan Myers <ncm@cantrip.org>.
71 * A string looks like this:
76 * [basic_string<char_type>] _M_capacity
77 * _M_dataplus _M_refcount
78 * _M_p ----------------> unnamed array of char_type
81 * Where the _M_p points to the first character in the string, and
82 * you cast it to a pointer-to-_Rep and subtract 1 to get a
83 * pointer to the header.
85 * This approach has the enormous advantage that a string object
86 * requires only one allocation. All the ugliness is confined
87 * within a single pair of inline functions, which each compile to
88 * a single "add" instruction: _Rep::_M_data(), and
89 * string::_M_rep(); and the allocation function which gets a
90 * block of raw bytes and with room enough and constructs a _Rep
91 * object at the front.
93 * The reason you want _M_data pointing to the character array and
94 * not the _Rep is so that the debugger can see the string
95 * contents. (Probably we should add a non-inline member to get
96 * the _Rep for the debugger to use, so users can check the actual
99 * Note that the _Rep object is a POD so that you can have a
100 * static "empty string" _Rep object already "constructed" before
101 * static constructors have run. The reference-count encoding is
102 * chosen so that a 0 indicates one reference, so you never try to
103 * destroy the empty-string _Rep object.
105 * All but the last paragraph is considered pretty conventional
106 * for a C++ string implementation.
109 // 21.3 Template class basic_string
110 template<typename _CharT, typename _Traits, typename _Alloc>
113 typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
117 typedef _Traits traits_type;
118 typedef typename _Traits::char_type value_type;
119 typedef _Alloc allocator_type;
120 typedef typename _CharT_alloc_type::size_type size_type;
121 typedef typename _CharT_alloc_type::difference_type difference_type;
122 typedef typename _CharT_alloc_type::reference reference;
123 typedef typename _CharT_alloc_type::const_reference const_reference;
124 typedef typename _CharT_alloc_type::pointer pointer;
125 typedef typename _CharT_alloc_type::const_pointer const_pointer;
126 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
127 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
129 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
130 typedef std::reverse_iterator<iterator> reverse_iterator;
133 // _Rep: string representation
135 // 1. String really contains _M_length + 1 characters: due to 21.3.4
136 // must be kept null-terminated.
137 // 2. _M_capacity >= _M_length
138 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
139 // 3. _M_refcount has three states:
140 // -1: leaked, one reference, no ref-copies allowed, non-const.
141 // 0: one reference, non-const.
142 // n>0: n + 1 references, operations require a lock, const.
143 // 4. All fields==0 is an empty string, given the extra storage
144 // beyond-the-end for a null terminator; thus, the shared
145 // empty string representation needs no constructor.
150 size_type _M_capacity;
151 _Atomic_word _M_refcount;
154 struct _Rep : _Rep_base
157 typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc;
159 // (Public) Data members:
161 // The maximum number of individual char_type elements of an
162 // individual string is determined by _S_max_size. This is the
163 // value that will be returned by max_size(). (Whereas npos
164 // is the maximum number of bytes the allocator can allocate.)
165 // If one was to divvy up the theoretical largest size string,
166 // with a terminating character and m _CharT elements, it'd
168 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
170 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
171 // In addition, this implementation quarters this amount.
172 static const size_type _S_max_size;
173 static const _CharT _S_terminal;
175 // The following storage is init'd to 0 by the linker, resulting
176 // (carefully) in an empty string with one reference.
177 static size_type _S_empty_rep_storage[];
182 // NB: Mild hack to avoid strict-aliasing warnings. Note that
183 // _S_empty_rep_storage is never modified and the punning should
184 // be reasonably safe in this case.
185 void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage);
186 return *reinterpret_cast<_Rep*>(__p);
191 { return this->_M_refcount < 0; }
195 { return this->_M_refcount > 0; }
199 { this->_M_refcount = -1; }
203 { this->_M_refcount = 0; }
206 _M_set_length_and_sharable(size_type __n)
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.
217 { return reinterpret_cast<_CharT*>(this + 1); }
220 _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
222 return (!_M_is_leaked() && __alloc1 == __alloc2)
223 ? _M_refcopy() : _M_clone(__alloc1);
228 _S_create(size_type, size_type, const _Alloc&);
231 _M_dispose(const _Alloc& __a)
233 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
234 if (__builtin_expect(this != &_S_empty_rep(), false))
236 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
242 _M_destroy(const _Alloc&) throw();
247 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
248 if (__builtin_expect(this != &_S_empty_rep(), false))
250 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
255 _M_clone(const _Alloc&, size_type __res = 0);
258 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
259 struct _Alloc_hider : _Alloc
261 _Alloc_hider(_CharT* __dat, const _Alloc& __a)
262 : _Alloc(__a), _M_p(__dat) { }
264 _CharT* _M_p; // The actual data.
268 // Data Members (public):
269 // NB: This is an unsigned type, and thus represents the maximum
270 // size that the allocator can hold.
271 /// Value returned by various member functions when they fail.
272 static const size_type npos = static_cast<size_type>(-1);
275 // Data Members (private):
276 mutable _Alloc_hider _M_dataplus;
280 { return _M_dataplus._M_p; }
284 { return (_M_dataplus._M_p = __p); }
288 { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
290 // For the internal use we have functions similar to `begin'/`end'
291 // but they do not call _M_leak.
294 { return iterator(_M_data()); }
298 { return iterator(_M_data() + this->size()); }
301 _M_leak() // for use in begin() & non-const op[]
303 if (!_M_rep()->_M_is_leaked())
308 _M_check(size_type __pos, const char* __s) const
310 if (__pos > this->size())
311 __throw_out_of_range(__N(__s));
316 _M_check_length(size_type __n1, size_type __n2, const char* __s) const
318 if (this->max_size() - (this->size() - __n1) < __n2)
319 __throw_length_error(__N(__s));
322 // NB: _M_limit doesn't check for a bad __pos value.
324 _M_limit(size_type __pos, size_type __off) const
326 const bool __testoff = __off < this->size() - __pos;
327 return __testoff ? __off : this->size() - __pos;
330 // True if _Rep and source do not overlap.
332 _M_disjunct(const _CharT* __s) const
334 return (less<const _CharT*>()(__s, _M_data())
335 || less<const _CharT*>()(_M_data() + this->size(), __s));
338 // When __n = 1 way faster than the general multichar
339 // traits_type::copy/move/assign.
341 _M_copy(_CharT* __d, const _CharT* __s, size_type __n)
344 traits_type::assign(*__d, *__s);
346 traits_type::copy(__d, __s, __n);
350 _M_move(_CharT* __d, const _CharT* __s, size_type __n)
353 traits_type::assign(*__d, *__s);
355 traits_type::move(__d, __s, __n);
359 _M_assign(_CharT* __d, size_type __n, _CharT __c)
362 traits_type::assign(*__d, __c);
364 traits_type::assign(__d, __n, __c);
367 // _S_copy_chars is a separate template to permit specialization
368 // to optimize for the common case of pointers as iterators.
369 template<class _Iterator>
371 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
373 for (; __k1 != __k2; ++__k1, ++__p)
374 traits_type::assign(*__p, *__k1); // These types are off.
378 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2)
379 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
382 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
383 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
386 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
387 { _M_copy(__p, __k1, __k2 - __k1); }
390 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
391 { _M_copy(__p, __k1, __k2 - __k1); }
394 _S_compare(size_type __n1, size_type __n2)
396 const difference_type __d = difference_type(__n1 - __n2);
398 if (__d > __gnu_cxx::__numeric_traits<int>::__max)
399 return __gnu_cxx::__numeric_traits<int>::__max;
400 else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
401 return __gnu_cxx::__numeric_traits<int>::__min;
407 _M_mutate(size_type __pos, size_type __len1, size_type __len2);
414 { return _Rep::_S_empty_rep(); }
417 // Construct/copy/destroy:
418 // NB: We overload ctors in some cases instead of using default
419 // arguments, per 17.4.4.4 para. 2 item 2.
422 * @brief Default constructor creates an empty string.
428 * @brief Construct an empty string using allocator @a a.
431 basic_string(const _Alloc& __a);
433 // NB: per LWG issue 42, semantics different from IS:
435 * @brief Construct string with copy of value of @a str.
436 * @param str Source string.
438 basic_string(const basic_string& __str);
440 * @brief Construct string as copy of a substring.
441 * @param str Source string.
442 * @param pos Index of first character to copy from.
443 * @param n Number of characters to copy (default remainder).
445 basic_string(const basic_string& __str, size_type __pos,
446 size_type __n = npos);
448 * @brief Construct string as copy of a substring.
449 * @param str Source string.
450 * @param pos Index of first character to copy from.
451 * @param n Number of characters to copy.
452 * @param a Allocator to use.
454 basic_string(const basic_string& __str, size_type __pos,
455 size_type __n, const _Alloc& __a);
458 * @brief Construct string initialized by a character array.
459 * @param s Source character array.
460 * @param n Number of characters to copy.
461 * @param a Allocator to use (default is default allocator).
463 * NB: @a s must have at least @a n characters, '\0' has no special
466 basic_string(const _CharT* __s, size_type __n,
467 const _Alloc& __a = _Alloc());
469 * @brief Construct string as copy of a C string.
470 * @param s Source C string.
471 * @param a Allocator to use (default is default allocator).
473 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
475 * @brief Construct string as multiple characters.
476 * @param n Number of characters.
477 * @param c Character to use.
478 * @param a Allocator to use (default is default allocator).
480 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
483 * @brief Construct string as copy of a range.
484 * @param beg Start of range.
485 * @param end End of range.
486 * @param a Allocator to use (default is default allocator).
488 template<class _InputIterator>
489 basic_string(_InputIterator __beg, _InputIterator __end,
490 const _Alloc& __a = _Alloc());
493 * @brief Destroy the string instance.
496 { _M_rep()->_M_dispose(this->get_allocator()); }
499 * @brief Assign the value of @a str to this string.
500 * @param str Source string.
503 operator=(const basic_string& __str)
504 { return this->assign(__str); }
507 * @brief Copy contents of @a s into this string.
508 * @param s Source null-terminated string.
511 operator=(const _CharT* __s)
512 { return this->assign(__s); }
515 * @brief Set value to string of length 1.
516 * @param c Source character.
518 * Assigning to a character makes this string length 1 and
519 * (*this)[0] == @a c.
522 operator=(_CharT __c)
524 this->assign(1, __c);
530 * Returns a read/write iterator that points to the first character in
531 * the %string. Unshares the string.
537 return iterator(_M_data());
541 * Returns a read-only (constant) iterator that points to the first
542 * character in the %string.
546 { return const_iterator(_M_data()); }
549 * Returns a read/write iterator that points one past the last
550 * character in the %string. Unshares the string.
556 return iterator(_M_data() + this->size());
560 * Returns a read-only (constant) iterator that points one past the
561 * last character in the %string.
565 { return const_iterator(_M_data() + this->size()); }
568 * Returns a read/write reverse iterator that points to the last
569 * character in the %string. Iteration is done in reverse element
570 * order. Unshares the string.
574 { return reverse_iterator(this->end()); }
577 * Returns a read-only (constant) reverse iterator that points
578 * to the last character in the %string. Iteration is done in
579 * reverse element order.
581 const_reverse_iterator
583 { return const_reverse_iterator(this->end()); }
586 * Returns a read/write reverse iterator that points to one before the
587 * first character in the %string. Iteration is done in reverse
588 * element order. Unshares the string.
592 { return reverse_iterator(this->begin()); }
595 * Returns a read-only (constant) reverse iterator that points
596 * to one before the first character in the %string. Iteration
597 * is done in reverse element order.
599 const_reverse_iterator
601 { return const_reverse_iterator(this->begin()); }
605 /// Returns the number of characters in the string, not including any
606 /// null-termination.
609 { return _M_rep()->_M_length; }
611 /// Returns the number of characters in the string, not including any
612 /// null-termination.
615 { return _M_rep()->_M_length; }
617 /// Returns the size() of the largest possible %string.
620 { return _Rep::_S_max_size; }
623 * @brief Resizes the %string to the specified number of characters.
624 * @param n Number of characters the %string should contain.
625 * @param c Character to fill any new elements.
627 * This function will %resize the %string to the specified
628 * number of characters. If the number is smaller than the
629 * %string's current size the %string is truncated, otherwise
630 * the %string is extended and new elements are set to @a c.
633 resize(size_type __n, _CharT __c);
636 * @brief Resizes the %string to the specified number of characters.
637 * @param n Number of characters the %string should contain.
639 * This function will resize the %string to the specified length. If
640 * the new size is smaller than the %string's current size the %string
641 * is truncated, otherwise the %string is extended and new characters
642 * are default-constructed. For basic types such as char, this means
646 resize(size_type __n)
647 { this->resize(__n, _CharT()); }
650 * Returns the total number of characters that the %string can hold
651 * before needing to allocate more memory.
655 { return _M_rep()->_M_capacity; }
658 * @brief Attempt to preallocate enough memory for specified number of
660 * @param res_arg Number of characters required.
661 * @throw std::length_error If @a res_arg exceeds @c max_size().
663 * This function attempts to reserve enough memory for the
664 * %string to hold the specified number of characters. If the
665 * number requested is more than max_size(), length_error is
668 * The advantage of this function is that if optimal code is a
669 * necessity and the user can determine the string length that will be
670 * required, the user can reserve the memory in %advance, and thus
671 * prevent a possible reallocation of memory and copying of %string
675 reserve(size_type __res_arg = 0);
678 * Erases the string, making it empty.
682 { _M_mutate(0, this->size(), 0); }
685 * Returns true if the %string is empty. Equivalent to *this == "".
689 { return this->size() == 0; }
693 * @brief Subscript access to the data contained in the %string.
694 * @param pos The index of the character to access.
695 * @return Read-only (constant) reference to the character.
697 * This operator allows for easy, array-style, data access.
698 * Note that data access with this operator is unchecked and
699 * out_of_range lookups are not defined. (For checked lookups
703 operator[] (size_type __pos) const
705 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
706 return _M_data()[__pos];
710 * @brief Subscript access to the data contained in the %string.
711 * @param pos The index of the character to access.
712 * @return Read/write reference to the character.
714 * This operator allows for easy, array-style, data access.
715 * Note that data access with this operator is unchecked and
716 * out_of_range lookups are not defined. (For checked lookups
717 * see at().) Unshares the string.
720 operator[](size_type __pos)
722 // allow pos == size() as v3 extension:
723 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
724 // but be strict in pedantic mode:
725 _GLIBCXX_DEBUG_PEDASSERT(__pos < size());
727 return _M_data()[__pos];
731 * @brief Provides access to the data contained in the %string.
732 * @param n The index of the character to access.
733 * @return Read-only (const) reference to the character.
734 * @throw std::out_of_range If @a n is an invalid index.
736 * This function provides for safer data access. The parameter is
737 * first checked that it is in the range of the string. The function
738 * throws out_of_range if the check fails.
741 at(size_type __n) const
743 if (__n >= this->size())
744 __throw_out_of_range(__N("basic_string::at"));
745 return _M_data()[__n];
749 * @brief Provides access to the data contained in the %string.
750 * @param n The index of the character to access.
751 * @return Read/write reference to the character.
752 * @throw std::out_of_range If @a n is an invalid index.
754 * This function provides for safer data access. The parameter is
755 * first checked that it is in the range of the string. The function
756 * throws out_of_range if the check fails. Success results in
757 * unsharing the string.
763 __throw_out_of_range(__N("basic_string::at"));
765 return _M_data()[__n];
770 * @brief Append a string to this string.
771 * @param str The string to append.
772 * @return Reference to this string.
775 operator+=(const basic_string& __str)
776 { return this->append(__str); }
779 * @brief Append a C string.
780 * @param s The C string to append.
781 * @return Reference to this string.
784 operator+=(const _CharT* __s)
785 { return this->append(__s); }
788 * @brief Append a character.
789 * @param c The character to append.
790 * @return Reference to this string.
793 operator+=(_CharT __c)
795 this->push_back(__c);
800 * @brief Append a string to this string.
801 * @param str The string to append.
802 * @return Reference to this string.
805 append(const basic_string& __str);
808 * @brief Append a substring.
809 * @param str The string to append.
810 * @param pos Index of the first character of str to append.
811 * @param n The number of characters to append.
812 * @return Reference to this string.
813 * @throw std::out_of_range if @a pos is not a valid index.
815 * This function appends @a n characters from @a str starting at @a pos
816 * to this string. If @a n is is larger than the number of available
817 * characters in @a str, the remainder of @a str is appended.
820 append(const basic_string& __str, size_type __pos, size_type __n);
823 * @brief Append a C substring.
824 * @param s The C string to append.
825 * @param n The number of characters to append.
826 * @return Reference to this string.
829 append(const _CharT* __s, size_type __n);
832 * @brief Append a C string.
833 * @param s The C string to append.
834 * @return Reference to this string.
837 append(const _CharT* __s)
839 __glibcxx_requires_string(__s);
840 return this->append(__s, traits_type::length(__s));
844 * @brief Append multiple characters.
845 * @param n The number of characters to append.
846 * @param c The character to use.
847 * @return Reference to this string.
849 * Appends n copies of c to this string.
852 append(size_type __n, _CharT __c);
855 * @brief Append a range of characters.
856 * @param first Iterator referencing the first character to append.
857 * @param last Iterator marking the end of the range.
858 * @return Reference to this string.
860 * Appends characters in the range [first,last) to this string.
862 template<class _InputIterator>
864 append(_InputIterator __first, _InputIterator __last)
865 { return this->replace(_M_iend(), _M_iend(), __first, __last); }
868 * @brief Append a single character.
869 * @param c Character to append.
872 push_back(_CharT __c)
874 const size_type __len = 1 + this->size();
875 if (__len > this->capacity() || _M_rep()->_M_is_shared())
876 this->reserve(__len);
877 traits_type::assign(_M_data()[this->size()], __c);
878 _M_rep()->_M_set_length_and_sharable(__len);
882 * @brief Set value to contents of another string.
883 * @param str Source string to use.
884 * @return Reference to this string.
887 assign(const basic_string& __str);
890 * @brief Set value to a substring of a string.
891 * @param str The string to use.
892 * @param pos Index of the first character of str.
893 * @param n Number of characters to use.
894 * @return Reference to this string.
895 * @throw std::out_of_range if @a pos is not a valid index.
897 * This function sets this string to the substring of @a str consisting
898 * of @a n characters at @a pos. If @a n is is larger than the number
899 * of available characters in @a str, the remainder of @a str is used.
902 assign(const basic_string& __str, size_type __pos, size_type __n)
903 { return this->assign(__str._M_data()
904 + __str._M_check(__pos, "basic_string::assign"),
905 __str._M_limit(__pos, __n)); }
908 * @brief Set value to a C substring.
909 * @param s The C string to use.
910 * @param n Number of characters to use.
911 * @return Reference to this string.
913 * This function sets the value of this string to the first @a n
914 * characters of @a s. If @a n is is larger than the number of
915 * available characters in @a s, the remainder of @a s is used.
918 assign(const _CharT* __s, size_type __n);
921 * @brief Set value to contents of a C string.
922 * @param s The C string to use.
923 * @return Reference to this string.
925 * This function sets the value of this string to the value of @a s.
926 * The data is copied, so there is no dependence on @a s once the
930 assign(const _CharT* __s)
932 __glibcxx_requires_string(__s);
933 return this->assign(__s, traits_type::length(__s));
937 * @brief Set value to multiple characters.
938 * @param n Length of the resulting string.
939 * @param c The character to use.
940 * @return Reference to this string.
942 * This function sets the value of this string to @a n copies of
946 assign(size_type __n, _CharT __c)
947 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
950 * @brief Set value to a range of characters.
951 * @param first Iterator referencing the first character to append.
952 * @param last Iterator marking the end of the range.
953 * @return Reference to this string.
955 * Sets value of string to characters in the range [first,last).
957 template<class _InputIterator>
959 assign(_InputIterator __first, _InputIterator __last)
960 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
963 * @brief Insert multiple characters.
964 * @param p Iterator referencing location in string to insert at.
965 * @param n Number of characters to insert
966 * @param c The character to insert.
967 * @throw std::length_error If new length exceeds @c max_size().
969 * Inserts @a n copies of character @a c starting at the position
970 * referenced by iterator @a p. If adding characters causes the length
971 * to exceed max_size(), length_error is thrown. The value of the
972 * string doesn't change if an error is thrown.
975 insert(iterator __p, size_type __n, _CharT __c)
976 { this->replace(__p, __p, __n, __c); }
979 * @brief Insert a range of characters.
980 * @param p Iterator referencing location in string to insert at.
981 * @param beg Start of range.
982 * @param end End of range.
983 * @throw std::length_error If new length exceeds @c max_size().
985 * Inserts characters in range [beg,end). If adding characters causes
986 * the length to exceed max_size(), length_error is thrown. The value
987 * of the string doesn't change if an error is thrown.
989 template<class _InputIterator>
991 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
992 { this->replace(__p, __p, __beg, __end); }
995 * @brief Insert value of a string.
996 * @param pos1 Iterator referencing location in string to insert at.
997 * @param str The string to insert.
998 * @return Reference to this string.
999 * @throw std::length_error If new length exceeds @c max_size().
1001 * Inserts value of @a str starting at @a pos1. If adding characters
1002 * causes the length to exceed max_size(), length_error is thrown. The
1003 * value of the string doesn't change if an error is thrown.
1006 insert(size_type __pos1, const basic_string& __str)
1007 { return this->insert(__pos1, __str, size_type(0), __str.size()); }
1010 * @brief Insert a substring.
1011 * @param pos1 Iterator referencing location in string to insert at.
1012 * @param str The string to insert.
1013 * @param pos2 Start of characters in str to insert.
1014 * @param n Number of characters to insert.
1015 * @return Reference to this string.
1016 * @throw std::length_error If new length exceeds @c max_size().
1017 * @throw std::out_of_range If @a pos1 > size() or
1018 * @a pos2 > @a str.size().
1020 * Starting at @a pos1, insert @a n character of @a str beginning with
1021 * @a pos2. If adding characters causes the length to exceed
1022 * max_size(), length_error is thrown. If @a pos1 is beyond the end of
1023 * this string or @a pos2 is beyond the end of @a str, out_of_range is
1024 * thrown. The value of the string doesn't change if an error is
1028 insert(size_type __pos1, const basic_string& __str,
1029 size_type __pos2, size_type __n)
1030 { return this->insert(__pos1, __str._M_data()
1031 + __str._M_check(__pos2, "basic_string::insert"),
1032 __str._M_limit(__pos2, __n)); }
1035 * @brief Insert a C substring.
1036 * @param pos Iterator referencing location in string to insert at.
1037 * @param s The C string to insert.
1038 * @param n The number of characters to insert.
1039 * @return Reference to this string.
1040 * @throw std::length_error If new length exceeds @c max_size().
1041 * @throw std::out_of_range If @a pos is beyond the end of this
1044 * Inserts the first @a n characters of @a s starting at @a pos. If
1045 * adding characters causes the length to exceed max_size(),
1046 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1047 * thrown. The value of the string doesn't change if an error is
1051 insert(size_type __pos, const _CharT* __s, size_type __n);
1054 * @brief Insert a C string.
1055 * @param pos Iterator referencing location in string to insert at.
1056 * @param s The C string to insert.
1057 * @return Reference to this string.
1058 * @throw std::length_error If new length exceeds @c max_size().
1059 * @throw std::out_of_range If @a pos is beyond the end of this
1062 * Inserts the first @a n characters of @a s starting at @a pos. If
1063 * adding characters causes the length to exceed max_size(),
1064 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1065 * thrown. The value of the string doesn't change if an error is
1069 insert(size_type __pos, const _CharT* __s)
1071 __glibcxx_requires_string(__s);
1072 return this->insert(__pos, __s, traits_type::length(__s));
1076 * @brief Insert multiple characters.
1077 * @param pos Index in string to insert at.
1078 * @param n Number of characters to insert
1079 * @param c The character to insert.
1080 * @return Reference to this string.
1081 * @throw std::length_error If new length exceeds @c max_size().
1082 * @throw std::out_of_range If @a pos is beyond the end of this
1085 * Inserts @a n copies of character @a c starting at index @a pos. If
1086 * adding characters causes the length to exceed max_size(),
1087 * length_error is thrown. If @a pos > length(), out_of_range is
1088 * thrown. The value of the string doesn't change if an error is
1092 insert(size_type __pos, size_type __n, _CharT __c)
1093 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
1094 size_type(0), __n, __c); }
1097 * @brief Insert one character.
1098 * @param p Iterator referencing position in string to insert at.
1099 * @param c The character to insert.
1100 * @return Iterator referencing newly inserted char.
1101 * @throw std::length_error If new length exceeds @c max_size().
1103 * Inserts character @a c at position referenced by @a p. If adding
1104 * character causes the length to exceed max_size(), length_error is
1105 * thrown. If @a p is beyond end of string, out_of_range is thrown.
1106 * The value of the string doesn't change if an error is thrown.
1109 insert(iterator __p, _CharT __c)
1111 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1112 const size_type __pos = __p - _M_ibegin();
1113 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1114 _M_rep()->_M_set_leaked();
1115 return iterator(_M_data() + __pos);
1119 * @brief Remove characters.
1120 * @param pos Index of first character to remove (default 0).
1121 * @param n Number of characters to remove (default remainder).
1122 * @return Reference to this string.
1123 * @throw std::out_of_range If @a pos is beyond the end of this
1126 * Removes @a n characters from this string starting at @a pos. The
1127 * length of the string is reduced by @a n. If there are < @a n
1128 * characters to remove, the remainder of the string is truncated. If
1129 * @a p is beyond end of string, out_of_range is thrown. The value of
1130 * the string doesn't change if an error is thrown.
1133 erase(size_type __pos = 0, size_type __n = npos)
1135 _M_mutate(_M_check(__pos, "basic_string::erase"),
1136 _M_limit(__pos, __n), size_type(0));
1141 * @brief Remove one character.
1142 * @param position Iterator referencing the character to remove.
1143 * @return iterator referencing same location after removal.
1145 * Removes the character at @a position from this string. The value
1146 * of the string doesn't change if an error is thrown.
1149 erase(iterator __position)
1151 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
1152 && __position < _M_iend());
1153 const size_type __pos = __position - _M_ibegin();
1154 _M_mutate(__pos, size_type(1), size_type(0));
1155 _M_rep()->_M_set_leaked();
1156 return iterator(_M_data() + __pos);
1160 * @brief Remove a range of characters.
1161 * @param first Iterator referencing the first character to remove.
1162 * @param last Iterator referencing the end of the range.
1163 * @return Iterator referencing location of first after removal.
1165 * Removes the characters in the range [first,last) from this string.
1166 * The value of the string doesn't change if an error is thrown.
1169 erase(iterator __first, iterator __last)
1171 _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
1172 && __last <= _M_iend());
1173 const size_type __pos = __first - _M_ibegin();
1174 _M_mutate(__pos, __last - __first, size_type(0));
1175 _M_rep()->_M_set_leaked();
1176 return iterator(_M_data() + __pos);
1180 * @brief Replace characters with value from another string.
1181 * @param pos Index of first character to replace.
1182 * @param n Number of characters to be replaced.
1183 * @param str String to insert.
1184 * @return Reference to this string.
1185 * @throw std::out_of_range If @a pos is beyond the end of this
1187 * @throw std::length_error If new length exceeds @c max_size().
1189 * Removes the characters in the range [pos,pos+n) from this string.
1190 * In place, the value of @a str is inserted. If @a pos is beyond end
1191 * of string, out_of_range is thrown. If the length of the result
1192 * exceeds max_size(), length_error is thrown. The value of the string
1193 * doesn't change if an error is thrown.
1196 replace(size_type __pos, size_type __n, const basic_string& __str)
1197 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1200 * @brief Replace characters with value from another string.
1201 * @param pos1 Index of first character to replace.
1202 * @param n1 Number of characters to be replaced.
1203 * @param str String to insert.
1204 * @param pos2 Index of first character of str to use.
1205 * @param n2 Number of characters from str to use.
1206 * @return Reference to this string.
1207 * @throw std::out_of_range If @a pos1 > size() or @a pos2 >
1209 * @throw std::length_error If new length exceeds @c max_size().
1211 * Removes the characters in the range [pos1,pos1 + n) from this
1212 * string. In place, the value of @a str is inserted. If @a pos is
1213 * beyond end of string, out_of_range is thrown. If the length of the
1214 * result exceeds max_size(), length_error is thrown. The value of the
1215 * string doesn't change if an error is thrown.
1218 replace(size_type __pos1, size_type __n1, const basic_string& __str,
1219 size_type __pos2, size_type __n2)
1220 { return this->replace(__pos1, __n1, __str._M_data()
1221 + __str._M_check(__pos2, "basic_string::replace"),
1222 __str._M_limit(__pos2, __n2)); }
1225 * @brief Replace characters with value of a C substring.
1226 * @param pos Index of first character to replace.
1227 * @param n1 Number of characters to be replaced.
1228 * @param s C string to insert.
1229 * @param n2 Number of characters from @a s to use.
1230 * @return Reference to this string.
1231 * @throw std::out_of_range If @a pos1 > size().
1232 * @throw std::length_error If new length exceeds @c max_size().
1234 * Removes the characters in the range [pos,pos + n1) from this string.
1235 * In place, the first @a n2 characters of @a s are inserted, or all
1236 * of @a s if @a n2 is too large. If @a pos is beyond end of string,
1237 * out_of_range is thrown. If the length of result exceeds max_size(),
1238 * length_error is thrown. The value of the string doesn't change if
1239 * an error is thrown.
1242 replace(size_type __pos, size_type __n1, const _CharT* __s,
1246 * @brief Replace characters with value of a C string.
1247 * @param pos Index of first character to replace.
1248 * @param n1 Number of characters to be replaced.
1249 * @param s C string to insert.
1250 * @return Reference to this string.
1251 * @throw std::out_of_range If @a pos > size().
1252 * @throw std::length_error If new length exceeds @c max_size().
1254 * Removes the characters in the range [pos,pos + n1) from this string.
1255 * In place, the first @a n characters of @a s are inserted. If @a
1256 * pos is beyond end of string, out_of_range is thrown. If the length
1257 * of result exceeds max_size(), length_error is thrown. The value of
1258 * the string doesn't change if an error is thrown.
1261 replace(size_type __pos, size_type __n1, const _CharT* __s)
1263 __glibcxx_requires_string(__s);
1264 return this->replace(__pos, __n1, __s, traits_type::length(__s));
1268 * @brief Replace characters with multiple characters.
1269 * @param pos Index of first character to replace.
1270 * @param n1 Number of characters to be replaced.
1271 * @param n2 Number of characters to insert.
1272 * @param c Character to insert.
1273 * @return Reference to this string.
1274 * @throw std::out_of_range If @a pos > size().
1275 * @throw std::length_error If new length exceeds @c max_size().
1277 * Removes the characters in the range [pos,pos + n1) from this string.
1278 * In place, @a n2 copies of @a c are inserted. If @a pos is beyond
1279 * end of string, out_of_range is thrown. If the length of result
1280 * exceeds max_size(), length_error is thrown. The value of the string
1281 * doesn't change if an error is thrown.
1284 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1285 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
1286 _M_limit(__pos, __n1), __n2, __c); }
1289 * @brief Replace range of characters with string.
1290 * @param i1 Iterator referencing start of range to replace.
1291 * @param i2 Iterator referencing end of range to replace.
1292 * @param str String value to insert.
1293 * @return Reference to this string.
1294 * @throw std::length_error If new length exceeds @c max_size().
1296 * Removes the characters in the range [i1,i2). In place, the value of
1297 * @a str is inserted. If the length of result exceeds max_size(),
1298 * length_error is thrown. The value of the string doesn't change if
1299 * an error is thrown.
1302 replace(iterator __i1, iterator __i2, const basic_string& __str)
1303 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
1306 * @brief Replace range of characters with C substring.
1307 * @param i1 Iterator referencing start of range to replace.
1308 * @param i2 Iterator referencing end of range to replace.
1309 * @param s C string value to insert.
1310 * @param n Number of characters from s to insert.
1311 * @return Reference to this string.
1312 * @throw std::length_error If new length exceeds @c max_size().
1314 * Removes the characters in the range [i1,i2). In place, the first @a
1315 * n characters of @a s are inserted. If the length of result exceeds
1316 * max_size(), length_error is thrown. The value of the string doesn't
1317 * change if an error is thrown.
1320 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
1322 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1323 && __i2 <= _M_iend());
1324 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
1328 * @brief Replace range of characters with C string.
1329 * @param i1 Iterator referencing start of range to replace.
1330 * @param i2 Iterator referencing end of range to replace.
1331 * @param s C string value to insert.
1332 * @return Reference to this string.
1333 * @throw std::length_error If new length exceeds @c max_size().
1335 * Removes the characters in the range [i1,i2). In place, the
1336 * characters of @a s are inserted. If the length of result exceeds
1337 * max_size(), length_error is thrown. The value of the string doesn't
1338 * change if an error is thrown.
1341 replace(iterator __i1, iterator __i2, const _CharT* __s)
1343 __glibcxx_requires_string(__s);
1344 return this->replace(__i1, __i2, __s, traits_type::length(__s));
1348 * @brief Replace range of characters with multiple characters
1349 * @param i1 Iterator referencing start of range to replace.
1350 * @param i2 Iterator referencing end of range to replace.
1351 * @param n Number of characters to insert.
1352 * @param c Character to insert.
1353 * @return Reference to this string.
1354 * @throw std::length_error If new length exceeds @c max_size().
1356 * Removes the characters in the range [i1,i2). In place, @a n copies
1357 * of @a c are inserted. If the length of result exceeds max_size(),
1358 * length_error is thrown. The value of the string doesn't change if
1359 * an error is thrown.
1362 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
1364 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1365 && __i2 <= _M_iend());
1366 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
1370 * @brief Replace range of characters with range.
1371 * @param i1 Iterator referencing start of range to replace.
1372 * @param i2 Iterator referencing end of range to replace.
1373 * @param k1 Iterator referencing start of range to insert.
1374 * @param k2 Iterator referencing end of range to insert.
1375 * @return Reference to this string.
1376 * @throw std::length_error If new length exceeds @c max_size().
1378 * Removes the characters in the range [i1,i2). In place, characters
1379 * in the range [k1,k2) are inserted. If the length of result exceeds
1380 * max_size(), length_error is thrown. The value of the string doesn't
1381 * change if an error is thrown.
1383 template<class _InputIterator>
1385 replace(iterator __i1, iterator __i2,
1386 _InputIterator __k1, _InputIterator __k2)
1388 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1389 && __i2 <= _M_iend());
1390 __glibcxx_requires_valid_range(__k1, __k2);
1391 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1392 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
1395 // Specializations for the common case of pointer and iterator:
1396 // useful to avoid the overhead of temporary buffering in _M_replace.
1398 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
1400 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1401 && __i2 <= _M_iend());
1402 __glibcxx_requires_valid_range(__k1, __k2);
1403 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1408 replace(iterator __i1, iterator __i2,
1409 const _CharT* __k1, const _CharT* __k2)
1411 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1412 && __i2 <= _M_iend());
1413 __glibcxx_requires_valid_range(__k1, __k2);
1414 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1419 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
1421 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1422 && __i2 <= _M_iend());
1423 __glibcxx_requires_valid_range(__k1, __k2);
1424 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1425 __k1.base(), __k2 - __k1);
1429 replace(iterator __i1, iterator __i2,
1430 const_iterator __k1, const_iterator __k2)
1432 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1433 && __i2 <= _M_iend());
1434 __glibcxx_requires_valid_range(__k1, __k2);
1435 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1436 __k1.base(), __k2 - __k1);
1440 template<class _Integer>
1442 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
1443 _Integer __val, __true_type)
1444 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
1446 template<class _InputIterator>
1448 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
1449 _InputIterator __k2, __false_type);
1452 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
1456 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
1459 // _S_construct_aux is used to implement the 21.3.1 para 15 which
1460 // requires special behaviour if _InIter is an integral type
1461 template<class _InIterator>
1463 _S_construct_aux(_InIterator __beg, _InIterator __end,
1464 const _Alloc& __a, __false_type)
1466 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
1467 return _S_construct(__beg, __end, __a, _Tag());
1470 template<class _InIterator>
1472 _S_construct_aux(_InIterator __beg, _InIterator __end,
1473 const _Alloc& __a, __true_type)
1474 { return _S_construct(static_cast<size_type>(__beg),
1475 static_cast<value_type>(__end), __a); }
1477 template<class _InIterator>
1479 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
1481 typedef typename std::__is_integer<_InIterator>::__type _Integral;
1482 return _S_construct_aux(__beg, __end, __a, _Integral());
1485 // For Input Iterators, used in istreambuf_iterators, etc.
1486 template<class _InIterator>
1488 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1489 input_iterator_tag);
1491 // For forward_iterators up to random_access_iterators, used for
1492 // string::iterator, _CharT*, etc.
1493 template<class _FwdIterator>
1495 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
1496 forward_iterator_tag);
1499 _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
1504 * @brief Copy substring into C string.
1505 * @param s C string to copy value into.
1506 * @param n Number of characters to copy.
1507 * @param pos Index of first character to copy.
1508 * @return Number of characters actually copied
1509 * @throw std::out_of_range If pos > size().
1511 * Copies up to @a n characters starting at @a pos into the C string @a
1512 * s. If @a pos is greater than size(), out_of_range is thrown.
1515 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
1518 * @brief Swap contents with another string.
1519 * @param s String to swap with.
1521 * Exchanges the contents of this string with that of @a s in constant
1525 swap(basic_string& __s);
1527 // String operations:
1529 * @brief Return const pointer to null-terminated contents.
1531 * This is a handle to internal data. Do not modify or dire things may
1536 { return _M_data(); }
1539 * @brief Return const pointer to contents.
1541 * This is a handle to internal data. Do not modify or dire things may
1546 { return _M_data(); }
1549 * @brief Return copy of allocator used to construct this string.
1552 get_allocator() const
1553 { return _M_dataplus; }
1556 * @brief Find position of a C substring.
1557 * @param s C string to locate.
1558 * @param pos Index of character to search from.
1559 * @param n Number of characters from @a s to search for.
1560 * @return Index of start of first occurrence.
1562 * Starting from @a pos, searches forward for the first @a n characters
1563 * in @a s within this string. If found, returns the index where it
1564 * begins. If not found, returns npos.
1567 find(const _CharT* __s, size_type __pos, size_type __n) const;
1570 * @brief Find position of a string.
1571 * @param str String to locate.
1572 * @param pos Index of character to search from (default 0).
1573 * @return Index of start of first occurrence.
1575 * Starting from @a pos, searches forward for value of @a str within
1576 * this string. If found, returns the index where it begins. If not
1577 * found, returns npos.
1580 find(const basic_string& __str, size_type __pos = 0) const
1581 { return this->find(__str.data(), __pos, __str.size()); }
1584 * @brief Find position of a C string.
1585 * @param s C string to locate.
1586 * @param pos Index of character to search from (default 0).
1587 * @return Index of start of first occurrence.
1589 * Starting from @a pos, searches forward for the value of @a s within
1590 * this string. If found, returns the index where it begins. If not
1591 * found, returns npos.
1594 find(const _CharT* __s, size_type __pos = 0) const
1596 __glibcxx_requires_string(__s);
1597 return this->find(__s, __pos, traits_type::length(__s));
1601 * @brief Find position of a character.
1602 * @param c Character to locate.
1603 * @param pos Index of character to search from (default 0).
1604 * @return Index of first occurrence.
1606 * Starting from @a pos, searches forward for @a c within this string.
1607 * If found, returns the index where it was found. If not found,
1611 find(_CharT __c, size_type __pos = 0) const;
1614 * @brief Find last position of a string.
1615 * @param str String to locate.
1616 * @param pos Index of character to search back from (default end).
1617 * @return Index of start of last occurrence.
1619 * Starting from @a pos, searches backward for value of @a str within
1620 * this string. If found, returns the index where it begins. If not
1621 * found, returns npos.
1624 rfind(const basic_string& __str, size_type __pos = npos) const
1625 { return this->rfind(__str.data(), __pos, __str.size()); }
1628 * @brief Find last position of a C substring.
1629 * @param s C string to locate.
1630 * @param pos Index of character to search back from.
1631 * @param n Number of characters from s to search for.
1632 * @return Index of start of last occurrence.
1634 * Starting from @a pos, searches backward for the first @a n
1635 * characters in @a s within this string. If found, returns the index
1636 * where it begins. If not found, returns npos.
1639 rfind(const _CharT* __s, size_type __pos, size_type __n) const;
1642 * @brief Find last position of a C string.
1643 * @param s C string to locate.
1644 * @param pos Index of character to start search at (default end).
1645 * @return Index of start of last occurrence.
1647 * Starting from @a pos, searches backward for the value of @a s within
1648 * this string. If found, returns the index where it begins. If not
1649 * found, returns npos.
1652 rfind(const _CharT* __s, size_type __pos = npos) const
1654 __glibcxx_requires_string(__s);
1655 return this->rfind(__s, __pos, traits_type::length(__s));
1659 * @brief Find last position of a character.
1660 * @param c Character to locate.
1661 * @param pos Index of character to search back from (default end).
1662 * @return Index of last occurrence.
1664 * Starting from @a pos, searches backward for @a c within this string.
1665 * If found, returns the index where it was found. If not found,
1669 rfind(_CharT __c, size_type __pos = npos) const;
1672 * @brief Find position of a character of string.
1673 * @param str String containing characters to locate.
1674 * @param pos Index of character to search from (default 0).
1675 * @return Index of first occurrence.
1677 * Starting from @a pos, searches forward for one of the characters of
1678 * @a str within this string. If found, returns the index where it was
1679 * found. If not found, returns npos.
1682 find_first_of(const basic_string& __str, size_type __pos = 0) const
1683 { return this->find_first_of(__str.data(), __pos, __str.size()); }
1686 * @brief Find position of a character of C substring.
1687 * @param s String containing characters to locate.
1688 * @param pos Index of character to search from (default 0).
1689 * @param n Number of characters from s to search for.
1690 * @return Index of first occurrence.
1692 * Starting from @a pos, searches forward for one of the first @a n
1693 * characters of @a s within this string. If found, returns the index
1694 * where it was found. If not found, returns npos.
1697 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
1700 * @brief Find position of a character of C string.
1701 * @param s String containing characters to locate.
1702 * @param pos Index of character to search from (default 0).
1703 * @return Index of first occurrence.
1705 * Starting from @a pos, searches forward for one of the characters of
1706 * @a s within this string. If found, returns the index where it was
1707 * found. If not found, returns npos.
1710 find_first_of(const _CharT* __s, size_type __pos = 0) const
1712 __glibcxx_requires_string(__s);
1713 return this->find_first_of(__s, __pos, traits_type::length(__s));
1717 * @brief Find position of a character.
1718 * @param c Character to locate.
1719 * @param pos Index of character to search from (default 0).
1720 * @return Index of first occurrence.
1722 * Starting from @a pos, searches forward for the character @a c within
1723 * this string. If found, returns the index where it was found. If
1724 * not found, returns npos.
1726 * Note: equivalent to find(c, pos).
1729 find_first_of(_CharT __c, size_type __pos = 0) const
1730 { return this->find(__c, __pos); }
1733 * @brief Find last position of a character of string.
1734 * @param str String containing characters to locate.
1735 * @param pos Index of character to search back from (default end).
1736 * @return Index of last occurrence.
1738 * Starting from @a pos, searches backward for one of the characters of
1739 * @a str within this string. If found, returns the index where it was
1740 * found. If not found, returns npos.
1743 find_last_of(const basic_string& __str, size_type __pos = npos) const
1744 { return this->find_last_of(__str.data(), __pos, __str.size()); }
1747 * @brief Find last position of a character of C substring.
1748 * @param s C string containing characters to locate.
1749 * @param pos Index of character to search back from (default end).
1750 * @param n Number of characters from s to search for.
1751 * @return Index of last occurrence.
1753 * Starting from @a pos, searches backward for one of the first @a n
1754 * characters of @a s within this string. If found, returns the index
1755 * where it was found. If not found, returns npos.
1758 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
1761 * @brief Find last position of a character of C string.
1762 * @param s C string containing characters to locate.
1763 * @param pos Index of character to search back from (default end).
1764 * @return Index of last occurrence.
1766 * Starting from @a pos, searches backward for one of the characters of
1767 * @a s within this string. If found, returns the index where it was
1768 * found. If not found, returns npos.
1771 find_last_of(const _CharT* __s, size_type __pos = npos) const
1773 __glibcxx_requires_string(__s);
1774 return this->find_last_of(__s, __pos, traits_type::length(__s));
1778 * @brief Find last position of a character.
1779 * @param c Character to locate.
1780 * @param pos Index of character to search back from (default 0).
1781 * @return Index of last occurrence.
1783 * Starting from @a pos, searches backward for @a c within this string.
1784 * If found, returns the index where it was found. If not found,
1787 * Note: equivalent to rfind(c, pos).
1790 find_last_of(_CharT __c, size_type __pos = npos) const
1791 { return this->rfind(__c, __pos); }
1794 * @brief Find position of a character not in string.
1795 * @param str String containing characters to avoid.
1796 * @param pos Index of character to search from (default 0).
1797 * @return Index of first occurrence.
1799 * Starting from @a pos, searches forward for a character not contained
1800 * in @a str within this string. If found, returns the index where it
1801 * was found. If not found, returns npos.
1804 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
1805 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
1808 * @brief Find position of a character not in C substring.
1809 * @param s C string containing characters to avoid.
1810 * @param pos Index of character to search from (default 0).
1811 * @param n Number of characters from s to consider.
1812 * @return Index of first occurrence.
1814 * Starting from @a pos, searches forward for a character not contained
1815 * in the first @a n characters of @a s within this string. If found,
1816 * returns the index where it was found. If not found, returns npos.
1819 find_first_not_of(const _CharT* __s, size_type __pos,
1820 size_type __n) const;
1823 * @brief Find position of a character not in C string.
1824 * @param s C string containing characters to avoid.
1825 * @param pos Index of character to search from (default 0).
1826 * @return Index of first occurrence.
1828 * Starting from @a pos, searches forward for a character not contained
1829 * in @a s within this string. If found, returns the index where it
1830 * was found. If not found, returns npos.
1833 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
1835 __glibcxx_requires_string(__s);
1836 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
1840 * @brief Find position of a different character.
1841 * @param c Character to avoid.
1842 * @param pos Index of character to search from (default 0).
1843 * @return Index of first occurrence.
1845 * Starting from @a pos, searches forward for a character other than @a c
1846 * within this string. If found, returns the index where it was found.
1847 * If not found, returns npos.
1850 find_first_not_of(_CharT __c, size_type __pos = 0) const;
1853 * @brief Find last position of a character not in string.
1854 * @param str String containing characters to avoid.
1855 * @param pos Index of character to search from (default 0).
1856 * @return Index of first occurrence.
1858 * Starting from @a pos, searches backward for a character not
1859 * contained in @a str within this string. If found, returns the index
1860 * where it was found. If not found, returns npos.
1863 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
1864 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
1867 * @brief Find last position of a character not in C substring.
1868 * @param s C string containing characters to avoid.
1869 * @param pos Index of character to search from (default 0).
1870 * @param n Number of characters from s to consider.
1871 * @return Index of first occurrence.
1873 * Starting from @a pos, searches backward for a character not
1874 * contained in the first @a n characters of @a s within this string.
1875 * If found, returns the index where it was found. If not found,
1879 find_last_not_of(const _CharT* __s, size_type __pos,
1880 size_type __n) const;
1882 * @brief Find position of a character not in C string.
1883 * @param s C string containing characters to avoid.
1884 * @param pos Index of character to search from (default 0).
1885 * @return Index of first occurrence.
1887 * Starting from @a pos, searches backward for a character not
1888 * contained in @a s within this string. If found, returns the index
1889 * where it was found. If not found, returns npos.
1892 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
1894 __glibcxx_requires_string(__s);
1895 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
1899 * @brief Find last position of a different character.
1900 * @param c Character to avoid.
1901 * @param pos Index of character to search from (default 0).
1902 * @return Index of first occurrence.
1904 * Starting from @a pos, searches backward for a character other than
1905 * @a c within this string. If found, returns the index where it was
1906 * found. If not found, returns npos.
1909 find_last_not_of(_CharT __c, size_type __pos = npos) const;
1912 * @brief Get a substring.
1913 * @param pos Index of first character (default 0).
1914 * @param n Number of characters in substring (default remainder).
1915 * @return The new string.
1916 * @throw std::out_of_range If pos > size().
1918 * Construct and return a new string using the @a n characters starting
1919 * at @a pos. If the string is too short, use the remainder of the
1920 * characters. If @a pos is beyond the end of the string, out_of_range
1924 substr(size_type __pos = 0, size_type __n = npos) const
1925 { return basic_string(*this,
1926 _M_check(__pos, "basic_string::substr"), __n); }
1929 * @brief Compare to a string.
1930 * @param str String to compare against.
1931 * @return Integer < 0, 0, or > 0.
1933 * Returns an integer < 0 if this string is ordered before @a str, 0 if
1934 * their values are equivalent, or > 0 if this string is ordered after
1935 * @a str. Determines the effective length rlen of the strings to
1936 * compare as the smallest of size() and str.size(). The function
1937 * then compares the two strings by calling traits::compare(data(),
1938 * str.data(),rlen). If the result of the comparison is nonzero returns
1939 * it, otherwise the shorter one is ordered first.
1942 compare(const basic_string& __str) const
1944 const size_type __size = this->size();
1945 const size_type __osize = __str.size();
1946 const size_type __len = std::min(__size, __osize);
1948 int __r = traits_type::compare(_M_data(), __str.data(), __len);
1950 __r = _S_compare(__size, __osize);
1955 * @brief Compare substring to a string.
1956 * @param pos Index of first character of substring.
1957 * @param n Number of characters in substring.
1958 * @param str String to compare against.
1959 * @return Integer < 0, 0, or > 0.
1961 * Form the substring of this string from the @a n characters starting
1962 * at @a pos. Returns an integer < 0 if the substring is ordered
1963 * before @a str, 0 if their values are equivalent, or > 0 if the
1964 * substring is ordered after @a str. Determines the effective length
1965 * rlen of the strings to compare as the smallest of the length of the
1966 * substring and @a str.size(). The function then compares the two
1967 * strings by calling traits::compare(substring.data(),str.data(),rlen).
1968 * If the result of the comparison is nonzero returns it, otherwise the
1969 * shorter one is ordered first.
1972 compare(size_type __pos, size_type __n, const basic_string& __str) const;
1975 * @brief Compare substring to a substring.
1976 * @param pos1 Index of first character of substring.
1977 * @param n1 Number of characters in substring.
1978 * @param str String to compare against.
1979 * @param pos2 Index of first character of substring of str.
1980 * @param n2 Number of characters in substring of str.
1981 * @return Integer < 0, 0, or > 0.
1983 * Form the substring of this string from the @a n1 characters starting
1984 * at @a pos1. Form the substring of @a str from the @a n2 characters
1985 * starting at @a pos2. Returns an integer < 0 if this substring is
1986 * ordered before the substring of @a str, 0 if their values are
1987 * equivalent, or > 0 if this substring is ordered after the substring
1988 * of @a str. Determines the effective length rlen of the strings
1989 * to compare as the smallest of the lengths of the substrings. The
1990 * function then compares the two strings by calling
1991 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
1992 * If the result of the comparison is nonzero returns it, otherwise the
1993 * shorter one is ordered first.
1996 compare(size_type __pos1, size_type __n1, const basic_string& __str,
1997 size_type __pos2, size_type __n2) const;
2000 * @brief Compare to a C string.
2001 * @param s C string to compare against.
2002 * @return Integer < 0, 0, or > 0.
2004 * Returns an integer < 0 if this string is ordered before @a s, 0 if
2005 * their values are equivalent, or > 0 if this string is ordered after
2006 * @a s. Determines the effective length rlen of the strings to
2007 * compare as the smallest of size() and the length of a string
2008 * constructed from @a s. The function then compares the two strings
2009 * by calling traits::compare(data(),s,rlen). If the result of the
2010 * comparison is nonzero returns it, otherwise the shorter one is
2014 compare(const _CharT* __s) const;
2016 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2017 // 5 String::compare specification questionable
2019 * @brief Compare substring to a C string.
2020 * @param pos Index of first character of substring.
2021 * @param n1 Number of characters in substring.
2022 * @param s C string to compare against.
2023 * @return Integer < 0, 0, or > 0.
2025 * Form the substring of this string from the @a n1 characters starting
2026 * at @a pos. Returns an integer < 0 if the substring is ordered
2027 * before @a s, 0 if their values are equivalent, or > 0 if the
2028 * substring is ordered after @a s. Determines the effective length
2029 * rlen of the strings to compare as the smallest of the length of the
2030 * substring and the length of a string constructed from @a s. The
2031 * function then compares the two string by calling
2032 * traits::compare(substring.data(),s,rlen). If the result of the
2033 * comparison is nonzero returns it, otherwise the shorter one is
2037 compare(size_type __pos, size_type __n1, const _CharT* __s) const;
2040 * @brief Compare substring against a character array.
2041 * @param pos1 Index of first character of substring.
2042 * @param n1 Number of characters in substring.
2043 * @param s character array to compare against.
2044 * @param n2 Number of characters of s.
2045 * @return Integer < 0, 0, or > 0.
2047 * Form the substring of this string from the @a n1 characters starting
2048 * at @a pos1. Form a string from the first @a n2 characters of @a s.
2049 * Returns an integer < 0 if this substring is ordered before the string
2050 * from @a s, 0 if their values are equivalent, or > 0 if this substring
2051 * is ordered after the string from @a s. Determines the effective
2052 * length rlen of the strings to compare as the smallest of the length
2053 * of the substring and @a n2. The function then compares the two
2054 * strings by calling traits::compare(substring.data(),s,rlen). If the
2055 * result of the comparison is nonzero returns it, otherwise the shorter
2056 * one is ordered first.
2058 * NB: s must have at least n2 characters, '\0' has no special
2062 compare(size_type __pos, size_type __n1, const _CharT* __s,
2063 size_type __n2) const;
2066 template<typename _CharT, typename _Traits, typename _Alloc>
2067 inline basic_string<_CharT, _Traits, _Alloc>::
2069 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
2070 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
2072 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()) { }
2077 * @brief Concatenate two strings.
2078 * @param lhs First string.
2079 * @param rhs Last string.
2080 * @return New string with value of @a lhs followed by @a rhs.
2082 template<typename _CharT, typename _Traits, typename _Alloc>
2083 basic_string<_CharT, _Traits, _Alloc>
2084 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2085 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2087 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2088 __str.append(__rhs);
2093 * @brief Concatenate C string and string.
2094 * @param lhs First string.
2095 * @param rhs Last string.
2096 * @return New string with value of @a lhs followed by @a rhs.
2098 template<typename _CharT, typename _Traits, typename _Alloc>
2099 basic_string<_CharT,_Traits,_Alloc>
2100 operator+(const _CharT* __lhs,
2101 const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2104 * @brief Concatenate character and string.
2105 * @param lhs First string.
2106 * @param rhs Last string.
2107 * @return New string with @a lhs followed by @a rhs.
2109 template<typename _CharT, typename _Traits, typename _Alloc>
2110 basic_string<_CharT,_Traits,_Alloc>
2111 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2114 * @brief Concatenate string and C string.
2115 * @param lhs First string.
2116 * @param rhs Last string.
2117 * @return New string with @a lhs followed by @a rhs.
2119 template<typename _CharT, typename _Traits, typename _Alloc>
2120 inline basic_string<_CharT, _Traits, _Alloc>
2121 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2122 const _CharT* __rhs)
2124 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2125 __str.append(__rhs);
2130 * @brief Concatenate string and character.
2131 * @param lhs First string.
2132 * @param rhs Last string.
2133 * @return New string with @a lhs followed by @a rhs.
2135 template<typename _CharT, typename _Traits, typename _Alloc>
2136 inline basic_string<_CharT, _Traits, _Alloc>
2137 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
2139 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
2140 typedef typename __string_type::size_type __size_type;
2141 __string_type __str(__lhs);
2142 __str.append(__size_type(1), __rhs);
2148 * @brief Test equivalence of two strings.
2149 * @param lhs First string.
2150 * @param rhs Second string.
2151 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2153 template<typename _CharT, typename _Traits, typename _Alloc>
2155 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2156 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2157 { return __lhs.compare(__rhs) == 0; }
2160 * @brief Test equivalence of C string and string.
2161 * @param lhs C string.
2162 * @param rhs String.
2163 * @return True if @a rhs.compare(@a lhs) == 0. False otherwise.
2165 template<typename _CharT, typename _Traits, typename _Alloc>
2167 operator==(const _CharT* __lhs,
2168 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2169 { return __rhs.compare(__lhs) == 0; }
2172 * @brief Test equivalence of string and C string.
2173 * @param lhs String.
2174 * @param rhs C string.
2175 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2177 template<typename _CharT, typename _Traits, typename _Alloc>
2179 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2180 const _CharT* __rhs)
2181 { return __lhs.compare(__rhs) == 0; }
2185 * @brief Test difference of two strings.
2186 * @param lhs First string.
2187 * @param rhs Second string.
2188 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2190 template<typename _CharT, typename _Traits, typename _Alloc>
2192 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2193 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2194 { return __rhs.compare(__lhs) != 0; }
2197 * @brief Test difference of C string and string.
2198 * @param lhs C string.
2199 * @param rhs String.
2200 * @return True if @a rhs.compare(@a lhs) != 0. False otherwise.
2202 template<typename _CharT, typename _Traits, typename _Alloc>
2204 operator!=(const _CharT* __lhs,
2205 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2206 { return __rhs.compare(__lhs) != 0; }
2209 * @brief Test difference of string and C string.
2210 * @param lhs String.
2211 * @param rhs C string.
2212 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2214 template<typename _CharT, typename _Traits, typename _Alloc>
2216 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2217 const _CharT* __rhs)
2218 { return __lhs.compare(__rhs) != 0; }
2222 * @brief Test if string precedes string.
2223 * @param lhs First string.
2224 * @param rhs Second string.
2225 * @return True if @a lhs precedes @a rhs. False otherwise.
2227 template<typename _CharT, typename _Traits, typename _Alloc>
2229 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2230 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2231 { return __lhs.compare(__rhs) < 0; }
2234 * @brief Test if string precedes C string.
2235 * @param lhs String.
2236 * @param rhs C string.
2237 * @return True if @a lhs precedes @a rhs. False otherwise.
2239 template<typename _CharT, typename _Traits, typename _Alloc>
2241 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2242 const _CharT* __rhs)
2243 { return __lhs.compare(__rhs) < 0; }
2246 * @brief Test if C string precedes string.
2247 * @param lhs C string.
2248 * @param rhs String.
2249 * @return True if @a lhs precedes @a rhs. False otherwise.
2251 template<typename _CharT, typename _Traits, typename _Alloc>
2253 operator<(const _CharT* __lhs,
2254 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2255 { return __rhs.compare(__lhs) > 0; }
2259 * @brief Test if string follows string.
2260 * @param lhs First string.
2261 * @param rhs Second string.
2262 * @return True if @a lhs follows @a rhs. False otherwise.
2264 template<typename _CharT, typename _Traits, typename _Alloc>
2266 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2267 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2268 { return __lhs.compare(__rhs) > 0; }
2271 * @brief Test if string follows C string.
2272 * @param lhs String.
2273 * @param rhs C string.
2274 * @return True if @a lhs follows @a rhs. False otherwise.
2276 template<typename _CharT, typename _Traits, typename _Alloc>
2278 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2279 const _CharT* __rhs)
2280 { return __lhs.compare(__rhs) > 0; }
2283 * @brief Test if C string follows string.
2284 * @param lhs C string.
2285 * @param rhs String.
2286 * @return True if @a lhs follows @a rhs. False otherwise.
2288 template<typename _CharT, typename _Traits, typename _Alloc>
2290 operator>(const _CharT* __lhs,
2291 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2292 { return __rhs.compare(__lhs) < 0; }
2296 * @brief Test if string doesn't follow string.
2297 * @param lhs First string.
2298 * @param rhs Second string.
2299 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2301 template<typename _CharT, typename _Traits, typename _Alloc>
2303 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2304 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2305 { return __lhs.compare(__rhs) <= 0; }
2308 * @brief Test if string doesn't follow C string.
2309 * @param lhs String.
2310 * @param rhs C string.
2311 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2313 template<typename _CharT, typename _Traits, typename _Alloc>
2315 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2316 const _CharT* __rhs)
2317 { return __lhs.compare(__rhs) <= 0; }
2320 * @brief Test if C string doesn't follow string.
2321 * @param lhs C string.
2322 * @param rhs String.
2323 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2325 template<typename _CharT, typename _Traits, typename _Alloc>
2327 operator<=(const _CharT* __lhs,
2328 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2329 { return __rhs.compare(__lhs) >= 0; }
2333 * @brief Test if string doesn't precede string.
2334 * @param lhs First string.
2335 * @param rhs Second string.
2336 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2338 template<typename _CharT, typename _Traits, typename _Alloc>
2340 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2341 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2342 { return __lhs.compare(__rhs) >= 0; }
2345 * @brief Test if string doesn't precede C string.
2346 * @param lhs String.
2347 * @param rhs C string.
2348 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2350 template<typename _CharT, typename _Traits, typename _Alloc>
2352 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2353 const _CharT* __rhs)
2354 { return __lhs.compare(__rhs) >= 0; }
2357 * @brief Test if C string doesn't precede string.
2358 * @param lhs C string.
2359 * @param rhs String.
2360 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2362 template<typename _CharT, typename _Traits, typename _Alloc>
2364 operator>=(const _CharT* __lhs,
2365 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2366 { return __rhs.compare(__lhs) <= 0; }
2369 * @brief Swap contents of two strings.
2370 * @param lhs First string.
2371 * @param rhs Second string.
2373 * Exchanges the contents of @a lhs and @a rhs in constant time.
2375 template<typename _CharT, typename _Traits, typename _Alloc>
2377 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
2378 basic_string<_CharT, _Traits, _Alloc>& __rhs)
2379 { __lhs.swap(__rhs); }
2382 * @brief Read stream into a string.
2383 * @param is Input stream.
2384 * @param str Buffer to store into.
2385 * @return Reference to the input stream.
2387 * Stores characters from @a is into @a str until whitespace is found, the
2388 * end of the stream is encountered, or str.max_size() is reached. If
2389 * is.width() is non-zero, that is the limit on the number of characters
2390 * stored into @a str. Any previous contents of @a str are erased.
2392 template<typename _CharT, typename _Traits, typename _Alloc>
2393 basic_istream<_CharT, _Traits>&
2394 operator>>(basic_istream<_CharT, _Traits>& __is,
2395 basic_string<_CharT, _Traits, _Alloc>& __str);
2398 basic_istream<char>&
2399 operator>>(basic_istream<char>& __is, basic_string<char>& __str);
2402 * @brief Write string to a stream.
2403 * @param os Output stream.
2404 * @param str String to write out.
2405 * @return Reference to the output stream.
2407 * Output characters of @a str into os following the same rules as for
2408 * writing a C string.
2410 template<typename _CharT, typename _Traits, typename _Alloc>
2411 inline basic_ostream<_CharT, _Traits>&
2412 operator<<(basic_ostream<_CharT, _Traits>& __os,
2413 const basic_string<_CharT, _Traits, _Alloc>& __str)
2415 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2416 // 586. string inserter not a formatted function
2417 return __ostream_insert(__os, __str.data(), __str.size());
2421 * @brief Read a line from stream into a string.
2422 * @param is Input stream.
2423 * @param str Buffer to store into.
2424 * @param delim Character marking end of line.
2425 * @return Reference to the input stream.
2427 * Stores characters from @a is into @a str until @a delim is found, the
2428 * end of the stream is encountered, or str.max_size() is reached. If
2429 * is.width() is non-zero, that is the limit on the number of characters
2430 * stored into @a str. Any previous contents of @a str are erased. If @a
2431 * delim was encountered, it is extracted but not stored into @a str.
2433 template<typename _CharT, typename _Traits, typename _Alloc>
2434 basic_istream<_CharT, _Traits>&
2435 getline(basic_istream<_CharT, _Traits>& __is,
2436 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
2439 * @brief Read a line from stream into a string.
2440 * @param is Input stream.
2441 * @param str Buffer to store into.
2442 * @return Reference to the input stream.
2444 * Stores characters from is into @a str until '\n' is found, the end of
2445 * the stream is encountered, or str.max_size() is reached. If is.width()
2446 * is non-zero, that is the limit on the number of characters stored into
2447 * @a str. Any previous contents of @a str are erased. If end of line was
2448 * encountered, it is extracted but not stored into @a str.
2450 template<typename _CharT, typename _Traits, typename _Alloc>
2451 inline basic_istream<_CharT, _Traits>&
2452 getline(basic_istream<_CharT, _Traits>& __is,
2453 basic_string<_CharT, _Traits, _Alloc>& __str)
2454 { return getline(__is, __str, __is.widen('\n')); }
2457 basic_istream<char>&
2458 getline(basic_istream<char>& __in, basic_string<char>& __str,
2461 #ifdef _GLIBCXX_USE_WCHAR_T
2463 basic_istream<wchar_t>&
2464 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
2468 _GLIBCXX_END_NAMESPACE
2470 #endif /* _BASIC_STRING_H */