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