5 * This material is provided "as is", with absolutely no warranty expressed
6 * or implied. Any use is at your own risk.
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.
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.
23 template <class _Left
, class _Right
, class _StorageDir
>
24 basic_string(_STLP_PRIV __bstr_sum
<_CharT
, _Traits
, _Alloc
, _Left
, _Right
, _StorageDir
> const& __s
)
25 : _STLP_STRING_SUM_BASE(_Reserve_t(), __s
.size(), __s
.get_allocator())
26 { _M_append_sum(__s
); }
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 : _STLP_STRING_SUM_BASE(_Reserve_t(), (__pos
<= __s
.size()) ? ((min
) (__n
, __s
.size() - __pos
)) : 0, __a
) {
33 size_type __size
= __s
.size();
35 this->_M_throw_out_of_range();
37 _M_append_sum_pos(__s
, __pos
, (min
) (__n
, __size
- __pos
));
41 _CharT
* _M_append_fast(_STLP_PRIV __char_wrapper
<_CharT
> __c
, _CharT
*__buf
) {
42 _STLP_STD::_Copy_Construct(__buf
, __c
.getValue());
45 _CharT
* _M_append_fast(_CharT
const* __s
, size_type __s_size
, _CharT
*__buf
)
46 { return uninitialized_copy(__s
, __s
+ __s_size
, __buf
); }
47 _CharT
* _M_append_fast(_STLP_PRIV __cstr_wrapper
<_CharT
> const& __s
, _CharT
*__buf
)
48 { return _M_append_fast(__s
.c_str(), __s
.size(), __buf
); }
49 _CharT
* _M_append_fast(_STLP_PRIV __bstr_wrapper
<_CharT
, _Traits
, _Alloc
> __s
, _CharT
*__buf
)
50 { return _M_append_fast(__s
.b_str(), __buf
); }
51 _CharT
* _M_append_fast(_Self
const& __s
, _CharT
*__buf
)
52 { return _M_append_fast(__s
.data(), __s
.size(), __buf
); }
53 _CharT
* _M_append_fast(_STLP_PRIV __sum_storage_elem
<_CharT
, _Traits
, _Alloc
> const&, _CharT
*__buf
)
55 template <class _Left
, class _Right
, class _StorageDir
>
56 _CharT
* _M_append_fast(_STLP_PRIV __bstr_sum
<_CharT
, _Traits
, _Alloc
, _Left
, _Right
, _StorageDir
> const& __s
, _CharT
*__buf
)
57 { return _M_append_fast(__s
.getRhs(), _M_append_fast(__s
.getLhs(), __buf
)); }
59 _CharT
* _M_append_fast_pos(_STLP_PRIV __char_wrapper
<_CharT
> __c
, _CharT
*__buf
, size_type
/*__pos*/, size_type __n
) {
62 _STLP_STD::_Copy_Construct(__buf
, __c
.getValue());
65 _CharT
* _M_append_fast_pos(_CharT
const* __s
, size_type __s_size
, _CharT
*__buf
,
66 size_type __pos
, size_type __n
)
67 { return uninitialized_copy(__s
+ __pos
, __s
+ __pos
+ (min
)(__n
, __s_size
- __pos
), __buf
); }
68 _CharT
* _M_append_fast_pos(_STLP_PRIV __cstr_wrapper
<_CharT
> const& __s
, _CharT
*__buf
,
69 size_type __pos
, size_type __n
)
70 { return _M_append_fast_pos(__s
.c_str(), __s
.size(), __buf
, __pos
, __n
); }
71 _CharT
* _M_append_fast_pos(_STLP_PRIV __bstr_wrapper
<_CharT
, _Traits
, _Alloc
> __s
, _CharT
*__buf
,
72 size_type __pos
, size_type __n
)
73 { return _M_append_fast_pos(__s
.b_str(), __buf
, __pos
, __n
); }
74 _CharT
* _M_append_fast_pos(_Self
const& __s
, _CharT
*__buf
,
75 size_type __pos
, size_type __n
)
76 { return _M_append_fast_pos(__s
.data(), __s
.size(), __buf
, __pos
, __n
); }
77 _CharT
* _M_append_fast_pos(_STLP_PRIV __sum_storage_elem
<_CharT
, _Traits
, _Alloc
> const&, _CharT
*__buf
,
81 template <class _Left
, class _Right
, class _StorageDir
>
82 _CharT
* _M_append_fast_pos(_STLP_PRIV __bstr_sum
<_CharT
, _Traits
, _Alloc
, _Left
, _Right
, _StorageDir
> const& __s
,
83 _CharT
*__buf
, size_type __pos
, size_type __n
) {
87 size_type __lhs_size
= __s
.getLhs().size();
88 if (__pos
< __lhs_size
) {
89 if (__n
< (__lhs_size
- __pos
)) {
90 return _M_append_fast_pos(__s
.getLhs(), __buf
, __pos
, __n
);
92 return _M_append_fast_pos(__s
.getRhs(), _M_append_fast_pos(__s
.getLhs(), __buf
, __pos
, __n
),
93 0, __n
- (__lhs_size
- __pos
));
96 return _M_append_fast_pos(__s
.getRhs(), __buf
, __pos
- __lhs_size
, __n
);
100 template <class _Left
, class _Right
, class _StorageDir
>
101 _Self
& _M_append_sum (_STLP_PRIV __bstr_sum
<_CharT
, _Traits
, _Alloc
, _Left
, _Right
, _StorageDir
> const& __s
) {
102 size_type __s_size
= __s
.size();
105 const size_type __old_size
= this->size();
106 if (__s_size
> this->max_size() || __old_size
> (this->max_size() - __s_size
))
107 this->_M_throw_length_error();
108 if (__old_size
+ __s_size
> this->capacity()) {
109 const size_type __len
= __old_size
+ (max
)(__old_size
, __s_size
) + 1;
110 pointer __new_start
= this->_M_start_of_storage
.allocate(__len
);
111 pointer __new_finish
= uninitialized_copy(this->_M_Start(), this->_M_Finish(), __new_start
);
112 __new_finish
= this->_M_append_fast(__s
, __new_finish
);
113 this->_M_construct_null(__new_finish
);
114 this->_M_deallocate_block();
115 this->_M_reset(__new_start
, __new_finish
, __new_start
+ __len
);
118 _M_append_sum_no_overflow(__s
, 0, __s_size
);
123 template <class _Left
, class _Right
, class _StorageDir
>
124 _Self
& _M_append_sum_pos(_STLP_PRIV __bstr_sum
<_CharT
, _Traits
, _Alloc
, _Left
, _Right
, _StorageDir
> const& __s
,
125 size_type __pos
, size_type __n
) {
126 size_type __s_size
= (min
)(__s
.size() - __pos
, __n
);
129 const size_type __old_size
= this->size();
130 if (__s_size
> this->max_size() || __old_size
> (this->max_size() - __s_size
))
131 this->_M_throw_length_error();
132 if (__old_size
+ __s_size
> this->capacity()) {
133 const size_type __len
= __old_size
+ (max
)(__old_size
, __s_size
) + 1;
134 pointer __new_start
= this->_M_start_of_storage
.allocate(__len
);
135 pointer __new_finish
= uninitialized_copy(this->_M_Start(), this->_M_Finish(), __new_start
);
136 __new_finish
= _M_append_fast_pos(__s
, __new_finish
, __pos
, __s_size
);
137 this->_M_construct_null(__new_finish
);
138 this->_M_deallocate_block();
139 this->_M_reset(__new_start
, __new_finish
, __new_start
+ __len
);
142 _M_append_sum_no_overflow(__s
, __pos
, __s_size
);
147 template <class _Left
, class _Right
, class _StorageDir
>
148 void _M_append_sum_no_overflow(_STLP_PRIV __bstr_sum
<_CharT
, _Traits
, _Alloc
, _Left
, _Right
, _StorageDir
> const& __s
,
149 size_type __pos
, size_type __n
) {
150 pointer __finish
= this->_M_Finish();
151 _M_append_fast_pos(__s
, __finish
+ 1, __pos
+ 1, __n
- 1);
152 this->_M_construct_null(__finish
+ __n
);
153 _Traits::assign(*this->_M_finish
, __s
[__pos
]);
154 this->_M_finish
+= __n
;