]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/basic_string.tcc
tree-vect-data-refs.c (vect_no_alias_p): New function.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / basic_string.tcc
CommitLineData
725dc051
BK
1// Components for manipulating sequences of characters -*- C++ -*-
2
818ab71a 3// Copyright (C) 1997-2016 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
46namespace 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 {
283 // Make sure we don't shrink below the current size.
284 if (__res < length())
285 __res = length();
286
287 const size_type __capacity = capacity();
288 if (__res != __capacity)
289 {
290 if (__res > __capacity
291 || __res > size_type(_S_local_capacity))
292 {
293 pointer __tmp = _M_create(__res, __capacity);
294 this->_S_copy(__tmp, _M_data(), length() + 1);
295 _M_dispose();
296 _M_data(__tmp);
297 _M_capacity(__res);
298 }
299 else if (!_M_is_local())
300 {
301 this->_S_copy(_M_local_data(), _M_data(), length() + 1);
302 _M_destroy(__capacity);
303 _M_data(_M_local_data());
304 }
305 }
306 }
307
308 template<typename _CharT, typename _Traits, typename _Alloc>
309 void
310 basic_string<_CharT, _Traits, _Alloc>::
311 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
312 size_type __len2)
313 {
314 const size_type __how_much = length() - __pos - __len1;
315
316 size_type __new_capacity = length() + __len2 - __len1;
317 pointer __r = _M_create(__new_capacity, capacity());
ceea48fc 318 _CharT* __p = std::__addressof(*__r);
34a2b755
JW
319
320 if (__pos)
ceea48fc 321 this->_S_copy(__p, _M_c_str(), __pos);
34a2b755 322 if (__s && __len2)
ceea48fc 323 this->_S_copy(__p + __pos, __s, __len2);
34a2b755 324 if (__how_much)
ceea48fc
JW
325 this->_S_copy(__p + __pos + __len2,
326 _M_c_str() + __pos + __len1, __how_much);
34a2b755
JW
327
328 _M_dispose();
329 _M_data(__r);
330 _M_capacity(__new_capacity);
331 }
332
333 template<typename _CharT, typename _Traits, typename _Alloc>
334 void
335 basic_string<_CharT, _Traits, _Alloc>::
336 _M_erase(size_type __pos, size_type __n)
337 {
338 const size_type __how_much = length() - __pos - __n;
339
ceea48fc 340 _CharT* __p = _M_c_str();
34a2b755 341 if (__how_much && __n)
ceea48fc 342 this->_S_move(__p + __pos, __p + __pos + __n, __how_much);
34a2b755
JW
343
344 _M_set_length(length() - __n);
345 }
346
347 template<typename _CharT, typename _Traits, typename _Alloc>
348 void
349 basic_string<_CharT, _Traits, _Alloc>::
350 resize(size_type __n, _CharT __c)
351 {
352 const size_type __size = this->size();
353 if (__size < __n)
354 this->append(__n - __size, __c);
355 else if (__n < __size)
356 this->_M_erase(__n, __size - __n);
357 }
358
359 template<typename _CharT, typename _Traits, typename _Alloc>
360 basic_string<_CharT, _Traits, _Alloc>&
361 basic_string<_CharT, _Traits, _Alloc>::
362 _M_append(const _CharT* __s, size_type __n)
363 {
364 const size_type __len = __n + this->size();
365
366 if (__len <= this->capacity())
367 {
368 if (__n)
369 this->_S_copy(this->_M_data() + this->size(), __s, __n);
370 }
371 else
372 this->_M_mutate(this->size(), size_type(0), __s, __n);
373
374 this->_M_set_length(__len);
375 return *this;
376 }
377
378 template<typename _CharT, typename _Traits, typename _Alloc>
379 template<typename _InputIterator>
380 basic_string<_CharT, _Traits, _Alloc>&
381 basic_string<_CharT, _Traits, _Alloc>::
382 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
383 _InputIterator __k1, _InputIterator __k2,
384 std::__false_type)
385 {
386 const basic_string __s(__k1, __k2);
387 const size_type __n1 = __i2 - __i1;
388 return _M_replace(__i1 - begin(), __n1, __s._M_data(),
389 __s.size());
390 }
391
392 template<typename _CharT, typename _Traits, typename _Alloc>
393 basic_string<_CharT, _Traits, _Alloc>&
394 basic_string<_CharT, _Traits, _Alloc>::
395 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
396 _CharT __c)
397 {
398 _M_check_length(__n1, __n2, "basic_string::_M_replace_aux");
399
400 const size_type __old_size = this->size();
401 const size_type __new_size = __old_size + __n2 - __n1;
402
403 if (__new_size <= this->capacity())
404 {
ceea48fc 405 _CharT* __p = this->_M_c_str() + __pos1;
34a2b755
JW
406
407 const size_type __how_much = __old_size - __pos1 - __n1;
408 if (__how_much && __n1 != __n2)
409 this->_S_move(__p + __n2, __p + __n1, __how_much);
410 }
411 else
412 this->_M_mutate(__pos1, __n1, 0, __n2);
413
414 if (__n2)
ceea48fc 415 this->_S_assign(this->_M_c_str() + __pos1, __n2, __c);
34a2b755
JW
416
417 this->_M_set_length(__new_size);
418 return *this;
419 }
420
421 template<typename _CharT, typename _Traits, typename _Alloc>
422 basic_string<_CharT, _Traits, _Alloc>&
423 basic_string<_CharT, _Traits, _Alloc>::
424 _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
425 const size_type __len2)
426 {
427 _M_check_length(__len1, __len2, "basic_string::_M_replace");
428
429 const size_type __old_size = this->size();
430 const size_type __new_size = __old_size + __len2 - __len1;
431
432 if (__new_size <= this->capacity())
433 {
5caff414 434 pointer __p = this->_M_data() + __pos;
34a2b755
JW
435
436 const size_type __how_much = __old_size - __pos - __len1;
437 if (_M_disjunct(__s))
438 {
439 if (__how_much && __len1 != __len2)
440 this->_S_move(__p + __len2, __p + __len1, __how_much);
441 if (__len2)
442 this->_S_copy(__p, __s, __len2);
443 }
444 else
445 {
446 // Work in-place.
447 if (__len2 && __len2 <= __len1)
448 this->_S_move(__p, __s, __len2);
449 if (__how_much && __len1 != __len2)
450 this->_S_move(__p + __len2, __p + __len1, __how_much);
451 if (__len2 > __len1)
452 {
453 if (__s + __len2 <= __p + __len1)
454 this->_S_move(__p, __s, __len2);
455 else if (__s >= __p + __len1)
456 this->_S_copy(__p, __s + __len2 - __len1, __len2);
457 else
458 {
459 const size_type __nleft = (__p + __len1) - __s;
460 this->_S_move(__p, __s, __nleft);
461 this->_S_copy(__p + __nleft, __p + __len2,
462 __len2 - __nleft);
463 }
464 }
465 }
466 }
467 else
468 this->_M_mutate(__pos, __len1, __s, __len2);
469
470 this->_M_set_length(__new_size);
471 return *this;
472 }
473
474 template<typename _CharT, typename _Traits, typename _Alloc>
475 typename basic_string<_CharT, _Traits, _Alloc>::size_type
476 basic_string<_CharT, _Traits, _Alloc>::
477 copy(_CharT* __s, size_type __n, size_type __pos) const
478 {
479 _M_check(__pos, "basic_string::copy");
480 __n = _M_limit(__pos, __n);
481 __glibcxx_requires_string_len(__s, __n);
482 if (__n)
483 _S_copy(__s, _M_data() + __pos, __n);
484 // 21.3.5.7 par 3: do not append null. (good.)
485 return __n;
486 }
487
488#else // !_GLIBCXX_USE_CXX11_ABI
489
725dc051 490 template<typename _CharT, typename _Traits, typename _Alloc>
ed6814f7 491 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
725dc051 492 basic_string<_CharT, _Traits, _Alloc>::
ca566e4c 493 _Rep::_S_max_size = (((npos - sizeof(_Rep_base))/sizeof(_CharT)) - 1) / 4;
725dc051
BK
494
495 template<typename _CharT, typename _Traits, typename _Alloc>
ed6814f7 496 const _CharT
725dc051 497 basic_string<_CharT, _Traits, _Alloc>::
00532602 498 _Rep::_S_terminal = _CharT();
725dc051
BK
499
500 template<typename _CharT, typename _Traits, typename _Alloc>
4f12dd3c 501 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
725dc051
BK
502 basic_string<_CharT, _Traits, _Alloc>::npos;
503
504 // Linker sets _S_empty_rep_storage to all 0s (one reference, empty string)
505 // at static init time (before static ctors are run).
506 template<typename _CharT, typename _Traits, typename _Alloc>
4f12dd3c 507 typename basic_string<_CharT, _Traits, _Alloc>::size_type
ca566e4c
NM
508 basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_empty_rep_storage[
509 (sizeof(_Rep_base) + sizeof(_CharT) + sizeof(size_type) - 1) /
510 sizeof(size_type)];
725dc051
BK
511
512 // NB: This is the special case for Input Iterators, used in
513 // istreambuf_iterators, etc.
514 // Input Iterators have a cost structure very different from
515 // pointers, calling for a different coding style.
516 template<typename _CharT, typename _Traits, typename _Alloc>
08addde6 517 template<typename _InIterator>
725dc051
BK
518 _CharT*
519 basic_string<_CharT, _Traits, _Alloc>::
08addde6 520 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
725dc051
BK
521 input_iterator_tag)
522 {
d7a3ef97 523#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
725dc051 524 if (__beg == __end && __a == _Alloc())
ca566e4c 525 return _S_empty_rep()._M_refdata();
1165dc50 526#endif
725dc051 527 // Avoid reallocation for common case.
247791f5 528 _CharT __buf[128];
690495b0
PC
529 size_type __len = 0;
530 while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT))
ed6814f7
BI
531 {
532 __buf[__len++] = *__beg;
690495b0 533 ++__beg;
725dc051 534 }
690495b0 535 _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
ec61e852 536 _M_copy(__r->_M_refdata(), __buf, __len);
bc2631e0 537 __try
e2c09482 538 {
690495b0 539 while (__beg != __end)
e2c09482 540 {
690495b0 541 if (__len == __r->_M_capacity)
e2c09482 542 {
690495b0
PC
543 // Allocate more space.
544 _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
ec61e852 545 _M_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
690495b0
PC
546 __r->_M_destroy(__a);
547 __r = __another;
e2c09482 548 }
ed6814f7 549 __r->_M_refdata()[__len++] = *__beg;
690495b0 550 ++__beg;
e2c09482
BK
551 }
552 }
bc2631e0 553 __catch(...)
e2c09482 554 {
ed6814f7 555 __r->_M_destroy(__a);
e2c09482
BK
556 __throw_exception_again;
557 }
cbf52bfa 558 __r->_M_set_length_and_sharable(__len);
690495b0 559 return __r->_M_refdata();
725dc051 560 }
ed6814f7 561
725dc051 562 template<typename _CharT, typename _Traits, typename _Alloc>
927dc7c6 563 template <typename _InIterator>
725dc051 564 _CharT*
9a304d17 565 basic_string<_CharT, _Traits, _Alloc>::
927dc7c6 566 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
725dc051
BK
567 forward_iterator_tag)
568 {
d7a3ef97 569#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
085825b8 570 if (__beg == __end && __a == _Alloc())
ca566e4c 571 return _S_empty_rep()._M_refdata();
1165dc50 572#endif
ed6814f7 573 // NB: Not required, but considered best practice.
e762c6f4 574 if (__gnu_cxx::__is_null_pointer(__beg) && __beg != __end)
8fc81078 575 __throw_logic_error(__N("basic_string::_S_construct null not valid"));
aa863dca 576
0e50667c
PC
577 const size_type __dnew = static_cast<size_type>(std::distance(__beg,
578 __end));
725dc051 579 // Check for out_of_range and length_error exceptions.
234e0d31 580 _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
bc2631e0 581 __try
e2c09482 582 { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
bc2631e0 583 __catch(...)
ed6814f7
BI
584 {
585 __r->_M_destroy(__a);
e2c09482
BK
586 __throw_exception_again;
587 }
cbf52bfa 588 __r->_M_set_length_and_sharable(__dnew);
725dc051
BK
589 return __r->_M_refdata();
590 }
591
592 template<typename _CharT, typename _Traits, typename _Alloc>
593 _CharT*
9a304d17 594 basic_string<_CharT, _Traits, _Alloc>::
725dc051
BK
595 _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
596 {
d7a3ef97 597#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
725dc051 598 if (__n == 0 && __a == _Alloc())
ca566e4c 599 return _S_empty_rep()._M_refdata();
1165dc50 600#endif
725dc051 601 // Check for out_of_range and length_error exceptions.
234e0d31 602 _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
ed6814f7 603 if (__n)
ec61e852 604 _M_assign(__r->_M_refdata(), __n, __c);
954b12d2 605
cbf52bfa 606 __r->_M_set_length_and_sharable(__n);
725dc051
BK
607 return __r->_M_refdata();
608 }
609
610 template<typename _CharT, typename _Traits, typename _Alloc>
611 basic_string<_CharT, _Traits, _Alloc>::
612 basic_string(const basic_string& __str)
f26e5597
CB
613 : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(__str.get_allocator()),
614 __str.get_allocator()),
615 __str.get_allocator())
725dc051
BK
616 { }
617
618 template<typename _CharT, typename _Traits, typename _Alloc>
619 basic_string<_CharT, _Traits, _Alloc>::
620 basic_string(const _Alloc& __a)
621 : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a)
622 { }
ed6814f7 623
725dc051
BK
624 template<typename _CharT, typename _Traits, typename _Alloc>
625 basic_string<_CharT, _Traits, _Alloc>::
626 basic_string(const basic_string& __str, size_type __pos, size_type __n)
a3af79ea 627 : _M_dataplus(_S_construct(__str._M_data()
0e50667c
PC
628 + __str._M_check(__pos,
629 "basic_string::basic_string"),
927dc7c6
PC
630 __str._M_data() + __str._M_limit(__pos, __n)
631 + __pos, _Alloc()), _Alloc())
725dc051
BK
632 { }
633
634 template<typename _CharT, typename _Traits, typename _Alloc>
635 basic_string<_CharT, _Traits, _Alloc>::
636 basic_string(const basic_string& __str, size_type __pos,
637 size_type __n, const _Alloc& __a)
a3af79ea 638 : _M_dataplus(_S_construct(__str._M_data()
0e50667c
PC
639 + __str._M_check(__pos,
640 "basic_string::basic_string"),
927dc7c6
PC
641 __str._M_data() + __str._M_limit(__pos, __n)
642 + __pos, __a), __a)
725dc051
BK
643 { }
644
285b36d6 645 // TBD: DPG annotate
725dc051
BK
646 template<typename _CharT, typename _Traits, typename _Alloc>
647 basic_string<_CharT, _Traits, _Alloc>::
648 basic_string(const _CharT* __s, size_type __n, const _Alloc& __a)
927dc7c6 649 : _M_dataplus(_S_construct(__s, __s + __n, __a), __a)
725dc051
BK
650 { }
651
285b36d6 652 // TBD: DPG annotate
725dc051
BK
653 template<typename _CharT, typename _Traits, typename _Alloc>
654 basic_string<_CharT, _Traits, _Alloc>::
655 basic_string(const _CharT* __s, const _Alloc& __a)
927dc7c6
PC
656 : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) :
657 __s + npos, __a), __a)
725dc051
BK
658 { }
659
660 template<typename _CharT, typename _Traits, typename _Alloc>
661 basic_string<_CharT, _Traits, _Alloc>::
662 basic_string(size_type __n, _CharT __c, const _Alloc& __a)
663 : _M_dataplus(_S_construct(__n, __c, __a), __a)
664 { }
285b36d6 665
ed6814f7 666 // TBD: DPG annotate
725dc051 667 template<typename _CharT, typename _Traits, typename _Alloc>
08addde6 668 template<typename _InputIterator>
927dc7c6
PC
669 basic_string<_CharT, _Traits, _Alloc>::
670 basic_string(_InputIterator __beg, _InputIterator __end, const _Alloc& __a)
671 : _M_dataplus(_S_construct(__beg, __end, __a), __a)
672 { }
725dc051 673
734f5023 674#if __cplusplus >= 201103L
988499f4
JM
675 template<typename _CharT, typename _Traits, typename _Alloc>
676 basic_string<_CharT, _Traits, _Alloc>::
677 basic_string(initializer_list<_CharT> __l, const _Alloc& __a)
927dc7c6 678 : _M_dataplus(_S_construct(__l.begin(), __l.end(), __a), __a)
988499f4
JM
679 { }
680#endif
681
5536e07d
PC
682 template<typename _CharT, typename _Traits, typename _Alloc>
683 basic_string<_CharT, _Traits, _Alloc>&
684 basic_string<_CharT, _Traits, _Alloc>::
685 assign(const basic_string& __str)
686 {
687 if (_M_rep() != __str._M_rep())
688 {
689 // XXX MT
690 const allocator_type __a = this->get_allocator();
691 _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());
692 _M_rep()->_M_dispose(__a);
693 _M_data(__tmp);
694 }
695 return *this;
696 }
697
725dc051
BK
698 template<typename _CharT, typename _Traits, typename _Alloc>
699 basic_string<_CharT, _Traits, _Alloc>&
ca566e4c 700 basic_string<_CharT, _Traits, _Alloc>::
ec61e852 701 assign(const _CharT* __s, size_type __n)
725dc051 702 {
ec61e852
PC
703 __glibcxx_requires_string_len(__s, __n);
704 _M_check_length(this->size(), __n, "basic_string::assign");
8eae76be 705 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
ec61e852
PC
706 return _M_replace_safe(size_type(0), this->size(), __s, __n);
707 else
725dc051 708 {
ec61e852
PC
709 // Work in-place.
710 const size_type __pos = __s - _M_data();
711 if (__pos >= __n)
d87bdb13 712 _M_copy(_M_data(), __s, __n);
ec61e852 713 else if (__pos)
d87bdb13 714 _M_move(_M_data(), __s, __n);
ec61e852
PC
715 _M_rep()->_M_set_length_and_sharable(__n);
716 return *this;
717 }
718 }
719
ab4375af
PC
720 template<typename _CharT, typename _Traits, typename _Alloc>
721 basic_string<_CharT, _Traits, _Alloc>&
722 basic_string<_CharT, _Traits, _Alloc>::
723 append(size_type __n, _CharT __c)
724 {
725 if (__n)
726 {
727 _M_check_length(size_type(0), __n, "basic_string::append");
728 const size_type __len = __n + this->size();
729 if (__len > this->capacity() || _M_rep()->_M_is_shared())
730 this->reserve(__len);
731 _M_assign(_M_data() + this->size(), __n, __c);
732 _M_rep()->_M_set_length_and_sharable(__len);
733 }
734 return *this;
735 }
736
ec61e852
PC
737 template<typename _CharT, typename _Traits, typename _Alloc>
738 basic_string<_CharT, _Traits, _Alloc>&
739 basic_string<_CharT, _Traits, _Alloc>::
740 append(const _CharT* __s, size_type __n)
741 {
742 __glibcxx_requires_string_len(__s, __n);
743 if (__n)
744 {
745 _M_check_length(size_type(0), __n, "basic_string::append");
746 const size_type __len = __n + this->size();
747 if (__len > this->capacity() || _M_rep()->_M_is_shared())
748 {
8eae76be 749 if (_M_disjunct(__s))
ec61e852
PC
750 this->reserve(__len);
751 else
752 {
753 const size_type __off = __s - _M_data();
754 this->reserve(__len);
755 __s = _M_data() + __off;
756 }
757 }
758 _M_copy(_M_data() + this->size(), __s, __n);
759 _M_rep()->_M_set_length_and_sharable(__len);
725dc051
BK
760 }
761 return *this;
762 }
2d9d5235 763
ab4375af
PC
764 template<typename _CharT, typename _Traits, typename _Alloc>
765 basic_string<_CharT, _Traits, _Alloc>&
766 basic_string<_CharT, _Traits, _Alloc>::
767 append(const basic_string& __str)
768 {
769 const size_type __size = __str.size();
770 if (__size)
771 {
772 const size_type __len = __size + this->size();
773 if (__len > this->capacity() || _M_rep()->_M_is_shared())
774 this->reserve(__len);
775 _M_copy(_M_data() + this->size(), __str._M_data(), __size);
776 _M_rep()->_M_set_length_and_sharable(__len);
777 }
778 return *this;
779 }
780
ec61e852
PC
781 template<typename _CharT, typename _Traits, typename _Alloc>
782 basic_string<_CharT, _Traits, _Alloc>&
783 basic_string<_CharT, _Traits, _Alloc>::
784 append(const basic_string& __str, size_type __pos, size_type __n)
785 {
786 __str._M_check(__pos, "basic_string::append");
787 __n = __str._M_limit(__pos, __n);
788 if (__n)
789 {
790 const size_type __len = __n + this->size();
791 if (__len > this->capacity() || _M_rep()->_M_is_shared())
792 this->reserve(__len);
793 _M_copy(_M_data() + this->size(), __str._M_data() + __pos, __n);
794 _M_rep()->_M_set_length_and_sharable(__len);
795 }
796 return *this;
797 }
2d9d5235 798
2d9d5235
NM
799 template<typename _CharT, typename _Traits, typename _Alloc>
800 basic_string<_CharT, _Traits, _Alloc>&
801 basic_string<_CharT, _Traits, _Alloc>::
802 insert(size_type __pos, const _CharT* __s, size_type __n)
803 {
285b36d6 804 __glibcxx_requires_string_len(__s, __n);
04cc8aef 805 _M_check(__pos, "basic_string::insert");
ec61e852 806 _M_check_length(size_type(0), __n, "basic_string::insert");
8eae76be 807 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
7bb9b33b 808 return _M_replace_safe(__pos, size_type(0), __s, __n);
2d9d5235
NM
809 else
810 {
ec61e852 811 // Work in-place.
2d9d5235
NM
812 const size_type __off = __s - _M_data();
813 _M_mutate(__pos, 0, __n);
814 __s = _M_data() + __off;
815 _CharT* __p = _M_data() + __pos;
816 if (__s + __n <= __p)
ec61e852 817 _M_copy(__p, __s, __n);
2d9d5235 818 else if (__s >= __p)
ec61e852 819 _M_copy(__p, __s + __n, __n);
2d9d5235
NM
820 else
821 {
2cb612d1 822 const size_type __nleft = __p - __s;
ec61e852
PC
823 _M_copy(__p, __s, __nleft);
824 _M_copy(__p + __nleft, __p + __n, __n - __nleft);
2d9d5235
NM
825 }
826 return *this;
827 }
828 }
ed6814f7 829
7309083f
PC
830 template<typename _CharT, typename _Traits, typename _Alloc>
831 typename basic_string<_CharT, _Traits, _Alloc>::iterator
832 basic_string<_CharT, _Traits, _Alloc>::
833 erase(iterator __first, iterator __last)
834 {
835 _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
836 && __last <= _M_iend());
837
838 // NB: This isn't just an optimization (bail out early when
839 // there is nothing to do, really), it's also a correctness
840 // issue vs MT, see libstdc++/40518.
841 const size_type __size = __last - __first;
842 if (__size)
843 {
844 const size_type __pos = __first - _M_ibegin();
845 _M_mutate(__pos, __size, size_type(0));
846 _M_rep()->_M_set_leaked();
847 return iterator(_M_data() + __pos);
848 }
849 else
850 return __first;
851 }
852
2d9d5235
NM
853 template<typename _CharT, typename _Traits, typename _Alloc>
854 basic_string<_CharT, _Traits, _Alloc>&
855 basic_string<_CharT, _Traits, _Alloc>::
856 replace(size_type __pos, size_type __n1, const _CharT* __s,
857 size_type __n2)
858 {
285b36d6 859 __glibcxx_requires_string_len(__s, __n2);
04cc8aef 860 _M_check(__pos, "basic_string::replace");
e03a6fb7 861 __n1 = _M_limit(__pos, __n1);
ec61e852 862 _M_check_length(__n1, __n2, "basic_string::replace");
2cb612d1 863 bool __left;
8eae76be 864 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
7bb9b33b 865 return _M_replace_safe(__pos, __n1, __s, __n2);
2cb612d1
PC
866 else if ((__left = __s + __n2 <= _M_data() + __pos)
867 || _M_data() + __pos + __n1 <= __s)
868 {
869 // Work in-place: non-overlapping case.
ec61e852
PC
870 size_type __off = __s - _M_data();
871 __left ? __off : (__off += __n2 - __n1);
2cb612d1 872 _M_mutate(__pos, __n1, __n2);
ec61e852 873 _M_copy(_M_data() + __pos, _M_data() + __off, __n2);
2cb612d1
PC
874 return *this;
875 }
2d9d5235 876 else
251804e6 877 {
2cb612d1 878 // Todo: overlapping case.
251804e6
PC
879 const basic_string __tmp(__s, __n2);
880 return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2);
881 }
2d9d5235 882 }
ed6814f7 883
725dc051
BK
884 template<typename _CharT, typename _Traits, typename _Alloc>
885 void
886 basic_string<_CharT, _Traits, _Alloc>::_Rep::
887 _M_destroy(const _Alloc& __a) throw ()
888 {
6be8b524
PC
889 const size_type __size = sizeof(_Rep_base) +
890 (this->_M_capacity + 1) * sizeof(_CharT);
891 _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);
725dc051
BK
892 }
893
894 template<typename _CharT, typename _Traits, typename _Alloc>
895 void
cc6e67d4
PC
896 basic_string<_CharT, _Traits, _Alloc>::
897 _M_leak_hard()
725dc051 898 {
d7a3ef97 899#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
ca566e4c 900 if (_M_rep() == &_S_empty_rep())
1165dc50
PC
901 return;
902#endif
ed6814f7 903 if (_M_rep()->_M_is_shared())
725dc051
BK
904 _M_mutate(0, 0, 0);
905 _M_rep()->_M_set_leaked();
906 }
907
908 template<typename _CharT, typename _Traits, typename _Alloc>
909 void
910 basic_string<_CharT, _Traits, _Alloc>::
911 _M_mutate(size_type __pos, size_type __len1, size_type __len2)
912 {
7778fa6e 913 const size_type __old_size = this->size();
725dc051 914 const size_type __new_size = __old_size + __len2 - __len1;
725dc051 915 const size_type __how_much = __old_size - __pos - __len1;
ed6814f7 916
cc6e67d4 917 if (__new_size > this->capacity() || _M_rep()->_M_is_shared())
725dc051
BK
918 {
919 // Must reallocate.
91eab378 920 const allocator_type __a = get_allocator();
cc6e67d4 921 _Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a);
954b12d2
PC
922
923 if (__pos)
ec61e852 924 _M_copy(__r->_M_refdata(), _M_data(), __pos);
954b12d2 925 if (__how_much)
ec61e852
PC
926 _M_copy(__r->_M_refdata() + __pos + __len2,
927 _M_data() + __pos + __len1, __how_much);
954b12d2 928
725dc051
BK
929 _M_rep()->_M_dispose(__a);
930 _M_data(__r->_M_refdata());
ca566e4c 931 }
725dc051
BK
932 else if (__how_much && __len1 != __len2)
933 {
ec61e852
PC
934 // Work in-place.
935 _M_move(_M_data() + __pos + __len2,
936 _M_data() + __pos + __len1, __how_much);
725dc051 937 }
cbf52bfa 938 _M_rep()->_M_set_length_and_sharable(__new_size);
725dc051 939 }
ed6814f7 940
725dc051
BK
941 template<typename _CharT, typename _Traits, typename _Alloc>
942 void
cc6e67d4
PC
943 basic_string<_CharT, _Traits, _Alloc>::
944 reserve(size_type __res)
725dc051 945 {
cbc67955 946 if (__res != this->capacity() || _M_rep()->_M_is_shared())
725dc051 947 {
dcc61724
PC
948 // Make sure we don't shrink below the current size
949 if (__res < this->size())
950 __res = this->size();
91eab378 951 const allocator_type __a = get_allocator();
725dc051
BK
952 _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size());
953 _M_rep()->_M_dispose(__a);
954 _M_data(__tmp);
955 }
956 }
ed6814f7 957
725dc051 958 template<typename _CharT, typename _Traits, typename _Alloc>
cc6e67d4
PC
959 void
960 basic_string<_CharT, _Traits, _Alloc>::
961 swap(basic_string& __s)
725dc051 962 {
ed6814f7 963 if (_M_rep()->_M_is_leaked())
725dc051 964 _M_rep()->_M_set_sharable();
ed6814f7 965 if (__s._M_rep()->_M_is_leaked())
725dc051
BK
966 __s._M_rep()->_M_set_sharable();
967 if (this->get_allocator() == __s.get_allocator())
968 {
969 _CharT* __tmp = _M_data();
970 _M_data(__s._M_data());
971 __s._M_data(__tmp);
972 }
973 // The code below can usually be optimized away.
ed6814f7 974 else
725dc051 975 {
0e50667c
PC
976 const basic_string __tmp1(_M_ibegin(), _M_iend(),
977 __s.get_allocator());
ed6814f7 978 const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
0e50667c 979 this->get_allocator());
725dc051
BK
980 *this = __tmp2;
981 __s = __tmp1;
982 }
983 }
984
725dc051 985 template<typename _CharT, typename _Traits, typename _Alloc>
31bfa177 986 typename basic_string<_CharT, _Traits, _Alloc>::_Rep*
725dc051 987 basic_string<_CharT, _Traits, _Alloc>::_Rep::
234e0d31
PC
988 _S_create(size_type __capacity, size_type __old_capacity,
989 const _Alloc& __alloc)
725dc051 990 {
f5677b15 991 // _GLIBCXX_RESOLVE_LIB_DEFECTS
725dc051 992 // 83. String::npos vs. string::max_size()
e2c09482 993 if (__capacity > _S_max_size)
0e50667c 994 __throw_length_error(__N("basic_string::_S_create"));
725dc051 995
2883d58b
LR
996 // The standard places no restriction on allocating more memory
997 // than is strictly needed within this layer at the moment or as
234e0d31
PC
998 // requested by an explicit application call to reserve().
999
1000 // Many malloc implementations perform quite poorly when an
2883d58b
LR
1001 // application attempts to allocate memory in a stepwise fashion
1002 // growing each allocation size by only 1 char. Additionally,
1003 // it makes little sense to allocate less linear memory than the
1004 // natural blocking size of the malloc implementation.
1005 // Unfortunately, we would need a somewhat low-level calculation
1006 // with tuned parameters to get this perfect for any particular
1007 // malloc implementation. Fortunately, generalizations about
1008 // common features seen among implementations seems to suffice.
2883d58b
LR
1009
1010 // __pagesize need not match the actual VM page size for good
1011 // results in practice, thus we pick a common value on the low
1012 // side. __malloc_header_size is an estimate of the amount of
1013 // overhead per memory allocation (in practice seen N * sizeof
1014 // (void*) where N is 0, 2 or 4). According to folklore,
1015 // picking this value on the high side is better than
1016 // low-balling it (especially when this algorithm is used with
1017 // malloc implementations that allocate memory blocks rounded up
1018 // to a size which is a power of 2).
cbb0dcef
PC
1019 const size_type __pagesize = 4096;
1020 const size_type __malloc_header_size = 4 * sizeof(void*);
234e0d31
PC
1021
1022 // The below implements an exponential growth policy, necessary to
1023 // meet amortized linear time requirements of the library: see
1024 // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
1025 // It's active for allocations requiring an amount of memory above
1026 // system pagesize. This is consistent with the requirements of the
1027 // standard: http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html
cbb0dcef 1028 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
234e0d31
PC
1029 __capacity = 2 * __old_capacity;
1030
1031 // NB: Need an array of char_type[__capacity], plus a terminating
6be8b524 1032 // null char_type() element, plus enough for the _Rep data structure.
234e0d31 1033 // Whew. Seemingly so needy, yet so elemental.
6be8b524 1034 size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
234e0d31 1035
24f33069 1036 const size_type __adj_size = __size + __malloc_header_size;
d1a7222c 1037 if (__adj_size > __pagesize && __capacity > __old_capacity)
2883d58b 1038 {
24f33069 1039 const size_type __extra = __pagesize - __adj_size % __pagesize;
2883d58b 1040 __capacity += __extra / sizeof(_CharT);
d1615643
PC
1041 // Never allocate a string bigger than _S_max_size.
1042 if (__capacity > _S_max_size)
1043 __capacity = _S_max_size;
6be8b524 1044 __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
2883d58b 1045 }
2883d58b 1046
725dc051
BK
1047 // NB: Might throw, but no worries about a leak, mate: _Rep()
1048 // does not throw.
6be8b524 1049 void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
725dc051
BK
1050 _Rep *__p = new (__place) _Rep;
1051 __p->_M_capacity = __capacity;
039f9e35
JJ
1052 // ABI compatibility - 3.4.x set in _S_create both
1053 // _M_refcount and _M_length. All callers of _S_create
1054 // in basic_string.tcc then set just _M_length.
1055 // In 4.0.x and later both _M_refcount and _M_length
1056 // are initialized in the callers, unfortunately we can
1057 // have 3.4.x compiled code with _S_create callers inlined
1058 // calling 4.0.x+ _S_create.
1059 __p->_M_set_sharable();
725dc051
BK
1060 return __p;
1061 }
1062
1063 template<typename _CharT, typename _Traits, typename _Alloc>
1064 _CharT*
1065 basic_string<_CharT, _Traits, _Alloc>::_Rep::
1066 _M_clone(const _Alloc& __alloc, size_type __res)
1067 {
79f57f23 1068 // Requested capacity of the clone.
ca566e4c 1069 const size_type __requested_cap = this->_M_length + __res;
234e0d31
PC
1070 _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity,
1071 __alloc);
ca566e4c 1072 if (this->_M_length)
ec61e852 1073 _M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length);
954b12d2 1074
cbf52bfa 1075 __r->_M_set_length_and_sharable(this->_M_length);
725dc051
BK
1076 return __r->_M_refdata();
1077 }
ed6814f7 1078
725dc051
BK
1079 template<typename _CharT, typename _Traits, typename _Alloc>
1080 void
cc6e67d4
PC
1081 basic_string<_CharT, _Traits, _Alloc>::
1082 resize(size_type __n, _CharT __c)
725dc051 1083 {
7778fa6e 1084 const size_type __size = this->size();
ec61e852 1085 _M_check_length(__size, __n, "basic_string::resize");
725dc051
BK
1086 if (__size < __n)
1087 this->append(__n - __size, __c);
1088 else if (__n < __size)
1089 this->erase(__n);
1090 // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
1091 }
418bb880 1092
725dc051 1093 template<typename _CharT, typename _Traits, typename _Alloc>
08addde6 1094 template<typename _InputIterator>
725dc051
BK
1095 basic_string<_CharT, _Traits, _Alloc>&
1096 basic_string<_CharT, _Traits, _Alloc>::
ed6814f7 1097 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
fefe561e 1098 _InputIterator __k2, __false_type)
725dc051 1099 {
7778fa6e 1100 const basic_string __s(__k1, __k2);
251804e6 1101 const size_type __n1 = __i2 - __i1;
ec61e852 1102 _M_check_length(__n1, __s.size(), "basic_string::_M_replace_dispatch");
251804e6 1103 return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
7bb9b33b 1104 __s.size());
725dc051 1105 }
6bfad5e1
PC
1106
1107 template<typename _CharT, typename _Traits, typename _Alloc>
1108 basic_string<_CharT, _Traits, _Alloc>&
1109 basic_string<_CharT, _Traits, _Alloc>::
1110 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
1111 _CharT __c)
1112 {
1113 _M_check_length(__n1, __n2, "basic_string::_M_replace_aux");
1114 _M_mutate(__pos1, __n1, __n2);
1115 if (__n2)
1116 _M_assign(_M_data() + __pos1, __n2, __c);
1117 return *this;
1118 }
1119
1120 template<typename _CharT, typename _Traits, typename _Alloc>
1121 basic_string<_CharT, _Traits, _Alloc>&
1122 basic_string<_CharT, _Traits, _Alloc>::
1123 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
1124 size_type __n2)
1125 {
1126 _M_mutate(__pos1, __n1, __n2);
1127 if (__n2)
1128 _M_copy(_M_data() + __pos1, __s, __n2);
1129 return *this;
1130 }
34a2b755
JW
1131
1132 template<typename _CharT, typename _Traits, typename _Alloc>
1133 typename basic_string<_CharT, _Traits, _Alloc>::size_type
1134 basic_string<_CharT, _Traits, _Alloc>::
1135 copy(_CharT* __s, size_type __n, size_type __pos) const
1136 {
1137 _M_check(__pos, "basic_string::copy");
1138 __n = _M_limit(__pos, __n);
1139 __glibcxx_requires_string_len(__s, __n);
1140 if (__n)
1141 _M_copy(__s, _M_data() + __pos, __n);
1142 // 21.3.5.7 par 3: do not append null. (good.)
1143 return __n;
1144 }
1145#endif // !_GLIBCXX_USE_CXX11_ABI
6bfad5e1 1146
725dc051 1147 template<typename _CharT, typename _Traits, typename _Alloc>
9a304d17 1148 basic_string<_CharT, _Traits, _Alloc>
725dc051 1149 operator+(const _CharT* __lhs,
9a304d17 1150 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
725dc051 1151 {
285b36d6 1152 __glibcxx_requires_string(__lhs);
9a304d17 1153 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
725dc051 1154 typedef typename __string_type::size_type __size_type;
7778fa6e 1155 const __size_type __len = _Traits::length(__lhs);
725dc051
BK
1156 __string_type __str;
1157 __str.reserve(__len + __rhs.size());
bb9909b0 1158 __str.append(__lhs, __len);
725dc051
BK
1159 __str.append(__rhs);
1160 return __str;
1161 }
1162
1163 template<typename _CharT, typename _Traits, typename _Alloc>
9a304d17
PC
1164 basic_string<_CharT, _Traits, _Alloc>
1165 operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs)
725dc051 1166 {
9a304d17 1167 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
725dc051
BK
1168 typedef typename __string_type::size_type __size_type;
1169 __string_type __str;
7778fa6e 1170 const __size_type __len = __rhs.size();
725dc051 1171 __str.reserve(__len + 1);
15f13f01 1172 __str.append(__size_type(1), __lhs);
725dc051
BK
1173 __str.append(__rhs);
1174 return __str;
1175 }
1176
725dc051 1177 template<typename _CharT, typename _Traits, typename _Alloc>
31bfa177 1178 typename basic_string<_CharT, _Traits, _Alloc>::size_type
725dc051 1179 basic_string<_CharT, _Traits, _Alloc>::
725dc051
BK
1180 find(const _CharT* __s, size_type __pos, size_type __n) const
1181 {
285b36d6 1182 __glibcxx_requires_string_len(__s, __n);
7778fa6e 1183 const size_type __size = this->size();
ceea48fc 1184 const _CharT* __data = data();
1a4ba99f
PC
1185
1186 if (__n == 0)
1187 return __pos <= __size ? __pos : npos;
1188
5527be59
PC
1189 if (__n <= __size)
1190 {
1c917b03 1191 for (; __pos <= __size - __n; ++__pos)
5527be59
PC
1192 if (traits_type::eq(__data[__pos], __s[0])
1193 && traits_type::compare(__data + __pos + 1,
1194 __s + 1, __n - 1) == 0)
1195 return __pos;
1196 }
1a4ba99f 1197 return npos;
725dc051
BK
1198 }
1199
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>::
cea8c6de 1203 find(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
725dc051 1204 {
725dc051 1205 size_type __ret = npos;
4a787fa8 1206 const size_type __size = this->size();
725dc051
BK
1207 if (__pos < __size)
1208 {
1209 const _CharT* __data = _M_data();
7778fa6e 1210 const size_type __n = __size - __pos;
97644827
BK
1211 const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
1212 if (__p)
725dc051
BK
1213 __ret = __p - __data;
1214 }
1215 return __ret;
1216 }
1217
725dc051 1218 template<typename _CharT, typename _Traits, typename _Alloc>
31bfa177 1219 typename basic_string<_CharT, _Traits, _Alloc>::size_type
725dc051
BK
1220 basic_string<_CharT, _Traits, _Alloc>::
1221 rfind(const _CharT* __s, size_type __pos, size_type __n) const
1222 {
285b36d6 1223 __glibcxx_requires_string_len(__s, __n);
7778fa6e 1224 const size_type __size = this->size();
725dc051
BK
1225 if (__n <= __size)
1226 {
dd6eaaed 1227 __pos = std::min(size_type(__size - __n), __pos);
725dc051 1228 const _CharT* __data = _M_data();
ed6814f7 1229 do
725dc051
BK
1230 {
1231 if (traits_type::compare(__data + __pos, __s, __n) == 0)
1232 return __pos;
ed6814f7 1233 }
725dc051
BK
1234 while (__pos-- > 0);
1235 }
1236 return npos;
1237 }
ed6814f7 1238
725dc051 1239 template<typename _CharT, typename _Traits, typename _Alloc>
31bfa177 1240 typename basic_string<_CharT, _Traits, _Alloc>::size_type
725dc051 1241 basic_string<_CharT, _Traits, _Alloc>::
cea8c6de 1242 rfind(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
725dc051 1243 {
04cc8aef 1244 size_type __size = this->size();
725dc051
BK
1245 if (__size)
1246 {
04cc8aef
PC
1247 if (--__size > __pos)
1248 __size = __pos;
1249 for (++__size; __size-- > 0; )
1250 if (traits_type::eq(_M_data()[__size], __c))
1251 return __size;
725dc051
BK
1252 }
1253 return npos;
1254 }
ed6814f7 1255
725dc051 1256 template<typename _CharT, typename _Traits, typename _Alloc>
31bfa177 1257 typename basic_string<_CharT, _Traits, _Alloc>::size_type
725dc051
BK
1258 basic_string<_CharT, _Traits, _Alloc>::
1259 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
1260 {
285b36d6 1261 __glibcxx_requires_string_len(__s, __n);
725dc051
BK
1262 for (; __n && __pos < this->size(); ++__pos)
1263 {
97644827
BK
1264 const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
1265 if (__p)
725dc051
BK
1266 return __pos;
1267 }
1268 return npos;
1269 }
ed6814f7 1270
725dc051 1271 template<typename _CharT, typename _Traits, typename _Alloc>
31bfa177 1272 typename basic_string<_CharT, _Traits, _Alloc>::size_type
725dc051
BK
1273 basic_string<_CharT, _Traits, _Alloc>::
1274 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
1275 {
285b36d6 1276 __glibcxx_requires_string_len(__s, __n);
725dc051
BK
1277 size_type __size = this->size();
1278 if (__size && __n)
ed6814f7
BI
1279 {
1280 if (--__size > __pos)
725dc051
BK
1281 __size = __pos;
1282 do
1283 {
97644827 1284 if (traits_type::find(__s, __n, _M_data()[__size]))
725dc051 1285 return __size;
ed6814f7 1286 }
725dc051
BK
1287 while (__size-- != 0);
1288 }
1289 return npos;
1290 }
ed6814f7 1291
725dc051 1292 template<typename _CharT, typename _Traits, typename _Alloc>
31bfa177 1293 typename basic_string<_CharT, _Traits, _Alloc>::size_type
725dc051
BK
1294 basic_string<_CharT, _Traits, _Alloc>::
1295 find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
1296 {
285b36d6 1297 __glibcxx_requires_string_len(__s, __n);
fefe561e
PC
1298 for (; __pos < this->size(); ++__pos)
1299 if (!traits_type::find(__s, __n, _M_data()[__pos]))
1300 return __pos;
725dc051
BK
1301 return npos;
1302 }
1303
1304 template<typename _CharT, typename _Traits, typename _Alloc>
31bfa177 1305 typename basic_string<_CharT, _Traits, _Alloc>::size_type
725dc051 1306 basic_string<_CharT, _Traits, _Alloc>::
cea8c6de 1307 find_first_not_of(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
725dc051 1308 {
fefe561e
PC
1309 for (; __pos < this->size(); ++__pos)
1310 if (!traits_type::eq(_M_data()[__pos], __c))
1311 return __pos;
725dc051
BK
1312 return npos;
1313 }
1314
1315 template<typename _CharT, typename _Traits, typename _Alloc>
31bfa177 1316 typename basic_string<_CharT, _Traits, _Alloc>::size_type
725dc051
BK
1317 basic_string<_CharT, _Traits, _Alloc>::
1318 find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
1319 {
285b36d6 1320 __glibcxx_requires_string_len(__s, __n);
04cc8aef 1321 size_type __size = this->size();
ba317c52 1322 if (__size)
04cc8aef
PC
1323 {
1324 if (--__size > __pos)
1325 __size = __pos;
ed6814f7 1326 do
725dc051 1327 {
04cc8aef
PC
1328 if (!traits_type::find(__s, __n, _M_data()[__size]))
1329 return __size;
ed6814f7 1330 }
04cc8aef 1331 while (__size--);
725dc051
BK
1332 }
1333 return npos;
1334 }
1335
1336 template<typename _CharT, typename _Traits, typename _Alloc>
31bfa177 1337 typename basic_string<_CharT, _Traits, _Alloc>::size_type
725dc051 1338 basic_string<_CharT, _Traits, _Alloc>::
cea8c6de 1339 find_last_not_of(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
725dc051 1340 {
04cc8aef 1341 size_type __size = this->size();
725dc051 1342 if (__size)
fefe561e 1343 {
04cc8aef 1344 if (--__size > __pos)
ed6814f7 1345 __size = __pos;
725dc051
BK
1346 do
1347 {
04cc8aef
PC
1348 if (!traits_type::eq(_M_data()[__size], __c))
1349 return __size;
ed6814f7 1350 }
04cc8aef 1351 while (__size--);
725dc051
BK
1352 }
1353 return npos;
1354 }
ed6814f7 1355
725dc051
BK
1356 template<typename _CharT, typename _Traits, typename _Alloc>
1357 int
1358 basic_string<_CharT, _Traits, _Alloc>::
1359 compare(size_type __pos, size_type __n, const basic_string& __str) const
1360 {
04cc8aef 1361 _M_check(__pos, "basic_string::compare");
e03a6fb7 1362 __n = _M_limit(__pos, __n);
7778fa6e 1363 const size_type __osize = __str.size();
e03a6fb7 1364 const size_type __len = std::min(__n, __osize);
725dc051
BK
1365 int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
1366 if (!__r)
9521dd6b 1367 __r = _S_compare(__n, __osize);
725dc051
BK
1368 return __r;
1369 }
1370
1371 template<typename _CharT, typename _Traits, typename _Alloc>
1372 int
1373 basic_string<_CharT, _Traits, _Alloc>::
1374 compare(size_type __pos1, size_type __n1, const basic_string& __str,
1375 size_type __pos2, size_type __n2) const
1376 {
04cc8aef
PC
1377 _M_check(__pos1, "basic_string::compare");
1378 __str._M_check(__pos2, "basic_string::compare");
e03a6fb7
PC
1379 __n1 = _M_limit(__pos1, __n1);
1380 __n2 = __str._M_limit(__pos2, __n2);
1381 const size_type __len = std::min(__n1, __n2);
ed6814f7 1382 int __r = traits_type::compare(_M_data() + __pos1,
725dc051
BK
1383 __str.data() + __pos2, __len);
1384 if (!__r)
9521dd6b 1385 __r = _S_compare(__n1, __n2);
725dc051
BK
1386 return __r;
1387 }
1388
725dc051
BK
1389 template<typename _CharT, typename _Traits, typename _Alloc>
1390 int
1391 basic_string<_CharT, _Traits, _Alloc>::
1392 compare(const _CharT* __s) const
1393 {
285b36d6 1394 __glibcxx_requires_string(__s);
7778fa6e
PC
1395 const size_type __size = this->size();
1396 const size_type __osize = traits_type::length(__s);
1397 const size_type __len = std::min(__size, __osize);
c86c54e6 1398 int __r = traits_type::compare(_M_data(), __s, __len);
725dc051 1399 if (!__r)
9521dd6b 1400 __r = _S_compare(__size, __osize);
725dc051
BK
1401 return __r;
1402 }
1403
3b0fd4bc
BK
1404 template<typename _CharT, typename _Traits, typename _Alloc>
1405 int
9a304d17 1406 basic_string <_CharT, _Traits, _Alloc>::
3b0fd4bc
BK
1407 compare(size_type __pos, size_type __n1, const _CharT* __s) const
1408 {
285b36d6 1409 __glibcxx_requires_string(__s);
04cc8aef 1410 _M_check(__pos, "basic_string::compare");
e03a6fb7 1411 __n1 = _M_limit(__pos, __n1);
7778fa6e 1412 const size_type __osize = traits_type::length(__s);
e03a6fb7 1413 const size_type __len = std::min(__n1, __osize);
3b0fd4bc
BK
1414 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
1415 if (!__r)
9521dd6b 1416 __r = _S_compare(__n1, __osize);
3b0fd4bc
BK
1417 return __r;
1418 }
1419
725dc051
BK
1420 template<typename _CharT, typename _Traits, typename _Alloc>
1421 int
9a304d17 1422 basic_string <_CharT, _Traits, _Alloc>::
ed6814f7 1423 compare(size_type __pos, size_type __n1, const _CharT* __s,
725dc051
BK
1424 size_type __n2) const
1425 {
285b36d6 1426 __glibcxx_requires_string_len(__s, __n2);
04cc8aef 1427 _M_check(__pos, "basic_string::compare");
e03a6fb7
PC
1428 __n1 = _M_limit(__pos, __n1);
1429 const size_type __len = std::min(__n1, __n2);
725dc051
BK
1430 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
1431 if (!__r)
9521dd6b 1432 __r = _S_compare(__n1, __n2);
725dc051
BK
1433 return __r;
1434 }
1435
11202768
PC
1436 // 21.3.7.9 basic_string::getline and operators
1437 template<typename _CharT, typename _Traits, typename _Alloc>
1438 basic_istream<_CharT, _Traits>&
1439 operator>>(basic_istream<_CharT, _Traits>& __in,
1440 basic_string<_CharT, _Traits, _Alloc>& __str)
1441 {
1442 typedef basic_istream<_CharT, _Traits> __istream_type;
1443 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1444 typedef typename __istream_type::ios_base __ios_base;
1445 typedef typename __istream_type::int_type __int_type;
1446 typedef typename __string_type::size_type __size_type;
1447 typedef ctype<_CharT> __ctype_type;
1448 typedef typename __ctype_type::ctype_base __ctype_base;
1449
1450 __size_type __extracted = 0;
1451 typename __ios_base::iostate __err = __ios_base::goodbit;
1452 typename __istream_type::sentry __cerb(__in, false);
880b527f 1453 if (__cerb)
11202768 1454 {
bc2631e0 1455 __try
11202768
PC
1456 {
1457 // Avoid reallocation for common case.
1458 __str.erase();
1459 _CharT __buf[128];
1460 __size_type __len = 0;
1461 const streamsize __w = __in.width();
1462 const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
1463 : __str.max_size();
1464 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
1465 const __int_type __eof = _Traits::eof();
1466 __int_type __c = __in.rdbuf()->sgetc();
1467
1468 while (__extracted < __n
1469 && !_Traits::eq_int_type(__c, __eof)
1470 && !__ct.is(__ctype_base::space,
1471 _Traits::to_char_type(__c)))
1472 {
1473 if (__len == sizeof(__buf) / sizeof(_CharT))
1474 {
1475 __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
1476 __len = 0;
1477 }
1478 __buf[__len++] = _Traits::to_char_type(__c);
1479 ++__extracted;
1480 __c = __in.rdbuf()->snextc();
1481 }
1482 __str.append(__buf, __len);
1483
1484 if (_Traits::eq_int_type(__c, __eof))
1485 __err |= __ios_base::eofbit;
1486 __in.width(0);
1487 }
bc2631e0 1488 __catch(__cxxabiv1::__forced_unwind&)
d05f74f1
JM
1489 {
1490 __in._M_setstate(__ios_base::badbit);
1491 __throw_exception_again;
1492 }
bc2631e0 1493 __catch(...)
11202768
PC
1494 {
1495 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1496 // 91. Description of operator>> and getline() for string<>
1497 // might cause endless loop
1498 __in._M_setstate(__ios_base::badbit);
1499 }
1500 }
1501 // 211. operator>>(istream&, string&) doesn't set failbit
1502 if (!__extracted)
1503 __err |= __ios_base::failbit;
1504 if (__err)
1505 __in.setstate(__err);
1506 return __in;
1507 }
1508
1509 template<typename _CharT, typename _Traits, typename _Alloc>
1510 basic_istream<_CharT, _Traits>&
1511 getline(basic_istream<_CharT, _Traits>& __in,
1512 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
1513 {
1514 typedef basic_istream<_CharT, _Traits> __istream_type;
1515 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1516 typedef typename __istream_type::ios_base __ios_base;
1517 typedef typename __istream_type::int_type __int_type;
1518 typedef typename __string_type::size_type __size_type;
1519
1520 __size_type __extracted = 0;
1521 const __size_type __n = __str.max_size();
1522 typename __ios_base::iostate __err = __ios_base::goodbit;
1523 typename __istream_type::sentry __cerb(__in, true);
880b527f 1524 if (__cerb)
11202768 1525 {
bc2631e0 1526 __try
11202768
PC
1527 {
1528 __str.erase();
1529 const __int_type __idelim = _Traits::to_int_type(__delim);
1530 const __int_type __eof = _Traits::eof();
1531 __int_type __c = __in.rdbuf()->sgetc();
1532
1533 while (__extracted < __n
1534 && !_Traits::eq_int_type(__c, __eof)
1535 && !_Traits::eq_int_type(__c, __idelim))
1536 {
1537 __str += _Traits::to_char_type(__c);
1538 ++__extracted;
1539 __c = __in.rdbuf()->snextc();
1540 }
1541
1542 if (_Traits::eq_int_type(__c, __eof))
1543 __err |= __ios_base::eofbit;
1544 else if (_Traits::eq_int_type(__c, __idelim))
1545 {
1546 ++__extracted;
1547 __in.rdbuf()->sbumpc();
1548 }
1549 else
1550 __err |= __ios_base::failbit;
1551 }
bc2631e0 1552 __catch(__cxxabiv1::__forced_unwind&)
d05f74f1
JM
1553 {
1554 __in._M_setstate(__ios_base::badbit);
1555 __throw_exception_again;
1556 }
bc2631e0 1557 __catch(...)
11202768
PC
1558 {
1559 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1560 // 91. Description of operator>> and getline() for string<>
1561 // might cause endless loop
1562 __in._M_setstate(__ios_base::badbit);
1563 }
1564 }
1565 if (!__extracted)
1566 __err |= __ios_base::failbit;
1567 if (__err)
1568 __in.setstate(__err);
1569 return __in;
1570 }
1571
a32e3c09 1572 // Inhibit implicit instantiations for required instantiations,
ed6814f7 1573 // which are defined via explicit instantiations elsewhere.
52e07aa1 1574#if _GLIBCXX_EXTERN_TEMPLATE > 0
a32e3c09 1575 extern template class basic_string<char>;
ed6814f7
BI
1576 extern template
1577 basic_istream<char>&
a32e3c09 1578 operator>>(basic_istream<char>&, string&);
ed6814f7
BI
1579 extern template
1580 basic_ostream<char>&
a32e3c09 1581 operator<<(basic_ostream<char>&, const string&);
ed6814f7
BI
1582 extern template
1583 basic_istream<char>&
a32e3c09 1584 getline(basic_istream<char>&, string&, char);
ed6814f7
BI
1585 extern template
1586 basic_istream<char>&
a32e3c09
BK
1587 getline(basic_istream<char>&, string&);
1588
3d7c150e 1589#ifdef _GLIBCXX_USE_WCHAR_T
a32e3c09 1590 extern template class basic_string<wchar_t>;
ed6814f7
BI
1591 extern template
1592 basic_istream<wchar_t>&
a32e3c09 1593 operator>>(basic_istream<wchar_t>&, wstring&);
ed6814f7
BI
1594 extern template
1595 basic_ostream<wchar_t>&
a32e3c09 1596 operator<<(basic_ostream<wchar_t>&, const wstring&);
ed6814f7
BI
1597 extern template
1598 basic_istream<wchar_t>&
a32e3c09 1599 getline(basic_istream<wchar_t>&, wstring&, wchar_t);
ed6814f7
BI
1600 extern template
1601 basic_istream<wchar_t>&
a32e3c09 1602 getline(basic_istream<wchar_t>&, wstring&);
5112ae3a 1603#endif
1bc8b0ad 1604#endif
3cbc7af0 1605
12ffa228 1606_GLIBCXX_END_NAMESPACE_VERSION
ed4f96af 1607} // namespace std
725dc051 1608
3b0fd4bc 1609#endif