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