1 // Components for manipulating sequences of characters -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 // ISO C++ 14882: 21 Strings library
35 /** @file basic_string.h
36 * This is an internal header file, included by other library headers.
37 * You should not attempt to use it directly.
40 #ifndef _BASIC_STRING_H
41 #define _BASIC_STRING_H 1
43 #pragma GCC system_header
45 #include <bits/atomicity.h>
46 #include <debug/debug.h>
51 * @class basic_string basic_string.h <string>
52 * @brief Managing sequences of characters and character-like objects.
57 * Meets the requirements of a <a href="tables.html#65">container</a>, a
58 * <a href="tables.html#66">reversible container</a>, and a
59 * <a href="tables.html#67">sequence</a>. Of the
60 * <a href="tables.html#68">optional sequence requirements</a>, only
61 * @c push_back, @c at, and array access are supported.
67 * Documentation? What's that?
68 * Nathan Myers <ncm@cantrip.org>.
70 * A string looks like this:
75 * [basic_string<char_type>] _M_capacity
76 * _M_dataplus _M_refcount
77 * _M_p ----------------> unnamed array of char_type
80 * Where the _M_p points to the first character in the string, and
81 * you cast it to a pointer-to-_Rep and subtract 1 to get a
82 * pointer to the header.
84 * This approach has the enormous advantage that a string object
85 * requires only one allocation. All the ugliness is confined
86 * within a single pair of inline functions, which each compile to
87 * a single "add" instruction: _Rep::_M_data(), and
88 * string::_M_rep(); and the allocation function which gets a
89 * block of raw bytes and with room enough and constructs a _Rep
90 * object at the front.
92 * The reason you want _M_data pointing to the character array and
93 * not the _Rep is so that the debugger can see the string
94 * contents. (Probably we should add a non-inline member to get
95 * the _Rep for the debugger to use, so users can check the actual
98 * Note that the _Rep object is a POD so that you can have a
99 * static "empty string" _Rep object already "constructed" before
100 * static constructors have run. The reference-count encoding is
101 * chosen so that a 0 indicates one reference, so you never try to
102 * destroy the empty-string _Rep object.
104 * All but the last paragraph is considered pretty conventional
105 * for a C++ string implementation.
108 // 21.3 Template class basic_string
109 template<typename _CharT, typename _Traits, typename _Alloc>
114 typedef _Traits traits_type;
115 typedef typename _Traits::char_type value_type;
116 typedef _Alloc allocator_type;
117 typedef typename _Alloc::size_type size_type;
118 typedef typename _Alloc::difference_type difference_type;
119 typedef typename _Alloc::reference reference;
120 typedef typename _Alloc::const_reference const_reference;
121 typedef typename _Alloc::pointer pointer;
122 typedef typename _Alloc::const_pointer const_pointer;
123 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
124 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
126 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
127 typedef std::reverse_iterator<iterator> reverse_iterator;
130 // _Rep: string representation
132 // 1. String really contains _M_length + 1 characters: due to 21.3.4
133 // must be kept null-terminated.
134 // 2. _M_capacity >= _M_length
135 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
136 // 3. _M_refcount has three states:
137 // -1: leaked, one reference, no ref-copies allowed, non-const.
138 // 0: one reference, non-const.
139 // n>0: n + 1 references, operations require a lock, const.
140 // 4. All fields==0 is an empty string, given the extra storage
141 // beyond-the-end for a null terminator; thus, the shared
142 // empty string representation needs no constructor.
147 size_type _M_capacity;
148 _Atomic_word _M_refcount;
151 struct _Rep : _Rep_base
154 typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc;
156 // (Public) Data members:
158 // The maximum number of individual char_type elements of an
159 // individual string is determined by _S_max_size. This is the
160 // value that will be returned by max_size(). (Whereas npos
161 // is the maximum number of bytes the allocator can allocate.)
162 // If one was to divvy up the theoretical largest size string,
163 // with a terminating character and m _CharT elements, it'd
165 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
167 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
168 // In addition, this implementation quarters this amount.
169 static const size_type _S_max_size;
170 static const _CharT _S_terminal;
172 // The following storage is init'd to 0 by the linker, resulting
173 // (carefully) in an empty string with one reference.
174 static size_type _S_empty_rep_storage[];
178 { return *reinterpret_cast<_Rep*>(&_S_empty_rep_storage); }
182 { return this->_M_refcount < 0; }
186 { return this->_M_refcount > 0; }
190 { this->_M_refcount = -1; }
194 { this->_M_refcount = 0; }
198 { return reinterpret_cast<_CharT*>(this + 1); }
201 _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
203 return (!_M_is_leaked() && __alloc1 == __alloc2)
204 ? _M_refcopy() : _M_clone(__alloc1);
209 _S_create(size_type, size_type, const _Alloc&);
212 _M_dispose(const _Alloc& __a)
214 if (__builtin_expect(this != &_S_empty_rep(), false))
215 if (__gnu_cxx::__exchange_and_add(&this->_M_refcount, -1) <= 0)
220 _M_destroy(const _Alloc&) throw();
225 if (__builtin_expect(this != &_S_empty_rep(), false))
226 __gnu_cxx::__atomic_add(&this->_M_refcount, 1);
231 _M_clone(const _Alloc&, size_type __res = 0);
234 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
235 struct _Alloc_hider : _Alloc
237 _Alloc_hider(_CharT* __dat, const _Alloc& __a)
238 : _Alloc(__a), _M_p(__dat) { }
240 _CharT* _M_p; // The actual data.
244 // Data Members (public):
245 // NB: This is an unsigned type, and thus represents the maximum
246 // size that the allocator can hold.
248 /// Value returned by various member functions when they fail.
249 static const size_type npos = static_cast<size_type>(-1);
252 // Data Members (private):
253 mutable _Alloc_hider _M_dataplus;
257 { return _M_dataplus._M_p; }
261 { return (_M_dataplus._M_p = __p); }
265 { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
267 // For the internal use we have functions similar to `begin'/`end'
268 // but they do not call _M_leak.
270 _M_ibegin() const { return iterator(_M_data()); }
273 _M_iend() const { return iterator(_M_data() + this->size()); }
276 _M_leak() // for use in begin() & non-const op[]
278 if (!_M_rep()->_M_is_leaked())
283 _M_check(size_type __pos, const char* __s) const
285 if (__pos > this->size())
286 __throw_out_of_range(__N(__s));
290 // NB: _M_limit doesn't check for a bad __pos value.
292 _M_limit(size_type __pos, size_type __off) const
294 const bool __testoff = __off < this->size() - __pos;
295 return __testoff ? __off : this->size() - __pos;
298 // _S_copy_chars is a separate template to permit specialization
299 // to optimize for the common case of pointers as iterators.
300 template<class _Iterator>
302 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
304 for (; __k1 != __k2; ++__k1, ++__p)
305 traits_type::assign(*__p, *__k1); // These types are off.
309 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2)
310 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
313 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
314 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
317 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
318 { traits_type::copy(__p, __k1, __k2 - __k1); }
321 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
322 { traits_type::copy(__p, __k1, __k2 - __k1); }
325 _M_mutate(size_type __pos, size_type __len1, size_type __len2);
332 { return _Rep::_S_empty_rep(); }
335 // Construct/copy/destroy:
336 // NB: We overload ctors in some cases instead of using default
337 // arguments, per 17.4.4.4 para. 2 item 2.
340 * @brief Default constructor creates an empty string.
346 * @brief Construct an empty string using allocator a.
349 basic_string(const _Alloc& __a);
351 // NB: per LWG issue 42, semantics different from IS:
353 * @brief Construct string with copy of value of @a str.
354 * @param str Source string.
356 basic_string(const basic_string& __str);
358 * @brief Construct string as copy of a substring.
359 * @param str Source string.
360 * @param pos Index of first character to copy from.
361 * @param n Number of characters to copy (default remainder).
363 basic_string(const basic_string& __str, size_type __pos,
364 size_type __n = npos);
366 * @brief Construct string as copy of a substring.
367 * @param str Source string.
368 * @param pos Index of first character to copy from.
369 * @param n Number of characters to copy.
370 * @param a Allocator to use.
372 basic_string(const basic_string& __str, size_type __pos,
373 size_type __n, const _Alloc& __a);
376 * @brief Construct string initialized by a character array.
377 * @param s Source character array.
378 * @param n Number of characters to copy.
379 * @param a Allocator to use (default is default allocator).
381 * NB: s must have at least n characters, '\0' has no special
384 basic_string(const _CharT* __s, size_type __n,
385 const _Alloc& __a = _Alloc());
387 * @brief Construct string as copy of a C string.
388 * @param s Source C string.
389 * @param a Allocator to use (default is default allocator).
391 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
393 * @brief Construct string as multiple characters.
394 * @param n Number of characters.
395 * @param c Character to use.
396 * @param a Allocator to use (default is default allocator).
398 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
401 * @brief Construct string as copy of a range.
402 * @param beg Start of range.
403 * @param end End of range.
404 * @param a Allocator to use (default is default allocator).
406 template<class _InputIterator>
407 basic_string(_InputIterator __beg, _InputIterator __end,
408 const _Alloc& __a = _Alloc());
411 * @brief Destroy the string instance.
414 { _M_rep()->_M_dispose(this->get_allocator()); }
417 * @brief Assign the value of @a str to this string.
418 * @param str Source string.
421 operator=(const basic_string& __str)
422 { return this->assign(__str); }
425 * @brief Copy contents of @a s into this string.
426 * @param s Source null-terminated string.
429 operator=(const _CharT* __s)
430 { return this->assign(__s); }
433 * @brief Set value to string of length 1.
434 * @param c Source character.
436 * Assigning to a character makes this string length 1 and
437 * (*this)[0] == @a c.
440 operator=(_CharT __c)
442 this->assign(1, __c);
448 * Returns a read/write iterator that points to the first character in
449 * the %string. Unshares the string.
455 return iterator(_M_data());
459 * Returns a read-only (constant) iterator that points to the first
460 * character in the %string.
464 { return const_iterator(_M_data()); }
467 * Returns a read/write iterator that points one past the last
468 * character in the %string. Unshares the string.
474 return iterator(_M_data() + this->size());
478 * Returns a read-only (constant) iterator that points one past the
479 * last character in the %string.
483 { return const_iterator(_M_data() + this->size()); }
486 * Returns a read/write reverse iterator that points to the last
487 * character in the %string. Iteration is done in reverse element
488 * order. Unshares the string.
492 { return reverse_iterator(this->end()); }
495 * Returns a read-only (constant) reverse iterator that points
496 * to the last character in the %string. Iteration is done in
497 * reverse element order.
499 const_reverse_iterator
501 { return const_reverse_iterator(this->end()); }
504 * Returns a read/write reverse iterator that points to one before the
505 * first character in the %string. Iteration is done in reverse
506 * element order. Unshares the string.
510 { return reverse_iterator(this->begin()); }
513 * Returns a read-only (constant) reverse iterator that points
514 * to one before the first character in the %string. Iteration
515 * is done in reverse element order.
517 const_reverse_iterator
519 { return const_reverse_iterator(this->begin()); }
523 /// Returns the number of characters in the string, not including any
524 /// null-termination.
526 size() const { return _M_rep()->_M_length; }
528 /// Returns the number of characters in the string, not including any
529 /// null-termination.
531 length() const { return _M_rep()->_M_length; }
533 /// Returns the size() of the largest possible %string.
535 max_size() const { return _Rep::_S_max_size; }
538 * @brief Resizes the %string to the specified number of characters.
539 * @param n Number of characters the %string should contain.
540 * @param c Character to fill any new elements.
542 * This function will %resize the %string to the specified
543 * number of characters. If the number is smaller than the
544 * %string's current size the %string is truncated, otherwise
545 * the %string is extended and new elements are set to @a c.
548 resize(size_type __n, _CharT __c);
551 * @brief Resizes the %string to the specified number of characters.
552 * @param n Number of characters the %string should contain.
554 * This function will resize the %string to the specified length. If
555 * the new size is smaller than the %string's current size the %string
556 * is truncated, otherwise the %string is extended and new characters
557 * are default-constructed. For basic types such as char, this means
561 resize(size_type __n) { this->resize(__n, _CharT()); }
564 * Returns the total number of characters that the %string can hold
565 * before needing to allocate more memory.
568 capacity() const { return _M_rep()->_M_capacity; }
571 * @brief Attempt to preallocate enough memory for specified number of
573 * @param n Number of characters required.
574 * @throw std::length_error If @a n exceeds @c max_size().
576 * This function attempts to reserve enough memory for the
577 * %string to hold the specified number of characters. If the
578 * number requested is more than max_size(), length_error is
581 * The advantage of this function is that if optimal code is a
582 * necessity and the user can determine the string length that will be
583 * required, the user can reserve the memory in %advance, and thus
584 * prevent a possible reallocation of memory and copying of %string
588 reserve(size_type __res_arg = 0);
591 * Erases the string, making it empty.
594 clear() { _M_mutate(0, this->size(), 0); }
597 * Returns true if the %string is empty. Equivalent to *this == "".
600 empty() const { return this->size() == 0; }
604 * @brief Subscript access to the data contained in the %string.
605 * @param n The index of the character to access.
606 * @return Read-only (constant) reference to the character.
608 * This operator allows for easy, array-style, data access.
609 * Note that data access with this operator is unchecked and
610 * out_of_range lookups are not defined. (For checked lookups
614 operator[] (size_type __pos) const
616 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
617 return _M_data()[__pos];
621 * @brief Subscript access to the data contained in the %string.
622 * @param n The index of the character to access.
623 * @return Read/write reference to the character.
625 * This operator allows for easy, array-style, data access.
626 * Note that data access with this operator is unchecked and
627 * out_of_range lookups are not defined. (For checked lookups
628 * see at().) Unshares the string.
631 operator[](size_type __pos)
633 _GLIBCXX_DEBUG_ASSERT(__pos < size());
635 return _M_data()[__pos];
639 * @brief Provides access to the data contained in the %string.
640 * @param n The index of the character to access.
641 * @return Read-only (const) reference to the character.
642 * @throw std::out_of_range If @a n is an invalid index.
644 * This function provides for safer data access. The parameter is
645 * first checked that it is in the range of the string. The function
646 * throws out_of_range if the check fails.
649 at(size_type __n) const
651 if (__n >= this->size())
652 __throw_out_of_range(__N("basic_string::at"));
653 return _M_data()[__n];
657 * @brief Provides access to the data contained in the %string.
658 * @param n The index of the character to access.
659 * @return Read/write reference to the character.
660 * @throw std::out_of_range If @a n is an invalid index.
662 * This function provides for safer data access. The parameter is
663 * first checked that it is in the range of the string. The function
664 * throws out_of_range if the check fails. Success results in
665 * unsharing the string.
671 __throw_out_of_range(__N("basic_string::at"));
673 return _M_data()[__n];
678 * @brief Append a string to this string.
679 * @param str The string to append.
680 * @return Reference to this string.
683 operator+=(const basic_string& __str) { return this->append(__str); }
686 * @brief Append a C string.
687 * @param s The C string to append.
688 * @return Reference to this string.
691 operator+=(const _CharT* __s) { return this->append(__s); }
694 * @brief Append a character.
695 * @param s The character to append.
696 * @return Reference to this string.
699 operator+=(_CharT __c) { return this->append(size_type(1), __c); }
702 * @brief Append a string to this string.
703 * @param str The string to append.
704 * @return Reference to this string.
707 append(const basic_string& __str);
710 * @brief Append a substring.
711 * @param str The string to append.
712 * @param pos Index of the first character of str to append.
713 * @param n The number of characters to append.
714 * @return Reference to this string.
715 * @throw std::out_of_range if @a pos is not a valid index.
717 * This function appends @a n characters from @a str starting at @a pos
718 * to this string. If @a n is is larger than the number of available
719 * characters in @a str, the remainder of @a str is appended.
722 append(const basic_string& __str, size_type __pos, size_type __n);
725 * @brief Append a C substring.
726 * @param s The C string to append.
727 * @param n The number of characters to append.
728 * @return Reference to this string.
731 append(const _CharT* __s, size_type __n);
734 * @brief Append a C string.
735 * @param s The C string to append.
736 * @return Reference to this string.
739 append(const _CharT* __s)
741 __glibcxx_requires_string(__s);
742 return this->append(__s, traits_type::length(__s));
746 * @brief Append multiple characters.
747 * @param n The number of characters to append.
748 * @param c The character to use.
749 * @return Reference to this string.
751 * Appends n copies of c to this string.
754 append(size_type __n, _CharT __c)
755 { return _M_replace_aux(this->size(), size_type(0), __n, __c); }
758 * @brief Append a range of characters.
759 * @param first Iterator referencing the first character to append.
760 * @param last Iterator marking the end of the range.
761 * @return Reference to this string.
763 * Appends characters in the range [first,last) to this string.
765 template<class _InputIterator>
767 append(_InputIterator __first, _InputIterator __last)
768 { return this->replace(_M_iend(), _M_iend(), __first, __last); }
771 * @brief Append a single character.
772 * @param c Character to append.
775 push_back(_CharT __c)
776 { _M_replace_aux(this->size(), size_type(0), size_type(1), __c); }
779 * @brief Set value to contents of another string.
780 * @param str Source string to use.
781 * @return Reference to this string.
784 assign(const basic_string& __str);
787 * @brief Set value to a substring of a string.
788 * @param str The string to use.
789 * @param pos Index of the first character of str.
790 * @param n Number of characters to use.
791 * @return Reference to this string.
792 * @throw std::out_of_range if @a pos is not a valid index.
794 * This function sets this string to the substring of @a str consisting
795 * of @a n characters at @a pos. If @a n is is larger than the number
796 * of available characters in @a str, the remainder of @a str is used.
799 assign(const basic_string& __str, size_type __pos, size_type __n)
800 { return this->assign(__str._M_data()
801 + __str._M_check(__pos, "basic_string::assign"),
802 __str._M_limit(__pos, __n)); }
805 * @brief Set value to a C substring.
806 * @param s The C string to use.
807 * @param n Number of characters to use.
808 * @return Reference to this string.
810 * This function sets the value of this string to the first @a n
811 * characters of @a s. If @a n is is larger than the number of
812 * available characters in @a s, the remainder of @a s is used.
815 assign(const _CharT* __s, size_type __n);
818 * @brief Set value to contents of a C string.
819 * @param s The C string to use.
820 * @return Reference to this string.
822 * This function sets the value of this string to the value of @a s.
823 * The data is copied, so there is no dependence on @a s once the
827 assign(const _CharT* __s)
829 __glibcxx_requires_string(__s);
830 return this->assign(__s, traits_type::length(__s));
834 * @brief Set value to multiple characters.
835 * @param n Length of the resulting string.
836 * @param c The character to use.
837 * @return Reference to this string.
839 * This function sets the value of this string to @a n copies of
843 assign(size_type __n, _CharT __c)
844 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
847 * @brief Set value to a range of characters.
848 * @param first Iterator referencing the first character to append.
849 * @param last Iterator marking the end of the range.
850 * @return Reference to this string.
852 * Sets value of string to characters in the range [first,last).
854 template<class _InputIterator>
856 assign(_InputIterator __first, _InputIterator __last)
857 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
860 * @brief Insert multiple characters.
861 * @param p Iterator referencing location in string to insert at.
862 * @param n Number of characters to insert
863 * @param c The character to insert.
864 * @throw std::length_error If new length exceeds @c max_size().
866 * Inserts @a n copies of character @a c starting at the position
867 * referenced by iterator @a p. If adding characters causes the length
868 * to exceed max_size(), length_error is thrown. The value of the
869 * string doesn't change if an error is thrown.
872 insert(iterator __p, size_type __n, _CharT __c)
873 { this->replace(__p, __p, __n, __c); }
876 * @brief Insert a range of characters.
877 * @param p Iterator referencing location in string to insert at.
878 * @param beg Start of range.
879 * @param end End of range.
880 * @throw std::length_error If new length exceeds @c max_size().
882 * Inserts characters in range [beg,end). If adding characters causes
883 * the length to exceed max_size(), length_error is thrown. The value
884 * of the string doesn't change if an error is thrown.
886 template<class _InputIterator>
887 void insert(iterator __p, _InputIterator __beg, _InputIterator __end)
888 { this->replace(__p, __p, __beg, __end); }
891 * @brief Insert value of a string.
892 * @param pos1 Iterator referencing location in string to insert at.
893 * @param str The string to insert.
894 * @return Reference to this string.
895 * @throw std::length_error If new length exceeds @c max_size().
897 * Inserts value of @a str starting at @a pos1. If adding characters
898 * causes the length to exceed max_size(), length_error is thrown. The
899 * value of the string doesn't change if an error is thrown.
902 insert(size_type __pos1, const basic_string& __str)
903 { return this->insert(__pos1, __str, size_type(0), __str.size()); }
906 * @brief Insert a substring.
907 * @param pos1 Iterator referencing location in string to insert at.
908 * @param str The string to insert.
909 * @param pos2 Start of characters in str to insert.
910 * @param n Number of characters to insert.
911 * @return Reference to this string.
912 * @throw std::length_error If new length exceeds @c max_size().
913 * @throw std::out_of_range If @a pos1 > size() or
914 * @a pos2 > @a str.size().
916 * Starting at @a pos1, insert @a n character of @a str beginning with
917 * @a pos2. If adding characters causes the length to exceed
918 * max_size(), length_error is thrown. If @a pos1 is beyond the end of
919 * this string or @a pos2 is beyond the end of @a str, out_of_range is
920 * thrown. The value of the string doesn't change if an error is
924 insert(size_type __pos1, const basic_string& __str,
925 size_type __pos2, size_type __n)
926 { return this->insert(__pos1, __str._M_data()
927 + __str._M_check(__pos2, "basic_string::insert"),
928 __str._M_limit(__pos2, __n)); }
931 * @brief Insert a C substring.
932 * @param pos Iterator referencing location in string to insert at.
933 * @param s The C string to insert.
934 * @param n The number of characters to insert.
935 * @return Reference to this string.
936 * @throw std::length_error If new length exceeds @c max_size().
937 * @throw std::out_of_range If @a pos is beyond the end of this
940 * Inserts the first @a n characters of @a s starting at @a pos. If
941 * adding characters causes the length to exceed max_size(),
942 * length_error is thrown. If @a pos is beyond end(), out_of_range is
943 * thrown. The value of the string doesn't change if an error is
947 insert(size_type __pos, const _CharT* __s, size_type __n);
950 * @brief Insert a C string.
951 * @param pos Iterator referencing location in string to insert at.
952 * @param s The C string to insert.
953 * @return Reference to this string.
954 * @throw std::length_error If new length exceeds @c max_size().
955 * @throw std::out_of_range If @a pos is beyond the end of this
958 * Inserts the first @a n characters of @a s starting at @a pos. If
959 * adding characters causes the length to exceed max_size(),
960 * length_error is thrown. If @a pos is beyond end(), out_of_range is
961 * thrown. The value of the string doesn't change if an error is
965 insert(size_type __pos, const _CharT* __s)
967 __glibcxx_requires_string(__s);
968 return this->insert(__pos, __s, traits_type::length(__s));
972 * @brief Insert multiple characters.
973 * @param pos Index in string to insert at.
974 * @param n Number of characters to insert
975 * @param c The character to insert.
976 * @return Reference to this string.
977 * @throw std::length_error If new length exceeds @c max_size().
978 * @throw std::out_of_range If @a pos is beyond the end of this
981 * Inserts @a n copies of character @a c starting at index @a pos. If
982 * adding characters causes the length to exceed max_size(),
983 * length_error is thrown. If @a pos > length(), out_of_range is
984 * thrown. The value of the string doesn't change if an error is
988 insert(size_type __pos, size_type __n, _CharT __c)
989 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
990 size_type(0), __n, __c); }
993 * @brief Insert one character.
994 * @param p Iterator referencing position in string to insert at.
995 * @param c The character to insert.
996 * @return Iterator referencing newly inserted char.
997 * @throw std::length_error If new length exceeds @c max_size().
999 * Inserts character @a c at position referenced by @a p. If adding
1000 * character causes the length to exceed max_size(), length_error is
1001 * thrown. If @a p is beyond end of string, out_of_range is thrown.
1002 * The value of the string doesn't change if an error is thrown.
1005 insert(iterator __p, _CharT __c)
1007 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1008 const size_type __pos = __p - _M_ibegin();
1009 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1010 _M_rep()->_M_set_leaked();
1011 return this->_M_ibegin() + __pos;
1015 * @brief Remove characters.
1016 * @param pos Index of first character to remove (default 0).
1017 * @param n Number of characters to remove (default remainder).
1018 * @return Reference to this string.
1019 * @throw std::out_of_range If @a pos is beyond the end of this
1022 * Removes @a n characters from this string starting at @a pos. The
1023 * length of the string is reduced by @a n. If there are < @a n
1024 * characters to remove, the remainder of the string is truncated. If
1025 * @a p is beyond end of string, out_of_range is thrown. The value of
1026 * the string doesn't change if an error is thrown.
1029 erase(size_type __pos = 0, size_type __n = npos)
1030 { return _M_replace_safe(_M_check(__pos, "basic_string::erase"),
1031 _M_limit(__pos, __n), NULL, size_type(0)); }
1034 * @brief Remove one character.
1035 * @param position Iterator referencing the character to remove.
1036 * @return iterator referencing same location after removal.
1038 * Removes the character at @a position from this string. The value
1039 * of the string doesn't change if an error is thrown.
1042 erase(iterator __position)
1044 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
1045 && __position < _M_iend());
1046 const size_type __pos = __position - _M_ibegin();
1047 _M_replace_safe(__pos, size_type(1), NULL, size_type(0));
1048 _M_rep()->_M_set_leaked();
1049 return _M_ibegin() + __pos;
1053 * @brief Remove a range of characters.
1054 * @param first Iterator referencing the first character to remove.
1055 * @param last Iterator referencing the end of the range.
1056 * @return Iterator referencing location of first after removal.
1058 * Removes the characters in the range [first,last) from this string.
1059 * The value of the string doesn't change if an error is thrown.
1062 erase(iterator __first, iterator __last)
1064 _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
1065 && __last <= _M_iend());
1066 const size_type __pos = __first - _M_ibegin();
1067 _M_replace_safe(__pos, __last - __first, NULL, size_type(0));
1068 _M_rep()->_M_set_leaked();
1069 return _M_ibegin() + __pos;
1073 * @brief Replace characters with value from another string.
1074 * @param pos Index of first character to replace.
1075 * @param n Number of characters to be replaced.
1076 * @param str String to insert.
1077 * @return Reference to this string.
1078 * @throw std::out_of_range If @a pos is beyond the end of this
1080 * @throw std::length_error If new length exceeds @c max_size().
1082 * Removes the characters in the range [pos,pos+n) from this string.
1083 * In place, the value of @a str is inserted. If @a pos is beyond end
1084 * of string, out_of_range is thrown. If the length of the result
1085 * exceeds max_size(), length_error is thrown. The value of the string
1086 * doesn't change if an error is thrown.
1089 replace(size_type __pos, size_type __n, const basic_string& __str)
1090 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1093 * @brief Replace characters with value from another string.
1094 * @param pos1 Index of first character to replace.
1095 * @param n1 Number of characters to be replaced.
1096 * @param str String to insert.
1097 * @param pos2 Index of first character of str to use.
1098 * @param n2 Number of characters from str to use.
1099 * @return Reference to this string.
1100 * @throw std::out_of_range If @a pos1 > size() or @a pos2 >
1102 * @throw std::length_error If new length exceeds @c max_size().
1104 * Removes the characters in the range [pos1,pos1 + n) from this
1105 * string. In place, the value of @a str is inserted. If @a pos is
1106 * beyond end of string, out_of_range is thrown. If the length of the
1107 * result exceeds max_size(), length_error is thrown. The value of the
1108 * string doesn't change if an error is thrown.
1111 replace(size_type __pos1, size_type __n1, const basic_string& __str,
1112 size_type __pos2, size_type __n2)
1113 { return this->replace(__pos1, __n1, __str._M_data()
1114 + __str._M_check(__pos2, "basic_string::replace"),
1115 __str._M_limit(__pos2, __n2)); }
1118 * @brief Replace characters with value of a C substring.
1119 * @param pos Index of first character to replace.
1120 * @param n1 Number of characters to be replaced.
1121 * @param str C string to insert.
1122 * @param n2 Number of characters from str to use.
1123 * @return Reference to this string.
1124 * @throw std::out_of_range If @a pos1 > size().
1125 * @throw std::length_error If new length exceeds @c max_size().
1127 * Removes the characters in the range [pos,pos + n1) from this string.
1128 * In place, the first @a n2 characters of @a str are inserted, or all
1129 * of @a str if @a n2 is too large. If @a pos is beyond end of string,
1130 * out_of_range is thrown. If the length of result exceeds max_size(),
1131 * length_error is thrown. The value of the string doesn't change if
1132 * an error is thrown.
1135 replace(size_type __pos, size_type __n1, const _CharT* __s,
1139 * @brief Replace characters with value of a C string.
1140 * @param pos Index of first character to replace.
1141 * @param n1 Number of characters to be replaced.
1142 * @param str C string to insert.
1143 * @return Reference to this string.
1144 * @throw std::out_of_range If @a pos > size().
1145 * @throw std::length_error If new length exceeds @c max_size().
1147 * Removes the characters in the range [pos,pos + n1) from this string.
1148 * In place, the first @a n characters of @a str are inserted. If @a
1149 * pos is beyond end of string, out_of_range is thrown. If the length
1150 * of result exceeds max_size(), length_error is thrown. The value of
1151 * the string doesn't change if an error is thrown.
1154 replace(size_type __pos, size_type __n1, const _CharT* __s)
1156 __glibcxx_requires_string(__s);
1157 return this->replace(__pos, __n1, __s, traits_type::length(__s));
1161 * @brief Replace characters with multiple characters.
1162 * @param pos Index of first character to replace.
1163 * @param n1 Number of characters to be replaced.
1164 * @param n2 Number of characters to insert.
1165 * @param c Character to insert.
1166 * @return Reference to this string.
1167 * @throw std::out_of_range If @a pos > size().
1168 * @throw std::length_error If new length exceeds @c max_size().
1170 * Removes the characters in the range [pos,pos + n1) from this string.
1171 * In place, @a n2 copies of @a c are inserted. If @a pos is beyond
1172 * end of string, out_of_range is thrown. If the length of result
1173 * exceeds max_size(), length_error is thrown. The value of the string
1174 * doesn't change if an error is thrown.
1177 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1178 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
1179 _M_limit(__pos, __n1), __n2, __c); }
1182 * @brief Replace range of characters with string.
1183 * @param i1 Iterator referencing start of range to replace.
1184 * @param i2 Iterator referencing end of range to replace.
1185 * @param str String value to insert.
1186 * @return Reference to this string.
1187 * @throw std::length_error If new length exceeds @c max_size().
1189 * Removes the characters in the range [i1,i2). In place, the value of
1190 * @a str is inserted. If the length of result exceeds max_size(),
1191 * length_error is thrown. The value of the string doesn't change if
1192 * an error is thrown.
1195 replace(iterator __i1, iterator __i2, const basic_string& __str)
1196 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
1199 * @brief Replace range of characters with C substring.
1200 * @param i1 Iterator referencing start of range to replace.
1201 * @param i2 Iterator referencing end of range to replace.
1202 * @param s C string value to insert.
1203 * @param n Number of characters from s to insert.
1204 * @return Reference to this string.
1205 * @throw std::length_error If new length exceeds @c max_size().
1207 * Removes the characters in the range [i1,i2). In place, the first @a
1208 * n characters of @a s are inserted. If the length of result exceeds
1209 * max_size(), length_error is thrown. The value of the string doesn't
1210 * change if an error is thrown.
1213 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
1215 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1216 && __i2 <= _M_iend());
1217 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
1221 * @brief Replace range of characters with C string.
1222 * @param i1 Iterator referencing start of range to replace.
1223 * @param i2 Iterator referencing end of range to replace.
1224 * @param s C string value to insert.
1225 * @return Reference to this string.
1226 * @throw std::length_error If new length exceeds @c max_size().
1228 * Removes the characters in the range [i1,i2). In place, the
1229 * characters of @a s are inserted. If the length of result exceeds
1230 * max_size(), length_error is thrown. The value of the string doesn't
1231 * change if an error is thrown.
1234 replace(iterator __i1, iterator __i2, const _CharT* __s)
1236 __glibcxx_requires_string(__s);
1237 return this->replace(__i1, __i2, __s, traits_type::length(__s));
1241 * @brief Replace range of characters with multiple characters
1242 * @param i1 Iterator referencing start of range to replace.
1243 * @param i2 Iterator referencing end of range to replace.
1244 * @param n Number of characters to insert.
1245 * @param c Character to insert.
1246 * @return Reference to this string.
1247 * @throw std::length_error If new length exceeds @c max_size().
1249 * Removes the characters in the range [i1,i2). In place, @a n copies
1250 * of @a c are inserted. If the length of result exceeds max_size(),
1251 * length_error is thrown. The value of the string doesn't change if
1252 * an error is thrown.
1255 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
1257 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1258 && __i2 <= _M_iend());
1259 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
1263 * @brief Replace range of characters with range.
1264 * @param i1 Iterator referencing start of range to replace.
1265 * @param i2 Iterator referencing end of range to replace.
1266 * @param k1 Iterator referencing start of range to insert.
1267 * @param k2 Iterator referencing end of range to insert.
1268 * @return Reference to this string.
1269 * @throw std::length_error If new length exceeds @c max_size().
1271 * Removes the characters in the range [i1,i2). In place, characters
1272 * in the range [k1,k2) are inserted. If the length of result exceeds
1273 * max_size(), length_error is thrown. The value of the string doesn't
1274 * change if an error is thrown.
1276 template<class _InputIterator>
1278 replace(iterator __i1, iterator __i2,
1279 _InputIterator __k1, _InputIterator __k2)
1281 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1282 && __i2 <= _M_iend());
1283 __glibcxx_requires_valid_range(__k1, __k2);
1284 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
1285 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
1288 // Specializations for the common case of pointer and iterator:
1289 // useful to avoid the overhead of temporary buffering in _M_replace.
1291 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
1293 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1294 && __i2 <= _M_iend());
1295 __glibcxx_requires_valid_range(__k1, __k2);
1296 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1301 replace(iterator __i1, iterator __i2,
1302 const _CharT* __k1, const _CharT* __k2)
1304 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1305 && __i2 <= _M_iend());
1306 __glibcxx_requires_valid_range(__k1, __k2);
1307 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1312 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
1314 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1315 && __i2 <= _M_iend());
1316 __glibcxx_requires_valid_range(__k1, __k2);
1317 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1318 __k1.base(), __k2 - __k1);
1322 replace(iterator __i1, iterator __i2,
1323 const_iterator __k1, const_iterator __k2)
1325 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1326 && __i2 <= _M_iend());
1327 __glibcxx_requires_valid_range(__k1, __k2);
1328 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1329 __k1.base(), __k2 - __k1);
1333 template<class _Integer>
1335 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
1336 _Integer __val, __true_type)
1337 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
1339 template<class _InputIterator>
1341 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
1342 _InputIterator __k2, __false_type);
1345 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
1348 if (this->max_size() - (this->size() - __n1) < __n2)
1349 __throw_length_error(__N("basic_string::_M_replace_aux"));
1350 _M_mutate(__pos1, __n1, __n2);
1352 _M_data()[__pos1] = __c;
1354 traits_type::assign(_M_data() + __pos1, __n2, __c);
1359 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
1362 _M_mutate(__pos1, __n1, __n2);
1364 _M_data()[__pos1] = *__s;
1366 traits_type::copy(_M_data() + __pos1, __s, __n2);
1370 // _S_construct_aux is used to implement the 21.3.1 para 15 which
1371 // requires special behaviour if _InIter is an integral type
1372 template<class _InIterator>
1374 _S_construct_aux(_InIterator __beg, _InIterator __end,
1375 const _Alloc& __a, __false_type)
1377 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
1378 return _S_construct(__beg, __end, __a, _Tag());
1381 template<class _InIterator>
1383 _S_construct_aux(_InIterator __beg, _InIterator __end,
1384 const _Alloc& __a, __true_type)
1385 { return _S_construct(static_cast<size_type>(__beg),
1386 static_cast<value_type>(__end), __a); }
1388 template<class _InIterator>
1390 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
1392 typedef typename _Is_integer<_InIterator>::_Integral _Integral;
1393 return _S_construct_aux(__beg, __end, __a, _Integral());
1396 // For Input Iterators, used in istreambuf_iterators, etc.
1397 template<class _InIterator>
1399 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1400 input_iterator_tag);
1402 // For forward_iterators up to random_access_iterators, used for
1403 // string::iterator, _CharT*, etc.
1404 template<class _FwdIterator>
1406 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
1407 forward_iterator_tag);
1410 _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
1415 * @brief Copy substring into C string.
1416 * @param s C string to copy value into.
1417 * @param n Number of characters to copy.
1418 * @param pos Index of first character to copy.
1419 * @return Number of characters actually copied
1420 * @throw std::out_of_range If pos > size().
1422 * Copies up to @a n characters starting at @a pos into the C string @a
1423 * s. If @a pos is greater than size(), out_of_range is thrown.
1426 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
1429 * @brief Swap contents with another string.
1430 * @param s String to swap with.
1432 * Exchanges the contents of this string with that of @a s in constant
1436 swap(basic_string& __s);
1438 // String operations:
1440 * @brief Return const pointer to null-terminated contents.
1442 * This is a handle to internal data. Do not modify or dire things may
1446 c_str() const { return _M_data(); }
1449 * @brief Return const pointer to contents.
1451 * This is a handle to internal data. Do not modify or dire things may
1455 data() const { return _M_data(); }
1458 * @brief Return copy of allocator used to construct this string.
1461 get_allocator() const { return _M_dataplus; }
1464 * @brief Find position of a C substring.
1465 * @param s C string to locate.
1466 * @param pos Index of character to search from.
1467 * @param n Number of characters from @a s to search for.
1468 * @return Index of start of first occurrence.
1470 * Starting from @a pos, searches forward for the first @a n characters
1471 * in @a s within this string. If found, returns the index where it
1472 * begins. If not found, returns npos.
1475 find(const _CharT* __s, size_type __pos, size_type __n) const;
1478 * @brief Find position of a string.
1479 * @param str String to locate.
1480 * @param pos Index of character to search from (default 0).
1481 * @return Index of start of first occurrence.
1483 * Starting from @a pos, searches forward for value of @a str within
1484 * this string. If found, returns the index where it begins. If not
1485 * found, returns npos.
1488 find(const basic_string& __str, size_type __pos = 0) const
1489 { return this->find(__str.data(), __pos, __str.size()); }
1492 * @brief Find position of a C string.
1493 * @param s C string to locate.
1494 * @param pos Index of character to search from (default 0).
1495 * @return Index of start of first occurrence.
1497 * Starting from @a pos, searches forward for the value of @a s within
1498 * this string. If found, returns the index where it begins. If not
1499 * found, returns npos.
1502 find(const _CharT* __s, size_type __pos = 0) const
1504 __glibcxx_requires_string(__s);
1505 return this->find(__s, __pos, traits_type::length(__s));
1509 * @brief Find position of a character.
1510 * @param c Character to locate.
1511 * @param pos Index of character to search from (default 0).
1512 * @return Index of first occurrence.
1514 * Starting from @a pos, searches forward for @a c within this string.
1515 * If found, returns the index where it was found. If not found,
1519 find(_CharT __c, size_type __pos = 0) const;
1522 * @brief Find last position of a string.
1523 * @param str String to locate.
1524 * @param pos Index of character to search back from (default end).
1525 * @return Index of start of last occurrence.
1527 * Starting from @a pos, searches backward for value of @a str within
1528 * this string. If found, returns the index where it begins. If not
1529 * found, returns npos.
1532 rfind(const basic_string& __str, size_type __pos = npos) const
1533 { return this->rfind(__str.data(), __pos, __str.size()); }
1536 * @brief Find last position of a C substring.
1537 * @param s C string to locate.
1538 * @param pos Index of character to search back from.
1539 * @param n Number of characters from s to search for.
1540 * @return Index of start of last occurrence.
1542 * Starting from @a pos, searches backward for the first @a n
1543 * characters in @a s within this string. If found, returns the index
1544 * where it begins. If not found, returns npos.
1547 rfind(const _CharT* __s, size_type __pos, size_type __n) const;
1550 * @brief Find last position of a C string.
1551 * @param s C string to locate.
1552 * @param pos Index of character to start search at (default 0).
1553 * @return Index of start of last occurrence.
1555 * Starting from @a pos, searches backward for the value of @a s within
1556 * this string. If found, returns the index where it begins. If not
1557 * found, returns npos.
1560 rfind(const _CharT* __s, size_type __pos = npos) const
1562 __glibcxx_requires_string(__s);
1563 return this->rfind(__s, __pos, traits_type::length(__s));
1567 * @brief Find last position of a character.
1568 * @param c Character to locate.
1569 * @param pos Index of character to search back from (default 0).
1570 * @return Index of last occurrence.
1572 * Starting from @a pos, searches backward for @a c within this string.
1573 * If found, returns the index where it was found. If not found,
1577 rfind(_CharT __c, size_type __pos = npos) const;
1580 * @brief Find position of a character of string.
1581 * @param str String containing characters to locate.
1582 * @param pos Index of character to search from (default 0).
1583 * @return Index of first occurrence.
1585 * Starting from @a pos, searches forward for one of the characters of
1586 * @a str within this string. If found, returns the index where it was
1587 * found. If not found, returns npos.
1590 find_first_of(const basic_string& __str, size_type __pos = 0) const
1591 { return this->find_first_of(__str.data(), __pos, __str.size()); }
1594 * @brief Find position of a character of C substring.
1595 * @param s String containing characters to locate.
1596 * @param pos Index of character to search from (default 0).
1597 * @param n Number of characters from s to search for.
1598 * @return Index of first occurrence.
1600 * Starting from @a pos, searches forward for one of the first @a n
1601 * characters of @a s within this string. If found, returns the index
1602 * where it was found. If not found, returns npos.
1605 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
1608 * @brief Find position of a character of C string.
1609 * @param s String containing characters to locate.
1610 * @param pos Index of character to search from (default 0).
1611 * @return Index of first occurrence.
1613 * Starting from @a pos, searches forward for one of the characters of
1614 * @a s within this string. If found, returns the index where it was
1615 * found. If not found, returns npos.
1618 find_first_of(const _CharT* __s, size_type __pos = 0) const
1620 __glibcxx_requires_string(__s);
1621 return this->find_first_of(__s, __pos, traits_type::length(__s));
1625 * @brief Find position of a character.
1626 * @param c Character to locate.
1627 * @param pos Index of character to search from (default 0).
1628 * @return Index of first occurrence.
1630 * Starting from @a pos, searches forward for the character @a c within
1631 * this string. If found, returns the index where it was found. If
1632 * not found, returns npos.
1634 * Note: equivalent to find(c, pos).
1637 find_first_of(_CharT __c, size_type __pos = 0) const
1638 { return this->find(__c, __pos); }
1641 * @brief Find last position of a character of string.
1642 * @param str String containing characters to locate.
1643 * @param pos Index of character to search back from (default end).
1644 * @return Index of last occurrence.
1646 * Starting from @a pos, searches backward for one of the characters of
1647 * @a str within this string. If found, returns the index where it was
1648 * found. If not found, returns npos.
1651 find_last_of(const basic_string& __str, size_type __pos = npos) const
1652 { return this->find_last_of(__str.data(), __pos, __str.size()); }
1655 * @brief Find last position of a character of C substring.
1656 * @param s C string containing characters to locate.
1657 * @param pos Index of character to search back from (default end).
1658 * @param n Number of characters from s to search for.
1659 * @return Index of last occurrence.
1661 * Starting from @a pos, searches backward for one of the first @a n
1662 * characters of @a s within this string. If found, returns the index
1663 * where it was found. If not found, returns npos.
1666 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
1669 * @brief Find last position of a character of C string.
1670 * @param s C string containing characters to locate.
1671 * @param pos Index of character to search back from (default end).
1672 * @return Index of last occurrence.
1674 * Starting from @a pos, searches backward for one of the characters of
1675 * @a s within this string. If found, returns the index where it was
1676 * found. If not found, returns npos.
1679 find_last_of(const _CharT* __s, size_type __pos = npos) const
1681 __glibcxx_requires_string(__s);
1682 return this->find_last_of(__s, __pos, traits_type::length(__s));
1686 * @brief Find last position of a character.
1687 * @param c Character to locate.
1688 * @param pos Index of character to search back from (default 0).
1689 * @return Index of last occurrence.
1691 * Starting from @a pos, searches backward for @a c within this string.
1692 * If found, returns the index where it was found. If not found,
1695 * Note: equivalent to rfind(c, pos).
1698 find_last_of(_CharT __c, size_type __pos = npos) const
1699 { return this->rfind(__c, __pos); }
1702 * @brief Find position of a character not in string.
1703 * @param str String containing characters to avoid.
1704 * @param pos Index of character to search from (default 0).
1705 * @return Index of first occurrence.
1707 * Starting from @a pos, searches forward for a character not contained
1708 * in @a str within this string. If found, returns the index where it
1709 * was found. If not found, returns npos.
1712 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
1713 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
1716 * @brief Find position of a character not in C substring.
1717 * @param s C string containing characters to avoid.
1718 * @param pos Index of character to search from (default 0).
1719 * @param n Number of characters from s to consider.
1720 * @return Index of first occurrence.
1722 * Starting from @a pos, searches forward for a character not contained
1723 * in the first @a n characters of @a s within this string. If found,
1724 * returns the index where it was found. If not found, returns npos.
1727 find_first_not_of(const _CharT* __s, size_type __pos,
1728 size_type __n) const;
1731 * @brief Find position of a character not in C string.
1732 * @param s C string containing characters to avoid.
1733 * @param pos Index of character to search from (default 0).
1734 * @return Index of first occurrence.
1736 * Starting from @a pos, searches forward for a character not contained
1737 * in @a s within this string. If found, returns the index where it
1738 * was found. If not found, returns npos.
1741 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
1743 __glibcxx_requires_string(__s);
1744 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
1748 * @brief Find position of a different character.
1749 * @param c Character to avoid.
1750 * @param pos Index of character to search from (default 0).
1751 * @return Index of first occurrence.
1753 * Starting from @a pos, searches forward for a character other than @a c
1754 * within this string. If found, returns the index where it was found.
1755 * If not found, returns npos.
1758 find_first_not_of(_CharT __c, size_type __pos = 0) const;
1761 * @brief Find last position of a character not in string.
1762 * @param str String containing characters to avoid.
1763 * @param pos Index of character to search from (default 0).
1764 * @return Index of first occurrence.
1766 * Starting from @a pos, searches backward for a character not
1767 * contained in @a str within this string. If found, returns the index
1768 * where it was found. If not found, returns npos.
1771 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
1772 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
1775 * @brief Find last position of a character not in C substring.
1776 * @param s C string containing characters to avoid.
1777 * @param pos Index of character to search from (default 0).
1778 * @param n Number of characters from s to consider.
1779 * @return Index of first occurrence.
1781 * Starting from @a pos, searches backward for a character not
1782 * contained in the first @a n characters of @a s within this string.
1783 * If found, returns the index where it was found. If not found,
1787 find_last_not_of(const _CharT* __s, size_type __pos,
1788 size_type __n) const;
1790 * @brief Find position of a character not in C string.
1791 * @param s C string containing characters to avoid.
1792 * @param pos Index of character to search from (default 0).
1793 * @return Index of first occurrence.
1795 * Starting from @a pos, searches backward for a character not
1796 * contained in @a s within this string. If found, returns the index
1797 * where it was found. If not found, returns npos.
1800 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
1802 __glibcxx_requires_string(__s);
1803 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
1807 * @brief Find last position of a different character.
1808 * @param c Character to avoid.
1809 * @param pos Index of character to search from (default 0).
1810 * @return Index of first occurrence.
1812 * Starting from @a pos, searches backward for a character other than
1813 * @a c within this string. If found, returns the index where it was
1814 * found. If not found, returns npos.
1817 find_last_not_of(_CharT __c, size_type __pos = npos) const;
1820 * @brief Get a substring.
1821 * @param pos Index of first character (default 0).
1822 * @param n Number of characters in substring (default remainder).
1823 * @return The new string.
1824 * @throw std::out_of_range If pos > size().
1826 * Construct and return a new string using the @a n characters starting
1827 * at @a pos. If the string is too short, use the remainder of the
1828 * characters. If @a pos is beyond the end of the string, out_of_range
1832 substr(size_type __pos = 0, size_type __n = npos) const
1833 { return basic_string(*this,
1834 _M_check(__pos, "basic_string::substr"), __n); }
1837 * @brief Compare to a string.
1838 * @param str String to compare against.
1839 * @return Integer < 0, 0, or > 0.
1841 * Returns an integer < 0 if this string is ordered before @a str, 0 if
1842 * their values are equivalent, or > 0 if this string is ordered after
1843 * @a str. If the lengths of @a str and this string are different, the
1844 * shorter one is ordered first. If they are the same, returns the
1845 * result of traits::compare(data(),str.data(),size());
1848 compare(const basic_string& __str) const
1850 const size_type __size = this->size();
1851 const size_type __osize = __str.size();
1852 const size_type __len = std::min(__size, __osize);
1854 int __r = traits_type::compare(_M_data(), __str.data(), __len);
1856 __r = __size - __osize;
1861 * @brief Compare substring to a string.
1862 * @param pos Index of first character of substring.
1863 * @param n Number of characters in substring.
1864 * @param str String to compare against.
1865 * @return Integer < 0, 0, or > 0.
1867 * Form the substring of this string from the @a n characters starting
1868 * at @a pos. Returns an integer < 0 if the substring is ordered
1869 * before @a str, 0 if their values are equivalent, or > 0 if the
1870 * substring is ordered after @a str. If the lengths @a of str and the
1871 * substring are different, the shorter one is ordered first. If they
1872 * are the same, returns the result of
1873 * traits::compare(substring.data(),str.data(),size());
1876 compare(size_type __pos, size_type __n, const basic_string& __str) const;
1879 * @brief Compare substring to a substring.
1880 * @param pos1 Index of first character of substring.
1881 * @param n1 Number of characters in substring.
1882 * @param str String to compare against.
1883 * @param pos2 Index of first character of substring of str.
1884 * @param n2 Number of characters in substring of str.
1885 * @return Integer < 0, 0, or > 0.
1887 * Form the substring of this string from the @a n1 characters starting
1888 * at @a pos1. Form the substring of @a str from the @a n2 characters
1889 * starting at @a pos2. Returns an integer < 0 if this substring is
1890 * ordered before the substring of @a str, 0 if their values are
1891 * equivalent, or > 0 if this substring is ordered after the substring
1892 * of @a str. If the lengths of the substring of @a str and this
1893 * substring are different, the shorter one is ordered first. If they
1894 * are the same, returns the result of
1895 * traits::compare(substring.data(),str.substr(pos2,n2).data(),size());
1898 compare(size_type __pos1, size_type __n1, const basic_string& __str,
1899 size_type __pos2, size_type __n2) const;
1902 * @brief Compare to a C string.
1903 * @param s C string to compare against.
1904 * @return Integer < 0, 0, or > 0.
1906 * Returns an integer < 0 if this string is ordered before @a s, 0 if
1907 * their values are equivalent, or > 0 if this string is ordered after
1908 * @a s. If the lengths of @a s and this string are different, the
1909 * shorter one is ordered first. If they are the same, returns the
1910 * result of traits::compare(data(),s,size());
1913 compare(const _CharT* __s) const;
1915 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1916 // 5 String::compare specification questionable
1918 * @brief Compare substring to a C string.
1919 * @param pos Index of first character of substring.
1920 * @param n1 Number of characters in substring.
1921 * @param s C string to compare against.
1922 * @return Integer < 0, 0, or > 0.
1924 * Form the substring of this string from the @a n1 characters starting
1925 * at @a pos. Returns an integer < 0 if the substring is ordered
1926 * before @a s, 0 if their values are equivalent, or > 0 if the
1927 * substring is ordered after @a s. If the lengths of @a s and the
1928 * substring are different, the shorter one is ordered first. If they
1929 * are the same, returns the result of
1930 * traits::compare(substring.data(),s,size());
1933 compare(size_type __pos, size_type __n1, const _CharT* __s) const;
1936 * @brief Compare substring against a character array.
1937 * @param pos1 Index of first character of substring.
1938 * @param n1 Number of characters in substring.
1939 * @param s character array to compare against.
1940 * @param n2 Number of characters of s.
1941 * @return Integer < 0, 0, or > 0.
1943 * Form the substring of this string from the @a n1 characters starting
1944 * at @a pos1. Form a string from the first @a n2 characters of @a s.
1945 * Returns an integer < 0 if this substring is ordered before the string
1946 * from @a s, 0 if their values are equivalent, or > 0 if this substring
1947 * is ordered after the string from @a s. If the lengths of this
1948 * substring and @a n2 are different, the shorter one is ordered first.
1949 * If they are the same, returns the result of
1950 * traits::compare(substring.data(),s,size());
1952 * NB: s must have at least n2 characters, '\0' has no special
1956 compare(size_type __pos, size_type __n1, const _CharT* __s,
1957 size_type __n2) const;
1961 template<typename _CharT, typename _Traits, typename _Alloc>
1962 inline basic_string<_CharT, _Traits, _Alloc>::
1964 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
1968 * @brief Concatenate two strings.
1969 * @param lhs First string.
1970 * @param rhs Last string.
1971 * @return New string with value of @a lhs followed by @a rhs.
1973 template<typename _CharT, typename _Traits, typename _Alloc>
1974 basic_string<_CharT, _Traits, _Alloc>
1975 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
1976 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
1978 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
1979 __str.append(__rhs);
1984 * @brief Concatenate C string and string.
1985 * @param lhs First string.
1986 * @param rhs Last string.
1987 * @return New string with value of @a lhs followed by @a rhs.
1989 template<typename _CharT, typename _Traits, typename _Alloc>
1990 basic_string<_CharT,_Traits,_Alloc>
1991 operator+(const _CharT* __lhs,
1992 const basic_string<_CharT,_Traits,_Alloc>& __rhs);
1995 * @brief Concatenate character and string.
1996 * @param lhs First string.
1997 * @param rhs Last string.
1998 * @return New string with @a lhs followed by @a rhs.
2000 template<typename _CharT, typename _Traits, typename _Alloc>
2001 basic_string<_CharT,_Traits,_Alloc>
2002 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2005 * @brief Concatenate string and C string.
2006 * @param lhs First string.
2007 * @param rhs Last string.
2008 * @return New string with @a lhs followed by @a rhs.
2010 template<typename _CharT, typename _Traits, typename _Alloc>
2011 inline basic_string<_CharT, _Traits, _Alloc>
2012 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2013 const _CharT* __rhs)
2015 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2016 __str.append(__rhs);
2021 * @brief Concatenate string and character.
2022 * @param lhs First string.
2023 * @param rhs Last string.
2024 * @return New string with @a lhs followed by @a rhs.
2026 template<typename _CharT, typename _Traits, typename _Alloc>
2027 inline basic_string<_CharT, _Traits, _Alloc>
2028 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
2030 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
2031 typedef typename __string_type::size_type __size_type;
2032 __string_type __str(__lhs);
2033 __str.append(__size_type(1), __rhs);
2039 * @brief Test equivalence of two strings.
2040 * @param lhs First string.
2041 * @param rhs Second string.
2042 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2044 template<typename _CharT, typename _Traits, typename _Alloc>
2046 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2047 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2048 { return __lhs.compare(__rhs) == 0; }
2051 * @brief Test equivalence of C string and string.
2052 * @param lhs C string.
2053 * @param rhs String.
2054 * @return True if @a rhs.compare(@a lhs) == 0. False otherwise.
2056 template<typename _CharT, typename _Traits, typename _Alloc>
2058 operator==(const _CharT* __lhs,
2059 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2060 { return __rhs.compare(__lhs) == 0; }
2063 * @brief Test equivalence of string and C string.
2064 * @param lhs String.
2065 * @param rhs C string.
2066 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2068 template<typename _CharT, typename _Traits, typename _Alloc>
2070 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2071 const _CharT* __rhs)
2072 { return __lhs.compare(__rhs) == 0; }
2076 * @brief Test difference of two strings.
2077 * @param lhs First string.
2078 * @param rhs Second string.
2079 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2081 template<typename _CharT, typename _Traits, typename _Alloc>
2083 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2084 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2085 { return __rhs.compare(__lhs) != 0; }
2088 * @brief Test difference of C string and string.
2089 * @param lhs C string.
2090 * @param rhs String.
2091 * @return True if @a rhs.compare(@a lhs) != 0. False otherwise.
2093 template<typename _CharT, typename _Traits, typename _Alloc>
2095 operator!=(const _CharT* __lhs,
2096 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2097 { return __rhs.compare(__lhs) != 0; }
2100 * @brief Test difference of string and C string.
2101 * @param lhs String.
2102 * @param rhs C string.
2103 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2105 template<typename _CharT, typename _Traits, typename _Alloc>
2107 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2108 const _CharT* __rhs)
2109 { return __lhs.compare(__rhs) != 0; }
2113 * @brief Test if string precedes string.
2114 * @param lhs First string.
2115 * @param rhs Second string.
2116 * @return True if @a lhs precedes @a rhs. False otherwise.
2118 template<typename _CharT, typename _Traits, typename _Alloc>
2120 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2121 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2122 { return __lhs.compare(__rhs) < 0; }
2125 * @brief Test if string precedes C string.
2126 * @param lhs String.
2127 * @param rhs C string.
2128 * @return True if @a lhs precedes @a rhs. False otherwise.
2130 template<typename _CharT, typename _Traits, typename _Alloc>
2132 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2133 const _CharT* __rhs)
2134 { return __lhs.compare(__rhs) < 0; }
2137 * @brief Test if C string precedes string.
2138 * @param lhs C string.
2139 * @param rhs String.
2140 * @return True if @a lhs precedes @a rhs. False otherwise.
2142 template<typename _CharT, typename _Traits, typename _Alloc>
2144 operator<(const _CharT* __lhs,
2145 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2146 { return __rhs.compare(__lhs) > 0; }
2150 * @brief Test if string follows string.
2151 * @param lhs First string.
2152 * @param rhs Second string.
2153 * @return True if @a lhs follows @a rhs. False otherwise.
2155 template<typename _CharT, typename _Traits, typename _Alloc>
2157 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2158 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2159 { return __lhs.compare(__rhs) > 0; }
2162 * @brief Test if string follows C string.
2163 * @param lhs String.
2164 * @param rhs C string.
2165 * @return True if @a lhs follows @a rhs. False otherwise.
2167 template<typename _CharT, typename _Traits, typename _Alloc>
2169 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2170 const _CharT* __rhs)
2171 { return __lhs.compare(__rhs) > 0; }
2174 * @brief Test if C string follows string.
2175 * @param lhs C string.
2176 * @param rhs String.
2177 * @return True if @a lhs follows @a rhs. False otherwise.
2179 template<typename _CharT, typename _Traits, typename _Alloc>
2181 operator>(const _CharT* __lhs,
2182 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2183 { return __rhs.compare(__lhs) < 0; }
2187 * @brief Test if string doesn't follow string.
2188 * @param lhs First string.
2189 * @param rhs Second string.
2190 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2192 template<typename _CharT, typename _Traits, typename _Alloc>
2194 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2195 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2196 { return __lhs.compare(__rhs) <= 0; }
2199 * @brief Test if string doesn't follow C string.
2200 * @param lhs String.
2201 * @param rhs C string.
2202 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2204 template<typename _CharT, typename _Traits, typename _Alloc>
2206 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2207 const _CharT* __rhs)
2208 { return __lhs.compare(__rhs) <= 0; }
2211 * @brief Test if C string doesn't follow string.
2212 * @param lhs C string.
2213 * @param rhs String.
2214 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2216 template<typename _CharT, typename _Traits, typename _Alloc>
2218 operator<=(const _CharT* __lhs,
2219 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2220 { return __rhs.compare(__lhs) >= 0; }
2224 * @brief Test if string doesn't precede string.
2225 * @param lhs First string.
2226 * @param rhs Second string.
2227 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2229 template<typename _CharT, typename _Traits, typename _Alloc>
2231 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2232 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2233 { return __lhs.compare(__rhs) >= 0; }
2236 * @brief Test if string doesn't precede C string.
2237 * @param lhs String.
2238 * @param rhs C string.
2239 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2241 template<typename _CharT, typename _Traits, typename _Alloc>
2243 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2244 const _CharT* __rhs)
2245 { return __lhs.compare(__rhs) >= 0; }
2248 * @brief Test if C string doesn't precede string.
2249 * @param lhs C string.
2250 * @param rhs String.
2251 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2253 template<typename _CharT, typename _Traits, typename _Alloc>
2255 operator>=(const _CharT* __lhs,
2256 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2257 { return __rhs.compare(__lhs) <= 0; }
2260 * @brief Swap contents of two strings.
2261 * @param lhs First string.
2262 * @param rhs Second string.
2264 * Exchanges the contents of @a lhs and @a rhs in constant time.
2266 template<typename _CharT, typename _Traits, typename _Alloc>
2268 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
2269 basic_string<_CharT, _Traits, _Alloc>& __rhs)
2270 { __lhs.swap(__rhs); }
2273 * @brief Read stream into a string.
2274 * @param is Input stream.
2275 * @param str Buffer to store into.
2276 * @return Reference to the input stream.
2278 * Stores characters from @a is into @a str until whitespace is found, the
2279 * end of the stream is encountered, or str.max_size() is reached. If
2280 * is.width() is non-zero, that is the limit on the number of characters
2281 * stored into @a str. Any previous contents of @a str are erased.
2283 template<typename _CharT, typename _Traits, typename _Alloc>
2284 basic_istream<_CharT, _Traits>&
2285 operator>>(basic_istream<_CharT, _Traits>& __is,
2286 basic_string<_CharT, _Traits, _Alloc>& __str);
2289 * @brief Write string to a stream.
2290 * @param os Output stream.
2291 * @param str String to write out.
2292 * @return Reference to the output stream.
2294 * Output characters of @a str into os following the same rules as for
2295 * writing a C string.
2297 template<typename _CharT, typename _Traits, typename _Alloc>
2298 basic_ostream<_CharT, _Traits>&
2299 operator<<(basic_ostream<_CharT, _Traits>& __os,
2300 const basic_string<_CharT, _Traits, _Alloc>& __str);
2303 * @brief Read a line from stream into a string.
2304 * @param is Input stream.
2305 * @param str Buffer to store into.
2306 * @param delim Character marking end of line.
2307 * @return Reference to the input stream.
2309 * Stores characters from @a is into @a str until @a delim is found, the
2310 * end of the stream is encountered, or str.max_size() is reached. If
2311 * is.width() is non-zero, that is the limit on the number of characters
2312 * stored into @a str. Any previous contents of @a str are erased. If @a
2313 * delim was encountered, it is extracted but not stored into @a str.
2315 template<typename _CharT, typename _Traits, typename _Alloc>
2316 basic_istream<_CharT,_Traits>&
2317 getline(basic_istream<_CharT, _Traits>& __is,
2318 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
2321 * @brief Read a line from stream into a string.
2322 * @param is Input stream.
2323 * @param str Buffer to store into.
2324 * @return Reference to the input stream.
2326 * Stores characters from is into @a str until '\n' is found, the end of
2327 * the stream is encountered, or str.max_size() is reached. If is.width()
2328 * is non-zero, that is the limit on the number of characters stored into
2329 * @a str. Any previous contents of @a str are erased. If end of line was
2330 * encountered, it is extracted but not stored into @a str.
2332 template<typename _CharT, typename _Traits, typename _Alloc>
2333 inline basic_istream<_CharT,_Traits>&
2334 getline(basic_istream<_CharT, _Traits>& __is,
2335 basic_string<_CharT, _Traits, _Alloc>& __str);
2338 #endif /* _BASIC_STRING_H */