OSDN Git Service

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