OSDN Git Service

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