OSDN Git Service

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