home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stlpt453.zip / STLport-4.5.3 / stlport / stl / _string.h < prev    next >
C/C++ Source or Header  |  2002-01-10  |  50KB  |  1,433 lines

  1. /*
  2.  * Copyright (c) 1997-1999
  3.  * Silicon Graphics Computer Systems, Inc.
  4.  *
  5.  * Copyright (c) 1999 
  6.  * Boris Fomitchev
  7.  *
  8.  * This material is provided "as is", with absolutely no warranty expressed
  9.  * or implied. Any use is at your own risk.
  10.  *
  11.  * Permission to use or copy this software for any purpose is hereby granted 
  12.  * without fee, provided the above notices are retained on all copies.
  13.  * Permission to modify the code and to distribute modified code is granted,
  14.  * provided the above notices are retained, and a notice that the code was
  15.  * modified is included with the above copyright notice.
  16.  *
  17.  */
  18.  
  19. #ifndef _STLP_STRING_H
  20. #define _STLP_STRING_H
  21.  
  22. #ifndef _STLP_MEMORY
  23. # include <memory> 
  24. #endif
  25.  
  26. # ifndef _STLP_CCTYPE
  27. #  include <cctype> 
  28. # endif
  29.  
  30. #ifndef _STLP_STRING_FWD_H
  31. #  include <stl/_string_fwd.h> 
  32. #endif
  33.  
  34. #ifndef _STLP_INTERNAL_FUNCTION_BASE_H
  35. # include <stl/_function.h> 
  36. #endif
  37.  
  38. # include <stl/_ctraits_fns.h>  
  39. #ifndef _STLP_INTERNAL_ALGOBASE_H
  40. # include <stl/_algobase.h> 
  41. #endif
  42.  
  43. #ifndef _STLP_INTERNAL_ITERATOR_H
  44. # include <stl/_iterator.h> 
  45. #endif
  46.  
  47. #if defined( __MWERKS__ ) && ! defined (_STLP_USE_OWN_NAMESPACE)
  48.  
  49. // MSL implementation classes expect to see the definition of streampos
  50. // when this header is included. We expect this to be fixed in later MSL
  51. // implementations
  52. # if !defined( __MSL_CPP__ ) || __MSL_CPP__ < 0x4105
  53. #  include <stl/msl_string.h> 
  54. # endif
  55.  
  56. #endif // __MWERKS__
  57.  
  58. // Standard C++ string class.  This class has performance
  59. // characteristics very much like vector<>, meaning, for example, that
  60. // it does not perform reference-count or copy-on-write, and that
  61. // concatenation of two strings is an O(N) operation. 
  62.  
  63. // There are three reasons why basic_string is not identical to
  64. // vector.  First, basic_string always stores a null character at the
  65. // end; this makes it possible for c_str to be a fast operation.
  66. // Second, the C++ standard requires basic_string to copy elements
  67. // using char_traits<>::assign, char_traits<>::copy, and
  68. // char_traits<>::move.  This means that all of vector<>'s low-level
  69. // operations must be rewritten.  Third, basic_string<> has a lot of
  70. // extra functions in its interface that are convenient but, strictly
  71. // speaking, redundant.
  72.  
  73. // Additionally, the C++ standard imposes a major restriction: according
  74. // to the standard, the character type _CharT must be a POD type.  This
  75. // implementation weakens that restriction, and allows _CharT to be a
  76. // a user-defined non-POD type.  However, _CharT must still have a
  77. // default constructor.
  78.  
  79. _STLP_BEGIN_NAMESPACE
  80.  
  81. # ifdef _STLP_DEBUG
  82. #  define basic_string _Nondebug_string
  83. # endif
  84.  
  85. // A helper class to use a char_traits as a function object.
  86.  
  87. template <class _Traits> struct _Not_within_traits
  88.   : public unary_function<typename _Traits::char_type, bool> {
  89.   typedef typename _Traits::char_type _CharT;
  90.   const _CharT* _M_first;
  91.   const _CharT* _M_last;
  92.  
  93.   _Not_within_traits(const typename _Traits::char_type* __f, 
  94.              const typename _Traits::char_type* __l) 
  95.     : _M_first(__f), _M_last(__l) {}
  96.  
  97.   bool operator()(const typename _Traits::char_type& __x) const {
  98.     return find_if((_CharT*)_M_first, (_CharT*)_M_last, 
  99.                    _Eq_char_bound<_Traits>(__x)) == (_CharT*)_M_last;
  100.   }
  101. };
  102.  
  103. // ------------------------------------------------------------
  104. // Class _String_base.  
  105.  
  106. // _String_base is a helper class that makes it it easier to write an
  107. // exception-safe version of basic_string.  The constructor allocates,
  108. // but does not initialize, a block of memory.  The destructor
  109. // deallocates, but does not destroy elements within, a block of
  110. // memory.  The destructor assumes that _M_start either is null, or else
  111. // points to a block of memory that was allocated using _String_base's 
  112. // allocator and whose size is _M_end_of_storage._M_data - _M_start.
  113.  
  114. template <class _Tp, class _Alloc> class _String_base {
  115. public:
  116.   _STLP_FORCE_ALLOCATORS(_Tp, _Alloc)
  117.   typedef typename _Alloc_traits<_Tp, _Alloc>::allocator_type allocator_type;
  118.   _Tp*    _M_start;
  119.   _Tp*    _M_finish;
  120.   _STLP_alloc_proxy<_Tp*, _Tp, allocator_type> _M_end_of_storage;
  121.                                 // Precondition: 0 < __n <= max_size().
  122.   void _M_allocate_block(size_t);
  123.   void _M_deallocate_block() 
  124.     { _M_end_of_storage.deallocate(_M_start, _M_end_of_storage._M_data - _M_start); }
  125.   
  126.   size_t max_size() const { return (size_t(-1) / sizeof(_Tp)) - 1; }
  127.  
  128.   _String_base(const allocator_type& __a)
  129.     : _M_start(0), _M_finish(0), _M_end_of_storage(__a, (_Tp*)0) {}
  130.   
  131.   _String_base(const allocator_type& __a, size_t __n)
  132.     : _M_start(0), _M_finish(0), _M_end_of_storage(__a, (_Tp*)0)
  133.     { _M_allocate_block(__n); }
  134.  
  135.   ~_String_base() { _M_deallocate_block(); }
  136.  
  137.   void _M_throw_length_error() const;
  138.   void _M_throw_out_of_range() const;
  139. };
  140.  
  141. # if defined (_STLP_USE_TEMPLATE_EXPORT)
  142. _STLP_EXPORT_TEMPLATE_CLASS _String_base<char, allocator<char> >;
  143. #  if defined (_STLP_HAS_WCHAR_T)
  144. _STLP_EXPORT_TEMPLATE_CLASS _String_base<wchar_t, allocator<wchar_t> >;
  145. #  endif
  146. # endif /* _STLP_USE_TEMPLATE_EXPORT */
  147.  
  148. // ------------------------------------------------------------
  149. // Class basic_string.  
  150.  
  151. // Class invariants:
  152. // (1) [start, finish) is a valid range.
  153. // (2) Each iterator in [start, finish) points to a valid object
  154. //     of type value_type.
  155. // (3) *finish is a valid object of type value_type; in particular,
  156. //     it is value_type().
  157. // (4) [finish + 1, end_of_storage) is a valid range.
  158. // (5) Each iterator in [finish + 1, end_of_storage) points to 
  159. //     unininitialized memory.
  160.  
  161. // Note one important consequence: a string of length n must manage
  162. // a block of memory whose size is at least n + 1.  
  163.  
  164. struct _String_reserve_t {};
  165.  
  166. template <class _CharT, class _Traits, class _Alloc> class basic_string : protected _String_base<_CharT,_Alloc> {
  167. private:                        // Protected members inherited from base.
  168.   typedef _String_base<_CharT,_Alloc> _Base;
  169.   typedef basic_string<_CharT, _Traits, _Alloc> _Self;
  170.   // fbp : used to optimize char/wchar_t cases, and to simplify
  171.   // _STLP_DEFAULT_CONSTRUCTOR_BUG problem workaround
  172.   typedef typename _Is_integer<_CharT>::_Integral _Char_Is_Integral;
  173. public:
  174.   typedef _CharT value_type;
  175.   typedef _Traits traits_type;
  176.  
  177.   typedef value_type* pointer;
  178.   typedef const value_type* const_pointer;
  179.   typedef value_type& reference;
  180.   typedef const value_type& const_reference;
  181.   typedef size_t size_type;
  182.   typedef ptrdiff_t difference_type;
  183.   typedef random_access_iterator_tag _Iterator_category;
  184.  
  185.   typedef const value_type*                const_iterator;
  186.   typedef value_type*                      iterator;
  187.  
  188.   _STLP_DECLARE_RANDOM_ACCESS_REVERSE_ITERATORS;
  189.  
  190. # ifdef _STLP_STATIC_CONST_INIT_BUG
  191.   enum { npos = -1 };
  192. # else
  193.   static const size_t npos = ~(size_t)0;
  194. # endif
  195.  
  196.   typedef _String_reserve_t _Reserve_t;
  197. # if defined (_STLP_USE_NATIVE_STRING) && ! defined (_STLP_DEBUG)
  198. #  if (defined(__IBMCPP__) && (500 <= __IBMCPP__) && (__IBMCPP__ < 600) )
  199.    // this typedef is being used for conversions
  200.    typedef typename _STLP_VENDOR_STD::basic_string<_CharT,_Traits, 
  201.     typename _STLP_VENDOR_STD::allocator<_CharT> > __std_string;
  202. #  else
  203.    // this typedef is being used for conversions
  204.    typedef _STLP_VENDOR_STD::basic_string<_CharT,_Traits, 
  205.     _STLP_VENDOR_STD::allocator<_CharT> > __std_string;
  206. #  endif
  207. # endif
  208.   
  209. public:                         // Constructor, destructor, assignment.
  210.   typedef typename _String_base<_CharT,_Alloc>::allocator_type allocator_type;
  211.  
  212.   allocator_type get_allocator() const {
  213.     return _STLP_CONVERT_ALLOCATOR((const allocator_type&)this->_M_end_of_storage, _CharT);
  214.   }
  215.  
  216.   basic_string();
  217.  
  218.   explicit basic_string(const allocator_type& __a)
  219.     : _String_base<_CharT,_Alloc>(__a, 8) { 
  220.     _M_terminate_string(); 
  221.   }
  222.  
  223.   basic_string(_Reserve_t, size_t __n,
  224.                const allocator_type& __a = allocator_type())
  225.     : _String_base<_CharT,_Alloc>(__a, __n + 1) { 
  226.     _M_terminate_string(); 
  227.   }
  228.  
  229.   basic_string(const basic_string<_CharT, _Traits, _Alloc>&);
  230.  
  231.   basic_string(const _Self& __s, size_type __pos, size_type __n = npos,
  232.                const allocator_type& __a = allocator_type()) 
  233.     : _String_base<_CharT,_Alloc>(__a) {
  234.     if (__pos > __s.size())
  235.       this->_M_throw_out_of_range();
  236.     else
  237.       _M_range_initialize(__s._M_start + __pos,
  238.                           __s._M_start + __pos + (min) (__n, __s.size() - __pos));
  239.   }
  240.  
  241.   basic_string(const _CharT* __s, size_type __n,
  242.                const allocator_type& __a = allocator_type()) 
  243.     : _String_base<_CharT,_Alloc>(__a) 
  244.     { 
  245.       _STLP_FIX_LITERAL_BUG(__s)
  246.       _M_range_initialize(__s, __s + __n); 
  247.     }
  248.  
  249.   basic_string(const _CharT* __s,
  250.                const allocator_type& __a = allocator_type());
  251.  
  252.   basic_string(size_type __n, _CharT __c,
  253.                const allocator_type& __a = allocator_type())
  254.     : _String_base<_CharT,_Alloc>(__a, __n + 1)
  255.   {
  256.     this->_M_finish = uninitialized_fill_n(this->_M_start, __n, __c);
  257.     _M_terminate_string();
  258.   }
  259.  
  260.   // Check to see if _InputIterator is an integer type.  If so, then
  261.   // it can't be an iterator.
  262. #if defined (_STLP_MEMBER_TEMPLATES) && !(defined(__MRC__)||defined(__SC__))        //*ty 04/30/2001 - mpw compilers choke on this ctor
  263. # ifdef _STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS
  264.   template <class _InputIterator> basic_string(_InputIterator __f, _InputIterator __l)
  265.     : _String_base<_CharT,_Alloc>(allocator_type())
  266.   {
  267.     typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
  268.     _M_initialize_dispatch(__f, __l, _Integral());
  269.   }
  270. # endif
  271.   template <class _InputIterator> basic_string(_InputIterator __f, _InputIterator __l,
  272.                const allocator_type & __a _STLP_ALLOCATOR_TYPE_DFL)
  273.     : _String_base<_CharT,_Alloc>(__a)
  274.   {
  275.     typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
  276.     _M_initialize_dispatch(__f, __l, _Integral());
  277.   }
  278. #else /* _STLP_MEMBER_TEMPLATES */
  279.  
  280.   basic_string(const _CharT* __f, const _CharT* __l,
  281.                const allocator_type& __a = allocator_type())
  282.     : _String_base<_CharT,_Alloc>(__a)
  283.   {
  284.     _STLP_FIX_LITERAL_BUG(__f)  _STLP_FIX_LITERAL_BUG(__l)
  285.     _M_range_initialize(__f, __l);
  286.   }
  287.  
  288. #endif
  289.  
  290. # if defined (_STLP_USE_NATIVE_STRING) && ! defined (_STLP_DEBUG)
  291.   // these conversion operations still needed for
  292.   // strstream, etc.
  293.   basic_string (const __std_string& __x): _String_base<_CharT,_Alloc>(allocator_type())
  294.     {
  295.       const _CharT* __s = __x.data();
  296.       _M_range_initialize(__s, __s + __x.size()); 
  297.     }
  298.   
  299.   operator __std_string() const { return __std_string(this->data(), this->size()); }
  300. # endif
  301.  
  302.   ~basic_string() { _Destroy(this->_M_start, this->_M_finish + 1); }
  303.     
  304.   _Self& operator=(const _Self& __s) {
  305.     if (&__s != this) 
  306.       assign(__s._M_start, __s._M_finish);
  307.     return *this;
  308.   }
  309.  
  310.   _Self& operator=(const _CharT* __s) { 
  311.     _STLP_FIX_LITERAL_BUG(__s)
  312.     return assign(__s, __s + traits_type::length(__s)); 
  313.   }
  314.  
  315.   _Self& operator=(_CharT __c)
  316.     { return assign(__STATIC_CAST(size_type,1), __c); }
  317.  
  318.   static _CharT _STLP_CALL _M_null() {
  319.     return _STLP_DEFAULT_CONSTRUCTED(_CharT);
  320.   }
  321.  
  322. private:                        // Helper functions used by constructors
  323.                                 // and elsewhere.
  324.   // fbp : simplify integer types (char, wchar)
  325.   void _M_construct_null_aux(_CharT* __p, const __false_type&) {
  326.     _Construct(__p);
  327.   }
  328.   void _M_construct_null_aux(_CharT* __p, const __true_type&) {
  329.     *__p = 0;
  330.   }
  331.  
  332.   void _M_construct_null(_CharT* __p) {
  333.     _M_construct_null_aux(__p, _Char_Is_Integral());
  334.   }
  335.  
  336. private:                        
  337.   // Helper functions used by constructors.  It is a severe error for
  338.   // any of them to be called anywhere except from within constructors.
  339.  
  340.   void _M_terminate_string_aux(const __false_type&) {
  341.     _STLP_TRY {
  342.       _M_construct_null(this->_M_finish);
  343.     }
  344.     _STLP_UNWIND(_Destroy(this->_M_start, this->_M_finish));
  345.   }
  346.  
  347.   void _M_terminate_string_aux(const __true_type&) {
  348.     *(this->_M_finish)=0;
  349.   }
  350.  
  351.   void _M_terminate_string() {
  352.     _M_terminate_string_aux(_Char_Is_Integral());
  353.   }
  354.  
  355. #ifdef _STLP_MEMBER_TEMPLATES
  356.     
  357.   template <class _InputIter> void _M_range_initialize(_InputIter __f, _InputIter __l,
  358.                            const input_iterator_tag &) {
  359.     this->_M_allocate_block(8);
  360.     _M_construct_null(this->_M_finish);
  361.     _STLP_TRY {
  362.       append(__f, __l);
  363.     }
  364.     _STLP_UNWIND(_Destroy(this->_M_start, this->_M_finish + 1));
  365.   }
  366.  
  367.   template <class _ForwardIter> void _M_range_initialize(_ForwardIter __f, _ForwardIter __l, 
  368.                            const forward_iterator_tag &) {
  369.     difference_type __n = distance(__f, __l);
  370.     this->_M_allocate_block(__n + 1);
  371.     this->_M_finish = uninitialized_copy(__f, __l, this->_M_start);
  372.     _M_terminate_string();
  373.   }
  374.  
  375.   template <class _InputIter> void _M_range_initialize(_InputIter __f, _InputIter __l) {
  376.     _M_range_initialize(__f, __l, _STLP_ITERATOR_CATEGORY(__f, _InputIter));
  377.   }
  378.  
  379.   template <class _Integer> void _M_initialize_dispatch(_Integer __n, _Integer __x, const __true_type&) {
  380.     this->_M_allocate_block(__n + 1);
  381.     this->_M_finish = uninitialized_fill_n(this->_M_start, __n, __x);
  382.     _M_terminate_string();
  383.   }
  384.  
  385.   template <class _InputIter> void _M_initialize_dispatch(_InputIter __f, _InputIter __l, const __false_type&) {
  386.      _M_range_initialize(__f, __l);
  387.   }
  388.     
  389. #else /* _STLP_MEMBER_TEMPLATES */
  390.  
  391.   void _M_range_initialize(const _CharT* __f, const _CharT* __l) {
  392.     ptrdiff_t __n = __l - __f;
  393.     this->_M_allocate_block(__n + 1);
  394.     this->_M_finish = uninitialized_copy(__f, __l, this->_M_start);
  395.     _M_terminate_string();
  396.   }
  397.  
  398. #endif /* _STLP_MEMBER_TEMPLATES */
  399.  
  400. public:                         // Iterators.
  401.   iterator begin()             { return this->_M_start; }
  402.   iterator end()               { return this->_M_finish; }
  403.   const_iterator begin() const { return this->_M_start; }
  404.   const_iterator end()   const { return this->_M_finish; }  
  405.  
  406.   reverse_iterator rbegin()             
  407.     { return reverse_iterator(this->_M_finish); }
  408.   reverse_iterator rend()               
  409.     { return reverse_iterator(this->_M_start); }
  410.   const_reverse_iterator rbegin() const 
  411.     { return const_reverse_iterator(this->_M_finish); }
  412.   const_reverse_iterator rend()   const 
  413.     { return const_reverse_iterator(this->_M_start); }
  414.  
  415. public:                         // Size, capacity, etc.
  416.   size_type size() const { return this->_M_finish - this->_M_start; }
  417.   size_type length() const { return size(); }
  418.  
  419.   size_t max_size() const { return _Base::max_size(); }
  420.  
  421.  
  422.   void resize(size_type __n, _CharT __c) {
  423.     if (__n <= size())
  424.       erase(begin() + __n, end());
  425.     else
  426.       append(__n - size(), __c);
  427.   }
  428.   void resize(size_type __n) { resize(__n, _M_null()); }
  429.  
  430.   void reserve(size_type = 0);
  431.  
  432.   size_type capacity() const { return (this->_M_end_of_storage._M_data - this->_M_start) - 1; }
  433.  
  434.   void clear() {
  435.     if (!empty()) {
  436.       _Traits::assign(*(this->_M_start), _M_null());
  437.       _Destroy(this->_M_start+1, this->_M_finish+1);
  438.       this->_M_finish = this->_M_start;
  439.     }
  440.   } 
  441.  
  442.   bool empty() const { return this->_M_start == this->_M_finish; }    
  443.  
  444. public:                         // Element access.
  445.  
  446.   const_reference operator[](size_type __n) const
  447.     { return *(this->_M_start + __n); }
  448.   reference operator[](size_type __n)
  449.     { return *(this->_M_start + __n); }
  450.  
  451.   const_reference at(size_type __n) const {
  452.     if (__n >= size())
  453.       this->_M_throw_out_of_range();
  454.     return *(this->_M_start + __n);
  455.   }
  456.  
  457.   reference at(size_type __n) {
  458.     if (__n >= size())
  459.       this->_M_throw_out_of_range();
  460.     return *(this->_M_start + __n);
  461.   }
  462.  
  463. public:                         // Append, operator+=, push_back.
  464.  
  465.   _Self& operator+=(const _Self& __s) { return append(__s); }
  466.   _Self& operator+=(const _CharT* __s) { _STLP_FIX_LITERAL_BUG(__s) return append(__s); }
  467.   _Self& operator+=(_CharT __c) { push_back(__c); return *this; }
  468.  
  469.   _Self& append(const _Self& __s) 
  470.     { return append(__s._M_start, __s._M_finish); }
  471.  
  472.   _Self& append(const _Self& __s,
  473.                        size_type __pos, size_type __n)
  474.   {
  475.     if (__pos > __s.size())
  476.       this->_M_throw_out_of_range();
  477.     return append(__s._M_start + __pos,
  478.                   __s._M_start + __pos + (min) (__n, __s.size() - __pos));
  479.   }
  480.  
  481.   _Self& append(const _CharT* __s, size_type __n) 
  482.     { _STLP_FIX_LITERAL_BUG(__s) return append(__s, __s+__n); }
  483.   _Self& append(const _CharT* __s) 
  484.     { _STLP_FIX_LITERAL_BUG(__s) return append(__s, __s + traits_type::length(__s)); }
  485.   _Self& append(size_type __n, _CharT __c);
  486.  
  487. #ifdef _STLP_MEMBER_TEMPLATES
  488.  
  489.   // Check to see if _InputIterator is an integer type.  If so, then
  490.   // it can't be an iterator.
  491.   template <class _InputIter> _Self& append(_InputIter __first, _InputIter __last) {
  492.     typedef typename _Is_integer<_InputIter>::_Integral _Integral;
  493.     return _M_append_dispatch(__first, __last, _Integral());
  494.   }
  495.  
  496. #else /* _STLP_MEMBER_TEMPLATES */
  497.  
  498.   _Self& append(const _CharT* __first, const _CharT* __last);
  499.  
  500. #endif /* _STLP_MEMBER_TEMPLATES */
  501.  
  502.   void push_back(_CharT __c) {
  503.     if (this->_M_finish + 1 == this->_M_end_of_storage._M_data)
  504.       reserve(size() + (max)(size(), __STATIC_CAST(size_type,1)));
  505.     _M_construct_null(this->_M_finish + 1);
  506.     _Traits::assign(*(this->_M_finish), __c);
  507.     ++this->_M_finish;
  508.   }
  509.  
  510.   void pop_back() {
  511.     _Traits::assign(*(this->_M_finish - 1), _M_null());
  512.     _Destroy(this->_M_finish);
  513.     --this->_M_finish;
  514.   }
  515.  
  516. private:                        // Helper functions for append.
  517.  
  518. #ifdef _STLP_MEMBER_TEMPLATES
  519.  
  520.   template <class _InputIter> _Self& append(_InputIter __first, _InputIter __last, const input_iterator_tag &)
  521.   {
  522.       for ( ; __first != __last ; ++__first)
  523.         push_back(*__first);
  524.       return *this;
  525.     }
  526.  
  527.   template <class _ForwardIter> _Self& append(_ForwardIter __first, _ForwardIter __last, 
  528.                        const forward_iterator_tag &)  {
  529.     if (__first != __last) {
  530.         const size_type __old_size = size();
  531.         difference_type __n = distance(__first, __last);
  532.         if (__STATIC_CAST(size_type,__n) > max_size() || __old_size > max_size() - __STATIC_CAST(size_type,__n))
  533.           this->_M_throw_length_error();
  534.         if (__old_size + __n > capacity()) {
  535.           const size_type __len = __old_size +
  536.                                 (max)(__old_size, __STATIC_CAST(size_type,__n)) + 1;
  537.           pointer __new_start = this->_M_end_of_storage.allocate(__len);
  538.           pointer __new_finish = __new_start;
  539.           _STLP_TRY {
  540.             __new_finish = uninitialized_copy(this->_M_start, this->_M_finish, __new_start);
  541.             __new_finish = uninitialized_copy(__first, __last, __new_finish);
  542.             _M_construct_null(__new_finish);
  543.           }
  544.           _STLP_UNWIND((_Destroy(__new_start,__new_finish),
  545.                         this->_M_end_of_storage.deallocate(__new_start,__len)));
  546.           _Destroy(this->_M_start, this->_M_finish + 1);
  547.           this->_M_deallocate_block();
  548.           this->_M_start = __new_start;
  549.           this->_M_finish = __new_finish;
  550.           this->_M_end_of_storage._M_data = __new_start + __len; 
  551.         }
  552.         else {
  553.           _ForwardIter __f1 = __first;
  554.           ++__f1;
  555.           uninitialized_copy(__f1, __last, this->_M_finish + 1);
  556.           _STLP_TRY {
  557.             _M_construct_null(this->_M_finish + __n);
  558.           }
  559.           _STLP_UNWIND(_Destroy(this->_M_finish + 1, this->_M_finish + __n));
  560.           _Traits::assign(*end(), *__first);
  561.           this->_M_finish += __n;
  562.         }
  563.       }
  564.       return *this;  
  565.     }
  566.  
  567.   template <class _Integer> _Self& _M_append_dispatch(_Integer __n, _Integer __x, const __true_type&) {
  568.     return append((size_type) __n, (_CharT) __x);
  569.   }
  570.  
  571.   template <class _InputIter> _Self& _M_append_dispatch(_InputIter __f, _InputIter __l,
  572.                                    const __false_type&) {
  573.     return append(__f, __l, _STLP_ITERATOR_CATEGORY(__f, _InputIter));
  574.   }
  575.  
  576. #endif /* _STLP_MEMBER_TEMPLATES */
  577.  
  578. public:                         // Assign
  579.   
  580.   _Self& assign(const _Self& __s) 
  581.     { return assign(__s._M_start, __s._M_finish); }
  582.  
  583.   _Self& assign(const _Self& __s, 
  584.                        size_type __pos, size_type __n) {
  585.     if (__pos > __s.size())
  586.       this->_M_throw_out_of_range();
  587.     return assign(__s._M_start + __pos, 
  588.                   __s._M_start + __pos + (min) (__n, __s.size() - __pos));
  589.   }
  590.  
  591.   _Self& assign(const _CharT* __s, size_type __n)
  592.     { _STLP_FIX_LITERAL_BUG(__s) return assign(__s, __s + __n); }
  593.  
  594.   _Self& assign(const _CharT* __s)
  595.     { _STLP_FIX_LITERAL_BUG(__s) return assign(__s, __s + _Traits::length(__s)); }
  596.  
  597.   _Self& assign(size_type __n, _CharT __c);
  598.  
  599. #ifdef _STLP_MEMBER_TEMPLATES
  600.  
  601. private:                        // Helper functions for assign.
  602.  
  603.   template <class _Integer> 
  604.   _Self& _M_assign_dispatch(_Integer __n, _Integer __x, const __true_type&) {
  605.     return assign((size_type) __n, (_CharT) __x);
  606.   }
  607.  
  608.   template <class _InputIter> 
  609.   _Self& _M_assign_dispatch(_InputIter __f, _InputIter __l,
  610.                 const __false_type&)  {
  611.     pointer __cur = this->_M_start;
  612.     while (__f != __l && __cur != this->_M_finish) {
  613.       _Traits::assign(*__cur, *__f);
  614.       ++__f;
  615.       ++__cur;
  616.     }
  617.     if (__f == __l)
  618.       erase(__cur, end());
  619.     else
  620.       append(__f, __l);
  621.     return *this;
  622.   }
  623.   
  624. public:
  625.   // Check to see if _InputIterator is an integer type.  If so, then
  626.   // it can't be an iterator.
  627.   template <class _InputIter> _Self& assign(_InputIter __first, _InputIter __last) {
  628.     typedef typename _Is_integer<_InputIter>::_Integral _Integral;
  629.     return _M_assign_dispatch(__first, __last, _Integral());
  630.   }
  631. #endif  /* _STLP_MEMBER_TEMPLATES */
  632.  
  633.   // if member templates are on, this works as specialization 
  634.   _Self& assign(const _CharT* __f, const _CharT* __l)
  635.   {
  636.     ptrdiff_t __n = __l - __f;
  637.     if (__STATIC_CAST(size_type,__n) <= size()) {
  638.       _Traits::copy(this->_M_start, __f, __n);
  639.       erase(begin() + __n, end());
  640.     }
  641.     else {
  642.       _Traits::copy(this->_M_start, __f, size());
  643.       append(__f + size(), __l);
  644.     }
  645.     return *this;
  646.   }
  647.   
  648. public:                         // Insert
  649.  
  650.   _Self& insert(size_type __pos, const _Self& __s) {
  651.     if (__pos > size())
  652.       this->_M_throw_out_of_range();
  653.     if (size() > max_size() - __s.size())
  654.       this->_M_throw_length_error();
  655.     insert(begin() + __pos, __s._M_start, __s._M_finish);
  656.     return *this;
  657.   }
  658.  
  659.   _Self& insert(size_type __pos, const _Self& __s,
  660.                        size_type __beg, size_type __n) {
  661.     if (__pos > size() || __beg > __s.size())
  662.       this->_M_throw_out_of_range();
  663.     size_type __len = (min) (__n, __s.size() - __beg);
  664.     if (size() > max_size() - __len)
  665.       this->_M_throw_length_error();
  666.     insert(begin() + __pos,
  667.            __s._M_start + __beg, __s._M_start + __beg + __len);
  668.     return *this;
  669.   }
  670.  
  671.   _Self& insert(size_type __pos, const _CharT* __s, size_type __n) {
  672.     _STLP_FIX_LITERAL_BUG(__s)
  673.     if (__pos > size())
  674.       this->_M_throw_out_of_range();
  675.     if (size() > max_size() - __n)
  676.       this->_M_throw_length_error();
  677.     insert(begin() + __pos, __s, __s + __n);
  678.     return *this;
  679.   }
  680.  
  681.   _Self& insert(size_type __pos, const _CharT* __s) {
  682.     _STLP_FIX_LITERAL_BUG(__s)
  683.     if (__pos > size())
  684.       this->_M_throw_out_of_range();
  685.     size_type __len = _Traits::length(__s);
  686.     if (size() > max_size() - __len)
  687.       this->_M_throw_length_error();
  688.     insert(this->_M_start + __pos, __s, __s + __len);
  689.     return *this;
  690.   }
  691.     
  692.   _Self& insert(size_type __pos, size_type __n, _CharT __c) {
  693.     if (__pos > size())
  694.       this->_M_throw_out_of_range();
  695.     if (size() > max_size() - __n)
  696.       this->_M_throw_length_error();
  697.     insert(begin() + __pos, __n, __c);
  698.     return *this;
  699.   }
  700.  
  701.   iterator insert(iterator __p, _CharT __c) {
  702.     _STLP_FIX_LITERAL_BUG(__p)
  703.     if (__p == end()) {
  704.       push_back(__c);
  705.       return this->_M_finish - 1;
  706.     }
  707.     else
  708.       return _M_insert_aux(__p, __c);
  709.   }
  710.  
  711.   void insert(iterator __p, size_t __n, _CharT __c);
  712.  
  713. #ifdef _STLP_MEMBER_TEMPLATES
  714.  
  715.   // Check to see if _InputIterator is an integer type.  If so, then
  716.   // it can't be an iterator.
  717.   template <class _InputIter> void insert(iterator __p, _InputIter __first, _InputIter __last) {
  718.     typedef typename _Is_integer<_InputIter>::_Integral _Integral;
  719.     _M_insert_dispatch(__p, __first, __last, _Integral());
  720.   }
  721.  
  722. #else /* _STLP_MEMBER_TEMPLATES */
  723.  
  724.   void insert(iterator __p, const _CharT* __first, const _CharT* __last);
  725.  
  726. #endif /* _STLP_MEMBER_TEMPLATES */
  727.  
  728. private:                        // Helper functions for insert.
  729.  
  730. #ifdef _STLP_MEMBER_TEMPLATES
  731.  
  732.   template <class _InputIter> void insert(iterator __p, _InputIter __first, _InputIter __last,
  733.           const input_iterator_tag &)
  734.   {
  735.       for ( ; __first != __last; ++__first) {
  736.         __p = insert(__p, *__first);
  737.         ++__p;
  738.       }
  739.     }
  740.  
  741.   template <class _ForwardIter> void insert(iterator __position, _ForwardIter __first, _ForwardIter __last, 
  742.           const forward_iterator_tag &)  {
  743.     if (__first != __last) {
  744.       difference_type __n = distance(__first, __last);
  745.       if (this->_M_end_of_storage._M_data - this->_M_finish >= __n + 1) {
  746.     const difference_type __elems_after = this->_M_finish - __position;
  747.     pointer __old_finish = this->_M_finish;
  748.     if (__elems_after >= __n) {
  749.       uninitialized_copy((this->_M_finish - __n) + 1, this->_M_finish + 1,
  750.                  this->_M_finish + 1);
  751.       this->_M_finish += __n;
  752.       _Traits::move(__position + __n,
  753.             __position, (__elems_after - __n) + 1);
  754.       _M_copy(__first, __last, __position);
  755.           }
  756.     else {
  757.       _ForwardIter __mid = __first;
  758.       advance(__mid, __elems_after + 1);
  759.       uninitialized_copy(__mid, __last, this->_M_finish + 1);
  760.       this->_M_finish += __n - __elems_after;
  761.             _STLP_TRY {
  762.               uninitialized_copy(__position, __old_finish + 1, this->_M_finish);
  763.               this->_M_finish += __elems_after;
  764.             }
  765.             _STLP_UNWIND((_Destroy(__old_finish + 1, this->_M_finish), 
  766.                           this->_M_finish = __old_finish));
  767.             _M_copy(__first, __mid, __position);
  768.     }
  769.       }
  770.       else {
  771.     const size_type __old_size = size();        
  772.     const size_type __len
  773.       = __old_size + (max)(__old_size, __STATIC_CAST(size_type,__n)) + 1;
  774.           pointer __new_start = this->_M_end_of_storage.allocate(__len);
  775.           pointer __new_finish = __new_start;
  776.           _STLP_TRY {
  777.             __new_finish = uninitialized_copy(this->_M_start, __position, __new_start);
  778.             __new_finish = uninitialized_copy(__first, __last, __new_finish);
  779.             __new_finish
  780.               = uninitialized_copy(__position, this->_M_finish, __new_finish);
  781.             _M_construct_null(__new_finish);
  782.           }
  783.           _STLP_UNWIND((_Destroy(__new_start,__new_finish),
  784.                         this->_M_end_of_storage.deallocate(__new_start,__len)));
  785.           _Destroy(this->_M_start, this->_M_finish + 1);
  786.           this->_M_deallocate_block();
  787.           this->_M_start = __new_start;
  788.           this->_M_finish = __new_finish;
  789.           this->_M_end_of_storage._M_data = __new_start + __len; 
  790.         }
  791.     }
  792.   }
  793.  
  794.   template <class _Integer> void _M_insert_dispatch(iterator __p, _Integer __n, _Integer __x,
  795.                           const __true_type&) {
  796.     insert(__p, (size_type) __n, (_CharT) __x);
  797.   }
  798.  
  799.   template <class _InputIter> void _M_insert_dispatch(iterator __p, _InputIter __first, _InputIter __last,
  800.                           const __false_type&) {
  801.     insert(__p, __first, __last, _STLP_ITERATOR_CATEGORY(__first, _InputIter));
  802.   }
  803.  
  804.   template <class _InputIterator> void 
  805.   _M_copy(_InputIterator __first, _InputIterator __last, pointer __result) {
  806.     for ( ; __first != __last; ++__first, ++__result)
  807.       _Traits::assign(*__result, *__first);
  808.   }
  809.  
  810. #endif /* _STLP_MEMBER_TEMPLATES */
  811.  
  812.   pointer _M_insert_aux(pointer, _CharT);
  813.  
  814.   void 
  815.   _M_copy(const _CharT* __first, const _CharT* __last, _CharT* __result) {
  816.     _Traits::copy(__result, __first, __last - __first);
  817.   }
  818.  
  819. public:                         // Erase.
  820.  
  821.   _Self& erase(size_type __pos = 0, size_type __n = npos) {
  822.     if (__pos > size())
  823.       this->_M_throw_out_of_range();
  824.     erase(begin() + __pos, begin() + __pos + (min) (__n, size() - __pos));
  825.     return *this;
  826.   }  
  827.  
  828.   iterator erase(iterator __position) {
  829.                                 // The move includes the terminating _CharT().
  830.     _Traits::move(__position, __position + 1, this->_M_finish - __position);
  831.     _Destroy(this->_M_finish);
  832.     --this->_M_finish;
  833.     return __position;
  834.   }
  835.  
  836.   iterator erase(iterator __first, iterator __last) {
  837.     if (__first != __last) {
  838.                                 // The move includes the terminating _CharT().
  839.       traits_type::move(__first, __last, (this->_M_finish - __last) + 1);
  840.       pointer __new_finish = this->_M_finish - (__last - __first);
  841.       _Destroy(__new_finish + 1, this->_M_finish + 1);
  842.       this->_M_finish = __new_finish;
  843.     }
  844.     return __first;
  845.   }
  846.  
  847. public:                         // Replace.  (Conceptually equivalent
  848.                                 // to erase followed by insert.)
  849.   _Self& replace(size_type __pos, size_type __n, 
  850.                         const _Self& __s) {
  851.     if (__pos > size())
  852.       this->_M_throw_out_of_range();
  853.     const size_type __len = (min) (__n, size() - __pos);
  854.     if (size() - __len >= max_size() - __s.size())
  855.       this->_M_throw_length_error();
  856.     return replace(begin() + __pos, begin() + __pos + __len, 
  857.                    __s._M_start, __s._M_finish);
  858.   }
  859.  
  860.   _Self& replace(size_type __pos1, size_type __n1,
  861.                         const _Self& __s,
  862.                         size_type __pos2, size_type __n2) {
  863.     if (__pos1 > size() || __pos2 > __s.size())
  864.       this->_M_throw_out_of_range();
  865.     const size_type __len1 = (min) (__n1, size() - __pos1);
  866.     const size_type __len2 = (min) (__n2, __s.size() - __pos2);
  867.     if (size() - __len1 >= max_size() - __len2)
  868.       this->_M_throw_length_error();
  869.     return replace(begin() + __pos1, begin() + __pos1 + __len1,
  870.                    __s._M_start + __pos2, __s._M_start + __pos2 + __len2);
  871.   }
  872.  
  873.   _Self& replace(size_type __pos, size_type __n1,
  874.                         const _CharT* __s, size_type __n2) {
  875.     _STLP_FIX_LITERAL_BUG(__s)
  876.     if (__pos > size())
  877.       this->_M_throw_out_of_range();
  878.     const size_type __len = (min) (__n1, size() - __pos);
  879.     if (__n2 > max_size() || size() - __len >= max_size() - __n2)
  880.       this->_M_throw_length_error();
  881.     return replace(begin() + __pos, begin() + __pos + __len,
  882.                    __s, __s + __n2);
  883.   }
  884.  
  885.   _Self& replace(size_type __pos, size_type __n1,
  886.                         const _CharT* __s) {
  887.     _STLP_FIX_LITERAL_BUG(__s)
  888.     if (__pos > size())
  889.       this->_M_throw_out_of_range();
  890.     const size_type __len = (min) (__n1, size() - __pos);
  891.     const size_type __n2 = _Traits::length(__s);
  892.     if (__n2 > max_size() || size() - __len >= max_size() - __n2)
  893.       this->_M_throw_length_error();
  894.     return replace(begin() + __pos, begin() + __pos + __len,
  895.                    __s, __s + _Traits::length(__s));
  896.   }
  897.  
  898.   _Self& replace(size_type __pos, size_type __n1,
  899.                         size_type __n2, _CharT __c) {
  900.     if (__pos > size())
  901.       this->_M_throw_out_of_range();
  902.     const size_type __len = (min) (__n1, size() - __pos);
  903.     if (__n2 > max_size() || size() - __len >= max_size() - __n2)
  904.       this->_M_throw_length_error();
  905.     return replace(begin() + __pos, begin() + __pos + __len, __n2, __c);
  906.   }
  907.  
  908.   _Self& replace(iterator __first, iterator __last, 
  909.                         const _Self& __s) 
  910.     { return replace(__first, __last, __s._M_start, __s._M_finish); }
  911.  
  912.   _Self& replace(iterator __first, iterator __last,
  913.                         const _CharT* __s, size_type __n) 
  914.     { _STLP_FIX_LITERAL_BUG(__s) return replace(__first, __last, __s, __s + __n); }
  915.  
  916.   _Self& replace(iterator __first, iterator __last,
  917.                         const _CharT* __s) {
  918.     _STLP_FIX_LITERAL_BUG(__s)
  919.     return replace(__first, __last, __s, __s + _Traits::length(__s));
  920.   }
  921.  
  922.   _Self& replace(iterator __first, iterator __last, 
  923.                         size_type __n, _CharT __c);
  924.  
  925.   // Check to see if _InputIterator is an integer type.  If so, then
  926.   // it can't be an iterator.
  927. #ifdef _STLP_MEMBER_TEMPLATES
  928.   template <class _InputIter> _Self& replace(iterator __first, iterator __last,
  929.                         _InputIter __f, _InputIter __l) {
  930.     typedef typename _Is_integer<_InputIter>::_Integral _Integral;
  931.     return _M_replace_dispatch(__first, __last, __f, __l,  _Integral());
  932.   }
  933. #else /* _STLP_MEMBER_TEMPLATES */
  934.   _Self& replace(iterator __first, iterator __last,
  935.          const _CharT* __f, const _CharT* __l);
  936. #endif /* _STLP_MEMBER_TEMPLATES */
  937.  
  938. private:                        // Helper functions for replace.
  939.  
  940. #ifdef _STLP_MEMBER_TEMPLATES
  941.  
  942.   template <class _Integer> _Self& _M_replace_dispatch(iterator __first, iterator __last,
  943.                                     _Integer __n, _Integer __x,
  944.                                     const __true_type&) {
  945.     return replace(__first, __last, (size_type) __n, (_CharT) __x);
  946.   }
  947.  
  948.   template <class _InputIter> _Self& _M_replace_dispatch(iterator __first, iterator __last,
  949.                                     _InputIter __f, _InputIter __l,
  950.                                     const __false_type&) {
  951.     return replace(__first, __last, __f, __l, _STLP_ITERATOR_CATEGORY(__f, _InputIter));
  952.   }
  953.  
  954.   template <class _InputIter> _Self& replace(iterator __first, iterator __last,
  955.                         _InputIter __f, _InputIter __l, const input_iterator_tag &)  {
  956.       for ( ; __first != __last && __f != __l; ++__first, ++__f)
  957.         _Traits::assign(*__first, *__f);
  958.  
  959.       if (__f == __l)
  960.         erase(__first, __last);
  961.       else
  962.         insert(__last, __f, __l);
  963.       return *this;
  964.     }
  965.  
  966.   template <class _ForwardIter> _Self& replace(iterator __first, iterator __last,
  967.                         _ForwardIter __f, _ForwardIter __l, 
  968.                         const forward_iterator_tag &)  {
  969.       difference_type __n = distance(__f, __l);
  970.       const difference_type __len = __last - __first;
  971.       if (__len >= __n) {
  972.         _M_copy(__f, __l, __first);
  973.         erase(__first + __n, __last);
  974.       }
  975.       else {
  976.         _ForwardIter __m = __f;
  977.         advance(__m, __len);
  978.         _M_copy(__f, __m, __first);
  979.         insert(__last, __m, __l);
  980.       }
  981.       return *this;
  982.     }
  983.  
  984. #endif /* _STLP_MEMBER_TEMPLATES */
  985.  
  986. public:                         // Other modifier member functions.
  987.  
  988.   size_type copy(_CharT* __s, size_type __n, size_type __pos = 0) const {
  989.     _STLP_FIX_LITERAL_BUG(__s)
  990.     if (__pos > size())
  991.       this->_M_throw_out_of_range();
  992.     const size_type __len = (min) (__n, size() - __pos);
  993.     _Traits::copy(__s, this->_M_start + __pos, __len);
  994.     return __len;
  995.   }
  996.  
  997.   void swap(_Self& __s) {
  998.     _STLP_STD::swap(this->_M_start, __s._M_start);
  999.     _STLP_STD::swap(this->_M_finish, __s._M_finish);
  1000.     _STLP_STD::swap(this->_M_end_of_storage, __s._M_end_of_storage);
  1001.   }
  1002.  
  1003. public:                         // Conversion to C string.
  1004.  
  1005.   const _CharT* c_str() const { return this->_M_start; }
  1006.   const _CharT* data()  const { return this->_M_start; }
  1007.  
  1008. public:                         // find.
  1009.  
  1010.   size_type find(const _Self& __s, size_type __pos = 0) const 
  1011.     { return find(__s._M_start, __pos, __s.size()); }
  1012.  
  1013.   size_type find(const _CharT* __s, size_type __pos = 0) const 
  1014.     { _STLP_FIX_LITERAL_BUG(__s) return find(__s, __pos, _Traits::length(__s)); }
  1015.  
  1016.   size_type find(const _CharT* __s, size_type __pos, size_type __n) const;
  1017.   size_type find(_CharT __c, size_type __pos = 0) const;
  1018.  
  1019. public:                         // rfind.
  1020.  
  1021.   size_type rfind(const _Self& __s, size_type __pos = npos) const 
  1022.     { return rfind(__s._M_start, __pos, __s.size()); }
  1023.  
  1024.   size_type rfind(const _CharT* __s, size_type __pos = npos) const 
  1025.     { _STLP_FIX_LITERAL_BUG(__s) return rfind(__s, __pos, _Traits::length(__s)); }
  1026.  
  1027.   size_type rfind(const _CharT* __s, size_type __pos, size_type __n) const;
  1028.   size_type rfind(_CharT __c, size_type __pos = npos) const;
  1029.  
  1030. public:                         // find_first_of
  1031.   
  1032.   size_type find_first_of(const _Self& __s, size_type __pos = 0) const 
  1033.     { return find_first_of(__s._M_start, __pos, __s.size()); }
  1034.  
  1035.   size_type find_first_of(const _CharT* __s, size_type __pos = 0) const 
  1036.     { _STLP_FIX_LITERAL_BUG(__s) return find_first_of(__s, __pos, _Traits::length(__s)); }
  1037.  
  1038.   size_type find_first_of(const _CharT* __s, size_type __pos, 
  1039.                           size_type __n) const;
  1040.  
  1041.   size_type find_first_of(_CharT __c, size_type __pos = 0) const 
  1042.     { return find(__c, __pos); }
  1043.  
  1044. public:                         // find_last_of
  1045.  
  1046.   size_type find_last_of(const _Self& __s,
  1047.                          size_type __pos = npos) const
  1048.     { return find_last_of(__s._M_start, __pos, __s.size()); }
  1049.  
  1050.   size_type find_last_of(const _CharT* __s, size_type __pos = npos) const 
  1051.     { _STLP_FIX_LITERAL_BUG(__s) return find_last_of(__s, __pos, _Traits::length(__s)); }
  1052.  
  1053.   size_type find_last_of(const _CharT* __s, size_type __pos, 
  1054.                          size_type __n) const;
  1055.  
  1056.   size_type find_last_of(_CharT __c, size_type __pos = npos) const {
  1057.     return rfind(__c, __pos);
  1058.   }
  1059.  
  1060. public:                         // find_first_not_of
  1061.  
  1062.   size_type find_first_not_of(const _Self& __s, 
  1063.                               size_type __pos = 0) const 
  1064.     { return find_first_not_of(__s._M_start, __pos, __s.size()); }
  1065.  
  1066.   size_type find_first_not_of(const _CharT* __s, size_type __pos = 0) const 
  1067.     { _STLP_FIX_LITERAL_BUG(__s) return find_first_not_of(__s, __pos, _Traits::length(__s)); }
  1068.  
  1069.   size_type find_first_not_of(const _CharT* __s, size_type __pos,
  1070.                               size_type __n) const;
  1071.  
  1072.   size_type find_first_not_of(_CharT __c, size_type __pos = 0) const;
  1073.  
  1074. public:                         // find_last_not_of
  1075.  
  1076.   size_type find_last_not_of(const _Self& __s, 
  1077.                              size_type __pos = npos) const
  1078.     { return find_last_not_of(__s._M_start, __pos, __s.size()); }
  1079.  
  1080.   size_type find_last_not_of(const _CharT* __s, size_type __pos = npos) const
  1081.     { _STLP_FIX_LITERAL_BUG(__s) return find_last_not_of(__s, __pos, _Traits::length(__s)); }
  1082.  
  1083.   size_type find_last_not_of(const _CharT* __s, size_type __pos,
  1084.                              size_type __n) const;
  1085.  
  1086.   size_type find_last_not_of(_CharT __c, size_type __pos = npos) const;
  1087.  
  1088. public:                         // Substring.
  1089.  
  1090.   _Self substr(size_type __pos = 0, size_type __n = npos) const {
  1091.     if (__pos > size())
  1092.       this->_M_throw_out_of_range();
  1093.     return _Self(this->_M_start + __pos, 
  1094.                         this->_M_start + __pos + (min) (__n, size() - __pos));
  1095.   }
  1096.  
  1097. public:                         // Compare
  1098.  
  1099.   int compare(const _Self& __s) const 
  1100.     { return _M_compare(this->_M_start, this->_M_finish, __s._M_start, __s._M_finish); }
  1101.  
  1102.   int compare(size_type __pos1, size_type __n1,
  1103.               const _Self& __s) const {
  1104.     if (__pos1 > size())
  1105.       this->_M_throw_out_of_range();
  1106.     return _M_compare(this->_M_start + __pos1, 
  1107.                       this->_M_start + __pos1 + (min) (__n1, size() - __pos1),
  1108.                       __s._M_start, __s._M_finish);
  1109.   }
  1110.     
  1111.   int compare(size_type __pos1, size_type __n1,
  1112.               const _Self& __s,
  1113.               size_type __pos2, size_type __n2) const {
  1114.     if (__pos1 > size() || __pos2 > __s.size())
  1115.       this->_M_throw_out_of_range();
  1116.     return _M_compare(this->_M_start + __pos1, 
  1117.                       this->_M_start + __pos1 + (min) (__n1, size() - __pos1),
  1118.                       __s._M_start + __pos2, 
  1119.                       __s._M_start + __pos2 + (min) (__n2, __s.size() - __pos2));
  1120.   }
  1121.  
  1122.   int compare(const _CharT* __s) const {
  1123.     _STLP_FIX_LITERAL_BUG(__s) 
  1124.       return _M_compare(this->_M_start, this->_M_finish, __s, __s + _Traits::length(__s));
  1125.   }
  1126.  
  1127.   int compare(size_type __pos1, size_type __n1, const _CharT* __s) const {
  1128.     _STLP_FIX_LITERAL_BUG(__s)
  1129.     if (__pos1 > size())
  1130.       this->_M_throw_out_of_range();
  1131.     return _M_compare(this->_M_start + __pos1, 
  1132.                       this->_M_start + __pos1 + (min) (__n1, size() - __pos1),
  1133.                       __s, __s + _Traits::length(__s));
  1134.   }
  1135.  
  1136.   int compare(size_type __pos1, size_type __n1, const _CharT* __s,
  1137.               size_type __n2) const {
  1138.     _STLP_FIX_LITERAL_BUG(__s)
  1139.     if (__pos1 > size())
  1140.       this->_M_throw_out_of_range();
  1141.     return _M_compare(this->_M_start + __pos1, 
  1142.                       this->_M_start + __pos1 + (min) (__n1, size() - __pos1),
  1143.                       __s, __s + __n2);
  1144.   }
  1145.  
  1146. public:                        // Helper functions for compare.
  1147.   
  1148.   static int _STLP_CALL _M_compare(const _CharT* __f1, const _CharT* __l1,
  1149.                         const _CharT* __f2, const _CharT* __l2) {
  1150.     const ptrdiff_t __n1 = __l1 - __f1;
  1151.     const ptrdiff_t __n2 = __l2 - __f2;
  1152.     const int cmp = _Traits::compare(__f1, __f2, (min) (__n1, __n2));
  1153.     return cmp != 0 ? cmp : (__n1 < __n2 ? -1 : (__n1 > __n2 ? 1 : 0));
  1154.   }
  1155. };
  1156.  
  1157.  
  1158. # if defined (_STLP_USE_TEMPLATE_EXPORT)
  1159. _STLP_EXPORT_TEMPLATE_CLASS basic_string<char, char_traits<char>, allocator<char> >;
  1160. #  if defined (_STLP_HAS_WCHAR_T)
  1161. _STLP_EXPORT_TEMPLATE_CLASS basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >;
  1162. #  endif
  1163. # endif /* _STLP_USE_TEMPLATE_EXPORT */
  1164.  
  1165. // ------------------------------------------------------------
  1166. // Non-member functions.
  1167.  
  1168. template <class _CharT, class _Traits, class _Alloc> inline basic_string<_CharT,_Traits,_Alloc> _STLP_CALL
  1169. operator+(const basic_string<_CharT,_Traits,_Alloc>& __s,
  1170.           const basic_string<_CharT,_Traits,_Alloc>& __y)
  1171. {
  1172.   typedef basic_string<_CharT,_Traits,_Alloc> _Str;
  1173.   typedef typename _Str::_Reserve_t _Reserve_t;
  1174. # ifdef __GNUC__
  1175.   // gcc counts this as a function
  1176.   _Str __result  = _Str(_Reserve_t(),__s.size() + __y.size());
  1177. # else
  1178.   _Str __result(_Reserve_t(), __s.size() + __y.size());
  1179. # endif
  1180.   __result.append(__s);
  1181.   __result.append(__y);
  1182.   return __result;
  1183. }
  1184.  
  1185. # if defined (__GNUC__) || defined (__MLCCPP__)
  1186. #  define _STLP_INIT_AMBIGUITY 1
  1187. # endif
  1188.  
  1189. template <class _CharT, class _Traits, class _Alloc> inline basic_string<_CharT,_Traits,_Alloc> _STLP_CALL
  1190. operator+(const _CharT* __s,
  1191.           const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1192.   _STLP_FIX_LITERAL_BUG(__s)
  1193.   typedef basic_string<_CharT,_Traits,_Alloc> _Str;
  1194.   typedef typename _Str::_Reserve_t _Reserve_t;
  1195.   const size_t __n = _Traits::length(__s);
  1196. # ifdef _STLP_INIT_AMBIGUITY
  1197.   _Str __result = _Str(_Reserve_t(), __n + __y.size());
  1198. # else
  1199.   _Str __result(_Reserve_t(), __n + __y.size());
  1200. # endif
  1201.   __result.append(__s, __s + __n);
  1202.   __result.append(__y);
  1203.   return __result;
  1204. }
  1205.  
  1206. template <class _CharT, class _Traits, class _Alloc> inline basic_string<_CharT,_Traits,_Alloc> _STLP_CALL
  1207. operator+(_CharT __c,
  1208.           const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1209.   typedef basic_string<_CharT,_Traits,_Alloc> _Str;
  1210.   typedef typename _Str::_Reserve_t _Reserve_t;
  1211. # ifdef _STLP_INIT_AMBIGUITY
  1212.   _Str __result = _Str(_Reserve_t(), 1 + __y.size());
  1213. # else
  1214.   _Str __result(_Reserve_t(), 1 + __y.size());
  1215. # endif
  1216.   __result.push_back(__c);
  1217.   __result.append(__y);
  1218.   return __result;
  1219. }
  1220.  
  1221. template <class _CharT, class _Traits, class _Alloc> inline basic_string<_CharT,_Traits,_Alloc> _STLP_CALL
  1222. operator+(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1223.           const _CharT* __s) {
  1224.   _STLP_FIX_LITERAL_BUG(__s)
  1225.   typedef basic_string<_CharT,_Traits,_Alloc> _Str;
  1226.   typedef typename _Str::_Reserve_t _Reserve_t;
  1227.   const size_t __n = _Traits::length(__s);
  1228. # ifdef _STLP_INIT_AMBIGUITY
  1229.   _Str __result = _Str(_Reserve_t(), __x.size() + __n, __x.get_allocator());
  1230. # else
  1231.   _Str __result(_Reserve_t(), __x.size() + __n, __x.get_allocator());
  1232. # endif
  1233.   __result.append(__x);
  1234.   __result.append(__s, __s + __n);
  1235.   return __result;
  1236. }
  1237.  
  1238. template <class _CharT, class _Traits, class _Alloc> inline basic_string<_CharT,_Traits,_Alloc> _STLP_CALL
  1239. operator+(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1240.           const _CharT __c) {
  1241.   typedef basic_string<_CharT,_Traits,_Alloc> _Str;
  1242.   typedef typename _Str::_Reserve_t _Reserve_t;
  1243. # ifdef _STLP_INIT_AMBIGUITY
  1244.   _Str __result = _Str(_Reserve_t(), __x.size() + 1, __x.get_allocator());
  1245. # else
  1246.   _Str __result(_Reserve_t(), __x.size() + 1, __x.get_allocator());
  1247. # endif
  1248.   __result.append(__x);
  1249.   __result.push_back(__c);
  1250.   return __result;
  1251. }
  1252.  
  1253. # undef _STLP_INIT_AMBIGUITY
  1254.  
  1255. // Operator== and operator!=
  1256.  
  1257. template <class _CharT, class _Traits, class _Alloc> inline bool _STLP_CALL
  1258. operator==(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1259.            const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1260.   return __x.size() == __y.size() && _Traits::compare(__x.data(), __y.data(), __x.size()) == 0;
  1261. }
  1262.  
  1263. template <class _CharT, class _Traits, class _Alloc> inline bool _STLP_CALL
  1264. operator==(const _CharT* __s,
  1265.            const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1266.   _STLP_FIX_LITERAL_BUG(__s)
  1267.   size_t __n = _Traits::length(__s);
  1268.   return __n == __y.size() && _Traits::compare(__s, __y.data(), __n) == 0;
  1269. }
  1270.  
  1271. template <class _CharT, class _Traits, class _Alloc> inline bool _STLP_CALL
  1272. operator==(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1273.            const _CharT* __s) {
  1274.   _STLP_FIX_LITERAL_BUG(__s)
  1275.   size_t __n = _Traits::length(__s);
  1276.   return __x.size() == __n && _Traits::compare(__x.data(), __s, __n) == 0;
  1277. }
  1278.  
  1279. // Operator< (and also >, <=, and >=).
  1280.  
  1281. template <class _CharT, class _Traits, class _Alloc> inline bool _STLP_CALL
  1282. operator<(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1283.           const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1284.   return basic_string<_CharT,_Traits,_Alloc> ::_M_compare(__x.begin(), __x.end(), 
  1285.          __y.begin(), __y.end()) < 0;
  1286. }
  1287.  
  1288. template <class _CharT, class _Traits, class _Alloc> inline bool _STLP_CALL
  1289. operator<(const _CharT* __s,
  1290.           const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1291.   _STLP_FIX_LITERAL_BUG(__s)
  1292.   size_t __n = _Traits::length(__s);
  1293.   return basic_string<_CharT,_Traits,_Alloc> ::_M_compare(__s, __s + __n, __y.begin(), __y.end()) < 0;
  1294. }
  1295.  
  1296. template <class _CharT, class _Traits, class _Alloc> inline bool _STLP_CALL
  1297. operator<(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1298.           const _CharT* __s) {
  1299.   _STLP_FIX_LITERAL_BUG(__s)
  1300.   size_t __n = _Traits::length(__s);
  1301.   return basic_string<_CharT,_Traits,_Alloc> ::_M_compare(__x.begin(), __x.end(), __s, __s + __n) < 0;
  1302. }
  1303.  
  1304. #ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACE
  1305.  
  1306. template <class _CharT, class _Traits, class _Alloc> inline bool _STLP_CALL
  1307. operator!=(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1308.            const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1309.   return !(__x == __y);
  1310. }
  1311.  
  1312. template <class _CharT, class _Traits, class _Alloc> inline bool _STLP_CALL
  1313. operator>(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1314.           const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1315.   return __y < __x;
  1316. }
  1317.  
  1318. template <class _CharT, class _Traits, class _Alloc> inline bool _STLP_CALL
  1319. operator<=(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1320.            const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1321.   return !(__y < __x);
  1322. }
  1323.  
  1324. template <class _CharT, class _Traits, class _Alloc> inline bool _STLP_CALL
  1325. operator>=(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1326.            const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1327.   return !(__x < __y);
  1328. }
  1329.  
  1330. #endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */
  1331.  
  1332. template <class _CharT, class _Traits, class _Alloc> inline bool _STLP_CALL 
  1333. operator!=(const _CharT* __s,
  1334.            const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1335.   _STLP_FIX_LITERAL_BUG(__s)
  1336.   return !(__s == __y);
  1337. }
  1338.  
  1339. template <class _CharT, class _Traits, class _Alloc> inline bool _STLP_CALL 
  1340. operator!=(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1341.            const _CharT* __s) {
  1342.   _STLP_FIX_LITERAL_BUG(__s)
  1343.   return !(__x == __s);
  1344. }
  1345.  
  1346. template <class _CharT, class _Traits, class _Alloc> inline bool _STLP_CALL
  1347. operator>(const _CharT* __s,
  1348.           const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1349.   _STLP_FIX_LITERAL_BUG(__s)
  1350.   return __y < __s;
  1351. }
  1352.  
  1353. template <class _CharT, class _Traits, class _Alloc> inline bool _STLP_CALL
  1354. operator>(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1355.           const _CharT* __s) {
  1356.   _STLP_FIX_LITERAL_BUG(__s)
  1357.   return __s < __x;
  1358. }
  1359.  
  1360. template <class _CharT, class _Traits, class _Alloc> inline bool _STLP_CALL
  1361. operator<=(const _CharT* __s,
  1362.            const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1363.   _STLP_FIX_LITERAL_BUG(__s)
  1364.   return !(__y < __s);
  1365. }
  1366.  
  1367. template <class _CharT, class _Traits, class _Alloc> inline bool _STLP_CALL
  1368. operator<=(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1369.            const _CharT* __s) {
  1370.   _STLP_FIX_LITERAL_BUG(__s)
  1371.   return !(__s < __x);
  1372. }
  1373.  
  1374. template <class _CharT, class _Traits, class _Alloc> inline bool _STLP_CALL
  1375. operator>=(const _CharT* __s,
  1376.            const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1377.   _STLP_FIX_LITERAL_BUG(__s)
  1378.   return !(__s < __y);
  1379. }
  1380.  
  1381. template <class _CharT, class _Traits, class _Alloc> inline bool _STLP_CALL
  1382. operator>=(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1383.            const _CharT* __s) {
  1384.   _STLP_FIX_LITERAL_BUG(__s)
  1385.   return !(__x < __s);
  1386. }
  1387.  
  1388.  
  1389. // Swap.
  1390.  
  1391. #ifdef _STLP_FUNCTION_TMPL_PARTIAL_ORDER
  1392.  
  1393. template <class _CharT, class _Traits, class _Alloc> inline void _STLP_CALL
  1394. swap(basic_string<_CharT,_Traits,_Alloc>& __x,
  1395.      basic_string<_CharT,_Traits,_Alloc>& __y) {
  1396.   __x.swap(__y);
  1397. }
  1398.  
  1399. #endif /* _STLP_FUNCTION_TMPL_PARTIAL_ORDER */
  1400.  
  1401. template <class _CharT, class _Traits, class _Alloc> void  _STLP_CALL _S_string_copy(const basic_string<_CharT,_Traits,_Alloc>& __s,
  1402.                     _CharT* __buf,
  1403.                     size_t __n);
  1404.  
  1405. # undef basic_string
  1406.  
  1407. #if defined(_STLP_WINCE)
  1408. // A couple of functions to transfer between ASCII/Unicode
  1409.  
  1410. wstring __ASCIIToWide(const char *ascii);
  1411. string __WideToASCII(const wchar_t *wide);
  1412. #endif
  1413.  
  1414. _STLP_END_NAMESPACE
  1415.  
  1416. # ifdef _STLP_DEBUG
  1417. #  include <stl/debug/_string.h> 
  1418. # endif
  1419.  
  1420. # if !defined (_STLP_LINK_TIME_INSTANTIATION)
  1421. #  include <stl/_string.c> 
  1422. # endif
  1423.  
  1424. # include <stl/_string_io.h>  
  1425. # include <stl/_string_hash.h>  
  1426.  
  1427. #endif /* _STLP_STRING */
  1428.  
  1429.  
  1430. // Local Variables:
  1431. // mode:C++
  1432. // End:
  1433.