1// Components for manipulating sequences of characters -*- C++ -*-
2
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.
12
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.
17
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.
22
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.
31
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.
35 */
36
37//
38// ISO C++ 14882: 21 Strings library
39//
40
41#ifndef _BASIC_STRING_H
42#define _BASIC_STRING_H 1
43
44#pragma GCC system_header
45
46#include <ext/atomicity.h>
47#include <debug/debug.h>
48
49_GLIBCXX_BEGIN_NAMESPACE(std)
50
51  /**
52   *  @class basic_string basic_string.h <string>
53   *  @brief  Managing sequences of characters and character-like objects.
54   *
55   *  @ingroup Containers
56   *  @ingroup Sequences
57   *
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.
63   *
64   *  @doctodo
65   *
66   *
67   *  @if maint
68   *  Documentation?  What's that?
69   *  Nathan Myers <ncm@cantrip.org>.
70   *
71   *  A string looks like this:
72   *
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
80   *
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.
84   *
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.
92   *
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.)
98   *
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.
104   *
105   *  All but the last paragraph is considered pretty conventional
106   *  for a C++ string implementation.
107   *  @endif
108  */
109  // 21.3  Template class basic_string
110  template<typename _CharT, typename _Traits, typename _Alloc>
111    class basic_string
112    {
113      typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
114
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;
131
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.
146
147      struct _Rep_base
148      {
149	size_type		_M_length;
150	size_type		_M_capacity;
151	_Atomic_word		_M_refcount;
152      };
153
154      struct _Rep : _Rep_base
155      {
156	// Types:
157	typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc;
158
159	// (Public) Data members:
160
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;
174
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[];
178
179        static _Rep&
180        _S_empty_rep()
181        {
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);
187	}
188
189        bool
190	_M_is_leaked() const
191        { return this->_M_refcount < 0; }
192
193        bool
194	_M_is_shared() const
195        { return this->_M_refcount > 0; }
196
197        void
198	_M_set_leaked()
199        { this->_M_refcount = -1; }
200
201        void
202	_M_set_sharable()
203        { this->_M_refcount = 0; }
204
205	void
206	_M_set_length_and_sharable(size_type __n)
207	{
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.
213	}
214
215	_CharT*
216	_M_refdata() throw()
217	{ return reinterpret_cast<_CharT*>(this + 1); }
218
219	_CharT*
220	_M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
221	{
222	  return (!_M_is_leaked() && __alloc1 == __alloc2)
223	          ? _M_refcopy() : _M_clone(__alloc1);
224	}
225
226	// Create & Destroy
227	static _Rep*
228	_S_create(size_type, size_type, const _Alloc&);
229
230	void
231	_M_dispose(const _Alloc& __a)
232	{
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
240
241	void
242	_M_destroy(const _Alloc&) throw();
243
244	_CharT*
245	_M_refcopy() throw()
246	{
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
253
254	_CharT*
255	_M_clone(const _Alloc&, size_type __res = 0);
256      };
257
258      // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
259      struct _Alloc_hider : _Alloc
260      {
261	_Alloc_hider(_CharT* __dat, const _Alloc& __a)
262	: _Alloc(__a), _M_p(__dat) { }
263
264	_CharT* _M_p; // The actual data.
265      };
266
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);
273
274    private:
275      // Data Members (private):
276      mutable _Alloc_hider	_M_dataplus;
277
278      _CharT*
279      _M_data() const
280      { return  _M_dataplus._M_p; }
281
282      _CharT*
283      _M_data(_CharT* __p)
284      { return (_M_dataplus._M_p = __p); }
285
286      _Rep*
287      _M_rep() const
288      { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
289
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()); }
295
296      iterator
297      _M_iend() const
298      { return iterator(_M_data() + this->size()); }
299
300      void
301      _M_leak()    // for use in begin() & non-const op[]
302      {
303	if (!_M_rep()->_M_is_leaked())
304	  _M_leak_hard();
305      }
306
307      size_type
308      _M_check(size_type __pos, const char* __s) const
309      {
310	if (__pos > this->size())
311	  __throw_out_of_range(__N(__s));
312	return __pos;
313      }
314
315      void
316      _M_check_length(size_type __n1, size_type __n2, const char* __s) const
317      {
318	if (this->max_size() - (this->size() - __n1) < __n2)
319	  __throw_length_error(__N(__s));
320      }
321
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
325      {
326	const bool __testoff =  __off < this->size() - __pos;
327	return __testoff ? __off : this->size() - __pos;
328      }
329
330      // True if _Rep and source do not overlap.
331      bool
332      _M_disjunct(const _CharT* __s) const
333      {
334	return (less<const _CharT*>()(__s, _M_data())
335		|| less<const _CharT*>()(_M_data() + this->size(), __s));
336      }
337
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)
342      {
343	if (__n == 1)
344	  traits_type::assign(*__d, *__s);
345	else
346	  traits_type::copy(__d, __s, __n);
347      }
348
349      static void
350      _M_move(_CharT* __d, const _CharT* __s, size_type __n)
351      {
352	if (__n == 1)
353	  traits_type::assign(*__d, *__s);
354	else
355	  traits_type::move(__d, __s, __n);
356      }
357
358      static void
359      _M_assign(_CharT* __d, size_type __n, _CharT __c)
360      {
361	if (__n == 1)
362	  traits_type::assign(*__d, __c);
363	else
364	  traits_type::assign(__d, __n, __c);
365      }
366
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)
372        {
373	  for (; __k1 != __k2; ++__k1, ++__p)
374	    traits_type::assign(*__p, *__k1); // These types are off.
375	}
376
377      static void
378      _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2)
379      { _S_copy_chars(__p, __k1.base(), __k2.base()); }
380
381      static void
382      _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
383      { _S_copy_chars(__p, __k1.base(), __k2.base()); }
384
385      static void
386      _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
387      { _M_copy(__p, __k1, __k2 - __k1); }
388
389      static void
390      _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
391      { _M_copy(__p, __k1, __k2 - __k1); }
392
393      static int
394      _S_compare(size_type __x, size_type __y)
395      {
396         if (__x > __y)
397            return 1;
398         if (__x < __y)
399            return -1;
400         return 0;
401      }
402
403      void
404      _M_mutate(size_type __pos, size_type __len1, size_type __len2);
405
406      void
407      _M_leak_hard();
408
409      static _Rep&
410      _S_empty_rep()
411      { return _Rep::_S_empty_rep(); }
412
413    public:
414      // Construct/copy/destroy:
415      // NB: We overload ctors in some cases instead of using default
416      // arguments, per 17.4.4.4 para. 2 item 2.
417
418      /**
419       *  @brief  Default constructor creates an empty string.
420       */
421      inline
422      basic_string();
423
424      /**
425       *  @brief  Construct an empty string using allocator @a a.
426       */
427      explicit
428      basic_string(const _Alloc& __a);
429
430      // NB: per LWG issue 42, semantics different from IS:
431      /**
432       *  @brief  Construct string with copy of value of @a str.
433       *  @param  str  Source string.
434       */
435      basic_string(const basic_string& __str);
436      /**
437       *  @brief  Construct string as copy of a substring.
438       *  @param  str  Source string.
439       *  @param  pos  Index of first character to copy from.
440       *  @param  n  Number of characters to copy (default remainder).
441       */
442      basic_string(const basic_string& __str, size_type __pos,
443		   size_type __n = npos);
444      /**
445       *  @brief  Construct string as copy of a substring.
446       *  @param  str  Source string.
447       *  @param  pos  Index of first character to copy from.
448       *  @param  n  Number of characters to copy.
449       *  @param  a  Allocator to use.
450       */
451      basic_string(const basic_string& __str, size_type __pos,
452		   size_type __n, const _Alloc& __a);
453
454      /**
455       *  @brief  Construct string initialized by a character array.
456       *  @param  s  Source character array.
457       *  @param  n  Number of characters to copy.
458       *  @param  a  Allocator to use (default is default allocator).
459       *
460       *  NB: @a s must have at least @a n characters, '\0' has no special
461       *  meaning.
462       */
463      basic_string(const _CharT* __s, size_type __n,
464		   const _Alloc& __a = _Alloc());
465      /**
466       *  @brief  Construct string as copy of a C string.
467       *  @param  s  Source C string.
468       *  @param  a  Allocator to use (default is default allocator).
469       */
470      basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
471      /**
472       *  @brief  Construct string as multiple characters.
473       *  @param  n  Number of characters.
474       *  @param  c  Character to use.
475       *  @param  a  Allocator to use (default is default allocator).
476       */
477      basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
478
479      /**
480       *  @brief  Construct string as copy of a range.
481       *  @param  beg  Start of range.
482       *  @param  end  End of range.
483       *  @param  a  Allocator to use (default is default allocator).
484       */
485      template<class _InputIterator>
486        basic_string(_InputIterator __beg, _InputIterator __end,
487		     const _Alloc& __a = _Alloc());
488
489      /**
490       *  @brief  Destroy the string instance.
491       */
492      ~basic_string()
493      { _M_rep()->_M_dispose(this->get_allocator()); }
494
495      /**
496       *  @brief  Assign the value of @a str to this string.
497       *  @param  str  Source string.
498       */
499      basic_string&
500      operator=(const basic_string& __str)
501      { return this->assign(__str); }
502
503      /**
504       *  @brief  Copy contents of @a s into this string.
505       *  @param  s  Source null-terminated string.
506       */
507      basic_string&
508      operator=(const _CharT* __s)
509      { return this->assign(__s); }
510
511      /**
512       *  @brief  Set value to string of length 1.
513       *  @param  c  Source character.
514       *
515       *  Assigning to a character makes this string length 1 and
516       *  (*this)[0] == @a c.
517       */
518      basic_string&
519      operator=(_CharT __c)
520      {
521	this->assign(1, __c);
522	return *this;
523      }
524
525      // Iterators:
526      /**
527       *  Returns a read/write iterator that points to the first character in
528       *  the %string.  Unshares the string.
529       */
530      iterator
531      begin()
532      {
533	_M_leak();
534	return iterator(_M_data());
535      }
536
537      /**
538       *  Returns a read-only (constant) iterator that points to the first
539       *  character in the %string.
540       */
541      const_iterator
542      begin() const
543      { return const_iterator(_M_data()); }
544
545      /**
546       *  Returns a read/write iterator that points one past the last
547       *  character in the %string.  Unshares the string.
548       */
549      iterator
550      end()
551      {
552	_M_leak();
553	return iterator(_M_data() + this->size());
554      }
555
556      /**
557       *  Returns a read-only (constant) iterator that points one past the
558       *  last character in the %string.
559       */
560      const_iterator
561      end() const
562      { return const_iterator(_M_data() + this->size()); }
563
564      /**
565       *  Returns a read/write reverse iterator that points to the last
566       *  character in the %string.  Iteration is done in reverse element
567       *  order.  Unshares the string.
568       */
569      reverse_iterator
570      rbegin()
571      { return reverse_iterator(this->end()); }
572
573      /**
574       *  Returns a read-only (constant) reverse iterator that points
575       *  to the last character in the %string.  Iteration is done in
576       *  reverse element order.
577       */
578      const_reverse_iterator
579      rbegin() const
580      { return const_reverse_iterator(this->end()); }
581
582      /**
583       *  Returns a read/write reverse iterator that points to one before the
584       *  first character in the %string.  Iteration is done in reverse
585       *  element order.  Unshares the string.
586       */
587      reverse_iterator
588      rend()
589      { return reverse_iterator(this->begin()); }
590
591      /**
592       *  Returns a read-only (constant) reverse iterator that points
593       *  to one before the first character in the %string.  Iteration
594       *  is done in reverse element order.
595       */
596      const_reverse_iterator
597      rend() const
598      { return const_reverse_iterator(this->begin()); }
599
600    public:
601      // Capacity:
602      ///  Returns the number of characters in the string, not including any
603      ///  null-termination.
604      size_type
605      size() const
606      { return _M_rep()->_M_length; }
607
608      ///  Returns the number of characters in the string, not including any
609      ///  null-termination.
610      size_type
611      length() const
612      { return _M_rep()->_M_length; }
613
614      /// Returns the size() of the largest possible %string.
615      size_type
616      max_size() const
617      { return _Rep::_S_max_size; }
618
619      /**
620       *  @brief  Resizes the %string to the specified number of characters.
621       *  @param  n  Number of characters the %string should contain.
622       *  @param  c  Character to fill any new elements.
623       *
624       *  This function will %resize the %string to the specified
625       *  number of characters.  If the number is smaller than the
626       *  %string's current size the %string is truncated, otherwise
627       *  the %string is extended and new elements are set to @a c.
628       */
629      void
630      resize(size_type __n, _CharT __c);
631
632      /**
633       *  @brief  Resizes the %string to the specified number of characters.
634       *  @param  n  Number of characters the %string should contain.
635       *
636       *  This function will resize the %string to the specified length.  If
637       *  the new size is smaller than the %string's current size the %string
638       *  is truncated, otherwise the %string is extended and new characters
639       *  are default-constructed.  For basic types such as char, this means
640       *  setting them to 0.
641       */
642      void
643      resize(size_type __n)
644      { this->resize(__n, _CharT()); }
645
646      /**
647       *  Returns the total number of characters that the %string can hold
648       *  before needing to allocate more memory.
649       */
650      size_type
651      capacity() const
652      { return _M_rep()->_M_capacity; }
653
654      /**
655       *  @brief  Attempt to preallocate enough memory for specified number of
656       *          characters.
657       *  @param  res_arg  Number of characters required.
658       *  @throw  std::length_error  If @a res_arg exceeds @c max_size().
659       *
660       *  This function attempts to reserve enough memory for the
661       *  %string to hold the specified number of characters.  If the
662       *  number requested is more than max_size(), length_error is
663       *  thrown.
664       *
665       *  The advantage of this function is that if optimal code is a
666       *  necessity and the user can determine the string length that will be
667       *  required, the user can reserve the memory in %advance, and thus
668       *  prevent a possible reallocation of memory and copying of %string
669       *  data.
670       */
671      void
672      reserve(size_type __res_arg = 0);
673
674      /**
675       *  Erases the string, making it empty.
676       */
677      void
678      clear()
679      { _M_mutate(0, this->size(), 0); }
680
681      /**
682       *  Returns true if the %string is empty.  Equivalent to *this == "".
683       */
684      bool
685      empty() const
686      { return this->size() == 0; }
687
688      // Element access:
689      /**
690       *  @brief  Subscript access to the data contained in the %string.
691       *  @param  pos  The index of the character to access.
692       *  @return  Read-only (constant) reference to the character.
693       *
694       *  This operator allows for easy, array-style, data access.
695       *  Note that data access with this operator is unchecked and
696       *  out_of_range lookups are not defined. (For checked lookups
697       *  see at().)
698       */
699      const_reference
700      operator[] (size_type __pos) const
701      {
702	_GLIBCXX_DEBUG_ASSERT(__pos <= size());
703	return _M_data()[__pos];
704      }
705
706      /**
707       *  @brief  Subscript access to the data contained in the %string.
708       *  @param  pos  The index of the character to access.
709       *  @return  Read/write reference to the character.
710       *
711       *  This operator allows for easy, array-style, data access.
712       *  Note that data access with this operator is unchecked and
713       *  out_of_range lookups are not defined. (For checked lookups
714       *  see at().)  Unshares the string.
715       */
716      reference
717      operator[](size_type __pos)
718      {
719        // allow pos == size() as v3 extension:
720	_GLIBCXX_DEBUG_ASSERT(__pos <= size());
721        // but be strict in pedantic mode:
722	_GLIBCXX_DEBUG_PEDASSERT(__pos < size());
723	_M_leak();
724	return _M_data()[__pos];
725      }
726
727      /**
728       *  @brief  Provides access to the data contained in the %string.
729       *  @param n The index of the character to access.
730       *  @return  Read-only (const) reference to the character.
731       *  @throw  std::out_of_range  If @a n is an invalid index.
732       *
733       *  This function provides for safer data access.  The parameter is
734       *  first checked that it is in the range of the string.  The function
735       *  throws out_of_range if the check fails.
736       */
737      const_reference
738      at(size_type __n) const
739      {
740	if (__n >= this->size())
741	  __throw_out_of_range(__N("basic_string::at"));
742	return _M_data()[__n];
743      }
744
745      /**
746       *  @brief  Provides access to the data contained in the %string.
747       *  @param n The index of the character to access.
748       *  @return  Read/write reference to the character.
749       *  @throw  std::out_of_range  If @a n is an invalid index.
750       *
751       *  This function provides for safer data access.  The parameter is
752       *  first checked that it is in the range of the string.  The function
753       *  throws out_of_range if the check fails.  Success results in
754       *  unsharing the string.
755       */
756      reference
757      at(size_type __n)
758      {
759	if (__n >= size())
760	  __throw_out_of_range(__N("basic_string::at"));
761	_M_leak();
762	return _M_data()[__n];
763      }
764
765      // Modifiers:
766      /**
767       *  @brief  Append a string to this string.
768       *  @param str  The string to append.
769       *  @return  Reference to this string.
770       */
771      basic_string&
772      operator+=(const basic_string& __str)
773      { return this->append(__str); }
774
775      /**
776       *  @brief  Append a C string.
777       *  @param s  The C string to append.
778       *  @return  Reference to this string.
779       */
780      basic_string&
781      operator+=(const _CharT* __s)
782      { return this->append(__s); }
783
784      /**
785       *  @brief  Append a character.
786       *  @param c  The character to append.
787       *  @return  Reference to this string.
788       */
789      basic_string&
790      operator+=(_CharT __c)
791      {
792	this->push_back(__c);
793	return *this;
794      }
795
796      /**
797       *  @brief  Append a string to this string.
798       *  @param str  The string to append.
799       *  @return  Reference to this string.
800       */
801      basic_string&
802      append(const basic_string& __str);
803
804      /**
805       *  @brief  Append a substring.
806       *  @param str  The string to append.
807       *  @param pos  Index of the first character of str to append.
808       *  @param n  The number of characters to append.
809       *  @return  Reference to this string.
810       *  @throw  std::out_of_range if @a pos is not a valid index.
811       *
812       *  This function appends @a n characters from @a str starting at @a pos
813       *  to this string.  If @a n is is larger than the number of available
814       *  characters in @a str, the remainder of @a str is appended.
815       */
816      basic_string&
817      append(const basic_string& __str, size_type __pos, size_type __n);
818
819      /**
820       *  @brief  Append a C substring.
821       *  @param s  The C string to append.
822       *  @param n  The number of characters to append.
823       *  @return  Reference to this string.
824       */
825      basic_string&
826      append(const _CharT* __s, size_type __n);
827
828      /**
829       *  @brief  Append a C string.
830       *  @param s  The C string to append.
831       *  @return  Reference to this string.
832       */
833      basic_string&
834      append(const _CharT* __s)
835      {
836	__glibcxx_requires_string(__s);
837	return this->append(__s, traits_type::length(__s));
838      }
839
840      /**
841       *  @brief  Append multiple characters.
842       *  @param n  The number of characters to append.
843       *  @param c  The character to use.
844       *  @return  Reference to this string.
845       *
846       *  Appends n copies of c to this string.
847       */
848      basic_string&
849      append(size_type __n, _CharT __c);
850
851      /**
852       *  @brief  Append a range of characters.
853       *  @param first  Iterator referencing the first character to append.
854       *  @param last  Iterator marking the end of the range.
855       *  @return  Reference to this string.
856       *
857       *  Appends characters in the range [first,last) to this string.
858       */
859      template<class _InputIterator>
860        basic_string&
861        append(_InputIterator __first, _InputIterator __last)
862        { return this->replace(_M_iend(), _M_iend(), __first, __last); }
863
864      /**
865       *  @brief  Append a single character.
866       *  @param c  Character to append.
867       */
868      void
869      push_back(_CharT __c)
870      {
871	const size_type __len = 1 + this->size();
872	if (__len > this->capacity() || _M_rep()->_M_is_shared())
873	  this->reserve(__len);
874	traits_type::assign(_M_data()[this->size()], __c);
875	_M_rep()->_M_set_length_and_sharable(__len);
876      }
877
878      /**
879       *  @brief  Set value to contents of another string.
880       *  @param  str  Source string to use.
881       *  @return  Reference to this string.
882       */
883      basic_string&
884      assign(const basic_string& __str);
885
886      /**
887       *  @brief  Set value to a substring of a string.
888       *  @param str  The string to use.
889       *  @param pos  Index of the first character of str.
890       *  @param n  Number of characters to use.
891       *  @return  Reference to this string.
892       *  @throw  std::out_of_range if @a pos is not a valid index.
893       *
894       *  This function sets this string to the substring of @a str consisting
895       *  of @a n characters at @a pos.  If @a n is is larger than the number
896       *  of available characters in @a str, the remainder of @a str is used.
897       */
898      basic_string&
899      assign(const basic_string& __str, size_type __pos, size_type __n)
900      { return this->assign(__str._M_data()
901			    + __str._M_check(__pos, "basic_string::assign"),
902			    __str._M_limit(__pos, __n)); }
903
904      /**
905       *  @brief  Set value to a C substring.
906       *  @param s  The C string to use.
907       *  @param n  Number of characters to use.
908       *  @return  Reference to this string.
909       *
910       *  This function sets the value of this string to the first @a n
911       *  characters of @a s.  If @a n is is larger than the number of
912       *  available characters in @a s, the remainder of @a s is used.
913       */
914      basic_string&
915      assign(const _CharT* __s, size_type __n);
916
917      /**
918       *  @brief  Set value to contents of a C string.
919       *  @param s  The C string to use.
920       *  @return  Reference to this string.
921       *
922       *  This function sets the value of this string to the value of @a s.
923       *  The data is copied, so there is no dependence on @a s once the
924       *  function returns.
925       */
926      basic_string&
927      assign(const _CharT* __s)
928      {
929	__glibcxx_requires_string(__s);
930	return this->assign(__s, traits_type::length(__s));
931      }
932
933      /**
934       *  @brief  Set value to multiple characters.
935       *  @param n  Length of the resulting string.
936       *  @param c  The character to use.
937       *  @return  Reference to this string.
938       *
939       *  This function sets the value of this string to @a n copies of
940       *  character @a c.
941       */
942      basic_string&
943      assign(size_type __n, _CharT __c)
944      { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
945
946      /**
947       *  @brief  Set value to a range of characters.
948       *  @param first  Iterator referencing the first character to append.
949       *  @param last  Iterator marking the end of the range.
950       *  @return  Reference to this string.
951       *
952       *  Sets value of string to characters in the range [first,last).
953      */
954      template<class _InputIterator>
955        basic_string&
956        assign(_InputIterator __first, _InputIterator __last)
957        { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
958
959      /**
960       *  @brief  Insert multiple characters.
961       *  @param p  Iterator referencing location in string to insert at.
962       *  @param n  Number of characters to insert
963       *  @param c  The character to insert.
964       *  @throw  std::length_error  If new length exceeds @c max_size().
965       *
966       *  Inserts @a n copies of character @a c starting at the position
967       *  referenced by iterator @a p.  If adding characters causes the length
968       *  to exceed max_size(), length_error is thrown.  The value of the
969       *  string doesn't change if an error is thrown.
970      */
971      void
972      insert(iterator __p, size_type __n, _CharT __c)
973      {	this->replace(__p, __p, __n, __c);  }
974
975      /**
976       *  @brief  Insert a range of characters.
977       *  @param p  Iterator referencing location in string to insert at.
978       *  @param beg  Start of range.
979       *  @param end  End of range.
980       *  @throw  std::length_error  If new length exceeds @c max_size().
981       *
982       *  Inserts characters in range [beg,end).  If adding characters causes
983       *  the length to exceed max_size(), length_error is thrown.  The value
984       *  of the string doesn't change if an error is thrown.
985      */
986      template<class _InputIterator>
987        void
988        insert(iterator __p, _InputIterator __beg, _InputIterator __end)
989        { this->replace(__p, __p, __beg, __end); }
990
991      /**
992       *  @brief  Insert value of a string.
993       *  @param pos1  Iterator referencing location in string to insert at.
994       *  @param str  The string to insert.
995       *  @return  Reference to this string.
996       *  @throw  std::length_error  If new length exceeds @c max_size().
997       *
998       *  Inserts value of @a str starting at @a pos1.  If adding characters
999       *  causes the length to exceed max_size(), length_error is thrown.  The
1000       *  value of the string doesn't change if an error is thrown.
1001      */
1002      basic_string&
1003      insert(size_type __pos1, const basic_string& __str)
1004      { return this->insert(__pos1, __str, size_type(0), __str.size()); }
1005
1006      /**
1007       *  @brief  Insert a substring.
1008       *  @param pos1  Iterator referencing location in string to insert at.
1009       *  @param str  The string to insert.
1010       *  @param pos2  Start of characters in str to insert.
1011       *  @param n  Number of characters to insert.
1012       *  @return  Reference to this string.
1013       *  @throw  std::length_error  If new length exceeds @c max_size().
1014       *  @throw  std::out_of_range  If @a pos1 > size() or
1015       *  @a pos2 > @a str.size().
1016       *
1017       *  Starting at @a pos1, insert @a n character of @a str beginning with
1018       *  @a pos2.  If adding characters causes the length to exceed
1019       *  max_size(), length_error is thrown.  If @a pos1 is beyond the end of
1020       *  this string or @a pos2 is beyond the end of @a str, out_of_range is
1021       *  thrown.  The value of the string doesn't change if an error is
1022       *  thrown.
1023      */
1024      basic_string&
1025      insert(size_type __pos1, const basic_string& __str,
1026	     size_type __pos2, size_type __n)
1027      { return this->insert(__pos1, __str._M_data()
1028			    + __str._M_check(__pos2, "basic_string::insert"),
1029			    __str._M_limit(__pos2, __n)); }
1030
1031      /**
1032       *  @brief  Insert a C substring.
1033       *  @param pos  Iterator referencing location in string to insert at.
1034       *  @param s  The C string to insert.
1035       *  @param n  The number of characters to insert.
1036       *  @return  Reference to this string.
1037       *  @throw  std::length_error  If new length exceeds @c max_size().
1038       *  @throw  std::out_of_range  If @a pos is beyond the end of this
1039       *  string.
1040       *
1041       *  Inserts the first @a n characters of @a s starting at @a pos.  If
1042       *  adding characters causes the length to exceed max_size(),
1043       *  length_error is thrown.  If @a pos is beyond end(), out_of_range is
1044       *  thrown.  The value of the string doesn't change if an error is
1045       *  thrown.
1046      */
1047      basic_string&
1048      insert(size_type __pos, const _CharT* __s, size_type __n);
1049
1050      /**
1051       *  @brief  Insert a C string.
1052       *  @param pos  Iterator referencing location in string to insert at.
1053       *  @param s  The C string to insert.
1054       *  @return  Reference to this string.
1055       *  @throw  std::length_error  If new length exceeds @c max_size().
1056       *  @throw  std::out_of_range  If @a pos is beyond the end of this
1057       *  string.
1058       *
1059       *  Inserts the first @a n characters of @a s starting at @a pos.  If
1060       *  adding characters causes the length to exceed max_size(),
1061       *  length_error is thrown.  If @a pos is beyond end(), out_of_range is
1062       *  thrown.  The value of the string doesn't change if an error is
1063       *  thrown.
1064      */
1065      basic_string&
1066      insert(size_type __pos, const _CharT* __s)
1067      {
1068	__glibcxx_requires_string(__s);
1069	return this->insert(__pos, __s, traits_type::length(__s));
1070      }
1071
1072      /**
1073       *  @brief  Insert multiple characters.
1074       *  @param pos  Index in string to insert at.
1075       *  @param n  Number of characters to insert
1076       *  @param c  The character to insert.
1077       *  @return  Reference to this string.
1078       *  @throw  std::length_error  If new length exceeds @c max_size().
1079       *  @throw  std::out_of_range  If @a pos is beyond the end of this
1080       *  string.
1081       *
1082       *  Inserts @a n copies of character @a c starting at index @a pos.  If
1083       *  adding characters causes the length to exceed max_size(),
1084       *  length_error is thrown.  If @a pos > length(), out_of_range is
1085       *  thrown.  The value of the string doesn't change if an error is
1086       *  thrown.
1087      */
1088      basic_string&
1089      insert(size_type __pos, size_type __n, _CharT __c)
1090      { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
1091			      size_type(0), __n, __c); }
1092
1093      /**
1094       *  @brief  Insert one character.
1095       *  @param p  Iterator referencing position in string to insert at.
1096       *  @param c  The character to insert.
1097       *  @return  Iterator referencing newly inserted char.
1098       *  @throw  std::length_error  If new length exceeds @c max_size().
1099       *
1100       *  Inserts character @a c at position referenced by @a p.  If adding
1101       *  character causes the length to exceed max_size(), length_error is
1102       *  thrown.  If @a p is beyond end of string, out_of_range is thrown.
1103       *  The value of the string doesn't change if an error is thrown.
1104      */
1105      iterator
1106      insert(iterator __p, _CharT __c)
1107      {
1108	_GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1109	const size_type __pos = __p - _M_ibegin();
1110	_M_replace_aux(__pos, size_type(0), size_type(1), __c);
1111	_M_rep()->_M_set_leaked();
1112	return iterator(_M_data() + __pos);
1113      }
1114
1115      /**
1116       *  @brief  Remove characters.
1117       *  @param pos  Index of first character to remove (default 0).
1118       *  @param n  Number of characters to remove (default remainder).
1119       *  @return  Reference to this string.
1120       *  @throw  std::out_of_range  If @a pos is beyond the end of this
1121       *  string.
1122       *
1123       *  Removes @a n characters from this string starting at @a pos.  The
1124       *  length of the string is reduced by @a n.  If there are < @a n
1125       *  characters to remove, the remainder of the string is truncated.  If
1126       *  @a p is beyond end of string, out_of_range is thrown.  The value of
1127       *  the string doesn't change if an error is thrown.
1128      */
1129      basic_string&
1130      erase(size_type __pos = 0, size_type __n = npos)
1131      {
1132	_M_mutate(_M_check(__pos, "basic_string::erase"),
1133		  _M_limit(__pos, __n), size_type(0));
1134	return *this;
1135      }
1136
1137      /**
1138       *  @brief  Remove one character.
1139       *  @param position  Iterator referencing the character to remove.
1140       *  @return  iterator referencing same location after removal.
1141       *
1142       *  Removes the character at @a position from this string. The value
1143       *  of the string doesn't change if an error is thrown.
1144      */
1145      iterator
1146      erase(iterator __position)
1147      {
1148	_GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
1149				 && __position < _M_iend());
1150	const size_type __pos = __position - _M_ibegin();
1151	_M_mutate(__pos, size_type(1), size_type(0));
1152	_M_rep()->_M_set_leaked();
1153	return iterator(_M_data() + __pos);
1154      }
1155
1156      /**
1157       *  @brief  Remove a range of characters.
1158       *  @param first  Iterator referencing the first character to remove.
1159       *  @param last  Iterator referencing the end of the range.
1160       *  @return  Iterator referencing location of first after removal.
1161       *
1162       *  Removes the characters in the range [first,last) from this string.
1163       *  The value of the string doesn't change if an error is thrown.
1164      */
1165      iterator
1166      erase(iterator __first, iterator __last)
1167      {
1168	_GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
1169				 && __last <= _M_iend());
1170        const size_type __pos = __first - _M_ibegin();
1171	_M_mutate(__pos, __last - __first, size_type(0));
1172	_M_rep()->_M_set_leaked();
1173	return iterator(_M_data() + __pos);
1174      }
1175
1176      /**
1177       *  @brief  Replace characters with value from another string.
1178       *  @param pos  Index of first character to replace.
1179       *  @param n  Number of characters to be replaced.
1180       *  @param str  String to insert.
1181       *  @return  Reference to this string.
1182       *  @throw  std::out_of_range  If @a pos is beyond the end of this
1183       *  string.
1184       *  @throw  std::length_error  If new length exceeds @c max_size().
1185       *
1186       *  Removes the characters in the range [pos,pos+n) from this string.
1187       *  In place, the value of @a str is inserted.  If @a pos is beyond end
1188       *  of string, out_of_range is thrown.  If the length of the result
1189       *  exceeds max_size(), length_error is thrown.  The value of the string
1190       *  doesn't change if an error is thrown.
1191      */
1192      basic_string&
1193      replace(size_type __pos, size_type __n, const basic_string& __str)
1194      { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1195
1196      /**
1197       *  @brief  Replace characters with value from another string.
1198       *  @param pos1  Index of first character to replace.
1199       *  @param n1  Number of characters to be replaced.
1200       *  @param str  String to insert.
1201       *  @param pos2  Index of first character of str to use.
1202       *  @param n2  Number of characters from str to use.
1203       *  @return  Reference to this string.
1204       *  @throw  std::out_of_range  If @a pos1 > size() or @a pos2 >
1205       *  str.size().
1206       *  @throw  std::length_error  If new length exceeds @c max_size().
1207       *
1208       *  Removes the characters in the range [pos1,pos1 + n) from this
1209       *  string.  In place, the value of @a str is inserted.  If @a pos is
1210       *  beyond end of string, out_of_range is thrown.  If the length of the
1211       *  result exceeds max_size(), length_error is thrown.  The value of the
1212       *  string doesn't change if an error is thrown.
1213      */
1214      basic_string&
1215      replace(size_type __pos1, size_type __n1, const basic_string& __str,
1216	      size_type __pos2, size_type __n2)
1217      { return this->replace(__pos1, __n1, __str._M_data()
1218			     + __str._M_check(__pos2, "basic_string::replace"),
1219			     __str._M_limit(__pos2, __n2)); }
1220
1221      /**
1222       *  @brief  Replace characters with value of a C substring.
1223       *  @param pos  Index of first character to replace.
1224       *  @param n1  Number of characters to be replaced.
1225       *  @param s  C string to insert.
1226       *  @param n2  Number of characters from @a s to use.
1227       *  @return  Reference to this string.
1228       *  @throw  std::out_of_range  If @a pos1 > size().
1229       *  @throw  std::length_error  If new length exceeds @c max_size().
1230       *
1231       *  Removes the characters in the range [pos,pos + n1) from this string.
1232       *  In place, the first @a n2 characters of @a s are inserted, or all
1233       *  of @a s if @a n2 is too large.  If @a pos is beyond end of string,
1234       *  out_of_range is thrown.  If the length of result exceeds max_size(),
1235       *  length_error is thrown.  The value of the string doesn't change if
1236       *  an error is thrown.
1237      */
1238      basic_string&
1239      replace(size_type __pos, size_type __n1, const _CharT* __s,
1240	      size_type __n2);
1241
1242      /**
1243       *  @brief  Replace characters with value of a C string.
1244       *  @param pos  Index of first character to replace.
1245       *  @param n1  Number of characters to be replaced.
1246       *  @param s  C string to insert.
1247       *  @return  Reference to this string.
1248       *  @throw  std::out_of_range  If @a pos > size().
1249       *  @throw  std::length_error  If new length exceeds @c max_size().
1250       *
1251       *  Removes the characters in the range [pos,pos + n1) from this string.
1252       *  In place, the first @a n characters of @a s are inserted.  If @a
1253       *  pos is beyond end of string, out_of_range is thrown.  If the length
1254       *  of result exceeds max_size(), length_error is thrown.  The value of
1255       *  the string doesn't change if an error is thrown.
1256      */
1257      basic_string&
1258      replace(size_type __pos, size_type __n1, const _CharT* __s)
1259      {
1260	__glibcxx_requires_string(__s);
1261	return this->replace(__pos, __n1, __s, traits_type::length(__s));
1262      }
1263
1264      /**
1265       *  @brief  Replace characters with multiple characters.
1266       *  @param pos  Index of first character to replace.
1267       *  @param n1  Number of characters to be replaced.
1268       *  @param n2  Number of characters to insert.
1269       *  @param c  Character to insert.
1270       *  @return  Reference to this string.
1271       *  @throw  std::out_of_range  If @a pos > size().
1272       *  @throw  std::length_error  If new length exceeds @c max_size().
1273       *
1274       *  Removes the characters in the range [pos,pos + n1) from this string.
1275       *  In place, @a n2 copies of @a c are inserted.  If @a pos is beyond
1276       *  end of string, out_of_range is thrown.  If the length of result
1277       *  exceeds max_size(), length_error is thrown.  The value of the string
1278       *  doesn't change if an error is thrown.
1279      */
1280      basic_string&
1281      replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1282      { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
1283			      _M_limit(__pos, __n1), __n2, __c); }
1284
1285      /**
1286       *  @brief  Replace range of characters with string.
1287       *  @param i1  Iterator referencing start of range to replace.
1288       *  @param i2  Iterator referencing end of range to replace.
1289       *  @param str  String value to insert.
1290       *  @return  Reference to this string.
1291       *  @throw  std::length_error  If new length exceeds @c max_size().
1292       *
1293       *  Removes the characters in the range [i1,i2).  In place, the value of
1294       *  @a str is inserted.  If the length of result exceeds max_size(),
1295       *  length_error is thrown.  The value of the string doesn't change if
1296       *  an error is thrown.
1297      */
1298      basic_string&
1299      replace(iterator __i1, iterator __i2, const basic_string& __str)
1300      { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
1301
1302      /**
1303       *  @brief  Replace range of characters with C substring.
1304       *  @param i1  Iterator referencing start of range to replace.
1305       *  @param i2  Iterator referencing end of range to replace.
1306       *  @param s  C string value to insert.
1307       *  @param n  Number of characters from s to insert.
1308       *  @return  Reference to this string.
1309       *  @throw  std::length_error  If new length exceeds @c max_size().
1310       *
1311       *  Removes the characters in the range [i1,i2).  In place, the first @a
1312       *  n characters of @a s are inserted.  If the length of result exceeds
1313       *  max_size(), length_error is thrown.  The value of the string doesn't
1314       *  change if an error is thrown.
1315      */
1316      basic_string&
1317      replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
1318      {
1319	_GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1320				 && __i2 <= _M_iend());
1321	return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
1322      }
1323
1324      /**
1325       *  @brief  Replace range of characters with C string.
1326       *  @param i1  Iterator referencing start of range to replace.
1327       *  @param i2  Iterator referencing end of range to replace.
1328       *  @param s  C string value to insert.
1329       *  @return  Reference to this string.
1330       *  @throw  std::length_error  If new length exceeds @c max_size().
1331       *
1332       *  Removes the characters in the range [i1,i2).  In place, the
1333       *  characters of @a s are inserted.  If the length of result exceeds
1334       *  max_size(), length_error is thrown.  The value of the string doesn't
1335       *  change if an error is thrown.
1336      */
1337      basic_string&
1338      replace(iterator __i1, iterator __i2, const _CharT* __s)
1339      {
1340	__glibcxx_requires_string(__s);
1341	return this->replace(__i1, __i2, __s, traits_type::length(__s));
1342      }
1343
1344      /**
1345       *  @brief  Replace range of characters with multiple characters
1346       *  @param i1  Iterator referencing start of range to replace.
1347       *  @param i2  Iterator referencing end of range to replace.
1348       *  @param n  Number of characters to insert.
1349       *  @param c  Character to insert.
1350       *  @return  Reference to this string.
1351       *  @throw  std::length_error  If new length exceeds @c max_size().
1352       *
1353       *  Removes the characters in the range [i1,i2).  In place, @a n copies
1354       *  of @a c are inserted.  If the length of result exceeds max_size(),
1355       *  length_error is thrown.  The value of the string doesn't change if
1356       *  an error is thrown.
1357      */
1358      basic_string&
1359      replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
1360      {
1361	_GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1362				 && __i2 <= _M_iend());
1363	return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
1364      }
1365
1366      /**
1367       *  @brief  Replace range of characters with range.
1368       *  @param i1  Iterator referencing start of range to replace.
1369       *  @param i2  Iterator referencing end of range to replace.
1370       *  @param k1  Iterator referencing start of range to insert.
1371       *  @param k2  Iterator referencing end of range to insert.
1372       *  @return  Reference to this string.
1373       *  @throw  std::length_error  If new length exceeds @c max_size().
1374       *
1375       *  Removes the characters in the range [i1,i2).  In place, characters
1376       *  in the range [k1,k2) are inserted.  If the length of result exceeds
1377       *  max_size(), length_error is thrown.  The value of the string doesn't
1378       *  change if an error is thrown.
1379      */
1380      template<class _InputIterator>
1381        basic_string&
1382        replace(iterator __i1, iterator __i2,
1383		_InputIterator __k1, _InputIterator __k2)
1384        {
1385	  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1386				   && __i2 <= _M_iend());
1387	  __glibcxx_requires_valid_range(__k1, __k2);
1388	  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1389	  return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
1390	}
1391
1392      // Specializations for the common case of pointer and iterator:
1393      // useful to avoid the overhead of temporary buffering in _M_replace.
1394      basic_string&
1395      replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
1396      {
1397	_GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1398				 && __i2 <= _M_iend());
1399	__glibcxx_requires_valid_range(__k1, __k2);
1400	return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1401			     __k1, __k2 - __k1);
1402      }
1403
1404      basic_string&
1405      replace(iterator __i1, iterator __i2,
1406	      const _CharT* __k1, const _CharT* __k2)
1407      {
1408	_GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1409				 && __i2 <= _M_iend());
1410	__glibcxx_requires_valid_range(__k1, __k2);
1411	return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1412			     __k1, __k2 - __k1);
1413      }
1414
1415      basic_string&
1416      replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
1417      {
1418	_GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1419				 && __i2 <= _M_iend());
1420	__glibcxx_requires_valid_range(__k1, __k2);
1421	return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1422			     __k1.base(), __k2 - __k1);
1423      }
1424
1425      basic_string&
1426      replace(iterator __i1, iterator __i2,
1427	      const_iterator __k1, const_iterator __k2)
1428      {
1429	_GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1430				 && __i2 <= _M_iend());
1431	__glibcxx_requires_valid_range(__k1, __k2);
1432	return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1433			     __k1.base(), __k2 - __k1);
1434      }
1435
1436    private:
1437      template<class _Integer>
1438	basic_string&
1439	_M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
1440			    _Integer __val, __true_type)
1441        { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
1442
1443      template<class _InputIterator>
1444	basic_string&
1445	_M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
1446			    _InputIterator __k2, __false_type);
1447
1448      basic_string&
1449      _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
1450		     _CharT __c);
1451
1452      basic_string&
1453      _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
1454		      size_type __n2);
1455
1456      // _S_construct_aux is used to implement the 21.3.1 para 15 which
1457      // requires special behaviour if _InIter is an integral type
1458      template<class _InIterator>
1459        static _CharT*
1460        _S_construct_aux(_InIterator __beg, _InIterator __end,
1461			 const _Alloc& __a, __false_type)
1462	{
1463          typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
1464          return _S_construct(__beg, __end, __a, _Tag());
1465	}
1466
1467      template<class _InIterator>
1468        static _CharT*
1469        _S_construct_aux(_InIterator __beg, _InIterator __end,
1470			 const _Alloc& __a, __true_type)
1471	{ return _S_construct(static_cast<size_type>(__beg),
1472			      static_cast<value_type>(__end), __a); }
1473
1474      template<class _InIterator>
1475        static _CharT*
1476        _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
1477	{
1478	  typedef typename std::__is_integer<_InIterator>::__type _Integral;
1479	  return _S_construct_aux(__beg, __end, __a, _Integral());
1480        }
1481
1482      // For Input Iterators, used in istreambuf_iterators, etc.
1483      template<class _InIterator>
1484        static _CharT*
1485         _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1486		      input_iterator_tag);
1487
1488      // For forward_iterators up to random_access_iterators, used for
1489      // string::iterator, _CharT*, etc.
1490      template<class _FwdIterator>
1491        static _CharT*
1492        _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
1493		     forward_iterator_tag);
1494
1495      static _CharT*
1496      _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
1497
1498    public:
1499
1500      /**
1501       *  @brief  Copy substring into C string.
1502       *  @param s  C string to copy value into.
1503       *  @param n  Number of characters to copy.
1504       *  @param pos  Index of first character to copy.
1505       *  @return  Number of characters actually copied
1506       *  @throw  std::out_of_range  If pos > size().
1507       *
1508       *  Copies up to @a n characters starting at @a pos into the C string @a
1509       *  s.  If @a pos is greater than size(), out_of_range is thrown.
1510      */
1511      size_type
1512      copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
1513
1514      /**
1515       *  @brief  Swap contents with another string.
1516       *  @param s  String to swap with.
1517       *
1518       *  Exchanges the contents of this string with that of @a s in constant
1519       *  time.
1520      */
1521      void
1522      swap(basic_string& __s);
1523
1524      // String operations:
1525      /**
1526       *  @brief  Return const pointer to null-terminated contents.
1527       *
1528       *  This is a handle to internal data.  Do not modify or dire things may
1529       *  happen.
1530      */
1531      const _CharT*
1532      c_str() const
1533      { return _M_data(); }
1534
1535      /**
1536       *  @brief  Return const pointer to contents.
1537       *
1538       *  This is a handle to internal data.  Do not modify or dire things may
1539       *  happen.
1540      */
1541      const _CharT*
1542      data() const
1543      { return _M_data(); }
1544
1545      /**
1546       *  @brief  Return copy of allocator used to construct this string.
1547      */
1548      allocator_type
1549      get_allocator() const
1550      { return _M_dataplus; }
1551
1552      /**
1553       *  @brief  Find position of a C substring.
1554       *  @param s  C string to locate.
1555       *  @param pos  Index of character to search from.
1556       *  @param n  Number of characters from @a s to search for.
1557       *  @return  Index of start of first occurrence.
1558       *
1559       *  Starting from @a pos, searches forward for the first @a n characters
1560       *  in @a s within this string.  If found, returns the index where it
1561       *  begins.  If not found, returns npos.
1562      */
1563      size_type
1564      find(const _CharT* __s, size_type __pos, size_type __n) const;
1565
1566      /**
1567       *  @brief  Find position of a string.
1568       *  @param str  String to locate.
1569       *  @param pos  Index of character to search from (default 0).
1570       *  @return  Index of start of first occurrence.
1571       *
1572       *  Starting from @a pos, searches forward for value of @a str within
1573       *  this string.  If found, returns the index where it begins.  If not
1574       *  found, returns npos.
1575      */
1576      size_type
1577      find(const basic_string& __str, size_type __pos = 0) const
1578      { return this->find(__str.data(), __pos, __str.size()); }
1579
1580      /**
1581       *  @brief  Find position of a C string.
1582       *  @param s  C string to locate.
1583       *  @param pos  Index of character to search from (default 0).
1584       *  @return  Index of start of first occurrence.
1585       *
1586       *  Starting from @a pos, searches forward for the value of @a s within
1587       *  this string.  If found, returns the index where it begins.  If not
1588       *  found, returns npos.
1589      */
1590      size_type
1591      find(const _CharT* __s, size_type __pos = 0) const
1592      {
1593	__glibcxx_requires_string(__s);
1594	return this->find(__s, __pos, traits_type::length(__s));
1595      }
1596
1597      /**
1598       *  @brief  Find position of a character.
1599       *  @param c  Character to locate.
1600       *  @param pos  Index of character to search from (default 0).
1601       *  @return  Index of first occurrence.
1602       *
1603       *  Starting from @a pos, searches forward for @a c within this string.
1604       *  If found, returns the index where it was found.  If not found,
1605       *  returns npos.
1606      */
1607      size_type
1608      find(_CharT __c, size_type __pos = 0) const;
1609
1610      /**
1611       *  @brief  Find last position of a string.
1612       *  @param str  String to locate.
1613       *  @param pos  Index of character to search back from (default end).
1614       *  @return  Index of start of last occurrence.
1615       *
1616       *  Starting from @a pos, searches backward for value of @a str within
1617       *  this string.  If found, returns the index where it begins.  If not
1618       *  found, returns npos.
1619      */
1620      size_type
1621      rfind(const basic_string& __str, size_type __pos = npos) const
1622      { return this->rfind(__str.data(), __pos, __str.size()); }
1623
1624      /**
1625       *  @brief  Find last position of a C substring.
1626       *  @param s  C string to locate.
1627       *  @param pos  Index of character to search back from.
1628       *  @param n  Number of characters from s to search for.
1629       *  @return  Index of start of last occurrence.
1630       *
1631       *  Starting from @a pos, searches backward for the first @a n
1632       *  characters in @a s within this string.  If found, returns the index
1633       *  where it begins.  If not found, returns npos.
1634      */
1635      size_type
1636      rfind(const _CharT* __s, size_type __pos, size_type __n) const;
1637
1638      /**
1639       *  @brief  Find last position of a C string.
1640       *  @param s  C string to locate.
1641       *  @param pos  Index of character to start search at (default end).
1642       *  @return  Index of start of  last occurrence.
1643       *
1644       *  Starting from @a pos, searches backward for the value of @a s within
1645       *  this string.  If found, returns the index where it begins.  If not
1646       *  found, returns npos.
1647      */
1648      size_type
1649      rfind(const _CharT* __s, size_type __pos = npos) const
1650      {
1651	__glibcxx_requires_string(__s);
1652	return this->rfind(__s, __pos, traits_type::length(__s));
1653      }
1654
1655      /**
1656       *  @brief  Find last position of a character.
1657       *  @param c  Character to locate.
1658       *  @param pos  Index of character to search back from (default end).
1659       *  @return  Index of last occurrence.
1660       *
1661       *  Starting from @a pos, searches backward for @a c within this string.
1662       *  If found, returns the index where it was found.  If not found,
1663       *  returns npos.
1664      */
1665      size_type
1666      rfind(_CharT __c, size_type __pos = npos) const;
1667
1668      /**
1669       *  @brief  Find position of a character of string.
1670       *  @param str  String containing characters to locate.
1671       *  @param pos  Index of character to search from (default 0).
1672       *  @return  Index of first occurrence.
1673       *
1674       *  Starting from @a pos, searches forward for one of the characters of
1675       *  @a str within this string.  If found, returns the index where it was
1676       *  found.  If not found, returns npos.
1677      */
1678      size_type
1679      find_first_of(const basic_string& __str, size_type __pos = 0) const
1680      { return this->find_first_of(__str.data(), __pos, __str.size()); }
1681
1682      /**
1683       *  @brief  Find position of a character of C substring.
1684       *  @param s  String containing characters to locate.
1685       *  @param pos  Index of character to search from.
1686       *  @param n  Number of characters from s to search for.
1687       *  @return  Index of first occurrence.
1688       *
1689       *  Starting from @a pos, searches forward for one of the first @a n
1690       *  characters of @a s within this string.  If found, returns the index
1691       *  where it was found.  If not found, returns npos.
1692      */
1693      size_type
1694      find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
1695
1696      /**
1697       *  @brief  Find position of a character of C string.
1698       *  @param s  String containing characters to locate.
1699       *  @param pos  Index of character to search from (default 0).
1700       *  @return  Index of first occurrence.
1701       *
1702       *  Starting from @a pos, searches forward for one of the characters of
1703       *  @a s within this string.  If found, returns the index where it was
1704       *  found.  If not found, returns npos.
1705      */
1706      size_type
1707      find_first_of(const _CharT* __s, size_type __pos = 0) const
1708      {
1709	__glibcxx_requires_string(__s);
1710	return this->find_first_of(__s, __pos, traits_type::length(__s));
1711      }
1712
1713      /**
1714       *  @brief  Find position of a character.
1715       *  @param c  Character to locate.
1716       *  @param pos  Index of character to search from (default 0).
1717       *  @return  Index of first occurrence.
1718       *
1719       *  Starting from @a pos, searches forward for the character @a c within
1720       *  this string.  If found, returns the index where it was found.  If
1721       *  not found, returns npos.
1722       *
1723       *  Note: equivalent to find(c, pos).
1724      */
1725      size_type
1726      find_first_of(_CharT __c, size_type __pos = 0) const
1727      { return this->find(__c, __pos); }
1728
1729      /**
1730       *  @brief  Find last position of a character of string.
1731       *  @param str  String containing characters to locate.
1732       *  @param pos  Index of character to search back from (default end).
1733       *  @return  Index of last occurrence.
1734       *
1735       *  Starting from @a pos, searches backward for one of the characters of
1736       *  @a str within this string.  If found, returns the index where it was
1737       *  found.  If not found, returns npos.
1738      */
1739      size_type
1740      find_last_of(const basic_string& __str, size_type __pos = npos) const
1741      { return this->find_last_of(__str.data(), __pos, __str.size()); }
1742
1743      /**
1744       *  @brief  Find last position of a character of C substring.
1745       *  @param s  C string containing characters to locate.
1746       *  @param pos  Index of character to search back from.
1747       *  @param n  Number of characters from s to search for.
1748       *  @return  Index of last occurrence.
1749       *
1750       *  Starting from @a pos, searches backward for one of the first @a n
1751       *  characters of @a s within this string.  If found, returns the index
1752       *  where it was found.  If not found, returns npos.
1753      */
1754      size_type
1755      find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
1756
1757      /**
1758       *  @brief  Find last position of a character of C string.
1759       *  @param s  C string containing characters to locate.
1760       *  @param pos  Index of character to search back from (default end).
1761       *  @return  Index of last occurrence.
1762       *
1763       *  Starting from @a pos, searches backward for one of the characters of
1764       *  @a s within this string.  If found, returns the index where it was
1765       *  found.  If not found, returns npos.
1766      */
1767      size_type
1768      find_last_of(const _CharT* __s, size_type __pos = npos) const
1769      {
1770	__glibcxx_requires_string(__s);
1771	return this->find_last_of(__s, __pos, traits_type::length(__s));
1772      }
1773
1774      /**
1775       *  @brief  Find last position of a character.
1776       *  @param c  Character to locate.
1777       *  @param pos  Index of character to search back from (default end).
1778       *  @return  Index of last occurrence.
1779       *
1780       *  Starting from @a pos, searches backward for @a c within this string.
1781       *  If found, returns the index where it was found.  If not found,
1782       *  returns npos.
1783       *
1784       *  Note: equivalent to rfind(c, pos).
1785      */
1786      size_type
1787      find_last_of(_CharT __c, size_type __pos = npos) const
1788      { return this->rfind(__c, __pos); }
1789
1790      /**
1791       *  @brief  Find position of a character not in string.
1792       *  @param str  String containing characters to avoid.
1793       *  @param pos  Index of character to search from (default 0).
1794       *  @return  Index of first occurrence.
1795       *
1796       *  Starting from @a pos, searches forward for a character not contained
1797       *  in @a str within this string.  If found, returns the index where it
1798       *  was found.  If not found, returns npos.
1799      */
1800      size_type
1801      find_first_not_of(const basic_string& __str, size_type __pos = 0) const
1802      { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
1803
1804      /**
1805       *  @brief  Find position of a character not in C substring.
1806       *  @param s  C string containing characters to avoid.
1807       *  @param pos  Index of character to search from.
1808       *  @param n  Number of characters from s to consider.
1809       *  @return  Index of first occurrence.
1810       *
1811       *  Starting from @a pos, searches forward for a character not contained
1812       *  in the first @a n characters of @a s within this string.  If found,
1813       *  returns the index where it was found.  If not found, returns npos.
1814      */
1815      size_type
1816      find_first_not_of(const _CharT* __s, size_type __pos,
1817			size_type __n) const;
1818
1819      /**
1820       *  @brief  Find position of a character not in C string.
1821       *  @param s  C string containing characters to avoid.
1822       *  @param pos  Index of character to search from (default 0).
1823       *  @return  Index of first occurrence.
1824       *
1825       *  Starting from @a pos, searches forward for a character not contained
1826       *  in @a s within this string.  If found, returns the index where it
1827       *  was found.  If not found, returns npos.
1828      */
1829      size_type
1830      find_first_not_of(const _CharT* __s, size_type __pos = 0) const
1831      {
1832	__glibcxx_requires_string(__s);
1833	return this->find_first_not_of(__s, __pos, traits_type::length(__s));
1834      }
1835
1836      /**
1837       *  @brief  Find position of a different character.
1838       *  @param c  Character to avoid.
1839       *  @param pos  Index of character to search from (default 0).
1840       *  @return  Index of first occurrence.
1841       *
1842       *  Starting from @a pos, searches forward for a character other than @a c
1843       *  within this string.  If found, returns the index where it was found.
1844       *  If not found, returns npos.
1845      */
1846      size_type
1847      find_first_not_of(_CharT __c, size_type __pos = 0) const;
1848
1849      /**
1850       *  @brief  Find last position of a character not in string.
1851       *  @param str  String containing characters to avoid.
1852       *  @param pos  Index of character to search back from (default end).
1853       *  @return  Index of last occurrence.
1854       *
1855       *  Starting from @a pos, searches backward for a character not
1856       *  contained in @a str within this string.  If found, returns the index
1857       *  where it was found.  If not found, returns npos.
1858      */
1859      size_type
1860      find_last_not_of(const basic_string& __str, size_type __pos = npos) const
1861      { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
1862
1863      /**
1864       *  @brief  Find last position of a character not in C substring.
1865       *  @param s  C string containing characters to avoid.
1866       *  @param pos  Index of character to search back from.
1867       *  @param n  Number of characters from s to consider.
1868       *  @return  Index of last occurrence.
1869       *
1870       *  Starting from @a pos, searches backward for a character not
1871       *  contained in the first @a n characters of @a s within this string.
1872       *  If found, returns the index where it was found.  If not found,
1873       *  returns npos.
1874      */
1875      size_type
1876      find_last_not_of(const _CharT* __s, size_type __pos,
1877		       size_type __n) const;
1878      /**
1879       *  @brief  Find last position of a character not in C string.
1880       *  @param s  C string containing characters to avoid.
1881       *  @param pos  Index of character to search back from (default end).
1882       *  @return  Index of last occurrence.
1883       *
1884       *  Starting from @a pos, searches backward for a character not
1885       *  contained in @a s within this string.  If found, returns the index
1886       *  where it was found.  If not found, returns npos.
1887      */
1888      size_type
1889      find_last_not_of(const _CharT* __s, size_type __pos = npos) const
1890      {
1891	__glibcxx_requires_string(__s);
1892	return this->find_last_not_of(__s, __pos, traits_type::length(__s));
1893      }
1894
1895      /**
1896       *  @brief  Find last position of a different character.
1897       *  @param c  Character to avoid.
1898       *  @param pos  Index of character to search back from (default end).
1899       *  @return  Index of last occurrence.
1900       *
1901       *  Starting from @a pos, searches backward for a character other than
1902       *  @a c within this string.  If found, returns the index where it was
1903       *  found.  If not found, returns npos.
1904      */
1905      size_type
1906      find_last_not_of(_CharT __c, size_type __pos = npos) const;
1907
1908      /**
1909       *  @brief  Get a substring.
1910       *  @param pos  Index of first character (default 0).
1911       *  @param n  Number of characters in substring (default remainder).
1912       *  @return  The new string.
1913       *  @throw  std::out_of_range  If pos > size().
1914       *
1915       *  Construct and return a new string using the @a n characters starting
1916       *  at @a pos.  If the string is too short, use the remainder of the
1917       *  characters.  If @a pos is beyond the end of the string, out_of_range
1918       *  is thrown.
1919      */
1920      basic_string
1921      substr(size_type __pos = 0, size_type __n = npos) const
1922      { return basic_string(*this,
1923			    _M_check(__pos, "basic_string::substr"), __n); }
1924
1925      /**
1926       *  @brief  Compare to a string.
1927       *  @param str  String to compare against.
1928       *  @return  Integer < 0, 0, or > 0.
1929       *
1930       *  Returns an integer < 0 if this string is ordered before @a str, 0 if
1931       *  their values are equivalent, or > 0 if this string is ordered after
1932       *  @a str.  Determines the effective length rlen of the strings to
1933       *  compare as the smallest of size() and str.size().  The function
1934       *  then compares the two strings by calling traits::compare(data(),
1935       *  str.data(),rlen).  If the result of the comparison is nonzero returns
1936       *  it, otherwise the shorter one is ordered first.
1937      */
1938      int
1939      compare(const basic_string& __str) const
1940      {
1941	const size_type __size = this->size();
1942	const size_type __osize = __str.size();
1943	const size_type __len = std::min(__size, __osize);
1944
1945	int __r = traits_type::compare(_M_data(), __str.data(), __len);
1946	if (!__r)
1947	  __r =  _S_compare(__size, __osize);
1948	return __r;
1949      }
1950
1951      /**
1952       *  @brief  Compare substring to a string.
1953       *  @param pos  Index of first character of substring.
1954       *  @param n  Number of characters in substring.
1955       *  @param str  String to compare against.
1956       *  @return  Integer < 0, 0, or > 0.
1957       *
1958       *  Form the substring of this string from the @a n characters starting
1959       *  at @a pos.  Returns an integer < 0 if the substring is ordered
1960       *  before @a str, 0 if their values are equivalent, or > 0 if the
1961       *  substring is ordered after @a str.  Determines the effective length
1962       *  rlen of the strings to compare as the smallest of the length of the
1963       *  substring and @a str.size().  The function then compares the two
1964       *  strings by calling traits::compare(substring.data(),str.data(),rlen).
1965       *  If the result of the comparison is nonzero returns it, otherwise the
1966       *  shorter one is ordered first.
1967      */
1968      int
1969      compare(size_type __pos, size_type __n, const basic_string& __str) const;
1970
1971      /**
1972       *  @brief  Compare substring to a substring.
1973       *  @param pos1  Index of first character of substring.
1974       *  @param n1  Number of characters in substring.
1975       *  @param str  String to compare against.
1976       *  @param pos2  Index of first character of substring of str.
1977       *  @param n2  Number of characters in substring of str.
1978       *  @return  Integer < 0, 0, or > 0.
1979       *
1980       *  Form the substring of this string from the @a n1 characters starting
1981       *  at @a pos1.  Form the substring of @a str from the @a n2 characters
1982       *  starting at @a pos2.  Returns an integer < 0 if this substring is
1983       *  ordered before the substring of @a str, 0 if their values are
1984       *  equivalent, or > 0 if this substring is ordered after the substring
1985       *  of @a str.  Determines the effective length rlen of the strings
1986       *  to compare as the smallest of the lengths of the substrings.  The
1987       *  function then compares the two strings by calling
1988       *  traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
1989       *  If the result of the comparison is nonzero returns it, otherwise the
1990       *  shorter one is ordered first.
1991      */
1992      int
1993      compare(size_type __pos1, size_type __n1, const basic_string& __str,
1994	      size_type __pos2, size_type __n2) const;
1995
1996      /**
1997       *  @brief  Compare to a C string.
1998       *  @param s  C string to compare against.
1999       *  @return  Integer < 0, 0, or > 0.
2000       *
2001       *  Returns an integer < 0 if this string is ordered before @a s, 0 if
2002       *  their values are equivalent, or > 0 if this string is ordered after
2003       *  @a s.  Determines the effective length rlen of the strings to
2004       *  compare as the smallest of size() and the length of a string
2005       *  constructed from @a s.  The function then compares the two strings
2006       *  by calling traits::compare(data(),s,rlen).  If the result of the
2007       *  comparison is nonzero returns it, otherwise the shorter one is
2008       *  ordered first.
2009      */
2010      int
2011      compare(const _CharT* __s) const;
2012
2013      // _GLIBCXX_RESOLVE_LIB_DEFECTS
2014      // 5 String::compare specification questionable
2015      /**
2016       *  @brief  Compare substring to a C string.
2017       *  @param pos  Index of first character of substring.
2018       *  @param n1  Number of characters in substring.
2019       *  @param s  C string to compare against.
2020       *  @return  Integer < 0, 0, or > 0.
2021       *
2022       *  Form the substring of this string from the @a n1 characters starting
2023       *  at @a pos.  Returns an integer < 0 if the substring is ordered
2024       *  before @a s, 0 if their values are equivalent, or > 0 if the
2025       *  substring is ordered after @a s.  Determines the effective length
2026       *  rlen of the strings to compare as the smallest of the length of the
2027       *  substring and the length of a string constructed from @a s.  The
2028       *  function then compares the two string by calling
2029       *  traits::compare(substring.data(),s,rlen).  If the result of the
2030       *  comparison is nonzero returns it, otherwise the shorter one is
2031       *  ordered first.
2032      */
2033      int
2034      compare(size_type __pos, size_type __n1, const _CharT* __s) const;
2035
2036      /**
2037       *  @brief  Compare substring against a character array.
2038       *  @param pos1  Index of first character of substring.
2039       *  @param n1  Number of characters in substring.
2040       *  @param s  character array to compare against.
2041       *  @param n2  Number of characters of s.
2042       *  @return  Integer < 0, 0, or > 0.
2043       *
2044       *  Form the substring of this string from the @a n1 characters starting
2045       *  at @a pos1.  Form a string from the first @a n2 characters of @a s.
2046       *  Returns an integer < 0 if this substring is ordered before the string
2047       *  from @a s, 0 if their values are equivalent, or > 0 if this substring
2048       *  is ordered after the string from @a s.   Determines the effective
2049       *  length rlen of the strings to compare as the smallest of the length
2050       *  of the substring and @a n2.  The function then compares the two
2051       *  strings by calling traits::compare(substring.data(),s,rlen).  If the
2052       *  result of the comparison is nonzero returns it, otherwise the shorter
2053       *  one is ordered first.
2054       *
2055       *  NB: s must have at least n2 characters, '\0' has no special
2056       *  meaning.
2057      */
2058      int
2059      compare(size_type __pos, size_type __n1, const _CharT* __s,
2060	      size_type __n2) const;
2061  };
2062
2063  template<typename _CharT, typename _Traits, typename _Alloc>
2064    inline basic_string<_CharT, _Traits, _Alloc>::
2065    basic_string()
2066#ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
2067    : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
2068#else
2069    : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()) { }
2070#endif
2071
2072  // operator+
2073  /**
2074   *  @brief  Concatenate two strings.
2075   *  @param lhs  First string.
2076   *  @param rhs  Last string.
2077   *  @return  New string with value of @a lhs followed by @a rhs.
2078   */
2079  template<typename _CharT, typename _Traits, typename _Alloc>
2080    basic_string<_CharT, _Traits, _Alloc>
2081    operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2082	      const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2083    {
2084      basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2085      __str.append(__rhs);
2086      return __str;
2087    }
2088
2089  /**
2090   *  @brief  Concatenate C string and string.
2091   *  @param lhs  First string.
2092   *  @param rhs  Last string.
2093   *  @return  New string with value of @a lhs followed by @a rhs.
2094   */
2095  template<typename _CharT, typename _Traits, typename _Alloc>
2096    basic_string<_CharT,_Traits,_Alloc>
2097    operator+(const _CharT* __lhs,
2098	      const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2099
2100  /**
2101   *  @brief  Concatenate character and string.
2102   *  @param lhs  First string.
2103   *  @param rhs  Last string.
2104   *  @return  New string with @a lhs followed by @a rhs.
2105   */
2106  template<typename _CharT, typename _Traits, typename _Alloc>
2107    basic_string<_CharT,_Traits,_Alloc>
2108    operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2109
2110  /**
2111   *  @brief  Concatenate string and C string.
2112   *  @param lhs  First string.
2113   *  @param rhs  Last string.
2114   *  @return  New string with @a lhs followed by @a rhs.
2115   */
2116  template<typename _CharT, typename _Traits, typename _Alloc>
2117    inline basic_string<_CharT, _Traits, _Alloc>
2118    operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2119	     const _CharT* __rhs)
2120    {
2121      basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2122      __str.append(__rhs);
2123      return __str;
2124    }
2125
2126  /**
2127   *  @brief  Concatenate string and character.
2128   *  @param lhs  First string.
2129   *  @param rhs  Last string.
2130   *  @return  New string with @a lhs followed by @a rhs.
2131   */
2132  template<typename _CharT, typename _Traits, typename _Alloc>
2133    inline basic_string<_CharT, _Traits, _Alloc>
2134    operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
2135    {
2136      typedef basic_string<_CharT, _Traits, _Alloc>	__string_type;
2137      typedef typename __string_type::size_type		__size_type;
2138      __string_type __str(__lhs);
2139      __str.append(__size_type(1), __rhs);
2140      return __str;
2141    }
2142
2143  // operator ==
2144  /**
2145   *  @brief  Test equivalence of two strings.
2146   *  @param lhs  First string.
2147   *  @param rhs  Second string.
2148   *  @return  True if @a lhs.compare(@a rhs) == 0.  False otherwise.
2149   */
2150  template<typename _CharT, typename _Traits, typename _Alloc>
2151    inline bool
2152    operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2153	       const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2154    { return __lhs.compare(__rhs) == 0; }
2155
2156  /**
2157   *  @brief  Test equivalence of C string and string.
2158   *  @param lhs  C string.
2159   *  @param rhs  String.
2160   *  @return  True if @a rhs.compare(@a lhs) == 0.  False otherwise.
2161   */
2162  template<typename _CharT, typename _Traits, typename _Alloc>
2163    inline bool
2164    operator==(const _CharT* __lhs,
2165	       const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2166    { return __rhs.compare(__lhs) == 0; }
2167
2168  /**
2169   *  @brief  Test equivalence of string and C string.
2170   *  @param lhs  String.
2171   *  @param rhs  C string.
2172   *  @return  True if @a lhs.compare(@a rhs) == 0.  False otherwise.
2173   */
2174  template<typename _CharT, typename _Traits, typename _Alloc>
2175    inline bool
2176    operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2177	       const _CharT* __rhs)
2178    { return __lhs.compare(__rhs) == 0; }
2179
2180  // operator !=
2181  /**
2182   *  @brief  Test difference of two strings.
2183   *  @param lhs  First string.
2184   *  @param rhs  Second string.
2185   *  @return  True if @a lhs.compare(@a rhs) != 0.  False otherwise.
2186   */
2187  template<typename _CharT, typename _Traits, typename _Alloc>
2188    inline bool
2189    operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2190	       const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2191    { return __rhs.compare(__lhs) != 0; }
2192
2193  /**
2194   *  @brief  Test difference of C string and string.
2195   *  @param lhs  C string.
2196   *  @param rhs  String.
2197   *  @return  True if @a rhs.compare(@a lhs) != 0.  False otherwise.
2198   */
2199  template<typename _CharT, typename _Traits, typename _Alloc>
2200    inline bool
2201    operator!=(const _CharT* __lhs,
2202	       const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2203    { return __rhs.compare(__lhs) != 0; }
2204
2205  /**
2206   *  @brief  Test difference of string and C string.
2207   *  @param lhs  String.
2208   *  @param rhs  C string.
2209   *  @return  True if @a lhs.compare(@a rhs) != 0.  False otherwise.
2210   */
2211  template<typename _CharT, typename _Traits, typename _Alloc>
2212    inline bool
2213    operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2214	       const _CharT* __rhs)
2215    { return __lhs.compare(__rhs) != 0; }
2216
2217  // operator <
2218  /**
2219   *  @brief  Test if string precedes string.
2220   *  @param lhs  First string.
2221   *  @param rhs  Second string.
2222   *  @return  True if @a lhs precedes @a rhs.  False otherwise.
2223   */
2224  template<typename _CharT, typename _Traits, typename _Alloc>
2225    inline bool
2226    operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2227	      const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2228    { return __lhs.compare(__rhs) < 0; }
2229
2230  /**
2231   *  @brief  Test if string precedes C string.
2232   *  @param lhs  String.
2233   *  @param rhs  C string.
2234   *  @return  True if @a lhs precedes @a rhs.  False otherwise.
2235   */
2236  template<typename _CharT, typename _Traits, typename _Alloc>
2237    inline bool
2238    operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2239	      const _CharT* __rhs)
2240    { return __lhs.compare(__rhs) < 0; }
2241
2242  /**
2243   *  @brief  Test if C string precedes string.
2244   *  @param lhs  C string.
2245   *  @param rhs  String.
2246   *  @return  True if @a lhs precedes @a rhs.  False otherwise.
2247   */
2248  template<typename _CharT, typename _Traits, typename _Alloc>
2249    inline bool
2250    operator<(const _CharT* __lhs,
2251	      const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2252    { return __rhs.compare(__lhs) > 0; }
2253
2254  // operator >
2255  /**
2256   *  @brief  Test if string follows string.
2257   *  @param lhs  First string.
2258   *  @param rhs  Second string.
2259   *  @return  True if @a lhs follows @a rhs.  False otherwise.
2260   */
2261  template<typename _CharT, typename _Traits, typename _Alloc>
2262    inline bool
2263    operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2264	      const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2265    { return __lhs.compare(__rhs) > 0; }
2266
2267  /**
2268   *  @brief  Test if string follows C string.
2269   *  @param lhs  String.
2270   *  @param rhs  C string.
2271   *  @return  True if @a lhs follows @a rhs.  False otherwise.
2272   */
2273  template<typename _CharT, typename _Traits, typename _Alloc>
2274    inline bool
2275    operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2276	      const _CharT* __rhs)
2277    { return __lhs.compare(__rhs) > 0; }
2278
2279  /**
2280   *  @brief  Test if C string follows string.
2281   *  @param lhs  C string.
2282   *  @param rhs  String.
2283   *  @return  True if @a lhs follows @a rhs.  False otherwise.
2284   */
2285  template<typename _CharT, typename _Traits, typename _Alloc>
2286    inline bool
2287    operator>(const _CharT* __lhs,
2288	      const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2289    { return __rhs.compare(__lhs) < 0; }
2290
2291  // operator <=
2292  /**
2293   *  @brief  Test if string doesn't follow string.
2294   *  @param lhs  First string.
2295   *  @param rhs  Second string.
2296   *  @return  True if @a lhs doesn't follow @a rhs.  False otherwise.
2297   */
2298  template<typename _CharT, typename _Traits, typename _Alloc>
2299    inline bool
2300    operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2301	       const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2302    { return __lhs.compare(__rhs) <= 0; }
2303
2304  /**
2305   *  @brief  Test if string doesn't follow C string.
2306   *  @param lhs  String.
2307   *  @param rhs  C string.
2308   *  @return  True if @a lhs doesn't follow @a rhs.  False otherwise.
2309   */
2310  template<typename _CharT, typename _Traits, typename _Alloc>
2311    inline bool
2312    operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2313	       const _CharT* __rhs)
2314    { return __lhs.compare(__rhs) <= 0; }
2315
2316  /**
2317   *  @brief  Test if C string doesn't follow string.
2318   *  @param lhs  C string.
2319   *  @param rhs  String.
2320   *  @return  True if @a lhs doesn't follow @a rhs.  False otherwise.
2321   */
2322  template<typename _CharT, typename _Traits, typename _Alloc>
2323    inline bool
2324    operator<=(const _CharT* __lhs,
2325	       const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2326    { return __rhs.compare(__lhs) >= 0; }
2327
2328  // operator >=
2329  /**
2330   *  @brief  Test if string doesn't precede string.
2331   *  @param lhs  First string.
2332   *  @param rhs  Second string.
2333   *  @return  True if @a lhs doesn't precede @a rhs.  False otherwise.
2334   */
2335  template<typename _CharT, typename _Traits, typename _Alloc>
2336    inline bool
2337    operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2338	       const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2339    { return __lhs.compare(__rhs) >= 0; }
2340
2341  /**
2342   *  @brief  Test if string doesn't precede C string.
2343   *  @param lhs  String.
2344   *  @param rhs  C string.
2345   *  @return  True if @a lhs doesn't precede @a rhs.  False otherwise.
2346   */
2347  template<typename _CharT, typename _Traits, typename _Alloc>
2348    inline bool
2349    operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2350	       const _CharT* __rhs)
2351    { return __lhs.compare(__rhs) >= 0; }
2352
2353  /**
2354   *  @brief  Test if C string doesn't precede string.
2355   *  @param lhs  C string.
2356   *  @param rhs  String.
2357   *  @return  True if @a lhs doesn't precede @a rhs.  False otherwise.
2358   */
2359  template<typename _CharT, typename _Traits, typename _Alloc>
2360    inline bool
2361    operator>=(const _CharT* __lhs,
2362	     const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2363    { return __rhs.compare(__lhs) <= 0; }
2364
2365  /**
2366   *  @brief  Swap contents of two strings.
2367   *  @param lhs  First string.
2368   *  @param rhs  Second string.
2369   *
2370   *  Exchanges the contents of @a lhs and @a rhs in constant time.
2371   */
2372  template<typename _CharT, typename _Traits, typename _Alloc>
2373    inline void
2374    swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
2375	 basic_string<_CharT, _Traits, _Alloc>& __rhs)
2376    { __lhs.swap(__rhs); }
2377
2378  /**
2379   *  @brief  Read stream into a string.
2380   *  @param is  Input stream.
2381   *  @param str  Buffer to store into.
2382   *  @return  Reference to the input stream.
2383   *
2384   *  Stores characters from @a is into @a str until whitespace is found, the
2385   *  end of the stream is encountered, or str.max_size() is reached.  If
2386   *  is.width() is non-zero, that is the limit on the number of characters
2387   *  stored into @a str.  Any previous contents of @a str are erased.
2388   */
2389  template<typename _CharT, typename _Traits, typename _Alloc>
2390    basic_istream<_CharT, _Traits>&
2391    operator>>(basic_istream<_CharT, _Traits>& __is,
2392	       basic_string<_CharT, _Traits, _Alloc>& __str);
2393
2394  template<>
2395    basic_istream<char>&
2396    operator>>(basic_istream<char>& __is, basic_string<char>& __str);
2397
2398  /**
2399   *  @brief  Write string to a stream.
2400   *  @param os  Output stream.
2401   *  @param str  String to write out.
2402   *  @return  Reference to the output stream.
2403   *
2404   *  Output characters of @a str into os following the same rules as for
2405   *  writing a C string.
2406   */
2407  template<typename _CharT, typename _Traits, typename _Alloc>
2408    inline basic_ostream<_CharT, _Traits>&
2409    operator<<(basic_ostream<_CharT, _Traits>& __os,
2410	       const basic_string<_CharT, _Traits, _Alloc>& __str)
2411    {
2412      // _GLIBCXX_RESOLVE_LIB_DEFECTS
2413      // 586. string inserter not a formatted function
2414      return __ostream_insert(__os, __str.data(), __str.size());
2415    }
2416
2417  /**
2418   *  @brief  Read a line from stream into a string.
2419   *  @param is  Input stream.
2420   *  @param str  Buffer to store into.
2421   *  @param delim  Character marking end of line.
2422   *  @return  Reference to the input stream.
2423   *
2424   *  Stores characters from @a is into @a str until @a delim is found, the
2425   *  end of the stream is encountered, or str.max_size() is reached.  If
2426   *  is.width() is non-zero, that is the limit on the number of characters
2427   *  stored into @a str.  Any previous contents of @a str are erased.  If @a
2428   *  delim was encountered, it is extracted but not stored into @a str.
2429   */
2430  template<typename _CharT, typename _Traits, typename _Alloc>
2431    basic_istream<_CharT, _Traits>&
2432    getline(basic_istream<_CharT, _Traits>& __is,
2433	    basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
2434
2435  /**
2436   *  @brief  Read a line from stream into a string.
2437   *  @param is  Input stream.
2438   *  @param str  Buffer to store into.
2439   *  @return  Reference to the input stream.
2440   *
2441   *  Stores characters from is into @a str until '\n' is found, the end of
2442   *  the stream is encountered, or str.max_size() is reached.  If is.width()
2443   *  is non-zero, that is the limit on the number of characters stored into
2444   *  @a str.  Any previous contents of @a str are erased.  If end of line was
2445   *  encountered, it is extracted but not stored into @a str.
2446   */
2447  template<typename _CharT, typename _Traits, typename _Alloc>
2448    inline basic_istream<_CharT, _Traits>&
2449    getline(basic_istream<_CharT, _Traits>& __is,
2450	    basic_string<_CharT, _Traits, _Alloc>& __str)
2451    { return getline(__is, __str, __is.widen('\n')); }
2452
2453  template<>
2454    basic_istream<char>&
2455    getline(basic_istream<char>& __in, basic_string<char>& __str,
2456	    char __delim);
2457
2458#ifdef _GLIBCXX_USE_WCHAR_T
2459  template<>
2460    basic_istream<wchar_t>&
2461    getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
2462	    wchar_t __delim);
2463#endif
2464
2465_GLIBCXX_END_NAMESPACE
2466
2467#endif /* _BASIC_STRING_H */
2468