OSDN Git Service

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