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