OSDN Git Service

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