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