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