]>
Commit | Line | Data |
---|---|---|
725dc051 BK |
1 | // Components for manipulating sequences of characters -*- C++ -*- |
2 | ||
99dee823 | 3 | // Copyright (C) 1997-2021 Free Software Foundation, Inc. |
725dc051 BK |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
725dc051 BK |
9 | // any later version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
19 | ||
20 | // You should have received a copy of the GNU General Public License and | |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
725dc051 | 24 | |
f910786b | 25 | /** @file bits/basic_string.tcc |
0aa06b18 | 26 | * This is an internal header file, included by other library headers. |
f910786b | 27 | * Do not attempt to use it directly. @headername{string} |
0aa06b18 BK |
28 | */ |
29 | ||
725dc051 BK |
30 | // |
31 | // ISO C++ 14882: 21 Strings library | |
32 | // | |
33 | ||
725dc051 BK |
34 | // Written by Jason Merrill based upon the specification by Takanori Adachi |
35 | // in ANSI X3J16/94-0013R2. Rewritten by Nathan Myers to ISO-14882. | |
34a2b755 JW |
36 | // Non-reference-counted implementation written by Paolo Carlini and |
37 | // updated by Jonathan Wakely for ISO-14882-2011. | |
725dc051 | 38 | |
3d7c150e BK |
39 | #ifndef _BASIC_STRING_TCC |
40 | #define _BASIC_STRING_TCC 1 | |
725dc051 | 41 | |
3b794528 BK |
42 | #pragma GCC system_header |
43 | ||
7c3e9502 | 44 | #include <bits/cxxabi_forced.h> |
d05f74f1 | 45 | |
12ffa228 BK |
46 | namespace std _GLIBCXX_VISIBILITY(default) |
47 | { | |
48 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
3cbc7af0 | 49 | |
34a2b755 JW |
50 | #if _GLIBCXX_USE_CXX11_ABI |
51 | ||
52 | template<typename _CharT, typename _Traits, typename _Alloc> | |
53 | const typename basic_string<_CharT, _Traits, _Alloc>::size_type | |
54 | basic_string<_CharT, _Traits, _Alloc>::npos; | |
55 | ||
56 | template<typename _CharT, typename _Traits, typename _Alloc> | |
57 | void | |
58 | basic_string<_CharT, _Traits, _Alloc>:: | |
59 | swap(basic_string& __s) _GLIBCXX_NOEXCEPT | |
60 | { | |
61 | if (this == &__s) | |
62 | return; | |
63 | ||
5caff414 | 64 | _Alloc_traits::_S_on_swap(_M_get_allocator(), __s._M_get_allocator()); |
34a2b755 JW |
65 | |
66 | if (_M_is_local()) | |
67 | if (__s._M_is_local()) | |
68 | { | |
69 | if (length() && __s.length()) | |
70 | { | |
71 | _CharT __tmp_data[_S_local_capacity + 1]; | |
72 | traits_type::copy(__tmp_data, __s._M_local_buf, | |
73 | _S_local_capacity + 1); | |
74 | traits_type::copy(__s._M_local_buf, _M_local_buf, | |
75 | _S_local_capacity + 1); | |
76 | traits_type::copy(_M_local_buf, __tmp_data, | |
77 | _S_local_capacity + 1); | |
78 | } | |
79 | else if (__s.length()) | |
80 | { | |
81 | traits_type::copy(_M_local_buf, __s._M_local_buf, | |
82 | _S_local_capacity + 1); | |
83 | _M_length(__s.length()); | |
84 | __s._M_set_length(0); | |
85 | return; | |
86 | } | |
87 | else if (length()) | |
88 | { | |
89 | traits_type::copy(__s._M_local_buf, _M_local_buf, | |
90 | _S_local_capacity + 1); | |
91 | __s._M_length(length()); | |
92 | _M_set_length(0); | |
93 | return; | |
94 | } | |
95 | } | |
96 | else | |
97 | { | |
98 | const size_type __tmp_capacity = __s._M_allocated_capacity; | |
99 | traits_type::copy(__s._M_local_buf, _M_local_buf, | |
100 | _S_local_capacity + 1); | |
101 | _M_data(__s._M_data()); | |
102 | __s._M_data(__s._M_local_buf); | |
103 | _M_capacity(__tmp_capacity); | |
104 | } | |
105 | else | |
106 | { | |
107 | const size_type __tmp_capacity = _M_allocated_capacity; | |
108 | if (__s._M_is_local()) | |
109 | { | |
110 | traits_type::copy(_M_local_buf, __s._M_local_buf, | |
111 | _S_local_capacity + 1); | |
112 | __s._M_data(_M_data()); | |
113 | _M_data(_M_local_buf); | |
114 | } | |
115 | else | |
116 | { | |
117 | pointer __tmp_ptr = _M_data(); | |
118 | _M_data(__s._M_data()); | |
119 | __s._M_data(__tmp_ptr); | |
120 | _M_capacity(__s._M_allocated_capacity); | |
121 | } | |
122 | __s._M_capacity(__tmp_capacity); | |
123 | } | |
124 | ||
125 | const size_type __tmp_length = length(); | |
126 | _M_length(__s.length()); | |
127 | __s._M_length(__tmp_length); | |
128 | } | |
129 | ||
130 | template<typename _CharT, typename _Traits, typename _Alloc> | |
131 | typename basic_string<_CharT, _Traits, _Alloc>::pointer | |
132 | basic_string<_CharT, _Traits, _Alloc>:: | |
133 | _M_create(size_type& __capacity, size_type __old_capacity) | |
134 | { | |
135 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
136 | // 83. String::npos vs. string::max_size() | |
137 | if (__capacity > max_size()) | |
138 | std::__throw_length_error(__N("basic_string::_M_create")); | |
139 | ||
140 | // The below implements an exponential growth policy, necessary to | |
141 | // meet amortized linear time requirements of the library: see | |
142 | // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html. | |
143 | if (__capacity > __old_capacity && __capacity < 2 * __old_capacity) | |
144 | { | |
145 | __capacity = 2 * __old_capacity; | |
146 | // Never allocate a string bigger than max_size. | |
147 | if (__capacity > max_size()) | |
148 | __capacity = max_size(); | |
149 | } | |
150 | ||
151 | // NB: Need an array of char_type[__capacity], plus a terminating | |
152 | // null char_type() element. | |
153 | return _Alloc_traits::allocate(_M_get_allocator(), __capacity + 1); | |
154 | } | |
155 | ||
156 | // NB: This is the special case for Input Iterators, used in | |
157 | // istreambuf_iterators, etc. | |
158 | // Input Iterators have a cost structure very different from | |
159 | // pointers, calling for a different coding style. | |
160 | template<typename _CharT, typename _Traits, typename _Alloc> | |
161 | template<typename _InIterator> | |
162 | void | |
163 | basic_string<_CharT, _Traits, _Alloc>:: | |
164 | _M_construct(_InIterator __beg, _InIterator __end, | |
165 | std::input_iterator_tag) | |
166 | { | |
167 | size_type __len = 0; | |
168 | size_type __capacity = size_type(_S_local_capacity); | |
169 | ||
170 | while (__beg != __end && __len < __capacity) | |
171 | { | |
172 | _M_data()[__len++] = *__beg; | |
173 | ++__beg; | |
174 | } | |
175 | ||
176 | __try | |
177 | { | |
178 | while (__beg != __end) | |
179 | { | |
180 | if (__len == __capacity) | |
181 | { | |
182 | // Allocate more space. | |
183 | __capacity = __len + 1; | |
184 | pointer __another = _M_create(__capacity, __len); | |
185 | this->_S_copy(__another, _M_data(), __len); | |
186 | _M_dispose(); | |
187 | _M_data(__another); | |
188 | _M_capacity(__capacity); | |
189 | } | |
190 | _M_data()[__len++] = *__beg; | |
191 | ++__beg; | |
192 | } | |
193 | } | |
194 | __catch(...) | |
195 | { | |
196 | _M_dispose(); | |
197 | __throw_exception_again; | |
198 | } | |
199 | ||
200 | _M_set_length(__len); | |
201 | } | |
202 | ||
203 | template<typename _CharT, typename _Traits, typename _Alloc> | |
204 | template<typename _InIterator> | |
205 | void | |
206 | basic_string<_CharT, _Traits, _Alloc>:: | |
207 | _M_construct(_InIterator __beg, _InIterator __end, | |
208 | std::forward_iterator_tag) | |
209 | { | |
210 | // NB: Not required, but considered best practice. | |
211 | if (__gnu_cxx::__is_null_pointer(__beg) && __beg != __end) | |
212 | std::__throw_logic_error(__N("basic_string::" | |
213 | "_M_construct null not valid")); | |
214 | ||
215 | size_type __dnew = static_cast<size_type>(std::distance(__beg, __end)); | |
216 | ||
217 | if (__dnew > size_type(_S_local_capacity)) | |
218 | { | |
219 | _M_data(_M_create(__dnew, size_type(0))); | |
220 | _M_capacity(__dnew); | |
221 | } | |
222 | ||
223 | // Check for out_of_range and length_error exceptions. | |
224 | __try | |
225 | { this->_S_copy_chars(_M_data(), __beg, __end); } | |
226 | __catch(...) | |
227 | { | |
228 | _M_dispose(); | |
229 | __throw_exception_again; | |
230 | } | |
231 | ||
232 | _M_set_length(__dnew); | |
233 | } | |
234 | ||
235 | template<typename _CharT, typename _Traits, typename _Alloc> | |
236 | void | |
237 | basic_string<_CharT, _Traits, _Alloc>:: | |
238 | _M_construct(size_type __n, _CharT __c) | |
239 | { | |
240 | if (__n > size_type(_S_local_capacity)) | |
241 | { | |
242 | _M_data(_M_create(__n, size_type(0))); | |
243 | _M_capacity(__n); | |
244 | } | |
245 | ||
246 | if (__n) | |
247 | this->_S_assign(_M_data(), __n, __c); | |
248 | ||
249 | _M_set_length(__n); | |
250 | } | |
251 | ||
252 | template<typename _CharT, typename _Traits, typename _Alloc> | |
253 | void | |
254 | basic_string<_CharT, _Traits, _Alloc>:: | |
255 | _M_assign(const basic_string& __str) | |
256 | { | |
257 | if (this != &__str) | |
258 | { | |
259 | const size_type __rsize = __str.length(); | |
260 | const size_type __capacity = capacity(); | |
261 | ||
262 | if (__rsize > __capacity) | |
263 | { | |
264 | size_type __new_capacity = __rsize; | |
265 | pointer __tmp = _M_create(__new_capacity, __capacity); | |
266 | _M_dispose(); | |
267 | _M_data(__tmp); | |
268 | _M_capacity(__new_capacity); | |
269 | } | |
270 | ||
271 | if (__rsize) | |
272 | this->_S_copy(_M_data(), __str._M_data(), __rsize); | |
273 | ||
274 | _M_set_length(__rsize); | |
275 | } | |
276 | } | |
277 | ||
278 | template<typename _CharT, typename _Traits, typename _Alloc> | |
279 | void | |
280 | basic_string<_CharT, _Traits, _Alloc>:: | |
281 | reserve(size_type __res) | |
282 | { | |
34a2b755 | 283 | const size_type __capacity = capacity(); |
140cf935 AL |
284 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
285 | // 2968. Inconsistencies between basic_string reserve and | |
286 | // vector/unordered_map/unordered_set reserve functions | |
287 | // P0966 reserve should not shrink | |
288 | if (__res <= __capacity) | |
289 | return; | |
290 | ||
291 | pointer __tmp = _M_create(__res, __capacity); | |
292 | this->_S_copy(__tmp, _M_data(), length() + 1); | |
293 | _M_dispose(); | |
294 | _M_data(__tmp); | |
295 | _M_capacity(__res); | |
34a2b755 JW |
296 | } |
297 | ||
298 | template<typename _CharT, typename _Traits, typename _Alloc> | |
299 | void | |
300 | basic_string<_CharT, _Traits, _Alloc>:: | |
301 | _M_mutate(size_type __pos, size_type __len1, const _CharT* __s, | |
302 | size_type __len2) | |
303 | { | |
304 | const size_type __how_much = length() - __pos - __len1; | |
305 | ||
306 | size_type __new_capacity = length() + __len2 - __len1; | |
307 | pointer __r = _M_create(__new_capacity, capacity()); | |
308 | ||
309 | if (__pos) | |
2d60da10 | 310 | this->_S_copy(__r, _M_data(), __pos); |
34a2b755 | 311 | if (__s && __len2) |
2d60da10 | 312 | this->_S_copy(__r + __pos, __s, __len2); |
34a2b755 | 313 | if (__how_much) |
2d60da10 JW |
314 | this->_S_copy(__r + __pos + __len2, |
315 | _M_data() + __pos + __len1, __how_much); | |
34a2b755 JW |
316 | |
317 | _M_dispose(); | |
318 | _M_data(__r); | |
319 | _M_capacity(__new_capacity); | |
320 | } | |
321 | ||
322 | template<typename _CharT, typename _Traits, typename _Alloc> | |
323 | void | |
324 | basic_string<_CharT, _Traits, _Alloc>:: | |
325 | _M_erase(size_type __pos, size_type __n) | |
326 | { | |
327 | const size_type __how_much = length() - __pos - __n; | |
328 | ||
329 | if (__how_much && __n) | |
2d60da10 | 330 | this->_S_move(_M_data() + __pos, _M_data() + __pos + __n, __how_much); |
34a2b755 JW |
331 | |
332 | _M_set_length(length() - __n); | |
333 | } | |
334 | ||
140cf935 AL |
335 | template<typename _CharT, typename _Traits, typename _Alloc> |
336 | void | |
337 | basic_string<_CharT, _Traits, _Alloc>:: | |
338 | reserve() | |
339 | { | |
340 | if (_M_is_local()) | |
341 | return; | |
342 | ||
343 | const size_type __length = length(); | |
344 | const size_type __capacity = _M_allocated_capacity; | |
345 | ||
346 | if (__length <= size_type(_S_local_capacity)) | |
347 | { | |
348 | this->_S_copy(_M_local_data(), _M_data(), __length + 1); | |
349 | _M_destroy(__capacity); | |
350 | _M_data(_M_local_data()); | |
351 | } | |
352 | #if __cpp_exceptions | |
353 | else if (__length < __capacity) | |
354 | try | |
355 | { | |
356 | pointer __tmp | |
357 | = _Alloc_traits::allocate(_M_get_allocator(), __length + 1); | |
358 | this->_S_copy(__tmp, _M_data(), __length + 1); | |
359 | _M_dispose(); | |
360 | _M_data(__tmp); | |
361 | _M_capacity(__length); | |
362 | } | |
363 | catch (const __cxxabiv1::__forced_unwind&) | |
364 | { throw; } | |
365 | catch (...) | |
366 | { /* swallow the exception */ } | |
367 | #endif | |
368 | } | |
369 | ||
34a2b755 JW |
370 | template<typename _CharT, typename _Traits, typename _Alloc> |
371 | void | |
372 | basic_string<_CharT, _Traits, _Alloc>:: | |
373 | resize(size_type __n, _CharT __c) | |
374 | { | |
375 | const size_type __size = this->size(); | |
376 | if (__size < __n) | |
377 | this->append(__n - __size, __c); | |
378 | else if (__n < __size) | |
a922c5ff | 379 | this->_M_set_length(__n); |
34a2b755 JW |
380 | } |
381 | ||
382 | template<typename _CharT, typename _Traits, typename _Alloc> | |
383 | basic_string<_CharT, _Traits, _Alloc>& | |
384 | basic_string<_CharT, _Traits, _Alloc>:: | |
385 | _M_append(const _CharT* __s, size_type __n) | |
386 | { | |
387 | const size_type __len = __n + this->size(); | |
388 | ||
389 | if (__len <= this->capacity()) | |
390 | { | |
391 | if (__n) | |
392 | this->_S_copy(this->_M_data() + this->size(), __s, __n); | |
393 | } | |
394 | else | |
395 | this->_M_mutate(this->size(), size_type(0), __s, __n); | |
396 | ||
397 | this->_M_set_length(__len); | |
398 | return *this; | |
399 | } | |
400 | ||
401 | template<typename _CharT, typename _Traits, typename _Alloc> | |
402 | template<typename _InputIterator> | |
403 | basic_string<_CharT, _Traits, _Alloc>& | |
404 | basic_string<_CharT, _Traits, _Alloc>:: | |
405 | _M_replace_dispatch(const_iterator __i1, const_iterator __i2, | |
406 | _InputIterator __k1, _InputIterator __k2, | |
407 | std::__false_type) | |
408 | { | |
046af809 NDR |
409 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
410 | // 2788. unintentionally require a default constructible allocator | |
411 | const basic_string __s(__k1, __k2, this->get_allocator()); | |
34a2b755 JW |
412 | const size_type __n1 = __i2 - __i1; |
413 | return _M_replace(__i1 - begin(), __n1, __s._M_data(), | |
414 | __s.size()); | |
415 | } | |
416 | ||
417 | template<typename _CharT, typename _Traits, typename _Alloc> | |
418 | basic_string<_CharT, _Traits, _Alloc>& | |
419 | basic_string<_CharT, _Traits, _Alloc>:: | |
420 | _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2, | |
421 | _CharT __c) | |
422 | { | |
423 | _M_check_length(__n1, __n2, "basic_string::_M_replace_aux"); | |
424 | ||
425 | const size_type __old_size = this->size(); | |
426 | const size_type __new_size = __old_size + __n2 - __n1; | |
427 | ||
428 | if (__new_size <= this->capacity()) | |
429 | { | |
2d60da10 | 430 | pointer __p = this->_M_data() + __pos1; |
34a2b755 JW |
431 | |
432 | const size_type __how_much = __old_size - __pos1 - __n1; | |
433 | if (__how_much && __n1 != __n2) | |
434 | this->_S_move(__p + __n2, __p + __n1, __how_much); | |
435 | } | |
436 | else | |
437 | this->_M_mutate(__pos1, __n1, 0, __n2); | |
438 | ||
439 | if (__n2) | |
2d60da10 | 440 | this->_S_assign(this->_M_data() + __pos1, __n2, __c); |
34a2b755 JW |
441 | |
442 | this->_M_set_length(__new_size); | |
443 | return *this; | |
444 | } | |
445 | ||
446 | template<typename _CharT, typename _Traits, typename _Alloc> | |
447 | basic_string<_CharT, _Traits, _Alloc>& | |
448 | basic_string<_CharT, _Traits, _Alloc>:: | |
449 | _M_replace(size_type __pos, size_type __len1, const _CharT* __s, | |
450 | const size_type __len2) | |
451 | { | |
452 | _M_check_length(__len1, __len2, "basic_string::_M_replace"); | |
453 | ||
454 | const size_type __old_size = this->size(); | |
455 | const size_type __new_size = __old_size + __len2 - __len1; | |
456 | ||
457 | if (__new_size <= this->capacity()) | |
458 | { | |
5caff414 | 459 | pointer __p = this->_M_data() + __pos; |
34a2b755 JW |
460 | |
461 | const size_type __how_much = __old_size - __pos - __len1; | |
462 | if (_M_disjunct(__s)) | |
463 | { | |
464 | if (__how_much && __len1 != __len2) | |
465 | this->_S_move(__p + __len2, __p + __len1, __how_much); | |
466 | if (__len2) | |
467 | this->_S_copy(__p, __s, __len2); | |
468 | } | |
469 | else | |
470 | { | |
471 | // Work in-place. | |
472 | if (__len2 && __len2 <= __len1) | |
473 | this->_S_move(__p, __s, __len2); | |
474 | if (__how_much && __len1 != __len2) | |
475 | this->_S_move(__p + __len2, __p + __len1, __how_much); | |
476 | if (__len2 > __len1) | |
477 | { | |
478 | if (__s + __len2 <= __p + __len1) | |
479 | this->_S_move(__p, __s, __len2); | |
480 | else if (__s >= __p + __len1) | |
481 | this->_S_copy(__p, __s + __len2 - __len1, __len2); | |
482 | else | |
483 | { | |
484 | const size_type __nleft = (__p + __len1) - __s; | |
485 | this->_S_move(__p, __s, __nleft); | |
486 | this->_S_copy(__p + __nleft, __p + __len2, | |
487 | __len2 - __nleft); | |
488 | } | |
489 | } | |
490 | } | |
491 | } | |
492 | else | |
493 | this->_M_mutate(__pos, __len1, __s, __len2); | |
494 | ||
495 | this->_M_set_length(__new_size); | |
496 | return *this; | |
497 | } | |
498 | ||
499 | template<typename _CharT, typename _Traits, typename _Alloc> | |
500 | typename basic_string<_CharT, _Traits, _Alloc>::size_type | |
501 | basic_string<_CharT, _Traits, _Alloc>:: | |
502 | copy(_CharT* __s, size_type __n, size_type __pos) const | |
503 | { | |
504 | _M_check(__pos, "basic_string::copy"); | |
505 | __n = _M_limit(__pos, __n); | |
506 | __glibcxx_requires_string_len(__s, __n); | |
507 | if (__n) | |
508 | _S_copy(__s, _M_data() + __pos, __n); | |
509 | // 21.3.5.7 par 3: do not append null. (good.) | |
510 | return __n; | |
511 | } | |
512 | ||
513 | #else // !_GLIBCXX_USE_CXX11_ABI | |
514 | ||
725dc051 | 515 | template<typename _CharT, typename _Traits, typename _Alloc> |
ed6814f7 | 516 | const typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 | 517 | basic_string<_CharT, _Traits, _Alloc>:: |
ca566e4c | 518 | _Rep::_S_max_size = (((npos - sizeof(_Rep_base))/sizeof(_CharT)) - 1) / 4; |
725dc051 BK |
519 | |
520 | template<typename _CharT, typename _Traits, typename _Alloc> | |
ed6814f7 | 521 | const _CharT |
725dc051 | 522 | basic_string<_CharT, _Traits, _Alloc>:: |
00532602 | 523 | _Rep::_S_terminal = _CharT(); |
725dc051 BK |
524 | |
525 | template<typename _CharT, typename _Traits, typename _Alloc> | |
4f12dd3c | 526 | const typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 BK |
527 | basic_string<_CharT, _Traits, _Alloc>::npos; |
528 | ||
529 | // Linker sets _S_empty_rep_storage to all 0s (one reference, empty string) | |
530 | // at static init time (before static ctors are run). | |
531 | template<typename _CharT, typename _Traits, typename _Alloc> | |
4f12dd3c | 532 | typename basic_string<_CharT, _Traits, _Alloc>::size_type |
ca566e4c NM |
533 | basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_empty_rep_storage[ |
534 | (sizeof(_Rep_base) + sizeof(_CharT) + sizeof(size_type) - 1) / | |
535 | sizeof(size_type)]; | |
725dc051 BK |
536 | |
537 | // NB: This is the special case for Input Iterators, used in | |
538 | // istreambuf_iterators, etc. | |
539 | // Input Iterators have a cost structure very different from | |
540 | // pointers, calling for a different coding style. | |
541 | template<typename _CharT, typename _Traits, typename _Alloc> | |
08addde6 | 542 | template<typename _InIterator> |
725dc051 BK |
543 | _CharT* |
544 | basic_string<_CharT, _Traits, _Alloc>:: | |
08addde6 | 545 | _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a, |
725dc051 BK |
546 | input_iterator_tag) |
547 | { | |
d7a3ef97 | 548 | #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 |
725dc051 | 549 | if (__beg == __end && __a == _Alloc()) |
ca566e4c | 550 | return _S_empty_rep()._M_refdata(); |
1165dc50 | 551 | #endif |
725dc051 | 552 | // Avoid reallocation for common case. |
247791f5 | 553 | _CharT __buf[128]; |
690495b0 PC |
554 | size_type __len = 0; |
555 | while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT)) | |
ed6814f7 BI |
556 | { |
557 | __buf[__len++] = *__beg; | |
690495b0 | 558 | ++__beg; |
725dc051 | 559 | } |
690495b0 | 560 | _Rep* __r = _Rep::_S_create(__len, size_type(0), __a); |
ec61e852 | 561 | _M_copy(__r->_M_refdata(), __buf, __len); |
bc2631e0 | 562 | __try |
e2c09482 | 563 | { |
690495b0 | 564 | while (__beg != __end) |
e2c09482 | 565 | { |
690495b0 | 566 | if (__len == __r->_M_capacity) |
e2c09482 | 567 | { |
690495b0 PC |
568 | // Allocate more space. |
569 | _Rep* __another = _Rep::_S_create(__len + 1, __len, __a); | |
ec61e852 | 570 | _M_copy(__another->_M_refdata(), __r->_M_refdata(), __len); |
690495b0 PC |
571 | __r->_M_destroy(__a); |
572 | __r = __another; | |
e2c09482 | 573 | } |
ed6814f7 | 574 | __r->_M_refdata()[__len++] = *__beg; |
690495b0 | 575 | ++__beg; |
e2c09482 BK |
576 | } |
577 | } | |
bc2631e0 | 578 | __catch(...) |
e2c09482 | 579 | { |
ed6814f7 | 580 | __r->_M_destroy(__a); |
e2c09482 BK |
581 | __throw_exception_again; |
582 | } | |
cbf52bfa | 583 | __r->_M_set_length_and_sharable(__len); |
690495b0 | 584 | return __r->_M_refdata(); |
725dc051 | 585 | } |
ed6814f7 | 586 | |
725dc051 | 587 | template<typename _CharT, typename _Traits, typename _Alloc> |
927dc7c6 | 588 | template <typename _InIterator> |
725dc051 | 589 | _CharT* |
9a304d17 | 590 | basic_string<_CharT, _Traits, _Alloc>:: |
927dc7c6 | 591 | _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a, |
725dc051 BK |
592 | forward_iterator_tag) |
593 | { | |
d7a3ef97 | 594 | #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 |
085825b8 | 595 | if (__beg == __end && __a == _Alloc()) |
ca566e4c | 596 | return _S_empty_rep()._M_refdata(); |
1165dc50 | 597 | #endif |
ed6814f7 | 598 | // NB: Not required, but considered best practice. |
e762c6f4 | 599 | if (__gnu_cxx::__is_null_pointer(__beg) && __beg != __end) |
8fc81078 | 600 | __throw_logic_error(__N("basic_string::_S_construct null not valid")); |
aa863dca | 601 | |
0e50667c PC |
602 | const size_type __dnew = static_cast<size_type>(std::distance(__beg, |
603 | __end)); | |
725dc051 | 604 | // Check for out_of_range and length_error exceptions. |
234e0d31 | 605 | _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a); |
bc2631e0 | 606 | __try |
e2c09482 | 607 | { _S_copy_chars(__r->_M_refdata(), __beg, __end); } |
bc2631e0 | 608 | __catch(...) |
ed6814f7 BI |
609 | { |
610 | __r->_M_destroy(__a); | |
e2c09482 BK |
611 | __throw_exception_again; |
612 | } | |
cbf52bfa | 613 | __r->_M_set_length_and_sharable(__dnew); |
725dc051 BK |
614 | return __r->_M_refdata(); |
615 | } | |
616 | ||
617 | template<typename _CharT, typename _Traits, typename _Alloc> | |
618 | _CharT* | |
9a304d17 | 619 | basic_string<_CharT, _Traits, _Alloc>:: |
725dc051 BK |
620 | _S_construct(size_type __n, _CharT __c, const _Alloc& __a) |
621 | { | |
d7a3ef97 | 622 | #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 |
725dc051 | 623 | if (__n == 0 && __a == _Alloc()) |
ca566e4c | 624 | return _S_empty_rep()._M_refdata(); |
1165dc50 | 625 | #endif |
725dc051 | 626 | // Check for out_of_range and length_error exceptions. |
234e0d31 | 627 | _Rep* __r = _Rep::_S_create(__n, size_type(0), __a); |
ed6814f7 | 628 | if (__n) |
ec61e852 | 629 | _M_assign(__r->_M_refdata(), __n, __c); |
954b12d2 | 630 | |
cbf52bfa | 631 | __r->_M_set_length_and_sharable(__n); |
725dc051 BK |
632 | return __r->_M_refdata(); |
633 | } | |
634 | ||
86bbf15b JW |
635 | template<typename _CharT, typename _Traits, typename _Alloc> |
636 | basic_string<_CharT, _Traits, _Alloc>:: | |
637 | basic_string(const basic_string& __str, size_type __pos, const _Alloc& __a) | |
638 | : _M_dataplus(_S_construct(__str._M_data() | |
639 | + __str._M_check(__pos, | |
640 | "basic_string::basic_string"), | |
641 | __str._M_data() + __str._M_limit(__pos, npos) | |
642 | + __pos, __a), __a) | |
643 | { } | |
644 | ||
725dc051 BK |
645 | template<typename _CharT, typename _Traits, typename _Alloc> |
646 | basic_string<_CharT, _Traits, _Alloc>:: | |
647 | basic_string(const basic_string& __str, size_type __pos, size_type __n) | |
a3af79ea | 648 | : _M_dataplus(_S_construct(__str._M_data() |
0e50667c PC |
649 | + __str._M_check(__pos, |
650 | "basic_string::basic_string"), | |
927dc7c6 PC |
651 | __str._M_data() + __str._M_limit(__pos, __n) |
652 | + __pos, _Alloc()), _Alloc()) | |
725dc051 BK |
653 | { } |
654 | ||
655 | template<typename _CharT, typename _Traits, typename _Alloc> | |
656 | basic_string<_CharT, _Traits, _Alloc>:: | |
657 | basic_string(const basic_string& __str, size_type __pos, | |
658 | size_type __n, const _Alloc& __a) | |
a3af79ea | 659 | : _M_dataplus(_S_construct(__str._M_data() |
0e50667c PC |
660 | + __str._M_check(__pos, |
661 | "basic_string::basic_string"), | |
927dc7c6 PC |
662 | __str._M_data() + __str._M_limit(__pos, __n) |
663 | + __pos, __a), __a) | |
725dc051 BK |
664 | { } |
665 | ||
5536e07d PC |
666 | template<typename _CharT, typename _Traits, typename _Alloc> |
667 | basic_string<_CharT, _Traits, _Alloc>& | |
668 | basic_string<_CharT, _Traits, _Alloc>:: | |
669 | assign(const basic_string& __str) | |
670 | { | |
671 | if (_M_rep() != __str._M_rep()) | |
672 | { | |
673 | // XXX MT | |
674 | const allocator_type __a = this->get_allocator(); | |
675 | _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator()); | |
676 | _M_rep()->_M_dispose(__a); | |
677 | _M_data(__tmp); | |
678 | } | |
679 | return *this; | |
680 | } | |
681 | ||
725dc051 BK |
682 | template<typename _CharT, typename _Traits, typename _Alloc> |
683 | basic_string<_CharT, _Traits, _Alloc>& | |
ca566e4c | 684 | basic_string<_CharT, _Traits, _Alloc>:: |
ec61e852 | 685 | assign(const _CharT* __s, size_type __n) |
725dc051 | 686 | { |
ec61e852 PC |
687 | __glibcxx_requires_string_len(__s, __n); |
688 | _M_check_length(this->size(), __n, "basic_string::assign"); | |
8eae76be | 689 | if (_M_disjunct(__s) || _M_rep()->_M_is_shared()) |
ec61e852 PC |
690 | return _M_replace_safe(size_type(0), this->size(), __s, __n); |
691 | else | |
725dc051 | 692 | { |
ec61e852 PC |
693 | // Work in-place. |
694 | const size_type __pos = __s - _M_data(); | |
695 | if (__pos >= __n) | |
d87bdb13 | 696 | _M_copy(_M_data(), __s, __n); |
ec61e852 | 697 | else if (__pos) |
d87bdb13 | 698 | _M_move(_M_data(), __s, __n); |
ec61e852 PC |
699 | _M_rep()->_M_set_length_and_sharable(__n); |
700 | return *this; | |
701 | } | |
702 | } | |
703 | ||
ab4375af PC |
704 | template<typename _CharT, typename _Traits, typename _Alloc> |
705 | basic_string<_CharT, _Traits, _Alloc>& | |
706 | basic_string<_CharT, _Traits, _Alloc>:: | |
707 | append(size_type __n, _CharT __c) | |
708 | { | |
709 | if (__n) | |
710 | { | |
711 | _M_check_length(size_type(0), __n, "basic_string::append"); | |
712 | const size_type __len = __n + this->size(); | |
713 | if (__len > this->capacity() || _M_rep()->_M_is_shared()) | |
714 | this->reserve(__len); | |
715 | _M_assign(_M_data() + this->size(), __n, __c); | |
716 | _M_rep()->_M_set_length_and_sharable(__len); | |
717 | } | |
718 | return *this; | |
719 | } | |
720 | ||
ec61e852 PC |
721 | template<typename _CharT, typename _Traits, typename _Alloc> |
722 | basic_string<_CharT, _Traits, _Alloc>& | |
723 | basic_string<_CharT, _Traits, _Alloc>:: | |
724 | append(const _CharT* __s, size_type __n) | |
725 | { | |
726 | __glibcxx_requires_string_len(__s, __n); | |
727 | if (__n) | |
728 | { | |
729 | _M_check_length(size_type(0), __n, "basic_string::append"); | |
730 | const size_type __len = __n + this->size(); | |
731 | if (__len > this->capacity() || _M_rep()->_M_is_shared()) | |
732 | { | |
8eae76be | 733 | if (_M_disjunct(__s)) |
ec61e852 PC |
734 | this->reserve(__len); |
735 | else | |
736 | { | |
737 | const size_type __off = __s - _M_data(); | |
738 | this->reserve(__len); | |
739 | __s = _M_data() + __off; | |
740 | } | |
741 | } | |
742 | _M_copy(_M_data() + this->size(), __s, __n); | |
743 | _M_rep()->_M_set_length_and_sharable(__len); | |
725dc051 BK |
744 | } |
745 | return *this; | |
746 | } | |
2d9d5235 | 747 | |
ab4375af PC |
748 | template<typename _CharT, typename _Traits, typename _Alloc> |
749 | basic_string<_CharT, _Traits, _Alloc>& | |
750 | basic_string<_CharT, _Traits, _Alloc>:: | |
751 | append(const basic_string& __str) | |
752 | { | |
753 | const size_type __size = __str.size(); | |
754 | if (__size) | |
755 | { | |
756 | const size_type __len = __size + this->size(); | |
757 | if (__len > this->capacity() || _M_rep()->_M_is_shared()) | |
758 | this->reserve(__len); | |
759 | _M_copy(_M_data() + this->size(), __str._M_data(), __size); | |
760 | _M_rep()->_M_set_length_and_sharable(__len); | |
761 | } | |
762 | return *this; | |
763 | } | |
764 | ||
ec61e852 PC |
765 | template<typename _CharT, typename _Traits, typename _Alloc> |
766 | basic_string<_CharT, _Traits, _Alloc>& | |
767 | basic_string<_CharT, _Traits, _Alloc>:: | |
768 | append(const basic_string& __str, size_type __pos, size_type __n) | |
769 | { | |
770 | __str._M_check(__pos, "basic_string::append"); | |
771 | __n = __str._M_limit(__pos, __n); | |
772 | if (__n) | |
773 | { | |
774 | const size_type __len = __n + this->size(); | |
775 | if (__len > this->capacity() || _M_rep()->_M_is_shared()) | |
776 | this->reserve(__len); | |
777 | _M_copy(_M_data() + this->size(), __str._M_data() + __pos, __n); | |
778 | _M_rep()->_M_set_length_and_sharable(__len); | |
779 | } | |
780 | return *this; | |
781 | } | |
2d9d5235 | 782 | |
2d9d5235 NM |
783 | template<typename _CharT, typename _Traits, typename _Alloc> |
784 | basic_string<_CharT, _Traits, _Alloc>& | |
785 | basic_string<_CharT, _Traits, _Alloc>:: | |
786 | insert(size_type __pos, const _CharT* __s, size_type __n) | |
787 | { | |
285b36d6 | 788 | __glibcxx_requires_string_len(__s, __n); |
04cc8aef | 789 | _M_check(__pos, "basic_string::insert"); |
ec61e852 | 790 | _M_check_length(size_type(0), __n, "basic_string::insert"); |
8eae76be | 791 | if (_M_disjunct(__s) || _M_rep()->_M_is_shared()) |
7bb9b33b | 792 | return _M_replace_safe(__pos, size_type(0), __s, __n); |
2d9d5235 NM |
793 | else |
794 | { | |
ec61e852 | 795 | // Work in-place. |
2d9d5235 NM |
796 | const size_type __off = __s - _M_data(); |
797 | _M_mutate(__pos, 0, __n); | |
798 | __s = _M_data() + __off; | |
799 | _CharT* __p = _M_data() + __pos; | |
800 | if (__s + __n <= __p) | |
ec61e852 | 801 | _M_copy(__p, __s, __n); |
2d9d5235 | 802 | else if (__s >= __p) |
ec61e852 | 803 | _M_copy(__p, __s + __n, __n); |
2d9d5235 NM |
804 | else |
805 | { | |
2cb612d1 | 806 | const size_type __nleft = __p - __s; |
ec61e852 PC |
807 | _M_copy(__p, __s, __nleft); |
808 | _M_copy(__p + __nleft, __p + __n, __n - __nleft); | |
2d9d5235 NM |
809 | } |
810 | return *this; | |
811 | } | |
812 | } | |
ed6814f7 | 813 | |
7309083f PC |
814 | template<typename _CharT, typename _Traits, typename _Alloc> |
815 | typename basic_string<_CharT, _Traits, _Alloc>::iterator | |
816 | basic_string<_CharT, _Traits, _Alloc>:: | |
817 | erase(iterator __first, iterator __last) | |
818 | { | |
819 | _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last | |
820 | && __last <= _M_iend()); | |
821 | ||
822 | // NB: This isn't just an optimization (bail out early when | |
823 | // there is nothing to do, really), it's also a correctness | |
824 | // issue vs MT, see libstdc++/40518. | |
825 | const size_type __size = __last - __first; | |
826 | if (__size) | |
827 | { | |
828 | const size_type __pos = __first - _M_ibegin(); | |
829 | _M_mutate(__pos, __size, size_type(0)); | |
830 | _M_rep()->_M_set_leaked(); | |
831 | return iterator(_M_data() + __pos); | |
832 | } | |
833 | else | |
834 | return __first; | |
835 | } | |
836 | ||
2d9d5235 NM |
837 | template<typename _CharT, typename _Traits, typename _Alloc> |
838 | basic_string<_CharT, _Traits, _Alloc>& | |
839 | basic_string<_CharT, _Traits, _Alloc>:: | |
840 | replace(size_type __pos, size_type __n1, const _CharT* __s, | |
841 | size_type __n2) | |
842 | { | |
285b36d6 | 843 | __glibcxx_requires_string_len(__s, __n2); |
04cc8aef | 844 | _M_check(__pos, "basic_string::replace"); |
e03a6fb7 | 845 | __n1 = _M_limit(__pos, __n1); |
ec61e852 | 846 | _M_check_length(__n1, __n2, "basic_string::replace"); |
2cb612d1 | 847 | bool __left; |
8eae76be | 848 | if (_M_disjunct(__s) || _M_rep()->_M_is_shared()) |
7bb9b33b | 849 | return _M_replace_safe(__pos, __n1, __s, __n2); |
2cb612d1 PC |
850 | else if ((__left = __s + __n2 <= _M_data() + __pos) |
851 | || _M_data() + __pos + __n1 <= __s) | |
852 | { | |
853 | // Work in-place: non-overlapping case. | |
ec61e852 PC |
854 | size_type __off = __s - _M_data(); |
855 | __left ? __off : (__off += __n2 - __n1); | |
2cb612d1 | 856 | _M_mutate(__pos, __n1, __n2); |
ec61e852 | 857 | _M_copy(_M_data() + __pos, _M_data() + __off, __n2); |
2cb612d1 PC |
858 | return *this; |
859 | } | |
2d9d5235 | 860 | else |
251804e6 | 861 | { |
2cb612d1 | 862 | // Todo: overlapping case. |
251804e6 PC |
863 | const basic_string __tmp(__s, __n2); |
864 | return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2); | |
865 | } | |
2d9d5235 | 866 | } |
ed6814f7 | 867 | |
725dc051 BK |
868 | template<typename _CharT, typename _Traits, typename _Alloc> |
869 | void | |
870 | basic_string<_CharT, _Traits, _Alloc>::_Rep:: | |
871 | _M_destroy(const _Alloc& __a) throw () | |
872 | { | |
6be8b524 PC |
873 | const size_type __size = sizeof(_Rep_base) + |
874 | (this->_M_capacity + 1) * sizeof(_CharT); | |
875 | _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size); | |
725dc051 BK |
876 | } |
877 | ||
878 | template<typename _CharT, typename _Traits, typename _Alloc> | |
879 | void | |
cc6e67d4 PC |
880 | basic_string<_CharT, _Traits, _Alloc>:: |
881 | _M_leak_hard() | |
725dc051 | 882 | { |
d7a3ef97 | 883 | #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 |
ca566e4c | 884 | if (_M_rep() == &_S_empty_rep()) |
1165dc50 PC |
885 | return; |
886 | #endif | |
ed6814f7 | 887 | if (_M_rep()->_M_is_shared()) |
725dc051 BK |
888 | _M_mutate(0, 0, 0); |
889 | _M_rep()->_M_set_leaked(); | |
890 | } | |
891 | ||
892 | template<typename _CharT, typename _Traits, typename _Alloc> | |
893 | void | |
894 | basic_string<_CharT, _Traits, _Alloc>:: | |
895 | _M_mutate(size_type __pos, size_type __len1, size_type __len2) | |
896 | { | |
7778fa6e | 897 | const size_type __old_size = this->size(); |
725dc051 | 898 | const size_type __new_size = __old_size + __len2 - __len1; |
725dc051 | 899 | const size_type __how_much = __old_size - __pos - __len1; |
ed6814f7 | 900 | |
cc6e67d4 | 901 | if (__new_size > this->capacity() || _M_rep()->_M_is_shared()) |
725dc051 BK |
902 | { |
903 | // Must reallocate. | |
91eab378 | 904 | const allocator_type __a = get_allocator(); |
cc6e67d4 | 905 | _Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a); |
954b12d2 PC |
906 | |
907 | if (__pos) | |
ec61e852 | 908 | _M_copy(__r->_M_refdata(), _M_data(), __pos); |
954b12d2 | 909 | if (__how_much) |
ec61e852 PC |
910 | _M_copy(__r->_M_refdata() + __pos + __len2, |
911 | _M_data() + __pos + __len1, __how_much); | |
954b12d2 | 912 | |
725dc051 BK |
913 | _M_rep()->_M_dispose(__a); |
914 | _M_data(__r->_M_refdata()); | |
ca566e4c | 915 | } |
725dc051 BK |
916 | else if (__how_much && __len1 != __len2) |
917 | { | |
ec61e852 PC |
918 | // Work in-place. |
919 | _M_move(_M_data() + __pos + __len2, | |
920 | _M_data() + __pos + __len1, __how_much); | |
725dc051 | 921 | } |
cbf52bfa | 922 | _M_rep()->_M_set_length_and_sharable(__new_size); |
725dc051 | 923 | } |
ed6814f7 | 924 | |
725dc051 BK |
925 | template<typename _CharT, typename _Traits, typename _Alloc> |
926 | void | |
cc6e67d4 PC |
927 | basic_string<_CharT, _Traits, _Alloc>:: |
928 | reserve(size_type __res) | |
725dc051 | 929 | { |
140cf935 AL |
930 | const size_type __capacity = capacity(); |
931 | ||
932 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
933 | // 2968. Inconsistencies between basic_string reserve and | |
934 | // vector/unordered_map/unordered_set reserve functions | |
935 | // P0966 reserve should not shrink | |
936 | if (__res <= __capacity) | |
937 | { | |
938 | if (!_M_rep()->_M_is_shared()) | |
939 | return; | |
940 | ||
941 | // unshare, but keep same capacity | |
942 | __res = __capacity; | |
943 | } | |
944 | ||
945 | const allocator_type __a = get_allocator(); | |
946 | _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size()); | |
947 | _M_rep()->_M_dispose(__a); | |
948 | _M_data(__tmp); | |
725dc051 | 949 | } |
ed6814f7 | 950 | |
725dc051 | 951 | template<typename _CharT, typename _Traits, typename _Alloc> |
cc6e67d4 PC |
952 | void |
953 | basic_string<_CharT, _Traits, _Alloc>:: | |
954 | swap(basic_string& __s) | |
d8d9b83b | 955 | _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value) |
725dc051 | 956 | { |
ed6814f7 | 957 | if (_M_rep()->_M_is_leaked()) |
725dc051 | 958 | _M_rep()->_M_set_sharable(); |
ed6814f7 | 959 | if (__s._M_rep()->_M_is_leaked()) |
725dc051 BK |
960 | __s._M_rep()->_M_set_sharable(); |
961 | if (this->get_allocator() == __s.get_allocator()) | |
962 | { | |
963 | _CharT* __tmp = _M_data(); | |
964 | _M_data(__s._M_data()); | |
965 | __s._M_data(__tmp); | |
966 | } | |
967 | // The code below can usually be optimized away. | |
ed6814f7 | 968 | else |
725dc051 | 969 | { |
0e50667c PC |
970 | const basic_string __tmp1(_M_ibegin(), _M_iend(), |
971 | __s.get_allocator()); | |
ed6814f7 | 972 | const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(), |
0e50667c | 973 | this->get_allocator()); |
725dc051 BK |
974 | *this = __tmp2; |
975 | __s = __tmp1; | |
976 | } | |
977 | } | |
978 | ||
725dc051 | 979 | template<typename _CharT, typename _Traits, typename _Alloc> |
31bfa177 | 980 | typename basic_string<_CharT, _Traits, _Alloc>::_Rep* |
725dc051 | 981 | basic_string<_CharT, _Traits, _Alloc>::_Rep:: |
234e0d31 PC |
982 | _S_create(size_type __capacity, size_type __old_capacity, |
983 | const _Alloc& __alloc) | |
725dc051 | 984 | { |
f5677b15 | 985 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
725dc051 | 986 | // 83. String::npos vs. string::max_size() |
e2c09482 | 987 | if (__capacity > _S_max_size) |
0e50667c | 988 | __throw_length_error(__N("basic_string::_S_create")); |
725dc051 | 989 | |
2883d58b LR |
990 | // The standard places no restriction on allocating more memory |
991 | // than is strictly needed within this layer at the moment or as | |
140cf935 | 992 | // requested by an explicit application call to reserve(n). |
234e0d31 PC |
993 | |
994 | // Many malloc implementations perform quite poorly when an | |
2883d58b LR |
995 | // application attempts to allocate memory in a stepwise fashion |
996 | // growing each allocation size by only 1 char. Additionally, | |
997 | // it makes little sense to allocate less linear memory than the | |
998 | // natural blocking size of the malloc implementation. | |
999 | // Unfortunately, we would need a somewhat low-level calculation | |
1000 | // with tuned parameters to get this perfect for any particular | |
1001 | // malloc implementation. Fortunately, generalizations about | |
1002 | // common features seen among implementations seems to suffice. | |
2883d58b LR |
1003 | |
1004 | // __pagesize need not match the actual VM page size for good | |
1005 | // results in practice, thus we pick a common value on the low | |
1006 | // side. __malloc_header_size is an estimate of the amount of | |
1007 | // overhead per memory allocation (in practice seen N * sizeof | |
1008 | // (void*) where N is 0, 2 or 4). According to folklore, | |
1009 | // picking this value on the high side is better than | |
1010 | // low-balling it (especially when this algorithm is used with | |
1011 | // malloc implementations that allocate memory blocks rounded up | |
1012 | // to a size which is a power of 2). | |
cbb0dcef PC |
1013 | const size_type __pagesize = 4096; |
1014 | const size_type __malloc_header_size = 4 * sizeof(void*); | |
234e0d31 PC |
1015 | |
1016 | // The below implements an exponential growth policy, necessary to | |
1017 | // meet amortized linear time requirements of the library: see | |
1018 | // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html. | |
1019 | // It's active for allocations requiring an amount of memory above | |
1020 | // system pagesize. This is consistent with the requirements of the | |
1021 | // standard: http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html | |
cbb0dcef | 1022 | if (__capacity > __old_capacity && __capacity < 2 * __old_capacity) |
234e0d31 PC |
1023 | __capacity = 2 * __old_capacity; |
1024 | ||
1025 | // NB: Need an array of char_type[__capacity], plus a terminating | |
6be8b524 | 1026 | // null char_type() element, plus enough for the _Rep data structure. |
234e0d31 | 1027 | // Whew. Seemingly so needy, yet so elemental. |
6be8b524 | 1028 | size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep); |
234e0d31 | 1029 | |
24f33069 | 1030 | const size_type __adj_size = __size + __malloc_header_size; |
d1a7222c | 1031 | if (__adj_size > __pagesize && __capacity > __old_capacity) |
2883d58b | 1032 | { |
24f33069 | 1033 | const size_type __extra = __pagesize - __adj_size % __pagesize; |
2883d58b | 1034 | __capacity += __extra / sizeof(_CharT); |
d1615643 PC |
1035 | // Never allocate a string bigger than _S_max_size. |
1036 | if (__capacity > _S_max_size) | |
1037 | __capacity = _S_max_size; | |
6be8b524 | 1038 | __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep); |
2883d58b | 1039 | } |
2883d58b | 1040 | |
725dc051 BK |
1041 | // NB: Might throw, but no worries about a leak, mate: _Rep() |
1042 | // does not throw. | |
6be8b524 | 1043 | void* __place = _Raw_bytes_alloc(__alloc).allocate(__size); |
725dc051 BK |
1044 | _Rep *__p = new (__place) _Rep; |
1045 | __p->_M_capacity = __capacity; | |
039f9e35 JJ |
1046 | // ABI compatibility - 3.4.x set in _S_create both |
1047 | // _M_refcount and _M_length. All callers of _S_create | |
1048 | // in basic_string.tcc then set just _M_length. | |
1049 | // In 4.0.x and later both _M_refcount and _M_length | |
1050 | // are initialized in the callers, unfortunately we can | |
1051 | // have 3.4.x compiled code with _S_create callers inlined | |
1052 | // calling 4.0.x+ _S_create. | |
1053 | __p->_M_set_sharable(); | |
725dc051 BK |
1054 | return __p; |
1055 | } | |
1056 | ||
1057 | template<typename _CharT, typename _Traits, typename _Alloc> | |
1058 | _CharT* | |
1059 | basic_string<_CharT, _Traits, _Alloc>::_Rep:: | |
1060 | _M_clone(const _Alloc& __alloc, size_type __res) | |
1061 | { | |
79f57f23 | 1062 | // Requested capacity of the clone. |
ca566e4c | 1063 | const size_type __requested_cap = this->_M_length + __res; |
234e0d31 PC |
1064 | _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity, |
1065 | __alloc); | |
ca566e4c | 1066 | if (this->_M_length) |
ec61e852 | 1067 | _M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length); |
954b12d2 | 1068 | |
cbf52bfa | 1069 | __r->_M_set_length_and_sharable(this->_M_length); |
725dc051 BK |
1070 | return __r->_M_refdata(); |
1071 | } | |
ed6814f7 | 1072 | |
725dc051 BK |
1073 | template<typename _CharT, typename _Traits, typename _Alloc> |
1074 | void | |
cc6e67d4 PC |
1075 | basic_string<_CharT, _Traits, _Alloc>:: |
1076 | resize(size_type __n, _CharT __c) | |
725dc051 | 1077 | { |
7778fa6e | 1078 | const size_type __size = this->size(); |
ec61e852 | 1079 | _M_check_length(__size, __n, "basic_string::resize"); |
725dc051 BK |
1080 | if (__size < __n) |
1081 | this->append(__n - __size, __c); | |
1082 | else if (__n < __size) | |
1083 | this->erase(__n); | |
1084 | // else nothing (in particular, avoid calling _M_mutate() unnecessarily.) | |
1085 | } | |
418bb880 | 1086 | |
725dc051 | 1087 | template<typename _CharT, typename _Traits, typename _Alloc> |
08addde6 | 1088 | template<typename _InputIterator> |
725dc051 BK |
1089 | basic_string<_CharT, _Traits, _Alloc>& |
1090 | basic_string<_CharT, _Traits, _Alloc>:: | |
ed6814f7 | 1091 | _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1, |
fefe561e | 1092 | _InputIterator __k2, __false_type) |
725dc051 | 1093 | { |
7778fa6e | 1094 | const basic_string __s(__k1, __k2); |
251804e6 | 1095 | const size_type __n1 = __i2 - __i1; |
ec61e852 | 1096 | _M_check_length(__n1, __s.size(), "basic_string::_M_replace_dispatch"); |
251804e6 | 1097 | return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(), |
7bb9b33b | 1098 | __s.size()); |
725dc051 | 1099 | } |
6bfad5e1 PC |
1100 | |
1101 | template<typename _CharT, typename _Traits, typename _Alloc> | |
1102 | basic_string<_CharT, _Traits, _Alloc>& | |
1103 | basic_string<_CharT, _Traits, _Alloc>:: | |
1104 | _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2, | |
1105 | _CharT __c) | |
1106 | { | |
1107 | _M_check_length(__n1, __n2, "basic_string::_M_replace_aux"); | |
1108 | _M_mutate(__pos1, __n1, __n2); | |
1109 | if (__n2) | |
1110 | _M_assign(_M_data() + __pos1, __n2, __c); | |
1111 | return *this; | |
1112 | } | |
1113 | ||
1114 | template<typename _CharT, typename _Traits, typename _Alloc> | |
1115 | basic_string<_CharT, _Traits, _Alloc>& | |
1116 | basic_string<_CharT, _Traits, _Alloc>:: | |
1117 | _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s, | |
1118 | size_type __n2) | |
1119 | { | |
1120 | _M_mutate(__pos1, __n1, __n2); | |
1121 | if (__n2) | |
1122 | _M_copy(_M_data() + __pos1, __s, __n2); | |
1123 | return *this; | |
1124 | } | |
34a2b755 | 1125 | |
140cf935 AL |
1126 | template<typename _CharT, typename _Traits, typename _Alloc> |
1127 | void | |
1128 | basic_string<_CharT, _Traits, _Alloc>:: | |
1129 | reserve() | |
1130 | { | |
8bd92d80 | 1131 | #if __cpp_exceptions |
140cf935 AL |
1132 | if (length() < capacity() || _M_rep()->_M_is_shared()) |
1133 | try | |
1134 | { | |
1135 | const allocator_type __a = get_allocator(); | |
1136 | _CharT* __tmp = _M_rep()->_M_clone(__a); | |
1137 | _M_rep()->_M_dispose(__a); | |
1138 | _M_data(__tmp); | |
1139 | } | |
1140 | catch (const __cxxabiv1::__forced_unwind&) | |
1141 | { throw; } | |
1142 | catch (...) | |
1143 | { /* swallow the exception */ } | |
8bd92d80 | 1144 | #endif |
140cf935 AL |
1145 | } |
1146 | ||
34a2b755 JW |
1147 | template<typename _CharT, typename _Traits, typename _Alloc> |
1148 | typename basic_string<_CharT, _Traits, _Alloc>::size_type | |
1149 | basic_string<_CharT, _Traits, _Alloc>:: | |
1150 | copy(_CharT* __s, size_type __n, size_type __pos) const | |
1151 | { | |
1152 | _M_check(__pos, "basic_string::copy"); | |
1153 | __n = _M_limit(__pos, __n); | |
1154 | __glibcxx_requires_string_len(__s, __n); | |
1155 | if (__n) | |
1156 | _M_copy(__s, _M_data() + __pos, __n); | |
1157 | // 21.3.5.7 par 3: do not append null. (good.) | |
1158 | return __n; | |
1159 | } | |
1160 | #endif // !_GLIBCXX_USE_CXX11_ABI | |
6bfad5e1 | 1161 | |
725dc051 | 1162 | template<typename _CharT, typename _Traits, typename _Alloc> |
9a304d17 | 1163 | basic_string<_CharT, _Traits, _Alloc> |
725dc051 | 1164 | operator+(const _CharT* __lhs, |
9a304d17 | 1165 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
725dc051 | 1166 | { |
285b36d6 | 1167 | __glibcxx_requires_string(__lhs); |
9a304d17 | 1168 | typedef basic_string<_CharT, _Traits, _Alloc> __string_type; |
725dc051 | 1169 | typedef typename __string_type::size_type __size_type; |
f4e678ef NDR |
1170 | typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template |
1171 | rebind<_CharT>::other _Char_alloc_type; | |
1172 | typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits; | |
7778fa6e | 1173 | const __size_type __len = _Traits::length(__lhs); |
f4e678ef NDR |
1174 | __string_type __str(_Alloc_traits::_S_select_on_copy( |
1175 | __rhs.get_allocator())); | |
725dc051 | 1176 | __str.reserve(__len + __rhs.size()); |
bb9909b0 | 1177 | __str.append(__lhs, __len); |
725dc051 BK |
1178 | __str.append(__rhs); |
1179 | return __str; | |
1180 | } | |
1181 | ||
1182 | template<typename _CharT, typename _Traits, typename _Alloc> | |
9a304d17 PC |
1183 | basic_string<_CharT, _Traits, _Alloc> |
1184 | operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs) | |
725dc051 | 1185 | { |
9a304d17 | 1186 | typedef basic_string<_CharT, _Traits, _Alloc> __string_type; |
725dc051 | 1187 | typedef typename __string_type::size_type __size_type; |
f4e678ef NDR |
1188 | typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template |
1189 | rebind<_CharT>::other _Char_alloc_type; | |
1190 | typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits; | |
1191 | __string_type __str(_Alloc_traits::_S_select_on_copy( | |
1192 | __rhs.get_allocator())); | |
7778fa6e | 1193 | const __size_type __len = __rhs.size(); |
725dc051 | 1194 | __str.reserve(__len + 1); |
15f13f01 | 1195 | __str.append(__size_type(1), __lhs); |
725dc051 BK |
1196 | __str.append(__rhs); |
1197 | return __str; | |
1198 | } | |
1199 | ||
725dc051 | 1200 | template<typename _CharT, typename _Traits, typename _Alloc> |
31bfa177 | 1201 | typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 | 1202 | basic_string<_CharT, _Traits, _Alloc>:: |
725dc051 | 1203 | find(const _CharT* __s, size_type __pos, size_type __n) const |
39a03251 | 1204 | _GLIBCXX_NOEXCEPT |
725dc051 | 1205 | { |
285b36d6 | 1206 | __glibcxx_requires_string_len(__s, __n); |
7778fa6e | 1207 | const size_type __size = this->size(); |
1a4ba99f PC |
1208 | |
1209 | if (__n == 0) | |
1210 | return __pos <= __size ? __pos : npos; | |
cb627cdf JW |
1211 | if (__pos >= __size) |
1212 | return npos; | |
1a4ba99f | 1213 | |
cb627cdf JW |
1214 | const _CharT __elem0 = __s[0]; |
1215 | const _CharT* const __data = data(); | |
1216 | const _CharT* __first = __data + __pos; | |
1217 | const _CharT* const __last = __data + __size; | |
1218 | size_type __len = __size - __pos; | |
1219 | ||
1220 | while (__len >= __n) | |
5527be59 | 1221 | { |
cb627cdf JW |
1222 | // Find the first occurrence of __elem0: |
1223 | __first = traits_type::find(__first, __len - __n + 1, __elem0); | |
1224 | if (!__first) | |
1225 | return npos; | |
1226 | // Compare the full strings from the first occurrence of __elem0. | |
1227 | // We already know that __first[0] == __s[0] but compare them again | |
1228 | // anyway because __s is probably aligned, which helps memcmp. | |
1229 | if (traits_type::compare(__first, __s, __n) == 0) | |
1230 | return __first - __data; | |
1231 | __len = __last - ++__first; | |
5527be59 | 1232 | } |
1a4ba99f | 1233 | return npos; |
725dc051 BK |
1234 | } |
1235 | ||
1236 | template<typename _CharT, typename _Traits, typename _Alloc> | |
31bfa177 | 1237 | typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 | 1238 | basic_string<_CharT, _Traits, _Alloc>:: |
cea8c6de | 1239 | find(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT |
725dc051 | 1240 | { |
725dc051 | 1241 | size_type __ret = npos; |
4a787fa8 | 1242 | const size_type __size = this->size(); |
725dc051 BK |
1243 | if (__pos < __size) |
1244 | { | |
1245 | const _CharT* __data = _M_data(); | |
7778fa6e | 1246 | const size_type __n = __size - __pos; |
97644827 BK |
1247 | const _CharT* __p = traits_type::find(__data + __pos, __n, __c); |
1248 | if (__p) | |
725dc051 BK |
1249 | __ret = __p - __data; |
1250 | } | |
1251 | return __ret; | |
1252 | } | |
1253 | ||
725dc051 | 1254 | template<typename _CharT, typename _Traits, typename _Alloc> |
31bfa177 | 1255 | typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 BK |
1256 | basic_string<_CharT, _Traits, _Alloc>:: |
1257 | rfind(const _CharT* __s, size_type __pos, size_type __n) const | |
39a03251 | 1258 | _GLIBCXX_NOEXCEPT |
725dc051 | 1259 | { |
285b36d6 | 1260 | __glibcxx_requires_string_len(__s, __n); |
7778fa6e | 1261 | const size_type __size = this->size(); |
725dc051 BK |
1262 | if (__n <= __size) |
1263 | { | |
dd6eaaed | 1264 | __pos = std::min(size_type(__size - __n), __pos); |
725dc051 | 1265 | const _CharT* __data = _M_data(); |
ed6814f7 | 1266 | do |
725dc051 BK |
1267 | { |
1268 | if (traits_type::compare(__data + __pos, __s, __n) == 0) | |
1269 | return __pos; | |
ed6814f7 | 1270 | } |
725dc051 BK |
1271 | while (__pos-- > 0); |
1272 | } | |
1273 | return npos; | |
1274 | } | |
ed6814f7 | 1275 | |
725dc051 | 1276 | template<typename _CharT, typename _Traits, typename _Alloc> |
31bfa177 | 1277 | typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 | 1278 | basic_string<_CharT, _Traits, _Alloc>:: |
cea8c6de | 1279 | rfind(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT |
725dc051 | 1280 | { |
04cc8aef | 1281 | size_type __size = this->size(); |
725dc051 BK |
1282 | if (__size) |
1283 | { | |
04cc8aef PC |
1284 | if (--__size > __pos) |
1285 | __size = __pos; | |
1286 | for (++__size; __size-- > 0; ) | |
1287 | if (traits_type::eq(_M_data()[__size], __c)) | |
1288 | return __size; | |
725dc051 BK |
1289 | } |
1290 | return npos; | |
1291 | } | |
ed6814f7 | 1292 | |
725dc051 | 1293 | template<typename _CharT, typename _Traits, typename _Alloc> |
31bfa177 | 1294 | typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 BK |
1295 | basic_string<_CharT, _Traits, _Alloc>:: |
1296 | find_first_of(const _CharT* __s, size_type __pos, size_type __n) const | |
39a03251 | 1297 | _GLIBCXX_NOEXCEPT |
725dc051 | 1298 | { |
285b36d6 | 1299 | __glibcxx_requires_string_len(__s, __n); |
725dc051 BK |
1300 | for (; __n && __pos < this->size(); ++__pos) |
1301 | { | |
97644827 BK |
1302 | const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]); |
1303 | if (__p) | |
725dc051 BK |
1304 | return __pos; |
1305 | } | |
1306 | return npos; | |
1307 | } | |
ed6814f7 | 1308 | |
725dc051 | 1309 | template<typename _CharT, typename _Traits, typename _Alloc> |
31bfa177 | 1310 | typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 BK |
1311 | basic_string<_CharT, _Traits, _Alloc>:: |
1312 | find_last_of(const _CharT* __s, size_type __pos, size_type __n) const | |
39a03251 | 1313 | _GLIBCXX_NOEXCEPT |
725dc051 | 1314 | { |
285b36d6 | 1315 | __glibcxx_requires_string_len(__s, __n); |
725dc051 BK |
1316 | size_type __size = this->size(); |
1317 | if (__size && __n) | |
ed6814f7 BI |
1318 | { |
1319 | if (--__size > __pos) | |
725dc051 BK |
1320 | __size = __pos; |
1321 | do | |
1322 | { | |
97644827 | 1323 | if (traits_type::find(__s, __n, _M_data()[__size])) |
725dc051 | 1324 | return __size; |
ed6814f7 | 1325 | } |
725dc051 BK |
1326 | while (__size-- != 0); |
1327 | } | |
1328 | return npos; | |
1329 | } | |
ed6814f7 | 1330 | |
725dc051 | 1331 | template<typename _CharT, typename _Traits, typename _Alloc> |
31bfa177 | 1332 | typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 BK |
1333 | basic_string<_CharT, _Traits, _Alloc>:: |
1334 | find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const | |
39a03251 | 1335 | _GLIBCXX_NOEXCEPT |
725dc051 | 1336 | { |
285b36d6 | 1337 | __glibcxx_requires_string_len(__s, __n); |
fefe561e PC |
1338 | for (; __pos < this->size(); ++__pos) |
1339 | if (!traits_type::find(__s, __n, _M_data()[__pos])) | |
1340 | return __pos; | |
725dc051 BK |
1341 | return npos; |
1342 | } | |
1343 | ||
1344 | template<typename _CharT, typename _Traits, typename _Alloc> | |
31bfa177 | 1345 | typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 | 1346 | basic_string<_CharT, _Traits, _Alloc>:: |
cea8c6de | 1347 | find_first_not_of(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT |
725dc051 | 1348 | { |
fefe561e PC |
1349 | for (; __pos < this->size(); ++__pos) |
1350 | if (!traits_type::eq(_M_data()[__pos], __c)) | |
1351 | return __pos; | |
725dc051 BK |
1352 | return npos; |
1353 | } | |
1354 | ||
1355 | template<typename _CharT, typename _Traits, typename _Alloc> | |
31bfa177 | 1356 | typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 BK |
1357 | basic_string<_CharT, _Traits, _Alloc>:: |
1358 | find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const | |
39a03251 | 1359 | _GLIBCXX_NOEXCEPT |
725dc051 | 1360 | { |
285b36d6 | 1361 | __glibcxx_requires_string_len(__s, __n); |
04cc8aef | 1362 | size_type __size = this->size(); |
ba317c52 | 1363 | if (__size) |
04cc8aef PC |
1364 | { |
1365 | if (--__size > __pos) | |
1366 | __size = __pos; | |
ed6814f7 | 1367 | do |
725dc051 | 1368 | { |
04cc8aef PC |
1369 | if (!traits_type::find(__s, __n, _M_data()[__size])) |
1370 | return __size; | |
ed6814f7 | 1371 | } |
04cc8aef | 1372 | while (__size--); |
725dc051 BK |
1373 | } |
1374 | return npos; | |
1375 | } | |
1376 | ||
1377 | template<typename _CharT, typename _Traits, typename _Alloc> | |
31bfa177 | 1378 | typename basic_string<_CharT, _Traits, _Alloc>::size_type |
725dc051 | 1379 | basic_string<_CharT, _Traits, _Alloc>:: |
cea8c6de | 1380 | find_last_not_of(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT |
725dc051 | 1381 | { |
04cc8aef | 1382 | size_type __size = this->size(); |
725dc051 | 1383 | if (__size) |
fefe561e | 1384 | { |
04cc8aef | 1385 | if (--__size > __pos) |
ed6814f7 | 1386 | __size = __pos; |
725dc051 BK |
1387 | do |
1388 | { | |
04cc8aef PC |
1389 | if (!traits_type::eq(_M_data()[__size], __c)) |
1390 | return __size; | |
ed6814f7 | 1391 | } |
04cc8aef | 1392 | while (__size--); |
725dc051 BK |
1393 | } |
1394 | return npos; | |
1395 | } | |
ed6814f7 | 1396 | |
725dc051 BK |
1397 | template<typename _CharT, typename _Traits, typename _Alloc> |
1398 | int | |
1399 | basic_string<_CharT, _Traits, _Alloc>:: | |
1400 | compare(size_type __pos, size_type __n, const basic_string& __str) const | |
1401 | { | |
04cc8aef | 1402 | _M_check(__pos, "basic_string::compare"); |
e03a6fb7 | 1403 | __n = _M_limit(__pos, __n); |
7778fa6e | 1404 | const size_type __osize = __str.size(); |
e03a6fb7 | 1405 | const size_type __len = std::min(__n, __osize); |
725dc051 BK |
1406 | int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len); |
1407 | if (!__r) | |
9521dd6b | 1408 | __r = _S_compare(__n, __osize); |
725dc051 BK |
1409 | return __r; |
1410 | } | |
1411 | ||
1412 | template<typename _CharT, typename _Traits, typename _Alloc> | |
1413 | int | |
1414 | basic_string<_CharT, _Traits, _Alloc>:: | |
1415 | compare(size_type __pos1, size_type __n1, const basic_string& __str, | |
1416 | size_type __pos2, size_type __n2) const | |
1417 | { | |
04cc8aef PC |
1418 | _M_check(__pos1, "basic_string::compare"); |
1419 | __str._M_check(__pos2, "basic_string::compare"); | |
e03a6fb7 PC |
1420 | __n1 = _M_limit(__pos1, __n1); |
1421 | __n2 = __str._M_limit(__pos2, __n2); | |
1422 | const size_type __len = std::min(__n1, __n2); | |
ed6814f7 | 1423 | int __r = traits_type::compare(_M_data() + __pos1, |
725dc051 BK |
1424 | __str.data() + __pos2, __len); |
1425 | if (!__r) | |
9521dd6b | 1426 | __r = _S_compare(__n1, __n2); |
725dc051 BK |
1427 | return __r; |
1428 | } | |
1429 | ||
725dc051 BK |
1430 | template<typename _CharT, typename _Traits, typename _Alloc> |
1431 | int | |
1432 | basic_string<_CharT, _Traits, _Alloc>:: | |
39a03251 | 1433 | compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT |
725dc051 | 1434 | { |
285b36d6 | 1435 | __glibcxx_requires_string(__s); |
7778fa6e PC |
1436 | const size_type __size = this->size(); |
1437 | const size_type __osize = traits_type::length(__s); | |
1438 | const size_type __len = std::min(__size, __osize); | |
c86c54e6 | 1439 | int __r = traits_type::compare(_M_data(), __s, __len); |
725dc051 | 1440 | if (!__r) |
9521dd6b | 1441 | __r = _S_compare(__size, __osize); |
725dc051 BK |
1442 | return __r; |
1443 | } | |
1444 | ||
3b0fd4bc BK |
1445 | template<typename _CharT, typename _Traits, typename _Alloc> |
1446 | int | |
9a304d17 | 1447 | basic_string <_CharT, _Traits, _Alloc>:: |
3b0fd4bc BK |
1448 | compare(size_type __pos, size_type __n1, const _CharT* __s) const |
1449 | { | |
285b36d6 | 1450 | __glibcxx_requires_string(__s); |
04cc8aef | 1451 | _M_check(__pos, "basic_string::compare"); |
e03a6fb7 | 1452 | __n1 = _M_limit(__pos, __n1); |
7778fa6e | 1453 | const size_type __osize = traits_type::length(__s); |
e03a6fb7 | 1454 | const size_type __len = std::min(__n1, __osize); |
3b0fd4bc BK |
1455 | int __r = traits_type::compare(_M_data() + __pos, __s, __len); |
1456 | if (!__r) | |
9521dd6b | 1457 | __r = _S_compare(__n1, __osize); |
3b0fd4bc BK |
1458 | return __r; |
1459 | } | |
1460 | ||
725dc051 BK |
1461 | template<typename _CharT, typename _Traits, typename _Alloc> |
1462 | int | |
9a304d17 | 1463 | basic_string <_CharT, _Traits, _Alloc>:: |
ed6814f7 | 1464 | compare(size_type __pos, size_type __n1, const _CharT* __s, |
725dc051 BK |
1465 | size_type __n2) const |
1466 | { | |
285b36d6 | 1467 | __glibcxx_requires_string_len(__s, __n2); |
04cc8aef | 1468 | _M_check(__pos, "basic_string::compare"); |
e03a6fb7 PC |
1469 | __n1 = _M_limit(__pos, __n1); |
1470 | const size_type __len = std::min(__n1, __n2); | |
725dc051 BK |
1471 | int __r = traits_type::compare(_M_data() + __pos, __s, __len); |
1472 | if (!__r) | |
9521dd6b | 1473 | __r = _S_compare(__n1, __n2); |
725dc051 BK |
1474 | return __r; |
1475 | } | |
1476 | ||
11202768 PC |
1477 | // 21.3.7.9 basic_string::getline and operators |
1478 | template<typename _CharT, typename _Traits, typename _Alloc> | |
1479 | basic_istream<_CharT, _Traits>& | |
1480 | operator>>(basic_istream<_CharT, _Traits>& __in, | |
1481 | basic_string<_CharT, _Traits, _Alloc>& __str) | |
1482 | { | |
1483 | typedef basic_istream<_CharT, _Traits> __istream_type; | |
1484 | typedef basic_string<_CharT, _Traits, _Alloc> __string_type; | |
1485 | typedef typename __istream_type::ios_base __ios_base; | |
1486 | typedef typename __istream_type::int_type __int_type; | |
1487 | typedef typename __string_type::size_type __size_type; | |
1488 | typedef ctype<_CharT> __ctype_type; | |
1489 | typedef typename __ctype_type::ctype_base __ctype_base; | |
1490 | ||
1491 | __size_type __extracted = 0; | |
1492 | typename __ios_base::iostate __err = __ios_base::goodbit; | |
1493 | typename __istream_type::sentry __cerb(__in, false); | |
880b527f | 1494 | if (__cerb) |
11202768 | 1495 | { |
bc2631e0 | 1496 | __try |
11202768 PC |
1497 | { |
1498 | // Avoid reallocation for common case. | |
1499 | __str.erase(); | |
1500 | _CharT __buf[128]; | |
1501 | __size_type __len = 0; | |
1502 | const streamsize __w = __in.width(); | |
1503 | const __size_type __n = __w > 0 ? static_cast<__size_type>(__w) | |
1504 | : __str.max_size(); | |
1505 | const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc()); | |
1506 | const __int_type __eof = _Traits::eof(); | |
1507 | __int_type __c = __in.rdbuf()->sgetc(); | |
1508 | ||
1509 | while (__extracted < __n | |
1510 | && !_Traits::eq_int_type(__c, __eof) | |
1511 | && !__ct.is(__ctype_base::space, | |
1512 | _Traits::to_char_type(__c))) | |
1513 | { | |
1514 | if (__len == sizeof(__buf) / sizeof(_CharT)) | |
1515 | { | |
1516 | __str.append(__buf, sizeof(__buf) / sizeof(_CharT)); | |
1517 | __len = 0; | |
1518 | } | |
1519 | __buf[__len++] = _Traits::to_char_type(__c); | |
1520 | ++__extracted; | |
1521 | __c = __in.rdbuf()->snextc(); | |
1522 | } | |
1523 | __str.append(__buf, __len); | |
1524 | ||
4e39f563 | 1525 | if (__extracted < __n && _Traits::eq_int_type(__c, __eof)) |
11202768 PC |
1526 | __err |= __ios_base::eofbit; |
1527 | __in.width(0); | |
1528 | } | |
bc2631e0 | 1529 | __catch(__cxxabiv1::__forced_unwind&) |
d05f74f1 JM |
1530 | { |
1531 | __in._M_setstate(__ios_base::badbit); | |
1532 | __throw_exception_again; | |
1533 | } | |
bc2631e0 | 1534 | __catch(...) |
11202768 PC |
1535 | { |
1536 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
1537 | // 91. Description of operator>> and getline() for string<> | |
1538 | // might cause endless loop | |
1539 | __in._M_setstate(__ios_base::badbit); | |
1540 | } | |
1541 | } | |
1542 | // 211. operator>>(istream&, string&) doesn't set failbit | |
1543 | if (!__extracted) | |
1544 | __err |= __ios_base::failbit; | |
1545 | if (__err) | |
1546 | __in.setstate(__err); | |
1547 | return __in; | |
1548 | } | |
1549 | ||
1550 | template<typename _CharT, typename _Traits, typename _Alloc> | |
1551 | basic_istream<_CharT, _Traits>& | |
1552 | getline(basic_istream<_CharT, _Traits>& __in, | |
1553 | basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim) | |
1554 | { | |
1555 | typedef basic_istream<_CharT, _Traits> __istream_type; | |
1556 | typedef basic_string<_CharT, _Traits, _Alloc> __string_type; | |
1557 | typedef typename __istream_type::ios_base __ios_base; | |
1558 | typedef typename __istream_type::int_type __int_type; | |
1559 | typedef typename __string_type::size_type __size_type; | |
1560 | ||
1561 | __size_type __extracted = 0; | |
1562 | const __size_type __n = __str.max_size(); | |
1563 | typename __ios_base::iostate __err = __ios_base::goodbit; | |
1564 | typename __istream_type::sentry __cerb(__in, true); | |
880b527f | 1565 | if (__cerb) |
11202768 | 1566 | { |
bc2631e0 | 1567 | __try |
11202768 PC |
1568 | { |
1569 | __str.erase(); | |
1570 | const __int_type __idelim = _Traits::to_int_type(__delim); | |
1571 | const __int_type __eof = _Traits::eof(); | |
1572 | __int_type __c = __in.rdbuf()->sgetc(); | |
1573 | ||
1574 | while (__extracted < __n | |
1575 | && !_Traits::eq_int_type(__c, __eof) | |
1576 | && !_Traits::eq_int_type(__c, __idelim)) | |
1577 | { | |
1578 | __str += _Traits::to_char_type(__c); | |
1579 | ++__extracted; | |
1580 | __c = __in.rdbuf()->snextc(); | |
1581 | } | |
1582 | ||
1583 | if (_Traits::eq_int_type(__c, __eof)) | |
1584 | __err |= __ios_base::eofbit; | |
1585 | else if (_Traits::eq_int_type(__c, __idelim)) | |
1586 | { | |
1587 | ++__extracted; | |
1588 | __in.rdbuf()->sbumpc(); | |
1589 | } | |
1590 | else | |
1591 | __err |= __ios_base::failbit; | |
1592 | } | |
bc2631e0 | 1593 | __catch(__cxxabiv1::__forced_unwind&) |
d05f74f1 JM |
1594 | { |
1595 | __in._M_setstate(__ios_base::badbit); | |
1596 | __throw_exception_again; | |
1597 | } | |
bc2631e0 | 1598 | __catch(...) |
11202768 PC |
1599 | { |
1600 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
1601 | // 91. Description of operator>> and getline() for string<> | |
1602 | // might cause endless loop | |
1603 | __in._M_setstate(__ios_base::badbit); | |
1604 | } | |
1605 | } | |
1606 | if (!__extracted) | |
1607 | __err |= __ios_base::failbit; | |
1608 | if (__err) | |
1609 | __in.setstate(__err); | |
1610 | return __in; | |
1611 | } | |
1612 | ||
a32e3c09 | 1613 | // Inhibit implicit instantiations for required instantiations, |
ed6814f7 | 1614 | // which are defined via explicit instantiations elsewhere. |
db9e7b2a | 1615 | #if _GLIBCXX_EXTERN_TEMPLATE |
8f159176 JW |
1616 | // The explicit instantiation definitions in src/c++11/string-inst.cc and |
1617 | // src/c++17/string-inst.cc only instantiate the members required for C++17 | |
1618 | // and earlier standards (so not C++20's starts_with and ends_with). | |
1619 | // Suppress the explicit instantiation declarations for C++20, so C++20 | |
1620 | // code will implicitly instantiate std::string and std::wstring as needed. | |
1a289fa3 | 1621 | # if __cplusplus <= 201703L && _GLIBCXX_EXTERN_TEMPLATE > 0 |
a32e3c09 | 1622 | extern template class basic_string<char>; |
d72888d3 JW |
1623 | # elif ! _GLIBCXX_USE_CXX11_ABI |
1624 | // Still need to prevent implicit instantiation of the COW empty rep, | |
1625 | // to ensure the definition in libstdc++.so is unique (PR 86138). | |
1626 | extern template basic_string<char>::size_type | |
1627 | basic_string<char>::_Rep::_S_empty_rep_storage[]; | |
1628 | # endif | |
1629 | ||
ed6814f7 BI |
1630 | extern template |
1631 | basic_istream<char>& | |
a32e3c09 | 1632 | operator>>(basic_istream<char>&, string&); |
ed6814f7 BI |
1633 | extern template |
1634 | basic_ostream<char>& | |
a32e3c09 | 1635 | operator<<(basic_ostream<char>&, const string&); |
ed6814f7 BI |
1636 | extern template |
1637 | basic_istream<char>& | |
a32e3c09 | 1638 | getline(basic_istream<char>&, string&, char); |
ed6814f7 BI |
1639 | extern template |
1640 | basic_istream<char>& | |
a32e3c09 BK |
1641 | getline(basic_istream<char>&, string&); |
1642 | ||
3d7c150e | 1643 | #ifdef _GLIBCXX_USE_WCHAR_T |
1a289fa3 | 1644 | # if __cplusplus <= 201703L && _GLIBCXX_EXTERN_TEMPLATE > 0 |
a32e3c09 | 1645 | extern template class basic_string<wchar_t>; |
d72888d3 JW |
1646 | # elif ! _GLIBCXX_USE_CXX11_ABI |
1647 | extern template basic_string<wchar_t>::size_type | |
1648 | basic_string<wchar_t>::_Rep::_S_empty_rep_storage[]; | |
1649 | # endif | |
1650 | ||
ed6814f7 BI |
1651 | extern template |
1652 | basic_istream<wchar_t>& | |
a32e3c09 | 1653 | operator>>(basic_istream<wchar_t>&, wstring&); |
ed6814f7 BI |
1654 | extern template |
1655 | basic_ostream<wchar_t>& | |
a32e3c09 | 1656 | operator<<(basic_ostream<wchar_t>&, const wstring&); |
ed6814f7 BI |
1657 | extern template |
1658 | basic_istream<wchar_t>& | |
a32e3c09 | 1659 | getline(basic_istream<wchar_t>&, wstring&, wchar_t); |
ed6814f7 BI |
1660 | extern template |
1661 | basic_istream<wchar_t>& | |
a32e3c09 | 1662 | getline(basic_istream<wchar_t>&, wstring&); |
d72888d3 | 1663 | #endif // _GLIBCXX_USE_WCHAR_T |
db9e7b2a | 1664 | #endif // _GLIBCXX_EXTERN_TEMPLATE |
3cbc7af0 | 1665 | |
12ffa228 | 1666 | _GLIBCXX_END_NAMESPACE_VERSION |
ed4f96af | 1667 | } // namespace std |
725dc051 | 1668 | |
3b0fd4bc | 1669 | #endif |