1 // Components for manipulating sequences of characters -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
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>
50 * @class basic_string basic_string.h <string>
51 * @brief Managing sequences of characters and character-like objects.
56 * Meets the requirements of a <a href="tables.html#65">container</a>, a
57 * <a href="tables.html#66">reversible container</a>, and a
58 * <a href="tables.html#67">sequence</a>. Of the
59 * <a href="tables.html#68">optional sequence requirements</a>, only
60 * @c push_back, @c at, and array access are supported.
66 * Documentation? What's that?
67 * Nathan Myers <ncm@cantrip.org>.
69 * A string looks like this:
74 * [basic_string<char_type>] _M_capacity
75 * _M_dataplus _M_state
76 * _M_p ----------------> unnamed array of char_type
79 * Where the _M_p points to the first character in the string, and
80 * you cast it to a pointer-to-_Rep and subtract 1 to get a
81 * pointer to the header.
83 * This approach has the enormous advantage that a string object
84 * requires only one allocation. All the ugliness is confined
85 * within a single pair of inline functions, which each compile to
86 * a single "add" instruction: _Rep::_M_data(), and
87 * string::_M_rep(); and the allocation function which gets a
88 * block of raw bytes and with room enough and constructs a _Rep
89 * object at the front.
91 * The reason you want _M_data pointing to the character array and
92 * not the _Rep is so that the debugger can see the string
93 * contents. (Probably we should add a non-inline member to get
94 * the _Rep for the debugger to use, so users can check the actual
97 * Note that the _Rep object is a POD so that you can have a
98 * static "empty string" _Rep object already "constructed" before
99 * static constructors have run. The reference-count encoding is
100 * chosen so that a 0 indicates one reference, so you never try to
101 * destroy the empty-string _Rep object.
103 * All but the last paragraph is considered pretty conventional
104 * for a C++ string implementation.
107 // 21.3 Template class basic_string
108 template<typename _CharT, typename _Traits, typename _Alloc>
113 typedef _Traits traits_type;
114 typedef typename _Traits::char_type value_type;
115 typedef _Alloc allocator_type;
116 typedef typename _Alloc::size_type size_type;
117 typedef typename _Alloc::difference_type difference_type;
118 typedef typename _Alloc::reference reference;
119 typedef typename _Alloc::const_reference const_reference;
120 typedef typename _Alloc::pointer pointer;
121 typedef typename _Alloc::const_pointer const_pointer;
122 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
123 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
125 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
126 typedef std::reverse_iterator<iterator> reverse_iterator;
129 // _Rep: string representation
131 // 1. String really contains _M_length + 1 characters; last is set
132 // to 0 only on call to c_str(). We avoid instantiating
133 // _CharT() where the interface does not require it.
134 // 2. _M_capacity >= _M_length
135 // Allocated memory is always _M_capacity + (1 * sizeof(_CharT)).
136 // 3. _M_references 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_references;
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_references < 0; }
186 { return this->_M_references > 0; }
190 { this->_M_references = -1; }
194 { this->_M_references = 0; }
198 { return reinterpret_cast<_CharT*>(this + 1); }
201 operator[](size_t __s) throw()
202 { return _M_refdata() [__s]; }
205 _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
207 return (!_M_is_leaked() && __alloc1 == __alloc2)
208 ? _M_refcopy() : _M_clone(__alloc1);
213 _S_create(size_t, const _Alloc&);
216 _M_dispose(const _Alloc& __a)
218 if (__builtin_expect(this != &_S_empty_rep(), false))
219 if (__exchange_and_add(&this->_M_references, -1) <= 0)
224 _M_destroy(const _Alloc&) throw();
229 if (__builtin_expect(this != &_S_empty_rep(), false))
230 __atomic_add(&this->_M_references, 1);
235 _M_clone(const _Alloc&, size_type __res = 0);
238 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
239 struct _Alloc_hider : _Alloc
241 _Alloc_hider(_CharT* __dat, const _Alloc& __a)
242 : _Alloc(__a), _M_p(__dat) { }
244 _CharT* _M_p; // The actual data.
248 // Data Members (public):
249 // NB: This is an unsigned type, and thus represents the maximum
250 // size that the allocator can hold.
252 /// Value returned by various member functions when they fail.
253 static const size_type npos = static_cast<size_type>(-1);
256 // Data Members (private):
257 mutable _Alloc_hider _M_dataplus;
261 { return _M_dataplus._M_p; }
265 { return (_M_dataplus._M_p = __p); }
269 { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
271 // For the internal use we have functions similar to `begin'/`end'
272 // but they do not call _M_leak.
274 _M_ibegin() const { return iterator(_M_data()); }
277 _M_iend() const { return iterator(_M_data() + this->size()); }
280 _M_leak() // for use in begin() & non-const op[]
282 if (!_M_rep()->_M_is_leaked())
287 _M_check(size_type __pos) const
289 if (__pos > this->size())
290 __throw_out_of_range(__N("basic_string::_M_check"));
291 return _M_ibegin() + __pos;
294 // NB: _M_fold doesn't check for a bad __pos1 value.
296 _M_fold(size_type __pos, size_type __off) const
298 const bool __testoff = __off < this->size() - __pos;
299 const size_type __newoff = __testoff ? __off : this->size() - __pos;
300 return (_M_ibegin() + __pos + __newoff);
303 // _S_copy_chars is a separate template to permit specialization
304 // to optimize for the common case of pointers as iterators.
305 template<class _Iterator>
307 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
309 for (; __k1 != __k2; ++__k1, ++__p)
310 traits_type::assign(*__p, *__k1); // These types are off.
314 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2)
315 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
318 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
319 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
322 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
323 { traits_type::copy(__p, __k1, __k2 - __k1); }
326 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
327 { traits_type::copy(__p, __k1, __k2 - __k1); }
330 _M_mutate(size_type __pos, size_type __len1, size_type __len2);
337 { return _Rep::_S_empty_rep(); }
340 // Construct/copy/destroy:
341 // NB: We overload ctors in some cases instead of using default
342 // arguments, per 17.4.4.4 para. 2 item 2.
345 * @brief Default constructor creates an empty string.
351 * @brief Construct an empty string using allocator a.
354 basic_string(const _Alloc& __a);
356 // NB: per LWG issue 42, semantics different from IS:
358 * @brief Construct string with copy of value of @a str.
359 * @param str Source string.
361 basic_string(const basic_string& __str);
363 * @brief Construct string as copy of a substring.
364 * @param str Source string.
365 * @param pos Index of first character to copy from.
366 * @param n Number of characters to copy (default remainder).
368 basic_string(const basic_string& __str, size_type __pos,
369 size_type __n = npos);
371 * @brief Construct string as copy of a substring.
372 * @param str Source string.
373 * @param pos Index of first character to copy from.
374 * @param n Number of characters to copy.
375 * @param a Allocator to use.
377 basic_string(const basic_string& __str, size_type __pos,
378 size_type __n, const _Alloc& __a);
381 * @brief Construct string as copy of a C substring.
382 * @param s Source C string.
383 * @param n Number of characters to copy.
384 * @param a Allocator to use (default is default allocator).
386 basic_string(const _CharT* __s, size_type __n,
387 const _Alloc& __a = _Alloc());
389 * @brief Construct string as copy of a C string.
390 * @param s Source C string.
391 * @param a Allocator to use (default is default allocator).
393 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
395 * @brief Construct string as multiple characters.
396 * @param n Number of characters.
397 * @param c Character to use.
398 * @param a Allocator to use (default is default allocator).
400 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
403 * @brief Construct string as copy of a range.
404 * @param beg Start of range.
405 * @param end End of range.
406 * @param a Allocator to use (default is default allocator).
408 template<class _InputIterator>
409 basic_string(_InputIterator __beg, _InputIterator __end,
410 const _Alloc& __a = _Alloc());
413 * @brief Destroy the string instance.
416 { _M_rep()->_M_dispose(this->get_allocator()); }
419 * @brief Assign the value of @a str to this string.
420 * @param str Source string.
423 operator=(const basic_string& __str) { return this->assign(__str); }
426 * @brief Copy contents of @a s into this string.
427 * @param s Source null-terminated string.
430 operator=(const _CharT* __s) { 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) { return this->assign(1, __c); }
444 * Returns a read/write iterator that points to the first character in
445 * the %string. Unshares the string.
451 return iterator(_M_data());
455 * Returns a read-only (constant) iterator that points to the first
456 * character in the %string.
460 { return const_iterator(_M_data()); }
463 * Returns a read/write iterator that points one past the last
464 * character in the %string. Unshares the string.
470 return iterator(_M_data() + this->size());
474 * Returns a read-only (constant) iterator that points one past the
475 * last character in the %string.
479 { return const_iterator(_M_data() + this->size()); }
482 * Returns a read/write reverse iterator that points to the last
483 * character in the %string. Iteration is done in reverse element
484 * order. Unshares the string.
488 { return reverse_iterator(this->end()); }
491 * Returns a read-only (constant) reverse iterator that points
492 * to the last character in the %string. Iteration is done in
493 * reverse element order.
495 const_reverse_iterator
497 { return const_reverse_iterator(this->end()); }
500 * Returns a read/write reverse iterator that points to one before the
501 * first character in the %string. Iteration is done in reverse
502 * element order. Unshares the string.
506 { return reverse_iterator(this->begin()); }
509 * Returns a read-only (constant) reverse iterator that points
510 * to one before the first character in the %string. Iteration
511 * is done in reverse element order.
513 const_reverse_iterator
515 { return const_reverse_iterator(this->begin()); }
519 /// Returns the number of characters in the string, not including any
520 /// null-termination.
522 size() const { return _M_rep()->_M_length; }
524 /// Returns the number of characters in the string, not including any
525 /// null-termination.
527 length() const { return _M_rep()->_M_length; }
529 /// Returns the size() of the largest possible %string.
531 max_size() const { return _Rep::_S_max_size; }
534 * @brief Resizes the %string to the specified number of characters.
535 * @param n Number of characters the %string should contain.
536 * @param c Character to fill any new elements.
538 * This function will %resize the %string to the specified
539 * number of characters. If the number is smaller than the
540 * %string's current size the %string is truncated, otherwise
541 * the %string is extended and new elements are set to @a c.
544 resize(size_type __n, _CharT __c);
547 * @brief Resizes the %string to the specified number of characters.
548 * @param n Number of characters the %string should contain.
550 * This function will resize the %string to the specified length. If
551 * the new size is smaller than the %string's current size the %string
552 * is truncated, otherwise the %string is extended and new characters
553 * are default-constructed. For basic types such as char, this means
557 resize(size_type __n) { this->resize(__n, _CharT()); }
560 * Returns the total number of characters that the %string can hold
561 * before needing to allocate more memory.
564 capacity() const { return _M_rep()->_M_capacity; }
567 * @brief Attempt to preallocate enough memory for specified number of
569 * @param n Number of characters required.
570 * @throw std::length_error If @a n exceeds @c max_size().
572 * This function attempts to reserve enough memory for the
573 * %string to hold the specified number of characters. If the
574 * number requested is more than max_size(), length_error is
577 * The advantage of this function is that if optimal code is a
578 * necessity and the user can determine the string length that will be
579 * required, the user can reserve the memory in %advance, and thus
580 * prevent a possible reallocation of memory and copying of %string
584 reserve(size_type __res_arg = 0);
587 * Erases the string, making it empty.
590 clear() { _M_mutate(0, this->size(), 0); }
593 * Returns true if the %string is empty. Equivalent to *this == "".
596 empty() const { return this->size() == 0; }
600 * @brief Subscript access to the data contained in the %string.
601 * @param n The index of the character to access.
602 * @return Read-only (constant) reference to the character.
604 * This operator allows for easy, array-style, data access.
605 * Note that data access with this operator is unchecked and
606 * out_of_range lookups are not defined. (For checked lookups
610 operator[] (size_type __pos) const
611 { return _M_data()[__pos]; }
614 * @brief Subscript access to the data contained in the %string.
615 * @param n The index of the character to access.
616 * @return Read/write reference to the character.
618 * This operator allows for easy, array-style, data access.
619 * Note that data access with this operator is unchecked and
620 * out_of_range lookups are not defined. (For checked lookups
621 * see at().) Unshares the string.
624 operator[](size_type __pos)
627 return _M_data()[__pos];
631 * @brief Provides access to the data contained in the %string.
632 * @param n The index of the character to access.
633 * @return Read-only (const) reference to the character.
634 * @throw std::out_of_range If @a n is an invalid index.
636 * This function provides for safer data access. The parameter is
637 * first checked that it is in the range of the string. The function
638 * throws out_of_range if the check fails.
641 at(size_type __n) const
643 if (__n >= this->size())
644 __throw_out_of_range(__N("basic_string::at"));
645 return _M_data()[__n];
649 * @brief Provides access to the data contained in the %string.
650 * @param n The index of the character to access.
651 * @return Read/write reference to the character.
652 * @throw std::out_of_range If @a n is an invalid index.
654 * This function provides for safer data access. The parameter is
655 * first checked that it is in the range of the string. The function
656 * throws out_of_range if the check fails. Success results in
657 * unsharing the string.
663 __throw_out_of_range(__N("basic_string::at"));
665 return _M_data()[__n];
670 * @brief Append a string to this string.
671 * @param str The string to append.
672 * @return Reference to this string.
675 operator+=(const basic_string& __str) { return this->append(__str); }
678 * @brief Append a C string.
679 * @param s The C string to append.
680 * @return Reference to this string.
683 operator+=(const _CharT* __s) { return this->append(__s); }
686 * @brief Append a character.
687 * @param s The character to append.
688 * @return Reference to this string.
691 operator+=(_CharT __c) { return this->append(size_type(1), __c); }
694 * @brief Append a string to this string.
695 * @param str The string to append.
696 * @return Reference to this string.
699 append(const basic_string& __str);
702 * @brief Append a substring.
703 * @param str The string to append.
704 * @param pos Index of the first character of str to append.
705 * @param n The number of characters to append.
706 * @return Reference to this string.
707 * @throw std::out_of_range if @a pos is not a valid index.
709 * This function appends @a n characters from @a str starting at @a pos
710 * to this string. If @a n is is larger than the number of available
711 * characters in @a str, the remainder of @a str is appended.
714 append(const basic_string& __str, size_type __pos, size_type __n);
717 * @brief Append a C substring.
718 * @param s The C string to append.
719 * @param n The number of characters to append.
720 * @return Reference to this string.
723 append(const _CharT* __s, size_type __n);
726 * @brief Append a C string.
727 * @param s The C string to append.
728 * @return Reference to this string.
731 append(const _CharT* __s)
732 { return this->append(__s, traits_type::length(__s)); }
735 * @brief Append multiple characters.
736 * @param n The number of characters to append.
737 * @param c The character to use.
738 * @return Reference to this string.
740 * Appends n copies of c to this string.
743 append(size_type __n, _CharT __c);
746 * @brief Append a range of characters.
747 * @param first Iterator referencing the first character to append.
748 * @param last Iterator marking the end of the range.
749 * @return Reference to this string.
751 * Appends characters in the range [first,last) to this string.
753 template<class _InputIterator>
755 append(_InputIterator __first, _InputIterator __last)
756 { return this->replace(_M_iend(), _M_iend(), __first, __last); }
759 * @brief Append a single character.
760 * @param c Character to append.
763 push_back(_CharT __c)
764 { this->replace(_M_iend(), _M_iend(), 1, __c); }
767 * @brief Set value to contents of another string.
768 * @param str Source string to use.
769 * @return Reference to this string.
772 assign(const basic_string& __str);
775 * @brief Set value to a substring of a string.
776 * @param str The string to use.
777 * @param pos Index of the first character of str.
778 * @param n Number of characters to use.
779 * @return Reference to this string.
780 * @throw std::out_of_range if @a pos is not a valid index.
782 * This function sets this string to the substring of @a str consisting
783 * of @a n characters at @a pos. If @a n is is larger than the number
784 * of available characters in @a str, the remainder of @a str is used.
787 assign(const basic_string& __str, size_type __pos, size_type __n);
790 * @brief Set value to a C substring.
791 * @param s The C string to use.
792 * @param n Number of characters to use.
793 * @return Reference to this string.
795 * This function sets the value of this string to the first @a n
796 * characters of @a s. If @a n is is larger than the number of
797 * available characters in @a s, the remainder of @a s is used.
800 assign(const _CharT* __s, size_type __n);
803 * @brief Set value to contents of a C string.
804 * @param s The C string to use.
805 * @return Reference to this string.
807 * This function sets the value of this string to the value of @a s.
808 * The data is copied, so there is no dependence on @a s once the
812 assign(const _CharT* __s)
813 { return this->assign(__s, traits_type::length(__s)); }
816 * @brief Set value to multiple characters.
817 * @param n Length of the resulting string.
818 * @param c The character to use.
819 * @return Reference to this string.
821 * This function sets the value of this string to @a n copies of
825 assign(size_type __n, _CharT __c)
826 { return this->replace(_M_ibegin(), _M_iend(), __n, __c); }
829 * @brief Set value to a range of characters.
830 * @param first Iterator referencing the first character to append.
831 * @param last Iterator marking the end of the range.
832 * @return Reference to this string.
834 * Sets value of string to characters in the range [first,last).
836 template<class _InputIterator>
838 assign(_InputIterator __first, _InputIterator __last)
839 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
842 * @brief Insert multiple characters.
843 * @param p Iterator referencing location in string to insert at.
844 * @param n Number of characters to insert
845 * @param c The character to insert.
846 * @throw std::length_error If new length exceeds @c max_size().
848 * Inserts @a n copies of character @a c starting at the position
849 * referenced by iterator @a p. If adding characters causes the length
850 * to exceed max_size(), length_error is thrown. The value of the
851 * string doesn't change if an error is thrown.
854 insert(iterator __p, size_type __n, _CharT __c)
855 { this->replace(__p, __p, __n, __c); }
858 * @brief Insert a range of characters.
859 * @param p Iterator referencing location in string to insert at.
860 * @param beg Start of range.
861 * @param end End of range.
862 * @throw std::length_error If new length exceeds @c max_size().
864 * Inserts characters in range [beg,end). If adding characters causes
865 * the length to exceed max_size(), length_error is thrown. The value
866 * of the string doesn't change if an error is thrown.
868 template<class _InputIterator>
869 void insert(iterator __p, _InputIterator __beg, _InputIterator __end)
870 { this->replace(__p, __p, __beg, __end); }
873 * @brief Insert value of a string.
874 * @param pos1 Iterator referencing location in string to insert at.
875 * @param str The string to insert.
876 * @return Reference to this string.
877 * @throw std::length_error If new length exceeds @c max_size().
879 * Inserts value of @a str starting at @a pos1. If adding characters
880 * causes the length to exceed max_size(), length_error is thrown. The
881 * value of the string doesn't change if an error is thrown.
884 insert(size_type __pos1, const basic_string& __str)
885 { return this->insert(__pos1, __str, 0, __str.size()); }
888 * @brief Insert a substring.
889 * @param pos1 Iterator referencing location in string to insert at.
890 * @param str The string to insert.
891 * @param pos2 Start of characters in str to insert.
892 * @param n Number of characters to insert.
893 * @return Reference to this string.
894 * @throw std::length_error If new length exceeds @c max_size().
895 * @throw std::out_of_range If @a pos1 > size() or
896 * @a pos2 > @a str.size().
898 * Starting at @a pos1, insert @a n character of @a str beginning with
899 * @a pos2. If adding characters causes the length to exceed
900 * max_size(), length_error is thrown. If @a pos1 is beyond the end of
901 * this string or @a pos2 is beyond the end of @a str, out_of_range is
902 * thrown. The value of the string doesn't change if an error is
906 insert(size_type __pos1, const basic_string& __str,
907 size_type __pos2, size_type __n);
910 * @brief Insert a C substring.
911 * @param pos Iterator referencing location in string to insert at.
912 * @param s The C string to insert.
913 * @param n The number of characters to insert.
914 * @return Reference to this string.
915 * @throw std::length_error If new length exceeds @c max_size().
916 * @throw std::out_of_range If @a pos is beyond the end of this
919 * Inserts the first @a n characters of @a s starting at @a pos. If
920 * adding characters causes the length to exceed max_size(),
921 * length_error is thrown. If @a pos is beyond end(), out_of_range is
922 * thrown. The value of the string doesn't change if an error is
926 insert(size_type __pos, const _CharT* __s, size_type __n);
929 * @brief Insert a C string.
930 * @param pos Iterator referencing location in string to insert at.
931 * @param s The C string to insert.
932 * @return Reference to this string.
933 * @throw std::length_error If new length exceeds @c max_size().
934 * @throw std::out_of_range If @a pos is beyond the end of this
937 * Inserts the first @a n characters of @a s starting at @a pos. If
938 * adding characters causes the length to exceed max_size(),
939 * length_error is thrown. If @a pos is beyond end(), out_of_range is
940 * thrown. The value of the string doesn't change if an error is
944 insert(size_type __pos, const _CharT* __s)
945 { return this->insert(__pos, __s, traits_type::length(__s)); }
948 * @brief Insert multiple characters.
949 * @param pos Index in string to insert at.
950 * @param n Number of characters to insert
951 * @param c The character to insert.
952 * @return Reference to this string.
953 * @throw std::length_error If new length exceeds @c max_size().
954 * @throw std::out_of_range If @a pos is beyond the end of this
957 * Inserts @a n copies of character @a c starting at index @a pos. If
958 * adding characters causes the length to exceed max_size(),
959 * length_error is thrown. If @a pos > length(), out_of_range is
960 * thrown. The value of the string doesn't change if an error is
964 insert(size_type __pos, size_type __n, _CharT __c)
966 this->insert(_M_check(__pos), __n, __c);
971 * @brief Insert one character.
972 * @param p Iterator referencing position in string to insert at.
973 * @param c The character to insert.
974 * @return Iterator referencing newly inserted char.
975 * @throw std::length_error If new length exceeds @c max_size().
976 * @throw std::out_of_range If @a p is beyond the end of this string.
978 * Inserts character @a c at position referenced by @a p. If adding
979 * character causes the length to exceed max_size(), length_error is
980 * thrown. If @a p is beyond end of string, out_of_range is thrown.
981 * The value of the string doesn't change if an error is thrown.
984 insert(iterator __p, _CharT __c = _CharT())
986 const size_type __pos = __p - _M_ibegin();
987 this->insert(_M_check(__pos), size_type(1), __c);
988 _M_rep()->_M_set_leaked();
989 return this->_M_ibegin() + __pos;
993 * @brief Remove characters.
994 * @param pos Index of first character to remove (default 0).
995 * @param n Number of characters to remove (default remainder).
996 * @return Reference to this string.
997 * @throw std::out_of_range If @a pos is beyond the end of this
1000 * Removes @a n characters from this string starting at @a pos. The
1001 * length of the string is reduced by @a n. If there are < @a n
1002 * characters to remove, the remainder of the string is truncated. If
1003 * @a p is beyond end of string, out_of_range is thrown. The value of
1004 * the string doesn't change if an error is thrown.
1007 erase(size_type __pos = 0, size_type __n = npos)
1009 return this->replace(_M_check(__pos), _M_fold(__pos, __n),
1010 _M_data(), _M_data());
1014 * @brief Remove one character.
1015 * @param position Iterator referencing the character to remove.
1016 * @return iterator referencing same location after removal.
1017 * @throw std::out_of_range If @a position is beyond the end of this
1020 * Removes the character at @a position from this string. If @a
1021 * position is beyond end of string, out_of_range is thrown. The value
1022 * of the string doesn't change if an error is thrown.
1025 erase(iterator __position)
1027 const size_type __i = __position - _M_ibegin();
1028 this->replace(__position, __position + 1, _M_data(), _M_data());
1029 _M_rep()->_M_set_leaked();
1030 return _M_ibegin() + __i;
1034 * @brief Remove a range of characters.
1035 * @param first Iterator referencing the first character to remove.
1036 * @param last Iterator referencing the end of the range.
1037 * @return Iterator referencing location of first after removal.
1038 * @throw std::out_of_range If @a first is beyond the end of this
1041 * Removes the characters in the range [first,last) from this string.
1042 * If @a first is beyond end of string, out_of_range is thrown. The
1043 * value of the string doesn't change if an error is thrown.
1046 erase(iterator __first, iterator __last)
1048 const size_type __i = __first - _M_ibegin();
1049 this->replace(__first, __last, _M_data(), _M_data());
1050 _M_rep()->_M_set_leaked();
1051 return _M_ibegin() + __i;
1055 * @brief Replace characters with value from another string.
1056 * @param pos Index of first character to replace.
1057 * @param n Number of characters to be replaced.
1058 * @param str String to insert.
1059 * @return Reference to this string.
1060 * @throw std::out_of_range If @a pos is beyond the end of this
1062 * @throw std::length_error If new length exceeds @c max_size().
1064 * Removes the characters in the range [pos,pos+n) from this string.
1065 * In place, the value of @a str is inserted. If @a pos is beyond end
1066 * of string, out_of_range is thrown. If the length of the result
1067 * exceeds max_size(), length_error is thrown. The value of the string
1068 * doesn't change if an error is thrown.
1071 replace(size_type __pos, size_type __n, const basic_string& __str)
1072 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1075 * @brief Replace characters with value from another string.
1076 * @param pos1 Index of first character to replace.
1077 * @param n1 Number of characters to be replaced.
1078 * @param str String to insert.
1079 * @param pos2 Index of first character of str to use.
1080 * @param n2 Number of characters from str to use.
1081 * @return Reference to this string.
1082 * @throw std::out_of_range If @a pos1 > size() or @a pos2 >
1084 * @throw std::length_error If new length exceeds @c max_size().
1086 * Removes the characters in the range [pos1,pos1 + n) from this
1087 * string. In place, the value of @a str is inserted. If @a pos is
1088 * beyond end of string, out_of_range is thrown. If the length of the
1089 * result exceeds max_size(), length_error is thrown. The value of the
1090 * string doesn't change if an error is thrown.
1093 replace(size_type __pos1, size_type __n1, const basic_string& __str,
1094 size_type __pos2, size_type __n2);
1097 * @brief Replace characters with value of a C substring.
1098 * @param pos Index of first character to replace.
1099 * @param n1 Number of characters to be replaced.
1100 * @param str C string to insert.
1101 * @param n2 Number of characters from str to use.
1102 * @return Reference to this string.
1103 * @throw std::out_of_range If @a pos1 > size().
1104 * @throw std::length_error If new length exceeds @c max_size().
1106 * Removes the characters in the range [pos,pos + n1) from this string.
1107 * In place, the first @a n2 characters of @a str are inserted, or all
1108 * of @a str if @a n2 is too large. If @a pos is beyond end of string,
1109 * out_of_range is thrown. If the length of result exceeds max_size(),
1110 * length_error is thrown. The value of the string doesn't change if
1111 * an error is thrown.
1114 replace(size_type __pos, size_type __n1, const _CharT* __s,
1118 * @brief Replace characters with value of a C string.
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 * @return Reference to this string.
1123 * @throw std::out_of_range If @a pos > size().
1124 * @throw std::length_error If new length exceeds @c max_size().
1126 * Removes the characters in the range [pos,pos + n1) from this string.
1127 * In place, the first @a n characters of @a str are inserted. If @a
1128 * pos is beyond end of string, out_of_range is thrown. If the length
1129 * of result exceeds max_size(), length_error is thrown. The value of
1130 * the string doesn't change if an error is thrown.
1133 replace(size_type __pos, size_type __n1, const _CharT* __s)
1134 { return this->replace(__pos, __n1, __s, traits_type::length(__s)); }
1137 * @brief Replace characters with multiple characters.
1138 * @param pos Index of first character to replace.
1139 * @param n1 Number of characters to be replaced.
1140 * @param n2 Number of characters to insert.
1141 * @param c Character to insert.
1142 * @return Reference to this string.
1143 * @throw std::out_of_range If @a pos > size().
1144 * @throw std::length_error If new length exceeds @c max_size().
1146 * Removes the characters in the range [pos,pos + n1) from this string.
1147 * In place, @a n2 copies of @a c are inserted. If @a pos is beyond
1148 * end of string, out_of_range is thrown. If the length of result
1149 * exceeds max_size(), length_error is thrown. The value of the string
1150 * doesn't change if an error is thrown.
1153 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1154 { return this->replace(_M_check(__pos), _M_fold(__pos, __n1), __n2, __c); }
1157 * @brief Replace range of characters with string.
1158 * @param i1 Iterator referencing start of range to replace.
1159 * @param i2 Iterator referencing end of range to replace.
1160 * @param str String value to insert.
1161 * @return Reference to this string.
1162 * @throw std::length_error If new length exceeds @c max_size().
1164 * Removes the characters in the range [i1,i2). In place, the value of
1165 * @a str is inserted. If the length of result exceeds max_size(),
1166 * length_error is thrown. The value of the string doesn't change if
1167 * an error is thrown.
1170 replace(iterator __i1, iterator __i2, const basic_string& __str)
1171 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
1174 * @brief Replace range of characters with C substring.
1175 * @param i1 Iterator referencing start of range to replace.
1176 * @param i2 Iterator referencing end of range to replace.
1177 * @param s C string value to insert.
1178 * @param n Number of characters from s to insert.
1179 * @return Reference to this string.
1180 * @throw std::length_error If new length exceeds @c max_size().
1182 * Removes the characters in the range [i1,i2). In place, the first @a
1183 * n characters of @a s are inserted. If the length of result exceeds
1184 * max_size(), length_error is thrown. The value of the string doesn't
1185 * change if an error is thrown.
1188 replace(iterator __i1, iterator __i2,
1189 const _CharT* __s, size_type __n)
1190 { return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n); }
1193 * @brief Replace range of characters with C string.
1194 * @param i1 Iterator referencing start of range to replace.
1195 * @param i2 Iterator referencing end of range to replace.
1196 * @param s C string value to insert.
1197 * @return Reference to this string.
1198 * @throw std::length_error If new length exceeds @c max_size().
1200 * Removes the characters in the range [i1,i2). In place, the
1201 * characters of @a s are inserted. If the length of result exceeds
1202 * max_size(), length_error is thrown. The value of the string doesn't
1203 * change if an error is thrown.
1206 replace(iterator __i1, iterator __i2, const _CharT* __s)
1207 { return this->replace(__i1, __i2, __s, traits_type::length(__s)); }
1210 * @brief Replace range of characters with multiple characters
1211 * @param i1 Iterator referencing start of range to replace.
1212 * @param i2 Iterator referencing end of range to replace.
1213 * @param n Number of characters to insert.
1214 * @param c Character to insert.
1215 * @return Reference to this string.
1216 * @throw std::length_error If new length exceeds @c max_size().
1218 * Removes the characters in the range [i1,i2). In place, @a n copies
1219 * of @a c are inserted. If the length of result exceeds max_size(),
1220 * length_error is thrown. The value of the string doesn't change if
1221 * an error is thrown.
1224 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
1225 { return _M_replace_aux(__i1, __i2, __n, __c); }
1228 * @brief Replace range of characters with range.
1229 * @param i1 Iterator referencing start of range to replace.
1230 * @param i2 Iterator referencing end of range to replace.
1231 * @param k1 Iterator referencing start of range to insert.
1232 * @param k2 Iterator referencing end of range to insert.
1233 * @return Reference to this string.
1234 * @throw std::length_error If new length exceeds @c max_size().
1236 * Removes the characters in the range [i1,i2). In place, characters
1237 * in the range [k1,k2) are inserted. If the length of result exceeds
1238 * max_size(), length_error is thrown. The value of the string doesn't
1239 * change if an error is thrown.
1241 template<class _InputIterator>
1243 replace(iterator __i1, iterator __i2,
1244 _InputIterator __k1, _InputIterator __k2)
1245 { typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
1246 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral()); }
1248 // Specializations for the common case of pointer and iterator:
1249 // useful to avoid the overhead of temporary buffering in _M_replace.
1251 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
1252 { return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1253 __k1, __k2 - __k1); }
1256 replace(iterator __i1, iterator __i2, const _CharT* __k1, const _CharT* __k2)
1257 { return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1258 __k1, __k2 - __k1); }
1261 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
1262 { return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1263 __k1.base(), __k2 - __k1);
1267 replace(iterator __i1, iterator __i2, const_iterator __k1, const_iterator __k2)
1268 { return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1269 __k1.base(), __k2 - __k1);
1273 template<class _Integer>
1275 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
1276 _Integer __val, __true_type)
1277 { return _M_replace_aux(__i1, __i2, __n, __val); }
1279 template<class _InputIterator>
1281 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
1282 _InputIterator __k2, __false_type)
1284 typedef typename iterator_traits<_InputIterator>::iterator_category
1286 return _M_replace(__i1, __i2, __k1, __k2, _Category());
1290 _M_replace_aux(iterator __i1, iterator __i2, size_type __n2, _CharT __c);
1292 template<class _InputIterator>
1294 _M_replace(iterator __i1, iterator __i2, _InputIterator __k1,
1295 _InputIterator __k2, input_iterator_tag);
1297 template<class _ForwardIterator>
1299 _M_replace_safe(iterator __i1, iterator __i2, _ForwardIterator __k1,
1300 _ForwardIterator __k2);
1302 // _S_construct_aux is used to implement the 21.3.1 para 15 which
1303 // requires special behaviour if _InIter is an integral type
1304 template<class _InIterator>
1306 _S_construct_aux(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1309 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
1310 return _S_construct(__beg, __end, __a, _Tag());
1313 template<class _InIterator>
1315 _S_construct_aux(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1318 return _S_construct(static_cast<size_type>(__beg),
1319 static_cast<value_type>(__end), __a);
1322 template<class _InIterator>
1324 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
1326 typedef typename _Is_integer<_InIterator>::_Integral _Integral;
1327 return _S_construct_aux(__beg, __end, __a, _Integral());
1330 // For Input Iterators, used in istreambuf_iterators, etc.
1331 template<class _InIterator>
1333 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1334 input_iterator_tag);
1336 // For forward_iterators up to random_access_iterators, used for
1337 // string::iterator, _CharT*, etc.
1338 template<class _FwdIterator>
1340 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
1341 forward_iterator_tag);
1344 _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
1349 * @brief Copy substring into C string.
1350 * @param s C string to copy value into.
1351 * @param n Number of characters to copy.
1352 * @param pos Index of first character to copy.
1353 * @return Number of characters actually copied
1354 * @throw std::out_of_range If pos > size().
1356 * Copies up to @a n characters starting at @a pos into the C string @a
1357 * s. If @a pos is greater than size(), out_of_range is thrown.
1360 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
1363 * @brief Swap contents with another string.
1364 * @param s String to swap with.
1366 * Exchanges the contents of this string with that of @a s in constant
1370 swap(basic_string& __s);
1372 // String operations:
1374 * @brief Return const pointer to null-terminated contents.
1376 * This is a handle to internal data. Do not modify or dire things may
1382 // MT: This assumes concurrent writes are OK.
1383 const size_type __n = this->size();
1384 traits_type::assign(_M_data()[__n], _Rep::_S_terminal);
1389 * @brief Return const pointer to contents.
1391 * This is a handle to internal data. It may not be null-terminated.
1392 * Do not modify or dire things may happen.
1395 data() const { return _M_data(); }
1398 * @brief Return copy of allocator used to construct this string.
1401 get_allocator() const { return _M_dataplus; }
1404 * @brief Find position of a C substring.
1405 * @param s C string to locate.
1406 * @param pos Index of character to search from.
1407 * @param n Number of characters from @a s to search for.
1408 * @return Index of start of first occurrence.
1410 * Starting from @a pos, searches forward for the first @a n characters
1411 * in @a s within this string. If found, returns the index where it
1412 * begins. If not found, returns npos.
1415 find(const _CharT* __s, size_type __pos, size_type __n) const;
1418 * @brief Find position of a string.
1419 * @param str String to locate.
1420 * @param pos Index of character to search from (default 0).
1421 * @return Index of start of first occurrence.
1423 * Starting from @a pos, searches forward for value of @a str within
1424 * this string. If found, returns the index where it begins. If not
1425 * found, returns npos.
1428 find(const basic_string& __str, size_type __pos = 0) const
1429 { return this->find(__str.data(), __pos, __str.size()); }
1432 * @brief Find position of a C string.
1433 * @param s C string to locate.
1434 * @param pos Index of character to search from (default 0).
1435 * @return Index of start of first occurrence.
1437 * Starting from @a pos, searches forward for the value of @a s within
1438 * this string. If found, returns the index where it begins. If not
1439 * found, returns npos.
1442 find(const _CharT* __s, size_type __pos = 0) const
1443 { return this->find(__s, __pos, traits_type::length(__s)); }
1446 * @brief Find position of a character.
1447 * @param c Character to locate.
1448 * @param pos Index of character to search from (default 0).
1449 * @return Index of first occurrence.
1451 * Starting from @a pos, searches forward for @a c within this string.
1452 * If found, returns the index where it was found. If not found,
1456 find(_CharT __c, size_type __pos = 0) const;
1459 * @brief Find last position of a string.
1460 * @param str String to locate.
1461 * @param pos Index of character to search back from (default end).
1462 * @return Index of start of last occurrence.
1464 * Starting from @a pos, searches backward for value of @a str within
1465 * this string. If found, returns the index where it begins. If not
1466 * found, returns npos.
1469 rfind(const basic_string& __str, size_type __pos = npos) const
1470 { return this->rfind(__str.data(), __pos, __str.size()); }
1473 * @brief Find last position of a C substring.
1474 * @param s C string to locate.
1475 * @param pos Index of character to search back from.
1476 * @param n Number of characters from s to search for.
1477 * @return Index of start of last occurrence.
1479 * Starting from @a pos, searches backward for the first @a n
1480 * characters in @a s within this string. If found, returns the index
1481 * where it begins. If not found, returns npos.
1484 rfind(const _CharT* __s, size_type __pos, size_type __n) const;
1487 * @brief Find last position of a C string.
1488 * @param s C string to locate.
1489 * @param pos Index of character to start search at (default 0).
1490 * @return Index of start of last occurrence.
1492 * Starting from @a pos, searches backward for the value of @a s within
1493 * this string. If found, returns the index where it begins. If not
1494 * found, returns npos.
1497 rfind(const _CharT* __s, size_type __pos = npos) const
1498 { return this->rfind(__s, __pos, traits_type::length(__s)); }
1501 * @brief Find last position of a character.
1502 * @param c Character to locate.
1503 * @param pos Index of character to search back from (default 0).
1504 * @return Index of last occurrence.
1506 * Starting from @a pos, searches backward for @a c within this string.
1507 * If found, returns the index where it was found. If not found,
1511 rfind(_CharT __c, size_type __pos = npos) const;
1514 * @brief Find position of a character of string.
1515 * @param str String containing characters to locate.
1516 * @param pos Index of character to search from (default 0).
1517 * @return Index of first occurrence.
1519 * Starting from @a pos, searches forward for one of the characters of
1520 * @a str within this string. If found, returns the index where it was
1521 * found. If not found, returns npos.
1524 find_first_of(const basic_string& __str, size_type __pos = 0) const
1525 { return this->find_first_of(__str.data(), __pos, __str.size()); }
1528 * @brief Find position of a character of C substring.
1529 * @param s String containing characters to locate.
1530 * @param pos Index of character to search from (default 0).
1531 * @param n Number of characters from s to search for.
1532 * @return Index of first occurrence.
1534 * Starting from @a pos, searches forward for one of the first @a n
1535 * characters of @a s within this string. If found, returns the index
1536 * where it was found. If not found, returns npos.
1539 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
1542 * @brief Find position of a character of C string.
1543 * @param s String containing characters to locate.
1544 * @param pos Index of character to search from (default 0).
1545 * @return Index of first occurrence.
1547 * Starting from @a pos, searches forward for one of the characters of
1548 * @a s within this string. If found, returns the index where it was
1549 * found. If not found, returns npos.
1552 find_first_of(const _CharT* __s, size_type __pos = 0) const
1553 { return this->find_first_of(__s, __pos, traits_type::length(__s)); }
1556 * @brief Find position of a character.
1557 * @param c Character to locate.
1558 * @param pos Index of character to search from (default 0).
1559 * @return Index of first occurrence.
1561 * Starting from @a pos, searches forward for the character @a c within
1562 * this string. If found, returns the index where it was found. If
1563 * not found, returns npos.
1565 * Note: equivalent to find(c, pos).
1568 find_first_of(_CharT __c, size_type __pos = 0) const
1569 { return this->find(__c, __pos); }
1572 * @brief Find last position of a character of string.
1573 * @param str String containing characters to locate.
1574 * @param pos Index of character to search back from (default end).
1575 * @return Index of last occurrence.
1577 * Starting from @a pos, searches backward for one of the characters of
1578 * @a str within this string. If found, returns the index where it was
1579 * found. If not found, returns npos.
1582 find_last_of(const basic_string& __str, size_type __pos = npos) const
1583 { return this->find_last_of(__str.data(), __pos, __str.size()); }
1586 * @brief Find last position of a character of C substring.
1587 * @param s C string containing characters to locate.
1588 * @param pos Index of character to search back from (default end).
1589 * @param n Number of characters from s to search for.
1590 * @return Index of last occurrence.
1592 * Starting from @a pos, searches backward for one of the first @a n
1593 * characters of @a s within this string. If found, returns the index
1594 * where it was found. If not found, returns npos.
1597 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
1600 * @brief Find last position of a character of C string.
1601 * @param s C string containing characters to locate.
1602 * @param pos Index of character to search back from (default end).
1603 * @return Index of last occurrence.
1605 * Starting from @a pos, searches backward for one of the characters of
1606 * @a s within this string. If found, returns the index where it was
1607 * found. If not found, returns npos.
1610 find_last_of(const _CharT* __s, size_type __pos = npos) const
1611 { return this->find_last_of(__s, __pos, traits_type::length(__s)); }
1614 * @brief Find last position of a character.
1615 * @param c Character to locate.
1616 * @param pos Index of character to search back from (default 0).
1617 * @return Index of last occurrence.
1619 * Starting from @a pos, searches backward for @a c within this string.
1620 * If found, returns the index where it was found. If not found,
1623 * Note: equivalent to rfind(c, pos).
1626 find_last_of(_CharT __c, size_type __pos = npos) const
1627 { return this->rfind(__c, __pos); }
1630 * @brief Find position of a character not in string.
1631 * @param str String containing characters to avoid.
1632 * @param pos Index of character to search from (default 0).
1633 * @return Index of first occurrence.
1635 * Starting from @a pos, searches forward for a character not contained
1636 * in @a str within this string. If found, returns the index where it
1637 * was found. If not found, returns npos.
1640 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
1641 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
1644 * @brief Find position of a character not in C substring.
1645 * @param s C string containing characters to avoid.
1646 * @param pos Index of character to search from (default 0).
1647 * @param n Number of characters from s to consider.
1648 * @return Index of first occurrence.
1650 * Starting from @a pos, searches forward for a character not contained
1651 * in the first @a n characters of @a s within this string. If found,
1652 * returns the index where it was found. If not found, returns npos.
1655 find_first_not_of(const _CharT* __s, size_type __pos,
1656 size_type __n) const;
1659 * @brief Find position of a character not in C string.
1660 * @param s C string containing characters to avoid.
1661 * @param pos Index of character to search from (default 0).
1662 * @return Index of first occurrence.
1664 * Starting from @a pos, searches forward for a character not contained
1665 * in @a s within this string. If found, returns the index where it
1666 * was found. If not found, returns npos.
1669 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
1670 { return this->find_first_not_of(__s, __pos, traits_type::length(__s)); }
1673 * @brief Find position of a different character.
1674 * @param c Character to avoid.
1675 * @param pos Index of character to search from (default 0).
1676 * @return Index of first occurrence.
1678 * Starting from @a pos, searches forward for a character other than @a c
1679 * within this string. If found, returns the index where it was found.
1680 * If not found, returns npos.
1683 find_first_not_of(_CharT __c, size_type __pos = 0) const;
1686 * @brief Find last position of a character not in string.
1687 * @param str String containing characters to avoid.
1688 * @param pos Index of character to search from (default 0).
1689 * @return Index of first occurrence.
1691 * Starting from @a pos, searches backward for a character not
1692 * contained in @a str within this string. If found, returns the index
1693 * where it was found. If not found, returns npos.
1696 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
1697 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
1700 * @brief Find last position of a character not in C substring.
1701 * @param s C string containing characters to avoid.
1702 * @param pos Index of character to search from (default 0).
1703 * @param n Number of characters from s to consider.
1704 * @return Index of first occurrence.
1706 * Starting from @a pos, searches backward for a character not
1707 * contained in the first @a n characters of @a s within this string.
1708 * If found, returns the index where it was found. If not found,
1712 find_last_not_of(const _CharT* __s, size_type __pos,
1713 size_type __n) const;
1715 * @brief Find position of a character not in C string.
1716 * @param s C string containing characters to avoid.
1717 * @param pos Index of character to search from (default 0).
1718 * @return Index of first occurrence.
1720 * Starting from @a pos, searches backward for a character not
1721 * contained in @a s within this string. If found, returns the index
1722 * where it was found. If not found, returns npos.
1725 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
1726 { return this->find_last_not_of(__s, __pos, traits_type::length(__s)); }
1729 * @brief Find last position of a different character.
1730 * @param c Character to avoid.
1731 * @param pos Index of character to search from (default 0).
1732 * @return Index of first occurrence.
1734 * Starting from @a pos, searches backward for a character other than
1735 * @a c within this string. If found, returns the index where it was
1736 * found. If not found, returns npos.
1739 find_last_not_of(_CharT __c, size_type __pos = npos) const;
1742 * @brief Get a substring.
1743 * @param pos Index of first character (default 0).
1744 * @param n Number of characters in substring (default remainder).
1745 * @return The new string.
1746 * @throw std::out_of_range If pos > size().
1748 * Construct and return a new string using the @a n characters starting
1749 * at @a pos. If the string is too short, use the remainder of the
1750 * characters. If @a pos is beyond the end of the string, out_of_range
1754 substr(size_type __pos = 0, size_type __n = npos) const
1756 if (__pos > this->size())
1757 __throw_out_of_range(__N("basic_string::substr"));
1758 return basic_string(*this, __pos, __n);
1762 * @brief Compare to a string.
1763 * @param str String to compare against.
1764 * @return Integer < 0, 0, or > 0.
1766 * Returns an integer < 0 if this string is ordered before @a str, 0 if
1767 * their values are equivalent, or > 0 if this string is ordered after
1768 * @a str. If the lengths of @a str and this string are different, the
1769 * shorter one is ordered first. If they are the same, returns the
1770 * result of traits::compare(data(),str.data(),size());
1773 compare(const basic_string& __str) const
1775 const size_type __size = this->size();
1776 const size_type __osize = __str.size();
1777 const size_type __len = std::min(__size, __osize);
1779 int __r = traits_type::compare(_M_data(), __str.data(), __len);
1781 __r = __size - __osize;
1786 * @brief Compare substring to a string.
1787 * @param pos Index of first character of substring.
1788 * @param n Number of characters in substring.
1789 * @param str String to compare against.
1790 * @return Integer < 0, 0, or > 0.
1792 * Form the substring of this string from the @a n characters starting
1793 * at @a pos. Returns an integer < 0 if the substring is ordered
1794 * before @a str, 0 if their values are equivalent, or > 0 if the
1795 * substring is ordered after @a str. If the lengths @a of str and the
1796 * substring are different, the shorter one is ordered first. If they
1797 * are the same, returns the result of
1798 * traits::compare(substring.data(),str.data(),size());
1801 compare(size_type __pos, size_type __n, const basic_string& __str) const;
1804 * @brief Compare substring to a substring.
1805 * @param pos1 Index of first character of substring.
1806 * @param n1 Number of characters in substring.
1807 * @param str String to compare against.
1808 * @param pos2 Index of first character of substring of str.
1809 * @param n2 Number of characters in substring of str.
1810 * @return Integer < 0, 0, or > 0.
1812 * Form the substring of this string from the @a n1 characters starting
1813 * at @a pos1. Form the substring of @a str from the @a n2 characters
1814 * starting at @a pos2. Returns an integer < 0 if this substring is
1815 * ordered before the substring of @a str, 0 if their values are
1816 * equivalent, or > 0 if this substring is ordered after the substring
1817 * of @a str. If the lengths of the substring of @a str and this
1818 * substring are different, the shorter one is ordered first. If they
1819 * are the same, returns the result of
1820 * traits::compare(substring.data(),str.substr(pos2,n2).data(),size());
1823 compare(size_type __pos1, size_type __n1, const basic_string& __str,
1824 size_type __pos2, size_type __n2) const;
1827 * @brief Compare to a C string.
1828 * @param s C string to compare against.
1829 * @return Integer < 0, 0, or > 0.
1831 * Returns an integer < 0 if this string is ordered before @a s, 0 if
1832 * their values are equivalent, or > 0 if this string is ordered after
1833 * @a s. If the lengths of @a s and this string are different, the
1834 * shorter one is ordered first. If they are the same, returns the
1835 * result of traits::compare(data(),s,size());
1838 compare(const _CharT* __s) const;
1840 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1841 // 5 String::compare specification questionable
1843 * @brief Compare substring to a C string.
1844 * @param pos Index of first character of substring.
1845 * @param n1 Number of characters in substring.
1846 * @param s C string to compare against.
1847 * @return Integer < 0, 0, or > 0.
1849 * Form the substring of this string from the @a n1 characters starting
1850 * at @a pos. Returns an integer < 0 if the substring is ordered
1851 * before @a s, 0 if their values are equivalent, or > 0 if the
1852 * substring is ordered after @a s. If the lengths of @a s and the
1853 * substring are different, the shorter one is ordered first. If they
1854 * are the same, returns the result of
1855 * traits::compare(substring.data(),s,size());
1858 compare(size_type __pos, size_type __n1, const _CharT* __s) const;
1861 * @brief Compare substring against a C substring.
1862 * @param pos1 Index of first character of substring.
1863 * @param n1 Number of characters in substring.
1864 * @param s C string to compare against.
1865 * @param n2 Number of characters in substring of s.
1866 * @return Integer < 0, 0, or > 0.
1868 * Form the substring of this string from the @a n1 characters starting
1869 * at @a pos1. Form the substring of @a s from the first @a n
1870 * characters of @a s. Returns an integer < 0 if this substring is
1871 * ordered before the substring of @a s, 0 if their values are
1872 * equivalent, or > 0 if this substring is ordered after the substring
1873 * of @a s. If the lengths of this substring and @a n are different,
1874 * the shorter one is ordered first. If they are the same, returns the
1875 * result of traits::compare(substring.data(),s,size());
1878 compare(size_type __pos, size_type __n1, const _CharT* __s,
1879 size_type __n2) const;
1883 template<typename _CharT, typename _Traits, typename _Alloc>
1884 inline basic_string<_CharT, _Traits, _Alloc>::
1886 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
1890 * @brief Concatenate two strings.
1891 * @param lhs First string.
1892 * @param rhs Last string.
1893 * @return New string with value of @a lhs followed by @a rhs.
1895 template<typename _CharT, typename _Traits, typename _Alloc>
1896 basic_string<_CharT, _Traits, _Alloc>
1897 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
1898 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
1900 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
1901 __str.append(__rhs);
1906 * @brief Concatenate C string and string.
1907 * @param lhs First string.
1908 * @param rhs Last string.
1909 * @return New string with value of @a lhs followed by @a rhs.
1911 template<typename _CharT, typename _Traits, typename _Alloc>
1912 basic_string<_CharT,_Traits,_Alloc>
1913 operator+(const _CharT* __lhs,
1914 const basic_string<_CharT,_Traits,_Alloc>& __rhs);
1917 * @brief Concatenate character and string.
1918 * @param lhs First string.
1919 * @param rhs Last string.
1920 * @return New string with @a lhs followed by @a rhs.
1922 template<typename _CharT, typename _Traits, typename _Alloc>
1923 basic_string<_CharT,_Traits,_Alloc>
1924 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
1927 * @brief Concatenate string and C string.
1928 * @param lhs First string.
1929 * @param rhs Last string.
1930 * @return New string with @a lhs followed by @a rhs.
1932 template<typename _CharT, typename _Traits, typename _Alloc>
1933 inline basic_string<_CharT, _Traits, _Alloc>
1934 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
1935 const _CharT* __rhs)
1937 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
1938 __str.append(__rhs);
1943 * @brief Concatenate string and character.
1944 * @param lhs First string.
1945 * @param rhs Last string.
1946 * @return New string with @a lhs followed by @a rhs.
1948 template<typename _CharT, typename _Traits, typename _Alloc>
1949 inline basic_string<_CharT, _Traits, _Alloc>
1950 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
1952 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1953 typedef typename __string_type::size_type __size_type;
1954 __string_type __str(__lhs);
1955 __str.append(__size_type(1), __rhs);
1961 * @brief Test equivalence of two strings.
1962 * @param lhs First string.
1963 * @param rhs Second string.
1964 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
1966 template<typename _CharT, typename _Traits, typename _Alloc>
1968 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
1969 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
1970 { return __lhs.compare(__rhs) == 0; }
1973 * @brief Test equivalence of C string and string.
1974 * @param lhs C string.
1975 * @param rhs String.
1976 * @return True if @a rhs.compare(@a lhs) == 0. False otherwise.
1978 template<typename _CharT, typename _Traits, typename _Alloc>
1980 operator==(const _CharT* __lhs,
1981 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
1982 { return __rhs.compare(__lhs) == 0; }
1985 * @brief Test equivalence of string and C string.
1986 * @param lhs String.
1987 * @param rhs C string.
1988 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
1990 template<typename _CharT, typename _Traits, typename _Alloc>
1992 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
1993 const _CharT* __rhs)
1994 { return __lhs.compare(__rhs) == 0; }
1998 * @brief Test difference of two strings.
1999 * @param lhs First string.
2000 * @param rhs Second string.
2001 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2003 template<typename _CharT, typename _Traits, typename _Alloc>
2005 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2006 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2007 { return __rhs.compare(__lhs) != 0; }
2010 * @brief Test difference of C string and string.
2011 * @param lhs C string.
2012 * @param rhs String.
2013 * @return True if @a rhs.compare(@a lhs) != 0. False otherwise.
2015 template<typename _CharT, typename _Traits, typename _Alloc>
2017 operator!=(const _CharT* __lhs,
2018 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2019 { return __rhs.compare(__lhs) != 0; }
2022 * @brief Test difference of string and C string.
2023 * @param lhs String.
2024 * @param rhs C string.
2025 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2027 template<typename _CharT, typename _Traits, typename _Alloc>
2029 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2030 const _CharT* __rhs)
2031 { return __lhs.compare(__rhs) != 0; }
2035 * @brief Test if string precedes string.
2036 * @param lhs First string.
2037 * @param rhs Second string.
2038 * @return True if @a lhs precedes @a rhs. False otherwise.
2040 template<typename _CharT, typename _Traits, typename _Alloc>
2042 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2043 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2044 { return __lhs.compare(__rhs) < 0; }
2047 * @brief Test if string precedes C string.
2048 * @param lhs String.
2049 * @param rhs C string.
2050 * @return True if @a lhs precedes @a rhs. False otherwise.
2052 template<typename _CharT, typename _Traits, typename _Alloc>
2054 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2055 const _CharT* __rhs)
2056 { return __lhs.compare(__rhs) < 0; }
2059 * @brief Test if C string precedes string.
2060 * @param lhs C string.
2061 * @param rhs String.
2062 * @return True if @a lhs precedes @a rhs. False otherwise.
2064 template<typename _CharT, typename _Traits, typename _Alloc>
2066 operator<(const _CharT* __lhs,
2067 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2068 { return __rhs.compare(__lhs) > 0; }
2072 * @brief Test if string follows string.
2073 * @param lhs First string.
2074 * @param rhs Second string.
2075 * @return True if @a lhs follows @a rhs. False otherwise.
2077 template<typename _CharT, typename _Traits, typename _Alloc>
2079 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2080 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2081 { return __lhs.compare(__rhs) > 0; }
2084 * @brief Test if string follows C string.
2085 * @param lhs String.
2086 * @param rhs C string.
2087 * @return True if @a lhs follows @a rhs. False otherwise.
2089 template<typename _CharT, typename _Traits, typename _Alloc>
2091 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2092 const _CharT* __rhs)
2093 { return __lhs.compare(__rhs) > 0; }
2096 * @brief Test if C string follows string.
2097 * @param lhs C string.
2098 * @param rhs String.
2099 * @return True if @a lhs follows @a rhs. False otherwise.
2101 template<typename _CharT, typename _Traits, typename _Alloc>
2103 operator>(const _CharT* __lhs,
2104 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2105 { return __rhs.compare(__lhs) < 0; }
2109 * @brief Test if string doesn't follow string.
2110 * @param lhs First string.
2111 * @param rhs Second string.
2112 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2114 template<typename _CharT, typename _Traits, typename _Alloc>
2116 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2117 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2118 { return __lhs.compare(__rhs) <= 0; }
2121 * @brief Test if string doesn't follow C string.
2122 * @param lhs String.
2123 * @param rhs C string.
2124 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2126 template<typename _CharT, typename _Traits, typename _Alloc>
2128 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2129 const _CharT* __rhs)
2130 { return __lhs.compare(__rhs) <= 0; }
2133 * @brief Test if C string doesn't follow string.
2134 * @param lhs C string.
2135 * @param rhs String.
2136 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2138 template<typename _CharT, typename _Traits, typename _Alloc>
2140 operator<=(const _CharT* __lhs,
2141 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2142 { return __rhs.compare(__lhs) >= 0; }
2146 * @brief Test if string doesn't precede string.
2147 * @param lhs First string.
2148 * @param rhs Second string.
2149 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2151 template<typename _CharT, typename _Traits, typename _Alloc>
2153 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2154 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2155 { return __lhs.compare(__rhs) >= 0; }
2158 * @brief Test if string doesn't precede C string.
2159 * @param lhs String.
2160 * @param rhs C string.
2161 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2163 template<typename _CharT, typename _Traits, typename _Alloc>
2165 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2166 const _CharT* __rhs)
2167 { return __lhs.compare(__rhs) >= 0; }
2170 * @brief Test if C string doesn't precede string.
2171 * @param lhs C string.
2172 * @param rhs String.
2173 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2175 template<typename _CharT, typename _Traits, typename _Alloc>
2177 operator>=(const _CharT* __lhs,
2178 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2179 { return __rhs.compare(__lhs) <= 0; }
2183 * @brief Swap contents of two strings.
2184 * @param lhs First string.
2185 * @param rhs Second string.
2187 * Exchanges the contents of @a lhs and @a rhs in constant time.
2189 template<typename _CharT, typename _Traits, typename _Alloc>
2191 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
2192 basic_string<_CharT, _Traits, _Alloc>& __rhs)
2193 { __lhs.swap(__rhs); }
2196 * @brief Read stream into a string.
2197 * @param is Input stream.
2198 * @param str Buffer to store into.
2199 * @return Reference to the input stream.
2201 * Stores characters from @a is into @a str until whitespace is found, the
2202 * end of the stream is encountered, or str.max_size() is reached. If
2203 * is.width() is non-zero, that is the limit on the number of characters
2204 * stored into @a str. Any previous contents of @a str are erased.
2206 template<typename _CharT, typename _Traits, typename _Alloc>
2207 basic_istream<_CharT, _Traits>&
2208 operator>>(basic_istream<_CharT, _Traits>& __is,
2209 basic_string<_CharT, _Traits, _Alloc>& __str);
2212 * @brief Write string to a stream.
2213 * @param os Output stream.
2214 * @param str String to write out.
2215 * @return Reference to the output stream.
2217 * Output characters of @a str into os following the same rules as for
2218 * writing a C string.
2220 template<typename _CharT, typename _Traits, typename _Alloc>
2221 basic_ostream<_CharT, _Traits>&
2222 operator<<(basic_ostream<_CharT, _Traits>& __os,
2223 const basic_string<_CharT, _Traits, _Alloc>& __str);
2226 * @brief Read a line from stream into a string.
2227 * @param is Input stream.
2228 * @param str Buffer to store into.
2229 * @param delim Character marking end of line.
2230 * @return Reference to the input stream.
2232 * Stores characters from @a is into @a str until @a delim is found, the
2233 * end of the stream is encountered, or str.max_size() is reached. If
2234 * is.width() is non-zero, that is the limit on the number of characters
2235 * stored into @a str. Any previous contents of @a str are erased. If @a
2236 * delim was encountered, it is extracted but not stored into @a str.
2238 template<typename _CharT, typename _Traits, typename _Alloc>
2239 basic_istream<_CharT,_Traits>&
2240 getline(basic_istream<_CharT, _Traits>& __is,
2241 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
2244 * @brief Read a line from stream into a string.
2245 * @param is Input stream.
2246 * @param str Buffer to store into.
2247 * @return Reference to the input stream.
2249 * Stores characters from is into @a str until '\n' is found, the end of
2250 * the stream is encountered, or str.max_size() is reached. If is.width()
2251 * is non-zero, that is the limit on the number of characters stored into
2252 * @a str. Any previous contents of @a str are erased. If end of line was
2253 * encountered, it is extracted but not stored into @a str.
2255 template<typename _CharT, typename _Traits, typename _Alloc>
2256 inline basic_istream<_CharT,_Traits>&
2257 getline(basic_istream<_CharT, _Traits>& __is,
2258 basic_string<_CharT, _Traits, _Alloc>& __str);
2261 #endif /* _BASIC_STRING_H */