OSDN Git Service

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