Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/c++/15/bits/cow_string.h
1 // Definition of gcc4-compatible Copy-on-Write basic_string -*- C++ -*- 2 3 // Copyright (C) 1997-2025 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/cow_string.h 26 * This is an internal header file, included by other library headers. 27 * Do not attempt to use it directly. @headername{string} 28 * 29 * Defines the reference-counted COW string implementation. 30 */ 31 32 #ifndef _COW_STRING_H 33 #define _COW_STRING_H 1 34 35 #if ! _GLIBCXX_USE_CXX11_ABI 36 37 #include <ext/atomicity.h> // _Atomic_word, __is_single_threaded 38 39 namespace std _GLIBCXX_VISIBILITY(default) 40 { 41 _GLIBCXX_BEGIN_NAMESPACE_VERSION 42 43 /** 44 * @class basic_string basic_string.h <string> 45 * @brief Managing sequences of characters and character-like objects. 46 * 47 * @ingroup strings 48 * @ingroup sequences 49 * @headerfile string 50 * @since C++98 51 * 52 * @tparam _CharT Type of character 53 * @tparam _Traits Traits for character type, defaults to 54 * char_traits<_CharT>. 55 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>. 56 * 57 * Meets the requirements of a <a href="tables.html#65">container</a>, a 58 * <a href="tables.html#66">reversible container</a>, and a 59 * <a href="tables.html#67">sequence</a>. Of the 60 * <a href="tables.html#68">optional sequence requirements</a>, only 61 * @c push_back, @c at, and @c %array access are supported. 62 * 63 * @doctodo 64 * 65 * 66 * Documentation? What's that? 67 * Nathan Myers <ncm@cantrip.org>. 68 * 69 * A string looks like this: 70 * 71 * @code 72 * [_Rep] 73 * _M_length 74 * [basic_string<char_type>] _M_capacity 75 * _M_dataplus _M_refcount 76 * _M_p ----------------> unnamed array of char_type 77 * @endcode 78 * 79 * Where the _M_p points to the first character in the string, and 80 * you cast it to a pointer-to-_Rep and subtract 1 to get a 81 * pointer to the header. 82 * 83 * This approach has the enormous advantage that a string object 84 * requires only one allocation. All the ugliness is confined 85 * within a single %pair of inline functions, which each compile to 86 * a single @a add instruction: _Rep::_M_data(), and 87 * string::_M_rep(); and the allocation function which gets a 88 * block of raw bytes and with room enough and constructs a _Rep 89 * object at the front. 90 * 91 * The reason you want _M_data pointing to the character %array and 92 * not the _Rep is so that the debugger can see the string 93 * contents. (Probably we should add a non-inline member to get 94 * the _Rep for the debugger to use, so users can check the actual 95 * string length.) 96 * 97 * Note that the _Rep object is a POD so that you can have a 98 * static <em>empty string</em> _Rep object already @a constructed before 99 * static constructors have run. The reference-count encoding is 100 * chosen so that a 0 indicates one reference, so you never try to 101 * destroy the empty-string _Rep object. 102 * 103 * All but the last paragraph is considered pretty conventional 104 * for a Copy-On-Write C++ string implementation. 105 */ 106 // 21.3 Template class basic_string 107 template<typename _CharT, typename _Traits, typename _Alloc> 108 class basic_string 109 { 110 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template 111 rebind<_CharT>::other _CharT_alloc_type; 112 typedef __gnu_cxx::__alloc_traits<_CharT_alloc_type> _CharT_alloc_traits; 113 114 // Types: 115 public: 116 typedef _Traits traits_type; 117 typedef typename _Traits::char_type value_type; 118 typedef _Alloc allocator_type; 119 typedef typename _CharT_alloc_traits::size_type size_type; 120 typedef typename _CharT_alloc_traits::difference_type difference_type; 121 #if __cplusplus < 201103L 122 typedef typename _CharT_alloc_type::reference reference; 123 typedef typename _CharT_alloc_type::const_reference const_reference; 124 #else 125 typedef value_type& reference; 126 typedef const value_type& const_reference; 127 #endif 128 typedef typename _CharT_alloc_traits::pointer pointer; 129 typedef typename _CharT_alloc_traits::const_pointer const_pointer; 130 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator; 131 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string> 132 const_iterator; 133 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 134 typedef std::reverse_iterator<iterator> reverse_iterator; 135 136 protected: 137 // type used for positions in insert, erase etc. 138 typedef iterator __const_iterator; 139 140 private: 141 // _Rep: string representation 142 // Invariants: 143 // 1. String really contains _M_length + 1 characters: due to 21.3.4 144 // must be kept null-terminated. 145 // 2. _M_capacity >= _M_length 146 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT). 147 // 3. _M_refcount has three states: 148 // -1: leaked, one reference, no ref-copies allowed, non-const. 149 // 0: one reference, non-const. 150 // n>0: n + 1 references, operations require a lock, const. 151 // 4. All fields==0 is an empty string, given the extra storage 152 // beyond-the-end for a null terminator; thus, the shared 153 // empty string representation needs no constructor. 154 155 struct _Rep_base 156 { 157 size_type _M_length; 158 size_type _M_capacity; 159 _Atomic_word _M_refcount; 160 }; 161 162 struct _Rep : _Rep_base 163 { 164 // Types: 165 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template 166 rebind<char>::other _Raw_bytes_alloc; 167 168 // (Public) Data members: 169 170 // The maximum number of individual char_type elements of an 171 // individual string is determined by _S_max_size. This is the 172 // value that will be returned by max_size(). (Whereas npos 173 // is the maximum number of bytes the allocator can allocate.) 174 // If one was to divvy up the theoretical largest size string, 175 // with a terminating character and m _CharT elements, it'd 176 // look like this: 177 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT) 178 // Solving for m: 179 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1 180 // In addition, this implementation quarters this amount. 181 static const size_type _S_max_size; 182 static const _CharT _S_terminal; 183 184 // The following storage is init'd to 0 by the linker, resulting 185 // (carefully) in an empty string with one reference. 186 static size_type _S_empty_rep_storage[]; 187 188 static _Rep& 189 _S_empty_rep() _GLIBCXX_NOEXCEPT 190 { 191 // NB: Mild hack to avoid strict-aliasing warnings. Note that 192 // _S_empty_rep_storage is never modified and the punning should 193 // be reasonably safe in this case. 194 void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage); 195 return *reinterpret_cast<_Rep*>(__p); 196 } 197 198 bool 199 _M_is_leaked() const _GLIBCXX_NOEXCEPT 200 { 201 #if defined(__GTHREADS) 202 // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose, 203 // so we need to use an atomic load. However, _M_is_leaked 204 // predicate does not change concurrently (i.e. the string is either 205 // leaked or not), so a relaxed load is enough. 206 return __atomic_load_n(&this->_M_refcount, __ATOMIC_RELAXED) < 0; 207 #else 208 return this->_M_refcount < 0; 209 #endif 210 } 211 212 bool 213 _M_is_shared() const _GLIBCXX_NOEXCEPT 214 { 215 #if defined(__GTHREADS) 216 // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose, 217 // so we need to use an atomic load. Another thread can drop last 218 // but one reference concurrently with this check, so we need this 219 // load to be acquire to synchronize with release fetch_and_add in 220 // _M_dispose. 221 if (!__gnu_cxx::__is_single_threaded()) 222 return __atomic_load_n(&this->_M_refcount, __ATOMIC_ACQUIRE) > 0; 223 #endif 224 return this->_M_refcount > 0; 225 } 226 227 void 228 _M_set_leaked() _GLIBCXX_NOEXCEPT 229 { this->_M_refcount = -1; } 230 231 void 232 _M_set_sharable() _GLIBCXX_NOEXCEPT 233 { this->_M_refcount = 0; } 234 235 void 236 _M_set_length_and_sharable(size_type __n) _GLIBCXX_NOEXCEPT 237 { 238 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 239 if (__builtin_expect(this != &_S_empty_rep(), false)) 240 #endif 241 { 242 this->_M_set_sharable(); // One reference. 243 this->_M_length = __n; 244 traits_type::assign(this->_M_refdata()[__n], _S_terminal); 245 // grrr. (per 21.3.4) 246 // You cannot leave those LWG people alone for a second. 247 } 248 } 249 250 _CharT* 251 _M_refdata() throw() 252 { return reinterpret_cast<_CharT*>(this + 1); } 253 254 _CharT* 255 _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2) 256 { 257 return (!_M_is_leaked() && __alloc1 == __alloc2) 258 ? _M_refcopy() : _M_clone(__alloc1); 259 } 260 261 // Create & Destroy 262 static _Rep* 263 _S_create(size_type, size_type, const _Alloc&); 264 265 void 266 _M_dispose(const _Alloc& __a) _GLIBCXX_NOEXCEPT 267 { 268 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 269 if (__builtin_expect(this != &_S_empty_rep(), false)) 270 #endif 271 { 272 // Be race-detector-friendly. For more info see bits/c++config. 273 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount); 274 // Decrement of _M_refcount is acq_rel, because: 275 // - all but last decrements need to release to synchronize with 276 // the last decrement that will delete the object. 277 // - the last decrement needs to acquire to synchronize with 278 // all the previous decrements. 279 // - last but one decrement needs to release to synchronize with 280 // the acquire load in _M_is_shared that will conclude that 281 // the object is not shared anymore. 282 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount, 283 -1) <= 0) 284 { 285 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount); 286 _M_destroy(__a); 287 } 288 } 289 } // XXX MT 290 291 void 292 _M_destroy(const _Alloc&) throw(); 293 294 _CharT* 295 _M_refcopy() throw() 296 { 297 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 298 if (__builtin_expect(this != &_S_empty_rep(), false)) 299 #endif 300 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1); 301 return _M_refdata(); 302 } // XXX MT 303 304 _CharT* 305 _M_clone(const _Alloc&, size_type __res = 0); 306 }; 307 308 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html 309 struct _Alloc_hider : _Alloc 310 { 311 _Alloc_hider(_CharT* __dat, const _Alloc& __a) _GLIBCXX_NOEXCEPT 312 : _Alloc(__a), _M_p(__dat) { } 313 314 _CharT* _M_p; // The actual data. 315 }; 316 317 public: 318 // Data Members (public): 319 // NB: This is an unsigned type, and thus represents the maximum 320 // size that the allocator can hold. 321 /// Value returned by various member functions when they fail. 322 static const size_type npos = static_cast<size_type>(-1); 323 324 private: 325 // Data Members (private): 326 mutable _Alloc_hider _M_dataplus; 327 328 _CharT* 329 _M_data() const _GLIBCXX_NOEXCEPT 330 { return _M_dataplus._M_p; } 331 332 _CharT* 333 _M_data(_CharT* __p) _GLIBCXX_NOEXCEPT 334 { return (_M_dataplus._M_p = __p); } 335 336 _Rep* 337 _M_rep() const _GLIBCXX_NOEXCEPT 338 { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); } 339 340 // For the internal use we have functions similar to `begin'/`end' 341 // but they do not call _M_leak. 342 iterator 343 _M_ibegin() const _GLIBCXX_NOEXCEPT 344 { return iterator(_M_data()); } 345 346 iterator 347 _M_iend() const _GLIBCXX_NOEXCEPT 348 { return iterator(_M_data() + this->size()); } 349 350 void 351 _M_leak() // for use in begin() & non-const op[] 352 { 353 if (!_M_rep()->_M_is_leaked()) 354 _M_leak_hard(); 355 } 356 357 size_type 358 _M_check(size_type __pos, const char* __s) const 359 { 360 if (__pos > this->size()) 361 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > " 362 "this->size() (which is %zu)"), 363 __s, __pos, this->size()); 364 return __pos; 365 } 366 367 void 368 _M_check_length(size_type __n1, size_type __n2, const char* __s) const 369 { 370 if (this->max_size() - (this->size() - __n1) < __n2) 371 __throw_length_error(__N(__s)); 372 } 373 374 // NB: _M_limit doesn't check for a bad __pos value. 375 size_type 376 _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT 377 { 378 const bool __testoff = __off < this->size() - __pos; 379 return __testoff ? __off : this->size() - __pos; 380 } 381 382 // True if _Rep and source do not overlap. 383 bool 384 _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT 385 { 386 return (less<const _CharT*>()(__s, _M_data()) 387 || less<const _CharT*>()(_M_data() + this->size(), __s)); 388 } 389 390 // When __n = 1 way faster than the general multichar 391 // traits_type::copy/move/assign. 392 static void 393 _M_copy(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT 394 { 395 if (__n == 1) 396 traits_type::assign(*__d, *__s); 397 else 398 traits_type::copy(__d, __s, __n); 399 } 400 401 static void 402 _M_move(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT 403 { 404 if (__n == 1) 405 traits_type::assign(*__d, *__s); 406 else 407 traits_type::move(__d, __s, __n); 408 } 409 410 static void 411 _M_assign(_CharT* __d, size_type __n, _CharT __c) _GLIBCXX_NOEXCEPT 412 { 413 if (__n == 1) 414 traits_type::assign(*__d, __c); 415 else 416 traits_type::assign(__d, __n, __c); 417 } 418 419 // _S_copy_chars is a separate template to permit specialization 420 // to optimize for the common case of pointers as iterators. 421 template<class _Iterator> 422 static void 423 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2) 424 { 425 for (; __k1 != __k2; ++__k1, (void)++__p) 426 traits_type::assign(*__p, static_cast<_CharT>(*__k1)); 427 } 428 429 static void 430 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT 431 { _S_copy_chars(__p, __k1.base(), __k2.base()); } 432 433 static void 434 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2) 435 _GLIBCXX_NOEXCEPT 436 { _S_copy_chars(__p, __k1.base(), __k2.base()); } 437 438 static void 439 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT 440 { _M_copy(__p, __k1, __k2 - __k1); } 441 442 static void 443 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2) 444 _GLIBCXX_NOEXCEPT 445 { _M_copy(__p, __k1, __k2 - __k1); } 446 447 static int 448 _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT 449 { 450 const difference_type __d = difference_type(__n1 - __n2); 451 452 if (__d > __gnu_cxx::__numeric_traits<int>::__max) 453 return __gnu_cxx::__numeric_traits<int>::__max; 454 else if (__d < __gnu_cxx::__numeric_traits<int>::__min) 455 return __gnu_cxx::__numeric_traits<int>::__min; 456 else 457 return int(__d); 458 } 459 460 void 461 _M_mutate(size_type __pos, size_type __len1, size_type __len2); 462 463 void 464 _M_leak_hard(); 465 466 static _Rep& 467 _S_empty_rep() _GLIBCXX_NOEXCEPT 468 { return _Rep::_S_empty_rep(); } 469 470 #ifdef __glibcxx_string_view // >= C++17 471 // A helper type for avoiding boiler-plate. 472 typedef basic_string_view<_CharT, _Traits> __sv_type; 473 474 template<typename _Tp, typename _Res> 475 using _If_sv = enable_if_t< 476 __and_<is_convertible<const _Tp&, __sv_type>, 477 __not_<is_convertible<const _Tp*, const basic_string*>>, 478 __not_<is_convertible<const _Tp&, const _CharT*>>>::value, 479 _Res>; 480 481 // Allows an implicit conversion to __sv_type. 482 static __sv_type 483 _S_to_string_view(__sv_type __svt) noexcept 484 { return __svt; } 485 486 // Wraps a string_view by explicit conversion and thus 487 // allows to add an internal constructor that does not 488 // participate in overload resolution when a string_view 489 // is provided. 490 struct __sv_wrapper 491 { 492 explicit __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { } 493 __sv_type _M_sv; 494 }; 495 496 /** 497 * @brief Only internally used: Construct string from a string view 498 * wrapper. 499 * @param __svw string view wrapper. 500 * @param __a Allocator to use. 501 */ 502 explicit 503 basic_string(__sv_wrapper __svw, const _Alloc& __a) 504 : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { } 505 #endif 506 507 public: 508 // Construct/copy/destroy: 509 // NB: We overload ctors in some cases instead of using default 510 // arguments, per 17.4.4.4 para. 2 item 2. 511 512 /** 513 * @brief Default constructor creates an empty string. 514 */ 515 basic_string() 516 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 517 _GLIBCXX_NOEXCEPT 518 #endif 519 #if __cpp_concepts && __glibcxx_type_trait_variable_templates 520 requires is_default_constructible_v<_Alloc> 521 #endif 522 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 523 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) 524 #else 525 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()) 526 #endif 527 { } 528 529 /** 530 * @brief Construct an empty string using allocator @a a. 531 */ 532 explicit 533 basic_string(const _Alloc& __a) 534 : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a) 535 { } 536 537 // NB: per LWG issue 42, semantics different from IS: 538 /** 539 * @brief Construct string with copy of value of @a str. 540 * @param __str Source string. 541 */ 542 basic_string(const basic_string& __str) 543 : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(__str.get_allocator()), 544 __str.get_allocator()), 545 __str.get_allocator()) 546 { } 547 548 // _GLIBCXX_RESOLVE_LIB_DEFECTS 549 // 2583. no way to supply an allocator for basic_string(str, pos) 550 /** 551 * @brief Construct string as copy of a substring. 552 * @param __str Source string. 553 * @param __pos Index of first character to copy from. 554 * @param __a Allocator to use. 555 */ 556 basic_string(const basic_string& __str, size_type __pos, 557 const _Alloc& __a = _Alloc()); 558 559 /** 560 * @brief Construct string as copy of a substring. 561 * @param __str Source string. 562 * @param __pos Index of first character to copy from. 563 * @param __n Number of characters to copy. 564 */ 565 basic_string(const basic_string& __str, size_type __pos, 566 size_type __n); 567 /** 568 * @brief Construct string as copy of a substring. 569 * @param __str Source string. 570 * @param __pos Index of first character to copy from. 571 * @param __n Number of characters to copy. 572 * @param __a Allocator to use. 573 */ 574 basic_string(const basic_string& __str, size_type __pos, 575 size_type __n, const _Alloc& __a); 576 577 /** 578 * @brief Construct string initialized by a character %array. 579 * @param __s Source character %array. 580 * @param __n Number of characters to copy. 581 * @param __a Allocator to use (default is default allocator). 582 * 583 * NB: @a __s must have at least @a __n characters, '\\0' 584 * has no special meaning. 585 */ 586 basic_string(const _CharT* __s, size_type __n, 587 const _Alloc& __a = _Alloc()) 588 : _M_dataplus(_S_construct(__s, __s + __n, __a), __a) 589 { } 590 591 /** 592 * @brief Construct string as copy of a C string. 593 * @param __s Source C string. 594 * @param __a Allocator to use (default is default allocator). 595 */ 596 #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS 597 // _GLIBCXX_RESOLVE_LIB_DEFECTS 598 // 3076. basic_string CTAD ambiguity 599 template<typename = _RequireAllocator<_Alloc>> 600 #endif 601 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc()) 602 : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) : 603 __s + npos, __a), __a) 604 { } 605 606 /** 607 * @brief Construct string as multiple characters. 608 * @param __n Number of characters. 609 * @param __c Character to use. 610 * @param __a Allocator to use (default is default allocator). 611 */ 612 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc()) 613 : _M_dataplus(_S_construct(__n, __c, __a), __a) 614 { } 615 616 #if __cplusplus >= 201103L 617 /** 618 * @brief Move construct string. 619 * @param __str Source string. 620 * 621 * The newly-created string contains the exact contents of @a __str. 622 * @a __str is a valid, but unspecified string. 623 */ 624 basic_string(basic_string&& __str) noexcept 625 : _M_dataplus(std::move(__str._M_dataplus)) 626 { 627 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 628 // Make __str use the shared empty string rep. 629 __str._M_data(_S_empty_rep()._M_refdata()); 630 #else 631 // Rather than allocate an empty string for the rvalue string, 632 // just share ownership with it by incrementing the reference count. 633 // If the rvalue string was the unique owner then there are exactly 634 // two owners now. 635 if (_M_rep()->_M_is_shared()) 636 __gnu_cxx::__atomic_add_dispatch(&_M_rep()->_M_refcount, 1); 637 else 638 _M_rep()->_M_refcount = 1; 639 #endif 640 } 641 642 #if __glibcxx_containers_ranges // C++ >= 23 643 /** 644 * @brief Construct a string from a range. 645 * @since C++23 646 */ 647 template<__detail::__container_compatible_range<_CharT> _Rg> 648 basic_string(from_range_t, _Rg&& __rg, const _Alloc& __a = _Alloc()) 649 : basic_string(__a) 650 { 651 if constexpr (ranges::forward_range<_Rg> || ranges::sized_range<_Rg>) 652 { 653 const auto __n = static_cast<size_type>(ranges::distance(__rg)); 654 if (__n == 0) 655 return; 656 657 reserve(__n); 658 pointer __p = _M_data(); 659 if constexpr (requires { 660 requires ranges::contiguous_range<_Rg>; 661 { ranges::data(std::forward<_Rg>(__rg)) } 662 -> convertible_to<const _CharT*>; 663 }) 664 _M_copy(__p, ranges::data(std::forward<_Rg>(__rg)), __n); 665 else 666 { 667 auto __first = ranges::begin(__rg); 668 const auto __last = ranges::end(__rg); 669 for (; __first != __last; ++__first) 670 traits_type::assign(*__p++, static_cast<_CharT>(*__first)); 671 } 672 _M_rep()->_M_set_length_and_sharable(__n); 673 } 674 else 675 { 676 auto __first = ranges::begin(__rg); 677 const auto __last = ranges::end(__rg); 678 for (; __first != __last; ++__first) 679 push_back(*__first); 680 } 681 } 682 #endif 683 684 /** 685 * @brief Construct string from an initializer %list. 686 * @param __l std::initializer_list of characters. 687 * @param __a Allocator to use (default is default allocator). 688 */ 689 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc()) 690 : _M_dataplus(_S_construct(__l.begin(), __l.end(), __a), __a) 691 { } 692 693 basic_string(const basic_string& __str, const _Alloc& __a) 694 : _M_dataplus(__str._M_rep()->_M_grab(__a, __str.get_allocator()), __a) 695 { } 696 697 basic_string(basic_string&& __str, const _Alloc& __a) 698 : _M_dataplus(__str._M_data(), __a) 699 { 700 if (__a == __str.get_allocator()) 701 { 702 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 703 __str._M_data(_S_empty_rep()._M_refdata()); 704 #else 705 __str._M_data(_S_construct(size_type(), _CharT(), __a)); 706 #endif 707 } 708 else 709 _M_dataplus._M_p = _S_construct(__str.begin(), __str.end(), __a); 710 } 711 #endif // C++11 712 713 #if __cplusplus >= 202100L 714 basic_string(nullptr_t) = delete; 715 basic_string& operator=(nullptr_t) = delete; 716 #endif // C++23 717 718 /** 719 * @brief Construct string as copy of a range. 720 * @param __beg Start of range. 721 * @param __end End of range. 722 * @param __a Allocator to use (default is default allocator). 723 */ 724 template<class _InputIterator> 725 basic_string(_InputIterator __beg, _InputIterator __end, 726 const _Alloc& __a = _Alloc()) 727 : _M_dataplus(_S_construct(__beg, __end, __a), __a) 728 { } 729 730 #ifdef __glibcxx_string_view // >= C++17 731 /** 732 * @brief Construct string from a substring of a string_view. 733 * @param __t Source object convertible to string view. 734 * @param __pos The index of the first character to copy from __t. 735 * @param __n The number of characters to copy from __t. 736 * @param __a Allocator to use. 737 */ 738 template<typename _Tp, 739 typename = enable_if_t<is_convertible_v<const _Tp&, __sv_type>>> 740 basic_string(const _Tp& __t, size_type __pos, size_type __n, 741 const _Alloc& __a = _Alloc()) 742 : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { } 743 744 /** 745 * @brief Construct string from a string_view. 746 * @param __t Source object convertible to string view. 747 * @param __a Allocator to use (default is default allocator). 748 */ 749 template<typename _Tp, typename = _If_sv<_Tp, void>> 750 explicit 751 basic_string(const _Tp& __t, const _Alloc& __a = _Alloc()) 752 : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { } 753 #endif // C++17 754 755 /** 756 * @brief Destroy the string instance. 757 */ 758 ~basic_string() _GLIBCXX_NOEXCEPT 759 { _M_rep()->_M_dispose(this->get_allocator()); } 760 761 /** 762 * @brief Assign the value of @a str to this string. 763 * @param __str Source string. 764 */ 765 basic_string& 766 operator=(const basic_string& __str) 767 { return this->assign(__str); } 768 769 /** 770 * @brief Copy contents of @a s into this string. 771 * @param __s Source null-terminated string. 772 */ 773 basic_string& 774 operator=(const _CharT* __s) 775 { return this->assign(__s); } 776 777 /** 778 * @brief Set value to string of length 1. 779 * @param __c Source character. 780 * 781 * Assigning to a character makes this string length 1 and 782 * (*this)[0] == @a c. 783 */ 784 basic_string& 785 operator=(_CharT __c) 786 { 787 this->assign(1, __c); 788 return *this; 789 } 790 791 #if __cplusplus >= 201103L 792 /** 793 * @brief Move assign the value of @a str to this string. 794 * @param __str Source string. 795 * 796 * The contents of @a str are moved into this string (without copying). 797 * @a str is a valid, but unspecified string. 798 */ 799 basic_string& 800 operator=(basic_string&& __str) 801 _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value) 802 { 803 // NB: DR 1204. 804 this->swap(__str); 805 return *this; 806 } 807 808 /** 809 * @brief Set value to string constructed from initializer %list. 810 * @param __l std::initializer_list. 811 */ 812 basic_string& 813 operator=(initializer_list<_CharT> __l) 814 { 815 this->assign(__l.begin(), __l.size()); 816 return *this; 817 } 818 #endif // C++11 819 820 #ifdef __glibcxx_string_view // >= C++17 821 /** 822 * @brief Set value to string constructed from a string_view. 823 * @param __svt An object convertible to string_view. 824 */ 825 template<typename _Tp> 826 _If_sv<_Tp, basic_string&> 827 operator=(const _Tp& __svt) 828 { return this->assign(__svt); } 829 830 /** 831 * @brief Convert to a string_view. 832 * @return A string_view. 833 */ 834 operator __sv_type() const noexcept 835 { return __sv_type(data(), size()); } 836 #endif // C++17 837 838 // Iterators: 839 /** 840 * Returns a read/write iterator that points to the first character in 841 * the %string. Unshares the string. 842 */ 843 iterator 844 begin() // FIXME C++11: should be noexcept. 845 { 846 _M_leak(); 847 return iterator(_M_data()); 848 } 849 850 /** 851 * Returns a read-only (constant) iterator that points to the first 852 * character in the %string. 853 */ 854 const_iterator 855 begin() const _GLIBCXX_NOEXCEPT 856 { return const_iterator(_M_data()); } 857 858 /** 859 * Returns a read/write iterator that points one past the last 860 * character in the %string. Unshares the string. 861 */ 862 iterator 863 end() // FIXME C++11: should be noexcept. 864 { 865 _M_leak(); 866 return iterator(_M_data() + this->size()); 867 } 868 869 /** 870 * Returns a read-only (constant) iterator that points one past the 871 * last character in the %string. 872 */ 873 const_iterator 874 end() const _GLIBCXX_NOEXCEPT 875 { return const_iterator(_M_data() + this->size()); } 876 877 /** 878 * Returns a read/write reverse iterator that points to the last 879 * character in the %string. Iteration is done in reverse element 880 * order. Unshares the string. 881 */ 882 reverse_iterator 883 rbegin() // FIXME C++11: should be noexcept. 884 { return reverse_iterator(this->end()); } 885 886 /** 887 * Returns a read-only (constant) reverse iterator that points 888 * to the last character in the %string. Iteration is done in 889 * reverse element order. 890 */ 891 const_reverse_iterator 892 rbegin() const _GLIBCXX_NOEXCEPT 893 { return const_reverse_iterator(this->end()); } 894 895 /** 896 * Returns a read/write reverse iterator that points to one before the 897 * first character in the %string. Iteration is done in reverse 898 * element order. Unshares the string. 899 */ 900 reverse_iterator 901 rend() // FIXME C++11: should be noexcept. 902 { return reverse_iterator(this->begin()); } 903 904 /** 905 * Returns a read-only (constant) reverse iterator that points 906 * to one before the first character in the %string. Iteration 907 * is done in reverse element order. 908 */ 909 const_reverse_iterator 910 rend() const _GLIBCXX_NOEXCEPT 911 { return const_reverse_iterator(this->begin()); } 912 913 #if __cplusplus >= 201103L 914 /** 915 * Returns a read-only (constant) iterator that points to the first 916 * character in the %string. 917 */ 918 const_iterator 919 cbegin() const noexcept 920 { return const_iterator(this->_M_data()); } 921 922 /** 923 * Returns a read-only (constant) iterator that points one past the 924 * last character in the %string. 925 */ 926 const_iterator 927 cend() const noexcept 928 { return const_iterator(this->_M_data() + this->size()); } 929 930 /** 931 * Returns a read-only (constant) reverse iterator that points 932 * to the last character in the %string. Iteration is done in 933 * reverse element order. 934 */ 935 const_reverse_iterator 936 crbegin() const noexcept 937 { return const_reverse_iterator(this->end()); } 938 939 /** 940 * Returns a read-only (constant) reverse iterator that points 941 * to one before the first character in the %string. Iteration 942 * is done in reverse element order. 943 */ 944 const_reverse_iterator 945 crend() const noexcept 946 { return const_reverse_iterator(this->begin()); } 947 #endif 948 949 public: 950 // Capacity: 951 952 /// Returns the number of characters in the string, not including any 953 /// null-termination. 954 size_type 955 size() const _GLIBCXX_NOEXCEPT 956 { 957 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 && __OPTIMIZE__ 958 if (_S_empty_rep()._M_length != 0) 959 __builtin_unreachable(); 960 #endif 961 return _M_rep()->_M_length; 962 } 963 964 /// Returns the number of characters in the string, not including any 965 /// null-termination. 966 size_type 967 length() const _GLIBCXX_NOEXCEPT 968 { return size(); } 969 970 /// Returns the size() of the largest possible %string. 971 size_type 972 max_size() const _GLIBCXX_NOEXCEPT 973 { return _Rep::_S_max_size; } 974 975 /** 976 * @brief Resizes the %string to the specified number of characters. 977 * @param __n Number of characters the %string should contain. 978 * @param __c Character to fill any new elements. 979 * 980 * This function will %resize the %string to the specified 981 * number of characters. If the number is smaller than the 982 * %string's current size the %string is truncated, otherwise 983 * the %string is extended and new elements are %set to @a __c. 984 */ 985 void 986 resize(size_type __n, _CharT __c); 987 988 /** 989 * @brief Resizes the %string to the specified number of characters. 990 * @param __n Number of characters the %string should contain. 991 * 992 * This function will resize the %string to the specified length. If 993 * the new size is smaller than the %string's current size the %string 994 * is truncated, otherwise the %string is extended and new characters 995 * are default-constructed. For basic types such as char, this means 996 * setting them to 0. 997 */ 998 void 999 resize(size_type __n) 1000 { this->resize(__n, _CharT()); } 1001 1002 #if __cplusplus >= 201103L 1003 #pragma GCC diagnostic push 1004 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 1005 /// A non-binding request to reduce capacity() to size(). 1006 void 1007 shrink_to_fit() noexcept 1008 { reserve(); } 1009 #pragma GCC diagnostic pop 1010 #endif 1011 1012 #ifdef __glibcxx_string_resize_and_overwrite // C++ >= 23 1013 /** Resize the string and call a function to fill it. 1014 * 1015 * @param __n The maximum size requested. 1016 * @param __op A callable object that writes characters to the string. 1017 * 1018 * This is a low-level function that is easy to misuse, be careful. 1019 * 1020 * Calling `str.resize_and_overwrite(n, op)` will reserve at least `n` 1021 * characters in `str`, evaluate `n2 = std::move(op)(str.data(), n)`, 1022 * and finally set the string length to `n2` (adding a null terminator 1023 * at the end). The function object `op` is allowed to write to the 1024 * extra capacity added by the initial reserve operation, which is not 1025 * allowed if you just call `str.reserve(n)` yourself. 1026 * 1027 * This can be used to efficiently fill a `string` buffer without the 1028 * overhead of zero-initializing characters that will be overwritten 1029 * anyway. 1030 * 1031 * The callable `op` must not access the string directly (only through 1032 * the pointer passed as its first argument), must not write more than 1033 * `n` characters to the string, must return a value no greater than `n`, 1034 * and must ensure that all characters up to the returned length are 1035 * valid after it returns (i.e. there must be no uninitialized values 1036 * left in the string after the call, because accessing them would 1037 * have undefined behaviour). If `op` exits by throwing an exception 1038 * the behaviour is undefined. 1039 * 1040 * @since C++23 1041 */ 1042 template<typename _Operation> 1043 void 1044 resize_and_overwrite(size_type __n, _Operation __op); 1045 #endif // __glibcxx_string_resize_and_overwrite 1046 1047 #if __cplusplus >= 201103L 1048 /// Non-standard version of resize_and_overwrite for C++11 and above. 1049 template<typename _Operation> 1050 void 1051 __resize_and_overwrite(size_type __n, _Operation __op); 1052 #endif 1053 1054 /** 1055 * Returns the total number of characters that the %string can hold 1056 * before needing to allocate more memory. 1057 */ 1058 size_type 1059 capacity() const _GLIBCXX_NOEXCEPT 1060 { return _M_rep()->_M_capacity; } 1061 1062 /** 1063 * @brief Attempt to preallocate enough memory for specified number of 1064 * characters. 1065 * @param __res_arg Number of characters required. 1066 * @throw std::length_error If @a __res_arg exceeds @c max_size(). 1067 * 1068 * This function attempts to reserve enough memory for the 1069 * %string to hold the specified number of characters. If the 1070 * number requested is more than max_size(), length_error is 1071 * thrown. 1072 * 1073 * The advantage of this function is that if optimal code is a 1074 * necessity and the user can determine the string length that will be 1075 * required, the user can reserve the memory in %advance, and thus 1076 * prevent a possible reallocation of memory and copying of %string 1077 * data. 1078 */ 1079 void 1080 reserve(size_type __res_arg); 1081 1082 /// Equivalent to shrink_to_fit(). 1083 #if __cplusplus > 201703L 1084 [[deprecated("use shrink_to_fit() instead")]] 1085 #endif 1086 void 1087 reserve(); 1088 1089 /** 1090 * Erases the string, making it empty. 1091 */ 1092 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 1093 void 1094 clear() _GLIBCXX_NOEXCEPT 1095 { 1096 if (_M_rep()->_M_is_shared()) 1097 { 1098 _M_rep()->_M_dispose(this->get_allocator()); 1099 _M_data(_S_empty_rep()._M_refdata()); 1100 } 1101 else 1102 _M_rep()->_M_set_length_and_sharable(0); 1103 } 1104 #else 1105 // PR 56166: this should not throw. 1106 void 1107 clear() 1108 { _M_mutate(0, this->size(), 0); } 1109 #endif 1110 1111 /** 1112 * Returns true if the %string is empty. Equivalent to 1113 * <code>*this == ""</code>. 1114 */ 1115 _GLIBCXX_NODISCARD bool 1116 empty() const _GLIBCXX_NOEXCEPT 1117 { return this->size() == 0; } 1118 1119 // Element access: 1120 /** 1121 * @brief Subscript access to the data contained in the %string. 1122 * @param __pos The index of the character to access. 1123 * @return Read-only (constant) reference to the character. 1124 * 1125 * This operator allows for easy, array-style, data access. 1126 * Note that data access with this operator is unchecked and 1127 * out_of_range lookups are not defined. (For checked lookups 1128 * see at().) 1129 */ 1130 const_reference 1131 operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT 1132 { 1133 __glibcxx_assert(__pos <= size()); 1134 return _M_data()[__pos]; 1135 } 1136 1137 /** 1138 * @brief Subscript access to the data contained in the %string. 1139 * @param __pos The index of the character to access. 1140 * @return Read/write reference to the character. 1141 * 1142 * This operator allows for easy, array-style, data access. 1143 * Note that data access with this operator is unchecked and 1144 * out_of_range lookups are not defined. (For checked lookups 1145 * see at().) Unshares the string. 1146 */ 1147 reference 1148 operator[](size_type __pos) 1149 { 1150 // Allow pos == size() both in C++98 mode, as v3 extension, 1151 // and in C++11 mode. 1152 __glibcxx_assert(__pos <= size()); 1153 // In pedantic mode be strict in C++98 mode. 1154 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size()); 1155 _M_leak(); 1156 return _M_data()[__pos]; 1157 } 1158 1159 /** 1160 * @brief Provides access to the data contained in the %string. 1161 * @param __n The index of the character to access. 1162 * @return Read-only (const) reference to the character. 1163 * @throw std::out_of_range If @a n is an invalid index. 1164 * 1165 * This function provides for safer data access. The parameter is 1166 * first checked that it is in the range of the string. The function 1167 * throws out_of_range if the check fails. 1168 */ 1169 const_reference 1170 at(size_type __n) const 1171 { 1172 if (__n >= this->size()) 1173 __throw_out_of_range_fmt(__N("basic_string::at: __n " 1174 "(which is %zu) >= this->size() " 1175 "(which is %zu)"), 1176 __n, this->size()); 1177 return _M_data()[__n]; 1178 } 1179 1180 /** 1181 * @brief Provides access to the data contained in the %string. 1182 * @param __n The index of the character to access. 1183 * @return Read/write reference to the character. 1184 * @throw std::out_of_range If @a n is an invalid index. 1185 * 1186 * This function provides for safer data access. The parameter is 1187 * first checked that it is in the range of the string. The function 1188 * throws out_of_range if the check fails. Success results in 1189 * unsharing the string. 1190 */ 1191 reference 1192 at(size_type __n) 1193 { 1194 if (__n >= size()) 1195 __throw_out_of_range_fmt(__N("basic_string::at: __n " 1196 "(which is %zu) >= this->size() " 1197 "(which is %zu)"), 1198 __n, this->size()); 1199 _M_leak(); 1200 return _M_data()[__n]; 1201 } 1202 1203 #if __cplusplus >= 201103L 1204 /** 1205 * Returns a read/write reference to the data at the first 1206 * element of the %string. 1207 */ 1208 reference 1209 front() 1210 { 1211 __glibcxx_assert(!empty()); 1212 return operator[](0); 1213 } 1214 1215 /** 1216 * Returns a read-only (constant) reference to the data at the first 1217 * element of the %string. 1218 */ 1219 const_reference 1220 front() const noexcept 1221 { 1222 __glibcxx_assert(!empty()); 1223 return operator[](0); 1224 } 1225 1226 /** 1227 * Returns a read/write reference to the data at the last 1228 * element of the %string. 1229 */ 1230 reference 1231 back() 1232 { 1233 __glibcxx_assert(!empty()); 1234 return operator[](this->size() - 1); 1235 } 1236 1237 /** 1238 * Returns a read-only (constant) reference to the data at the 1239 * last element of the %string. 1240 */ 1241 const_reference 1242 back() const noexcept 1243 { 1244 __glibcxx_assert(!empty()); 1245 return operator[](this->size() - 1); 1246 } 1247 #endif 1248 1249 // Modifiers: 1250 /** 1251 * @brief Append a string to this string. 1252 * @param __str The string to append. 1253 * @return Reference to this string. 1254 */ 1255 basic_string& 1256 operator+=(const basic_string& __str) 1257 { return this->append(__str); } 1258 1259 /** 1260 * @brief Append a C string. 1261 * @param __s The C string to append. 1262 * @return Reference to this string. 1263 */ 1264 basic_string& 1265 operator+=(const _CharT* __s) 1266 { return this->append(__s); } 1267 1268 /** 1269 * @brief Append a character. 1270 * @param __c The character to append. 1271 * @return Reference to this string. 1272 */ 1273 basic_string& 1274 operator+=(_CharT __c) 1275 { 1276 this->push_back(__c); 1277 return *this; 1278 } 1279 1280 #if __cplusplus >= 201103L 1281 /** 1282 * @brief Append an initializer_list of characters. 1283 * @param __l The initializer_list of characters to be appended. 1284 * @return Reference to this string. 1285 */ 1286 basic_string& 1287 operator+=(initializer_list<_CharT> __l) 1288 { return this->append(__l.begin(), __l.size()); } 1289 #endif // C++11 1290 1291 #ifdef __glibcxx_string_view // >= C++17 1292 /** 1293 * @brief Append a string_view. 1294 * @param __svt The object convertible to string_view to be appended. 1295 * @return Reference to this string. 1296 */ 1297 template<typename _Tp> 1298 _If_sv<_Tp, basic_string&> 1299 operator+=(const _Tp& __svt) 1300 { return this->append(__svt); } 1301 #endif // C++17 1302 1303 /** 1304 * @brief Append a string to this string. 1305 * @param __str The string to append. 1306 * @return Reference to this string. 1307 */ 1308 basic_string& 1309 append(const basic_string& __str); 1310 1311 /** 1312 * @brief Append a substring. 1313 * @param __str The string to append. 1314 * @param __pos Index of the first character of str to append. 1315 * @param __n The number of characters to append. 1316 * @return Reference to this string. 1317 * @throw std::out_of_range if @a __pos is not a valid index. 1318 * 1319 * This function appends @a __n characters from @a __str 1320 * starting at @a __pos to this string. If @a __n is is larger 1321 * than the number of available characters in @a __str, the 1322 * remainder of @a __str is appended. 1323 */ 1324 basic_string& 1325 append(const basic_string& __str, size_type __pos, size_type __n = npos); 1326 1327 /** 1328 * @brief Append a C substring. 1329 * @param __s The C string to append. 1330 * @param __n The number of characters to append. 1331 * @return Reference to this string. 1332 */ 1333 basic_string& 1334 append(const _CharT* __s, size_type __n); 1335 1336 /** 1337 * @brief Append a C string. 1338 * @param __s The C string to append. 1339 * @return Reference to this string. 1340 */ 1341 basic_string& 1342 append(const _CharT* __s) 1343 { 1344 __glibcxx_requires_string(__s); 1345 return this->append(__s, traits_type::length(__s)); 1346 } 1347 1348 /** 1349 * @brief Append multiple characters. 1350 * @param __n The number of characters to append. 1351 * @param __c The character to use. 1352 * @return Reference to this string. 1353 * 1354 * Appends __n copies of __c to this string. 1355 */ 1356 basic_string& 1357 append(size_type __n, _CharT __c); 1358 1359 #if __glibcxx_containers_ranges // C++ >= 23 1360 /** 1361 * @brief Append a range to the string. 1362 * @since C++23 1363 */ 1364 template<__detail::__container_compatible_range<_CharT> _Rg> 1365 basic_string& 1366 append_range(_Rg&& __rg) 1367 { 1368 basic_string __s(from_range, std::forward<_Rg>(__rg), 1369 get_allocator()); 1370 append(__s); 1371 return *this; 1372 } 1373 #endif 1374 1375 #if __cplusplus >= 201103L 1376 /** 1377 * @brief Append an initializer_list of characters. 1378 * @param __l The initializer_list of characters to append. 1379 * @return Reference to this string. 1380 */ 1381 basic_string& 1382 append(initializer_list<_CharT> __l) 1383 { return this->append(__l.begin(), __l.size()); } 1384 #endif // C++11 1385 1386 /** 1387 * @brief Append a range of characters. 1388 * @param __first Iterator referencing the first character to append. 1389 * @param __last Iterator marking the end of the range. 1390 * @return Reference to this string. 1391 * 1392 * Appends characters in the range [__first,__last) to this string. 1393 */ 1394 template<class _InputIterator> 1395 basic_string& 1396 append(_InputIterator __first, _InputIterator __last) 1397 { return this->replace(_M_iend(), _M_iend(), __first, __last); } 1398 1399 #ifdef __glibcxx_string_view // >= C++17 1400 /** 1401 * @brief Append a string_view. 1402 * @param __svt The object convertible to string_view to be appended. 1403 * @return Reference to this string. 1404 */ 1405 template<typename _Tp> 1406 _If_sv<_Tp, basic_string&> 1407 append(const _Tp& __svt) 1408 { 1409 __sv_type __sv = __svt; 1410 return this->append(__sv.data(), __sv.size()); 1411 } 1412 1413 /** 1414 * @brief Append a range of characters from a string_view. 1415 * @param __svt The object convertible to string_view to be appended 1416 * from. 1417 * @param __pos The position in the string_view to append from. 1418 * @param __n The number of characters to append from the string_view. 1419 * @return Reference to this string. 1420 */ 1421 template<typename _Tp> 1422 _If_sv<_Tp, basic_string&> 1423 append(const _Tp& __svt, size_type __pos, size_type __n = npos) 1424 { 1425 __sv_type __sv = __svt; 1426 return append(__sv.data() 1427 + std::__sv_check(__sv.size(), __pos, "basic_string::append"), 1428 std::__sv_limit(__sv.size(), __pos, __n)); 1429 } 1430 #endif // C++17 1431 1432 /** 1433 * @brief Append a single character. 1434 * @param __c Character to append. 1435 */ 1436 void 1437 push_back(_CharT __c) 1438 { 1439 const size_type __len = 1 + this->size(); 1440 if (__len > this->capacity() || _M_rep()->_M_is_shared()) 1441 this->reserve(__len); 1442 traits_type::assign(_M_data()[this->size()], __c); 1443 _M_rep()->_M_set_length_and_sharable(__len); 1444 } 1445 1446 /** 1447 * @brief Set value to contents of another string. 1448 * @param __str Source string to use. 1449 * @return Reference to this string. 1450 */ 1451 basic_string& 1452 assign(const basic_string& __str); 1453 1454 #if __cplusplus >= 201103L 1455 /** 1456 * @brief Set value to contents of another string. 1457 * @param __str Source string to use. 1458 * @return Reference to this string. 1459 * 1460 * This function sets this string to the exact contents of @a __str. 1461 * @a __str is a valid, but unspecified string. 1462 */ 1463 basic_string& 1464 assign(basic_string&& __str) 1465 noexcept(allocator_traits<_Alloc>::is_always_equal::value) 1466 { 1467 this->swap(__str); 1468 return *this; 1469 } 1470 #endif // C++11 1471 1472 /** 1473 * @brief Set value to a substring of a string. 1474 * @param __str The string to use. 1475 * @param __pos Index of the first character of str. 1476 * @param __n Number of characters to use. 1477 * @return Reference to this string. 1478 * @throw std::out_of_range if @a pos is not a valid index. 1479 * 1480 * This function sets this string to the substring of @a __str 1481 * consisting of @a __n characters at @a __pos. If @a __n is 1482 * is larger than the number of available characters in @a 1483 * __str, the remainder of @a __str is used. 1484 */ 1485 basic_string& 1486 assign(const basic_string& __str, size_type __pos, size_type __n = npos) 1487 { return this->assign(__str._M_data() 1488 + __str._M_check(__pos, "basic_string::assign"), 1489 __str._M_limit(__pos, __n)); } 1490 1491 /** 1492 * @brief Set value to a C substring. 1493 * @param __s The C string to use. 1494 * @param __n Number of characters to use. 1495 * @return Reference to this string. 1496 * 1497 * This function sets the value of this string to the first @a __n 1498 * characters of @a __s. If @a __n is is larger than the number of 1499 * available characters in @a __s, the remainder of @a __s is used. 1500 */ 1501 basic_string& 1502 assign(const _CharT* __s, size_type __n); 1503 1504 /** 1505 * @brief Set value to contents of a C string. 1506 * @param __s The C string to use. 1507 * @return Reference to this string. 1508 * 1509 * This function sets the value of this string to the value of @a __s. 1510 * The data is copied, so there is no dependence on @a __s once the 1511 * function returns. 1512 */ 1513 basic_string& 1514 assign(const _CharT* __s) 1515 { 1516 __glibcxx_requires_string(__s); 1517 return this->assign(__s, traits_type::length(__s)); 1518 } 1519 1520 /** 1521 * @brief Set value to multiple characters. 1522 * @param __n Length of the resulting string. 1523 * @param __c The character to use. 1524 * @return Reference to this string. 1525 * 1526 * This function sets the value of this string to @a __n copies of 1527 * character @a __c. 1528 */ 1529 basic_string& 1530 assign(size_type __n, _CharT __c) 1531 { return _M_replace_aux(size_type(0), this->size(), __n, __c); } 1532 1533 /** 1534 * @brief Set value to a range of characters. 1535 * @param __first Iterator referencing the first character to append. 1536 * @param __last Iterator marking the end of the range. 1537 * @return Reference to this string. 1538 * 1539 * Sets value of string to characters in the range [__first,__last). 1540 */ 1541 template<class _InputIterator> 1542 basic_string& 1543 assign(_InputIterator __first, _InputIterator __last) 1544 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); } 1545 1546 #if __glibcxx_containers_ranges // C++ >= 23 1547 /** 1548 * @brief Set value to a range of characters. 1549 * @since C++23 1550 */ 1551 template<__detail::__container_compatible_range<_CharT> _Rg> 1552 basic_string& 1553 assign_range(_Rg&& __rg) 1554 { 1555 basic_string __s(from_range, std::forward<_Rg>(__rg), 1556 get_allocator()); 1557 assign(std::move(__s)); 1558 return *this; 1559 } 1560 #endif 1561 1562 #if __cplusplus >= 201103L 1563 /** 1564 * @brief Set value to an initializer_list of characters. 1565 * @param __l The initializer_list of characters to assign. 1566 * @return Reference to this string. 1567 */ 1568 basic_string& 1569 assign(initializer_list<_CharT> __l) 1570 { return this->assign(__l.begin(), __l.size()); } 1571 #endif // C++11 1572 1573 #ifdef __glibcxx_string_view // >= C++17 1574 /** 1575 * @brief Set value from a string_view. 1576 * @param __svt The source object convertible to string_view. 1577 * @return Reference to this string. 1578 */ 1579 template<typename _Tp> 1580 _If_sv<_Tp, basic_string&> 1581 assign(const _Tp& __svt) 1582 { 1583 __sv_type __sv = __svt; 1584 return this->assign(__sv.data(), __sv.size()); 1585 } 1586 1587 /** 1588 * @brief Set value from a range of characters in a string_view. 1589 * @param __svt The source object convertible to string_view. 1590 * @param __pos The position in the string_view to assign from. 1591 * @param __n The number of characters to assign. 1592 * @return Reference to this string. 1593 */ 1594 template<typename _Tp> 1595 _If_sv<_Tp, basic_string&> 1596 assign(const _Tp& __svt, size_type __pos, size_type __n = npos) 1597 { 1598 __sv_type __sv = __svt; 1599 return assign(__sv.data() 1600 + std::__sv_check(__sv.size(), __pos, "basic_string::assign"), 1601 std::__sv_limit(__sv.size(), __pos, __n)); 1602 } 1603 #endif // C++17 1604 1605 /** 1606 * @brief Insert multiple characters. 1607 * @param __p Iterator referencing location in string to insert at. 1608 * @param __n Number of characters to insert 1609 * @param __c The character to insert. 1610 * @throw std::length_error If new length exceeds @c max_size(). 1611 * 1612 * Inserts @a __n copies of character @a __c starting at the 1613 * position referenced by iterator @a __p. If adding 1614 * characters causes the length to exceed max_size(), 1615 * length_error is thrown. The value of the string doesn't 1616 * change if an error is thrown. 1617 */ 1618 void 1619 insert(iterator __p, size_type __n, _CharT __c) 1620 { this->replace(__p, __p, __n, __c); } 1621 1622 /** 1623 * @brief Insert a range of characters. 1624 * @param __p Iterator referencing location in string to insert at. 1625 * @param __beg Start of range. 1626 * @param __end End of range. 1627 * @throw std::length_error If new length exceeds @c max_size(). 1628 * 1629 * Inserts characters in range [__beg,__end). If adding 1630 * characters causes the length to exceed max_size(), 1631 * length_error is thrown. The value of the string doesn't 1632 * change if an error is thrown. 1633 */ 1634 template<class _InputIterator> 1635 void 1636 insert(iterator __p, _InputIterator __beg, _InputIterator __end) 1637 { this->replace(__p, __p, __beg, __end); } 1638 1639 #if __glibcxx_containers_ranges // C++ >= 23 1640 /** 1641 * @brief Insert a range into the string. 1642 * @since C++23 1643 */ 1644 template<__detail::__container_compatible_range<_CharT> _Rg> 1645 iterator 1646 insert_range(const_iterator __p, _Rg&& __rg) 1647 { 1648 auto __pos = __p - cbegin(); 1649 1650 if constexpr (ranges::forward_range<_Rg>) 1651 if (ranges::empty(__rg)) 1652 return begin() + __pos; 1653 1654 if (__p == cend()) 1655 append_range(std::forward<_Rg>(__rg)); 1656 else 1657 { 1658 basic_string __s(from_range, std::forward<_Rg>(__rg), 1659 get_allocator()); 1660 insert(__pos, __s); 1661 } 1662 return begin() + __pos; 1663 } 1664 #endif 1665 1666 #if __cplusplus >= 201103L 1667 /** 1668 * @brief Insert an initializer_list of characters. 1669 * @param __p Iterator referencing location in string to insert at. 1670 * @param __l The initializer_list of characters to insert. 1671 * @throw std::length_error If new length exceeds @c max_size(). 1672 */ 1673 void 1674 insert(iterator __p, initializer_list<_CharT> __l) 1675 { 1676 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend()); 1677 this->insert(__p - _M_ibegin(), __l.begin(), __l.size()); 1678 } 1679 #endif // C++11 1680 1681 /** 1682 * @brief Insert value of a string. 1683 * @param __pos1 Position in string to insert at. 1684 * @param __str The string to insert. 1685 * @return Reference to this string. 1686 * @throw std::length_error If new length exceeds @c max_size(). 1687 * 1688 * Inserts value of @a __str starting at @a __pos1. If adding 1689 * characters causes the length to exceed max_size(), 1690 * length_error is thrown. The value of the string doesn't 1691 * change if an error is thrown. 1692 */ 1693 basic_string& 1694 insert(size_type __pos1, const basic_string& __str) 1695 { return this->insert(__pos1, __str, size_type(0), __str.size()); } 1696 1697 /** 1698 * @brief Insert a substring. 1699 * @param __pos1 Position in string to insert at. 1700 * @param __str The string to insert. 1701 * @param __pos2 Start of characters in str to insert. 1702 * @param __n Number of characters to insert. 1703 * @return Reference to this string. 1704 * @throw std::length_error If new length exceeds @c max_size(). 1705 * @throw std::out_of_range If @a pos1 > size() or 1706 * @a __pos2 > @a str.size(). 1707 * 1708 * Starting at @a pos1, insert @a __n character of @a __str 1709 * beginning with @a __pos2. If adding characters causes the 1710 * length to exceed max_size(), length_error is thrown. If @a 1711 * __pos1 is beyond the end of this string or @a __pos2 is 1712 * beyond the end of @a __str, out_of_range is thrown. The 1713 * value of the string doesn't change if an error is thrown. 1714 */ 1715 basic_string& 1716 insert(size_type __pos1, const basic_string& __str, 1717 size_type __pos2, size_type __n = npos) 1718 { return this->insert(__pos1, __str._M_data() 1719 + __str._M_check(__pos2, "basic_string::insert"), 1720 __str._M_limit(__pos2, __n)); } 1721 1722 /** 1723 * @brief Insert a C substring. 1724 * @param __pos Position in string to insert at. 1725 * @param __s The C string to insert. 1726 * @param __n The number of characters to insert. 1727 * @return Reference to this string. 1728 * @throw std::length_error If new length exceeds @c max_size(). 1729 * @throw std::out_of_range If @a __pos is beyond the end of this 1730 * string. 1731 * 1732 * Inserts the first @a __n characters of @a __s starting at @a 1733 * __pos. If adding characters causes the length to exceed 1734 * max_size(), length_error is thrown. If @a __pos is beyond 1735 * end(), out_of_range is thrown. The value of the string 1736 * doesn't change if an error is thrown. 1737 */ 1738 basic_string& 1739 insert(size_type __pos, const _CharT* __s, size_type __n); 1740 1741 /** 1742 * @brief Insert a C string. 1743 * @param __pos Position in string to insert at. 1744 * @param __s The C string to insert. 1745 * @return Reference to this string. 1746 * @throw std::length_error If new length exceeds @c max_size(). 1747 * @throw std::out_of_range If @a pos is beyond the end of this 1748 * string. 1749 * 1750 * Inserts the first @a n characters of @a __s starting at @a __pos. If 1751 * adding characters causes the length to exceed max_size(), 1752 * length_error is thrown. If @a __pos is beyond end(), out_of_range is 1753 * thrown. The value of the string doesn't change if an error is 1754 * thrown. 1755 */ 1756 basic_string& 1757 insert(size_type __pos, const _CharT* __s) 1758 { 1759 __glibcxx_requires_string(__s); 1760 return this->insert(__pos, __s, traits_type::length(__s)); 1761 } 1762 1763 /** 1764 * @brief Insert multiple characters. 1765 * @param __pos Index in string to insert at. 1766 * @param __n Number of characters to insert 1767 * @param __c The character to insert. 1768 * @return Reference to this string. 1769 * @throw std::length_error If new length exceeds @c max_size(). 1770 * @throw std::out_of_range If @a __pos is beyond the end of this 1771 * string. 1772 * 1773 * Inserts @a __n copies of character @a __c starting at index 1774 * @a __pos. If adding characters causes the length to exceed 1775 * max_size(), length_error is thrown. If @a __pos > length(), 1776 * out_of_range is thrown. The value of the string doesn't 1777 * change if an error is thrown. 1778 */ 1779 basic_string& 1780 insert(size_type __pos, size_type __n, _CharT __c) 1781 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"), 1782 size_type(0), __n, __c); } 1783 1784 /** 1785 * @brief Insert one character. 1786 * @param __p Iterator referencing position in string to insert at. 1787 * @param __c The character to insert. 1788 * @return Iterator referencing newly inserted char. 1789 * @throw std::length_error If new length exceeds @c max_size(). 1790 * 1791 * Inserts character @a __c at position referenced by @a __p. 1792 * If adding character causes the length to exceed max_size(), 1793 * length_error is thrown. If @a __p is beyond end of string, 1794 * out_of_range is thrown. The value of the string doesn't 1795 * change if an error is thrown. 1796 */ 1797 iterator 1798 insert(iterator __p, _CharT __c) 1799 { 1800 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend()); 1801 const size_type __pos = __p - _M_ibegin(); 1802 _M_replace_aux(__pos, size_type(0), size_type(1), __c); 1803 _M_rep()->_M_set_leaked(); 1804 return iterator(_M_data() + __pos); 1805 } 1806 1807 #ifdef __glibcxx_string_view // >= C++17 1808 /** 1809 * @brief Insert a string_view. 1810 * @param __pos Position in string to insert at. 1811 * @param __svt The object convertible to string_view to insert. 1812 * @return Reference to this string. 1813 */ 1814 template<typename _Tp> 1815 _If_sv<_Tp, basic_string&> 1816 insert(size_type __pos, const _Tp& __svt) 1817 { 1818 __sv_type __sv = __svt; 1819 return this->insert(__pos, __sv.data(), __sv.size()); 1820 } 1821 1822 /** 1823 * @brief Insert a string_view. 1824 * @param __pos1 Position in string to insert at. 1825 * @param __svt The object convertible to string_view to insert from. 1826 * @param __pos2 Position in string_view to insert from. 1827 * @param __n The number of characters to insert. 1828 * @return Reference to this string. 1829 */ 1830 template<typename _Tp> 1831 _If_sv<_Tp, basic_string&> 1832 insert(size_type __pos1, const _Tp& __svt, 1833 size_type __pos2, size_type __n = npos) 1834 { 1835 __sv_type __sv = __svt; 1836 return this->replace(__pos1, size_type(0), __sv.data() 1837 + std::__sv_check(__sv.size(), __pos2, "basic_string::insert"), 1838 std::__sv_limit(__sv.size(), __pos2, __n)); 1839 } 1840 #endif // C++17 1841 1842 /** 1843 * @brief Remove characters. 1844 * @param __pos Index of first character to remove (default 0). 1845 * @param __n Number of characters to remove (default remainder). 1846 * @return Reference to this string. 1847 * @throw std::out_of_range If @a pos is beyond the end of this 1848 * string. 1849 * 1850 * Removes @a __n characters from this string starting at @a 1851 * __pos. The length of the string is reduced by @a __n. If 1852 * there are < @a __n characters to remove, the remainder of 1853 * the string is truncated. If @a __p is beyond end of string, 1854 * out_of_range is thrown. The value of the string doesn't 1855 * change if an error is thrown. 1856 */ 1857 basic_string& 1858 erase(size_type __pos = 0, size_type __n = npos) 1859 { 1860 _M_mutate(_M_check(__pos, "basic_string::erase"), 1861 _M_limit(__pos, __n), size_type(0)); 1862 return *this; 1863 } 1864 1865 /** 1866 * @brief Remove one character. 1867 * @param __position Iterator referencing the character to remove. 1868 * @return iterator referencing same location after removal. 1869 * 1870 * Removes the character at @a __position from this string. The value 1871 * of the string doesn't change if an error is thrown. 1872 */ 1873 iterator 1874 erase(iterator __position) 1875 { 1876 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin() 1877 && __position < _M_iend()); 1878 const size_type __pos = __position - _M_ibegin(); 1879 _M_mutate(__pos, size_type(1), size_type(0)); 1880 _M_rep()->_M_set_leaked(); 1881 return iterator(_M_data() + __pos); 1882 } 1883 1884 /** 1885 * @brief Remove a range of characters. 1886 * @param __first Iterator referencing the first character to remove. 1887 * @param __last Iterator referencing the end of the range. 1888 * @return Iterator referencing location of first after removal. 1889 * 1890 * Removes the characters in the range [first,last) from this string. 1891 * The value of the string doesn't change if an error is thrown. 1892 */ 1893 iterator 1894 erase(iterator __first, iterator __last); 1895 1896 #if __cplusplus >= 201103L 1897 /** 1898 * @brief Remove the last character. 1899 * 1900 * The string must be non-empty. 1901 */ 1902 void 1903 pop_back() // FIXME C++11: should be noexcept. 1904 { 1905 __glibcxx_assert(!empty()); 1906 erase(size() - 1, 1); 1907 } 1908 #endif // C++11 1909 1910 /** 1911 * @brief Replace characters with value from another string. 1912 * @param __pos Index of first character to replace. 1913 * @param __n Number of characters to be replaced. 1914 * @param __str String to insert. 1915 * @return Reference to this string. 1916 * @throw std::out_of_range If @a pos is beyond the end of this 1917 * string. 1918 * @throw std::length_error If new length exceeds @c max_size(). 1919 * 1920 * Removes the characters in the range [__pos,__pos+__n) from 1921 * this string. In place, the value of @a __str is inserted. 1922 * If @a __pos is beyond end of string, out_of_range is thrown. 1923 * If the length of the result exceeds max_size(), length_error 1924 * is thrown. The value of the string doesn't change if an 1925 * error is thrown. 1926 */ 1927 basic_string& 1928 replace(size_type __pos, size_type __n, const basic_string& __str) 1929 { return this->replace(__pos, __n, __str._M_data(), __str.size()); } 1930 1931 /** 1932 * @brief Replace characters with value from another string. 1933 * @param __pos1 Index of first character to replace. 1934 * @param __n1 Number of characters to be replaced. 1935 * @param __str String to insert. 1936 * @param __pos2 Index of first character of str to use. 1937 * @param __n2 Number of characters from str to use. 1938 * @return Reference to this string. 1939 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 > 1940 * __str.size(). 1941 * @throw std::length_error If new length exceeds @c max_size(). 1942 * 1943 * Removes the characters in the range [__pos1,__pos1 + n) from this 1944 * string. In place, the value of @a __str is inserted. If @a __pos is 1945 * beyond end of string, out_of_range is thrown. If the length of the 1946 * result exceeds max_size(), length_error is thrown. The value of the 1947 * string doesn't change if an error is thrown. 1948 */ 1949 basic_string& 1950 replace(size_type __pos1, size_type __n1, const basic_string& __str, 1951 size_type __pos2, size_type __n2 = npos) 1952 { return this->replace(__pos1, __n1, __str._M_data() 1953 + __str._M_check(__pos2, "basic_string::replace"), 1954 __str._M_limit(__pos2, __n2)); } 1955 1956 /** 1957 * @brief Replace characters with value of a C substring. 1958 * @param __pos Index of first character to replace. 1959 * @param __n1 Number of characters to be replaced. 1960 * @param __s C string to insert. 1961 * @param __n2 Number of characters from @a s to use. 1962 * @return Reference to this string. 1963 * @throw std::out_of_range If @a pos1 > size(). 1964 * @throw std::length_error If new length exceeds @c max_size(). 1965 * 1966 * Removes the characters in the range [__pos,__pos + __n1) 1967 * from this string. In place, the first @a __n2 characters of 1968 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If 1969 * @a __pos is beyond end of string, out_of_range is thrown. If 1970 * the length of result exceeds max_size(), length_error is 1971 * thrown. The value of the string doesn't change if an error 1972 * is thrown. 1973 */ 1974 basic_string& 1975 replace(size_type __pos, size_type __n1, const _CharT* __s, 1976 size_type __n2); 1977 1978 /** 1979 * @brief Replace characters with value of a C string. 1980 * @param __pos Index of first character to replace. 1981 * @param __n1 Number of characters to be replaced. 1982 * @param __s C string to insert. 1983 * @return Reference to this string. 1984 * @throw std::out_of_range If @a pos > size(). 1985 * @throw std::length_error If new length exceeds @c max_size(). 1986 * 1987 * Removes the characters in the range [__pos,__pos + __n1) 1988 * from this string. In place, the characters of @a __s are 1989 * inserted. If @a __pos is beyond end of string, out_of_range 1990 * is thrown. If the length of result exceeds max_size(), 1991 * length_error is thrown. The value of the string doesn't 1992 * change if an error is thrown. 1993 */ 1994 basic_string& 1995 replace(size_type __pos, size_type __n1, const _CharT* __s) 1996 { 1997 __glibcxx_requires_string(__s); 1998 return this->replace(__pos, __n1, __s, traits_type::length(__s)); 1999 } 2000 2001 /** 2002 * @brief Replace characters with multiple characters. 2003 * @param __pos Index of first character to replace. 2004 * @param __n1 Number of characters to be replaced. 2005 * @param __n2 Number of characters to insert. 2006 * @param __c Character to insert. 2007 * @return Reference to this string. 2008 * @throw std::out_of_range If @a __pos > size(). 2009 * @throw std::length_error If new length exceeds @c max_size(). 2010 * 2011 * Removes the characters in the range [pos,pos + n1) from this 2012 * string. In place, @a __n2 copies of @a __c are inserted. 2013 * If @a __pos is beyond end of string, out_of_range is thrown. 2014 * If the length of result exceeds max_size(), length_error is 2015 * thrown. The value of the string doesn't change if an error 2016 * is thrown. 2017 */ 2018 basic_string& 2019 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c) 2020 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"), 2021 _M_limit(__pos, __n1), __n2, __c); } 2022 2023 /** 2024 * @brief Replace range of characters with string. 2025 * @param __i1 Iterator referencing start of range to replace. 2026 * @param __i2 Iterator referencing end of range to replace. 2027 * @param __str String value to insert. 2028 * @return Reference to this string. 2029 * @throw std::length_error If new length exceeds @c max_size(). 2030 * 2031 * Removes the characters in the range [__i1,__i2). In place, 2032 * the value of @a __str is inserted. If the length of result 2033 * exceeds max_size(), length_error is thrown. The value of 2034 * the string doesn't change if an error is thrown. 2035 */ 2036 basic_string& 2037 replace(iterator __i1, iterator __i2, const basic_string& __str) 2038 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); } 2039 2040 /** 2041 * @brief Replace range of characters with C substring. 2042 * @param __i1 Iterator referencing start of range to replace. 2043 * @param __i2 Iterator referencing end of range to replace. 2044 * @param __s C string value to insert. 2045 * @param __n Number of characters from s to insert. 2046 * @return Reference to this string. 2047 * @throw std::length_error If new length exceeds @c max_size(). 2048 * 2049 * Removes the characters in the range [__i1,__i2). In place, 2050 * the first @a __n characters of @a __s are inserted. If the 2051 * length of result exceeds max_size(), length_error is thrown. 2052 * The value of the string doesn't change if an error is 2053 * thrown. 2054 */ 2055 basic_string& 2056 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n) 2057 { 2058 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 2059 && __i2 <= _M_iend()); 2060 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n); 2061 } 2062 2063 /** 2064 * @brief Replace range of characters with C string. 2065 * @param __i1 Iterator referencing start of range to replace. 2066 * @param __i2 Iterator referencing end of range to replace. 2067 * @param __s C string value to insert. 2068 * @return Reference to this string. 2069 * @throw std::length_error If new length exceeds @c max_size(). 2070 * 2071 * Removes the characters in the range [__i1,__i2). In place, 2072 * the characters of @a __s are inserted. If the length of 2073 * result exceeds max_size(), length_error is thrown. The 2074 * value of the string doesn't change if an error is thrown. 2075 */ 2076 basic_string& 2077 replace(iterator __i1, iterator __i2, const _CharT* __s) 2078 { 2079 __glibcxx_requires_string(__s); 2080 return this->replace(__i1, __i2, __s, traits_type::length(__s)); 2081 } 2082 2083 /** 2084 * @brief Replace range of characters with multiple characters 2085 * @param __i1 Iterator referencing start of range to replace. 2086 * @param __i2 Iterator referencing end of range to replace. 2087 * @param __n Number of characters to insert. 2088 * @param __c Character to insert. 2089 * @return Reference to this string. 2090 * @throw std::length_error If new length exceeds @c max_size(). 2091 * 2092 * Removes the characters in the range [__i1,__i2). In place, 2093 * @a __n copies of @a __c are inserted. If the length of 2094 * result exceeds max_size(), length_error is thrown. The 2095 * value of the string doesn't change if an error is thrown. 2096 */ 2097 basic_string& 2098 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c) 2099 { 2100 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 2101 && __i2 <= _M_iend()); 2102 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c); 2103 } 2104 2105 /** 2106 * @brief Replace range of characters with range. 2107 * @param __i1 Iterator referencing start of range to replace. 2108 * @param __i2 Iterator referencing end of range to replace. 2109 * @param __k1 Iterator referencing start of range to insert. 2110 * @param __k2 Iterator referencing end of range to insert. 2111 * @return Reference to this string. 2112 * @throw std::length_error If new length exceeds @c max_size(). 2113 * 2114 * Removes the characters in the range [__i1,__i2). In place, 2115 * characters in the range [__k1,__k2) are inserted. If the 2116 * length of result exceeds max_size(), length_error is thrown. 2117 * The value of the string doesn't change if an error is 2118 * thrown. 2119 */ 2120 template<class _InputIterator> 2121 basic_string& 2122 replace(iterator __i1, iterator __i2, 2123 _InputIterator __k1, _InputIterator __k2) 2124 { 2125 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 2126 && __i2 <= _M_iend()); 2127 __glibcxx_requires_valid_range(__k1, __k2); 2128 typedef typename std::__is_integer<_InputIterator>::__type _Integral; 2129 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral()); 2130 } 2131 2132 // Specializations for the common case of pointer and iterator: 2133 // useful to avoid the overhead of temporary buffering in _M_replace. 2134 basic_string& 2135 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2) 2136 { 2137 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 2138 && __i2 <= _M_iend()); 2139 __glibcxx_requires_valid_range(__k1, __k2); 2140 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, 2141 __k1, __k2 - __k1); 2142 } 2143 2144 basic_string& 2145 replace(iterator __i1, iterator __i2, 2146 const _CharT* __k1, const _CharT* __k2) 2147 { 2148 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 2149 && __i2 <= _M_iend()); 2150 __glibcxx_requires_valid_range(__k1, __k2); 2151 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, 2152 __k1, __k2 - __k1); 2153 } 2154 2155 basic_string& 2156 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2) 2157 { 2158 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 2159 && __i2 <= _M_iend()); 2160 __glibcxx_requires_valid_range(__k1, __k2); 2161 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, 2162 __k1.base(), __k2 - __k1); 2163 } 2164 2165 basic_string& 2166 replace(iterator __i1, iterator __i2, 2167 const_iterator __k1, const_iterator __k2) 2168 { 2169 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 2170 && __i2 <= _M_iend()); 2171 __glibcxx_requires_valid_range(__k1, __k2); 2172 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, 2173 __k1.base(), __k2 - __k1); 2174 } 2175 2176 #if __glibcxx_containers_ranges // C++ >= 23 2177 /** 2178 * @brief Replace part of the string with a range. 2179 * @since C++23 2180 */ 2181 template<__detail::__container_compatible_range<_CharT> _Rg> 2182 basic_string& 2183 replace_with_range(const_iterator __i1, const_iterator __i2, _Rg&& __rg) 2184 { 2185 if (__i1 == cend()) 2186 append_range(std::forward<_Rg>(__rg)); 2187 else 2188 { 2189 basic_string __s(from_range, std::forward<_Rg>(__rg), 2190 get_allocator()); 2191 replace(__i1 - cbegin(), __i2 - __i1, __s); 2192 } 2193 return *this; 2194 } 2195 #endif 2196 2197 #if __cplusplus >= 201103L 2198 /** 2199 * @brief Replace range of characters with initializer_list. 2200 * @param __i1 Iterator referencing start of range to replace. 2201 * @param __i2 Iterator referencing end of range to replace. 2202 * @param __l The initializer_list of characters to insert. 2203 * @return Reference to this string. 2204 * @throw std::length_error If new length exceeds @c max_size(). 2205 * 2206 * Removes the characters in the range [__i1,__i2). In place, 2207 * characters in the range [__k1,__k2) are inserted. If the 2208 * length of result exceeds max_size(), length_error is thrown. 2209 * The value of the string doesn't change if an error is 2210 * thrown. 2211 */ 2212 basic_string& replace(iterator __i1, iterator __i2, 2213 initializer_list<_CharT> __l) 2214 { return this->replace(__i1, __i2, __l.begin(), __l.end()); } 2215 #endif // C++11 2216 2217 #ifdef __glibcxx_string_view // >= C++17 2218 /** 2219 * @brief Replace range of characters with string_view. 2220 * @param __pos The position to replace at. 2221 * @param __n The number of characters to replace. 2222 * @param __svt The object convertible to string_view to insert. 2223 * @return Reference to this string. 2224 */ 2225 template<typename _Tp> 2226 _If_sv<_Tp, basic_string&> 2227 replace(size_type __pos, size_type __n, const _Tp& __svt) 2228 { 2229 __sv_type __sv = __svt; 2230 return this->replace(__pos, __n, __sv.data(), __sv.size()); 2231 } 2232 2233 /** 2234 * @brief Replace range of characters with string_view. 2235 * @param __pos1 The position to replace at. 2236 * @param __n1 The number of characters to replace. 2237 * @param __svt The object convertible to string_view to insert from. 2238 * @param __pos2 The position in the string_view to insert from. 2239 * @param __n2 The number of characters to insert. 2240 * @return Reference to this string. 2241 */ 2242 template<typename _Tp> 2243 _If_sv<_Tp, basic_string&> 2244 replace(size_type __pos1, size_type __n1, const _Tp& __svt, 2245 size_type __pos2, size_type __n2 = npos) 2246 { 2247 __sv_type __sv = __svt; 2248 return this->replace(__pos1, __n1, 2249 __sv.data() 2250 + std::__sv_check(__sv.size(), __pos2, "basic_string::replace"), 2251 std::__sv_limit(__sv.size(), __pos2, __n2)); 2252 } 2253 2254 /** 2255 * @brief Replace range of characters with string_view. 2256 * @param __i1 An iterator referencing the start position 2257 * to replace at. 2258 * @param __i2 An iterator referencing the end position 2259 * for the replace. 2260 * @param __svt The object convertible to string_view to insert from. 2261 * @return Reference to this string. 2262 */ 2263 template<typename _Tp> 2264 _If_sv<_Tp, basic_string&> 2265 replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt) 2266 { 2267 __sv_type __sv = __svt; 2268 return this->replace(__i1 - begin(), __i2 - __i1, __sv); 2269 } 2270 #endif // C++17 2271 2272 private: 2273 template<class _Integer> 2274 basic_string& 2275 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n, 2276 _Integer __val, __true_type) 2277 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); } 2278 2279 template<class _InputIterator> 2280 basic_string& 2281 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1, 2282 _InputIterator __k2, __false_type); 2283 2284 basic_string& 2285 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2, 2286 _CharT __c); 2287 2288 basic_string& 2289 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s, 2290 size_type __n2); 2291 2292 // _S_construct_aux is used to implement the 21.3.1 para 15 which 2293 // requires special behaviour if _InIter is an integral type 2294 template<class _InIterator> 2295 static _CharT* 2296 _S_construct_aux(_InIterator __beg, _InIterator __end, 2297 const _Alloc& __a, __false_type) 2298 { 2299 typedef typename iterator_traits<_InIterator>::iterator_category _Tag; 2300 return _S_construct(__beg, __end, __a, _Tag()); 2301 } 2302 2303 // _GLIBCXX_RESOLVE_LIB_DEFECTS 2304 // 438. Ambiguity in the "do the right thing" clause 2305 template<class _Integer> 2306 static _CharT* 2307 _S_construct_aux(_Integer __beg, _Integer __end, 2308 const _Alloc& __a, __true_type) 2309 { return _S_construct_aux_2(static_cast<size_type>(__beg), 2310 __end, __a); } 2311 2312 static _CharT* 2313 _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a) 2314 { return _S_construct(__req, __c, __a); } 2315 2316 template<class _InIterator> 2317 static _CharT* 2318 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a) 2319 { 2320 typedef typename std::__is_integer<_InIterator>::__type _Integral; 2321 return _S_construct_aux(__beg, __end, __a, _Integral()); 2322 } 2323 2324 // For Input Iterators, used in istreambuf_iterators, etc. 2325 template<class _InIterator> 2326 static _CharT* 2327 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a, 2328 input_iterator_tag); 2329 2330 // For forward_iterators up to random_access_iterators, used for 2331 // string::iterator, _CharT*, etc. 2332 template<class _FwdIterator> 2333 static _CharT* 2334 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a, 2335 forward_iterator_tag); 2336 2337 static _CharT* 2338 _S_construct(size_type __req, _CharT __c, const _Alloc& __a); 2339 2340 public: 2341 2342 /** 2343 * @brief Copy substring into C string. 2344 * @param __s C string to copy value into. 2345 * @param __n Number of characters to copy. 2346 * @param __pos Index of first character to copy. 2347 * @return Number of characters actually copied 2348 * @throw std::out_of_range If __pos > size(). 2349 * 2350 * Copies up to @a __n characters starting at @a __pos into the 2351 * C string @a __s. If @a __pos is %greater than size(), 2352 * out_of_range is thrown. 2353 */ 2354 size_type 2355 copy(_CharT* __s, size_type __n, size_type __pos = 0) const; 2356 2357 /** 2358 * @brief Swap contents with another string. 2359 * @param __s String to swap with. 2360 * 2361 * Exchanges the contents of this string with that of @a __s in constant 2362 * time. 2363 */ 2364 void 2365 swap(basic_string& __s) 2366 _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value); 2367 2368 // String operations: 2369 /** 2370 * @brief Return const pointer to null-terminated contents. 2371 * 2372 * This is a handle to internal data. Do not modify or dire things may 2373 * happen. 2374 */ 2375 const _CharT* 2376 c_str() const _GLIBCXX_NOEXCEPT 2377 { return _M_data(); } 2378 2379 /** 2380 * @brief Return const pointer to contents. 2381 * 2382 * This is a pointer to internal data. It is undefined to modify 2383 * the contents through the returned pointer. To get a pointer that 2384 * allows modifying the contents use @c &str[0] instead, 2385 * (or in C++17 the non-const @c str.data() overload). 2386 */ 2387 const _CharT* 2388 data() const _GLIBCXX_NOEXCEPT 2389 { return _M_data(); } 2390 2391 #if __cplusplus >= 201703L 2392 /** 2393 * @brief Return non-const pointer to contents. 2394 * 2395 * This is a pointer to the character sequence held by the string. 2396 * Modifying the characters in the sequence is allowed. 2397 * 2398 * The standard requires this function to be `noexcept` but for the 2399 * Copy-On-Write string implementation it can throw. This function 2400 * allows modifying the string contents directly, which means we 2401 * must copy-on-write to unshare it, which requires allocating memory. 2402 */ 2403 _CharT* 2404 data() noexcept(false) 2405 { 2406 _M_leak(); 2407 return _M_data(); 2408 } 2409 #endif 2410 2411 /** 2412 * @brief Return copy of allocator used to construct this string. 2413 */ 2414 allocator_type 2415 get_allocator() const _GLIBCXX_NOEXCEPT 2416 { return _M_dataplus; } 2417 2418 /** 2419 * @brief Find position of a C substring. 2420 * @param __s C string to locate. 2421 * @param __pos Index of character to search from. 2422 * @param __n Number of characters from @a s to search for. 2423 * @return Index of start of first occurrence. 2424 * 2425 * Starting from @a __pos, searches forward for the first @a 2426 * __n characters in @a __s within this string. If found, 2427 * returns the index where it begins. If not found, returns 2428 * npos. 2429 */ 2430 size_type 2431 find(const _CharT* __s, size_type __pos, size_type __n) const 2432 _GLIBCXX_NOEXCEPT; 2433 2434 /** 2435 * @brief Find position of a string. 2436 * @param __str String to locate. 2437 * @param __pos Index of character to search from (default 0). 2438 * @return Index of start of first occurrence. 2439 * 2440 * Starting from @a __pos, searches forward for value of @a __str within 2441 * this string. If found, returns the index where it begins. If not 2442 * found, returns npos. 2443 */ 2444 size_type 2445 find(const basic_string& __str, size_type __pos = 0) const 2446 _GLIBCXX_NOEXCEPT 2447 { return this->find(__str.data(), __pos, __str.size()); } 2448 2449 /** 2450 * @brief Find position of a C string. 2451 * @param __s C string to locate. 2452 * @param __pos Index of character to search from (default 0). 2453 * @return Index of start of first occurrence. 2454 * 2455 * Starting from @a __pos, searches forward for the value of @a 2456 * __s within this string. If found, returns the index where 2457 * it begins. If not found, returns npos. 2458 */ 2459 size_type 2460 find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT 2461 { 2462 __glibcxx_requires_string(__s); 2463 return this->find(__s, __pos, traits_type::length(__s)); 2464 } 2465 2466 /** 2467 * @brief Find position of a character. 2468 * @param __c Character to locate. 2469 * @param __pos Index of character to search from (default 0). 2470 * @return Index of first occurrence. 2471 * 2472 * Starting from @a __pos, searches forward for @a __c within 2473 * this string. If found, returns the index where it was 2474 * found. If not found, returns npos. 2475 */ 2476 size_type 2477 find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT; 2478 2479 #ifdef __glibcxx_string_view // >= C++17 2480 /** 2481 * @brief Find position of a string_view. 2482 * @param __svt The object convertible to string_view to locate. 2483 * @param __pos Index of character to search from (default 0). 2484 * @return Index of start of first occurrence. 2485 */ 2486 template<typename _Tp> 2487 _If_sv<_Tp, size_type> 2488 find(const _Tp& __svt, size_type __pos = 0) const 2489 noexcept(is_same<_Tp, __sv_type>::value) 2490 { 2491 __sv_type __sv = __svt; 2492 return this->find(__sv.data(), __pos, __sv.size()); 2493 } 2494 #endif // C++17 2495 2496 /** 2497 * @brief Find last position of a string. 2498 * @param __str String to locate. 2499 * @param __pos Index of character to search back from (default end). 2500 * @return Index of start of last occurrence. 2501 * 2502 * Starting from @a __pos, searches backward for value of @a 2503 * __str within this string. If found, returns the index where 2504 * it begins. If not found, returns npos. 2505 */ 2506 size_type 2507 rfind(const basic_string& __str, size_type __pos = npos) const 2508 _GLIBCXX_NOEXCEPT 2509 { return this->rfind(__str.data(), __pos, __str.size()); } 2510 2511 /** 2512 * @brief Find last position of a C substring. 2513 * @param __s C string to locate. 2514 * @param __pos Index of character to search back from. 2515 * @param __n Number of characters from s to search for. 2516 * @return Index of start of last occurrence. 2517 * 2518 * Starting from @a __pos, searches backward for the first @a 2519 * __n characters in @a __s within this string. If found, 2520 * returns the index where it begins. If not found, returns 2521 * npos. 2522 */ 2523 size_type 2524 rfind(const _CharT* __s, size_type __pos, size_type __n) const 2525 _GLIBCXX_NOEXCEPT; 2526 2527 /** 2528 * @brief Find last position of a C string. 2529 * @param __s C string to locate. 2530 * @param __pos Index of character to start search at (default end). 2531 * @return Index of start of last occurrence. 2532 * 2533 * Starting from @a __pos, searches backward for the value of 2534 * @a __s within this string. If found, returns the index 2535 * where it begins. If not found, returns npos. 2536 */ 2537 size_type 2538 rfind(const _CharT* __s, size_type __pos = npos) const _GLIBCXX_NOEXCEPT 2539 { 2540 __glibcxx_requires_string(__s); 2541 return this->rfind(__s, __pos, traits_type::length(__s)); 2542 } 2543 2544 /** 2545 * @brief Find last position of a character. 2546 * @param __c Character to locate. 2547 * @param __pos Index of character to search back from (default end). 2548 * @return Index of last occurrence. 2549 * 2550 * Starting from @a __pos, searches backward for @a __c within 2551 * this string. If found, returns the index where it was 2552 * found. If not found, returns npos. 2553 */ 2554 size_type 2555 rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT; 2556 2557 #ifdef __glibcxx_string_view // >= C++17 2558 /** 2559 * @brief Find last position of a string_view. 2560 * @param __svt The object convertible to string_view to locate. 2561 * @param __pos Index of character to search back from (default end). 2562 * @return Index of start of last occurrence. 2563 */ 2564 template<typename _Tp> 2565 _If_sv<_Tp, size_type> 2566 rfind(const _Tp& __svt, size_type __pos = npos) const 2567 noexcept(is_same<_Tp, __sv_type>::value) 2568 { 2569 __sv_type __sv = __svt; 2570 return this->rfind(__sv.data(), __pos, __sv.size()); 2571 } 2572 #endif // C++17 2573 2574 /** 2575 * @brief Find position of a character of string. 2576 * @param __str String containing characters to locate. 2577 * @param __pos Index of character to search from (default 0). 2578 * @return Index of first occurrence. 2579 * 2580 * Starting from @a __pos, searches forward for one of the 2581 * characters of @a __str within this string. If found, 2582 * returns the index where it was found. If not found, returns 2583 * npos. 2584 */ 2585 size_type 2586 find_first_of(const basic_string& __str, size_type __pos = 0) const 2587 _GLIBCXX_NOEXCEPT 2588 { return this->find_first_of(__str.data(), __pos, __str.size()); } 2589 2590 /** 2591 * @brief Find position of a character of C substring. 2592 * @param __s String containing characters to locate. 2593 * @param __pos Index of character to search from. 2594 * @param __n Number of characters from s to search for. 2595 * @return Index of first occurrence. 2596 * 2597 * Starting from @a __pos, searches forward for one of the 2598 * first @a __n characters of @a __s within this string. If 2599 * found, returns the index where it was found. If not found, 2600 * returns npos. 2601 */ 2602 size_type 2603 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const 2604 _GLIBCXX_NOEXCEPT; 2605 2606 /** 2607 * @brief Find position of a character of C string. 2608 * @param __s String containing characters to locate. 2609 * @param __pos Index of character to search from (default 0). 2610 * @return Index of first occurrence. 2611 * 2612 * Starting from @a __pos, searches forward for one of the 2613 * characters of @a __s within this string. If found, returns 2614 * the index where it was found. If not found, returns npos. 2615 */ 2616 size_type 2617 find_first_of(const _CharT* __s, size_type __pos = 0) const 2618 _GLIBCXX_NOEXCEPT 2619 { 2620 __glibcxx_requires_string(__s); 2621 return this->find_first_of(__s, __pos, traits_type::length(__s)); 2622 } 2623 2624 /** 2625 * @brief Find position of a character. 2626 * @param __c Character to locate. 2627 * @param __pos Index of character to search from (default 0). 2628 * @return Index of first occurrence. 2629 * 2630 * Starting from @a __pos, searches forward for the character 2631 * @a __c within this string. If found, returns the index 2632 * where it was found. If not found, returns npos. 2633 * 2634 * Note: equivalent to find(__c, __pos). 2635 */ 2636 size_type 2637 find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT 2638 { return this->find(__c, __pos); } 2639 2640 #ifdef __glibcxx_string_view // >= C++17 2641 /** 2642 * @brief Find position of a character of a string_view. 2643 * @param __svt An object convertible to string_view containing 2644 * characters to locate. 2645 * @param __pos Index of character to search from (default 0). 2646 * @return Index of first occurrence. 2647 */ 2648 template<typename _Tp> 2649 _If_sv<_Tp, size_type> 2650 find_first_of(const _Tp& __svt, size_type __pos = 0) const 2651 noexcept(is_same<_Tp, __sv_type>::value) 2652 { 2653 __sv_type __sv = __svt; 2654 return this->find_first_of(__sv.data(), __pos, __sv.size()); 2655 } 2656 #endif // C++17 2657 2658 /** 2659 * @brief Find last position of a character of string. 2660 * @param __str String containing characters to locate. 2661 * @param __pos Index of character to search back from (default end). 2662 * @return Index of last occurrence. 2663 * 2664 * Starting from @a __pos, searches backward for one of the 2665 * characters of @a __str within this string. If found, 2666 * returns the index where it was found. If not found, returns 2667 * npos. 2668 */ 2669 size_type 2670 find_last_of(const basic_string& __str, size_type __pos = npos) const 2671 _GLIBCXX_NOEXCEPT 2672 { return this->find_last_of(__str.data(), __pos, __str.size()); } 2673 2674 /** 2675 * @brief Find last position of a character of C substring. 2676 * @param __s C string containing characters to locate. 2677 * @param __pos Index of character to search back from. 2678 * @param __n Number of characters from s to search for. 2679 * @return Index of last occurrence. 2680 * 2681 * Starting from @a __pos, searches backward for one of the 2682 * first @a __n characters of @a __s within this string. If 2683 * found, returns the index where it was found. If not found, 2684 * returns npos. 2685 */ 2686 size_type 2687 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const 2688 _GLIBCXX_NOEXCEPT; 2689 2690 /** 2691 * @brief Find last position of a character of C string. 2692 * @param __s C string containing characters to locate. 2693 * @param __pos Index of character to search back from (default end). 2694 * @return Index of last occurrence. 2695 * 2696 * Starting from @a __pos, searches backward for one of the 2697 * characters of @a __s within this string. If found, returns 2698 * the index where it was found. If not found, returns npos. 2699 */ 2700 size_type 2701 find_last_of(const _CharT* __s, size_type __pos = npos) const 2702 _GLIBCXX_NOEXCEPT 2703 { 2704 __glibcxx_requires_string(__s); 2705 return this->find_last_of(__s, __pos, traits_type::length(__s)); 2706 } 2707 2708 /** 2709 * @brief Find last position of a character. 2710 * @param __c Character to locate. 2711 * @param __pos Index of character to search back from (default end). 2712 * @return Index of last occurrence. 2713 * 2714 * Starting from @a __pos, searches backward for @a __c within 2715 * this string. If found, returns the index where it was 2716 * found. If not found, returns npos. 2717 * 2718 * Note: equivalent to rfind(__c, __pos). 2719 */ 2720 size_type 2721 find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT 2722 { return this->rfind(__c, __pos); } 2723 2724 #ifdef __glibcxx_string_view // >= C++17 2725 /** 2726 * @brief Find last position of a character of string. 2727 * @param __svt An object convertible to string_view containing 2728 * characters to locate. 2729 * @param __pos Index of character to search back from (default end). 2730 * @return Index of last occurrence. 2731 */ 2732 template<typename _Tp> 2733 _If_sv<_Tp, size_type> 2734 find_last_of(const _Tp& __svt, size_type __pos = npos) const 2735 noexcept(is_same<_Tp, __sv_type>::value) 2736 { 2737 __sv_type __sv = __svt; 2738 return this->find_last_of(__sv.data(), __pos, __sv.size()); 2739 } 2740 #endif // C++17 2741 2742 /** 2743 * @brief Find position of a character not in string. 2744 * @param __str String containing characters to avoid. 2745 * @param __pos Index of character to search from (default 0). 2746 * @return Index of first occurrence. 2747 * 2748 * Starting from @a __pos, searches forward for a character not contained 2749 * in @a __str within this string. If found, returns the index where it 2750 * was found. If not found, returns npos. 2751 */ 2752 size_type 2753 find_first_not_of(const basic_string& __str, size_type __pos = 0) const 2754 _GLIBCXX_NOEXCEPT 2755 { return this->find_first_not_of(__str.data(), __pos, __str.size()); } 2756 2757 /** 2758 * @brief Find position of a character not in C substring. 2759 * @param __s C string containing characters to avoid. 2760 * @param __pos Index of character to search from. 2761 * @param __n Number of characters from __s to consider. 2762 * @return Index of first occurrence. 2763 * 2764 * Starting from @a __pos, searches forward for a character not 2765 * contained in the first @a __n characters of @a __s within 2766 * this string. If found, returns the index where it was 2767 * found. If not found, returns npos. 2768 */ 2769 size_type 2770 find_first_not_of(const _CharT* __s, size_type __pos, 2771 size_type __n) const _GLIBCXX_NOEXCEPT; 2772 2773 /** 2774 * @brief Find position of a character not in C string. 2775 * @param __s C string containing characters to avoid. 2776 * @param __pos Index of character to search from (default 0). 2777 * @return Index of first occurrence. 2778 * 2779 * Starting from @a __pos, searches forward for a character not 2780 * contained in @a __s within this string. If found, returns 2781 * the index where it was found. If not found, returns npos. 2782 */ 2783 size_type 2784 find_first_not_of(const _CharT* __s, size_type __pos = 0) const 2785 _GLIBCXX_NOEXCEPT 2786 { 2787 __glibcxx_requires_string(__s); 2788 return this->find_first_not_of(__s, __pos, traits_type::length(__s)); 2789 } 2790 2791 /** 2792 * @brief Find position of a different character. 2793 * @param __c Character to avoid. 2794 * @param __pos Index of character to search from (default 0). 2795 * @return Index of first occurrence. 2796 * 2797 * Starting from @a __pos, searches forward for a character 2798 * other than @a __c within this string. If found, returns the 2799 * index where it was found. If not found, returns npos. 2800 */ 2801 size_type 2802 find_first_not_of(_CharT __c, size_type __pos = 0) const 2803 _GLIBCXX_NOEXCEPT; 2804 2805 #ifdef __glibcxx_string_view // >= C++17 2806 /** 2807 * @brief Find position of a character not in a string_view. 2808 * @param __svt An object convertible to string_view containing 2809 * characters to avoid. 2810 * @param __pos Index of character to search from (default 0). 2811 * @return Index of first occurrence. 2812 */ 2813 template<typename _Tp> 2814 _If_sv<_Tp, size_type> 2815 find_first_not_of(const _Tp& __svt, size_type __pos = 0) const 2816 noexcept(is_same<_Tp, __sv_type>::value) 2817 { 2818 __sv_type __sv = __svt; 2819 return this->find_first_not_of(__sv.data(), __pos, __sv.size()); 2820 } 2821 #endif // C++17 2822 2823 /** 2824 * @brief Find last position of a character not in string. 2825 * @param __str String containing characters to avoid. 2826 * @param __pos Index of character to search back from (default end). 2827 * @return Index of last occurrence. 2828 * 2829 * Starting from @a __pos, searches backward for a character 2830 * not contained in @a __str within this string. If found, 2831 * returns the index where it was found. If not found, returns 2832 * npos. 2833 */ 2834 size_type 2835 find_last_not_of(const basic_string& __str, size_type __pos = npos) const 2836 _GLIBCXX_NOEXCEPT 2837 { return this->find_last_not_of(__str.data(), __pos, __str.size()); } 2838 2839 /** 2840 * @brief Find last position of a character not in C substring. 2841 * @param __s C string containing characters to avoid. 2842 * @param __pos Index of character to search back from. 2843 * @param __n Number of characters from s to consider. 2844 * @return Index of last occurrence. 2845 * 2846 * Starting from @a __pos, searches backward for a character not 2847 * contained in the first @a __n characters of @a __s within this string. 2848 * If found, returns the index where it was found. If not found, 2849 * returns npos. 2850 */ 2851 size_type 2852 find_last_not_of(const _CharT* __s, size_type __pos, 2853 size_type __n) const _GLIBCXX_NOEXCEPT; 2854 /** 2855 * @brief Find last position of a character not in C string. 2856 * @param __s C string containing characters to avoid. 2857 * @param __pos Index of character to search back from (default end). 2858 * @return Index of last occurrence. 2859 * 2860 * Starting from @a __pos, searches backward for a character 2861 * not contained in @a __s within this string. If found, 2862 * returns the index where it was found. If not found, returns 2863 * npos. 2864 */ 2865 size_type 2866 find_last_not_of(const _CharT* __s, size_type __pos = npos) const 2867 _GLIBCXX_NOEXCEPT 2868 { 2869 __glibcxx_requires_string(__s); 2870 return this->find_last_not_of(__s, __pos, traits_type::length(__s)); 2871 } 2872 2873 /** 2874 * @brief Find last position of a different character. 2875 * @param __c Character to avoid. 2876 * @param __pos Index of character to search back from (default end). 2877 * @return Index of last occurrence. 2878 * 2879 * Starting from @a __pos, searches backward for a character other than 2880 * @a __c within this string. If found, returns the index where it was 2881 * found. If not found, returns npos. 2882 */ 2883 size_type 2884 find_last_not_of(_CharT __c, size_type __pos = npos) const 2885 _GLIBCXX_NOEXCEPT; 2886 2887 #ifdef __glibcxx_string_view // >= C++17 2888 /** 2889 * @brief Find last position of a character not in a string_view. 2890 * @param __svt An object convertible to string_view containing 2891 * characters to avoid. 2892 * @param __pos Index of character to search back from (default end). 2893 * @return Index of last occurrence. 2894 */ 2895 template<typename _Tp> 2896 _If_sv<_Tp, size_type> 2897 find_last_not_of(const _Tp& __svt, size_type __pos = npos) const 2898 noexcept(is_same<_Tp, __sv_type>::value) 2899 { 2900 __sv_type __sv = __svt; 2901 return this->find_last_not_of(__sv.data(), __pos, __sv.size()); 2902 } 2903 #endif // C++17 2904 2905 /** 2906 * @brief Get a substring. 2907 * @param __pos Index of first character (default 0). 2908 * @param __n Number of characters in substring (default remainder). 2909 * @return The new string. 2910 * @throw std::out_of_range If __pos > size(). 2911 * 2912 * Construct and return a new string using the @a __n 2913 * characters starting at @a __pos. If the string is too 2914 * short, use the remainder of the characters. If @a __pos is 2915 * beyond the end of the string, out_of_range is thrown. 2916 */ 2917 basic_string 2918 substr(size_type __pos = 0, size_type __n = npos) const 2919 { return basic_string(*this, 2920 _M_check(__pos, "basic_string::substr"), __n); } 2921 2922 /** 2923 * @brief Compare to a string. 2924 * @param __str String to compare against. 2925 * @return Integer < 0, 0, or > 0. 2926 * 2927 * Returns an integer < 0 if this string is ordered before @a 2928 * __str, 0 if their values are equivalent, or > 0 if this 2929 * string is ordered after @a __str. Determines the effective 2930 * length rlen of the strings to compare as the smallest of 2931 * size() and str.size(). The function then compares the two 2932 * strings by calling traits::compare(data(), str.data(),rlen). 2933 * If the result of the comparison is nonzero returns it, 2934 * otherwise the shorter one is ordered first. 2935 */ 2936 int 2937 compare(const basic_string& __str) const 2938 { 2939 const size_type __size = this->size(); 2940 const size_type __osize = __str.size(); 2941 const size_type __len = std::min(__size, __osize); 2942 2943 int __r = traits_type::compare(_M_data(), __str.data(), __len); 2944 if (!__r) 2945 __r = _S_compare(__size, __osize); 2946 return __r; 2947 } 2948 2949 #ifdef __glibcxx_string_view // >= C++17 2950 /** 2951 * @brief Compare to a string_view. 2952 * @param __svt An object convertible to string_view to compare against. 2953 * @return Integer < 0, 0, or > 0. 2954 */ 2955 template<typename _Tp> 2956 _If_sv<_Tp, int> 2957 compare(const _Tp& __svt) const 2958 noexcept(is_same<_Tp, __sv_type>::value) 2959 { 2960 __sv_type __sv = __svt; 2961 const size_type __size = this->size(); 2962 const size_type __osize = __sv.size(); 2963 const size_type __len = std::min(__size, __osize); 2964 2965 int __r = traits_type::compare(_M_data(), __sv.data(), __len); 2966 if (!__r) 2967 __r = _S_compare(__size, __osize); 2968 return __r; 2969 } 2970 2971 /** 2972 * @brief Compare to a string_view. 2973 * @param __pos A position in the string to start comparing from. 2974 * @param __n The number of characters to compare. 2975 * @param __svt An object convertible to string_view to compare 2976 * against. 2977 * @return Integer < 0, 0, or > 0. 2978 */ 2979 template<typename _Tp> 2980 _If_sv<_Tp, int> 2981 compare(size_type __pos, size_type __n, const _Tp& __svt) const 2982 { 2983 __sv_type __sv = __svt; 2984 return __sv_type(*this).substr(__pos, __n).compare(__sv); 2985 } 2986 2987 /** 2988 * @brief Compare to a string_view. 2989 * @param __pos1 A position in the string to start comparing from. 2990 * @param __n1 The number of characters to compare. 2991 * @param __svt An object convertible to string_view to compare 2992 * against. 2993 * @param __pos2 A position in the string_view to start comparing from. 2994 * @param __n2 The number of characters to compare. 2995 * @return Integer < 0, 0, or > 0. 2996 */ 2997 template<typename _Tp> 2998 _If_sv<_Tp, int> 2999 compare(size_type __pos1, size_type __n1, const _Tp& __svt, 3000 size_type __pos2, size_type __n2 = npos) const 3001 { 3002 __sv_type __sv = __svt; 3003 return __sv_type(*this) 3004 .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2)); 3005 } 3006 #endif // C++17 3007 3008 /** 3009 * @brief Compare substring to a string. 3010 * @param __pos Index of first character of substring. 3011 * @param __n Number of characters in substring. 3012 * @param __str String to compare against. 3013 * @return Integer < 0, 0, or > 0. 3014 * 3015 * Form the substring of this string from the @a __n characters 3016 * starting at @a __pos. Returns an integer < 0 if the 3017 * substring is ordered before @a __str, 0 if their values are 3018 * equivalent, or > 0 if the substring is ordered after @a 3019 * __str. Determines the effective length rlen of the strings 3020 * to compare as the smallest of the length of the substring 3021 * and @a __str.size(). The function then compares the two 3022 * strings by calling 3023 * traits::compare(substring.data(),str.data(),rlen). If the 3024 * result of the comparison is nonzero returns it, otherwise 3025 * the shorter one is ordered first. 3026 */ 3027 int 3028 compare(size_type __pos, size_type __n, const basic_string& __str) const 3029 { 3030 _M_check(__pos, "basic_string::compare"); 3031 __n = _M_limit(__pos, __n); 3032 const size_type __osize = __str.size(); 3033 const size_type __len = std::min(__n, __osize); 3034 int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len); 3035 if (!__r) 3036 __r = _S_compare(__n, __osize); 3037 return __r; 3038 } 3039 3040 /** 3041 * @brief Compare substring to a substring. 3042 * @param __pos1 Index of first character of substring. 3043 * @param __n1 Number of characters in substring. 3044 * @param __str String to compare against. 3045 * @param __pos2 Index of first character of substring of str. 3046 * @param __n2 Number of characters in substring of str. 3047 * @return Integer < 0, 0, or > 0. 3048 * 3049 * Form the substring of this string from the @a __n1 3050 * characters starting at @a __pos1. Form the substring of @a 3051 * __str from the @a __n2 characters starting at @a __pos2. 3052 * Returns an integer < 0 if this substring is ordered before 3053 * the substring of @a __str, 0 if their values are equivalent, 3054 * or > 0 if this substring is ordered after the substring of 3055 * @a __str. Determines the effective length rlen of the 3056 * strings to compare as the smallest of the lengths of the 3057 * substrings. The function then compares the two strings by 3058 * calling 3059 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen). 3060 * If the result of the comparison is nonzero returns it, 3061 * otherwise the shorter one is ordered first. 3062 */ 3063 int 3064 compare(size_type __pos1, size_type __n1, const basic_string& __str, 3065 size_type __pos2, size_type __n2 = npos) const 3066 { 3067 _M_check(__pos1, "basic_string::compare"); 3068 __str._M_check(__pos2, "basic_string::compare"); 3069 __n1 = _M_limit(__pos1, __n1); 3070 __n2 = __str._M_limit(__pos2, __n2); 3071 const size_type __len = std::min(__n1, __n2); 3072 int __r = traits_type::compare(_M_data() + __pos1, 3073 __str.data() + __pos2, __len); 3074 if (!__r) 3075 __r = _S_compare(__n1, __n2); 3076 return __r; 3077 } 3078 3079 /** 3080 * @brief Compare to a C string. 3081 * @param __s C string to compare against. 3082 * @return Integer < 0, 0, or > 0. 3083 * 3084 * Returns an integer < 0 if this string is ordered before @a __s, 0 if 3085 * their values are equivalent, or > 0 if this string is ordered after 3086 * @a __s. Determines the effective length rlen of the strings to 3087 * compare as the smallest of size() and the length of a string 3088 * constructed from @a __s. The function then compares the two strings 3089 * by calling traits::compare(data(),s,rlen). If the result of the 3090 * comparison is nonzero returns it, otherwise the shorter one is 3091 * ordered first. 3092 */ 3093 int 3094 compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT 3095 { 3096 __glibcxx_requires_string(__s); 3097 const size_type __size = this->size(); 3098 const size_type __osize = traits_type::length(__s); 3099 const size_type __len = std::min(__size, __osize); 3100 int __r = traits_type::compare(_M_data(), __s, __len); 3101 if (!__r) 3102 __r = _S_compare(__size, __osize); 3103 return __r; 3104 } 3105 3106 // _GLIBCXX_RESOLVE_LIB_DEFECTS 3107 // 5 String::compare specification questionable 3108 /** 3109 * @brief Compare substring to a C string. 3110 * @param __pos Index of first character of substring. 3111 * @param __n1 Number of characters in substring. 3112 * @param __s C string to compare against. 3113 * @return Integer < 0, 0, or > 0. 3114 * 3115 * Form the substring of this string from the @a __n1 3116 * characters starting at @a pos. Returns an integer < 0 if 3117 * the substring is ordered before @a __s, 0 if their values 3118 * are equivalent, or > 0 if the substring is ordered after @a 3119 * __s. Determines the effective length rlen of the strings to 3120 * compare as the smallest of the length of the substring and 3121 * the length of a string constructed from @a __s. The 3122 * function then compares the two string by calling 3123 * traits::compare(substring.data(),__s,rlen). If the result of 3124 * the comparison is nonzero returns it, otherwise the shorter 3125 * one is ordered first. 3126 */ 3127 int 3128 compare(size_type __pos, size_type __n1, const _CharT* __s) const 3129 { 3130 __glibcxx_requires_string(__s); 3131 _M_check(__pos, "basic_string::compare"); 3132 __n1 = _M_limit(__pos, __n1); 3133 const size_type __osize = traits_type::length(__s); 3134 const size_type __len = std::min(__n1, __osize); 3135 int __r = traits_type::compare(_M_data() + __pos, __s, __len); 3136 if (!__r) 3137 __r = _S_compare(__n1, __osize); 3138 return __r; 3139 } 3140 3141 /** 3142 * @brief Compare substring against a character %array. 3143 * @param __pos Index of first character of substring. 3144 * @param __n1 Number of characters in substring. 3145 * @param __s character %array to compare against. 3146 * @param __n2 Number of characters of s. 3147 * @return Integer < 0, 0, or > 0. 3148 * 3149 * Form the substring of this string from the @a __n1 3150 * characters starting at @a __pos. Form a string from the 3151 * first @a __n2 characters of @a __s. Returns an integer < 0 3152 * if this substring is ordered before the string from @a __s, 3153 * 0 if their values are equivalent, or > 0 if this substring 3154 * is ordered after the string from @a __s. Determines the 3155 * effective length rlen of the strings to compare as the 3156 * smallest of the length of the substring and @a __n2. The 3157 * function then compares the two strings by calling 3158 * traits::compare(substring.data(),s,rlen). If the result of 3159 * the comparison is nonzero returns it, otherwise the shorter 3160 * one is ordered first. 3161 * 3162 * NB: s must have at least n2 characters, '\\0' has 3163 * no special meaning. 3164 */ 3165 int 3166 compare(size_type __pos, size_type __n1, const _CharT* __s, 3167 size_type __n2) const 3168 { 3169 __glibcxx_requires_string_len(__s, __n2); 3170 _M_check(__pos, "basic_string::compare"); 3171 __n1 = _M_limit(__pos, __n1); 3172 const size_type __len = std::min(__n1, __n2); 3173 int __r = traits_type::compare(_M_data() + __pos, __s, __len); 3174 if (!__r) 3175 __r = _S_compare(__n1, __n2); 3176 return __r; 3177 } 3178 3179 #if __cplusplus > 201703L 3180 bool 3181 starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept 3182 { return __sv_type(this->data(), this->size()).starts_with(__x); } 3183 3184 bool 3185 starts_with(_CharT __x) const noexcept 3186 { return __sv_type(this->data(), this->size()).starts_with(__x); } 3187 3188 [[__gnu__::__nonnull__]] 3189 bool 3190 starts_with(const _CharT* __x) const noexcept 3191 { return __sv_type(this->data(), this->size()).starts_with(__x); } 3192 3193 bool 3194 ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept 3195 { return __sv_type(this->data(), this->size()).ends_with(__x); } 3196 3197 bool 3198 ends_with(_CharT __x) const noexcept 3199 { return __sv_type(this->data(), this->size()).ends_with(__x); } 3200 3201 [[__gnu__::__nonnull__]] 3202 bool 3203 ends_with(const _CharT* __x) const noexcept 3204 { return __sv_type(this->data(), this->size()).ends_with(__x); } 3205 #endif // C++20 3206 3207 #if __cplusplus > 202011L 3208 bool 3209 contains(basic_string_view<_CharT, _Traits> __x) const noexcept 3210 { return __sv_type(this->data(), this->size()).contains(__x); } 3211 3212 bool 3213 contains(_CharT __x) const noexcept 3214 { return __sv_type(this->data(), this->size()).contains(__x); } 3215 3216 [[__gnu__::__nonnull__]] 3217 bool 3218 contains(const _CharT* __x) const noexcept 3219 { return __sv_type(this->data(), this->size()).contains(__x); } 3220 #endif // C++23 3221 3222 # ifdef _GLIBCXX_TM_TS_INTERNAL 3223 friend void 3224 ::_txnal_cow_string_C1_for_exceptions(void* that, const char* s, 3225 void* exc); 3226 friend const char* 3227 ::_txnal_cow_string_c_str(const void *that); 3228 friend void 3229 ::_txnal_cow_string_D1(void *that); 3230 friend void 3231 ::_txnal_cow_string_D1_commit(void *that); 3232 # endif 3233 }; 3234 3235 template<typename _CharT, typename _Traits, typename _Alloc> 3236 const typename basic_string<_CharT, _Traits, _Alloc>::size_type 3237 basic_string<_CharT, _Traits, _Alloc>:: 3238 _Rep::_S_max_size = (((npos - sizeof(_Rep_base))/sizeof(_CharT)) - 1) / 4; 3239 3240 template<typename _CharT, typename _Traits, typename _Alloc> 3241 const _CharT 3242 basic_string<_CharT, _Traits, _Alloc>:: 3243 _Rep::_S_terminal = _CharT(); 3244 3245 template<typename _CharT, typename _Traits, typename _Alloc> 3246 const typename basic_string<_CharT, _Traits, _Alloc>::size_type 3247 basic_string<_CharT, _Traits, _Alloc>::npos; 3248 3249 // Linker sets _S_empty_rep_storage to all 0s (one reference, empty string) 3250 // at static init time (before static ctors are run). 3251 template<typename _CharT, typename _Traits, typename _Alloc> 3252 typename basic_string<_CharT, _Traits, _Alloc>::size_type 3253 basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_empty_rep_storage[ 3254 (sizeof(_Rep_base) + sizeof(_CharT) + sizeof(size_type) - 1) / 3255 sizeof(size_type)]; 3256 3257 // NB: This is the special case for Input Iterators, used in 3258 // istreambuf_iterators, etc. 3259 // Input Iterators have a cost structure very different from 3260 // pointers, calling for a different coding style. 3261 template<typename _CharT, typename _Traits, typename _Alloc> 3262 template<typename _InIterator> 3263 _CharT* 3264 basic_string<_CharT, _Traits, _Alloc>:: 3265 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a, 3266 input_iterator_tag) 3267 { 3268 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 3269 if (__beg == __end && __a == _Alloc()) 3270 return _S_empty_rep()._M_refdata(); 3271 #endif 3272 // Avoid reallocation for common case. 3273 _CharT __buf[128]; 3274 size_type __len = 0; 3275 while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT)) 3276 { 3277 __buf[__len++] = *__beg; 3278 ++__beg; 3279 } 3280 _Rep* __r = _Rep::_S_create(__len, size_type(0), __a); 3281 _M_copy(__r->_M_refdata(), __buf, __len); 3282 __try 3283 { 3284 while (__beg != __end) 3285 { 3286 if (__len == __r->_M_capacity) 3287 { 3288 // Allocate more space. 3289 _Rep* __another = _Rep::_S_create(__len + 1, __len, __a); 3290 _M_copy(__another->_M_refdata(), __r->_M_refdata(), __len); 3291 __r->_M_destroy(__a); 3292 __r = __another; 3293 } 3294 __r->_M_refdata()[__len++] = *__beg; 3295 ++__beg; 3296 } 3297 } 3298 __catch(...) 3299 { 3300 __r->_M_destroy(__a); 3301 __throw_exception_again; 3302 } 3303 __r->_M_set_length_and_sharable(__len); 3304 return __r->_M_refdata(); 3305 } 3306 3307 template<typename _CharT, typename _Traits, typename _Alloc> 3308 template <typename _InIterator> 3309 _CharT* 3310 basic_string<_CharT, _Traits, _Alloc>:: 3311 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a, 3312 forward_iterator_tag) 3313 { 3314 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 3315 if (__beg == __end && __a == _Alloc()) 3316 return _S_empty_rep()._M_refdata(); 3317 #endif 3318 // NB: Not required, but considered best practice. 3319 if (__gnu_cxx::__is_null_pointer(__beg) && __beg != __end) 3320 __throw_logic_error(__N("basic_string::_S_construct null not valid")); 3321 3322 const size_type __dnew = static_cast<size_type>(std::distance(__beg, 3323 __end)); 3324 // Check for out_of_range and length_error exceptions. 3325 _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a); 3326 __try 3327 { _S_copy_chars(__r->_M_refdata(), __beg, __end); } 3328 __catch(...) 3329 { 3330 __r->_M_destroy(__a); 3331 __throw_exception_again; 3332 } 3333 __r->_M_set_length_and_sharable(__dnew); 3334 return __r->_M_refdata(); 3335 } 3336 3337 template<typename _CharT, typename _Traits, typename _Alloc> 3338 _CharT* 3339 basic_string<_CharT, _Traits, _Alloc>:: 3340 _S_construct(size_type __n, _CharT __c, const _Alloc& __a) 3341 { 3342 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 3343 if (__n == 0 && __a == _Alloc()) 3344 return _S_empty_rep()._M_refdata(); 3345 #endif 3346 // Check for out_of_range and length_error exceptions. 3347 _Rep* __r = _Rep::_S_create(__n, size_type(0), __a); 3348 if (__n) 3349 _M_assign(__r->_M_refdata(), __n, __c); 3350 3351 __r->_M_set_length_and_sharable(__n); 3352 return __r->_M_refdata(); 3353 } 3354 3355 template<typename _CharT, typename _Traits, typename _Alloc> 3356 basic_string<_CharT, _Traits, _Alloc>:: 3357 basic_string(const basic_string& __str, size_type __pos, const _Alloc& __a) 3358 : _M_dataplus(_S_construct(__str._M_data() 3359 + __str._M_check(__pos, 3360 "basic_string::basic_string"), 3361 __str._M_data() + __str._M_limit(__pos, npos) 3362 + __pos, __a), __a) 3363 { } 3364 3365 template<typename _CharT, typename _Traits, typename _Alloc> 3366 basic_string<_CharT, _Traits, _Alloc>:: 3367 basic_string(const basic_string& __str, size_type __pos, size_type __n) 3368 : _M_dataplus(_S_construct(__str._M_data() 3369 + __str._M_check(__pos, 3370 "basic_string::basic_string"), 3371 __str._M_data() + __str._M_limit(__pos, __n) 3372 + __pos, _Alloc()), _Alloc()) 3373 { } 3374 3375 template<typename _CharT, typename _Traits, typename _Alloc> 3376 basic_string<_CharT, _Traits, _Alloc>:: 3377 basic_string(const basic_string& __str, size_type __pos, 3378 size_type __n, const _Alloc& __a) 3379 : _M_dataplus(_S_construct(__str._M_data() 3380 + __str._M_check(__pos, 3381 "basic_string::basic_string"), 3382 __str._M_data() + __str._M_limit(__pos, __n) 3383 + __pos, __a), __a) 3384 { } 3385 3386 template<typename _CharT, typename _Traits, typename _Alloc> 3387 basic_string<_CharT, _Traits, _Alloc>& 3388 basic_string<_CharT, _Traits, _Alloc>:: 3389 assign(const basic_string& __str) 3390 { 3391 if (_M_rep() != __str._M_rep()) 3392 { 3393 // XXX MT 3394 const allocator_type __a = this->get_allocator(); 3395 _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator()); 3396 _M_rep()->_M_dispose(__a); 3397 _M_data(__tmp); 3398 } 3399 return *this; 3400 } 3401 3402 template<typename _CharT, typename _Traits, typename _Alloc> 3403 basic_string<_CharT, _Traits, _Alloc>& 3404 basic_string<_CharT, _Traits, _Alloc>:: 3405 assign(const _CharT* __s, size_type __n) 3406 { 3407 __glibcxx_requires_string_len(__s, __n); 3408 _M_check_length(this->size(), __n, "basic_string::assign"); 3409 if (_M_disjunct(__s) || _M_rep()->_M_is_shared()) 3410 return _M_replace_safe(size_type(0), this->size(), __s, __n); 3411 else 3412 { 3413 // Work in-place. 3414 const size_type __pos = __s - _M_data(); 3415 if (__pos >= __n) 3416 _M_copy(_M_data(), __s, __n); 3417 else if (__pos) 3418 _M_move(_M_data(), __s, __n); 3419 _M_rep()->_M_set_length_and_sharable(__n); 3420 return *this; 3421 } 3422 } 3423 3424 template<typename _CharT, typename _Traits, typename _Alloc> 3425 basic_string<_CharT, _Traits, _Alloc>& 3426 basic_string<_CharT, _Traits, _Alloc>:: 3427 append(size_type __n, _CharT __c) 3428 { 3429 if (__n) 3430 { 3431 _M_check_length(size_type(0), __n, "basic_string::append"); 3432 const size_type __len = __n + this->size(); 3433 if (__len > this->capacity() || _M_rep()->_M_is_shared()) 3434 this->reserve(__len); 3435 _M_assign(_M_data() + this->size(), __n, __c); 3436 _M_rep()->_M_set_length_and_sharable(__len); 3437 } 3438 return *this; 3439 } 3440 3441 template<typename _CharT, typename _Traits, typename _Alloc> 3442 basic_string<_CharT, _Traits, _Alloc>& 3443 basic_string<_CharT, _Traits, _Alloc>:: 3444 append(const _CharT* __s, size_type __n) 3445 { 3446 __glibcxx_requires_string_len(__s, __n); 3447 if (__n) 3448 { 3449 _M_check_length(size_type(0), __n, "basic_string::append"); 3450 const size_type __len = __n + this->size(); 3451 if (__len > this->capacity() || _M_rep()->_M_is_shared()) 3452 { 3453 if (_M_disjunct(__s)) 3454 this->reserve(__len); 3455 else 3456 { 3457 const size_type __off = __s - _M_data(); 3458 this->reserve(__len); 3459 __s = _M_data() + __off; 3460 } 3461 } 3462 _M_copy(_M_data() + this->size(), __s, __n); 3463 _M_rep()->_M_set_length_and_sharable(__len); 3464 } 3465 return *this; 3466 } 3467 3468 template<typename _CharT, typename _Traits, typename _Alloc> 3469 basic_string<_CharT, _Traits, _Alloc>& 3470 basic_string<_CharT, _Traits, _Alloc>:: 3471 append(const basic_string& __str) 3472 { 3473 const size_type __size = __str.size(); 3474 if (__size) 3475 { 3476 const size_type __len = __size + this->size(); 3477 if (__len > this->capacity() || _M_rep()->_M_is_shared()) 3478 this->reserve(__len); 3479 _M_copy(_M_data() + this->size(), __str._M_data(), __size); 3480 _M_rep()->_M_set_length_and_sharable(__len); 3481 } 3482 return *this; 3483 } 3484 3485 template<typename _CharT, typename _Traits, typename _Alloc> 3486 basic_string<_CharT, _Traits, _Alloc>& 3487 basic_string<_CharT, _Traits, _Alloc>:: 3488 append(const basic_string& __str, size_type __pos, size_type __n) 3489 { 3490 __str._M_check(__pos, "basic_string::append"); 3491 __n = __str._M_limit(__pos, __n); 3492 if (__n) 3493 { 3494 const size_type __len = __n + this->size(); 3495 if (__len > this->capacity() || _M_rep()->_M_is_shared()) 3496 this->reserve(__len); 3497 _M_copy(_M_data() + this->size(), __str._M_data() + __pos, __n); 3498 _M_rep()->_M_set_length_and_sharable(__len); 3499 } 3500 return *this; 3501 } 3502 3503 template<typename _CharT, typename _Traits, typename _Alloc> 3504 basic_string<_CharT, _Traits, _Alloc>& 3505 basic_string<_CharT, _Traits, _Alloc>:: 3506 insert(size_type __pos, const _CharT* __s, size_type __n) 3507 { 3508 __glibcxx_requires_string_len(__s, __n); 3509 _M_check(__pos, "basic_string::insert"); 3510 _M_check_length(size_type(0), __n, "basic_string::insert"); 3511 if (_M_disjunct(__s) || _M_rep()->_M_is_shared()) 3512 return _M_replace_safe(__pos, size_type(0), __s, __n); 3513 else 3514 { 3515 // Work in-place. 3516 const size_type __off = __s - _M_data(); 3517 _M_mutate(__pos, 0, __n); 3518 __s = _M_data() + __off; 3519 _CharT* __p = _M_data() + __pos; 3520 if (__s + __n <= __p) 3521 _M_copy(__p, __s, __n); 3522 else if (__s >= __p) 3523 _M_copy(__p, __s + __n, __n); 3524 else 3525 { 3526 const size_type __nleft = __p - __s; 3527 _M_copy(__p, __s, __nleft); 3528 _M_copy(__p + __nleft, __p + __n, __n - __nleft); 3529 } 3530 return *this; 3531 } 3532 } 3533 3534 template<typename _CharT, typename _Traits, typename _Alloc> 3535 typename basic_string<_CharT, _Traits, _Alloc>::iterator 3536 basic_string<_CharT, _Traits, _Alloc>:: 3537 erase(iterator __first, iterator __last) 3538 { 3539 _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last 3540 && __last <= _M_iend()); 3541 3542 // NB: This isn't just an optimization (bail out early when 3543 // there is nothing to do, really), it's also a correctness 3544 // issue vs MT, see libstdc++/40518. 3545 const size_type __size = __last - __first; 3546 if (__size) 3547 { 3548 const size_type __pos = __first - _M_ibegin(); 3549 _M_mutate(__pos, __size, size_type(0)); 3550 _M_rep()->_M_set_leaked(); 3551 return iterator(_M_data() + __pos); 3552 } 3553 else 3554 return __first; 3555 } 3556 3557 template<typename _CharT, typename _Traits, typename _Alloc> 3558 basic_string<_CharT, _Traits, _Alloc>& 3559 basic_string<_CharT, _Traits, _Alloc>:: 3560 replace(size_type __pos, size_type __n1, const _CharT* __s, 3561 size_type __n2) 3562 { 3563 __glibcxx_requires_string_len(__s, __n2); 3564 _M_check(__pos, "basic_string::replace"); 3565 __n1 = _M_limit(__pos, __n1); 3566 _M_check_length(__n1, __n2, "basic_string::replace"); 3567 bool __left; 3568 if (_M_disjunct(__s) || _M_rep()->_M_is_shared()) 3569 return _M_replace_safe(__pos, __n1, __s, __n2); 3570 else if ((__left = __s + __n2 <= _M_data() + __pos) 3571 || _M_data() + __pos + __n1 <= __s) 3572 { 3573 // Work in-place: non-overlapping case. 3574 size_type __off = __s - _M_data(); 3575 __left ? __off : (__off += __n2 - __n1); 3576 _M_mutate(__pos, __n1, __n2); 3577 _M_copy(_M_data() + __pos, _M_data() + __off, __n2); 3578 return *this; 3579 } 3580 else 3581 { 3582 // TODO: overlapping case. 3583 const basic_string __tmp(__s, __n2); 3584 return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2); 3585 } 3586 } 3587 3588 template<typename _CharT, typename _Traits, typename _Alloc> 3589 void 3590 basic_string<_CharT, _Traits, _Alloc>::_Rep:: 3591 _M_destroy(const _Alloc& __a) throw () 3592 { 3593 const size_type __size = sizeof(_Rep_base) 3594 + (this->_M_capacity + 1) * sizeof(_CharT); 3595 _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size); 3596 } 3597 3598 template<typename _CharT, typename _Traits, typename _Alloc> 3599 void 3600 basic_string<_CharT, _Traits, _Alloc>:: 3601 _M_leak_hard() 3602 { 3603 // No need to create a new copy of an empty string when a non-const 3604 // reference/pointer/iterator into it is obtained. Modifying the 3605 // trailing null character is undefined, so the ref/pointer/iterator 3606 // is effectively const anyway. 3607 if (this->empty()) 3608 return; 3609 3610 if (_M_rep()->_M_is_shared()) 3611 _M_mutate(0, 0, 0); 3612 _M_rep()->_M_set_leaked(); 3613 } 3614 3615 template<typename _CharT, typename _Traits, typename _Alloc> 3616 void 3617 basic_string<_CharT, _Traits, _Alloc>:: 3618 _M_mutate(size_type __pos, size_type __len1, size_type __len2) 3619 { 3620 const size_type __old_size = this->size(); 3621 const size_type __new_size = __old_size + __len2 - __len1; 3622 const size_type __how_much = __old_size - __pos - __len1; 3623 3624 if (__new_size > this->capacity() || _M_rep()->_M_is_shared()) 3625 { 3626 // Must reallocate. 3627 const allocator_type __a = get_allocator(); 3628 _Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a); 3629 3630 if (__pos) 3631 _M_copy(__r->_M_refdata(), _M_data(), __pos); 3632 if (__how_much) 3633 _M_copy(__r->_M_refdata() + __pos + __len2, 3634 _M_data() + __pos + __len1, __how_much); 3635 3636 _M_rep()->_M_dispose(__a); 3637 _M_data(__r->_M_refdata()); 3638 } 3639 else if (__how_much && __len1 != __len2) 3640 { 3641 // Work in-place. 3642 _M_move(_M_data() + __pos + __len2, 3643 _M_data() + __pos + __len1, __how_much); 3644 } 3645 _M_rep()->_M_set_length_and_sharable(__new_size); 3646 } 3647 3648 template<typename _CharT, typename _Traits, typename _Alloc> 3649 void 3650 basic_string<_CharT, _Traits, _Alloc>:: 3651 reserve(size_type __res) 3652 { 3653 const size_type __capacity = capacity(); 3654 3655 // _GLIBCXX_RESOLVE_LIB_DEFECTS 3656 // 2968. Inconsistencies between basic_string reserve and 3657 // vector/unordered_map/unordered_set reserve functions 3658 // P0966 reserve should not shrink 3659 if (__res <= __capacity) 3660 { 3661 if (!_M_rep()->_M_is_shared()) 3662 return; 3663 3664 // unshare, but keep same capacity 3665 __res = __capacity; 3666 } 3667 3668 const allocator_type __a = get_allocator(); 3669 _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size()); 3670 _M_rep()->_M_dispose(__a); 3671 _M_data(__tmp); 3672 } 3673 3674 template<typename _CharT, typename _Traits, typename _Alloc> 3675 void 3676 basic_string<_CharT, _Traits, _Alloc>:: 3677 swap(basic_string& __s) 3678 _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value) 3679 { 3680 if (_M_rep()->_M_is_leaked()) 3681 _M_rep()->_M_set_sharable(); 3682 if (__s._M_rep()->_M_is_leaked()) 3683 __s._M_rep()->_M_set_sharable(); 3684 if (this->get_allocator() == __s.get_allocator()) 3685 { 3686 _CharT* __tmp = _M_data(); 3687 _M_data(__s._M_data()); 3688 __s._M_data(__tmp); 3689 } 3690 // The code below can usually be optimized away. 3691 else 3692 { 3693 const basic_string __tmp1(_M_ibegin(), _M_iend(), 3694 __s.get_allocator()); 3695 const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(), 3696 this->get_allocator()); 3697 *this = __tmp2; 3698 __s = __tmp1; 3699 } 3700 } 3701 3702 template<typename _CharT, typename _Traits, typename _Alloc> 3703 typename basic_string<_CharT, _Traits, _Alloc>::_Rep* 3704 basic_string<_CharT, _Traits, _Alloc>::_Rep:: 3705 _S_create(size_type __capacity, size_type __old_capacity, 3706 const _Alloc& __alloc) 3707 { 3708 // _GLIBCXX_RESOLVE_LIB_DEFECTS 3709 // 83. String::npos vs. string::max_size() 3710 if (__capacity > _S_max_size) 3711 __throw_length_error(__N("basic_string::_S_create")); 3712 3713 // The standard places no restriction on allocating more memory 3714 // than is strictly needed within this layer at the moment or as 3715 // requested by an explicit application call to reserve(n). 3716 3717 // Many malloc implementations perform quite poorly when an 3718 // application attempts to allocate memory in a stepwise fashion 3719 // growing each allocation size by only 1 char. Additionally, 3720 // it makes little sense to allocate less linear memory than the 3721 // natural blocking size of the malloc implementation. 3722 // Unfortunately, we would need a somewhat low-level calculation 3723 // with tuned parameters to get this perfect for any particular 3724 // malloc implementation. Fortunately, generalizations about 3725 // common features seen among implementations seems to suffice. 3726 3727 // __pagesize need not match the actual VM page size for good 3728 // results in practice, thus we pick a common value on the low 3729 // side. __malloc_header_size is an estimate of the amount of 3730 // overhead per memory allocation (in practice seen N * sizeof 3731 // (void*) where N is 0, 2 or 4). According to folklore, 3732 // picking this value on the high side is better than 3733 // low-balling it (especially when this algorithm is used with 3734 // malloc implementations that allocate memory blocks rounded up 3735 // to a size which is a power of 2). 3736 const size_type __pagesize = 4096; 3737 const size_type __malloc_header_size = 4 * sizeof(void*); 3738 3739 // The below implements an exponential growth policy, necessary to 3740 // meet amortized linear time requirements of the library: see 3741 // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html. 3742 // It's active for allocations requiring an amount of memory above 3743 // system pagesize. This is consistent with the requirements of the 3744 // standard: http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html 3745 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity) 3746 __capacity = 2 * __old_capacity; 3747 3748 // NB: Need an array of char_type[__capacity], plus a terminating 3749 // null char_type() element, plus enough for the _Rep data structure. 3750 // Whew. Seemingly so needy, yet so elemental. 3751 size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep); 3752 3753 const size_type __adj_size = __size + __malloc_header_size; 3754 if (__adj_size > __pagesize && __capacity > __old_capacity) 3755 { 3756 const size_type __extra = __pagesize - __adj_size % __pagesize; 3757 __capacity += __extra / sizeof(_CharT); 3758 // Never allocate a string bigger than _S_max_size. 3759 if (__capacity > _S_max_size) 3760 __capacity = _S_max_size; 3761 __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep); 3762 } 3763 3764 // NB: Might throw, but no worries about a leak, mate: _Rep() 3765 // does not throw. 3766 void* __place = _Raw_bytes_alloc(__alloc).allocate(__size); 3767 _Rep *__p = new (__place) _Rep; 3768 __p->_M_capacity = __capacity; 3769 // ABI compatibility - 3.4.x set in _S_create both 3770 // _M_refcount and _M_length. All callers of _S_create 3771 // in basic_string.tcc then set just _M_length. 3772 // In 4.0.x and later both _M_refcount and _M_length 3773 // are initialized in the callers, unfortunately we can 3774 // have 3.4.x compiled code with _S_create callers inlined 3775 // calling 4.0.x+ _S_create. 3776 __p->_M_set_sharable(); 3777 return __p; 3778 } 3779 3780 template<typename _CharT, typename _Traits, typename _Alloc> 3781 _CharT* 3782 basic_string<_CharT, _Traits, _Alloc>::_Rep:: 3783 _M_clone(const _Alloc& __alloc, size_type __res) 3784 { 3785 // Requested capacity of the clone. 3786 const size_type __requested_cap = this->_M_length + __res; 3787 _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity, 3788 __alloc); 3789 if (this->_M_length) 3790 _M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length); 3791 3792 __r->_M_set_length_and_sharable(this->_M_length); 3793 return __r->_M_refdata(); 3794 } 3795 3796 template<typename _CharT, typename _Traits, typename _Alloc> 3797 void 3798 basic_string<_CharT, _Traits, _Alloc>:: 3799 resize(size_type __n, _CharT __c) 3800 { 3801 const size_type __size = this->size(); 3802 _M_check_length(__size, __n, "basic_string::resize"); 3803 if (__size < __n) 3804 this->append(__n - __size, __c); 3805 else if (__n < __size) 3806 this->erase(__n); 3807 // else nothing (in particular, avoid calling _M_mutate() unnecessarily.) 3808 } 3809 3810 template<typename _CharT, typename _Traits, typename _Alloc> 3811 template<typename _InputIterator> 3812 basic_string<_CharT, _Traits, _Alloc>& 3813 basic_string<_CharT, _Traits, _Alloc>:: 3814 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1, 3815 _InputIterator __k2, __false_type) 3816 { 3817 const basic_string __s(__k1, __k2); 3818 const size_type __n1 = __i2 - __i1; 3819 _M_check_length(__n1, __s.size(), "basic_string::_M_replace_dispatch"); 3820 return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(), 3821 __s.size()); 3822 } 3823 3824 template<typename _CharT, typename _Traits, typename _Alloc> 3825 basic_string<_CharT, _Traits, _Alloc>& 3826 basic_string<_CharT, _Traits, _Alloc>:: 3827 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2, 3828 _CharT __c) 3829 { 3830 _M_check_length(__n1, __n2, "basic_string::_M_replace_aux"); 3831 _M_mutate(__pos1, __n1, __n2); 3832 if (__n2) 3833 _M_assign(_M_data() + __pos1, __n2, __c); 3834 return *this; 3835 } 3836 3837 template<typename _CharT, typename _Traits, typename _Alloc> 3838 basic_string<_CharT, _Traits, _Alloc>& 3839 basic_string<_CharT, _Traits, _Alloc>:: 3840 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s, 3841 size_type __n2) 3842 { 3843 _M_mutate(__pos1, __n1, __n2); 3844 if (__n2) 3845 _M_copy(_M_data() + __pos1, __s, __n2); 3846 return *this; 3847 } 3848 3849 template<typename _CharT, typename _Traits, typename _Alloc> 3850 void 3851 basic_string<_CharT, _Traits, _Alloc>:: 3852 reserve() 3853 { 3854 #if __cpp_exceptions 3855 if (length() < capacity() || _M_rep()->_M_is_shared()) 3856 try 3857 { 3858 const allocator_type __a = get_allocator(); 3859 _CharT* __tmp = _M_rep()->_M_clone(__a); 3860 _M_rep()->_M_dispose(__a); 3861 _M_data(__tmp); 3862 } 3863 catch (const __cxxabiv1::__forced_unwind&) 3864 { throw; } 3865 catch (...) 3866 { /* swallow the exception */ } 3867 #endif 3868 } 3869 3870 template<typename _CharT, typename _Traits, typename _Alloc> 3871 typename basic_string<_CharT, _Traits, _Alloc>::size_type 3872 basic_string<_CharT, _Traits, _Alloc>:: 3873 copy(_CharT* __s, size_type __n, size_type __pos) const 3874 { 3875 _M_check(__pos, "basic_string::copy"); 3876 __n = _M_limit(__pos, __n); 3877 __glibcxx_requires_string_len(__s, __n); 3878 if (__n) 3879 _M_copy(__s, _M_data() + __pos, __n); 3880 // 21.3.5.7 par 3: do not append null. (good.) 3881 return __n; 3882 } 3883 3884 #ifdef __glibcxx_string_resize_and_overwrite // C++ >= 23 3885 template<typename _CharT, typename _Traits, typename _Alloc> 3886 template<typename _Operation> 3887 [[__gnu__::__always_inline__]] 3888 inline void 3889 basic_string<_CharT, _Traits, _Alloc>:: 3890 __resize_and_overwrite(const size_type __n, _Operation __op) 3891 { resize_and_overwrite<_Operation&>(__n, __op); } 3892 #endif 3893 3894 #if __cplusplus >= 201103L 3895 template<typename _CharT, typename _Traits, typename _Alloc> 3896 template<typename _Operation> 3897 void 3898 basic_string<_CharT, _Traits, _Alloc>:: 3899 #ifdef __glibcxx_string_resize_and_overwrite // C++ >= 23 3900 resize_and_overwrite(const size_type __n, _Operation __op) 3901 #else 3902 __resize_and_overwrite(const size_type __n, _Operation __op) 3903 #endif 3904 { 3905 const size_type __capacity = capacity(); 3906 _CharT* __p; 3907 if (__n > __capacity || _M_rep()->_M_is_shared()) 3908 this->reserve(__n); 3909 __p = _M_data(); 3910 struct _Terminator { 3911 ~_Terminator() { _M_this->_M_rep()->_M_set_length_and_sharable(_M_r); } 3912 basic_string* _M_this; 3913 size_type _M_r; 3914 }; 3915 _Terminator __term{this, 0}; 3916 auto __r = std::move(__op)(__p + 0, __n + 0); 3917 #ifdef __cpp_lib_concepts 3918 static_assert(ranges::__detail::__is_integer_like<decltype(__r)>); 3919 #else 3920 static_assert(__gnu_cxx::__is_integer_nonstrict<decltype(__r)>::__value, 3921 "resize_and_overwrite operation must return an integer"); 3922 #endif 3923 _GLIBCXX_DEBUG_ASSERT(__r >= 0 && size_type(__r) <= __n); 3924 __term._M_r = size_type(__r); 3925 if (__term._M_r > __n) 3926 __builtin_unreachable(); 3927 } 3928 #endif // C++11 3929 3930 3931 _GLIBCXX_END_NAMESPACE_VERSION 3932 } // namespace std 3933 #endif // ! _GLIBCXX_USE_CXX11_ABI 3934 #endif // _COW_STRING_H