OSDN Git Service

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