]>
Commit | Line | Data |
---|---|---|
725dc051 BK |
1 | // Components for manipulating sequences of characters -*- C++ -*- |
2 | ||
9521dd6b | 3 | // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, |
bc2631e0 | 4 | // 2006, 2007, 2008, 2009 |
c68cd521 | 5 | // Free Software Foundation, Inc. |
725dc051 BK |
6 | // |
7 | // This file is part of the GNU ISO C++ Library. This library is free | |
8 | // software; you can redistribute it and/or modify it under the | |
9 | // terms of the GNU General Public License as published by the | |
748086b7 | 10 | // Free Software Foundation; either version 3, or (at your option) |
725dc051 BK |
11 | // any later version. |
12 | ||
13 | // This library is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
748086b7 JJ |
18 | // Under Section 7 of GPL version 3, you are granted additional |
19 | // permissions described in the GCC Runtime Library Exception, version | |
20 | // 3.1, as published by the Free Software Foundation. | |
21 | ||
22 | // You should have received a copy of the GNU General Public License and | |
23 | // a copy of the GCC Runtime Library Exception along with this program; | |
24 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
25 | // <http://www.gnu.org/licenses/>. | |
725dc051 | 26 | |
0aa06b18 BK |
27 | /** @file basic_string.tcc |
28 | * This is an internal header file, included by other library headers. | |
29 | * You should not attempt to use it directly. | |
30 | */ | |
31 | ||
725dc051 BK |
32 | // |
33 | // ISO C++ 14882: 21 Strings library | |
34 | // | |
35 | ||
725dc051 BK |
36 | // Written by Jason Merrill based upon the specification by Takanori Adachi |
37 | // in ANSI X3J16/94-0013R2. Rewritten by Nathan Myers to ISO-14882. | |
38 | ||
3d7c150e BK |
39 | #ifndef _BASIC_STRING_TCC |
40 | #define _BASIC_STRING_TCC 1 | |
725dc051 | 41 | |
3b794528 BK |
42 | #pragma GCC system_header |
43 | ||
fba10f59 | 44 | #include <cxxabi-forced.h> |
d05f74f1 | 45 | |
3cbc7af0 BK |
46 | _GLIBCXX_BEGIN_NAMESPACE(std) |
47 | ||
725dc051 | 48 | template<typename _CharT, typename _Traits, typename _Alloc> |
ed6814f7 | 49 | const typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 | 50 | basic_string<_CharT, _Traits, _Alloc>:: |
ca566e4c | 51 | _Rep::_S_max_size = (((npos - sizeof(_Rep_base))/sizeof(_CharT)) - 1) / 4; |
725dc051 BK |
52 | |
53 | template<typename _CharT, typename _Traits, typename _Alloc> | |
ed6814f7 | 54 | const _CharT |
725dc051 | 55 | basic_string<_CharT, _Traits, _Alloc>:: |
00532602 | 56 | _Rep::_S_terminal = _CharT(); |
725dc051 BK |
57 | |
58 | template<typename _CharT, typename _Traits, typename _Alloc> | |
4f12dd3c | 59 | const typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 BK |
60 | basic_string<_CharT, _Traits, _Alloc>::npos; |
61 | ||
62 | // Linker sets _S_empty_rep_storage to all 0s (one reference, empty string) | |
63 | // at static init time (before static ctors are run). | |
64 | template<typename _CharT, typename _Traits, typename _Alloc> | |
4f12dd3c | 65 | typename basic_string<_CharT, _Traits, _Alloc>::size_type |
ca566e4c NM |
66 | basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_empty_rep_storage[ |
67 | (sizeof(_Rep_base) + sizeof(_CharT) + sizeof(size_type) - 1) / | |
68 | sizeof(size_type)]; | |
725dc051 BK |
69 | |
70 | // NB: This is the special case for Input Iterators, used in | |
71 | // istreambuf_iterators, etc. | |
72 | // Input Iterators have a cost structure very different from | |
73 | // pointers, calling for a different coding style. | |
74 | template<typename _CharT, typename _Traits, typename _Alloc> | |
08addde6 | 75 | template<typename _InIterator> |
725dc051 BK |
76 | _CharT* |
77 | basic_string<_CharT, _Traits, _Alloc>:: | |
08addde6 | 78 | _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a, |
725dc051 BK |
79 | input_iterator_tag) |
80 | { | |
1165dc50 | 81 | #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING |
725dc051 | 82 | if (__beg == __end && __a == _Alloc()) |
ca566e4c | 83 | return _S_empty_rep()._M_refdata(); |
1165dc50 | 84 | #endif |
725dc051 | 85 | // Avoid reallocation for common case. |
247791f5 | 86 | _CharT __buf[128]; |
690495b0 PC |
87 | size_type __len = 0; |
88 | while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT)) | |
ed6814f7 BI |
89 | { |
90 | __buf[__len++] = *__beg; | |
690495b0 | 91 | ++__beg; |
725dc051 | 92 | } |
690495b0 | 93 | _Rep* __r = _Rep::_S_create(__len, size_type(0), __a); |
ec61e852 | 94 | _M_copy(__r->_M_refdata(), __buf, __len); |
bc2631e0 | 95 | __try |
e2c09482 | 96 | { |
690495b0 | 97 | while (__beg != __end) |
e2c09482 | 98 | { |
690495b0 | 99 | if (__len == __r->_M_capacity) |
e2c09482 | 100 | { |
690495b0 PC |
101 | // Allocate more space. |
102 | _Rep* __another = _Rep::_S_create(__len + 1, __len, __a); | |
ec61e852 | 103 | _M_copy(__another->_M_refdata(), __r->_M_refdata(), __len); |
690495b0 PC |
104 | __r->_M_destroy(__a); |
105 | __r = __another; | |
e2c09482 | 106 | } |
ed6814f7 | 107 | __r->_M_refdata()[__len++] = *__beg; |
690495b0 | 108 | ++__beg; |
e2c09482 BK |
109 | } |
110 | } | |
bc2631e0 | 111 | __catch(...) |
e2c09482 | 112 | { |
ed6814f7 | 113 | __r->_M_destroy(__a); |
e2c09482 BK |
114 | __throw_exception_again; |
115 | } | |
cbf52bfa | 116 | __r->_M_set_length_and_sharable(__len); |
690495b0 | 117 | return __r->_M_refdata(); |
725dc051 | 118 | } |
ed6814f7 | 119 | |
725dc051 | 120 | template<typename _CharT, typename _Traits, typename _Alloc> |
91eab378 | 121 | template <typename _InIterator> |
725dc051 | 122 | _CharT* |
9a304d17 | 123 | basic_string<_CharT, _Traits, _Alloc>:: |
ed6814f7 | 124 | _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a, |
725dc051 BK |
125 | forward_iterator_tag) |
126 | { | |
1165dc50 | 127 | #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING |
085825b8 | 128 | if (__beg == __end && __a == _Alloc()) |
ca566e4c | 129 | return _S_empty_rep()._M_refdata(); |
1165dc50 | 130 | #endif |
ed6814f7 | 131 | // NB: Not required, but considered best practice. |
e762c6f4 | 132 | if (__gnu_cxx::__is_null_pointer(__beg) && __beg != __end) |
0e50667c | 133 | __throw_logic_error(__N("basic_string::_S_construct NULL not valid")); |
aa863dca | 134 | |
0e50667c PC |
135 | const size_type __dnew = static_cast<size_type>(std::distance(__beg, |
136 | __end)); | |
725dc051 | 137 | // Check for out_of_range and length_error exceptions. |
234e0d31 | 138 | _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a); |
bc2631e0 | 139 | __try |
e2c09482 | 140 | { _S_copy_chars(__r->_M_refdata(), __beg, __end); } |
bc2631e0 | 141 | __catch(...) |
ed6814f7 BI |
142 | { |
143 | __r->_M_destroy(__a); | |
e2c09482 BK |
144 | __throw_exception_again; |
145 | } | |
cbf52bfa | 146 | __r->_M_set_length_and_sharable(__dnew); |
725dc051 BK |
147 | return __r->_M_refdata(); |
148 | } | |
149 | ||
150 | template<typename _CharT, typename _Traits, typename _Alloc> | |
151 | _CharT* | |
9a304d17 | 152 | basic_string<_CharT, _Traits, _Alloc>:: |
725dc051 BK |
153 | _S_construct(size_type __n, _CharT __c, const _Alloc& __a) |
154 | { | |
1165dc50 | 155 | #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING |
725dc051 | 156 | if (__n == 0 && __a == _Alloc()) |
ca566e4c | 157 | return _S_empty_rep()._M_refdata(); |
1165dc50 | 158 | #endif |
725dc051 | 159 | // Check for out_of_range and length_error exceptions. |
234e0d31 | 160 | _Rep* __r = _Rep::_S_create(__n, size_type(0), __a); |
ed6814f7 | 161 | if (__n) |
ec61e852 | 162 | _M_assign(__r->_M_refdata(), __n, __c); |
954b12d2 | 163 | |
cbf52bfa | 164 | __r->_M_set_length_and_sharable(__n); |
725dc051 BK |
165 | return __r->_M_refdata(); |
166 | } | |
167 | ||
168 | template<typename _CharT, typename _Traits, typename _Alloc> | |
169 | basic_string<_CharT, _Traits, _Alloc>:: | |
170 | basic_string(const basic_string& __str) | |
f26e5597 CB |
171 | : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(__str.get_allocator()), |
172 | __str.get_allocator()), | |
173 | __str.get_allocator()) | |
725dc051 BK |
174 | { } |
175 | ||
176 | template<typename _CharT, typename _Traits, typename _Alloc> | |
177 | basic_string<_CharT, _Traits, _Alloc>:: | |
178 | basic_string(const _Alloc& __a) | |
179 | : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a) | |
180 | { } | |
ed6814f7 | 181 | |
725dc051 BK |
182 | template<typename _CharT, typename _Traits, typename _Alloc> |
183 | basic_string<_CharT, _Traits, _Alloc>:: | |
184 | basic_string(const basic_string& __str, size_type __pos, size_type __n) | |
a3af79ea | 185 | : _M_dataplus(_S_construct(__str._M_data() |
0e50667c PC |
186 | + __str._M_check(__pos, |
187 | "basic_string::basic_string"), | |
690495b0 PC |
188 | __str._M_data() + __str._M_limit(__pos, __n) |
189 | + __pos, _Alloc()), _Alloc()) | |
725dc051 BK |
190 | { } |
191 | ||
192 | template<typename _CharT, typename _Traits, typename _Alloc> | |
193 | basic_string<_CharT, _Traits, _Alloc>:: | |
194 | basic_string(const basic_string& __str, size_type __pos, | |
195 | size_type __n, const _Alloc& __a) | |
a3af79ea | 196 | : _M_dataplus(_S_construct(__str._M_data() |
0e50667c PC |
197 | + __str._M_check(__pos, |
198 | "basic_string::basic_string"), | |
690495b0 PC |
199 | __str._M_data() + __str._M_limit(__pos, __n) |
200 | + __pos, __a), __a) | |
725dc051 BK |
201 | { } |
202 | ||
285b36d6 | 203 | // TBD: DPG annotate |
725dc051 BK |
204 | template<typename _CharT, typename _Traits, typename _Alloc> |
205 | basic_string<_CharT, _Traits, _Alloc>:: | |
206 | basic_string(const _CharT* __s, size_type __n, const _Alloc& __a) | |
207 | : _M_dataplus(_S_construct(__s, __s + __n, __a), __a) | |
208 | { } | |
209 | ||
285b36d6 | 210 | // TBD: DPG annotate |
725dc051 BK |
211 | template<typename _CharT, typename _Traits, typename _Alloc> |
212 | basic_string<_CharT, _Traits, _Alloc>:: | |
213 | basic_string(const _CharT* __s, const _Alloc& __a) | |
085825b8 PC |
214 | : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) : |
215 | __s + npos, __a), __a) | |
725dc051 BK |
216 | { } |
217 | ||
218 | template<typename _CharT, typename _Traits, typename _Alloc> | |
219 | basic_string<_CharT, _Traits, _Alloc>:: | |
220 | basic_string(size_type __n, _CharT __c, const _Alloc& __a) | |
221 | : _M_dataplus(_S_construct(__n, __c, __a), __a) | |
222 | { } | |
285b36d6 | 223 | |
ed6814f7 | 224 | // TBD: DPG annotate |
725dc051 | 225 | template<typename _CharT, typename _Traits, typename _Alloc> |
08addde6 | 226 | template<typename _InputIterator> |
725dc051 | 227 | basic_string<_CharT, _Traits, _Alloc>:: |
08addde6 | 228 | basic_string(_InputIterator __beg, _InputIterator __end, const _Alloc& __a) |
725dc051 BK |
229 | : _M_dataplus(_S_construct(__beg, __end, __a), __a) |
230 | { } | |
231 | ||
988499f4 JM |
232 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
233 | template<typename _CharT, typename _Traits, typename _Alloc> | |
234 | basic_string<_CharT, _Traits, _Alloc>:: | |
235 | basic_string(initializer_list<_CharT> __l, const _Alloc& __a) | |
236 | : _M_dataplus(_S_construct(__l.begin(), __l.end(), __a), __a) | |
237 | { } | |
238 | #endif | |
239 | ||
5536e07d PC |
240 | template<typename _CharT, typename _Traits, typename _Alloc> |
241 | basic_string<_CharT, _Traits, _Alloc>& | |
242 | basic_string<_CharT, _Traits, _Alloc>:: | |
243 | assign(const basic_string& __str) | |
244 | { | |
245 | if (_M_rep() != __str._M_rep()) | |
246 | { | |
247 | // XXX MT | |
248 | const allocator_type __a = this->get_allocator(); | |
249 | _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator()); | |
250 | _M_rep()->_M_dispose(__a); | |
251 | _M_data(__tmp); | |
252 | } | |
253 | return *this; | |
254 | } | |
255 | ||
725dc051 BK |
256 | template<typename _CharT, typename _Traits, typename _Alloc> |
257 | basic_string<_CharT, _Traits, _Alloc>& | |
ca566e4c | 258 | basic_string<_CharT, _Traits, _Alloc>:: |
ec61e852 | 259 | assign(const _CharT* __s, size_type __n) |
725dc051 | 260 | { |
ec61e852 PC |
261 | __glibcxx_requires_string_len(__s, __n); |
262 | _M_check_length(this->size(), __n, "basic_string::assign"); | |
8eae76be | 263 | if (_M_disjunct(__s) || _M_rep()->_M_is_shared()) |
ec61e852 PC |
264 | return _M_replace_safe(size_type(0), this->size(), __s, __n); |
265 | else | |
725dc051 | 266 | { |
ec61e852 PC |
267 | // Work in-place. |
268 | const size_type __pos = __s - _M_data(); | |
269 | if (__pos >= __n) | |
d87bdb13 | 270 | _M_copy(_M_data(), __s, __n); |
ec61e852 | 271 | else if (__pos) |
d87bdb13 | 272 | _M_move(_M_data(), __s, __n); |
ec61e852 PC |
273 | _M_rep()->_M_set_length_and_sharable(__n); |
274 | return *this; | |
275 | } | |
276 | } | |
277 | ||
ab4375af PC |
278 | template<typename _CharT, typename _Traits, typename _Alloc> |
279 | basic_string<_CharT, _Traits, _Alloc>& | |
280 | basic_string<_CharT, _Traits, _Alloc>:: | |
281 | append(size_type __n, _CharT __c) | |
282 | { | |
283 | if (__n) | |
284 | { | |
285 | _M_check_length(size_type(0), __n, "basic_string::append"); | |
286 | const size_type __len = __n + this->size(); | |
287 | if (__len > this->capacity() || _M_rep()->_M_is_shared()) | |
288 | this->reserve(__len); | |
289 | _M_assign(_M_data() + this->size(), __n, __c); | |
290 | _M_rep()->_M_set_length_and_sharable(__len); | |
291 | } | |
292 | return *this; | |
293 | } | |
294 | ||
ec61e852 PC |
295 | template<typename _CharT, typename _Traits, typename _Alloc> |
296 | basic_string<_CharT, _Traits, _Alloc>& | |
297 | basic_string<_CharT, _Traits, _Alloc>:: | |
298 | append(const _CharT* __s, size_type __n) | |
299 | { | |
300 | __glibcxx_requires_string_len(__s, __n); | |
301 | if (__n) | |
302 | { | |
303 | _M_check_length(size_type(0), __n, "basic_string::append"); | |
304 | const size_type __len = __n + this->size(); | |
305 | if (__len > this->capacity() || _M_rep()->_M_is_shared()) | |
306 | { | |
8eae76be | 307 | if (_M_disjunct(__s)) |
ec61e852 PC |
308 | this->reserve(__len); |
309 | else | |
310 | { | |
311 | const size_type __off = __s - _M_data(); | |
312 | this->reserve(__len); | |
313 | __s = _M_data() + __off; | |
314 | } | |
315 | } | |
316 | _M_copy(_M_data() + this->size(), __s, __n); | |
317 | _M_rep()->_M_set_length_and_sharable(__len); | |
725dc051 BK |
318 | } |
319 | return *this; | |
320 | } | |
2d9d5235 | 321 | |
ab4375af PC |
322 | template<typename _CharT, typename _Traits, typename _Alloc> |
323 | basic_string<_CharT, _Traits, _Alloc>& | |
324 | basic_string<_CharT, _Traits, _Alloc>:: | |
325 | append(const basic_string& __str) | |
326 | { | |
327 | const size_type __size = __str.size(); | |
328 | if (__size) | |
329 | { | |
330 | const size_type __len = __size + this->size(); | |
331 | if (__len > this->capacity() || _M_rep()->_M_is_shared()) | |
332 | this->reserve(__len); | |
333 | _M_copy(_M_data() + this->size(), __str._M_data(), __size); | |
334 | _M_rep()->_M_set_length_and_sharable(__len); | |
335 | } | |
336 | return *this; | |
337 | } | |
338 | ||
ec61e852 PC |
339 | template<typename _CharT, typename _Traits, typename _Alloc> |
340 | basic_string<_CharT, _Traits, _Alloc>& | |
341 | basic_string<_CharT, _Traits, _Alloc>:: | |
342 | append(const basic_string& __str, size_type __pos, size_type __n) | |
343 | { | |
344 | __str._M_check(__pos, "basic_string::append"); | |
345 | __n = __str._M_limit(__pos, __n); | |
346 | if (__n) | |
347 | { | |
348 | const size_type __len = __n + this->size(); | |
349 | if (__len > this->capacity() || _M_rep()->_M_is_shared()) | |
350 | this->reserve(__len); | |
351 | _M_copy(_M_data() + this->size(), __str._M_data() + __pos, __n); | |
352 | _M_rep()->_M_set_length_and_sharable(__len); | |
353 | } | |
354 | return *this; | |
355 | } | |
2d9d5235 | 356 | |
2d9d5235 NM |
357 | template<typename _CharT, typename _Traits, typename _Alloc> |
358 | basic_string<_CharT, _Traits, _Alloc>& | |
359 | basic_string<_CharT, _Traits, _Alloc>:: | |
360 | insert(size_type __pos, const _CharT* __s, size_type __n) | |
361 | { | |
285b36d6 | 362 | __glibcxx_requires_string_len(__s, __n); |
04cc8aef | 363 | _M_check(__pos, "basic_string::insert"); |
ec61e852 | 364 | _M_check_length(size_type(0), __n, "basic_string::insert"); |
8eae76be | 365 | if (_M_disjunct(__s) || _M_rep()->_M_is_shared()) |
7bb9b33b | 366 | return _M_replace_safe(__pos, size_type(0), __s, __n); |
2d9d5235 NM |
367 | else |
368 | { | |
ec61e852 | 369 | // Work in-place. |
2d9d5235 NM |
370 | const size_type __off = __s - _M_data(); |
371 | _M_mutate(__pos, 0, __n); | |
372 | __s = _M_data() + __off; | |
373 | _CharT* __p = _M_data() + __pos; | |
374 | if (__s + __n <= __p) | |
ec61e852 | 375 | _M_copy(__p, __s, __n); |
2d9d5235 | 376 | else if (__s >= __p) |
ec61e852 | 377 | _M_copy(__p, __s + __n, __n); |
2d9d5235 NM |
378 | else |
379 | { | |
2cb612d1 | 380 | const size_type __nleft = __p - __s; |
ec61e852 PC |
381 | _M_copy(__p, __s, __nleft); |
382 | _M_copy(__p + __nleft, __p + __n, __n - __nleft); | |
2d9d5235 NM |
383 | } |
384 | return *this; | |
385 | } | |
386 | } | |
ed6814f7 | 387 | |
2d9d5235 NM |
388 | template<typename _CharT, typename _Traits, typename _Alloc> |
389 | basic_string<_CharT, _Traits, _Alloc>& | |
390 | basic_string<_CharT, _Traits, _Alloc>:: | |
391 | replace(size_type __pos, size_type __n1, const _CharT* __s, | |
392 | size_type __n2) | |
393 | { | |
285b36d6 | 394 | __glibcxx_requires_string_len(__s, __n2); |
04cc8aef | 395 | _M_check(__pos, "basic_string::replace"); |
e03a6fb7 | 396 | __n1 = _M_limit(__pos, __n1); |
ec61e852 | 397 | _M_check_length(__n1, __n2, "basic_string::replace"); |
2cb612d1 | 398 | bool __left; |
8eae76be | 399 | if (_M_disjunct(__s) || _M_rep()->_M_is_shared()) |
7bb9b33b | 400 | return _M_replace_safe(__pos, __n1, __s, __n2); |
2cb612d1 PC |
401 | else if ((__left = __s + __n2 <= _M_data() + __pos) |
402 | || _M_data() + __pos + __n1 <= __s) | |
403 | { | |
404 | // Work in-place: non-overlapping case. | |
ec61e852 PC |
405 | size_type __off = __s - _M_data(); |
406 | __left ? __off : (__off += __n2 - __n1); | |
2cb612d1 | 407 | _M_mutate(__pos, __n1, __n2); |
ec61e852 | 408 | _M_copy(_M_data() + __pos, _M_data() + __off, __n2); |
2cb612d1 PC |
409 | return *this; |
410 | } | |
2d9d5235 | 411 | else |
251804e6 | 412 | { |
2cb612d1 | 413 | // Todo: overlapping case. |
251804e6 PC |
414 | const basic_string __tmp(__s, __n2); |
415 | return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2); | |
416 | } | |
2d9d5235 | 417 | } |
ed6814f7 | 418 | |
725dc051 BK |
419 | template<typename _CharT, typename _Traits, typename _Alloc> |
420 | void | |
421 | basic_string<_CharT, _Traits, _Alloc>::_Rep:: | |
422 | _M_destroy(const _Alloc& __a) throw () | |
423 | { | |
6be8b524 PC |
424 | const size_type __size = sizeof(_Rep_base) + |
425 | (this->_M_capacity + 1) * sizeof(_CharT); | |
426 | _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size); | |
725dc051 BK |
427 | } |
428 | ||
429 | template<typename _CharT, typename _Traits, typename _Alloc> | |
430 | void | |
cc6e67d4 PC |
431 | basic_string<_CharT, _Traits, _Alloc>:: |
432 | _M_leak_hard() | |
725dc051 | 433 | { |
1165dc50 | 434 | #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING |
ca566e4c | 435 | if (_M_rep() == &_S_empty_rep()) |
1165dc50 PC |
436 | return; |
437 | #endif | |
ed6814f7 | 438 | if (_M_rep()->_M_is_shared()) |
725dc051 BK |
439 | _M_mutate(0, 0, 0); |
440 | _M_rep()->_M_set_leaked(); | |
441 | } | |
442 | ||
443 | template<typename _CharT, typename _Traits, typename _Alloc> | |
444 | void | |
445 | basic_string<_CharT, _Traits, _Alloc>:: | |
446 | _M_mutate(size_type __pos, size_type __len1, size_type __len2) | |
447 | { | |
7778fa6e | 448 | const size_type __old_size = this->size(); |
725dc051 | 449 | const size_type __new_size = __old_size + __len2 - __len1; |
725dc051 | 450 | const size_type __how_much = __old_size - __pos - __len1; |
ed6814f7 | 451 | |
cc6e67d4 | 452 | if (__new_size > this->capacity() || _M_rep()->_M_is_shared()) |
725dc051 BK |
453 | { |
454 | // Must reallocate. | |
91eab378 | 455 | const allocator_type __a = get_allocator(); |
cc6e67d4 | 456 | _Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a); |
954b12d2 PC |
457 | |
458 | if (__pos) | |
ec61e852 | 459 | _M_copy(__r->_M_refdata(), _M_data(), __pos); |
954b12d2 | 460 | if (__how_much) |
ec61e852 PC |
461 | _M_copy(__r->_M_refdata() + __pos + __len2, |
462 | _M_data() + __pos + __len1, __how_much); | |
954b12d2 | 463 | |
725dc051 BK |
464 | _M_rep()->_M_dispose(__a); |
465 | _M_data(__r->_M_refdata()); | |
ca566e4c | 466 | } |
725dc051 BK |
467 | else if (__how_much && __len1 != __len2) |
468 | { | |
ec61e852 PC |
469 | // Work in-place. |
470 | _M_move(_M_data() + __pos + __len2, | |
471 | _M_data() + __pos + __len1, __how_much); | |
725dc051 | 472 | } |
cbf52bfa | 473 | _M_rep()->_M_set_length_and_sharable(__new_size); |
725dc051 | 474 | } |
ed6814f7 | 475 | |
725dc051 BK |
476 | template<typename _CharT, typename _Traits, typename _Alloc> |
477 | void | |
cc6e67d4 PC |
478 | basic_string<_CharT, _Traits, _Alloc>:: |
479 | reserve(size_type __res) | |
725dc051 | 480 | { |
cbc67955 | 481 | if (__res != this->capacity() || _M_rep()->_M_is_shared()) |
725dc051 | 482 | { |
dcc61724 PC |
483 | // Make sure we don't shrink below the current size |
484 | if (__res < this->size()) | |
485 | __res = this->size(); | |
91eab378 | 486 | const allocator_type __a = get_allocator(); |
725dc051 BK |
487 | _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size()); |
488 | _M_rep()->_M_dispose(__a); | |
489 | _M_data(__tmp); | |
490 | } | |
491 | } | |
ed6814f7 | 492 | |
725dc051 | 493 | template<typename _CharT, typename _Traits, typename _Alloc> |
cc6e67d4 PC |
494 | void |
495 | basic_string<_CharT, _Traits, _Alloc>:: | |
496 | swap(basic_string& __s) | |
725dc051 | 497 | { |
ed6814f7 | 498 | if (_M_rep()->_M_is_leaked()) |
725dc051 | 499 | _M_rep()->_M_set_sharable(); |
ed6814f7 | 500 | if (__s._M_rep()->_M_is_leaked()) |
725dc051 BK |
501 | __s._M_rep()->_M_set_sharable(); |
502 | if (this->get_allocator() == __s.get_allocator()) | |
503 | { | |
504 | _CharT* __tmp = _M_data(); | |
505 | _M_data(__s._M_data()); | |
506 | __s._M_data(__tmp); | |
507 | } | |
508 | // The code below can usually be optimized away. | |
ed6814f7 | 509 | else |
725dc051 | 510 | { |
0e50667c PC |
511 | const basic_string __tmp1(_M_ibegin(), _M_iend(), |
512 | __s.get_allocator()); | |
ed6814f7 | 513 | const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(), |
0e50667c | 514 | this->get_allocator()); |
725dc051 BK |
515 | *this = __tmp2; |
516 | __s = __tmp1; | |
517 | } | |
518 | } | |
519 | ||
725dc051 | 520 | template<typename _CharT, typename _Traits, typename _Alloc> |
31bfa177 | 521 | typename basic_string<_CharT, _Traits, _Alloc>::_Rep* |
725dc051 | 522 | basic_string<_CharT, _Traits, _Alloc>::_Rep:: |
234e0d31 PC |
523 | _S_create(size_type __capacity, size_type __old_capacity, |
524 | const _Alloc& __alloc) | |
725dc051 | 525 | { |
f5677b15 | 526 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
725dc051 | 527 | // 83. String::npos vs. string::max_size() |
e2c09482 | 528 | if (__capacity > _S_max_size) |
0e50667c | 529 | __throw_length_error(__N("basic_string::_S_create")); |
725dc051 | 530 | |
2883d58b LR |
531 | // The standard places no restriction on allocating more memory |
532 | // than is strictly needed within this layer at the moment or as | |
234e0d31 PC |
533 | // requested by an explicit application call to reserve(). |
534 | ||
535 | // Many malloc implementations perform quite poorly when an | |
2883d58b LR |
536 | // application attempts to allocate memory in a stepwise fashion |
537 | // growing each allocation size by only 1 char. Additionally, | |
538 | // it makes little sense to allocate less linear memory than the | |
539 | // natural blocking size of the malloc implementation. | |
540 | // Unfortunately, we would need a somewhat low-level calculation | |
541 | // with tuned parameters to get this perfect for any particular | |
542 | // malloc implementation. Fortunately, generalizations about | |
543 | // common features seen among implementations seems to suffice. | |
2883d58b LR |
544 | |
545 | // __pagesize need not match the actual VM page size for good | |
546 | // results in practice, thus we pick a common value on the low | |
547 | // side. __malloc_header_size is an estimate of the amount of | |
548 | // overhead per memory allocation (in practice seen N * sizeof | |
549 | // (void*) where N is 0, 2 or 4). According to folklore, | |
550 | // picking this value on the high side is better than | |
551 | // low-balling it (especially when this algorithm is used with | |
552 | // malloc implementations that allocate memory blocks rounded up | |
553 | // to a size which is a power of 2). | |
cbb0dcef PC |
554 | const size_type __pagesize = 4096; |
555 | const size_type __malloc_header_size = 4 * sizeof(void*); | |
234e0d31 PC |
556 | |
557 | // The below implements an exponential growth policy, necessary to | |
558 | // meet amortized linear time requirements of the library: see | |
559 | // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html. | |
560 | // It's active for allocations requiring an amount of memory above | |
561 | // system pagesize. This is consistent with the requirements of the | |
562 | // standard: http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html | |
cbb0dcef | 563 | if (__capacity > __old_capacity && __capacity < 2 * __old_capacity) |
234e0d31 PC |
564 | __capacity = 2 * __old_capacity; |
565 | ||
566 | // NB: Need an array of char_type[__capacity], plus a terminating | |
6be8b524 | 567 | // null char_type() element, plus enough for the _Rep data structure. |
234e0d31 | 568 | // Whew. Seemingly so needy, yet so elemental. |
6be8b524 | 569 | size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep); |
234e0d31 | 570 | |
24f33069 | 571 | const size_type __adj_size = __size + __malloc_header_size; |
d1a7222c | 572 | if (__adj_size > __pagesize && __capacity > __old_capacity) |
2883d58b | 573 | { |
24f33069 | 574 | const size_type __extra = __pagesize - __adj_size % __pagesize; |
2883d58b | 575 | __capacity += __extra / sizeof(_CharT); |
d1615643 PC |
576 | // Never allocate a string bigger than _S_max_size. |
577 | if (__capacity > _S_max_size) | |
578 | __capacity = _S_max_size; | |
6be8b524 | 579 | __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep); |
2883d58b | 580 | } |
2883d58b | 581 | |
725dc051 BK |
582 | // NB: Might throw, but no worries about a leak, mate: _Rep() |
583 | // does not throw. | |
6be8b524 | 584 | void* __place = _Raw_bytes_alloc(__alloc).allocate(__size); |
725dc051 BK |
585 | _Rep *__p = new (__place) _Rep; |
586 | __p->_M_capacity = __capacity; | |
039f9e35 JJ |
587 | // ABI compatibility - 3.4.x set in _S_create both |
588 | // _M_refcount and _M_length. All callers of _S_create | |
589 | // in basic_string.tcc then set just _M_length. | |
590 | // In 4.0.x and later both _M_refcount and _M_length | |
591 | // are initialized in the callers, unfortunately we can | |
592 | // have 3.4.x compiled code with _S_create callers inlined | |
593 | // calling 4.0.x+ _S_create. | |
594 | __p->_M_set_sharable(); | |
725dc051 BK |
595 | return __p; |
596 | } | |
597 | ||
598 | template<typename _CharT, typename _Traits, typename _Alloc> | |
599 | _CharT* | |
600 | basic_string<_CharT, _Traits, _Alloc>::_Rep:: | |
601 | _M_clone(const _Alloc& __alloc, size_type __res) | |
602 | { | |
79f57f23 | 603 | // Requested capacity of the clone. |
ca566e4c | 604 | const size_type __requested_cap = this->_M_length + __res; |
234e0d31 PC |
605 | _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity, |
606 | __alloc); | |
ca566e4c | 607 | if (this->_M_length) |
ec61e852 | 608 | _M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length); |
954b12d2 | 609 | |
cbf52bfa | 610 | __r->_M_set_length_and_sharable(this->_M_length); |
725dc051 BK |
611 | return __r->_M_refdata(); |
612 | } | |
ed6814f7 | 613 | |
725dc051 BK |
614 | template<typename _CharT, typename _Traits, typename _Alloc> |
615 | void | |
cc6e67d4 PC |
616 | basic_string<_CharT, _Traits, _Alloc>:: |
617 | resize(size_type __n, _CharT __c) | |
725dc051 | 618 | { |
7778fa6e | 619 | const size_type __size = this->size(); |
ec61e852 | 620 | _M_check_length(__size, __n, "basic_string::resize"); |
725dc051 BK |
621 | if (__size < __n) |
622 | this->append(__n - __size, __c); | |
623 | else if (__n < __size) | |
624 | this->erase(__n); | |
625 | // else nothing (in particular, avoid calling _M_mutate() unnecessarily.) | |
626 | } | |
418bb880 | 627 | |
725dc051 | 628 | template<typename _CharT, typename _Traits, typename _Alloc> |
08addde6 | 629 | template<typename _InputIterator> |
725dc051 BK |
630 | basic_string<_CharT, _Traits, _Alloc>& |
631 | basic_string<_CharT, _Traits, _Alloc>:: | |
ed6814f7 | 632 | _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1, |
fefe561e | 633 | _InputIterator __k2, __false_type) |
725dc051 | 634 | { |
7778fa6e | 635 | const basic_string __s(__k1, __k2); |
251804e6 | 636 | const size_type __n1 = __i2 - __i1; |
ec61e852 | 637 | _M_check_length(__n1, __s.size(), "basic_string::_M_replace_dispatch"); |
251804e6 | 638 | return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(), |
7bb9b33b | 639 | __s.size()); |
725dc051 | 640 | } |
6bfad5e1 PC |
641 | |
642 | template<typename _CharT, typename _Traits, typename _Alloc> | |
643 | basic_string<_CharT, _Traits, _Alloc>& | |
644 | basic_string<_CharT, _Traits, _Alloc>:: | |
645 | _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2, | |
646 | _CharT __c) | |
647 | { | |
648 | _M_check_length(__n1, __n2, "basic_string::_M_replace_aux"); | |
649 | _M_mutate(__pos1, __n1, __n2); | |
650 | if (__n2) | |
651 | _M_assign(_M_data() + __pos1, __n2, __c); | |
652 | return *this; | |
653 | } | |
654 | ||
655 | template<typename _CharT, typename _Traits, typename _Alloc> | |
656 | basic_string<_CharT, _Traits, _Alloc>& | |
657 | basic_string<_CharT, _Traits, _Alloc>:: | |
658 | _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s, | |
659 | size_type __n2) | |
660 | { | |
661 | _M_mutate(__pos1, __n1, __n2); | |
662 | if (__n2) | |
663 | _M_copy(_M_data() + __pos1, __s, __n2); | |
664 | return *this; | |
665 | } | |
666 | ||
725dc051 | 667 | template<typename _CharT, typename _Traits, typename _Alloc> |
9a304d17 | 668 | basic_string<_CharT, _Traits, _Alloc> |
725dc051 | 669 | operator+(const _CharT* __lhs, |
9a304d17 | 670 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
725dc051 | 671 | { |
285b36d6 | 672 | __glibcxx_requires_string(__lhs); |
9a304d17 | 673 | typedef basic_string<_CharT, _Traits, _Alloc> __string_type; |
725dc051 | 674 | typedef typename __string_type::size_type __size_type; |
7778fa6e | 675 | const __size_type __len = _Traits::length(__lhs); |
725dc051 BK |
676 | __string_type __str; |
677 | __str.reserve(__len + __rhs.size()); | |
bb9909b0 | 678 | __str.append(__lhs, __len); |
725dc051 BK |
679 | __str.append(__rhs); |
680 | return __str; | |
681 | } | |
682 | ||
683 | template<typename _CharT, typename _Traits, typename _Alloc> | |
9a304d17 PC |
684 | basic_string<_CharT, _Traits, _Alloc> |
685 | operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs) | |
725dc051 | 686 | { |
9a304d17 | 687 | typedef basic_string<_CharT, _Traits, _Alloc> __string_type; |
725dc051 BK |
688 | typedef typename __string_type::size_type __size_type; |
689 | __string_type __str; | |
7778fa6e | 690 | const __size_type __len = __rhs.size(); |
725dc051 | 691 | __str.reserve(__len + 1); |
15f13f01 | 692 | __str.append(__size_type(1), __lhs); |
725dc051 BK |
693 | __str.append(__rhs); |
694 | return __str; | |
695 | } | |
696 | ||
725dc051 | 697 | template<typename _CharT, typename _Traits, typename _Alloc> |
31bfa177 | 698 | typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 BK |
699 | basic_string<_CharT, _Traits, _Alloc>:: |
700 | copy(_CharT* __s, size_type __n, size_type __pos) const | |
701 | { | |
04cc8aef | 702 | _M_check(__pos, "basic_string::copy"); |
e03a6fb7 | 703 | __n = _M_limit(__pos, __n); |
285b36d6 | 704 | __glibcxx_requires_string_len(__s, __n); |
251804e6 | 705 | if (__n) |
ec61e852 | 706 | _M_copy(__s, _M_data() + __pos, __n); |
725dc051 BK |
707 | // 21.3.5.7 par 3: do not append null. (good.) |
708 | return __n; | |
709 | } | |
710 | ||
725dc051 | 711 | template<typename _CharT, typename _Traits, typename _Alloc> |
31bfa177 | 712 | typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 BK |
713 | basic_string<_CharT, _Traits, _Alloc>:: |
714 | find(const _CharT* __s, size_type __pos, size_type __n) const | |
715 | { | |
285b36d6 | 716 | __glibcxx_requires_string_len(__s, __n); |
7778fa6e | 717 | const size_type __size = this->size(); |
1a4ba99f PC |
718 | const _CharT* __data = _M_data(); |
719 | ||
720 | if (__n == 0) | |
721 | return __pos <= __size ? __pos : npos; | |
722 | ||
5527be59 PC |
723 | if (__n <= __size) |
724 | { | |
1c917b03 | 725 | for (; __pos <= __size - __n; ++__pos) |
5527be59 PC |
726 | if (traits_type::eq(__data[__pos], __s[0]) |
727 | && traits_type::compare(__data + __pos + 1, | |
728 | __s + 1, __n - 1) == 0) | |
729 | return __pos; | |
730 | } | |
1a4ba99f | 731 | return npos; |
725dc051 BK |
732 | } |
733 | ||
734 | template<typename _CharT, typename _Traits, typename _Alloc> | |
31bfa177 | 735 | typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 BK |
736 | basic_string<_CharT, _Traits, _Alloc>:: |
737 | find(_CharT __c, size_type __pos) const | |
738 | { | |
725dc051 | 739 | size_type __ret = npos; |
4a787fa8 | 740 | const size_type __size = this->size(); |
725dc051 BK |
741 | if (__pos < __size) |
742 | { | |
743 | const _CharT* __data = _M_data(); | |
7778fa6e | 744 | const size_type __n = __size - __pos; |
97644827 BK |
745 | const _CharT* __p = traits_type::find(__data + __pos, __n, __c); |
746 | if (__p) | |
725dc051 BK |
747 | __ret = __p - __data; |
748 | } | |
749 | return __ret; | |
750 | } | |
751 | ||
725dc051 | 752 | template<typename _CharT, typename _Traits, typename _Alloc> |
31bfa177 | 753 | typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 BK |
754 | basic_string<_CharT, _Traits, _Alloc>:: |
755 | rfind(const _CharT* __s, size_type __pos, size_type __n) const | |
756 | { | |
285b36d6 | 757 | __glibcxx_requires_string_len(__s, __n); |
7778fa6e | 758 | const size_type __size = this->size(); |
725dc051 BK |
759 | if (__n <= __size) |
760 | { | |
dd6eaaed | 761 | __pos = std::min(size_type(__size - __n), __pos); |
725dc051 | 762 | const _CharT* __data = _M_data(); |
ed6814f7 | 763 | do |
725dc051 BK |
764 | { |
765 | if (traits_type::compare(__data + __pos, __s, __n) == 0) | |
766 | return __pos; | |
ed6814f7 | 767 | } |
725dc051 BK |
768 | while (__pos-- > 0); |
769 | } | |
770 | return npos; | |
771 | } | |
ed6814f7 | 772 | |
725dc051 | 773 | template<typename _CharT, typename _Traits, typename _Alloc> |
31bfa177 | 774 | typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 BK |
775 | basic_string<_CharT, _Traits, _Alloc>:: |
776 | rfind(_CharT __c, size_type __pos) const | |
777 | { | |
04cc8aef | 778 | size_type __size = this->size(); |
725dc051 BK |
779 | if (__size) |
780 | { | |
04cc8aef PC |
781 | if (--__size > __pos) |
782 | __size = __pos; | |
783 | for (++__size; __size-- > 0; ) | |
784 | if (traits_type::eq(_M_data()[__size], __c)) | |
785 | return __size; | |
725dc051 BK |
786 | } |
787 | return npos; | |
788 | } | |
ed6814f7 | 789 | |
725dc051 | 790 | template<typename _CharT, typename _Traits, typename _Alloc> |
31bfa177 | 791 | typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 BK |
792 | basic_string<_CharT, _Traits, _Alloc>:: |
793 | find_first_of(const _CharT* __s, size_type __pos, size_type __n) const | |
794 | { | |
285b36d6 | 795 | __glibcxx_requires_string_len(__s, __n); |
725dc051 BK |
796 | for (; __n && __pos < this->size(); ++__pos) |
797 | { | |
97644827 BK |
798 | const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]); |
799 | if (__p) | |
725dc051 BK |
800 | return __pos; |
801 | } | |
802 | return npos; | |
803 | } | |
ed6814f7 | 804 | |
725dc051 | 805 | template<typename _CharT, typename _Traits, typename _Alloc> |
31bfa177 | 806 | typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 BK |
807 | basic_string<_CharT, _Traits, _Alloc>:: |
808 | find_last_of(const _CharT* __s, size_type __pos, size_type __n) const | |
809 | { | |
285b36d6 | 810 | __glibcxx_requires_string_len(__s, __n); |
725dc051 BK |
811 | size_type __size = this->size(); |
812 | if (__size && __n) | |
ed6814f7 BI |
813 | { |
814 | if (--__size > __pos) | |
725dc051 BK |
815 | __size = __pos; |
816 | do | |
817 | { | |
97644827 | 818 | if (traits_type::find(__s, __n, _M_data()[__size])) |
725dc051 | 819 | return __size; |
ed6814f7 | 820 | } |
725dc051 BK |
821 | while (__size-- != 0); |
822 | } | |
823 | return npos; | |
824 | } | |
ed6814f7 | 825 | |
725dc051 | 826 | template<typename _CharT, typename _Traits, typename _Alloc> |
31bfa177 | 827 | typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 BK |
828 | basic_string<_CharT, _Traits, _Alloc>:: |
829 | find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const | |
830 | { | |
285b36d6 | 831 | __glibcxx_requires_string_len(__s, __n); |
fefe561e PC |
832 | for (; __pos < this->size(); ++__pos) |
833 | if (!traits_type::find(__s, __n, _M_data()[__pos])) | |
834 | return __pos; | |
725dc051 BK |
835 | return npos; |
836 | } | |
837 | ||
838 | template<typename _CharT, typename _Traits, typename _Alloc> | |
31bfa177 | 839 | typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 BK |
840 | basic_string<_CharT, _Traits, _Alloc>:: |
841 | find_first_not_of(_CharT __c, size_type __pos) const | |
842 | { | |
fefe561e PC |
843 | for (; __pos < this->size(); ++__pos) |
844 | if (!traits_type::eq(_M_data()[__pos], __c)) | |
845 | return __pos; | |
725dc051 BK |
846 | return npos; |
847 | } | |
848 | ||
849 | template<typename _CharT, typename _Traits, typename _Alloc> | |
31bfa177 | 850 | typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 BK |
851 | basic_string<_CharT, _Traits, _Alloc>:: |
852 | find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const | |
853 | { | |
285b36d6 | 854 | __glibcxx_requires_string_len(__s, __n); |
04cc8aef | 855 | size_type __size = this->size(); |
ba317c52 | 856 | if (__size) |
04cc8aef PC |
857 | { |
858 | if (--__size > __pos) | |
859 | __size = __pos; | |
ed6814f7 | 860 | do |
725dc051 | 861 | { |
04cc8aef PC |
862 | if (!traits_type::find(__s, __n, _M_data()[__size])) |
863 | return __size; | |
ed6814f7 | 864 | } |
04cc8aef | 865 | while (__size--); |
725dc051 BK |
866 | } |
867 | return npos; | |
868 | } | |
869 | ||
870 | template<typename _CharT, typename _Traits, typename _Alloc> | |
31bfa177 | 871 | typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 BK |
872 | basic_string<_CharT, _Traits, _Alloc>:: |
873 | find_last_not_of(_CharT __c, size_type __pos) const | |
874 | { | |
04cc8aef | 875 | size_type __size = this->size(); |
725dc051 | 876 | if (__size) |
fefe561e | 877 | { |
04cc8aef | 878 | if (--__size > __pos) |
ed6814f7 | 879 | __size = __pos; |
725dc051 BK |
880 | do |
881 | { | |
04cc8aef PC |
882 | if (!traits_type::eq(_M_data()[__size], __c)) |
883 | return __size; | |
ed6814f7 | 884 | } |
04cc8aef | 885 | while (__size--); |
725dc051 BK |
886 | } |
887 | return npos; | |
888 | } | |
ed6814f7 | 889 | |
725dc051 BK |
890 | template<typename _CharT, typename _Traits, typename _Alloc> |
891 | int | |
892 | basic_string<_CharT, _Traits, _Alloc>:: | |
893 | compare(size_type __pos, size_type __n, const basic_string& __str) const | |
894 | { | |
04cc8aef | 895 | _M_check(__pos, "basic_string::compare"); |
e03a6fb7 | 896 | __n = _M_limit(__pos, __n); |
7778fa6e | 897 | const size_type __osize = __str.size(); |
e03a6fb7 | 898 | const size_type __len = std::min(__n, __osize); |
725dc051 BK |
899 | int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len); |
900 | if (!__r) | |
9521dd6b | 901 | __r = _S_compare(__n, __osize); |
725dc051 BK |
902 | return __r; |
903 | } | |
904 | ||
905 | template<typename _CharT, typename _Traits, typename _Alloc> | |
906 | int | |
907 | basic_string<_CharT, _Traits, _Alloc>:: | |
908 | compare(size_type __pos1, size_type __n1, const basic_string& __str, | |
909 | size_type __pos2, size_type __n2) const | |
910 | { | |
04cc8aef PC |
911 | _M_check(__pos1, "basic_string::compare"); |
912 | __str._M_check(__pos2, "basic_string::compare"); | |
e03a6fb7 PC |
913 | __n1 = _M_limit(__pos1, __n1); |
914 | __n2 = __str._M_limit(__pos2, __n2); | |
915 | const size_type __len = std::min(__n1, __n2); | |
ed6814f7 | 916 | int __r = traits_type::compare(_M_data() + __pos1, |
725dc051 BK |
917 | __str.data() + __pos2, __len); |
918 | if (!__r) | |
9521dd6b | 919 | __r = _S_compare(__n1, __n2); |
725dc051 BK |
920 | return __r; |
921 | } | |
922 | ||
725dc051 BK |
923 | template<typename _CharT, typename _Traits, typename _Alloc> |
924 | int | |
925 | basic_string<_CharT, _Traits, _Alloc>:: | |
926 | compare(const _CharT* __s) const | |
927 | { | |
285b36d6 | 928 | __glibcxx_requires_string(__s); |
7778fa6e PC |
929 | const size_type __size = this->size(); |
930 | const size_type __osize = traits_type::length(__s); | |
931 | const size_type __len = std::min(__size, __osize); | |
c86c54e6 | 932 | int __r = traits_type::compare(_M_data(), __s, __len); |
725dc051 | 933 | if (!__r) |
9521dd6b | 934 | __r = _S_compare(__size, __osize); |
725dc051 BK |
935 | return __r; |
936 | } | |
937 | ||
3b0fd4bc BK |
938 | template<typename _CharT, typename _Traits, typename _Alloc> |
939 | int | |
9a304d17 | 940 | basic_string <_CharT, _Traits, _Alloc>:: |
3b0fd4bc BK |
941 | compare(size_type __pos, size_type __n1, const _CharT* __s) const |
942 | { | |
285b36d6 | 943 | __glibcxx_requires_string(__s); |
04cc8aef | 944 | _M_check(__pos, "basic_string::compare"); |
e03a6fb7 | 945 | __n1 = _M_limit(__pos, __n1); |
7778fa6e | 946 | const size_type __osize = traits_type::length(__s); |
e03a6fb7 | 947 | const size_type __len = std::min(__n1, __osize); |
3b0fd4bc BK |
948 | int __r = traits_type::compare(_M_data() + __pos, __s, __len); |
949 | if (!__r) | |
9521dd6b | 950 | __r = _S_compare(__n1, __osize); |
3b0fd4bc BK |
951 | return __r; |
952 | } | |
953 | ||
725dc051 BK |
954 | template<typename _CharT, typename _Traits, typename _Alloc> |
955 | int | |
9a304d17 | 956 | basic_string <_CharT, _Traits, _Alloc>:: |
ed6814f7 | 957 | compare(size_type __pos, size_type __n1, const _CharT* __s, |
725dc051 BK |
958 | size_type __n2) const |
959 | { | |
285b36d6 | 960 | __glibcxx_requires_string_len(__s, __n2); |
04cc8aef | 961 | _M_check(__pos, "basic_string::compare"); |
e03a6fb7 PC |
962 | __n1 = _M_limit(__pos, __n1); |
963 | const size_type __len = std::min(__n1, __n2); | |
725dc051 BK |
964 | int __r = traits_type::compare(_M_data() + __pos, __s, __len); |
965 | if (!__r) | |
9521dd6b | 966 | __r = _S_compare(__n1, __n2); |
725dc051 BK |
967 | return __r; |
968 | } | |
969 | ||
11202768 PC |
970 | // 21.3.7.9 basic_string::getline and operators |
971 | template<typename _CharT, typename _Traits, typename _Alloc> | |
972 | basic_istream<_CharT, _Traits>& | |
973 | operator>>(basic_istream<_CharT, _Traits>& __in, | |
974 | basic_string<_CharT, _Traits, _Alloc>& __str) | |
975 | { | |
976 | typedef basic_istream<_CharT, _Traits> __istream_type; | |
977 | typedef basic_string<_CharT, _Traits, _Alloc> __string_type; | |
978 | typedef typename __istream_type::ios_base __ios_base; | |
979 | typedef typename __istream_type::int_type __int_type; | |
980 | typedef typename __string_type::size_type __size_type; | |
981 | typedef ctype<_CharT> __ctype_type; | |
982 | typedef typename __ctype_type::ctype_base __ctype_base; | |
983 | ||
984 | __size_type __extracted = 0; | |
985 | typename __ios_base::iostate __err = __ios_base::goodbit; | |
986 | typename __istream_type::sentry __cerb(__in, false); | |
987 | if (__cerb) | |
988 | { | |
bc2631e0 | 989 | __try |
11202768 PC |
990 | { |
991 | // Avoid reallocation for common case. | |
992 | __str.erase(); | |
993 | _CharT __buf[128]; | |
994 | __size_type __len = 0; | |
995 | const streamsize __w = __in.width(); | |
996 | const __size_type __n = __w > 0 ? static_cast<__size_type>(__w) | |
997 | : __str.max_size(); | |
998 | const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc()); | |
999 | const __int_type __eof = _Traits::eof(); | |
1000 | __int_type __c = __in.rdbuf()->sgetc(); | |
1001 | ||
1002 | while (__extracted < __n | |
1003 | && !_Traits::eq_int_type(__c, __eof) | |
1004 | && !__ct.is(__ctype_base::space, | |
1005 | _Traits::to_char_type(__c))) | |
1006 | { | |
1007 | if (__len == sizeof(__buf) / sizeof(_CharT)) | |
1008 | { | |
1009 | __str.append(__buf, sizeof(__buf) / sizeof(_CharT)); | |
1010 | __len = 0; | |
1011 | } | |
1012 | __buf[__len++] = _Traits::to_char_type(__c); | |
1013 | ++__extracted; | |
1014 | __c = __in.rdbuf()->snextc(); | |
1015 | } | |
1016 | __str.append(__buf, __len); | |
1017 | ||
1018 | if (_Traits::eq_int_type(__c, __eof)) | |
1019 | __err |= __ios_base::eofbit; | |
1020 | __in.width(0); | |
1021 | } | |
bc2631e0 | 1022 | __catch(__cxxabiv1::__forced_unwind&) |
d05f74f1 JM |
1023 | { |
1024 | __in._M_setstate(__ios_base::badbit); | |
1025 | __throw_exception_again; | |
1026 | } | |
bc2631e0 | 1027 | __catch(...) |
11202768 PC |
1028 | { |
1029 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
1030 | // 91. Description of operator>> and getline() for string<> | |
1031 | // might cause endless loop | |
1032 | __in._M_setstate(__ios_base::badbit); | |
1033 | } | |
1034 | } | |
1035 | // 211. operator>>(istream&, string&) doesn't set failbit | |
1036 | if (!__extracted) | |
1037 | __err |= __ios_base::failbit; | |
1038 | if (__err) | |
1039 | __in.setstate(__err); | |
1040 | return __in; | |
1041 | } | |
1042 | ||
1043 | template<typename _CharT, typename _Traits, typename _Alloc> | |
1044 | basic_istream<_CharT, _Traits>& | |
1045 | getline(basic_istream<_CharT, _Traits>& __in, | |
1046 | basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim) | |
1047 | { | |
1048 | typedef basic_istream<_CharT, _Traits> __istream_type; | |
1049 | typedef basic_string<_CharT, _Traits, _Alloc> __string_type; | |
1050 | typedef typename __istream_type::ios_base __ios_base; | |
1051 | typedef typename __istream_type::int_type __int_type; | |
1052 | typedef typename __string_type::size_type __size_type; | |
1053 | ||
1054 | __size_type __extracted = 0; | |
1055 | const __size_type __n = __str.max_size(); | |
1056 | typename __ios_base::iostate __err = __ios_base::goodbit; | |
1057 | typename __istream_type::sentry __cerb(__in, true); | |
1058 | if (__cerb) | |
1059 | { | |
bc2631e0 | 1060 | __try |
11202768 PC |
1061 | { |
1062 | __str.erase(); | |
1063 | const __int_type __idelim = _Traits::to_int_type(__delim); | |
1064 | const __int_type __eof = _Traits::eof(); | |
1065 | __int_type __c = __in.rdbuf()->sgetc(); | |
1066 | ||
1067 | while (__extracted < __n | |
1068 | && !_Traits::eq_int_type(__c, __eof) | |
1069 | && !_Traits::eq_int_type(__c, __idelim)) | |
1070 | { | |
1071 | __str += _Traits::to_char_type(__c); | |
1072 | ++__extracted; | |
1073 | __c = __in.rdbuf()->snextc(); | |
1074 | } | |
1075 | ||
1076 | if (_Traits::eq_int_type(__c, __eof)) | |
1077 | __err |= __ios_base::eofbit; | |
1078 | else if (_Traits::eq_int_type(__c, __idelim)) | |
1079 | { | |
1080 | ++__extracted; | |
1081 | __in.rdbuf()->sbumpc(); | |
1082 | } | |
1083 | else | |
1084 | __err |= __ios_base::failbit; | |
1085 | } | |
bc2631e0 | 1086 | __catch(__cxxabiv1::__forced_unwind&) |
d05f74f1 JM |
1087 | { |
1088 | __in._M_setstate(__ios_base::badbit); | |
1089 | __throw_exception_again; | |
1090 | } | |
bc2631e0 | 1091 | __catch(...) |
11202768 PC |
1092 | { |
1093 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
1094 | // 91. Description of operator>> and getline() for string<> | |
1095 | // might cause endless loop | |
1096 | __in._M_setstate(__ios_base::badbit); | |
1097 | } | |
1098 | } | |
1099 | if (!__extracted) | |
1100 | __err |= __ios_base::failbit; | |
1101 | if (__err) | |
1102 | __in.setstate(__err); | |
1103 | return __in; | |
1104 | } | |
1105 | ||
a32e3c09 | 1106 | // Inhibit implicit instantiations for required instantiations, |
ed6814f7 | 1107 | // which are defined via explicit instantiations elsewhere. |
a32e3c09 | 1108 | // NB: This syntax is a GNU extension. |
52e07aa1 | 1109 | #if _GLIBCXX_EXTERN_TEMPLATE > 0 |
a32e3c09 | 1110 | extern template class basic_string<char>; |
ed6814f7 BI |
1111 | extern template |
1112 | basic_istream<char>& | |
a32e3c09 | 1113 | operator>>(basic_istream<char>&, string&); |
ed6814f7 BI |
1114 | extern template |
1115 | basic_ostream<char>& | |
a32e3c09 | 1116 | operator<<(basic_ostream<char>&, const string&); |
ed6814f7 BI |
1117 | extern template |
1118 | basic_istream<char>& | |
a32e3c09 | 1119 | getline(basic_istream<char>&, string&, char); |
ed6814f7 BI |
1120 | extern template |
1121 | basic_istream<char>& | |
a32e3c09 BK |
1122 | getline(basic_istream<char>&, string&); |
1123 | ||
3d7c150e | 1124 | #ifdef _GLIBCXX_USE_WCHAR_T |
a32e3c09 | 1125 | extern template class basic_string<wchar_t>; |
ed6814f7 BI |
1126 | extern template |
1127 | basic_istream<wchar_t>& | |
a32e3c09 | 1128 | operator>>(basic_istream<wchar_t>&, wstring&); |
ed6814f7 BI |
1129 | extern template |
1130 | basic_ostream<wchar_t>& | |
a32e3c09 | 1131 | operator<<(basic_ostream<wchar_t>&, const wstring&); |
ed6814f7 BI |
1132 | extern template |
1133 | basic_istream<wchar_t>& | |
a32e3c09 | 1134 | getline(basic_istream<wchar_t>&, wstring&, wchar_t); |
ed6814f7 BI |
1135 | extern template |
1136 | basic_istream<wchar_t>& | |
a32e3c09 | 1137 | getline(basic_istream<wchar_t>&, wstring&); |
5112ae3a | 1138 | #endif |
1bc8b0ad | 1139 | #endif |
3cbc7af0 BK |
1140 | |
1141 | _GLIBCXX_END_NAMESPACE | |
725dc051 | 1142 | |
3b0fd4bc | 1143 | #endif |