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>
48 #include <initializer_list>
50 _GLIBCXX_BEGIN_NAMESPACE(std)
53 * @class basic_string basic_string.h <string>
54 * @brief Managing sequences of characters and character-like objects.
59 * Meets the requirements of a <a href="tables.html#65">container</a>, a
60 * <a href="tables.html#66">reversible container</a>, and a
61 * <a href="tables.html#67">sequence</a>. Of the
62 * <a href="tables.html#68">optional sequence requirements</a>, only
63 * @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.
108 // 21.3 Template class basic_string
109 template<typename _CharT, typename _Traits, typename _Alloc>
112 typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
116 typedef _Traits traits_type;
117 typedef typename _Traits::char_type value_type;
118 typedef _Alloc allocator_type;
119 typedef typename _CharT_alloc_type::size_type size_type;
120 typedef typename _CharT_alloc_type::difference_type difference_type;
121 typedef typename _CharT_alloc_type::reference reference;
122 typedef typename _CharT_alloc_type::const_reference const_reference;
123 typedef typename _CharT_alloc_type::pointer pointer;
124 typedef typename _CharT_alloc_type::const_pointer const_pointer;
125 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
126 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
128 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
129 typedef std::reverse_iterator<iterator> reverse_iterator;
132 // _Rep: string representation
134 // 1. String really contains _M_length + 1 characters: due to 21.3.4
135 // must be kept null-terminated.
136 // 2. _M_capacity >= _M_length
137 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
138 // 3. _M_refcount has three states:
139 // -1: leaked, one reference, no ref-copies allowed, non-const.
140 // 0: one reference, non-const.
141 // n>0: n + 1 references, operations require a lock, const.
142 // 4. All fields==0 is an empty string, given the extra storage
143 // beyond-the-end for a null terminator; thus, the shared
144 // empty string representation needs no constructor.
149 size_type _M_capacity;
150 _Atomic_word _M_refcount;
153 struct _Rep : _Rep_base
156 typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc;
158 // (Public) Data members:
160 // The maximum number of individual char_type elements of an
161 // individual string is determined by _S_max_size. This is the
162 // value that will be returned by max_size(). (Whereas npos
163 // is the maximum number of bytes the allocator can allocate.)
164 // If one was to divvy up the theoretical largest size string,
165 // with a terminating character and m _CharT elements, it'd
167 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
169 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
170 // In addition, this implementation quarters this amount.
171 static const size_type _S_max_size;
172 static const _CharT _S_terminal;
174 // The following storage is init'd to 0 by the linker, resulting
175 // (carefully) in an empty string with one reference.
176 static size_type _S_empty_rep_storage[];
181 // NB: Mild hack to avoid strict-aliasing warnings. Note that
182 // _S_empty_rep_storage is never modified and the punning should
183 // be reasonably safe in this case.
184 void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage);
185 return *reinterpret_cast<_Rep*>(__p);
190 { return this->_M_refcount < 0; }
194 { return this->_M_refcount > 0; }
198 { this->_M_refcount = -1; }
202 { this->_M_refcount = 0; }
205 _M_set_length_and_sharable(size_type __n)
207 this->_M_set_sharable(); // One reference.
208 this->_M_length = __n;
209 traits_type::assign(this->_M_refdata()[__n], _S_terminal);
210 // grrr. (per 21.3.4)
211 // You cannot leave those LWG people alone for a second.
216 { return reinterpret_cast<_CharT*>(this + 1); }
219 _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
221 return (!_M_is_leaked() && __alloc1 == __alloc2)
222 ? _M_refcopy() : _M_clone(__alloc1);
227 _S_create(size_type, size_type, const _Alloc&);
230 _M_dispose(const _Alloc& __a)
232 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
233 if (__builtin_expect(this != &_S_empty_rep(), false))
235 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
241 _M_destroy(const _Alloc&) throw();
246 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
247 if (__builtin_expect(this != &_S_empty_rep(), false))
249 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
254 _M_clone(const _Alloc&, size_type __res = 0);
257 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
258 struct _Alloc_hider : _Alloc
260 _Alloc_hider(_CharT* __dat, const _Alloc& __a)
261 : _Alloc(__a), _M_p(__dat) { }
263 _CharT* _M_p; // The actual data.
267 // Data Members (public):
268 // NB: This is an unsigned type, and thus represents the maximum
269 // size that the allocator can hold.
270 /// Value returned by various member functions when they fail.
271 static const size_type npos = static_cast<size_type>(-1);
274 // Data Members (private):
275 mutable _Alloc_hider _M_dataplus;
279 { return _M_dataplus._M_p; }
283 { return (_M_dataplus._M_p = __p); }
287 { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
289 // For the internal use we have functions similar to `begin'/`end'
290 // but they do not call _M_leak.
293 { return iterator(_M_data()); }
297 { return iterator(_M_data() + this->size()); }
300 _M_leak() // for use in begin() & non-const op[]
302 if (!_M_rep()->_M_is_leaked())
307 _M_check(size_type __pos, const char* __s) const
309 if (__pos > this->size())
310 __throw_out_of_range(__N(__s));
315 _M_check_length(size_type __n1, size_type __n2, const char* __s) const
317 if (this->max_size() - (this->size() - __n1) < __n2)
318 __throw_length_error(__N(__s));
321 // NB: _M_limit doesn't check for a bad __pos value.
323 _M_limit(size_type __pos, size_type __off) const
325 const bool __testoff = __off < this->size() - __pos;
326 return __testoff ? __off : this->size() - __pos;
329 // True if _Rep and source do not overlap.
331 _M_disjunct(const _CharT* __s) const
333 return (less<const _CharT*>()(__s, _M_data())
334 || less<const _CharT*>()(_M_data() + this->size(), __s));
337 // When __n = 1 way faster than the general multichar
338 // traits_type::copy/move/assign.
340 _M_copy(_CharT* __d, const _CharT* __s, size_type __n)
343 traits_type::assign(*__d, *__s);
345 traits_type::copy(__d, __s, __n);
349 _M_move(_CharT* __d, const _CharT* __s, size_type __n)
352 traits_type::assign(*__d, *__s);
354 traits_type::move(__d, __s, __n);
358 _M_assign(_CharT* __d, size_type __n, _CharT __c)
361 traits_type::assign(*__d, __c);
363 traits_type::assign(__d, __n, __c);
366 // _S_copy_chars is a separate template to permit specialization
367 // to optimize for the common case of pointers as iterators.
368 template<class _Iterator>
370 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
372 for (; __k1 != __k2; ++__k1, ++__p)
373 traits_type::assign(*__p, *__k1); // These types are off.
377 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2)
378 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
381 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
382 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
385 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
386 { _M_copy(__p, __k1, __k2 - __k1); }
389 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
390 { _M_copy(__p, __k1, __k2 - __k1); }
393 _S_compare(size_type __n1, size_type __n2)
395 const difference_type __d = difference_type(__n1 - __n2);
397 if (__d > __gnu_cxx::__numeric_traits<int>::__max)
398 return __gnu_cxx::__numeric_traits<int>::__max;
399 else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
400 return __gnu_cxx::__numeric_traits<int>::__min;
406 _M_mutate(size_type __pos, size_type __len1, size_type __len2);
413 { return _Rep::_S_empty_rep(); }
416 // Construct/copy/destroy:
417 // NB: We overload ctors in some cases instead of using default
418 // arguments, per 17.4.4.4 para. 2 item 2.
421 * @brief Default constructor creates an empty string.
427 * @brief Construct an empty string using allocator @a a.
430 basic_string(const _Alloc& __a);
432 // NB: per LWG issue 42, semantics different from IS:
434 * @brief Construct string with copy of value of @a str.
435 * @param str Source string.
437 basic_string(const basic_string& __str);
439 * @brief Construct string as copy of a substring.
440 * @param str Source string.
441 * @param pos Index of first character to copy from.
442 * @param n Number of characters to copy (default remainder).
444 basic_string(const basic_string& __str, size_type __pos,
445 size_type __n = npos);
447 * @brief Construct string as copy of a substring.
448 * @param str Source string.
449 * @param pos Index of first character to copy from.
450 * @param n Number of characters to copy.
451 * @param a Allocator to use.
453 basic_string(const basic_string& __str, size_type __pos,
454 size_type __n, const _Alloc& __a);
457 * @brief Construct string initialized by a character array.
458 * @param s Source character array.
459 * @param n Number of characters to copy.
460 * @param a Allocator to use (default is default allocator).
462 * NB: @a s must have at least @a n characters, '\0' has no special
465 basic_string(const _CharT* __s, size_type __n,
466 const _Alloc& __a = _Alloc());
468 * @brief Construct string as copy of a C string.
469 * @param s Source C string.
470 * @param a Allocator to use (default is default allocator).
472 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
474 * @brief Construct string as multiple characters.
475 * @param n Number of characters.
476 * @param c Character to use.
477 * @param a Allocator to use (default is default allocator).
479 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
481 #ifdef __GXX_EXPERIMENTAL_CXX0X__
483 * @brief Construct string from an initializer list.
484 * @param l std::initializer_list of characters.
485 * @param a Allocator to use (default is default allocator).
487 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc());
488 #endif // __GXX_EXPERIMENTAL_CXX0X__
491 * @brief Construct string as copy of a range.
492 * @param beg Start of range.
493 * @param end End of range.
494 * @param a Allocator to use (default is default allocator).
496 template<class _InputIterator>
497 basic_string(_InputIterator __beg, _InputIterator __end,
498 const _Alloc& __a = _Alloc());
501 * @brief Destroy the string instance.
504 { _M_rep()->_M_dispose(this->get_allocator()); }
507 * @brief Assign the value of @a str to this string.
508 * @param str Source string.
511 operator=(const basic_string& __str)
512 { return this->assign(__str); }
515 * @brief Copy contents of @a s into this string.
516 * @param s Source null-terminated string.
519 operator=(const _CharT* __s)
520 { return this->assign(__s); }
523 * @brief Set value to string of length 1.
524 * @param c Source character.
526 * Assigning to a character makes this string length 1 and
527 * (*this)[0] == @a c.
530 operator=(_CharT __c)
532 this->assign(1, __c);
536 #ifdef __GXX_EXPERIMENTAL_CXX0X__
538 * @brief Set value to string constructed from initializer list.
539 * @param l std::initializer_list.
542 operator=(initializer_list<_CharT> __l)
544 this->assign (__l.begin(), __l.end());
547 #endif // __GXX_EXPERIMENTAL_CXX0X__
551 * Returns a read/write iterator that points to the first character in
552 * the %string. Unshares the string.
558 return iterator(_M_data());
562 * Returns a read-only (constant) iterator that points to the first
563 * character in the %string.
567 { return const_iterator(_M_data()); }
570 * Returns a read/write iterator that points one past the last
571 * character in the %string. Unshares the string.
577 return iterator(_M_data() + this->size());
581 * Returns a read-only (constant) iterator that points one past the
582 * last character in the %string.
586 { return const_iterator(_M_data() + this->size()); }
589 * Returns a read/write reverse iterator that points to the last
590 * character in the %string. Iteration is done in reverse element
591 * order. Unshares the string.
595 { return reverse_iterator(this->end()); }
598 * Returns a read-only (constant) reverse iterator that points
599 * to the last character in the %string. Iteration is done in
600 * reverse element order.
602 const_reverse_iterator
604 { return const_reverse_iterator(this->end()); }
607 * Returns a read/write reverse iterator that points to one before the
608 * first character in the %string. Iteration is done in reverse
609 * element order. Unshares the string.
613 { return reverse_iterator(this->begin()); }
616 * Returns a read-only (constant) reverse iterator that points
617 * to one before the first character in the %string. Iteration
618 * is done in reverse element order.
620 const_reverse_iterator
622 { return const_reverse_iterator(this->begin()); }
626 /// Returns the number of characters in the string, not including any
627 /// null-termination.
630 { return _M_rep()->_M_length; }
632 /// Returns the number of characters in the string, not including any
633 /// null-termination.
636 { return _M_rep()->_M_length; }
638 /// Returns the size() of the largest possible %string.
641 { return _Rep::_S_max_size; }
644 * @brief Resizes the %string to the specified number of characters.
645 * @param n Number of characters the %string should contain.
646 * @param c Character to fill any new elements.
648 * This function will %resize the %string to the specified
649 * number of characters. If the number is smaller than the
650 * %string's current size the %string is truncated, otherwise
651 * the %string is extended and new elements are set to @a c.
654 resize(size_type __n, _CharT __c);
657 * @brief Resizes the %string to the specified number of characters.
658 * @param n Number of characters the %string should contain.
660 * This function will resize the %string to the specified length. If
661 * the new size is smaller than the %string's current size the %string
662 * is truncated, otherwise the %string is extended and new characters
663 * are default-constructed. For basic types such as char, this means
667 resize(size_type __n)
668 { this->resize(__n, _CharT()); }
671 * Returns the total number of characters that the %string can hold
672 * before needing to allocate more memory.
676 { return _M_rep()->_M_capacity; }
679 * @brief Attempt to preallocate enough memory for specified number of
681 * @param res_arg Number of characters required.
682 * @throw std::length_error If @a res_arg exceeds @c max_size().
684 * This function attempts to reserve enough memory for the
685 * %string to hold the specified number of characters. If the
686 * number requested is more than max_size(), length_error is
689 * The advantage of this function is that if optimal code is a
690 * necessity and the user can determine the string length that will be
691 * required, the user can reserve the memory in %advance, and thus
692 * prevent a possible reallocation of memory and copying of %string
696 reserve(size_type __res_arg = 0);
699 * Erases the string, making it empty.
703 { _M_mutate(0, this->size(), 0); }
706 * Returns true if the %string is empty. Equivalent to *this == "".
710 { return this->size() == 0; }
714 * @brief Subscript access to the data contained in the %string.
715 * @param pos The index of the character to access.
716 * @return Read-only (constant) reference to the character.
718 * This operator allows for easy, array-style, data access.
719 * Note that data access with this operator is unchecked and
720 * out_of_range lookups are not defined. (For checked lookups
724 operator[] (size_type __pos) const
726 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
727 return _M_data()[__pos];
731 * @brief Subscript access to the data contained in the %string.
732 * @param pos The index of the character to access.
733 * @return Read/write reference to the character.
735 * This operator allows for easy, array-style, data access.
736 * Note that data access with this operator is unchecked and
737 * out_of_range lookups are not defined. (For checked lookups
738 * see at().) Unshares the string.
741 operator[](size_type __pos)
743 // allow pos == size() as v3 extension:
744 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
745 // but be strict in pedantic mode:
746 _GLIBCXX_DEBUG_PEDASSERT(__pos < size());
748 return _M_data()[__pos];
752 * @brief Provides access to the data contained in the %string.
753 * @param n The index of the character to access.
754 * @return Read-only (const) reference to the character.
755 * @throw std::out_of_range If @a n is an invalid index.
757 * This function provides for safer data access. The parameter is
758 * first checked that it is in the range of the string. The function
759 * throws out_of_range if the check fails.
762 at(size_type __n) const
764 if (__n >= this->size())
765 __throw_out_of_range(__N("basic_string::at"));
766 return _M_data()[__n];
770 * @brief Provides access to the data contained in the %string.
771 * @param n The index of the character to access.
772 * @return Read/write reference to the character.
773 * @throw std::out_of_range If @a n is an invalid index.
775 * This function provides for safer data access. The parameter is
776 * first checked that it is in the range of the string. The function
777 * throws out_of_range if the check fails. Success results in
778 * unsharing the string.
784 __throw_out_of_range(__N("basic_string::at"));
786 return _M_data()[__n];
791 * @brief Append a string to this string.
792 * @param str The string to append.
793 * @return Reference to this string.
796 operator+=(const basic_string& __str)
797 { return this->append(__str); }
800 * @brief Append a C string.
801 * @param s The C string to append.
802 * @return Reference to this string.
805 operator+=(const _CharT* __s)
806 { return this->append(__s); }
809 * @brief Append a character.
810 * @param c The character to append.
811 * @return Reference to this string.
814 operator+=(_CharT __c)
816 this->push_back(__c);
820 #ifdef __GXX_EXPERIMENTAL_CXX0X__
822 * @brief Append an initializer_list of characters.
823 * @param l The initializer_list of characters to be appended.
824 * @return Reference to this string.
827 operator+=(initializer_list<_CharT> __l)
828 { return this->append(__l.begin(), __l.end()); }
829 #endif // __GXX_EXPERIMENTAL_CXX0X__
832 * @brief Append a string to this string.
833 * @param str The string to append.
834 * @return Reference to this string.
837 append(const basic_string& __str);
840 * @brief Append a substring.
841 * @param str The string to append.
842 * @param pos Index of the first character of str to append.
843 * @param n The number of characters to append.
844 * @return Reference to this string.
845 * @throw std::out_of_range if @a pos is not a valid index.
847 * This function appends @a n characters from @a str starting at @a pos
848 * to this string. If @a n is is larger than the number of available
849 * characters in @a str, the remainder of @a str is appended.
852 append(const basic_string& __str, size_type __pos, size_type __n);
855 * @brief Append a C substring.
856 * @param s The C string to append.
857 * @param n The number of characters to append.
858 * @return Reference to this string.
861 append(const _CharT* __s, size_type __n);
864 * @brief Append a C string.
865 * @param s The C string to append.
866 * @return Reference to this string.
869 append(const _CharT* __s)
871 __glibcxx_requires_string(__s);
872 return this->append(__s, traits_type::length(__s));
876 * @brief Append multiple characters.
877 * @param n The number of characters to append.
878 * @param c The character to use.
879 * @return Reference to this string.
881 * Appends n copies of c to this string.
884 append(size_type __n, _CharT __c);
886 #ifdef __GXX_EXPERIMENTAL_CXX0X__
888 * @brief Append an initializer_list of characters.
889 * @param l The initializer_list of characters to append.
890 * @return Reference to this string.
893 append(initializer_list<_CharT> __l)
894 { return this->append(__l.begin(), __l.end()); }
895 #endif // __GXX_EXPERIMENTAL_CXX0X__
898 * @brief Append a range of characters.
899 * @param first Iterator referencing the first character to append.
900 * @param last Iterator marking the end of the range.
901 * @return Reference to this string.
903 * Appends characters in the range [first,last) to this string.
905 template<class _InputIterator>
907 append(_InputIterator __first, _InputIterator __last)
908 { return this->replace(_M_iend(), _M_iend(), __first, __last); }
911 * @brief Append a single character.
912 * @param c Character to append.
915 push_back(_CharT __c)
917 const size_type __len = 1 + this->size();
918 if (__len > this->capacity() || _M_rep()->_M_is_shared())
919 this->reserve(__len);
920 traits_type::assign(_M_data()[this->size()], __c);
921 _M_rep()->_M_set_length_and_sharable(__len);
925 * @brief Set value to contents of another string.
926 * @param str Source string to use.
927 * @return Reference to this string.
930 assign(const basic_string& __str);
933 * @brief Set value to a substring of a string.
934 * @param str The string to use.
935 * @param pos Index of the first character of str.
936 * @param n Number of characters to use.
937 * @return Reference to this string.
938 * @throw std::out_of_range if @a pos is not a valid index.
940 * This function sets this string to the substring of @a str consisting
941 * of @a n characters at @a pos. If @a n is is larger than the number
942 * of available characters in @a str, the remainder of @a str is used.
945 assign(const basic_string& __str, size_type __pos, size_type __n)
946 { return this->assign(__str._M_data()
947 + __str._M_check(__pos, "basic_string::assign"),
948 __str._M_limit(__pos, __n)); }
951 * @brief Set value to a C substring.
952 * @param s The C string to use.
953 * @param n Number of characters to use.
954 * @return Reference to this string.
956 * This function sets the value of this string to the first @a n
957 * characters of @a s. If @a n is is larger than the number of
958 * available characters in @a s, the remainder of @a s is used.
961 assign(const _CharT* __s, size_type __n);
964 * @brief Set value to contents of a C string.
965 * @param s The C string to use.
966 * @return Reference to this string.
968 * This function sets the value of this string to the value of @a s.
969 * The data is copied, so there is no dependence on @a s once the
973 assign(const _CharT* __s)
975 __glibcxx_requires_string(__s);
976 return this->assign(__s, traits_type::length(__s));
980 * @brief Set value to multiple characters.
981 * @param n Length of the resulting string.
982 * @param c The character to use.
983 * @return Reference to this string.
985 * This function sets the value of this string to @a n copies of
989 assign(size_type __n, _CharT __c)
990 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
993 * @brief Set value to a range of characters.
994 * @param first Iterator referencing the first character to append.
995 * @param last Iterator marking the end of the range.
996 * @return Reference to this string.
998 * Sets value of string to characters in the range [first,last).
1000 template<class _InputIterator>
1002 assign(_InputIterator __first, _InputIterator __last)
1003 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
1005 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1007 * @brief Set value to an initializer_list of characters.
1008 * @param l The initializer_list of characters to assign.
1009 * @return Reference to this string.
1012 assign(initializer_list<_CharT> __l)
1013 { return this->assign(__l.begin(), __l.end()); }
1014 #endif // __GXX_EXPERIMENTAL_CXX0X__
1017 * @brief Insert multiple characters.
1018 * @param p Iterator referencing location in string to insert at.
1019 * @param n Number of characters to insert
1020 * @param c The character to insert.
1021 * @throw std::length_error If new length exceeds @c max_size().
1023 * Inserts @a n copies of character @a c starting at the position
1024 * referenced by iterator @a p. If adding characters causes the length
1025 * to exceed max_size(), length_error is thrown. The value of the
1026 * string doesn't change if an error is thrown.
1029 insert(iterator __p, size_type __n, _CharT __c)
1030 { this->replace(__p, __p, __n, __c); }
1033 * @brief Insert a range of characters.
1034 * @param p Iterator referencing location in string to insert at.
1035 * @param beg Start of range.
1036 * @param end End of range.
1037 * @throw std::length_error If new length exceeds @c max_size().
1039 * Inserts characters in range [beg,end). If adding characters causes
1040 * the length to exceed max_size(), length_error is thrown. The value
1041 * of the string doesn't change if an error is thrown.
1043 template<class _InputIterator>
1045 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
1046 { this->replace(__p, __p, __beg, __end); }
1048 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1050 * @brief Insert an initializer_list of characters.
1051 * @param p Iterator referencing location in string to insert at.
1052 * @param l The initializer_list of characters to insert.
1053 * @throw std::length_error If new length exceeds @c max_size().
1056 insert(iterator __p, initializer_list<_CharT> __l)
1057 { this->insert(__p, __l.begin(), __l.end()); }
1058 #endif // __GXX_EXPERIMENTAL_CXX0X__
1061 * @brief Insert value of a string.
1062 * @param pos1 Iterator referencing location in string to insert at.
1063 * @param str The string to insert.
1064 * @return Reference to this string.
1065 * @throw std::length_error If new length exceeds @c max_size().
1067 * Inserts value of @a str starting at @a pos1. If adding characters
1068 * causes the length to exceed max_size(), length_error is thrown. The
1069 * value of the string doesn't change if an error is thrown.
1072 insert(size_type __pos1, const basic_string& __str)
1073 { return this->insert(__pos1, __str, size_type(0), __str.size()); }
1076 * @brief Insert a substring.
1077 * @param pos1 Iterator referencing location in string to insert at.
1078 * @param str The string to insert.
1079 * @param pos2 Start of characters in str to insert.
1080 * @param n Number of characters to insert.
1081 * @return Reference to this string.
1082 * @throw std::length_error If new length exceeds @c max_size().
1083 * @throw std::out_of_range If @a pos1 > size() or
1084 * @a pos2 > @a str.size().
1086 * Starting at @a pos1, insert @a n character of @a str beginning with
1087 * @a pos2. If adding characters causes the length to exceed
1088 * max_size(), length_error is thrown. If @a pos1 is beyond the end of
1089 * this string or @a pos2 is beyond the end of @a str, out_of_range is
1090 * thrown. The value of the string doesn't change if an error is
1094 insert(size_type __pos1, const basic_string& __str,
1095 size_type __pos2, size_type __n)
1096 { return this->insert(__pos1, __str._M_data()
1097 + __str._M_check(__pos2, "basic_string::insert"),
1098 __str._M_limit(__pos2, __n)); }
1101 * @brief Insert a C substring.
1102 * @param pos Iterator referencing location in string to insert at.
1103 * @param s The C string to insert.
1104 * @param n The number of characters to insert.
1105 * @return Reference to this string.
1106 * @throw std::length_error If new length exceeds @c max_size().
1107 * @throw std::out_of_range If @a pos is beyond the end of this
1110 * Inserts the first @a n characters of @a s starting at @a pos. If
1111 * adding characters causes the length to exceed max_size(),
1112 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1113 * thrown. The value of the string doesn't change if an error is
1117 insert(size_type __pos, const _CharT* __s, size_type __n);
1120 * @brief Insert a C string.
1121 * @param pos Iterator referencing location in string to insert at.
1122 * @param s The C string to insert.
1123 * @return Reference to this string.
1124 * @throw std::length_error If new length exceeds @c max_size().
1125 * @throw std::out_of_range If @a pos is beyond the end of this
1128 * Inserts the first @a n characters of @a s starting at @a pos. If
1129 * adding characters causes the length to exceed max_size(),
1130 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1131 * thrown. The value of the string doesn't change if an error is
1135 insert(size_type __pos, const _CharT* __s)
1137 __glibcxx_requires_string(__s);
1138 return this->insert(__pos, __s, traits_type::length(__s));
1142 * @brief Insert multiple characters.
1143 * @param pos Index in string to insert at.
1144 * @param n Number of characters to insert
1145 * @param c The character to insert.
1146 * @return Reference to this string.
1147 * @throw std::length_error If new length exceeds @c max_size().
1148 * @throw std::out_of_range If @a pos is beyond the end of this
1151 * Inserts @a n copies of character @a c starting at index @a pos. If
1152 * adding characters causes the length to exceed max_size(),
1153 * length_error is thrown. If @a pos > length(), out_of_range is
1154 * thrown. The value of the string doesn't change if an error is
1158 insert(size_type __pos, size_type __n, _CharT __c)
1159 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
1160 size_type(0), __n, __c); }
1163 * @brief Insert one character.
1164 * @param p Iterator referencing position in string to insert at.
1165 * @param c The character to insert.
1166 * @return Iterator referencing newly inserted char.
1167 * @throw std::length_error If new length exceeds @c max_size().
1169 * Inserts character @a c at position referenced by @a p. If adding
1170 * character causes the length to exceed max_size(), length_error is
1171 * thrown. If @a p is beyond end of string, out_of_range is thrown.
1172 * The value of the string doesn't change if an error is thrown.
1175 insert(iterator __p, _CharT __c)
1177 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1178 const size_type __pos = __p - _M_ibegin();
1179 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1180 _M_rep()->_M_set_leaked();
1181 return iterator(_M_data() + __pos);
1185 * @brief Remove characters.
1186 * @param pos Index of first character to remove (default 0).
1187 * @param n Number of characters to remove (default remainder).
1188 * @return Reference to this string.
1189 * @throw std::out_of_range If @a pos is beyond the end of this
1192 * Removes @a n characters from this string starting at @a pos. The
1193 * length of the string is reduced by @a n. If there are < @a n
1194 * characters to remove, the remainder of the string is truncated. If
1195 * @a p is beyond end of string, out_of_range is thrown. The value of
1196 * the string doesn't change if an error is thrown.
1199 erase(size_type __pos = 0, size_type __n = npos)
1201 _M_mutate(_M_check(__pos, "basic_string::erase"),
1202 _M_limit(__pos, __n), size_type(0));
1207 * @brief Remove one character.
1208 * @param position Iterator referencing the character to remove.
1209 * @return iterator referencing same location after removal.
1211 * Removes the character at @a position from this string. The value
1212 * of the string doesn't change if an error is thrown.
1215 erase(iterator __position)
1217 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
1218 && __position < _M_iend());
1219 const size_type __pos = __position - _M_ibegin();
1220 _M_mutate(__pos, size_type(1), size_type(0));
1221 _M_rep()->_M_set_leaked();
1222 return iterator(_M_data() + __pos);
1226 * @brief Remove a range of characters.
1227 * @param first Iterator referencing the first character to remove.
1228 * @param last Iterator referencing the end of the range.
1229 * @return Iterator referencing location of first after removal.
1231 * Removes the characters in the range [first,last) from this string.
1232 * The value of the string doesn't change if an error is thrown.
1235 erase(iterator __first, iterator __last)
1237 _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
1238 && __last <= _M_iend());
1239 const size_type __pos = __first - _M_ibegin();
1240 _M_mutate(__pos, __last - __first, size_type(0));
1241 _M_rep()->_M_set_leaked();
1242 return iterator(_M_data() + __pos);
1246 * @brief Replace characters with value from another string.
1247 * @param pos Index of first character to replace.
1248 * @param n Number of characters to be replaced.
1249 * @param str String to insert.
1250 * @return Reference to this string.
1251 * @throw std::out_of_range If @a pos is beyond the end of this
1253 * @throw std::length_error If new length exceeds @c max_size().
1255 * Removes the characters in the range [pos,pos+n) from this string.
1256 * In place, the value of @a str is inserted. If @a pos is beyond end
1257 * of string, out_of_range is thrown. If the length of the result
1258 * exceeds max_size(), length_error is thrown. The value of the string
1259 * doesn't change if an error is thrown.
1262 replace(size_type __pos, size_type __n, const basic_string& __str)
1263 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1266 * @brief Replace characters with value from another string.
1267 * @param pos1 Index of first character to replace.
1268 * @param n1 Number of characters to be replaced.
1269 * @param str String to insert.
1270 * @param pos2 Index of first character of str to use.
1271 * @param n2 Number of characters from str to use.
1272 * @return Reference to this string.
1273 * @throw std::out_of_range If @a pos1 > size() or @a pos2 >
1275 * @throw std::length_error If new length exceeds @c max_size().
1277 * Removes the characters in the range [pos1,pos1 + n) from this
1278 * string. In place, the value of @a str is inserted. If @a pos is
1279 * beyond end of string, out_of_range is thrown. If the length of the
1280 * result exceeds max_size(), length_error is thrown. The value of the
1281 * string doesn't change if an error is thrown.
1284 replace(size_type __pos1, size_type __n1, const basic_string& __str,
1285 size_type __pos2, size_type __n2)
1286 { return this->replace(__pos1, __n1, __str._M_data()
1287 + __str._M_check(__pos2, "basic_string::replace"),
1288 __str._M_limit(__pos2, __n2)); }
1291 * @brief Replace characters with value of a C substring.
1292 * @param pos Index of first character to replace.
1293 * @param n1 Number of characters to be replaced.
1294 * @param s C string to insert.
1295 * @param n2 Number of characters from @a s to use.
1296 * @return Reference to this string.
1297 * @throw std::out_of_range If @a pos1 > size().
1298 * @throw std::length_error If new length exceeds @c max_size().
1300 * Removes the characters in the range [pos,pos + n1) from this string.
1301 * In place, the first @a n2 characters of @a s are inserted, or all
1302 * of @a s if @a n2 is too large. If @a pos is beyond end of string,
1303 * out_of_range is thrown. If the length of result exceeds max_size(),
1304 * length_error is thrown. The value of the string doesn't change if
1305 * an error is thrown.
1308 replace(size_type __pos, size_type __n1, const _CharT* __s,
1312 * @brief Replace characters with value of a C string.
1313 * @param pos Index of first character to replace.
1314 * @param n1 Number of characters to be replaced.
1315 * @param s C string to insert.
1316 * @return Reference to this string.
1317 * @throw std::out_of_range If @a pos > size().
1318 * @throw std::length_error If new length exceeds @c max_size().
1320 * Removes the characters in the range [pos,pos + n1) from this string.
1321 * In place, the first @a n characters of @a s are inserted. If @a
1322 * pos is beyond end of string, out_of_range is thrown. If the length
1323 * of result exceeds max_size(), length_error is thrown. The value of
1324 * the string doesn't change if an error is thrown.
1327 replace(size_type __pos, size_type __n1, const _CharT* __s)
1329 __glibcxx_requires_string(__s);
1330 return this->replace(__pos, __n1, __s, traits_type::length(__s));
1334 * @brief Replace characters with multiple characters.
1335 * @param pos Index of first character to replace.
1336 * @param n1 Number of characters to be replaced.
1337 * @param n2 Number of characters to insert.
1338 * @param c Character to insert.
1339 * @return Reference to this string.
1340 * @throw std::out_of_range If @a pos > size().
1341 * @throw std::length_error If new length exceeds @c max_size().
1343 * Removes the characters in the range [pos,pos + n1) from this string.
1344 * In place, @a n2 copies of @a c are inserted. If @a pos is beyond
1345 * end of string, out_of_range is thrown. If the length of result
1346 * exceeds max_size(), length_error is thrown. The value of the string
1347 * doesn't change if an error is thrown.
1350 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1351 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
1352 _M_limit(__pos, __n1), __n2, __c); }
1355 * @brief Replace range of characters with string.
1356 * @param i1 Iterator referencing start of range to replace.
1357 * @param i2 Iterator referencing end of range to replace.
1358 * @param str String value to insert.
1359 * @return Reference to this string.
1360 * @throw std::length_error If new length exceeds @c max_size().
1362 * Removes the characters in the range [i1,i2). In place, the value of
1363 * @a str is inserted. If the length of result exceeds max_size(),
1364 * length_error is thrown. The value of the string doesn't change if
1365 * an error is thrown.
1368 replace(iterator __i1, iterator __i2, const basic_string& __str)
1369 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
1372 * @brief Replace range of characters with C substring.
1373 * @param i1 Iterator referencing start of range to replace.
1374 * @param i2 Iterator referencing end of range to replace.
1375 * @param s C string value to insert.
1376 * @param n Number of characters from s to insert.
1377 * @return Reference to this string.
1378 * @throw std::length_error If new length exceeds @c max_size().
1380 * Removes the characters in the range [i1,i2). In place, the first @a
1381 * n characters of @a s are inserted. If the length of result exceeds
1382 * max_size(), length_error is thrown. The value of the string doesn't
1383 * change if an error is thrown.
1386 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
1388 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1389 && __i2 <= _M_iend());
1390 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
1394 * @brief Replace range of characters with C string.
1395 * @param i1 Iterator referencing start of range to replace.
1396 * @param i2 Iterator referencing end of range to replace.
1397 * @param s C string value to insert.
1398 * @return Reference to this string.
1399 * @throw std::length_error If new length exceeds @c max_size().
1401 * Removes the characters in the range [i1,i2). In place, the
1402 * characters of @a s are inserted. If the length of result exceeds
1403 * max_size(), length_error is thrown. The value of the string doesn't
1404 * change if an error is thrown.
1407 replace(iterator __i1, iterator __i2, const _CharT* __s)
1409 __glibcxx_requires_string(__s);
1410 return this->replace(__i1, __i2, __s, traits_type::length(__s));
1414 * @brief Replace range of characters with multiple characters
1415 * @param i1 Iterator referencing start of range to replace.
1416 * @param i2 Iterator referencing end of range to replace.
1417 * @param n Number of characters to insert.
1418 * @param c Character to insert.
1419 * @return Reference to this string.
1420 * @throw std::length_error If new length exceeds @c max_size().
1422 * Removes the characters in the range [i1,i2). In place, @a n copies
1423 * of @a c are inserted. If the length of result exceeds max_size(),
1424 * length_error is thrown. The value of the string doesn't change if
1425 * an error is thrown.
1428 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
1430 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1431 && __i2 <= _M_iend());
1432 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
1436 * @brief Replace range of characters with range.
1437 * @param i1 Iterator referencing start of range to replace.
1438 * @param i2 Iterator referencing end of range to replace.
1439 * @param k1 Iterator referencing start of range to insert.
1440 * @param k2 Iterator referencing end of range to insert.
1441 * @return Reference to this string.
1442 * @throw std::length_error If new length exceeds @c max_size().
1444 * Removes the characters in the range [i1,i2). In place, characters
1445 * in the range [k1,k2) are inserted. If the length of result exceeds
1446 * max_size(), length_error is thrown. The value of the string doesn't
1447 * change if an error is thrown.
1449 template<class _InputIterator>
1451 replace(iterator __i1, iterator __i2,
1452 _InputIterator __k1, _InputIterator __k2)
1454 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1455 && __i2 <= _M_iend());
1456 __glibcxx_requires_valid_range(__k1, __k2);
1457 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1458 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
1461 // Specializations for the common case of pointer and iterator:
1462 // useful to avoid the overhead of temporary buffering in _M_replace.
1464 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
1466 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1467 && __i2 <= _M_iend());
1468 __glibcxx_requires_valid_range(__k1, __k2);
1469 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1474 replace(iterator __i1, iterator __i2,
1475 const _CharT* __k1, const _CharT* __k2)
1477 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1478 && __i2 <= _M_iend());
1479 __glibcxx_requires_valid_range(__k1, __k2);
1480 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1485 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
1487 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1488 && __i2 <= _M_iend());
1489 __glibcxx_requires_valid_range(__k1, __k2);
1490 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1491 __k1.base(), __k2 - __k1);
1495 replace(iterator __i1, iterator __i2,
1496 const_iterator __k1, const_iterator __k2)
1498 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1499 && __i2 <= _M_iend());
1500 __glibcxx_requires_valid_range(__k1, __k2);
1501 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1502 __k1.base(), __k2 - __k1);
1505 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1507 * @brief Replace range of characters with initializer_list.
1508 * @param i1 Iterator referencing start of range to replace.
1509 * @param i2 Iterator referencing end of range to replace.
1510 * @param l The initializer_list of characters to insert.
1511 * @return Reference to this string.
1512 * @throw std::length_error If new length exceeds @c max_size().
1514 * Removes the characters in the range [i1,i2). In place, characters
1515 * in the range [k1,k2) are inserted. If the length of result exceeds
1516 * max_size(), length_error is thrown. The value of the string doesn't
1517 * change if an error is thrown.
1519 basic_string& replace(iterator __i1, iterator __i2,
1520 initializer_list<_CharT> __l)
1521 { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
1522 #endif // __GXX_EXPERIMENTAL_CXX0X__
1525 template<class _Integer>
1527 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
1528 _Integer __val, __true_type)
1529 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
1531 template<class _InputIterator>
1533 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
1534 _InputIterator __k2, __false_type);
1537 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
1541 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
1544 // _S_construct_aux is used to implement the 21.3.1 para 15 which
1545 // requires special behaviour if _InIter is an integral type
1546 template<class _InIterator>
1548 _S_construct_aux(_InIterator __beg, _InIterator __end,
1549 const _Alloc& __a, __false_type)
1551 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
1552 return _S_construct(__beg, __end, __a, _Tag());
1555 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1556 // 438. Ambiguity in the "do the right thing" clause
1557 template<class _Integer>
1559 _S_construct_aux(_Integer __beg, _Integer __end,
1560 const _Alloc& __a, __true_type)
1561 { return _S_construct(static_cast<size_type>(__beg), __end, __a); }
1563 template<class _InIterator>
1565 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
1567 typedef typename std::__is_integer<_InIterator>::__type _Integral;
1568 return _S_construct_aux(__beg, __end, __a, _Integral());
1571 // For Input Iterators, used in istreambuf_iterators, etc.
1572 template<class _InIterator>
1574 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1575 input_iterator_tag);
1577 // For forward_iterators up to random_access_iterators, used for
1578 // string::iterator, _CharT*, etc.
1579 template<class _FwdIterator>
1581 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
1582 forward_iterator_tag);
1585 _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
1590 * @brief Copy substring into C string.
1591 * @param s C string to copy value into.
1592 * @param n Number of characters to copy.
1593 * @param pos Index of first character to copy.
1594 * @return Number of characters actually copied
1595 * @throw std::out_of_range If pos > size().
1597 * Copies up to @a n characters starting at @a pos into the C string @a
1598 * s. If @a pos is greater than size(), out_of_range is thrown.
1601 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
1604 * @brief Swap contents with another string.
1605 * @param s String to swap with.
1607 * Exchanges the contents of this string with that of @a s in constant
1611 swap(basic_string& __s);
1613 // String operations:
1615 * @brief Return const pointer to null-terminated contents.
1617 * This is a handle to internal data. Do not modify or dire things may
1622 { return _M_data(); }
1625 * @brief Return const pointer to contents.
1627 * This is a handle to internal data. Do not modify or dire things may
1632 { return _M_data(); }
1635 * @brief Return copy of allocator used to construct this string.
1638 get_allocator() const
1639 { return _M_dataplus; }
1642 * @brief Find position of a C substring.
1643 * @param s C string to locate.
1644 * @param pos Index of character to search from.
1645 * @param n Number of characters from @a s to search for.
1646 * @return Index of start of first occurrence.
1648 * Starting from @a pos, searches forward for the first @a n characters
1649 * in @a s within this string. If found, returns the index where it
1650 * begins. If not found, returns npos.
1653 find(const _CharT* __s, size_type __pos, size_type __n) const;
1656 * @brief Find position of a string.
1657 * @param str String to locate.
1658 * @param pos Index of character to search from (default 0).
1659 * @return Index of start of first occurrence.
1661 * Starting from @a pos, searches forward for value of @a str within
1662 * this string. If found, returns the index where it begins. If not
1663 * found, returns npos.
1666 find(const basic_string& __str, size_type __pos = 0) const
1667 { return this->find(__str.data(), __pos, __str.size()); }
1670 * @brief Find position of a C string.
1671 * @param s C string to locate.
1672 * @param pos Index of character to search from (default 0).
1673 * @return Index of start of first occurrence.
1675 * Starting from @a pos, searches forward for the value of @a s within
1676 * this string. If found, returns the index where it begins. If not
1677 * found, returns npos.
1680 find(const _CharT* __s, size_type __pos = 0) const
1682 __glibcxx_requires_string(__s);
1683 return this->find(__s, __pos, traits_type::length(__s));
1687 * @brief Find position of a character.
1688 * @param c Character to locate.
1689 * @param pos Index of character to search from (default 0).
1690 * @return Index of first occurrence.
1692 * Starting from @a pos, searches forward for @a c within this string.
1693 * If found, returns the index where it was found. If not found,
1697 find(_CharT __c, size_type __pos = 0) const;
1700 * @brief Find last position of a string.
1701 * @param str String to locate.
1702 * @param pos Index of character to search back from (default end).
1703 * @return Index of start of last occurrence.
1705 * Starting from @a pos, searches backward for value of @a str within
1706 * this string. If found, returns the index where it begins. If not
1707 * found, returns npos.
1710 rfind(const basic_string& __str, size_type __pos = npos) const
1711 { return this->rfind(__str.data(), __pos, __str.size()); }
1714 * @brief Find last position of a C substring.
1715 * @param s C string to locate.
1716 * @param pos Index of character to search back from.
1717 * @param n Number of characters from s to search for.
1718 * @return Index of start of last occurrence.
1720 * Starting from @a pos, searches backward for the first @a n
1721 * characters in @a s within this string. If found, returns the index
1722 * where it begins. If not found, returns npos.
1725 rfind(const _CharT* __s, size_type __pos, size_type __n) const;
1728 * @brief Find last position of a C string.
1729 * @param s C string to locate.
1730 * @param pos Index of character to start search at (default end).
1731 * @return Index of start of last occurrence.
1733 * Starting from @a pos, searches backward for the value of @a s within
1734 * this string. If found, returns the index where it begins. If not
1735 * found, returns npos.
1738 rfind(const _CharT* __s, size_type __pos = npos) const
1740 __glibcxx_requires_string(__s);
1741 return this->rfind(__s, __pos, traits_type::length(__s));
1745 * @brief Find last position of a character.
1746 * @param c Character to locate.
1747 * @param pos Index of character to search back from (default end).
1748 * @return Index of last occurrence.
1750 * Starting from @a pos, searches backward for @a c within this string.
1751 * If found, returns the index where it was found. If not found,
1755 rfind(_CharT __c, size_type __pos = npos) const;
1758 * @brief Find position of a character of string.
1759 * @param str String containing characters to locate.
1760 * @param pos Index of character to search from (default 0).
1761 * @return Index of first occurrence.
1763 * Starting from @a pos, searches forward for one of the characters of
1764 * @a str within this string. If found, returns the index where it was
1765 * found. If not found, returns npos.
1768 find_first_of(const basic_string& __str, size_type __pos = 0) const
1769 { return this->find_first_of(__str.data(), __pos, __str.size()); }
1772 * @brief Find position of a character of C substring.
1773 * @param s String containing characters to locate.
1774 * @param pos Index of character to search from.
1775 * @param n Number of characters from s to search for.
1776 * @return Index of first occurrence.
1778 * Starting from @a pos, searches forward for one of the first @a n
1779 * characters of @a s within this string. If found, returns the index
1780 * where it was found. If not found, returns npos.
1783 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
1786 * @brief Find position of a character of C string.
1787 * @param s String containing characters to locate.
1788 * @param pos Index of character to search from (default 0).
1789 * @return Index of first occurrence.
1791 * Starting from @a pos, searches forward for one of the characters of
1792 * @a s within this string. If found, returns the index where it was
1793 * found. If not found, returns npos.
1796 find_first_of(const _CharT* __s, size_type __pos = 0) const
1798 __glibcxx_requires_string(__s);
1799 return this->find_first_of(__s, __pos, traits_type::length(__s));
1803 * @brief Find position of a character.
1804 * @param c Character to locate.
1805 * @param pos Index of character to search from (default 0).
1806 * @return Index of first occurrence.
1808 * Starting from @a pos, searches forward for the character @a c within
1809 * this string. If found, returns the index where it was found. If
1810 * not found, returns npos.
1812 * Note: equivalent to find(c, pos).
1815 find_first_of(_CharT __c, size_type __pos = 0) const
1816 { return this->find(__c, __pos); }
1819 * @brief Find last position of a character of string.
1820 * @param str String containing characters to locate.
1821 * @param pos Index of character to search back from (default end).
1822 * @return Index of last occurrence.
1824 * Starting from @a pos, searches backward for one of the characters of
1825 * @a str within this string. If found, returns the index where it was
1826 * found. If not found, returns npos.
1829 find_last_of(const basic_string& __str, size_type __pos = npos) const
1830 { return this->find_last_of(__str.data(), __pos, __str.size()); }
1833 * @brief Find last position of a character of C substring.
1834 * @param s C string containing characters to locate.
1835 * @param pos Index of character to search back from.
1836 * @param n Number of characters from s to search for.
1837 * @return Index of last occurrence.
1839 * Starting from @a pos, searches backward for one of the first @a n
1840 * characters of @a s within this string. If found, returns the index
1841 * where it was found. If not found, returns npos.
1844 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
1847 * @brief Find last position of a character of C string.
1848 * @param s C string containing characters to locate.
1849 * @param pos Index of character to search back from (default end).
1850 * @return Index of last occurrence.
1852 * Starting from @a pos, searches backward for one of the characters of
1853 * @a s within this string. If found, returns the index where it was
1854 * found. If not found, returns npos.
1857 find_last_of(const _CharT* __s, size_type __pos = npos) const
1859 __glibcxx_requires_string(__s);
1860 return this->find_last_of(__s, __pos, traits_type::length(__s));
1864 * @brief Find last position of a character.
1865 * @param c Character to locate.
1866 * @param pos Index of character to search back from (default end).
1867 * @return Index of last occurrence.
1869 * Starting from @a pos, searches backward for @a c within this string.
1870 * If found, returns the index where it was found. If not found,
1873 * Note: equivalent to rfind(c, pos).
1876 find_last_of(_CharT __c, size_type __pos = npos) const
1877 { return this->rfind(__c, __pos); }
1880 * @brief Find position of a character not in string.
1881 * @param str String containing characters to avoid.
1882 * @param pos Index of character to search from (default 0).
1883 * @return Index of first occurrence.
1885 * Starting from @a pos, searches forward for a character not contained
1886 * in @a str within this string. If found, returns the index where it
1887 * was found. If not found, returns npos.
1890 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
1891 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
1894 * @brief Find position of a character not in C substring.
1895 * @param s C string containing characters to avoid.
1896 * @param pos Index of character to search from.
1897 * @param n Number of characters from s to consider.
1898 * @return Index of first occurrence.
1900 * Starting from @a pos, searches forward for a character not contained
1901 * in the first @a n characters of @a s within this string. If found,
1902 * returns the index where it was found. If not found, returns npos.
1905 find_first_not_of(const _CharT* __s, size_type __pos,
1906 size_type __n) const;
1909 * @brief Find position of a character not in C string.
1910 * @param s C string containing characters to avoid.
1911 * @param pos Index of character to search from (default 0).
1912 * @return Index of first occurrence.
1914 * Starting from @a pos, searches forward for a character not contained
1915 * in @a s within this string. If found, returns the index where it
1916 * was found. If not found, returns npos.
1919 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
1921 __glibcxx_requires_string(__s);
1922 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
1926 * @brief Find position of a different character.
1927 * @param c Character to avoid.
1928 * @param pos Index of character to search from (default 0).
1929 * @return Index of first occurrence.
1931 * Starting from @a pos, searches forward for a character other than @a c
1932 * within this string. If found, returns the index where it was found.
1933 * If not found, returns npos.
1936 find_first_not_of(_CharT __c, size_type __pos = 0) const;
1939 * @brief Find last position of a character not in string.
1940 * @param str String containing characters to avoid.
1941 * @param pos Index of character to search back from (default end).
1942 * @return Index of last occurrence.
1944 * Starting from @a pos, searches backward for a character not
1945 * contained in @a str within this string. If found, returns the index
1946 * where it was found. If not found, returns npos.
1949 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
1950 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
1953 * @brief Find last position of a character not in C substring.
1954 * @param s C string containing characters to avoid.
1955 * @param pos Index of character to search back from.
1956 * @param n Number of characters from s to consider.
1957 * @return Index of last occurrence.
1959 * Starting from @a pos, searches backward for a character not
1960 * contained in the first @a n characters of @a s within this string.
1961 * If found, returns the index where it was found. If not found,
1965 find_last_not_of(const _CharT* __s, size_type __pos,
1966 size_type __n) const;
1968 * @brief Find last position of a character not in C string.
1969 * @param s C string containing characters to avoid.
1970 * @param pos Index of character to search back from (default end).
1971 * @return Index of last occurrence.
1973 * Starting from @a pos, searches backward for a character not
1974 * contained in @a s within this string. If found, returns the index
1975 * where it was found. If not found, returns npos.
1978 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
1980 __glibcxx_requires_string(__s);
1981 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
1985 * @brief Find last position of a different character.
1986 * @param c Character to avoid.
1987 * @param pos Index of character to search back from (default end).
1988 * @return Index of last occurrence.
1990 * Starting from @a pos, searches backward for a character other than
1991 * @a c within this string. If found, returns the index where it was
1992 * found. If not found, returns npos.
1995 find_last_not_of(_CharT __c, size_type __pos = npos) const;
1998 * @brief Get a substring.
1999 * @param pos Index of first character (default 0).
2000 * @param n Number of characters in substring (default remainder).
2001 * @return The new string.
2002 * @throw std::out_of_range If pos > size().
2004 * Construct and return a new string using the @a n characters starting
2005 * at @a pos. If the string is too short, use the remainder of the
2006 * characters. If @a pos is beyond the end of the string, out_of_range
2010 substr(size_type __pos = 0, size_type __n = npos) const
2011 { return basic_string(*this,
2012 _M_check(__pos, "basic_string::substr"), __n); }
2015 * @brief Compare to a string.
2016 * @param str String to compare against.
2017 * @return Integer < 0, 0, or > 0.
2019 * Returns an integer < 0 if this string is ordered before @a str, 0 if
2020 * their values are equivalent, or > 0 if this string is ordered after
2021 * @a str. Determines the effective length rlen of the strings to
2022 * compare as the smallest of size() and str.size(). The function
2023 * then compares the two strings by calling traits::compare(data(),
2024 * str.data(),rlen). If the result of the comparison is nonzero returns
2025 * it, otherwise the shorter one is ordered first.
2028 compare(const basic_string& __str) const
2030 const size_type __size = this->size();
2031 const size_type __osize = __str.size();
2032 const size_type __len = std::min(__size, __osize);
2034 int __r = traits_type::compare(_M_data(), __str.data(), __len);
2036 __r = _S_compare(__size, __osize);
2041 * @brief Compare substring to a string.
2042 * @param pos Index of first character of substring.
2043 * @param n Number of characters in substring.
2044 * @param str String to compare against.
2045 * @return Integer < 0, 0, or > 0.
2047 * Form the substring of this string from the @a n characters starting
2048 * at @a pos. Returns an integer < 0 if the substring is ordered
2049 * before @a str, 0 if their values are equivalent, or > 0 if the
2050 * substring is ordered after @a str. Determines the effective length
2051 * rlen of the strings to compare as the smallest of the length of the
2052 * substring and @a str.size(). The function then compares the two
2053 * strings by calling traits::compare(substring.data(),str.data(),rlen).
2054 * If the result of the comparison is nonzero returns it, otherwise the
2055 * shorter one is ordered first.
2058 compare(size_type __pos, size_type __n, const basic_string& __str) const;
2061 * @brief Compare substring to a substring.
2062 * @param pos1 Index of first character of substring.
2063 * @param n1 Number of characters in substring.
2064 * @param str String to compare against.
2065 * @param pos2 Index of first character of substring of str.
2066 * @param n2 Number of characters in substring of str.
2067 * @return Integer < 0, 0, or > 0.
2069 * Form the substring of this string from the @a n1 characters starting
2070 * at @a pos1. Form the substring of @a str from the @a n2 characters
2071 * starting at @a pos2. Returns an integer < 0 if this substring is
2072 * ordered before the substring of @a str, 0 if their values are
2073 * equivalent, or > 0 if this substring is ordered after the substring
2074 * of @a str. Determines the effective length rlen of the strings
2075 * to compare as the smallest of the lengths of the substrings. The
2076 * function then compares the two strings by calling
2077 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
2078 * If the result of the comparison is nonzero returns it, otherwise the
2079 * shorter one is ordered first.
2082 compare(size_type __pos1, size_type __n1, const basic_string& __str,
2083 size_type __pos2, size_type __n2) const;
2086 * @brief Compare to a C string.
2087 * @param s C string to compare against.
2088 * @return Integer < 0, 0, or > 0.
2090 * Returns an integer < 0 if this string is ordered before @a s, 0 if
2091 * their values are equivalent, or > 0 if this string is ordered after
2092 * @a s. Determines the effective length rlen of the strings to
2093 * compare as the smallest of size() and the length of a string
2094 * constructed from @a s. The function then compares the two strings
2095 * by calling traits::compare(data(),s,rlen). If the result of the
2096 * comparison is nonzero returns it, otherwise the shorter one is
2100 compare(const _CharT* __s) const;
2102 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2103 // 5 String::compare specification questionable
2105 * @brief Compare substring to a C string.
2106 * @param pos Index of first character of substring.
2107 * @param n1 Number of characters in substring.
2108 * @param s C string to compare against.
2109 * @return Integer < 0, 0, or > 0.
2111 * Form the substring of this string from the @a n1 characters starting
2112 * at @a pos. Returns an integer < 0 if the substring is ordered
2113 * before @a s, 0 if their values are equivalent, or > 0 if the
2114 * substring is ordered after @a s. Determines the effective length
2115 * rlen of the strings to compare as the smallest of the length of the
2116 * substring and the length of a string constructed from @a s. The
2117 * function then compares the two string by calling
2118 * traits::compare(substring.data(),s,rlen). If the result of the
2119 * comparison is nonzero returns it, otherwise the shorter one is
2123 compare(size_type __pos, size_type __n1, const _CharT* __s) const;
2126 * @brief Compare substring against a character array.
2127 * @param pos1 Index of first character of substring.
2128 * @param n1 Number of characters in substring.
2129 * @param s character array to compare against.
2130 * @param n2 Number of characters of s.
2131 * @return Integer < 0, 0, or > 0.
2133 * Form the substring of this string from the @a n1 characters starting
2134 * at @a pos1. Form a string from the first @a n2 characters of @a s.
2135 * Returns an integer < 0 if this substring is ordered before the string
2136 * from @a s, 0 if their values are equivalent, or > 0 if this substring
2137 * is ordered after the string from @a s. Determines the effective
2138 * length rlen of the strings to compare as the smallest of the length
2139 * of the substring and @a n2. The function then compares the two
2140 * strings by calling traits::compare(substring.data(),s,rlen). If the
2141 * result of the comparison is nonzero returns it, otherwise the shorter
2142 * one is ordered first.
2144 * NB: s must have at least n2 characters, '\0' has no special
2148 compare(size_type __pos, size_type __n1, const _CharT* __s,
2149 size_type __n2) const;
2152 template<typename _CharT, typename _Traits, typename _Alloc>
2153 inline basic_string<_CharT, _Traits, _Alloc>::
2155 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
2156 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
2158 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()) { }
2163 * @brief Concatenate two strings.
2164 * @param lhs First string.
2165 * @param rhs Last string.
2166 * @return New string with value of @a lhs followed by @a rhs.
2168 template<typename _CharT, typename _Traits, typename _Alloc>
2169 basic_string<_CharT, _Traits, _Alloc>
2170 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2171 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2173 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2174 __str.append(__rhs);
2179 * @brief Concatenate C string and string.
2180 * @param lhs First string.
2181 * @param rhs Last string.
2182 * @return New string with value of @a lhs followed by @a rhs.
2184 template<typename _CharT, typename _Traits, typename _Alloc>
2185 basic_string<_CharT,_Traits,_Alloc>
2186 operator+(const _CharT* __lhs,
2187 const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2190 * @brief Concatenate character and string.
2191 * @param lhs First string.
2192 * @param rhs Last string.
2193 * @return New string with @a lhs followed by @a rhs.
2195 template<typename _CharT, typename _Traits, typename _Alloc>
2196 basic_string<_CharT,_Traits,_Alloc>
2197 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2200 * @brief Concatenate string and C string.
2201 * @param lhs First string.
2202 * @param rhs Last string.
2203 * @return New string with @a lhs followed by @a rhs.
2205 template<typename _CharT, typename _Traits, typename _Alloc>
2206 inline basic_string<_CharT, _Traits, _Alloc>
2207 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2208 const _CharT* __rhs)
2210 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2211 __str.append(__rhs);
2216 * @brief Concatenate string and character.
2217 * @param lhs First string.
2218 * @param rhs Last string.
2219 * @return New string with @a lhs followed by @a rhs.
2221 template<typename _CharT, typename _Traits, typename _Alloc>
2222 inline basic_string<_CharT, _Traits, _Alloc>
2223 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
2225 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
2226 typedef typename __string_type::size_type __size_type;
2227 __string_type __str(__lhs);
2228 __str.append(__size_type(1), __rhs);
2234 * @brief Test equivalence of two strings.
2235 * @param lhs First string.
2236 * @param rhs Second string.
2237 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2239 template<typename _CharT, typename _Traits, typename _Alloc>
2241 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2242 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2243 { return __lhs.compare(__rhs) == 0; }
2245 template<typename _CharT>
2247 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type
2248 operator==(const basic_string<_CharT>& __lhs,
2249 const basic_string<_CharT>& __rhs)
2250 { return (__lhs.size() == __rhs.size()
2251 && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
2255 * @brief Test equivalence of C string and string.
2256 * @param lhs C string.
2257 * @param rhs String.
2258 * @return True if @a rhs.compare(@a lhs) == 0. False otherwise.
2260 template<typename _CharT, typename _Traits, typename _Alloc>
2262 operator==(const _CharT* __lhs,
2263 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2264 { return __rhs.compare(__lhs) == 0; }
2267 * @brief Test equivalence of string and C string.
2268 * @param lhs String.
2269 * @param rhs C string.
2270 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2272 template<typename _CharT, typename _Traits, typename _Alloc>
2274 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2275 const _CharT* __rhs)
2276 { return __lhs.compare(__rhs) == 0; }
2280 * @brief Test difference of two strings.
2281 * @param lhs First string.
2282 * @param rhs Second string.
2283 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2285 template<typename _CharT, typename _Traits, typename _Alloc>
2287 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2288 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2289 { return !(__lhs == __rhs); }
2292 * @brief Test difference of C string and string.
2293 * @param lhs C string.
2294 * @param rhs String.
2295 * @return True if @a rhs.compare(@a lhs) != 0. False otherwise.
2297 template<typename _CharT, typename _Traits, typename _Alloc>
2299 operator!=(const _CharT* __lhs,
2300 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2301 { return !(__lhs == __rhs); }
2304 * @brief Test difference of string and C string.
2305 * @param lhs String.
2306 * @param rhs C string.
2307 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2309 template<typename _CharT, typename _Traits, typename _Alloc>
2311 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2312 const _CharT* __rhs)
2313 { return !(__lhs == __rhs); }
2317 * @brief Test if string precedes string.
2318 * @param lhs First string.
2319 * @param rhs Second string.
2320 * @return True if @a lhs precedes @a rhs. False otherwise.
2322 template<typename _CharT, typename _Traits, typename _Alloc>
2324 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2325 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2326 { return __lhs.compare(__rhs) < 0; }
2329 * @brief Test if string precedes C string.
2330 * @param lhs String.
2331 * @param rhs C string.
2332 * @return True if @a lhs precedes @a rhs. False otherwise.
2334 template<typename _CharT, typename _Traits, typename _Alloc>
2336 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2337 const _CharT* __rhs)
2338 { return __lhs.compare(__rhs) < 0; }
2341 * @brief Test if C string precedes string.
2342 * @param lhs C string.
2343 * @param rhs String.
2344 * @return True if @a lhs precedes @a rhs. False otherwise.
2346 template<typename _CharT, typename _Traits, typename _Alloc>
2348 operator<(const _CharT* __lhs,
2349 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2350 { return __rhs.compare(__lhs) > 0; }
2354 * @brief Test if string follows string.
2355 * @param lhs First string.
2356 * @param rhs Second string.
2357 * @return True if @a lhs follows @a rhs. False otherwise.
2359 template<typename _CharT, typename _Traits, typename _Alloc>
2361 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2362 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2363 { return __lhs.compare(__rhs) > 0; }
2366 * @brief Test if string follows C string.
2367 * @param lhs String.
2368 * @param rhs C string.
2369 * @return True if @a lhs follows @a rhs. False otherwise.
2371 template<typename _CharT, typename _Traits, typename _Alloc>
2373 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2374 const _CharT* __rhs)
2375 { return __lhs.compare(__rhs) > 0; }
2378 * @brief Test if C string follows string.
2379 * @param lhs C string.
2380 * @param rhs String.
2381 * @return True if @a lhs follows @a rhs. False otherwise.
2383 template<typename _CharT, typename _Traits, typename _Alloc>
2385 operator>(const _CharT* __lhs,
2386 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2387 { return __rhs.compare(__lhs) < 0; }
2391 * @brief Test if string doesn't follow string.
2392 * @param lhs First string.
2393 * @param rhs Second string.
2394 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2396 template<typename _CharT, typename _Traits, typename _Alloc>
2398 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2399 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2400 { return __lhs.compare(__rhs) <= 0; }
2403 * @brief Test if string doesn't follow C string.
2404 * @param lhs String.
2405 * @param rhs C string.
2406 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2408 template<typename _CharT, typename _Traits, typename _Alloc>
2410 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2411 const _CharT* __rhs)
2412 { return __lhs.compare(__rhs) <= 0; }
2415 * @brief Test if C string doesn't follow string.
2416 * @param lhs C string.
2417 * @param rhs String.
2418 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2420 template<typename _CharT, typename _Traits, typename _Alloc>
2422 operator<=(const _CharT* __lhs,
2423 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2424 { return __rhs.compare(__lhs) >= 0; }
2428 * @brief Test if string doesn't precede string.
2429 * @param lhs First string.
2430 * @param rhs Second string.
2431 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2433 template<typename _CharT, typename _Traits, typename _Alloc>
2435 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2436 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2437 { return __lhs.compare(__rhs) >= 0; }
2440 * @brief Test if string doesn't precede C string.
2441 * @param lhs String.
2442 * @param rhs C string.
2443 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2445 template<typename _CharT, typename _Traits, typename _Alloc>
2447 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2448 const _CharT* __rhs)
2449 { return __lhs.compare(__rhs) >= 0; }
2452 * @brief Test if C string doesn't precede string.
2453 * @param lhs C string.
2454 * @param rhs String.
2455 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2457 template<typename _CharT, typename _Traits, typename _Alloc>
2459 operator>=(const _CharT* __lhs,
2460 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2461 { return __rhs.compare(__lhs) <= 0; }
2464 * @brief Swap contents of two strings.
2465 * @param lhs First string.
2466 * @param rhs Second string.
2468 * Exchanges the contents of @a lhs and @a rhs in constant time.
2470 template<typename _CharT, typename _Traits, typename _Alloc>
2472 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
2473 basic_string<_CharT, _Traits, _Alloc>& __rhs)
2474 { __lhs.swap(__rhs); }
2477 * @brief Read stream into a string.
2478 * @param is Input stream.
2479 * @param str Buffer to store into.
2480 * @return Reference to the input stream.
2482 * Stores characters from @a is into @a str until whitespace is found, the
2483 * end of the stream is encountered, or str.max_size() is reached. If
2484 * is.width() is non-zero, that is the limit on the number of characters
2485 * stored into @a str. Any previous contents of @a str are erased.
2487 template<typename _CharT, typename _Traits, typename _Alloc>
2488 basic_istream<_CharT, _Traits>&
2489 operator>>(basic_istream<_CharT, _Traits>& __is,
2490 basic_string<_CharT, _Traits, _Alloc>& __str);
2493 basic_istream<char>&
2494 operator>>(basic_istream<char>& __is, basic_string<char>& __str);
2497 * @brief Write string to a stream.
2498 * @param os Output stream.
2499 * @param str String to write out.
2500 * @return Reference to the output stream.
2502 * Output characters of @a str into os following the same rules as for
2503 * writing a C string.
2505 template<typename _CharT, typename _Traits, typename _Alloc>
2506 inline basic_ostream<_CharT, _Traits>&
2507 operator<<(basic_ostream<_CharT, _Traits>& __os,
2508 const basic_string<_CharT, _Traits, _Alloc>& __str)
2510 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2511 // 586. string inserter not a formatted function
2512 return __ostream_insert(__os, __str.data(), __str.size());
2516 * @brief Read a line from stream into a string.
2517 * @param is Input stream.
2518 * @param str Buffer to store into.
2519 * @param delim Character marking end of line.
2520 * @return Reference to the input stream.
2522 * Stores characters from @a is into @a str until @a delim is found, the
2523 * end of the stream is encountered, or str.max_size() is reached. If
2524 * is.width() is non-zero, that is the limit on the number of characters
2525 * stored into @a str. Any previous contents of @a str are erased. If @a
2526 * delim was encountered, it is extracted but not stored into @a str.
2528 template<typename _CharT, typename _Traits, typename _Alloc>
2529 basic_istream<_CharT, _Traits>&
2530 getline(basic_istream<_CharT, _Traits>& __is,
2531 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
2534 * @brief Read a line from stream into a string.
2535 * @param is Input stream.
2536 * @param str Buffer to store into.
2537 * @return Reference to the input stream.
2539 * Stores characters from is into @a str until '\n' is found, the end of
2540 * the stream is encountered, or str.max_size() is reached. If is.width()
2541 * is non-zero, that is the limit on the number of characters stored into
2542 * @a str. Any previous contents of @a str are erased. If end of line was
2543 * encountered, it is extracted but not stored into @a str.
2545 template<typename _CharT, typename _Traits, typename _Alloc>
2546 inline basic_istream<_CharT, _Traits>&
2547 getline(basic_istream<_CharT, _Traits>& __is,
2548 basic_string<_CharT, _Traits, _Alloc>& __str)
2549 { return getline(__is, __str, __is.widen('\n')); }
2552 basic_istream<char>&
2553 getline(basic_istream<char>& __in, basic_string<char>& __str,
2556 #ifdef _GLIBCXX_USE_WCHAR_T
2558 basic_istream<wchar_t>&
2559 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
2563 _GLIBCXX_END_NAMESPACE
2565 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) && defined(_GLIBCXX_USE_C99) \
2566 && !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))
2568 #include <ext/string_conversions.h>
2570 _GLIBCXX_BEGIN_NAMESPACE(std)
2572 // 21.4 Numeric Conversions [string.conversions].
2574 stoi(const string& __str, size_t* __idx = 0, int __base = 10)
2575 { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
2579 stol(const string& __str, size_t* __idx = 0, int __base = 10)
2580 { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
2583 inline unsigned long
2584 stoul(const string& __str, size_t* __idx = 0, int __base = 10)
2585 { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
2589 stoll(const string& __str, size_t* __idx = 0, int __base = 10)
2590 { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
2593 inline unsigned long long
2594 stoull(const string& __str, size_t* __idx = 0, int __base = 10)
2595 { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
2598 // NB: strtof vs strtod.
2600 stof(const string& __str, size_t* __idx = 0)
2601 { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
2604 stod(const string& __str, size_t* __idx = 0)
2605 { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
2608 stold(const string& __str, size_t* __idx = 0)
2609 { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
2611 // NB: (v)snprintf vs sprintf.
2613 to_string(long long __val)
2614 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
2615 4 * sizeof(long long),
2619 to_string(unsigned long long __val)
2620 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
2621 4 * sizeof(unsigned long long),
2625 to_string(long double __val)
2628 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
2629 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
2633 #ifdef _GLIBCXX_USE_WCHAR_T
2635 stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
2636 { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
2640 stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
2641 { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
2644 inline unsigned long
2645 stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
2646 { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
2650 stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
2651 { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
2654 inline unsigned long long
2655 stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
2656 { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
2659 // NB: wcstof vs wcstod.
2661 stof(const wstring& __str, size_t* __idx = 0)
2662 { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
2665 stod(const wstring& __str, size_t* __idx = 0)
2666 { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
2669 stold(const wstring& __str, size_t* __idx = 0)
2670 { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
2673 to_wstring(long long __val)
2674 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
2675 4 * sizeof(long long),
2679 to_wstring(unsigned long long __val)
2680 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
2681 4 * sizeof(unsigned long long),
2685 to_wstring(long double __val)
2688 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
2689 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
2694 _GLIBCXX_END_NAMESPACE
2698 #endif /* _BASIC_STRING_H */