OSDN Git Service

2010-12-19 Paolo Carlini <paolo.carlini@oracle.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / ext / vstring.h
1 // Versatile string -*- C++ -*-
2
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010
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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 // <http://www.gnu.org/licenses/>.
25
26 /** @file ext/vstring.h
27  *  This file is a GNU extension to the Standard C++ Library.
28  */
29
30 #ifndef _VSTRING_H
31 #define _VSTRING_H 1
32
33 #pragma GCC system_header
34
35 #include <initializer_list>
36 #include <ext/vstring_util.h>
37 #include <ext/rc_string_base.h>
38 #include <ext/sso_string_base.h>
39
40 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
41
42   /**
43    *  @class __versa_string vstring.h
44    *  @brief  Template class __versa_string. 
45    *  @ingroup extensions
46    *
47    *  Data structure managing sequences of characters and
48    *  character-like objects. 
49    */
50   template<typename _CharT, typename _Traits, typename _Alloc,
51            template <typename, typename, typename> class _Base>
52     class __versa_string
53     : private _Base<_CharT, _Traits, _Alloc>
54     {
55       typedef _Base<_CharT, _Traits, _Alloc>                __vstring_base;    
56       typedef typename __vstring_base::_CharT_alloc_type    _CharT_alloc_type;
57
58       // Types:
59     public:
60       typedef _Traits                                       traits_type;
61       typedef typename _Traits::char_type                   value_type;
62       typedef _Alloc                                        allocator_type;
63       typedef typename _CharT_alloc_type::size_type         size_type;
64       typedef typename _CharT_alloc_type::difference_type   difference_type;
65       typedef value_type&                                   reference;
66       typedef const value_type&                             const_reference;
67       typedef typename _CharT_alloc_type::pointer           pointer;
68       typedef typename _CharT_alloc_type::const_pointer     const_pointer;
69       typedef __gnu_cxx::__normal_iterator<pointer, __versa_string>  iterator;
70       typedef __gnu_cxx::__normal_iterator<const_pointer, __versa_string>
71                                                             const_iterator;
72       typedef std::reverse_iterator<const_iterator>     const_reverse_iterator;
73       typedef std::reverse_iterator<iterator>               reverse_iterator;
74
75       // Data Member (public):
76       ///  Value returned by various member functions when they fail.
77       static const size_type    npos = static_cast<size_type>(-1);
78
79     private:
80       size_type
81       _M_check(size_type __pos, const char* __s) const
82       {
83         if (__pos > this->size())
84           std::__throw_out_of_range(__N(__s));
85         return __pos;
86       }
87
88       void
89       _M_check_length(size_type __n1, size_type __n2, const char* __s) const
90       {
91         if (this->max_size() - (this->size() - __n1) < __n2)
92           std::__throw_length_error(__N(__s));
93       }
94
95       // NB: _M_limit doesn't check for a bad __pos value.
96       size_type
97       _M_limit(size_type __pos, size_type __off) const
98       {
99         const bool __testoff =  __off < this->size() - __pos;
100         return __testoff ? __off : this->size() - __pos;
101       }
102
103       // True if _Rep and source do not overlap.
104       bool
105       _M_disjunct(const _CharT* __s) const
106       {
107         return (std::less<const _CharT*>()(__s, this->_M_data())
108                 || std::less<const _CharT*>()(this->_M_data()
109                                               + this->size(), __s));
110       }
111
112       // For the internal use we have functions similar to `begin'/`end'
113       // but they do not call _M_leak.
114       iterator
115       _M_ibegin() const
116       { return iterator(this->_M_data()); }
117
118       iterator
119       _M_iend() const
120       { return iterator(this->_M_data() + this->_M_length()); }
121
122     public:
123       // Construct/copy/destroy:
124       // NB: We overload ctors in some cases instead of using default
125       // arguments, per 17.4.4.4 para. 2 item 2.
126
127       /**
128        *  @brief  Default constructor creates an empty string.
129        */
130       __versa_string()
131       : __vstring_base() { }
132       
133       /**
134        *  @brief  Construct an empty string using allocator @a a.
135        */
136       explicit
137       __versa_string(const _Alloc& __a)
138       : __vstring_base(__a) { }
139
140       // NB: per LWG issue 42, semantics different from IS:
141       /**
142        *  @brief  Construct string with copy of value of @a str.
143        *  @param  __str  Source string.
144        */
145       __versa_string(const __versa_string& __str)
146       : __vstring_base(__str) { }
147
148 #ifdef __GXX_EXPERIMENTAL_CXX0X__
149       /**
150        *  @brief  String move constructor.
151        *  @param  __str  Source string.
152        *
153        *  The newly-constructed %string contains the exact contents of
154        *  @a str.  The contents of @a str are a valid, but unspecified
155        *  string.
156        */
157       __versa_string(__versa_string&& __str)
158       : __vstring_base(std::move(__str)) { }
159
160       /**
161        *  @brief  Construct string from an initializer list.
162        *  @param  __l  std::initializer_list of characters.
163        *  @param  __a  Allocator to use (default is default allocator).
164        */
165       __versa_string(std::initializer_list<_CharT> __l,
166                      const _Alloc& __a = _Alloc())
167       : __vstring_base(__l.begin(), __l.end(), __a) { }
168 #endif
169
170       /**
171        *  @brief  Construct string as copy of a substring.
172        *  @param  __str  Source string.
173        *  @param  __pos  Index of first character to copy from.
174        *  @param  __n  Number of characters to copy (default remainder).
175        */
176       __versa_string(const __versa_string& __str, size_type __pos,
177                      size_type __n = npos)
178       : __vstring_base(__str._M_data()
179                        + __str._M_check(__pos,
180                                         "__versa_string::__versa_string"),
181                        __str._M_data() + __str._M_limit(__pos, __n)
182                        + __pos, _Alloc()) { }
183
184       /**
185        *  @brief  Construct string as copy of a substring.
186        *  @param  __str  Source string.
187        *  @param  __pos  Index of first character to copy from.
188        *  @param  __n  Number of characters to copy.
189        *  @param  __a  Allocator to use.
190        */
191       __versa_string(const __versa_string& __str, size_type __pos,
192                      size_type __n, const _Alloc& __a)
193       : __vstring_base(__str._M_data()
194                        + __str._M_check(__pos,
195                                         "__versa_string::__versa_string"),
196                        __str._M_data() + __str._M_limit(__pos, __n)
197                        + __pos, __a) { }
198
199       /**
200        *  @brief  Construct string initialized by a character array.
201        *  @param  __s  Source character array.
202        *  @param  __n  Number of characters to copy.
203        *  @param  __a  Allocator to use (default is default allocator).
204        *
205        *  NB: @a __s must have at least @a __n characters, '\\0' has no special
206        *  meaning.
207        */
208       __versa_string(const _CharT* __s, size_type __n,
209                      const _Alloc& __a = _Alloc())
210       : __vstring_base(__s, __s + __n, __a) { }
211
212       /**
213        *  @brief  Construct string as copy of a C string.
214        *  @param  __s  Source C string.
215        *  @param  __a  Allocator to use (default is default allocator).
216        */
217       __versa_string(const _CharT* __s, const _Alloc& __a = _Alloc())
218       : __vstring_base(__s, __s ? __s + traits_type::length(__s) :
219                        __s + npos, __a) { }
220
221       /**
222        *  @brief  Construct string as multiple characters.
223        *  @param  __n  Number of characters.
224        *  @param  __c  Character to use.
225        *  @param  __a  Allocator to use (default is default allocator).
226        */
227       __versa_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
228       : __vstring_base(__n, __c, __a) { }
229
230       /**
231        *  @brief  Construct string as copy of a range.
232        *  @param  __beg  Start of range.
233        *  @param  __end  End of range.
234        *  @param  __a  Allocator to use (default is default allocator).
235        */
236       template<class _InputIterator>
237         __versa_string(_InputIterator __beg, _InputIterator __end,
238                        const _Alloc& __a = _Alloc())
239         : __vstring_base(__beg, __end, __a) { }
240
241       /**
242        *  @brief  Destroy the string instance.
243        */
244       ~__versa_string() { }     
245
246       /**
247        *  @brief  Assign the value of @a str to this string.
248        *  @param  __str  Source string.
249        */
250       __versa_string&
251       operator=(const __versa_string& __str) 
252       { return this->assign(__str); }
253
254 #ifdef __GXX_EXPERIMENTAL_CXX0X__
255       /**
256        *  @brief  String move assignment operator.
257        *  @param  __str  Source string.
258        *
259        *  The contents of @a __str are moved into this string (without
260        *  copying).  @a __str is a valid, but unspecified string.
261        */
262       __versa_string&
263       operator=(__versa_string&& __str)
264       {
265         // NB: DR 1204.
266         this->swap(__str);
267         return *this;
268       }
269
270       /**
271        *  @brief  Set value to string constructed from initializer list.
272        *  @param  __l  std::initializer_list.
273        */
274       __versa_string&
275       operator=(std::initializer_list<_CharT> __l)
276       {
277         this->assign(__l.begin(), __l.end());
278         return *this;
279       }
280 #endif
281
282       /**
283        *  @brief  Copy contents of @a __s into this string.
284        *  @param  __s  Source null-terminated string.
285        */
286       __versa_string&
287       operator=(const _CharT* __s) 
288       { return this->assign(__s); }
289
290       /**
291        *  @brief  Set value to string of length 1.
292        *  @param  __c  Source character.
293        *
294        *  Assigning to a character makes this string length 1 and
295        *  (*this)[0] == @a __c.
296        */
297       __versa_string&
298       operator=(_CharT __c) 
299       { 
300         this->assign(1, __c); 
301         return *this;
302       }
303
304       // Iterators:
305       /**
306        *  Returns a read/write iterator that points to the first character in
307        *  the %string.  Unshares the string.
308        */
309       iterator
310       begin()
311       {
312         this->_M_leak();
313         return iterator(this->_M_data());
314       }
315
316       /**
317        *  Returns a read-only (constant) iterator that points to the first
318        *  character in the %string.
319        */
320       const_iterator
321       begin() const
322       { return const_iterator(this->_M_data()); }
323
324       /**
325        *  Returns a read/write iterator that points one past the last
326        *  character in the %string.  Unshares the string.
327        */
328       iterator
329       end()
330       {
331         this->_M_leak();
332         return iterator(this->_M_data() + this->size());
333       }
334
335       /**
336        *  Returns a read-only (constant) iterator that points one past the
337        *  last character in the %string.
338        */
339       const_iterator
340       end() const
341       { return const_iterator(this->_M_data() + this->size()); }
342
343       /**
344        *  Returns a read/write reverse iterator that points to the last
345        *  character in the %string.  Iteration is done in reverse element
346        *  order.  Unshares the string.
347        */
348       reverse_iterator
349       rbegin()
350       { return reverse_iterator(this->end()); }
351
352       /**
353        *  Returns a read-only (constant) reverse iterator that points
354        *  to the last character in the %string.  Iteration is done in
355        *  reverse element order.
356        */
357       const_reverse_iterator
358       rbegin() const
359       { return const_reverse_iterator(this->end()); }
360
361       /**
362        *  Returns a read/write reverse iterator that points to one before the
363        *  first character in the %string.  Iteration is done in reverse
364        *  element order.  Unshares the string.
365        */
366       reverse_iterator
367       rend()
368       { return reverse_iterator(this->begin()); }
369
370       /**
371        *  Returns a read-only (constant) reverse iterator that points
372        *  to one before the first character in the %string.  Iteration
373        *  is done in reverse element order.
374        */
375       const_reverse_iterator
376       rend() const
377       { return const_reverse_iterator(this->begin()); }
378
379 #ifdef __GXX_EXPERIMENTAL_CXX0X__
380       /**
381        *  Returns a read-only (constant) iterator that points to the first
382        *  character in the %string.
383        */
384       const_iterator
385       cbegin() const
386       { return const_iterator(this->_M_data()); }
387
388       /**
389        *  Returns a read-only (constant) iterator that points one past the
390        *  last character in the %string.
391        */
392       const_iterator
393       cend() const
394       { return const_iterator(this->_M_data() + this->size()); }
395
396       /**
397        *  Returns a read-only (constant) reverse iterator that points
398        *  to the last character in the %string.  Iteration is done in
399        *  reverse element order.
400        */
401       const_reverse_iterator
402       crbegin() const
403       { return const_reverse_iterator(this->end()); }
404
405       /**
406        *  Returns a read-only (constant) reverse iterator that points
407        *  to one before the first character in the %string.  Iteration
408        *  is done in reverse element order.
409        */
410       const_reverse_iterator
411       crend() const
412       { return const_reverse_iterator(this->begin()); }
413 #endif
414
415     public:
416       // Capacity:
417       ///  Returns the number of characters in the string, not including any
418       ///  null-termination.
419       size_type
420       size() const
421       { return this->_M_length(); }
422
423       ///  Returns the number of characters in the string, not including any
424       ///  null-termination.
425       size_type
426       length() const
427       { return this->_M_length(); }
428
429       /// Returns the size() of the largest possible %string.
430       size_type
431       max_size() const
432       { return this->_M_max_size(); }
433
434       /**
435        *  @brief  Resizes the %string to the specified number of characters.
436        *  @param  __n  Number of characters the %string should contain.
437        *  @param  __c  Character to fill any new elements.
438        *
439        *  This function will %resize the %string to the specified
440        *  number of characters.  If the number is smaller than the
441        *  %string's current size the %string is truncated, otherwise
442        *  the %string is extended and new elements are set to @a __c.
443        */
444       void
445       resize(size_type __n, _CharT __c);
446
447       /**
448        *  @brief  Resizes the %string to the specified number of characters.
449        *  @param  __n  Number of characters the %string should contain.
450        *
451        *  This function will resize the %string to the specified
452        *  length.  If the new size is smaller than the %string's
453        *  current size the %string is truncated, otherwise the %string
454        *  is extended and new characters are default-constructed.  For
455        *  basic types such as char, this means setting them to 0.
456        */
457       void
458       resize(size_type __n)
459       { this->resize(__n, _CharT()); }
460
461 #ifdef __GXX_EXPERIMENTAL_CXX0X__
462       /// A non-binding request to reduce capacity() to size().
463       void
464       shrink_to_fit()
465       {
466         __try
467           { this->reserve(0); }
468         __catch(...)
469           { }
470       }
471 #endif
472
473       /**
474        *  Returns the total number of characters that the %string can
475        *  hold before needing to allocate more memory.
476        */
477       size_type
478       capacity() const
479       { return this->_M_capacity(); }
480
481       /**
482        *  @brief  Attempt to preallocate enough memory for specified number of
483        *          characters.
484        *  @param  __res_arg  Number of characters required.
485        *  @throw  std::length_error  If @a __res_arg exceeds @c max_size().
486        *
487        *  This function attempts to reserve enough memory for the
488        *  %string to hold the specified number of characters.  If the
489        *  number requested is more than max_size(), length_error is
490        *  thrown.
491        *
492        *  The advantage of this function is that if optimal code is a
493        *  necessity and the user can determine the string length that
494        *  will be required, the user can reserve the memory in
495        *  %advance, and thus prevent a possible reallocation of memory
496        *  and copying of %string data.
497        */
498       void
499       reserve(size_type __res_arg = 0)
500       { this->_M_reserve(__res_arg); }
501
502       /**
503        *  Erases the string, making it empty.
504        */
505       void
506       clear()
507       { this->_M_clear(); }
508
509       /**
510        *  Returns true if the %string is empty.  Equivalent to 
511        *  <code>*this == ""</code>.
512        */
513       bool
514       empty() const
515       { return this->size() == 0; }
516
517       // Element access:
518       /**
519        *  @brief  Subscript access to the data contained in the %string.
520        *  @param  __pos  The index of the character to access.
521        *  @return  Read-only (constant) reference to the character.
522        *
523        *  This operator allows for easy, array-style, data access.
524        *  Note that data access with this operator is unchecked and
525        *  out_of_range lookups are not defined. (For checked lookups
526        *  see at().)
527        */
528       const_reference
529       operator[] (size_type __pos) const
530       {
531         _GLIBCXX_DEBUG_ASSERT(__pos <= this->size());
532         return this->_M_data()[__pos];
533       }
534
535       /**
536        *  @brief  Subscript access to the data contained in the %string.
537        *  @param  __pos  The index of the character to access.
538        *  @return  Read/write reference to the character.
539        *
540        *  This operator allows for easy, array-style, data access.
541        *  Note that data access with this operator is unchecked and
542        *  out_of_range lookups are not defined. (For checked lookups
543        *  see at().)  Unshares the string.
544        */
545       reference
546       operator[](size_type __pos)
547       {
548         // allow pos == size() as v3 extension:
549         _GLIBCXX_DEBUG_ASSERT(__pos <= this->size());
550         // but be strict in pedantic mode:
551         _GLIBCXX_DEBUG_PEDASSERT(__pos < this->size());
552         this->_M_leak();
553         return this->_M_data()[__pos];
554       }
555
556       /**
557        *  @brief  Provides access to the data contained in the %string.
558        *  @param __n The index of the character to access.
559        *  @return  Read-only (const) reference to the character.
560        *  @throw  std::out_of_range  If @a __n is an invalid index.
561        *
562        *  This function provides for safer data access.  The parameter
563        *  is first checked that it is in the range of the string.  The
564        *  function throws out_of_range if the check fails.
565        */
566       const_reference
567       at(size_type __n) const
568       {
569         if (__n >= this->size())
570           std::__throw_out_of_range(__N("__versa_string::at"));
571         return this->_M_data()[__n];
572       }
573
574       /**
575        *  @brief  Provides access to the data contained in the %string.
576        *  @param __n The index of the character to access.
577        *  @return  Read/write reference to the character.
578        *  @throw  std::out_of_range  If @a __n is an invalid index.
579        *
580        *  This function provides for safer data access.  The parameter
581        *  is first checked that it is in the range of the string.  The
582        *  function throws out_of_range if the check fails.  Success
583        *  results in unsharing the string.
584        */
585       reference
586       at(size_type __n)
587       {
588         if (__n >= this->size())
589           std::__throw_out_of_range(__N("__versa_string::at"));
590         this->_M_leak();
591         return this->_M_data()[__n];
592       }
593
594 #ifdef __GXX_EXPERIMENTAL_CXX0X__
595       /**
596        *  Returns a read/write reference to the data at the first
597        *  element of the %string.
598        */
599       reference
600       front()
601       { return operator[](0); }
602
603       /**
604        *  Returns a read-only (constant) reference to the data at the first
605        *  element of the %string.
606        */
607       const_reference
608       front() const
609       { return operator[](0); }
610
611       /**
612        *  Returns a read/write reference to the data at the last
613        *  element of the %string.
614        */
615       reference
616       back()
617       { return operator[](this->size() - 1); }
618
619       /**
620        *  Returns a read-only (constant) reference to the data at the
621        *  last element of the %string.
622        */
623       const_reference
624       back() const
625       { return operator[](this->size() - 1); }
626 #endif
627
628       // Modifiers:
629       /**
630        *  @brief  Append a string to this string.
631        *  @param __str  The string to append.
632        *  @return  Reference to this string.
633        */
634       __versa_string&
635       operator+=(const __versa_string& __str)
636       { return this->append(__str); }
637
638       /**
639        *  @brief  Append a C string.
640        *  @param __s  The C string to append.
641        *  @return  Reference to this string.
642        */
643       __versa_string&
644       operator+=(const _CharT* __s)
645       { return this->append(__s); }
646
647       /**
648        *  @brief  Append a character.
649        *  @param __c  The character to append.
650        *  @return  Reference to this string.
651        */
652       __versa_string&
653       operator+=(_CharT __c)
654       { 
655         this->push_back(__c);
656         return *this;
657       }
658
659 #ifdef __GXX_EXPERIMENTAL_CXX0X__
660       /**
661        *  @brief  Append an initializer_list of characters.
662        *  @param __l  The initializer_list of characters to be appended.
663        *  @return  Reference to this string.
664        */
665       __versa_string&
666       operator+=(std::initializer_list<_CharT> __l)
667       { return this->append(__l.begin(), __l.end()); }
668 #endif // __GXX_EXPERIMENTAL_CXX0X__
669
670       /**
671        *  @brief  Append a string to this string.
672        *  @param __str  The string to append.
673        *  @return  Reference to this string.
674        */
675       __versa_string&
676       append(const __versa_string& __str)
677       { return _M_append(__str._M_data(), __str.size()); }
678
679       /**
680        *  @brief  Append a substring.
681        *  @param __str  The string to append.
682        *  @param __pos  Index of the first character of str to append.
683        *  @param __n  The number of characters to append.
684        *  @return  Reference to this string.
685        *  @throw  std::out_of_range if @a pos is not a valid index.
686        *
687        *  This function appends @a __n characters from @a __str
688        *  starting at @a __pos to this string.  If @a __n is is larger
689        *  than the number of available characters in @a __str, the
690        *  remainder of @a __str is appended.
691        */
692       __versa_string&
693       append(const __versa_string& __str, size_type __pos, size_type __n)
694       { return _M_append(__str._M_data()
695                          + __str._M_check(__pos, "__versa_string::append"),
696                          __str._M_limit(__pos, __n)); }
697
698       /**
699        *  @brief  Append a C substring.
700        *  @param __s  The C string to append.
701        *  @param __n  The number of characters to append.
702        *  @return  Reference to this string.
703        */
704       __versa_string&
705       append(const _CharT* __s, size_type __n)
706       {
707         __glibcxx_requires_string_len(__s, __n);
708         _M_check_length(size_type(0), __n, "__versa_string::append");
709         return _M_append(__s, __n);
710       }
711
712       /**
713        *  @brief  Append a C string.
714        *  @param __s  The C string to append.
715        *  @return  Reference to this string.
716        */
717       __versa_string&
718       append(const _CharT* __s)
719       {
720         __glibcxx_requires_string(__s);
721         const size_type __n = traits_type::length(__s);
722         _M_check_length(size_type(0), __n, "__versa_string::append");
723         return _M_append(__s, __n);
724       }
725
726       /**
727        *  @brief  Append multiple characters.
728        *  @param __n  The number of characters to append.
729        *  @param __c  The character to use.
730        *  @return  Reference to this string.
731        *
732        *  Appends n copies of c to this string.
733        */
734       __versa_string&
735       append(size_type __n, _CharT __c)
736       { return _M_replace_aux(this->size(), size_type(0), __n, __c); }
737
738 #ifdef __GXX_EXPERIMENTAL_CXX0X__
739       /**
740        *  @brief  Append an initializer_list of characters.
741        *  @param __l  The initializer_list of characters to append.
742        *  @return  Reference to this string.
743        */
744       __versa_string&
745       append(std::initializer_list<_CharT> __l)
746       { return this->append(__l.begin(), __l.end()); }
747 #endif // __GXX_EXPERIMENTAL_CXX0X__
748
749       /**
750        *  @brief  Append a range of characters.
751        *  @param __first  Iterator referencing the first character to append.
752        *  @param __last  Iterator marking the end of the range.
753        *  @return  Reference to this string.
754        *
755        *  Appends characters in the range [first,last) to this string.
756        */
757       template<class _InputIterator>
758         __versa_string&
759         append(_InputIterator __first, _InputIterator __last)
760         { return this->replace(_M_iend(), _M_iend(), __first, __last); }
761
762       /**
763        *  @brief  Append a single character.
764        *  @param __c  Character to append.
765        */
766       void
767       push_back(_CharT __c)
768       { 
769         const size_type __size = this->size();
770         if (__size + 1 > this->capacity() || this->_M_is_shared())
771           this->_M_mutate(__size, size_type(0), 0, size_type(1));
772         traits_type::assign(this->_M_data()[__size], __c);
773         this->_M_set_length(__size + 1);
774       }
775
776       /**
777        *  @brief  Set value to contents of another string.
778        *  @param  __str  Source string to use.
779        *  @return  Reference to this string.
780        */
781       __versa_string&
782       assign(const __versa_string& __str)
783       {
784         this->_M_assign(__str);
785         return *this;
786       }
787
788 #ifdef __GXX_EXPERIMENTAL_CXX0X__
789       /**
790        *  @brief  Set value to contents of another string.
791        *  @param  __str  Source string to use.
792        *  @return  Reference to this string.
793        *
794        *  This function sets this string to the exact contents of @a __str.
795        *  @a __str is a valid, but unspecified string.
796        */
797       __versa_string&
798       assign(__versa_string&& __str)
799       {
800         this->swap(__str);
801         return *this;
802       }
803 #endif // __GXX_EXPERIMENTAL_CXX0X__
804
805       /**
806        *  @brief  Set value to a substring of a string.
807        *  @param __str  The string to use.
808        *  @param __pos  Index of the first character of str.
809        *  @param __n  Number of characters to use.
810        *  @return  Reference to this string.
811        *  @throw  std::out_of_range if @a __pos is not a valid index.
812        *
813        *  This function sets this string to the substring of @a __str
814        *  consisting of @a __n characters at @a __pos.  If @a __n is
815        *  is larger than the number of available characters in @a
816        *  __str, the remainder of @a __str is used.
817        */
818       __versa_string&
819       assign(const __versa_string& __str, size_type __pos, size_type __n)
820       { return _M_replace(size_type(0), this->size(), __str._M_data()
821                           + __str._M_check(__pos, "__versa_string::assign"),
822                           __str._M_limit(__pos, __n)); }
823
824       /**
825        *  @brief  Set value to a C substring.
826        *  @param __s  The C string to use.
827        *  @param __n  Number of characters to use.
828        *  @return  Reference to this string.
829        *
830        *  This function sets the value of this string to the first @a
831        *  __n characters of @a __s.  If @a __n is is larger than the
832        *  number of available characters in @a __s, the remainder of
833        *  @a __s is used.
834        */
835       __versa_string&
836       assign(const _CharT* __s, size_type __n)
837       {
838         __glibcxx_requires_string_len(__s, __n);
839         return _M_replace(size_type(0), this->size(), __s, __n);
840       }
841
842       /**
843        *  @brief  Set value to contents of a C string.
844        *  @param __s  The C string to use.
845        *  @return  Reference to this string.
846        *
847        *  This function sets the value of this string to the value of
848        *  @a __s.  The data is copied, so there is no dependence on @a
849        *  __s once the function returns.
850        */
851       __versa_string&
852       assign(const _CharT* __s)
853       {
854         __glibcxx_requires_string(__s);
855         return _M_replace(size_type(0), this->size(), __s,
856                           traits_type::length(__s));
857       }
858
859       /**
860        *  @brief  Set value to multiple characters.
861        *  @param __n  Length of the resulting string.
862        *  @param __c  The character to use.
863        *  @return  Reference to this string.
864        *
865        *  This function sets the value of this string to @a __n copies of
866        *  character @a __c.
867        */
868       __versa_string&
869       assign(size_type __n, _CharT __c)
870       { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
871
872       /**
873        *  @brief  Set value to a range of characters.
874        *  @param __first  Iterator referencing the first character to append.
875        *  @param __last  Iterator marking the end of the range.
876        *  @return  Reference to this string.
877        *
878        *  Sets value of string to characters in the range
879        *  [first,last).
880       */
881       template<class _InputIterator>
882         __versa_string&
883         assign(_InputIterator __first, _InputIterator __last)
884         { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
885
886 #ifdef __GXX_EXPERIMENTAL_CXX0X__
887       /**
888        *  @brief  Set value to an initializer_list of characters.
889        *  @param __l  The initializer_list of characters to assign.
890        *  @return  Reference to this string.
891        */
892       __versa_string&
893       assign(std::initializer_list<_CharT> __l)
894       { return this->assign(__l.begin(), __l.end()); }
895 #endif // __GXX_EXPERIMENTAL_CXX0X__
896
897       /**
898        *  @brief  Insert multiple characters.
899        *  @param __p  Iterator referencing location in string to insert at.
900        *  @param __n  Number of characters to insert
901        *  @param __c  The character to insert.
902        *  @throw  std::length_error  If new length exceeds @c max_size().
903        *
904        *  Inserts @a __n copies of character @a __c starting at the
905        *  position referenced by iterator @a __p.  If adding
906        *  characters causes the length to exceed max_size(),
907        *  length_error is thrown.  The value of the string doesn't
908        *  change if an error is thrown.
909       */
910       void
911       insert(iterator __p, size_type __n, _CharT __c)
912       { this->replace(__p, __p, __n, __c);  }
913
914       /**
915        *  @brief  Insert a range of characters.
916        *  @param __p  Iterator referencing location in string to insert at.
917        *  @param __beg  Start of range.
918        *  @param __end  End of range.
919        *  @throw  std::length_error  If new length exceeds @c max_size().
920        *
921        *  Inserts characters in range [beg,end).  If adding characters
922        *  causes the length to exceed max_size(), length_error is
923        *  thrown.  The value of the string doesn't change if an error
924        *  is thrown.
925       */
926       template<class _InputIterator>
927         void
928         insert(iterator __p, _InputIterator __beg, _InputIterator __end)
929         { this->replace(__p, __p, __beg, __end); }
930
931 #ifdef __GXX_EXPERIMENTAL_CXX0X__
932       /**
933        *  @brief  Insert an initializer_list of characters.
934        *  @param __p  Iterator referencing location in string to insert at.
935        *  @param __l  The initializer_list of characters to insert.
936        *  @throw  std::length_error  If new length exceeds @c max_size().
937        */
938       void
939       insert(iterator __p, std::initializer_list<_CharT> __l)
940       { this->insert(__p, __l.begin(), __l.end()); }
941 #endif // __GXX_EXPERIMENTAL_CXX0X__
942
943       /**
944        *  @brief  Insert value of a string.
945        *  @param __pos1  Iterator referencing location in string to insert at.
946        *  @param __str  The string to insert.
947        *  @return  Reference to this string.
948        *  @throw  std::length_error  If new length exceeds @c max_size().
949        *
950        *  Inserts value of @a __str starting at @a __pos1.  If adding
951        *  characters causes the length to exceed max_size(),
952        *  length_error is thrown.  The value of the string doesn't
953        *  change if an error is thrown.
954       */
955       __versa_string&
956       insert(size_type __pos1, const __versa_string& __str)
957       { return this->replace(__pos1, size_type(0),
958                              __str._M_data(), __str.size()); }
959
960       /**
961        *  @brief  Insert a substring.
962        *  @param __pos1  Iterator referencing location in string to insert at.
963        *  @param __str  The string to insert.
964        *  @param __pos2  Start of characters in str to insert.
965        *  @param __n  Number of characters to insert.
966        *  @return  Reference to this string.
967        *  @throw  std::length_error  If new length exceeds @c max_size().
968        *  @throw  std::out_of_range  If @a __pos1 > size() or
969        *  @a __pos2 > @a __str.size().
970        *
971        *  Starting at @a __pos1, insert @a __n character of @a __str
972        *  beginning with @a __pos2.  If adding characters causes the
973        *  length to exceed max_size(), length_error is thrown.  If @a
974        *  __pos1 is beyond the end of this string or @a __pos2 is
975        *  beyond the end of @a __str, out_of_range is thrown.  The
976        *  value of the string doesn't change if an error is thrown.
977       */
978       __versa_string&
979       insert(size_type __pos1, const __versa_string& __str,
980              size_type __pos2, size_type __n)
981       { return this->replace(__pos1, size_type(0), __str._M_data()
982                              + __str._M_check(__pos2, "__versa_string::insert"),
983                              __str._M_limit(__pos2, __n)); }
984
985       /**
986        *  @brief  Insert a C substring.
987        *  @param __pos  Iterator referencing location in string to insert at.
988        *  @param __s  The C string to insert.
989        *  @param __n  The number of characters to insert.
990        *  @return  Reference to this string.
991        *  @throw  std::length_error  If new length exceeds @c max_size().
992        *  @throw  std::out_of_range  If @a __pos is beyond the end of this
993        *  string.
994        *
995        *  Inserts the first @a __n characters of @a __s starting at @a
996        *  __pos.  If adding characters causes the length to exceed
997        *  max_size(), length_error is thrown.  If @a __pos is beyond
998        *  end(), out_of_range is thrown.  The value of the string
999        *  doesn't change if an error is thrown.
1000       */
1001       __versa_string&
1002       insert(size_type __pos, const _CharT* __s, size_type __n)
1003       { return this->replace(__pos, size_type(0), __s, __n); }
1004
1005       /**
1006        *  @brief  Insert a C string.
1007        *  @param __pos  Iterator referencing location in string to insert at.
1008        *  @param __s  The C string to insert.
1009        *  @return  Reference to this string.
1010        *  @throw  std::length_error  If new length exceeds @c max_size().
1011        *  @throw  std::out_of_range  If @a __pos is beyond the end of this
1012        *  string.
1013        *
1014        *  Inserts the first @a __n characters of @a __s starting at @a
1015        *  __pos.  If adding characters causes the length to exceed
1016        *  max_size(), length_error is thrown.  If @a __pos is beyond
1017        *  end(), out_of_range is thrown.  The value of the string
1018        *  doesn't change if an error is thrown.
1019       */
1020       __versa_string&
1021       insert(size_type __pos, const _CharT* __s)
1022       {
1023         __glibcxx_requires_string(__s);
1024         return this->replace(__pos, size_type(0), __s,
1025                              traits_type::length(__s));
1026       }
1027
1028       /**
1029        *  @brief  Insert multiple characters.
1030        *  @param __pos  Index in string to insert at.
1031        *  @param __n  Number of characters to insert
1032        *  @param __c  The character to insert.
1033        *  @return  Reference to this string.
1034        *  @throw  std::length_error  If new length exceeds @c max_size().
1035        *  @throw  std::out_of_range  If @a __pos is beyond the end of this
1036        *  string.
1037        *
1038        *  Inserts @a __n copies of character @a __c starting at index
1039        *  @a __pos.  If adding characters causes the length to exceed
1040        *  max_size(), length_error is thrown.  If @a __pos > length(),
1041        *  out_of_range is thrown.  The value of the string doesn't
1042        *  change if an error is thrown.
1043       */
1044       __versa_string&
1045       insert(size_type __pos, size_type __n, _CharT __c)
1046       { return _M_replace_aux(_M_check(__pos, "__versa_string::insert"),
1047                               size_type(0), __n, __c); }
1048
1049       /**
1050        *  @brief  Insert one character.
1051        *  @param __p  Iterator referencing position in string to insert at.
1052        *  @param __c  The character to insert.
1053        *  @return  Iterator referencing newly inserted char.
1054        *  @throw  std::length_error  If new length exceeds @c max_size().
1055        *
1056        *  Inserts character @a __c at position referenced by @a __p.
1057        *  If adding character causes the length to exceed max_size(),
1058        *  length_error is thrown.  If @a __p is beyond end of string,
1059        *  out_of_range is thrown.  The value of the string doesn't
1060        *  change if an error is thrown.
1061       */
1062       iterator
1063       insert(iterator __p, _CharT __c)
1064       {
1065         _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1066         const size_type __pos = __p - _M_ibegin();
1067         _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1068         this->_M_set_leaked();
1069         return iterator(this->_M_data() + __pos);
1070       }
1071
1072       /**
1073        *  @brief  Remove characters.
1074        *  @param __pos  Index of first character to remove (default 0).
1075        *  @param __n  Number of characters to remove (default remainder).
1076        *  @return  Reference to this string.
1077        *  @throw  std::out_of_range  If @a __pos is beyond the end of this
1078        *  string.
1079        *
1080        *  Removes @a __n characters from this string starting at @a
1081        *  __pos.  The length of the string is reduced by @a __n.  If
1082        *  there are < @a __n characters to remove, the remainder of
1083        *  the string is truncated.  If @a __p is beyond end of string,
1084        *  out_of_range is thrown.  The value of the string doesn't
1085        *  change if an error is thrown.
1086       */
1087       __versa_string&
1088       erase(size_type __pos = 0, size_type __n = npos)
1089       { 
1090         this->_M_erase(_M_check(__pos, "__versa_string::erase"),
1091                        _M_limit(__pos, __n));
1092         return *this;
1093       }
1094
1095       /**
1096        *  @brief  Remove one character.
1097        *  @param __position  Iterator referencing the character to remove.
1098        *  @return  iterator referencing same location after removal.
1099        *
1100        *  Removes the character at @a __position from this string. The
1101        *  value of the string doesn't change if an error is thrown.
1102       */
1103       iterator
1104       erase(iterator __position)
1105       {
1106         _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
1107                                  && __position < _M_iend());
1108         const size_type __pos = __position - _M_ibegin();
1109         this->_M_erase(__pos, size_type(1));
1110         this->_M_set_leaked();
1111         return iterator(this->_M_data() + __pos);
1112       }
1113
1114       /**
1115        *  @brief  Remove a range of characters.
1116        *  @param __first  Iterator referencing the first character to remove.
1117        *  @param __last  Iterator referencing the end of the range.
1118        *  @return  Iterator referencing location of first after removal.
1119        *
1120        *  Removes the characters in the range [first,last) from this
1121        *  string.  The value of the string doesn't change if an error
1122        *  is thrown.
1123       */
1124       iterator
1125       erase(iterator __first, iterator __last)
1126       {
1127         _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
1128                                  && __last <= _M_iend());
1129         const size_type __pos = __first - _M_ibegin();
1130         this->_M_erase(__pos, __last - __first);
1131         this->_M_set_leaked();
1132         return iterator(this->_M_data() + __pos);
1133       }
1134
1135       /**
1136        *  @brief  Replace characters with value from another string.
1137        *  @param __pos  Index of first character to replace.
1138        *  @param __n  Number of characters to be replaced.
1139        *  @param __str  String to insert.
1140        *  @return  Reference to this string.
1141        *  @throw  std::out_of_range  If @a __pos is beyond the end of this
1142        *  string.
1143        *  @throw  std::length_error  If new length exceeds @c max_size().
1144        *
1145        *  Removes the characters in the range [pos,pos+n) from this
1146        *  string.  In place, the value of @a __str is inserted.  If @a
1147        *  __pos is beyond end of string, out_of_range is thrown.  If
1148        *  the length of the result exceeds max_size(), length_error is
1149        *  thrown.  The value of the string doesn't change if an error
1150        *  is thrown.
1151       */
1152       __versa_string&
1153       replace(size_type __pos, size_type __n, const __versa_string& __str)
1154       { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1155
1156       /**
1157        *  @brief  Replace characters with value from another string.
1158        *  @param __pos1  Index of first character to replace.
1159        *  @param __n1  Number of characters to be replaced.
1160        *  @param __str  String to insert.
1161        *  @param __pos2  Index of first character of str to use.
1162        *  @param __n2  Number of characters from str to use.
1163        *  @return  Reference to this string.
1164        *  @throw  std::out_of_range  If @a __pos1 > size() or @a __pos2 >
1165        *  str.size().
1166        *  @throw  std::length_error  If new length exceeds @c max_size().
1167        *
1168        *  Removes the characters in the range [pos1,pos1 + n) from
1169        *  this string.  In place, the value of @a __str is inserted.
1170        *  If @a __pos is beyond end of string, out_of_range is thrown.
1171        *  If the length of the result exceeds max_size(), length_error
1172        *  is thrown.  The value of the string doesn't change if an
1173        *  error is thrown.
1174       */
1175       __versa_string&
1176       replace(size_type __pos1, size_type __n1, const __versa_string& __str,
1177               size_type __pos2, size_type __n2)
1178       {
1179         return this->replace(__pos1, __n1, __str._M_data()
1180                              + __str._M_check(__pos2,
1181                                               "__versa_string::replace"),
1182                              __str._M_limit(__pos2, __n2));
1183       }
1184
1185       /**
1186        *  @brief  Replace characters with value of a C substring.
1187        *  @param __pos  Index of first character to replace.
1188        *  @param __n1  Number of characters to be replaced.
1189        *  @param __s  C string to insert.
1190        *  @param __n2  Number of characters from @a __s to use.
1191        *  @return  Reference to this string.
1192        *  @throw  std::out_of_range  If @a __pos1 > size().
1193        *  @throw  std::length_error  If new length exceeds @c max_size().
1194        *
1195        *  Removes the characters in the range [pos,pos + n1) from this
1196        *  string.  In place, the first @a __n2 characters of @a __s
1197        *  are inserted, or all of @a __s if @a __n2 is too large.  If
1198        *  @a __pos is beyond end of string, out_of_range is thrown.
1199        *  If the length of result exceeds max_size(), length_error is
1200        *  thrown.  The value of the string doesn't change if an error
1201        *  is thrown.
1202       */
1203       __versa_string&
1204       replace(size_type __pos, size_type __n1, const _CharT* __s,
1205               size_type __n2)
1206       {
1207         __glibcxx_requires_string_len(__s, __n2);
1208         return _M_replace(_M_check(__pos, "__versa_string::replace"),
1209                           _M_limit(__pos, __n1), __s, __n2);
1210       }
1211
1212       /**
1213        *  @brief  Replace characters with value of a C string.
1214        *  @param __pos  Index of first character to replace.
1215        *  @param __n1  Number of characters to be replaced.
1216        *  @param __s  C string to insert.
1217        *  @return  Reference to this string.
1218        *  @throw  std::out_of_range  If @a __pos > size().
1219        *  @throw  std::length_error  If new length exceeds @c max_size().
1220        *
1221        *  Removes the characters in the range [pos,pos + n1) from this
1222        *  string.  In place, the characters of @a __s are inserted.  If
1223        *  @a pos is beyond end of string, out_of_range is thrown.  If
1224        *  the length of result exceeds max_size(), length_error is thrown.  
1225        *  The value of the string doesn't change if an error is thrown.
1226       */
1227       __versa_string&
1228       replace(size_type __pos, size_type __n1, const _CharT* __s)
1229       {
1230         __glibcxx_requires_string(__s);
1231         return this->replace(__pos, __n1, __s, traits_type::length(__s));
1232       }
1233
1234       /**
1235        *  @brief  Replace characters with multiple characters.
1236        *  @param __pos  Index of first character to replace.
1237        *  @param __n1  Number of characters to be replaced.
1238        *  @param __n2  Number of characters to insert.
1239        *  @param __c  Character to insert.
1240        *  @return  Reference to this string.
1241        *  @throw  std::out_of_range  If @a __pos > size().
1242        *  @throw  std::length_error  If new length exceeds @c max_size().
1243        *
1244        *  Removes the characters in the range [pos,pos + n1) from this
1245        *  string.  In place, @a __n2 copies of @a __c are inserted.
1246        *  If @a __pos is beyond end of string, out_of_range is thrown.
1247        *  If the length of result exceeds max_size(), length_error is
1248        *  thrown.  The value of the string doesn't change if an error
1249        *  is thrown.
1250       */
1251       __versa_string&
1252       replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1253       { return _M_replace_aux(_M_check(__pos, "__versa_string::replace"),
1254                               _M_limit(__pos, __n1), __n2, __c); }
1255
1256       /**
1257        *  @brief  Replace range of characters with string.
1258        *  @param __i1  Iterator referencing start of range to replace.
1259        *  @param __i2  Iterator referencing end of range to replace.
1260        *  @param __str  String value to insert.
1261        *  @return  Reference to this string.
1262        *  @throw  std::length_error  If new length exceeds @c max_size().
1263        *
1264        *  Removes the characters in the range [i1,i2).  In place, the
1265        *  value of @a __str is inserted.  If the length of result
1266        *  exceeds max_size(), length_error is thrown.  The value of
1267        *  the string doesn't change if an error is thrown.
1268       */
1269       __versa_string&
1270       replace(iterator __i1, iterator __i2, const __versa_string& __str)
1271       { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
1272
1273       /**
1274        *  @brief  Replace range of characters with C substring.
1275        *  @param __i1  Iterator referencing start of range to replace.
1276        *  @param __i2  Iterator referencing end of range to replace.
1277        *  @param __s  C string value to insert.
1278        *  @param __n  Number of characters from s to insert.
1279        *  @return  Reference to this string.
1280        *  @throw  std::length_error  If new length exceeds @c max_size().
1281        *
1282        *  Removes the characters in the range [i1,i2).  In place, the
1283        *  first @a n characters of @a __s are inserted.  If the length
1284        *  of result exceeds max_size(), length_error is thrown.  The
1285        *  value of the string doesn't change if an error is thrown.
1286       */
1287       __versa_string&
1288       replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
1289       {
1290         _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1291                                  && __i2 <= _M_iend());
1292         return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
1293       }
1294
1295       /**
1296        *  @brief  Replace range of characters with C string.
1297        *  @param __i1  Iterator referencing start of range to replace.
1298        *  @param __i2  Iterator referencing end of range to replace.
1299        *  @param __s  C string value to insert.
1300        *  @return  Reference to this string.
1301        *  @throw  std::length_error  If new length exceeds @c max_size().
1302        *
1303        *  Removes the characters in the range [i1,i2).  In place, the
1304        *  characters of @a __s are inserted.  If the length of result
1305        *  exceeds max_size(), length_error is thrown.  The value of
1306        *  the string doesn't change if an error is thrown.
1307       */
1308       __versa_string&
1309       replace(iterator __i1, iterator __i2, const _CharT* __s)
1310       {
1311         __glibcxx_requires_string(__s);
1312         return this->replace(__i1, __i2, __s, traits_type::length(__s));
1313       }
1314
1315       /**
1316        *  @brief  Replace range of characters with multiple characters
1317        *  @param __i1  Iterator referencing start of range to replace.
1318        *  @param __i2  Iterator referencing end of range to replace.
1319        *  @param __n  Number of characters to insert.
1320        *  @param __c  Character to insert.
1321        *  @return  Reference to this string.
1322        *  @throw  std::length_error  If new length exceeds @c max_size().
1323        *
1324        *  Removes the characters in the range [i1,i2).  In place, @a
1325        *  __n copies of @a __c are inserted.  If the length of result
1326        *  exceeds max_size(), length_error is thrown.  The value of
1327        *  the string doesn't change if an error is thrown.
1328       */
1329       __versa_string&
1330       replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
1331       {
1332         _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1333                                  && __i2 <= _M_iend());
1334         return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
1335       }
1336
1337       /**
1338        *  @brief  Replace range of characters with range.
1339        *  @param __i1  Iterator referencing start of range to replace.
1340        *  @param __i2  Iterator referencing end of range to replace.
1341        *  @param __k1  Iterator referencing start of range to insert.
1342        *  @param __k2  Iterator referencing end of range to insert.
1343        *  @return  Reference to this string.
1344        *  @throw  std::length_error  If new length exceeds @c max_size().
1345        *
1346        *  Removes the characters in the range [i1,i2).  In place,
1347        *  characters in the range [k1,k2) are inserted.  If the length
1348        *  of result exceeds max_size(), length_error is thrown.  The
1349        *  value of the string doesn't change if an error is thrown.
1350       */
1351       template<class _InputIterator>
1352         __versa_string&
1353         replace(iterator __i1, iterator __i2,
1354                 _InputIterator __k1, _InputIterator __k2)
1355         {
1356           _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1357                                    && __i2 <= _M_iend());
1358           __glibcxx_requires_valid_range(__k1, __k2);
1359           typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1360           return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
1361         }
1362
1363       // Specializations for the common case of pointer and iterator:
1364       // useful to avoid the overhead of temporary buffering in _M_replace.
1365       __versa_string&
1366       replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
1367       {
1368         _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1369                                  && __i2 <= _M_iend());
1370         __glibcxx_requires_valid_range(__k1, __k2);
1371         return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1372                              __k1, __k2 - __k1);
1373       }
1374
1375       __versa_string&
1376       replace(iterator __i1, iterator __i2,
1377               const _CharT* __k1, const _CharT* __k2)
1378       {
1379         _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1380                                  && __i2 <= _M_iend());
1381         __glibcxx_requires_valid_range(__k1, __k2);
1382         return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1383                              __k1, __k2 - __k1);
1384       }
1385
1386       __versa_string&
1387       replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
1388       {
1389         _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1390                                  && __i2 <= _M_iend());
1391         __glibcxx_requires_valid_range(__k1, __k2);
1392         return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1393                              __k1.base(), __k2 - __k1);
1394       }
1395
1396       __versa_string&
1397       replace(iterator __i1, iterator __i2,
1398               const_iterator __k1, const_iterator __k2)
1399       {
1400         _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1401                                  && __i2 <= _M_iend());
1402         __glibcxx_requires_valid_range(__k1, __k2);
1403         return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1404                              __k1.base(), __k2 - __k1);
1405       }
1406       
1407 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1408       /**
1409        *  @brief  Replace range of characters with initializer_list.
1410        *  @param __i1  Iterator referencing start of range to replace.
1411        *  @param __i2  Iterator referencing end of range to replace.
1412        *  @param __l  The initializer_list of characters to insert.
1413        *  @return  Reference to this string.
1414        *  @throw  std::length_error  If new length exceeds @c max_size().
1415        *
1416        *  Removes the characters in the range [i1,i2).  In place,
1417        *  characters in the range [k1,k2) are inserted.  If the length
1418        *  of result exceeds max_size(), length_error is thrown.  The
1419        *  value of the string doesn't change if an error is thrown.
1420       */
1421       __versa_string& replace(iterator __i1, iterator __i2,
1422                               std::initializer_list<_CharT> __l)
1423       { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
1424 #endif // __GXX_EXPERIMENTAL_CXX0X__
1425
1426     private:
1427       template<class _Integer>
1428         __versa_string&
1429         _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
1430                             _Integer __val, std::__true_type)
1431         { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
1432
1433       template<class _InputIterator>
1434         __versa_string&
1435         _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
1436                             _InputIterator __k2, std::__false_type);
1437
1438       __versa_string&
1439       _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
1440                      _CharT __c);
1441
1442       __versa_string&
1443       _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
1444                  const size_type __len2);
1445
1446       __versa_string&
1447       _M_append(const _CharT* __s, size_type __n);
1448
1449     public:
1450
1451       /**
1452        *  @brief  Copy substring into C string.
1453        *  @param __s  C string to copy value into.
1454        *  @param __n  Number of characters to copy.
1455        *  @param __pos  Index of first character to copy.
1456        *  @return  Number of characters actually copied
1457        *  @throw  std::out_of_range  If pos > size().
1458        *
1459        *  Copies up to @a __n characters starting at @a __pos into the
1460        *  C string @a s.  If @a __pos is greater than size(),
1461        *  out_of_range is thrown.
1462       */
1463       size_type
1464       copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
1465
1466       /**
1467        *  @brief  Swap contents with another string.
1468        *  @param __s  String to swap with.
1469        *
1470        *  Exchanges the contents of this string with that of @a __s in
1471        *  constant time.
1472       */
1473       void
1474       swap(__versa_string& __s)
1475       { this->_M_swap(__s); }
1476
1477       // String operations:
1478       /**
1479        *  @brief  Return const pointer to null-terminated contents.
1480        *
1481        *  This is a handle to internal data.  Do not modify or dire things may
1482        *  happen.
1483       */
1484       const _CharT*
1485       c_str() const
1486       { return this->_M_data(); }
1487
1488       /**
1489        *  @brief  Return const pointer to contents.
1490        *
1491        *  This is a handle to internal data.  Do not modify or dire things may
1492        *  happen.
1493       */
1494       const _CharT*
1495       data() const
1496       { return this->_M_data(); }
1497
1498       /**
1499        *  @brief  Return copy of allocator used to construct this string.
1500       */
1501       allocator_type
1502       get_allocator() const
1503       { return allocator_type(this->_M_get_allocator()); }
1504
1505       /**
1506        *  @brief  Find position of a C substring.
1507        *  @param __s  C string to locate.
1508        *  @param __pos  Index of character to search from.
1509        *  @param __n  Number of characters from @a __s to search for.
1510        *  @return  Index of start of first occurrence.
1511        *
1512        *  Starting from @a __pos, searches forward for the first @a
1513        *  __n characters in @a __s within this string.  If found,
1514        *  returns the index where it begins.  If not found, returns
1515        *  npos.
1516       */
1517       size_type
1518       find(const _CharT* __s, size_type __pos, size_type __n) const;
1519
1520       /**
1521        *  @brief  Find position of a string.
1522        *  @param __str  String to locate.
1523        *  @param __pos  Index of character to search from (default 0).
1524        *  @return  Index of start of first occurrence.
1525        *
1526        *  Starting from @a __pos, searches forward for value of @a
1527        *  __str within this string.  If found, returns the index where
1528        *  it begins.  If not found, returns npos.
1529       */
1530       size_type
1531       find(const __versa_string& __str, size_type __pos = 0) const
1532       { return this->find(__str.data(), __pos, __str.size()); }
1533
1534       /**
1535        *  @brief  Find position of a C string.
1536        *  @param __s  C string to locate.
1537        *  @param __pos  Index of character to search from (default 0).
1538        *  @return  Index of start of first occurrence.
1539        *
1540        *  Starting from @a __pos, searches forward for the value of @a
1541        *  __s within this string.  If found, returns the index where
1542        *  it begins.  If not found, returns npos.
1543       */
1544       size_type
1545       find(const _CharT* __s, size_type __pos = 0) const
1546       {
1547         __glibcxx_requires_string(__s);
1548         return this->find(__s, __pos, traits_type::length(__s));
1549       }
1550
1551       /**
1552        *  @brief  Find position of a character.
1553        *  @param __c  Character to locate.
1554        *  @param __pos  Index of character to search from (default 0).
1555        *  @return  Index of first occurrence.
1556        *
1557        *  Starting from @a __pos, searches forward for @a __c within
1558        *  this string.  If found, returns the index where it was
1559        *  found.  If not found, returns npos.
1560       */
1561       size_type
1562       find(_CharT __c, size_type __pos = 0) const;
1563
1564       /**
1565        *  @brief  Find last position of a string.
1566        *  @param __str  String to locate.
1567        *  @param __pos  Index of character to search back from (default end).
1568        *  @return  Index of start of last occurrence.
1569        *
1570        *  Starting from @a __pos, searches backward for value of @a
1571        *  __str within this string.  If found, returns the index where
1572        *  it begins.  If not found, returns npos.
1573       */
1574       size_type
1575       rfind(const __versa_string& __str, size_type __pos = npos) const
1576       { return this->rfind(__str.data(), __pos, __str.size()); }
1577
1578       /**
1579        *  @brief  Find last position of a C substring.
1580        *  @param __s  C string to locate.
1581        *  @param __pos  Index of character to search back from.
1582        *  @param __n  Number of characters from s to search for.
1583        *  @return  Index of start of last occurrence.
1584        *
1585        *  Starting from @a __pos, searches backward for the first @a
1586        *  __n characters in @a __s within this string.  If found,
1587        *  returns the index where it begins.  If not found, returns
1588        *  npos.
1589       */
1590       size_type
1591       rfind(const _CharT* __s, size_type __pos, size_type __n) const;
1592
1593       /**
1594        *  @brief  Find last position of a C string.
1595        *  @param __s  C string to locate.
1596        *  @param __pos  Index of character to start search at (default end).
1597        *  @return  Index of start of  last occurrence.
1598        *
1599        *  Starting from @a __pos, searches backward for the value of
1600        *  @a __s within this string.  If found, returns the index
1601        *  where it begins.  If not found, returns npos.
1602       */
1603       size_type
1604       rfind(const _CharT* __s, size_type __pos = npos) const
1605       {
1606         __glibcxx_requires_string(__s);
1607         return this->rfind(__s, __pos, traits_type::length(__s));
1608       }
1609
1610       /**
1611        *  @brief  Find last position of a character.
1612        *  @param __c  Character to locate.
1613        *  @param __pos  Index of character to search back from (default end).
1614        *  @return  Index of last occurrence.
1615        *
1616        *  Starting from @a __pos, searches backward for @a __c within
1617        *  this string.  If found, returns the index where it was
1618        *  found.  If not found, returns npos.
1619       */
1620       size_type
1621       rfind(_CharT __c, size_type __pos = npos) const;
1622
1623       /**
1624        *  @brief  Find position of a character of string.
1625        *  @param __str  String containing characters to locate.
1626        *  @param __pos  Index of character to search from (default 0).
1627        *  @return  Index of first occurrence.
1628        *
1629        *  Starting from @a __pos, searches forward for one of the characters of
1630        *  @a __str within this string.  If found, returns the index where it was
1631        *  found.  If not found, returns npos.
1632       */
1633       size_type
1634       find_first_of(const __versa_string& __str, size_type __pos = 0) const
1635       { return this->find_first_of(__str.data(), __pos, __str.size()); }
1636
1637       /**
1638        *  @brief  Find position of a character of C substring.
1639        *  @param __s  String containing characters to locate.
1640        *  @param __pos  Index of character to search from.
1641        *  @param __n  Number of characters from s to search for.
1642        *  @return  Index of first occurrence.
1643        *
1644        *  Starting from @a __pos, searches forward for one of the
1645        *  first @a __n characters of @a __s within this string.  If
1646        *  found, returns the index where it was found.  If not found,
1647        *  returns npos.
1648       */
1649       size_type
1650       find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
1651
1652       /**
1653        *  @brief  Find position of a character of C string.
1654        *  @param __s  String containing characters to locate.
1655        *  @param __pos  Index of character to search from (default 0).
1656        *  @return  Index of first occurrence.
1657        *
1658        *  Starting from @a __pos, searches forward for one of the
1659        *  characters of @a __s within this string.  If found, returns
1660        *  the index where it was found.  If not found, returns npos.
1661       */
1662       size_type
1663       find_first_of(const _CharT* __s, size_type __pos = 0) const
1664       {
1665         __glibcxx_requires_string(__s);
1666         return this->find_first_of(__s, __pos, traits_type::length(__s));
1667       }
1668
1669       /**
1670        *  @brief  Find position of a character.
1671        *  @param __c  Character to locate.
1672        *  @param __pos  Index of character to search from (default 0).
1673        *  @return  Index of first occurrence.
1674        *
1675        *  Starting from @a __pos, searches forward for the character
1676        *  @a __c within this string.  If found, returns the index
1677        *  where it was found.  If not found, returns npos.
1678        *
1679        *  Note: equivalent to find(c, pos).
1680       */
1681       size_type
1682       find_first_of(_CharT __c, size_type __pos = 0) const
1683       { return this->find(__c, __pos); }
1684
1685       /**
1686        *  @brief  Find last position of a character of string.
1687        *  @param __str  String containing characters to locate.
1688        *  @param __pos  Index of character to search back from (default end).
1689        *  @return  Index of last occurrence.
1690        *
1691        *  Starting from @a __pos, searches backward for one of the
1692        *  characters of @a __str within this string.  If found,
1693        *  returns the index where it was found.  If not found, returns
1694        *  npos.
1695       */
1696       size_type
1697       find_last_of(const __versa_string& __str, size_type __pos = npos) const
1698       { return this->find_last_of(__str.data(), __pos, __str.size()); }
1699
1700       /**
1701        *  @brief  Find last position of a character of C substring.
1702        *  @param __s  C string containing characters to locate.
1703        *  @param __pos  Index of character to search back from.
1704        *  @param __n  Number of characters from s to search for.
1705        *  @return  Index of last occurrence.
1706        *
1707        *  Starting from @a __pos, searches backward for one of the
1708        *  first @a __n characters of @a __s within this string.  If
1709        *  found, returns the index where it was found.  If not found,
1710        *  returns npos.
1711       */
1712       size_type
1713       find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
1714
1715       /**
1716        *  @brief  Find last position of a character of C string.
1717        *  @param __s  C string containing characters to locate.
1718        *  @param __pos  Index of character to search back from (default end).
1719        *  @return  Index of last occurrence.
1720        *
1721        *  Starting from @a __pos, searches backward for one of the
1722        *  characters of @a __s within this string.  If found, returns
1723        *  the index where it was found.  If not found, returns npos.
1724       */
1725       size_type
1726       find_last_of(const _CharT* __s, size_type __pos = npos) const
1727       {
1728         __glibcxx_requires_string(__s);
1729         return this->find_last_of(__s, __pos, traits_type::length(__s));
1730       }
1731
1732       /**
1733        *  @brief  Find last position of a character.
1734        *  @param __c  Character to locate.
1735        *  @param __pos  Index of character to search back from (default end).
1736        *  @return  Index of last occurrence.
1737        *
1738        *  Starting from @a __pos, searches backward for @a __c within
1739        *  this string.  If found, returns the index where it was
1740        *  found.  If not found, returns npos.
1741        *
1742        *  Note: equivalent to rfind(c, pos).
1743       */
1744       size_type
1745       find_last_of(_CharT __c, size_type __pos = npos) const
1746       { return this->rfind(__c, __pos); }
1747
1748       /**
1749        *  @brief  Find position of a character not in string.
1750        *  @param __str  String containing characters to avoid.
1751        *  @param __pos  Index of character to search from (default 0).
1752        *  @return  Index of first occurrence.
1753        *
1754        *  Starting from @a __pos, searches forward for a character not
1755        *  contained in @a __str within this string.  If found, returns
1756        *  the index where it was found.  If not found, returns npos.
1757       */
1758       size_type
1759       find_first_not_of(const __versa_string& __str, size_type __pos = 0) const
1760       { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
1761
1762       /**
1763        *  @brief  Find position of a character not in C substring.
1764        *  @param __s  C string containing characters to avoid.
1765        *  @param __pos  Index of character to search from.
1766        *  @param __n  Number of characters from s to consider.
1767        *  @return  Index of first occurrence.
1768        *
1769        *  Starting from @a __pos, searches forward for a character not
1770        *  contained in the first @a __n characters of @a __s within
1771        *  this string.  If found, returns the index where it was
1772        *  found.  If not found, returns npos.
1773       */
1774       size_type
1775       find_first_not_of(const _CharT* __s, size_type __pos,
1776                         size_type __n) const;
1777
1778       /**
1779        *  @brief  Find position of a character not in C string.
1780        *  @param __s  C string containing characters to avoid.
1781        *  @param __pos  Index of character to search from (default 0).
1782        *  @return  Index of first occurrence.
1783        *
1784        *  Starting from @a __pos, searches forward for a character not
1785        *  contained in @a __s within this string.  If found, returns
1786        *  the index where it was found.  If not found, returns npos.
1787       */
1788       size_type
1789       find_first_not_of(const _CharT* __s, size_type __pos = 0) const
1790       {
1791         __glibcxx_requires_string(__s);
1792         return this->find_first_not_of(__s, __pos, traits_type::length(__s));
1793       }
1794
1795       /**
1796        *  @brief  Find position of a different character.
1797        *  @param __c  Character to avoid.
1798        *  @param __pos  Index of character to search from (default 0).
1799        *  @return  Index of first occurrence.
1800        *
1801        *  Starting from @a __pos, searches forward for a character
1802        *  other than @a __c within this string.  If found, returns the
1803        *  index where it was found.  If not found, returns npos.
1804       */
1805       size_type
1806       find_first_not_of(_CharT __c, size_type __pos = 0) const;
1807
1808       /**
1809        *  @brief  Find last position of a character not in string.
1810        *  @param __str  String containing characters to avoid.
1811        *  @param __pos  Index of character to search back from (default end).
1812        *  @return  Index of last occurrence.
1813        *
1814        *  Starting from @a __pos, searches backward for a character
1815        *  not contained in @a __str within this string.  If found,
1816        *  returns the index where it was found.  If not found, returns
1817        *  npos.
1818       */
1819       size_type
1820       find_last_not_of(const __versa_string& __str,
1821                        size_type __pos = npos) const
1822       { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
1823
1824       /**
1825        *  @brief  Find last position of a character not in C substring.
1826        *  @param __s  C string containing characters to avoid.
1827        *  @param __pos  Index of character to search back from.
1828        *  @param __n  Number of characters from s to consider.
1829        *  @return  Index of last occurrence.
1830        *
1831        *  Starting from @a __pos, searches backward for a character
1832        *  not contained in the first @a __n characters of @a __s
1833        *  within this string.  If found, returns the index where it
1834        *  was found.  If not found, returns npos.
1835       */
1836       size_type
1837       find_last_not_of(const _CharT* __s, size_type __pos,
1838                        size_type __n) const;
1839       /**
1840        *  @brief  Find last position of a character not in C string.
1841        *  @param __s  C string containing characters to avoid.
1842        *  @param __pos  Index of character to search back from (default end).
1843        *  @return  Index of last occurrence.
1844        *
1845        *  Starting from @a __pos, searches backward for a character
1846        *  not contained in @a __s within this string.  If found,
1847        *  returns the index where it was found.  If not found, returns
1848        *  npos.
1849       */
1850       size_type
1851       find_last_not_of(const _CharT* __s, size_type __pos = npos) const
1852       {
1853         __glibcxx_requires_string(__s);
1854         return this->find_last_not_of(__s, __pos, traits_type::length(__s));
1855       }
1856
1857       /**
1858        *  @brief  Find last position of a different character.
1859        *  @param __c  Character to avoid.
1860        *  @param __pos  Index of character to search back from (default end).
1861        *  @return  Index of last occurrence.
1862        *
1863        *  Starting from @a __pos, searches backward for a character
1864        *  other than @a __c within this string.  If found, returns the
1865        *  index where it was found.  If not found, returns npos.
1866       */
1867       size_type
1868       find_last_not_of(_CharT __c, size_type __pos = npos) const;
1869
1870       /**
1871        *  @brief  Get a substring.
1872        *  @param __pos  Index of first character (default 0).
1873        *  @param __n  Number of characters in substring (default remainder).
1874        *  @return  The new string.
1875        *  @throw  std::out_of_range  If pos > size().
1876        *
1877        *  Construct and return a new string using the @a __n
1878        *  characters starting at @a __pos.  If the string is too
1879        *  short, use the remainder of the characters.  If @a __pos is
1880        *  beyond the end of the string, out_of_range is thrown.
1881       */
1882       __versa_string
1883       substr(size_type __pos = 0, size_type __n = npos) const
1884       {
1885         return __versa_string(*this, _M_check(__pos, "__versa_string::substr"),
1886                               __n);
1887       }
1888
1889       /**
1890        *  @brief  Compare to a string.
1891        *  @param __str  String to compare against.
1892        *  @return  Integer < 0, 0, or > 0.
1893        *
1894        *  Returns an integer < 0 if this string is ordered before @a
1895        *  __str, 0 if their values are equivalent, or > 0 if this
1896        *  string is ordered after @a __str.  Determines the effective
1897        *  length rlen of the strings to compare as the smallest of
1898        *  size() and str.size().  The function then compares the two
1899        *  strings by calling traits::compare(data(), str.data(),rlen).
1900        *  If the result of the comparison is nonzero returns it,
1901        *  otherwise the shorter one is ordered first.
1902       */
1903       int
1904       compare(const __versa_string& __str) const
1905       {
1906         if (this->_M_compare(__str))
1907           return 0;
1908
1909         const size_type __size = this->size();
1910         const size_type __osize = __str.size();
1911         const size_type __len = std::min(__size, __osize);
1912
1913         int __r = traits_type::compare(this->_M_data(), __str.data(), __len);
1914         if (!__r)
1915           __r = _S_compare(__size, __osize);
1916         return __r;
1917       }
1918
1919       /**
1920        *  @brief  Compare substring to a string.
1921        *  @param __pos  Index of first character of substring.
1922        *  @param __n  Number of characters in substring.
1923        *  @param __str  String to compare against.
1924        *  @return  Integer < 0, 0, or > 0.
1925        *
1926        *  Form the substring of this string from the @a __n characters
1927        *  starting at @a __pos.  Returns an integer < 0 if the
1928        *  substring is ordered before @a __str, 0 if their values are
1929        *  equivalent, or > 0 if the substring is ordered after @a
1930        *  __str.  Determines the effective length rlen of the strings
1931        *  to compare as the smallest of the length of the substring
1932        *  and @a __str.size().  The function then compares the two
1933        *  strings by calling
1934        *  traits::compare(substring.data(),str.data(),rlen).  If the
1935        *  result of the comparison is nonzero returns it, otherwise
1936        *  the shorter one is ordered first.
1937       */
1938       int
1939       compare(size_type __pos, size_type __n,
1940               const __versa_string& __str) const;
1941
1942       /**
1943        *  @brief  Compare substring to a substring.
1944        *  @param __pos1  Index of first character of substring.
1945        *  @param __n1  Number of characters in substring.
1946        *  @param __str  String to compare against.
1947        *  @param __pos2  Index of first character of substring of str.
1948        *  @param __n2  Number of characters in substring of str.
1949        *  @return  Integer < 0, 0, or > 0.
1950        *
1951        *  Form the substring of this string from the @a __n1
1952        *  characters starting at @a __pos1.  Form the substring of @a
1953        *  __str from the @a __n2 characters starting at @a __pos2.
1954        *  Returns an integer < 0 if this substring is ordered before
1955        *  the substring of @a __str, 0 if their values are equivalent,
1956        *  or > 0 if this substring is ordered after the substring of
1957        *  @a __str.  Determines the effective length rlen of the
1958        *  strings to compare as the smallest of the lengths of the
1959        *  substrings.  The function then compares the two strings by
1960        *  calling
1961        *  traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
1962        *  If the result of the comparison is nonzero returns it,
1963        *  otherwise the shorter one is ordered first.
1964       */
1965       int
1966       compare(size_type __pos1, size_type __n1, const __versa_string& __str,
1967               size_type __pos2, size_type __n2) const;
1968
1969       /**
1970        *  @brief  Compare to a C string.
1971        *  @param __s  C string to compare against.
1972        *  @return  Integer < 0, 0, or > 0.
1973        *
1974        *  Returns an integer < 0 if this string is ordered before @a
1975        *  __s, 0 if their values are equivalent, or > 0 if this string
1976        *  is ordered after @a __s.  Determines the effective length
1977        *  rlen of the strings to compare as the smallest of size() and
1978        *  the length of a string constructed from @a __s.  The
1979        *  function then compares the two strings by calling
1980        *  traits::compare(data(),s,rlen).  If the result of the
1981        *  comparison is nonzero returns it, otherwise the shorter one
1982        *  is ordered first.
1983       */
1984       int
1985       compare(const _CharT* __s) const;
1986
1987       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1988       // 5 String::compare specification questionable
1989       /**
1990        *  @brief  Compare substring to a C string.
1991        *  @param __pos  Index of first character of substring.
1992        *  @param __n1  Number of characters in substring.
1993        *  @param __s  C string to compare against.
1994        *  @return  Integer < 0, 0, or > 0.
1995        *
1996        *  Form the substring of this string from the @a __n1
1997        *  characters starting at @a __pos.  Returns an integer < 0 if
1998        *  the substring is ordered before @a __s, 0 if their values
1999        *  are equivalent, or > 0 if the substring is ordered after @a
2000        *  __s.  Determines the effective length rlen of the strings to
2001        *  compare as the smallest of the length of the substring and
2002        *  the length of a string constructed from @a __s.  The
2003        *  function then compares the two string by calling
2004        *  traits::compare(substring.data(),s,rlen).  If the result of
2005        *  the comparison is nonzero returns it, otherwise the shorter
2006        *  one is ordered first.
2007       */
2008       int
2009       compare(size_type __pos, size_type __n1, const _CharT* __s) const;
2010
2011       /**
2012        *  @brief  Compare substring against a character array.
2013        *  @param __pos1  Index of first character of substring.
2014        *  @param __n1  Number of characters in substring.
2015        *  @param __s  character array to compare against.
2016        *  @param __n2  Number of characters of s.
2017        *  @return  Integer < 0, 0, or > 0.
2018        *
2019        *  Form the substring of this string from the @a __n1
2020        *  characters starting at @a __pos1.  Form a string from the
2021        *  first @a __n2 characters of @a __s.  Returns an integer < 0
2022        *  if this substring is ordered before the string from @a __s,
2023        *  0 if their values are equivalent, or > 0 if this substring
2024        *  is ordered after the string from @a __s.  Determines the
2025        *  effective length rlen of the strings to compare as the
2026        *  smallest of the length of the substring and @a __n2.  The
2027        *  function then compares the two strings by calling
2028        *  traits::compare(substring.data(),s,rlen).  If the result of
2029        *  the comparison is nonzero returns it, otherwise the shorter
2030        *  one is ordered first.
2031        *
2032        *  NB: s must have at least n2 characters, <em>\\0</em> has no special
2033        *  meaning.
2034       */
2035       int
2036       compare(size_type __pos, size_type __n1, const _CharT* __s,
2037               size_type __n2) const;
2038     };
2039
2040   // operator+
2041   /**
2042    *  @brief  Concatenate two strings.
2043    *  @param __lhs  First string.
2044    *  @param __rhs  Last string.
2045    *  @return  New string with value of @a __lhs followed by @a __rhs.
2046    */
2047   template<typename _CharT, typename _Traits, typename _Alloc,
2048            template <typename, typename, typename> class _Base>
2049     __versa_string<_CharT, _Traits, _Alloc, _Base>
2050     operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2051               const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs);
2052
2053   /**
2054    *  @brief  Concatenate C string and string.
2055    *  @param __lhs  First string.
2056    *  @param __rhs  Last string.
2057    *  @return  New string with value of @a __lhs followed by @a __rhs.
2058    */
2059   template<typename _CharT, typename _Traits, typename _Alloc,
2060            template <typename, typename, typename> class _Base>
2061     __versa_string<_CharT, _Traits, _Alloc, _Base>
2062     operator+(const _CharT* __lhs,
2063               const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs);
2064
2065   /**
2066    *  @brief  Concatenate character and string.
2067    *  @param __lhs  First string.
2068    *  @param __rhs  Last string.
2069    *  @return  New string with @a __lhs followed by @a __rhs.
2070    */
2071   template<typename _CharT, typename _Traits, typename _Alloc,
2072            template <typename, typename, typename> class _Base>
2073     __versa_string<_CharT, _Traits, _Alloc, _Base>
2074     operator+(_CharT __lhs,
2075               const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs);
2076
2077   /**
2078    *  @brief  Concatenate string and C string.
2079    *  @param __lhs  First string.
2080    *  @param __rhs  Last string.
2081    *  @return  New string with @a __lhs followed by @a __rhs.
2082    */
2083   template<typename _CharT, typename _Traits, typename _Alloc,
2084            template <typename, typename, typename> class _Base>
2085     __versa_string<_CharT, _Traits, _Alloc, _Base>
2086     operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2087               const _CharT* __rhs);
2088
2089   /**
2090    *  @brief  Concatenate string and character.
2091    *  @param __lhs  First string.
2092    *  @param __rhs  Last string.
2093    *  @return  New string with @a __lhs followed by @a __rhs.
2094    */
2095   template<typename _CharT, typename _Traits, typename _Alloc,
2096            template <typename, typename, typename> class _Base>
2097     __versa_string<_CharT, _Traits, _Alloc, _Base>
2098     operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2099               _CharT __rhs);
2100
2101 #ifdef __GXX_EXPERIMENTAL_CXX0X__
2102   template<typename _CharT, typename _Traits, typename _Alloc,
2103            template <typename, typename, typename> class _Base>
2104     inline __versa_string<_CharT, _Traits, _Alloc, _Base>
2105     operator+(__versa_string<_CharT, _Traits, _Alloc, _Base>&& __lhs,
2106               const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2107     { return std::move(__lhs.append(__rhs)); }
2108
2109   template<typename _CharT, typename _Traits, typename _Alloc,
2110            template <typename, typename, typename> class _Base>
2111     inline __versa_string<_CharT, _Traits, _Alloc, _Base>
2112     operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2113               __versa_string<_CharT, _Traits, _Alloc, _Base>&& __rhs)
2114     { return std::move(__rhs.insert(0, __lhs)); }
2115
2116   template<typename _CharT, typename _Traits, typename _Alloc,
2117            template <typename, typename, typename> class _Base>
2118     inline __versa_string<_CharT, _Traits, _Alloc, _Base>
2119     operator+(__versa_string<_CharT, _Traits, _Alloc, _Base>&& __lhs,
2120               __versa_string<_CharT, _Traits, _Alloc, _Base>&& __rhs)
2121     {
2122       const auto __size = __lhs.size() + __rhs.size();
2123       const bool __cond = (__size > __lhs.capacity()
2124                            && __size <= __rhs.capacity());
2125       return __cond ? std::move(__rhs.insert(0, __lhs))
2126                     : std::move(__lhs.append(__rhs));
2127     }
2128
2129   template<typename _CharT, typename _Traits, typename _Alloc,
2130            template <typename, typename, typename> class _Base>
2131     inline __versa_string<_CharT, _Traits, _Alloc, _Base>
2132     operator+(const _CharT* __lhs,
2133               __versa_string<_CharT, _Traits, _Alloc, _Base>&& __rhs)
2134     { return std::move(__rhs.insert(0, __lhs)); }
2135
2136   template<typename _CharT, typename _Traits, typename _Alloc,
2137            template <typename, typename, typename> class _Base>
2138     inline __versa_string<_CharT, _Traits, _Alloc, _Base>
2139     operator+(_CharT __lhs,
2140               __versa_string<_CharT, _Traits, _Alloc, _Base>&& __rhs)
2141     { return std::move(__rhs.insert(0, 1, __lhs)); }
2142
2143   template<typename _CharT, typename _Traits, typename _Alloc,
2144            template <typename, typename, typename> class _Base>
2145     inline __versa_string<_CharT, _Traits, _Alloc, _Base>
2146     operator+(__versa_string<_CharT, _Traits, _Alloc, _Base>&& __lhs,
2147               const _CharT* __rhs)
2148     { return std::move(__lhs.append(__rhs)); }
2149
2150   template<typename _CharT, typename _Traits, typename _Alloc,
2151            template <typename, typename, typename> class _Base>
2152     inline __versa_string<_CharT, _Traits, _Alloc, _Base>
2153     operator+(__versa_string<_CharT, _Traits, _Alloc, _Base>&& __lhs,
2154               _CharT __rhs)
2155     { return std::move(__lhs.append(1, __rhs)); }
2156 #endif
2157
2158   // operator ==
2159   /**
2160    *  @brief  Test equivalence of two strings.
2161    *  @param __lhs  First string.
2162    *  @param __rhs  Second string.
2163    *  @return  True if @a __lhs.compare(@a __rhs) == 0.  False otherwise.
2164    */
2165   template<typename _CharT, typename _Traits, typename _Alloc,
2166            template <typename, typename, typename> class _Base>
2167     inline bool
2168     operator==(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2169                const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2170     { return __lhs.compare(__rhs) == 0; }
2171
2172   template<typename _CharT,
2173            template <typename, typename, typename> class _Base>
2174     inline typename __enable_if<std::__is_char<_CharT>::__value, bool>::__type
2175     operator==(const __versa_string<_CharT, std::char_traits<_CharT>,
2176                std::allocator<_CharT>, _Base>& __lhs,
2177                const __versa_string<_CharT, std::char_traits<_CharT>,
2178                std::allocator<_CharT>, _Base>& __rhs)
2179     { return (__lhs.size() == __rhs.size()
2180               && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
2181                                                     __lhs.size())); }
2182
2183   /**
2184    *  @brief  Test equivalence of C string and string.
2185    *  @param __lhs  C string.
2186    *  @param __rhs  String.
2187    *  @return  True if @a __rhs.compare(@a __lhs) == 0.  False otherwise.
2188    */
2189   template<typename _CharT, typename _Traits, typename _Alloc,
2190            template <typename, typename, typename> class _Base>
2191     inline bool
2192     operator==(const _CharT* __lhs,
2193                const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2194     { return __rhs.compare(__lhs) == 0; }
2195
2196   /**
2197    *  @brief  Test equivalence of string and C string.
2198    *  @param __lhs  String.
2199    *  @param __rhs  C string.
2200    *  @return  True if @a __lhs.compare(@a __rhs) == 0.  False otherwise.
2201    */
2202   template<typename _CharT, typename _Traits, typename _Alloc,
2203            template <typename, typename, typename> class _Base>
2204     inline bool
2205     operator==(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2206                const _CharT* __rhs)
2207     { return __lhs.compare(__rhs) == 0; }
2208
2209   // operator !=
2210   /**
2211    *  @brief  Test difference of two strings.
2212    *  @param __lhs  First string.
2213    *  @param __rhs  Second string.
2214    *  @return  True if @a __lhs.compare(@a __rhs) != 0.  False otherwise.
2215    */
2216   template<typename _CharT, typename _Traits, typename _Alloc,
2217            template <typename, typename, typename> class _Base>
2218     inline bool
2219     operator!=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2220                const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2221     { return !(__lhs == __rhs); }
2222
2223   /**
2224    *  @brief  Test difference of C string and string.
2225    *  @param __lhs  C string.
2226    *  @param __rhs  String.
2227    *  @return  True if @a __rhs.compare(@a __lhs) != 0.  False otherwise.
2228    */
2229   template<typename _CharT, typename _Traits, typename _Alloc,
2230            template <typename, typename, typename> class _Base>
2231     inline bool
2232     operator!=(const _CharT* __lhs,
2233                const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2234     { return !(__lhs == __rhs); }
2235
2236   /**
2237    *  @brief  Test difference of string and C string.
2238    *  @param __lhs  String.
2239    *  @param __rhs  C string.
2240    *  @return  True if @a __lhs.compare(@a __rhs) != 0.  False otherwise.
2241    */
2242   template<typename _CharT, typename _Traits, typename _Alloc,
2243            template <typename, typename, typename> class _Base>
2244     inline bool
2245     operator!=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2246                const _CharT* __rhs)
2247     { return !(__lhs == __rhs); }
2248
2249   // operator <
2250   /**
2251    *  @brief  Test if string precedes string.
2252    *  @param __lhs  First string.
2253    *  @param __rhs  Second string.
2254    *  @return  True if @a __lhs precedes @a __rhs.  False otherwise.
2255    */
2256   template<typename _CharT, typename _Traits, typename _Alloc,
2257            template <typename, typename, typename> class _Base>
2258     inline bool
2259     operator<(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2260               const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2261     { return __lhs.compare(__rhs) < 0; }
2262
2263   /**
2264    *  @brief  Test if string precedes C string.
2265    *  @param __lhs  String.
2266    *  @param __rhs  C string.
2267    *  @return  True if @a __lhs precedes @a __rhs.  False otherwise.
2268    */
2269   template<typename _CharT, typename _Traits, typename _Alloc,
2270            template <typename, typename, typename> class _Base>
2271     inline bool
2272     operator<(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2273               const _CharT* __rhs)
2274     { return __lhs.compare(__rhs) < 0; }
2275
2276   /**
2277    *  @brief  Test if C string precedes string.
2278    *  @param __lhs  C string.
2279    *  @param __rhs  String.
2280    *  @return  True if @a __lhs precedes @a __rhs.  False otherwise.
2281    */
2282   template<typename _CharT, typename _Traits, typename _Alloc,
2283            template <typename, typename, typename> class _Base>
2284     inline bool
2285     operator<(const _CharT* __lhs,
2286               const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2287     { return __rhs.compare(__lhs) > 0; }
2288
2289   // operator >
2290   /**
2291    *  @brief  Test if string follows string.
2292    *  @param __lhs  First string.
2293    *  @param __rhs  Second string.
2294    *  @return  True if @a __lhs follows @a __rhs.  False otherwise.
2295    */
2296   template<typename _CharT, typename _Traits, typename _Alloc,
2297            template <typename, typename, typename> class _Base>
2298     inline bool
2299     operator>(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2300               const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2301     { return __lhs.compare(__rhs) > 0; }
2302
2303   /**
2304    *  @brief  Test if string follows C string.
2305    *  @param __lhs  String.
2306    *  @param __rhs  C string.
2307    *  @return  True if @a __lhs follows @a __rhs.  False otherwise.
2308    */
2309   template<typename _CharT, typename _Traits, typename _Alloc,
2310            template <typename, typename, typename> class _Base>
2311     inline bool
2312     operator>(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2313               const _CharT* __rhs)
2314     { return __lhs.compare(__rhs) > 0; }
2315
2316   /**
2317    *  @brief  Test if C string follows string.
2318    *  @param __lhs  C string.
2319    *  @param __rhs  String.
2320    *  @return  True if @a __lhs follows @a __rhs.  False otherwise.
2321    */
2322   template<typename _CharT, typename _Traits, typename _Alloc,
2323            template <typename, typename, typename> class _Base>
2324     inline bool
2325     operator>(const _CharT* __lhs,
2326               const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2327     { return __rhs.compare(__lhs) < 0; }
2328
2329   // operator <=
2330   /**
2331    *  @brief  Test if string doesn't follow string.
2332    *  @param __lhs  First string.
2333    *  @param __rhs  Second string.
2334    *  @return  True if @a __lhs doesn't follow @a __rhs.  False otherwise.
2335    */
2336   template<typename _CharT, typename _Traits, typename _Alloc,
2337            template <typename, typename, typename> class _Base>
2338     inline bool
2339     operator<=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2340                const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2341     { return __lhs.compare(__rhs) <= 0; }
2342
2343   /**
2344    *  @brief  Test if string doesn't follow C string.
2345    *  @param __lhs  String.
2346    *  @param __rhs  C string.
2347    *  @return  True if @a __lhs doesn't follow @a __rhs.  False otherwise.
2348    */
2349   template<typename _CharT, typename _Traits, typename _Alloc,
2350            template <typename, typename, typename> class _Base>
2351     inline bool
2352     operator<=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2353                const _CharT* __rhs)
2354     { return __lhs.compare(__rhs) <= 0; }
2355
2356   /**
2357    *  @brief  Test if C string doesn't follow string.
2358    *  @param __lhs  C string.
2359    *  @param __rhs  String.
2360    *  @return  True if @a __lhs doesn't follow @a __rhs.  False otherwise.
2361    */
2362   template<typename _CharT, typename _Traits, typename _Alloc,
2363            template <typename, typename, typename> class _Base>
2364     inline bool
2365     operator<=(const _CharT* __lhs,
2366                const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2367     { return __rhs.compare(__lhs) >= 0; }
2368
2369   // operator >=
2370   /**
2371    *  @brief  Test if string doesn't precede string.
2372    *  @param __lhs  First string.
2373    *  @param __rhs  Second string.
2374    *  @return  True if @a __lhs doesn't precede @a __rhs.  False otherwise.
2375    */
2376   template<typename _CharT, typename _Traits, typename _Alloc,
2377            template <typename, typename, typename> class _Base>
2378     inline bool
2379     operator>=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2380                const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2381     { return __lhs.compare(__rhs) >= 0; }
2382
2383   /**
2384    *  @brief  Test if string doesn't precede C string.
2385    *  @param __lhs  String.
2386    *  @param __rhs  C string.
2387    *  @return  True if @a __lhs doesn't precede @a __rhs.  False otherwise.
2388    */
2389   template<typename _CharT, typename _Traits, typename _Alloc,
2390            template <typename, typename, typename> class _Base>
2391     inline bool
2392     operator>=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2393                const _CharT* __rhs)
2394     { return __lhs.compare(__rhs) >= 0; }
2395
2396   /**
2397    *  @brief  Test if C string doesn't precede string.
2398    *  @param __lhs  C string.
2399    *  @param __rhs  String.
2400    *  @return  True if @a __lhs doesn't precede @a __rhs.  False otherwise.
2401    */
2402   template<typename _CharT, typename _Traits, typename _Alloc,
2403            template <typename, typename, typename> class _Base>
2404     inline bool
2405     operator>=(const _CharT* __lhs,
2406                const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2407     { return __rhs.compare(__lhs) <= 0; }
2408
2409   /**
2410    *  @brief  Swap contents of two strings.
2411    *  @param __lhs  First string.
2412    *  @param __rhs  Second string.
2413    *
2414    *  Exchanges the contents of @a __lhs and @a __rhs in constant time.
2415    */
2416   template<typename _CharT, typename _Traits, typename _Alloc,
2417            template <typename, typename, typename> class _Base>
2418     inline void
2419     swap(__versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
2420          __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
2421     { __lhs.swap(__rhs); }
2422
2423 _GLIBCXX_END_NAMESPACE
2424
2425 _GLIBCXX_BEGIN_NAMESPACE(std)
2426
2427   /**
2428    *  @brief  Read stream into a string.
2429    *  @param __is  Input stream.
2430    *  @param __str  Buffer to store into.
2431    *  @return  Reference to the input stream.
2432    *
2433    *  Stores characters from @a __is into @a __str until whitespace is
2434    *  found, the end of the stream is encountered, or str.max_size()
2435    *  is reached.  If is.width() is non-zero, that is the limit on the
2436    *  number of characters stored into @a __str.  Any previous
2437    *  contents of @a __str are erased.
2438    */
2439   template<typename _CharT, typename _Traits, typename _Alloc,
2440            template <typename, typename, typename> class _Base>
2441     basic_istream<_CharT, _Traits>&
2442     operator>>(basic_istream<_CharT, _Traits>& __is,
2443                __gnu_cxx::__versa_string<_CharT, _Traits,
2444                                          _Alloc, _Base>& __str);
2445
2446   /**
2447    *  @brief  Write string to a stream.
2448    *  @param __os  Output stream.
2449    *  @param __str  String to write out.
2450    *  @return  Reference to the output stream.
2451    *
2452    *  Output characters of @a __str into os following the same rules as for
2453    *  writing a C string.
2454    */
2455   template<typename _CharT, typename _Traits, typename _Alloc,
2456            template <typename, typename, typename> class _Base>
2457     inline basic_ostream<_CharT, _Traits>&
2458     operator<<(basic_ostream<_CharT, _Traits>& __os,
2459                const __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc,
2460                _Base>& __str)
2461     {
2462       // _GLIBCXX_RESOLVE_LIB_DEFECTS
2463       // 586. string inserter not a formatted function
2464       return __ostream_insert(__os, __str.data(), __str.size());
2465     }
2466
2467   /**
2468    *  @brief  Read a line from stream into a string.
2469    *  @param __is  Input stream.
2470    *  @param __str  Buffer to store into.
2471    *  @param __delim  Character marking end of line.
2472    *  @return  Reference to the input stream.
2473    *
2474    *  Stores characters from @a __is into @a __str until @a __delim is
2475    *  found, the end of the stream is encountered, or str.max_size()
2476    *  is reached.  If is.width() is non-zero, that is the limit on the
2477    *  number of characters stored into @a __str.  Any previous
2478    *  contents of @a __str are erased.  If @a delim was encountered,
2479    *  it is extracted but not stored into @a __str.
2480    */
2481   template<typename _CharT, typename _Traits, typename _Alloc,
2482            template <typename, typename, typename> class _Base>
2483     basic_istream<_CharT, _Traits>&
2484     getline(basic_istream<_CharT, _Traits>& __is,
2485             __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base>& __str,
2486             _CharT __delim);
2487
2488   /**
2489    *  @brief  Read a line from stream into a string.
2490    *  @param __is  Input stream.
2491    *  @param __str  Buffer to store into.
2492    *  @return  Reference to the input stream.
2493    *
2494    *  Stores characters from is into @a __str until &apos;\n&apos; is
2495    *  found, the end of the stream is encountered, or str.max_size()
2496    *  is reached.  If is.width() is non-zero, that is the limit on the
2497    *  number of characters stored into @a __str.  Any previous
2498    *  contents of @a __str are erased.  If end of line was
2499    *  encountered, it is extracted but not stored into @a __str.
2500    */
2501   template<typename _CharT, typename _Traits, typename _Alloc,
2502            template <typename, typename, typename> class _Base>
2503     inline basic_istream<_CharT, _Traits>&
2504     getline(basic_istream<_CharT, _Traits>& __is,
2505             __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base>& __str)
2506     { return getline(__is, __str, __is.widen('\n')); }      
2507
2508 _GLIBCXX_END_NAMESPACE
2509
2510 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) && defined(_GLIBCXX_USE_C99))
2511
2512 #include <ext/string_conversions.h>
2513
2514 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
2515
2516   // 21.4 Numeric Conversions [string.conversions].
2517   inline int
2518   stoi(const __vstring& __str, std::size_t* __idx = 0, int __base = 10)
2519   { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
2520                                         __idx, __base); }
2521
2522   inline long
2523   stol(const __vstring& __str, std::size_t* __idx = 0, int __base = 10)
2524   { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
2525                              __idx, __base); }
2526
2527   inline unsigned long
2528   stoul(const __vstring& __str, std::size_t* __idx = 0, int __base = 10)
2529   { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
2530                              __idx, __base); }
2531
2532   inline long long
2533   stoll(const __vstring& __str, std::size_t* __idx = 0, int __base = 10)
2534   { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
2535                              __idx, __base); }
2536
2537   inline unsigned long long
2538   stoull(const __vstring& __str, std::size_t* __idx, int __base = 10)
2539   { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
2540                              __idx, __base); }
2541
2542   // NB: strtof vs strtod.
2543   inline float
2544   stof(const __vstring& __str, std::size_t* __idx = 0)
2545   { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
2546
2547   inline double
2548   stod(const __vstring& __str, std::size_t* __idx = 0)
2549   { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
2550
2551   inline long double
2552   stold(const __vstring& __str, std::size_t* __idx = 0)
2553   { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
2554
2555   // NB: (v)snprintf vs sprintf.
2556
2557   // DR 1261.
2558   inline __vstring
2559   to_string(int __val)
2560   { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf, 4 * sizeof(int),
2561                                               "%d", __val); }
2562
2563   inline __vstring
2564   to_string(unsigned __val)
2565   { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf,
2566                                               4 * sizeof(unsigned),
2567                                               "%u", __val); }
2568
2569   inline __vstring
2570   to_string(long __val)
2571   { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf,
2572                                               4 * sizeof(long),
2573                                               "%ld", __val); }
2574
2575   inline __vstring
2576   to_string(unsigned long __val)
2577   { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf,
2578                                               4 * sizeof(unsigned long),
2579                                               "%lu", __val); }
2580
2581
2582   inline __vstring
2583   to_string(long long __val)
2584   { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf,
2585                                               4 * sizeof(long long),
2586                                               "%lld", __val); }
2587
2588   inline __vstring
2589   to_string(unsigned long long __val)
2590   { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf,
2591                                               4 * sizeof(unsigned long long),
2592                                               "%llu", __val); }
2593
2594   inline __vstring
2595   to_string(float __val)
2596   {
2597     const int __n = __numeric_traits<float>::__max_exponent10 + 20;
2598     return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf, __n,
2599                                               "%f", __val);
2600   }
2601
2602   inline __vstring
2603   to_string(double __val)
2604   {
2605     const int __n = __numeric_traits<double>::__max_exponent10 + 20;
2606     return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf, __n,
2607                                               "%f", __val);
2608   }
2609
2610   inline __vstring
2611   to_string(long double __val)
2612   {
2613     const int __n = __numeric_traits<long double>::__max_exponent10 + 20;
2614     return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf, __n,
2615                                               "%Lf", __val);
2616   }
2617
2618 #ifdef _GLIBCXX_USE_WCHAR_T
2619   inline int 
2620   stoi(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10)
2621   { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
2622                                         __idx, __base); }
2623
2624   inline long 
2625   stol(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10)
2626   { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
2627                              __idx, __base); }
2628
2629   inline unsigned long
2630   stoul(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10)
2631   { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
2632                              __idx, __base); }
2633
2634   inline long long
2635   stoll(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10)
2636   { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
2637                              __idx, __base); }
2638
2639   inline unsigned long long
2640   stoull(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10)
2641   { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
2642                              __idx, __base); }
2643
2644   // NB: wcstof vs wcstod.
2645   inline float
2646   stof(const __wvstring& __str, std::size_t* __idx = 0)
2647   { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
2648
2649   inline double
2650   stod(const __wvstring& __str, std::size_t* __idx = 0)
2651   { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
2652
2653   inline long double
2654   stold(const __wvstring& __str, std::size_t* __idx = 0)
2655   { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
2656
2657 #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
2658   // DR 1261.
2659   inline __wvstring
2660   to_wstring(int __val)
2661   { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
2662                                                4 * sizeof(int),
2663                                                L"%d", __val); }
2664
2665   inline __wvstring
2666   to_wstring(unsigned __val)
2667   { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
2668                                                4 * sizeof(unsigned),
2669                                                L"%u", __val); }
2670
2671   inline __wvstring
2672   to_wstring(long __val)
2673   { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
2674                                                4 * sizeof(long),
2675                                                L"%ld", __val); }
2676
2677   inline __wvstring
2678   to_wstring(unsigned long __val)
2679   { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
2680                                                4 * sizeof(unsigned long),
2681                                                L"%lu", __val); }
2682
2683   inline __wvstring
2684   to_wstring(long long __val)
2685   { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
2686                                                4 * sizeof(long long),
2687                                                L"%lld", __val); }
2688
2689   inline __wvstring
2690   to_wstring(unsigned long long __val)
2691   { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
2692                                                4 * sizeof(unsigned long long),
2693                                                L"%llu", __val); }
2694
2695   inline __wvstring
2696   to_wstring(float __val)
2697   {
2698     const int __n = __numeric_traits<float>::__max_exponent10 + 20;
2699     return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf, __n,
2700                                                L"%f", __val);
2701   }
2702
2703   inline __wvstring
2704   to_wstring(double __val)
2705   {
2706     const int __n = __numeric_traits<double>::__max_exponent10 + 20;
2707     return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf, __n,
2708                                                L"%f", __val);
2709   }
2710
2711   inline __wvstring
2712   to_wstring(long double __val)
2713   {
2714     const int __n = __numeric_traits<long double>::__max_exponent10 + 20;
2715     return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf, __n,
2716                                                L"%Lf", __val);
2717   }
2718 #endif
2719 #endif
2720
2721 _GLIBCXX_END_NAMESPACE
2722
2723 #endif
2724
2725 #ifndef _GLIBCXX_EXPORT_TEMPLATE
2726 # include "vstring.tcc" 
2727 #endif
2728
2729 #endif /* _VSTRING_H */