OSDN Git Service

2005-04-25 Paolo Carlini <pcarlini@suse.de>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / basic_string.h
1 // Components for manipulating sequences of characters -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING.  If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction.  Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License.  This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 //
32 // ISO C++ 14882: 21 Strings library
33 //
34
35 /** @file basic_string.h
36  *  This is an internal header file, included by other library headers.
37  *  You should not attempt to use it directly.
38  */
39
40 #ifndef _BASIC_STRING_H
41 #define _BASIC_STRING_H 1
42
43 #pragma GCC system_header
44
45 #include <bits/atomicity.h>
46 #include <debug/debug.h>
47
48 namespace std
49 {
50   /**
51    *  @class basic_string basic_string.h <string>
52    *  @brief  Managing sequences of characters and character-like objects.
53    *
54    *  @ingroup Containers
55    *  @ingroup Sequences
56    *
57    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
58    *  <a href="tables.html#66">reversible container</a>, and a
59    *  <a href="tables.html#67">sequence</a>.  Of the
60    *  <a href="tables.html#68">optional sequence requirements</a>, only
61    *  @c push_back, @c at, and array access are supported.
62    *
63    *  @doctodo
64    *
65    *
66    *  @if maint
67    *  Documentation?  What's that?
68    *  Nathan Myers <ncm@cantrip.org>.
69    *
70    *  A string looks like this:
71    *
72    *  @code
73    *                                        [_Rep]
74    *                                        _M_length
75    *   [basic_string<char_type>]            _M_capacity
76    *   _M_dataplus                          _M_refcount
77    *   _M_p ---------------->               unnamed array of char_type
78    *  @endcode
79    *
80    *  Where the _M_p points to the first character in the string, and
81    *  you cast it to a pointer-to-_Rep and subtract 1 to get a
82    *  pointer to the header.
83    *
84    *  This approach has the enormous advantage that a string object
85    *  requires only one allocation.  All the ugliness is confined
86    *  within a single pair of inline functions, which each compile to
87    *  a single "add" instruction: _Rep::_M_data(), and
88    *  string::_M_rep(); and the allocation function which gets a
89    *  block of raw bytes and with room enough and constructs a _Rep
90    *  object at the front.
91    *
92    *  The reason you want _M_data pointing to the character array and
93    *  not the _Rep is so that the debugger can see the string
94    *  contents. (Probably we should add a non-inline member to get
95    *  the _Rep for the debugger to use, so users can check the actual
96    *  string length.)
97    *
98    *  Note that the _Rep object is a POD so that you can have a
99    *  static "empty string" _Rep object already "constructed" before
100    *  static constructors have run.  The reference-count encoding is
101    *  chosen so that a 0 indicates one reference, so you never try to
102    *  destroy the empty-string _Rep object.
103    *
104    *  All but the last paragraph is considered pretty conventional
105    *  for a C++ string implementation.
106    *  @endif
107   */
108   // 21.3  Template class basic_string
109   template<typename _CharT, typename _Traits, typename _Alloc>
110     class basic_string
111     {
112       // Types:
113     public:
114       typedef _Traits                                       traits_type;
115       typedef typename _Traits::char_type                   value_type;
116       typedef _Alloc                                        allocator_type;
117       typedef typename _Alloc::size_type                    size_type;
118       typedef typename _Alloc::difference_type              difference_type;
119       typedef typename _Alloc::reference                    reference;
120       typedef typename _Alloc::const_reference              const_reference;
121       typedef typename _Alloc::pointer                      pointer;
122       typedef typename _Alloc::const_pointer                const_pointer;
123       typedef __gnu_cxx::__normal_iterator<pointer, basic_string>  iterator;
124       typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
125                                                             const_iterator;
126       typedef std::reverse_iterator<const_iterator>     const_reverse_iterator;
127       typedef std::reverse_iterator<iterator>               reverse_iterator;
128
129     private:
130       // _Rep: string representation
131       //   Invariants:
132       //   1. String really contains _M_length + 1 characters: due to 21.3.4
133       //      must be kept null-terminated.
134       //   2. _M_capacity >= _M_length
135       //      Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
136       //   3. _M_refcount has three states:
137       //      -1: leaked, one reference, no ref-copies allowed, non-const.
138       //       0: one reference, non-const.
139       //     n>0: n + 1 references, operations require a lock, const.
140       //   4. All fields==0 is an empty string, given the extra storage
141       //      beyond-the-end for a null terminator; thus, the shared
142       //      empty string representation needs no constructor.
143
144       struct _Rep_base
145       {
146         size_type               _M_length;
147         size_type               _M_capacity;
148         _Atomic_word            _M_refcount;
149       };
150
151       struct _Rep : _Rep_base
152       {
153         // Types:
154         typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc;
155
156         // (Public) Data members:
157
158         // The maximum number of individual char_type elements of an
159         // individual string is determined by _S_max_size. This is the
160         // value that will be returned by max_size().  (Whereas npos
161         // is the maximum number of bytes the allocator can allocate.)
162         // If one was to divvy up the theoretical largest size string,
163         // with a terminating character and m _CharT elements, it'd
164         // look like this:
165         // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
166         // Solving for m:
167         // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
168         // In addition, this implementation quarters this amount.
169         static const size_type  _S_max_size;
170         static const _CharT     _S_terminal;
171
172         // The following storage is init'd to 0 by the linker, resulting
173         // (carefully) in an empty string with one reference.
174         static size_type _S_empty_rep_storage[];
175
176         static _Rep&
177         _S_empty_rep()
178         { return *reinterpret_cast<_Rep*>(&_S_empty_rep_storage); }
179
180         bool
181         _M_is_leaked() const
182         { return this->_M_refcount < 0; }
183
184         bool
185         _M_is_shared() const
186         { return this->_M_refcount > 0; }
187
188         void
189         _M_set_leaked()
190         { this->_M_refcount = -1; }
191
192         void
193         _M_set_sharable()
194         { this->_M_refcount = 0; }
195
196         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         _GLIBCXX_DEBUG_ASSERT(__pos < size());
699         _M_leak();
700         return _M_data()[__pos];
701       }
702
703       /**
704        *  @brief  Provides access to the data contained in the %string.
705        *  @param n The index of the character to access.
706        *  @return  Read-only (const) reference to the character.
707        *  @throw  std::out_of_range  If @a n is an invalid index.
708        *
709        *  This function provides for safer data access.  The parameter is
710        *  first checked that it is in the range of the string.  The function
711        *  throws out_of_range if the check fails.
712        */
713       const_reference
714       at(size_type __n) const
715       {
716         if (__n >= this->size())
717           __throw_out_of_range(__N("basic_string::at"));
718         return _M_data()[__n];
719       }
720
721       /**
722        *  @brief  Provides access to the data contained in the %string.
723        *  @param n The index of the character to access.
724        *  @return  Read/write reference to the character.
725        *  @throw  std::out_of_range  If @a n is an invalid index.
726        *
727        *  This function provides for safer data access.  The parameter is
728        *  first checked that it is in the range of the string.  The function
729        *  throws out_of_range if the check fails.  Success results in
730        *  unsharing the string.
731        */
732       reference
733       at(size_type __n)
734       {
735         if (__n >= size())
736           __throw_out_of_range(__N("basic_string::at"));
737         _M_leak();
738         return _M_data()[__n];
739       }
740
741       // Modifiers:
742       /**
743        *  @brief  Append a string to this string.
744        *  @param str  The string to append.
745        *  @return  Reference to this string.
746        */
747       basic_string&
748       operator+=(const basic_string& __str)
749       { return this->append(__str); }
750
751       /**
752        *  @brief  Append a C string.
753        *  @param s  The C string to append.
754        *  @return  Reference to this string.
755        */
756       basic_string&
757       operator+=(const _CharT* __s)
758       { return this->append(__s); }
759
760       /**
761        *  @brief  Append a character.
762        *  @param s  The character to append.
763        *  @return  Reference to this string.
764        */
765       basic_string&
766       operator+=(_CharT __c)
767       { 
768         this->push_back(__c);
769         return *this;
770       }
771
772       /**
773        *  @brief  Append a string to this string.
774        *  @param str  The string to append.
775        *  @return  Reference to this string.
776        */
777       basic_string&
778       append(const basic_string& __str);
779
780       /**
781        *  @brief  Append a substring.
782        *  @param str  The string to append.
783        *  @param pos  Index of the first character of str to append.
784        *  @param n  The number of characters to append.
785        *  @return  Reference to this string.
786        *  @throw  std::out_of_range if @a pos is not a valid index.
787        *
788        *  This function appends @a n characters from @a str starting at @a pos
789        *  to this string.  If @a n is is larger than the number of available
790        *  characters in @a str, the remainder of @a str is appended.
791        */
792       basic_string&
793       append(const basic_string& __str, size_type __pos, size_type __n);
794
795       /**
796        *  @brief  Append a C substring.
797        *  @param s  The C string to append.
798        *  @param n  The number of characters to append.
799        *  @return  Reference to this string.
800        */
801       basic_string&
802       append(const _CharT* __s, size_type __n);
803
804       /**
805        *  @brief  Append a C string.
806        *  @param s  The C string to append.
807        *  @return  Reference to this string.
808        */
809       basic_string&
810       append(const _CharT* __s)
811       {
812         __glibcxx_requires_string(__s);
813         return this->append(__s, traits_type::length(__s));
814       }
815
816       /**
817        *  @brief  Append multiple characters.
818        *  @param n  The number of characters to append.
819        *  @param c  The character to use.
820        *  @return  Reference to this string.
821        *
822        *  Appends n copies of c to this string.
823        */
824       basic_string&
825       append(size_type __n, _CharT __c);
826
827       /**
828        *  @brief  Append a range of characters.
829        *  @param first  Iterator referencing the first character to append.
830        *  @param last  Iterator marking the end of the range.
831        *  @return  Reference to this string.
832        *
833        *  Appends characters in the range [first,last) to this string.
834        */
835       template<class _InputIterator>
836         basic_string&
837         append(_InputIterator __first, _InputIterator __last)
838         { return this->replace(_M_iend(), _M_iend(), __first, __last); }
839
840       /**
841        *  @brief  Append a single character.
842        *  @param c  Character to append.
843        */
844       void
845       push_back(_CharT __c)
846       { 
847         const size_type __len = 1 + this->size();
848         if (__len > this->capacity() || _M_rep()->_M_is_shared())
849           this->reserve(__len);
850         traits_type::assign(_M_data()[this->size()], __c);
851         _M_rep()->_M_set_length_and_sharable(__len);
852       }
853
854       /**
855        *  @brief  Set value to contents of another string.
856        *  @param  str  Source string to use.
857        *  @return  Reference to this string.
858        */
859       basic_string&
860       assign(const basic_string& __str);
861
862       /**
863        *  @brief  Set value to a substring of a string.
864        *  @param str  The string to use.
865        *  @param pos  Index of the first character of str.
866        *  @param n  Number of characters to use.
867        *  @return  Reference to this string.
868        *  @throw  std::out_of_range if @a pos is not a valid index.
869        *
870        *  This function sets this string to the substring of @a str consisting
871        *  of @a n characters at @a pos.  If @a n is is larger than the number
872        *  of available characters in @a str, the remainder of @a str is used.
873        */
874       basic_string&
875       assign(const basic_string& __str, size_type __pos, size_type __n)
876       { return this->assign(__str._M_data()
877                             + __str._M_check(__pos, "basic_string::assign"),
878                             __str._M_limit(__pos, __n)); }
879
880       /**
881        *  @brief  Set value to a C substring.
882        *  @param s  The C string to use.
883        *  @param n  Number of characters to use.
884        *  @return  Reference to this string.
885        *
886        *  This function sets the value of this string to the first @a n
887        *  characters of @a s.  If @a n is is larger than the number of
888        *  available characters in @a s, the remainder of @a s is used.
889        */
890       basic_string&
891       assign(const _CharT* __s, size_type __n);
892
893       /**
894        *  @brief  Set value to contents of a C string.
895        *  @param s  The C string to use.
896        *  @return  Reference to this string.
897        *
898        *  This function sets the value of this string to the value of @a s.
899        *  The data is copied, so there is no dependence on @a s once the
900        *  function returns.
901        */
902       basic_string&
903       assign(const _CharT* __s)
904       {
905         __glibcxx_requires_string(__s);
906         return this->assign(__s, traits_type::length(__s));
907       }
908
909       /**
910        *  @brief  Set value to multiple characters.
911        *  @param n  Length of the resulting string.
912        *  @param c  The character to use.
913        *  @return  Reference to this string.
914        *
915        *  This function sets the value of this string to @a n copies of
916        *  character @a c.
917        */
918       basic_string&
919       assign(size_type __n, _CharT __c)
920       { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
921
922       /**
923        *  @brief  Set value to a range of characters.
924        *  @param first  Iterator referencing the first character to append.
925        *  @param last  Iterator marking the end of the range.
926        *  @return  Reference to this string.
927        *
928        *  Sets value of string to characters in the range [first,last).
929       */
930       template<class _InputIterator>
931         basic_string&
932         assign(_InputIterator __first, _InputIterator __last)
933         { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
934
935       /**
936        *  @brief  Insert multiple characters.
937        *  @param p  Iterator referencing location in string to insert at.
938        *  @param n  Number of characters to insert
939        *  @param c  The character to insert.
940        *  @throw  std::length_error  If new length exceeds @c max_size().
941        *
942        *  Inserts @a n copies of character @a c starting at the position
943        *  referenced by iterator @a p.  If adding characters causes the length
944        *  to exceed max_size(), length_error is thrown.  The value of the
945        *  string doesn't change if an error is thrown.
946       */
947       void
948       insert(iterator __p, size_type __n, _CharT __c)
949       { this->replace(__p, __p, __n, __c);  }
950
951       /**
952        *  @brief  Insert a range of characters.
953        *  @param p  Iterator referencing location in string to insert at.
954        *  @param beg  Start of range.
955        *  @param end  End of range.
956        *  @throw  std::length_error  If new length exceeds @c max_size().
957        *
958        *  Inserts characters in range [beg,end).  If adding characters causes
959        *  the length to exceed max_size(), length_error is thrown.  The value
960        *  of the string doesn't change if an error is thrown.
961       */
962       template<class _InputIterator>
963         void
964         insert(iterator __p, _InputIterator __beg, _InputIterator __end)
965         { this->replace(__p, __p, __beg, __end); }
966
967       /**
968        *  @brief  Insert value of a string.
969        *  @param pos1  Iterator referencing location in string to insert at.
970        *  @param str  The string to insert.
971        *  @return  Reference to this string.
972        *  @throw  std::length_error  If new length exceeds @c max_size().
973        *
974        *  Inserts value of @a str starting at @a pos1.  If adding characters
975        *  causes the length to exceed max_size(), length_error is thrown.  The
976        *  value of the string doesn't change if an error is thrown.
977       */
978       basic_string&
979       insert(size_type __pos1, const basic_string& __str)
980       { return this->insert(__pos1, __str, size_type(0), __str.size()); }
981
982       /**
983        *  @brief  Insert a substring.
984        *  @param pos1  Iterator referencing location in string to insert at.
985        *  @param str  The string to insert.
986        *  @param pos2  Start of characters in str to insert.
987        *  @param n  Number of characters to insert.
988        *  @return  Reference to this string.
989        *  @throw  std::length_error  If new length exceeds @c max_size().
990        *  @throw  std::out_of_range  If @a pos1 > size() or
991        *  @a pos2 > @a str.size().
992        *
993        *  Starting at @a pos1, insert @a n character of @a str beginning with
994        *  @a pos2.  If adding characters causes the length to exceed
995        *  max_size(), length_error is thrown.  If @a pos1 is beyond the end of
996        *  this string or @a pos2 is beyond the end of @a str, out_of_range is
997        *  thrown.  The value of the string doesn't change if an error is
998        *  thrown.
999       */
1000       basic_string&
1001       insert(size_type __pos1, const basic_string& __str,
1002              size_type __pos2, size_type __n)
1003       { return this->insert(__pos1, __str._M_data()
1004                             + __str._M_check(__pos2, "basic_string::insert"),
1005                             __str._M_limit(__pos2, __n)); }
1006
1007       /**
1008        *  @brief  Insert a C substring.
1009        *  @param pos  Iterator referencing location in string to insert at.
1010        *  @param s  The C string to insert.
1011        *  @param n  The number of characters to insert.
1012        *  @return  Reference to this string.
1013        *  @throw  std::length_error  If new length exceeds @c max_size().
1014        *  @throw  std::out_of_range  If @a pos is beyond the end of this
1015        *  string.
1016        *
1017        *  Inserts the first @a n characters of @a s starting at @a pos.  If
1018        *  adding characters causes the length to exceed max_size(),
1019        *  length_error is thrown.  If @a pos is beyond end(), out_of_range is
1020        *  thrown.  The value of the string doesn't change if an error is
1021        *  thrown.
1022       */
1023       basic_string&
1024       insert(size_type __pos, const _CharT* __s, size_type __n);
1025
1026       /**
1027        *  @brief  Insert a C string.
1028        *  @param pos  Iterator referencing location in string to insert at.
1029        *  @param s  The C string to insert.
1030        *  @return  Reference to this string.
1031        *  @throw  std::length_error  If new length exceeds @c max_size().
1032        *  @throw  std::out_of_range  If @a pos is beyond the end of this
1033        *  string.
1034        *
1035        *  Inserts the first @a n characters of @a s starting at @a pos.  If
1036        *  adding characters causes the length to exceed max_size(),
1037        *  length_error is thrown.  If @a pos is beyond end(), out_of_range is
1038        *  thrown.  The value of the string doesn't change if an error is
1039        *  thrown.
1040       */
1041       basic_string&
1042       insert(size_type __pos, const _CharT* __s)
1043       {
1044         __glibcxx_requires_string(__s);
1045         return this->insert(__pos, __s, traits_type::length(__s));
1046       }
1047
1048       /**
1049        *  @brief  Insert multiple characters.
1050        *  @param pos  Index in string to insert at.
1051        *  @param n  Number of characters to insert
1052        *  @param c  The character to insert.
1053        *  @return  Reference to this string.
1054        *  @throw  std::length_error  If new length exceeds @c max_size().
1055        *  @throw  std::out_of_range  If @a pos is beyond the end of this
1056        *  string.
1057        *
1058        *  Inserts @a n copies of character @a c starting at index @a pos.  If
1059        *  adding characters causes the length to exceed max_size(),
1060        *  length_error is thrown.  If @a pos > length(), out_of_range is
1061        *  thrown.  The value of the string doesn't change if an error is
1062        *  thrown.
1063       */
1064       basic_string&
1065       insert(size_type __pos, size_type __n, _CharT __c)
1066       { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
1067                               size_type(0), __n, __c); }
1068
1069       /**
1070        *  @brief  Insert one character.
1071        *  @param p  Iterator referencing position in string to insert at.
1072        *  @param c  The character to insert.
1073        *  @return  Iterator referencing newly inserted char.
1074        *  @throw  std::length_error  If new length exceeds @c max_size().
1075        *
1076        *  Inserts character @a c at position referenced by @a p.  If adding
1077        *  character causes the length to exceed max_size(), length_error is
1078        *  thrown.  If @a p is beyond end of string, out_of_range is thrown.
1079        *  The value of the string doesn't change if an error is thrown.
1080       */
1081       iterator
1082       insert(iterator __p, _CharT __c)
1083       {
1084         _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1085         const size_type __pos = __p - _M_ibegin();
1086         _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1087         _M_rep()->_M_set_leaked();
1088         return this->_M_ibegin() + __pos;
1089       }
1090
1091       /**
1092        *  @brief  Remove characters.
1093        *  @param pos  Index of first character to remove (default 0).
1094        *  @param n  Number of characters to remove (default remainder).
1095        *  @return  Reference to this string.
1096        *  @throw  std::out_of_range  If @a pos is beyond the end of this
1097        *  string.
1098        *
1099        *  Removes @a n characters from this string starting at @a pos.  The
1100        *  length of the string is reduced by @a n.  If there are < @a n
1101        *  characters to remove, the remainder of the string is truncated.  If
1102        *  @a p is beyond end of string, out_of_range is thrown.  The value of
1103        *  the string doesn't change if an error is thrown.
1104       */
1105       basic_string&
1106       erase(size_type __pos = 0, size_type __n = npos)
1107       { 
1108         _M_mutate(_M_check(__pos, "basic_string::erase"),
1109                   _M_limit(__pos, __n), size_type(0));
1110         return *this;
1111       }
1112
1113       /**
1114        *  @brief  Remove one character.
1115        *  @param position  Iterator referencing the character to remove.
1116        *  @return  iterator referencing same location after removal.
1117        *
1118        *  Removes the character at @a position from this string. The value
1119        *  of the string doesn't change if an error is thrown.
1120       */
1121       iterator
1122       erase(iterator __position)
1123       {
1124         _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
1125                                  && __position < _M_iend());
1126         const size_type __pos = __position - _M_ibegin();
1127         _M_mutate(__pos, size_type(1), size_type(0));
1128         _M_rep()->_M_set_leaked();
1129         return _M_ibegin() + __pos;
1130       }
1131
1132       /**
1133        *  @brief  Remove a range of characters.
1134        *  @param first  Iterator referencing the first character to remove.
1135        *  @param last  Iterator referencing the end of the range.
1136        *  @return  Iterator referencing location of first after removal.
1137        *
1138        *  Removes the characters in the range [first,last) from this string.
1139        *  The value of the string doesn't change if an error is thrown.
1140       */
1141       iterator
1142       erase(iterator __first, iterator __last)
1143       {
1144         _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
1145                                  && __last <= _M_iend());
1146         const size_type __pos = __first - _M_ibegin();
1147         _M_mutate(__pos, __last - __first, size_type(0));
1148         _M_rep()->_M_set_leaked();
1149         return _M_ibegin() + __pos;
1150       }
1151
1152       /**
1153        *  @brief  Replace characters with value from another string.
1154        *  @param pos  Index of first character to replace.
1155        *  @param n  Number of characters to be replaced.
1156        *  @param str  String to insert.
1157        *  @return  Reference to this string.
1158        *  @throw  std::out_of_range  If @a pos is beyond the end of this
1159        *  string.
1160        *  @throw  std::length_error  If new length exceeds @c max_size().
1161        *
1162        *  Removes the characters in the range [pos,pos+n) from this string.
1163        *  In place, the value of @a str is inserted.  If @a pos is beyond end
1164        *  of string, out_of_range is thrown.  If the length of the result
1165        *  exceeds max_size(), length_error is thrown.  The value of the string
1166        *  doesn't change if an error is thrown.
1167       */
1168       basic_string&
1169       replace(size_type __pos, size_type __n, const basic_string& __str)
1170       { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1171
1172       /**
1173        *  @brief  Replace characters with value from another string.
1174        *  @param pos1  Index of first character to replace.
1175        *  @param n1  Number of characters to be replaced.
1176        *  @param str  String to insert.
1177        *  @param pos2  Index of first character of str to use.
1178        *  @param n2  Number of characters from str to use.
1179        *  @return  Reference to this string.
1180        *  @throw  std::out_of_range  If @a pos1 > size() or @a pos2 >
1181        *  str.size().
1182        *  @throw  std::length_error  If new length exceeds @c max_size().
1183        *
1184        *  Removes the characters in the range [pos1,pos1 + n) from this
1185        *  string.  In place, the value of @a str is inserted.  If @a pos is
1186        *  beyond end of string, out_of_range is thrown.  If the length of the
1187        *  result exceeds max_size(), length_error is thrown.  The value of the
1188        *  string doesn't change if an error is thrown.
1189       */
1190       basic_string&
1191       replace(size_type __pos1, size_type __n1, const basic_string& __str,
1192               size_type __pos2, size_type __n2)
1193       { return this->replace(__pos1, __n1, __str._M_data()
1194                              + __str._M_check(__pos2, "basic_string::replace"),
1195                              __str._M_limit(__pos2, __n2)); }
1196
1197       /**
1198        *  @brief  Replace characters with value of a C substring.
1199        *  @param pos  Index of first character to replace.
1200        *  @param n1  Number of characters to be replaced.
1201        *  @param str  C string to insert.
1202        *  @param n2  Number of characters from str to use.
1203        *  @return  Reference to this string.
1204        *  @throw  std::out_of_range  If @a pos1 > size().
1205        *  @throw  std::length_error  If new length exceeds @c max_size().
1206        *
1207        *  Removes the characters in the range [pos,pos + n1) from this string.
1208        *  In place, the first @a n2 characters of @a str are inserted, or all
1209        *  of @a str if @a n2 is too large.  If @a pos is beyond end of string,
1210        *  out_of_range is thrown.  If the length of result exceeds max_size(),
1211        *  length_error is thrown.  The value of the string doesn't change if
1212        *  an error is thrown.
1213       */
1214       basic_string&
1215       replace(size_type __pos, size_type __n1, const _CharT* __s,
1216               size_type __n2);
1217
1218       /**
1219        *  @brief  Replace characters with value of a C string.
1220        *  @param pos  Index of first character to replace.
1221        *  @param n1  Number of characters to be replaced.
1222        *  @param str  C string to insert.
1223        *  @return  Reference to this string.
1224        *  @throw  std::out_of_range  If @a pos > size().
1225        *  @throw  std::length_error  If new length exceeds @c max_size().
1226        *
1227        *  Removes the characters in the range [pos,pos + n1) from this string.
1228        *  In place, the first @a n characters of @a str are inserted.  If @a
1229        *  pos is beyond end of string, out_of_range is thrown.  If the length
1230        *  of result exceeds max_size(), length_error is thrown.  The value of
1231        *  the string doesn't change if an error is thrown.
1232       */
1233       basic_string&
1234       replace(size_type __pos, size_type __n1, const _CharT* __s)
1235       {
1236         __glibcxx_requires_string(__s);
1237         return this->replace(__pos, __n1, __s, traits_type::length(__s));
1238       }
1239
1240       /**
1241        *  @brief  Replace characters with multiple characters.
1242        *  @param pos  Index of first character to replace.
1243        *  @param n1  Number of characters to be replaced.
1244        *  @param n2  Number of characters to insert.
1245        *  @param c  Character to insert.
1246        *  @return  Reference to this string.
1247        *  @throw  std::out_of_range  If @a pos > size().
1248        *  @throw  std::length_error  If new length exceeds @c max_size().
1249        *
1250        *  Removes the characters in the range [pos,pos + n1) from this string.
1251        *  In place, @a n2 copies of @a c are inserted.  If @a pos is beyond
1252        *  end of string, out_of_range is thrown.  If the length of result
1253        *  exceeds max_size(), length_error is thrown.  The value of the string
1254        *  doesn't change if an error is thrown.
1255       */
1256       basic_string&
1257       replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1258       { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
1259                               _M_limit(__pos, __n1), __n2, __c); }
1260
1261       /**
1262        *  @brief  Replace range of characters with string.
1263        *  @param i1  Iterator referencing start of range to replace.
1264        *  @param i2  Iterator referencing end of range to replace.
1265        *  @param str  String value to insert.
1266        *  @return  Reference to this string.
1267        *  @throw  std::length_error  If new length exceeds @c max_size().
1268        *
1269        *  Removes the characters in the range [i1,i2).  In place, the value of
1270        *  @a str is inserted.  If the length of result exceeds max_size(),
1271        *  length_error is thrown.  The value of the string doesn't change if
1272        *  an error is thrown.
1273       */
1274       basic_string&
1275       replace(iterator __i1, iterator __i2, const basic_string& __str)
1276       { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
1277
1278       /**
1279        *  @brief  Replace range of characters with C substring.
1280        *  @param i1  Iterator referencing start of range to replace.
1281        *  @param i2  Iterator referencing end of range to replace.
1282        *  @param s  C string value to insert.
1283        *  @param n  Number of characters from s to insert.
1284        *  @return  Reference to this string.
1285        *  @throw  std::length_error  If new length exceeds @c max_size().
1286        *
1287        *  Removes the characters in the range [i1,i2).  In place, the first @a
1288        *  n characters of @a s are inserted.  If the length of result exceeds
1289        *  max_size(), length_error is thrown.  The value of the string doesn't
1290        *  change if an error is thrown.
1291       */
1292       basic_string&
1293       replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
1294       {
1295         _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1296                                  && __i2 <= _M_iend());
1297         return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
1298       }
1299
1300       /**
1301        *  @brief  Replace range of characters with C string.
1302        *  @param i1  Iterator referencing start of range to replace.
1303        *  @param i2  Iterator referencing end of range to replace.
1304        *  @param s  C string value to insert.
1305        *  @return  Reference to this string.
1306        *  @throw  std::length_error  If new length exceeds @c max_size().
1307        *
1308        *  Removes the characters in the range [i1,i2).  In place, the
1309        *  characters of @a s are inserted.  If the length of result exceeds
1310        *  max_size(), length_error is thrown.  The value of the string doesn't
1311        *  change if an error is thrown.
1312       */
1313       basic_string&
1314       replace(iterator __i1, iterator __i2, const _CharT* __s)
1315       {
1316         __glibcxx_requires_string(__s);
1317         return this->replace(__i1, __i2, __s, traits_type::length(__s));
1318       }
1319
1320       /**
1321        *  @brief  Replace range of characters with multiple characters
1322        *  @param i1  Iterator referencing start of range to replace.
1323        *  @param i2  Iterator referencing end of range to replace.
1324        *  @param n  Number of characters to insert.
1325        *  @param c  Character to insert.
1326        *  @return  Reference to this string.
1327        *  @throw  std::length_error  If new length exceeds @c max_size().
1328        *
1329        *  Removes the characters in the range [i1,i2).  In place, @a n copies
1330        *  of @a c are inserted.  If the length of result exceeds max_size(),
1331        *  length_error is thrown.  The value of the string doesn't change if
1332        *  an error is thrown.
1333       */
1334       basic_string&
1335       replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
1336       {
1337         _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1338                                  && __i2 <= _M_iend());
1339         return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
1340       }
1341
1342       /**
1343        *  @brief  Replace range of characters with range.
1344        *  @param i1  Iterator referencing start of range to replace.
1345        *  @param i2  Iterator referencing end of range to replace.
1346        *  @param k1  Iterator referencing start of range to insert.
1347        *  @param k2  Iterator referencing end of range to insert.
1348        *  @return  Reference to this string.
1349        *  @throw  std::length_error  If new length exceeds @c max_size().
1350        *
1351        *  Removes the characters in the range [i1,i2).  In place, characters
1352        *  in the range [k1,k2) are inserted.  If the length of result exceeds
1353        *  max_size(), length_error is thrown.  The value of the string doesn't
1354        *  change if an error is thrown.
1355       */
1356       template<class _InputIterator>
1357         basic_string&
1358         replace(iterator __i1, iterator __i2,
1359                 _InputIterator __k1, _InputIterator __k2)
1360         {
1361           _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1362                                    && __i2 <= _M_iend());
1363           __glibcxx_requires_valid_range(__k1, __k2);
1364           typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1365           return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
1366         }
1367
1368       // Specializations for the common case of pointer and iterator:
1369       // useful to avoid the overhead of temporary buffering in _M_replace.
1370       basic_string&
1371       replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
1372       {
1373         _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1374                                  && __i2 <= _M_iend());
1375         __glibcxx_requires_valid_range(__k1, __k2);
1376         return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1377                              __k1, __k2 - __k1);
1378       }
1379
1380       basic_string&
1381       replace(iterator __i1, iterator __i2,
1382               const _CharT* __k1, const _CharT* __k2)
1383       {
1384         _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1385                                  && __i2 <= _M_iend());
1386         __glibcxx_requires_valid_range(__k1, __k2);
1387         return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1388                              __k1, __k2 - __k1);
1389       }
1390
1391       basic_string&
1392       replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
1393       {
1394         _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1395                                  && __i2 <= _M_iend());
1396         __glibcxx_requires_valid_range(__k1, __k2);
1397         return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1398                              __k1.base(), __k2 - __k1);
1399       }
1400
1401       basic_string&
1402       replace(iterator __i1, iterator __i2,
1403               const_iterator __k1, const_iterator __k2)
1404       {
1405         _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1406                                  && __i2 <= _M_iend());
1407         __glibcxx_requires_valid_range(__k1, __k2);
1408         return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1409                              __k1.base(), __k2 - __k1);
1410       }
1411       
1412     private:
1413       template<class _Integer>
1414         basic_string&
1415         _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
1416                             _Integer __val, __true_type)
1417         { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
1418
1419       template<class _InputIterator>
1420         basic_string&
1421         _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
1422                             _InputIterator __k2, __false_type);
1423
1424       basic_string&
1425       _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
1426                      _CharT __c);
1427
1428       basic_string&
1429       _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
1430                       size_type __n2);
1431
1432       // _S_construct_aux is used to implement the 21.3.1 para 15 which
1433       // requires special behaviour if _InIter is an integral type
1434       template<class _InIterator>
1435         static _CharT*
1436         _S_construct_aux(_InIterator __beg, _InIterator __end,
1437                          const _Alloc& __a, __false_type)
1438         {
1439           typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
1440           return _S_construct(__beg, __end, __a, _Tag());
1441         }
1442
1443       template<class _InIterator>
1444         static _CharT*
1445         _S_construct_aux(_InIterator __beg, _InIterator __end,
1446                          const _Alloc& __a, __true_type)
1447         { return _S_construct(static_cast<size_type>(__beg),
1448                               static_cast<value_type>(__end), __a); }
1449
1450       template<class _InIterator>
1451         static _CharT*
1452         _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
1453         {
1454           typedef typename std::__is_integer<_InIterator>::__type _Integral;
1455           return _S_construct_aux(__beg, __end, __a, _Integral());
1456         }
1457
1458       // For Input Iterators, used in istreambuf_iterators, etc.
1459       template<class _InIterator>
1460         static _CharT*
1461          _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1462                       input_iterator_tag);
1463
1464       // For forward_iterators up to random_access_iterators, used for
1465       // string::iterator, _CharT*, etc.
1466       template<class _FwdIterator>
1467         static _CharT*
1468         _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
1469                      forward_iterator_tag);
1470
1471       static _CharT*
1472       _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
1473
1474     public:
1475
1476       /**
1477        *  @brief  Copy substring into C string.
1478        *  @param s  C string to copy value into.
1479        *  @param n  Number of characters to copy.
1480        *  @param pos  Index of first character to copy.
1481        *  @return  Number of characters actually copied
1482        *  @throw  std::out_of_range  If pos > size().
1483        *
1484        *  Copies up to @a n characters starting at @a pos into the C string @a
1485        *  s.  If @a pos is greater than size(), out_of_range is thrown.
1486       */
1487       size_type
1488       copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
1489
1490       /**
1491        *  @brief  Swap contents with another string.
1492        *  @param s  String to swap with.
1493        *
1494        *  Exchanges the contents of this string with that of @a s in constant
1495        *  time.
1496       */
1497       void
1498       swap(basic_string& __s);
1499
1500       // String operations:
1501       /**
1502        *  @brief  Return const pointer to null-terminated contents.
1503        *
1504        *  This is a handle to internal data.  Do not modify or dire things may
1505        *  happen.
1506       */
1507       const _CharT*
1508       c_str() const
1509       { return _M_data(); }
1510
1511       /**
1512        *  @brief  Return const pointer to contents.
1513        *
1514        *  This is a handle to internal data.  Do not modify or dire things may
1515        *  happen.
1516       */
1517       const _CharT*
1518       data() const
1519       { return _M_data(); }
1520
1521       /**
1522        *  @brief  Return copy of allocator used to construct this string.
1523       */
1524       allocator_type
1525       get_allocator() const
1526       { return _M_dataplus; }
1527
1528       /**
1529        *  @brief  Find position of a C substring.
1530        *  @param s  C string to locate.
1531        *  @param pos  Index of character to search from.
1532        *  @param n  Number of characters from @a s to search for.
1533        *  @return  Index of start of first occurrence.
1534        *
1535        *  Starting from @a pos, searches forward for the first @a n characters
1536        *  in @a s within this string.  If found, returns the index where it
1537        *  begins.  If not found, returns npos.
1538       */
1539       size_type
1540       find(const _CharT* __s, size_type __pos, size_type __n) const;
1541
1542       /**
1543        *  @brief  Find position of a string.
1544        *  @param str  String to locate.
1545        *  @param pos  Index of character to search from (default 0).
1546        *  @return  Index of start of first occurrence.
1547        *
1548        *  Starting from @a pos, searches forward for value of @a str within
1549        *  this string.  If found, returns the index where it begins.  If not
1550        *  found, returns npos.
1551       */
1552       size_type
1553       find(const basic_string& __str, size_type __pos = 0) const
1554       { return this->find(__str.data(), __pos, __str.size()); }
1555
1556       /**
1557        *  @brief  Find position of a C string.
1558        *  @param s  C string to locate.
1559        *  @param pos  Index of character to search from (default 0).
1560        *  @return  Index of start of first occurrence.
1561        *
1562        *  Starting from @a pos, searches forward for the value of @a s within
1563        *  this string.  If found, returns the index where it begins.  If not
1564        *  found, returns npos.
1565       */
1566       size_type
1567       find(const _CharT* __s, size_type __pos = 0) const
1568       {
1569         __glibcxx_requires_string(__s);
1570         return this->find(__s, __pos, traits_type::length(__s));
1571       }
1572
1573       /**
1574        *  @brief  Find position of a character.
1575        *  @param c  Character to locate.
1576        *  @param pos  Index of character to search from (default 0).
1577        *  @return  Index of first occurrence.
1578        *
1579        *  Starting from @a pos, searches forward for @a c within this string.
1580        *  If found, returns the index where it was found.  If not found,
1581        *  returns npos.
1582       */
1583       size_type
1584       find(_CharT __c, size_type __pos = 0) const;
1585
1586       /**
1587        *  @brief  Find last position of a string.
1588        *  @param str  String to locate.
1589        *  @param pos  Index of character to search back from (default end).
1590        *  @return  Index of start of last occurrence.
1591        *
1592        *  Starting from @a pos, searches backward for value of @a str within
1593        *  this string.  If found, returns the index where it begins.  If not
1594        *  found, returns npos.
1595       */
1596       size_type
1597       rfind(const basic_string& __str, size_type __pos = npos) const
1598       { return this->rfind(__str.data(), __pos, __str.size()); }
1599
1600       /**
1601        *  @brief  Find last position of a C substring.
1602        *  @param s  C string to locate.
1603        *  @param pos  Index of character to search back from.
1604        *  @param n  Number of characters from s to search for.
1605        *  @return  Index of start of last occurrence.
1606        *
1607        *  Starting from @a pos, searches backward for the first @a n
1608        *  characters in @a s within this string.  If found, returns the index
1609        *  where it begins.  If not found, returns npos.
1610       */
1611       size_type
1612       rfind(const _CharT* __s, size_type __pos, size_type __n) const;
1613
1614       /**
1615        *  @brief  Find last position of a C string.
1616        *  @param s  C string to locate.
1617        *  @param pos  Index of character to start search at (default 0).
1618        *  @return  Index of start of  last occurrence.
1619        *
1620        *  Starting from @a pos, searches backward for the value of @a s within
1621        *  this string.  If found, returns the index where it begins.  If not
1622        *  found, returns npos.
1623       */
1624       size_type
1625       rfind(const _CharT* __s, size_type __pos = npos) const
1626       {
1627         __glibcxx_requires_string(__s);
1628         return this->rfind(__s, __pos, traits_type::length(__s));
1629       }
1630
1631       /**
1632        *  @brief  Find last position of a character.
1633        *  @param c  Character to locate.
1634        *  @param pos  Index of character to search back from (default 0).
1635        *  @return  Index of last occurrence.
1636        *
1637        *  Starting from @a pos, searches backward for @a c within this string.
1638        *  If found, returns the index where it was found.  If not found,
1639        *  returns npos.
1640       */
1641       size_type
1642       rfind(_CharT __c, size_type __pos = npos) const;
1643
1644       /**
1645        *  @brief  Find position of a character of string.
1646        *  @param str  String containing characters to locate.
1647        *  @param pos  Index of character to search from (default 0).
1648        *  @return  Index of first occurrence.
1649        *
1650        *  Starting from @a pos, searches forward for one of the characters of
1651        *  @a str within this string.  If found, returns the index where it was
1652        *  found.  If not found, returns npos.
1653       */
1654       size_type
1655       find_first_of(const basic_string& __str, size_type __pos = 0) const
1656       { return this->find_first_of(__str.data(), __pos, __str.size()); }
1657
1658       /**
1659        *  @brief  Find position of a character of C substring.
1660        *  @param s  String containing characters to locate.
1661        *  @param pos  Index of character to search from (default 0).
1662        *  @param n  Number of characters from s to search for.
1663        *  @return  Index of first occurrence.
1664        *
1665        *  Starting from @a pos, searches forward for one of the first @a n
1666        *  characters of @a s within this string.  If found, returns the index
1667        *  where it was found.  If not found, returns npos.
1668       */
1669       size_type
1670       find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
1671
1672       /**
1673        *  @brief  Find position of a character of C string.
1674        *  @param s  String containing characters to locate.
1675        *  @param pos  Index of character to search from (default 0).
1676        *  @return  Index of first occurrence.
1677        *
1678        *  Starting from @a pos, searches forward for one of the characters of
1679        *  @a s within this string.  If found, returns the index where it was
1680        *  found.  If not found, returns npos.
1681       */
1682       size_type
1683       find_first_of(const _CharT* __s, size_type __pos = 0) const
1684       {
1685         __glibcxx_requires_string(__s);
1686         return this->find_first_of(__s, __pos, traits_type::length(__s));
1687       }
1688
1689       /**
1690        *  @brief  Find position of a character.
1691        *  @param c  Character to locate.
1692        *  @param pos  Index of character to search from (default 0).
1693        *  @return  Index of first occurrence.
1694        *
1695        *  Starting from @a pos, searches forward for the character @a c within
1696        *  this string.  If found, returns the index where it was found.  If
1697        *  not found, returns npos.
1698        *
1699        *  Note: equivalent to find(c, pos).
1700       */
1701       size_type
1702       find_first_of(_CharT __c, size_type __pos = 0) const
1703       { return this->find(__c, __pos); }
1704
1705       /**
1706        *  @brief  Find last position of a character of string.
1707        *  @param str  String containing characters to locate.
1708        *  @param pos  Index of character to search back from (default end).
1709        *  @return  Index of last occurrence.
1710        *
1711        *  Starting from @a pos, searches backward for one of the characters of
1712        *  @a str within this string.  If found, returns the index where it was
1713        *  found.  If not found, returns npos.
1714       */
1715       size_type
1716       find_last_of(const basic_string& __str, size_type __pos = npos) const
1717       { return this->find_last_of(__str.data(), __pos, __str.size()); }
1718
1719       /**
1720        *  @brief  Find last position of a character of C substring.
1721        *  @param s  C string containing characters to locate.
1722        *  @param pos  Index of character to search back from (default end).
1723        *  @param n  Number of characters from s to search for.
1724        *  @return  Index of last occurrence.
1725        *
1726        *  Starting from @a pos, searches backward for one of the first @a n
1727        *  characters of @a s within this string.  If found, returns the index
1728        *  where it was found.  If not found, returns npos.
1729       */
1730       size_type
1731       find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
1732
1733       /**
1734        *  @brief  Find last position of a character of C string.
1735        *  @param s  C string containing characters to locate.
1736        *  @param pos  Index of character to search back from (default end).
1737        *  @return  Index of last occurrence.
1738        *
1739        *  Starting from @a pos, searches backward for one of the characters of
1740        *  @a s within this string.  If found, returns the index where it was
1741        *  found.  If not found, returns npos.
1742       */
1743       size_type
1744       find_last_of(const _CharT* __s, size_type __pos = npos) const
1745       {
1746         __glibcxx_requires_string(__s);
1747         return this->find_last_of(__s, __pos, traits_type::length(__s));
1748       }
1749
1750       /**
1751        *  @brief  Find last position of a character.
1752        *  @param c  Character to locate.
1753        *  @param pos  Index of character to search back from (default 0).
1754        *  @return  Index of last occurrence.
1755        *
1756        *  Starting from @a pos, searches backward for @a c within this string.
1757        *  If found, returns the index where it was found.  If not found,
1758        *  returns npos.
1759        *
1760        *  Note: equivalent to rfind(c, pos).
1761       */
1762       size_type
1763       find_last_of(_CharT __c, size_type __pos = npos) const
1764       { return this->rfind(__c, __pos); }
1765
1766       /**
1767        *  @brief  Find position of a character not in string.
1768        *  @param str  String containing characters to avoid.
1769        *  @param pos  Index of character to search from (default 0).
1770        *  @return  Index of first occurrence.
1771        *
1772        *  Starting from @a pos, searches forward for a character not contained
1773        *  in @a str within this string.  If found, returns the index where it
1774        *  was found.  If not found, returns npos.
1775       */
1776       size_type
1777       find_first_not_of(const basic_string& __str, size_type __pos = 0) const
1778       { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
1779
1780       /**
1781        *  @brief  Find position of a character not in C substring.
1782        *  @param s  C string containing characters to avoid.
1783        *  @param pos  Index of character to search from (default 0).
1784        *  @param n  Number of characters from s to consider.
1785        *  @return  Index of first occurrence.
1786        *
1787        *  Starting from @a pos, searches forward for a character not contained
1788        *  in the first @a n characters of @a s within this string.  If found,
1789        *  returns the index where it was found.  If not found, returns npos.
1790       */
1791       size_type
1792       find_first_not_of(const _CharT* __s, size_type __pos,
1793                         size_type __n) const;
1794
1795       /**
1796        *  @brief  Find position of a character not in C string.
1797        *  @param s  C string containing characters to avoid.
1798        *  @param pos  Index of character to search from (default 0).
1799        *  @return  Index of first occurrence.
1800        *
1801        *  Starting from @a pos, searches forward for a character not contained
1802        *  in @a s within this string.  If found, returns the index where it
1803        *  was found.  If not found, returns npos.
1804       */
1805       size_type
1806       find_first_not_of(const _CharT* __s, size_type __pos = 0) const
1807       {
1808         __glibcxx_requires_string(__s);
1809         return this->find_first_not_of(__s, __pos, traits_type::length(__s));
1810       }
1811
1812       /**
1813        *  @brief  Find position of a different character.
1814        *  @param c  Character to avoid.
1815        *  @param pos  Index of character to search from (default 0).
1816        *  @return  Index of first occurrence.
1817        *
1818        *  Starting from @a pos, searches forward for a character other than @a c
1819        *  within this string.  If found, returns the index where it was found.
1820        *  If not found, returns npos.
1821       */
1822       size_type
1823       find_first_not_of(_CharT __c, size_type __pos = 0) const;
1824
1825       /**
1826        *  @brief  Find last position of a character not in string.
1827        *  @param str  String containing characters to avoid.
1828        *  @param pos  Index of character to search from (default 0).
1829        *  @return  Index of first occurrence.
1830        *
1831        *  Starting from @a pos, searches backward for a character not
1832        *  contained in @a str within this string.  If found, returns the index
1833        *  where it was found.  If not found, returns npos.
1834       */
1835       size_type
1836       find_last_not_of(const basic_string& __str, size_type __pos = npos) const
1837       { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
1838
1839       /**
1840        *  @brief  Find last position of a character not in C substring.
1841        *  @param s  C string containing characters to avoid.
1842        *  @param pos  Index of character to search from (default 0).
1843        *  @param n  Number of characters from s to consider.
1844        *  @return  Index of first occurrence.
1845        *
1846        *  Starting from @a pos, searches backward for a character not
1847        *  contained in the first @a n characters of @a s within this string.
1848        *  If found, returns the index where it was found.  If not found,
1849        *  returns npos.
1850       */
1851       size_type
1852       find_last_not_of(const _CharT* __s, size_type __pos,
1853                        size_type __n) const;
1854       /**
1855        *  @brief  Find position of a character not in C string.
1856        *  @param s  C string containing characters to avoid.
1857        *  @param pos  Index of character to search from (default 0).
1858        *  @return  Index of first occurrence.
1859        *
1860        *  Starting from @a pos, searches backward for a character not
1861        *  contained in @a s within this string.  If found, returns the index
1862        *  where it was found.  If not found, returns npos.
1863       */
1864       size_type
1865       find_last_not_of(const _CharT* __s, size_type __pos = npos) const
1866       {
1867         __glibcxx_requires_string(__s);
1868         return this->find_last_not_of(__s, __pos, traits_type::length(__s));
1869       }
1870
1871       /**
1872        *  @brief  Find last position of a different character.
1873        *  @param c  Character to avoid.
1874        *  @param pos  Index of character to search from (default 0).
1875        *  @return  Index of first occurrence.
1876        *
1877        *  Starting from @a pos, searches backward for a character other than
1878        *  @a c within this string.  If found, returns the index where it was
1879        *  found.  If not found, returns npos.
1880       */
1881       size_type
1882       find_last_not_of(_CharT __c, size_type __pos = npos) const;
1883
1884       /**
1885        *  @brief  Get a substring.
1886        *  @param pos  Index of first character (default 0).
1887        *  @param n  Number of characters in substring (default remainder).
1888        *  @return  The new string.
1889        *  @throw  std::out_of_range  If pos > size().
1890        *
1891        *  Construct and return a new string using the @a n characters starting
1892        *  at @a pos.  If the string is too short, use the remainder of the
1893        *  characters.  If @a pos is beyond the end of the string, out_of_range
1894        *  is thrown.
1895       */
1896       basic_string
1897       substr(size_type __pos = 0, size_type __n = npos) const
1898       { return basic_string(*this,
1899                             _M_check(__pos, "basic_string::substr"), __n); }
1900
1901       /**
1902        *  @brief  Compare to a string.
1903        *  @param str  String to compare against.
1904        *  @return  Integer < 0, 0, or > 0.
1905        *
1906        *  Returns an integer < 0 if this string is ordered before @a str, 0 if
1907        *  their values are equivalent, or > 0 if this string is ordered after
1908        *  @a str.  Determines the effective length rlen of the strings to
1909        *  compare as the smallest of size() and str.size().  The function
1910        *  then compares the two strings by calling traits::compare(data(),
1911        *  str.data(),rlen).  If the result of the comparison is nonzero returns
1912        *  it, otherwise the shorter one is ordered first.
1913       */
1914       int
1915       compare(const basic_string& __str) const
1916       {
1917         const size_type __size = this->size();
1918         const size_type __osize = __str.size();
1919         const size_type __len = std::min(__size, __osize);
1920
1921         int __r = traits_type::compare(_M_data(), __str.data(), __len);
1922         if (!__r)
1923           __r =  __size - __osize;
1924         return __r;
1925       }
1926
1927       /**
1928        *  @brief  Compare substring to a string.
1929        *  @param pos  Index of first character of substring.
1930        *  @param n  Number of characters in substring.
1931        *  @param str  String to compare against.
1932        *  @return  Integer < 0, 0, or > 0.
1933        *
1934        *  Form the substring of this string from the @a n characters starting
1935        *  at @a pos.  Returns an integer < 0 if the substring is ordered
1936        *  before @a str, 0 if their values are equivalent, or > 0 if the
1937        *  substring is ordered after @a str.  Determines the effective length
1938        *  rlen of the strings to compare as the smallest of the length of the
1939        *  substring and @a str.size().  The function then compares the two
1940        *  strings by calling traits::compare(substring.data(),str.data(),rlen).
1941        *  If the result of the comparison is nonzero returns it, otherwise the
1942        *  shorter one is ordered first.
1943       */
1944       int
1945       compare(size_type __pos, size_type __n, const basic_string& __str) const;
1946
1947       /**
1948        *  @brief  Compare substring to a substring.
1949        *  @param pos1  Index of first character of substring.
1950        *  @param n1  Number of characters in substring.
1951        *  @param str  String to compare against.
1952        *  @param pos2  Index of first character of substring of str.
1953        *  @param n2  Number of characters in substring of str.
1954        *  @return  Integer < 0, 0, or > 0.
1955        *
1956        *  Form the substring of this string from the @a n1 characters starting
1957        *  at @a pos1.  Form the substring of @a str from the @a n2 characters
1958        *  starting at @a pos2.  Returns an integer < 0 if this substring is
1959        *  ordered before the substring of @a str, 0 if their values are
1960        *  equivalent, or > 0 if this substring is ordered after the substring
1961        *  of @a str.  Determines the effective length rlen of the strings
1962        *  to compare as the smallest of the lengths of the substrings.  The
1963        *  function then compares the two strings by calling
1964        *  traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
1965        *  If the result of the comparison is nonzero returns it, otherwise the
1966        *  shorter one is ordered first.
1967       */
1968       int
1969       compare(size_type __pos1, size_type __n1, const basic_string& __str,
1970               size_type __pos2, size_type __n2) const;
1971
1972       /**
1973        *  @brief  Compare to a C string.
1974        *  @param s  C string to compare against.
1975        *  @return  Integer < 0, 0, or > 0.
1976        *
1977        *  Returns an integer < 0 if this string is ordered before @a s, 0 if
1978        *  their values are equivalent, or > 0 if this string is ordered after
1979        *  @a s.  Determines the effective length rlen of the strings to
1980        *  compare as the smallest of size() and the length of a string
1981        *  constructed from @a s.  The function then compares the two strings
1982        *  by calling traits::compare(data(),s,rlen).  If the result of the
1983        *  comparison is nonzero returns it, otherwise the shorter one is
1984        *  ordered first.
1985       */
1986       int
1987       compare(const _CharT* __s) const;
1988
1989       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1990       // 5 String::compare specification questionable
1991       /**
1992        *  @brief  Compare substring to a C string.
1993        *  @param pos  Index of first character of substring.
1994        *  @param n1  Number of characters in substring.
1995        *  @param s  C string to compare against.
1996        *  @return  Integer < 0, 0, or > 0.
1997        *
1998        *  Form the substring of this string from the @a n1 characters starting
1999        *  at @a pos.  Returns an integer < 0 if the substring is ordered
2000        *  before @a s, 0 if their values are equivalent, or > 0 if the
2001        *  substring is ordered after @a s.  Determines the effective length
2002        *  rlen of the strings to compare as the smallest of the length of the 
2003        *  substring and the length of a string constructed from @a s.  The
2004        *  function then compares the two string by calling
2005        *  traits::compare(substring.data(),s,rlen).  If the result of the
2006        *  comparison is nonzero returns it, otherwise the shorter one is
2007        *  ordered first.
2008       */
2009       int
2010       compare(size_type __pos, size_type __n1, const _CharT* __s) const;
2011
2012       /**
2013        *  @brief  Compare substring against a character array.
2014        *  @param pos1  Index of first character of substring.
2015        *  @param n1  Number of characters in substring.
2016        *  @param s  character array to compare against.
2017        *  @param n2  Number of characters of s.
2018        *  @return  Integer < 0, 0, or > 0.
2019        *
2020        *  Form the substring of this string from the @a n1 characters starting
2021        *  at @a pos1.  Form a string from the first @a n2 characters of @a s.
2022        *  Returns an integer < 0 if this substring is ordered before the string
2023        *  from @a s, 0 if their values are equivalent, or > 0 if this substring
2024        *  is ordered after the string from @a s.   Determines the effective
2025        *  length rlen of the strings to compare as the smallest of the length
2026        *  of the substring and @a n2.  The function then compares the two
2027        *  strings by calling traits::compare(substring.data(),s,rlen).  If the
2028        *  result of the comparison is nonzero returns it, otherwise the shorter
2029        *  one is ordered first.
2030        *
2031        *  NB: s must have at least n2 characters, '\0' has no special
2032        *  meaning.
2033       */
2034       int
2035       compare(size_type __pos, size_type __n1, const _CharT* __s,
2036               size_type __n2) const;
2037   };
2038
2039   template<typename _CharT, typename _Traits, typename _Alloc>
2040     inline basic_string<_CharT, _Traits, _Alloc>::
2041     basic_string()
2042 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
2043     : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
2044 #else
2045     : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()) { }
2046 #endif
2047
2048   // operator+
2049   /**
2050    *  @brief  Concatenate two strings.
2051    *  @param lhs  First string.
2052    *  @param rhs  Last string.
2053    *  @return  New string with value of @a lhs followed by @a rhs.
2054    */
2055   template<typename _CharT, typename _Traits, typename _Alloc>
2056     basic_string<_CharT, _Traits, _Alloc>
2057     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2058               const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2059     {
2060       basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2061       __str.append(__rhs);
2062       return __str;
2063     }
2064
2065   /**
2066    *  @brief  Concatenate C string and string.
2067    *  @param lhs  First string.
2068    *  @param rhs  Last string.
2069    *  @return  New string with value of @a lhs followed by @a rhs.
2070    */
2071   template<typename _CharT, typename _Traits, typename _Alloc>
2072     basic_string<_CharT,_Traits,_Alloc>
2073     operator+(const _CharT* __lhs,
2074               const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2075
2076   /**
2077    *  @brief  Concatenate character and string.
2078    *  @param lhs  First string.
2079    *  @param rhs  Last string.
2080    *  @return  New string with @a lhs followed by @a rhs.
2081    */
2082   template<typename _CharT, typename _Traits, typename _Alloc>
2083     basic_string<_CharT,_Traits,_Alloc>
2084     operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2085
2086   /**
2087    *  @brief  Concatenate string and C string.
2088    *  @param lhs  First string.
2089    *  @param rhs  Last string.
2090    *  @return  New string with @a lhs followed by @a rhs.
2091    */
2092   template<typename _CharT, typename _Traits, typename _Alloc>
2093     inline basic_string<_CharT, _Traits, _Alloc>
2094     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2095              const _CharT* __rhs)
2096     {
2097       basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
2098       __str.append(__rhs);
2099       return __str;
2100     }
2101
2102   /**
2103    *  @brief  Concatenate string and character.
2104    *  @param lhs  First string.
2105    *  @param rhs  Last string.
2106    *  @return  New string with @a lhs followed by @a rhs.
2107    */
2108   template<typename _CharT, typename _Traits, typename _Alloc>
2109     inline basic_string<_CharT, _Traits, _Alloc>
2110     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
2111     {
2112       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
2113       typedef typename __string_type::size_type         __size_type;
2114       __string_type __str(__lhs);
2115       __str.append(__size_type(1), __rhs);
2116       return __str;
2117     }
2118
2119   // operator ==
2120   /**
2121    *  @brief  Test equivalence of two strings.
2122    *  @param lhs  First string.
2123    *  @param rhs  Second string.
2124    *  @return  True if @a lhs.compare(@a rhs) == 0.  False otherwise.
2125    */
2126   template<typename _CharT, typename _Traits, typename _Alloc>
2127     inline bool
2128     operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2129                const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2130     { return __lhs.compare(__rhs) == 0; }
2131
2132   /**
2133    *  @brief  Test equivalence of C string and string.
2134    *  @param lhs  C string.
2135    *  @param rhs  String.
2136    *  @return  True if @a rhs.compare(@a lhs) == 0.  False otherwise.
2137    */
2138   template<typename _CharT, typename _Traits, typename _Alloc>
2139     inline bool
2140     operator==(const _CharT* __lhs,
2141                const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2142     { return __rhs.compare(__lhs) == 0; }
2143
2144   /**
2145    *  @brief  Test equivalence of string and C string.
2146    *  @param lhs  String.
2147    *  @param rhs  C string.
2148    *  @return  True if @a lhs.compare(@a rhs) == 0.  False otherwise.
2149    */
2150   template<typename _CharT, typename _Traits, typename _Alloc>
2151     inline bool
2152     operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2153                const _CharT* __rhs)
2154     { return __lhs.compare(__rhs) == 0; }
2155
2156   // operator !=
2157   /**
2158    *  @brief  Test difference of two strings.
2159    *  @param lhs  First string.
2160    *  @param rhs  Second string.
2161    *  @return  True if @a lhs.compare(@a rhs) != 0.  False otherwise.
2162    */
2163   template<typename _CharT, typename _Traits, typename _Alloc>
2164     inline bool
2165     operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2166                const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2167     { return __rhs.compare(__lhs) != 0; }
2168
2169   /**
2170    *  @brief  Test difference of C string and string.
2171    *  @param lhs  C string.
2172    *  @param rhs  String.
2173    *  @return  True if @a rhs.compare(@a lhs) != 0.  False otherwise.
2174    */
2175   template<typename _CharT, typename _Traits, typename _Alloc>
2176     inline bool
2177     operator!=(const _CharT* __lhs,
2178                const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2179     { return __rhs.compare(__lhs) != 0; }
2180
2181   /**
2182    *  @brief  Test difference of string and C string.
2183    *  @param lhs  String.
2184    *  @param rhs  C string.
2185    *  @return  True if @a lhs.compare(@a rhs) != 0.  False otherwise.
2186    */
2187   template<typename _CharT, typename _Traits, typename _Alloc>
2188     inline bool
2189     operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2190                const _CharT* __rhs)
2191     { return __lhs.compare(__rhs) != 0; }
2192
2193   // operator <
2194   /**
2195    *  @brief  Test if string precedes string.
2196    *  @param lhs  First string.
2197    *  @param rhs  Second string.
2198    *  @return  True if @a lhs precedes @a rhs.  False otherwise.
2199    */
2200   template<typename _CharT, typename _Traits, typename _Alloc>
2201     inline bool
2202     operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2203               const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2204     { return __lhs.compare(__rhs) < 0; }
2205
2206   /**
2207    *  @brief  Test if string precedes C string.
2208    *  @param lhs  String.
2209    *  @param rhs  C string.
2210    *  @return  True if @a lhs precedes @a rhs.  False otherwise.
2211    */
2212   template<typename _CharT, typename _Traits, typename _Alloc>
2213     inline bool
2214     operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2215               const _CharT* __rhs)
2216     { return __lhs.compare(__rhs) < 0; }
2217
2218   /**
2219    *  @brief  Test if C string precedes string.
2220    *  @param lhs  C string.
2221    *  @param rhs  String.
2222    *  @return  True if @a lhs precedes @a rhs.  False otherwise.
2223    */
2224   template<typename _CharT, typename _Traits, typename _Alloc>
2225     inline bool
2226     operator<(const _CharT* __lhs,
2227               const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2228     { return __rhs.compare(__lhs) > 0; }
2229
2230   // operator >
2231   /**
2232    *  @brief  Test if string follows string.
2233    *  @param lhs  First string.
2234    *  @param rhs  Second string.
2235    *  @return  True if @a lhs follows @a rhs.  False otherwise.
2236    */
2237   template<typename _CharT, typename _Traits, typename _Alloc>
2238     inline bool
2239     operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2240               const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2241     { return __lhs.compare(__rhs) > 0; }
2242
2243   /**
2244    *  @brief  Test if string follows C string.
2245    *  @param lhs  String.
2246    *  @param rhs  C string.
2247    *  @return  True if @a lhs follows @a rhs.  False otherwise.
2248    */
2249   template<typename _CharT, typename _Traits, typename _Alloc>
2250     inline bool
2251     operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2252               const _CharT* __rhs)
2253     { return __lhs.compare(__rhs) > 0; }
2254
2255   /**
2256    *  @brief  Test if C string follows string.
2257    *  @param lhs  C string.
2258    *  @param rhs  String.
2259    *  @return  True if @a lhs follows @a rhs.  False otherwise.
2260    */
2261   template<typename _CharT, typename _Traits, typename _Alloc>
2262     inline bool
2263     operator>(const _CharT* __lhs,
2264               const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2265     { return __rhs.compare(__lhs) < 0; }
2266
2267   // operator <=
2268   /**
2269    *  @brief  Test if string doesn't follow string.
2270    *  @param lhs  First string.
2271    *  @param rhs  Second string.
2272    *  @return  True if @a lhs doesn't follow @a rhs.  False otherwise.
2273    */
2274   template<typename _CharT, typename _Traits, typename _Alloc>
2275     inline bool
2276     operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2277                const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2278     { return __lhs.compare(__rhs) <= 0; }
2279
2280   /**
2281    *  @brief  Test if string doesn't follow C string.
2282    *  @param lhs  String.
2283    *  @param rhs  C string.
2284    *  @return  True if @a lhs doesn't follow @a rhs.  False otherwise.
2285    */
2286   template<typename _CharT, typename _Traits, typename _Alloc>
2287     inline bool
2288     operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2289                const _CharT* __rhs)
2290     { return __lhs.compare(__rhs) <= 0; }
2291
2292   /**
2293    *  @brief  Test if C string doesn't follow string.
2294    *  @param lhs  C string.
2295    *  @param rhs  String.
2296    *  @return  True if @a lhs doesn't follow @a rhs.  False otherwise.
2297    */
2298   template<typename _CharT, typename _Traits, typename _Alloc>
2299     inline bool
2300     operator<=(const _CharT* __lhs,
2301                const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2302     { return __rhs.compare(__lhs) >= 0; }
2303
2304   // operator >=
2305   /**
2306    *  @brief  Test if string doesn't precede string.
2307    *  @param lhs  First string.
2308    *  @param rhs  Second string.
2309    *  @return  True if @a lhs doesn't precede @a rhs.  False otherwise.
2310    */
2311   template<typename _CharT, typename _Traits, typename _Alloc>
2312     inline bool
2313     operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2314                const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2315     { return __lhs.compare(__rhs) >= 0; }
2316
2317   /**
2318    *  @brief  Test if string doesn't precede C string.
2319    *  @param lhs  String.
2320    *  @param rhs  C string.
2321    *  @return  True if @a lhs doesn't precede @a rhs.  False otherwise.
2322    */
2323   template<typename _CharT, typename _Traits, typename _Alloc>
2324     inline bool
2325     operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2326                const _CharT* __rhs)
2327     { return __lhs.compare(__rhs) >= 0; }
2328
2329   /**
2330    *  @brief  Test if C string doesn't precede string.
2331    *  @param lhs  C string.
2332    *  @param rhs  String.
2333    *  @return  True if @a lhs doesn't precede @a rhs.  False otherwise.
2334    */
2335   template<typename _CharT, typename _Traits, typename _Alloc>
2336     inline bool
2337     operator>=(const _CharT* __lhs,
2338              const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2339     { return __rhs.compare(__lhs) <= 0; }
2340
2341   /**
2342    *  @brief  Swap contents of two strings.
2343    *  @param lhs  First string.
2344    *  @param rhs  Second string.
2345    *
2346    *  Exchanges the contents of @a lhs and @a rhs in constant time.
2347    */
2348   template<typename _CharT, typename _Traits, typename _Alloc>
2349     inline void
2350     swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
2351          basic_string<_CharT, _Traits, _Alloc>& __rhs)
2352     { __lhs.swap(__rhs); }
2353
2354   /**
2355    *  @brief  Read stream into a string.
2356    *  @param is  Input stream.
2357    *  @param str  Buffer to store into.
2358    *  @return  Reference to the input stream.
2359    *
2360    *  Stores characters from @a is into @a str until whitespace is found, the
2361    *  end of the stream is encountered, or str.max_size() is reached.  If
2362    *  is.width() is non-zero, that is the limit on the number of characters
2363    *  stored into @a str.  Any previous contents of @a str are erased.
2364    */
2365   template<typename _CharT, typename _Traits, typename _Alloc>
2366     basic_istream<_CharT, _Traits>&
2367     operator>>(basic_istream<_CharT, _Traits>& __is,
2368                basic_string<_CharT, _Traits, _Alloc>& __str);
2369
2370   /**
2371    *  @brief  Write string to a stream.
2372    *  @param os  Output stream.
2373    *  @param str  String to write out.
2374    *  @return  Reference to the output stream.
2375    *
2376    *  Output characters of @a str into os following the same rules as for
2377    *  writing a C string.
2378    */
2379   template<typename _CharT, typename _Traits, typename _Alloc>
2380     basic_ostream<_CharT, _Traits>&
2381     operator<<(basic_ostream<_CharT, _Traits>& __os,
2382                const basic_string<_CharT, _Traits, _Alloc>& __str);
2383
2384   /**
2385    *  @brief  Read a line from stream into a string.
2386    *  @param is  Input stream.
2387    *  @param str  Buffer to store into.
2388    *  @param delim  Character marking end of line.
2389    *  @return  Reference to the input stream.
2390    *
2391    *  Stores characters from @a is into @a str until @a delim is found, the
2392    *  end of the stream is encountered, or str.max_size() is reached.  If
2393    *  is.width() is non-zero, that is the limit on the number of characters
2394    *  stored into @a str.  Any previous contents of @a str are erased.  If @a
2395    *  delim was encountered, it is extracted but not stored into @a str.
2396    */
2397   template<typename _CharT, typename _Traits, typename _Alloc>
2398     basic_istream<_CharT, _Traits>&
2399     getline(basic_istream<_CharT, _Traits>& __is,
2400             basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
2401
2402   /**
2403    *  @brief  Read a line from stream into a string.
2404    *  @param is  Input stream.
2405    *  @param str  Buffer to store into.
2406    *  @return  Reference to the input stream.
2407    *
2408    *  Stores characters from is into @a str until '\n' is found, the end of
2409    *  the stream is encountered, or str.max_size() is reached.  If is.width()
2410    *  is non-zero, that is the limit on the number of characters stored into
2411    *  @a str.  Any previous contents of @a str are erased.  If end of line was
2412    *  encountered, it is extracted but not stored into @a str.
2413    */
2414   template<typename _CharT, typename _Traits, typename _Alloc>
2415     inline basic_istream<_CharT, _Traits>&
2416     getline(basic_istream<_CharT, _Traits>& __is,
2417             basic_string<_CharT, _Traits, _Alloc>& __str);
2418     
2419   template<>
2420     basic_istream<char>&
2421     getline(basic_istream<char>& __in, basic_string<char>& __str,
2422             char __delim);
2423
2424 #ifdef _GLIBCXX_USE_WCHAR_T
2425   template<>
2426     basic_istream<wchar_t>&
2427     getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
2428             wchar_t __delim);
2429 #endif  
2430 } // namespace std
2431
2432 #endif /* _BASIC_STRING_H */