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 // NB: size() (i.e. _M_length) is initialized only at the end.
203 while (__beg != __end && __len < __capacity)
204 {
205 _M_local_buf[__len++] = *__beg;
206 ++__beg;
207 }
208
209 struct _Guard
210 {
211 _GLIBCXX20_CONSTEXPR
212 explicit _Guard(basic_string* __s) : _M_guarded(__s) { }
213
214 _GLIBCXX20_CONSTEXPR
215 ~_Guard() { if (_M_guarded) _M_guarded->_M_dispose(); }
216
217 basic_string* _M_guarded;
218 } __guard(this);
219
220 while (__beg != __end)
221 {
222 if (__len == __capacity)
223 {
224 // Allocate more space.
225 _Alloc_result __another = _M_create_plus(__len + 1, __len);
226 __capacity = __another.__count - 1; // Leave room for NUL.
227 this->_S_copy(__another.__ptr, _M_data(), __len);
228 _M_dispose();
229 _M_data(__another.__ptr);
230 _M_capacity(__capacity);
231 }
232
233#if __cplusplus >= 201103L
234# pragma GCC diagnostic push
235# pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
236
240 {
241 auto __r = std::__copy_n_a(__beg, __capacity - __len,
242 _M_data() + __len, false);
243 __len = __r - data();
244 // We continue using __beg after passing it to copy_n_a.
245 // That is permitted for streambuf iterators.
246 continue;
247 }
248
249# pragma GCC diagnostic pop
250#endif
251 traits_type::assign(_M_data()[__len++],
252 static_cast<_CharT>(*__beg));
253 ++__beg;
254 }
255
256 __guard._M_guarded = 0;
257
258 _M_set_length(__len);
259 }
260
261 template<typename _CharT, typename _Traits, typename _Alloc>
262 template<typename _InIterator>
263 _GLIBCXX20_CONSTEXPR
264 void
266 _M_construct(_InIterator __beg, _InIterator __end,
267 std::forward_iterator_tag)
268 {
269 size_type __dnew = static_cast<size_type>(std::distance(__beg, __end));
270
271 if (__dnew > size_type(_S_local_capacity))
272 _M_create_and_place(__dnew, size_type(0));
273 else
274 _M_init_local_buf();
275
276 // Check for out_of_range and length_error exceptions.
277 struct _Guard
278 {
279 _GLIBCXX20_CONSTEXPR
280 explicit _Guard(basic_string* __s) : _M_guarded(__s) { }
281
282 _GLIBCXX20_CONSTEXPR
283 ~_Guard() { if (_M_guarded) _M_guarded->_M_dispose(); }
284
285 basic_string* _M_guarded;
286 } __guard(this);
287
288 this->_S_copy_chars(_M_data(), __beg, __end);
289
290 __guard._M_guarded = 0;
291
292 _M_set_length(__dnew);
293 }
294
295 template<typename _CharT, typename _Traits, typename _Alloc>
296 _GLIBCXX20_CONSTEXPR
297 void
299 _M_construct(size_type __n, _CharT __c)
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)
307 this->_S_assign(_M_data(), __n, __c);
308
309 _M_set_length(__n);
310 }
311
312 // Length of string constructed is easier to propagate inter-procedurally
313 // than difference between iterators.
314 template<typename _CharT, typename _Traits, typename _Alloc>
315 template<bool _Terminated>
316 _GLIBCXX20_CONSTEXPR
317 void
319 _M_construct(const _CharT* __str, size_type __n)
320 {
321 if (__n > size_type(_S_local_capacity))
322 _M_create_and_place(__n, size_type(0));
323 else
324 _M_init_local_buf();
325
326 if (__n || _Terminated)
327 this->_S_copy(_M_data(), __str, __n + _Terminated);
328
329 _M_length(__n);
330 if (!_Terminated)
331 traits_type::assign(_M_data()[__n], _CharT());
332 }
333
334#if __cplusplus >= 202302L
335 template<typename _CharT, typename _Traits, typename _Alloc>
336 constexpr void
339 {
340 const _CharT* __start = __str._M_data() + __pos;
341 if (__n <= _S_local_capacity)
342 {
343 _M_init_local_buf();
344 traits_type::copy(_M_local_buf, __start, __n);
345 _M_set_length(__n);
346 return;
347 }
348
350 if (get_allocator() != __str.get_allocator())
351 {
352 _M_construct<false>(__start, __n);
353 return;
354 }
355
356 _M_data(__str._M_data());
357 _M_capacity(__str._M_allocated_capacity);
358 __str._M_data(__str._M_use_local_data());
359 __str._M_set_length(0);
360
361 _S_move(_M_data(), _M_data() + __pos, __n);
362 _M_set_length(__n);
363 }
364#endif // C++23
365
366 template<typename _CharT, typename _Traits, typename _Alloc>
367 _GLIBCXX20_CONSTEXPR
368 void
370 _M_assign(const basic_string& __str)
371 {
372 if (this != std::__addressof(__str))
373 {
374 const size_type __rsize = __str.length();
375 const size_type __capacity = capacity();
376
377 if (__rsize > __capacity)
378 {
379 // if _M_create_plus throws, there is no effect.
380 _Alloc_result __tmp = _M_create_plus(__rsize, __capacity);
381 _M_dispose();
382 _M_data(__tmp.__ptr);
383 _M_capacity(__tmp.__count - 1);
384 }
385
386 if (__rsize)
387 this->_S_copy(_M_data(), __str._M_data(), __rsize);
388
389 _M_set_length(__rsize);
390 }
391 }
392
393 template<typename _CharT, typename _Traits, typename _Alloc>
394 _GLIBCXX20_CONSTEXPR
395 void
397 reserve(size_type __res)
398 {
399 const size_type __capacity = capacity();
400 // _GLIBCXX_RESOLVE_LIB_DEFECTS
401 // 2968. Inconsistencies between basic_string reserve and
402 // vector/unordered_map/unordered_set reserve functions
403 // P0966 reserve should not shrink
404 if (__res <= __capacity)
405 return;
406
407 _Alloc_result __r = _M_create_plus(__res, __capacity);
408 this->_S_copy(__r.__ptr, _M_data(), length() + 1);
409 _M_dispose();
410 _M_data(__r.__ptr);
411 _M_capacity(__r.__count - 1); // Leave room for NUL.
412 }
413
414 template<typename _CharT, typename _Traits, typename _Alloc>
415 _GLIBCXX20_CONSTEXPR
416 void
418 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
419 size_type __len2)
420 {
421 const size_type __how_much = length() - __pos - __len1;
422
423 size_type __new_capacity = length() + __len2 - __len1;
424 _Alloc_result __r = _M_create_plus(__new_capacity, capacity());
425
426 if (__pos)
427 this->_S_copy(__r.__ptr, _M_data(), __pos);
428 if (__s && __len2)
429 this->_S_copy(__r.__ptr + __pos, __s, __len2);
430 if (__how_much)
431 this->_S_copy(__r.__ptr + __pos + __len2,
432 _M_data() + __pos + __len1, __how_much);
433
434 _M_dispose();
435 _M_data(__r.__ptr);
436 _M_capacity(__r.__count - 1); // Leave room for NUL.
437 }
438
439 template<typename _CharT, typename _Traits, typename _Alloc>
440 _GLIBCXX20_CONSTEXPR
441 void
443 _M_erase(size_type __pos, size_type __n)
444 {
445 const size_type __how_much = length() - __pos - __n;
446
447 if (__how_much && __n)
448 this->_S_move(_M_data() + __pos, _M_data() + __pos + __n, __how_much);
449
450 _M_set_length(length() - __n);
451 }
452
453 template<typename _CharT, typename _Traits, typename _Alloc>
454 _GLIBCXX20_CONSTEXPR
455 void
457 reserve()
458 {
459 if (_M_is_local())
460 return;
461
462 const size_type __length = length();
463 const size_type __capacity = _M_allocated_capacity;
464
465 if (__length <= size_type(_S_local_capacity))
466 {
467 _M_init_local_buf();
468 this->_S_copy(_M_local_buf, _M_data(), __length + 1);
469 _M_destroy(__capacity);
470 _M_data(_M_local_data());
471 return;
472 }
473#if __cpp_exceptions
474#ifdef __glibcxx_allocate_at_least // C++23
475 const size_type __limit =
476 (__STDCPP_DEFAULT_NEW_ALIGNMENT__ - 1) / sizeof(_CharT);
477#else
478 const size_type __limit = 0;
479#endif
480 if (__capacity - __length > __limit )
481 try
482 {
483 _Alloc_result __r = _S_allocate_at_least(
484 _M_get_allocator(), __length + 1);
485 this->_S_copy(__r.__ptr, _M_data(), __length + 1);
486 _M_dispose();
487 _M_data(__r.__ptr);
488 _M_capacity(__r.__count - 1); // reserve room for NUL.
489 }
490 catch (const __cxxabiv1::__forced_unwind&)
491 { throw; }
492 catch (...)
493 { /* swallow the exception */ }
494#endif
495 }
496
497 template<typename _CharT, typename _Traits, typename _Alloc>
498 _GLIBCXX20_CONSTEXPR
499 void
501 resize(size_type __n, _CharT __c)
502 {
503 const size_type __size = this->size();
504 if (__size < __n)
505 this->append(__n - __size, __c);
506 else if (__n < __size)
507 this->_M_set_length(__n);
508 }
509
510 template<typename _CharT, typename _Traits, typename _Alloc>
511 _GLIBCXX20_CONSTEXPR
514 _M_append(const _CharT* __s, size_type __n)
515 {
516 const size_type __len = __n + this->size();
517
518 if (__len <= this->capacity())
519 {
520 if (__n)
521 this->_S_copy(this->_M_data() + this->size(), __s, __n);
522 }
523 else
524 this->_M_mutate(this->size(), size_type(0), __s, __n);
525
526 this->_M_set_length(__len);
527 return *this;
528 }
529
530 template<typename _CharT, typename _Traits, typename _Alloc>
531 template<typename _InputIterator>
532 _GLIBCXX20_CONSTEXPR
535 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
536 _InputIterator __k1, _InputIterator __k2,
537 std::__false_type)
538 {
539 // _GLIBCXX_RESOLVE_LIB_DEFECTS
540 // 2788. unintentionally require a default constructible allocator
541 const basic_string __s(__k1, __k2, this->get_allocator());
542 const size_type __n1 = __i2 - __i1;
543 return _M_replace(__i1 - begin(), __n1, __s._M_data(),
544 __s.size());
545 }
546
547 template<typename _CharT, typename _Traits, typename _Alloc>
548 _GLIBCXX20_CONSTEXPR
552 _CharT __c)
553 {
554 _M_check_length(__n1, __n2, "basic_string::_M_replace_aux");
555
556 const size_type __old_size = this->size();
557 const size_type __new_size = __old_size + __n2 - __n1;
558
559 if (__new_size <= this->capacity())
560 {
561 pointer __p = this->_M_data() + __pos1;
562
563 const size_type __how_much = __old_size - __pos1 - __n1;
564 if (__how_much && __n1 != __n2)
565 this->_S_move(__p + __n2, __p + __n1, __how_much);
566 }
567 else
568 this->_M_mutate(__pos1, __n1, 0, __n2);
569
570 if (__n2)
571 this->_S_assign(this->_M_data() + __pos1, __n2, __c);
572
573 this->_M_set_length(__new_size);
574 return *this;
575 }
576
577 template<typename _CharT, typename _Traits, typename _Alloc>
578 __attribute__((__noinline__, __noclone__, __cold__)) void
580 _M_replace_cold(pointer __p, size_type __len1, const _CharT* __s,
581 const size_type __len2, const size_type __how_much)
582 {
583 // Work in-place.
584 if (__len2 && __len2 <= __len1)
585 this->_S_move(__p, __s, __len2);
586 if (__how_much && __len1 != __len2)
587 this->_S_move(__p + __len2, __p + __len1, __how_much);
588 if (__len2 > __len1)
589 {
590 if (__s + __len2 <= __p + __len1)
591 this->_S_move(__p, __s, __len2);
592 else if (__s >= __p + __len1)
593 {
594 // Hint to middle end that __p and __s overlap
595 // (PR 98465).
596 const size_type __poff = (__s - __p) + (__len2 - __len1);
597 this->_S_copy(__p, __p + __poff, __len2);
598 }
599 else
600 {
601 const size_type __nleft = (__p + __len1) - __s;
602 this->_S_move(__p, __s, __nleft);
603 this->_S_copy(__p + __nleft, __p + __len2, __len2 - __nleft);
604 }
605 }
606 }
607
608 template<typename _CharT, typename _Traits, typename _Alloc>
609 _GLIBCXX20_CONSTEXPR
612 _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
613 const size_type __len2)
614 {
615 _M_check_length(__len1, __len2, "basic_string::_M_replace");
616
617 const size_type __old_size = this->size();
618 const size_type __new_size = __old_size + __len2 - __len1;
619
620 if (__new_size <= this->capacity())
621 {
622 pointer __p = this->_M_data() + __pos;
623
624 const size_type __how_much = __old_size - __pos - __len1;
625#if __cpp_lib_is_constant_evaluated
626 if (std::is_constant_evaluated())
627 {
628 auto __newp =
629 _S_allocate_at_least(_M_get_allocator(), __new_size).__ptr;
630 _S_copy(__newp, this->_M_data(), __pos);
631 _S_copy(__newp + __pos, __s, __len2);
632 _S_copy(__newp + __pos + __len2, __p + __len1, __how_much);
633 _S_copy(this->_M_data(), __newp, __new_size);
634 this->_M_get_allocator().deallocate(__newp, __new_size);
635 }
636 else
637#endif
638 if (__builtin_expect(_M_disjunct(__s), true))
639 {
640 if (__how_much && __len1 != __len2)
641 this->_S_move(__p + __len2, __p + __len1, __how_much);
642 if (__len2)
643 this->_S_copy(__p, __s, __len2);
644 }
645 else
646 _M_replace_cold(__p, __len1, __s, __len2, __how_much);
647 }
648 else
649 this->_M_mutate(__pos, __len1, __s, __len2);
650
651 this->_M_set_length(__new_size);
652 return *this;
653 }
654
655 template<typename _CharT, typename _Traits, typename _Alloc>
656 _GLIBCXX20_CONSTEXPR
657 typename basic_string<_CharT, _Traits, _Alloc>::size_type
659 copy(_CharT* __s, size_type __n, size_type __pos) const
660 {
661 _M_check(__pos, "basic_string::copy");
662 __n = _M_limit(__pos, __n);
663 __glibcxx_requires_string_len(__s, __n);
664 if (__n)
665 _S_copy(__s, _M_data() + __pos, __n);
666 // 21.3.5.7 par 3: do not append null. (good.)
667 return __n;
668 }
669
670#ifdef __glibcxx_string_resize_and_overwrite // C++ >= 23
671 template<typename _CharT, typename _Traits, typename _Alloc>
672 template<typename _Operation>
673 [[__gnu__::__always_inline__]]
674 constexpr void
676 __resize_and_overwrite(const size_type __n, _Operation __op)
677 { resize_and_overwrite<_Operation&>(__n, __op); }
678#endif
679
680#if __cplusplus >= 201103L
681 template<typename _CharT, typename _Traits, typename _Alloc>
682 template<typename _Operation>
683 _GLIBCXX20_CONSTEXPR void
685#ifdef __glibcxx_string_resize_and_overwrite // C++ >= 23
686 resize_and_overwrite(const size_type __n, _Operation __op)
687#else
688 __resize_and_overwrite(const size_type __n, _Operation __op)
689#endif
690 {
691 reserve(__n);
692 _CharT* const __p = _M_data();
693#if __cpp_lib_is_constant_evaluated
694 if (std::__is_constant_evaluated() && __n > size())
695 traits_type::assign(__p + size(), __n - size(), _CharT());
696#endif
697 struct _Terminator {
698 _GLIBCXX20_CONSTEXPR ~_Terminator() { _M_this->_M_set_length(_M_r); }
699 basic_string* _M_this;
700 size_type _M_r;
701 };
702 _Terminator __term{this, 0};
703 auto __r = std::move(__op)(__p + 0, __n + 0);
704#ifdef __cpp_lib_concepts
705 static_assert(ranges::__detail::__is_integer_like<decltype(__r)>);
706#else
707 static_assert(__gnu_cxx::__is_integer_nonstrict<decltype(__r)>::__value,
708 "resize_and_overwrite operation must return an integer");
709#endif
710 _GLIBCXX_DEBUG_ASSERT(__r >= 0 && size_type(__r) <= __n);
711 __term._M_r = size_type(__r);
712 if (__term._M_r > __n)
713 __builtin_unreachable();
714 }
715#endif // C++11
716
717#endif // _GLIBCXX_USE_CXX11_ABI
718
719#if __glibcxx_constexpr_string >= 201907L
720# define _GLIBCXX_STRING_CONSTEXPR constexpr
721#else
722# define _GLIBCXX_STRING_CONSTEXPR
723#endif
724 template<typename _CharT, typename _Traits, typename _Alloc>
725 _GLIBCXX_STRING_CONSTEXPR
726 typename basic_string<_CharT, _Traits, _Alloc>::size_type
728 find(const _CharT* __s, size_type __pos, size_type __n) const
729 _GLIBCXX_NOEXCEPT
730 {
731 __glibcxx_requires_string_len(__s, __n);
732 const size_type __size = this->size();
733
734 if (__n == 0)
735 return __pos <= __size ? __pos : npos;
736 if (__pos >= __size)
737 return npos;
738
739 const _CharT __elem0 = __s[0];
740 const _CharT* const __data = data();
741 const _CharT* __first = __data + __pos;
742 const _CharT* const __last = __data + __size;
743 size_type __len = __size - __pos;
744
745 while (__len >= __n)
746 {
747 // Find the first occurrence of __elem0:
748 __first = traits_type::find(__first, __len - __n + 1, __elem0);
749 if (!__first)
750 return npos;
751 // Compare the full strings from the first occurrence of __elem0.
752 // We already know that __first[0] == __s[0] but compare them again
753 // anyway because __s is probably aligned, which helps memcmp.
754 if (traits_type::compare(__first, __s, __n) == 0)
755 return __first - __data;
756 __len = __last - ++__first;
757 }
758 return npos;
759 }
760
761 template<typename _CharT, typename _Traits, typename _Alloc>
762 _GLIBCXX_STRING_CONSTEXPR
763 typename basic_string<_CharT, _Traits, _Alloc>::size_type
765 find(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
766 {
767 size_type __ret = npos;
768 const size_type __size = this->size();
769 if (__pos < __size)
770 {
771 const _CharT* __data = _M_data();
772 const size_type __n = __size - __pos;
773 const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
774 if (__p)
775 __ret = __p - __data;
776 }
777 return __ret;
778 }
779
780 template<typename _CharT, typename _Traits, typename _Alloc>
781 _GLIBCXX_STRING_CONSTEXPR
782 typename basic_string<_CharT, _Traits, _Alloc>::size_type
784 rfind(const _CharT* __s, size_type __pos, size_type __n) const
785 _GLIBCXX_NOEXCEPT
786 {
787 __glibcxx_requires_string_len(__s, __n);
788 const size_type __size = this->size();
789 if (__n <= __size)
790 {
791 __pos = std::min(size_type(__size - __n), __pos);
792 const _CharT* __data = _M_data();
793 do
794 {
795 if (traits_type::compare(__data + __pos, __s, __n) == 0)
796 return __pos;
797 }
798 while (__pos-- > 0);
799 }
800 return npos;
801 }
802
803 template<typename _CharT, typename _Traits, typename _Alloc>
804 _GLIBCXX_STRING_CONSTEXPR
805 typename basic_string<_CharT, _Traits, _Alloc>::size_type
807 rfind(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
808 {
809 size_type __size = this->size();
810 if (__size)
811 {
812 if (--__size > __pos)
813 __size = __pos;
814 for (++__size; __size-- > 0; )
815 if (traits_type::eq(_M_data()[__size], __c))
816 return __size;
817 }
818 return npos;
819 }
820
821 template<typename _CharT, typename _Traits, typename _Alloc>
822 _GLIBCXX_STRING_CONSTEXPR
823 typename basic_string<_CharT, _Traits, _Alloc>::size_type
825 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
826 _GLIBCXX_NOEXCEPT
827 {
828 __glibcxx_requires_string_len(__s, __n);
829 for (; __n && __pos < this->size(); ++__pos)
830 {
831 const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
832 if (__p)
833 return __pos;
834 }
835 return npos;
836 }
837
838 template<typename _CharT, typename _Traits, typename _Alloc>
839 _GLIBCXX_STRING_CONSTEXPR
840 typename basic_string<_CharT, _Traits, _Alloc>::size_type
842 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
843 _GLIBCXX_NOEXCEPT
844 {
845 __glibcxx_requires_string_len(__s, __n);
846 size_type __size = this->size();
847 if (__size && __n)
848 {
849 if (--__size > __pos)
850 __size = __pos;
851 do
852 {
853 if (traits_type::find(__s, __n, _M_data()[__size]))
854 return __size;
855 }
856 while (__size-- != 0);
857 }
858 return npos;
859 }
860
861 template<typename _CharT, typename _Traits, typename _Alloc>
862 _GLIBCXX_STRING_CONSTEXPR
863 typename basic_string<_CharT, _Traits, _Alloc>::size_type
865 find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
866 _GLIBCXX_NOEXCEPT
867 {
868 __glibcxx_requires_string_len(__s, __n);
869 for (; __pos < this->size(); ++__pos)
870 if (!traits_type::find(__s, __n, _M_data()[__pos]))
871 return __pos;
872 return npos;
873 }
874
875 template<typename _CharT, typename _Traits, typename _Alloc>
876 _GLIBCXX_STRING_CONSTEXPR
877 typename basic_string<_CharT, _Traits, _Alloc>::size_type
879 find_first_not_of(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
880 {
881 for (; __pos < this->size(); ++__pos)
882 if (!traits_type::eq(_M_data()[__pos], __c))
883 return __pos;
884 return npos;
885 }
886
887 template<typename _CharT, typename _Traits, typename _Alloc>
888 _GLIBCXX_STRING_CONSTEXPR
889 typename basic_string<_CharT, _Traits, _Alloc>::size_type
891 find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
892 _GLIBCXX_NOEXCEPT
893 {
894 __glibcxx_requires_string_len(__s, __n);
895 size_type __size = this->size();
896 if (__size)
897 {
898 if (--__size > __pos)
899 __size = __pos;
900 do
901 {
902 if (!traits_type::find(__s, __n, _M_data()[__size]))
903 return __size;
904 }
905 while (__size--);
906 }
907 return npos;
908 }
909
910 template<typename _CharT, typename _Traits, typename _Alloc>
911 _GLIBCXX_STRING_CONSTEXPR
912 typename basic_string<_CharT, _Traits, _Alloc>::size_type
914 find_last_not_of(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
915 {
916 size_type __size = this->size();
917 if (__size)
918 {
919 if (--__size > __pos)
920 __size = __pos;
921 do
922 {
923 if (!traits_type::eq(_M_data()[__size], __c))
924 return __size;
925 }
926 while (__size--);
927 }
928 return npos;
929 }
930
931#undef _GLIBCXX_STRING_CONSTEXPR
932
933 // 21.3.7.9 basic_string::getline and operators
934 template<typename _CharT, typename _Traits, typename _Alloc>
938 {
939 typedef basic_istream<_CharT, _Traits> __istream_type;
940 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
941 typedef typename __istream_type::ios_base __ios_base;
942 typedef typename __istream_type::int_type __int_type;
943 typedef typename __string_type::size_type __size_type;
944 typedef ctype<_CharT> __ctype_type;
945 typedef typename __ctype_type::ctype_base __ctype_base;
946
947 __size_type __extracted = 0;
948 typename __ios_base::iostate __err = __ios_base::goodbit;
949 typename __istream_type::sentry __cerb(__in, false);
950 if (__cerb)
951 {
952 __try
953 {
954 // Avoid reallocation for common case.
955 __str.erase();
956 _CharT __buf[128];
957 __size_type __len = 0;
958 const streamsize __w = __in.width();
959 const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
960 : __str.max_size();
961 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
962 const __int_type __eof = _Traits::eof();
963 __int_type __c = __in.rdbuf()->sgetc();
964
965 while (__extracted < __n
966 && !_Traits::eq_int_type(__c, __eof)
967 && !__ct.is(__ctype_base::space,
968 _Traits::to_char_type(__c)))
969 {
970 if (__len == sizeof(__buf) / sizeof(_CharT))
971 {
972 __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
973 __len = 0;
974 }
975 __buf[__len++] = _Traits::to_char_type(__c);
976 ++__extracted;
977 __c = __in.rdbuf()->snextc();
978 }
979 __str.append(__buf, __len);
980
981 if (__extracted < __n && _Traits::eq_int_type(__c, __eof))
982 __err |= __ios_base::eofbit;
983 __in.width(0);
984 }
986 {
987 __in._M_setstate(__ios_base::badbit);
988 __throw_exception_again;
989 }
990 __catch(...)
991 {
992 // _GLIBCXX_RESOLVE_LIB_DEFECTS
993 // 91. Description of operator>> and getline() for string<>
994 // might cause endless loop
995 __in._M_setstate(__ios_base::badbit);
996 }
997 }
998 // 211. operator>>(istream&, string&) doesn't set failbit
999 if (!__extracted)
1000 __err |= __ios_base::failbit;
1001 if (__err)
1002 __in.setstate(__err);
1003 return __in;
1004 }
1005
1006 template<typename _CharT, typename _Traits, typename _Alloc>
1007 basic_istream<_CharT, _Traits>&
1009 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
1010 {
1011 typedef basic_istream<_CharT, _Traits> __istream_type;
1012 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1013 typedef typename __istream_type::ios_base __ios_base;
1014 typedef typename __istream_type::int_type __int_type;
1015 typedef typename __string_type::size_type __size_type;
1016
1017 __size_type __extracted = 0;
1018 const __size_type __n = __str.max_size();
1019 typename __ios_base::iostate __err = __ios_base::goodbit;
1020 typename __istream_type::sentry __cerb(__in, true);
1021 if (__cerb)
1022 {
1023 __try
1024 {
1025 __str.erase();
1026 const __int_type __idelim = _Traits::to_int_type(__delim);
1027 const __int_type __eof = _Traits::eof();
1028 __int_type __c = __in.rdbuf()->sgetc();
1029
1030 while (__extracted < __n
1031 && !_Traits::eq_int_type(__c, __eof)
1032 && !_Traits::eq_int_type(__c, __idelim))
1033 {
1034 __str += _Traits::to_char_type(__c);
1035 ++__extracted;
1036 __c = __in.rdbuf()->snextc();
1037 }
1038
1039 if (_Traits::eq_int_type(__c, __eof))
1040 __err |= __ios_base::eofbit;
1041 else if (_Traits::eq_int_type(__c, __idelim))
1042 {
1043 ++__extracted;
1044 __in.rdbuf()->sbumpc();
1045 }
1046 else
1047 __err |= __ios_base::failbit;
1048 }
1050 {
1051 __in._M_setstate(__ios_base::badbit);
1052 __throw_exception_again;
1053 }
1054 __catch(...)
1055 {
1056 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1057 // 91. Description of operator>> and getline() for string<>
1058 // might cause endless loop
1059 __in._M_setstate(__ios_base::badbit);
1060 }
1061 }
1062 if (!__extracted)
1063 __err |= __ios_base::failbit;
1064 if (__err)
1065 __in.setstate(__err);
1066 return __in;
1067 }
1068
1069 // Inhibit implicit instantiations for required instantiations,
1070 // which are defined via explicit instantiations elsewhere.
1071#if _GLIBCXX_EXTERN_TEMPLATE
1072 // The explicit instantiation definitions in src/c++11/string-inst.cc,
1073 // src/c++17/string-inst.cc and src/c++20/string-inst.cc only instantiate
1074 // the members required for C++20 and earlier standards (so not C++23's
1075 // contains).
1076 // Suppress the explicit instantiation declarations for C++23, so C++23
1077 // code will implicitly instantiate std::string and std::wstring as needed.
1078# if __cplusplus <= 202002L && _GLIBCXX_EXTERN_TEMPLATE > 0
1079 extern template class basic_string<char>;
1080# elif ! _GLIBCXX_USE_CXX11_ABI
1081 // Still need to prevent implicit instantiation of the COW empty rep,
1082 // to ensure the definition in libstdc++.so is unique (PR 86138).
1083 extern template basic_string<char>::size_type
1084 basic_string<char>::_Rep::_S_empty_rep_storage[];
1085# elif _GLIBCXX_EXTERN_TEMPLATE > 0
1086 // Export _M_replace_cold even for C++23.
1087 extern template void
1088 basic_string<char>::_M_replace_cold(char *, size_type, const char*,
1089 const size_type, const size_type);
1090# endif
1091
1092 extern template
1093 basic_istream<char>&
1094 operator>>(basic_istream<char>&, string&);
1095 extern template
1096 basic_ostream<char>&
1097 operator<<(basic_ostream<char>&, const string&);
1098 extern template
1099 basic_istream<char>&
1100 getline(basic_istream<char>&, string&, char);
1101 extern template
1102 basic_istream<char>&
1103 getline(basic_istream<char>&, string&);
1104
1105#ifdef _GLIBCXX_USE_WCHAR_T
1106# if __cplusplus <= 202002L && _GLIBCXX_EXTERN_TEMPLATE > 0
1107 extern template class basic_string<wchar_t>;
1108# elif ! _GLIBCXX_USE_CXX11_ABI
1109 extern template basic_string<wchar_t>::size_type
1110 basic_string<wchar_t>::_Rep::_S_empty_rep_storage[];
1111# elif _GLIBCXX_EXTERN_TEMPLATE > 0
1112 // Export _M_replace_cold even for C++23.
1113 extern template void
1114 basic_string<wchar_t>::_M_replace_cold(wchar_t*, size_type, const wchar_t*,
1115 const size_type, const size_type);
1116# endif
1117
1118 extern template
1119 basic_istream<wchar_t>&
1120 operator>>(basic_istream<wchar_t>&, wstring&);
1121 extern template
1122 basic_ostream<wchar_t>&
1123 operator<<(basic_ostream<wchar_t>&, const wstring&);
1124 extern template
1125 basic_istream<wchar_t>&
1126 getline(basic_istream<wchar_t>&, wstring&, wchar_t);
1127 extern template
1128 basic_istream<wchar_t>&
1129 getline(basic_istream<wchar_t>&, wstring&);
1130#endif // _GLIBCXX_USE_WCHAR_T
1131#endif // _GLIBCXX_EXTERN_TEMPLATE
1132
1133_GLIBCXX_END_NAMESPACE_VERSION
1134} // namespace std
1135
1136#pragma GCC diagnostic pop
1137#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
Provides input iterator semantics for streambufs.
Primary class template ctype facet.