_string_sum_methods.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. template <class _Left, class _Right, class _StorageDir>
  21. basic_string(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s)
  22. : _M_non_dbg_impl(_Reserve_t(), __s.size(), __s.get_allocator()),
  23. _M_iter_list(&_M_non_dbg_impl)
  24. { _M_append_sum(__s, _M_non_dbg_impl); }
  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. : _M_non_dbg_impl(_Reserve_t(), (__pos <= __s.size()) ? ((min) (__n, __s.size() - __pos)) : 0, __a),
  30. _M_iter_list(&_M_non_dbg_impl) {
  31. size_type __size = __s.size();
  32. if (__pos > __size)
  33. //This call will generate the necessary out of range exception:
  34. _M_non_dbg_impl.at(0);
  35. else
  36. _M_append_sum_pos(__s, __pos, (min) (__n, __size - __pos), _M_non_dbg_impl);
  37. }
  38. private:
  39. _Base& _M_append_fast(_STLP_PRIV __char_wrapper<_CharT> __c, _Base &__str)
  40. { return __str += __c.getValue(); }
  41. _Base& _M_append_fast(_CharT const* __s, size_type __s_size, _Base &__str)
  42. { return __str.append(__s, __s_size); }
  43. _Base& _M_append_fast(_STLP_PRIV __cstr_wrapper<_CharT> const& __s, _Base &__str)
  44. { return _M_append_fast(__s.c_str(), __s.size(), __str); }
  45. _Base& _M_append_fast(_STLP_PRIV __bstr_wrapper<_CharT, _Traits, _Alloc> __s, _Base &__str)
  46. { return _M_append_fast(__s.b_str(), __str); }
  47. _Base& _M_append_fast(_Self const& __s, _Base &__str)
  48. { return _M_append_fast(__s.data(), __s.size(), __str); }
  49. _Base& _M_append_fast(_STLP_PRIV __sum_storage_elem<_CharT, _Traits, _Alloc> const&, _Base &__str)
  50. { return __str; }
  51. template <class _Left, class _Right, class _StorageDir>
  52. _Base& _M_append_fast(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s, _Base &__str)
  53. { return _M_append_fast(__s.getRhs(), _M_append_fast(__s.getLhs(), __str)); }
  54. _Base& _M_append_fast_pos(_STLP_PRIV __char_wrapper<_CharT> __c, _Base &__str, size_type /*__pos*/, size_type __n) {
  55. if (__n == 0)
  56. return __str;
  57. return __str += __c.getValue();
  58. }
  59. _Base& _M_append_fast_pos(_CharT const* __s, size_type __s_size, _Base &__str,
  60. size_type __pos, size_type __n)
  61. { return __str.append(__s + __pos, __s + __pos + (min)(__n, __s_size - __pos)); }
  62. _Base& _M_append_fast_pos(_STLP_PRIV __cstr_wrapper<_CharT> const& __s, _Base &__str,
  63. size_type __pos, size_type __n)
  64. { return _M_append_fast_pos(__s.c_str(), __s.size(), __str, __pos, __n); }
  65. _Base& _M_append_fast_pos(_STLP_PRIV __bstr_wrapper<_CharT, _Traits, _Alloc> __s, _Base &__str,
  66. size_type __pos, size_type __n)
  67. { return _M_append_fast_pos(__s.b_str(), __str, __pos, __n); }
  68. _Base& _M_append_fast_pos(_Self const& __s, _Base &__str, size_type __pos, size_type __n)
  69. { return _M_append_fast_pos(__s.data(), __s.size(), __str, __pos, __n); }
  70. _Base& _M_append_fast_pos(_STLP_PRIV __sum_storage_elem<_CharT, _Traits, _Alloc> const&, _Base &__str,
  71. size_type /*__pos*/, size_type /*__n*/)
  72. { return __str; }
  73. template <class _Left, class _Right, class _StorageDir>
  74. _Base& _M_append_fast_pos(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s,
  75. _Base &__str, size_type __pos, size_type __n) {
  76. if (__n == 0) {
  77. return __str;
  78. }
  79. size_type __lhs_size = __s.getLhs().size();
  80. if (__pos < __lhs_size) {
  81. if (__n < (__lhs_size - __pos)) {
  82. return _M_append_fast_pos(__s.getLhs(), __str, __pos, __n);
  83. } else {
  84. return _M_append_fast_pos(__s.getRhs(), _M_append_fast_pos(__s.getLhs(), __str, __pos, __n),
  85. 0, __n - (__lhs_size - __pos));
  86. }
  87. } else {
  88. return _M_append_fast_pos(__s.getRhs(), __str, __pos - __lhs_size, __n);
  89. }
  90. }
  91. template <class _Left, class _Right, class _StorageDir>
  92. _Self& _M_append_sum (_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s,
  93. _Base &__impl) {
  94. _M_append_fast(__s, __impl);
  95. return *this;
  96. }
  97. template <class _Left, class _Right, class _StorageDir>
  98. _Self& _M_append_sum_pos (_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s,
  99. size_type __pos, size_type __n, _Base &__impl) {
  100. _M_non_dbg_impl.reserve(_M_non_dbg_impl.size() + (min) (__s.size() - __pos, __n));
  101. _M_append_fast_pos(__s, __impl, __pos, __n);
  102. return *this;
  103. }