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)
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;
992 #ifdef _GLIBCXX_DEPRECATED
994 * @brief Insert one default-constructed character.
995 * @param p Iterator referencing position in string to insert at.
996 * @return Iterator referencing newly inserted char.
997 * @throw std::length_error If new length exceeds @c max_size().
998 * @throw std::out_of_range If @a p is beyond the end of this string.
1000 * Inserts a default-constructed character at position
1001 * referenced by @a p. If adding character causes the length
1002 * to exceed max_size(), length_error is thrown. If @a p is
1003 * beyond end of string, out_of_range is thrown. The value of
1004 * the string doesn't change if an error is thrown.
1007 insert(iterator __p)
1008 { return this->insert(__p, _CharT()); }
1009 #endif /* _GLIBCXX_DEPRECATED */
1012 * @brief Remove characters.
1013 * @param pos Index of first character to remove (default 0).
1014 * @param n Number of characters to remove (default remainder).
1015 * @return Reference to this string.
1016 * @throw std::out_of_range If @a pos is beyond the end of this
1019 * Removes @a n characters from this string starting at @a pos. The
1020 * length of the string is reduced by @a n. If there are < @a n
1021 * characters to remove, the remainder of the string is truncated. If
1022 * @a p is beyond end of string, out_of_range is thrown. The value of
1023 * the string doesn't change if an error is thrown.
1026 erase(size_type __pos = 0, size_type __n = npos)
1028 return this->replace(_M_check(__pos), _M_fold(__pos, __n),
1029 _M_data(), _M_data());
1033 * @brief Remove one character.
1034 * @param position Iterator referencing the character to remove.
1035 * @return iterator referencing same location after removal.
1036 * @throw std::out_of_range If @a position is beyond the end of this
1039 * Removes the character at @a position from this string. If @a
1040 * position is beyond end of string, out_of_range is thrown. The value
1041 * of the string doesn't change if an error is thrown.
1044 erase(iterator __position)
1046 const size_type __i = __position - _M_ibegin();
1047 this->replace(__position, __position + 1, _M_data(), _M_data());
1048 _M_rep()->_M_set_leaked();
1049 return _M_ibegin() + __i;
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.
1057 * @throw std::out_of_range If @a first is beyond the end of this
1060 * Removes the characters in the range [first,last) from this string.
1061 * If @a first is beyond end of string, out_of_range is thrown. The
1062 * value of the string doesn't change if an error is thrown.
1065 erase(iterator __first, iterator __last)
1067 const size_type __i = __first - _M_ibegin();
1068 this->replace(__first, __last, _M_data(), _M_data());
1069 _M_rep()->_M_set_leaked();
1070 return _M_ibegin() + __i;
1074 * @brief Replace characters with value from another string.
1075 * @param pos Index of first character to replace.
1076 * @param n Number of characters to be replaced.
1077 * @param str String to insert.
1078 * @return Reference to this string.
1079 * @throw std::out_of_range If @a pos is beyond the end of this
1081 * @throw std::length_error If new length exceeds @c max_size().
1083 * Removes the characters in the range [pos,pos+n) from this string.
1084 * In place, the value of @a str is inserted. If @a pos is beyond end
1085 * of string, out_of_range is thrown. If the length of the result
1086 * exceeds max_size(), length_error is thrown. The value of the string
1087 * doesn't change if an error is thrown.
1090 replace(size_type __pos, size_type __n, const basic_string& __str)
1091 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1094 * @brief Replace characters with value from another string.
1095 * @param pos1 Index of first character to replace.
1096 * @param n1 Number of characters to be replaced.
1097 * @param str String to insert.
1098 * @param pos2 Index of first character of str to use.
1099 * @param n2 Number of characters from str to use.
1100 * @return Reference to this string.
1101 * @throw std::out_of_range If @a pos1 > size() or @a pos2 >
1103 * @throw std::length_error If new length exceeds @c max_size().
1105 * Removes the characters in the range [pos1,pos1 + n) from this
1106 * string. In place, the value of @a str is inserted. If @a pos is
1107 * beyond end of string, out_of_range is thrown. If the length of the
1108 * result exceeds max_size(), length_error is thrown. The value of the
1109 * string doesn't change if an error is thrown.
1112 replace(size_type __pos1, size_type __n1, const basic_string& __str,
1113 size_type __pos2, size_type __n2);
1116 * @brief Replace characters with value of a C substring.
1117 * @param pos Index of first character to replace.
1118 * @param n1 Number of characters to be replaced.
1119 * @param str C string to insert.
1120 * @param n2 Number of characters from str to use.
1121 * @return Reference to this string.
1122 * @throw std::out_of_range If @a pos1 > size().
1123 * @throw std::length_error If new length exceeds @c max_size().
1125 * Removes the characters in the range [pos,pos + n1) from this string.
1126 * In place, the first @a n2 characters of @a str are inserted, or all
1127 * of @a str if @a n2 is too large. If @a pos is beyond end of string,
1128 * out_of_range is thrown. If the length of result exceeds max_size(),
1129 * length_error is thrown. The value of the string doesn't change if
1130 * an error is thrown.
1133 replace(size_type __pos, size_type __n1, const _CharT* __s,
1137 * @brief Replace characters with value of a C string.
1138 * @param pos Index of first character to replace.
1139 * @param n1 Number of characters to be replaced.
1140 * @param str C string to insert.
1141 * @return Reference to this string.
1142 * @throw std::out_of_range If @a pos > size().
1143 * @throw std::length_error If new length exceeds @c max_size().
1145 * Removes the characters in the range [pos,pos + n1) from this string.
1146 * In place, the first @a n characters of @a str are inserted. If @a
1147 * pos is beyond end of string, out_of_range is thrown. If the length
1148 * of result exceeds max_size(), length_error is thrown. The value of
1149 * the string doesn't change if an error is thrown.
1152 replace(size_type __pos, size_type __n1, const _CharT* __s)
1153 { return this->replace(__pos, __n1, __s, traits_type::length(__s)); }
1156 * @brief Replace characters with multiple characters.
1157 * @param pos Index of first character to replace.
1158 * @param n1 Number of characters to be replaced.
1159 * @param n2 Number of characters to insert.
1160 * @param c Character to insert.
1161 * @return Reference to this string.
1162 * @throw std::out_of_range If @a pos > size().
1163 * @throw std::length_error If new length exceeds @c max_size().
1165 * Removes the characters in the range [pos,pos + n1) from this string.
1166 * In place, @a n2 copies of @a c are inserted. If @a pos is beyond
1167 * end of string, out_of_range is thrown. If the length of result
1168 * exceeds max_size(), length_error is thrown. The value of the string
1169 * doesn't change if an error is thrown.
1172 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1173 { return this->replace(_M_check(__pos), _M_fold(__pos, __n1), __n2, __c); }
1176 * @brief Replace range of characters with string.
1177 * @param i1 Iterator referencing start of range to replace.
1178 * @param i2 Iterator referencing end of range to replace.
1179 * @param str String value to insert.
1180 * @return Reference to this string.
1181 * @throw std::length_error If new length exceeds @c max_size().
1183 * Removes the characters in the range [i1,i2). In place, the value of
1184 * @a str is inserted. If the length of result exceeds max_size(),
1185 * length_error is thrown. The value of the string doesn't change if
1186 * an error is thrown.
1189 replace(iterator __i1, iterator __i2, const basic_string& __str)
1190 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
1193 * @brief Replace range of characters with C substring.
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 * @param n Number of characters from s to insert.
1198 * @return Reference to this string.
1199 * @throw std::length_error If new length exceeds @c max_size().
1201 * Removes the characters in the range [i1,i2). In place, the first @a
1202 * n characters of @a s are inserted. If the length of result exceeds
1203 * max_size(), length_error is thrown. The value of the string doesn't
1204 * change if an error is thrown.
1207 replace(iterator __i1, iterator __i2,
1208 const _CharT* __s, size_type __n)
1209 { return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n); }
1212 * @brief Replace range of characters with C string.
1213 * @param i1 Iterator referencing start of range to replace.
1214 * @param i2 Iterator referencing end of range to replace.
1215 * @param s C string value to insert.
1216 * @return Reference to this string.
1217 * @throw std::length_error If new length exceeds @c max_size().
1219 * Removes the characters in the range [i1,i2). In place, the
1220 * characters of @a s are inserted. If the length of result exceeds
1221 * max_size(), length_error is thrown. The value of the string doesn't
1222 * change if an error is thrown.
1225 replace(iterator __i1, iterator __i2, const _CharT* __s)
1226 { return this->replace(__i1, __i2, __s, traits_type::length(__s)); }
1229 * @brief Replace range of characters with multiple characters
1230 * @param i1 Iterator referencing start of range to replace.
1231 * @param i2 Iterator referencing end of range to replace.
1232 * @param n Number of characters to insert.
1233 * @param c Character to insert.
1234 * @return Reference to this string.
1235 * @throw std::length_error If new length exceeds @c max_size().
1237 * Removes the characters in the range [i1,i2). In place, @a n copies
1238 * of @a c are inserted. If the length of result exceeds max_size(),
1239 * length_error is thrown. The value of the string doesn't change if
1240 * an error is thrown.
1243 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
1244 { return _M_replace_aux(__i1, __i2, __n, __c); }
1247 * @brief Replace range of characters with range.
1248 * @param i1 Iterator referencing start of range to replace.
1249 * @param i2 Iterator referencing end of range to replace.
1250 * @param k1 Iterator referencing start of range to insert.
1251 * @param k2 Iterator referencing end of range to insert.
1252 * @return Reference to this string.
1253 * @throw std::length_error If new length exceeds @c max_size().
1255 * Removes the characters in the range [i1,i2). In place, characters
1256 * in the range [k1,k2) are inserted. If the length of result exceeds
1257 * max_size(), length_error is thrown. The value of the string doesn't
1258 * change if an error is thrown.
1260 template<class _InputIterator>
1262 replace(iterator __i1, iterator __i2,
1263 _InputIterator __k1, _InputIterator __k2)
1264 { typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
1265 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral()); }
1267 // Specializations for the common case of pointer and iterator:
1268 // useful to avoid the overhead of temporary buffering in _M_replace.
1270 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
1271 { return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1272 __k1, __k2 - __k1); }
1275 replace(iterator __i1, iterator __i2, const _CharT* __k1, const _CharT* __k2)
1276 { return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1277 __k1, __k2 - __k1); }
1280 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
1281 { return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1282 __k1.base(), __k2 - __k1);
1286 replace(iterator __i1, iterator __i2, const_iterator __k1, const_iterator __k2)
1287 { return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1288 __k1.base(), __k2 - __k1);
1292 template<class _Integer>
1294 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
1295 _Integer __val, __true_type)
1296 { return _M_replace_aux(__i1, __i2, __n, __val); }
1298 template<class _InputIterator>
1300 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
1301 _InputIterator __k2, __false_type)
1303 typedef typename iterator_traits<_InputIterator>::iterator_category
1305 return _M_replace(__i1, __i2, __k1, __k2, _Category());
1309 _M_replace_aux(iterator __i1, iterator __i2, size_type __n2, _CharT __c);
1311 template<class _InputIterator>
1313 _M_replace(iterator __i1, iterator __i2, _InputIterator __k1,
1314 _InputIterator __k2, input_iterator_tag);
1316 template<class _ForwardIterator>
1318 _M_replace_safe(iterator __i1, iterator __i2, _ForwardIterator __k1,
1319 _ForwardIterator __k2);
1321 // _S_construct_aux is used to implement the 21.3.1 para 15 which
1322 // requires special behaviour if _InIter is an integral type
1323 template<class _InIterator>
1325 _S_construct_aux(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1328 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
1329 return _S_construct(__beg, __end, __a, _Tag());
1332 template<class _InIterator>
1334 _S_construct_aux(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1337 return _S_construct(static_cast<size_type>(__beg),
1338 static_cast<value_type>(__end), __a);
1341 template<class _InIterator>
1343 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
1345 typedef typename _Is_integer<_InIterator>::_Integral _Integral;
1346 return _S_construct_aux(__beg, __end, __a, _Integral());
1349 // For Input Iterators, used in istreambuf_iterators, etc.
1350 template<class _InIterator>
1352 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1353 input_iterator_tag);
1355 // For forward_iterators up to random_access_iterators, used for
1356 // string::iterator, _CharT*, etc.
1357 template<class _FwdIterator>
1359 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
1360 forward_iterator_tag);
1363 _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
1368 * @brief Copy substring into C string.
1369 * @param s C string to copy value into.
1370 * @param n Number of characters to copy.
1371 * @param pos Index of first character to copy.
1372 * @return Number of characters actually copied
1373 * @throw std::out_of_range If pos > size().
1375 * Copies up to @a n characters starting at @a pos into the C string @a
1376 * s. If @a pos is greater than size(), out_of_range is thrown.
1379 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
1382 * @brief Swap contents with another string.
1383 * @param s String to swap with.
1385 * Exchanges the contents of this string with that of @a s in constant
1389 swap(basic_string& __s);
1391 // String operations:
1393 * @brief Return const pointer to null-terminated contents.
1395 * This is a handle to internal data. Do not modify or dire things may
1401 // MT: This assumes concurrent writes are OK.
1402 const size_type __n = this->size();
1403 traits_type::assign(_M_data()[__n], _Rep::_S_terminal);
1408 * @brief Return const pointer to contents.
1410 * This is a handle to internal data. It may not be null-terminated.
1411 * Do not modify or dire things may happen.
1414 data() const { return _M_data(); }
1417 * @brief Return copy of allocator used to construct this string.
1420 get_allocator() const { return _M_dataplus; }
1423 * @brief Find position of a C substring.
1424 * @param s C string to locate.
1425 * @param pos Index of character to search from.
1426 * @param n Number of characters from @a s to search for.
1427 * @return Index of start of first occurrence.
1429 * Starting from @a pos, searches forward for the first @a n characters
1430 * in @a s within this string. If found, returns the index where it
1431 * begins. If not found, returns npos.
1434 find(const _CharT* __s, size_type __pos, size_type __n) const;
1437 * @brief Find position of a string.
1438 * @param str String to locate.
1439 * @param pos Index of character to search from (default 0).
1440 * @return Index of start of first occurrence.
1442 * Starting from @a pos, searches forward for value of @a str within
1443 * this string. If found, returns the index where it begins. If not
1444 * found, returns npos.
1447 find(const basic_string& __str, size_type __pos = 0) const
1448 { return this->find(__str.data(), __pos, __str.size()); }
1451 * @brief Find position of a C string.
1452 * @param s C string to locate.
1453 * @param pos Index of character to search from (default 0).
1454 * @return Index of start of first occurrence.
1456 * Starting from @a pos, searches forward for the value of @a s within
1457 * this string. If found, returns the index where it begins. If not
1458 * found, returns npos.
1461 find(const _CharT* __s, size_type __pos = 0) const
1462 { return this->find(__s, __pos, traits_type::length(__s)); }
1465 * @brief Find position of a character.
1466 * @param c Character to locate.
1467 * @param pos Index of character to search from (default 0).
1468 * @return Index of first occurrence.
1470 * Starting from @a pos, searches forward for @a c within this string.
1471 * If found, returns the index where it was found. If not found,
1475 find(_CharT __c, size_type __pos = 0) const;
1478 * @brief Find last position of a string.
1479 * @param str String to locate.
1480 * @param pos Index of character to search back from (default end).
1481 * @return Index of start of last occurrence.
1483 * Starting from @a pos, searches backward for value of @a str within
1484 * this string. If found, returns the index where it begins. If not
1485 * found, returns npos.
1488 rfind(const basic_string& __str, size_type __pos = npos) const
1489 { return this->rfind(__str.data(), __pos, __str.size()); }
1492 * @brief Find last position of a C substring.
1493 * @param s C string to locate.
1494 * @param pos Index of character to search back from.
1495 * @param n Number of characters from s to search for.
1496 * @return Index of start of last occurrence.
1498 * Starting from @a pos, searches backward for the first @a n
1499 * characters in @a s within this string. If found, returns the index
1500 * where it begins. If not found, returns npos.
1503 rfind(const _CharT* __s, size_type __pos, size_type __n) const;
1506 * @brief Find last position of a C string.
1507 * @param s C string to locate.
1508 * @param pos Index of character to start search at (default 0).
1509 * @return Index of start of last occurrence.
1511 * Starting from @a pos, searches backward for the value of @a s within
1512 * this string. If found, returns the index where it begins. If not
1513 * found, returns npos.
1516 rfind(const _CharT* __s, size_type __pos = npos) const
1517 { return this->rfind(__s, __pos, traits_type::length(__s)); }
1520 * @brief Find last position of a character.
1521 * @param c Character to locate.
1522 * @param pos Index of character to search back from (default 0).
1523 * @return Index of last occurrence.
1525 * Starting from @a pos, searches backward for @a c within this string.
1526 * If found, returns the index where it was found. If not found,
1530 rfind(_CharT __c, size_type __pos = npos) const;
1533 * @brief Find position of a character of string.
1534 * @param str String containing characters to locate.
1535 * @param pos Index of character to search from (default 0).
1536 * @return Index of first occurrence.
1538 * Starting from @a pos, searches forward for one of the characters of
1539 * @a str within this string. If found, returns the index where it was
1540 * found. If not found, returns npos.
1543 find_first_of(const basic_string& __str, size_type __pos = 0) const
1544 { return this->find_first_of(__str.data(), __pos, __str.size()); }
1547 * @brief Find position of a character of C substring.
1548 * @param s String containing characters to locate.
1549 * @param pos Index of character to search from (default 0).
1550 * @param n Number of characters from s to search for.
1551 * @return Index of first occurrence.
1553 * Starting from @a pos, searches forward for one of the first @a n
1554 * characters of @a s within this string. If found, returns the index
1555 * where it was found. If not found, returns npos.
1558 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
1561 * @brief Find position of a character of C string.
1562 * @param s String containing characters to locate.
1563 * @param pos Index of character to search from (default 0).
1564 * @return Index of first occurrence.
1566 * Starting from @a pos, searches forward for one of the characters of
1567 * @a s within this string. If found, returns the index where it was
1568 * found. If not found, returns npos.
1571 find_first_of(const _CharT* __s, size_type __pos = 0) const
1572 { return this->find_first_of(__s, __pos, traits_type::length(__s)); }
1575 * @brief Find position of a character.
1576 * @param c Character to locate.
1577 * @param pos Index of character to search from (default 0).
1578 * @return Index of first occurrence.
1580 * Starting from @a pos, searches forward for the character @a c within
1581 * this string. If found, returns the index where it was found. If
1582 * not found, returns npos.
1584 * Note: equivalent to find(c, pos).
1587 find_first_of(_CharT __c, size_type __pos = 0) const
1588 { return this->find(__c, __pos); }
1591 * @brief Find last position of a character of string.
1592 * @param str String containing characters to locate.
1593 * @param pos Index of character to search back from (default end).
1594 * @return Index of last occurrence.
1596 * Starting from @a pos, searches backward for one of the characters of
1597 * @a str within this string. If found, returns the index where it was
1598 * found. If not found, returns npos.
1601 find_last_of(const basic_string& __str, size_type __pos = npos) const
1602 { return this->find_last_of(__str.data(), __pos, __str.size()); }
1605 * @brief Find last position of a character of C substring.
1606 * @param s C string containing characters to locate.
1607 * @param pos Index of character to search back from (default end).
1608 * @param n Number of characters from s to search for.
1609 * @return Index of last occurrence.
1611 * Starting from @a pos, searches backward for one of the first @a n
1612 * characters of @a s within this string. If found, returns the index
1613 * where it was found. If not found, returns npos.
1616 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
1619 * @brief Find last position of a character of C string.
1620 * @param s C string containing characters to locate.
1621 * @param pos Index of character to search back from (default end).
1622 * @return Index of last occurrence.
1624 * Starting from @a pos, searches backward for one of the characters of
1625 * @a s within this string. If found, returns the index where it was
1626 * found. If not found, returns npos.
1629 find_last_of(const _CharT* __s, size_type __pos = npos) const
1630 { return this->find_last_of(__s, __pos, traits_type::length(__s)); }
1633 * @brief Find last position of a character.
1634 * @param c Character to locate.
1635 * @param pos Index of character to search back from (default 0).
1636 * @return Index of last occurrence.
1638 * Starting from @a pos, searches backward for @a c within this string.
1639 * If found, returns the index where it was found. If not found,
1642 * Note: equivalent to rfind(c, pos).
1645 find_last_of(_CharT __c, size_type __pos = npos) const
1646 { return this->rfind(__c, __pos); }
1649 * @brief Find position of a character not in string.
1650 * @param str String containing characters to avoid.
1651 * @param pos Index of character to search from (default 0).
1652 * @return Index of first occurrence.
1654 * Starting from @a pos, searches forward for a character not contained
1655 * in @a str within this string. If found, returns the index where it
1656 * was found. If not found, returns npos.
1659 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
1660 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
1663 * @brief Find position of a character not in C substring.
1664 * @param s C string containing characters to avoid.
1665 * @param pos Index of character to search from (default 0).
1666 * @param n Number of characters from s to consider.
1667 * @return Index of first occurrence.
1669 * Starting from @a pos, searches forward for a character not contained
1670 * in the first @a n characters of @a s within this string. If found,
1671 * returns the index where it was found. If not found, returns npos.
1674 find_first_not_of(const _CharT* __s, size_type __pos,
1675 size_type __n) const;
1678 * @brief Find position of a character not in C string.
1679 * @param s C string containing characters to avoid.
1680 * @param pos Index of character to search from (default 0).
1681 * @return Index of first occurrence.
1683 * Starting from @a pos, searches forward for a character not contained
1684 * in @a s within this string. If found, returns the index where it
1685 * was found. If not found, returns npos.
1688 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
1689 { return this->find_first_not_of(__s, __pos, traits_type::length(__s)); }
1692 * @brief Find position of a different character.
1693 * @param c Character to avoid.
1694 * @param pos Index of character to search from (default 0).
1695 * @return Index of first occurrence.
1697 * Starting from @a pos, searches forward for a character other than @a c
1698 * within this string. If found, returns the index where it was found.
1699 * If not found, returns npos.
1702 find_first_not_of(_CharT __c, size_type __pos = 0) const;
1705 * @brief Find last position of a character not in string.
1706 * @param str String containing characters to avoid.
1707 * @param pos Index of character to search from (default 0).
1708 * @return Index of first occurrence.
1710 * Starting from @a pos, searches backward for a character not
1711 * contained in @a str within this string. If found, returns the index
1712 * where it was found. If not found, returns npos.
1715 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
1716 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
1719 * @brief Find last position of a character not in C substring.
1720 * @param s C string containing characters to avoid.
1721 * @param pos Index of character to search from (default 0).
1722 * @param n Number of characters from s to consider.
1723 * @return Index of first occurrence.
1725 * Starting from @a pos, searches backward for a character not
1726 * contained in the first @a n characters of @a s within this string.
1727 * If found, returns the index where it was found. If not found,
1731 find_last_not_of(const _CharT* __s, size_type __pos,
1732 size_type __n) const;
1734 * @brief Find position of a character not in C string.
1735 * @param s C string containing characters to avoid.
1736 * @param pos Index of character to search from (default 0).
1737 * @return Index of first occurrence.
1739 * Starting from @a pos, searches backward for a character not
1740 * contained in @a s within this string. If found, returns the index
1741 * where it was found. If not found, returns npos.
1744 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
1745 { return this->find_last_not_of(__s, __pos, traits_type::length(__s)); }
1748 * @brief Find last 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 backward for a character other than
1754 * @a c within this string. If found, returns the index where it was
1755 * found. If not found, returns npos.
1758 find_last_not_of(_CharT __c, size_type __pos = npos) const;
1761 * @brief Get a substring.
1762 * @param pos Index of first character (default 0).
1763 * @param n Number of characters in substring (default remainder).
1764 * @return The new string.
1765 * @throw std::out_of_range If pos > size().
1767 * Construct and return a new string using the @a n characters starting
1768 * at @a pos. If the string is too short, use the remainder of the
1769 * characters. If @a pos is beyond the end of the string, out_of_range
1773 substr(size_type __pos = 0, size_type __n = npos) const
1775 if (__pos > this->size())
1776 __throw_out_of_range(__N("basic_string::substr"));
1777 return basic_string(*this, __pos, __n);
1781 * @brief Compare to a string.
1782 * @param str String to compare against.
1783 * @return Integer < 0, 0, or > 0.
1785 * Returns an integer < 0 if this string is ordered before @a str, 0 if
1786 * their values are equivalent, or > 0 if this string is ordered after
1787 * @a str. If the lengths of @a str and this string are different, the
1788 * shorter one is ordered first. If they are the same, returns the
1789 * result of traits::compare(data(),str.data(),size());
1792 compare(const basic_string& __str) const
1794 const size_type __size = this->size();
1795 const size_type __osize = __str.size();
1796 const size_type __len = std::min(__size, __osize);
1798 int __r = traits_type::compare(_M_data(), __str.data(), __len);
1800 __r = __size - __osize;
1805 * @brief Compare substring to a string.
1806 * @param pos Index of first character of substring.
1807 * @param n Number of characters in substring.
1808 * @param str String to compare against.
1809 * @return Integer < 0, 0, or > 0.
1811 * Form the substring of this string from the @a n characters starting
1812 * at @a pos. Returns an integer < 0 if the substring is ordered
1813 * before @a str, 0 if their values are equivalent, or > 0 if the
1814 * substring is ordered after @a str. If the lengths @a of str and the
1815 * substring are different, the shorter one is ordered first. If they
1816 * are the same, returns the result of
1817 * traits::compare(substring.data(),str.data(),size());
1820 compare(size_type __pos, size_type __n, const basic_string& __str) const;
1823 * @brief Compare substring to a substring.
1824 * @param pos1 Index of first character of substring.
1825 * @param n1 Number of characters in substring.
1826 * @param str String to compare against.
1827 * @param pos2 Index of first character of substring of str.
1828 * @param n2 Number of characters in substring of str.
1829 * @return Integer < 0, 0, or > 0.
1831 * Form the substring of this string from the @a n1 characters starting
1832 * at @a pos1. Form the substring of @a str from the @a n2 characters
1833 * starting at @a pos2. Returns an integer < 0 if this substring is
1834 * ordered before the substring of @a str, 0 if their values are
1835 * equivalent, or > 0 if this substring is ordered after the substring
1836 * of @a str. If the lengths of the substring of @a str and this
1837 * substring are different, the shorter one is ordered first. If they
1838 * are the same, returns the result of
1839 * traits::compare(substring.data(),str.substr(pos2,n2).data(),size());
1842 compare(size_type __pos1, size_type __n1, const basic_string& __str,
1843 size_type __pos2, size_type __n2) const;
1846 * @brief Compare to a C string.
1847 * @param s C string to compare against.
1848 * @return Integer < 0, 0, or > 0.
1850 * Returns an integer < 0 if this string is ordered before @a s, 0 if
1851 * their values are equivalent, or > 0 if this string is ordered after
1852 * @a s. If the lengths of @a s and this string are different, the
1853 * shorter one is ordered first. If they are the same, returns the
1854 * result of traits::compare(data(),s,size());
1857 compare(const _CharT* __s) const;
1859 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1860 // 5 String::compare specification questionable
1862 * @brief Compare substring to a C string.
1863 * @param pos Index of first character of substring.
1864 * @param n1 Number of characters in substring.
1865 * @param s C string to compare against.
1866 * @return Integer < 0, 0, or > 0.
1868 * Form the substring of this string from the @a n1 characters starting
1869 * at @a pos. Returns an integer < 0 if the substring is ordered
1870 * before @a s, 0 if their values are equivalent, or > 0 if the
1871 * substring is ordered after @a s. If the lengths of @a s and the
1872 * substring are different, the shorter one is ordered first. If they
1873 * are the same, returns the result of
1874 * traits::compare(substring.data(),s,size());
1877 compare(size_type __pos, size_type __n1, const _CharT* __s) const;
1880 * @brief Compare substring against a C substring.
1881 * @param pos1 Index of first character of substring.
1882 * @param n1 Number of characters in substring.
1883 * @param s C string to compare against.
1884 * @param n2 Number of characters in substring of s.
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 s from the first @a n
1889 * characters of @a s. Returns an integer < 0 if this substring is
1890 * ordered before the substring of @a s, 0 if their values are
1891 * equivalent, or > 0 if this substring is ordered after the substring
1892 * of @a s. If the lengths of this substring and @a n are different,
1893 * the shorter one is ordered first. If they are the same, returns the
1894 * result of traits::compare(substring.data(),s,size());
1897 compare(size_type __pos, size_type __n1, const _CharT* __s,
1898 size_type __n2) const;
1902 template<typename _CharT, typename _Traits, typename _Alloc>
1903 inline basic_string<_CharT, _Traits, _Alloc>::
1905 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
1909 * @brief Concatenate two strings.
1910 * @param lhs First string.
1911 * @param rhs Last string.
1912 * @return New string with value of @a lhs followed by @a rhs.
1914 template<typename _CharT, typename _Traits, typename _Alloc>
1915 basic_string<_CharT, _Traits, _Alloc>
1916 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
1917 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
1919 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
1920 __str.append(__rhs);
1925 * @brief Concatenate C string and string.
1926 * @param lhs First string.
1927 * @param rhs Last string.
1928 * @return New string with value of @a lhs followed by @a rhs.
1930 template<typename _CharT, typename _Traits, typename _Alloc>
1931 basic_string<_CharT,_Traits,_Alloc>
1932 operator+(const _CharT* __lhs,
1933 const basic_string<_CharT,_Traits,_Alloc>& __rhs);
1936 * @brief Concatenate character and string.
1937 * @param lhs First string.
1938 * @param rhs Last string.
1939 * @return New string with @a lhs followed by @a rhs.
1941 template<typename _CharT, typename _Traits, typename _Alloc>
1942 basic_string<_CharT,_Traits,_Alloc>
1943 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
1946 * @brief Concatenate string and C string.
1947 * @param lhs First string.
1948 * @param rhs Last string.
1949 * @return New string with @a lhs followed by @a rhs.
1951 template<typename _CharT, typename _Traits, typename _Alloc>
1952 inline basic_string<_CharT, _Traits, _Alloc>
1953 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
1954 const _CharT* __rhs)
1956 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
1957 __str.append(__rhs);
1962 * @brief Concatenate string and character.
1963 * @param lhs First string.
1964 * @param rhs Last string.
1965 * @return New string with @a lhs followed by @a rhs.
1967 template<typename _CharT, typename _Traits, typename _Alloc>
1968 inline basic_string<_CharT, _Traits, _Alloc>
1969 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
1971 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1972 typedef typename __string_type::size_type __size_type;
1973 __string_type __str(__lhs);
1974 __str.append(__size_type(1), __rhs);
1980 * @brief Test equivalence of two strings.
1981 * @param lhs First string.
1982 * @param rhs Second string.
1983 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
1985 template<typename _CharT, typename _Traits, typename _Alloc>
1987 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
1988 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
1989 { return __lhs.compare(__rhs) == 0; }
1992 * @brief Test equivalence of C string and string.
1993 * @param lhs C string.
1994 * @param rhs String.
1995 * @return True if @a rhs.compare(@a lhs) == 0. False otherwise.
1997 template<typename _CharT, typename _Traits, typename _Alloc>
1999 operator==(const _CharT* __lhs,
2000 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2001 { return __rhs.compare(__lhs) == 0; }
2004 * @brief Test equivalence of string and C string.
2005 * @param lhs String.
2006 * @param rhs C string.
2007 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2009 template<typename _CharT, typename _Traits, typename _Alloc>
2011 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2012 const _CharT* __rhs)
2013 { return __lhs.compare(__rhs) == 0; }
2017 * @brief Test difference of two strings.
2018 * @param lhs First string.
2019 * @param rhs Second string.
2020 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2022 template<typename _CharT, typename _Traits, typename _Alloc>
2024 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2025 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2026 { return __rhs.compare(__lhs) != 0; }
2029 * @brief Test difference of C string and string.
2030 * @param lhs C string.
2031 * @param rhs String.
2032 * @return True if @a rhs.compare(@a lhs) != 0. False otherwise.
2034 template<typename _CharT, typename _Traits, typename _Alloc>
2036 operator!=(const _CharT* __lhs,
2037 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2038 { return __rhs.compare(__lhs) != 0; }
2041 * @brief Test difference of string and C string.
2042 * @param lhs String.
2043 * @param rhs C string.
2044 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2046 template<typename _CharT, typename _Traits, typename _Alloc>
2048 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2049 const _CharT* __rhs)
2050 { return __lhs.compare(__rhs) != 0; }
2054 * @brief Test if string precedes string.
2055 * @param lhs First string.
2056 * @param rhs Second string.
2057 * @return True if @a lhs precedes @a rhs. False otherwise.
2059 template<typename _CharT, typename _Traits, typename _Alloc>
2061 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2062 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2063 { return __lhs.compare(__rhs) < 0; }
2066 * @brief Test if string precedes C string.
2067 * @param lhs String.
2068 * @param rhs C string.
2069 * @return True if @a lhs precedes @a rhs. False otherwise.
2071 template<typename _CharT, typename _Traits, typename _Alloc>
2073 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2074 const _CharT* __rhs)
2075 { return __lhs.compare(__rhs) < 0; }
2078 * @brief Test if C string precedes string.
2079 * @param lhs C string.
2080 * @param rhs String.
2081 * @return True if @a lhs precedes @a rhs. False otherwise.
2083 template<typename _CharT, typename _Traits, typename _Alloc>
2085 operator<(const _CharT* __lhs,
2086 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2087 { return __rhs.compare(__lhs) > 0; }
2091 * @brief Test if string follows string.
2092 * @param lhs First string.
2093 * @param rhs Second string.
2094 * @return True if @a lhs follows @a rhs. False otherwise.
2096 template<typename _CharT, typename _Traits, typename _Alloc>
2098 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2099 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2100 { return __lhs.compare(__rhs) > 0; }
2103 * @brief Test if string follows C string.
2104 * @param lhs String.
2105 * @param rhs C string.
2106 * @return True if @a lhs follows @a rhs. False otherwise.
2108 template<typename _CharT, typename _Traits, typename _Alloc>
2110 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2111 const _CharT* __rhs)
2112 { return __lhs.compare(__rhs) > 0; }
2115 * @brief Test if C string follows string.
2116 * @param lhs C string.
2117 * @param rhs String.
2118 * @return True if @a lhs follows @a rhs. False otherwise.
2120 template<typename _CharT, typename _Traits, typename _Alloc>
2122 operator>(const _CharT* __lhs,
2123 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2124 { return __rhs.compare(__lhs) < 0; }
2128 * @brief Test if string doesn't follow string.
2129 * @param lhs First string.
2130 * @param rhs Second string.
2131 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2133 template<typename _CharT, typename _Traits, typename _Alloc>
2135 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2136 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2137 { return __lhs.compare(__rhs) <= 0; }
2140 * @brief Test if string doesn't follow C string.
2141 * @param lhs String.
2142 * @param rhs C string.
2143 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2145 template<typename _CharT, typename _Traits, typename _Alloc>
2147 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2148 const _CharT* __rhs)
2149 { return __lhs.compare(__rhs) <= 0; }
2152 * @brief Test if C string doesn't follow string.
2153 * @param lhs C string.
2154 * @param rhs String.
2155 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2157 template<typename _CharT, typename _Traits, typename _Alloc>
2159 operator<=(const _CharT* __lhs,
2160 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2161 { return __rhs.compare(__lhs) >= 0; }
2165 * @brief Test if string doesn't precede string.
2166 * @param lhs First string.
2167 * @param rhs Second string.
2168 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2170 template<typename _CharT, typename _Traits, typename _Alloc>
2172 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2173 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2174 { return __lhs.compare(__rhs) >= 0; }
2177 * @brief Test if string doesn't precede C string.
2178 * @param lhs String.
2179 * @param rhs C string.
2180 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2182 template<typename _CharT, typename _Traits, typename _Alloc>
2184 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2185 const _CharT* __rhs)
2186 { return __lhs.compare(__rhs) >= 0; }
2189 * @brief Test if C string doesn't precede string.
2190 * @param lhs C string.
2191 * @param rhs String.
2192 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2194 template<typename _CharT, typename _Traits, typename _Alloc>
2196 operator>=(const _CharT* __lhs,
2197 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2198 { return __rhs.compare(__lhs) <= 0; }
2202 * @brief Swap contents of two strings.
2203 * @param lhs First string.
2204 * @param rhs Second string.
2206 * Exchanges the contents of @a lhs and @a rhs in constant time.
2208 template<typename _CharT, typename _Traits, typename _Alloc>
2210 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
2211 basic_string<_CharT, _Traits, _Alloc>& __rhs)
2212 { __lhs.swap(__rhs); }
2215 * @brief Read stream into a string.
2216 * @param is Input stream.
2217 * @param str Buffer to store into.
2218 * @return Reference to the input stream.
2220 * Stores characters from @a is into @a str until whitespace is found, the
2221 * end of the stream is encountered, or str.max_size() is reached. If
2222 * is.width() is non-zero, that is the limit on the number of characters
2223 * stored into @a str. Any previous contents of @a str are erased.
2225 template<typename _CharT, typename _Traits, typename _Alloc>
2226 basic_istream<_CharT, _Traits>&
2227 operator>>(basic_istream<_CharT, _Traits>& __is,
2228 basic_string<_CharT, _Traits, _Alloc>& __str);
2231 * @brief Write string to a stream.
2232 * @param os Output stream.
2233 * @param str String to write out.
2234 * @return Reference to the output stream.
2236 * Output characters of @a str into os following the same rules as for
2237 * writing a C string.
2239 template<typename _CharT, typename _Traits, typename _Alloc>
2240 basic_ostream<_CharT, _Traits>&
2241 operator<<(basic_ostream<_CharT, _Traits>& __os,
2242 const basic_string<_CharT, _Traits, _Alloc>& __str);
2245 * @brief Read a line from stream into a string.
2246 * @param is Input stream.
2247 * @param str Buffer to store into.
2248 * @param delim Character marking end of line.
2249 * @return Reference to the input stream.
2251 * Stores characters from @a is into @a str until @a delim is found, the
2252 * end of the stream is encountered, or str.max_size() is reached. If
2253 * is.width() is non-zero, that is the limit on the number of characters
2254 * stored into @a str. Any previous contents of @a str are erased. If @a
2255 * delim was encountered, it is extracted but not stored into @a str.
2257 template<typename _CharT, typename _Traits, typename _Alloc>
2258 basic_istream<_CharT,_Traits>&
2259 getline(basic_istream<_CharT, _Traits>& __is,
2260 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
2263 * @brief Read a line from stream into a string.
2264 * @param is Input stream.
2265 * @param str Buffer to store into.
2266 * @return Reference to the input stream.
2268 * Stores characters from is into @a str until '\n' is found, the end of
2269 * the stream is encountered, or str.max_size() is reached. If is.width()
2270 * is non-zero, that is the limit on the number of characters stored into
2271 * @a str. Any previous contents of @a str are erased. If end of line was
2272 * encountered, it is extracted but not stored into @a str.
2274 template<typename _CharT, typename _Traits, typename _Alloc>
2275 inline basic_istream<_CharT,_Traits>&
2276 getline(basic_istream<_CharT, _Traits>& __is,
2277 basic_string<_CharT, _Traits, _Alloc>& __str);
2280 #endif /* _BASIC_STRING_H */