libstdc++
basic_string.tcc
Go to the documentation of this file.
1// Components for manipulating sequences of characters -*- C++ -*-
2
3// Copyright (C) 1997-2022 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
46namespace 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 _GLIBCXX20_CONSTEXPR
58 void
60 swap(basic_string& __s) _GLIBCXX_NOEXCEPT
61 {
62 if (this == std::__addressof(__s))
63 return;
64
65 _Alloc_traits::_S_on_swap(_M_get_allocator(), __s._M_get_allocator());
66
67 if (_M_is_local())
68 if (__s._M_is_local())
69 {
70 if (length() && __s.length())
71 {
72 _CharT __tmp_data[_S_local_capacity + 1];
73 traits_type::copy(__tmp_data, __s._M_local_buf,
74 __s.length() + 1);
75 traits_type::copy(__s._M_local_buf, _M_local_buf,
76 length() + 1);
77 traits_type::copy(_M_local_buf, __tmp_data,
78 __s.length() + 1);
79 }
80 else if (__s.length())
81 {
82 traits_type::copy(_M_local_buf, __s._M_local_buf,
83 __s.length() + 1);
84 _M_length(__s.length());
85 __s._M_set_length(0);
86 return;
87 }
88 else if (length())
89 {
90 traits_type::copy(__s._M_local_buf, _M_local_buf,
91 length() + 1);
92 __s._M_length(length());
93 _M_set_length(0);
94 return;
95 }
96 }
97 else
98 {
99 const size_type __tmp_capacity = __s._M_allocated_capacity;
100 traits_type::copy(__s._M_local_buf, _M_local_buf,
101 length() + 1);
102 _M_data(__s._M_data());
103 __s._M_data(__s._M_local_buf);
104 _M_capacity(__tmp_capacity);
105 }
106 else
107 {
108 const size_type __tmp_capacity = _M_allocated_capacity;
109 if (__s._M_is_local())
110 {
111 traits_type::copy(_M_local_buf, __s._M_local_buf,
112 __s.length() + 1);
113 __s._M_data(_M_data());
114 _M_data(_M_local_buf);
115 }
116 else
117 {
118 pointer __tmp_ptr = _M_data();
119 _M_data(__s._M_data());
120 __s._M_data(__tmp_ptr);
121 _M_capacity(__s._M_allocated_capacity);
122 }
123 __s._M_capacity(__tmp_capacity);
124 }
125
126 const size_type __tmp_length = length();
127 _M_length(__s.length());
128 __s._M_length(__tmp_length);
129 }
130
131 template<typename _CharT, typename _Traits, typename _Alloc>
132 _GLIBCXX20_CONSTEXPR
133 typename basic_string<_CharT, _Traits, _Alloc>::pointer
134 basic_string<_CharT, _Traits, _Alloc>::
135 _M_create(size_type& __capacity, size_type __old_capacity)
136 {
137 // _GLIBCXX_RESOLVE_LIB_DEFECTS
138 // 83. String::npos vs. string::max_size()
139 if (__capacity > max_size())
140 std::__throw_length_error(__N("basic_string::_M_create"));
141
142 // The below implements an exponential growth policy, necessary to
143 // meet amortized linear time requirements of the library: see
144 // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
145 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
146 {
147 __capacity = 2 * __old_capacity;
148 // Never allocate a string bigger than max_size.
149 if (__capacity > max_size())
150 __capacity = max_size();
151 }
152
153 // NB: Need an array of char_type[__capacity], plus a terminating
154 // null char_type() element.
155 return _Alloc_traits::allocate(_M_get_allocator(), __capacity + 1);
156 }
157
158 // NB: This is the special case for Input Iterators, used in
159 // istreambuf_iterators, etc.
160 // Input Iterators have a cost structure very different from
161 // pointers, calling for a different coding style.
162 template<typename _CharT, typename _Traits, typename _Alloc>
163 template<typename _InIterator>
164 _GLIBCXX20_CONSTEXPR
165 void
166 basic_string<_CharT, _Traits, _Alloc>::
167 _M_construct(_InIterator __beg, _InIterator __end,
169 {
170 size_type __len = 0;
171 size_type __capacity = size_type(_S_local_capacity);
172
173 pointer __p = _M_use_local_data();
174
175 while (__beg != __end && __len < __capacity)
176 {
177 __p[__len++] = *__beg;
178 ++__beg;
179 }
180
181 struct _Guard
182 {
183 _GLIBCXX20_CONSTEXPR
184 explicit _Guard(basic_string* __s) : _M_guarded(__s) { }
185
186 _GLIBCXX20_CONSTEXPR
187 ~_Guard() { if (_M_guarded) _M_guarded->_M_dispose(); }
188
189 basic_string* _M_guarded;
190 } __guard(this);
191
192 while (__beg != __end)
193 {
194 if (__len == __capacity)
195 {
196 // Allocate more space.
197 __capacity = __len + 1;
198 pointer __another = _M_create(__capacity, __len);
199 this->_S_copy(__another, _M_data(), __len);
200 _M_dispose();
201 _M_data(__another);
202 _M_capacity(__capacity);
203 }
204 traits_type::assign(_M_data()[__len++], *__beg);
205 ++__beg;
206 }
207
208 __guard._M_guarded = 0;
209
210 _M_set_length(__len);
211 }
212
213 template<typename _CharT, typename _Traits, typename _Alloc>
214 template<typename _InIterator>
215 _GLIBCXX20_CONSTEXPR
216 void
217 basic_string<_CharT, _Traits, _Alloc>::
218 _M_construct(_InIterator __beg, _InIterator __end,
220 {
221 size_type __dnew = static_cast<size_type>(std::distance(__beg, __end));
222
223 if (__dnew > size_type(_S_local_capacity))
224 {
225 _M_data(_M_create(__dnew, size_type(0)));
226 _M_capacity(__dnew);
227 }
228 else
229 _M_use_local_data();
230
231 // Check for out_of_range and length_error exceptions.
232 struct _Guard
233 {
234 _GLIBCXX20_CONSTEXPR
235 explicit _Guard(basic_string* __s) : _M_guarded(__s) { }
236
237 _GLIBCXX20_CONSTEXPR
238 ~_Guard() { if (_M_guarded) _M_guarded->_M_dispose(); }
239
240 basic_string* _M_guarded;
241 } __guard(this);
242
243 this->_S_copy_chars(_M_data(), __beg, __end);
244
245 __guard._M_guarded = 0;
246
247 _M_set_length(__dnew);
248 }
249
250 template<typename _CharT, typename _Traits, typename _Alloc>
251 _GLIBCXX20_CONSTEXPR
252 void
253 basic_string<_CharT, _Traits, _Alloc>::
254 _M_construct(size_type __n, _CharT __c)
255 {
256 if (__n > size_type(_S_local_capacity))
257 {
258 _M_data(_M_create(__n, size_type(0)));
259 _M_capacity(__n);
260 }
261 else
262 _M_use_local_data();
263
264 if (__n)
265 this->_S_assign(_M_data(), __n, __c);
266
267 _M_set_length(__n);
268 }
269
270 template<typename _CharT, typename _Traits, typename _Alloc>
271 _GLIBCXX20_CONSTEXPR
272 void
273 basic_string<_CharT, _Traits, _Alloc>::
274 _M_assign(const basic_string& __str)
275 {
276 if (this != std::__addressof(__str))
277 {
278 const size_type __rsize = __str.length();
279 const size_type __capacity = capacity();
280
281 if (__rsize > __capacity)
282 {
283 size_type __new_capacity = __rsize;
284 pointer __tmp = _M_create(__new_capacity, __capacity);
285 _M_dispose();
286 _M_data(__tmp);
287 _M_capacity(__new_capacity);
288 }
289
290 if (__rsize)
291 this->_S_copy(_M_data(), __str._M_data(), __rsize);
292
293 _M_set_length(__rsize);
294 }
295 }
296
297 template<typename _CharT, typename _Traits, typename _Alloc>
298 _GLIBCXX20_CONSTEXPR
299 void
301 reserve(size_type __res)
302 {
303 const size_type __capacity = capacity();
304 // _GLIBCXX_RESOLVE_LIB_DEFECTS
305 // 2968. Inconsistencies between basic_string reserve and
306 // vector/unordered_map/unordered_set reserve functions
307 // P0966 reserve should not shrink
308 if (__res <= __capacity)
309 return;
310
311 pointer __tmp = _M_create(__res, __capacity);
312 this->_S_copy(__tmp, _M_data(), length() + 1);
313 _M_dispose();
314 _M_data(__tmp);
315 _M_capacity(__res);
316 }
317
318 template<typename _CharT, typename _Traits, typename _Alloc>
319 _GLIBCXX20_CONSTEXPR
320 void
321 basic_string<_CharT, _Traits, _Alloc>::
322 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
323 size_type __len2)
324 {
325 const size_type __how_much = length() - __pos - __len1;
326
327 size_type __new_capacity = length() + __len2 - __len1;
328 pointer __r = _M_create(__new_capacity, capacity());
329
330 if (__pos)
331 this->_S_copy(__r, _M_data(), __pos);
332 if (__s && __len2)
333 this->_S_copy(__r + __pos, __s, __len2);
334 if (__how_much)
335 this->_S_copy(__r + __pos + __len2,
336 _M_data() + __pos + __len1, __how_much);
337
338 _M_dispose();
339 _M_data(__r);
340 _M_capacity(__new_capacity);
341 }
342
343 template<typename _CharT, typename _Traits, typename _Alloc>
344 _GLIBCXX20_CONSTEXPR
345 void
346 basic_string<_CharT, _Traits, _Alloc>::
347 _M_erase(size_type __pos, size_type __n)
348 {
349 const size_type __how_much = length() - __pos - __n;
350
351 if (__how_much && __n)
352 this->_S_move(_M_data() + __pos, _M_data() + __pos + __n, __how_much);
353
354 _M_set_length(length() - __n);
355 }
356
357 template<typename _CharT, typename _Traits, typename _Alloc>
358 _GLIBCXX20_CONSTEXPR
359 void
361 reserve()
362 {
363 if (_M_is_local())
364 return;
365
366 const size_type __length = length();
367 const size_type __capacity = _M_allocated_capacity;
368
369 if (__length <= size_type(_S_local_capacity))
370 {
371 this->_S_copy(_M_use_local_data(), _M_data(), __length + 1);
372 _M_destroy(__capacity);
373 _M_data(_M_local_data());
374 }
375#if __cpp_exceptions
376 else if (__length < __capacity)
377 try
378 {
379 pointer __tmp
380 = _Alloc_traits::allocate(_M_get_allocator(), __length + 1);
381 this->_S_copy(__tmp, _M_data(), __length + 1);
382 _M_dispose();
383 _M_data(__tmp);
384 _M_capacity(__length);
385 }
386 catch (const __cxxabiv1::__forced_unwind&)
387 { throw; }
388 catch (...)
389 { /* swallow the exception */ }
390#endif
391 }
392
393 template<typename _CharT, typename _Traits, typename _Alloc>
394 _GLIBCXX20_CONSTEXPR
395 void
397 resize(size_type __n, _CharT __c)
398 {
399 const size_type __size = this->size();
400 if (__size < __n)
401 this->append(__n - __size, __c);
402 else if (__n < __size)
403 this->_M_set_length(__n);
404 }
405
406 template<typename _CharT, typename _Traits, typename _Alloc>
407 _GLIBCXX20_CONSTEXPR
408 basic_string<_CharT, _Traits, _Alloc>&
409 basic_string<_CharT, _Traits, _Alloc>::
410 _M_append(const _CharT* __s, size_type __n)
411 {
412 const size_type __len = __n + this->size();
413
414 if (__len <= this->capacity())
415 {
416 if (__n)
417 this->_S_copy(this->_M_data() + this->size(), __s, __n);
418 }
419 else
420 this->_M_mutate(this->size(), size_type(0), __s, __n);
421
422 this->_M_set_length(__len);
423 return *this;
424 }
425
426 template<typename _CharT, typename _Traits, typename _Alloc>
427 template<typename _InputIterator>
428 _GLIBCXX20_CONSTEXPR
429 basic_string<_CharT, _Traits, _Alloc>&
430 basic_string<_CharT, _Traits, _Alloc>::
431 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
432 _InputIterator __k1, _InputIterator __k2,
433 std::__false_type)
434 {
435 // _GLIBCXX_RESOLVE_LIB_DEFECTS
436 // 2788. unintentionally require a default constructible allocator
437 const basic_string __s(__k1, __k2, this->get_allocator());
438 const size_type __n1 = __i2 - __i1;
439 return _M_replace(__i1 - begin(), __n1, __s._M_data(),
440 __s.size());
441 }
442
443 template<typename _CharT, typename _Traits, typename _Alloc>
444 _GLIBCXX20_CONSTEXPR
445 basic_string<_CharT, _Traits, _Alloc>&
446 basic_string<_CharT, _Traits, _Alloc>::
447 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
448 _CharT __c)
449 {
450 _M_check_length(__n1, __n2, "basic_string::_M_replace_aux");
451
452 const size_type __old_size = this->size();
453 const size_type __new_size = __old_size + __n2 - __n1;
454
455 if (__new_size <= this->capacity())
456 {
457 pointer __p = this->_M_data() + __pos1;
458
459 const size_type __how_much = __old_size - __pos1 - __n1;
460 if (__how_much && __n1 != __n2)
461 this->_S_move(__p + __n2, __p + __n1, __how_much);
462 }
463 else
464 this->_M_mutate(__pos1, __n1, 0, __n2);
465
466 if (__n2)
467 this->_S_assign(this->_M_data() + __pos1, __n2, __c);
468
469 this->_M_set_length(__new_size);
470 return *this;
471 }
472
473 template<typename _CharT, typename _Traits, typename _Alloc>
474 __attribute__((__noinline__, __noclone__, __cold__)) void
475 basic_string<_CharT, _Traits, _Alloc>::
476 _M_replace_cold(pointer __p, size_type __len1, const _CharT* __s,
477 const size_type __len2, const size_type __how_much)
478 {
479 // Work in-place.
480 if (__len2 && __len2 <= __len1)
481 this->_S_move(__p, __s, __len2);
482 if (__how_much && __len1 != __len2)
483 this->_S_move(__p + __len2, __p + __len1, __how_much);
484 if (__len2 > __len1)
485 {
486 if (__s + __len2 <= __p + __len1)
487 this->_S_move(__p, __s, __len2);
488 else if (__s >= __p + __len1)
489 {
490 // Hint to middle end that __p and __s overlap
491 // (PR 98465).
492 const size_type __poff = (__s - __p) + (__len2 - __len1);
493 this->_S_copy(__p, __p + __poff, __len2);
494 }
495 else
496 {
497 const size_type __nleft = (__p + __len1) - __s;
498 this->_S_move(__p, __s, __nleft);
499 this->_S_copy(__p + __nleft, __p + __len2, __len2 - __nleft);
500 }
501 }
502 }
503
504 template<typename _CharT, typename _Traits, typename _Alloc>
505 _GLIBCXX20_CONSTEXPR
506 basic_string<_CharT, _Traits, _Alloc>&
507 basic_string<_CharT, _Traits, _Alloc>::
508 _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
509 const size_type __len2)
510 {
511 _M_check_length(__len1, __len2, "basic_string::_M_replace");
512
513 const size_type __old_size = this->size();
514 const size_type __new_size = __old_size + __len2 - __len1;
515
516 if (__new_size <= this->capacity())
517 {
518 pointer __p = this->_M_data() + __pos;
519
520 const size_type __how_much = __old_size - __pos - __len1;
521#if __cpp_lib_is_constant_evaluated
523 {
524 auto __newp = _Alloc_traits::allocate(_M_get_allocator(),
525 __new_size);
526 _S_copy(__newp, this->_M_data(), __pos);
527 _S_copy(__newp + __pos, __s, __len2);
528 _S_copy(__newp + __pos + __len2, __p + __len1, __how_much);
529 _S_copy(this->_M_data(), __newp, __new_size);
530 this->_M_get_allocator().deallocate(__newp, __new_size);
531 }
532 else
533#endif
534 if (__builtin_expect(_M_disjunct(__s), true))
535 {
536 if (__how_much && __len1 != __len2)
537 this->_S_move(__p + __len2, __p + __len1, __how_much);
538 if (__len2)
539 this->_S_copy(__p, __s, __len2);
540 }
541 else
542 _M_replace_cold(__p, __len1, __s, __len2, __how_much);
543 }
544 else
545 this->_M_mutate(__pos, __len1, __s, __len2);
546
547 this->_M_set_length(__new_size);
548 return *this;
549 }
550
551 template<typename _CharT, typename _Traits, typename _Alloc>
552 _GLIBCXX20_CONSTEXPR
553 typename basic_string<_CharT, _Traits, _Alloc>::size_type
555 copy(_CharT* __s, size_type __n, size_type __pos) const
556 {
557 _M_check(__pos, "basic_string::copy");
558 __n = _M_limit(__pos, __n);
559 __glibcxx_requires_string_len(__s, __n);
560 if (__n)
561 _S_copy(__s, _M_data() + __pos, __n);
562 // 21.3.5.7 par 3: do not append null. (good.)
563 return __n;
564 }
565
566#if __cplusplus > 202002L
567 template<typename _CharT, typename _Traits, typename _Alloc>
568 template<typename _Operation>
569 constexpr void
570 basic_string<_CharT, _Traits, _Alloc>::
571 resize_and_overwrite(size_type __n, _Operation __op)
572 {
573 const size_type __capacity = capacity();
574 _CharT* __p;
575 if (__n > __capacity)
576 {
577 __p = _M_create(__n, __capacity);
578 this->_S_copy(__p, _M_data(), length()); // exclude trailing null
579#if __cpp_lib_is_constant_evaluated
581 traits_type::assign(__p + length(), __n - length(), _CharT());
582#endif
583 _M_dispose();
584 _M_data(__p);
585 _M_capacity(__n);
586 }
587 else
588 __p = _M_data();
589 struct _Terminator {
590 constexpr ~_Terminator() { _M_this->_M_set_length(_M_r); }
591 basic_string* _M_this;
592 size_type _M_r;
593 };
594 _Terminator __term{this};
595 const size_type __n2 [[maybe_unused]] = __n;
596 __term._M_r = std::move(__op)(__p, __n);
597 _GLIBCXX_DEBUG_ASSERT(__term._M_r >= 0 && __term._M_r <= __n2);
598 }
599#endif // C++23
600
601#endif // _GLIBCXX_USE_CXX11_ABI
602
603#if __cpp_lib_constexpr_string >= 201907L
604# define _GLIBCXX_STRING_CONSTEXPR constexpr
605#else
606# define _GLIBCXX_STRING_CONSTEXPR
607#endif
608
609 template<typename _CharT, typename _Traits, typename _Alloc>
610 _GLIBCXX20_CONSTEXPR
611 basic_string<_CharT, _Traits, _Alloc>
612 operator+(const _CharT* __lhs,
614 {
615 __glibcxx_requires_string(__lhs);
616 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
617 typedef typename __string_type::size_type __size_type;
619 rebind<_CharT>::other _Char_alloc_type;
621 const __size_type __len = _Traits::length(__lhs);
622 __string_type __str(_Alloc_traits::_S_select_on_copy(
623 __rhs.get_allocator()));
624 __str.reserve(__len + __rhs.size());
625 __str.append(__lhs, __len);
626 __str.append(__rhs);
627 return __str;
628 }
629
630 template<typename _CharT, typename _Traits, typename _Alloc>
631 _GLIBCXX20_CONSTEXPR
632 basic_string<_CharT, _Traits, _Alloc>
634 {
635 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
636 typedef typename __string_type::size_type __size_type;
638 rebind<_CharT>::other _Char_alloc_type;
640 __string_type __str(_Alloc_traits::_S_select_on_copy(
641 __rhs.get_allocator()));
642 const __size_type __len = __rhs.size();
643 __str.reserve(__len + 1);
644 __str.append(__size_type(1), __lhs);
645 __str.append(__rhs);
646 return __str;
647 }
648
649 template<typename _CharT, typename _Traits, typename _Alloc>
650 _GLIBCXX_STRING_CONSTEXPR
651 typename basic_string<_CharT, _Traits, _Alloc>::size_type
653 find(const _CharT* __s, size_type __pos, size_type __n) const
654 _GLIBCXX_NOEXCEPT
655 {
656 __glibcxx_requires_string_len(__s, __n);
657 const size_type __size = this->size();
658
659 if (__n == 0)
660 return __pos <= __size ? __pos : npos;
661 if (__pos >= __size)
662 return npos;
663
664 const _CharT __elem0 = __s[0];
665 const _CharT* const __data = data();
666 const _CharT* __first = __data + __pos;
667 const _CharT* const __last = __data + __size;
668 size_type __len = __size - __pos;
669
670 while (__len >= __n)
671 {
672 // Find the first occurrence of __elem0:
673 __first = traits_type::find(__first, __len - __n + 1, __elem0);
674 if (!__first)
675 return npos;
676 // Compare the full strings from the first occurrence of __elem0.
677 // We already know that __first[0] == __s[0] but compare them again
678 // anyway because __s is probably aligned, which helps memcmp.
679 if (traits_type::compare(__first, __s, __n) == 0)
680 return __first - __data;
681 __len = __last - ++__first;
682 }
683 return npos;
684 }
685
686 template<typename _CharT, typename _Traits, typename _Alloc>
687 _GLIBCXX_STRING_CONSTEXPR
688 typename basic_string<_CharT, _Traits, _Alloc>::size_type
690 find(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
691 {
692 size_type __ret = npos;
693 const size_type __size = this->size();
694 if (__pos < __size)
695 {
696 const _CharT* __data = _M_data();
697 const size_type __n = __size - __pos;
698 const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
699 if (__p)
700 __ret = __p - __data;
701 }
702 return __ret;
703 }
704
705 template<typename _CharT, typename _Traits, typename _Alloc>
706 _GLIBCXX_STRING_CONSTEXPR
707 typename basic_string<_CharT, _Traits, _Alloc>::size_type
709 rfind(const _CharT* __s, size_type __pos, size_type __n) const
710 _GLIBCXX_NOEXCEPT
711 {
712 __glibcxx_requires_string_len(__s, __n);
713 const size_type __size = this->size();
714 if (__n <= __size)
715 {
716 __pos = std::min(size_type(__size - __n), __pos);
717 const _CharT* __data = _M_data();
718 do
719 {
720 if (traits_type::compare(__data + __pos, __s, __n) == 0)
721 return __pos;
722 }
723 while (__pos-- > 0);
724 }
725 return npos;
726 }
727
728 template<typename _CharT, typename _Traits, typename _Alloc>
729 _GLIBCXX_STRING_CONSTEXPR
730 typename basic_string<_CharT, _Traits, _Alloc>::size_type
732 rfind(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
733 {
734 size_type __size = this->size();
735 if (__size)
736 {
737 if (--__size > __pos)
738 __size = __pos;
739 for (++__size; __size-- > 0; )
740 if (traits_type::eq(_M_data()[__size], __c))
741 return __size;
742 }
743 return npos;
744 }
745
746 template<typename _CharT, typename _Traits, typename _Alloc>
747 _GLIBCXX_STRING_CONSTEXPR
748 typename basic_string<_CharT, _Traits, _Alloc>::size_type
750 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
751 _GLIBCXX_NOEXCEPT
752 {
753 __glibcxx_requires_string_len(__s, __n);
754 for (; __n && __pos < this->size(); ++__pos)
755 {
756 const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
757 if (__p)
758 return __pos;
759 }
760 return npos;
761 }
762
763 template<typename _CharT, typename _Traits, typename _Alloc>
764 _GLIBCXX_STRING_CONSTEXPR
765 typename basic_string<_CharT, _Traits, _Alloc>::size_type
767 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
768 _GLIBCXX_NOEXCEPT
769 {
770 __glibcxx_requires_string_len(__s, __n);
771 size_type __size = this->size();
772 if (__size && __n)
773 {
774 if (--__size > __pos)
775 __size = __pos;
776 do
777 {
778 if (traits_type::find(__s, __n, _M_data()[__size]))
779 return __size;
780 }
781 while (__size-- != 0);
782 }
783 return npos;
784 }
785
786 template<typename _CharT, typename _Traits, typename _Alloc>
787 _GLIBCXX_STRING_CONSTEXPR
788 typename basic_string<_CharT, _Traits, _Alloc>::size_type
790 find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
791 _GLIBCXX_NOEXCEPT
792 {
793 __glibcxx_requires_string_len(__s, __n);
794 for (; __pos < this->size(); ++__pos)
795 if (!traits_type::find(__s, __n, _M_data()[__pos]))
796 return __pos;
797 return npos;
798 }
799
800 template<typename _CharT, typename _Traits, typename _Alloc>
801 _GLIBCXX_STRING_CONSTEXPR
802 typename basic_string<_CharT, _Traits, _Alloc>::size_type
804 find_first_not_of(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
805 {
806 for (; __pos < this->size(); ++__pos)
807 if (!traits_type::eq(_M_data()[__pos], __c))
808 return __pos;
809 return npos;
810 }
811
812 template<typename _CharT, typename _Traits, typename _Alloc>
813 _GLIBCXX_STRING_CONSTEXPR
814 typename basic_string<_CharT, _Traits, _Alloc>::size_type
816 find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
817 _GLIBCXX_NOEXCEPT
818 {
819 __glibcxx_requires_string_len(__s, __n);
820 size_type __size = this->size();
821 if (__size)
822 {
823 if (--__size > __pos)
824 __size = __pos;
825 do
826 {
827 if (!traits_type::find(__s, __n, _M_data()[__size]))
828 return __size;
829 }
830 while (__size--);
831 }
832 return npos;
833 }
834
835 template<typename _CharT, typename _Traits, typename _Alloc>
836 _GLIBCXX_STRING_CONSTEXPR
837 typename basic_string<_CharT, _Traits, _Alloc>::size_type
839 find_last_not_of(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
840 {
841 size_type __size = this->size();
842 if (__size)
843 {
844 if (--__size > __pos)
845 __size = __pos;
846 do
847 {
848 if (!traits_type::eq(_M_data()[__size], __c))
849 return __size;
850 }
851 while (__size--);
852 }
853 return npos;
854 }
855
856#undef _GLIBCXX_STRING_CONSTEXPR
857
858 // 21.3.7.9 basic_string::getline and operators
859 template<typename _CharT, typename _Traits, typename _Alloc>
863 {
864 typedef basic_istream<_CharT, _Traits> __istream_type;
865 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
866 typedef typename __istream_type::ios_base __ios_base;
867 typedef typename __istream_type::int_type __int_type;
868 typedef typename __string_type::size_type __size_type;
869 typedef ctype<_CharT> __ctype_type;
870 typedef typename __ctype_type::ctype_base __ctype_base;
871
872 __size_type __extracted = 0;
873 typename __ios_base::iostate __err = __ios_base::goodbit;
874 typename __istream_type::sentry __cerb(__in, false);
875 if (__cerb)
876 {
877 __try
878 {
879 // Avoid reallocation for common case.
880 __str.erase();
881 _CharT __buf[128];
882 __size_type __len = 0;
883 const streamsize __w = __in.width();
884 const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
885 : __str.max_size();
886 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
887 const __int_type __eof = _Traits::eof();
888 __int_type __c = __in.rdbuf()->sgetc();
889
890 while (__extracted < __n
891 && !_Traits::eq_int_type(__c, __eof)
892 && !__ct.is(__ctype_base::space,
893 _Traits::to_char_type(__c)))
894 {
895 if (__len == sizeof(__buf) / sizeof(_CharT))
896 {
897 __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
898 __len = 0;
899 }
900 __buf[__len++] = _Traits::to_char_type(__c);
901 ++__extracted;
902 __c = __in.rdbuf()->snextc();
903 }
904 __str.append(__buf, __len);
905
906 if (__extracted < __n && _Traits::eq_int_type(__c, __eof))
907 __err |= __ios_base::eofbit;
908 __in.width(0);
909 }
911 {
912 __in._M_setstate(__ios_base::badbit);
913 __throw_exception_again;
914 }
915 __catch(...)
916 {
917 // _GLIBCXX_RESOLVE_LIB_DEFECTS
918 // 91. Description of operator>> and getline() for string<>
919 // might cause endless loop
920 __in._M_setstate(__ios_base::badbit);
921 }
922 }
923 // 211. operator>>(istream&, string&) doesn't set failbit
924 if (!__extracted)
925 __err |= __ios_base::failbit;
926 if (__err)
927 __in.setstate(__err);
928 return __in;
929 }
930
931 template<typename _CharT, typename _Traits, typename _Alloc>
932 basic_istream<_CharT, _Traits>&
934 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
935 {
936 typedef basic_istream<_CharT, _Traits> __istream_type;
937 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
938 typedef typename __istream_type::ios_base __ios_base;
939 typedef typename __istream_type::int_type __int_type;
940 typedef typename __string_type::size_type __size_type;
941
942 __size_type __extracted = 0;
943 const __size_type __n = __str.max_size();
944 typename __ios_base::iostate __err = __ios_base::goodbit;
945 typename __istream_type::sentry __cerb(__in, true);
946 if (__cerb)
947 {
948 __try
949 {
950 __str.erase();
951 const __int_type __idelim = _Traits::to_int_type(__delim);
952 const __int_type __eof = _Traits::eof();
953 __int_type __c = __in.rdbuf()->sgetc();
954
955 while (__extracted < __n
956 && !_Traits::eq_int_type(__c, __eof)
957 && !_Traits::eq_int_type(__c, __idelim))
958 {
959 __str += _Traits::to_char_type(__c);
960 ++__extracted;
961 __c = __in.rdbuf()->snextc();
962 }
963
964 if (_Traits::eq_int_type(__c, __eof))
965 __err |= __ios_base::eofbit;
966 else if (_Traits::eq_int_type(__c, __idelim))
967 {
968 ++__extracted;
969 __in.rdbuf()->sbumpc();
970 }
971 else
972 __err |= __ios_base::failbit;
973 }
975 {
976 __in._M_setstate(__ios_base::badbit);
977 __throw_exception_again;
978 }
979 __catch(...)
980 {
981 // _GLIBCXX_RESOLVE_LIB_DEFECTS
982 // 91. Description of operator>> and getline() for string<>
983 // might cause endless loop
984 __in._M_setstate(__ios_base::badbit);
985 }
986 }
987 if (!__extracted)
988 __err |= __ios_base::failbit;
989 if (__err)
990 __in.setstate(__err);
991 return __in;
992 }
993
994 // Inhibit implicit instantiations for required instantiations,
995 // which are defined via explicit instantiations elsewhere.
996#if _GLIBCXX_EXTERN_TEMPLATE
997 // The explicit instantiation definitions in src/c++11/string-inst.cc and
998 // src/c++17/string-inst.cc only instantiate the members required for C++17
999 // and earlier standards (so not C++20's starts_with and ends_with).
1000 // Suppress the explicit instantiation declarations for C++20, so C++20
1001 // code will implicitly instantiate std::string and std::wstring as needed.
1002# if __cplusplus <= 201703L && _GLIBCXX_EXTERN_TEMPLATE > 0
1003 extern template class basic_string<char>;
1004# elif ! _GLIBCXX_USE_CXX11_ABI
1005 // Still need to prevent implicit instantiation of the COW empty rep,
1006 // to ensure the definition in libstdc++.so is unique (PR 86138).
1007 extern template basic_string<char>::size_type
1008 basic_string<char>::_Rep::_S_empty_rep_storage[];
1009# elif _GLIBCXX_EXTERN_TEMPLATE > 0
1010 // Export _M_replace_cold even for C++20.
1011 extern template void
1012 basic_string<char>::_M_replace_cold(char *, size_type, const char*,
1013 const size_type, const size_type);
1014# endif
1015
1016 extern template
1017 basic_istream<char>&
1018 operator>>(basic_istream<char>&, string&);
1019 extern template
1020 basic_ostream<char>&
1021 operator<<(basic_ostream<char>&, const string&);
1022 extern template
1023 basic_istream<char>&
1024 getline(basic_istream<char>&, string&, char);
1025 extern template
1026 basic_istream<char>&
1027 getline(basic_istream<char>&, string&);
1028
1029#ifdef _GLIBCXX_USE_WCHAR_T
1030# if __cplusplus <= 201703L && _GLIBCXX_EXTERN_TEMPLATE > 0
1031 extern template class basic_string<wchar_t>;
1032# elif ! _GLIBCXX_USE_CXX11_ABI
1033 extern template basic_string<wchar_t>::size_type
1034 basic_string<wchar_t>::_Rep::_S_empty_rep_storage[];
1035# elif _GLIBCXX_EXTERN_TEMPLATE > 0
1036 // Export _M_replace_cold even for C++20.
1037 extern template void
1038 basic_string<wchar_t>::_M_replace_cold(wchar_t*, size_type, const wchar_t*,
1039 const size_type, const size_type);
1040# endif
1041
1042 extern template
1043 basic_istream<wchar_t>&
1044 operator>>(basic_istream<wchar_t>&, wstring&);
1045 extern template
1046 basic_ostream<wchar_t>&
1047 operator<<(basic_ostream<wchar_t>&, const wstring&);
1048 extern template
1049 basic_istream<wchar_t>&
1050 getline(basic_istream<wchar_t>&, wstring&, wchar_t);
1051 extern template
1052 basic_istream<wchar_t>&
1053 getline(basic_istream<wchar_t>&, wstring&);
1054#endif // _GLIBCXX_USE_WCHAR_T
1055#endif // _GLIBCXX_EXTERN_TEMPLATE
1056
1057_GLIBCXX_END_NAMESPACE_VERSION
1058} // namespace std
1059
1060#endif
constexpr complex< _Tp > operator+(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x plus y.
Definition: complex:332
constexpr bool is_constant_evaluated() noexcept
Returns true only when called during constant evaluation.
Definition: type_traits:3579
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:104
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition: move.h:49
_Tp * begin(valarray< _Tp > &__va) noexcept
Return an iterator pointing to the first element of the valarray.
Definition: valarray:1217
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:80
ISO C++ entities toplevel namespace is std.
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
Definition: postypes.h:68
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 iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
Definition: range_access.h:264
std::basic_istream< _CharT, _Traits > & operator>>(std::basic_istream< _CharT, _Traits > &__is, bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition: bitset:1599
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition: bitset:1685
constexpr auto data(_Container &__cont) noexcept(noexcept(__cont.data())) -> decltype(__cont.data())
Return the data pointer of a container.
Definition: range_access.h:311
void setstate(iostate __state)
Sets additional flags in the error state.
Definition: basic_ios.h:157
basic_streambuf< _CharT, _Traits > * rdbuf() const
Accessing the underlying buffer.
Definition: basic_ios.h:321
Template class basic_istream.
Definition: istream:61
Managing sequences of characters and character-like objects.
Definition: cow_string.h:115
void swap(basic_string &__s) noexcept(/*conditional */)
Swap contents with another string.
Definition: cow_string.h:3504
size_type find_first_of(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a character of string.
Definition: cow_string.h:2411
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.
Definition: cow_string.h:2660
size_type find_first_not_of(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a character not in string.
Definition: cow_string.h:2578
size_type copy(_CharT *__s, size_type __n, size_type __pos=0) const
Copy substring into C string.
Definition: cow_string.h:3700
size_type find_last_of(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a character of string.
Definition: cow_string.h:2495
size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
Definition: cow_string.h:913
size_type rfind(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a string.
Definition: cow_string.h:2332
void resize(size_type __n, _CharT __c)
Resizes the string to the specified number of characters.
Definition: cow_string.h:3626
void reserve()
Equivalent to shrink_to_fit().
Definition: cow_string.h:3679
basic_string & append(const basic_string &__str)
Append a string to this string.
Definition: cow_string.h:3298
static const size_type npos
Value returned by various member functions when they fail.
Definition: cow_string.h:328
allocator_type get_allocator() const noexcept
Return copy of allocator used to construct this string.
Definition: cow_string.h:2240
size_type max_size() const noexcept
Returns the size() of the largest possible string.
Definition: cow_string.h:924
basic_string & erase(size_type __pos=0, size_type __n=npos)
Remove characters.
Definition: cow_string.h:1709
Thrown as part of forced unwinding.
Definition: cxxabi_forced.h:49
streamsize width() const
Flags access.
Definition: ios_base.h:755
locale getloc() const
Locale access.
Definition: ios_base.h:806
Primary class template ctype facet.
Marking input iterators.
Forward iterators support a superset of input iterator operations.
Uniform interface to C++98 and C++11 allocators.