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