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