OSDN Git Service

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