OSDN Git Service

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