OSDN Git Service

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