Merged with mainline at revision 128810.
[official-gcc.git] / libstdc++-v3 / include / bits / basic_string.h
blob2dc3b37253eba7135f23e6cdda336baa91bc64d6
1 // Components for manipulating sequences of characters -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007
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>
49 _GLIBCXX_BEGIN_NAMESPACE(std)
51 /**
52 * @class basic_string basic_string.h <string>
53 * @brief Managing sequences of characters and character-like objects.
55 * @ingroup Containers
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 * @if maint
68 * Documentation? What's that?
69 * Nathan Myers <ncm@cantrip.org>.
71 * A string looks like this:
73 * @code
74 * [_Rep]
75 * _M_length
76 * [basic_string<char_type>] _M_capacity
77 * _M_dataplus _M_refcount
78 * _M_p ----------------> unnamed array of char_type
79 * @endcode
81 * Where the _M_p points to the first character in the string, and
82 * you cast it to a pointer-to-_Rep and subtract 1 to get a
83 * pointer to the header.
85 * This approach has the enormous advantage that a string object
86 * requires only one allocation. All the ugliness is confined
87 * within a single pair of inline functions, which each compile to
88 * a single "add" instruction: _Rep::_M_data(), and
89 * string::_M_rep(); and the allocation function which gets a
90 * block of raw bytes and with room enough and constructs a _Rep
91 * object at the front.
93 * The reason you want _M_data pointing to the character array and
94 * not the _Rep is so that the debugger can see the string
95 * contents. (Probably we should add a non-inline member to get
96 * the _Rep for the debugger to use, so users can check the actual
97 * string length.)
99 * Note that the _Rep object is a POD so that you can have a
100 * static "empty string" _Rep object already "constructed" before
101 * static constructors have run. The reference-count encoding is
102 * chosen so that a 0 indicates one reference, so you never try to
103 * destroy the empty-string _Rep object.
105 * All but the last paragraph is considered pretty conventional
106 * for a C++ string implementation.
107 * @endif
109 // 21.3 Template class basic_string
110 template<typename _CharT, typename _Traits, typename _Alloc>
111 class basic_string
113 typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
115 // Types:
116 public:
117 typedef _Traits traits_type;
118 typedef typename _Traits::char_type value_type;
119 typedef _Alloc allocator_type;
120 typedef typename _CharT_alloc_type::size_type size_type;
121 typedef typename _CharT_alloc_type::difference_type difference_type;
122 typedef typename _CharT_alloc_type::reference reference;
123 typedef typename _CharT_alloc_type::const_reference const_reference;
124 typedef typename _CharT_alloc_type::pointer pointer;
125 typedef typename _CharT_alloc_type::const_pointer const_pointer;
126 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
127 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
128 const_iterator;
129 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
130 typedef std::reverse_iterator<iterator> reverse_iterator;
132 private:
133 // _Rep: string representation
134 // Invariants:
135 // 1. String really contains _M_length + 1 characters: due to 21.3.4
136 // must be kept null-terminated.
137 // 2. _M_capacity >= _M_length
138 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
139 // 3. _M_refcount has three states:
140 // -1: leaked, one reference, no ref-copies allowed, non-const.
141 // 0: one reference, non-const.
142 // n>0: n + 1 references, operations require a lock, const.
143 // 4. All fields==0 is an empty string, given the extra storage
144 // beyond-the-end for a null terminator; thus, the shared
145 // empty string representation needs no constructor.
147 struct _Rep_base
149 size_type _M_length;
150 size_type _M_capacity;
151 _Atomic_word _M_refcount;
154 struct _Rep : _Rep_base
156 // Types:
157 typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc;
159 // (Public) Data members:
161 // The maximum number of individual char_type elements of an
162 // individual string is determined by _S_max_size. This is the
163 // value that will be returned by max_size(). (Whereas npos
164 // is the maximum number of bytes the allocator can allocate.)
165 // If one was to divvy up the theoretical largest size string,
166 // with a terminating character and m _CharT elements, it'd
167 // look like this:
168 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
169 // Solving for m:
170 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
171 // In addition, this implementation quarters this amount.
172 static const size_type _S_max_size;
173 static const _CharT _S_terminal;
175 // The following storage is init'd to 0 by the linker, resulting
176 // (carefully) in an empty string with one reference.
177 static size_type _S_empty_rep_storage[];
179 static _Rep&
180 _S_empty_rep()
182 // NB: Mild hack to avoid strict-aliasing warnings. Note that
183 // _S_empty_rep_storage is never modified and the punning should
184 // be reasonably safe in this case.
185 void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage);
186 return *reinterpret_cast<_Rep*>(__p);
189 bool
190 _M_is_leaked() const
191 { return this->_M_refcount < 0; }
193 bool
194 _M_is_shared() const
195 { return this->_M_refcount > 0; }
197 void
198 _M_set_leaked()
199 { this->_M_refcount = -1; }
201 void
202 _M_set_sharable()
203 { this->_M_refcount = 0; }
205 void
206 _M_set_length_and_sharable(size_type __n)
208 this->_M_set_sharable(); // One reference.
209 this->_M_length = __n;
210 traits_type::assign(this->_M_refdata()[__n], _S_terminal);
211 // grrr. (per 21.3.4)
212 // You cannot leave those LWG people alone for a second.
215 _CharT*
216 _M_refdata() throw()
217 { return reinterpret_cast<_CharT*>(this + 1); }
219 _CharT*
220 _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
222 return (!_M_is_leaked() && __alloc1 == __alloc2)
223 ? _M_refcopy() : _M_clone(__alloc1);
226 // Create & Destroy
227 static _Rep*
228 _S_create(size_type, size_type, const _Alloc&);
230 void
231 _M_dispose(const _Alloc& __a)
233 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
234 if (__builtin_expect(this != &_S_empty_rep(), false))
235 #endif
236 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
237 -1) <= 0)
238 _M_destroy(__a);
239 } // XXX MT
241 void
242 _M_destroy(const _Alloc&) throw();
244 _CharT*
245 _M_refcopy() throw()
247 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
248 if (__builtin_expect(this != &_S_empty_rep(), false))
249 #endif
250 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
251 return _M_refdata();
252 } // XXX MT
254 _CharT*
255 _M_clone(const _Alloc&, size_type __res = 0);
258 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
259 struct _Alloc_hider : _Alloc
261 _Alloc_hider(_CharT* __dat, const _Alloc& __a)
262 : _Alloc(__a), _M_p(__dat) { }
264 _CharT* _M_p; // The actual data.
267 public:
268 // Data Members (public):
269 // NB: This is an unsigned type, and thus represents the maximum
270 // size that the allocator can hold.
271 /// Value returned by various member functions when they fail.
272 static const size_type npos = static_cast<size_type>(-1);
274 private:
275 // Data Members (private):
276 mutable _Alloc_hider _M_dataplus;
278 _CharT*
279 _M_data() const
280 { return _M_dataplus._M_p; }
282 _CharT*
283 _M_data(_CharT* __p)
284 { return (_M_dataplus._M_p = __p); }
286 _Rep*
287 _M_rep() const
288 { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
290 // For the internal use we have functions similar to `begin'/`end'
291 // but they do not call _M_leak.
292 iterator
293 _M_ibegin() const
294 { return iterator(_M_data()); }
296 iterator
297 _M_iend() const
298 { return iterator(_M_data() + this->size()); }
300 void
301 _M_leak() // for use in begin() & non-const op[]
303 if (!_M_rep()->_M_is_leaked())
304 _M_leak_hard();
307 size_type
308 _M_check(size_type __pos, const char* __s) const
310 if (__pos > this->size())
311 __throw_out_of_range(__N(__s));
312 return __pos;
315 void
316 _M_check_length(size_type __n1, size_type __n2, const char* __s) const
318 if (this->max_size() - (this->size() - __n1) < __n2)
319 __throw_length_error(__N(__s));
322 // NB: _M_limit doesn't check for a bad __pos value.
323 size_type
324 _M_limit(size_type __pos, size_type __off) const
326 const bool __testoff = __off < this->size() - __pos;
327 return __testoff ? __off : this->size() - __pos;
330 // True if _Rep and source do not overlap.
331 bool
332 _M_disjunct(const _CharT* __s) const
334 return (less<const _CharT*>()(__s, _M_data())
335 || less<const _CharT*>()(_M_data() + this->size(), __s));
338 // When __n = 1 way faster than the general multichar
339 // traits_type::copy/move/assign.
340 static void
341 _M_copy(_CharT* __d, const _CharT* __s, size_type __n)
343 if (__n == 1)
344 traits_type::assign(*__d, *__s);
345 else
346 traits_type::copy(__d, __s, __n);
349 static void
350 _M_move(_CharT* __d, const _CharT* __s, size_type __n)
352 if (__n == 1)
353 traits_type::assign(*__d, *__s);
354 else
355 traits_type::move(__d, __s, __n);
358 static void
359 _M_assign(_CharT* __d, size_type __n, _CharT __c)
361 if (__n == 1)
362 traits_type::assign(*__d, __c);
363 else
364 traits_type::assign(__d, __n, __c);
367 // _S_copy_chars is a separate template to permit specialization
368 // to optimize for the common case of pointers as iterators.
369 template<class _Iterator>
370 static void
371 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
373 for (; __k1 != __k2; ++__k1, ++__p)
374 traits_type::assign(*__p, *__k1); // These types are off.
377 static void
378 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2)
379 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
381 static void
382 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
383 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
385 static void
386 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
387 { _M_copy(__p, __k1, __k2 - __k1); }
389 static void
390 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
391 { _M_copy(__p, __k1, __k2 - __k1); }
393 static int
394 _S_compare(size_type __n1, size_type __n2)
396 const difference_type __d = difference_type(__n1 - __n2);
398 if (__d > __gnu_cxx::__numeric_traits<int>::__max)
399 return __gnu_cxx::__numeric_traits<int>::__max;
400 else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
401 return __gnu_cxx::__numeric_traits<int>::__min;
402 else
403 return int(__d);
406 void
407 _M_mutate(size_type __pos, size_type __len1, size_type __len2);
409 void
410 _M_leak_hard();
412 static _Rep&
413 _S_empty_rep()
414 { return _Rep::_S_empty_rep(); }
416 public:
417 // Construct/copy/destroy:
418 // NB: We overload ctors in some cases instead of using default
419 // arguments, per 17.4.4.4 para. 2 item 2.
422 * @brief Default constructor creates an empty string.
424 inline
425 basic_string();
428 * @brief Construct an empty string using allocator @a a.
430 explicit
431 basic_string(const _Alloc& __a);
433 // NB: per LWG issue 42, semantics different from IS:
435 * @brief Construct string with copy of value of @a str.
436 * @param str Source string.
438 basic_string(const basic_string& __str);
440 * @brief Construct string as copy of a substring.
441 * @param str Source string.
442 * @param pos Index of first character to copy from.
443 * @param n Number of characters to copy (default remainder).
445 basic_string(const basic_string& __str, size_type __pos,
446 size_type __n = npos);
448 * @brief Construct string as copy of a substring.
449 * @param str Source string.
450 * @param pos Index of first character to copy from.
451 * @param n Number of characters to copy.
452 * @param a Allocator to use.
454 basic_string(const basic_string& __str, size_type __pos,
455 size_type __n, const _Alloc& __a);
458 * @brief Construct string initialized by a character array.
459 * @param s Source character array.
460 * @param n Number of characters to copy.
461 * @param a Allocator to use (default is default allocator).
463 * NB: @a s must have at least @a n characters, '\0' has no special
464 * meaning.
466 basic_string(const _CharT* __s, size_type __n,
467 const _Alloc& __a = _Alloc());
469 * @brief Construct string as copy of a C string.
470 * @param s Source C string.
471 * @param a Allocator to use (default is default allocator).
473 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
475 * @brief Construct string as multiple characters.
476 * @param n Number of characters.
477 * @param c Character to use.
478 * @param a Allocator to use (default is default allocator).
480 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
483 * @brief Construct string as copy of a range.
484 * @param beg Start of range.
485 * @param end End of range.
486 * @param a Allocator to use (default is default allocator).
488 template<class _InputIterator>
489 basic_string(_InputIterator __beg, _InputIterator __end,
490 const _Alloc& __a = _Alloc());
493 * @brief Destroy the string instance.
495 ~basic_string()
496 { _M_rep()->_M_dispose(this->get_allocator()); }
499 * @brief Assign the value of @a str to this string.
500 * @param str Source string.
502 basic_string&
503 operator=(const basic_string& __str)
504 { return this->assign(__str); }
507 * @brief Copy contents of @a s into this string.
508 * @param s Source null-terminated string.
510 basic_string&
511 operator=(const _CharT* __s)
512 { return this->assign(__s); }
515 * @brief Set value to string of length 1.
516 * @param c Source character.
518 * Assigning to a character makes this string length 1 and
519 * (*this)[0] == @a c.
521 basic_string&
522 operator=(_CharT __c)
524 this->assign(1, __c);
525 return *this;
528 // Iterators:
530 * Returns a read/write iterator that points to the first character in
531 * the %string. Unshares the string.
533 iterator
534 begin()
536 _M_leak();
537 return iterator(_M_data());
541 * Returns a read-only (constant) iterator that points to the first
542 * character in the %string.
544 const_iterator
545 begin() const
546 { return const_iterator(_M_data()); }
549 * Returns a read/write iterator that points one past the last
550 * character in the %string. Unshares the string.
552 iterator
553 end()
555 _M_leak();
556 return iterator(_M_data() + this->size());
560 * Returns a read-only (constant) iterator that points one past the
561 * last character in the %string.
563 const_iterator
564 end() const
565 { return const_iterator(_M_data() + this->size()); }
568 * Returns a read/write reverse iterator that points to the last
569 * character in the %string. Iteration is done in reverse element
570 * order. Unshares the string.
572 reverse_iterator
573 rbegin()
574 { return reverse_iterator(this->end()); }
577 * Returns a read-only (constant) reverse iterator that points
578 * to the last character in the %string. Iteration is done in
579 * reverse element order.
581 const_reverse_iterator
582 rbegin() const
583 { return const_reverse_iterator(this->end()); }
586 * Returns a read/write reverse iterator that points to one before the
587 * first character in the %string. Iteration is done in reverse
588 * element order. Unshares the string.
590 reverse_iterator
591 rend()
592 { return reverse_iterator(this->begin()); }
595 * Returns a read-only (constant) reverse iterator that points
596 * to one before the first character in the %string. Iteration
597 * is done in reverse element order.
599 const_reverse_iterator
600 rend() const
601 { return const_reverse_iterator(this->begin()); }
603 public:
604 // Capacity:
605 /// Returns the number of characters in the string, not including any
606 /// null-termination.
607 size_type
608 size() const
609 { return _M_rep()->_M_length; }
611 /// Returns the number of characters in the string, not including any
612 /// null-termination.
613 size_type
614 length() const
615 { return _M_rep()->_M_length; }
617 /// Returns the size() of the largest possible %string.
618 size_type
619 max_size() const
620 { return _Rep::_S_max_size; }
623 * @brief Resizes the %string to the specified number of characters.
624 * @param n Number of characters the %string should contain.
625 * @param c Character to fill any new elements.
627 * This function will %resize the %string to the specified
628 * number of characters. If the number is smaller than the
629 * %string's current size the %string is truncated, otherwise
630 * the %string is extended and new elements are set to @a c.
632 void
633 resize(size_type __n, _CharT __c);
636 * @brief Resizes the %string to the specified number of characters.
637 * @param n Number of characters the %string should contain.
639 * This function will resize the %string to the specified length. If
640 * the new size is smaller than the %string's current size the %string
641 * is truncated, otherwise the %string is extended and new characters
642 * are default-constructed. For basic types such as char, this means
643 * setting them to 0.
645 void
646 resize(size_type __n)
647 { this->resize(__n, _CharT()); }
650 * Returns the total number of characters that the %string can hold
651 * before needing to allocate more memory.
653 size_type
654 capacity() const
655 { return _M_rep()->_M_capacity; }
658 * @brief Attempt to preallocate enough memory for specified number of
659 * characters.
660 * @param res_arg Number of characters required.
661 * @throw std::length_error If @a res_arg exceeds @c max_size().
663 * This function attempts to reserve enough memory for the
664 * %string to hold the specified number of characters. If the
665 * number requested is more than max_size(), length_error is
666 * thrown.
668 * The advantage of this function is that if optimal code is a
669 * necessity and the user can determine the string length that will be
670 * required, the user can reserve the memory in %advance, and thus
671 * prevent a possible reallocation of memory and copying of %string
672 * data.
674 void
675 reserve(size_type __res_arg = 0);
678 * Erases the string, making it empty.
680 void
681 clear()
682 { _M_mutate(0, this->size(), 0); }
685 * Returns true if the %string is empty. Equivalent to *this == "".
687 bool
688 empty() const
689 { return this->size() == 0; }
691 // Element access:
693 * @brief Subscript access to the data contained in the %string.
694 * @param pos The index of the character to access.
695 * @return Read-only (constant) reference to the character.
697 * This operator allows for easy, array-style, data access.
698 * Note that data access with this operator is unchecked and
699 * out_of_range lookups are not defined. (For checked lookups
700 * see at().)
702 const_reference
703 operator[] (size_type __pos) const
705 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
706 return _M_data()[__pos];
710 * @brief Subscript access to the data contained in the %string.
711 * @param pos The index of the character to access.
712 * @return Read/write reference to the character.
714 * This operator allows for easy, array-style, data access.
715 * Note that data access with this operator is unchecked and
716 * out_of_range lookups are not defined. (For checked lookups
717 * see at().) Unshares the string.
719 reference
720 operator[](size_type __pos)
722 // allow pos == size() as v3 extension:
723 _GLIBCXX_DEBUG_ASSERT(__pos <= size());
724 // but be strict in pedantic mode:
725 _GLIBCXX_DEBUG_PEDASSERT(__pos < size());
726 _M_leak();
727 return _M_data()[__pos];
731 * @brief Provides access to the data contained in the %string.
732 * @param n The index of the character to access.
733 * @return Read-only (const) reference to the character.
734 * @throw std::out_of_range If @a n is an invalid index.
736 * This function provides for safer data access. The parameter is
737 * first checked that it is in the range of the string. The function
738 * throws out_of_range if the check fails.
740 const_reference
741 at(size_type __n) const
743 if (__n >= this->size())
744 __throw_out_of_range(__N("basic_string::at"));
745 return _M_data()[__n];
749 * @brief Provides access to the data contained in the %string.
750 * @param n The index of the character to access.
751 * @return Read/write reference to the character.
752 * @throw std::out_of_range If @a n is an invalid index.
754 * This function provides for safer data access. The parameter is
755 * first checked that it is in the range of the string. The function
756 * throws out_of_range if the check fails. Success results in
757 * unsharing the string.
759 reference
760 at(size_type __n)
762 if (__n >= size())
763 __throw_out_of_range(__N("basic_string::at"));
764 _M_leak();
765 return _M_data()[__n];
768 // Modifiers:
770 * @brief Append a string to this string.
771 * @param str The string to append.
772 * @return Reference to this string.
774 basic_string&
775 operator+=(const basic_string& __str)
776 { return this->append(__str); }
779 * @brief Append a C string.
780 * @param s The C string to append.
781 * @return Reference to this string.
783 basic_string&
784 operator+=(const _CharT* __s)
785 { return this->append(__s); }
788 * @brief Append a character.
789 * @param c The character to append.
790 * @return Reference to this string.
792 basic_string&
793 operator+=(_CharT __c)
795 this->push_back(__c);
796 return *this;
800 * @brief Append a string to this string.
801 * @param str The string to append.
802 * @return Reference to this string.
804 basic_string&
805 append(const basic_string& __str);
808 * @brief Append a substring.
809 * @param str The string to append.
810 * @param pos Index of the first character of str to append.
811 * @param n The number of characters to append.
812 * @return Reference to this string.
813 * @throw std::out_of_range if @a pos is not a valid index.
815 * This function appends @a n characters from @a str starting at @a pos
816 * to this string. If @a n is is larger than the number of available
817 * characters in @a str, the remainder of @a str is appended.
819 basic_string&
820 append(const basic_string& __str, size_type __pos, size_type __n);
823 * @brief Append a C substring.
824 * @param s The C string to append.
825 * @param n The number of characters to append.
826 * @return Reference to this string.
828 basic_string&
829 append(const _CharT* __s, size_type __n);
832 * @brief Append a C string.
833 * @param s The C string to append.
834 * @return Reference to this string.
836 basic_string&
837 append(const _CharT* __s)
839 __glibcxx_requires_string(__s);
840 return this->append(__s, traits_type::length(__s));
844 * @brief Append multiple characters.
845 * @param n The number of characters to append.
846 * @param c The character to use.
847 * @return Reference to this string.
849 * Appends n copies of c to this string.
851 basic_string&
852 append(size_type __n, _CharT __c);
855 * @brief Append a range of characters.
856 * @param first Iterator referencing the first character to append.
857 * @param last Iterator marking the end of the range.
858 * @return Reference to this string.
860 * Appends characters in the range [first,last) to this string.
862 template<class _InputIterator>
863 basic_string&
864 append(_InputIterator __first, _InputIterator __last)
865 { return this->replace(_M_iend(), _M_iend(), __first, __last); }
868 * @brief Append a single character.
869 * @param c Character to append.
871 void
872 push_back(_CharT __c)
874 const size_type __len = 1 + this->size();
875 if (__len > this->capacity() || _M_rep()->_M_is_shared())
876 this->reserve(__len);
877 traits_type::assign(_M_data()[this->size()], __c);
878 _M_rep()->_M_set_length_and_sharable(__len);
882 * @brief Set value to contents of another string.
883 * @param str Source string to use.
884 * @return Reference to this string.
886 basic_string&
887 assign(const basic_string& __str);
890 * @brief Set value to a substring of a string.
891 * @param str The string to use.
892 * @param pos Index of the first character of str.
893 * @param n Number of characters to use.
894 * @return Reference to this string.
895 * @throw std::out_of_range if @a pos is not a valid index.
897 * This function sets this string to the substring of @a str consisting
898 * of @a n characters at @a pos. If @a n is is larger than the number
899 * of available characters in @a str, the remainder of @a str is used.
901 basic_string&
902 assign(const basic_string& __str, size_type __pos, size_type __n)
903 { return this->assign(__str._M_data()
904 + __str._M_check(__pos, "basic_string::assign"),
905 __str._M_limit(__pos, __n)); }
908 * @brief Set value to a C substring.
909 * @param s The C string to use.
910 * @param n Number of characters to use.
911 * @return Reference to this string.
913 * This function sets the value of this string to the first @a n
914 * characters of @a s. If @a n is is larger than the number of
915 * available characters in @a s, the remainder of @a s is used.
917 basic_string&
918 assign(const _CharT* __s, size_type __n);
921 * @brief Set value to contents of a C string.
922 * @param s The C string to use.
923 * @return Reference to this string.
925 * This function sets the value of this string to the value of @a s.
926 * The data is copied, so there is no dependence on @a s once the
927 * function returns.
929 basic_string&
930 assign(const _CharT* __s)
932 __glibcxx_requires_string(__s);
933 return this->assign(__s, traits_type::length(__s));
937 * @brief Set value to multiple characters.
938 * @param n Length of the resulting string.
939 * @param c The character to use.
940 * @return Reference to this string.
942 * This function sets the value of this string to @a n copies of
943 * character @a c.
945 basic_string&
946 assign(size_type __n, _CharT __c)
947 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
950 * @brief Set value to a range of characters.
951 * @param first Iterator referencing the first character to append.
952 * @param last Iterator marking the end of the range.
953 * @return Reference to this string.
955 * Sets value of string to characters in the range [first,last).
957 template<class _InputIterator>
958 basic_string&
959 assign(_InputIterator __first, _InputIterator __last)
960 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
963 * @brief Insert multiple characters.
964 * @param p Iterator referencing location in string to insert at.
965 * @param n Number of characters to insert
966 * @param c The character to insert.
967 * @throw std::length_error If new length exceeds @c max_size().
969 * Inserts @a n copies of character @a c starting at the position
970 * referenced by iterator @a p. If adding characters causes the length
971 * to exceed max_size(), length_error is thrown. The value of the
972 * string doesn't change if an error is thrown.
974 void
975 insert(iterator __p, size_type __n, _CharT __c)
976 { this->replace(__p, __p, __n, __c); }
979 * @brief Insert a range of characters.
980 * @param p Iterator referencing location in string to insert at.
981 * @param beg Start of range.
982 * @param end End of range.
983 * @throw std::length_error If new length exceeds @c max_size().
985 * Inserts characters in range [beg,end). If adding characters causes
986 * the length to exceed max_size(), length_error is thrown. The value
987 * of the string doesn't change if an error is thrown.
989 template<class _InputIterator>
990 void
991 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
992 { this->replace(__p, __p, __beg, __end); }
995 * @brief Insert value of a string.
996 * @param pos1 Iterator referencing location in string to insert at.
997 * @param str The string to insert.
998 * @return Reference to this string.
999 * @throw std::length_error If new length exceeds @c max_size().
1001 * Inserts value of @a str starting at @a pos1. If adding characters
1002 * causes the length to exceed max_size(), length_error is thrown. The
1003 * value of the string doesn't change if an error is thrown.
1005 basic_string&
1006 insert(size_type __pos1, const basic_string& __str)
1007 { return this->insert(__pos1, __str, size_type(0), __str.size()); }
1010 * @brief Insert a substring.
1011 * @param pos1 Iterator referencing location in string to insert at.
1012 * @param str The string to insert.
1013 * @param pos2 Start of characters in str to insert.
1014 * @param n Number of characters to insert.
1015 * @return Reference to this string.
1016 * @throw std::length_error If new length exceeds @c max_size().
1017 * @throw std::out_of_range If @a pos1 > size() or
1018 * @a pos2 > @a str.size().
1020 * Starting at @a pos1, insert @a n character of @a str beginning with
1021 * @a pos2. If adding characters causes the length to exceed
1022 * max_size(), length_error is thrown. If @a pos1 is beyond the end of
1023 * this string or @a pos2 is beyond the end of @a str, out_of_range is
1024 * thrown. The value of the string doesn't change if an error is
1025 * thrown.
1027 basic_string&
1028 insert(size_type __pos1, const basic_string& __str,
1029 size_type __pos2, size_type __n)
1030 { return this->insert(__pos1, __str._M_data()
1031 + __str._M_check(__pos2, "basic_string::insert"),
1032 __str._M_limit(__pos2, __n)); }
1035 * @brief Insert a C substring.
1036 * @param pos Iterator referencing location in string to insert at.
1037 * @param s The C string to insert.
1038 * @param n The number of characters to insert.
1039 * @return Reference to this string.
1040 * @throw std::length_error If new length exceeds @c max_size().
1041 * @throw std::out_of_range If @a pos is beyond the end of this
1042 * string.
1044 * Inserts the first @a n characters of @a s starting at @a pos. If
1045 * adding characters causes the length to exceed max_size(),
1046 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1047 * thrown. The value of the string doesn't change if an error is
1048 * thrown.
1050 basic_string&
1051 insert(size_type __pos, const _CharT* __s, size_type __n);
1054 * @brief Insert a C string.
1055 * @param pos Iterator referencing location in string to insert at.
1056 * @param s The C string to insert.
1057 * @return Reference to this string.
1058 * @throw std::length_error If new length exceeds @c max_size().
1059 * @throw std::out_of_range If @a pos is beyond the end of this
1060 * string.
1062 * Inserts the first @a n characters of @a s starting at @a pos. If
1063 * adding characters causes the length to exceed max_size(),
1064 * length_error is thrown. If @a pos is beyond end(), out_of_range is
1065 * thrown. The value of the string doesn't change if an error is
1066 * thrown.
1068 basic_string&
1069 insert(size_type __pos, const _CharT* __s)
1071 __glibcxx_requires_string(__s);
1072 return this->insert(__pos, __s, traits_type::length(__s));
1076 * @brief Insert multiple characters.
1077 * @param pos Index in string to insert at.
1078 * @param n Number of characters to insert
1079 * @param c The character 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 pos is beyond the end of this
1083 * string.
1085 * Inserts @a n copies of character @a c starting at index @a pos. If
1086 * adding characters causes the length to exceed max_size(),
1087 * length_error is thrown. If @a pos > length(), out_of_range is
1088 * thrown. The value of the string doesn't change if an error is
1089 * thrown.
1091 basic_string&
1092 insert(size_type __pos, size_type __n, _CharT __c)
1093 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
1094 size_type(0), __n, __c); }
1097 * @brief Insert one character.
1098 * @param p Iterator referencing position in string to insert at.
1099 * @param c The character to insert.
1100 * @return Iterator referencing newly inserted char.
1101 * @throw std::length_error If new length exceeds @c max_size().
1103 * Inserts character @a c at position referenced by @a p. If adding
1104 * character causes the length to exceed max_size(), length_error is
1105 * thrown. If @a p is beyond end of string, out_of_range is thrown.
1106 * The value of the string doesn't change if an error is thrown.
1108 iterator
1109 insert(iterator __p, _CharT __c)
1111 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1112 const size_type __pos = __p - _M_ibegin();
1113 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1114 _M_rep()->_M_set_leaked();
1115 return iterator(_M_data() + __pos);
1119 * @brief Remove characters.
1120 * @param pos Index of first character to remove (default 0).
1121 * @param n Number of characters to remove (default remainder).
1122 * @return Reference to this string.
1123 * @throw std::out_of_range If @a pos is beyond the end of this
1124 * string.
1126 * Removes @a n characters from this string starting at @a pos. The
1127 * length of the string is reduced by @a n. If there are < @a n
1128 * characters to remove, the remainder of the string is truncated. If
1129 * @a p is beyond end of string, out_of_range is thrown. The value of
1130 * the string doesn't change if an error is thrown.
1132 basic_string&
1133 erase(size_type __pos = 0, size_type __n = npos)
1135 _M_mutate(_M_check(__pos, "basic_string::erase"),
1136 _M_limit(__pos, __n), size_type(0));
1137 return *this;
1141 * @brief Remove one character.
1142 * @param position Iterator referencing the character to remove.
1143 * @return iterator referencing same location after removal.
1145 * Removes the character at @a position from this string. The value
1146 * of the string doesn't change if an error is thrown.
1148 iterator
1149 erase(iterator __position)
1151 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
1152 && __position < _M_iend());
1153 const size_type __pos = __position - _M_ibegin();
1154 _M_mutate(__pos, size_type(1), size_type(0));
1155 _M_rep()->_M_set_leaked();
1156 return iterator(_M_data() + __pos);
1160 * @brief Remove a range of characters.
1161 * @param first Iterator referencing the first character to remove.
1162 * @param last Iterator referencing the end of the range.
1163 * @return Iterator referencing location of first after removal.
1165 * Removes the characters in the range [first,last) from this string.
1166 * The value of the string doesn't change if an error is thrown.
1168 iterator
1169 erase(iterator __first, iterator __last)
1171 _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
1172 && __last <= _M_iend());
1173 const size_type __pos = __first - _M_ibegin();
1174 _M_mutate(__pos, __last - __first, size_type(0));
1175 _M_rep()->_M_set_leaked();
1176 return iterator(_M_data() + __pos);
1180 * @brief Replace characters with value from another string.
1181 * @param pos Index of first character to replace.
1182 * @param n Number of characters to be replaced.
1183 * @param str String to insert.
1184 * @return Reference to this string.
1185 * @throw std::out_of_range If @a pos is beyond the end of this
1186 * string.
1187 * @throw std::length_error If new length exceeds @c max_size().
1189 * Removes the characters in the range [pos,pos+n) from this string.
1190 * In place, the value of @a str is inserted. If @a pos is beyond end
1191 * of string, out_of_range is thrown. If the length of the result
1192 * exceeds max_size(), length_error is thrown. The value of the string
1193 * doesn't change if an error is thrown.
1195 basic_string&
1196 replace(size_type __pos, size_type __n, const basic_string& __str)
1197 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1200 * @brief Replace characters with value from another string.
1201 * @param pos1 Index of first character to replace.
1202 * @param n1 Number of characters to be replaced.
1203 * @param str String to insert.
1204 * @param pos2 Index of first character of str to use.
1205 * @param n2 Number of characters from str to use.
1206 * @return Reference to this string.
1207 * @throw std::out_of_range If @a pos1 > size() or @a pos2 >
1208 * str.size().
1209 * @throw std::length_error If new length exceeds @c max_size().
1211 * Removes the characters in the range [pos1,pos1 + n) from this
1212 * string. In place, the value of @a str is inserted. If @a pos is
1213 * beyond end of string, out_of_range is thrown. If the length of the
1214 * result exceeds max_size(), length_error is thrown. The value of the
1215 * string doesn't change if an error is thrown.
1217 basic_string&
1218 replace(size_type __pos1, size_type __n1, const basic_string& __str,
1219 size_type __pos2, size_type __n2)
1220 { return this->replace(__pos1, __n1, __str._M_data()
1221 + __str._M_check(__pos2, "basic_string::replace"),
1222 __str._M_limit(__pos2, __n2)); }
1225 * @brief Replace characters with value of a C substring.
1226 * @param pos Index of first character to replace.
1227 * @param n1 Number of characters to be replaced.
1228 * @param s C string to insert.
1229 * @param n2 Number of characters from @a s to use.
1230 * @return Reference to this string.
1231 * @throw std::out_of_range If @a pos1 > size().
1232 * @throw std::length_error If new length exceeds @c max_size().
1234 * Removes the characters in the range [pos,pos + n1) from this string.
1235 * In place, the first @a n2 characters of @a s are inserted, or all
1236 * of @a s if @a n2 is too large. If @a pos is beyond end of string,
1237 * out_of_range is thrown. If the length of result exceeds max_size(),
1238 * length_error is thrown. The value of the string doesn't change if
1239 * an error is thrown.
1241 basic_string&
1242 replace(size_type __pos, size_type __n1, const _CharT* __s,
1243 size_type __n2);
1246 * @brief Replace characters with value of a C string.
1247 * @param pos Index of first character to replace.
1248 * @param n1 Number of characters to be replaced.
1249 * @param s C string to insert.
1250 * @return Reference to this string.
1251 * @throw std::out_of_range If @a pos > size().
1252 * @throw std::length_error If new length exceeds @c max_size().
1254 * Removes the characters in the range [pos,pos + n1) from this string.
1255 * In place, the first @a n characters of @a s are inserted. If @a
1256 * pos is beyond end of string, out_of_range is thrown. If the length
1257 * of result exceeds max_size(), length_error is thrown. The value of
1258 * the string doesn't change if an error is thrown.
1260 basic_string&
1261 replace(size_type __pos, size_type __n1, const _CharT* __s)
1263 __glibcxx_requires_string(__s);
1264 return this->replace(__pos, __n1, __s, traits_type::length(__s));
1268 * @brief Replace characters with multiple characters.
1269 * @param pos Index of first character to replace.
1270 * @param n1 Number of characters to be replaced.
1271 * @param n2 Number of characters to insert.
1272 * @param c Character to insert.
1273 * @return Reference to this string.
1274 * @throw std::out_of_range If @a pos > size().
1275 * @throw std::length_error If new length exceeds @c max_size().
1277 * Removes the characters in the range [pos,pos + n1) from this string.
1278 * In place, @a n2 copies of @a c are inserted. If @a pos is beyond
1279 * end of string, out_of_range is thrown. If the length of result
1280 * exceeds max_size(), length_error is thrown. The value of the string
1281 * doesn't change if an error is thrown.
1283 basic_string&
1284 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1285 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
1286 _M_limit(__pos, __n1), __n2, __c); }
1289 * @brief Replace range of characters with string.
1290 * @param i1 Iterator referencing start of range to replace.
1291 * @param i2 Iterator referencing end of range to replace.
1292 * @param str String value to insert.
1293 * @return Reference to this string.
1294 * @throw std::length_error If new length exceeds @c max_size().
1296 * Removes the characters in the range [i1,i2). In place, the value of
1297 * @a str is inserted. If the length of result exceeds max_size(),
1298 * length_error is thrown. The value of the string doesn't change if
1299 * an error is thrown.
1301 basic_string&
1302 replace(iterator __i1, iterator __i2, const basic_string& __str)
1303 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
1306 * @brief Replace range of characters with C substring.
1307 * @param i1 Iterator referencing start of range to replace.
1308 * @param i2 Iterator referencing end of range to replace.
1309 * @param s C string value to insert.
1310 * @param n Number of characters from s to insert.
1311 * @return Reference to this string.
1312 * @throw std::length_error If new length exceeds @c max_size().
1314 * Removes the characters in the range [i1,i2). In place, the first @a
1315 * n characters of @a s are inserted. If the length of result exceeds
1316 * max_size(), length_error is thrown. The value of the string doesn't
1317 * change if an error is thrown.
1319 basic_string&
1320 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
1322 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1323 && __i2 <= _M_iend());
1324 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
1328 * @brief Replace range of characters with C string.
1329 * @param i1 Iterator referencing start of range to replace.
1330 * @param i2 Iterator referencing end of range to replace.
1331 * @param s C string value to insert.
1332 * @return Reference to this string.
1333 * @throw std::length_error If new length exceeds @c max_size().
1335 * Removes the characters in the range [i1,i2). In place, the
1336 * characters of @a s are inserted. If the length of result exceeds
1337 * max_size(), length_error is thrown. The value of the string doesn't
1338 * change if an error is thrown.
1340 basic_string&
1341 replace(iterator __i1, iterator __i2, const _CharT* __s)
1343 __glibcxx_requires_string(__s);
1344 return this->replace(__i1, __i2, __s, traits_type::length(__s));
1348 * @brief Replace range of characters with multiple characters
1349 * @param i1 Iterator referencing start of range to replace.
1350 * @param i2 Iterator referencing end of range to replace.
1351 * @param n Number of characters to insert.
1352 * @param c Character to insert.
1353 * @return Reference to this string.
1354 * @throw std::length_error If new length exceeds @c max_size().
1356 * Removes the characters in the range [i1,i2). In place, @a n copies
1357 * of @a c are inserted. If the length of result exceeds max_size(),
1358 * length_error is thrown. The value of the string doesn't change if
1359 * an error is thrown.
1361 basic_string&
1362 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
1364 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1365 && __i2 <= _M_iend());
1366 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
1370 * @brief Replace range of characters with range.
1371 * @param i1 Iterator referencing start of range to replace.
1372 * @param i2 Iterator referencing end of range to replace.
1373 * @param k1 Iterator referencing start of range to insert.
1374 * @param k2 Iterator referencing end of range to insert.
1375 * @return Reference to this string.
1376 * @throw std::length_error If new length exceeds @c max_size().
1378 * Removes the characters in the range [i1,i2). In place, characters
1379 * in the range [k1,k2) are inserted. If the length of result exceeds
1380 * max_size(), length_error is thrown. The value of the string doesn't
1381 * change if an error is thrown.
1383 template<class _InputIterator>
1384 basic_string&
1385 replace(iterator __i1, iterator __i2,
1386 _InputIterator __k1, _InputIterator __k2)
1388 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1389 && __i2 <= _M_iend());
1390 __glibcxx_requires_valid_range(__k1, __k2);
1391 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1392 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
1395 // Specializations for the common case of pointer and iterator:
1396 // useful to avoid the overhead of temporary buffering in _M_replace.
1397 basic_string&
1398 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
1400 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1401 && __i2 <= _M_iend());
1402 __glibcxx_requires_valid_range(__k1, __k2);
1403 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1404 __k1, __k2 - __k1);
1407 basic_string&
1408 replace(iterator __i1, iterator __i2,
1409 const _CharT* __k1, const _CharT* __k2)
1411 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1412 && __i2 <= _M_iend());
1413 __glibcxx_requires_valid_range(__k1, __k2);
1414 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1415 __k1, __k2 - __k1);
1418 basic_string&
1419 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
1421 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1422 && __i2 <= _M_iend());
1423 __glibcxx_requires_valid_range(__k1, __k2);
1424 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1425 __k1.base(), __k2 - __k1);
1428 basic_string&
1429 replace(iterator __i1, iterator __i2,
1430 const_iterator __k1, const_iterator __k2)
1432 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1433 && __i2 <= _M_iend());
1434 __glibcxx_requires_valid_range(__k1, __k2);
1435 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1436 __k1.base(), __k2 - __k1);
1439 private:
1440 template<class _Integer>
1441 basic_string&
1442 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
1443 _Integer __val, __true_type)
1444 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
1446 template<class _InputIterator>
1447 basic_string&
1448 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
1449 _InputIterator __k2, __false_type);
1451 basic_string&
1452 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
1453 _CharT __c);
1455 basic_string&
1456 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
1457 size_type __n2);
1459 // _S_construct_aux is used to implement the 21.3.1 para 15 which
1460 // requires special behaviour if _InIter is an integral type
1461 template<class _InIterator>
1462 static _CharT*
1463 _S_construct_aux(_InIterator __beg, _InIterator __end,
1464 const _Alloc& __a, __false_type)
1466 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
1467 return _S_construct(__beg, __end, __a, _Tag());
1470 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1471 // 438. Ambiguity in the "do the right thing" clause
1472 template<class _Integer>
1473 static _CharT*
1474 _S_construct_aux(_Integer __beg, _Integer __end,
1475 const _Alloc& __a, __true_type)
1476 { return _S_construct(static_cast<size_type>(__beg), __end, __a); }
1478 template<class _InIterator>
1479 static _CharT*
1480 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
1482 typedef typename std::__is_integer<_InIterator>::__type _Integral;
1483 return _S_construct_aux(__beg, __end, __a, _Integral());
1486 // For Input Iterators, used in istreambuf_iterators, etc.
1487 template<class _InIterator>
1488 static _CharT*
1489 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1490 input_iterator_tag);
1492 // For forward_iterators up to random_access_iterators, used for
1493 // string::iterator, _CharT*, etc.
1494 template<class _FwdIterator>
1495 static _CharT*
1496 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
1497 forward_iterator_tag);
1499 static _CharT*
1500 _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
1502 public:
1505 * @brief Copy substring into C string.
1506 * @param s C string to copy value into.
1507 * @param n Number of characters to copy.
1508 * @param pos Index of first character to copy.
1509 * @return Number of characters actually copied
1510 * @throw std::out_of_range If pos > size().
1512 * Copies up to @a n characters starting at @a pos into the C string @a
1513 * s. If @a pos is greater than size(), out_of_range is thrown.
1515 size_type
1516 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
1519 * @brief Swap contents with another string.
1520 * @param s String to swap with.
1522 * Exchanges the contents of this string with that of @a s in constant
1523 * time.
1525 void
1526 swap(basic_string& __s);
1528 // String operations:
1530 * @brief Return const pointer to null-terminated contents.
1532 * This is a handle to internal data. Do not modify or dire things may
1533 * happen.
1535 const _CharT*
1536 c_str() const
1537 { return _M_data(); }
1540 * @brief Return const pointer to contents.
1542 * This is a handle to internal data. Do not modify or dire things may
1543 * happen.
1545 const _CharT*
1546 data() const
1547 { return _M_data(); }
1550 * @brief Return copy of allocator used to construct this string.
1552 allocator_type
1553 get_allocator() const
1554 { return _M_dataplus; }
1557 * @brief Find position of a C substring.
1558 * @param s C string to locate.
1559 * @param pos Index of character to search from.
1560 * @param n Number of characters from @a s to search for.
1561 * @return Index of start of first occurrence.
1563 * Starting from @a pos, searches forward for the first @a n characters
1564 * in @a s within this string. If found, returns the index where it
1565 * begins. If not found, returns npos.
1567 size_type
1568 find(const _CharT* __s, size_type __pos, size_type __n) const;
1571 * @brief Find position of a string.
1572 * @param str String to locate.
1573 * @param pos Index of character to search from (default 0).
1574 * @return Index of start of first occurrence.
1576 * Starting from @a pos, searches forward for value of @a str within
1577 * this string. If found, returns the index where it begins. If not
1578 * found, returns npos.
1580 size_type
1581 find(const basic_string& __str, size_type __pos = 0) const
1582 { return this->find(__str.data(), __pos, __str.size()); }
1585 * @brief Find position of a C string.
1586 * @param s C string to locate.
1587 * @param pos Index of character to search from (default 0).
1588 * @return Index of start of first occurrence.
1590 * Starting from @a pos, searches forward for the value of @a s within
1591 * this string. If found, returns the index where it begins. If not
1592 * found, returns npos.
1594 size_type
1595 find(const _CharT* __s, size_type __pos = 0) const
1597 __glibcxx_requires_string(__s);
1598 return this->find(__s, __pos, traits_type::length(__s));
1602 * @brief Find position of a character.
1603 * @param c Character to locate.
1604 * @param pos Index of character to search from (default 0).
1605 * @return Index of first occurrence.
1607 * Starting from @a pos, searches forward for @a c within this string.
1608 * If found, returns the index where it was found. If not found,
1609 * returns npos.
1611 size_type
1612 find(_CharT __c, size_type __pos = 0) const;
1615 * @brief Find last position of a string.
1616 * @param str String to locate.
1617 * @param pos Index of character to search back from (default end).
1618 * @return Index of start of last occurrence.
1620 * Starting from @a pos, searches backward for value of @a str within
1621 * this string. If found, returns the index where it begins. If not
1622 * found, returns npos.
1624 size_type
1625 rfind(const basic_string& __str, size_type __pos = npos) const
1626 { return this->rfind(__str.data(), __pos, __str.size()); }
1629 * @brief Find last position of a C substring.
1630 * @param s C string to locate.
1631 * @param pos Index of character to search back from.
1632 * @param n Number of characters from s to search for.
1633 * @return Index of start of last occurrence.
1635 * Starting from @a pos, searches backward for the first @a n
1636 * characters in @a s within this string. If found, returns the index
1637 * where it begins. If not found, returns npos.
1639 size_type
1640 rfind(const _CharT* __s, size_type __pos, size_type __n) const;
1643 * @brief Find last position of a C string.
1644 * @param s C string to locate.
1645 * @param pos Index of character to start search at (default end).
1646 * @return Index of start of last occurrence.
1648 * Starting from @a pos, searches backward for the value of @a s within
1649 * this string. If found, returns the index where it begins. If not
1650 * found, returns npos.
1652 size_type
1653 rfind(const _CharT* __s, size_type __pos = npos) const
1655 __glibcxx_requires_string(__s);
1656 return this->rfind(__s, __pos, traits_type::length(__s));
1660 * @brief Find last position of a character.
1661 * @param c Character to locate.
1662 * @param pos Index of character to search back from (default end).
1663 * @return Index of last occurrence.
1665 * Starting from @a pos, searches backward for @a c within this string.
1666 * If found, returns the index where it was found. If not found,
1667 * returns npos.
1669 size_type
1670 rfind(_CharT __c, size_type __pos = npos) const;
1673 * @brief Find position of a character of string.
1674 * @param str String containing characters to locate.
1675 * @param pos Index of character to search from (default 0).
1676 * @return Index of first occurrence.
1678 * Starting from @a pos, searches forward for one of the characters of
1679 * @a str within this string. If found, returns the index where it was
1680 * found. If not found, returns npos.
1682 size_type
1683 find_first_of(const basic_string& __str, size_type __pos = 0) const
1684 { return this->find_first_of(__str.data(), __pos, __str.size()); }
1687 * @brief Find position of a character of C substring.
1688 * @param s String containing characters to locate.
1689 * @param pos Index of character to search from (default 0).
1690 * @param n Number of characters from s to search for.
1691 * @return Index of first occurrence.
1693 * Starting from @a pos, searches forward for one of the first @a n
1694 * characters of @a s within this string. If found, returns the index
1695 * where it was found. If not found, returns npos.
1697 size_type
1698 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
1701 * @brief Find position of a character of C string.
1702 * @param s String containing characters to locate.
1703 * @param pos Index of character to search from (default 0).
1704 * @return Index of first occurrence.
1706 * Starting from @a pos, searches forward for one of the characters of
1707 * @a s within this string. If found, returns the index where it was
1708 * found. If not found, returns npos.
1710 size_type
1711 find_first_of(const _CharT* __s, size_type __pos = 0) const
1713 __glibcxx_requires_string(__s);
1714 return this->find_first_of(__s, __pos, traits_type::length(__s));
1718 * @brief Find position of a character.
1719 * @param c Character to locate.
1720 * @param pos Index of character to search from (default 0).
1721 * @return Index of first occurrence.
1723 * Starting from @a pos, searches forward for the character @a c within
1724 * this string. If found, returns the index where it was found. If
1725 * not found, returns npos.
1727 * Note: equivalent to find(c, pos).
1729 size_type
1730 find_first_of(_CharT __c, size_type __pos = 0) const
1731 { return this->find(__c, __pos); }
1734 * @brief Find last position of a character of string.
1735 * @param str String containing characters to locate.
1736 * @param pos Index of character to search back from (default end).
1737 * @return Index of last occurrence.
1739 * Starting from @a pos, searches backward for one of the characters of
1740 * @a str within this string. If found, returns the index where it was
1741 * found. If not found, returns npos.
1743 size_type
1744 find_last_of(const basic_string& __str, size_type __pos = npos) const
1745 { return this->find_last_of(__str.data(), __pos, __str.size()); }
1748 * @brief Find last position of a character of C substring.
1749 * @param s C string containing characters to locate.
1750 * @param pos Index of character to search back from (default end).
1751 * @param n Number of characters from s to search for.
1752 * @return Index of last occurrence.
1754 * Starting from @a pos, searches backward for one of the first @a n
1755 * characters of @a s within this string. If found, returns the index
1756 * where it was found. If not found, returns npos.
1758 size_type
1759 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
1762 * @brief Find last position of a character of C string.
1763 * @param s C string containing characters to locate.
1764 * @param pos Index of character to search back from (default end).
1765 * @return Index of last occurrence.
1767 * Starting from @a pos, searches backward for one of the characters of
1768 * @a s within this string. If found, returns the index where it was
1769 * found. If not found, returns npos.
1771 size_type
1772 find_last_of(const _CharT* __s, size_type __pos = npos) const
1774 __glibcxx_requires_string(__s);
1775 return this->find_last_of(__s, __pos, traits_type::length(__s));
1779 * @brief Find last position of a character.
1780 * @param c Character to locate.
1781 * @param pos Index of character to search back from (default 0).
1782 * @return Index of last occurrence.
1784 * Starting from @a pos, searches backward for @a c within this string.
1785 * If found, returns the index where it was found. If not found,
1786 * returns npos.
1788 * Note: equivalent to rfind(c, pos).
1790 size_type
1791 find_last_of(_CharT __c, size_type __pos = npos) const
1792 { return this->rfind(__c, __pos); }
1795 * @brief Find position of a character not in string.
1796 * @param str String containing characters to avoid.
1797 * @param pos Index of character to search from (default 0).
1798 * @return Index of first occurrence.
1800 * Starting from @a pos, searches forward for a character not contained
1801 * in @a str within this string. If found, returns the index where it
1802 * was found. If not found, returns npos.
1804 size_type
1805 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
1806 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
1809 * @brief Find position of a character not in C substring.
1810 * @param s C string containing characters to avoid.
1811 * @param pos Index of character to search from (default 0).
1812 * @param n Number of characters from s to consider.
1813 * @return Index of first occurrence.
1815 * Starting from @a pos, searches forward for a character not contained
1816 * in the first @a n characters of @a s within this string. If found,
1817 * returns the index where it was found. If not found, returns npos.
1819 size_type
1820 find_first_not_of(const _CharT* __s, size_type __pos,
1821 size_type __n) const;
1824 * @brief Find position of a character not in C string.
1825 * @param s C string containing characters to avoid.
1826 * @param pos Index of character to search from (default 0).
1827 * @return Index of first occurrence.
1829 * Starting from @a pos, searches forward for a character not contained
1830 * in @a s within this string. If found, returns the index where it
1831 * was found. If not found, returns npos.
1833 size_type
1834 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
1836 __glibcxx_requires_string(__s);
1837 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
1841 * @brief Find position of a different character.
1842 * @param c Character to avoid.
1843 * @param pos Index of character to search from (default 0).
1844 * @return Index of first occurrence.
1846 * Starting from @a pos, searches forward for a character other than @a c
1847 * within this string. If found, returns the index where it was found.
1848 * If not found, returns npos.
1850 size_type
1851 find_first_not_of(_CharT __c, size_type __pos = 0) const;
1854 * @brief Find last position of a character not in string.
1855 * @param str String containing characters to avoid.
1856 * @param pos Index of character to search from (default 0).
1857 * @return Index of first occurrence.
1859 * Starting from @a pos, searches backward for a character not
1860 * contained in @a str within this string. If found, returns the index
1861 * where it was found. If not found, returns npos.
1863 size_type
1864 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
1865 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
1868 * @brief Find last position of a character not in C substring.
1869 * @param s C string containing characters to avoid.
1870 * @param pos Index of character to search from (default 0).
1871 * @param n Number of characters from s to consider.
1872 * @return Index of first occurrence.
1874 * Starting from @a pos, searches backward for a character not
1875 * contained in the first @a n characters of @a s within this string.
1876 * If found, returns the index where it was found. If not found,
1877 * returns npos.
1879 size_type
1880 find_last_not_of(const _CharT* __s, size_type __pos,
1881 size_type __n) const;
1883 * @brief Find position of a character not in C string.
1884 * @param s C string containing characters to avoid.
1885 * @param pos Index of character to search from (default 0).
1886 * @return Index of first occurrence.
1888 * Starting from @a pos, searches backward for a character not
1889 * contained in @a s within this string. If found, returns the index
1890 * where it was found. If not found, returns npos.
1892 size_type
1893 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
1895 __glibcxx_requires_string(__s);
1896 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
1900 * @brief Find last position of a different character.
1901 * @param c Character to avoid.
1902 * @param pos Index of character to search from (default 0).
1903 * @return Index of first occurrence.
1905 * Starting from @a pos, searches backward for a character other than
1906 * @a c within this string. If found, returns the index where it was
1907 * found. If not found, returns npos.
1909 size_type
1910 find_last_not_of(_CharT __c, size_type __pos = npos) const;
1913 * @brief Get a substring.
1914 * @param pos Index of first character (default 0).
1915 * @param n Number of characters in substring (default remainder).
1916 * @return The new string.
1917 * @throw std::out_of_range If pos > size().
1919 * Construct and return a new string using the @a n characters starting
1920 * at @a pos. If the string is too short, use the remainder of the
1921 * characters. If @a pos is beyond the end of the string, out_of_range
1922 * is thrown.
1924 basic_string
1925 substr(size_type __pos = 0, size_type __n = npos) const
1926 { return basic_string(*this,
1927 _M_check(__pos, "basic_string::substr"), __n); }
1930 * @brief Compare to a string.
1931 * @param str String to compare against.
1932 * @return Integer < 0, 0, or > 0.
1934 * Returns an integer < 0 if this string is ordered before @a str, 0 if
1935 * their values are equivalent, or > 0 if this string is ordered after
1936 * @a str. Determines the effective length rlen of the strings to
1937 * compare as the smallest of size() and str.size(). The function
1938 * then compares the two strings by calling traits::compare(data(),
1939 * str.data(),rlen). If the result of the comparison is nonzero returns
1940 * it, otherwise the shorter one is ordered first.
1943 compare(const basic_string& __str) const
1945 const size_type __size = this->size();
1946 const size_type __osize = __str.size();
1947 const size_type __len = std::min(__size, __osize);
1949 int __r = traits_type::compare(_M_data(), __str.data(), __len);
1950 if (!__r)
1951 __r = _S_compare(__size, __osize);
1952 return __r;
1956 * @brief Compare substring to a string.
1957 * @param pos Index of first character of substring.
1958 * @param n Number of characters in substring.
1959 * @param str String to compare against.
1960 * @return Integer < 0, 0, or > 0.
1962 * Form the substring of this string from the @a n characters starting
1963 * at @a pos. Returns an integer < 0 if the substring is ordered
1964 * before @a str, 0 if their values are equivalent, or > 0 if the
1965 * substring is ordered after @a str. Determines the effective length
1966 * rlen of the strings to compare as the smallest of the length of the
1967 * substring and @a str.size(). The function then compares the two
1968 * strings by calling traits::compare(substring.data(),str.data(),rlen).
1969 * If the result of the comparison is nonzero returns it, otherwise the
1970 * shorter one is ordered first.
1973 compare(size_type __pos, size_type __n, const basic_string& __str) const;
1976 * @brief Compare substring to a substring.
1977 * @param pos1 Index of first character of substring.
1978 * @param n1 Number of characters in substring.
1979 * @param str String to compare against.
1980 * @param pos2 Index of first character of substring of str.
1981 * @param n2 Number of characters in substring of str.
1982 * @return Integer < 0, 0, or > 0.
1984 * Form the substring of this string from the @a n1 characters starting
1985 * at @a pos1. Form the substring of @a str from the @a n2 characters
1986 * starting at @a pos2. Returns an integer < 0 if this substring is
1987 * ordered before the substring of @a str, 0 if their values are
1988 * equivalent, or > 0 if this substring is ordered after the substring
1989 * of @a str. Determines the effective length rlen of the strings
1990 * to compare as the smallest of the lengths of the substrings. The
1991 * function then compares the two strings by calling
1992 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
1993 * If the result of the comparison is nonzero returns it, otherwise the
1994 * shorter one is ordered first.
1997 compare(size_type __pos1, size_type __n1, const basic_string& __str,
1998 size_type __pos2, size_type __n2) const;
2001 * @brief Compare to a C string.
2002 * @param s C string to compare against.
2003 * @return Integer < 0, 0, or > 0.
2005 * Returns an integer < 0 if this string is ordered before @a s, 0 if
2006 * their values are equivalent, or > 0 if this string is ordered after
2007 * @a s. Determines the effective length rlen of the strings to
2008 * compare as the smallest of size() and the length of a string
2009 * constructed from @a s. The function then compares the two strings
2010 * by calling traits::compare(data(),s,rlen). If the result of the
2011 * comparison is nonzero returns it, otherwise the shorter one is
2012 * ordered first.
2015 compare(const _CharT* __s) const;
2017 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2018 // 5 String::compare specification questionable
2020 * @brief Compare substring to a C string.
2021 * @param pos Index of first character of substring.
2022 * @param n1 Number of characters in substring.
2023 * @param s C string to compare against.
2024 * @return Integer < 0, 0, or > 0.
2026 * Form the substring of this string from the @a n1 characters starting
2027 * at @a pos. Returns an integer < 0 if the substring is ordered
2028 * before @a s, 0 if their values are equivalent, or > 0 if the
2029 * substring is ordered after @a s. Determines the effective length
2030 * rlen of the strings to compare as the smallest of the length of the
2031 * substring and the length of a string constructed from @a s. The
2032 * function then compares the two string by calling
2033 * traits::compare(substring.data(),s,rlen). If the result of the
2034 * comparison is nonzero returns it, otherwise the shorter one is
2035 * ordered first.
2038 compare(size_type __pos, size_type __n1, const _CharT* __s) const;
2041 * @brief Compare substring against a character array.
2042 * @param pos1 Index of first character of substring.
2043 * @param n1 Number of characters in substring.
2044 * @param s character array to compare against.
2045 * @param n2 Number of characters of s.
2046 * @return Integer < 0, 0, or > 0.
2048 * Form the substring of this string from the @a n1 characters starting
2049 * at @a pos1. Form a string from the first @a n2 characters of @a s.
2050 * Returns an integer < 0 if this substring is ordered before the string
2051 * from @a s, 0 if their values are equivalent, or > 0 if this substring
2052 * is ordered after the string from @a s. Determines the effective
2053 * length rlen of the strings to compare as the smallest of the length
2054 * of the substring and @a n2. The function then compares the two
2055 * strings by calling traits::compare(substring.data(),s,rlen). If the
2056 * result of the comparison is nonzero returns it, otherwise the shorter
2057 * one is ordered first.
2059 * NB: s must have at least n2 characters, '\0' has no special
2060 * meaning.
2063 compare(size_type __pos, size_type __n1, const _CharT* __s,
2064 size_type __n2) const;
2067 template<typename _CharT, typename _Traits, typename _Alloc>
2068 inline basic_string<_CharT, _Traits, _Alloc>::
2069 basic_string()
2070 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
2071 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
2072 #else
2073 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()) { }
2074 #endif
2076 // operator+
2078 * @brief Concatenate two strings.
2079 * @param lhs First string.
2080 * @param rhs Last string.
2081 * @return New string with value of @a lhs followed by @a rhs.
2083 template<typename _CharT, typename _Traits, typename _Alloc>
2084 basic_string<_CharT, _Traits, _Alloc>
2085 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2086 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2088 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2089 __str.append(__rhs);
2090 return __str;
2094 * @brief Concatenate C string and string.
2095 * @param lhs First string.
2096 * @param rhs Last string.
2097 * @return New string with value of @a lhs followed by @a rhs.
2099 template<typename _CharT, typename _Traits, typename _Alloc>
2100 basic_string<_CharT,_Traits,_Alloc>
2101 operator+(const _CharT* __lhs,
2102 const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2105 * @brief Concatenate character and string.
2106 * @param lhs First string.
2107 * @param rhs Last string.
2108 * @return New string with @a lhs followed by @a rhs.
2110 template<typename _CharT, typename _Traits, typename _Alloc>
2111 basic_string<_CharT,_Traits,_Alloc>
2112 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2115 * @brief Concatenate string and C string.
2116 * @param lhs First string.
2117 * @param rhs Last string.
2118 * @return New string with @a lhs followed by @a rhs.
2120 template<typename _CharT, typename _Traits, typename _Alloc>
2121 inline basic_string<_CharT, _Traits, _Alloc>
2122 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2123 const _CharT* __rhs)
2125 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2126 __str.append(__rhs);
2127 return __str;
2131 * @brief Concatenate string and character.
2132 * @param lhs First string.
2133 * @param rhs Last string.
2134 * @return New string with @a lhs followed by @a rhs.
2136 template<typename _CharT, typename _Traits, typename _Alloc>
2137 inline basic_string<_CharT, _Traits, _Alloc>
2138 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
2140 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
2141 typedef typename __string_type::size_type __size_type;
2142 __string_type __str(__lhs);
2143 __str.append(__size_type(1), __rhs);
2144 return __str;
2147 // operator ==
2149 * @brief Test equivalence of two strings.
2150 * @param lhs First string.
2151 * @param rhs Second string.
2152 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2154 template<typename _CharT, typename _Traits, typename _Alloc>
2155 inline bool
2156 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2157 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2158 { return __lhs.compare(__rhs) == 0; }
2160 template<typename _CharT>
2161 inline
2162 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type
2163 operator==(const basic_string<_CharT>& __lhs,
2164 const basic_string<_CharT>& __rhs)
2165 { return (__lhs.size() == __rhs.size()
2166 && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
2167 __lhs.size())); }
2170 * @brief Test equivalence of C string and string.
2171 * @param lhs C string.
2172 * @param rhs String.
2173 * @return True if @a rhs.compare(@a lhs) == 0. False otherwise.
2175 template<typename _CharT, typename _Traits, typename _Alloc>
2176 inline bool
2177 operator==(const _CharT* __lhs,
2178 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2179 { return __rhs.compare(__lhs) == 0; }
2182 * @brief Test equivalence of string and C string.
2183 * @param lhs String.
2184 * @param rhs C string.
2185 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise.
2187 template<typename _CharT, typename _Traits, typename _Alloc>
2188 inline bool
2189 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2190 const _CharT* __rhs)
2191 { return __lhs.compare(__rhs) == 0; }
2193 // operator !=
2195 * @brief Test difference of two strings.
2196 * @param lhs First string.
2197 * @param rhs Second string.
2198 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2200 template<typename _CharT, typename _Traits, typename _Alloc>
2201 inline bool
2202 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2203 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2204 { return !(__lhs == __rhs); }
2207 * @brief Test difference of C string and string.
2208 * @param lhs C string.
2209 * @param rhs String.
2210 * @return True if @a rhs.compare(@a lhs) != 0. False otherwise.
2212 template<typename _CharT, typename _Traits, typename _Alloc>
2213 inline bool
2214 operator!=(const _CharT* __lhs,
2215 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2216 { return !(__lhs == __rhs); }
2219 * @brief Test difference of string and C string.
2220 * @param lhs String.
2221 * @param rhs C string.
2222 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise.
2224 template<typename _CharT, typename _Traits, typename _Alloc>
2225 inline bool
2226 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2227 const _CharT* __rhs)
2228 { return !(__lhs == __rhs); }
2230 // operator <
2232 * @brief Test if string precedes string.
2233 * @param lhs First string.
2234 * @param rhs Second string.
2235 * @return True if @a lhs precedes @a rhs. False otherwise.
2237 template<typename _CharT, typename _Traits, typename _Alloc>
2238 inline bool
2239 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2240 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2241 { return __lhs.compare(__rhs) < 0; }
2244 * @brief Test if string precedes C string.
2245 * @param lhs String.
2246 * @param rhs C string.
2247 * @return True if @a lhs precedes @a rhs. False otherwise.
2249 template<typename _CharT, typename _Traits, typename _Alloc>
2250 inline bool
2251 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2252 const _CharT* __rhs)
2253 { return __lhs.compare(__rhs) < 0; }
2256 * @brief Test if C string precedes string.
2257 * @param lhs C string.
2258 * @param rhs String.
2259 * @return True if @a lhs precedes @a rhs. False otherwise.
2261 template<typename _CharT, typename _Traits, typename _Alloc>
2262 inline bool
2263 operator<(const _CharT* __lhs,
2264 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2265 { return __rhs.compare(__lhs) > 0; }
2267 // operator >
2269 * @brief Test if string follows string.
2270 * @param lhs First string.
2271 * @param rhs Second string.
2272 * @return True if @a lhs follows @a rhs. False otherwise.
2274 template<typename _CharT, typename _Traits, typename _Alloc>
2275 inline bool
2276 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2277 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2278 { return __lhs.compare(__rhs) > 0; }
2281 * @brief Test if string follows C string.
2282 * @param lhs String.
2283 * @param rhs C string.
2284 * @return True if @a lhs follows @a rhs. False otherwise.
2286 template<typename _CharT, typename _Traits, typename _Alloc>
2287 inline bool
2288 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2289 const _CharT* __rhs)
2290 { return __lhs.compare(__rhs) > 0; }
2293 * @brief Test if C string follows string.
2294 * @param lhs C string.
2295 * @param rhs String.
2296 * @return True if @a lhs follows @a rhs. False otherwise.
2298 template<typename _CharT, typename _Traits, typename _Alloc>
2299 inline bool
2300 operator>(const _CharT* __lhs,
2301 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2302 { return __rhs.compare(__lhs) < 0; }
2304 // operator <=
2306 * @brief Test if string doesn't follow string.
2307 * @param lhs First string.
2308 * @param rhs Second string.
2309 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2311 template<typename _CharT, typename _Traits, typename _Alloc>
2312 inline bool
2313 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2314 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2315 { return __lhs.compare(__rhs) <= 0; }
2318 * @brief Test if string doesn't follow C string.
2319 * @param lhs String.
2320 * @param rhs C string.
2321 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2323 template<typename _CharT, typename _Traits, typename _Alloc>
2324 inline bool
2325 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2326 const _CharT* __rhs)
2327 { return __lhs.compare(__rhs) <= 0; }
2330 * @brief Test if C string doesn't follow string.
2331 * @param lhs C string.
2332 * @param rhs String.
2333 * @return True if @a lhs doesn't follow @a rhs. False otherwise.
2335 template<typename _CharT, typename _Traits, typename _Alloc>
2336 inline bool
2337 operator<=(const _CharT* __lhs,
2338 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2339 { return __rhs.compare(__lhs) >= 0; }
2341 // operator >=
2343 * @brief Test if string doesn't precede string.
2344 * @param lhs First string.
2345 * @param rhs Second string.
2346 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2348 template<typename _CharT, typename _Traits, typename _Alloc>
2349 inline bool
2350 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2351 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2352 { return __lhs.compare(__rhs) >= 0; }
2355 * @brief Test if string doesn't precede C string.
2356 * @param lhs String.
2357 * @param rhs C string.
2358 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2360 template<typename _CharT, typename _Traits, typename _Alloc>
2361 inline bool
2362 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2363 const _CharT* __rhs)
2364 { return __lhs.compare(__rhs) >= 0; }
2367 * @brief Test if C string doesn't precede string.
2368 * @param lhs C string.
2369 * @param rhs String.
2370 * @return True if @a lhs doesn't precede @a rhs. False otherwise.
2372 template<typename _CharT, typename _Traits, typename _Alloc>
2373 inline bool
2374 operator>=(const _CharT* __lhs,
2375 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2376 { return __rhs.compare(__lhs) <= 0; }
2379 * @brief Swap contents of two strings.
2380 * @param lhs First string.
2381 * @param rhs Second string.
2383 * Exchanges the contents of @a lhs and @a rhs in constant time.
2385 template<typename _CharT, typename _Traits, typename _Alloc>
2386 inline void
2387 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
2388 basic_string<_CharT, _Traits, _Alloc>& __rhs)
2389 { __lhs.swap(__rhs); }
2392 * @brief Read stream into a string.
2393 * @param is Input stream.
2394 * @param str Buffer to store into.
2395 * @return Reference to the input stream.
2397 * Stores characters from @a is into @a str until whitespace is found, the
2398 * end of the stream is encountered, or str.max_size() is reached. If
2399 * is.width() is non-zero, that is the limit on the number of characters
2400 * stored into @a str. Any previous contents of @a str are erased.
2402 template<typename _CharT, typename _Traits, typename _Alloc>
2403 basic_istream<_CharT, _Traits>&
2404 operator>>(basic_istream<_CharT, _Traits>& __is,
2405 basic_string<_CharT, _Traits, _Alloc>& __str);
2407 template<>
2408 basic_istream<char>&
2409 operator>>(basic_istream<char>& __is, basic_string<char>& __str);
2412 * @brief Write string to a stream.
2413 * @param os Output stream.
2414 * @param str String to write out.
2415 * @return Reference to the output stream.
2417 * Output characters of @a str into os following the same rules as for
2418 * writing a C string.
2420 template<typename _CharT, typename _Traits, typename _Alloc>
2421 inline basic_ostream<_CharT, _Traits>&
2422 operator<<(basic_ostream<_CharT, _Traits>& __os,
2423 const basic_string<_CharT, _Traits, _Alloc>& __str)
2425 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2426 // 586. string inserter not a formatted function
2427 return __ostream_insert(__os, __str.data(), __str.size());
2431 * @brief Read a line from stream into a string.
2432 * @param is Input stream.
2433 * @param str Buffer to store into.
2434 * @param delim Character marking end of line.
2435 * @return Reference to the input stream.
2437 * Stores characters from @a is into @a str until @a delim is found, the
2438 * end of the stream is encountered, or str.max_size() is reached. If
2439 * is.width() is non-zero, that is the limit on the number of characters
2440 * stored into @a str. Any previous contents of @a str are erased. If @a
2441 * delim was encountered, it is extracted but not stored into @a str.
2443 template<typename _CharT, typename _Traits, typename _Alloc>
2444 basic_istream<_CharT, _Traits>&
2445 getline(basic_istream<_CharT, _Traits>& __is,
2446 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
2449 * @brief Read a line from stream into a string.
2450 * @param is Input stream.
2451 * @param str Buffer to store into.
2452 * @return Reference to the input stream.
2454 * Stores characters from is into @a str until '\n' is found, the end of
2455 * the stream is encountered, or str.max_size() is reached. If is.width()
2456 * is non-zero, that is the limit on the number of characters stored into
2457 * @a str. Any previous contents of @a str are erased. If end of line was
2458 * encountered, it is extracted but not stored into @a str.
2460 template<typename _CharT, typename _Traits, typename _Alloc>
2461 inline basic_istream<_CharT, _Traits>&
2462 getline(basic_istream<_CharT, _Traits>& __is,
2463 basic_string<_CharT, _Traits, _Alloc>& __str)
2464 { return getline(__is, __str, __is.widen('\n')); }
2466 template<>
2467 basic_istream<char>&
2468 getline(basic_istream<char>& __in, basic_string<char>& __str,
2469 char __delim);
2471 #ifdef _GLIBCXX_USE_WCHAR_T
2472 template<>
2473 basic_istream<wchar_t>&
2474 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
2475 wchar_t __delim);
2476 #endif
2478 _GLIBCXX_END_NAMESPACE
2480 #endif /* _BASIC_STRING_H */