libstdc++
basic_string.tcc
Go to the documentation of this file.
1 // Components for manipulating sequences of characters -*- C++ -*-
2 
3 // Copyright (C) 1997-2021 Free Software Foundation, Inc.
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
8 // Free Software Foundation; either version 3, or (at your option)
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 
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/>.
24 
25 /** @file bits/basic_string.tcc
26  * This is an internal header file, included by other library headers.
27  * Do not attempt to use it directly. @headername{string}
28  */
29 
30 //
31 // ISO C++ 14882: 21 Strings library
32 //
33 
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.
36 // Non-reference-counted implementation written by Paolo Carlini and
37 // updated by Jonathan Wakely for ISO-14882-2011.
38 
39 #ifndef _BASIC_STRING_TCC
40 #define _BASIC_STRING_TCC 1
41 
42 #pragma GCC system_header
43 
44 #include <bits/cxxabi_forced.h>
45 
46 namespace std _GLIBCXX_VISIBILITY(default)
47 {
48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
49 
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
55 
56  template<typename _CharT, typename _Traits, typename _Alloc>
57  void
59  swap(basic_string& __s) _GLIBCXX_NOEXCEPT
60  {
61  if (this == &__s)
62  return;
63 
64  _Alloc_traits::_S_on_swap(_M_get_allocator(), __s._M_get_allocator());
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,
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,
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
281  reserve(size_type __res)
282  {
283  const size_type __capacity = capacity();
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);
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)
310  this->_S_copy(__r, _M_data(), __pos);
311  if (__s && __len2)
312  this->_S_copy(__r + __pos, __s, __len2);
313  if (__how_much)
314  this->_S_copy(__r + __pos + __len2,
315  _M_data() + __pos + __len1, __how_much);
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)
330  this->_S_move(_M_data() + __pos, _M_data() + __pos + __n, __how_much);
331 
332  _M_set_length(length() - __n);
333  }
334 
335  template<typename _CharT, typename _Traits, typename _Alloc>
336  void
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 
370  template<typename _CharT, typename _Traits, typename _Alloc>
371  void
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)
379  this->_M_set_length(__n);
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  {
409  // _GLIBCXX_RESOLVE_LIB_DEFECTS
410  // 2788. unintentionally require a default constructible allocator
411  const basic_string __s(__k1, __k2, this->get_allocator());
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  {
430  pointer __p = this->_M_data() + __pos1;
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)
440  this->_S_assign(this->_M_data() + __pos1, __n2, __c);
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  {
459  pointer __p = this->_M_data() + __pos;
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
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 
515  template<typename _CharT, typename _Traits, typename _Alloc>
516  const typename basic_string<_CharT, _Traits, _Alloc>::size_type
517  basic_string<_CharT, _Traits, _Alloc>::
518  _Rep::_S_max_size = (((npos - sizeof(_Rep_base))/sizeof(_CharT)) - 1) / 4;
519 
520  template<typename _CharT, typename _Traits, typename _Alloc>
521  const _CharT
522  basic_string<_CharT, _Traits, _Alloc>::
523  _Rep::_S_terminal = _CharT();
524 
525  template<typename _CharT, typename _Traits, typename _Alloc>
526  const typename basic_string<_CharT, _Traits, _Alloc>::size_type
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>
532  typename basic_string<_CharT, _Traits, _Alloc>::size_type
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)];
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>
542  template<typename _InIterator>
543  _CharT*
544  basic_string<_CharT, _Traits, _Alloc>::
545  _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
546  input_iterator_tag)
547  {
548 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
549  if (__beg == __end && __a == _Alloc())
550  return _S_empty_rep()._M_refdata();
551 #endif
552  // Avoid reallocation for common case.
553  _CharT __buf[128];
554  size_type __len = 0;
555  while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT))
556  {
557  __buf[__len++] = *__beg;
558  ++__beg;
559  }
560  _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
561  _M_copy(__r->_M_refdata(), __buf, __len);
562  __try
563  {
564  while (__beg != __end)
565  {
566  if (__len == __r->_M_capacity)
567  {
568  // Allocate more space.
569  _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
570  _M_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
571  __r->_M_destroy(__a);
572  __r = __another;
573  }
574  __r->_M_refdata()[__len++] = *__beg;
575  ++__beg;
576  }
577  }
578  __catch(...)
579  {
580  __r->_M_destroy(__a);
581  __throw_exception_again;
582  }
583  __r->_M_set_length_and_sharable(__len);
584  return __r->_M_refdata();
585  }
586 
587  template<typename _CharT, typename _Traits, typename _Alloc>
588  template <typename _InIterator>
589  _CharT*
590  basic_string<_CharT, _Traits, _Alloc>::
591  _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
592  forward_iterator_tag)
593  {
594 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
595  if (__beg == __end && __a == _Alloc())
596  return _S_empty_rep()._M_refdata();
597 #endif
598  // NB: Not required, but considered best practice.
599  if (__gnu_cxx::__is_null_pointer(__beg) && __beg != __end)
600  __throw_logic_error(__N("basic_string::_S_construct null not valid"));
601 
602  const size_type __dnew = static_cast<size_type>(std::distance(__beg,
603  __end));
604  // Check for out_of_range and length_error exceptions.
605  _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
606  __try
607  { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
608  __catch(...)
609  {
610  __r->_M_destroy(__a);
611  __throw_exception_again;
612  }
613  __r->_M_set_length_and_sharable(__dnew);
614  return __r->_M_refdata();
615  }
616 
617  template<typename _CharT, typename _Traits, typename _Alloc>
618  _CharT*
619  basic_string<_CharT, _Traits, _Alloc>::
620  _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
621  {
622 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
623  if (__n == 0 && __a == _Alloc())
624  return _S_empty_rep()._M_refdata();
625 #endif
626  // Check for out_of_range and length_error exceptions.
627  _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
628  if (__n)
629  _M_assign(__r->_M_refdata(), __n, __c);
630 
631  __r->_M_set_length_and_sharable(__n);
632  return __r->_M_refdata();
633  }
634 
635  template<typename _CharT, typename _Traits, typename _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 
645  template<typename _CharT, typename _Traits, typename _Alloc>
647  basic_string(const basic_string& __str, size_type __pos, size_type __n)
648  : _M_dataplus(_S_construct(__str._M_data()
649  + __str._M_check(__pos,
650  "basic_string::basic_string"),
651  __str._M_data() + __str._M_limit(__pos, __n)
652  + __pos, _Alloc()), _Alloc())
653  { }
654 
655  template<typename _CharT, typename _Traits, typename _Alloc>
657  basic_string(const basic_string& __str, size_type __pos,
658  size_type __n, const _Alloc& __a)
659  : _M_dataplus(_S_construct(__str._M_data()
660  + __str._M_check(__pos,
661  "basic_string::basic_string"),
662  __str._M_data() + __str._M_limit(__pos, __n)
663  + __pos, __a), __a)
664  { }
665 
666  template<typename _CharT, typename _Traits, typename _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 
682  template<typename _CharT, typename _Traits, typename _Alloc>
685  assign(const _CharT* __s, size_type __n)
686  {
687  __glibcxx_requires_string_len(__s, __n);
688  _M_check_length(this->size(), __n, "basic_string::assign");
689  if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
690  return _M_replace_safe(size_type(0), this->size(), __s, __n);
691  else
692  {
693  // Work in-place.
694  const size_type __pos = __s - _M_data();
695  if (__pos >= __n)
696  _M_copy(_M_data(), __s, __n);
697  else if (__pos)
698  _M_move(_M_data(), __s, __n);
699  _M_rep()->_M_set_length_and_sharable(__n);
700  return *this;
701  }
702  }
703 
704  template<typename _CharT, typename _Traits, typename _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 
721  template<typename _CharT, typename _Traits, typename _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  {
733  if (_M_disjunct(__s))
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);
744  }
745  return *this;
746  }
747 
748  template<typename _CharT, typename _Traits, typename _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 
765  template<typename _CharT, typename _Traits, typename _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  }
782 
783  template<typename _CharT, typename _Traits, typename _Alloc>
786  insert(size_type __pos, const _CharT* __s, size_type __n)
787  {
788  __glibcxx_requires_string_len(__s, __n);
789  _M_check(__pos, "basic_string::insert");
790  _M_check_length(size_type(0), __n, "basic_string::insert");
791  if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
792  return _M_replace_safe(__pos, size_type(0), __s, __n);
793  else
794  {
795  // Work in-place.
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)
801  _M_copy(__p, __s, __n);
802  else if (__s >= __p)
803  _M_copy(__p, __s + __n, __n);
804  else
805  {
806  const size_type __nleft = __p - __s;
807  _M_copy(__p, __s, __nleft);
808  _M_copy(__p + __nleft, __p + __n, __n - __nleft);
809  }
810  return *this;
811  }
812  }
813 
814  template<typename _CharT, typename _Traits, typename _Alloc>
815  typename basic_string<_CharT, _Traits, _Alloc>::iterator
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 
837  template<typename _CharT, typename _Traits, typename _Alloc>
840  replace(size_type __pos, size_type __n1, const _CharT* __s,
841  size_type __n2)
842  {
843  __glibcxx_requires_string_len(__s, __n2);
844  _M_check(__pos, "basic_string::replace");
845  __n1 = _M_limit(__pos, __n1);
846  _M_check_length(__n1, __n2, "basic_string::replace");
847  bool __left;
848  if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
849  return _M_replace_safe(__pos, __n1, __s, __n2);
850  else if ((__left = __s + __n2 <= _M_data() + __pos)
851  || _M_data() + __pos + __n1 <= __s)
852  {
853  // Work in-place: non-overlapping case.
854  size_type __off = __s - _M_data();
855  __left ? __off : (__off += __n2 - __n1);
856  _M_mutate(__pos, __n1, __n2);
857  _M_copy(_M_data() + __pos, _M_data() + __off, __n2);
858  return *this;
859  }
860  else
861  {
862  // Todo: overlapping case.
863  const basic_string __tmp(__s, __n2);
864  return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2);
865  }
866  }
867 
868  template<typename _CharT, typename _Traits, typename _Alloc>
869  void
871  _M_destroy(const _Alloc& __a) throw ()
872  {
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);
876  }
877 
878  template<typename _CharT, typename _Traits, typename _Alloc>
879  void
880  basic_string<_CharT, _Traits, _Alloc>::
881  _M_leak_hard()
882  {
883 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
884  if (_M_rep() == &_S_empty_rep())
885  return;
886 #endif
887  if (_M_rep()->_M_is_shared())
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  {
897  const size_type __old_size = this->size();
898  const size_type __new_size = __old_size + __len2 - __len1;
899  const size_type __how_much = __old_size - __pos - __len1;
900 
901  if (__new_size > this->capacity() || _M_rep()->_M_is_shared())
902  {
903  // Must reallocate.
904  const allocator_type __a = get_allocator();
905  _Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a);
906 
907  if (__pos)
908  _M_copy(__r->_M_refdata(), _M_data(), __pos);
909  if (__how_much)
910  _M_copy(__r->_M_refdata() + __pos + __len2,
911  _M_data() + __pos + __len1, __how_much);
912 
913  _M_rep()->_M_dispose(__a);
914  _M_data(__r->_M_refdata());
915  }
916  else if (__how_much && __len1 != __len2)
917  {
918  // Work in-place.
919  _M_move(_M_data() + __pos + __len2,
920  _M_data() + __pos + __len1, __how_much);
921  }
922  _M_rep()->_M_set_length_and_sharable(__new_size);
923  }
924 
925  template<typename _CharT, typename _Traits, typename _Alloc>
926  void
928  reserve(size_type __res)
929  {
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);
949  }
950 
951  template<typename _CharT, typename _Traits, typename _Alloc>
952  void
954  swap(basic_string& __s)
956  {
957  if (_M_rep()->_M_is_leaked())
958  _M_rep()->_M_set_sharable();
959  if (__s._M_rep()->_M_is_leaked())
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.
968  else
969  {
970  const basic_string __tmp1(_M_ibegin(), _M_iend(),
971  __s.get_allocator());
972  const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
973  this->get_allocator());
974  *this = __tmp2;
975  __s = __tmp1;
976  }
977  }
978 
979  template<typename _CharT, typename _Traits, typename _Alloc>
982  _S_create(size_type __capacity, size_type __old_capacity,
983  const _Alloc& __alloc)
984  {
985  // _GLIBCXX_RESOLVE_LIB_DEFECTS
986  // 83. String::npos vs. string::max_size()
987  if (__capacity > _S_max_size)
988  __throw_length_error(__N("basic_string::_S_create"));
989 
990  // The standard places no restriction on allocating more memory
991  // than is strictly needed within this layer at the moment or as
992  // requested by an explicit application call to reserve(n).
993 
994  // Many malloc implementations perform quite poorly when an
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.
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).
1013  const size_type __pagesize = 4096;
1014  const size_type __malloc_header_size = 4 * sizeof(void*);
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
1022  if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
1023  __capacity = 2 * __old_capacity;
1024 
1025  // NB: Need an array of char_type[__capacity], plus a terminating
1026  // null char_type() element, plus enough for the _Rep data structure.
1027  // Whew. Seemingly so needy, yet so elemental.
1028  size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
1029 
1030  const size_type __adj_size = __size + __malloc_header_size;
1031  if (__adj_size > __pagesize && __capacity > __old_capacity)
1032  {
1033  const size_type __extra = __pagesize - __adj_size % __pagesize;
1034  __capacity += __extra / sizeof(_CharT);
1035  // Never allocate a string bigger than _S_max_size.
1036  if (__capacity > _S_max_size)
1037  __capacity = _S_max_size;
1038  __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
1039  }
1040 
1041  // NB: Might throw, but no worries about a leak, mate: _Rep()
1042  // does not throw.
1043  void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
1044  _Rep *__p = new (__place) _Rep;
1045  __p->_M_capacity = __capacity;
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();
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  {
1062  // Requested capacity of the clone.
1063  const size_type __requested_cap = this->_M_length + __res;
1064  _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity,
1065  __alloc);
1066  if (this->_M_length)
1067  _M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length);
1068 
1069  __r->_M_set_length_and_sharable(this->_M_length);
1070  return __r->_M_refdata();
1071  }
1072 
1073  template<typename _CharT, typename _Traits, typename _Alloc>
1074  void
1076  resize(size_type __n, _CharT __c)
1077  {
1078  const size_type __size = this->size();
1079  _M_check_length(__size, __n, "basic_string::resize");
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  }
1086 
1087  template<typename _CharT, typename _Traits, typename _Alloc>
1088  template<typename _InputIterator>
1091  _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
1092  _InputIterator __k2, __false_type)
1093  {
1094  const basic_string __s(__k1, __k2);
1095  const size_type __n1 = __i2 - __i1;
1096  _M_check_length(__n1, __s.size(), "basic_string::_M_replace_dispatch");
1097  return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
1098  __s.size());
1099  }
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  }
1125 
1126  template<typename _CharT, typename _Traits, typename _Alloc>
1127  void
1129  reserve()
1130  {
1131 #if __cpp_exceptions
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 */ }
1144 #endif
1145  }
1146 
1147  template<typename _CharT, typename _Traits, typename _Alloc>
1148  typename basic_string<_CharT, _Traits, _Alloc>::size_type
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
1161 
1162  template<typename _CharT, typename _Traits, typename _Alloc>
1164  operator+(const _CharT* __lhs,
1166  {
1167  __glibcxx_requires_string(__lhs);
1168  typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1169  typedef typename __string_type::size_type __size_type;
1171  rebind<_CharT>::other _Char_alloc_type;
1172  typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits;
1173  const __size_type __len = _Traits::length(__lhs);
1174  __string_type __str(_Alloc_traits::_S_select_on_copy(
1175  __rhs.get_allocator()));
1176  __str.reserve(__len + __rhs.size());
1177  __str.append(__lhs, __len);
1178  __str.append(__rhs);
1179  return __str;
1180  }
1181 
1182  template<typename _CharT, typename _Traits, typename _Alloc>
1183  basic_string<_CharT, _Traits, _Alloc>
1185  {
1186  typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1187  typedef typename __string_type::size_type __size_type;
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()));
1193  const __size_type __len = __rhs.size();
1194  __str.reserve(__len + 1);
1195  __str.append(__size_type(1), __lhs);
1196  __str.append(__rhs);
1197  return __str;
1198  }
1199 
1200  template<typename _CharT, typename _Traits, typename _Alloc>
1201  typename basic_string<_CharT, _Traits, _Alloc>::size_type
1203  find(const _CharT* __s, size_type __pos, size_type __n) const
1204  _GLIBCXX_NOEXCEPT
1205  {
1206  __glibcxx_requires_string_len(__s, __n);
1207  const size_type __size = this->size();
1208 
1209  if (__n == 0)
1210  return __pos <= __size ? __pos : npos;
1211  if (__pos >= __size)
1212  return npos;
1213 
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)
1221  {
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;
1232  }
1233  return npos;
1234  }
1235 
1236  template<typename _CharT, typename _Traits, typename _Alloc>
1237  typename basic_string<_CharT, _Traits, _Alloc>::size_type
1239  find(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
1240  {
1241  size_type __ret = npos;
1242  const size_type __size = this->size();
1243  if (__pos < __size)
1244  {
1245  const _CharT* __data = _M_data();
1246  const size_type __n = __size - __pos;
1247  const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
1248  if (__p)
1249  __ret = __p - __data;
1250  }
1251  return __ret;
1252  }
1253 
1254  template<typename _CharT, typename _Traits, typename _Alloc>
1255  typename basic_string<_CharT, _Traits, _Alloc>::size_type
1257  rfind(const _CharT* __s, size_type __pos, size_type __n) const
1258  _GLIBCXX_NOEXCEPT
1259  {
1260  __glibcxx_requires_string_len(__s, __n);
1261  const size_type __size = this->size();
1262  if (__n <= __size)
1263  {
1264  __pos = std::min(size_type(__size - __n), __pos);
1265  const _CharT* __data = _M_data();
1266  do
1267  {
1268  if (traits_type::compare(__data + __pos, __s, __n) == 0)
1269  return __pos;
1270  }
1271  while (__pos-- > 0);
1272  }
1273  return npos;
1274  }
1275 
1276  template<typename _CharT, typename _Traits, typename _Alloc>
1277  typename basic_string<_CharT, _Traits, _Alloc>::size_type
1279  rfind(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
1280  {
1281  size_type __size = this->size();
1282  if (__size)
1283  {
1284  if (--__size > __pos)
1285  __size = __pos;
1286  for (++__size; __size-- > 0; )
1287  if (traits_type::eq(_M_data()[__size], __c))
1288  return __size;
1289  }
1290  return npos;
1291  }
1292 
1293  template<typename _CharT, typename _Traits, typename _Alloc>
1294  typename basic_string<_CharT, _Traits, _Alloc>::size_type
1296  find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
1297  _GLIBCXX_NOEXCEPT
1298  {
1299  __glibcxx_requires_string_len(__s, __n);
1300  for (; __n && __pos < this->size(); ++__pos)
1301  {
1302  const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
1303  if (__p)
1304  return __pos;
1305  }
1306  return npos;
1307  }
1308 
1309  template<typename _CharT, typename _Traits, typename _Alloc>
1310  typename basic_string<_CharT, _Traits, _Alloc>::size_type
1312  find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
1313  _GLIBCXX_NOEXCEPT
1314  {
1315  __glibcxx_requires_string_len(__s, __n);
1316  size_type __size = this->size();
1317  if (__size && __n)
1318  {
1319  if (--__size > __pos)
1320  __size = __pos;
1321  do
1322  {
1323  if (traits_type::find(__s, __n, _M_data()[__size]))
1324  return __size;
1325  }
1326  while (__size-- != 0);
1327  }
1328  return npos;
1329  }
1330 
1331  template<typename _CharT, typename _Traits, typename _Alloc>
1332  typename basic_string<_CharT, _Traits, _Alloc>::size_type
1334  find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
1335  _GLIBCXX_NOEXCEPT
1336  {
1337  __glibcxx_requires_string_len(__s, __n);
1338  for (; __pos < this->size(); ++__pos)
1339  if (!traits_type::find(__s, __n, _M_data()[__pos]))
1340  return __pos;
1341  return npos;
1342  }
1343 
1344  template<typename _CharT, typename _Traits, typename _Alloc>
1345  typename basic_string<_CharT, _Traits, _Alloc>::size_type
1347  find_first_not_of(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
1348  {
1349  for (; __pos < this->size(); ++__pos)
1350  if (!traits_type::eq(_M_data()[__pos], __c))
1351  return __pos;
1352  return npos;
1353  }
1354 
1355  template<typename _CharT, typename _Traits, typename _Alloc>
1356  typename basic_string<_CharT, _Traits, _Alloc>::size_type
1358  find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
1359  _GLIBCXX_NOEXCEPT
1360  {
1361  __glibcxx_requires_string_len(__s, __n);
1362  size_type __size = this->size();
1363  if (__size)
1364  {
1365  if (--__size > __pos)
1366  __size = __pos;
1367  do
1368  {
1369  if (!traits_type::find(__s, __n, _M_data()[__size]))
1370  return __size;
1371  }
1372  while (__size--);
1373  }
1374  return npos;
1375  }
1376 
1377  template<typename _CharT, typename _Traits, typename _Alloc>
1378  typename basic_string<_CharT, _Traits, _Alloc>::size_type
1380  find_last_not_of(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
1381  {
1382  size_type __size = this->size();
1383  if (__size)
1384  {
1385  if (--__size > __pos)
1386  __size = __pos;
1387  do
1388  {
1389  if (!traits_type::eq(_M_data()[__size], __c))
1390  return __size;
1391  }
1392  while (__size--);
1393  }
1394  return npos;
1395  }
1396 
1397  template<typename _CharT, typename _Traits, typename _Alloc>
1398  int
1400  compare(size_type __pos, size_type __n, const basic_string& __str) const
1401  {
1402  _M_check(__pos, "basic_string::compare");
1403  __n = _M_limit(__pos, __n);
1404  const size_type __osize = __str.size();
1405  const size_type __len = std::min(__n, __osize);
1406  int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
1407  if (!__r)
1408  __r = _S_compare(__n, __osize);
1409  return __r;
1410  }
1411 
1412  template<typename _CharT, typename _Traits, typename _Alloc>
1413  int
1415  compare(size_type __pos1, size_type __n1, const basic_string& __str,
1416  size_type __pos2, size_type __n2) const
1417  {
1418  _M_check(__pos1, "basic_string::compare");
1419  __str._M_check(__pos2, "basic_string::compare");
1420  __n1 = _M_limit(__pos1, __n1);
1421  __n2 = __str._M_limit(__pos2, __n2);
1422  const size_type __len = std::min(__n1, __n2);
1423  int __r = traits_type::compare(_M_data() + __pos1,
1424  __str.data() + __pos2, __len);
1425  if (!__r)
1426  __r = _S_compare(__n1, __n2);
1427  return __r;
1428  }
1429 
1430  template<typename _CharT, typename _Traits, typename _Alloc>
1431  int
1433  compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT
1434  {
1435  __glibcxx_requires_string(__s);
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);
1439  int __r = traits_type::compare(_M_data(), __s, __len);
1440  if (!__r)
1441  __r = _S_compare(__size, __osize);
1442  return __r;
1443  }
1444 
1445  template<typename _CharT, typename _Traits, typename _Alloc>
1446  int
1448  compare(size_type __pos, size_type __n1, const _CharT* __s) const
1449  {
1450  __glibcxx_requires_string(__s);
1451  _M_check(__pos, "basic_string::compare");
1452  __n1 = _M_limit(__pos, __n1);
1453  const size_type __osize = traits_type::length(__s);
1454  const size_type __len = std::min(__n1, __osize);
1455  int __r = traits_type::compare(_M_data() + __pos, __s, __len);
1456  if (!__r)
1457  __r = _S_compare(__n1, __osize);
1458  return __r;
1459  }
1460 
1461  template<typename _CharT, typename _Traits, typename _Alloc>
1462  int
1464  compare(size_type __pos, size_type __n1, const _CharT* __s,
1465  size_type __n2) const
1466  {
1467  __glibcxx_requires_string_len(__s, __n2);
1468  _M_check(__pos, "basic_string::compare");
1469  __n1 = _M_limit(__pos, __n1);
1470  const size_type __len = std::min(__n1, __n2);
1471  int __r = traits_type::compare(_M_data() + __pos, __s, __len);
1472  if (!__r)
1473  __r = _S_compare(__n1, __n2);
1474  return __r;
1475  }
1476 
1477  // 21.3.7.9 basic_string::getline and operators
1478  template<typename _CharT, typename _Traits, typename _Alloc>
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);
1494  if (__cerb)
1495  {
1496  __try
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 
1525  if (__extracted < __n && _Traits::eq_int_type(__c, __eof))
1526  __err |= __ios_base::eofbit;
1527  __in.width(0);
1528  }
1529  __catch(__cxxabiv1::__forced_unwind&)
1530  {
1531  __in._M_setstate(__ios_base::badbit);
1532  __throw_exception_again;
1533  }
1534  __catch(...)
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>&
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);
1565  if (__cerb)
1566  {
1567  __try
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  }
1593  __catch(__cxxabiv1::__forced_unwind&)
1594  {
1595  __in._M_setstate(__ios_base::badbit);
1596  __throw_exception_again;
1597  }
1598  __catch(...)
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 
1613  // Inhibit implicit instantiations for required instantiations,
1614  // which are defined via explicit instantiations elsewhere.
1615 #if _GLIBCXX_EXTERN_TEMPLATE
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.
1621 # if __cplusplus <= 201703L && _GLIBCXX_EXTERN_TEMPLATE > 0
1622  extern template class basic_string<char>;
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 
1630  extern template
1631  basic_istream<char>&
1632  operator>>(basic_istream<char>&, string&);
1633  extern template
1634  basic_ostream<char>&
1635  operator<<(basic_ostream<char>&, const string&);
1636  extern template
1637  basic_istream<char>&
1638  getline(basic_istream<char>&, string&, char);
1639  extern template
1640  basic_istream<char>&
1641  getline(basic_istream<char>&, string&);
1642 
1643 #ifdef _GLIBCXX_USE_WCHAR_T
1644 # if __cplusplus <= 201703L && _GLIBCXX_EXTERN_TEMPLATE > 0
1645  extern template class basic_string<wchar_t>;
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 
1651  extern template
1652  basic_istream<wchar_t>&
1653  operator>>(basic_istream<wchar_t>&, wstring&);
1654  extern template
1655  basic_ostream<wchar_t>&
1656  operator<<(basic_ostream<wchar_t>&, const wstring&);
1657  extern template
1658  basic_istream<wchar_t>&
1659  getline(basic_istream<wchar_t>&, wstring&, wchar_t);
1660  extern template
1661  basic_istream<wchar_t>&
1662  getline(basic_istream<wchar_t>&, wstring&);
1663 #endif // _GLIBCXX_USE_WCHAR_T
1664 #endif // _GLIBCXX_EXTERN_TEMPLATE
1665 
1666 _GLIBCXX_END_NAMESPACE_VERSION
1667 } // namespace std
1668 
1669 #endif
constexpr complex< _Tp > operator+(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x plus y.
Definition: complex:332
_Tp * begin(valarray< _Tp > &__va)
Return an iterator pointing to the first element of the valarray.
Definition: valarray:1214
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Definition: stl_algobase.h:230
basic_string< wchar_t > wstring
A string of wchar_t.
Definition: stringfwd.h:83
ISO C++ entities toplevel namespace is std.
std::basic_istream< _CharT, _Traits > & operator>>(std::basic_istream< _CharT, _Traits > &__is, bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition: bitset:1472
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
Definition: postypes.h:98
basic_istream< _CharT, _Traits > & getline(basic_istream< _CharT, _Traits > &__is, basic_string< _CharT, _Traits, _Alloc > &__str, _CharT __delim)
Read a line from stream into a string.
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
Definition: range_access.h:244
constexpr auto data(_Container &__cont) noexcept(noexcept(__cont.data())) -> decltype(__cont.data())
Return the data pointer of a container.
Definition: range_access.h:289
basic_streambuf< _CharT, _Traits > * rdbuf() const
Accessing the underlying buffer.
Definition: basic_ios.h:321
void setstate(iostate __state)
Sets additional flags in the error state.
Definition: basic_ios.h:157
Template class basic_istream.
Definition: istream:59
Uniform interface to all allocator types.
Managing sequences of characters and character-like objects.
void swap(basic_string &__s) noexcept(/*conditional */)
Swap contents with another string.
size_type find_first_of(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a character of string.
size_type find(const _CharT *__s, size_type __pos, size_type __n) const noexcept
Find position of a C substring.
size_type find_last_not_of(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a character not in string.
int compare(const basic_string &__str) const
Compare to a string.
size_type find_first_not_of(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a character not in string.
void insert(iterator __p, size_type __n, _CharT __c)
Insert multiple characters.
basic_string & assign(const basic_string &__str)
Set value to contents of another string.
basic_string & replace(size_type __pos, size_type __n, const basic_string &__str)
Replace characters with value from another string.
size_type copy(_CharT *__s, size_type __n, size_type __pos=0) const
Copy substring into C string.
size_type find_last_of(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a character of string.
size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
size_type rfind(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a string.
void resize(size_type __n, _CharT __c)
Resizes the string to the specified number of characters.
void reserve()
Equivalent to shrink_to_fit().
const _CharT * data() const noexcept
Return const pointer to contents.
basic_string & append(const basic_string &__str)
Append a string to this string.
static const size_type npos
Value returned by various member functions when they fail.
allocator_type get_allocator() const noexcept
Return copy of allocator used to construct this string.
basic_string & erase(size_type __pos=0, size_type __n=npos)
Remove characters.
basic_string() noexcept
Default constructor creates an empty string.
size_type max_size() const noexcept
Returns the size() of the largest possible string.
Thrown as part of forced unwinding.
Definition: cxxabi_forced.h:49
streamsize width() const
Flags access.
Definition: ios_base.h:742
locale getloc() const
Locale access.
Definition: ios_base.h:793
Primary class template ctype facet.
Marking input iterators.
Forward iterators support a superset of input iterator operations.
Common iterator class.
Uniform interface to C++98 and C++11 allocators.