OSDN Git Service

1a534a15b6506c57eeb9cdb1466e12a51da42048
[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 = _CharT())
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       /**
993        *  @brief  Remove characters.
994        *  @param pos  Index of first character to remove (default 0).
995        *  @param n  Number of characters to remove (default remainder).
996        *  @return  Reference to this string.
997        *  @throw  std::out_of_range  If @a pos is beyond the end of this
998        *  string.
999        *
1000        *  Removes @a n characters from this string starting at @a pos.  The
1001        *  length of the string is reduced by @a n.  If there are < @a n
1002        *  characters to remove, the remainder of the string is truncated.  If
1003        *  @a p is 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       basic_string&
1007       erase(size_type __pos = 0, size_type __n = npos)
1008       {
1009         return this->replace(_M_check(__pos), _M_fold(__pos, __n),
1010                              _M_data(), _M_data());
1011       }
1012
1013       /**
1014        *  @brief  Remove one character.
1015        *  @param position  Iterator referencing the character to remove.
1016        *  @return  iterator referencing same location after removal.
1017        *  @throw  std::out_of_range  If @a position is beyond the end of this
1018        *  string. 
1019        *
1020        *  Removes the character at @a position from this string.  If @a
1021        *  position is beyond end of string, out_of_range is thrown.  The value
1022        *  of the string doesn't change if an error is thrown.
1023       */
1024       iterator
1025       erase(iterator __position)
1026       {
1027         const size_type __i = __position - _M_ibegin();
1028         this->replace(__position, __position + 1, _M_data(), _M_data());
1029         _M_rep()->_M_set_leaked();
1030         return _M_ibegin() + __i;
1031       }
1032
1033       /**
1034        *  @brief  Remove a range of characters.
1035        *  @param first  Iterator referencing the first character to remove.
1036        *  @param last  Iterator referencing the end of the range.
1037        *  @return  Iterator referencing location of first after removal.
1038        *  @throw  std::out_of_range  If @a first is beyond the end of this
1039        *  string. 
1040        *
1041        *  Removes the characters in the range [first,last) from this string.
1042        *  If @a first is beyond end of string, out_of_range is thrown.  The
1043        *  value of the string doesn't change if an error is thrown.
1044       */
1045       iterator
1046       erase(iterator __first, iterator __last)
1047       {
1048         const size_type __i = __first - _M_ibegin();
1049         this->replace(__first, __last, _M_data(), _M_data());
1050         _M_rep()->_M_set_leaked();
1051         return _M_ibegin() + __i;
1052       }
1053
1054       /**
1055        *  @brief  Replace characters with value from another string.
1056        *  @param pos  Index of first character to replace.
1057        *  @param n  Number of characters to be replaced.
1058        *  @param str  String to insert.
1059        *  @return  Reference to this string.
1060        *  @throw  std::out_of_range  If @a pos is beyond the end of this
1061        *  string. 
1062        *  @throw  std::length_error  If new length exceeds @c max_size().
1063        *
1064        *  Removes the characters in the range [pos,pos+n) from this string.
1065        *  In place, the value of @a str is inserted.  If @a pos is beyond end
1066        *  of string, out_of_range is thrown.  If the length of the result
1067        *  exceeds max_size(), length_error is thrown.  The value of the string
1068        *  doesn't change if an error is thrown.
1069       */
1070       basic_string&
1071       replace(size_type __pos, size_type __n, const basic_string& __str)
1072       { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1073
1074       /**
1075        *  @brief  Replace characters with value from another string.
1076        *  @param pos1  Index of first character to replace.
1077        *  @param n1  Number of characters to be replaced.
1078        *  @param str  String to insert.
1079        *  @param pos2  Index of first character of str to use.
1080        *  @param n2  Number of characters from str to use.
1081        *  @return  Reference to this string.
1082        *  @throw  std::out_of_range  If @a pos1 > size() or @a pos2 >
1083        *  str.size(). 
1084        *  @throw  std::length_error  If new length exceeds @c max_size().
1085        *
1086        *  Removes the characters in the range [pos1,pos1 + n) from this
1087        *  string.  In place, the value of @a str is inserted.  If @a pos is
1088        *  beyond end of string, out_of_range is thrown.  If the length of the
1089        *  result exceeds max_size(), length_error is thrown.  The value of the
1090        *  string doesn't change if an error is thrown.
1091       */
1092       basic_string&
1093       replace(size_type __pos1, size_type __n1, const basic_string& __str,
1094               size_type __pos2, size_type __n2);
1095
1096       /**
1097        *  @brief  Replace characters with value of a C substring.
1098        *  @param pos  Index of first character to replace.
1099        *  @param n1  Number of characters to be replaced.
1100        *  @param str  C string to insert.
1101        *  @param n2  Number of characters from str to use.
1102        *  @return  Reference to this string.
1103        *  @throw  std::out_of_range  If @a pos1 > size().
1104        *  @throw  std::length_error  If new length exceeds @c max_size().
1105        *
1106        *  Removes the characters in the range [pos,pos + n1) from this string.
1107        *  In place, the first @a n2 characters of @a str are inserted, or all
1108        *  of @a str if @a n2 is too large.  If @a pos is beyond end of string,
1109        *  out_of_range is thrown.  If the length of result exceeds max_size(),
1110        *  length_error is thrown.  The value of the string doesn't change if
1111        *  an error is thrown.
1112       */
1113       basic_string&
1114       replace(size_type __pos, size_type __n1, const _CharT* __s,
1115               size_type __n2);
1116
1117       /**
1118        *  @brief  Replace characters with value of a C string.
1119        *  @param pos  Index of first character to replace.
1120        *  @param n1  Number of characters to be replaced.
1121        *  @param str  C string to insert.
1122        *  @return  Reference to this string.
1123        *  @throw  std::out_of_range  If @a pos > size().
1124        *  @throw  std::length_error  If new length exceeds @c max_size().
1125        *
1126        *  Removes the characters in the range [pos,pos + n1) from this string.
1127        *  In place, the first @a n characters of @a str are inserted.  If @a
1128        *  pos is beyond end of string, out_of_range is thrown.  If the length
1129        *  of result exceeds max_size(), length_error is thrown.  The value of
1130        *  the string doesn't change if an error is thrown.
1131       */
1132       basic_string&
1133       replace(size_type __pos, size_type __n1, const _CharT* __s)
1134       { return this->replace(__pos, __n1, __s, traits_type::length(__s)); }
1135
1136       /**
1137        *  @brief  Replace characters with multiple characters.
1138        *  @param pos  Index of first character to replace.
1139        *  @param n1  Number of characters to be replaced.
1140        *  @param n2  Number of characters to insert.
1141        *  @param c  Character to insert.
1142        *  @return  Reference to this string.
1143        *  @throw  std::out_of_range  If @a pos > size().
1144        *  @throw  std::length_error  If new length exceeds @c max_size().
1145        *
1146        *  Removes the characters in the range [pos,pos + n1) from this string.
1147        *  In place, @a n2 copies of @a c are inserted.  If @a pos is beyond
1148        *  end of string, out_of_range is thrown.  If the length of result
1149        *  exceeds max_size(), length_error is thrown.  The value of the string
1150        *  doesn't change if an error is thrown.
1151       */
1152       basic_string&
1153       replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1154       { return this->replace(_M_check(__pos), _M_fold(__pos, __n1), __n2, __c); }
1155
1156       /**
1157        *  @brief  Replace range of characters with string.
1158        *  @param i1  Iterator referencing start of range to replace.
1159        *  @param i2  Iterator referencing end of range to replace.
1160        *  @param str  String value to insert.
1161        *  @return  Reference to this string.
1162        *  @throw  std::length_error  If new length exceeds @c max_size().
1163        *
1164        *  Removes the characters in the range [i1,i2).  In place, the value of
1165        *  @a str is inserted.  If the length of result exceeds max_size(),
1166        *  length_error is thrown.  The value of the string doesn't change if
1167        *  an error is thrown.
1168       */
1169       basic_string&
1170       replace(iterator __i1, iterator __i2, const basic_string& __str)
1171       { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
1172
1173       /**
1174        *  @brief  Replace range of characters with C substring.
1175        *  @param i1  Iterator referencing start of range to replace.
1176        *  @param i2  Iterator referencing end of range to replace.
1177        *  @param s  C string value to insert.
1178        *  @param n  Number of characters from s to insert.
1179        *  @return  Reference to this string.
1180        *  @throw  std::length_error  If new length exceeds @c max_size().
1181        *
1182        *  Removes the characters in the range [i1,i2).  In place, the first @a
1183        *  n characters of @a s are inserted.  If the length of result exceeds
1184        *  max_size(), length_error is thrown.  The value of the string doesn't
1185        *  change if an error is thrown.
1186       */
1187       basic_string&
1188       replace(iterator __i1, iterator __i2,
1189                            const _CharT* __s, size_type __n)
1190       { return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n); }
1191
1192       /**
1193        *  @brief  Replace range of characters with C string.
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        *  @return  Reference to this string.
1198        *  @throw  std::length_error  If new length exceeds @c max_size().
1199        *
1200        *  Removes the characters in the range [i1,i2).  In place, the
1201        *  characters of @a s are inserted.  If the length of result exceeds
1202        *  max_size(), length_error is thrown.  The value of the string doesn't
1203        *  change if an error is thrown.
1204       */
1205       basic_string&
1206       replace(iterator __i1, iterator __i2, const _CharT* __s)
1207       { return this->replace(__i1, __i2, __s, traits_type::length(__s)); }
1208
1209       /**
1210        *  @brief  Replace range of characters with multiple characters
1211        *  @param i1  Iterator referencing start of range to replace.
1212        *  @param i2  Iterator referencing end of range to replace.
1213        *  @param n  Number of characters to insert.
1214        *  @param c  Character to insert.
1215        *  @return  Reference to this string.
1216        *  @throw  std::length_error  If new length exceeds @c max_size().
1217        *
1218        *  Removes the characters in the range [i1,i2).  In place, @a n copies
1219        *  of @a c are inserted.  If the length of result exceeds max_size(),
1220        *  length_error is thrown.  The value of the string doesn't change if
1221        *  an error is thrown.
1222       */
1223       basic_string&
1224       replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
1225       { return _M_replace_aux(__i1, __i2, __n, __c); }
1226
1227       /**
1228        *  @brief  Replace range of characters with range.
1229        *  @param i1  Iterator referencing start of range to replace.
1230        *  @param i2  Iterator referencing end of range to replace.
1231        *  @param k1  Iterator referencing start of range to insert.
1232        *  @param k2  Iterator referencing end of range to insert.
1233        *  @return  Reference to this string.
1234        *  @throw  std::length_error  If new length exceeds @c max_size().
1235        *
1236        *  Removes the characters in the range [i1,i2).  In place, characters
1237        *  in the range [k1,k2) are inserted.  If the length of result exceeds
1238        *  max_size(), length_error is thrown.  The value of the string doesn't
1239        *  change if an error is thrown.
1240       */
1241       template<class _InputIterator>
1242         basic_string&
1243         replace(iterator __i1, iterator __i2,
1244                 _InputIterator __k1, _InputIterator __k2)
1245         { typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
1246           return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral()); }
1247
1248       // Specializations for the common case of pointer and iterator:
1249       // useful to avoid the overhead of temporary buffering in _M_replace.
1250       basic_string&
1251       replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
1252         { return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1253                                __k1, __k2 - __k1); }
1254
1255       basic_string&
1256       replace(iterator __i1, iterator __i2, const _CharT* __k1, const _CharT* __k2)
1257         { return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1258                                __k1, __k2 - __k1); }
1259
1260       basic_string&
1261       replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
1262         { return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1263                                __k1.base(), __k2 - __k1);
1264         }
1265
1266       basic_string&
1267       replace(iterator __i1, iterator __i2, const_iterator __k1, const_iterator __k2)
1268         { return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1269                                __k1.base(), __k2 - __k1);
1270         }
1271
1272     private:
1273       template<class _Integer>
1274         basic_string&
1275         _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n, 
1276                             _Integer __val, __true_type)
1277         { return _M_replace_aux(__i1, __i2, __n, __val); }
1278
1279       template<class _InputIterator>
1280         basic_string&
1281         _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
1282                             _InputIterator __k2, __false_type)
1283         { 
1284           typedef typename iterator_traits<_InputIterator>::iterator_category
1285             _Category;
1286           return _M_replace(__i1, __i2, __k1, __k2, _Category());
1287         }
1288
1289       basic_string&
1290       _M_replace_aux(iterator __i1, iterator __i2, size_type __n2, _CharT __c);
1291
1292       template<class _InputIterator>
1293         basic_string&
1294         _M_replace(iterator __i1, iterator __i2, _InputIterator __k1,
1295                    _InputIterator __k2, input_iterator_tag);
1296
1297       template<class _ForwardIterator>
1298         basic_string&
1299         _M_replace_safe(iterator __i1, iterator __i2, _ForwardIterator __k1,
1300                    _ForwardIterator __k2);
1301
1302       // _S_construct_aux is used to implement the 21.3.1 para 15 which
1303       // requires special behaviour if _InIter is an integral type
1304       template<class _InIterator>
1305         static _CharT*
1306         _S_construct_aux(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1307                          __false_type)
1308         {
1309           typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
1310           return _S_construct(__beg, __end, __a, _Tag());
1311         }
1312
1313       template<class _InIterator>
1314         static _CharT*
1315         _S_construct_aux(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1316                          __true_type)
1317         {
1318           return _S_construct(static_cast<size_type>(__beg),
1319                               static_cast<value_type>(__end), __a);
1320         }
1321
1322       template<class _InIterator>
1323         static _CharT*
1324         _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
1325         {
1326           typedef typename _Is_integer<_InIterator>::_Integral _Integral;
1327           return _S_construct_aux(__beg, __end, __a, _Integral());
1328         }
1329
1330       // For Input Iterators, used in istreambuf_iterators, etc.
1331       template<class _InIterator>
1332         static _CharT*
1333          _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1334                       input_iterator_tag);
1335
1336       // For forward_iterators up to random_access_iterators, used for
1337       // string::iterator, _CharT*, etc.
1338       template<class _FwdIterator>
1339         static _CharT*
1340         _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
1341                      forward_iterator_tag);
1342
1343       static _CharT*
1344       _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
1345
1346     public:
1347
1348       /**
1349        *  @brief  Copy substring into C string.
1350        *  @param s  C string to copy value into.
1351        *  @param n  Number of characters to copy.
1352        *  @param pos  Index of first character to copy.
1353        *  @return  Number of characters actually copied
1354        *  @throw  std::out_of_range  If pos > size().
1355        *
1356        *  Copies up to @a n characters starting at @a pos into the C string @a
1357        *  s.  If @a pos is greater than size(), out_of_range is thrown.
1358       */
1359       size_type
1360       copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
1361
1362       /**
1363        *  @brief  Swap contents with another string.
1364        *  @param s  String to swap with.
1365        *
1366        *  Exchanges the contents of this string with that of @a s in constant
1367        *  time.
1368       */
1369       void
1370       swap(basic_string& __s);
1371
1372       // String operations:
1373       /**
1374        *  @brief  Return const pointer to null-terminated contents.
1375        *
1376        *  This is a handle to internal data.  Do not modify or dire things may
1377        *  happen.
1378       */
1379       const _CharT*
1380       c_str() const
1381       {
1382         // MT: This assumes concurrent writes are OK.
1383         const size_type __n = this->size();
1384         traits_type::assign(_M_data()[__n], _Rep::_S_terminal);
1385         return _M_data();
1386       }
1387
1388       /**
1389        *  @brief  Return const pointer to contents.
1390        *
1391        *  This is a handle to internal data.  It may not be null-terminated.
1392        *  Do not modify or dire things may happen.
1393       */
1394       const _CharT*
1395       data() const { return _M_data(); }
1396
1397       /**
1398        *  @brief  Return copy of allocator used to construct this string.
1399       */
1400       allocator_type
1401       get_allocator() const { return _M_dataplus; }
1402
1403       /**
1404        *  @brief  Find position of a C substring.
1405        *  @param s  C string to locate.
1406        *  @param pos  Index of character to search from.
1407        *  @param n  Number of characters from @a s to search for.
1408        *  @return  Index of start of first occurrence.
1409        *
1410        *  Starting from @a pos, searches forward for the first @a n characters
1411        *  in @a s within this string.  If found, returns the index where it
1412        *  begins.  If not found, returns npos.
1413       */
1414       size_type
1415       find(const _CharT* __s, size_type __pos, size_type __n) const;
1416
1417       /**
1418        *  @brief  Find position of a string.
1419        *  @param str  String to locate.
1420        *  @param pos  Index of character to search from (default 0).
1421        *  @return  Index of start of first occurrence.
1422        *
1423        *  Starting from @a pos, searches forward for value of @a str within
1424        *  this string.  If found, returns the index where it begins.  If not
1425        *  found, returns npos.
1426       */
1427       size_type
1428       find(const basic_string& __str, size_type __pos = 0) const
1429       { return this->find(__str.data(), __pos, __str.size()); }
1430
1431       /**
1432        *  @brief  Find position of a C string.
1433        *  @param s  C string to locate.
1434        *  @param pos  Index of character to search from (default 0).
1435        *  @return  Index of start of first occurrence.
1436        *
1437        *  Starting from @a pos, searches forward for the value of @a s within
1438        *  this string.  If found, returns the index where it begins.  If not
1439        *  found, returns npos.
1440       */
1441       size_type
1442       find(const _CharT* __s, size_type __pos = 0) const
1443       { return this->find(__s, __pos, traits_type::length(__s)); }
1444
1445       /**
1446        *  @brief  Find position of a character.
1447        *  @param c  Character to locate.
1448        *  @param pos  Index of character to search from (default 0).
1449        *  @return  Index of first occurrence.
1450        *
1451        *  Starting from @a pos, searches forward for @a c within this string.
1452        *  If found, returns the index where it was found.  If not found,
1453        *  returns npos.
1454       */
1455       size_type
1456       find(_CharT __c, size_type __pos = 0) const;
1457
1458       /**
1459        *  @brief  Find last position of a string.
1460        *  @param str  String to locate.
1461        *  @param pos  Index of character to search back from (default end).
1462        *  @return  Index of start of last occurrence.
1463        *
1464        *  Starting from @a pos, searches backward for value of @a str within
1465        *  this string.  If found, returns the index where it begins.  If not
1466        *  found, returns npos.
1467       */
1468       size_type
1469       rfind(const basic_string& __str, size_type __pos = npos) const
1470       { return this->rfind(__str.data(), __pos, __str.size()); }
1471
1472       /**
1473        *  @brief  Find last position of a C substring.
1474        *  @param s  C string to locate.
1475        *  @param pos  Index of character to search back from.
1476        *  @param n  Number of characters from s to search for.
1477        *  @return  Index of start of last occurrence.
1478        *
1479        *  Starting from @a pos, searches backward for the first @a n
1480        *  characters in @a s within this string.  If found, returns the index
1481        *  where it begins.  If not found, returns npos.
1482       */
1483       size_type
1484       rfind(const _CharT* __s, size_type __pos, size_type __n) const;
1485
1486       /**
1487        *  @brief  Find last position of a C string.
1488        *  @param s  C string to locate.
1489        *  @param pos  Index of character to start search at (default 0).
1490        *  @return  Index of start of  last occurrence.
1491        *
1492        *  Starting from @a pos, searches backward for the value of @a s within
1493        *  this string.  If found, returns the index where it begins.  If not
1494        *  found, returns npos.
1495       */
1496       size_type
1497       rfind(const _CharT* __s, size_type __pos = npos) const
1498       { return this->rfind(__s, __pos, traits_type::length(__s)); }
1499
1500       /**
1501        *  @brief  Find last position of a character.
1502        *  @param c  Character to locate.
1503        *  @param pos  Index of character to search back from (default 0).
1504        *  @return  Index of last occurrence.
1505        *
1506        *  Starting from @a pos, searches backward for @a c within this string.
1507        *  If found, returns the index where it was found.  If not found,
1508        *  returns npos.
1509       */
1510       size_type
1511       rfind(_CharT __c, size_type __pos = npos) const;
1512
1513       /**
1514        *  @brief  Find position of a character of string.
1515        *  @param str  String containing characters to locate.
1516        *  @param pos  Index of character to search from (default 0).
1517        *  @return  Index of first occurrence.
1518        *
1519        *  Starting from @a pos, searches forward for one of the characters of
1520        *  @a str within this string.  If found, returns the index where it was
1521        *  found.  If not found, returns npos.
1522       */
1523       size_type
1524       find_first_of(const basic_string& __str, size_type __pos = 0) const
1525       { return this->find_first_of(__str.data(), __pos, __str.size()); }
1526
1527       /**
1528        *  @brief  Find position of a character of C substring.
1529        *  @param s  String containing characters to locate.
1530        *  @param pos  Index of character to search from (default 0).
1531        *  @param n  Number of characters from s to search for.
1532        *  @return  Index of first occurrence.
1533        *
1534        *  Starting from @a pos, searches forward for one of the first @a n
1535        *  characters of @a s within this string.  If found, returns the index
1536        *  where it was found.  If not found, returns npos.
1537       */
1538       size_type
1539       find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
1540
1541       /**
1542        *  @brief  Find position of a character of C string.
1543        *  @param s  String containing characters to locate.
1544        *  @param pos  Index of character to search from (default 0).
1545        *  @return  Index of first occurrence.
1546        *
1547        *  Starting from @a pos, searches forward for one of the characters of
1548        *  @a s within this string.  If found, returns the index where it was
1549        *  found.  If not found, returns npos.
1550       */
1551       size_type
1552       find_first_of(const _CharT* __s, size_type __pos = 0) const
1553       { return this->find_first_of(__s, __pos, traits_type::length(__s)); }
1554
1555       /**
1556        *  @brief  Find position of a character.
1557        *  @param c  Character to locate.
1558        *  @param pos  Index of character to search from (default 0).
1559        *  @return  Index of first occurrence.
1560        *
1561        *  Starting from @a pos, searches forward for the character @a c within
1562        *  this string.  If found, returns the index where it was found.  If
1563        *  not found, returns npos.
1564        *
1565        *  Note: equivalent to find(c, pos).
1566       */
1567       size_type
1568       find_first_of(_CharT __c, size_type __pos = 0) const
1569       { return this->find(__c, __pos); }
1570
1571       /**
1572        *  @brief  Find last position of a character of string.
1573        *  @param str  String containing characters to locate.
1574        *  @param pos  Index of character to search back from (default end).
1575        *  @return  Index of last occurrence.
1576        *
1577        *  Starting from @a pos, searches backward for one of the characters of
1578        *  @a str within this string.  If found, returns the index where it was
1579        *  found.  If not found, returns npos.
1580       */
1581       size_type
1582       find_last_of(const basic_string& __str, size_type __pos = npos) const
1583       { return this->find_last_of(__str.data(), __pos, __str.size()); }
1584
1585       /**
1586        *  @brief  Find last position of a character of C substring.
1587        *  @param s  C string containing characters to locate.
1588        *  @param pos  Index of character to search back from (default end).
1589        *  @param n  Number of characters from s to search for.
1590        *  @return  Index of last occurrence.
1591        *
1592        *  Starting from @a pos, searches backward for one of the first @a n
1593        *  characters of @a s within this string.  If found, returns the index
1594        *  where it was found.  If not found, returns npos.
1595       */
1596       size_type
1597       find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
1598
1599       /**
1600        *  @brief  Find last position of a character of C string.
1601        *  @param s  C string containing characters to locate.
1602        *  @param pos  Index of character to search back from (default end).
1603        *  @return  Index of last occurrence.
1604        *
1605        *  Starting from @a pos, searches backward for one of the characters of
1606        *  @a s within this string.  If found, returns the index where it was
1607        *  found.  If not found, returns npos.
1608       */
1609       size_type
1610       find_last_of(const _CharT* __s, size_type __pos = npos) const
1611       { return this->find_last_of(__s, __pos, traits_type::length(__s)); }
1612
1613       /**
1614        *  @brief  Find last position of a character.
1615        *  @param c  Character to locate.
1616        *  @param pos  Index of character to search back from (default 0).
1617        *  @return  Index of last occurrence.
1618        *
1619        *  Starting from @a pos, searches backward for @a c within this string.
1620        *  If found, returns the index where it was found.  If not found,
1621        *  returns npos.
1622        *
1623        *  Note: equivalent to rfind(c, pos).
1624       */
1625       size_type
1626       find_last_of(_CharT __c, size_type __pos = npos) const
1627       { return this->rfind(__c, __pos); }
1628
1629       /**
1630        *  @brief  Find position of a character not in string.
1631        *  @param str  String containing characters to avoid.
1632        *  @param pos  Index of character to search from (default 0).
1633        *  @return  Index of first occurrence.
1634        *
1635        *  Starting from @a pos, searches forward for a character not contained
1636        *  in @a str within this string.  If found, returns the index where it
1637        *  was found.  If not found, returns npos.
1638       */
1639       size_type
1640       find_first_not_of(const basic_string& __str, size_type __pos = 0) const
1641       { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
1642
1643       /**
1644        *  @brief  Find position of a character not in C substring.
1645        *  @param s  C string containing characters to avoid.
1646        *  @param pos  Index of character to search from (default 0).
1647        *  @param n  Number of characters from s to consider.
1648        *  @return  Index of first occurrence.
1649        *
1650        *  Starting from @a pos, searches forward for a character not contained
1651        *  in the first @a n characters of @a s within this string.  If found,
1652        *  returns the index where it was found.  If not found, returns npos.
1653       */
1654       size_type
1655       find_first_not_of(const _CharT* __s, size_type __pos,
1656                         size_type __n) const;
1657
1658       /**
1659        *  @brief  Find position of a character not in C string.
1660        *  @param s  C string containing characters to avoid.
1661        *  @param pos  Index of character to search from (default 0).
1662        *  @return  Index of first occurrence.
1663        *
1664        *  Starting from @a pos, searches forward for a character not contained
1665        *  in @a s within this string.  If found, returns the index where it
1666        *  was found.  If not found, returns npos.
1667       */
1668       size_type
1669       find_first_not_of(const _CharT* __s, size_type __pos = 0) const
1670       { return this->find_first_not_of(__s, __pos, traits_type::length(__s)); }
1671
1672       /**
1673        *  @brief  Find position of a different character.
1674        *  @param c  Character to avoid.
1675        *  @param pos  Index of character to search from (default 0).
1676        *  @return  Index of first occurrence.
1677        *
1678        *  Starting from @a pos, searches forward for a character other than @a c
1679        *  within this string.  If found, returns the index where it was found.
1680        *  If not found, returns npos.
1681       */
1682       size_type
1683       find_first_not_of(_CharT __c, size_type __pos = 0) const;
1684
1685       /**
1686        *  @brief  Find last position of a character not in string.
1687        *  @param str  String containing characters to avoid.
1688        *  @param pos  Index of character to search from (default 0).
1689        *  @return  Index of first occurrence.
1690        *
1691        *  Starting from @a pos, searches backward for a character not
1692        *  contained in @a str within this string.  If found, returns the index
1693        *  where it was found.  If not found, returns npos.
1694       */
1695       size_type
1696       find_last_not_of(const basic_string& __str, size_type __pos = npos) const
1697       { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
1698
1699       /**
1700        *  @brief  Find last position of a character not in C substring.
1701        *  @param s  C string containing characters to avoid.
1702        *  @param pos  Index of character to search from (default 0).
1703        *  @param n  Number of characters from s to consider.
1704        *  @return  Index of first occurrence.
1705        *
1706        *  Starting from @a pos, searches backward for a character not
1707        *  contained in the first @a n characters of @a s within this string.
1708        *  If found, returns the index where it was found.  If not found,
1709        *  returns npos.
1710       */
1711       size_type
1712       find_last_not_of(const _CharT* __s, size_type __pos,
1713                        size_type __n) const;
1714       /**
1715        *  @brief  Find position of a character not in C string.
1716        *  @param s  C string containing characters to avoid.
1717        *  @param pos  Index of character to search from (default 0).
1718        *  @return  Index of first occurrence.
1719        *
1720        *  Starting from @a pos, searches backward for a character not
1721        *  contained in @a s within this string.  If found, returns the index
1722        *  where it was found.  If not found, returns npos.
1723       */
1724       size_type
1725       find_last_not_of(const _CharT* __s, size_type __pos = npos) const
1726       { return this->find_last_not_of(__s, __pos, traits_type::length(__s)); }
1727
1728       /**
1729        *  @brief  Find last position of a different character.
1730        *  @param c  Character to avoid.
1731        *  @param pos  Index of character to search from (default 0).
1732        *  @return  Index of first occurrence.
1733        *
1734        *  Starting from @a pos, searches backward for a character other than
1735        *  @a c within this string.  If found, returns the index where it was
1736        *  found.  If not found, returns npos.
1737       */
1738       size_type
1739       find_last_not_of(_CharT __c, size_type __pos = npos) const;
1740
1741       /**
1742        *  @brief  Get a substring.
1743        *  @param pos  Index of first character (default 0).
1744        *  @param n  Number of characters in substring (default remainder).
1745        *  @return  The new string.
1746        *  @throw  std::out_of_range  If pos > size().
1747        *
1748        *  Construct and return a new string using the @a n characters starting
1749        *  at @a pos.  If the string is too short, use the remainder of the
1750        *  characters.  If @a pos is beyond the end of the string, out_of_range
1751        *  is thrown.
1752       */
1753       basic_string
1754       substr(size_type __pos = 0, size_type __n = npos) const
1755       {
1756         if (__pos > this->size())
1757           __throw_out_of_range(__N("basic_string::substr"));
1758         return basic_string(*this, __pos, __n);
1759       }
1760
1761       /**
1762        *  @brief  Compare to a string.
1763        *  @param str  String to compare against.
1764        *  @return  Integer < 0, 0, or > 0.
1765        *
1766        *  Returns an integer < 0 if this string is ordered before @a str, 0 if
1767        *  their values are equivalent, or > 0 if this string is ordered after
1768        *  @a str.  If the lengths of @a str and this string are different, the
1769        *  shorter one is ordered first.  If they are the same, returns the
1770        *  result of traits::compare(data(),str.data(),size());
1771       */
1772       int
1773       compare(const basic_string& __str) const
1774       {
1775         const size_type __size = this->size();
1776         const size_type __osize = __str.size();
1777         const size_type __len = std::min(__size, __osize);
1778
1779         int __r = traits_type::compare(_M_data(), __str.data(), __len);
1780         if (!__r)
1781           __r =  __size - __osize;
1782         return __r;
1783       }
1784
1785       /**
1786        *  @brief  Compare substring to a string.
1787        *  @param pos  Index of first character of substring.
1788        *  @param n  Number of characters in substring.
1789        *  @param str  String to compare against.
1790        *  @return  Integer < 0, 0, or > 0.
1791        *
1792        *  Form the substring of this string from the @a n characters starting
1793        *  at @a pos.  Returns an integer < 0 if the substring is ordered
1794        *  before @a str, 0 if their values are equivalent, or > 0 if the
1795        *  substring is ordered after @a str.  If the lengths @a of str and the
1796        *  substring are different, the shorter one is ordered first.  If they
1797        *  are the same, returns the result of
1798        *  traits::compare(substring.data(),str.data(),size());
1799       */
1800       int
1801       compare(size_type __pos, size_type __n, const basic_string& __str) const;
1802
1803       /**
1804        *  @brief  Compare substring to a substring.
1805        *  @param pos1  Index of first character of substring.
1806        *  @param n1  Number of characters in substring.
1807        *  @param str  String to compare against.
1808        *  @param pos2  Index of first character of substring of str.
1809        *  @param n2  Number of characters in substring of str.
1810        *  @return  Integer < 0, 0, or > 0.
1811        *
1812        *  Form the substring of this string from the @a n1 characters starting
1813        *  at @a pos1.  Form the substring of @a str from the @a n2 characters
1814        *  starting at @a pos2.  Returns an integer < 0 if this substring is
1815        *  ordered before the substring of @a str, 0 if their values are
1816        *  equivalent, or > 0 if this substring is ordered after the substring
1817        *  of @a str.  If the lengths of the substring of @a str and this
1818        *  substring are different, the shorter one is ordered first.  If they
1819        *  are the same, returns the result of
1820        *  traits::compare(substring.data(),str.substr(pos2,n2).data(),size());
1821       */
1822       int
1823       compare(size_type __pos1, size_type __n1, const basic_string& __str,
1824               size_type __pos2, size_type __n2) const;
1825
1826       /**
1827        *  @brief  Compare to a C string.
1828        *  @param s  C string to compare against.
1829        *  @return  Integer < 0, 0, or > 0.
1830        *
1831        *  Returns an integer < 0 if this string is ordered before @a s, 0 if
1832        *  their values are equivalent, or > 0 if this string is ordered after
1833        *  @a s.  If the lengths of @a s and this string are different, the
1834        *  shorter one is ordered first.  If they are the same, returns the
1835        *  result of traits::compare(data(),s,size());
1836       */
1837       int
1838       compare(const _CharT* __s) const;
1839
1840       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1841       // 5 String::compare specification questionable
1842       /**
1843        *  @brief  Compare substring to a C string.
1844        *  @param pos  Index of first character of substring.
1845        *  @param n1  Number of characters in substring.
1846        *  @param s  C string to compare against.
1847        *  @return  Integer < 0, 0, or > 0.
1848        *
1849        *  Form the substring of this string from the @a n1 characters starting
1850        *  at @a pos.  Returns an integer < 0 if the substring is ordered
1851        *  before @a s, 0 if their values are equivalent, or > 0 if the
1852        *  substring is ordered after @a s.  If the lengths of @a s and the
1853        *  substring are different, the shorter one is ordered first.  If they
1854        *  are the same, returns the result of
1855        *  traits::compare(substring.data(),s,size());
1856       */
1857       int
1858       compare(size_type __pos, size_type __n1, const _CharT* __s) const;
1859
1860       /**
1861        *  @brief  Compare substring against a C substring.
1862        *  @param pos1  Index of first character of substring.
1863        *  @param n1  Number of characters in substring.
1864        *  @param s  C string to compare against.
1865        *  @param n2  Number of characters in substring of s.
1866        *  @return  Integer < 0, 0, or > 0.
1867        *
1868        *  Form the substring of this string from the @a n1 characters starting
1869        *  at @a pos1.  Form the substring of @a s from the first @a n
1870        *  characters of @a s.  Returns an integer < 0 if this substring is
1871        *  ordered before the substring of @a s, 0 if their values are
1872        *  equivalent, or > 0 if this substring is ordered after the substring
1873        *  of @a s.  If the lengths of this substring and @a n are different,
1874        *  the shorter one is ordered first.  If they are the same, returns the
1875        *  result of traits::compare(substring.data(),s,size());
1876       */
1877       int
1878       compare(size_type __pos, size_type __n1, const _CharT* __s,
1879               size_type __n2) const;
1880   };
1881
1882
1883   template<typename _CharT, typename _Traits, typename _Alloc>
1884     inline basic_string<_CharT, _Traits, _Alloc>::
1885     basic_string()
1886     : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
1887
1888   // operator+
1889   /**
1890    *  @brief  Concatenate two strings.
1891    *  @param lhs  First string.
1892    *  @param rhs  Last string.
1893    *  @return  New string with value of @a lhs followed by @a rhs.
1894    */ 
1895  template<typename _CharT, typename _Traits, typename _Alloc>
1896     basic_string<_CharT, _Traits, _Alloc>
1897     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
1898               const basic_string<_CharT, _Traits, _Alloc>& __rhs)
1899     {
1900       basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
1901       __str.append(__rhs);
1902       return __str;
1903     }
1904
1905   /**
1906    *  @brief  Concatenate C string and string.
1907    *  @param lhs  First string.
1908    *  @param rhs  Last string.
1909    *  @return  New string with value of @a lhs followed by @a rhs.
1910    */ 
1911   template<typename _CharT, typename _Traits, typename _Alloc>
1912     basic_string<_CharT,_Traits,_Alloc>
1913     operator+(const _CharT* __lhs,
1914               const basic_string<_CharT,_Traits,_Alloc>& __rhs);
1915
1916   /**
1917    *  @brief  Concatenate character and string.
1918    *  @param lhs  First string.
1919    *  @param rhs  Last string.
1920    *  @return  New string with @a lhs followed by @a rhs.
1921    */ 
1922   template<typename _CharT, typename _Traits, typename _Alloc>
1923     basic_string<_CharT,_Traits,_Alloc>
1924     operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
1925
1926   /**
1927    *  @brief  Concatenate string and C string.
1928    *  @param lhs  First string.
1929    *  @param rhs  Last string.
1930    *  @return  New string with @a lhs followed by @a rhs.
1931    */ 
1932   template<typename _CharT, typename _Traits, typename _Alloc>
1933     inline basic_string<_CharT, _Traits, _Alloc>
1934     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
1935              const _CharT* __rhs)
1936     {
1937       basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
1938       __str.append(__rhs);
1939       return __str;
1940     }
1941
1942   /**
1943    *  @brief  Concatenate string and character.
1944    *  @param lhs  First string.
1945    *  @param rhs  Last string.
1946    *  @return  New string with @a lhs followed by @a rhs.
1947    */ 
1948   template<typename _CharT, typename _Traits, typename _Alloc>
1949     inline basic_string<_CharT, _Traits, _Alloc>
1950     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
1951     {
1952       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
1953       typedef typename __string_type::size_type         __size_type;
1954       __string_type __str(__lhs);
1955       __str.append(__size_type(1), __rhs);
1956       return __str;
1957     }
1958
1959   // operator ==
1960   /**
1961    *  @brief  Test equivalence of two strings.
1962    *  @param lhs  First string.
1963    *  @param rhs  Second string.
1964    *  @return  True if @a lhs.compare(@a rhs) == 0.  False otherwise.
1965    */ 
1966   template<typename _CharT, typename _Traits, typename _Alloc>
1967     inline bool
1968     operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
1969                const basic_string<_CharT, _Traits, _Alloc>& __rhs)
1970     { return __lhs.compare(__rhs) == 0; }
1971
1972   /**
1973    *  @brief  Test equivalence of C string and string.
1974    *  @param lhs  C string.
1975    *  @param rhs  String.
1976    *  @return  True if @a rhs.compare(@a lhs) == 0.  False otherwise.
1977    */ 
1978   template<typename _CharT, typename _Traits, typename _Alloc>
1979     inline bool
1980     operator==(const _CharT* __lhs,
1981                const basic_string<_CharT, _Traits, _Alloc>& __rhs)
1982     { return __rhs.compare(__lhs) == 0; }
1983
1984   /**
1985    *  @brief  Test equivalence of string and C string.
1986    *  @param lhs  String.
1987    *  @param rhs  C string.
1988    *  @return  True if @a lhs.compare(@a rhs) == 0.  False otherwise.
1989    */ 
1990   template<typename _CharT, typename _Traits, typename _Alloc>
1991     inline bool
1992     operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
1993                const _CharT* __rhs)
1994     { return __lhs.compare(__rhs) == 0; }
1995
1996   // operator !=
1997   /**
1998    *  @brief  Test difference of two strings.
1999    *  @param lhs  First string.
2000    *  @param rhs  Second string.
2001    *  @return  True if @a lhs.compare(@a rhs) != 0.  False otherwise.
2002    */ 
2003   template<typename _CharT, typename _Traits, typename _Alloc>
2004     inline bool
2005     operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2006                const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2007     { return __rhs.compare(__lhs) != 0; }
2008
2009   /**
2010    *  @brief  Test difference of C string and string.
2011    *  @param lhs  C string.
2012    *  @param rhs  String.
2013    *  @return  True if @a rhs.compare(@a lhs) != 0.  False otherwise.
2014    */ 
2015   template<typename _CharT, typename _Traits, typename _Alloc>
2016     inline bool
2017     operator!=(const _CharT* __lhs,
2018                const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2019     { return __rhs.compare(__lhs) != 0; }
2020
2021   /**
2022    *  @brief  Test difference of string and C string.
2023    *  @param lhs  String.
2024    *  @param rhs  C string.
2025    *  @return  True if @a lhs.compare(@a rhs) != 0.  False otherwise.
2026    */ 
2027   template<typename _CharT, typename _Traits, typename _Alloc>
2028     inline bool
2029     operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2030                const _CharT* __rhs)
2031     { return __lhs.compare(__rhs) != 0; }
2032
2033   // operator <
2034   /**
2035    *  @brief  Test if string precedes string.
2036    *  @param lhs  First string.
2037    *  @param rhs  Second string.
2038    *  @return  True if @a lhs precedes @a rhs.  False otherwise.
2039    */ 
2040   template<typename _CharT, typename _Traits, typename _Alloc>
2041     inline bool
2042     operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2043               const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2044     { return __lhs.compare(__rhs) < 0; }
2045
2046   /**
2047    *  @brief  Test if string precedes C string.
2048    *  @param lhs  String.
2049    *  @param rhs  C string.
2050    *  @return  True if @a lhs precedes @a rhs.  False otherwise.
2051    */ 
2052   template<typename _CharT, typename _Traits, typename _Alloc>
2053     inline bool
2054     operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2055               const _CharT* __rhs)
2056     { return __lhs.compare(__rhs) < 0; }
2057
2058   /**
2059    *  @brief  Test if C string precedes string.
2060    *  @param lhs  C string.
2061    *  @param rhs  String.
2062    *  @return  True if @a lhs precedes @a rhs.  False otherwise.
2063    */ 
2064   template<typename _CharT, typename _Traits, typename _Alloc>
2065     inline bool
2066     operator<(const _CharT* __lhs,
2067               const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2068     { return __rhs.compare(__lhs) > 0; }
2069
2070   // operator >
2071   /**
2072    *  @brief  Test if string follows string.
2073    *  @param lhs  First string.
2074    *  @param rhs  Second string.
2075    *  @return  True if @a lhs follows @a rhs.  False otherwise.
2076    */ 
2077   template<typename _CharT, typename _Traits, typename _Alloc>
2078     inline bool
2079     operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2080               const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2081     { return __lhs.compare(__rhs) > 0; }
2082
2083   /**
2084    *  @brief  Test if string follows C string.
2085    *  @param lhs  String.
2086    *  @param rhs  C string.
2087    *  @return  True if @a lhs follows @a rhs.  False otherwise.
2088    */ 
2089   template<typename _CharT, typename _Traits, typename _Alloc>
2090     inline bool
2091     operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2092               const _CharT* __rhs)
2093     { return __lhs.compare(__rhs) > 0; }
2094
2095   /**
2096    *  @brief  Test if C string follows string.
2097    *  @param lhs  C string.
2098    *  @param rhs  String.
2099    *  @return  True if @a lhs follows @a rhs.  False otherwise.
2100    */ 
2101   template<typename _CharT, typename _Traits, typename _Alloc>
2102     inline bool
2103     operator>(const _CharT* __lhs,
2104               const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2105     { return __rhs.compare(__lhs) < 0; }
2106
2107   // operator <=
2108   /**
2109    *  @brief  Test if string doesn't follow string.
2110    *  @param lhs  First string.
2111    *  @param rhs  Second string.
2112    *  @return  True if @a lhs doesn't follow @a rhs.  False otherwise.
2113    */ 
2114   template<typename _CharT, typename _Traits, typename _Alloc>
2115     inline bool
2116     operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2117                const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2118     { return __lhs.compare(__rhs) <= 0; }
2119
2120   /**
2121    *  @brief  Test if string doesn't follow C string.
2122    *  @param lhs  String.
2123    *  @param rhs  C string.
2124    *  @return  True if @a lhs doesn't follow @a rhs.  False otherwise.
2125    */ 
2126   template<typename _CharT, typename _Traits, typename _Alloc>
2127     inline bool
2128     operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2129                const _CharT* __rhs)
2130     { return __lhs.compare(__rhs) <= 0; }
2131
2132   /**
2133    *  @brief  Test if C string doesn't follow string.
2134    *  @param lhs  C string.
2135    *  @param rhs  String.
2136    *  @return  True if @a lhs doesn't follow @a rhs.  False otherwise.
2137    */ 
2138   template<typename _CharT, typename _Traits, typename _Alloc>
2139     inline bool
2140     operator<=(const _CharT* __lhs,
2141                const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2142   { return __rhs.compare(__lhs) >= 0; }
2143
2144   // operator >=
2145   /**
2146    *  @brief  Test if string doesn't precede string.
2147    *  @param lhs  First string.
2148    *  @param rhs  Second string.
2149    *  @return  True if @a lhs doesn't precede @a rhs.  False otherwise.
2150    */ 
2151   template<typename _CharT, typename _Traits, typename _Alloc>
2152     inline bool
2153     operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2154                const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2155     { return __lhs.compare(__rhs) >= 0; }
2156
2157   /**
2158    *  @brief  Test if string doesn't precede C string.
2159    *  @param lhs  String.
2160    *  @param rhs  C string.
2161    *  @return  True if @a lhs doesn't precede @a rhs.  False otherwise.
2162    */ 
2163   template<typename _CharT, typename _Traits, typename _Alloc>
2164     inline bool
2165     operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2166                const _CharT* __rhs)
2167     { return __lhs.compare(__rhs) >= 0; }
2168
2169   /**
2170    *  @brief  Test if C string doesn't precede string.
2171    *  @param lhs  C string.
2172    *  @param rhs  String.
2173    *  @return  True if @a lhs doesn't precede @a rhs.  False otherwise.
2174    */ 
2175   template<typename _CharT, typename _Traits, typename _Alloc>
2176     inline bool
2177     operator>=(const _CharT* __lhs,
2178              const basic_string<_CharT, _Traits, _Alloc>& __rhs)
2179     { return __rhs.compare(__lhs) <= 0; }
2180
2181
2182   /**
2183    *  @brief  Swap contents of two strings.
2184    *  @param lhs  First string.
2185    *  @param rhs  Second string.
2186    *
2187    *  Exchanges the contents of @a lhs and @a rhs in constant time. 
2188    */
2189   template<typename _CharT, typename _Traits, typename _Alloc>
2190     inline void
2191     swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
2192          basic_string<_CharT, _Traits, _Alloc>& __rhs)
2193     { __lhs.swap(__rhs); }
2194
2195   /**
2196    *  @brief  Read stream into a string.
2197    *  @param is  Input stream.
2198    *  @param str  Buffer to store into.
2199    *  @return  Reference to the input stream.
2200    *
2201    *  Stores characters from @a is into @a str until whitespace is found, the
2202    *  end of the stream is encountered, or str.max_size() is reached.  If
2203    *  is.width() is non-zero, that is the limit on the number of characters
2204    *  stored into @a str.  Any previous contents of @a str are erased.
2205    */
2206   template<typename _CharT, typename _Traits, typename _Alloc>
2207     basic_istream<_CharT, _Traits>&
2208     operator>>(basic_istream<_CharT, _Traits>& __is,
2209                basic_string<_CharT, _Traits, _Alloc>& __str);
2210
2211   /**
2212    *  @brief  Write string to a stream.
2213    *  @param os  Output stream.
2214    *  @param str  String to write out.
2215    *  @return  Reference to the output stream.
2216    *
2217    *  Output characters of @a str into os following the same rules as for
2218    *  writing a C string.
2219    */
2220   template<typename _CharT, typename _Traits, typename _Alloc>
2221     basic_ostream<_CharT, _Traits>&
2222     operator<<(basic_ostream<_CharT, _Traits>& __os,
2223                const basic_string<_CharT, _Traits, _Alloc>& __str);
2224
2225   /**
2226    *  @brief  Read a line from stream into a string.
2227    *  @param is  Input stream.
2228    *  @param str  Buffer to store into.
2229    *  @param delim  Character marking end of line.
2230    *  @return  Reference to the input stream.
2231    *
2232    *  Stores characters from @a is into @a str until @a delim is found, the
2233    *  end of the stream is encountered, or str.max_size() is reached.  If
2234    *  is.width() is non-zero, that is the limit on the number of characters
2235    *  stored into @a str.  Any previous contents of @a str are erased.  If @a
2236    *  delim was encountered, it is extracted but not stored into @a str.
2237    */
2238   template<typename _CharT, typename _Traits, typename _Alloc>
2239     basic_istream<_CharT,_Traits>&
2240     getline(basic_istream<_CharT, _Traits>& __is,
2241             basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
2242
2243   /**
2244    *  @brief  Read a line from stream into a string.
2245    *  @param is  Input stream.
2246    *  @param str  Buffer to store into.
2247    *  @return  Reference to the input stream.
2248    *
2249    *  Stores characters from is into @a str until '\n' is found, the end of
2250    *  the stream is encountered, or str.max_size() is reached.  If is.width()
2251    *  is non-zero, that is the limit on the number of characters stored into
2252    *  @a str.  Any previous contents of @a str are erased.  If end of line was
2253    *  encountered, it is extracted but not stored into @a str.
2254    */
2255   template<typename _CharT, typename _Traits, typename _Alloc>
2256     inline basic_istream<_CharT,_Traits>&
2257     getline(basic_istream<_CharT, _Traits>& __is,
2258             basic_string<_CharT, _Traits, _Alloc>& __str);
2259 } // namespace std
2260
2261 #endif /* _BASIC_STRING_H */