_string_sum_methods.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2003
  3. * Francois Dumont
  4. *
  5. * This material is provided "as is", with absolutely no warranty expressed
  6. * or implied. Any use is at your own risk.
  7. *
  8. * Permission to use or copy this software for any purpose is hereby granted
  9. * without fee, provided the above notices are retained on all copies.
  10. * Permission to modify the code and to distribute modified code is granted,
  11. * provided the above notices are retained, and a notice that the code was
  12. * modified is included with the above copyright notice.
  13. *
  14. */
  15. /*
  16. * All the necessary methods used for template expressions with basic_string
  17. * This file do not have to be macro guarded as it is only used in the _string.h
  18. * file and it is a part of the basic_string definition.
  19. */
  20. public:
  21. template <class _Left, class _Right, class _StorageDir>
  22. basic_string(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s)
  23. : _STLP_STRING_SUM_BASE(_Reserve_t(), __s.size(), __s.get_allocator())
  24. { _M_append_sum(__s); }
  25. template <class _Left, class _Right, class _StorageDir>
  26. basic_string(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s,
  27. size_type __pos, size_type __n = npos,
  28. const allocator_type& __a = allocator_type())
  29. : _STLP_STRING_SUM_BASE(_Reserve_t(), (__pos <= __s.size()) ? ((min) (__n, __s.size() - __pos)) : 0, __a) {
  30. size_type __size = __s.size();
  31. if (__pos > __size)
  32. this->_M_throw_out_of_range();
  33. else
  34. _M_append_sum_pos(__s, __pos, (min) (__n, __size - __pos));
  35. }
  36. private:
  37. _CharT* _M_append_fast(_STLP_PRIV __char_wrapper<_CharT> __c, _CharT *__buf) {
  38. _STLP_STD::_Copy_Construct(__buf, __c.getValue());
  39. return __buf + 1;
  40. }
  41. _CharT* _M_append_fast(_CharT const* __s, size_type __s_size, _CharT *__buf)
  42. { return uninitialized_copy(__s, __s + __s_size, __buf); }
  43. _CharT* _M_append_fast(_STLP_PRIV __cstr_wrapper<_CharT> const& __s, _CharT *__buf)
  44. { return _M_append_fast(__s.c_str(), __s.size(), __buf); }
  45. _CharT* _M_append_fast(_STLP_PRIV __bstr_wrapper<_CharT, _Traits, _Alloc> __s, _CharT *__buf)
  46. { return _M_append_fast(__s.b_str(), __buf); }
  47. _CharT* _M_append_fast(_Self const& __s, _CharT *__buf)
  48. { return _M_append_fast(__s.data(), __s.size(), __buf); }
  49. _CharT* _M_append_fast(_STLP_PRIV __sum_storage_elem<_CharT, _Traits, _Alloc> const&, _CharT *__buf)
  50. { return __buf; }
  51. template <class _Left, class _Right, class _StorageDir>
  52. _CharT* _M_append_fast(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s, _CharT *__buf)
  53. { return _M_append_fast(__s.getRhs(), _M_append_fast(__s.getLhs(), __buf)); }
  54. _CharT* _M_append_fast_pos(_STLP_PRIV __char_wrapper<_CharT> __c, _CharT *__buf, size_type /*__pos*/, size_type __n) {
  55. if (__n == 0)
  56. return __buf;
  57. _STLP_STD::_Copy_Construct(__buf, __c.getValue());
  58. return __buf + 1;
  59. }
  60. _CharT* _M_append_fast_pos(_CharT const* __s, size_type __s_size, _CharT *__buf,
  61. size_type __pos, size_type __n)
  62. { return uninitialized_copy(__s + __pos, __s + __pos + (min)(__n, __s_size - __pos), __buf); }
  63. _CharT* _M_append_fast_pos(_STLP_PRIV __cstr_wrapper<_CharT> const& __s, _CharT *__buf,
  64. size_type __pos, size_type __n)
  65. { return _M_append_fast_pos(__s.c_str(), __s.size(), __buf, __pos, __n); }
  66. _CharT* _M_append_fast_pos(_STLP_PRIV __bstr_wrapper<_CharT, _Traits, _Alloc> __s, _CharT *__buf,
  67. size_type __pos, size_type __n)
  68. { return _M_append_fast_pos(__s.b_str(), __buf, __pos, __n); }
  69. _CharT* _M_append_fast_pos(_Self const& __s, _CharT *__buf,
  70. size_type __pos, size_type __n)
  71. { return _M_append_fast_pos(__s.data(), __s.size(), __buf, __pos, __n); }
  72. _CharT* _M_append_fast_pos(_STLP_PRIV __sum_storage_elem<_CharT, _Traits, _Alloc> const&, _CharT *__buf,
  73. size_type, size_type)
  74. { return __buf; }
  75. template <class _Left, class _Right, class _StorageDir>
  76. _CharT* _M_append_fast_pos(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s,
  77. _CharT *__buf, size_type __pos, size_type __n) {
  78. if (__n == 0) {
  79. return __buf;
  80. }
  81. size_type __lhs_size = __s.getLhs().size();
  82. if (__pos < __lhs_size) {
  83. if (__n < (__lhs_size - __pos)) {
  84. return _M_append_fast_pos(__s.getLhs(), __buf, __pos, __n);
  85. } else {
  86. return _M_append_fast_pos(__s.getRhs(), _M_append_fast_pos(__s.getLhs(), __buf, __pos, __n),
  87. 0, __n - (__lhs_size - __pos));
  88. }
  89. } else {
  90. return _M_append_fast_pos(__s.getRhs(), __buf, __pos - __lhs_size, __n);
  91. }
  92. }
  93. template <class _Left, class _Right, class _StorageDir>
  94. _Self& _M_append_sum (_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s) {
  95. size_type __s_size = __s.size();
  96. if (__s_size == 0)
  97. return *this;
  98. const size_type __old_size = this->size();
  99. if (__s_size > this->max_size() || __old_size > (this->max_size() - __s_size))
  100. this->_M_throw_length_error();
  101. if (__old_size + __s_size > this->capacity()) {
  102. const size_type __len = __old_size + (max)(__old_size, __s_size) + 1;
  103. pointer __new_start = this->_M_start_of_storage.allocate(__len);
  104. pointer __new_finish = uninitialized_copy(this->_M_Start(), this->_M_Finish(), __new_start);
  105. __new_finish = this->_M_append_fast(__s, __new_finish);
  106. this->_M_construct_null(__new_finish);
  107. this->_M_deallocate_block();
  108. this->_M_reset(__new_start, __new_finish, __new_start + __len);
  109. }
  110. else {
  111. _M_append_sum_no_overflow(__s, 0, __s_size);
  112. }
  113. return *this;
  114. }
  115. template <class _Left, class _Right, class _StorageDir>
  116. _Self& _M_append_sum_pos(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s,
  117. size_type __pos, size_type __n) {
  118. size_type __s_size = (min)(__s.size() - __pos, __n);
  119. if (__s_size == 0)
  120. return *this;
  121. const size_type __old_size = this->size();
  122. if (__s_size > this->max_size() || __old_size > (this->max_size() - __s_size))
  123. this->_M_throw_length_error();
  124. if (__old_size + __s_size > this->capacity()) {
  125. const size_type __len = __old_size + (max)(__old_size, __s_size) + 1;
  126. pointer __new_start = this->_M_start_of_storage.allocate(__len);
  127. pointer __new_finish = uninitialized_copy(this->_M_Start(), this->_M_Finish(), __new_start);
  128. __new_finish = _M_append_fast_pos(__s, __new_finish, __pos, __s_size);
  129. this->_M_construct_null(__new_finish);
  130. this->_M_deallocate_block();
  131. this->_M_reset(__new_start, __new_finish, __new_start + __len);
  132. }
  133. else {
  134. _M_append_sum_no_overflow(__s, __pos, __s_size);
  135. }
  136. return *this;
  137. }
  138. template <class _Left, class _Right, class _StorageDir>
  139. void _M_append_sum_no_overflow(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s,
  140. size_type __pos, size_type __n) {
  141. pointer __finish = this->_M_Finish();
  142. _M_append_fast_pos(__s, __finish + 1, __pos + 1, __n - 1);
  143. this->_M_construct_null(__finish + __n);
  144. _Traits::assign(*this->_M_finish, __s[__pos]);
  145. this->_M_finish += __n;
  146. }