libstdc++: basic_string.tcc Source File

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
59 basic_string<_CharT, _Traits, _Alloc>::npos;
60
61 template<typename _CharT, typename _Traits, typename _Alloc>
62 _GLIBCXX20_CONSTEXPR
63 void
64 basic_string<_CharT, _Traits, _Alloc>::
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
143 basic_string<_CharT, _Traits, _Alloc>::
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
171 basic_string<_CharT, _Traits, _Alloc>::
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
187 basic_string<_CharT, _Traits, _Alloc>::
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
239 basic_string<_CharT, _Traits, _Alloc>::
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
272 basic_string<_CharT, _Traits, _Alloc>::
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
292 basic_string<_CharT, _Traits, _Alloc>::
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
311 basic_string<_CharT, _Traits, _Alloc>::
312 _M_construct(basic_string&& __str, size_type __pos, size_type __n)
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
323 if constexpr (!allocator_traits<_Alloc>::is_always_equal::value)
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
343 basic_string<_CharT, _Traits, _Alloc>::
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
370 basic_string<_CharT, _Traits, _Alloc>::
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
391 basic_string<_CharT, _Traits, _Alloc>::
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
416 basic_string<_CharT, _Traits, _Alloc>::
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
430 basic_string<_CharT, _Traits, _Alloc>::
431 reserve()
432 {
433 if (_M_is_local())
434 return;
435
436 const size_type __length = length();
437 const size_type __capacity = _M_allocated_capacity;
438
439 if (__length <= size_type(_S_local_capacity))
440 {
441 _M_init_local_buf();
442 this->_S_copy(_M_local_buf, _M_data(), __length + 1);
443 _M_destroy(__capacity);
444 _M_data(_M_local_data());
445 return;
446 }
447#if __cpp_exceptions
448#ifdef __glibcxx_allocate_at_least // C++23
449 const size_type __limit =
450 (__STDCPP_DEFAULT_NEW_ALIGNMENT__ - 1) / sizeof(_CharT);
451#else
452 const size_type __limit = 0;
453#endif
454 if (__capacity - __length > __limit )
455 try
456 {
457 _Alloc_result __r = _S_allocate_at_least(
458 _M_get_allocator(), __length + 1);
459 this->_S_copy(__r.__ptr, _M_data(), __length + 1);
460 _M_dispose();
461 _M_data(__r.__ptr);
462 _M_capacity(__r.__count - 1); // reserve room for NUL.
463 }
464 catch (const __cxxabiv1::__forced_unwind&)
465 { throw; }
466 catch (...)
467 { /* swallow the exception */ }
468#endif
469 }
470
471 template<typename _CharT, typename _Traits, typename _Alloc>
472 _GLIBCXX20_CONSTEXPR
473 void
474 basic_string<_CharT, _Traits, _Alloc>::
475 resize(size_type __n, _CharT __c)
476 {
477 const size_type __size = this->size();
478 if (__size < __n)
479 this->append(__n - __size, __c);
480 else if (__n < __size)
481 this->_M_set_length(__n);
482 }
483
484 template<typename _CharT, typename _Traits, typename _Alloc>
485 _GLIBCXX20_CONSTEXPR
486 basic_string<_CharT, _Traits, _Alloc>&
487 basic_string<_CharT, _Traits, _Alloc>::
488 _M_append(const _CharT* __s, size_type __n)
489 {
490 const size_type __len = __n + this->size();
491
492 if (__len <= this->capacity())
493 {
494 if (__n)
495 this->_S_copy(this->_M_data() + this->size(), __s, __n);
496 }
497 else
498 this->_M_mutate(this->size(), size_type(0), __s, __n);
499
500 this->_M_set_length(__len);
501 return *this;
502 }
503
504 template<typename _CharT, typename _Traits, typename _Alloc>
505 template<typename _InputIterator>
506 _GLIBCXX20_CONSTEXPR
507 basic_string<_CharT, _Traits, _Alloc>&
508 basic_string<_CharT, _Traits, _Alloc>::
509 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
510 _InputIterator __k1, _InputIterator __k2,
511 std::__false_type)
512 {
513 // _GLIBCXX_RESOLVE_LIB_DEFECTS
514 // 2788. unintentionally require a default constructible allocator
515 const basic_string __s(__k1, __k2, this->get_allocator());
516 const size_type __n1 = __i2 - __i1;
517 return _M_replace(__i1 - begin(), __n1, __s._M_data(),
518 __s.size());
519 }
520
521 template<typename _CharT, typename _Traits, typename _Alloc>
522 _GLIBCXX20_CONSTEXPR
523 basic_string<_CharT, _Traits, _Alloc>&
524 basic_string<_CharT, _Traits, _Alloc>::
525 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
526 _CharT __c)
527 {
528 _M_check_length(__n1, __n2, "basic_string::_M_replace_aux");
529
530 const size_type __old_size = this->size();
531 const size_type __new_size = __old_size + __n2 - __n1;
532
533 if (__new_size <= this->capacity())
534 {
535 pointer __p = this->_M_data() + __pos1;
536
537 const size_type __how_much = __old_size - __pos1 - __n1;
538 if (__how_much && __n1 != __n2)
539 this->_S_move(__p + __n2, __p + __n1, __how_much);
540 }
541 else
542 this->_M_mutate(__pos1, __n1, 0, __n2);
543
544 if (__n2)
545 this->_S_assign(this->_M_data() + __pos1, __n2, __c);
546
547 this->_M_set_length(__new_size);
548 return *this;
549 }
550
551 template<typename _CharT, typename _Traits, typename _Alloc>
552 __attribute__((__noinline__, __noclone__, __cold__)) void
553 basic_string<_CharT, _Traits, _Alloc>::
554 _M_replace_cold(pointer __p, size_type __len1, const _CharT* __s,
555 const size_type __len2, const size_type __how_much)
556 {
557 // Work in-place.
558 if (__len2 && __len2 <= __len1)
559 this->_S_move(__p, __s, __len2);
560 if (__how_much && __len1 != __len2)
561 this->_S_move(__p + __len2, __p + __len1, __how_much);
562 if (__len2 > __len1)
563 {
564 if (__s + __len2 <= __p + __len1)
565 this->_S_move(__p, __s, __len2);
566 else if (__s >= __p + __len1)
567 {
568 // Hint to middle end that __p and __s overlap
569 // (PR 98465).
570 const size_type __poff = (__s - __p) + (__len2 - __len1);
571 this->_S_copy(__p, __p + __poff, __len2);
572 }
573 else
574 {
575 const size_type __nleft = (__p + __len1) - __s;
576 this->_S_move(__p, __s, __nleft);
577 this->_S_copy(__p + __nleft, __p + __len2, __len2 - __nleft);
578 }
579 }
580 }
581
582 template<typename _CharT, typename _Traits, typename _Alloc>
583 _GLIBCXX20_CONSTEXPR
584 basic_string<_CharT, _Traits, _Alloc>&
585 basic_string<_CharT, _Traits, _Alloc>::
586 _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
587 const size_type __len2)
588 {
589 _M_check_length(__len1, __len2, "basic_string::_M_replace");
590
591 const size_type __old_size = this->size();
592 const size_type __new_size = __old_size + __len2 - __len1;
593
594 if (__new_size <= this->capacity())
595 {
596 pointer __p = this->_M_data() + __pos;
597
598 const size_type __how_much = __old_size - __pos - __len1;
599#if __cpp_lib_is_constant_evaluated
600 if (std::is_constant_evaluated())
601 {
602 auto __newp =
603 _S_allocate_at_least(_M_get_allocator(), __new_size).__ptr;
604 _S_copy(__newp, this->_M_data(), __pos);
605 _S_copy(__newp + __pos, __s, __len2);
606 _S_copy(__newp + __pos + __len2, __p + __len1, __how_much);
607 _S_copy(this->_M_data(), __newp, __new_size);
608 this->_M_get_allocator().deallocate(__newp, __new_size);
609 }
610 else
611#endif
612 if (__builtin_expect(_M_disjunct(__s), true))
613 {
614 if (__how_much && __len1 != __len2)
615 this->_S_move(__p + __len2, __p + __len1, __how_much);
616 if (__len2)
617 this->_S_copy(__p, __s, __len2);
618 }
619 else
620 _M_replace_cold(__p, __len1, __s, __len2, __how_much);
621 }
622 else
623 this->_M_mutate(__pos, __len1, __s, __len2);
624
625 this->_M_set_length(__new_size);
626 return *this;
627 }
628
629 template<typename _CharT, typename _Traits, typename _Alloc>
630 _GLIBCXX20_CONSTEXPR
631 typename basic_string<_CharT, _Traits, _Alloc>::size_type
632 basic_string<_CharT, _Traits, _Alloc>::
633 copy(_CharT* __s, size_type __n, size_type __pos) const
634 {
635 _M_check(__pos, "basic_string::copy");
636 __n = _M_limit(__pos, __n);
637 __glibcxx_requires_string_len(__s, __n);
638 if (__n)
639 _S_copy(__s, _M_data() + __pos, __n);
640 // 21.3.5.7 par 3: do not append null. (good.)
641 return __n;
642 }
643
644#ifdef __glibcxx_string_resize_and_overwrite // C++ >= 23
645 template<typename _CharT, typename _Traits, typename _Alloc>
646 template<typename _Operation>
647 [[__gnu__::__always_inline__]]
648 constexpr void
649 basic_string<_CharT, _Traits, _Alloc>::
650 __resize_and_overwrite(const size_type __n, _Operation __op)
651 { resize_and_overwrite<_Operation&>(__n, __op); }
652#endif
653
654#if __cplusplus >= 201103L
655 template<typename _CharT, typename _Traits, typename _Alloc>
656 template<typename _Operation>
657 _GLIBCXX20_CONSTEXPR void
658 basic_string<_CharT, _Traits, _Alloc>::
659#ifdef __glibcxx_string_resize_and_overwrite // C++ >= 23
660 resize_and_overwrite(const size_type __n, _Operation __op)
661#else
662 __resize_and_overwrite(const size_type __n, _Operation __op)
663#endif
664 {
665 reserve(__n);
666 _CharT* const __p = _M_data();
667#if __cpp_lib_is_constant_evaluated
668 if (std::__is_constant_evaluated() && __n > size())
669 traits_type::assign(__p + size(), __n - size(), _CharT());
670#endif
671 struct _Terminator {
672 _GLIBCXX20_CONSTEXPR ~_Terminator() { _M_this->_M_set_length(_M_r); }
673 basic_string* _M_this;
674 size_type _M_r;
675 };
676 _Terminator __term{this, 0};
677 auto __r = std::move(__op)(__p + 0, __n + 0);
678#ifdef __cpp_lib_concepts
679 static_assert(ranges::__detail::__is_integer_like<decltype(__r)>);
680#else
681 static_assert(__gnu_cxx::__is_integer_nonstrict<decltype(__r)>::__value,
682 "resize_and_overwrite operation must return an integer");
683#endif
684 _GLIBCXX_DEBUG_ASSERT(__r >= 0 && size_type(__r) <= __n);
685 __term._M_r = size_type(__r);
686 if (__term._M_r > __n)
687 __builtin_unreachable();
688 }
689#endif // C++11
690
691#endif // _GLIBCXX_USE_CXX11_ABI
692
693#if __glibcxx_constexpr_string >= 201907L
694# define _GLIBCXX_STRING_CONSTEXPR constexpr
695#else
696# define _GLIBCXX_STRING_CONSTEXPR
697#endif
698 template<typename _CharT, typename _Traits, typename _Alloc>
699 _GLIBCXX_STRING_CONSTEXPR
700 typename basic_string<_CharT, _Traits, _Alloc>::size_type
701 basic_string<_CharT, _Traits, _Alloc>::
702 find(const _CharT* __s, size_type __pos, size_type __n) const
703 _GLIBCXX_NOEXCEPT
704 {
705 __glibcxx_requires_string_len(__s, __n);
706 const size_type __size = this->size();
707
708 if (__n == 0)
709 return __pos <= __size ? __pos : npos;
710 if (__pos >= __size)
711 return npos;
712
713 const _CharT __elem0 = __s[0];
714 const _CharT* const __data = data();
715 const _CharT* __first = __data + __pos;
716 const _CharT* const __last = __data + __size;
717 size_type __len = __size - __pos;
718
719 while (__len >= __n)
720 {
721 // Find the first occurrence of __elem0:
722 __first = traits_type::find(__first, __len - __n + 1, __elem0);
723 if (!__first)
724 return npos;
725 // Compare the full strings from the first occurrence of __elem0.
726 // We already know that __first[0] == __s[0] but compare them again
727 // anyway because __s is probably aligned, which helps memcmp.
728 if (traits_type::compare(__first, __s, __n) == 0)
729 return __first - __data;
730 __len = __last - ++__first;
731 }
732 return npos;
733 }
734
735 template<typename _CharT, typename _Traits, typename _Alloc>
736 _GLIBCXX_STRING_CONSTEXPR
737 typename basic_string<_CharT, _Traits, _Alloc>::size_type
738 basic_string<_CharT, _Traits, _Alloc>::
739 find(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
740 {
741 size_type __ret = npos;
742 const size_type __size = this->size();
743 if (__pos < __size)
744 {
745 const _CharT* __data = _M_data();
746 const size_type __n = __size - __pos;
747 const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
748 if (__p)
749 __ret = __p - __data;
750 }
751 return __ret;
752 }
753
754 template<typename _CharT, typename _Traits, typename _Alloc>
755 _GLIBCXX_STRING_CONSTEXPR
756 typename basic_string<_CharT, _Traits, _Alloc>::size_type
757 basic_string<_CharT, _Traits, _Alloc>::
758 rfind(const _CharT* __s, size_type __pos, size_type __n) const
759 _GLIBCXX_NOEXCEPT
760 {
761 __glibcxx_requires_string_len(__s, __n);
762 const size_type __size = this->size();
763 if (__n <= __size)
764 {
765 __pos = std::min(size_type(__size - __n), __pos);
766 const _CharT* __data = _M_data();
767 do
768 {
769 if (traits_type::compare(__data + __pos, __s, __n) == 0)
770 return __pos;
771 }
772 while (__pos-- > 0);
773 }
774 return npos;
775 }
776
777 template<typename _CharT, typename _Traits, typename _Alloc>
778 _GLIBCXX_STRING_CONSTEXPR
779 typename basic_string<_CharT, _Traits, _Alloc>::size_type
780 basic_string<_CharT, _Traits, _Alloc>::
781 rfind(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
782 {
783 size_type __size = this->size();
784 if (__size)
785 {
786 if (--__size > __pos)
787 __size = __pos;
788 for (++__size; __size-- > 0; )
789 if (traits_type::eq(_M_data()[__size], __c))
790 return __size;
791 }
792 return npos;
793 }
794
795 template<typename _CharT, typename _Traits, typename _Alloc>
796 _GLIBCXX_STRING_CONSTEXPR
797 typename basic_string<_CharT, _Traits, _Alloc>::size_type
798 basic_string<_CharT, _Traits, _Alloc>::
799 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
800 _GLIBCXX_NOEXCEPT
801 {
802 __glibcxx_requires_string_len(__s, __n);
803 for (; __n && __pos < this->size(); ++__pos)
804 {
805 const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
806 if (__p)
807 return __pos;
808 }
809 return npos;
810 }
811
812 template<typename _CharT, typename _Traits, typename _Alloc>
813 _GLIBCXX_STRING_CONSTEXPR
814 typename basic_string<_CharT, _Traits, _Alloc>::size_type
815 basic_string<_CharT, _Traits, _Alloc>::
816 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
817 _GLIBCXX_NOEXCEPT
818 {
819 __glibcxx_requires_string_len(__s, __n);
820 size_type __size = this->size();
821 if (__size && __n)
822 {
823 if (--__size > __pos)
824 __size = __pos;
825 do
826 {
827 if (traits_type::find(__s, __n, _M_data()[__size]))
828 return __size;
829 }
830 while (__size-- != 0);
831 }
832 return npos;
833 }
834
835 template<typename _CharT, typename _Traits, typename _Alloc>
836 _GLIBCXX_STRING_CONSTEXPR
837 typename basic_string<_CharT, _Traits, _Alloc>::size_type
838 basic_string<_CharT, _Traits, _Alloc>::
839 find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
840 _GLIBCXX_NOEXCEPT
841 {
842 __glibcxx_requires_string_len(__s, __n);
843 for (; __pos < this->size(); ++__pos)
844 if (!traits_type::find(__s, __n, _M_data()[__pos]))
845 return __pos;
846 return npos;
847 }
848
849 template<typename _CharT, typename _Traits, typename _Alloc>
850 _GLIBCXX_STRING_CONSTEXPR
851 typename basic_string<_CharT, _Traits, _Alloc>::size_type
852 basic_string<_CharT, _Traits, _Alloc>::
853 find_first_not_of(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
854 {
855 for (; __pos < this->size(); ++__pos)
856 if (!traits_type::eq(_M_data()[__pos], __c))
857 return __pos;
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
864 basic_string<_CharT, _Traits, _Alloc>::
865 find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
866 _GLIBCXX_NOEXCEPT
867 {
868 __glibcxx_requires_string_len(__s, __n);
869 size_type __size = this->size();
870 if (__size)
871 {
872 if (--__size > __pos)
873 __size = __pos;
874 do
875 {
876 if (!traits_type::find(__s, __n, _M_data()[__size]))
877 return __size;
878 }
879 while (__size--);
880 }
881 return npos;
882 }
883
884 template<typename _CharT, typename _Traits, typename _Alloc>
885 _GLIBCXX_STRING_CONSTEXPR
886 typename basic_string<_CharT, _Traits, _Alloc>::size_type
887 basic_string<_CharT, _Traits, _Alloc>::
888 find_last_not_of(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
889 {
890 size_type __size = this->size();
891 if (__size)
892 {
893 if (--__size > __pos)
894 __size = __pos;
895 do
896 {
897 if (!traits_type::eq(_M_data()[__size], __c))
898 return __size;
899 }
900 while (__size--);
901 }
902 return npos;
903 }
904
905#undef _GLIBCXX_STRING_CONSTEXPR
906
907 // 21.3.7.9 basic_string::getline and operators
908 template<typename _CharT, typename _Traits, typename _Alloc>
909 basic_istream<_CharT, _Traits>&
910 operator>>(basic_istream<_CharT, _Traits>& __in,
911 basic_string<_CharT, _Traits, _Alloc>& __str)
912 {
913 typedef basic_istream<_CharT, _Traits> __istream_type;
914 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
915 typedef typename __istream_type::ios_base __ios_base;
916 typedef typename __istream_type::int_type __int_type;
917 typedef typename __string_type::size_type __size_type;
918 typedef ctype<_CharT> __ctype_type;
919 typedef typename __ctype_type::ctype_base __ctype_base;
920
921 __size_type __extracted = 0;
922 typename __ios_base::iostate __err = __ios_base::goodbit;
923 typename __istream_type::sentry __cerb(__in, false);
924 if (__cerb)
925 {
926 __try
927 {
928 // Avoid reallocation for common case.
929 __str.erase();
930 _CharT __buf[128];
931 __size_type __len = 0;
932 const streamsize __w = __in.width();
933 const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
934 : __str.max_size();
935 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
936 const __int_type __eof = _Traits::eof();
937 __int_type __c = __in.rdbuf()->sgetc();
938
939 while (__extracted < __n
940 && !_Traits::eq_int_type(__c, __eof)
941 && !__ct.is(__ctype_base::space,
942 _Traits::to_char_type(__c)))
943 {
944 if (__len == sizeof(__buf) / sizeof(_CharT))
945 {
946 __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
947 __len = 0;
948 }
949 __buf[__len++] = _Traits::to_char_type(__c);
950 ++__extracted;
951 __c = __in.rdbuf()->snextc();
952 }
953 __str.append(__buf, __len);
954
955 if (__extracted < __n && _Traits::eq_int_type(__c, __eof))
956 __err |= __ios_base::eofbit;
957 __in.width(0);
958 }
959 __catch(__cxxabiv1::__forced_unwind&)
960 {
961 __in._M_setstate(__ios_base::badbit);
962 __throw_exception_again;
963 }
964 __catch(...)
965 {
966 // _GLIBCXX_RESOLVE_LIB_DEFECTS
967 // 91. Description of operator>> and getline() for string<>
968 // might cause endless loop
969 __in._M_setstate(__ios_base::badbit);
970 }
971 }
972 // 211. operator>>(istream&, string&) doesn't set failbit
973 if (!__extracted)
974 __err |= __ios_base::failbit;
975 if (__err)
976 __in.setstate(__err);
977 return __in;
978 }
979
980 template<typename _CharT, typename _Traits, typename _Alloc>
981 basic_istream<_CharT, _Traits>&
982 getline(basic_istream<_CharT, _Traits>& __in,
983 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
984 {
985 typedef basic_istream<_CharT, _Traits> __istream_type;
986 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
987 typedef typename __istream_type::ios_base __ios_base;
988 typedef typename __istream_type::int_type __int_type;
989 typedef typename __string_type::size_type __size_type;
990
991 __size_type __extracted = 0;
992 const __size_type __n = __str.max_size();
993 typename __ios_base::iostate __err = __ios_base::goodbit;
994 typename __istream_type::sentry __cerb(__in, true);
995 if (__cerb)
996 {
997 __try
998 {
999 __str.erase();
1000 const __int_type __idelim = _Traits::to_int_type(__delim);
1001 const __int_type __eof = _Traits::eof();
1002 __int_type __c = __in.rdbuf()->sgetc();
1003
1004 while (__extracted < __n
1005 && !_Traits::eq_int_type(__c, __eof)
1006 && !_Traits::eq_int_type(__c, __idelim))
1007 {
1008 __str += _Traits::to_char_type(__c);
1009 ++__extracted;
1010 __c = __in.rdbuf()->snextc();
1011 }
1012
1013 if (_Traits::eq_int_type(__c, __eof))
1014 __err |= __ios_base::eofbit;
1015 else if (_Traits::eq_int_type(__c, __idelim))
1016 {
1017 ++__extracted;
1018 __in.rdbuf()->sbumpc();
1019 }
1020 else
1021 __err |= __ios_base::failbit;
1022 }
1023 __catch(__cxxabiv1::__forced_unwind&)
1024 {
1025 __in._M_setstate(__ios_base::badbit);
1026 __throw_exception_again;
1027 }
1028 __catch(...)
1029 {
1030 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1031 // 91. Description of operator>> and getline() for string<>
1032 // might cause endless loop
1033 __in._M_setstate(__ios_base::badbit);
1034 }
1035 }
1036 if (!__extracted)
1037 __err |= __ios_base::failbit;
1038 if (__err)
1039 __in.setstate(__err);
1040 return __in;
1041 }
1042
1043 // Inhibit implicit instantiations for required instantiations,
1044 // which are defined via explicit instantiations elsewhere.
1045#if _GLIBCXX_EXTERN_TEMPLATE
1046 // The explicit instantiation definitions in src/c++11/string-inst.cc,
1047 // src/c++17/string-inst.cc and src/c++20/string-inst.cc only instantiate
1048 // the members required for C++20 and earlier standards (so not C++23's
1049 // contains).
1050 // Suppress the explicit instantiation declarations for C++23, so C++23
1051 // code will implicitly instantiate std::string and std::wstring as needed.
1052# if __cplusplus <= 202002L && _GLIBCXX_EXTERN_TEMPLATE > 0
1053 extern template class basic_string<char>;
1054# elif ! _GLIBCXX_USE_CXX11_ABI
1055 // Still need to prevent implicit instantiation of the COW empty rep,
1056 // to ensure the definition in libstdc++.so is unique (PR 86138).
1057 extern template basic_string<char>::size_type
1058 basic_string<char>::_Rep::_S_empty_rep_storage[];
1059# elif _GLIBCXX_EXTERN_TEMPLATE > 0
1060 // Export _M_replace_cold even for C++23.
1061 extern template void
1062 basic_string<char>::_M_replace_cold(char *, size_type, const char*,
1063 const size_type, const size_type);
1064# endif
1065
1066 extern template
1067 basic_istream<char>&
1068 operator>>(basic_istream<char>&, string&);
1069 extern template
1070 basic_ostream<char>&
1071 operator<<(basic_ostream<char>&, const string&);
1072 extern template
1073 basic_istream<char>&
1074 getline(basic_istream<char>&, string&, char);
1075 extern template
1076 basic_istream<char>&
1077 getline(basic_istream<char>&, string&);
1078
1079#ifdef _GLIBCXX_USE_WCHAR_T
1080# if __cplusplus <= 202002L && _GLIBCXX_EXTERN_TEMPLATE > 0
1081 extern template class basic_string<wchar_t>;
1082# elif ! _GLIBCXX_USE_CXX11_ABI
1083 extern template basic_string<wchar_t>::size_type
1084 basic_string<wchar_t>::_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<wchar_t>::_M_replace_cold(wchar_t*, size_type, const wchar_t*,
1089 const size_type, const size_type);
1090# endif
1091
1092 extern template
1093 basic_istream<wchar_t>&
1094 operator>>(basic_istream<wchar_t>&, wstring&);
1095 extern template
1096 basic_ostream<wchar_t>&
1097 operator<<(basic_ostream<wchar_t>&, const wstring&);
1098 extern template
1099 basic_istream<wchar_t>&
1100 getline(basic_istream<wchar_t>&, wstring&, wchar_t);
1101 extern template
1102 basic_istream<wchar_t>&
1103 getline(basic_istream<wchar_t>&, wstring&);
1104#endif // _GLIBCXX_USE_WCHAR_T
1105#endif // _GLIBCXX_EXTERN_TEMPLATE
1106
1107_GLIBCXX_END_NAMESPACE_VERSION
1108} // namespace std
1109
1110#pragma GCC diagnostic pop
1111#endif
cxxabi_forced.h
std::move
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
std::__addressof
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:52
std::begin
_Tp * begin(valarray< _Tp > &__va) noexcept
Return an iterator pointing to the first element of the valarray.
Definition valarray:1230
std::min
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Definition stl_algobase.h:232
std::use_facet
const _Facet & use_facet(const locale &__loc)
Return a facet.
Definition locale_classes.tcc:220
std::wstring
basic_string< wchar_t > wstring
A string of wchar_t.
Definition stringfwd.h:82
std
ISO C++ entities toplevel namespace is std.
std::streamsize
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
Definition postypes.h:73
std::getline
basic_istream< _CharT, _Traits > & getline(basic_istream< _CharT, _Traits > &__is, basic_string< _CharT, _Traits, _Alloc > &__str, _CharT __delim)
Read a line from stream into a string.
Definition basic_string.tcc:982
std::distance
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
Definition stl_iterator_base_funcs.h:177
std::size
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
Definition range_access.h:292
std::operator>>
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::operator<<
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
std::data
constexpr auto data(_Container &__cont) noexcept(noexcept(__cont.data())) -> decltype(__cont.data())
Return the data pointer of a container.
Definition range_access.h:350
std::basic_ios::setstate
void setstate(iostate __state)
Sets additional flags in the error state.
Definition basic_ios.h:167
std::basic_ios::rdbuf
basic_streambuf< _CharT, _Traits > * rdbuf() const
Accessing the underlying buffer.
Definition basic_ios.h:338
std::basic_istream
Template class basic_istream.
Definition istream:67
std::size_type
std::basic_string
Managing sequences of characters and character-like objects.
Definition basic_string.h:94
std::basic_string::find_last_not_of
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.
Definition basic_string.h:3407
std::basic_string::find
constexpr size_type find(const _CharT *__s, size_type __pos, size_type __n) const noexcept
Find position of a C substring.
Definition basic_string.tcc:702
std::basic_string::copy
constexpr size_type copy(_CharT *__s, size_type __n, size_type __pos=0) const
Copy substring into C string.
Definition cow_string.h:3892
std::basic_string::find_first_of
constexpr size_type find_first_of(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a character of string.
Definition basic_string.h:3143
std::basic_string::resize
constexpr void resize(size_type __n, _CharT __c)
Resizes the string to the specified number of characters.
Definition cow_string.h:3817
std::basic_string::reserve
constexpr void reserve()
Definition cow_string.h:3871
std::basic_string::__resize_and_overwrite
constexpr void __resize_and_overwrite(size_type __n, _Operation __op)
Non-standard version of resize_and_overwrite for C++11 and above.
std::basic_string::rfind
constexpr size_type rfind(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a string.
Definition basic_string.h:3059
std::basic_string::append
constexpr basic_string & append(const basic_string &__str)
Append a string to this string.
Definition basic_string.h:1589
std::basic_string::find_last_of
constexpr size_type find_last_of(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a character of string.
Definition basic_string.h:3232
std::basic_string::find_first_not_of
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.
Definition basic_string.h:3320
std::basic_string::npos
static const size_type npos
Value returned by various member functions when they fail.
Definition basic_string.h:127
std::basic_string::erase
constexpr basic_string & erase(size_type __pos=0, size_type __n=npos)
Remove characters.
Definition basic_string.h:2377
std::basic_string::max_size
constexpr size_type max_size() const noexcept
Returns the size() of the largest possible string.
Definition basic_string.h:1235
std::basic_string::swap
constexpr void swap(basic_string &__s) noexcept
Swap contents with another string.
Definition cow_string.h:3695
__cxxabiv1::__forced_unwind
Thrown as part of forced unwinding.
Definition cxxabi_forced.h:51
std::ios_base::width
streamsize width() const
Flags access.
Definition ios_base.h:789
std::ios_base::getloc
locale getloc() const
Locale access.
Definition ios_base.h:841
std::ctype
Primary class template ctype facet.
Definition locale_facets.h:618