19720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block/*
29720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * Copyright (c) 2003
39720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * Francois Dumont
49720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block *
59720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * This material is provided "as is", with absolutely no warranty expressed
69720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * or implied. Any use is at your own risk.
79720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block *
89720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * Permission to use or copy this software for any purpose is hereby granted
99720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * without fee, provided the above notices are retained on all copies.
109720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * Permission to modify the code and to distribute modified code is granted,
119720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * provided the above notices are retained, and a notice that the code was
129720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * modified is included with the above copyright notice.
139720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block *
149720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block */
159720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
169720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block/*
179720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * All the necessary methods used for template expressions with basic_string
189720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * This file do not have to be macro guarded as it is only used in the _string.h
199720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * file and it is a part of the basic_string definition.
209720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block */
219720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
229720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockpublic:
239720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  template <class _Left, class _Right, class _StorageDir>
249720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  basic_string(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s)
259720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    : _STLP_STRING_SUM_BASE(_Reserve_t(), __s.size(), __s.get_allocator())
269720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  { _M_append_sum(__s); }
279720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
289720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  template <class _Left, class _Right, class _StorageDir>
299720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  basic_string(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s,
309720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block               size_type __pos, size_type __n = npos,
319720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block               const allocator_type& __a = allocator_type())
329720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    : _STLP_STRING_SUM_BASE(_Reserve_t(), (__pos <= __s.size()) ? ((min) (__n, __s.size() - __pos)) : 0, __a) {
339720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    size_type __size = __s.size();
349720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    if (__pos > __size)
359720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      this->_M_throw_out_of_range();
369720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    else
379720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      _M_append_sum_pos(__s, __pos, (min) (__n, __size - __pos));
389720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
399720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
409720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockprivate:
419720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  _CharT* _M_append_fast(_STLP_PRIV __char_wrapper<_CharT> __c, _CharT *__buf) {
429720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    _STLP_STD::_Copy_Construct(__buf, __c.getValue());
439720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    return __buf + 1;
449720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
459720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  _CharT* _M_append_fast(_CharT const* __s, size_type __s_size, _CharT *__buf)
469720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  { return uninitialized_copy(__s, __s + __s_size, __buf); }
479720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  _CharT* _M_append_fast(_STLP_PRIV __cstr_wrapper<_CharT> const& __s, _CharT *__buf)
489720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  { return _M_append_fast(__s.c_str(), __s.size(), __buf); }
499720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  _CharT* _M_append_fast(_STLP_PRIV __bstr_wrapper<_CharT, _Traits, _Alloc> __s, _CharT *__buf)
509720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  { return _M_append_fast(__s.b_str(), __buf); }
519720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  _CharT* _M_append_fast(_Self const& __s, _CharT *__buf)
529720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  { return _M_append_fast(__s.data(), __s.size(), __buf); }
539720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  _CharT* _M_append_fast(_STLP_PRIV __sum_storage_elem<_CharT, _Traits, _Alloc> const&, _CharT *__buf)
549720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  { return __buf; }
559720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  template <class _Left, class _Right, class _StorageDir>
569720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  _CharT* _M_append_fast(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s, _CharT *__buf)
579720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  { return _M_append_fast(__s.getRhs(), _M_append_fast(__s.getLhs(), __buf)); }
589720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
599720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  _CharT* _M_append_fast_pos(_STLP_PRIV __char_wrapper<_CharT> __c, _CharT *__buf, size_type /*__pos*/, size_type __n) {
609720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    if (__n == 0)
619720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      return __buf;
629720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    _STLP_STD::_Copy_Construct(__buf, __c.getValue());
639720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    return __buf + 1;
649720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
659720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  _CharT* _M_append_fast_pos(_CharT const* __s, size_type __s_size, _CharT *__buf,
669720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                             size_type __pos, size_type __n)
679720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  { return uninitialized_copy(__s + __pos, __s + __pos + (min)(__n, __s_size - __pos), __buf); }
689720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  _CharT* _M_append_fast_pos(_STLP_PRIV __cstr_wrapper<_CharT> const& __s, _CharT *__buf,
699720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                             size_type __pos, size_type __n)
709720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  { return _M_append_fast_pos(__s.c_str(), __s.size(), __buf, __pos, __n); }
719720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  _CharT* _M_append_fast_pos(_STLP_PRIV __bstr_wrapper<_CharT, _Traits, _Alloc> __s, _CharT *__buf,
729720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                             size_type __pos, size_type __n)
739720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  { return _M_append_fast_pos(__s.b_str(), __buf, __pos, __n); }
749720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  _CharT* _M_append_fast_pos(_Self const& __s, _CharT *__buf,
759720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                             size_type __pos, size_type __n)
769720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  { return _M_append_fast_pos(__s.data(), __s.size(), __buf, __pos, __n); }
779720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  _CharT* _M_append_fast_pos(_STLP_PRIV __sum_storage_elem<_CharT, _Traits, _Alloc> const&, _CharT *__buf,
789720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                             size_type, size_type)
799720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  { return __buf; }
809720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
819720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  template <class _Left, class _Right, class _StorageDir>
829720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  _CharT* _M_append_fast_pos(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s,
839720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                             _CharT *__buf, size_type __pos, size_type __n) {
849720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    if (__n == 0) {
859720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      return __buf;
869720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    }
879720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    size_type __lhs_size = __s.getLhs().size();
889720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    if (__pos < __lhs_size) {
899720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      if (__n < (__lhs_size - __pos)) {
909720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block        return _M_append_fast_pos(__s.getLhs(), __buf, __pos, __n);
919720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      } else {
929720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block        return _M_append_fast_pos(__s.getRhs(), _M_append_fast_pos(__s.getLhs(), __buf, __pos, __n),
939720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                                  0, __n - (__lhs_size - __pos));
949720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      }
959720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    } else {
969720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      return _M_append_fast_pos(__s.getRhs(), __buf, __pos - __lhs_size, __n);
979720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    }
989720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
999720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
1009720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  template <class _Left, class _Right, class _StorageDir>
1019720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  _Self& _M_append_sum (_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s) {
1029720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    size_type __s_size = __s.size();
1039720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    if (__s_size == 0)
1049720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      return *this;
1059720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    const size_type __old_size = this->size();
1069720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    if (__s_size > this->max_size() || __old_size > (this->max_size() - __s_size))
1079720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      this->_M_throw_length_error();
108e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    if (__old_size + __s_size > this->capacity()) {
109e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      const size_type __len = __old_size + (max)(__old_size, __s_size) + 1;
110e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      pointer __new_start = this->_M_start_of_storage.allocate(__len);
111e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      pointer __new_finish = uninitialized_copy(this->_M_Start(), this->_M_Finish(), __new_start);
112e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      __new_finish = this->_M_append_fast(__s, __new_finish);
113e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      this->_M_construct_null(__new_finish);
1149720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      this->_M_deallocate_block();
1159720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      this->_M_reset(__new_start, __new_finish, __new_start + __len);
1169720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    }
1179720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    else {
1189720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      _M_append_sum_no_overflow(__s, 0, __s_size);
1199720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    }
1209720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    return *this;
1219720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
1229720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
1239720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  template <class _Left, class _Right, class _StorageDir>
1249720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  _Self& _M_append_sum_pos(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s,
1259720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                           size_type __pos, size_type __n) {
1269720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    size_type __s_size = (min)(__s.size() - __pos, __n);
1279720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    if (__s_size == 0)
1289720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      return *this;
1299720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    const size_type __old_size = this->size();
1309720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    if (__s_size > this->max_size() || __old_size > (this->max_size() - __s_size))
1319720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      this->_M_throw_length_error();
132e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    if (__old_size + __s_size > this->capacity()) {
133e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      const size_type __len = __old_size + (max)(__old_size, __s_size) + 1;
134e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      pointer __new_start = this->_M_start_of_storage.allocate(__len);
135e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      pointer __new_finish = uninitialized_copy(this->_M_Start(), this->_M_Finish(), __new_start);
136e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      __new_finish = _M_append_fast_pos(__s, __new_finish, __pos, __s_size);
137e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      this->_M_construct_null(__new_finish);
1389720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      this->_M_deallocate_block();
1399720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      this->_M_reset(__new_start, __new_finish, __new_start + __len);
1409720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    }
1419720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    else {
1429720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      _M_append_sum_no_overflow(__s, __pos, __s_size);
1439720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    }
1449720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    return *this;
1459720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
1469720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
1479720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  template <class _Left, class _Right, class _StorageDir>
1489720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  void _M_append_sum_no_overflow(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s,
1499720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                                 size_type __pos, size_type __n) {
1509720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    pointer __finish = this->_M_Finish();
1519720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    _M_append_fast_pos(__s, __finish + 1, __pos + 1, __n - 1);
152e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    this->_M_construct_null(__finish + __n);
1539720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    _Traits::assign(*this->_M_finish, __s[__pos]);
1549720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    this->_M_finish += __n;
1559720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
156