libstdc++
basic_string.tcc
Go to the documentation of this file.
1 // Components for manipulating sequences of characters -*- C++ -*-
2 
3 // Copyright (C) 1997-2020 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  // 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());
318 
319  if (__pos)
320  this->_S_copy(__r, _M_data(), __pos);
321  if (__s && __len2)
322  this->_S_copy(__r + __pos, __s, __len2);
323  if (__how_much)
324  this->_S_copy(__r + __pos + __len2,
325  _M_data() + __pos + __len1, __how_much);
326 
327  _M_dispose();
328  _M_data(__r);
329  _M_capacity(__new_capacity);
330  }
331 
332  template<typename _CharT, typename _Traits, typename _Alloc>
333  void
334  basic_string<_CharT, _Traits, _Alloc>::
335  _M_erase(size_type __pos, size_type __n)
336  {
337  const size_type __how_much = length() - __pos - __n;
338 
339  if (__how_much && __n)
340  this->_S_move(_M_data() + __pos, _M_data() + __pos + __n, __how_much);
341 
342  _M_set_length(length() - __n);
343  }
344 
345  template<typename _CharT, typename _Traits, typename _Alloc>
346  void
348  resize(size_type __n, _CharT __c)
349  {
350  const size_type __size = this->size();
351  if (__size < __n)
352  this->append(__n - __size, __c);
353  else if (__n < __size)
354  this->_M_set_length(__n);
355  }
356 
357  template<typename _CharT, typename _Traits, typename _Alloc>
358  basic_string<_CharT, _Traits, _Alloc>&
359  basic_string<_CharT, _Traits, _Alloc>::
360  _M_append(const _CharT* __s, size_type __n)
361  {
362  const size_type __len = __n + this->size();
363 
364  if (__len <= this->capacity())
365  {
366  if (__n)
367  this->_S_copy(this->_M_data() + this->size(), __s, __n);
368  }
369  else
370  this->_M_mutate(this->size(), size_type(0), __s, __n);
371 
372  this->_M_set_length(__len);
373  return *this;
374  }
375 
376  template<typename _CharT, typename _Traits, typename _Alloc>
377  template<typename _InputIterator>
378  basic_string<_CharT, _Traits, _Alloc>&
379  basic_string<_CharT, _Traits, _Alloc>::
380  _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
381  _InputIterator __k1, _InputIterator __k2,
382  std::__false_type)
383  {
384  // _GLIBCXX_RESOLVE_LIB_DEFECTS
385  // 2788. unintentionally require a default constructible allocator
386  const basic_string __s(__k1, __k2, this->get_allocator());
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  {
405  pointer __p = this->_M_data() + __pos1;
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)
415  this->_S_assign(this->_M_data() + __pos1, __n2, __c);
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  {
434  pointer __p = this->_M_data() + __pos;
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
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 
490  template<typename _CharT, typename _Traits, typename _Alloc>
491  const typename basic_string<_CharT, _Traits, _Alloc>::size_type
492  basic_string<_CharT, _Traits, _Alloc>::
493  _Rep::_S_max_size = (((npos - sizeof(_Rep_base))/sizeof(_CharT)) - 1) / 4;
494 
495  template<typename _CharT, typename _Traits, typename _Alloc>
496  const _CharT
497  basic_string<_CharT, _Traits, _Alloc>::
498  _Rep::_S_terminal = _CharT();
499 
500  template<typename _CharT, typename _Traits, typename _Alloc>
501  const typename basic_string<_CharT, _Traits, _Alloc>::size_type
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>
507  typename basic_string<_CharT, _Traits, _Alloc>::size_type
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)];
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>
517  template<typename _InIterator>
518  _CharT*
519  basic_string<_CharT, _Traits, _Alloc>::
520  _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
521  input_iterator_tag)
522  {
523 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
524  if (__beg == __end && __a == _Alloc())
525  return _S_empty_rep()._M_refdata();
526 #endif
527  // Avoid reallocation for common case.
528  _CharT __buf[128];
529  size_type __len = 0;
530  while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT))
531  {
532  __buf[__len++] = *__beg;
533  ++__beg;
534  }
535  _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
536  _M_copy(__r->_M_refdata(), __buf, __len);
537  __try
538  {
539  while (__beg != __end)
540  {
541  if (__len == __r->_M_capacity)
542  {
543  // Allocate more space.
544  _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
545  _M_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
546  __r->_M_destroy(__a);
547  __r = __another;
548  }
549  __r->_M_refdata()[__len++] = *__beg;
550  ++__beg;
551  }
552  }
553  __catch(...)
554  {
555  __r->_M_destroy(__a);
556  __throw_exception_again;
557  }
558  __r->_M_set_length_and_sharable(__len);
559  return __r->_M_refdata();
560  }
561 
562  template<typename _CharT, typename _Traits, typename _Alloc>
563  template <typename _InIterator>
564  _CharT*
565  basic_string<_CharT, _Traits, _Alloc>::
566  _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
567  forward_iterator_tag)
568  {
569 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
570  if (__beg == __end && __a == _Alloc())
571  return _S_empty_rep()._M_refdata();
572 #endif
573  // NB: Not required, but considered best practice.
574  if (__gnu_cxx::__is_null_pointer(__beg) && __beg != __end)
575  __throw_logic_error(__N("basic_string::_S_construct null not valid"));
576 
577  const size_type __dnew = static_cast<size_type>(std::distance(__beg,
578  __end));
579  // Check for out_of_range and length_error exceptions.
580  _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
581  __try
582  { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
583  __catch(...)
584  {
585  __r->_M_destroy(__a);
586  __throw_exception_again;
587  }
588  __r->_M_set_length_and_sharable(__dnew);
589  return __r->_M_refdata();
590  }
591 
592  template<typename _CharT, typename _Traits, typename _Alloc>
593  _CharT*
594  basic_string<_CharT, _Traits, _Alloc>::
595  _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
596  {
597 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
598  if (__n == 0 && __a == _Alloc())
599  return _S_empty_rep()._M_refdata();
600 #endif
601  // Check for out_of_range and length_error exceptions.
602  _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
603  if (__n)
604  _M_assign(__r->_M_refdata(), __n, __c);
605 
606  __r->_M_set_length_and_sharable(__n);
607  return __r->_M_refdata();
608  }
609 
610  template<typename _CharT, typename _Traits, typename _Alloc>
613  : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(__str.get_allocator()),
614  __str.get_allocator()),
615  __str.get_allocator())
616  { }
617 
618  template<typename _CharT, typename _Traits, typename _Alloc>
620  basic_string(const _Alloc& __a)
621  : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a)
622  { }
623 
624  template<typename _CharT, typename _Traits, typename _Alloc>
626  basic_string(const basic_string& __str, size_type __pos, const _Alloc& __a)
627  : _M_dataplus(_S_construct(__str._M_data()
628  + __str._M_check(__pos,
629  "basic_string::basic_string"),
630  __str._M_data() + __str._M_limit(__pos, npos)
631  + __pos, __a), __a)
632  { }
633 
634  template<typename _CharT, typename _Traits, typename _Alloc>
636  basic_string(const basic_string& __str, size_type __pos, size_type __n)
637  : _M_dataplus(_S_construct(__str._M_data()
638  + __str._M_check(__pos,
639  "basic_string::basic_string"),
640  __str._M_data() + __str._M_limit(__pos, __n)
641  + __pos, _Alloc()), _Alloc())
642  { }
643 
644  template<typename _CharT, typename _Traits, typename _Alloc>
646  basic_string(const basic_string& __str, size_type __pos,
647  size_type __n, const _Alloc& __a)
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, __a), __a)
653  { }
654 
655  // TBD: DPG annotate
656  template<typename _CharT, typename _Traits, typename _Alloc>
658  basic_string(const _CharT* __s, size_type __n, const _Alloc& __a)
659  : _M_dataplus(_S_construct(__s, __s + __n, __a), __a)
660  { }
661 
662  // TBD: DPG annotate
663  template<typename _CharT, typename _Traits, typename _Alloc>
665  basic_string(const _CharT* __s, const _Alloc& __a)
666  : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) :
667  __s + npos, __a), __a)
668  { }
669 
670  template<typename _CharT, typename _Traits, typename _Alloc>
672  basic_string(size_type __n, _CharT __c, const _Alloc& __a)
673  : _M_dataplus(_S_construct(__n, __c, __a), __a)
674  { }
675 
676  // TBD: DPG annotate
677  template<typename _CharT, typename _Traits, typename _Alloc>
678  template<typename _InputIterator>
680  basic_string(_InputIterator __beg, _InputIterator __end, const _Alloc& __a)
681  : _M_dataplus(_S_construct(__beg, __end, __a), __a)
682  { }
683 
684 #if __cplusplus >= 201103L
685  template<typename _CharT, typename _Traits, typename _Alloc>
688  : _M_dataplus(_S_construct(__l.begin(), __l.end(), __a), __a)
689  { }
690 #endif
691 
692  template<typename _CharT, typename _Traits, typename _Alloc>
695  assign(const basic_string& __str)
696  {
697  if (_M_rep() != __str._M_rep())
698  {
699  // XXX MT
700  const allocator_type __a = this->get_allocator();
701  _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());
702  _M_rep()->_M_dispose(__a);
703  _M_data(__tmp);
704  }
705  return *this;
706  }
707 
708  template<typename _CharT, typename _Traits, typename _Alloc>
711  assign(const _CharT* __s, size_type __n)
712  {
713  __glibcxx_requires_string_len(__s, __n);
714  _M_check_length(this->size(), __n, "basic_string::assign");
715  if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
716  return _M_replace_safe(size_type(0), this->size(), __s, __n);
717  else
718  {
719  // Work in-place.
720  const size_type __pos = __s - _M_data();
721  if (__pos >= __n)
722  _M_copy(_M_data(), __s, __n);
723  else if (__pos)
724  _M_move(_M_data(), __s, __n);
725  _M_rep()->_M_set_length_and_sharable(__n);
726  return *this;
727  }
728  }
729 
730  template<typename _CharT, typename _Traits, typename _Alloc>
733  append(size_type __n, _CharT __c)
734  {
735  if (__n)
736  {
737  _M_check_length(size_type(0), __n, "basic_string::append");
738  const size_type __len = __n + this->size();
739  if (__len > this->capacity() || _M_rep()->_M_is_shared())
740  this->reserve(__len);
741  _M_assign(_M_data() + this->size(), __n, __c);
742  _M_rep()->_M_set_length_and_sharable(__len);
743  }
744  return *this;
745  }
746 
747  template<typename _CharT, typename _Traits, typename _Alloc>
750  append(const _CharT* __s, size_type __n)
751  {
752  __glibcxx_requires_string_len(__s, __n);
753  if (__n)
754  {
755  _M_check_length(size_type(0), __n, "basic_string::append");
756  const size_type __len = __n + this->size();
757  if (__len > this->capacity() || _M_rep()->_M_is_shared())
758  {
759  if (_M_disjunct(__s))
760  this->reserve(__len);
761  else
762  {
763  const size_type __off = __s - _M_data();
764  this->reserve(__len);
765  __s = _M_data() + __off;
766  }
767  }
768  _M_copy(_M_data() + this->size(), __s, __n);
769  _M_rep()->_M_set_length_and_sharable(__len);
770  }
771  return *this;
772  }
773 
774  template<typename _CharT, typename _Traits, typename _Alloc>
777  append(const basic_string& __str)
778  {
779  const size_type __size = __str.size();
780  if (__size)
781  {
782  const size_type __len = __size + this->size();
783  if (__len > this->capacity() || _M_rep()->_M_is_shared())
784  this->reserve(__len);
785  _M_copy(_M_data() + this->size(), __str._M_data(), __size);
786  _M_rep()->_M_set_length_and_sharable(__len);
787  }
788  return *this;
789  }
790 
791  template<typename _CharT, typename _Traits, typename _Alloc>
794  append(const basic_string& __str, size_type __pos, size_type __n)
795  {
796  __str._M_check(__pos, "basic_string::append");
797  __n = __str._M_limit(__pos, __n);
798  if (__n)
799  {
800  const size_type __len = __n + this->size();
801  if (__len > this->capacity() || _M_rep()->_M_is_shared())
802  this->reserve(__len);
803  _M_copy(_M_data() + this->size(), __str._M_data() + __pos, __n);
804  _M_rep()->_M_set_length_and_sharable(__len);
805  }
806  return *this;
807  }
808 
809  template<typename _CharT, typename _Traits, typename _Alloc>
812  insert(size_type __pos, const _CharT* __s, size_type __n)
813  {
814  __glibcxx_requires_string_len(__s, __n);
815  _M_check(__pos, "basic_string::insert");
816  _M_check_length(size_type(0), __n, "basic_string::insert");
817  if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
818  return _M_replace_safe(__pos, size_type(0), __s, __n);
819  else
820  {
821  // Work in-place.
822  const size_type __off = __s - _M_data();
823  _M_mutate(__pos, 0, __n);
824  __s = _M_data() + __off;
825  _CharT* __p = _M_data() + __pos;
826  if (__s + __n <= __p)
827  _M_copy(__p, __s, __n);
828  else if (__s >= __p)
829  _M_copy(__p, __s + __n, __n);
830  else
831  {
832  const size_type __nleft = __p - __s;
833  _M_copy(__p, __s, __nleft);
834  _M_copy(__p + __nleft, __p + __n, __n - __nleft);
835  }
836  return *this;
837  }
838  }
839 
840  template<typename _CharT, typename _Traits, typename _Alloc>
841  typename basic_string<_CharT, _Traits, _Alloc>::iterator
843  erase(iterator __first, iterator __last)
844  {
845  _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
846  && __last <= _M_iend());
847 
848  // NB: This isn't just an optimization (bail out early when
849  // there is nothing to do, really), it's also a correctness
850  // issue vs MT, see libstdc++/40518.
851  const size_type __size = __last - __first;
852  if (__size)
853  {
854  const size_type __pos = __first - _M_ibegin();
855  _M_mutate(__pos, __size, size_type(0));
856  _M_rep()->_M_set_leaked();
857  return iterator(_M_data() + __pos);
858  }
859  else
860  return __first;
861  }
862 
863  template<typename _CharT, typename _Traits, typename _Alloc>
866  replace(size_type __pos, size_type __n1, const _CharT* __s,
867  size_type __n2)
868  {
869  __glibcxx_requires_string_len(__s, __n2);
870  _M_check(__pos, "basic_string::replace");
871  __n1 = _M_limit(__pos, __n1);
872  _M_check_length(__n1, __n2, "basic_string::replace");
873  bool __left;
874  if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
875  return _M_replace_safe(__pos, __n1, __s, __n2);
876  else if ((__left = __s + __n2 <= _M_data() + __pos)
877  || _M_data() + __pos + __n1 <= __s)
878  {
879  // Work in-place: non-overlapping case.
880  size_type __off = __s - _M_data();
881  __left ? __off : (__off += __n2 - __n1);
882  _M_mutate(__pos, __n1, __n2);
883  _M_copy(_M_data() + __pos, _M_data() + __off, __n2);
884  return *this;
885  }
886  else
887  {
888  // Todo: overlapping case.
889  const basic_string __tmp(__s, __n2);
890  return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2);
891  }
892  }
893 
894  template<typename _CharT, typename _Traits, typename _Alloc>
895  void
897  _M_destroy(const _Alloc& __a) throw ()
898  {
899  const size_type __size = sizeof(_Rep_base) +
900  (this->_M_capacity + 1) * sizeof(_CharT);
901  _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);
902  }
903 
904  template<typename _CharT, typename _Traits, typename _Alloc>
905  void
906  basic_string<_CharT, _Traits, _Alloc>::
907  _M_leak_hard()
908  {
909 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
910  if (_M_rep() == &_S_empty_rep())
911  return;
912 #endif
913  if (_M_rep()->_M_is_shared())
914  _M_mutate(0, 0, 0);
915  _M_rep()->_M_set_leaked();
916  }
917 
918  template<typename _CharT, typename _Traits, typename _Alloc>
919  void
920  basic_string<_CharT, _Traits, _Alloc>::
921  _M_mutate(size_type __pos, size_type __len1, size_type __len2)
922  {
923  const size_type __old_size = this->size();
924  const size_type __new_size = __old_size + __len2 - __len1;
925  const size_type __how_much = __old_size - __pos - __len1;
926 
927  if (__new_size > this->capacity() || _M_rep()->_M_is_shared())
928  {
929  // Must reallocate.
930  const allocator_type __a = get_allocator();
931  _Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a);
932 
933  if (__pos)
934  _M_copy(__r->_M_refdata(), _M_data(), __pos);
935  if (__how_much)
936  _M_copy(__r->_M_refdata() + __pos + __len2,
937  _M_data() + __pos + __len1, __how_much);
938 
939  _M_rep()->_M_dispose(__a);
940  _M_data(__r->_M_refdata());
941  }
942  else if (__how_much && __len1 != __len2)
943  {
944  // Work in-place.
945  _M_move(_M_data() + __pos + __len2,
946  _M_data() + __pos + __len1, __how_much);
947  }
948  _M_rep()->_M_set_length_and_sharable(__new_size);
949  }
950 
951  template<typename _CharT, typename _Traits, typename _Alloc>
952  void
953  basic_string<_CharT, _Traits, _Alloc>::
954  reserve(size_type __res)
955  {
956  if (__res != this->capacity() || _M_rep()->_M_is_shared())
957  {
958  // Make sure we don't shrink below the current size
959  if (__res < this->size())
960  __res = this->size();
961  const allocator_type __a = get_allocator();
962  _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size());
963  _M_rep()->_M_dispose(__a);
964  _M_data(__tmp);
965  }
966  }
967 
968  template<typename _CharT, typename _Traits, typename _Alloc>
969  void
973  {
974  if (_M_rep()->_M_is_leaked())
975  _M_rep()->_M_set_sharable();
976  if (__s._M_rep()->_M_is_leaked())
977  __s._M_rep()->_M_set_sharable();
978  if (this->get_allocator() == __s.get_allocator())
979  {
980  _CharT* __tmp = _M_data();
981  _M_data(__s._M_data());
982  __s._M_data(__tmp);
983  }
984  // The code below can usually be optimized away.
985  else
986  {
987  const basic_string __tmp1(_M_ibegin(), _M_iend(),
988  __s.get_allocator());
989  const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
990  this->get_allocator());
991  *this = __tmp2;
992  __s = __tmp1;
993  }
994  }
995 
996  template<typename _CharT, typename _Traits, typename _Alloc>
999  _S_create(size_type __capacity, size_type __old_capacity,
1000  const _Alloc& __alloc)
1001  {
1002  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1003  // 83. String::npos vs. string::max_size()
1004  if (__capacity > _S_max_size)
1005  __throw_length_error(__N("basic_string::_S_create"));
1006 
1007  // The standard places no restriction on allocating more memory
1008  // than is strictly needed within this layer at the moment or as
1009  // requested by an explicit application call to reserve().
1010 
1011  // Many malloc implementations perform quite poorly when an
1012  // application attempts to allocate memory in a stepwise fashion
1013  // growing each allocation size by only 1 char. Additionally,
1014  // it makes little sense to allocate less linear memory than the
1015  // natural blocking size of the malloc implementation.
1016  // Unfortunately, we would need a somewhat low-level calculation
1017  // with tuned parameters to get this perfect for any particular
1018  // malloc implementation. Fortunately, generalizations about
1019  // common features seen among implementations seems to suffice.
1020 
1021  // __pagesize need not match the actual VM page size for good
1022  // results in practice, thus we pick a common value on the low
1023  // side. __malloc_header_size is an estimate of the amount of
1024  // overhead per memory allocation (in practice seen N * sizeof
1025  // (void*) where N is 0, 2 or 4). According to folklore,
1026  // picking this value on the high side is better than
1027  // low-balling it (especially when this algorithm is used with
1028  // malloc implementations that allocate memory blocks rounded up
1029  // to a size which is a power of 2).
1030  const size_type __pagesize = 4096;
1031  const size_type __malloc_header_size = 4 * sizeof(void*);
1032 
1033  // The below implements an exponential growth policy, necessary to
1034  // meet amortized linear time requirements of the library: see
1035  // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
1036  // It's active for allocations requiring an amount of memory above
1037  // system pagesize. This is consistent with the requirements of the
1038  // standard: http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html
1039  if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
1040  __capacity = 2 * __old_capacity;
1041 
1042  // NB: Need an array of char_type[__capacity], plus a terminating
1043  // null char_type() element, plus enough for the _Rep data structure.
1044  // Whew. Seemingly so needy, yet so elemental.
1045  size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
1046 
1047  const size_type __adj_size = __size + __malloc_header_size;
1048  if (__adj_size > __pagesize && __capacity > __old_capacity)
1049  {
1050  const size_type __extra = __pagesize - __adj_size % __pagesize;
1051  __capacity += __extra / sizeof(_CharT);
1052  // Never allocate a string bigger than _S_max_size.
1053  if (__capacity > _S_max_size)
1054  __capacity = _S_max_size;
1055  __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
1056  }
1057 
1058  // NB: Might throw, but no worries about a leak, mate: _Rep()
1059  // does not throw.
1060  void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
1061  _Rep *__p = new (__place) _Rep;
1062  __p->_M_capacity = __capacity;
1063  // ABI compatibility - 3.4.x set in _S_create both
1064  // _M_refcount and _M_length. All callers of _S_create
1065  // in basic_string.tcc then set just _M_length.
1066  // In 4.0.x and later both _M_refcount and _M_length
1067  // are initialized in the callers, unfortunately we can
1068  // have 3.4.x compiled code with _S_create callers inlined
1069  // calling 4.0.x+ _S_create.
1070  __p->_M_set_sharable();
1071  return __p;
1072  }
1073 
1074  template<typename _CharT, typename _Traits, typename _Alloc>
1075  _CharT*
1076  basic_string<_CharT, _Traits, _Alloc>::_Rep::
1077  _M_clone(const _Alloc& __alloc, size_type __res)
1078  {
1079  // Requested capacity of the clone.
1080  const size_type __requested_cap = this->_M_length + __res;
1081  _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity,
1082  __alloc);
1083  if (this->_M_length)
1084  _M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length);
1085 
1086  __r->_M_set_length_and_sharable(this->_M_length);
1087  return __r->_M_refdata();
1088  }
1089 
1090  template<typename _CharT, typename _Traits, typename _Alloc>
1091  void
1092  basic_string<_CharT, _Traits, _Alloc>::
1093  resize(size_type __n, _CharT __c)
1094  {
1095  const size_type __size = this->size();
1096  _M_check_length(__size, __n, "basic_string::resize");
1097  if (__size < __n)
1098  this->append(__n - __size, __c);
1099  else if (__n < __size)
1100  this->erase(__n);
1101  // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
1102  }
1103 
1104  template<typename _CharT, typename _Traits, typename _Alloc>
1105  template<typename _InputIterator>
1108  _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
1109  _InputIterator __k2, __false_type)
1110  {
1111  const basic_string __s(__k1, __k2);
1112  const size_type __n1 = __i2 - __i1;
1113  _M_check_length(__n1, __s.size(), "basic_string::_M_replace_dispatch");
1114  return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
1115  __s.size());
1116  }
1117 
1118  template<typename _CharT, typename _Traits, typename _Alloc>
1119  basic_string<_CharT, _Traits, _Alloc>&
1120  basic_string<_CharT, _Traits, _Alloc>::
1121  _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
1122  _CharT __c)
1123  {
1124  _M_check_length(__n1, __n2, "basic_string::_M_replace_aux");
1125  _M_mutate(__pos1, __n1, __n2);
1126  if (__n2)
1127  _M_assign(_M_data() + __pos1, __n2, __c);
1128  return *this;
1129  }
1130 
1131  template<typename _CharT, typename _Traits, typename _Alloc>
1132  basic_string<_CharT, _Traits, _Alloc>&
1133  basic_string<_CharT, _Traits, _Alloc>::
1134  _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
1135  size_type __n2)
1136  {
1137  _M_mutate(__pos1, __n1, __n2);
1138  if (__n2)
1139  _M_copy(_M_data() + __pos1, __s, __n2);
1140  return *this;
1141  }
1142 
1143  template<typename _CharT, typename _Traits, typename _Alloc>
1144  typename basic_string<_CharT, _Traits, _Alloc>::size_type
1145  basic_string<_CharT, _Traits, _Alloc>::
1146  copy(_CharT* __s, size_type __n, size_type __pos) const
1147  {
1148  _M_check(__pos, "basic_string::copy");
1149  __n = _M_limit(__pos, __n);
1150  __glibcxx_requires_string_len(__s, __n);
1151  if (__n)
1152  _M_copy(__s, _M_data() + __pos, __n);
1153  // 21.3.5.7 par 3: do not append null. (good.)
1154  return __n;
1155  }
1156 #endif // !_GLIBCXX_USE_CXX11_ABI
1157 
1158  template<typename _CharT, typename _Traits, typename _Alloc>
1160  operator+(const _CharT* __lhs,
1162  {
1163  __glibcxx_requires_string(__lhs);
1164  typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1165  typedef typename __string_type::size_type __size_type;
1167  rebind<_CharT>::other _Char_alloc_type;
1168  typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits;
1169  const __size_type __len = _Traits::length(__lhs);
1170  __string_type __str(_Alloc_traits::_S_select_on_copy(
1171  __rhs.get_allocator()));
1172  __str.reserve(__len + __rhs.size());
1173  __str.append(__lhs, __len);
1174  __str.append(__rhs);
1175  return __str;
1176  }
1177 
1178  template<typename _CharT, typename _Traits, typename _Alloc>
1179  basic_string<_CharT, _Traits, _Alloc>
1181  {
1182  typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1183  typedef typename __string_type::size_type __size_type;
1185  rebind<_CharT>::other _Char_alloc_type;
1186  typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits;
1187  __string_type __str(_Alloc_traits::_S_select_on_copy(
1188  __rhs.get_allocator()));
1189  const __size_type __len = __rhs.size();
1190  __str.reserve(__len + 1);
1191  __str.append(__size_type(1), __lhs);
1192  __str.append(__rhs);
1193  return __str;
1194  }
1195 
1196  template<typename _CharT, typename _Traits, typename _Alloc>
1197  typename basic_string<_CharT, _Traits, _Alloc>::size_type
1198  basic_string<_CharT, _Traits, _Alloc>::
1199  find(const _CharT* __s, size_type __pos, size_type __n) const
1200  _GLIBCXX_NOEXCEPT
1201  {
1202  __glibcxx_requires_string_len(__s, __n);
1203  const size_type __size = this->size();
1204 
1205  if (__n == 0)
1206  return __pos <= __size ? __pos : npos;
1207  if (__pos >= __size)
1208  return npos;
1209 
1210  const _CharT __elem0 = __s[0];
1211  const _CharT* const __data = data();
1212  const _CharT* __first = __data + __pos;
1213  const _CharT* const __last = __data + __size;
1214  size_type __len = __size - __pos;
1215 
1216  while (__len >= __n)
1217  {
1218  // Find the first occurrence of __elem0:
1219  __first = traits_type::find(__first, __len - __n + 1, __elem0);
1220  if (!__first)
1221  return npos;
1222  // Compare the full strings from the first occurrence of __elem0.
1223  // We already know that __first[0] == __s[0] but compare them again
1224  // anyway because __s is probably aligned, which helps memcmp.
1225  if (traits_type::compare(__first, __s, __n) == 0)
1226  return __first - __data;
1227  __len = __last - ++__first;
1228  }
1229  return npos;
1230  }
1231 
1232  template<typename _CharT, typename _Traits, typename _Alloc>
1233  typename basic_string<_CharT, _Traits, _Alloc>::size_type
1235  find(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
1236  {
1237  size_type __ret = npos;
1238  const size_type __size = this->size();
1239  if (__pos < __size)
1240  {
1241  const _CharT* __data = _M_data();
1242  const size_type __n = __size - __pos;
1243  const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
1244  if (__p)
1245  __ret = __p - __data;
1246  }
1247  return __ret;
1248  }
1249 
1250  template<typename _CharT, typename _Traits, typename _Alloc>
1251  typename basic_string<_CharT, _Traits, _Alloc>::size_type
1253  rfind(const _CharT* __s, size_type __pos, size_type __n) const
1254  _GLIBCXX_NOEXCEPT
1255  {
1256  __glibcxx_requires_string_len(__s, __n);
1257  const size_type __size = this->size();
1258  if (__n <= __size)
1259  {
1260  __pos = std::min(size_type(__size - __n), __pos);
1261  const _CharT* __data = _M_data();
1262  do
1263  {
1264  if (traits_type::compare(__data + __pos, __s, __n) == 0)
1265  return __pos;
1266  }
1267  while (__pos-- > 0);
1268  }
1269  return npos;
1270  }
1271 
1272  template<typename _CharT, typename _Traits, typename _Alloc>
1273  typename basic_string<_CharT, _Traits, _Alloc>::size_type
1275  rfind(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
1276  {
1277  size_type __size = this->size();
1278  if (__size)
1279  {
1280  if (--__size > __pos)
1281  __size = __pos;
1282  for (++__size; __size-- > 0; )
1283  if (traits_type::eq(_M_data()[__size], __c))
1284  return __size;
1285  }
1286  return npos;
1287  }
1288 
1289  template<typename _CharT, typename _Traits, typename _Alloc>
1290  typename basic_string<_CharT, _Traits, _Alloc>::size_type
1292  find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
1293  _GLIBCXX_NOEXCEPT
1294  {
1295  __glibcxx_requires_string_len(__s, __n);
1296  for (; __n && __pos < this->size(); ++__pos)
1297  {
1298  const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
1299  if (__p)
1300  return __pos;
1301  }
1302  return npos;
1303  }
1304 
1305  template<typename _CharT, typename _Traits, typename _Alloc>
1306  typename basic_string<_CharT, _Traits, _Alloc>::size_type
1308  find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
1309  _GLIBCXX_NOEXCEPT
1310  {
1311  __glibcxx_requires_string_len(__s, __n);
1312  size_type __size = this->size();
1313  if (__size && __n)
1314  {
1315  if (--__size > __pos)
1316  __size = __pos;
1317  do
1318  {
1319  if (traits_type::find(__s, __n, _M_data()[__size]))
1320  return __size;
1321  }
1322  while (__size-- != 0);
1323  }
1324  return npos;
1325  }
1326 
1327  template<typename _CharT, typename _Traits, typename _Alloc>
1328  typename basic_string<_CharT, _Traits, _Alloc>::size_type
1330  find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
1331  _GLIBCXX_NOEXCEPT
1332  {
1333  __glibcxx_requires_string_len(__s, __n);
1334  for (; __pos < this->size(); ++__pos)
1335  if (!traits_type::find(__s, __n, _M_data()[__pos]))
1336  return __pos;
1337  return npos;
1338  }
1339 
1340  template<typename _CharT, typename _Traits, typename _Alloc>
1341  typename basic_string<_CharT, _Traits, _Alloc>::size_type
1343  find_first_not_of(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
1344  {
1345  for (; __pos < this->size(); ++__pos)
1346  if (!traits_type::eq(_M_data()[__pos], __c))
1347  return __pos;
1348  return npos;
1349  }
1350 
1351  template<typename _CharT, typename _Traits, typename _Alloc>
1352  typename basic_string<_CharT, _Traits, _Alloc>::size_type
1354  find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
1355  _GLIBCXX_NOEXCEPT
1356  {
1357  __glibcxx_requires_string_len(__s, __n);
1358  size_type __size = this->size();
1359  if (__size)
1360  {
1361  if (--__size > __pos)
1362  __size = __pos;
1363  do
1364  {
1365  if (!traits_type::find(__s, __n, _M_data()[__size]))
1366  return __size;
1367  }
1368  while (__size--);
1369  }
1370  return npos;
1371  }
1372 
1373  template<typename _CharT, typename _Traits, typename _Alloc>
1374  typename basic_string<_CharT, _Traits, _Alloc>::size_type
1376  find_last_not_of(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
1377  {
1378  size_type __size = this->size();
1379  if (__size)
1380  {
1381  if (--__size > __pos)
1382  __size = __pos;
1383  do
1384  {
1385  if (!traits_type::eq(_M_data()[__size], __c))
1386  return __size;
1387  }
1388  while (__size--);
1389  }
1390  return npos;
1391  }
1392 
1393  template<typename _CharT, typename _Traits, typename _Alloc>
1394  int
1396  compare(size_type __pos, size_type __n, const basic_string& __str) const
1397  {
1398  _M_check(__pos, "basic_string::compare");
1399  __n = _M_limit(__pos, __n);
1400  const size_type __osize = __str.size();
1401  const size_type __len = std::min(__n, __osize);
1402  int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
1403  if (!__r)
1404  __r = _S_compare(__n, __osize);
1405  return __r;
1406  }
1407 
1408  template<typename _CharT, typename _Traits, typename _Alloc>
1409  int
1411  compare(size_type __pos1, size_type __n1, const basic_string& __str,
1412  size_type __pos2, size_type __n2) const
1413  {
1414  _M_check(__pos1, "basic_string::compare");
1415  __str._M_check(__pos2, "basic_string::compare");
1416  __n1 = _M_limit(__pos1, __n1);
1417  __n2 = __str._M_limit(__pos2, __n2);
1418  const size_type __len = std::min(__n1, __n2);
1419  int __r = traits_type::compare(_M_data() + __pos1,
1420  __str.data() + __pos2, __len);
1421  if (!__r)
1422  __r = _S_compare(__n1, __n2);
1423  return __r;
1424  }
1425 
1426  template<typename _CharT, typename _Traits, typename _Alloc>
1427  int
1429  compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT
1430  {
1431  __glibcxx_requires_string(__s);
1432  const size_type __size = this->size();
1433  const size_type __osize = traits_type::length(__s);
1434  const size_type __len = std::min(__size, __osize);
1435  int __r = traits_type::compare(_M_data(), __s, __len);
1436  if (!__r)
1437  __r = _S_compare(__size, __osize);
1438  return __r;
1439  }
1440 
1441  template<typename _CharT, typename _Traits, typename _Alloc>
1442  int
1444  compare(size_type __pos, size_type __n1, const _CharT* __s) const
1445  {
1446  __glibcxx_requires_string(__s);
1447  _M_check(__pos, "basic_string::compare");
1448  __n1 = _M_limit(__pos, __n1);
1449  const size_type __osize = traits_type::length(__s);
1450  const size_type __len = std::min(__n1, __osize);
1451  int __r = traits_type::compare(_M_data() + __pos, __s, __len);
1452  if (!__r)
1453  __r = _S_compare(__n1, __osize);
1454  return __r;
1455  }
1456 
1457  template<typename _CharT, typename _Traits, typename _Alloc>
1458  int
1460  compare(size_type __pos, size_type __n1, const _CharT* __s,
1461  size_type __n2) const
1462  {
1463  __glibcxx_requires_string_len(__s, __n2);
1464  _M_check(__pos, "basic_string::compare");
1465  __n1 = _M_limit(__pos, __n1);
1466  const size_type __len = std::min(__n1, __n2);
1467  int __r = traits_type::compare(_M_data() + __pos, __s, __len);
1468  if (!__r)
1469  __r = _S_compare(__n1, __n2);
1470  return __r;
1471  }
1472 
1473  // 21.3.7.9 basic_string::getline and operators
1474  template<typename _CharT, typename _Traits, typename _Alloc>
1478  {
1479  typedef basic_istream<_CharT, _Traits> __istream_type;
1480  typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1481  typedef typename __istream_type::ios_base __ios_base;
1482  typedef typename __istream_type::int_type __int_type;
1483  typedef typename __string_type::size_type __size_type;
1484  typedef ctype<_CharT> __ctype_type;
1485  typedef typename __ctype_type::ctype_base __ctype_base;
1486 
1487  __size_type __extracted = 0;
1488  typename __ios_base::iostate __err = __ios_base::goodbit;
1489  typename __istream_type::sentry __cerb(__in, false);
1490  if (__cerb)
1491  {
1492  __try
1493  {
1494  // Avoid reallocation for common case.
1495  __str.erase();
1496  _CharT __buf[128];
1497  __size_type __len = 0;
1498  const streamsize __w = __in.width();
1499  const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
1500  : __str.max_size();
1501  const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
1502  const __int_type __eof = _Traits::eof();
1503  __int_type __c = __in.rdbuf()->sgetc();
1504 
1505  while (__extracted < __n
1506  && !_Traits::eq_int_type(__c, __eof)
1507  && !__ct.is(__ctype_base::space,
1508  _Traits::to_char_type(__c)))
1509  {
1510  if (__len == sizeof(__buf) / sizeof(_CharT))
1511  {
1512  __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
1513  __len = 0;
1514  }
1515  __buf[__len++] = _Traits::to_char_type(__c);
1516  ++__extracted;
1517  __c = __in.rdbuf()->snextc();
1518  }
1519  __str.append(__buf, __len);
1520 
1521  if (_Traits::eq_int_type(__c, __eof))
1522  __err |= __ios_base::eofbit;
1523  __in.width(0);
1524  }
1525  __catch(__cxxabiv1::__forced_unwind&)
1526  {
1527  __in._M_setstate(__ios_base::badbit);
1528  __throw_exception_again;
1529  }
1530  __catch(...)
1531  {
1532  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1533  // 91. Description of operator>> and getline() for string<>
1534  // might cause endless loop
1535  __in._M_setstate(__ios_base::badbit);
1536  }
1537  }
1538  // 211. operator>>(istream&, string&) doesn't set failbit
1539  if (!__extracted)
1540  __err |= __ios_base::failbit;
1541  if (__err)
1542  __in.setstate(__err);
1543  return __in;
1544  }
1545 
1546  template<typename _CharT, typename _Traits, typename _Alloc>
1547  basic_istream<_CharT, _Traits>&
1549  basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
1550  {
1551  typedef basic_istream<_CharT, _Traits> __istream_type;
1552  typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1553  typedef typename __istream_type::ios_base __ios_base;
1554  typedef typename __istream_type::int_type __int_type;
1555  typedef typename __string_type::size_type __size_type;
1556 
1557  __size_type __extracted = 0;
1558  const __size_type __n = __str.max_size();
1559  typename __ios_base::iostate __err = __ios_base::goodbit;
1560  typename __istream_type::sentry __cerb(__in, true);
1561  if (__cerb)
1562  {
1563  __try
1564  {
1565  __str.erase();
1566  const __int_type __idelim = _Traits::to_int_type(__delim);
1567  const __int_type __eof = _Traits::eof();
1568  __int_type __c = __in.rdbuf()->sgetc();
1569 
1570  while (__extracted < __n
1571  && !_Traits::eq_int_type(__c, __eof)
1572  && !_Traits::eq_int_type(__c, __idelim))
1573  {
1574  __str += _Traits::to_char_type(__c);
1575  ++__extracted;
1576  __c = __in.rdbuf()->snextc();
1577  }
1578 
1579  if (_Traits::eq_int_type(__c, __eof))
1580  __err |= __ios_base::eofbit;
1581  else if (_Traits::eq_int_type(__c, __idelim))
1582  {
1583  ++__extracted;
1584  __in.rdbuf()->sbumpc();
1585  }
1586  else
1587  __err |= __ios_base::failbit;
1588  }
1589  __catch(__cxxabiv1::__forced_unwind&)
1590  {
1591  __in._M_setstate(__ios_base::badbit);
1592  __throw_exception_again;
1593  }
1594  __catch(...)
1595  {
1596  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1597  // 91. Description of operator>> and getline() for string<>
1598  // might cause endless loop
1599  __in._M_setstate(__ios_base::badbit);
1600  }
1601  }
1602  if (!__extracted)
1603  __err |= __ios_base::failbit;
1604  if (__err)
1605  __in.setstate(__err);
1606  return __in;
1607  }
1608 
1609  // Inhibit implicit instantiations for required instantiations,
1610  // which are defined via explicit instantiations elsewhere.
1611 #if _GLIBCXX_EXTERN_TEMPLATE
1612  // The explicit instantiations definitions in src/c++11/string-inst.cc
1613  // are compiled as C++14, so the new C++17 members aren't instantiated.
1614  // Until those definitions are compiled as C++17 suppress the declaration,
1615  // so C++17 code will implicitly instantiate std::string and std::wstring
1616  // as needed.
1617 # if __cplusplus <= 201703L && _GLIBCXX_EXTERN_TEMPLATE > 0
1618  extern template class basic_string<char>;
1619 # elif ! _GLIBCXX_USE_CXX11_ABI
1620  // Still need to prevent implicit instantiation of the COW empty rep,
1621  // to ensure the definition in libstdc++.so is unique (PR 86138).
1622  extern template basic_string<char>::size_type
1623  basic_string<char>::_Rep::_S_empty_rep_storage[];
1624 # endif
1625 
1626  extern template
1627  basic_istream<char>&
1628  operator>>(basic_istream<char>&, string&);
1629  extern template
1630  basic_ostream<char>&
1631  operator<<(basic_ostream<char>&, const string&);
1632  extern template
1633  basic_istream<char>&
1634  getline(basic_istream<char>&, string&, char);
1635  extern template
1636  basic_istream<char>&
1637  getline(basic_istream<char>&, string&);
1638 
1639 #ifdef _GLIBCXX_USE_WCHAR_T
1640 # if __cplusplus <= 201703L && _GLIBCXX_EXTERN_TEMPLATE > 0
1641  extern template class basic_string<wchar_t>;
1642 # elif ! _GLIBCXX_USE_CXX11_ABI
1643  extern template basic_string<wchar_t>::size_type
1644  basic_string<wchar_t>::_Rep::_S_empty_rep_storage[];
1645 # endif
1646 
1647  extern template
1648  basic_istream<wchar_t>&
1649  operator>>(basic_istream<wchar_t>&, wstring&);
1650  extern template
1651  basic_ostream<wchar_t>&
1652  operator<<(basic_ostream<wchar_t>&, const wstring&);
1653  extern template
1654  basic_istream<wchar_t>&
1655  getline(basic_istream<wchar_t>&, wstring&, wchar_t);
1656  extern template
1657  basic_istream<wchar_t>&
1658  getline(basic_istream<wchar_t>&, wstring&);
1659 #endif // _GLIBCXX_USE_WCHAR_T
1660 #endif // _GLIBCXX_EXTERN_TEMPLATE
1661 
1662 _GLIBCXX_END_NAMESPACE_VERSION
1663 } // namespace std
1664 
1665 #endif
std::distance
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
Definition: stl_iterator_base_funcs.h:138
std::chrono::duration::operator+
constexpr common_type< duration< _Rep1, _Period1 >, duration< _Rep2, _Period2 > >::type operator+(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
The sum of two durations.
Definition: chrono:482
std::ctype
Primary class template ctype facet.
Definition: locale_facets.h:612
std::min
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Definition: stl_algobase.h:230
std::initializer_list::end
constexpr const _Tp * end(initializer_list< _Tp > __ils) noexcept
Return an iterator pointing to one past the last element of the initializer_list.
Definition: initializer_list:101
std::basic_string::reserve
void reserve(size_type __res_arg=0)
Attempt to preallocate enough memory for specified number of characters.
Definition: basic_string.tcc:954
std::getline
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.
Definition: basic_string.tcc:1548
std::iterator
Common iterator class.
Definition: stl_iterator_base_types.h:127
cxxabi_forced.h
std
ISO C++ entities toplevel namespace is std.
std::forward_iterator_tag
Forward iterators support a superset of input iterator operations.
Definition: stl_iterator_base_types.h:99
std::basic_string::npos
static const size_type npos
Value returned by various member functions when they fail.
Definition: basic_string.h:3353
std::basic_string::max_size
size_type max_size() const noexcept
Returns the size() of the largest possible string.
Definition: basic_string.h:3914
std::input_iterator_tag
Marking input iterators.
Definition: stl_iterator_base_types.h:93
std::basic_string::get_allocator
allocator_type get_allocator() const noexcept
Return copy of allocator used to construct this string.
Definition: basic_string.h:5231
std::basic_string::data
const _CharT * data() const noexcept
Return const pointer to contents.
Definition: basic_string.h:5209
std::basic_string::swap
void swap(basic_string &__s) noexcept(/*conditional */)
Swap contents with another string.
Definition: basic_string.tcc:971
__gnu_cxx::__alloc_traits
Uniform interface to C++98 and C++11 allocators.
Definition: ext/alloc_traits.h:48
std::char_traits< char >
21.1.3.1 char_traits specializations
Definition: char_traits.h:290
std::initializer_list::begin
constexpr const _Tp * begin(initializer_list< _Tp > __ils) noexcept
Return an iterator pointing to the first element of the initializer_list.
Definition: initializer_list:90
std::streamsize
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
Definition: postypes.h:98
std::basic_istream
Template class basic_istream.
Definition: iosfwd:83
std::wstring
basic_string< wchar_t > wstring
A string of wchar_t.
Definition: stringfwd.h:83
std::basic_string::append
basic_string & append(const basic_string &__str)
Append a string to this string.
Definition: basic_string.tcc:777
std::begin
_Tp * begin(valarray< _Tp > &__va)
Return an iterator pointing to the first element of the valarray.
Definition: valarray:1214
std::allocator_traits
Uniform interface to all allocator types.
Definition: bits/alloc_traits.h:86
std::basic_string::resize
void resize(size_type __n, _CharT __c)
Resizes the string to the specified number of characters.
Definition: basic_string.tcc:1093
std::basic_string
Managing sequences of characters and character-like objects.
Definition: basic_string.h:3139
std::allocator
The standard allocator, as per [20.4].
Definition: allocator.h:120
std::basic_string::basic_string
basic_string() noexcept
Default constructor creates an empty string.
Definition: basic_string.h:3546
std::basic_string::erase
basic_string & erase(size_type __pos=0, size_type __n=npos)
Remove characters.
Definition: basic_string.h:4700
std::initializer_list
initializer_list
Definition: initializer_list:47
std::basic_string::copy
size_type copy(_CharT *__s, size_type __n, size_type __pos=0) const
Copy substring into C string.
Definition: basic_string.tcc:1146
__cxxabiv1::__forced_unwind
Thrown as part of forced unwinding.
Definition: cxxabi_forced.h:48
std::basic_string::size
size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
Definition: basic_string.h:3903
std::experimental::filesystem::v1::path::operator>>
basic_istream< _CharT, _Traits > & operator>>(basic_istream< _CharT, _Traits > &__is, path &__p)
Read a path from a stream.
Definition: experimental/bits/fs_path.h:593
std::sub_match::operator<<
basic_ostream< _Ch_type, _Ch_traits > & operator<<(basic_ostream< _Ch_type, _Ch_traits > &__os, const sub_match< _Bi_iter > &__m)
Inserts a matched string into an output stream.
Definition: regex.h:1563