OSDN Git Service

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