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/*
17 * All the necessary methods used for template expressions with basic_string
18 * This file do not have to be macro guarded as it is only used in the _string.h
19 * file and it is a part of the basic_string definition.
20 */
21
22  template <class _Left, class _Right, class _StorageDir>
23  basic_string(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s)
24    : _M_non_dbg_impl(_Reserve_t(), __s.size(), __s.get_allocator()),
25      _M_iter_list(&_M_non_dbg_impl)
26  { _M_append_sum(__s, _M_non_dbg_impl); }
27
28  template <class _Left, class _Right, class _StorageDir>
29  basic_string(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s,
30               size_type __pos, size_type __n = npos,
31               const allocator_type& __a = allocator_type())
32    : _M_non_dbg_impl(_Reserve_t(), (__pos <= __s.size()) ? ((min) (__n, __s.size() - __pos)) : 0, __a),
33      _M_iter_list(&_M_non_dbg_impl) {
34    size_type __size = __s.size();
35    if (__pos > __size)
36      //This call will generate the necessary out of range exception:
37      _M_non_dbg_impl.at(0);
38    else
39      _M_append_sum_pos(__s, __pos, (min) (__n, __size - __pos), _M_non_dbg_impl);
40  }
41
42private:
43  _Base& _M_append_fast(_STLP_PRIV __char_wrapper<_CharT> __c, _Base &__str)
44  { return __str += __c.getValue(); }
45  _Base& _M_append_fast(_CharT const* __s, size_type __s_size, _Base &__str)
46  { return __str.append(__s, __s_size); }
47  _Base& _M_append_fast(_STLP_PRIV __cstr_wrapper<_CharT> const& __s, _Base &__str)
48  { return _M_append_fast(__s.c_str(), __s.size(), __str); }
49  _Base& _M_append_fast(_STLP_PRIV __bstr_wrapper<_CharT, _Traits, _Alloc> __s, _Base &__str)
50  { return _M_append_fast(__s.b_str(), __str); }
51  _Base& _M_append_fast(_Self const& __s, _Base &__str)
52  { return _M_append_fast(__s.data(), __s.size(), __str); }
53  _Base& _M_append_fast(_STLP_PRIV __sum_storage_elem<_CharT, _Traits, _Alloc> const&, _Base &__str)
54  { return __str; }
55  template <class _Left, class _Right, class _StorageDir>
56  _Base& _M_append_fast(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s, _Base &__str)
57  { return _M_append_fast(__s.getRhs(), _M_append_fast(__s.getLhs(), __str)); }
58
59  _Base& _M_append_fast_pos(_STLP_PRIV __char_wrapper<_CharT> __c, _Base &__str, size_type /*__pos*/, size_type __n) {
60    if (__n == 0)
61      return __str;
62    return __str += __c.getValue();
63  }
64  _Base& _M_append_fast_pos(_CharT const* __s, size_type __s_size, _Base &__str,
65                            size_type __pos, size_type __n)
66  { return __str.append(__s + __pos, __s + __pos + (min)(__n, __s_size - __pos)); }
67  _Base& _M_append_fast_pos(_STLP_PRIV __cstr_wrapper<_CharT> const& __s, _Base &__str,
68                            size_type __pos, size_type __n)
69  { return _M_append_fast_pos(__s.c_str(), __s.size(), __str, __pos, __n); }
70  _Base& _M_append_fast_pos(_STLP_PRIV __bstr_wrapper<_CharT, _Traits, _Alloc> __s, _Base &__str,
71                            size_type __pos, size_type __n)
72  { return _M_append_fast_pos(__s.b_str(), __str, __pos, __n); }
73  _Base& _M_append_fast_pos(_Self const& __s, _Base &__str, size_type __pos, size_type __n)
74  { return _M_append_fast_pos(__s.data(), __s.size(), __str, __pos, __n); }
75  _Base& _M_append_fast_pos(_STLP_PRIV __sum_storage_elem<_CharT, _Traits, _Alloc> const&, _Base &__str,
76                            size_type /*__pos*/, size_type /*__n*/)
77  { return __str; }
78
79  template <class _Left, class _Right, class _StorageDir>
80  _Base& _M_append_fast_pos(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s,
81                            _Base &__str, size_type __pos, size_type __n) {
82    if (__n == 0) {
83      return __str;
84    }
85    size_type __lhs_size = __s.getLhs().size();
86    if (__pos < __lhs_size) {
87      if (__n < (__lhs_size - __pos)) {
88        return _M_append_fast_pos(__s.getLhs(), __str, __pos, __n);
89      } else {
90        return _M_append_fast_pos(__s.getRhs(), _M_append_fast_pos(__s.getLhs(), __str, __pos, __n),
91                                  0, __n - (__lhs_size - __pos));
92      }
93    } else {
94      return _M_append_fast_pos(__s.getRhs(), __str, __pos - __lhs_size, __n);
95    }
96  }
97
98  template <class _Left, class _Right, class _StorageDir>
99  _Self& _M_append_sum (_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s,
100                        _Base &__impl) {
101    _M_append_fast(__s, __impl);
102    return *this;
103  }
104
105  template <class _Left, class _Right, class _StorageDir>
106  _Self& _M_append_sum_pos (_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s,
107                            size_type __pos, size_type __n, _Base &__impl) {
108    _M_non_dbg_impl.reserve(_M_non_dbg_impl.size() + (min) (__s.size() - __pos, __n));
109    _M_append_fast_pos(__s, __impl, __pos, __n);
110    return *this;
111  }
112