OSDN Git Service

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