Merged revisions 143552,143554,143557,143560,143562,143564-143567,143570-143573,14357...
[official-gcc.git] / libstdc++-v3 / include / bits / basic_string.h
blob2a49837029dc738bc89ff6c82bc4db083eec808d
1 // Components for manipulating sequences of characters -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008
5 // Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2, or (at your option)
11 // any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING. If not, write to the Free
20 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // USA.
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction. Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License. This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
32 /** @file basic_string.h
33 * This is an internal header file, included by other library headers.
34 * You should not attempt to use it directly.
38 // ISO C++ 14882: 21 Strings library
41 #ifndef _BASIC_STRING_H
42 #define _BASIC_STRING_H 1
44 #pragma GCC system_header
46 #include <ext/atomicity.h>
47 #include <debug/debug.h>
48 #include <initializer_list>
50 _GLIBCXX_BEGIN_NAMESPACE(std)
52 /**
53 * @class basic_string basic_string.h <string>
54 * @brief Managing sequences of characters and character-like objects.
56 * @ingroup sequences
58 * Meets the requirements of a <a href="tables.html#65">container</a>, a
59 * <a href="tables.html#66">reversible container</a>, and a
60 * <a href="tables.html#67">sequence</a>. Of the
61 * <a href="tables.html#68">optional sequence requirements</a>, only
62 * @c push_back, @c at, and array access are supported.
64 * @doctodo
67 * Documentation? What's that?
68 * Nathan Myers <ncm@cantrip.org>.
70 * A string looks like this:
72 * @code
73 * [_Rep]
74 * _M_length
75 * [basic_string<char_type>] _M_capacity
76 * _M_dataplus _M_refcount
77 * _M_p ----------------> unnamed array of char_type
78 * @endcode
80 * Where the _M_p points to the first character in the string, and
81 * you cast it to a pointer-to-_Rep and subtract 1 to get a
82 * pointer to the header.
84 * This approach has the enormous advantage that a string object
85 * requires only one allocation. All the ugliness is confined
86 * within a single pair of inline functions, which each compile to
87 * a single "add" instruction: _Rep::_M_data(), and
88 * string::_M_rep(); and the allocation function which gets a
89 * block of raw bytes and with room enough and constructs a _Rep
90 * object at the front.
92 * The reason you want _M_data pointing to the character array and
93 * not the _Rep is so that the debugger can see the string
94 * contents. (Probably we should add a non-inline member to get
95 * the _Rep for the debugger to use, so users can check the actual
96 * string length.)
98 * Note that the _Rep object is a POD so that you can have a
99 * static "empty string" _Rep object already "constructed" before
100 * static constructors have run. The reference-count encoding is
101 * chosen so that a 0 indicates one reference, so you never try to
102 * destroy the empty-string _Rep object.
104 * All but the last paragraph is considered pretty conventional
105 * for a C++ string implementation.
107 // 21.3 Template class basic_string
108 template<typename _CharT, typename _Traits, typename _Alloc>
109 class basic_string
111 typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
113 // Types:
114 public:
115 typedef _Traits traits_type;
116 typedef typename _Traits::char_type value_type;
117 typedef _Alloc allocator_type;
118 typedef typename _CharT_alloc_type::size_type size_type;
119 typedef typename _CharT_alloc_type::difference_type difference_type;
120 typedef typename _CharT_alloc_type::reference reference;
121 typedef typename _CharT_alloc_type::const_reference const_reference;
122 typedef typename _CharT_alloc_type::pointer pointer;
123 typedef typename _CharT_alloc_type::const_pointer const_pointer;
124 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
125 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
126 const_iterator;
127 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
128 typedef std::reverse_iterator<iterator> reverse_iterator;
130 private:
131 // _Rep: string representation
132 // Invariants:
133 // 1. String really contains _M_length + 1 characters: due to 21.3.4
134 // must be kept null-terminated.
135 // 2. _M_capacity >= _M_length
136 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
137 // 3. _M_refcount has three states:
138 // -1: leaked, one reference, no ref-copies allowed, non-const.
139 // 0: one reference, non-const.
140 // n>0: n + 1 references, operations require a lock, const.
141 // 4. All fields==0 is an empty string, given the extra storage
142 // beyond-the-end for a null terminator; thus, the shared
143 // empty string representation needs no constructor.
145 struct _Rep_base
147 size_type _M_length;
148 size_type _M_capacity;
149 _Atomic_word _M_refcount;
152 struct _Rep : _Rep_base
154 // Types:
155 typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc;
157 // (Public) Data members:
159 // The maximum number of individual char_type elements of an
160 // individual string is determined by _S_max_size. This is the
161 // value that will be returned by max_size(). (Whereas npos
162 // is the maximum number of bytes the allocator can allocate.)
163 // If one was to divvy up the theoretical largest size string,
164 // with a terminating character and m _CharT elements, it'd
165 // look like this:
166 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
167 // Solving for m:
168 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
169 // In addition, this implementation quarters this amount.
170 static const size_type _S_max_size;
171 static const _CharT _S_terminal;
173 // The following storage is init'd to 0 by the linker, resulting
174 // (carefully) in an empty string with one reference.
175 static size_type _S_empty_rep_storage[];
177 static _Rep&
178 _S_empty_rep()
180 // NB: Mild hack to avoid strict-aliasing warnings. Note that
181 // _S_empty_rep_storage is never modified and the punning should
182 // be reasonably safe in this case.
183 void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage);
184 return *reinterpret_cast<_Rep*>(__p);
187 bool
188 _M_is_leaked() const
189 { return this->_M_refcount < 0; }
191 bool
192 _M_is_shared() const
193 { return this->_M_refcount > 0; }
195 void
196 _M_set_leaked()
197 { this->_M_refcount = -1; }
199 void
200 _M_set_sharable()
201 { this->_M_refcount = 0; }
203 void
204 _M_set_length_and_sharable(size_type __n)
206 this->_M_set_sharable(); // One reference.
207 this->_M_length = __n;
208 traits_type::assign(this->_M_refdata()[__n], _S_terminal);
209 // grrr. (per 21.3.4)
210 // You cannot leave those LWG people alone for a second.
213 _CharT*
214 _M_refdata() throw()
215 { return reinterpret_cast<_CharT*>(this + 1); }
217 _CharT*
218 _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
220 return (!_M_is_leaked() && __alloc1 == __alloc2)
221 ? _M_refcopy() : _M_clone(__alloc1);
224 // Create & Destroy
225 static _Rep*
226 _S_create(size_type, size_type, const _Alloc&);
228 void
229 _M_dispose(const _Alloc& __a)
231 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
232 if (__builtin_expect(this != &_S_empty_rep(), false))
233 #endif
234 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
235 -1) <= 0)
236 _M_destroy(__a);
237 } // XXX MT
239 void
240 _M_destroy(const _Alloc&) throw();
242 _CharT*
243 _M_refcopy() throw()
245 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
246 if (__builtin_expect(this != &_S_empty_rep(), false))
247 #endif
248 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
249 return _M_refdata();
250 } // XXX MT
252 _CharT*
253 _M_clone(const _Alloc&, size_type __res = 0);
256 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
257 struct _Alloc_hider : _Alloc
259 _Alloc_hider(_CharT* __dat, const _Alloc& __a)
260 : _Alloc(__a), _M_p(__dat) { }
262 _CharT* _M_p; // The actual data.
265 public:
266 // Data Members (public):
267 // NB: This is an unsigned type, and thus represents the maximum
268 // size that the allocator can hold.
269 /// Value returned by various member functions when they fail.
270 static const size_type npos = static_cast<size_type>(-1);
272 private:
273 // Data Members (private):
274 mutable _Alloc_hider _M_dataplus;
276 _CharT*
277 _M_data() const
278 { return _M_dataplus._M_p; }
280 _CharT*
281 _M_data(_CharT* __p)
282 { return (_M_dataplus._M_p = __p); }
284 _Rep*
285 _M_rep() const
286 { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
288 // For the internal use we have functions similar to `begin'/`end'
289 // but they do not call _M_leak.
290 iterator
291 _M_ibegin() const
292 { return iterator(_M_data()); }
294 iterator
295 _M_iend() const
296 { return iterator(_M_data() + this->size()); }
298 void
299 _M_leak() // for use in begin() & non-const op[]
301 if (!_M_rep()->_M_is_leaked())
302 _M_leak_hard();
305 size_type
306 _M_check(size_type __pos, const char* __s) const
308 if (__pos > this->size())
309 __throw_out_of_range(__N(__s));
310 return __pos;
313 void
314 _M_check_length(size_type __n1, size_type __n2, const char* __s) const
316 if (this->max_size() - (this->size() - __n1) < __n2)
317 __throw_length_error(__N(__s));
320 // NB: _M_limit doesn't check for a bad __pos value.
321 size_type
322 _M_limit(size_type __pos, size_type __off) const
324 const bool __testoff = __off < this->size() - __pos;
325 return __testoff ? __off : this->size() - __pos;
328 // True if _Rep and source do not overlap.
329 bool
330 _M_disjunct(const _CharT* __s) const
332 return (less<const _CharT*>()(__s, _M_data())
333 || less<const _CharT*>()(_M_data() + this->size(), __s));
336 // When __n = 1 way faster than the general multichar
337 // traits_type::copy/move/assign.
338 static void
339 _M_copy(_CharT* __d, const _CharT* __s, size_type __n)
341 if (__n == 1)
342 traits_type::assign(*__d, *__s);
343 else
344 traits_type::copy(__d, __s, __n);
347 static void
348 _M_move(_CharT* __d, const _CharT* __s, size_type __n)
350 if (__n == 1)
351 traits_type::assign(*__d, *__s);
352 else
353 traits_type::move(__d, __s, __n);
356 static void
357 _M_assign(_CharT* __d, size_type __n, _CharT __c)
359 if (__n == 1)
360 traits_type::assign(*__d, __c);
361 else
362 traits_type::assign(__d, __n, __c);
365 // _S_copy_chars is a separate template to permit specialization
366 // to optimize for the common case of pointers as iterators.
367 template<class _Iterator>
368 static void
369 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
371 for (; __k1 != __k2; ++__k1, ++__p)
372 traits_type::assign(*__p, *__k1); // These types are off.
375 static void
376 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2)
377 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
379 static void
380 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
381 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
383 static void
384 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
385 { _M_copy(__p, __k1, __k2 - __k1); }
387 static void
388 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
389 { _M_copy(__p, __k1, __k2 - __k1); }
391 static int
392 _S_compare(size_type __n1, size_type __n2)
394 const difference_type __d = difference_type(__n1 - __n2);
396 if (__d > __gnu_cxx::__numeric_traits<int>::__max)
397 return __gnu_cxx::__numeric_traits<int>::__max;
398 else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
399 return __gnu_cxx::__numeric_traits<int>::__min;
400 else
401 return int(__d);
404 void
405 _M_mutate(size_type __pos, size_type __len1, size_type __len2);
407 void
408 _M_leak_hard();
410 static _Rep&
411 _S_empty_rep()
412 { return _Rep::_S_empty_rep(); }
414 public:
415 // Construct/copy/destroy:
416 // NB: We overload ctors in some cases instead of using default
417 // arguments, per 17.4.4.4 para. 2 item 2.
420 * @brief Default constructor creates an empty string.
422 inline
423 basic_string();
426 * @brief Construct an empty string using allocator @a a.
428 explicit
429 basic_string(const _Alloc& __a);
431 // NB: per LWG issue 42, semantics different from IS:
433 * @brief Construct string with copy of value of @a str.
434 * @param str Source string.
436 basic_string(const basic_string& __str);
438 * @brief Construct string as copy of a substring.
439 * @param str Source string.
440 * @param pos Index of first character to copy from.
441 * @param n Number of characters to copy (default remainder).
443 basic_string(const basic_string& __str, size_type __pos,
444 size_type __n = npos);
446 * @brief Construct string as copy of a substring.
447 * @param str Source string.
448 * @param pos Index of first character to copy from.
449 * @param n Number of characters to copy.
450 * @param a Allocator to use.
452 basic_string(const basic_string& __str, size_type __pos,
453 size_type __n, const _Alloc& __a);
456 * @brief Construct string initialized by a character array.
457 * @param s Source character array.
458 * @param n Number of characters to copy.
459 * @param a Allocator to use (default is default allocator).
461 * NB: @a s must have at least @a n characters, '\0' has no special
462 * meaning.
464 basic_string(const _CharT* __s, size_type __n,
465 const _Alloc& __a = _Alloc());
467 * @brief Construct string as copy of a C string.
468 * @param s Source C string.
469 * @param a Allocator to use (default is default allocator).
471 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
473 * @brief Construct string as multiple characters.
474 * @param n Number of characters.
475 * @param c Character to use.
476 * @param a Allocator to use (default is default allocator).
478 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
480 #ifdef __GXX_EXPERIMENTAL_CXX0X__
482 * @brief Construct string from an initializer list.
483 * @param l std::initializer_list of characters.
484 * @param a Allocator to use (default is default allocator).
486 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc());
487 #endif // __GXX_EXPERIMENTAL_CXX0X__
490 * @brief Construct string as copy of a range.
491 * @param beg Start of range.
492 * @param end End of range.
493 * @param a Allocator to use (default is default allocator).
495 template<class _InputIterator>
496 basic_string(_InputIterator __beg, _InputIterator __end,
497 const _Alloc& __a = _Alloc());
500 * @brief Destroy the string instance.
502 ~basic_string()
503 { _M_rep()->_M_dispose(this->get_allocator()); }
506 * @brief Assign the value of @a str to this string.
507 * @param str Source string.
509 basic_string&
510 operator=(const basic_string& __str)
511 { return this->assign(__str); }
514 * @brief Copy contents of @a s into this string.
515 * @param s Source null-terminated string.
517 basic_string&
518 operator=(const _CharT* __s)
519 { return this->assign(__s); }
522 * @brief Set value to string of length 1.
523 * @param c Source character.
525 * Assigning to a character makes this string length 1 and
526 * (*this)[0] == @a c.
528 basic_string&
529 operator=(_CharT __c)
531 this->assign(1, __c);
532 return *this;
535 #ifdef __GXX_EXPERIMENTAL_CXX0X__
537 * @brief Set value to string constructed from initializer list.
538 * @param l std::initializer_list.
540 basic_string&
541 operator=(initializer_list<_CharT> __l)
543 this->assign (__l.begin(), __l.end());
544 return *this;
546 #endif // __GXX_EXPERIMENTAL_CXX0X__
548 // Iterators:
550 * Returns a read/write iterator that points to the first character in
551 * the %string. Unshares the string.
553 iterator
554 begin()
556 _M_leak();
557 return iterator(_M_data());
561 * Returns a read-only (constant) iterator that points to the first
562 * character in the %string.
564 const_iterator
565 begin() const
566 { return const_iterator(_M_data()); }
569 * Returns a read/write iterator that points one past the last
570 * character in the %string. Unshares the string.
572 iterator
573 end()
575 _M_leak();
576 return iterator(_M_data() + this->size());
580 * Returns a read-only (constant) iterator that points one past the
581 * last character in the %string.
583 const_iterator
584 end() const
585 { return const_iterator(_M_data() + this->size()); }
588 * Returns a read/write reverse iterator that points to the last
589 * character in the %string. Iteration is done in reverse element
590 * order. Unshares the string.
592 reverse_iterator
593 rbegin()
594 { return reverse_iterator(this->end()); }
597 * Returns a read-only (constant) reverse iterator that points
598 * to the last character in the %string. Iteration is done in
599 * reverse element order.
601 const_reverse_iterator
602 rbegin() const
603 { return const_reverse_iterator(this->end()); }
606 * Returns a read/write reverse iterator that points to one before the
607 * first character in the %string. Iteration is done in reverse
608 * element order. Unshares the string.
610 reverse_iterator
611 rend()
612 { return reverse_iterator(this->begin()); }
615 * Returns a read-only (constant) reverse iterator that points
616 * to one before the first character in the %string. Iteration
617 * is done in reverse element order.
619 const_reverse_iterator
620 rend() const
621 { return const_reverse_iterator(this->begin()); }
623 public:
624 // Capacity:
625 /// Returns the number of characters in the string, not including any
626 /// null-termination.
627 size_type
628 size() const
629 { return _M_rep()->_M_length; }
631 /// Returns the number of characters in the string, not including any
632 /// null-termination.
633 size_type
634 length() const
635 { return _M_rep()->_M_length; }
637 /// Returns the size() of the largest possible %string.
638 size_type
639 max_size() const
640 { return _Rep::_S_max_size; }
643 * @brief Resizes the %string to the specified number of characters.
644 * @param n Number of characters the %string should contain.
645 * @param c Character to fill any new elements.
647 * This function will %resize the %string to the specified
648 * number of characters. If the number is smaller than the
649 * %string's current size the %string is truncated, otherwise
650 * the %string is extended and new elements are set to @a c.
652 void
653 resize(size_type __n, _CharT __c);
656 * @brief Resizes the %string to the specified number of characters.
657 * @param n Number of characters the %string should contain.
659 * This function will resize the %string to the specified length. If
660 * the new size is smaller than the %string's current size the %string
661 * is truncated, otherwise the %string is extended and new characters
662 * are default-constructed. For basic types such as char, this means
663 * setting them to 0.
665 void
666 resize(size_type __n)
667 { this->resize(__n, _CharT()); }
670 * Returns the total number of characters that the %string can hold
671 * before needing to allocate more memory.
673 size_type
674 capacity() const
675 { return _M_rep()->_M_capacity; }
678 * @brief Attempt to preallocate enough memory for specified number of
679 * characters.
680 * @param res_arg Number of characters required.
681 * @throw std::length_error If @a res_arg exceeds @c max_size().
683 * This function attempts to reserve enough memory for the
684 * %string to hold the specified number of characters. If the
685 * number requested is more than max_size(), length_error is
686 * thrown.
688 * The advantage of this function is that if optimal code is a
689 * necessity and the user can determine the string length that will be
690 * required, the user can reserve the memory in %advance, and thus
691 * prevent a possible reallocation of memory and copying of %string
692 * data.
694 void
695 reserve(size_type __res_arg = 0);
698 * Erases the string, making it empty.
700 void
701 clear()
702 { _M_mutate(0, this->size(), 0); }
705 * Returns true if the %string is empty. Equivalent to *this == "".
707 bool
708 empty() const
709 { return this->size() == 0; }
711 // Element access:
713 * @brief Subscript access to the data contained in the %string.
714 * @param pos The index of the character to access.
715 * @return Read-only (constant) reference to the character.
717 * This operator allows for easy, array-style, data access.
718 * Note that data access with this operator is unchecked and
719 * out_of_range lookups are not defined. (For checked lookups
720 * see at().)
722 const_reference
723 operator[] (size_type __pos) const
725 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
726 return _M_data()[__pos];
730 * @brief Subscript access to the data contained in the %string.
731 * @param pos The index of the character to access.
732 * @return Read/write reference to the character.
734 * This operator allows for easy, array-style, data access.
735 * Note that data access with this operator is unchecked and
736 * out_of_range lookups are not defined. (For checked lookups
737 * see at().) Unshares the string.
739 reference
740 operator[](size_type __pos)
742 // allow pos == size() as v3 extension:
743 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
744 // but be strict in pedantic mode:
745 _GLIBCXX_DEBUG_PEDASSERT(__pos < size());
746 _M_leak();
747 return _M_data()[__pos];
751 * @brief Provides access to the data contained in the %string.
752 * @param n The index of the character to access.
753 * @return Read-only (const) reference to the character.
754 * @throw std::out_of_range If @a n is an invalid index.
756 * This function provides for safer data access. The parameter is
757 * first checked that it is in the range of the string. The function
758 * throws out_of_range if the check fails.
760 const_reference
761 at(size_type __n) const
763 if (__n >= this->size())
764 __throw_out_of_range(__N("basic_string::at"));
765 return _M_data()[__n];
769 * @brief Provides access to the data contained in the %string.
770 * @param n The index of the character to access.
771 * @return Read/write reference to the character.
772 * @throw std::out_of_range If @a n is an invalid index.
774 * This function provides for safer data access. The parameter is
775 * first checked that it is in the range of the string. The function
776 * throws out_of_range if the check fails. Success results in
777 * unsharing the string.
779 reference
780 at(size_type __n)
782 if (__n >= size())
783 __throw_out_of_range(__N("basic_string::at"));
784 _M_leak();
785 return _M_data()[__n];
788 // Modifiers:
790 * @brief Append a string to this string.
791 * @param str The string to append.
792 * @return Reference to this string.
794 basic_string&
795 operator+=(const basic_string& __str)
796 { return this->append(__str); }
799 * @brief Append a C string.
800 * @param s The C string to append.
801 * @return Reference to this string.
803 basic_string&
804 operator+=(const _CharT* __s)
805 { return this->append(__s); }
808 * @brief Append a character.
809 * @param c The character to append.
810 * @return Reference to this string.
812 basic_string&
813 operator+=(_CharT __c)
815 this->push_back(__c);
816 return *this;
819 #ifdef __GXX_EXPERIMENTAL_CXX0X__
821 * @brief Append an initializer_list of characters.
822 * @param l The initializer_list of characters to be appended.
823 * @return Reference to this string.
825 basic_string&
826 operator+=(initializer_list<_CharT> __l)
827 { return this->append(__l.begin(), __l.end()); }
828 #endif // __GXX_EXPERIMENTAL_CXX0X__
831 * @brief Append a string to this string.
832 * @param str The string to append.
833 * @return Reference to this string.
835 basic_string&
836 append(const basic_string& __str);
839 * @brief Append a substring.
840 * @param str The string to append.
841 * @param pos Index of the first character of str to append.
842 * @param n The number of characters to append.
843 * @return Reference to this string.
844 * @throw std::out_of_range if @a pos is not a valid index.
846 * This function appends @a n characters from @a str starting at @a pos
847 * to this string. If @a n is is larger than the number of available
848 * characters in @a str, the remainder of @a str is appended.
850 basic_string&
851 append(const basic_string& __str, size_type __pos, size_type __n);
854 * @brief Append a C substring.
855 * @param s The C string to append.
856 * @param n The number of characters to append.
857 * @return Reference to this string.
859 basic_string&
860 append(const _CharT* __s, size_type __n);
863 * @brief Append a C string.
864 * @param s The C string to append.
865 * @return Reference to this string.
867 basic_string&
868 append(const _CharT* __s)
870 __glibcxx_requires_string(__s);
871 return this->append(__s, traits_type::length(__s));
875 * @brief Append multiple characters.
876 * @param n The number of characters to append.
877 * @param c The character to use.
878 * @return Reference to this string.
880 * Appends n copies of c to this string.
882 basic_string&
883 append(size_type __n, _CharT __c);
885 #ifdef __GXX_EXPERIMENTAL_CXX0X__
887 * @brief Append an initializer_list of characters.
888 * @param l The initializer_list of characters to append.
889 * @return Reference to this string.
891 basic_string&
892 append(initializer_list<_CharT> __l)
893 { return this->append(__l.begin(), __l.end()); }
894 #endif // __GXX_EXPERIMENTAL_CXX0X__
897 * @brief Append a range of characters.
898 * @param first Iterator referencing the first character to append.
899 * @param last Iterator marking the end of the range.
900 * @return Reference to this string.
902 * Appends characters in the range [first,last) to this string.
904 template<class _InputIterator>
905 basic_string&
906 append(_InputIterator __first, _InputIterator __last)
907 { return this->replace(_M_iend(), _M_iend(), __first, __last); }
910 * @brief Append a single character.
911 * @param c Character to append.
913 void
914 push_back(_CharT __c)
916 const size_type __len = 1 + this->size();
917 if (__len > this->capacity() || _M_rep()->_M_is_shared())
918 this->reserve(__len);
919 traits_type::assign(_M_data()[this->size()], __c);
920 _M_rep()->_M_set_length_and_sharable(__len);
924 * @brief Set value to contents of another string.
925 * @param str Source string to use.
926 * @return Reference to this string.
928 basic_string&
929 assign(const basic_string& __str);
932 * @brief Set value to a substring of a string.
933 * @param str The string to use.
934 * @param pos Index of the first character of str.
935 * @param n Number of characters to use.
936 * @return Reference to this string.
937 * @throw std::out_of_range if @a pos is not a valid index.
939 * This function sets this string to the substring of @a str consisting
940 * of @a n characters at @a pos. If @a n is is larger than the number
941 * of available characters in @a str, the remainder of @a str is used.
943 basic_string&
944 assign(const basic_string& __str, size_type __pos, size_type __n)
945 { return this->assign(__str._M_data()
946 + __str._M_check(__pos, "basic_string::assign"),
947 __str._M_limit(__pos, __n)); }
950 * @brief Set value to a C substring.
951 * @param s The C string to use.
952 * @param n Number of characters to use.
953 * @return Reference to this string.
955 * This function sets the value of this string to the first @a n
956 * characters of @a s. If @a n is is larger than the number of
957 * available characters in @a s, the remainder of @a s is used.
959 basic_string&
960 assign(const _CharT* __s, size_type __n);
963 * @brief Set value to contents of a C string.
964 * @param s The C string to use.
965 * @return Reference to this string.
967 * This function sets the value of this string to the value of @a s.
968 * The data is copied, so there is no dependence on @a s once the
969 * function returns.
971 basic_string&
972 assign(const _CharT* __s)
974 __glibcxx_requires_string(__s);
975 return this->assign(__s, traits_type::length(__s));
979 * @brief Set value to multiple characters.
980 * @param n Length of the resulting string.
981 * @param c The character to use.
982 * @return Reference to this string.
984 * This function sets the value of this string to @a n copies of
985 * character @a c.
987 basic_string&
988 assign(size_type __n, _CharT __c)
989 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
992 * @brief Set value to a range of characters.
993 * @param first Iterator referencing the first character to append.
994 * @param last Iterator marking the end of the range.
995 * @return Reference to this string.
997 * Sets value of string to characters in the range [first,last).
999 template<class _InputIterator>
1000 basic_string&
1001 assign(_InputIterator __first, _InputIterator __last)
1002 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
1004 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1006 * @brief Set value to an initializer_list of characters.
1007 * @param l The initializer_list of characters to assign.
1008 * @return Reference to this string.
1010 basic_string&
1011 assign(initializer_list<_CharT> __l)
1012 { return this->assign(__l.begin(), __l.end()); }
1013 #endif // __GXX_EXPERIMENTAL_CXX0X__
1016 * @brief Insert multiple characters.
1017 * @param p Iterator referencing location in string to insert at.
1018 * @param n Number of characters to insert
1019 * @param c The character to insert.
1020 * @throw std::length_error If new length exceeds @c max_size().
1022 * Inserts @a n copies of character @a c starting at the position
1023 * referenced by iterator @a p. If adding characters causes the length
1024 * to exceed max_size(), length_error is thrown. The value of the
1025 * string doesn't change if an error is thrown.
1027 void
1028 insert(iterator __p, size_type __n, _CharT __c)
1029 { this->replace(__p, __p, __n, __c); }
1032 * @brief Insert a range of characters.
1033 * @param p Iterator referencing location in string to insert at.
1034 * @param beg Start of range.
1035 * @param end End of range.
1036 * @throw std::length_error If new length exceeds @c max_size().
1038 * Inserts characters in range [beg,end). If adding characters causes
1039 * the length to exceed max_size(), length_error is thrown. The value
1040 * of the string doesn't change if an error is thrown.
1042 template<class _InputIterator>
1043 void
1044 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
1045 { this->replace(__p, __p, __beg, __end); }
1047 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1049 * @brief Insert an initializer_list of characters.
1050 * @param p Iterator referencing location in string to insert at.
1051 * @param l The initializer_list of characters to insert.
1052 * @throw std::length_error If new length exceeds @c max_size().
1054 void
1055 insert(iterator __p, initializer_list<_CharT> __l)
1056 { this->insert(__p, __l.begin(), __l.end()); }
1057 #endif // __GXX_EXPERIMENTAL_CXX0X__
1060 * @brief Insert value of a string.
1061 * @param pos1 Iterator referencing location in string to insert at.
1062 * @param str The string to insert.
1063 * @return Reference to this string.
1064 * @throw std::length_error If new length exceeds @c max_size().
1066 * Inserts value of @a str starting at @a pos1. If adding characters
1067 * causes the length to exceed max_size(), length_error is thrown. The
1068 * value of the string doesn't change if an error is thrown.
1070 basic_string&
1071 insert(size_type __pos1, const basic_string& __str)
1072 { return this->insert(__pos1, __str, size_type(0), __str.size()); }
1075 * @brief Insert a substring.
1076 * @param pos1 Iterator referencing location in string to insert at.
1077 * @param str The string to insert.
1078 * @param pos2 Start of characters in str to insert.
1079 * @param n Number of characters to insert.
1080 * @return Reference to this string.
1081 * @throw std::length_error If new length exceeds @c max_size().
1082 * @throw std::out_of_range If @a pos1 > size() or
1083 * @a pos2 > @a str.size().
1085 * Starting at @a pos1, insert @a n character of @a str beginning with
1086 * @a pos2. If adding characters causes the length to exceed
1087 * max_size(), length_error is thrown. If @a pos1 is beyond the end of
1088 * this string or @a pos2 is beyond the end of @a str, out_of_range is
1089 * thrown. The value of the string doesn't change if an error is
1090 * thrown.
1092 basic_string&
1093 insert(size_type __pos1, const basic_string& __str,
1094 size_type __pos2, size_type __n)
1095 { return this->insert(__pos1, __str._M_data()
1096 + __str._M_check(__pos2, "basic_string::insert"),
1097 __str._M_limit(__pos2, __n)); }
1100 * @brief Insert a C substring.
1101 * @param pos Iterator referencing location in string to insert at.
1102 * @param s The C string to insert.
1103 * @param n The number of characters to insert.
1104 * @return Reference to this string.
1105 * @throw std::length_error If new length exceeds @c max_size().
1106 * @throw std::out_of_range If @a pos is beyond the end of this
1107 * string.
1109 * Inserts the first @a n characters of @a s starting at @a pos. If
1110 * adding characters causes the length to exceed max_size(),
1111 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1112 * thrown. The value of the string doesn't change if an error is
1113 * thrown.
1115 basic_string&
1116 insert(size_type __pos, const _CharT* __s, size_type __n);
1119 * @brief Insert a C string.
1120 * @param pos Iterator referencing location in string to insert at.
1121 * @param s The C string to insert.
1122 * @return Reference to this string.
1123 * @throw std::length_error If new length exceeds @c max_size().
1124 * @throw std::out_of_range If @a pos is beyond the end of this
1125 * string.
1127 * Inserts the first @a n characters of @a s starting at @a pos. If
1128 * adding characters causes the length to exceed max_size(),
1129 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1130 * thrown. The value of the string doesn't change if an error is
1131 * thrown.
1133 basic_string&
1134 insert(size_type __pos, const _CharT* __s)
1136 __glibcxx_requires_string(__s);
1137 return this->insert(__pos, __s, traits_type::length(__s));
1141 * @brief Insert multiple characters.
1142 * @param pos Index in string to insert at.
1143 * @param n Number of characters to insert
1144 * @param c The character to insert.
1145 * @return Reference to this string.
1146 * @throw std::length_error If new length exceeds @c max_size().
1147 * @throw std::out_of_range If @a pos is beyond the end of this
1148 * string.
1150 * Inserts @a n copies of character @a c starting at index @a pos. If
1151 * adding characters causes the length to exceed max_size(),
1152 * length_error is thrown. If @a pos > length(), out_of_range is
1153 * thrown. The value of the string doesn't change if an error is
1154 * thrown.
1156 basic_string&
1157 insert(size_type __pos, size_type __n, _CharT __c)
1158 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
1159 size_type(0), __n, __c); }
1162 * @brief Insert one character.
1163 * @param p Iterator referencing position in string to insert at.
1164 * @param c The character to insert.
1165 * @return Iterator referencing newly inserted char.
1166 * @throw std::length_error If new length exceeds @c max_size().
1168 * Inserts character @a c at position referenced by @a p. If adding
1169 * character causes the length to exceed max_size(), length_error is
1170 * thrown. If @a p is beyond end of string, out_of_range is thrown.
1171 * The value of the string doesn't change if an error is thrown.
1173 iterator
1174 insert(iterator __p, _CharT __c)
1176 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1177 const size_type __pos = __p - _M_ibegin();
1178 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1179 _M_rep()->_M_set_leaked();
1180 return iterator(_M_data() + __pos);
1184 * @brief Remove characters.
1185 * @param pos Index of first character to remove (default 0).
1186 * @param n Number of characters to remove (default remainder).
1187 * @return Reference to this string.
1188 * @throw std::out_of_range If @a pos is beyond the end of this
1189 * string.
1191 * Removes @a n characters from this string starting at @a pos. The
1192 * length of the string is reduced by @a n. If there are < @a n
1193 * characters to remove, the remainder of the string is truncated. If
1194 * @a p is beyond end of string, out_of_range is thrown. The value of
1195 * the string doesn't change if an error is thrown.
1197 basic_string&
1198 erase(size_type __pos = 0, size_type __n = npos)
1200 _M_mutate(_M_check(__pos, "basic_string::erase"),
1201 _M_limit(__pos, __n), size_type(0));
1202 return *this;
1206 * @brief Remove one character.
1207 * @param position Iterator referencing the character to remove.
1208 * @return iterator referencing same location after removal.
1210 * Removes the character at @a position from this string. The value
1211 * of the string doesn't change if an error is thrown.
1213 iterator
1214 erase(iterator __position)
1216 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
1217 && __position < _M_iend());
1218 const size_type __pos = __position - _M_ibegin();
1219 _M_mutate(__pos, size_type(1), size_type(0));
1220 _M_rep()->_M_set_leaked();
1221 return iterator(_M_data() + __pos);
1225 * @brief Remove a range of characters.
1226 * @param first Iterator referencing the first character to remove.
1227 * @param last Iterator referencing the end of the range.
1228 * @return Iterator referencing location of first after removal.
1230 * Removes the characters in the range [first,last) from this string.
1231 * The value of the string doesn't change if an error is thrown.
1233 iterator
1234 erase(iterator __first, iterator __last)
1236 _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
1237 && __last <= _M_iend());
1238 const size_type __pos = __first - _M_ibegin();
1239 _M_mutate(__pos, __last - __first, size_type(0));
1240 _M_rep()->_M_set_leaked();
1241 return iterator(_M_data() + __pos);
1245 * @brief Replace characters with value from another string.
1246 * @param pos Index of first character to replace.
1247 * @param n Number of characters to be replaced.
1248 * @param str String to insert.
1249 * @return Reference to this string.
1250 * @throw std::out_of_range If @a pos is beyond the end of this
1251 * string.
1252 * @throw std::length_error If new length exceeds @c max_size().
1254 * Removes the characters in the range [pos,pos+n) from this string.
1255 * In place, the value of @a str is inserted. If @a pos is beyond end
1256 * of string, out_of_range is thrown. If the length of the result
1257 * exceeds max_size(), length_error is thrown. The value of the string
1258 * doesn't change if an error is thrown.
1260 basic_string&
1261 replace(size_type __pos, size_type __n, const basic_string& __str)
1262 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1265 * @brief Replace characters with value from another string.
1266 * @param pos1 Index of first character to replace.
1267 * @param n1 Number of characters to be replaced.
1268 * @param str String to insert.
1269 * @param pos2 Index of first character of str to use.
1270 * @param n2 Number of characters from str to use.
1271 * @return Reference to this string.
1272 * @throw std::out_of_range If @a pos1 > size() or @a pos2 >
1273 * str.size().
1274 * @throw std::length_error If new length exceeds @c max_size().
1276 * Removes the characters in the range [pos1,pos1 + n) from this
1277 * string. In place, the value of @a str is inserted. If @a pos is
1278 * beyond end of string, out_of_range is thrown. If the length of the
1279 * result exceeds max_size(), length_error is thrown. The value of the
1280 * string doesn't change if an error is thrown.
1282 basic_string&
1283 replace(size_type __pos1, size_type __n1, const basic_string& __str,
1284 size_type __pos2, size_type __n2)
1285 { return this->replace(__pos1, __n1, __str._M_data()
1286 + __str._M_check(__pos2, "basic_string::replace"),
1287 __str._M_limit(__pos2, __n2)); }
1290 * @brief Replace characters with value of a C substring.
1291 * @param pos Index of first character to replace.
1292 * @param n1 Number of characters to be replaced.
1293 * @param s C string to insert.
1294 * @param n2 Number of characters from @a s to use.
1295 * @return Reference to this string.
1296 * @throw std::out_of_range If @a pos1 > size().
1297 * @throw std::length_error If new length exceeds @c max_size().
1299 * Removes the characters in the range [pos,pos + n1) from this string.
1300 * In place, the first @a n2 characters of @a s are inserted, or all
1301 * of @a s if @a n2 is too large. If @a pos is beyond end of string,
1302 * out_of_range is thrown. If the length of result exceeds max_size(),
1303 * length_error is thrown. The value of the string doesn't change if
1304 * an error is thrown.
1306 basic_string&
1307 replace(size_type __pos, size_type __n1, const _CharT* __s,
1308 size_type __n2);
1311 * @brief Replace characters with value of a C string.
1312 * @param pos Index of first character to replace.
1313 * @param n1 Number of characters to be replaced.
1314 * @param s C string to insert.
1315 * @return Reference to this string.
1316 * @throw std::out_of_range If @a pos > size().
1317 * @throw std::length_error If new length exceeds @c max_size().
1319 * Removes the characters in the range [pos,pos + n1) from this string.
1320 * In place, the first @a n characters of @a s are inserted. If @a
1321 * pos is beyond end of string, out_of_range is thrown. If the length
1322 * of result exceeds max_size(), length_error is thrown. The value of
1323 * the string doesn't change if an error is thrown.
1325 basic_string&
1326 replace(size_type __pos, size_type __n1, const _CharT* __s)
1328 __glibcxx_requires_string(__s);
1329 return this->replace(__pos, __n1, __s, traits_type::length(__s));
1333 * @brief Replace characters with multiple characters.
1334 * @param pos Index of first character to replace.
1335 * @param n1 Number of characters to be replaced.
1336 * @param n2 Number of characters to insert.
1337 * @param c Character to insert.
1338 * @return Reference to this string.
1339 * @throw std::out_of_range If @a pos > size().
1340 * @throw std::length_error If new length exceeds @c max_size().
1342 * Removes the characters in the range [pos,pos + n1) from this string.
1343 * In place, @a n2 copies of @a c are inserted. If @a pos is beyond
1344 * end of string, out_of_range is thrown. If the length of result
1345 * exceeds max_size(), length_error is thrown. The value of the string
1346 * doesn't change if an error is thrown.
1348 basic_string&
1349 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1350 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
1351 _M_limit(__pos, __n1), __n2, __c); }
1354 * @brief Replace range of characters with string.
1355 * @param i1 Iterator referencing start of range to replace.
1356 * @param i2 Iterator referencing end of range to replace.
1357 * @param str String value to insert.
1358 * @return Reference to this string.
1359 * @throw std::length_error If new length exceeds @c max_size().
1361 * Removes the characters in the range [i1,i2). In place, the value of
1362 * @a str is inserted. If the length of result exceeds max_size(),
1363 * length_error is thrown. The value of the string doesn't change if
1364 * an error is thrown.
1366 basic_string&
1367 replace(iterator __i1, iterator __i2, const basic_string& __str)
1368 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
1371 * @brief Replace range of characters with C substring.
1372 * @param i1 Iterator referencing start of range to replace.
1373 * @param i2 Iterator referencing end of range to replace.
1374 * @param s C string value to insert.
1375 * @param n Number of characters from s to insert.
1376 * @return Reference to this string.
1377 * @throw std::length_error If new length exceeds @c max_size().
1379 * Removes the characters in the range [i1,i2). In place, the first @a
1380 * n characters of @a s are inserted. If the length of result exceeds
1381 * max_size(), length_error is thrown. The value of the string doesn't
1382 * change if an error is thrown.
1384 basic_string&
1385 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
1387 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1388 && __i2 <= _M_iend());
1389 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
1393 * @brief Replace range of characters with C string.
1394 * @param i1 Iterator referencing start of range to replace.
1395 * @param i2 Iterator referencing end of range to replace.
1396 * @param s C string value to insert.
1397 * @return Reference to this string.
1398 * @throw std::length_error If new length exceeds @c max_size().
1400 * Removes the characters in the range [i1,i2). In place, the
1401 * characters of @a s are inserted. If the length of result exceeds
1402 * max_size(), length_error is thrown. The value of the string doesn't
1403 * change if an error is thrown.
1405 basic_string&
1406 replace(iterator __i1, iterator __i2, const _CharT* __s)
1408 __glibcxx_requires_string(__s);
1409 return this->replace(__i1, __i2, __s, traits_type::length(__s));
1413 * @brief Replace range of characters with multiple characters
1414 * @param i1 Iterator referencing start of range to replace.
1415 * @param i2 Iterator referencing end of range to replace.
1416 * @param n Number of characters to insert.
1417 * @param c Character to insert.
1418 * @return Reference to this string.
1419 * @throw std::length_error If new length exceeds @c max_size().
1421 * Removes the characters in the range [i1,i2). In place, @a n copies
1422 * of @a c are inserted. If the length of result exceeds max_size(),
1423 * length_error is thrown. The value of the string doesn't change if
1424 * an error is thrown.
1426 basic_string&
1427 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
1429 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1430 && __i2 <= _M_iend());
1431 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
1435 * @brief Replace range of characters with range.
1436 * @param i1 Iterator referencing start of range to replace.
1437 * @param i2 Iterator referencing end of range to replace.
1438 * @param k1 Iterator referencing start of range to insert.
1439 * @param k2 Iterator referencing end of range to insert.
1440 * @return Reference to this string.
1441 * @throw std::length_error If new length exceeds @c max_size().
1443 * Removes the characters in the range [i1,i2). In place, characters
1444 * in the range [k1,k2) are inserted. If the length of result exceeds
1445 * max_size(), length_error is thrown. The value of the string doesn't
1446 * change if an error is thrown.
1448 template<class _InputIterator>
1449 basic_string&
1450 replace(iterator __i1, iterator __i2,
1451 _InputIterator __k1, _InputIterator __k2)
1453 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1454 && __i2 <= _M_iend());
1455 __glibcxx_requires_valid_range(__k1, __k2);
1456 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1457 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
1460 // Specializations for the common case of pointer and iterator:
1461 // useful to avoid the overhead of temporary buffering in _M_replace.
1462 basic_string&
1463 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
1465 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1466 && __i2 <= _M_iend());
1467 __glibcxx_requires_valid_range(__k1, __k2);
1468 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1469 __k1, __k2 - __k1);
1472 basic_string&
1473 replace(iterator __i1, iterator __i2,
1474 const _CharT* __k1, const _CharT* __k2)
1476 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1477 && __i2 <= _M_iend());
1478 __glibcxx_requires_valid_range(__k1, __k2);
1479 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1480 __k1, __k2 - __k1);
1483 basic_string&
1484 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
1486 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1487 && __i2 <= _M_iend());
1488 __glibcxx_requires_valid_range(__k1, __k2);
1489 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1490 __k1.base(), __k2 - __k1);
1493 basic_string&
1494 replace(iterator __i1, iterator __i2,
1495 const_iterator __k1, const_iterator __k2)
1497 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1498 && __i2 <= _M_iend());
1499 __glibcxx_requires_valid_range(__k1, __k2);
1500 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1501 __k1.base(), __k2 - __k1);
1504 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1506 * @brief Replace range of characters with initializer_list.
1507 * @param i1 Iterator referencing start of range to replace.
1508 * @param i2 Iterator referencing end of range to replace.
1509 * @param l The initializer_list of characters to insert.
1510 * @return Reference to this string.
1511 * @throw std::length_error If new length exceeds @c max_size().
1513 * Removes the characters in the range [i1,i2). In place, characters
1514 * in the range [k1,k2) are inserted. If the length of result exceeds
1515 * max_size(), length_error is thrown. The value of the string doesn't
1516 * change if an error is thrown.
1518 basic_string& replace(iterator __i1, iterator __i2,
1519 initializer_list<_CharT> __l)
1520 { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
1521 #endif // __GXX_EXPERIMENTAL_CXX0X__
1523 private:
1524 template<class _Integer>
1525 basic_string&
1526 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
1527 _Integer __val, __true_type)
1528 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
1530 template<class _InputIterator>
1531 basic_string&
1532 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
1533 _InputIterator __k2, __false_type);
1535 basic_string&
1536 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
1537 _CharT __c);
1539 basic_string&
1540 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
1541 size_type __n2);
1543 // _S_construct_aux is used to implement the 21.3.1 para 15 which
1544 // requires special behaviour if _InIter is an integral type
1545 template<class _InIterator>
1546 static _CharT*
1547 _S_construct_aux(_InIterator __beg, _InIterator __end,
1548 const _Alloc& __a, __false_type)
1550 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
1551 return _S_construct(__beg, __end, __a, _Tag());
1554 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1555 // 438. Ambiguity in the "do the right thing" clause
1556 template<class _Integer>
1557 static _CharT*
1558 _S_construct_aux(_Integer __beg, _Integer __end,
1559 const _Alloc& __a, __true_type)
1560 { return _S_construct(static_cast<size_type>(__beg), __end, __a); }
1562 template<class _InIterator>
1563 static _CharT*
1564 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
1566 typedef typename std::__is_integer<_InIterator>::__type _Integral;
1567 return _S_construct_aux(__beg, __end, __a, _Integral());
1570 // For Input Iterators, used in istreambuf_iterators, etc.
1571 template<class _InIterator>
1572 static _CharT*
1573 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1574 input_iterator_tag);
1576 // For forward_iterators up to random_access_iterators, used for
1577 // string::iterator, _CharT*, etc.
1578 template<class _FwdIterator>
1579 static _CharT*
1580 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
1581 forward_iterator_tag);
1583 static _CharT*
1584 _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
1586 public:
1589 * @brief Copy substring into C string.
1590 * @param s C string to copy value into.
1591 * @param n Number of characters to copy.
1592 * @param pos Index of first character to copy.
1593 * @return Number of characters actually copied
1594 * @throw std::out_of_range If pos > size().
1596 * Copies up to @a n characters starting at @a pos into the C string @a
1597 * s. If @a pos is greater than size(), out_of_range is thrown.
1599 size_type
1600 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
1603 * @brief Swap contents with another string.
1604 * @param s String to swap with.
1606 * Exchanges the contents of this string with that of @a s in constant
1607 * time.
1609 void
1610 swap(basic_string& __s);
1612 // String operations:
1614 * @brief Return const pointer to null-terminated contents.
1616 * This is a handle to internal data. Do not modify or dire things may
1617 * happen.
1619 const _CharT*
1620 c_str() const
1621 { return _M_data(); }
1624 * @brief Return const pointer to contents.
1626 * This is a handle to internal data. Do not modify or dire things may
1627 * happen.
1629 const _CharT*
1630 data() const
1631 { return _M_data(); }
1634 * @brief Return copy of allocator used to construct this string.
1636 allocator_type
1637 get_allocator() const
1638 { return _M_dataplus; }
1641 * @brief Find position of a C substring.
1642 * @param s C string to locate.
1643 * @param pos Index of character to search from.
1644 * @param n Number of characters from @a s to search for.
1645 * @return Index of start of first occurrence.
1647 * Starting from @a pos, searches forward for the first @a n characters
1648 * in @a s within this string. If found, returns the index where it
1649 * begins. If not found, returns npos.
1651 size_type
1652 find(const _CharT* __s, size_type __pos, size_type __n) const;
1655 * @brief Find position of a string.
1656 * @param str String to locate.
1657 * @param pos Index of character to search from (default 0).
1658 * @return Index of start of first occurrence.
1660 * Starting from @a pos, searches forward for value of @a str within
1661 * this string. If found, returns the index where it begins. If not
1662 * found, returns npos.
1664 size_type
1665 find(const basic_string& __str, size_type __pos = 0) const
1666 { return this->find(__str.data(), __pos, __str.size()); }
1669 * @brief Find position of a C string.
1670 * @param s C string to locate.
1671 * @param pos Index of character to search from (default 0).
1672 * @return Index of start of first occurrence.
1674 * Starting from @a pos, searches forward for the value of @a s within
1675 * this string. If found, returns the index where it begins. If not
1676 * found, returns npos.
1678 size_type
1679 find(const _CharT* __s, size_type __pos = 0) const
1681 __glibcxx_requires_string(__s);
1682 return this->find(__s, __pos, traits_type::length(__s));
1686 * @brief Find position of a character.
1687 * @param c Character to locate.
1688 * @param pos Index of character to search from (default 0).
1689 * @return Index of first occurrence.
1691 * Starting from @a pos, searches forward for @a c within this string.
1692 * If found, returns the index where it was found. If not found,
1693 * returns npos.
1695 size_type
1696 find(_CharT __c, size_type __pos = 0) const;
1699 * @brief Find last position of a string.
1700 * @param str String to locate.
1701 * @param pos Index of character to search back from (default end).
1702 * @return Index of start of last occurrence.
1704 * Starting from @a pos, searches backward for value of @a str within
1705 * this string. If found, returns the index where it begins. If not
1706 * found, returns npos.
1708 size_type
1709 rfind(const basic_string& __str, size_type __pos = npos) const
1710 { return this->rfind(__str.data(), __pos, __str.size()); }
1713 * @brief Find last position of a C substring.
1714 * @param s C string to locate.
1715 * @param pos Index of character to search back from.
1716 * @param n Number of characters from s to search for.
1717 * @return Index of start of last occurrence.
1719 * Starting from @a pos, searches backward for the first @a n
1720 * characters in @a s within this string. If found, returns the index
1721 * where it begins. If not found, returns npos.
1723 size_type
1724 rfind(const _CharT* __s, size_type __pos, size_type __n) const;
1727 * @brief Find last position of a C string.
1728 * @param s C string to locate.
1729 * @param pos Index of character to start search at (default end).
1730 * @return Index of start of last occurrence.
1732 * Starting from @a pos, searches backward for the value of @a s within
1733 * this string. If found, returns the index where it begins. If not
1734 * found, returns npos.
1736 size_type
1737 rfind(const _CharT* __s, size_type __pos = npos) const
1739 __glibcxx_requires_string(__s);
1740 return this->rfind(__s, __pos, traits_type::length(__s));
1744 * @brief Find last position of a character.
1745 * @param c Character to locate.
1746 * @param pos Index of character to search back from (default end).
1747 * @return Index of last occurrence.
1749 * Starting from @a pos, searches backward for @a c within this string.
1750 * If found, returns the index where it was found. If not found,
1751 * returns npos.
1753 size_type
1754 rfind(_CharT __c, size_type __pos = npos) const;
1757 * @brief Find position of a character of string.
1758 * @param str String containing characters to locate.
1759 * @param pos Index of character to search from (default 0).
1760 * @return Index of first occurrence.
1762 * Starting from @a pos, searches forward for one of the characters of
1763 * @a str within this string. If found, returns the index where it was
1764 * found. If not found, returns npos.
1766 size_type
1767 find_first_of(const basic_string& __str, size_type __pos = 0) const
1768 { return this->find_first_of(__str.data(), __pos, __str.size()); }
1771 * @brief Find position of a character of C substring.
1772 * @param s String containing characters to locate.
1773 * @param pos Index of character to search from.
1774 * @param n Number of characters from s to search for.
1775 * @return Index of first occurrence.
1777 * Starting from @a pos, searches forward for one of the first @a n
1778 * characters of @a s within this string. If found, returns the index
1779 * where it was found. If not found, returns npos.
1781 size_type
1782 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
1785 * @brief Find position of a character of C string.
1786 * @param s String containing characters to locate.
1787 * @param pos Index of character to search from (default 0).
1788 * @return Index of first occurrence.
1790 * Starting from @a pos, searches forward for one of the characters of
1791 * @a s within this string. If found, returns the index where it was
1792 * found. If not found, returns npos.
1794 size_type
1795 find_first_of(const _CharT* __s, size_type __pos = 0) const
1797 __glibcxx_requires_string(__s);
1798 return this->find_first_of(__s, __pos, traits_type::length(__s));
1802 * @brief Find position of a character.
1803 * @param c Character to locate.
1804 * @param pos Index of character to search from (default 0).
1805 * @return Index of first occurrence.
1807 * Starting from @a pos, searches forward for the character @a c within
1808 * this string. If found, returns the index where it was found. If
1809 * not found, returns npos.
1811 * Note: equivalent to find(c, pos).
1813 size_type
1814 find_first_of(_CharT __c, size_type __pos = 0) const
1815 { return this->find(__c, __pos); }
1818 * @brief Find last position of a character of string.
1819 * @param str String containing characters to locate.
1820 * @param pos Index of character to search back from (default end).
1821 * @return Index of last occurrence.
1823 * Starting from @a pos, searches backward for one of the characters of
1824 * @a str within this string. If found, returns the index where it was
1825 * found. If not found, returns npos.
1827 size_type
1828 find_last_of(const basic_string& __str, size_type __pos = npos) const
1829 { return this->find_last_of(__str.data(), __pos, __str.size()); }
1832 * @brief Find last position of a character of C substring.
1833 * @param s C string containing characters to locate.
1834 * @param pos Index of character to search back from.
1835 * @param n Number of characters from s to search for.
1836 * @return Index of last occurrence.
1838 * Starting from @a pos, searches backward for one of the first @a n
1839 * characters of @a s within this string. If found, returns the index
1840 * where it was found. If not found, returns npos.
1842 size_type
1843 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
1846 * @brief Find last position of a character of C string.
1847 * @param s C string containing characters to locate.
1848 * @param pos Index of character to search back from (default end).
1849 * @return Index of last occurrence.
1851 * Starting from @a pos, searches backward for one of the characters of
1852 * @a s within this string. If found, returns the index where it was
1853 * found. If not found, returns npos.
1855 size_type
1856 find_last_of(const _CharT* __s, size_type __pos = npos) const
1858 __glibcxx_requires_string(__s);
1859 return this->find_last_of(__s, __pos, traits_type::length(__s));
1863 * @brief Find last position of a character.
1864 * @param c Character to locate.
1865 * @param pos Index of character to search back from (default end).
1866 * @return Index of last occurrence.
1868 * Starting from @a pos, searches backward for @a c within this string.
1869 * If found, returns the index where it was found. If not found,
1870 * returns npos.
1872 * Note: equivalent to rfind(c, pos).
1874 size_type
1875 find_last_of(_CharT __c, size_type __pos = npos) const
1876 { return this->rfind(__c, __pos); }
1879 * @brief Find position of a character not in string.
1880 * @param str String containing characters to avoid.
1881 * @param pos Index of character to search from (default 0).
1882 * @return Index of first occurrence.
1884 * Starting from @a pos, searches forward for a character not contained
1885 * in @a str within this string. If found, returns the index where it
1886 * was found. If not found, returns npos.
1888 size_type
1889 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
1890 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
1893 * @brief Find position of a character not in C substring.
1894 * @param s C string containing characters to avoid.
1895 * @param pos Index of character to search from.
1896 * @param n Number of characters from s to consider.
1897 * @return Index of first occurrence.
1899 * Starting from @a pos, searches forward for a character not contained
1900 * in the first @a n characters of @a s within this string. If found,
1901 * returns the index where it was found. If not found, returns npos.
1903 size_type
1904 find_first_not_of(const _CharT* __s, size_type __pos,
1905 size_type __n) const;
1908 * @brief Find position of a character not in C string.
1909 * @param s C string containing characters to avoid.
1910 * @param pos Index of character to search from (default 0).
1911 * @return Index of first occurrence.
1913 * Starting from @a pos, searches forward for a character not contained
1914 * in @a s within this string. If found, returns the index where it
1915 * was found. If not found, returns npos.
1917 size_type
1918 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
1920 __glibcxx_requires_string(__s);
1921 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
1925 * @brief Find position of a different character.
1926 * @param c Character to avoid.
1927 * @param pos Index of character to search from (default 0).
1928 * @return Index of first occurrence.
1930 * Starting from @a pos, searches forward for a character other than @a c
1931 * within this string. If found, returns the index where it was found.
1932 * If not found, returns npos.
1934 size_type
1935 find_first_not_of(_CharT __c, size_type __pos = 0) const;
1938 * @brief Find last position of a character not in string.
1939 * @param str String containing characters to avoid.
1940 * @param pos Index of character to search back from (default end).
1941 * @return Index of last occurrence.
1943 * Starting from @a pos, searches backward for a character not
1944 * contained in @a str within this string. If found, returns the index
1945 * where it was found. If not found, returns npos.
1947 size_type
1948 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
1949 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
1952 * @brief Find last position of a character not in C substring.
1953 * @param s C string containing characters to avoid.
1954 * @param pos Index of character to search back from.
1955 * @param n Number of characters from s to consider.
1956 * @return Index of last occurrence.
1958 * Starting from @a pos, searches backward for a character not
1959 * contained in the first @a n characters of @a s within this string.
1960 * If found, returns the index where it was found. If not found,
1961 * returns npos.
1963 size_type
1964 find_last_not_of(const _CharT* __s, size_type __pos,
1965 size_type __n) const;
1967 * @brief Find last position of a character not in C string.
1968 * @param s C string containing characters to avoid.
1969 * @param pos Index of character to search back from (default end).
1970 * @return Index of last occurrence.
1972 * Starting from @a pos, searches backward for a character not
1973 * contained in @a s within this string. If found, returns the index
1974 * where it was found. If not found, returns npos.
1976 size_type
1977 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
1979 __glibcxx_requires_string(__s);
1980 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
1984 * @brief Find last position of a different character.
1985 * @param c Character to avoid.
1986 * @param pos Index of character to search back from (default end).
1987 * @return Index of last occurrence.
1989 * Starting from @a pos, searches backward for a character other than
1990 * @a c within this string. If found, returns the index where it was
1991 * found. If not found, returns npos.
1993 size_type
1994 find_last_not_of(_CharT __c, size_type __pos = npos) const;
1997 * @brief Get a substring.
1998 * @param pos Index of first character (default 0).
1999 * @param n Number of characters in substring (default remainder).
2000 * @return The new string.
2001 * @throw std::out_of_range If pos > size().
2003 * Construct and return a new string using the @a n characters starting
2004 * at @a pos. If the string is too short, use the remainder of the
2005 * characters. If @a pos is beyond the end of the string, out_of_range
2006 * is thrown.
2008 basic_string
2009 substr(size_type __pos = 0, size_type __n = npos) const
2010 { return basic_string(*this,
2011 _M_check(__pos, "basic_string::substr"), __n); }
2014 * @brief Compare to a string.
2015 * @param str String to compare against.
2016 * @return Integer < 0, 0, or > 0.
2018 * Returns an integer < 0 if this string is ordered before @a str, 0 if
2019 * their values are equivalent, or > 0 if this string is ordered after
2020 * @a str. Determines the effective length rlen of the strings to
2021 * compare as the smallest of size() and str.size(). The function
2022 * then compares the two strings by calling traits::compare(data(),
2023 * str.data(),rlen). If the result of the comparison is nonzero returns
2024 * it, otherwise the shorter one is ordered first.
2027 compare(const basic_string& __str) const
2029 const size_type __size = this->size();
2030 const size_type __osize = __str.size();
2031 const size_type __len = std::min(__size, __osize);
2033 int __r = traits_type::compare(_M_data(), __str.data(), __len);
2034 if (!__r)
2035 __r = _S_compare(__size, __osize);
2036 return __r;
2040 * @brief Compare substring to a string.
2041 * @param pos Index of first character of substring.
2042 * @param n Number of characters in substring.
2043 * @param str String to compare against.
2044 * @return Integer < 0, 0, or > 0.
2046 * Form the substring of this string from the @a n characters starting
2047 * at @a pos. Returns an integer < 0 if the substring is ordered
2048 * before @a str, 0 if their values are equivalent, or > 0 if the
2049 * substring is ordered after @a str. Determines the effective length
2050 * rlen of the strings to compare as the smallest of the length of the
2051 * substring and @a str.size(). The function then compares the two
2052 * strings by calling traits::compare(substring.data(),str.data(),rlen).
2053 * If the result of the comparison is nonzero returns it, otherwise the
2054 * shorter one is ordered first.
2057 compare(size_type __pos, size_type __n, const basic_string& __str) const;
2060 * @brief Compare substring to a substring.
2061 * @param pos1 Index of first character of substring.
2062 * @param n1 Number of characters in substring.
2063 * @param str String to compare against.
2064 * @param pos2 Index of first character of substring of str.
2065 * @param n2 Number of characters in substring of str.
2066 * @return Integer < 0, 0, or > 0.
2068 * Form the substring of this string from the @a n1 characters starting
2069 * at @a pos1. Form the substring of @a str from the @a n2 characters
2070 * starting at @a pos2. Returns an integer < 0 if this substring is
2071 * ordered before the substring of @a str, 0 if their values are
2072 * equivalent, or > 0 if this substring is ordered after the substring
2073 * of @a str. Determines the effective length rlen of the strings
2074 * to compare as the smallest of the lengths of the substrings. The
2075 * function then compares the two strings by calling
2076 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
2077 * If the result of the comparison is nonzero returns it, otherwise the
2078 * shorter one is ordered first.
2081 compare(size_type __pos1, size_type __n1, const basic_string& __str,
2082 size_type __pos2, size_type __n2) const;
2085 * @brief Compare to a C string.
2086 * @param s C string to compare against.
2087 * @return Integer < 0, 0, or > 0.
2089 * Returns an integer < 0 if this string is ordered before @a s, 0 if
2090 * their values are equivalent, or > 0 if this string is ordered after
2091 * @a s. Determines the effective length rlen of the strings to
2092 * compare as the smallest of size() and the length of a string
2093 * constructed from @a s. The function then compares the two strings
2094 * by calling traits::compare(data(),s,rlen). If the result of the
2095 * comparison is nonzero returns it, otherwise the shorter one is
2096 * ordered first.
2099 compare(const _CharT* __s) const;
2101 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2102 // 5 String::compare specification questionable
2104 * @brief Compare substring to a C string.
2105 * @param pos Index of first character of substring.
2106 * @param n1 Number of characters in substring.
2107 * @param s C string to compare against.
2108 * @return Integer < 0, 0, or > 0.
2110 * Form the substring of this string from the @a n1 characters starting
2111 * at @a pos. Returns an integer < 0 if the substring is ordered
2112 * before @a s, 0 if their values are equivalent, or > 0 if the
2113 * substring is ordered after @a s. Determines the effective length
2114 * rlen of the strings to compare as the smallest of the length of the
2115 * substring and the length of a string constructed from @a s. The
2116 * function then compares the two string by calling
2117 * traits::compare(substring.data(),s,rlen). If the result of the
2118 * comparison is nonzero returns it, otherwise the shorter one is
2119 * ordered first.
2122 compare(size_type __pos, size_type __n1, const _CharT* __s) const;
2125 * @brief Compare substring against a character array.
2126 * @param pos1 Index of first character of substring.
2127 * @param n1 Number of characters in substring.
2128 * @param s character array to compare against.
2129 * @param n2 Number of characters of s.
2130 * @return Integer < 0, 0, or > 0.
2132 * Form the substring of this string from the @a n1 characters starting
2133 * at @a pos1. Form a string from the first @a n2 characters of @a s.
2134 * Returns an integer < 0 if this substring is ordered before the string
2135 * from @a s, 0 if their values are equivalent, or > 0 if this substring
2136 * is ordered after the string from @a s. Determines the effective
2137 * length rlen of the strings to compare as the smallest of the length
2138 * of the substring and @a n2. The function then compares the two
2139 * strings by calling traits::compare(substring.data(),s,rlen). If the
2140 * result of the comparison is nonzero returns it, otherwise the shorter
2141 * one is ordered first.
2143 * NB: s must have at least n2 characters, '\0' has no special
2144 * meaning.
2147 compare(size_type __pos, size_type __n1, const _CharT* __s,
2148 size_type __n2) const;
2151 template<typename _CharT, typename _Traits, typename _Alloc>
2152 inline basic_string<_CharT, _Traits, _Alloc>::
2153 basic_string()
2154 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
2155 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
2156 #else
2157 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()) { }
2158 #endif
2160 // operator+
2162 * @brief Concatenate two strings.
2163 * @param lhs First string.
2164 * @param rhs Last string.
2165 * @return New string with value of @a lhs followed by @a rhs.
2167 template<typename _CharT, typename _Traits, typename _Alloc>
2168 basic_string<_CharT, _Traits, _Alloc>
2169 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2170 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2172 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2173 __str.append(__rhs);
2174 return __str;
2178 * @brief Concatenate C string and string.
2179 * @param lhs First string.
2180 * @param rhs Last string.
2181 * @return New string with value of @a lhs followed by @a rhs.
2183 template<typename _CharT, typename _Traits, typename _Alloc>
2184 basic_string<_CharT,_Traits,_Alloc>
2185 operator+(const _CharT* __lhs,
2186 const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2189 * @brief Concatenate character and string.
2190 * @param lhs First string.
2191 * @param rhs Last string.
2192 * @return New string with @a lhs followed by @a rhs.
2194 template<typename _CharT, typename _Traits, typename _Alloc>
2195 basic_string<_CharT,_Traits,_Alloc>
2196 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2199 * @brief Concatenate string and C string.
2200 * @param lhs First string.
2201 * @param rhs Last string.
2202 * @return New string with @a lhs followed by @a rhs.
2204 template<typename _CharT, typename _Traits, typename _Alloc>
2205 inline basic_string<_CharT, _Traits, _Alloc>
2206 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2207 const _CharT* __rhs)
2209 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2210 __str.append(__rhs);
2211 return __str;
2215 * @brief Concatenate string and character.
2216 * @param lhs First string.
2217 * @param rhs Last string.
2218 * @return New string with @a lhs followed by @a rhs.
2220 template<typename _CharT, typename _Traits, typename _Alloc>
2221 inline basic_string<_CharT, _Traits, _Alloc>
2222 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
2224 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
2225 typedef typename __string_type::size_type __size_type;
2226 __string_type __str(__lhs);
2227 __str.append(__size_type(1), __rhs);
2228 return __str;
2231 // operator ==
2233 * @brief Test equivalence of two strings.
2234 * @param lhs First string.
2235 * @param rhs Second string.
2236 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2238 template<typename _CharT, typename _Traits, typename _Alloc>
2239 inline bool
2240 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2241 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2242 { return __lhs.compare(__rhs) == 0; }
2244 template<typename _CharT>
2245 inline
2246 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type
2247 operator==(const basic_string<_CharT>& __lhs,
2248 const basic_string<_CharT>& __rhs)
2249 { return (__lhs.size() == __rhs.size()
2250 && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
2251 __lhs.size())); }
2254 * @brief Test equivalence of C string and string.
2255 * @param lhs C string.
2256 * @param rhs String.
2257 * @return True if @a rhs.compare(@a lhs) == 0. False otherwise.
2259 template<typename _CharT, typename _Traits, typename _Alloc>
2260 inline bool
2261 operator==(const _CharT* __lhs,
2262 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2263 { return __rhs.compare(__lhs) == 0; }
2266 * @brief Test equivalence of string and C string.
2267 * @param lhs String.
2268 * @param rhs C string.
2269 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2271 template<typename _CharT, typename _Traits, typename _Alloc>
2272 inline bool
2273 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2274 const _CharT* __rhs)
2275 { return __lhs.compare(__rhs) == 0; }
2277 // operator !=
2279 * @brief Test difference of two strings.
2280 * @param lhs First string.
2281 * @param rhs Second string.
2282 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2284 template<typename _CharT, typename _Traits, typename _Alloc>
2285 inline bool
2286 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2287 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2288 { return !(__lhs == __rhs); }
2291 * @brief Test difference of C string and string.
2292 * @param lhs C string.
2293 * @param rhs String.
2294 * @return True if @a rhs.compare(@a lhs) != 0. False otherwise.
2296 template<typename _CharT, typename _Traits, typename _Alloc>
2297 inline bool
2298 operator!=(const _CharT* __lhs,
2299 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2300 { return !(__lhs == __rhs); }
2303 * @brief Test difference of string and C string.
2304 * @param lhs String.
2305 * @param rhs C string.
2306 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2308 template<typename _CharT, typename _Traits, typename _Alloc>
2309 inline bool
2310 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2311 const _CharT* __rhs)
2312 { return !(__lhs == __rhs); }
2314 // operator <
2316 * @brief Test if string precedes string.
2317 * @param lhs First string.
2318 * @param rhs Second string.
2319 * @return True if @a lhs precedes @a rhs. False otherwise.
2321 template<typename _CharT, typename _Traits, typename _Alloc>
2322 inline bool
2323 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2324 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2325 { return __lhs.compare(__rhs) < 0; }
2328 * @brief Test if string precedes C string.
2329 * @param lhs String.
2330 * @param rhs C string.
2331 * @return True if @a lhs precedes @a rhs. False otherwise.
2333 template<typename _CharT, typename _Traits, typename _Alloc>
2334 inline bool
2335 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2336 const _CharT* __rhs)
2337 { return __lhs.compare(__rhs) < 0; }
2340 * @brief Test if C string precedes string.
2341 * @param lhs C string.
2342 * @param rhs String.
2343 * @return True if @a lhs precedes @a rhs. False otherwise.
2345 template<typename _CharT, typename _Traits, typename _Alloc>
2346 inline bool
2347 operator<(const _CharT* __lhs,
2348 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2349 { return __rhs.compare(__lhs) > 0; }
2351 // operator >
2353 * @brief Test if string follows string.
2354 * @param lhs First string.
2355 * @param rhs Second string.
2356 * @return True if @a lhs follows @a rhs. False otherwise.
2358 template<typename _CharT, typename _Traits, typename _Alloc>
2359 inline bool
2360 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2361 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2362 { return __lhs.compare(__rhs) > 0; }
2365 * @brief Test if string follows C string.
2366 * @param lhs String.
2367 * @param rhs C string.
2368 * @return True if @a lhs follows @a rhs. False otherwise.
2370 template<typename _CharT, typename _Traits, typename _Alloc>
2371 inline bool
2372 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2373 const _CharT* __rhs)
2374 { return __lhs.compare(__rhs) > 0; }
2377 * @brief Test if C string follows string.
2378 * @param lhs C string.
2379 * @param rhs String.
2380 * @return True if @a lhs follows @a rhs. False otherwise.
2382 template<typename _CharT, typename _Traits, typename _Alloc>
2383 inline bool
2384 operator>(const _CharT* __lhs,
2385 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2386 { return __rhs.compare(__lhs) < 0; }
2388 // operator <=
2390 * @brief Test if string doesn't follow string.
2391 * @param lhs First string.
2392 * @param rhs Second string.
2393 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2395 template<typename _CharT, typename _Traits, typename _Alloc>
2396 inline bool
2397 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2398 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2399 { return __lhs.compare(__rhs) <= 0; }
2402 * @brief Test if string doesn't follow C string.
2403 * @param lhs String.
2404 * @param rhs C string.
2405 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2407 template<typename _CharT, typename _Traits, typename _Alloc>
2408 inline bool
2409 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2410 const _CharT* __rhs)
2411 { return __lhs.compare(__rhs) <= 0; }
2414 * @brief Test if C string doesn't follow string.
2415 * @param lhs C string.
2416 * @param rhs String.
2417 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2419 template<typename _CharT, typename _Traits, typename _Alloc>
2420 inline bool
2421 operator<=(const _CharT* __lhs,
2422 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2423 { return __rhs.compare(__lhs) >= 0; }
2425 // operator >=
2427 * @brief Test if string doesn't precede string.
2428 * @param lhs First string.
2429 * @param rhs Second string.
2430 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2432 template<typename _CharT, typename _Traits, typename _Alloc>
2433 inline bool
2434 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2435 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2436 { return __lhs.compare(__rhs) >= 0; }
2439 * @brief Test if string doesn't precede C string.
2440 * @param lhs String.
2441 * @param rhs C string.
2442 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2444 template<typename _CharT, typename _Traits, typename _Alloc>
2445 inline bool
2446 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2447 const _CharT* __rhs)
2448 { return __lhs.compare(__rhs) >= 0; }
2451 * @brief Test if C string doesn't precede string.
2452 * @param lhs C string.
2453 * @param rhs String.
2454 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2456 template<typename _CharT, typename _Traits, typename _Alloc>
2457 inline bool
2458 operator>=(const _CharT* __lhs,
2459 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2460 { return __rhs.compare(__lhs) <= 0; }
2463 * @brief Swap contents of two strings.
2464 * @param lhs First string.
2465 * @param rhs Second string.
2467 * Exchanges the contents of @a lhs and @a rhs in constant time.
2469 template<typename _CharT, typename _Traits, typename _Alloc>
2470 inline void
2471 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
2472 basic_string<_CharT, _Traits, _Alloc>& __rhs)
2473 { __lhs.swap(__rhs); }
2476 * @brief Read stream into a string.
2477 * @param is Input stream.
2478 * @param str Buffer to store into.
2479 * @return Reference to the input stream.
2481 * Stores characters from @a is into @a str until whitespace is found, the
2482 * end of the stream is encountered, or str.max_size() is reached. If
2483 * is.width() is non-zero, that is the limit on the number of characters
2484 * stored into @a str. Any previous contents of @a str are erased.
2486 template<typename _CharT, typename _Traits, typename _Alloc>
2487 basic_istream<_CharT, _Traits>&
2488 operator>>(basic_istream<_CharT, _Traits>& __is,
2489 basic_string<_CharT, _Traits, _Alloc>& __str);
2491 template<>
2492 basic_istream<char>&
2493 operator>>(basic_istream<char>& __is, basic_string<char>& __str);
2496 * @brief Write string to a stream.
2497 * @param os Output stream.
2498 * @param str String to write out.
2499 * @return Reference to the output stream.
2501 * Output characters of @a str into os following the same rules as for
2502 * writing a C string.
2504 template<typename _CharT, typename _Traits, typename _Alloc>
2505 inline basic_ostream<_CharT, _Traits>&
2506 operator<<(basic_ostream<_CharT, _Traits>& __os,
2507 const basic_string<_CharT, _Traits, _Alloc>& __str)
2509 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2510 // 586. string inserter not a formatted function
2511 return __ostream_insert(__os, __str.data(), __str.size());
2515 * @brief Read a line from stream into a string.
2516 * @param is Input stream.
2517 * @param str Buffer to store into.
2518 * @param delim Character marking end of line.
2519 * @return Reference to the input stream.
2521 * Stores characters from @a is into @a str until @a delim is found, the
2522 * end of the stream is encountered, or str.max_size() is reached. If
2523 * is.width() is non-zero, that is the limit on the number of characters
2524 * stored into @a str. Any previous contents of @a str are erased. If @a
2525 * delim was encountered, it is extracted but not stored into @a str.
2527 template<typename _CharT, typename _Traits, typename _Alloc>
2528 basic_istream<_CharT, _Traits>&
2529 getline(basic_istream<_CharT, _Traits>& __is,
2530 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
2533 * @brief Read a line from stream into a string.
2534 * @param is Input stream.
2535 * @param str Buffer to store into.
2536 * @return Reference to the input stream.
2538 * Stores characters from is into @a str until '\n' is found, the end of
2539 * the stream is encountered, or str.max_size() is reached. If is.width()
2540 * is non-zero, that is the limit on the number of characters stored into
2541 * @a str. Any previous contents of @a str are erased. If end of line was
2542 * encountered, it is extracted but not stored into @a str.
2544 template<typename _CharT, typename _Traits, typename _Alloc>
2545 inline basic_istream<_CharT, _Traits>&
2546 getline(basic_istream<_CharT, _Traits>& __is,
2547 basic_string<_CharT, _Traits, _Alloc>& __str)
2548 { return getline(__is, __str, __is.widen('\n')); }
2550 template<>
2551 basic_istream<char>&
2552 getline(basic_istream<char>& __in, basic_string<char>& __str,
2553 char __delim);
2555 #ifdef _GLIBCXX_USE_WCHAR_T
2556 template<>
2557 basic_istream<wchar_t>&
2558 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
2559 wchar_t __delim);
2560 #endif
2562 _GLIBCXX_END_NAMESPACE
2564 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) && defined(_GLIBCXX_USE_C99) \
2565 && !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))
2567 #include <ext/string_conversions.h>
2569 _GLIBCXX_BEGIN_NAMESPACE(std)
2571 // 21.4 Numeric Conversions [string.conversions].
2572 inline int
2573 stoi(const string& __str, size_t* __idx = 0, int __base = 10)
2574 { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
2575 __idx, __base); }
2577 inline long
2578 stol(const string& __str, size_t* __idx = 0, int __base = 10)
2579 { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
2580 __idx, __base); }
2582 inline unsigned long
2583 stoul(const string& __str, size_t* __idx = 0, int __base = 10)
2584 { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
2585 __idx, __base); }
2587 inline long long
2588 stoll(const string& __str, size_t* __idx = 0, int __base = 10)
2589 { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
2590 __idx, __base); }
2592 inline unsigned long long
2593 stoull(const string& __str, size_t* __idx = 0, int __base = 10)
2594 { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
2595 __idx, __base); }
2597 // NB: strtof vs strtod.
2598 inline float
2599 stof(const string& __str, size_t* __idx = 0)
2600 { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
2602 inline double
2603 stod(const string& __str, size_t* __idx = 0)
2604 { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
2606 inline long double
2607 stold(const string& __str, size_t* __idx = 0)
2608 { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
2610 // NB: (v)snprintf vs sprintf.
2611 inline string
2612 to_string(long long __val)
2613 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
2614 4 * sizeof(long long),
2615 "%lld", __val); }
2617 inline string
2618 to_string(unsigned long long __val)
2619 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
2620 4 * sizeof(unsigned long long),
2621 "%llu", __val); }
2623 inline string
2624 to_string(long double __val)
2626 const int __n =
2627 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
2628 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
2629 "%Lf", __val);
2632 #ifdef _GLIBCXX_USE_WCHAR_T
2633 inline int
2634 stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
2635 { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
2636 __idx, __base); }
2638 inline long
2639 stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
2640 { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
2641 __idx, __base); }
2643 inline unsigned long
2644 stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
2645 { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
2646 __idx, __base); }
2648 inline long long
2649 stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
2650 { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
2651 __idx, __base); }
2653 inline unsigned long long
2654 stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
2655 { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
2656 __idx, __base); }
2658 // NB: wcstof vs wcstod.
2659 inline float
2660 stof(const wstring& __str, size_t* __idx = 0)
2661 { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
2663 inline double
2664 stod(const wstring& __str, size_t* __idx = 0)
2665 { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
2667 inline long double
2668 stold(const wstring& __str, size_t* __idx = 0)
2669 { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
2671 inline wstring
2672 to_wstring(long long __val)
2673 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
2674 4 * sizeof(long long),
2675 L"%lld", __val); }
2677 inline wstring
2678 to_wstring(unsigned long long __val)
2679 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
2680 4 * sizeof(unsigned long long),
2681 L"%llu", __val); }
2683 inline wstring
2684 to_wstring(long double __val)
2686 const int __n =
2687 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
2688 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
2689 L"%Lf", __val);
2691 #endif
2693 _GLIBCXX_END_NAMESPACE
2695 #endif
2697 #endif /* _BASIC_STRING_H */