OSDN Git Service

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