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 __x, size_type __y)
404 _M_mutate(size_type __pos, size_type __len1, size_type __len2);
411 { return _Rep::_S_empty_rep(); }
414 // Construct/copy/destroy:
415 // NB: We overload ctors in some cases instead of using default
416 // arguments, per 17.4.4.4 para. 2 item 2.
419 * @brief Default constructor creates an empty string.
425 * @brief Construct an empty string using allocator @a a.
428 basic_string(const _Alloc& __a);
430 // NB: per LWG issue 42, semantics different from IS:
432 * @brief Construct string with copy of value of @a str.
433 * @param str Source string.
435 basic_string(const basic_string& __str);
437 * @brief Construct string as copy of a substring.
438 * @param str Source string.
439 * @param pos Index of first character to copy from.
440 * @param n Number of characters to copy (default remainder).
442 basic_string(const basic_string& __str, size_type __pos,
443 size_type __n = npos);
445 * @brief Construct string as copy of a substring.
446 * @param str Source string.
447 * @param pos Index of first character to copy from.
448 * @param n Number of characters to copy.
449 * @param a Allocator to use.
451 basic_string(const basic_string& __str, size_type __pos,
452 size_type __n, const _Alloc& __a);
455 * @brief Construct string initialized by a character array.
456 * @param s Source character array.
457 * @param n Number of characters to copy.
458 * @param a Allocator to use (default is default allocator).
460 * NB: @a s must have at least @a n characters, '\0' has no special
463 basic_string(const _CharT* __s, size_type __n,
464 const _Alloc& __a = _Alloc());
466 * @brief Construct string as copy of a C string.
467 * @param s Source C string.
468 * @param a Allocator to use (default is default allocator).
470 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
472 * @brief Construct string as multiple characters.
473 * @param n Number of characters.
474 * @param c Character to use.
475 * @param a Allocator to use (default is default allocator).
477 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
480 * @brief Construct string as copy of a range.
481 * @param beg Start of range.
482 * @param end End of range.
483 * @param a Allocator to use (default is default allocator).
485 template<class _InputIterator>
486 basic_string(_InputIterator __beg, _InputIterator __end,
487 const _Alloc& __a = _Alloc());
490 * @brief Destroy the string instance.
493 { _M_rep()->_M_dispose(this->get_allocator()); }
496 * @brief Assign the value of @a str to this string.
497 * @param str Source string.
500 operator=(const basic_string& __str)
501 { return this->assign(__str); }
504 * @brief Copy contents of @a s into this string.
505 * @param s Source null-terminated string.
508 operator=(const _CharT* __s)
509 { return this->assign(__s); }
512 * @brief Set value to string of length 1.
513 * @param c Source character.
515 * Assigning to a character makes this string length 1 and
516 * (*this)[0] == @a c.
519 operator=(_CharT __c)
521 this->assign(1, __c);
527 * Returns a read/write iterator that points to the first character in
528 * the %string. Unshares the string.
534 return iterator(_M_data());
538 * Returns a read-only (constant) iterator that points to the first
539 * character in the %string.
543 { return const_iterator(_M_data()); }
546 * Returns a read/write iterator that points one past the last
547 * character in the %string. Unshares the string.
553 return iterator(_M_data() + this->size());
557 * Returns a read-only (constant) iterator that points one past the
558 * last character in the %string.
562 { return const_iterator(_M_data() + this->size()); }
565 * Returns a read/write reverse iterator that points to the last
566 * character in the %string. Iteration is done in reverse element
567 * order. Unshares the string.
571 { return reverse_iterator(this->end()); }
574 * Returns a read-only (constant) reverse iterator that points
575 * to the last character in the %string. Iteration is done in
576 * reverse element order.
578 const_reverse_iterator
580 { return const_reverse_iterator(this->end()); }
583 * Returns a read/write reverse iterator that points to one before the
584 * first character in the %string. Iteration is done in reverse
585 * element order. Unshares the string.
589 { return reverse_iterator(this->begin()); }
592 * Returns a read-only (constant) reverse iterator that points
593 * to one before the first character in the %string. Iteration
594 * is done in reverse element order.
596 const_reverse_iterator
598 { return const_reverse_iterator(this->begin()); }
602 /// Returns the number of characters in the string, not including any
603 /// null-termination.
606 { return _M_rep()->_M_length; }
608 /// Returns the number of characters in the string, not including any
609 /// null-termination.
612 { return _M_rep()->_M_length; }
614 /// Returns the size() of the largest possible %string.
617 { return _Rep::_S_max_size; }
620 * @brief Resizes the %string to the specified number of characters.
621 * @param n Number of characters the %string should contain.
622 * @param c Character to fill any new elements.
624 * This function will %resize the %string to the specified
625 * number of characters. If the number is smaller than the
626 * %string's current size the %string is truncated, otherwise
627 * the %string is extended and new elements are set to @a c.
630 resize(size_type __n, _CharT __c);
633 * @brief Resizes the %string to the specified number of characters.
634 * @param n Number of characters the %string should contain.
636 * This function will resize the %string to the specified length. If
637 * the new size is smaller than the %string's current size the %string
638 * is truncated, otherwise the %string is extended and new characters
639 * are default-constructed. For basic types such as char, this means
643 resize(size_type __n)
644 { this->resize(__n, _CharT()); }
647 * Returns the total number of characters that the %string can hold
648 * before needing to allocate more memory.
652 { return _M_rep()->_M_capacity; }
655 * @brief Attempt to preallocate enough memory for specified number of
657 * @param res_arg Number of characters required.
658 * @throw std::length_error If @a res_arg exceeds @c max_size().
660 * This function attempts to reserve enough memory for the
661 * %string to hold the specified number of characters. If the
662 * number requested is more than max_size(), length_error is
665 * The advantage of this function is that if optimal code is a
666 * necessity and the user can determine the string length that will be
667 * required, the user can reserve the memory in %advance, and thus
668 * prevent a possible reallocation of memory and copying of %string
672 reserve(size_type __res_arg = 0);
675 * Erases the string, making it empty.
679 { _M_mutate(0, this->size(), 0); }
682 * Returns true if the %string is empty. Equivalent to *this == "".
686 { return this->size() == 0; }
690 * @brief Subscript access to the data contained in the %string.
691 * @param pos The index of the character to access.
692 * @return Read-only (constant) reference to the character.
694 * This operator allows for easy, array-style, data access.
695 * Note that data access with this operator is unchecked and
696 * out_of_range lookups are not defined. (For checked lookups
700 operator[] (size_type __pos) const
702 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
703 return _M_data()[__pos];
707 * @brief Subscript access to the data contained in the %string.
708 * @param pos The index of the character to access.
709 * @return Read/write reference to the character.
711 * This operator allows for easy, array-style, data access.
712 * Note that data access with this operator is unchecked and
713 * out_of_range lookups are not defined. (For checked lookups
714 * see at().) Unshares the string.
717 operator[](size_type __pos)
719 // allow pos == size() as v3 extension:
720 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
721 // but be strict in pedantic mode:
722 _GLIBCXX_DEBUG_PEDASSERT(__pos < size());
724 return _M_data()[__pos];
728 * @brief Provides access to the data contained in the %string.
729 * @param n The index of the character to access.
730 * @return Read-only (const) reference to the character.
731 * @throw std::out_of_range If @a n is an invalid index.
733 * This function provides for safer data access. The parameter is
734 * first checked that it is in the range of the string. The function
735 * throws out_of_range if the check fails.
738 at(size_type __n) const
740 if (__n >= this->size())
741 __throw_out_of_range(__N("basic_string::at"));
742 return _M_data()[__n];
746 * @brief Provides access to the data contained in the %string.
747 * @param n The index of the character to access.
748 * @return Read/write reference to the character.
749 * @throw std::out_of_range If @a n is an invalid index.
751 * This function provides for safer data access. The parameter is
752 * first checked that it is in the range of the string. The function
753 * throws out_of_range if the check fails. Success results in
754 * unsharing the string.
760 __throw_out_of_range(__N("basic_string::at"));
762 return _M_data()[__n];
767 * @brief Append a string to this string.
768 * @param str The string to append.
769 * @return Reference to this string.
772 operator+=(const basic_string& __str)
773 { return this->append(__str); }
776 * @brief Append a C string.
777 * @param s The C string to append.
778 * @return Reference to this string.
781 operator+=(const _CharT* __s)
782 { return this->append(__s); }
785 * @brief Append a character.
786 * @param c The character to append.
787 * @return Reference to this string.
790 operator+=(_CharT __c)
792 this->push_back(__c);
797 * @brief Append a string to this string.
798 * @param str The string to append.
799 * @return Reference to this string.
802 append(const basic_string& __str);
805 * @brief Append a substring.
806 * @param str The string to append.
807 * @param pos Index of the first character of str to append.
808 * @param n The number of characters to append.
809 * @return Reference to this string.
810 * @throw std::out_of_range if @a pos is not a valid index.
812 * This function appends @a n characters from @a str starting at @a pos
813 * to this string. If @a n is is larger than the number of available
814 * characters in @a str, the remainder of @a str is appended.
817 append(const basic_string& __str, size_type __pos, size_type __n);
820 * @brief Append a C substring.
821 * @param s The C string to append.
822 * @param n The number of characters to append.
823 * @return Reference to this string.
826 append(const _CharT* __s, size_type __n);
829 * @brief Append a C string.
830 * @param s The C string to append.
831 * @return Reference to this string.
834 append(const _CharT* __s)
836 __glibcxx_requires_string(__s);
837 return this->append(__s, traits_type::length(__s));
841 * @brief Append multiple characters.
842 * @param n The number of characters to append.
843 * @param c The character to use.
844 * @return Reference to this string.
846 * Appends n copies of c to this string.
849 append(size_type __n, _CharT __c);
852 * @brief Append a range of characters.
853 * @param first Iterator referencing the first character to append.
854 * @param last Iterator marking the end of the range.
855 * @return Reference to this string.
857 * Appends characters in the range [first,last) to this string.
859 template<class _InputIterator>
861 append(_InputIterator __first, _InputIterator __last)
862 { return this->replace(_M_iend(), _M_iend(), __first, __last); }
865 * @brief Append a single character.
866 * @param c Character to append.
869 push_back(_CharT __c)
871 const size_type __len = 1 + this->size();
872 if (__len > this->capacity() || _M_rep()->_M_is_shared())
873 this->reserve(__len);
874 traits_type::assign(_M_data()[this->size()], __c);
875 _M_rep()->_M_set_length_and_sharable(__len);
879 * @brief Set value to contents of another string.
880 * @param str Source string to use.
881 * @return Reference to this string.
884 assign(const basic_string& __str);
887 * @brief Set value to a substring of a string.
888 * @param str The string to use.
889 * @param pos Index of the first character of str.
890 * @param n Number of characters to use.
891 * @return Reference to this string.
892 * @throw std::out_of_range if @a pos is not a valid index.
894 * This function sets this string to the substring of @a str consisting
895 * of @a n characters at @a pos. If @a n is is larger than the number
896 * of available characters in @a str, the remainder of @a str is used.
899 assign(const basic_string& __str, size_type __pos, size_type __n)
900 { return this->assign(__str._M_data()
901 + __str._M_check(__pos, "basic_string::assign"),
902 __str._M_limit(__pos, __n)); }
905 * @brief Set value to a C substring.
906 * @param s The C string to use.
907 * @param n Number of characters to use.
908 * @return Reference to this string.
910 * This function sets the value of this string to the first @a n
911 * characters of @a s. If @a n is is larger than the number of
912 * available characters in @a s, the remainder of @a s is used.
915 assign(const _CharT* __s, size_type __n);
918 * @brief Set value to contents of a C string.
919 * @param s The C string to use.
920 * @return Reference to this string.
922 * This function sets the value of this string to the value of @a s.
923 * The data is copied, so there is no dependence on @a s once the
927 assign(const _CharT* __s)
929 __glibcxx_requires_string(__s);
930 return this->assign(__s, traits_type::length(__s));
934 * @brief Set value to multiple characters.
935 * @param n Length of the resulting string.
936 * @param c The character to use.
937 * @return Reference to this string.
939 * This function sets the value of this string to @a n copies of
943 assign(size_type __n, _CharT __c)
944 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
947 * @brief Set value to a range of characters.
948 * @param first Iterator referencing the first character to append.
949 * @param last Iterator marking the end of the range.
950 * @return Reference to this string.
952 * Sets value of string to characters in the range [first,last).
954 template<class _InputIterator>
956 assign(_InputIterator __first, _InputIterator __last)
957 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
960 * @brief Insert multiple characters.
961 * @param p Iterator referencing location in string to insert at.
962 * @param n Number of characters to insert
963 * @param c The character to insert.
964 * @throw std::length_error If new length exceeds @c max_size().
966 * Inserts @a n copies of character @a c starting at the position
967 * referenced by iterator @a p. If adding characters causes the length
968 * to exceed max_size(), length_error is thrown. The value of the
969 * string doesn't change if an error is thrown.
972 insert(iterator __p, size_type __n, _CharT __c)
973 { this->replace(__p, __p, __n, __c); }
976 * @brief Insert a range of characters.
977 * @param p Iterator referencing location in string to insert at.
978 * @param beg Start of range.
979 * @param end End of range.
980 * @throw std::length_error If new length exceeds @c max_size().
982 * Inserts characters in range [beg,end). If adding characters causes
983 * the length to exceed max_size(), length_error is thrown. The value
984 * of the string doesn't change if an error is thrown.
986 template<class _InputIterator>
988 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
989 { this->replace(__p, __p, __beg, __end); }
992 * @brief Insert value of a string.
993 * @param pos1 Iterator referencing location in string to insert at.
994 * @param str The string to insert.
995 * @return Reference to this string.
996 * @throw std::length_error If new length exceeds @c max_size().
998 * Inserts value of @a str starting at @a pos1. If adding characters
999 * causes the length to exceed max_size(), length_error is thrown. The
1000 * value of the string doesn't change if an error is thrown.
1003 insert(size_type __pos1, const basic_string& __str)
1004 { return this->insert(__pos1, __str, size_type(0), __str.size()); }
1007 * @brief Insert a substring.
1008 * @param pos1 Iterator referencing location in string to insert at.
1009 * @param str The string to insert.
1010 * @param pos2 Start of characters in str to insert.
1011 * @param n Number of characters to insert.
1012 * @return Reference to this string.
1013 * @throw std::length_error If new length exceeds @c max_size().
1014 * @throw std::out_of_range If @a pos1 > size() or
1015 * @a pos2 > @a str.size().
1017 * Starting at @a pos1, insert @a n character of @a str beginning with
1018 * @a pos2. If adding characters causes the length to exceed
1019 * max_size(), length_error is thrown. If @a pos1 is beyond the end of
1020 * this string or @a pos2 is beyond the end of @a str, out_of_range is
1021 * thrown. The value of the string doesn't change if an error is
1025 insert(size_type __pos1, const basic_string& __str,
1026 size_type __pos2, size_type __n)
1027 { return this->insert(__pos1, __str._M_data()
1028 + __str._M_check(__pos2, "basic_string::insert"),
1029 __str._M_limit(__pos2, __n)); }
1032 * @brief Insert a C substring.
1033 * @param pos Iterator referencing location in string to insert at.
1034 * @param s The C string to insert.
1035 * @param n The number of characters to insert.
1036 * @return Reference to this string.
1037 * @throw std::length_error If new length exceeds @c max_size().
1038 * @throw std::out_of_range If @a pos is beyond the end of this
1041 * Inserts the first @a n characters of @a s starting at @a pos. If
1042 * adding characters causes the length to exceed max_size(),
1043 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1044 * thrown. The value of the string doesn't change if an error is
1048 insert(size_type __pos, const _CharT* __s, size_type __n);
1051 * @brief Insert a C string.
1052 * @param pos Iterator referencing location in string to insert at.
1053 * @param s The C string to insert.
1054 * @return Reference to this string.
1055 * @throw std::length_error If new length exceeds @c max_size().
1056 * @throw std::out_of_range If @a pos is beyond the end of this
1059 * Inserts the first @a n characters of @a s starting at @a pos. If
1060 * adding characters causes the length to exceed max_size(),
1061 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1062 * thrown. The value of the string doesn't change if an error is
1066 insert(size_type __pos, const _CharT* __s)
1068 __glibcxx_requires_string(__s);
1069 return this->insert(__pos, __s, traits_type::length(__s));
1073 * @brief Insert multiple characters.
1074 * @param pos Index in string to insert at.
1075 * @param n Number of characters to insert
1076 * @param c The character to insert.
1077 * @return Reference to this string.
1078 * @throw std::length_error If new length exceeds @c max_size().
1079 * @throw std::out_of_range If @a pos is beyond the end of this
1082 * Inserts @a n copies of character @a c starting at index @a pos. If
1083 * adding characters causes the length to exceed max_size(),
1084 * length_error is thrown. If @a pos > length(), out_of_range is
1085 * thrown. The value of the string doesn't change if an error is
1089 insert(size_type __pos, size_type __n, _CharT __c)
1090 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
1091 size_type(0), __n, __c); }
1094 * @brief Insert one character.
1095 * @param p Iterator referencing position in string to insert at.
1096 * @param c The character to insert.
1097 * @return Iterator referencing newly inserted char.
1098 * @throw std::length_error If new length exceeds @c max_size().
1100 * Inserts character @a c at position referenced by @a p. If adding
1101 * character causes the length to exceed max_size(), length_error is
1102 * thrown. If @a p is beyond end of string, out_of_range is thrown.
1103 * The value of the string doesn't change if an error is thrown.
1106 insert(iterator __p, _CharT __c)
1108 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1109 const size_type __pos = __p - _M_ibegin();
1110 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1111 _M_rep()->_M_set_leaked();
1112 return iterator(_M_data() + __pos);
1116 * @brief Remove characters.
1117 * @param pos Index of first character to remove (default 0).
1118 * @param n Number of characters to remove (default remainder).
1119 * @return Reference to this string.
1120 * @throw std::out_of_range If @a pos is beyond the end of this
1123 * Removes @a n characters from this string starting at @a pos. The
1124 * length of the string is reduced by @a n. If there are < @a n
1125 * characters to remove, the remainder of the string is truncated. If
1126 * @a p is beyond end of string, out_of_range is thrown. The value of
1127 * the string doesn't change if an error is thrown.
1130 erase(size_type __pos = 0, size_type __n = npos)
1132 _M_mutate(_M_check(__pos, "basic_string::erase"),
1133 _M_limit(__pos, __n), size_type(0));
1138 * @brief Remove one character.
1139 * @param position Iterator referencing the character to remove.
1140 * @return iterator referencing same location after removal.
1142 * Removes the character at @a position from this string. The value
1143 * of the string doesn't change if an error is thrown.
1146 erase(iterator __position)
1148 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
1149 && __position < _M_iend());
1150 const size_type __pos = __position - _M_ibegin();
1151 _M_mutate(__pos, size_type(1), size_type(0));
1152 _M_rep()->_M_set_leaked();
1153 return iterator(_M_data() + __pos);
1157 * @brief Remove a range of characters.
1158 * @param first Iterator referencing the first character to remove.
1159 * @param last Iterator referencing the end of the range.
1160 * @return Iterator referencing location of first after removal.
1162 * Removes the characters in the range [first,last) from this string.
1163 * The value of the string doesn't change if an error is thrown.
1166 erase(iterator __first, iterator __last)
1168 _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
1169 && __last <= _M_iend());
1170 const size_type __pos = __first - _M_ibegin();
1171 _M_mutate(__pos, __last - __first, size_type(0));
1172 _M_rep()->_M_set_leaked();
1173 return iterator(_M_data() + __pos);
1177 * @brief Replace characters with value from another string.
1178 * @param pos Index of first character to replace.
1179 * @param n Number of characters to be replaced.
1180 * @param str String to insert.
1181 * @return Reference to this string.
1182 * @throw std::out_of_range If @a pos is beyond the end of this
1184 * @throw std::length_error If new length exceeds @c max_size().
1186 * Removes the characters in the range [pos,pos+n) from this string.
1187 * In place, the value of @a str is inserted. If @a pos is beyond end
1188 * of string, out_of_range is thrown. If the length of the result
1189 * exceeds max_size(), length_error is thrown. The value of the string
1190 * doesn't change if an error is thrown.
1193 replace(size_type __pos, size_type __n, const basic_string& __str)
1194 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1197 * @brief Replace characters with value from another string.
1198 * @param pos1 Index of first character to replace.
1199 * @param n1 Number of characters to be replaced.
1200 * @param str String to insert.
1201 * @param pos2 Index of first character of str to use.
1202 * @param n2 Number of characters from str to use.
1203 * @return Reference to this string.
1204 * @throw std::out_of_range If @a pos1 > size() or @a pos2 >
1206 * @throw std::length_error If new length exceeds @c max_size().
1208 * Removes the characters in the range [pos1,pos1 + n) from this
1209 * string. In place, the value of @a str is inserted. If @a pos is
1210 * beyond end of string, out_of_range is thrown. If the length of the
1211 * result exceeds max_size(), length_error is thrown. The value of the
1212 * string doesn't change if an error is thrown.
1215 replace(size_type __pos1, size_type __n1, const basic_string& __str,
1216 size_type __pos2, size_type __n2)
1217 { return this->replace(__pos1, __n1, __str._M_data()
1218 + __str._M_check(__pos2, "basic_string::replace"),
1219 __str._M_limit(__pos2, __n2)); }
1222 * @brief Replace characters with value of a C substring.
1223 * @param pos Index of first character to replace.
1224 * @param n1 Number of characters to be replaced.
1225 * @param s C string to insert.
1226 * @param n2 Number of characters from @a s to use.
1227 * @return Reference to this string.
1228 * @throw std::out_of_range If @a pos1 > size().
1229 * @throw std::length_error If new length exceeds @c max_size().
1231 * Removes the characters in the range [pos,pos + n1) from this string.
1232 * In place, the first @a n2 characters of @a s are inserted, or all
1233 * of @a s if @a n2 is too large. If @a pos is beyond end of string,
1234 * out_of_range is thrown. If the length of result exceeds max_size(),
1235 * length_error is thrown. The value of the string doesn't change if
1236 * an error is thrown.
1239 replace(size_type __pos, size_type __n1, const _CharT* __s,
1243 * @brief Replace characters with value of a C string.
1244 * @param pos Index of first character to replace.
1245 * @param n1 Number of characters to be replaced.
1246 * @param s C string to insert.
1247 * @return Reference to this string.
1248 * @throw std::out_of_range If @a pos > size().
1249 * @throw std::length_error If new length exceeds @c max_size().
1251 * Removes the characters in the range [pos,pos + n1) from this string.
1252 * In place, the first @a n characters of @a s are inserted. If @a
1253 * pos is beyond end of string, out_of_range is thrown. If the length
1254 * of result exceeds max_size(), length_error is thrown. The value of
1255 * the string doesn't change if an error is thrown.
1258 replace(size_type __pos, size_type __n1, const _CharT* __s)
1260 __glibcxx_requires_string(__s);
1261 return this->replace(__pos, __n1, __s, traits_type::length(__s));
1265 * @brief Replace characters with multiple characters.
1266 * @param pos Index of first character to replace.
1267 * @param n1 Number of characters to be replaced.
1268 * @param n2 Number of characters to insert.
1269 * @param c Character to insert.
1270 * @return Reference to this string.
1271 * @throw std::out_of_range If @a pos > size().
1272 * @throw std::length_error If new length exceeds @c max_size().
1274 * Removes the characters in the range [pos,pos + n1) from this string.
1275 * In place, @a n2 copies of @a c are inserted. If @a pos is beyond
1276 * end of string, out_of_range is thrown. If the length of result
1277 * exceeds max_size(), length_error is thrown. The value of the string
1278 * doesn't change if an error is thrown.
1281 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1282 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
1283 _M_limit(__pos, __n1), __n2, __c); }
1286 * @brief Replace range of characters with string.
1287 * @param i1 Iterator referencing start of range to replace.
1288 * @param i2 Iterator referencing end of range to replace.
1289 * @param str String value to insert.
1290 * @return Reference to this string.
1291 * @throw std::length_error If new length exceeds @c max_size().
1293 * Removes the characters in the range [i1,i2). In place, the value of
1294 * @a str is inserted. If the length of result exceeds max_size(),
1295 * length_error is thrown. The value of the string doesn't change if
1296 * an error is thrown.
1299 replace(iterator __i1, iterator __i2, const basic_string& __str)
1300 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
1303 * @brief Replace range of characters with C substring.
1304 * @param i1 Iterator referencing start of range to replace.
1305 * @param i2 Iterator referencing end of range to replace.
1306 * @param s C string value to insert.
1307 * @param n Number of characters from s to insert.
1308 * @return Reference to this string.
1309 * @throw std::length_error If new length exceeds @c max_size().
1311 * Removes the characters in the range [i1,i2). In place, the first @a
1312 * n characters of @a s are inserted. If the length of result exceeds
1313 * max_size(), length_error is thrown. The value of the string doesn't
1314 * change if an error is thrown.
1317 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
1319 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1320 && __i2 <= _M_iend());
1321 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
1325 * @brief Replace range of characters with C string.
1326 * @param i1 Iterator referencing start of range to replace.
1327 * @param i2 Iterator referencing end of range to replace.
1328 * @param s C string value to insert.
1329 * @return Reference to this string.
1330 * @throw std::length_error If new length exceeds @c max_size().
1332 * Removes the characters in the range [i1,i2). In place, the
1333 * characters of @a s are inserted. If the length of result exceeds
1334 * max_size(), length_error is thrown. The value of the string doesn't
1335 * change if an error is thrown.
1338 replace(iterator __i1, iterator __i2, const _CharT* __s)
1340 __glibcxx_requires_string(__s);
1341 return this->replace(__i1, __i2, __s, traits_type::length(__s));
1345 * @brief Replace range of characters with multiple characters
1346 * @param i1 Iterator referencing start of range to replace.
1347 * @param i2 Iterator referencing end of range to replace.
1348 * @param n Number of characters to insert.
1349 * @param c Character to insert.
1350 * @return Reference to this string.
1351 * @throw std::length_error If new length exceeds @c max_size().
1353 * Removes the characters in the range [i1,i2). In place, @a n copies
1354 * of @a c are inserted. If the length of result exceeds max_size(),
1355 * length_error is thrown. The value of the string doesn't change if
1356 * an error is thrown.
1359 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
1361 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1362 && __i2 <= _M_iend());
1363 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
1367 * @brief Replace range of characters with range.
1368 * @param i1 Iterator referencing start of range to replace.
1369 * @param i2 Iterator referencing end of range to replace.
1370 * @param k1 Iterator referencing start of range to insert.
1371 * @param k2 Iterator referencing end of range to insert.
1372 * @return Reference to this string.
1373 * @throw std::length_error If new length exceeds @c max_size().
1375 * Removes the characters in the range [i1,i2). In place, characters
1376 * in the range [k1,k2) are inserted. If the length of result exceeds
1377 * max_size(), length_error is thrown. The value of the string doesn't
1378 * change if an error is thrown.
1380 template<class _InputIterator>
1382 replace(iterator __i1, iterator __i2,
1383 _InputIterator __k1, _InputIterator __k2)
1385 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1386 && __i2 <= _M_iend());
1387 __glibcxx_requires_valid_range(__k1, __k2);
1388 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1389 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
1392 // Specializations for the common case of pointer and iterator:
1393 // useful to avoid the overhead of temporary buffering in _M_replace.
1395 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
1397 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1398 && __i2 <= _M_iend());
1399 __glibcxx_requires_valid_range(__k1, __k2);
1400 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1405 replace(iterator __i1, iterator __i2,
1406 const _CharT* __k1, const _CharT* __k2)
1408 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1409 && __i2 <= _M_iend());
1410 __glibcxx_requires_valid_range(__k1, __k2);
1411 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1416 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
1418 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1419 && __i2 <= _M_iend());
1420 __glibcxx_requires_valid_range(__k1, __k2);
1421 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1422 __k1.base(), __k2 - __k1);
1426 replace(iterator __i1, iterator __i2,
1427 const_iterator __k1, const_iterator __k2)
1429 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1430 && __i2 <= _M_iend());
1431 __glibcxx_requires_valid_range(__k1, __k2);
1432 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1433 __k1.base(), __k2 - __k1);
1437 template<class _Integer>
1439 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
1440 _Integer __val, __true_type)
1441 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
1443 template<class _InputIterator>
1445 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
1446 _InputIterator __k2, __false_type);
1449 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
1453 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
1456 // _S_construct_aux is used to implement the 21.3.1 para 15 which
1457 // requires special behaviour if _InIter is an integral type
1458 template<class _InIterator>
1460 _S_construct_aux(_InIterator __beg, _InIterator __end,
1461 const _Alloc& __a, __false_type)
1463 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
1464 return _S_construct(__beg, __end, __a, _Tag());
1467 template<class _InIterator>
1469 _S_construct_aux(_InIterator __beg, _InIterator __end,
1470 const _Alloc& __a, __true_type)
1471 { return _S_construct(static_cast<size_type>(__beg),
1472 static_cast<value_type>(__end), __a); }
1474 template<class _InIterator>
1476 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
1478 typedef typename std::__is_integer<_InIterator>::__type _Integral;
1479 return _S_construct_aux(__beg, __end, __a, _Integral());
1482 // For Input Iterators, used in istreambuf_iterators, etc.
1483 template<class _InIterator>
1485 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1486 input_iterator_tag);
1488 // For forward_iterators up to random_access_iterators, used for
1489 // string::iterator, _CharT*, etc.
1490 template<class _FwdIterator>
1492 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
1493 forward_iterator_tag);
1496 _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
1501 * @brief Copy substring into C string.
1502 * @param s C string to copy value into.
1503 * @param n Number of characters to copy.
1504 * @param pos Index of first character to copy.
1505 * @return Number of characters actually copied
1506 * @throw std::out_of_range If pos > size().
1508 * Copies up to @a n characters starting at @a pos into the C string @a
1509 * s. If @a pos is greater than size(), out_of_range is thrown.
1512 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
1515 * @brief Swap contents with another string.
1516 * @param s String to swap with.
1518 * Exchanges the contents of this string with that of @a s in constant
1522 swap(basic_string& __s);
1524 // String operations:
1526 * @brief Return const pointer to null-terminated contents.
1528 * This is a handle to internal data. Do not modify or dire things may
1533 { return _M_data(); }
1536 * @brief Return const pointer to contents.
1538 * This is a handle to internal data. Do not modify or dire things may
1543 { return _M_data(); }
1546 * @brief Return copy of allocator used to construct this string.
1549 get_allocator() const
1550 { return _M_dataplus; }
1553 * @brief Find position of a C substring.
1554 * @param s C string to locate.
1555 * @param pos Index of character to search from.
1556 * @param n Number of characters from @a s to search for.
1557 * @return Index of start of first occurrence.
1559 * Starting from @a pos, searches forward for the first @a n characters
1560 * in @a s within this string. If found, returns the index where it
1561 * begins. If not found, returns npos.
1564 find(const _CharT* __s, size_type __pos, size_type __n) const;
1567 * @brief Find position of a string.
1568 * @param str String to locate.
1569 * @param pos Index of character to search from (default 0).
1570 * @return Index of start of first occurrence.
1572 * Starting from @a pos, searches forward for value of @a str within
1573 * this string. If found, returns the index where it begins. If not
1574 * found, returns npos.
1577 find(const basic_string& __str, size_type __pos = 0) const
1578 { return this->find(__str.data(), __pos, __str.size()); }
1581 * @brief Find position of a C string.
1582 * @param s C string to locate.
1583 * @param pos Index of character to search from (default 0).
1584 * @return Index of start of first occurrence.
1586 * Starting from @a pos, searches forward for the value of @a s within
1587 * this string. If found, returns the index where it begins. If not
1588 * found, returns npos.
1591 find(const _CharT* __s, size_type __pos = 0) const
1593 __glibcxx_requires_string(__s);
1594 return this->find(__s, __pos, traits_type::length(__s));
1598 * @brief Find position of a character.
1599 * @param c Character to locate.
1600 * @param pos Index of character to search from (default 0).
1601 * @return Index of first occurrence.
1603 * Starting from @a pos, searches forward for @a c within this string.
1604 * If found, returns the index where it was found. If not found,
1608 find(_CharT __c, size_type __pos = 0) const;
1611 * @brief Find last position of a string.
1612 * @param str String to locate.
1613 * @param pos Index of character to search back from (default end).
1614 * @return Index of start of last occurrence.
1616 * Starting from @a pos, searches backward for value of @a str within
1617 * this string. If found, returns the index where it begins. If not
1618 * found, returns npos.
1621 rfind(const basic_string& __str, size_type __pos = npos) const
1622 { return this->rfind(__str.data(), __pos, __str.size()); }
1625 * @brief Find last position of a C substring.
1626 * @param s C string to locate.
1627 * @param pos Index of character to search back from.
1628 * @param n Number of characters from s to search for.
1629 * @return Index of start of last occurrence.
1631 * Starting from @a pos, searches backward for the first @a n
1632 * characters in @a s within this string. If found, returns the index
1633 * where it begins. If not found, returns npos.
1636 rfind(const _CharT* __s, size_type __pos, size_type __n) const;
1639 * @brief Find last position of a C string.
1640 * @param s C string to locate.
1641 * @param pos Index of character to start search at (default end).
1642 * @return Index of start of last occurrence.
1644 * Starting from @a pos, searches backward for the value of @a s within
1645 * this string. If found, returns the index where it begins. If not
1646 * found, returns npos.
1649 rfind(const _CharT* __s, size_type __pos = npos) const
1651 __glibcxx_requires_string(__s);
1652 return this->rfind(__s, __pos, traits_type::length(__s));
1656 * @brief Find last position of a character.
1657 * @param c Character to locate.
1658 * @param pos Index of character to search back from (default end).
1659 * @return Index of last occurrence.
1661 * Starting from @a pos, searches backward for @a c within this string.
1662 * If found, returns the index where it was found. If not found,
1666 rfind(_CharT __c, size_type __pos = npos) const;
1669 * @brief Find position of a character of string.
1670 * @param str String containing characters to locate.
1671 * @param pos Index of character to search from (default 0).
1672 * @return Index of first occurrence.
1674 * Starting from @a pos, searches forward for one of the characters of
1675 * @a str within this string. If found, returns the index where it was
1676 * found. If not found, returns npos.
1679 find_first_of(const basic_string& __str, size_type __pos = 0) const
1680 { return this->find_first_of(__str.data(), __pos, __str.size()); }
1683 * @brief Find position of a character of C substring.
1684 * @param s String containing characters to locate.
1685 * @param pos Index of character to search from.
1686 * @param n Number of characters from s to search for.
1687 * @return Index of first occurrence.
1689 * Starting from @a pos, searches forward for one of the first @a n
1690 * characters of @a s within this string. If found, returns the index
1691 * where it was found. If not found, returns npos.
1694 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
1697 * @brief Find position of a character of C string.
1698 * @param s String containing characters to locate.
1699 * @param pos Index of character to search from (default 0).
1700 * @return Index of first occurrence.
1702 * Starting from @a pos, searches forward for one of the characters of
1703 * @a s within this string. If found, returns the index where it was
1704 * found. If not found, returns npos.
1707 find_first_of(const _CharT* __s, size_type __pos = 0) const
1709 __glibcxx_requires_string(__s);
1710 return this->find_first_of(__s, __pos, traits_type::length(__s));
1714 * @brief Find position of a character.
1715 * @param c Character to locate.
1716 * @param pos Index of character to search from (default 0).
1717 * @return Index of first occurrence.
1719 * Starting from @a pos, searches forward for the character @a c within
1720 * this string. If found, returns the index where it was found. If
1721 * not found, returns npos.
1723 * Note: equivalent to find(c, pos).
1726 find_first_of(_CharT __c, size_type __pos = 0) const
1727 { return this->find(__c, __pos); }
1730 * @brief Find last position of a character of string.
1731 * @param str String containing characters to locate.
1732 * @param pos Index of character to search back from (default end).
1733 * @return Index of last occurrence.
1735 * Starting from @a pos, searches backward for one of the characters of
1736 * @a str within this string. If found, returns the index where it was
1737 * found. If not found, returns npos.
1740 find_last_of(const basic_string& __str, size_type __pos = npos) const
1741 { return this->find_last_of(__str.data(), __pos, __str.size()); }
1744 * @brief Find last position of a character of C substring.
1745 * @param s C string containing characters to locate.
1746 * @param pos Index of character to search back from.
1747 * @param n Number of characters from s to search for.
1748 * @return Index of last occurrence.
1750 * Starting from @a pos, searches backward for one of the first @a n
1751 * characters of @a s within this string. If found, returns the index
1752 * where it was found. If not found, returns npos.
1755 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
1758 * @brief Find last position of a character of C string.
1759 * @param s C string containing characters to locate.
1760 * @param pos Index of character to search back from (default end).
1761 * @return Index of last occurrence.
1763 * Starting from @a pos, searches backward for one of the characters of
1764 * @a s within this string. If found, returns the index where it was
1765 * found. If not found, returns npos.
1768 find_last_of(const _CharT* __s, size_type __pos = npos) const
1770 __glibcxx_requires_string(__s);
1771 return this->find_last_of(__s, __pos, traits_type::length(__s));
1775 * @brief Find last position of a character.
1776 * @param c Character to locate.
1777 * @param pos Index of character to search back from (default end).
1778 * @return Index of last occurrence.
1780 * Starting from @a pos, searches backward for @a c within this string.
1781 * If found, returns the index where it was found. If not found,
1784 * Note: equivalent to rfind(c, pos).
1787 find_last_of(_CharT __c, size_type __pos = npos) const
1788 { return this->rfind(__c, __pos); }
1791 * @brief Find position of a character not in string.
1792 * @param str String containing characters to avoid.
1793 * @param pos Index of character to search from (default 0).
1794 * @return Index of first occurrence.
1796 * Starting from @a pos, searches forward for a character not contained
1797 * in @a str within this string. If found, returns the index where it
1798 * was found. If not found, returns npos.
1801 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
1802 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
1805 * @brief Find position of a character not in C substring.
1806 * @param s C string containing characters to avoid.
1807 * @param pos Index of character to search from.
1808 * @param n Number of characters from s to consider.
1809 * @return Index of first occurrence.
1811 * Starting from @a pos, searches forward for a character not contained
1812 * in the first @a n characters of @a s within this string. If found,
1813 * returns the index where it was found. If not found, returns npos.
1816 find_first_not_of(const _CharT* __s, size_type __pos,
1817 size_type __n) const;
1820 * @brief Find position of a character not in C string.
1821 * @param s C string containing characters to avoid.
1822 * @param pos Index of character to search from (default 0).
1823 * @return Index of first occurrence.
1825 * Starting from @a pos, searches forward for a character not contained
1826 * in @a s within this string. If found, returns the index where it
1827 * was found. If not found, returns npos.
1830 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
1832 __glibcxx_requires_string(__s);
1833 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
1837 * @brief Find position of a different character.
1838 * @param c Character to avoid.
1839 * @param pos Index of character to search from (default 0).
1840 * @return Index of first occurrence.
1842 * Starting from @a pos, searches forward for a character other than @a c
1843 * within this string. If found, returns the index where it was found.
1844 * If not found, returns npos.
1847 find_first_not_of(_CharT __c, size_type __pos = 0) const;
1850 * @brief Find last position of a character not in string.
1851 * @param str String containing characters to avoid.
1852 * @param pos Index of character to search back from (default end).
1853 * @return Index of last occurrence.
1855 * Starting from @a pos, searches backward for a character not
1856 * contained in @a str within this string. If found, returns the index
1857 * where it was found. If not found, returns npos.
1860 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
1861 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
1864 * @brief Find last position of a character not in C substring.
1865 * @param s C string containing characters to avoid.
1866 * @param pos Index of character to search back from.
1867 * @param n Number of characters from s to consider.
1868 * @return Index of last occurrence.
1870 * Starting from @a pos, searches backward for a character not
1871 * contained in the first @a n characters of @a s within this string.
1872 * If found, returns the index where it was found. If not found,
1876 find_last_not_of(const _CharT* __s, size_type __pos,
1877 size_type __n) const;
1879 * @brief Find last position of a character not in C string.
1880 * @param s C string containing characters to avoid.
1881 * @param pos Index of character to search back from (default end).
1882 * @return Index of last occurrence.
1884 * Starting from @a pos, searches backward for a character not
1885 * contained in @a s within this string. If found, returns the index
1886 * where it was found. If not found, returns npos.
1889 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
1891 __glibcxx_requires_string(__s);
1892 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
1896 * @brief Find last position of a different character.
1897 * @param c Character to avoid.
1898 * @param pos Index of character to search back from (default end).
1899 * @return Index of last occurrence.
1901 * Starting from @a pos, searches backward for a character other than
1902 * @a c within this string. If found, returns the index where it was
1903 * found. If not found, returns npos.
1906 find_last_not_of(_CharT __c, size_type __pos = npos) const;
1909 * @brief Get a substring.
1910 * @param pos Index of first character (default 0).
1911 * @param n Number of characters in substring (default remainder).
1912 * @return The new string.
1913 * @throw std::out_of_range If pos > size().
1915 * Construct and return a new string using the @a n characters starting
1916 * at @a pos. If the string is too short, use the remainder of the
1917 * characters. If @a pos is beyond the end of the string, out_of_range
1921 substr(size_type __pos = 0, size_type __n = npos) const
1922 { return basic_string(*this,
1923 _M_check(__pos, "basic_string::substr"), __n); }
1926 * @brief Compare to a string.
1927 * @param str String to compare against.
1928 * @return Integer < 0, 0, or > 0.
1930 * Returns an integer < 0 if this string is ordered before @a str, 0 if
1931 * their values are equivalent, or > 0 if this string is ordered after
1932 * @a str. Determines the effective length rlen of the strings to
1933 * compare as the smallest of size() and str.size(). The function
1934 * then compares the two strings by calling traits::compare(data(),
1935 * str.data(),rlen). If the result of the comparison is nonzero returns
1936 * it, otherwise the shorter one is ordered first.
1939 compare(const basic_string& __str) const
1941 const size_type __size = this->size();
1942 const size_type __osize = __str.size();
1943 const size_type __len = std::min(__size, __osize);
1945 int __r = traits_type::compare(_M_data(), __str.data(), __len);
1947 __r = _S_compare(__size, __osize);
1952 * @brief Compare substring to a string.
1953 * @param pos Index of first character of substring.
1954 * @param n Number of characters in substring.
1955 * @param str String to compare against.
1956 * @return Integer < 0, 0, or > 0.
1958 * Form the substring of this string from the @a n characters starting
1959 * at @a pos. Returns an integer < 0 if the substring is ordered
1960 * before @a str, 0 if their values are equivalent, or > 0 if the
1961 * substring is ordered after @a str. Determines the effective length
1962 * rlen of the strings to compare as the smallest of the length of the
1963 * substring and @a str.size(). The function then compares the two
1964 * strings by calling traits::compare(substring.data(),str.data(),rlen).
1965 * If the result of the comparison is nonzero returns it, otherwise the
1966 * shorter one is ordered first.
1969 compare(size_type __pos, size_type __n, const basic_string& __str) const;
1972 * @brief Compare substring to a substring.
1973 * @param pos1 Index of first character of substring.
1974 * @param n1 Number of characters in substring.
1975 * @param str String to compare against.
1976 * @param pos2 Index of first character of substring of str.
1977 * @param n2 Number of characters in substring of str.
1978 * @return Integer < 0, 0, or > 0.
1980 * Form the substring of this string from the @a n1 characters starting
1981 * at @a pos1. Form the substring of @a str from the @a n2 characters
1982 * starting at @a pos2. Returns an integer < 0 if this substring is
1983 * ordered before the substring of @a str, 0 if their values are
1984 * equivalent, or > 0 if this substring is ordered after the substring
1985 * of @a str. Determines the effective length rlen of the strings
1986 * to compare as the smallest of the lengths of the substrings. The
1987 * function then compares the two strings by calling
1988 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
1989 * If the result of the comparison is nonzero returns it, otherwise the
1990 * shorter one is ordered first.
1993 compare(size_type __pos1, size_type __n1, const basic_string& __str,
1994 size_type __pos2, size_type __n2) const;
1997 * @brief Compare to a C string.
1998 * @param s C string to compare against.
1999 * @return Integer < 0, 0, or > 0.
2001 * Returns an integer < 0 if this string is ordered before @a s, 0 if
2002 * their values are equivalent, or > 0 if this string is ordered after
2003 * @a s. Determines the effective length rlen of the strings to
2004 * compare as the smallest of size() and the length of a string
2005 * constructed from @a s. The function then compares the two strings
2006 * by calling traits::compare(data(),s,rlen). If the result of the
2007 * comparison is nonzero returns it, otherwise the shorter one is
2011 compare(const _CharT* __s) const;
2013 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2014 // 5 String::compare specification questionable
2016 * @brief Compare substring to a C string.
2017 * @param pos Index of first character of substring.
2018 * @param n1 Number of characters in substring.
2019 * @param s C string to compare against.
2020 * @return Integer < 0, 0, or > 0.
2022 * Form the substring of this string from the @a n1 characters starting
2023 * at @a pos. Returns an integer < 0 if the substring is ordered
2024 * before @a s, 0 if their values are equivalent, or > 0 if the
2025 * substring is ordered after @a s. Determines the effective length
2026 * rlen of the strings to compare as the smallest of the length of the
2027 * substring and the length of a string constructed from @a s. The
2028 * function then compares the two string by calling
2029 * traits::compare(substring.data(),s,rlen). If the result of the
2030 * comparison is nonzero returns it, otherwise the shorter one is
2034 compare(size_type __pos, size_type __n1, const _CharT* __s) const;
2037 * @brief Compare substring against a character array.
2038 * @param pos1 Index of first character of substring.
2039 * @param n1 Number of characters in substring.
2040 * @param s character array to compare against.
2041 * @param n2 Number of characters of s.
2042 * @return Integer < 0, 0, or > 0.
2044 * Form the substring of this string from the @a n1 characters starting
2045 * at @a pos1. Form a string from the first @a n2 characters of @a s.
2046 * Returns an integer < 0 if this substring is ordered before the string
2047 * from @a s, 0 if their values are equivalent, or > 0 if this substring
2048 * is ordered after the string from @a s. Determines the effective
2049 * length rlen of the strings to compare as the smallest of the length
2050 * of the substring and @a n2. The function then compares the two
2051 * strings by calling traits::compare(substring.data(),s,rlen). If the
2052 * result of the comparison is nonzero returns it, otherwise the shorter
2053 * one is ordered first.
2055 * NB: s must have at least n2 characters, '\0' has no special
2059 compare(size_type __pos, size_type __n1, const _CharT* __s,
2060 size_type __n2) const;
2063 template<typename _CharT, typename _Traits, typename _Alloc>
2064 inline basic_string<_CharT, _Traits, _Alloc>::
2066 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
2067 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
2069 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()) { }
2074 * @brief Concatenate two strings.
2075 * @param lhs First string.
2076 * @param rhs Last string.
2077 * @return New string with value of @a lhs followed by @a rhs.
2079 template<typename _CharT, typename _Traits, typename _Alloc>
2080 basic_string<_CharT, _Traits, _Alloc>
2081 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2082 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2084 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2085 __str.append(__rhs);
2090 * @brief Concatenate C string and string.
2091 * @param lhs First string.
2092 * @param rhs Last string.
2093 * @return New string with value of @a lhs followed by @a rhs.
2095 template<typename _CharT, typename _Traits, typename _Alloc>
2096 basic_string<_CharT,_Traits,_Alloc>
2097 operator+(const _CharT* __lhs,
2098 const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2101 * @brief Concatenate character and string.
2102 * @param lhs First string.
2103 * @param rhs Last string.
2104 * @return New string with @a lhs followed by @a rhs.
2106 template<typename _CharT, typename _Traits, typename _Alloc>
2107 basic_string<_CharT,_Traits,_Alloc>
2108 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2111 * @brief Concatenate string and C string.
2112 * @param lhs First string.
2113 * @param rhs Last string.
2114 * @return New string with @a lhs followed by @a rhs.
2116 template<typename _CharT, typename _Traits, typename _Alloc>
2117 inline basic_string<_CharT, _Traits, _Alloc>
2118 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2119 const _CharT* __rhs)
2121 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2122 __str.append(__rhs);
2127 * @brief Concatenate string and character.
2128 * @param lhs First string.
2129 * @param rhs Last string.
2130 * @return New string with @a lhs followed by @a rhs.
2132 template<typename _CharT, typename _Traits, typename _Alloc>
2133 inline basic_string<_CharT, _Traits, _Alloc>
2134 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
2136 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
2137 typedef typename __string_type::size_type __size_type;
2138 __string_type __str(__lhs);
2139 __str.append(__size_type(1), __rhs);
2145 * @brief Test equivalence of two strings.
2146 * @param lhs First string.
2147 * @param rhs Second string.
2148 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2150 template<typename _CharT, typename _Traits, typename _Alloc>
2152 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2153 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2154 { return __lhs.compare(__rhs) == 0; }
2157 * @brief Test equivalence of C string and string.
2158 * @param lhs C string.
2159 * @param rhs String.
2160 * @return True if @a rhs.compare(@a lhs) == 0. False otherwise.
2162 template<typename _CharT, typename _Traits, typename _Alloc>
2164 operator==(const _CharT* __lhs,
2165 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2166 { return __rhs.compare(__lhs) == 0; }
2169 * @brief Test equivalence of string and C string.
2170 * @param lhs String.
2171 * @param rhs C string.
2172 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2174 template<typename _CharT, typename _Traits, typename _Alloc>
2176 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2177 const _CharT* __rhs)
2178 { return __lhs.compare(__rhs) == 0; }
2182 * @brief Test difference of two strings.
2183 * @param lhs First string.
2184 * @param rhs Second string.
2185 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2187 template<typename _CharT, typename _Traits, typename _Alloc>
2189 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2190 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2191 { return __rhs.compare(__lhs) != 0; }
2194 * @brief Test difference of C string and string.
2195 * @param lhs C string.
2196 * @param rhs String.
2197 * @return True if @a rhs.compare(@a lhs) != 0. False otherwise.
2199 template<typename _CharT, typename _Traits, typename _Alloc>
2201 operator!=(const _CharT* __lhs,
2202 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2203 { return __rhs.compare(__lhs) != 0; }
2206 * @brief Test difference of string and C string.
2207 * @param lhs String.
2208 * @param rhs C string.
2209 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2211 template<typename _CharT, typename _Traits, typename _Alloc>
2213 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2214 const _CharT* __rhs)
2215 { return __lhs.compare(__rhs) != 0; }
2219 * @brief Test if string precedes string.
2220 * @param lhs First string.
2221 * @param rhs Second string.
2222 * @return True if @a lhs precedes @a rhs. False otherwise.
2224 template<typename _CharT, typename _Traits, typename _Alloc>
2226 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2227 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2228 { return __lhs.compare(__rhs) < 0; }
2231 * @brief Test if string precedes C string.
2232 * @param lhs String.
2233 * @param rhs C string.
2234 * @return True if @a lhs precedes @a rhs. False otherwise.
2236 template<typename _CharT, typename _Traits, typename _Alloc>
2238 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2239 const _CharT* __rhs)
2240 { return __lhs.compare(__rhs) < 0; }
2243 * @brief Test if C string precedes string.
2244 * @param lhs C string.
2245 * @param rhs String.
2246 * @return True if @a lhs precedes @a rhs. False otherwise.
2248 template<typename _CharT, typename _Traits, typename _Alloc>
2250 operator<(const _CharT* __lhs,
2251 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2252 { return __rhs.compare(__lhs) > 0; }
2256 * @brief Test if string follows string.
2257 * @param lhs First string.
2258 * @param rhs Second string.
2259 * @return True if @a lhs follows @a rhs. False otherwise.
2261 template<typename _CharT, typename _Traits, typename _Alloc>
2263 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2264 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2265 { return __lhs.compare(__rhs) > 0; }
2268 * @brief Test if string follows C string.
2269 * @param lhs String.
2270 * @param rhs C string.
2271 * @return True if @a lhs follows @a rhs. False otherwise.
2273 template<typename _CharT, typename _Traits, typename _Alloc>
2275 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2276 const _CharT* __rhs)
2277 { return __lhs.compare(__rhs) > 0; }
2280 * @brief Test if C string follows string.
2281 * @param lhs C string.
2282 * @param rhs String.
2283 * @return True if @a lhs follows @a rhs. False otherwise.
2285 template<typename _CharT, typename _Traits, typename _Alloc>
2287 operator>(const _CharT* __lhs,
2288 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2289 { return __rhs.compare(__lhs) < 0; }
2293 * @brief Test if string doesn't follow string.
2294 * @param lhs First string.
2295 * @param rhs Second string.
2296 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2298 template<typename _CharT, typename _Traits, typename _Alloc>
2300 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2301 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2302 { return __lhs.compare(__rhs) <= 0; }
2305 * @brief Test if string doesn't follow C string.
2306 * @param lhs String.
2307 * @param rhs C string.
2308 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2310 template<typename _CharT, typename _Traits, typename _Alloc>
2312 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2313 const _CharT* __rhs)
2314 { return __lhs.compare(__rhs) <= 0; }
2317 * @brief Test if C string doesn't follow string.
2318 * @param lhs C string.
2319 * @param rhs String.
2320 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2322 template<typename _CharT, typename _Traits, typename _Alloc>
2324 operator<=(const _CharT* __lhs,
2325 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2326 { return __rhs.compare(__lhs) >= 0; }
2330 * @brief Test if string doesn't precede string.
2331 * @param lhs First string.
2332 * @param rhs Second string.
2333 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2335 template<typename _CharT, typename _Traits, typename _Alloc>
2337 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2338 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2339 { return __lhs.compare(__rhs) >= 0; }
2342 * @brief Test if string doesn't precede C string.
2343 * @param lhs String.
2344 * @param rhs C string.
2345 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2347 template<typename _CharT, typename _Traits, typename _Alloc>
2349 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2350 const _CharT* __rhs)
2351 { return __lhs.compare(__rhs) >= 0; }
2354 * @brief Test if C string doesn't precede string.
2355 * @param lhs C string.
2356 * @param rhs String.
2357 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2359 template<typename _CharT, typename _Traits, typename _Alloc>
2361 operator>=(const _CharT* __lhs,
2362 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2363 { return __rhs.compare(__lhs) <= 0; }
2366 * @brief Swap contents of two strings.
2367 * @param lhs First string.
2368 * @param rhs Second string.
2370 * Exchanges the contents of @a lhs and @a rhs in constant time.
2372 template<typename _CharT, typename _Traits, typename _Alloc>
2374 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
2375 basic_string<_CharT, _Traits, _Alloc>& __rhs)
2376 { __lhs.swap(__rhs); }
2379 * @brief Read stream into a string.
2380 * @param is Input stream.
2381 * @param str Buffer to store into.
2382 * @return Reference to the input stream.
2384 * Stores characters from @a is into @a str until whitespace is found, the
2385 * end of the stream is encountered, or str.max_size() is reached. If
2386 * is.width() is non-zero, that is the limit on the number of characters
2387 * stored into @a str. Any previous contents of @a str are erased.
2389 template<typename _CharT, typename _Traits, typename _Alloc>
2390 basic_istream<_CharT, _Traits>&
2391 operator>>(basic_istream<_CharT, _Traits>& __is,
2392 basic_string<_CharT, _Traits, _Alloc>& __str);
2395 basic_istream<char>&
2396 operator>>(basic_istream<char>& __is, basic_string<char>& __str);
2399 * @brief Write string to a stream.
2400 * @param os Output stream.
2401 * @param str String to write out.
2402 * @return Reference to the output stream.
2404 * Output characters of @a str into os following the same rules as for
2405 * writing a C string.
2407 template<typename _CharT, typename _Traits, typename _Alloc>
2408 inline basic_ostream<_CharT, _Traits>&
2409 operator<<(basic_ostream<_CharT, _Traits>& __os,
2410 const basic_string<_CharT, _Traits, _Alloc>& __str)
2412 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2413 // 586. string inserter not a formatted function
2414 return __ostream_insert(__os, __str.data(), __str.size());
2418 * @brief Read a line from stream into a string.
2419 * @param is Input stream.
2420 * @param str Buffer to store into.
2421 * @param delim Character marking end of line.
2422 * @return Reference to the input stream.
2424 * Stores characters from @a is into @a str until @a delim is found, the
2425 * end of the stream is encountered, or str.max_size() is reached. If
2426 * is.width() is non-zero, that is the limit on the number of characters
2427 * stored into @a str. Any previous contents of @a str are erased. If @a
2428 * delim was encountered, it is extracted but not stored into @a str.
2430 template<typename _CharT, typename _Traits, typename _Alloc>
2431 basic_istream<_CharT, _Traits>&
2432 getline(basic_istream<_CharT, _Traits>& __is,
2433 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
2436 * @brief Read a line from stream into a string.
2437 * @param is Input stream.
2438 * @param str Buffer to store into.
2439 * @return Reference to the input stream.
2441 * Stores characters from is into @a str until '\n' is found, the end of
2442 * the stream is encountered, or str.max_size() is reached. If is.width()
2443 * is non-zero, that is the limit on the number of characters stored into
2444 * @a str. Any previous contents of @a str are erased. If end of line was
2445 * encountered, it is extracted but not stored into @a str.
2447 template<typename _CharT, typename _Traits, typename _Alloc>
2448 inline basic_istream<_CharT, _Traits>&
2449 getline(basic_istream<_CharT, _Traits>& __is,
2450 basic_string<_CharT, _Traits, _Alloc>& __str)
2451 { return getline(__is, __str, __is.widen('\n')); }
2454 basic_istream<char>&
2455 getline(basic_istream<char>& __in, basic_string<char>& __str,
2458 #ifdef _GLIBCXX_USE_WCHAR_T
2460 basic_istream<wchar_t>&
2461 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
2465 _GLIBCXX_END_NAMESPACE
2467 #endif /* _BASIC_STRING_H */