1// Components for manipulating sequences of characters -*- C++ -*- 2 3// Copyright (C) 1997-2015 Free Software Foundation, Inc. 4// 5// This file is part of the GNU ISO C++ Library. This library is free 6// software; you can redistribute it and/or modify it under the 7// terms of the GNU General Public License as published by the 8// Free Software Foundation; either version 3, or (at your option) 9// any later version. 10 11// This library is distributed in the hope that it will be useful, 12// but WITHOUT ANY WARRANTY; without even the implied warranty of 13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14// GNU General Public License for more details. 15 16// Under Section 7 of GPL version 3, you are granted additional 17// permissions described in the GCC Runtime Library Exception, version 18// 3.1, as published by the Free Software Foundation. 19 20// You should have received a copy of the GNU General Public License and 21// a copy of the GCC Runtime Library Exception along with this program; 22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23// <http://www.gnu.org/licenses/>. 24 25/** @file bits/basic_string.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 30// 31// ISO C++ 14882: 21 Strings library 32// 33 34#ifndef _BASIC_STRING_H 35#define _BASIC_STRING_H 1 36 37#pragma GCC system_header 38 39#include <ext/atomicity.h> 40#include <ext/alloc_traits.h> 41#include <debug/debug.h> 42#if __cplusplus >= 201103L 43#include <initializer_list> 44#endif 45 46namespace std _GLIBCXX_VISIBILITY(default) 47{ 48_GLIBCXX_BEGIN_NAMESPACE_VERSION 49 50#if _GLIBCXX_USE_CXX11_ABI 51_GLIBCXX_BEGIN_NAMESPACE_CXX11 52 /** 53 * @class basic_string basic_string.h <string> 54 * @brief Managing sequences of characters and character-like objects. 55 * 56 * @ingroup strings 57 * @ingroup sequences 58 * 59 * @tparam _CharT Type of character 60 * @tparam _Traits Traits for character type, defaults to 61 * char_traits<_CharT>. 62 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>. 63 * 64 * Meets the requirements of a <a href="tables.html#65">container</a>, a 65 * <a href="tables.html#66">reversible container</a>, and a 66 * <a href="tables.html#67">sequence</a>. Of the 67 * <a href="tables.html#68">optional sequence requirements</a>, only 68 * @c push_back, @c at, and @c %array access are supported. 69 */ 70 template<typename _CharT, typename _Traits, typename _Alloc> 71 class basic_string 72 { 73 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template 74 rebind<_CharT>::other _Char_alloc_type; 75 typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits; 76 77 // Types: 78 public: 79 typedef _Traits traits_type; 80 typedef typename _Traits::char_type value_type; 81 typedef _Char_alloc_type allocator_type; 82 typedef typename _Alloc_traits::size_type size_type; 83 typedef typename _Alloc_traits::difference_type difference_type; 84 typedef typename _Alloc_traits::reference reference; 85 typedef typename _Alloc_traits::const_reference const_reference; 86 typedef typename _Alloc_traits::pointer pointer; 87 typedef typename _Alloc_traits::const_pointer const_pointer; 88 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator; 89 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string> 90 const_iterator; 91 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 92 typedef std::reverse_iterator<iterator> reverse_iterator; 93 94 /// Value returned by various member functions when they fail. 95 static const size_type npos = static_cast<size_type>(-1); 96 97 private: 98 // type used for positions in insert, erase etc. 99#if __cplusplus < 201103L 100 typedef iterator __const_iterator; 101#else 102 typedef const_iterator __const_iterator; 103#endif 104 105 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html 106 struct _Alloc_hider : allocator_type // TODO check __is_final 107 { 108 _Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc()) 109 : allocator_type(__a), _M_p(__dat) { } 110 111 pointer _M_p; // The actual data. 112 }; 113 114 _Alloc_hider _M_dataplus; 115 size_type _M_string_length; 116 117 enum { _S_local_capacity = 15 / sizeof(_CharT) }; 118 119 union 120 { 121 _CharT _M_local_buf[_S_local_capacity + 1]; 122 size_type _M_allocated_capacity; 123 }; 124 125 void 126 _M_data(pointer __p) 127 { _M_dataplus._M_p = __p; } 128 129 void 130 _M_length(size_type __length) 131 { _M_string_length = __length; } 132 133 pointer 134 _M_data() const 135 { return _M_dataplus._M_p; } 136 137 pointer 138 _M_local_data() 139 { 140#if __cplusplus >= 201103L 141 return std::pointer_traits<pointer>::pointer_to(*_M_local_buf); 142#else 143 return pointer(_M_local_buf); 144#endif 145 } 146 147 const_pointer 148 _M_local_data() const 149 { 150#if __cplusplus >= 201103L 151 return std::pointer_traits<const_pointer>::pointer_to(*_M_local_buf); 152#else 153 return const_pointer(_M_local_buf); 154#endif 155 } 156 157 void 158 _M_capacity(size_type __capacity) 159 { _M_allocated_capacity = __capacity; } 160 161 void 162 _M_set_length(size_type __n) 163 { 164 _M_length(__n); 165 traits_type::assign(_M_data()[__n], _CharT()); 166 } 167 168 bool 169 _M_is_local() const 170 { return _M_data() == _M_local_data(); } 171 172 // Create & Destroy 173 pointer 174 _M_create(size_type&, size_type); 175 176 void 177 _M_dispose() 178 { 179 if (!_M_is_local()) 180 _M_destroy(_M_allocated_capacity); 181 } 182 183 void 184 _M_destroy(size_type __size) throw() 185 { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size + 1); } 186 187 // _M_construct_aux is used to implement the 21.3.1 para 15 which 188 // requires special behaviour if _InIterator is an integral type 189 template<typename _InIterator> 190 void 191 _M_construct_aux(_InIterator __beg, _InIterator __end, 192 std::__false_type) 193 { 194 typedef typename iterator_traits<_InIterator>::iterator_category _Tag; 195 _M_construct(__beg, __end, _Tag()); 196 } 197 198 // _GLIBCXX_RESOLVE_LIB_DEFECTS 199 // 438. Ambiguity in the "do the right thing" clause 200 template<typename _Integer> 201 void 202 _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type) 203 { _M_construct_aux_2(static_cast<size_type>(__beg), __end); } 204 205 void 206 _M_construct_aux_2(size_type __req, _CharT __c) 207 { _M_construct(__req, __c); } 208 209 template<typename _InIterator> 210 void 211 _M_construct(_InIterator __beg, _InIterator __end) 212 { 213 typedef typename std::__is_integer<_InIterator>::__type _Integral; 214 _M_construct_aux(__beg, __end, _Integral()); 215 } 216 217 // For Input Iterators, used in istreambuf_iterators, etc. 218 template<typename _InIterator> 219 void 220 _M_construct(_InIterator __beg, _InIterator __end, 221 std::input_iterator_tag); 222 223 // For forward_iterators up to random_access_iterators, used for 224 // string::iterator, _CharT*, etc. 225 template<typename _FwdIterator> 226 void 227 _M_construct(_FwdIterator __beg, _FwdIterator __end, 228 std::forward_iterator_tag); 229 230 void 231 _M_construct(size_type __req, _CharT __c); 232 233 allocator_type& 234 _M_get_allocator() 235 { return _M_dataplus; } 236 237 const allocator_type& 238 _M_get_allocator() const 239 { return _M_dataplus; } 240 241 private: 242 243#ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST 244 // The explicit instantiations in misc-inst.cc require this due to 245 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063 246 template<typename _Tp, bool _Requires = 247 !__are_same<_Tp, _CharT*>::__value 248 && !__are_same<_Tp, const _CharT*>::__value 249 && !__are_same<_Tp, iterator>::__value 250 && !__are_same<_Tp, const_iterator>::__value> 251 struct __enable_if_not_native_iterator 252 { typedef basic_string& __type; }; 253 template<typename _Tp> 254 struct __enable_if_not_native_iterator<_Tp, false> { }; 255#endif 256 257 size_type 258 _M_check(size_type __pos, const char* __s) const 259 { 260 if (__pos > this->size()) 261 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > " 262 "this->size() (which is %zu)"), 263 __s, __pos, this->size()); 264 return __pos; 265 } 266 267 void 268 _M_check_length(size_type __n1, size_type __n2, const char* __s) const 269 { 270 if (this->max_size() - (this->size() - __n1) < __n2) 271 __throw_length_error(__N(__s)); 272 } 273 274 275 // NB: _M_limit doesn't check for a bad __pos value. 276 size_type 277 _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT 278 { 279 const bool __testoff = __off < this->size() - __pos; 280 return __testoff ? __off : this->size() - __pos; 281 } 282 283 // True if _Rep and source do not overlap. 284 bool 285 _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT 286 { 287 return (less<const _CharT*>()(__s, _M_data()) 288 || less<const _CharT*>()(_M_data() + this->size(), __s)); 289 } 290 291 // When __n = 1 way faster than the general multichar 292 // traits_type::copy/move/assign. 293 static void 294 _S_copy(_CharT* __d, const _CharT* __s, size_type __n) 295 { 296 if (__n == 1) 297 traits_type::assign(*__d, *__s); 298 else 299 traits_type::copy(__d, __s, __n); 300 } 301 302 static void 303 _S_move(_CharT* __d, const _CharT* __s, size_type __n) 304 { 305 if (__n == 1) 306 traits_type::assign(*__d, *__s); 307 else 308 traits_type::move(__d, __s, __n); 309 } 310 311 static void 312 _S_assign(_CharT* __d, size_type __n, _CharT __c) 313 { 314 if (__n == 1) 315 traits_type::assign(*__d, __c); 316 else 317 traits_type::assign(__d, __n, __c); 318 } 319 320 // _S_copy_chars is a separate template to permit specialization 321 // to optimize for the common case of pointers as iterators. 322 template<class _Iterator> 323 static void 324 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2) 325 { 326 for (; __k1 != __k2; ++__k1, ++__p) 327 traits_type::assign(*__p, *__k1); // These types are off. 328 } 329 330 static void 331 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT 332 { _S_copy_chars(__p, __k1.base(), __k2.base()); } 333 334 static void 335 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2) 336 _GLIBCXX_NOEXCEPT 337 { _S_copy_chars(__p, __k1.base(), __k2.base()); } 338 339 static void 340 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT 341 { _S_copy(__p, __k1, __k2 - __k1); } 342 343 static void 344 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2) 345 _GLIBCXX_NOEXCEPT 346 { _S_copy(__p, __k1, __k2 - __k1); } 347 348 static int 349 _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT 350 { 351 const difference_type __d = difference_type(__n1 - __n2); 352 353 if (__d > __gnu_cxx::__numeric_traits<int>::__max) 354 return __gnu_cxx::__numeric_traits<int>::__max; 355 else if (__d < __gnu_cxx::__numeric_traits<int>::__min) 356 return __gnu_cxx::__numeric_traits<int>::__min; 357 else 358 return int(__d); 359 } 360 361 void 362 _M_assign(const basic_string& __rcs); 363 364 void 365 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s, 366 size_type __len2); 367 368 void 369 _M_erase(size_type __pos, size_type __n); 370 371 public: 372 // Construct/copy/destroy: 373 // NB: We overload ctors in some cases instead of using default 374 // arguments, per 17.4.4.4 para. 2 item 2. 375 376 /** 377 * @brief Default constructor creates an empty string. 378 */ 379 basic_string() 380#if __cplusplus >= 201103L 381 noexcept(is_nothrow_default_constructible<_Alloc>::value) 382#endif 383 : _M_dataplus(_M_local_data()) 384 { _M_set_length(0); } 385 386 /** 387 * @brief Construct an empty string using allocator @a a. 388 */ 389 explicit 390 basic_string(const _Alloc& __a) 391 : _M_dataplus(_M_local_data(), __a) 392 { _M_set_length(0); } 393 394 /** 395 * @brief Construct string with copy of value of @a __str. 396 * @param __str Source string. 397 */ 398 basic_string(const basic_string& __str) 399 : _M_dataplus(_M_local_data(), __str._M_get_allocator()) // TODO A traits 400 { _M_construct(__str._M_data(), __str._M_data() + __str.length()); } 401 402 /** 403 * @brief Construct string as copy of a substring. 404 * @param __str Source string. 405 * @param __pos Index of first character to copy from. 406 * @param __n Number of characters to copy (default remainder). 407 */ 408 // _GLIBCXX_RESOLVE_LIB_DEFECTS 409 // 2402. [this constructor] shouldn't use Allocator() 410 basic_string(const basic_string& __str, size_type __pos, 411 size_type __n = npos) 412 : _M_dataplus(_M_local_data()) 413 { 414 const _CharT* __start = __str._M_data() 415 + __str._M_check(__pos, "basic_string::basic_string"); 416 _M_construct(__start, __start + __str._M_limit(__pos, __n)); 417 } 418 419 /** 420 * @brief Construct string as copy of a substring. 421 * @param __str Source string. 422 * @param __pos Index of first character to copy from. 423 * @param __n Number of characters to copy (default remainder). 424 * @param __a Allocator to use. 425 */ 426 basic_string(const basic_string& __str, size_type __pos, 427 size_type __n, const _Alloc& __a) 428 : _M_dataplus(_M_local_data(), __a) 429 { 430 const _CharT* __start 431 = __str._M_data() + __str._M_check(__pos, "string::string"); 432 _M_construct(__start, __start + __str._M_limit(__pos, __n)); 433 } 434 435 /** 436 * @brief Construct string initialized by a character %array. 437 * @param __s Source character %array. 438 * @param __n Number of characters to copy. 439 * @param __a Allocator to use (default is default allocator). 440 * 441 * NB: @a __s must have at least @a __n characters, '\\0' 442 * has no special meaning. 443 */ 444 basic_string(const _CharT* __s, size_type __n, 445 const _Alloc& __a = _Alloc()) 446 : _M_dataplus(_M_local_data(), __a) 447 { _M_construct(__s, __s + __n); } 448 449 /** 450 * @brief Construct string as copy of a C string. 451 * @param __s Source C string. 452 * @param __a Allocator to use (default is default allocator). 453 */ 454 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc()) 455 : _M_dataplus(_M_local_data(), __a) 456 { _M_construct(__s, __s ? __s + traits_type::length(__s) : __s+npos); } 457 458 /** 459 * @brief Construct string as multiple characters. 460 * @param __n Number of characters. 461 * @param __c Character to use. 462 * @param __a Allocator to use (default is default allocator). 463 */ 464 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc()) 465 : _M_dataplus(_M_local_data(), __a) 466 { _M_construct(__n, __c); } 467 468#if __cplusplus >= 201103L 469 /** 470 * @brief Move construct string. 471 * @param __str Source string. 472 * 473 * The newly-created string contains the exact contents of @a __str. 474 * @a __str is a valid, but unspecified string. 475 **/ 476 basic_string(basic_string&& __str) noexcept 477 : _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator())) 478 { 479 if (__str._M_is_local()) 480 { 481 traits_type::copy(_M_local_buf, __str._M_local_buf, 482 _S_local_capacity + 1); 483 } 484 else 485 { 486 _M_data(__str._M_data()); 487 _M_capacity(__str._M_allocated_capacity); 488 } 489 490 // Must use _M_length() here not _M_set_length() because 491 // basic_stringbuf relies on writing into unallocated capacity so 492 // we mess up the contents if we put a '\0' in the string. 493 _M_length(__str.length()); 494 __str._M_data(__str._M_local_data()); 495 __str._M_set_length(0); 496 } 497 498 /** 499 * @brief Construct string from an initializer %list. 500 * @param __l std::initializer_list of characters. 501 * @param __a Allocator to use (default is default allocator). 502 */ 503 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc()) 504 : _M_dataplus(_M_local_data(), __a) 505 { _M_construct(__l.begin(), __l.end()); } 506 507 basic_string(const basic_string& __str, const _Alloc& __a) 508 : _M_dataplus(_M_local_data(), __a) 509 { _M_construct(__str.begin(), __str.end()); } 510 511 basic_string(basic_string&& __str, const _Alloc& __a) 512 : _M_dataplus(_M_local_data(), __a) 513 { 514 if (__str.get_allocator() == __a) 515 *this = std::move(__str); 516 else 517 _M_construct(__str.begin(), __str.end()); 518 } 519 520#endif // C++11 521 522 /** 523 * @brief Construct string as copy of a range. 524 * @param __beg Start of range. 525 * @param __end End of range. 526 * @param __a Allocator to use (default is default allocator). 527 */ 528#if __cplusplus >= 201103L 529 template<typename _InputIterator, 530 typename = std::_RequireInputIter<_InputIterator>> 531#else 532 template<typename _InputIterator> 533#endif 534 basic_string(_InputIterator __beg, _InputIterator __end, 535 const _Alloc& __a = _Alloc()) 536 : _M_dataplus(_M_local_data(), __a) 537 { _M_construct(__beg, __end); } 538 539 /** 540 * @brief Destroy the string instance. 541 */ 542 ~basic_string() 543 { _M_dispose(); } 544 545 /** 546 * @brief Assign the value of @a str to this string. 547 * @param __str Source string. 548 */ 549 basic_string& 550 operator=(const basic_string& __str) 551 { return this->assign(__str); } 552 553 /** 554 * @brief Copy contents of @a s into this string. 555 * @param __s Source null-terminated string. 556 */ 557 basic_string& 558 operator=(const _CharT* __s) 559 { return this->assign(__s); } 560 561 /** 562 * @brief Set value to string of length 1. 563 * @param __c Source character. 564 * 565 * Assigning to a character makes this string length 1 and 566 * (*this)[0] == @a c. 567 */ 568 basic_string& 569 operator=(_CharT __c) 570 { 571 this->assign(1, __c); 572 return *this; 573 } 574 575#if __cplusplus >= 201103L 576 /** 577 * @brief Move assign the value of @a str to this string. 578 * @param __str Source string. 579 * 580 * The contents of @a str are moved into this string (without copying). 581 * @a str is a valid, but unspecified string. 582 **/ 583 // PR 58265, this should be noexcept. 584 // _GLIBCXX_RESOLVE_LIB_DEFECTS 585 // 2063. Contradictory requirements for string move assignment 586 basic_string& 587 operator=(basic_string&& __str) 588 { 589 this->swap(__str); 590 return *this; 591 } 592 593 /** 594 * @brief Set value to string constructed from initializer %list. 595 * @param __l std::initializer_list. 596 */ 597 basic_string& 598 operator=(initializer_list<_CharT> __l) 599 { 600 this->assign(__l.begin(), __l.size()); 601 return *this; 602 } 603#endif // C++11 604 605 // Iterators: 606 /** 607 * Returns a read/write iterator that points to the first character in 608 * the %string. 609 */ 610 iterator 611 begin() _GLIBCXX_NOEXCEPT 612 { return iterator(_M_data()); } 613 614 /** 615 * Returns a read-only (constant) iterator that points to the first 616 * character in the %string. 617 */ 618 const_iterator 619 begin() const _GLIBCXX_NOEXCEPT 620 { return const_iterator(_M_data()); } 621 622 /** 623 * Returns a read/write iterator that points one past the last 624 * character in the %string. 625 */ 626 iterator 627 end() _GLIBCXX_NOEXCEPT 628 { return iterator(_M_data() + this->size()); } 629 630 /** 631 * Returns a read-only (constant) iterator that points one past the 632 * last character in the %string. 633 */ 634 const_iterator 635 end() const _GLIBCXX_NOEXCEPT 636 { return const_iterator(_M_data() + this->size()); } 637 638 /** 639 * Returns a read/write reverse iterator that points to the last 640 * character in the %string. Iteration is done in reverse element 641 * order. 642 */ 643 reverse_iterator 644 rbegin() _GLIBCXX_NOEXCEPT 645 { return reverse_iterator(this->end()); } 646 647 /** 648 * Returns a read-only (constant) reverse iterator that points 649 * to the last character in the %string. Iteration is done in 650 * reverse element order. 651 */ 652 const_reverse_iterator 653 rbegin() const _GLIBCXX_NOEXCEPT 654 { return const_reverse_iterator(this->end()); } 655 656 /** 657 * Returns a read/write reverse iterator that points to one before the 658 * first character in the %string. Iteration is done in reverse 659 * element order. 660 */ 661 reverse_iterator 662 rend() _GLIBCXX_NOEXCEPT 663 { return reverse_iterator(this->begin()); } 664 665 /** 666 * Returns a read-only (constant) reverse iterator that points 667 * to one before the first character in the %string. Iteration 668 * is done in reverse element order. 669 */ 670 const_reverse_iterator 671 rend() const _GLIBCXX_NOEXCEPT 672 { return const_reverse_iterator(this->begin()); } 673 674#if __cplusplus >= 201103L 675 /** 676 * Returns a read-only (constant) iterator that points to the first 677 * character in the %string. 678 */ 679 const_iterator 680 cbegin() const noexcept 681 { return const_iterator(this->_M_data()); } 682 683 /** 684 * Returns a read-only (constant) iterator that points one past the 685 * last character in the %string. 686 */ 687 const_iterator 688 cend() const noexcept 689 { return const_iterator(this->_M_data() + this->size()); } 690 691 /** 692 * Returns a read-only (constant) reverse iterator that points 693 * to the last character in the %string. Iteration is done in 694 * reverse element order. 695 */ 696 const_reverse_iterator 697 crbegin() const noexcept 698 { return const_reverse_iterator(this->end()); } 699 700 /** 701 * Returns a read-only (constant) reverse iterator that points 702 * to one before the first character in the %string. Iteration 703 * is done in reverse element order. 704 */ 705 const_reverse_iterator 706 crend() const noexcept 707 { return const_reverse_iterator(this->begin()); } 708#endif 709 710 public: 711 // Capacity: 712 /// Returns the number of characters in the string, not including any 713 /// null-termination. 714 size_type 715 size() const _GLIBCXX_NOEXCEPT 716 { return _M_string_length; } 717 718 /// Returns the number of characters in the string, not including any 719 /// null-termination. 720 size_type 721 length() const _GLIBCXX_NOEXCEPT 722 { return _M_string_length; } 723 724 /// Returns the size() of the largest possible %string. 725 size_type 726 max_size() const _GLIBCXX_NOEXCEPT 727 { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; } 728 729 /** 730 * @brief Resizes the %string to the specified number of characters. 731 * @param __n Number of characters the %string should contain. 732 * @param __c Character to fill any new elements. 733 * 734 * This function will %resize the %string to the specified 735 * number of characters. If the number is smaller than the 736 * %string's current size the %string is truncated, otherwise 737 * the %string is extended and new elements are %set to @a __c. 738 */ 739 void 740 resize(size_type __n, _CharT __c); 741 742 /** 743 * @brief Resizes the %string to the specified number of characters. 744 * @param __n Number of characters the %string should contain. 745 * 746 * This function will resize the %string to the specified length. If 747 * the new size is smaller than the %string's current size the %string 748 * is truncated, otherwise the %string is extended and new characters 749 * are default-constructed. For basic types such as char, this means 750 * setting them to 0. 751 */ 752 void 753 resize(size_type __n) 754 { this->resize(__n, _CharT()); } 755 756#if __cplusplus >= 201103L 757 /// A non-binding request to reduce capacity() to size(). 758 void 759 shrink_to_fit() noexcept 760 { 761#if __cpp_exceptions 762 if (capacity() > size()) 763 { 764 try 765 { reserve(0); } 766 catch(...) 767 { } 768 } 769#endif 770 } 771#endif 772 773 /** 774 * Returns the total number of characters that the %string can hold 775 * before needing to allocate more memory. 776 */ 777 size_type 778 capacity() const _GLIBCXX_NOEXCEPT 779 { 780 return _M_is_local() ? size_type(_S_local_capacity) 781 : _M_allocated_capacity; 782 } 783 784 /** 785 * @brief Attempt to preallocate enough memory for specified number of 786 * characters. 787 * @param __res_arg Number of characters required. 788 * @throw std::length_error If @a __res_arg exceeds @c max_size(). 789 * 790 * This function attempts to reserve enough memory for the 791 * %string to hold the specified number of characters. If the 792 * number requested is more than max_size(), length_error is 793 * thrown. 794 * 795 * The advantage of this function is that if optimal code is a 796 * necessity and the user can determine the string length that will be 797 * required, the user can reserve the memory in %advance, and thus 798 * prevent a possible reallocation of memory and copying of %string 799 * data. 800 */ 801 void 802 reserve(size_type __res_arg = 0); 803 804 /** 805 * Erases the string, making it empty. 806 */ 807 void 808 clear() _GLIBCXX_NOEXCEPT 809 { _M_set_length(0); } 810 811 /** 812 * Returns true if the %string is empty. Equivalent to 813 * <code>*this == ""</code>. 814 */ 815 bool 816 empty() const _GLIBCXX_NOEXCEPT 817 { return this->size() == 0; } 818 819 // Element access: 820 /** 821 * @brief Subscript access to the data contained in the %string. 822 * @param __pos The index of the character to access. 823 * @return Read-only (constant) reference to the character. 824 * 825 * This operator allows for easy, array-style, data access. 826 * Note that data access with this operator is unchecked and 827 * out_of_range lookups are not defined. (For checked lookups 828 * see at().) 829 */ 830 const_reference 831 operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT 832 { 833 _GLIBCXX_DEBUG_ASSERT(__pos <= size()); 834 return _M_data()[__pos]; 835 } 836 837 /** 838 * @brief Subscript access to the data contained in the %string. 839 * @param __pos The index of the character to access. 840 * @return Read/write reference to the character. 841 * 842 * This operator allows for easy, array-style, data access. 843 * Note that data access with this operator is unchecked and 844 * out_of_range lookups are not defined. (For checked lookups 845 * see at().) 846 */ 847 reference 848 operator[](size_type __pos) 849 { 850 // Allow pos == size() both in C++98 mode, as v3 extension, 851 // and in C++11 mode. 852 _GLIBCXX_DEBUG_ASSERT(__pos <= size()); 853 // In pedantic mode be strict in C++98 mode. 854 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size()); 855 return _M_data()[__pos]; 856 } 857 858 /** 859 * @brief Provides access to the data contained in the %string. 860 * @param __n The index of the character to access. 861 * @return Read-only (const) reference to the character. 862 * @throw std::out_of_range If @a n is an invalid index. 863 * 864 * This function provides for safer data access. The parameter is 865 * first checked that it is in the range of the string. The function 866 * throws out_of_range if the check fails. 867 */ 868 const_reference 869 at(size_type __n) const 870 { 871 if (__n >= this->size()) 872 __throw_out_of_range_fmt(__N("basic_string::at: __n " 873 "(which is %zu) >= this->size() " 874 "(which is %zu)"), 875 __n, this->size()); 876 return _M_data()[__n]; 877 } 878 879 /** 880 * @brief Provides access to the data contained in the %string. 881 * @param __n The index of the character to access. 882 * @return Read/write reference to the character. 883 * @throw std::out_of_range If @a n is an invalid index. 884 * 885 * This function provides for safer data access. The parameter is 886 * first checked that it is in the range of the string. The function 887 * throws out_of_range if the check fails. 888 */ 889 reference 890 at(size_type __n) 891 { 892 if (__n >= size()) 893 __throw_out_of_range_fmt(__N("basic_string::at: __n " 894 "(which is %zu) >= this->size() " 895 "(which is %zu)"), 896 __n, this->size()); 897 return _M_data()[__n]; 898 } 899 900#if __cplusplus >= 201103L 901 /** 902 * Returns a read/write reference to the data at the first 903 * element of the %string. 904 */ 905 reference 906 front() noexcept 907 { return operator[](0); } 908 909 /** 910 * Returns a read-only (constant) reference to the data at the first 911 * element of the %string. 912 */ 913 const_reference 914 front() const noexcept 915 { return operator[](0); } 916 917 /** 918 * Returns a read/write reference to the data at the last 919 * element of the %string. 920 */ 921 reference 922 back() noexcept 923 { return operator[](this->size() - 1); } 924 925 /** 926 * Returns a read-only (constant) reference to the data at the 927 * last element of the %string. 928 */ 929 const_reference 930 back() const noexcept 931 { return operator[](this->size() - 1); } 932#endif 933 934 // Modifiers: 935 /** 936 * @brief Append a string to this string. 937 * @param __str The string to append. 938 * @return Reference to this string. 939 */ 940 basic_string& 941 operator+=(const basic_string& __str) 942 { return this->append(__str); } 943 944 /** 945 * @brief Append a C string. 946 * @param __s The C string to append. 947 * @return Reference to this string. 948 */ 949 basic_string& 950 operator+=(const _CharT* __s) 951 { return this->append(__s); } 952 953 /** 954 * @brief Append a character. 955 * @param __c The character to append. 956 * @return Reference to this string. 957 */ 958 basic_string& 959 operator+=(_CharT __c) 960 { 961 this->push_back(__c); 962 return *this; 963 } 964 965#if __cplusplus >= 201103L 966 /** 967 * @brief Append an initializer_list of characters. 968 * @param __l The initializer_list of characters to be appended. 969 * @return Reference to this string. 970 */ 971 basic_string& 972 operator+=(initializer_list<_CharT> __l) 973 { return this->append(__l.begin(), __l.size()); } 974#endif // C++11 975 976 /** 977 * @brief Append a string to this string. 978 * @param __str The string to append. 979 * @return Reference to this string. 980 */ 981 basic_string& 982 append(const basic_string& __str) 983 { return _M_append(__str._M_data(), __str.size()); } 984 985 /** 986 * @brief Append a substring. 987 * @param __str The string to append. 988 * @param __pos Index of the first character of str to append. 989 * @param __n The number of characters to append. 990 * @return Reference to this string. 991 * @throw std::out_of_range if @a __pos is not a valid index. 992 * 993 * This function appends @a __n characters from @a __str 994 * starting at @a __pos to this string. If @a __n is is larger 995 * than the number of available characters in @a __str, the 996 * remainder of @a __str is appended. 997 */ 998 basic_string& 999 append(const basic_string& __str, size_type __pos, size_type __n) 1000 { return _M_append(__str._M_data() 1001 + __str._M_check(__pos, "basic_string::append"), 1002 __str._M_limit(__pos, __n)); } 1003 1004 /** 1005 * @brief Append a C substring. 1006 * @param __s The C string to append. 1007 * @param __n The number of characters to append. 1008 * @return Reference to this string. 1009 */ 1010 basic_string& 1011 append(const _CharT* __s, size_type __n) 1012 { 1013 __glibcxx_requires_string_len(__s, __n); 1014 _M_check_length(size_type(0), __n, "basic_string::append"); 1015 return _M_append(__s, __n); 1016 } 1017 1018 /** 1019 * @brief Append a C string. 1020 * @param __s The C string to append. 1021 * @return Reference to this string. 1022 */ 1023 basic_string& 1024 append(const _CharT* __s) 1025 { 1026 __glibcxx_requires_string(__s); 1027 const size_type __n = traits_type::length(__s); 1028 _M_check_length(size_type(0), __n, "basic_string::append"); 1029 return _M_append(__s, __n); 1030 } 1031 1032 /** 1033 * @brief Append multiple characters. 1034 * @param __n The number of characters to append. 1035 * @param __c The character to use. 1036 * @return Reference to this string. 1037 * 1038 * Appends __n copies of __c to this string. 1039 */ 1040 basic_string& 1041 append(size_type __n, _CharT __c) 1042 { return _M_replace_aux(this->size(), size_type(0), __n, __c); } 1043 1044#if __cplusplus >= 201103L 1045 /** 1046 * @brief Append an initializer_list of characters. 1047 * @param __l The initializer_list of characters to append. 1048 * @return Reference to this string. 1049 */ 1050 basic_string& 1051 append(initializer_list<_CharT> __l) 1052 { return this->append(__l.begin(), __l.size()); } 1053#endif // C++11 1054 1055 /** 1056 * @brief Append a range of characters. 1057 * @param __first Iterator referencing the first character to append. 1058 * @param __last Iterator marking the end of the range. 1059 * @return Reference to this string. 1060 * 1061 * Appends characters in the range [__first,__last) to this string. 1062 */ 1063#if __cplusplus >= 201103L 1064 template<class _InputIterator, 1065 typename = std::_RequireInputIter<_InputIterator>> 1066#else 1067 template<class _InputIterator> 1068#endif 1069 basic_string& 1070 append(_InputIterator __first, _InputIterator __last) 1071 { return this->replace(end(), end(), __first, __last); } 1072 1073 /** 1074 * @brief Append a single character. 1075 * @param __c Character to append. 1076 */ 1077 void 1078 push_back(_CharT __c) 1079 { 1080 const size_type __size = this->size(); 1081 if (__size + 1 > this->capacity()) 1082 this->_M_mutate(__size, size_type(0), 0, size_type(1)); 1083 traits_type::assign(this->_M_data()[__size], __c); 1084 this->_M_set_length(__size + 1); 1085 } 1086 1087 /** 1088 * @brief Set value to contents of another string. 1089 * @param __str Source string to use. 1090 * @return Reference to this string. 1091 */ 1092 basic_string& 1093 assign(const basic_string& __str) 1094 { 1095 this->_M_assign(__str); 1096 return *this; 1097 } 1098 1099#if __cplusplus >= 201103L 1100 /** 1101 * @brief Set value to contents of another string. 1102 * @param __str Source string to use. 1103 * @return Reference to this string. 1104 * 1105 * This function sets this string to the exact contents of @a __str. 1106 * @a __str is a valid, but unspecified string. 1107 */ 1108 basic_string& 1109 assign(basic_string&& __str) 1110 { 1111 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1112 // 2063. Contradictory requirements for string move assignment 1113 return *this = std::move(__str); 1114 } 1115#endif // C++11 1116 1117 /** 1118 * @brief Set value to a substring of a string. 1119 * @param __str The string to use. 1120 * @param __pos Index of the first character of str. 1121 * @param __n Number of characters to use. 1122 * @return Reference to this string. 1123 * @throw std::out_of_range if @a pos is not a valid index. 1124 * 1125 * This function sets this string to the substring of @a __str 1126 * consisting of @a __n characters at @a __pos. If @a __n is 1127 * is larger than the number of available characters in @a 1128 * __str, the remainder of @a __str is used. 1129 */ 1130 basic_string& 1131 assign(const basic_string& __str, size_type __pos, size_type __n) 1132 { return _M_replace(size_type(0), this->size(), __str._M_data() 1133 + __str._M_check(__pos, "basic_string::assign"), 1134 __str._M_limit(__pos, __n)); } 1135 1136 /** 1137 * @brief Set value to a C substring. 1138 * @param __s The C string to use. 1139 * @param __n Number of characters to use. 1140 * @return Reference to this string. 1141 * 1142 * This function sets the value of this string to the first @a __n 1143 * characters of @a __s. If @a __n is is larger than the number of 1144 * available characters in @a __s, the remainder of @a __s is used. 1145 */ 1146 basic_string& 1147 assign(const _CharT* __s, size_type __n) 1148 { 1149 __glibcxx_requires_string_len(__s, __n); 1150 return _M_replace(size_type(0), this->size(), __s, __n); 1151 } 1152 1153 /** 1154 * @brief Set value to contents of a C string. 1155 * @param __s The C string to use. 1156 * @return Reference to this string. 1157 * 1158 * This function sets the value of this string to the value of @a __s. 1159 * The data is copied, so there is no dependence on @a __s once the 1160 * function returns. 1161 */ 1162 basic_string& 1163 assign(const _CharT* __s) 1164 { 1165 __glibcxx_requires_string(__s); 1166 return _M_replace(size_type(0), this->size(), __s, 1167 traits_type::length(__s)); 1168 } 1169 1170 /** 1171 * @brief Set value to multiple characters. 1172 * @param __n Length of the resulting string. 1173 * @param __c The character to use. 1174 * @return Reference to this string. 1175 * 1176 * This function sets the value of this string to @a __n copies of 1177 * character @a __c. 1178 */ 1179 basic_string& 1180 assign(size_type __n, _CharT __c) 1181 { return _M_replace_aux(size_type(0), this->size(), __n, __c); } 1182 1183 /** 1184 * @brief Set value to a range of characters. 1185 * @param __first Iterator referencing the first character to append. 1186 * @param __last Iterator marking the end of the range. 1187 * @return Reference to this string. 1188 * 1189 * Sets value of string to characters in the range [__first,__last). 1190 */ 1191#if __cplusplus >= 201103L 1192 template<class _InputIterator, 1193 typename = std::_RequireInputIter<_InputIterator>> 1194#else 1195 template<class _InputIterator> 1196#endif 1197 basic_string& 1198 assign(_InputIterator __first, _InputIterator __last) 1199 { return this->replace(begin(), end(), __first, __last); } 1200 1201#if __cplusplus >= 201103L 1202 /** 1203 * @brief Set value to an initializer_list of characters. 1204 * @param __l The initializer_list of characters to assign. 1205 * @return Reference to this string. 1206 */ 1207 basic_string& 1208 assign(initializer_list<_CharT> __l) 1209 { return this->assign(__l.begin(), __l.size()); } 1210#endif // C++11 1211 1212#if __cplusplus >= 201103L 1213 /** 1214 * @brief Insert multiple characters. 1215 * @param __p Const_iterator referencing location in string to 1216 * insert at. 1217 * @param __n Number of characters to insert 1218 * @param __c The character to insert. 1219 * @return Iterator referencing the first inserted char. 1220 * @throw std::length_error If new length exceeds @c max_size(). 1221 * 1222 * Inserts @a __n copies of character @a __c starting at the 1223 * position referenced by iterator @a __p. If adding 1224 * characters causes the length to exceed max_size(), 1225 * length_error is thrown. The value of the string doesn't 1226 * change if an error is thrown. 1227 */ 1228 iterator 1229 insert(const_iterator __p, size_type __n, _CharT __c) 1230 { 1231 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end()); 1232 const size_type __pos = __p - begin(); 1233 this->replace(__p, __p, __n, __c); 1234 return iterator(this->_M_data() + __pos); 1235 } 1236#else 1237 /** 1238 * @brief Insert multiple characters. 1239 * @param __p Iterator referencing location in string to insert at. 1240 * @param __n Number of characters to insert 1241 * @param __c The character to insert. 1242 * @throw std::length_error If new length exceeds @c max_size(). 1243 * 1244 * Inserts @a __n copies of character @a __c starting at the 1245 * position referenced by iterator @a __p. If adding 1246 * characters causes the length to exceed max_size(), 1247 * length_error is thrown. The value of the string doesn't 1248 * change if an error is thrown. 1249 */ 1250 void 1251 insert(iterator __p, size_type __n, _CharT __c) 1252 { this->replace(__p, __p, __n, __c); } 1253#endif 1254 1255#if __cplusplus >= 201103L 1256 /** 1257 * @brief Insert a range of characters. 1258 * @param __p Const_iterator referencing location in string to 1259 * insert at. 1260 * @param __beg Start of range. 1261 * @param __end End of range. 1262 * @return Iterator referencing the first inserted char. 1263 * @throw std::length_error If new length exceeds @c max_size(). 1264 * 1265 * Inserts characters in range [beg,end). If adding characters 1266 * causes the length to exceed max_size(), length_error is 1267 * thrown. The value of the string doesn't change if an error 1268 * is thrown. 1269 */ 1270 template<class _InputIterator, 1271 typename = std::_RequireInputIter<_InputIterator>> 1272 iterator 1273 insert(const_iterator __p, _InputIterator __beg, _InputIterator __end) 1274 { 1275 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end()); 1276 const size_type __pos = __p - begin(); 1277 this->replace(__p, __p, __beg, __end); 1278 return iterator(this->_M_data() + __pos); 1279 } 1280#else 1281 /** 1282 * @brief Insert a range of characters. 1283 * @param __p Iterator referencing location in string to insert at. 1284 * @param __beg Start of range. 1285 * @param __end End of range. 1286 * @throw std::length_error If new length exceeds @c max_size(). 1287 * 1288 * Inserts characters in range [__beg,__end). If adding 1289 * characters causes the length to exceed max_size(), 1290 * length_error is thrown. The value of the string doesn't 1291 * change if an error is thrown. 1292 */ 1293 template<class _InputIterator> 1294 void 1295 insert(iterator __p, _InputIterator __beg, _InputIterator __end) 1296 { this->replace(__p, __p, __beg, __end); } 1297#endif 1298 1299#if __cplusplus >= 201103L 1300 /** 1301 * @brief Insert an initializer_list of characters. 1302 * @param __p Iterator referencing location in string to insert at. 1303 * @param __l The initializer_list of characters to insert. 1304 * @throw std::length_error If new length exceeds @c max_size(). 1305 */ 1306 void 1307 insert(iterator __p, initializer_list<_CharT> __l) 1308 { 1309 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end()); 1310 this->insert(__p - begin(), __l.begin(), __l.size()); 1311 } 1312#endif // C++11 1313 1314 /** 1315 * @brief Insert value of a string. 1316 * @param __pos1 Iterator referencing location in string to insert at. 1317 * @param __str The string to insert. 1318 * @return Reference to this string. 1319 * @throw std::length_error If new length exceeds @c max_size(). 1320 * 1321 * Inserts value of @a __str starting at @a __pos1. If adding 1322 * characters causes the length to exceed max_size(), 1323 * length_error is thrown. The value of the string doesn't 1324 * change if an error is thrown. 1325 */ 1326 basic_string& 1327 insert(size_type __pos1, const basic_string& __str) 1328 { return this->replace(__pos1, size_type(0), 1329 __str._M_data(), __str.size()); } 1330 1331 /** 1332 * @brief Insert a substring. 1333 * @param __pos1 Iterator referencing location in string to insert at. 1334 * @param __str The string to insert. 1335 * @param __pos2 Start of characters in str to insert. 1336 * @param __n Number of characters to insert. 1337 * @return Reference to this string. 1338 * @throw std::length_error If new length exceeds @c max_size(). 1339 * @throw std::out_of_range If @a pos1 > size() or 1340 * @a __pos2 > @a str.size(). 1341 * 1342 * Starting at @a pos1, insert @a __n character of @a __str 1343 * beginning with @a __pos2. If adding characters causes the 1344 * length to exceed max_size(), length_error is thrown. If @a 1345 * __pos1 is beyond the end of this string or @a __pos2 is 1346 * beyond the end of @a __str, out_of_range is thrown. The 1347 * value of the string doesn't change if an error is thrown. 1348 */ 1349 basic_string& 1350 insert(size_type __pos1, const basic_string& __str, 1351 size_type __pos2, size_type __n) 1352 { return this->replace(__pos1, size_type(0), __str._M_data() 1353 + __str._M_check(__pos2, "basic_string::insert"), 1354 __str._M_limit(__pos2, __n)); } 1355 1356 /** 1357 * @brief Insert a C substring. 1358 * @param __pos Iterator referencing location in string to insert at. 1359 * @param __s The C string to insert. 1360 * @param __n The number of characters to insert. 1361 * @return Reference to this string. 1362 * @throw std::length_error If new length exceeds @c max_size(). 1363 * @throw std::out_of_range If @a __pos is beyond the end of this 1364 * string. 1365 * 1366 * Inserts the first @a __n characters of @a __s starting at @a 1367 * __pos. If adding characters causes the length to exceed 1368 * max_size(), length_error is thrown. If @a __pos is beyond 1369 * end(), out_of_range is thrown. The value of the string 1370 * doesn't change if an error is thrown. 1371 */ 1372 basic_string& 1373 insert(size_type __pos, const _CharT* __s, size_type __n) 1374 { return this->replace(__pos, size_type(0), __s, __n); } 1375 1376 /** 1377 * @brief Insert a C string. 1378 * @param __pos Iterator referencing location in string to insert at. 1379 * @param __s The C string to insert. 1380 * @return Reference to this string. 1381 * @throw std::length_error If new length exceeds @c max_size(). 1382 * @throw std::out_of_range If @a pos is beyond the end of this 1383 * string. 1384 * 1385 * Inserts the first @a n characters of @a __s starting at @a __pos. If 1386 * adding characters causes the length to exceed max_size(), 1387 * length_error is thrown. If @a __pos is beyond end(), out_of_range is 1388 * thrown. The value of the string doesn't change if an error is 1389 * thrown. 1390 */ 1391 basic_string& 1392 insert(size_type __pos, const _CharT* __s) 1393 { 1394 __glibcxx_requires_string(__s); 1395 return this->replace(__pos, size_type(0), __s, 1396 traits_type::length(__s)); 1397 } 1398 1399 /** 1400 * @brief Insert multiple characters. 1401 * @param __pos Index in string to insert at. 1402 * @param __n Number of characters to insert 1403 * @param __c The character to insert. 1404 * @return Reference to this string. 1405 * @throw std::length_error If new length exceeds @c max_size(). 1406 * @throw std::out_of_range If @a __pos is beyond the end of this 1407 * string. 1408 * 1409 * Inserts @a __n copies of character @a __c starting at index 1410 * @a __pos. If adding characters causes the length to exceed 1411 * max_size(), length_error is thrown. If @a __pos > length(), 1412 * out_of_range is thrown. The value of the string doesn't 1413 * change if an error is thrown. 1414 */ 1415 basic_string& 1416 insert(size_type __pos, size_type __n, _CharT __c) 1417 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"), 1418 size_type(0), __n, __c); } 1419 1420 /** 1421 * @brief Insert one character. 1422 * @param __p Iterator referencing position in string to insert at. 1423 * @param __c The character to insert. 1424 * @return Iterator referencing newly inserted char. 1425 * @throw std::length_error If new length exceeds @c max_size(). 1426 * 1427 * Inserts character @a __c at position referenced by @a __p. 1428 * If adding character causes the length to exceed max_size(), 1429 * length_error is thrown. If @a __p is beyond end of string, 1430 * out_of_range is thrown. The value of the string doesn't 1431 * change if an error is thrown. 1432 */ 1433 iterator 1434 insert(__const_iterator __p, _CharT __c) 1435 { 1436 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end()); 1437 const size_type __pos = __p - begin(); 1438 _M_replace_aux(__pos, size_type(0), size_type(1), __c); 1439 return iterator(_M_data() + __pos); 1440 } 1441 1442 /** 1443 * @brief Remove characters. 1444 * @param __pos Index of first character to remove (default 0). 1445 * @param __n Number of characters to remove (default remainder). 1446 * @return Reference to this string. 1447 * @throw std::out_of_range If @a pos is beyond the end of this 1448 * string. 1449 * 1450 * Removes @a __n characters from this string starting at @a 1451 * __pos. The length of the string is reduced by @a __n. If 1452 * there are < @a __n characters to remove, the remainder of 1453 * the string is truncated. If @a __p is beyond end of string, 1454 * out_of_range is thrown. The value of the string doesn't 1455 * change if an error is thrown. 1456 */ 1457 basic_string& 1458 erase(size_type __pos = 0, size_type __n = npos) 1459 { 1460 this->_M_erase(_M_check(__pos, "basic_string::erase"), 1461 _M_limit(__pos, __n)); 1462 return *this; 1463 } 1464 1465 /** 1466 * @brief Remove one character. 1467 * @param __position Iterator referencing the character to remove. 1468 * @return iterator referencing same location after removal. 1469 * 1470 * Removes the character at @a __position from this string. The value 1471 * of the string doesn't change if an error is thrown. 1472 */ 1473 iterator 1474 erase(__const_iterator __position) 1475 { 1476 _GLIBCXX_DEBUG_PEDASSERT(__position >= begin() 1477 && __position < end()); 1478 const size_type __pos = __position - begin(); 1479 this->_M_erase(__pos, size_type(1)); 1480 return iterator(_M_data() + __pos); 1481 } 1482 1483 /** 1484 * @brief Remove a range of characters. 1485 * @param __first Iterator referencing the first character to remove. 1486 * @param __last Iterator referencing the end of the range. 1487 * @return Iterator referencing location of first after removal. 1488 * 1489 * Removes the characters in the range [first,last) from this string. 1490 * The value of the string doesn't change if an error is thrown. 1491 */ 1492 iterator 1493 erase(__const_iterator __first, __const_iterator __last) 1494 { 1495 _GLIBCXX_DEBUG_PEDASSERT(__first >= begin() && __first <= __last 1496 && __last <= end()); 1497 const size_type __pos = __first - begin(); 1498 this->_M_erase(__pos, __last - __first); 1499 return iterator(this->_M_data() + __pos); 1500 } 1501 1502#if __cplusplus >= 201103L 1503 /** 1504 * @brief Remove the last character. 1505 * 1506 * The string must be non-empty. 1507 */ 1508 void 1509 pop_back() noexcept 1510 { _M_erase(size()-1, 1); } 1511#endif // C++11 1512 1513 /** 1514 * @brief Replace characters with value from another string. 1515 * @param __pos Index of first character to replace. 1516 * @param __n Number of characters to be replaced. 1517 * @param __str String to insert. 1518 * @return Reference to this string. 1519 * @throw std::out_of_range If @a pos is beyond the end of this 1520 * string. 1521 * @throw std::length_error If new length exceeds @c max_size(). 1522 * 1523 * Removes the characters in the range [__pos,__pos+__n) from 1524 * this string. In place, the value of @a __str is inserted. 1525 * If @a __pos is beyond end of string, out_of_range is thrown. 1526 * If the length of the result exceeds max_size(), length_error 1527 * is thrown. The value of the string doesn't change if an 1528 * error is thrown. 1529 */ 1530 basic_string& 1531 replace(size_type __pos, size_type __n, const basic_string& __str) 1532 { return this->replace(__pos, __n, __str._M_data(), __str.size()); } 1533 1534 /** 1535 * @brief Replace characters with value from another string. 1536 * @param __pos1 Index of first character to replace. 1537 * @param __n1 Number of characters to be replaced. 1538 * @param __str String to insert. 1539 * @param __pos2 Index of first character of str to use. 1540 * @param __n2 Number of characters from str to use. 1541 * @return Reference to this string. 1542 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 > 1543 * __str.size(). 1544 * @throw std::length_error If new length exceeds @c max_size(). 1545 * 1546 * Removes the characters in the range [__pos1,__pos1 + n) from this 1547 * string. In place, the value of @a __str is inserted. If @a __pos is 1548 * beyond end of string, out_of_range is thrown. If the length of the 1549 * result exceeds max_size(), length_error is thrown. The value of the 1550 * string doesn't change if an error is thrown. 1551 */ 1552 basic_string& 1553 replace(size_type __pos1, size_type __n1, const basic_string& __str, 1554 size_type __pos2, size_type __n2) 1555 { return this->replace(__pos1, __n1, __str._M_data() 1556 + __str._M_check(__pos2, "basic_string::replace"), 1557 __str._M_limit(__pos2, __n2)); } 1558 1559 /** 1560 * @brief Replace characters with value of a C substring. 1561 * @param __pos Index of first character to replace. 1562 * @param __n1 Number of characters to be replaced. 1563 * @param __s C string to insert. 1564 * @param __n2 Number of characters from @a s to use. 1565 * @return Reference to this string. 1566 * @throw std::out_of_range If @a pos1 > size(). 1567 * @throw std::length_error If new length exceeds @c max_size(). 1568 * 1569 * Removes the characters in the range [__pos,__pos + __n1) 1570 * from this string. In place, the first @a __n2 characters of 1571 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If 1572 * @a __pos is beyond end of string, out_of_range is thrown. If 1573 * the length of result exceeds max_size(), length_error is 1574 * thrown. The value of the string doesn't change if an error 1575 * is thrown. 1576 */ 1577 basic_string& 1578 replace(size_type __pos, size_type __n1, const _CharT* __s, 1579 size_type __n2) 1580 { 1581 __glibcxx_requires_string_len(__s, __n2); 1582 return _M_replace(_M_check(__pos, "basic_string::replace"), 1583 _M_limit(__pos, __n1), __s, __n2); 1584 } 1585 1586 /** 1587 * @brief Replace characters with value of a C string. 1588 * @param __pos Index of first character to replace. 1589 * @param __n1 Number of characters to be replaced. 1590 * @param __s C string to insert. 1591 * @return Reference to this string. 1592 * @throw std::out_of_range If @a pos > size(). 1593 * @throw std::length_error If new length exceeds @c max_size(). 1594 * 1595 * Removes the characters in the range [__pos,__pos + __n1) 1596 * from this string. In place, the characters of @a __s are 1597 * inserted. If @a __pos is beyond end of string, out_of_range 1598 * is thrown. If the length of result exceeds max_size(), 1599 * length_error is thrown. The value of the string doesn't 1600 * change if an error is thrown. 1601 */ 1602 basic_string& 1603 replace(size_type __pos, size_type __n1, const _CharT* __s) 1604 { 1605 __glibcxx_requires_string(__s); 1606 return this->replace(__pos, __n1, __s, traits_type::length(__s)); 1607 } 1608 1609 /** 1610 * @brief Replace characters with multiple characters. 1611 * @param __pos Index of first character to replace. 1612 * @param __n1 Number of characters to be replaced. 1613 * @param __n2 Number of characters to insert. 1614 * @param __c Character to insert. 1615 * @return Reference to this string. 1616 * @throw std::out_of_range If @a __pos > size(). 1617 * @throw std::length_error If new length exceeds @c max_size(). 1618 * 1619 * Removes the characters in the range [pos,pos + n1) from this 1620 * string. In place, @a __n2 copies of @a __c are inserted. 1621 * If @a __pos is beyond end of string, out_of_range is thrown. 1622 * If the length of result exceeds max_size(), length_error is 1623 * thrown. The value of the string doesn't change if an error 1624 * is thrown. 1625 */ 1626 basic_string& 1627 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c) 1628 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"), 1629 _M_limit(__pos, __n1), __n2, __c); } 1630 1631 /** 1632 * @brief Replace range of characters with string. 1633 * @param __i1 Iterator referencing start of range to replace. 1634 * @param __i2 Iterator referencing end of range to replace. 1635 * @param __str String value to insert. 1636 * @return Reference to this string. 1637 * @throw std::length_error If new length exceeds @c max_size(). 1638 * 1639 * Removes the characters in the range [__i1,__i2). In place, 1640 * the value of @a __str is inserted. If the length of result 1641 * exceeds max_size(), length_error is thrown. The value of 1642 * the string doesn't change if an error is thrown. 1643 */ 1644 basic_string& 1645 replace(__const_iterator __i1, __const_iterator __i2, 1646 const basic_string& __str) 1647 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); } 1648 1649 /** 1650 * @brief Replace range of characters with C substring. 1651 * @param __i1 Iterator referencing start of range to replace. 1652 * @param __i2 Iterator referencing end of range to replace. 1653 * @param __s C string value to insert. 1654 * @param __n Number of characters from s to insert. 1655 * @return Reference to this string. 1656 * @throw std::length_error If new length exceeds @c max_size(). 1657 * 1658 * Removes the characters in the range [__i1,__i2). In place, 1659 * the first @a __n characters of @a __s are inserted. If the 1660 * length of result exceeds max_size(), length_error is thrown. 1661 * The value of the string doesn't change if an error is 1662 * thrown. 1663 */ 1664 basic_string& 1665 replace(__const_iterator __i1, __const_iterator __i2, 1666 const _CharT* __s, size_type __n) 1667 { 1668 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 1669 && __i2 <= end()); 1670 return this->replace(__i1 - begin(), __i2 - __i1, __s, __n); 1671 } 1672 1673 /** 1674 * @brief Replace range of characters with C string. 1675 * @param __i1 Iterator referencing start of range to replace. 1676 * @param __i2 Iterator referencing end of range to replace. 1677 * @param __s C string value to insert. 1678 * @return Reference to this string. 1679 * @throw std::length_error If new length exceeds @c max_size(). 1680 * 1681 * Removes the characters in the range [__i1,__i2). In place, 1682 * the characters of @a __s are inserted. If the length of 1683 * result exceeds max_size(), length_error is thrown. The 1684 * value of the string doesn't change if an error is thrown. 1685 */ 1686 basic_string& 1687 replace(__const_iterator __i1, __const_iterator __i2, const _CharT* __s) 1688 { 1689 __glibcxx_requires_string(__s); 1690 return this->replace(__i1, __i2, __s, traits_type::length(__s)); 1691 } 1692 1693 /** 1694 * @brief Replace range of characters with multiple characters 1695 * @param __i1 Iterator referencing start of range to replace. 1696 * @param __i2 Iterator referencing end of range to replace. 1697 * @param __n Number of characters to insert. 1698 * @param __c Character to insert. 1699 * @return Reference to this string. 1700 * @throw std::length_error If new length exceeds @c max_size(). 1701 * 1702 * Removes the characters in the range [__i1,__i2). In place, 1703 * @a __n copies of @a __c are inserted. If the length of 1704 * result exceeds max_size(), length_error is thrown. The 1705 * value of the string doesn't change if an error is thrown. 1706 */ 1707 basic_string& 1708 replace(__const_iterator __i1, __const_iterator __i2, size_type __n, 1709 _CharT __c) 1710 { 1711 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 1712 && __i2 <= end()); 1713 return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __c); 1714 } 1715 1716 /** 1717 * @brief Replace range of characters with range. 1718 * @param __i1 Iterator referencing start of range to replace. 1719 * @param __i2 Iterator referencing end of range to replace. 1720 * @param __k1 Iterator referencing start of range to insert. 1721 * @param __k2 Iterator referencing end of range to insert. 1722 * @return Reference to this string. 1723 * @throw std::length_error If new length exceeds @c max_size(). 1724 * 1725 * Removes the characters in the range [__i1,__i2). In place, 1726 * characters in the range [__k1,__k2) are inserted. If the 1727 * length of result exceeds max_size(), length_error is thrown. 1728 * The value of the string doesn't change if an error is 1729 * thrown. 1730 */ 1731#if __cplusplus >= 201103L 1732 template<class _InputIterator, 1733 typename = std::_RequireInputIter<_InputIterator>> 1734 basic_string& 1735 replace(const_iterator __i1, const_iterator __i2, 1736 _InputIterator __k1, _InputIterator __k2) 1737 { 1738 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 1739 && __i2 <= end()); 1740 __glibcxx_requires_valid_range(__k1, __k2); 1741 return this->_M_replace_dispatch(__i1, __i2, __k1, __k2, 1742 std::__false_type()); 1743 } 1744#else 1745 template<class _InputIterator> 1746#ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST 1747 typename __enable_if_not_native_iterator<_InputIterator>::__type 1748#else 1749 basic_string& 1750#endif 1751 replace(iterator __i1, iterator __i2, 1752 _InputIterator __k1, _InputIterator __k2) 1753 { 1754 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 1755 && __i2 <= end()); 1756 __glibcxx_requires_valid_range(__k1, __k2); 1757 typedef typename std::__is_integer<_InputIterator>::__type _Integral; 1758 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral()); 1759 } 1760#endif 1761 1762 // Specializations for the common case of pointer and iterator: 1763 // useful to avoid the overhead of temporary buffering in _M_replace. 1764 basic_string& 1765 replace(__const_iterator __i1, __const_iterator __i2, 1766 _CharT* __k1, _CharT* __k2) 1767 { 1768 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 1769 && __i2 <= end()); 1770 __glibcxx_requires_valid_range(__k1, __k2); 1771 return this->replace(__i1 - begin(), __i2 - __i1, 1772 __k1, __k2 - __k1); 1773 } 1774 1775 basic_string& 1776 replace(__const_iterator __i1, __const_iterator __i2, 1777 const _CharT* __k1, const _CharT* __k2) 1778 { 1779 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 1780 && __i2 <= end()); 1781 __glibcxx_requires_valid_range(__k1, __k2); 1782 return this->replace(__i1 - begin(), __i2 - __i1, 1783 __k1, __k2 - __k1); 1784 } 1785 1786 basic_string& 1787 replace(__const_iterator __i1, __const_iterator __i2, 1788 iterator __k1, iterator __k2) 1789 { 1790 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 1791 && __i2 <= end()); 1792 __glibcxx_requires_valid_range(__k1, __k2); 1793 return this->replace(__i1 - begin(), __i2 - __i1, 1794 __k1.base(), __k2 - __k1); 1795 } 1796 1797 basic_string& 1798 replace(__const_iterator __i1, __const_iterator __i2, 1799 const_iterator __k1, const_iterator __k2) 1800 { 1801 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 1802 && __i2 <= end()); 1803 __glibcxx_requires_valid_range(__k1, __k2); 1804 return this->replace(__i1 - begin(), __i2 - __i1, 1805 __k1.base(), __k2 - __k1); 1806 } 1807 1808#if __cplusplus >= 201103L 1809 /** 1810 * @brief Replace range of characters with initializer_list. 1811 * @param __i1 Iterator referencing start of range to replace. 1812 * @param __i2 Iterator referencing end of range to replace. 1813 * @param __l The initializer_list of characters to insert. 1814 * @return Reference to this string. 1815 * @throw std::length_error If new length exceeds @c max_size(). 1816 * 1817 * Removes the characters in the range [__i1,__i2). In place, 1818 * characters in the range [__k1,__k2) are inserted. If the 1819 * length of result exceeds max_size(), length_error is thrown. 1820 * The value of the string doesn't change if an error is 1821 * thrown. 1822 */ 1823 basic_string& replace(const_iterator __i1, const_iterator __i2, 1824 initializer_list<_CharT> __l) 1825 { return this->replace(__i1, __i2, __l.begin(), __l.end()); } 1826#endif // C++11 1827 1828 private: 1829 template<class _Integer> 1830 basic_string& 1831 _M_replace_dispatch(const_iterator __i1, const_iterator __i2, 1832 _Integer __n, _Integer __val, __true_type) 1833 { return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __val); } 1834 1835 template<class _InputIterator> 1836 basic_string& 1837 _M_replace_dispatch(const_iterator __i1, const_iterator __i2, 1838 _InputIterator __k1, _InputIterator __k2, 1839 __false_type); 1840 1841 basic_string& 1842 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2, 1843 _CharT __c); 1844 1845 basic_string& 1846 _M_replace(size_type __pos, size_type __len1, const _CharT* __s, 1847 const size_type __len2); 1848 1849 basic_string& 1850 _M_append(const _CharT* __s, size_type __n); 1851 1852 public: 1853 1854 /** 1855 * @brief Copy substring into C string. 1856 * @param __s C string to copy value into. 1857 * @param __n Number of characters to copy. 1858 * @param __pos Index of first character to copy. 1859 * @return Number of characters actually copied 1860 * @throw std::out_of_range If __pos > size(). 1861 * 1862 * Copies up to @a __n characters starting at @a __pos into the 1863 * C string @a __s. If @a __pos is %greater than size(), 1864 * out_of_range is thrown. 1865 */ 1866 size_type 1867 copy(_CharT* __s, size_type __n, size_type __pos = 0) const; 1868 1869 /** 1870 * @brief Swap contents with another string. 1871 * @param __s String to swap with. 1872 * 1873 * Exchanges the contents of this string with that of @a __s in constant 1874 * time. 1875 */ 1876 void 1877 swap(basic_string& __s) _GLIBCXX_NOEXCEPT; 1878 1879 // String operations: 1880 /** 1881 * @brief Return const pointer to null-terminated contents. 1882 * 1883 * This is a handle to internal data. Do not modify or dire things may 1884 * happen. 1885 */ 1886 const _CharT* 1887 c_str() const _GLIBCXX_NOEXCEPT 1888 { return _M_data(); } 1889 1890 /** 1891 * @brief Return const pointer to contents. 1892 * 1893 * This is a handle to internal data. Do not modify or dire things may 1894 * happen. 1895 */ 1896 const _CharT* 1897 data() const _GLIBCXX_NOEXCEPT 1898 { return _M_data(); } 1899 1900 /** 1901 * @brief Return copy of allocator used to construct this string. 1902 */ 1903 allocator_type 1904 get_allocator() const _GLIBCXX_NOEXCEPT 1905 { return _M_get_allocator(); } 1906 1907 /** 1908 * @brief Find position of a C substring. 1909 * @param __s C string to locate. 1910 * @param __pos Index of character to search from. 1911 * @param __n Number of characters from @a s to search for. 1912 * @return Index of start of first occurrence. 1913 * 1914 * Starting from @a __pos, searches forward for the first @a 1915 * __n characters in @a __s within this string. If found, 1916 * returns the index where it begins. If not found, returns 1917 * npos. 1918 */ 1919 size_type 1920 find(const _CharT* __s, size_type __pos, size_type __n) const; 1921 1922 /** 1923 * @brief Find position of a string. 1924 * @param __str String to locate. 1925 * @param __pos Index of character to search from (default 0). 1926 * @return Index of start of first occurrence. 1927 * 1928 * Starting from @a __pos, searches forward for value of @a __str within 1929 * this string. If found, returns the index where it begins. If not 1930 * found, returns npos. 1931 */ 1932 size_type 1933 find(const basic_string& __str, size_type __pos = 0) const 1934 _GLIBCXX_NOEXCEPT 1935 { return this->find(__str.data(), __pos, __str.size()); } 1936 1937 /** 1938 * @brief Find position of a C string. 1939 * @param __s C string to locate. 1940 * @param __pos Index of character to search from (default 0). 1941 * @return Index of start of first occurrence. 1942 * 1943 * Starting from @a __pos, searches forward for the value of @a 1944 * __s within this string. If found, returns the index where 1945 * it begins. If not found, returns npos. 1946 */ 1947 size_type 1948 find(const _CharT* __s, size_type __pos = 0) const 1949 { 1950 __glibcxx_requires_string(__s); 1951 return this->find(__s, __pos, traits_type::length(__s)); 1952 } 1953 1954 /** 1955 * @brief Find position of a character. 1956 * @param __c Character to locate. 1957 * @param __pos Index of character to search from (default 0). 1958 * @return Index of first occurrence. 1959 * 1960 * Starting from @a __pos, searches forward for @a __c within 1961 * this string. If found, returns the index where it was 1962 * found. If not found, returns npos. 1963 */ 1964 size_type 1965 find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT; 1966 1967 /** 1968 * @brief Find last position of a string. 1969 * @param __str String to locate. 1970 * @param __pos Index of character to search back from (default end). 1971 * @return Index of start of last occurrence. 1972 * 1973 * Starting from @a __pos, searches backward for value of @a 1974 * __str within this string. If found, returns the index where 1975 * it begins. If not found, returns npos. 1976 */ 1977 size_type 1978 rfind(const basic_string& __str, size_type __pos = npos) const 1979 _GLIBCXX_NOEXCEPT 1980 { return this->rfind(__str.data(), __pos, __str.size()); } 1981 1982 /** 1983 * @brief Find last position of a C substring. 1984 * @param __s C string to locate. 1985 * @param __pos Index of character to search back from. 1986 * @param __n Number of characters from s to search for. 1987 * @return Index of start of last occurrence. 1988 * 1989 * Starting from @a __pos, searches backward for the first @a 1990 * __n characters in @a __s within this string. If found, 1991 * returns the index where it begins. If not found, returns 1992 * npos. 1993 */ 1994 size_type 1995 rfind(const _CharT* __s, size_type __pos, size_type __n) const; 1996 1997 /** 1998 * @brief Find last position of a C string. 1999 * @param __s C string to locate. 2000 * @param __pos Index of character to start search at (default end). 2001 * @return Index of start of last occurrence. 2002 * 2003 * Starting from @a __pos, searches backward for the value of 2004 * @a __s within this string. If found, returns the index 2005 * where it begins. If not found, returns npos. 2006 */ 2007 size_type 2008 rfind(const _CharT* __s, size_type __pos = npos) const 2009 { 2010 __glibcxx_requires_string(__s); 2011 return this->rfind(__s, __pos, traits_type::length(__s)); 2012 } 2013 2014 /** 2015 * @brief Find last position of a character. 2016 * @param __c Character to locate. 2017 * @param __pos Index of character to search back from (default end). 2018 * @return Index of last occurrence. 2019 * 2020 * Starting from @a __pos, searches backward for @a __c within 2021 * this string. If found, returns the index where it was 2022 * found. If not found, returns npos. 2023 */ 2024 size_type 2025 rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT; 2026 2027 /** 2028 * @brief Find position of a character of string. 2029 * @param __str String containing characters to locate. 2030 * @param __pos Index of character to search from (default 0). 2031 * @return Index of first occurrence. 2032 * 2033 * Starting from @a __pos, searches forward for one of the 2034 * characters of @a __str within this string. If found, 2035 * returns the index where it was found. If not found, returns 2036 * npos. 2037 */ 2038 size_type 2039 find_first_of(const basic_string& __str, size_type __pos = 0) const 2040 _GLIBCXX_NOEXCEPT 2041 { return this->find_first_of(__str.data(), __pos, __str.size()); } 2042 2043 /** 2044 * @brief Find position of a character of C substring. 2045 * @param __s String containing characters to locate. 2046 * @param __pos Index of character to search from. 2047 * @param __n Number of characters from s to search for. 2048 * @return Index of first occurrence. 2049 * 2050 * Starting from @a __pos, searches forward for one of the 2051 * first @a __n characters of @a __s within this string. If 2052 * found, returns the index where it was found. If not found, 2053 * returns npos. 2054 */ 2055 size_type 2056 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const; 2057 2058 /** 2059 * @brief Find position of a character of C string. 2060 * @param __s String containing characters to locate. 2061 * @param __pos Index of character to search from (default 0). 2062 * @return Index of first occurrence. 2063 * 2064 * Starting from @a __pos, searches forward for one of the 2065 * characters of @a __s within this string. If found, returns 2066 * the index where it was found. If not found, returns npos. 2067 */ 2068 size_type 2069 find_first_of(const _CharT* __s, size_type __pos = 0) const 2070 { 2071 __glibcxx_requires_string(__s); 2072 return this->find_first_of(__s, __pos, traits_type::length(__s)); 2073 } 2074 2075 /** 2076 * @brief Find position of a character. 2077 * @param __c Character to locate. 2078 * @param __pos Index of character to search from (default 0). 2079 * @return Index of first occurrence. 2080 * 2081 * Starting from @a __pos, searches forward for the character 2082 * @a __c within this string. If found, returns the index 2083 * where it was found. If not found, returns npos. 2084 * 2085 * Note: equivalent to find(__c, __pos). 2086 */ 2087 size_type 2088 find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT 2089 { return this->find(__c, __pos); } 2090 2091 /** 2092 * @brief Find last position of a character of string. 2093 * @param __str String containing characters to locate. 2094 * @param __pos Index of character to search back from (default end). 2095 * @return Index of last occurrence. 2096 * 2097 * Starting from @a __pos, searches backward for one of the 2098 * characters of @a __str within this string. If found, 2099 * returns the index where it was found. If not found, returns 2100 * npos. 2101 */ 2102 size_type 2103 find_last_of(const basic_string& __str, size_type __pos = npos) const 2104 _GLIBCXX_NOEXCEPT 2105 { return this->find_last_of(__str.data(), __pos, __str.size()); } 2106 2107 /** 2108 * @brief Find last position of a character of C substring. 2109 * @param __s C string containing characters to locate. 2110 * @param __pos Index of character to search back from. 2111 * @param __n Number of characters from s to search for. 2112 * @return Index of last occurrence. 2113 * 2114 * Starting from @a __pos, searches backward for one of the 2115 * first @a __n characters of @a __s within this string. If 2116 * found, returns the index where it was found. If not found, 2117 * returns npos. 2118 */ 2119 size_type 2120 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const; 2121 2122 /** 2123 * @brief Find last position of a character of C string. 2124 * @param __s C string containing characters to locate. 2125 * @param __pos Index of character to search back from (default end). 2126 * @return Index of last occurrence. 2127 * 2128 * Starting from @a __pos, searches backward for one of the 2129 * characters of @a __s within this string. If found, returns 2130 * the index where it was found. If not found, returns npos. 2131 */ 2132 size_type 2133 find_last_of(const _CharT* __s, size_type __pos = npos) const 2134 { 2135 __glibcxx_requires_string(__s); 2136 return this->find_last_of(__s, __pos, traits_type::length(__s)); 2137 } 2138 2139 /** 2140 * @brief Find last position of a character. 2141 * @param __c Character to locate. 2142 * @param __pos Index of character to search back from (default end). 2143 * @return Index of last occurrence. 2144 * 2145 * Starting from @a __pos, searches backward for @a __c within 2146 * this string. If found, returns the index where it was 2147 * found. If not found, returns npos. 2148 * 2149 * Note: equivalent to rfind(__c, __pos). 2150 */ 2151 size_type 2152 find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT 2153 { return this->rfind(__c, __pos); } 2154 2155 /** 2156 * @brief Find position of a character not in string. 2157 * @param __str String containing characters to avoid. 2158 * @param __pos Index of character to search from (default 0). 2159 * @return Index of first occurrence. 2160 * 2161 * Starting from @a __pos, searches forward for a character not contained 2162 * in @a __str within this string. If found, returns the index where it 2163 * was found. If not found, returns npos. 2164 */ 2165 size_type 2166 find_first_not_of(const basic_string& __str, size_type __pos = 0) const 2167 _GLIBCXX_NOEXCEPT 2168 { return this->find_first_not_of(__str.data(), __pos, __str.size()); } 2169 2170 /** 2171 * @brief Find position of a character not in C substring. 2172 * @param __s C string containing characters to avoid. 2173 * @param __pos Index of character to search from. 2174 * @param __n Number of characters from __s to consider. 2175 * @return Index of first occurrence. 2176 * 2177 * Starting from @a __pos, searches forward for a character not 2178 * contained in the first @a __n characters of @a __s within 2179 * this string. If found, returns the index where it was 2180 * found. If not found, returns npos. 2181 */ 2182 size_type 2183 find_first_not_of(const _CharT* __s, size_type __pos, 2184 size_type __n) const; 2185 2186 /** 2187 * @brief Find position of a character not in C string. 2188 * @param __s C string containing characters to avoid. 2189 * @param __pos Index of character to search from (default 0). 2190 * @return Index of first occurrence. 2191 * 2192 * Starting from @a __pos, searches forward for a character not 2193 * contained in @a __s within this string. If found, returns 2194 * the index where it was found. If not found, returns npos. 2195 */ 2196 size_type 2197 find_first_not_of(const _CharT* __s, size_type __pos = 0) const 2198 { 2199 __glibcxx_requires_string(__s); 2200 return this->find_first_not_of(__s, __pos, traits_type::length(__s)); 2201 } 2202 2203 /** 2204 * @brief Find position of a different character. 2205 * @param __c Character to avoid. 2206 * @param __pos Index of character to search from (default 0). 2207 * @return Index of first occurrence. 2208 * 2209 * Starting from @a __pos, searches forward for a character 2210 * other than @a __c within this string. If found, returns the 2211 * index where it was found. If not found, returns npos. 2212 */ 2213 size_type 2214 find_first_not_of(_CharT __c, size_type __pos = 0) const 2215 _GLIBCXX_NOEXCEPT; 2216 2217 /** 2218 * @brief Find last position of a character not in string. 2219 * @param __str String containing characters to avoid. 2220 * @param __pos Index of character to search back from (default end). 2221 * @return Index of last occurrence. 2222 * 2223 * Starting from @a __pos, searches backward for a character 2224 * not contained in @a __str within this string. If found, 2225 * returns the index where it was found. If not found, returns 2226 * npos. 2227 */ 2228 size_type 2229 find_last_not_of(const basic_string& __str, size_type __pos = npos) const 2230 _GLIBCXX_NOEXCEPT 2231 { return this->find_last_not_of(__str.data(), __pos, __str.size()); } 2232 2233 /** 2234 * @brief Find last position of a character not in C substring. 2235 * @param __s C string containing characters to avoid. 2236 * @param __pos Index of character to search back from. 2237 * @param __n Number of characters from s to consider. 2238 * @return Index of last occurrence. 2239 * 2240 * Starting from @a __pos, searches backward for a character not 2241 * contained in the first @a __n characters of @a __s within this string. 2242 * If found, returns the index where it was found. If not found, 2243 * returns npos. 2244 */ 2245 size_type 2246 find_last_not_of(const _CharT* __s, size_type __pos, 2247 size_type __n) const; 2248 /** 2249 * @brief Find last position of a character not in C string. 2250 * @param __s C string containing characters to avoid. 2251 * @param __pos Index of character to search back from (default end). 2252 * @return Index of last occurrence. 2253 * 2254 * Starting from @a __pos, searches backward for a character 2255 * not contained in @a __s within this string. If found, 2256 * returns the index where it was found. If not found, returns 2257 * npos. 2258 */ 2259 size_type 2260 find_last_not_of(const _CharT* __s, size_type __pos = npos) const 2261 { 2262 __glibcxx_requires_string(__s); 2263 return this->find_last_not_of(__s, __pos, traits_type::length(__s)); 2264 } 2265 2266 /** 2267 * @brief Find last position of a different character. 2268 * @param __c Character to avoid. 2269 * @param __pos Index of character to search back from (default end). 2270 * @return Index of last occurrence. 2271 * 2272 * Starting from @a __pos, searches backward for a character other than 2273 * @a __c within this string. If found, returns the index where it was 2274 * found. If not found, returns npos. 2275 */ 2276 size_type 2277 find_last_not_of(_CharT __c, size_type __pos = npos) const 2278 _GLIBCXX_NOEXCEPT; 2279 2280 /** 2281 * @brief Get a substring. 2282 * @param __pos Index of first character (default 0). 2283 * @param __n Number of characters in substring (default remainder). 2284 * @return The new string. 2285 * @throw std::out_of_range If __pos > size(). 2286 * 2287 * Construct and return a new string using the @a __n 2288 * characters starting at @a __pos. If the string is too 2289 * short, use the remainder of the characters. If @a __pos is 2290 * beyond the end of the string, out_of_range is thrown. 2291 */ 2292 basic_string 2293 substr(size_type __pos = 0, size_type __n = npos) const 2294 { return basic_string(*this, 2295 _M_check(__pos, "basic_string::substr"), __n); } 2296 2297 /** 2298 * @brief Compare to a string. 2299 * @param __str String to compare against. 2300 * @return Integer < 0, 0, or > 0. 2301 * 2302 * Returns an integer < 0 if this string is ordered before @a 2303 * __str, 0 if their values are equivalent, or > 0 if this 2304 * string is ordered after @a __str. Determines the effective 2305 * length rlen of the strings to compare as the smallest of 2306 * size() and str.size(). The function then compares the two 2307 * strings by calling traits::compare(data(), str.data(),rlen). 2308 * If the result of the comparison is nonzero returns it, 2309 * otherwise the shorter one is ordered first. 2310 */ 2311 int 2312 compare(const basic_string& __str) const 2313 { 2314 const size_type __size = this->size(); 2315 const size_type __osize = __str.size(); 2316 const size_type __len = std::min(__size, __osize); 2317 2318 int __r = traits_type::compare(_M_data(), __str.data(), __len); 2319 if (!__r) 2320 __r = _S_compare(__size, __osize); 2321 return __r; 2322 } 2323 2324 /** 2325 * @brief Compare substring to a string. 2326 * @param __pos Index of first character of substring. 2327 * @param __n Number of characters in substring. 2328 * @param __str String to compare against. 2329 * @return Integer < 0, 0, or > 0. 2330 * 2331 * Form the substring of this string from the @a __n characters 2332 * starting at @a __pos. Returns an integer < 0 if the 2333 * substring is ordered before @a __str, 0 if their values are 2334 * equivalent, or > 0 if the substring is ordered after @a 2335 * __str. Determines the effective length rlen of the strings 2336 * to compare as the smallest of the length of the substring 2337 * and @a __str.size(). The function then compares the two 2338 * strings by calling 2339 * traits::compare(substring.data(),str.data(),rlen). If the 2340 * result of the comparison is nonzero returns it, otherwise 2341 * the shorter one is ordered first. 2342 */ 2343 int 2344 compare(size_type __pos, size_type __n, const basic_string& __str) const; 2345 2346 /** 2347 * @brief Compare substring to a substring. 2348 * @param __pos1 Index of first character of substring. 2349 * @param __n1 Number of characters in substring. 2350 * @param __str String to compare against. 2351 * @param __pos2 Index of first character of substring of str. 2352 * @param __n2 Number of characters in substring of str. 2353 * @return Integer < 0, 0, or > 0. 2354 * 2355 * Form the substring of this string from the @a __n1 2356 * characters starting at @a __pos1. Form the substring of @a 2357 * __str from the @a __n2 characters starting at @a __pos2. 2358 * Returns an integer < 0 if this substring is ordered before 2359 * the substring of @a __str, 0 if their values are equivalent, 2360 * or > 0 if this substring is ordered after the substring of 2361 * @a __str. Determines the effective length rlen of the 2362 * strings to compare as the smallest of the lengths of the 2363 * substrings. The function then compares the two strings by 2364 * calling 2365 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen). 2366 * If the result of the comparison is nonzero returns it, 2367 * otherwise the shorter one is ordered first. 2368 */ 2369 int 2370 compare(size_type __pos1, size_type __n1, const basic_string& __str, 2371 size_type __pos2, size_type __n2) const; 2372 2373 /** 2374 * @brief Compare to a C string. 2375 * @param __s C string to compare against. 2376 * @return Integer < 0, 0, or > 0. 2377 * 2378 * Returns an integer < 0 if this string is ordered before @a __s, 0 if 2379 * their values are equivalent, or > 0 if this string is ordered after 2380 * @a __s. Determines the effective length rlen of the strings to 2381 * compare as the smallest of size() and the length of a string 2382 * constructed from @a __s. The function then compares the two strings 2383 * by calling traits::compare(data(),s,rlen). If the result of the 2384 * comparison is nonzero returns it, otherwise the shorter one is 2385 * ordered first. 2386 */ 2387 int 2388 compare(const _CharT* __s) const; 2389 2390 // _GLIBCXX_RESOLVE_LIB_DEFECTS 2391 // 5 String::compare specification questionable 2392 /** 2393 * @brief Compare substring to a C string. 2394 * @param __pos Index of first character of substring. 2395 * @param __n1 Number of characters in substring. 2396 * @param __s C string to compare against. 2397 * @return Integer < 0, 0, or > 0. 2398 * 2399 * Form the substring of this string from the @a __n1 2400 * characters starting at @a pos. Returns an integer < 0 if 2401 * the substring is ordered before @a __s, 0 if their values 2402 * are equivalent, or > 0 if the substring is ordered after @a 2403 * __s. Determines the effective length rlen of the strings to 2404 * compare as the smallest of the length of the substring and 2405 * the length of a string constructed from @a __s. The 2406 * function then compares the two string by calling 2407 * traits::compare(substring.data(),__s,rlen). If the result of 2408 * the comparison is nonzero returns it, otherwise the shorter 2409 * one is ordered first. 2410 */ 2411 int 2412 compare(size_type __pos, size_type __n1, const _CharT* __s) const; 2413 2414 /** 2415 * @brief Compare substring against a character %array. 2416 * @param __pos Index of first character of substring. 2417 * @param __n1 Number of characters in substring. 2418 * @param __s character %array to compare against. 2419 * @param __n2 Number of characters of s. 2420 * @return Integer < 0, 0, or > 0. 2421 * 2422 * Form the substring of this string from the @a __n1 2423 * characters starting at @a __pos. Form a string from the 2424 * first @a __n2 characters of @a __s. Returns an integer < 0 2425 * if this substring is ordered before the string from @a __s, 2426 * 0 if their values are equivalent, or > 0 if this substring 2427 * is ordered after the string from @a __s. Determines the 2428 * effective length rlen of the strings to compare as the 2429 * smallest of the length of the substring and @a __n2. The 2430 * function then compares the two strings by calling 2431 * traits::compare(substring.data(),s,rlen). If the result of 2432 * the comparison is nonzero returns it, otherwise the shorter 2433 * one is ordered first. 2434 * 2435 * NB: s must have at least n2 characters, '\\0' has 2436 * no special meaning. 2437 */ 2438 int 2439 compare(size_type __pos, size_type __n1, const _CharT* __s, 2440 size_type __n2) const; 2441 }; 2442_GLIBCXX_END_NAMESPACE_CXX11 2443#else // !_GLIBCXX_USE_CXX11_ABI 2444 // Reference-counted COW string implentation 2445 2446 /** 2447 * @class basic_string basic_string.h <string> 2448 * @brief Managing sequences of characters and character-like objects. 2449 * 2450 * @ingroup strings 2451 * @ingroup sequences 2452 * 2453 * @tparam _CharT Type of character 2454 * @tparam _Traits Traits for character type, defaults to 2455 * char_traits<_CharT>. 2456 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>. 2457 * 2458 * Meets the requirements of a <a href="tables.html#65">container</a>, a 2459 * <a href="tables.html#66">reversible container</a>, and a 2460 * <a href="tables.html#67">sequence</a>. Of the 2461 * <a href="tables.html#68">optional sequence requirements</a>, only 2462 * @c push_back, @c at, and @c %array access are supported. 2463 * 2464 * @doctodo 2465 * 2466 * 2467 * Documentation? What's that? 2468 * Nathan Myers <ncm@cantrip.org>. 2469 * 2470 * A string looks like this: 2471 * 2472 * @code 2473 * [_Rep] 2474 * _M_length 2475 * [basic_string<char_type>] _M_capacity 2476 * _M_dataplus _M_refcount 2477 * _M_p ----------------> unnamed array of char_type 2478 * @endcode 2479 * 2480 * Where the _M_p points to the first character in the string, and 2481 * you cast it to a pointer-to-_Rep and subtract 1 to get a 2482 * pointer to the header. 2483 * 2484 * This approach has the enormous advantage that a string object 2485 * requires only one allocation. All the ugliness is confined 2486 * within a single %pair of inline functions, which each compile to 2487 * a single @a add instruction: _Rep::_M_data(), and 2488 * string::_M_rep(); and the allocation function which gets a 2489 * block of raw bytes and with room enough and constructs a _Rep 2490 * object at the front. 2491 * 2492 * The reason you want _M_data pointing to the character %array and 2493 * not the _Rep is so that the debugger can see the string 2494 * contents. (Probably we should add a non-inline member to get 2495 * the _Rep for the debugger to use, so users can check the actual 2496 * string length.) 2497 * 2498 * Note that the _Rep object is a POD so that you can have a 2499 * static <em>empty string</em> _Rep object already @a constructed before 2500 * static constructors have run. The reference-count encoding is 2501 * chosen so that a 0 indicates one reference, so you never try to 2502 * destroy the empty-string _Rep object. 2503 * 2504 * All but the last paragraph is considered pretty conventional 2505 * for a C++ string implementation. 2506 */ 2507 // 21.3 Template class basic_string 2508 template<typename _CharT, typename _Traits, typename _Alloc> 2509 class basic_string 2510 { 2511 typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type; 2512 2513 // Types: 2514 public: 2515 typedef _Traits traits_type; 2516 typedef typename _Traits::char_type value_type; 2517 typedef _Alloc allocator_type; 2518 typedef typename _CharT_alloc_type::size_type size_type; 2519 typedef typename _CharT_alloc_type::difference_type difference_type; 2520 typedef typename _CharT_alloc_type::reference reference; 2521 typedef typename _CharT_alloc_type::const_reference const_reference; 2522 typedef typename _CharT_alloc_type::pointer pointer; 2523 typedef typename _CharT_alloc_type::const_pointer const_pointer; 2524 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator; 2525 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string> 2526 const_iterator; 2527 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 2528 typedef std::reverse_iterator<iterator> reverse_iterator; 2529 2530 private: 2531 // _Rep: string representation 2532 // Invariants: 2533 // 1. String really contains _M_length + 1 characters: due to 21.3.4 2534 // must be kept null-terminated. 2535 // 2. _M_capacity >= _M_length 2536 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT). 2537 // 3. _M_refcount has three states: 2538 // -1: leaked, one reference, no ref-copies allowed, non-const. 2539 // 0: one reference, non-const. 2540 // n>0: n + 1 references, operations require a lock, const. 2541 // 4. All fields==0 is an empty string, given the extra storage 2542 // beyond-the-end for a null terminator; thus, the shared 2543 // empty string representation needs no constructor. 2544 2545 struct _Rep_base 2546 { 2547 size_type _M_length; 2548 size_type _M_capacity; 2549 _Atomic_word _M_refcount; 2550 }; 2551 2552 struct _Rep : _Rep_base 2553 { 2554 // Types: 2555 typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc; 2556 2557 // (Public) Data members: 2558 2559 // The maximum number of individual char_type elements of an 2560 // individual string is determined by _S_max_size. This is the 2561 // value that will be returned by max_size(). (Whereas npos 2562 // is the maximum number of bytes the allocator can allocate.) 2563 // If one was to divvy up the theoretical largest size string, 2564 // with a terminating character and m _CharT elements, it'd 2565 // look like this: 2566 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT) 2567 // Solving for m: 2568 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1 2569 // In addition, this implementation quarters this amount. 2570 static const size_type _S_max_size; 2571 static const _CharT _S_terminal; 2572 2573 // The following storage is init'd to 0 by the linker, resulting 2574 // (carefully) in an empty string with one reference. 2575 static size_type _S_empty_rep_storage[]; 2576 2577 static _Rep& 2578 _S_empty_rep() _GLIBCXX_NOEXCEPT 2579 { 2580 // NB: Mild hack to avoid strict-aliasing warnings. Note that 2581 // _S_empty_rep_storage is never modified and the punning should 2582 // be reasonably safe in this case. 2583 void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage); 2584 return *reinterpret_cast<_Rep*>(__p); 2585 } 2586 2587 bool 2588 _M_is_leaked() const _GLIBCXX_NOEXCEPT 2589 { return this->_M_refcount < 0; } 2590 2591 bool 2592 _M_is_shared() const _GLIBCXX_NOEXCEPT 2593 { return this->_M_refcount > 0; } 2594 2595 void 2596 _M_set_leaked() _GLIBCXX_NOEXCEPT 2597 { this->_M_refcount = -1; } 2598 2599 void 2600 _M_set_sharable() _GLIBCXX_NOEXCEPT 2601 { this->_M_refcount = 0; } 2602 2603 void 2604 _M_set_length_and_sharable(size_type __n) _GLIBCXX_NOEXCEPT 2605 { 2606#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 2607 if (__builtin_expect(this != &_S_empty_rep(), false)) 2608#endif 2609 { 2610 this->_M_set_sharable(); // One reference. 2611 this->_M_length = __n; 2612 traits_type::assign(this->_M_refdata()[__n], _S_terminal); 2613 // grrr. (per 21.3.4) 2614 // You cannot leave those LWG people alone for a second. 2615 } 2616 } 2617 2618 _CharT* 2619 _M_refdata() throw() 2620 { return reinterpret_cast<_CharT*>(this + 1); } 2621 2622 _CharT* 2623 _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2) 2624 { 2625 return (!_M_is_leaked() && __alloc1 == __alloc2) 2626 ? _M_refcopy() : _M_clone(__alloc1); 2627 } 2628 2629 // Create & Destroy 2630 static _Rep* 2631 _S_create(size_type, size_type, const _Alloc&); 2632 2633 void 2634 _M_dispose(const _Alloc& __a) _GLIBCXX_NOEXCEPT 2635 { 2636#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 2637 if (__builtin_expect(this != &_S_empty_rep(), false)) 2638#endif 2639 { 2640 // Be race-detector-friendly. For more info see bits/c++config. 2641 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount); 2642 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount, 2643 -1) <= 0) 2644 { 2645 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount); 2646 _M_destroy(__a); 2647 } 2648 } 2649 } // XXX MT 2650 2651 void 2652 _M_destroy(const _Alloc&) throw(); 2653 2654 _CharT* 2655 _M_refcopy() throw() 2656 { 2657#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 2658 if (__builtin_expect(this != &_S_empty_rep(), false)) 2659#endif 2660 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1); 2661 return _M_refdata(); 2662 } // XXX MT 2663 2664 _CharT* 2665 _M_clone(const _Alloc&, size_type __res = 0); 2666 }; 2667 2668 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html 2669 struct _Alloc_hider : _Alloc 2670 { 2671 _Alloc_hider(_CharT* __dat, const _Alloc& __a) _GLIBCXX_NOEXCEPT 2672 : _Alloc(__a), _M_p(__dat) { } 2673 2674 _CharT* _M_p; // The actual data. 2675 }; 2676 2677 public: 2678 // Data Members (public): 2679 // NB: This is an unsigned type, and thus represents the maximum 2680 // size that the allocator can hold. 2681 /// Value returned by various member functions when they fail. 2682 static const size_type npos = static_cast<size_type>(-1); 2683 2684 private: 2685 // Data Members (private): 2686 mutable _Alloc_hider _M_dataplus; 2687 2688 _CharT* 2689 _M_data() const _GLIBCXX_NOEXCEPT 2690 { return _M_dataplus._M_p; } 2691 2692 _CharT* 2693 _M_data(_CharT* __p) _GLIBCXX_NOEXCEPT 2694 { return (_M_dataplus._M_p = __p); } 2695 2696 _Rep* 2697 _M_rep() const _GLIBCXX_NOEXCEPT 2698 { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); } 2699 2700 // For the internal use we have functions similar to `begin'/`end' 2701 // but they do not call _M_leak. 2702 iterator 2703 _M_ibegin() const _GLIBCXX_NOEXCEPT 2704 { return iterator(_M_data()); } 2705 2706 iterator 2707 _M_iend() const _GLIBCXX_NOEXCEPT 2708 { return iterator(_M_data() + this->size()); } 2709 2710 void 2711 _M_leak() // for use in begin() & non-const op[] 2712 { 2713 if (!_M_rep()->_M_is_leaked()) 2714 _M_leak_hard(); 2715 } 2716 2717 size_type 2718 _M_check(size_type __pos, const char* __s) const 2719 { 2720 if (__pos > this->size()) 2721 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > " 2722 "this->size() (which is %zu)"), 2723 __s, __pos, this->size()); 2724 return __pos; 2725 } 2726 2727 void 2728 _M_check_length(size_type __n1, size_type __n2, const char* __s) const 2729 { 2730 if (this->max_size() - (this->size() - __n1) < __n2) 2731 __throw_length_error(__N(__s)); 2732 } 2733 2734 // NB: _M_limit doesn't check for a bad __pos value. 2735 size_type 2736 _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT 2737 { 2738 const bool __testoff = __off < this->size() - __pos; 2739 return __testoff ? __off : this->size() - __pos; 2740 } 2741 2742 // True if _Rep and source do not overlap. 2743 bool 2744 _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT 2745 { 2746 return (less<const _CharT*>()(__s, _M_data()) 2747 || less<const _CharT*>()(_M_data() + this->size(), __s)); 2748 } 2749 2750 // When __n = 1 way faster than the general multichar 2751 // traits_type::copy/move/assign. 2752 static void 2753 _M_copy(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT 2754 { 2755 if (__n == 1) 2756 traits_type::assign(*__d, *__s); 2757 else 2758 traits_type::copy(__d, __s, __n); 2759 } 2760 2761 static void 2762 _M_move(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT 2763 { 2764 if (__n == 1) 2765 traits_type::assign(*__d, *__s); 2766 else 2767 traits_type::move(__d, __s, __n); 2768 } 2769 2770 static void 2771 _M_assign(_CharT* __d, size_type __n, _CharT __c) _GLIBCXX_NOEXCEPT 2772 { 2773 if (__n == 1) 2774 traits_type::assign(*__d, __c); 2775 else 2776 traits_type::assign(__d, __n, __c); 2777 } 2778 2779 // _S_copy_chars is a separate template to permit specialization 2780 // to optimize for the common case of pointers as iterators. 2781 template<class _Iterator> 2782 static void 2783 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2) 2784 { 2785 for (; __k1 != __k2; ++__k1, ++__p) 2786 traits_type::assign(*__p, *__k1); // These types are off. 2787 } 2788 2789 static void 2790 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT 2791 { _S_copy_chars(__p, __k1.base(), __k2.base()); } 2792 2793 static void 2794 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2) 2795 _GLIBCXX_NOEXCEPT 2796 { _S_copy_chars(__p, __k1.base(), __k2.base()); } 2797 2798 static void 2799 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT 2800 { _M_copy(__p, __k1, __k2 - __k1); } 2801 2802 static void 2803 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2) 2804 _GLIBCXX_NOEXCEPT 2805 { _M_copy(__p, __k1, __k2 - __k1); } 2806 2807 static int 2808 _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT 2809 { 2810 const difference_type __d = difference_type(__n1 - __n2); 2811 2812 if (__d > __gnu_cxx::__numeric_traits<int>::__max) 2813 return __gnu_cxx::__numeric_traits<int>::__max; 2814 else if (__d < __gnu_cxx::__numeric_traits<int>::__min) 2815 return __gnu_cxx::__numeric_traits<int>::__min; 2816 else 2817 return int(__d); 2818 } 2819 2820 void 2821 _M_mutate(size_type __pos, size_type __len1, size_type __len2); 2822 2823 void 2824 _M_leak_hard(); 2825 2826 static _Rep& 2827 _S_empty_rep() _GLIBCXX_NOEXCEPT 2828 { return _Rep::_S_empty_rep(); } 2829 2830 public: 2831 // Construct/copy/destroy: 2832 // NB: We overload ctors in some cases instead of using default 2833 // arguments, per 17.4.4.4 para. 2 item 2. 2834 2835 /** 2836 * @brief Default constructor creates an empty string. 2837 */ 2838 basic_string() 2839#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 2840 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { } 2841#else 2842 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()){ } 2843#endif 2844 2845 /** 2846 * @brief Construct an empty string using allocator @a a. 2847 */ 2848 explicit 2849 basic_string(const _Alloc& __a); 2850 2851 // NB: per LWG issue 42, semantics different from IS: 2852 /** 2853 * @brief Construct string with copy of value of @a str. 2854 * @param __str Source string. 2855 */ 2856 basic_string(const basic_string& __str); 2857 /** 2858 * @brief Construct string as copy of a substring. 2859 * @param __str Source string. 2860 * @param __pos Index of first character to copy from. 2861 * @param __n Number of characters to copy (default remainder). 2862 */ 2863 basic_string(const basic_string& __str, size_type __pos, 2864 size_type __n = npos); 2865 /** 2866 * @brief Construct string as copy of a substring. 2867 * @param __str Source string. 2868 * @param __pos Index of first character to copy from. 2869 * @param __n Number of characters to copy. 2870 * @param __a Allocator to use. 2871 */ 2872 basic_string(const basic_string& __str, size_type __pos, 2873 size_type __n, const _Alloc& __a); 2874 2875 /** 2876 * @brief Construct string initialized by a character %array. 2877 * @param __s Source character %array. 2878 * @param __n Number of characters to copy. 2879 * @param __a Allocator to use (default is default allocator). 2880 * 2881 * NB: @a __s must have at least @a __n characters, '\\0' 2882 * has no special meaning. 2883 */ 2884 basic_string(const _CharT* __s, size_type __n, 2885 const _Alloc& __a = _Alloc()); 2886 /** 2887 * @brief Construct string as copy of a C string. 2888 * @param __s Source C string. 2889 * @param __a Allocator to use (default is default allocator). 2890 */ 2891 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc()); 2892 /** 2893 * @brief Construct string as multiple characters. 2894 * @param __n Number of characters. 2895 * @param __c Character to use. 2896 * @param __a Allocator to use (default is default allocator). 2897 */ 2898 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc()); 2899 2900#if __cplusplus >= 201103L 2901 /** 2902 * @brief Move construct string. 2903 * @param __str Source string. 2904 * 2905 * The newly-created string contains the exact contents of @a __str. 2906 * @a __str is a valid, but unspecified string. 2907 **/ 2908 basic_string(basic_string&& __str) 2909#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 2910 noexcept // FIXME C++11: should always be noexcept. 2911#endif 2912 : _M_dataplus(__str._M_dataplus) 2913 { 2914#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 2915 __str._M_data(_S_empty_rep()._M_refdata()); 2916#else 2917 __str._M_data(_S_construct(size_type(), _CharT(), get_allocator())); 2918#endif 2919 } 2920 2921 /** 2922 * @brief Construct string from an initializer %list. 2923 * @param __l std::initializer_list of characters. 2924 * @param __a Allocator to use (default is default allocator). 2925 */ 2926 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc()); 2927#endif // C++11 2928 2929 /** 2930 * @brief Construct string as copy of a range. 2931 * @param __beg Start of range. 2932 * @param __end End of range. 2933 * @param __a Allocator to use (default is default allocator). 2934 */ 2935 template<class _InputIterator> 2936 basic_string(_InputIterator __beg, _InputIterator __end, 2937 const _Alloc& __a = _Alloc()); 2938 2939 /** 2940 * @brief Destroy the string instance. 2941 */ 2942 ~basic_string() _GLIBCXX_NOEXCEPT 2943 { _M_rep()->_M_dispose(this->get_allocator()); } 2944 2945 /** 2946 * @brief Assign the value of @a str to this string. 2947 * @param __str Source string. 2948 */ 2949 basic_string& 2950 operator=(const basic_string& __str) 2951 { return this->assign(__str); } 2952 2953 /** 2954 * @brief Copy contents of @a s into this string. 2955 * @param __s Source null-terminated string. 2956 */ 2957 basic_string& 2958 operator=(const _CharT* __s) 2959 { return this->assign(__s); } 2960 2961 /** 2962 * @brief Set value to string of length 1. 2963 * @param __c Source character. 2964 * 2965 * Assigning to a character makes this string length 1 and 2966 * (*this)[0] == @a c. 2967 */ 2968 basic_string& 2969 operator=(_CharT __c) 2970 { 2971 this->assign(1, __c); 2972 return *this; 2973 } 2974 2975#if __cplusplus >= 201103L 2976 /** 2977 * @brief Move assign the value of @a str to this string. 2978 * @param __str Source string. 2979 * 2980 * The contents of @a str are moved into this string (without copying). 2981 * @a str is a valid, but unspecified string. 2982 **/ 2983 // PR 58265, this should be noexcept. 2984 basic_string& 2985 operator=(basic_string&& __str) 2986 { 2987 // NB: DR 1204. 2988 this->swap(__str); 2989 return *this; 2990 } 2991 2992 /** 2993 * @brief Set value to string constructed from initializer %list. 2994 * @param __l std::initializer_list. 2995 */ 2996 basic_string& 2997 operator=(initializer_list<_CharT> __l) 2998 { 2999 this->assign(__l.begin(), __l.size()); 3000 return *this; 3001 } 3002#endif // C++11 3003 3004 // Iterators: 3005 /** 3006 * Returns a read/write iterator that points to the first character in 3007 * the %string. Unshares the string. 3008 */ 3009 iterator 3010 begin() // FIXME C++11: should be noexcept. 3011 { 3012 _M_leak(); 3013 return iterator(_M_data()); 3014 } 3015 3016 /** 3017 * Returns a read-only (constant) iterator that points to the first 3018 * character in the %string. 3019 */ 3020 const_iterator 3021 begin() const _GLIBCXX_NOEXCEPT 3022 { return const_iterator(_M_data()); } 3023 3024 /** 3025 * Returns a read/write iterator that points one past the last 3026 * character in the %string. Unshares the string. 3027 */ 3028 iterator 3029 end() // FIXME C++11: should be noexcept. 3030 { 3031 _M_leak(); 3032 return iterator(_M_data() + this->size()); 3033 } 3034 3035 /** 3036 * Returns a read-only (constant) iterator that points one past the 3037 * last character in the %string. 3038 */ 3039 const_iterator 3040 end() const _GLIBCXX_NOEXCEPT 3041 { return const_iterator(_M_data() + this->size()); } 3042 3043 /** 3044 * Returns a read/write reverse iterator that points to the last 3045 * character in the %string. Iteration is done in reverse element 3046 * order. Unshares the string. 3047 */ 3048 reverse_iterator 3049 rbegin() // FIXME C++11: should be noexcept. 3050 { return reverse_iterator(this->end()); } 3051 3052 /** 3053 * Returns a read-only (constant) reverse iterator that points 3054 * to the last character in the %string. Iteration is done in 3055 * reverse element order. 3056 */ 3057 const_reverse_iterator 3058 rbegin() const _GLIBCXX_NOEXCEPT 3059 { return const_reverse_iterator(this->end()); } 3060 3061 /** 3062 * Returns a read/write reverse iterator that points to one before the 3063 * first character in the %string. Iteration is done in reverse 3064 * element order. Unshares the string. 3065 */ 3066 reverse_iterator 3067 rend() // FIXME C++11: should be noexcept. 3068 { return reverse_iterator(this->begin()); } 3069 3070 /** 3071 * Returns a read-only (constant) reverse iterator that points 3072 * to one before the first character in the %string. Iteration 3073 * is done in reverse element order. 3074 */ 3075 const_reverse_iterator 3076 rend() const _GLIBCXX_NOEXCEPT 3077 { return const_reverse_iterator(this->begin()); } 3078 3079#if __cplusplus >= 201103L 3080 /** 3081 * Returns a read-only (constant) iterator that points to the first 3082 * character in the %string. 3083 */ 3084 const_iterator 3085 cbegin() const noexcept 3086 { return const_iterator(this->_M_data()); } 3087 3088 /** 3089 * Returns a read-only (constant) iterator that points one past the 3090 * last character in the %string. 3091 */ 3092 const_iterator 3093 cend() const noexcept 3094 { return const_iterator(this->_M_data() + this->size()); } 3095 3096 /** 3097 * Returns a read-only (constant) reverse iterator that points 3098 * to the last character in the %string. Iteration is done in 3099 * reverse element order. 3100 */ 3101 const_reverse_iterator 3102 crbegin() const noexcept 3103 { return const_reverse_iterator(this->end()); } 3104 3105 /** 3106 * Returns a read-only (constant) reverse iterator that points 3107 * to one before the first character in the %string. Iteration 3108 * is done in reverse element order. 3109 */ 3110 const_reverse_iterator 3111 crend() const noexcept 3112 { return const_reverse_iterator(this->begin()); } 3113#endif 3114 3115 public: 3116 // Capacity: 3117 /// Returns the number of characters in the string, not including any 3118 /// null-termination. 3119 size_type 3120 size() const _GLIBCXX_NOEXCEPT 3121 { return _M_rep()->_M_length; } 3122 3123 /// Returns the number of characters in the string, not including any 3124 /// null-termination. 3125 size_type 3126 length() const _GLIBCXX_NOEXCEPT 3127 { return _M_rep()->_M_length; } 3128 3129 /// Returns the size() of the largest possible %string. 3130 size_type 3131 max_size() const _GLIBCXX_NOEXCEPT 3132 { return _Rep::_S_max_size; } 3133 3134 /** 3135 * @brief Resizes the %string to the specified number of characters. 3136 * @param __n Number of characters the %string should contain. 3137 * @param __c Character to fill any new elements. 3138 * 3139 * This function will %resize the %string to the specified 3140 * number of characters. If the number is smaller than the 3141 * %string's current size the %string is truncated, otherwise 3142 * the %string is extended and new elements are %set to @a __c. 3143 */ 3144 void 3145 resize(size_type __n, _CharT __c); 3146 3147 /** 3148 * @brief Resizes the %string to the specified number of characters. 3149 * @param __n Number of characters the %string should contain. 3150 * 3151 * This function will resize the %string to the specified length. If 3152 * the new size is smaller than the %string's current size the %string 3153 * is truncated, otherwise the %string is extended and new characters 3154 * are default-constructed. For basic types such as char, this means 3155 * setting them to 0. 3156 */ 3157 void 3158 resize(size_type __n) 3159 { this->resize(__n, _CharT()); } 3160 3161#if __cplusplus >= 201103L 3162 /// A non-binding request to reduce capacity() to size(). 3163 void 3164 shrink_to_fit() _GLIBCXX_NOEXCEPT 3165 { 3166#if __cpp_exceptions 3167 if (capacity() > size()) 3168 { 3169 try 3170 { reserve(0); } 3171 catch(...) 3172 { } 3173 } 3174#endif 3175 } 3176#endif 3177 3178 /** 3179 * Returns the total number of characters that the %string can hold 3180 * before needing to allocate more memory. 3181 */ 3182 size_type 3183 capacity() const _GLIBCXX_NOEXCEPT 3184 { return _M_rep()->_M_capacity; } 3185 3186 /** 3187 * @brief Attempt to preallocate enough memory for specified number of 3188 * characters. 3189 * @param __res_arg Number of characters required. 3190 * @throw std::length_error If @a __res_arg exceeds @c max_size(). 3191 * 3192 * This function attempts to reserve enough memory for the 3193 * %string to hold the specified number of characters. If the 3194 * number requested is more than max_size(), length_error is 3195 * thrown. 3196 * 3197 * The advantage of this function is that if optimal code is a 3198 * necessity and the user can determine the string length that will be 3199 * required, the user can reserve the memory in %advance, and thus 3200 * prevent a possible reallocation of memory and copying of %string 3201 * data. 3202 */ 3203 void 3204 reserve(size_type __res_arg = 0); 3205 3206 /** 3207 * Erases the string, making it empty. 3208 */ 3209 // PR 56166: this should not throw. 3210 void 3211 clear() 3212 { _M_mutate(0, this->size(), 0); } 3213 3214 /** 3215 * Returns true if the %string is empty. Equivalent to 3216 * <code>*this == ""</code>. 3217 */ 3218 bool 3219 empty() const _GLIBCXX_NOEXCEPT 3220 { return this->size() == 0; } 3221 3222 // Element access: 3223 /** 3224 * @brief Subscript access to the data contained in the %string. 3225 * @param __pos The index of the character to access. 3226 * @return Read-only (constant) reference to the character. 3227 * 3228 * This operator allows for easy, array-style, data access. 3229 * Note that data access with this operator is unchecked and 3230 * out_of_range lookups are not defined. (For checked lookups 3231 * see at().) 3232 */ 3233 const_reference 3234 operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT 3235 { 3236 _GLIBCXX_DEBUG_ASSERT(__pos <= size()); 3237 return _M_data()[__pos]; 3238 } 3239 3240 /** 3241 * @brief Subscript access to the data contained in the %string. 3242 * @param __pos The index of the character to access. 3243 * @return Read/write reference to the character. 3244 * 3245 * This operator allows for easy, array-style, data access. 3246 * Note that data access with this operator is unchecked and 3247 * out_of_range lookups are not defined. (For checked lookups 3248 * see at().) Unshares the string. 3249 */ 3250 reference 3251 operator[](size_type __pos) 3252 { 3253 // Allow pos == size() both in C++98 mode, as v3 extension, 3254 // and in C++11 mode. 3255 _GLIBCXX_DEBUG_ASSERT(__pos <= size()); 3256 // In pedantic mode be strict in C++98 mode. 3257 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size()); 3258 _M_leak(); 3259 return _M_data()[__pos]; 3260 } 3261 3262 /** 3263 * @brief Provides access to the data contained in the %string. 3264 * @param __n The index of the character to access. 3265 * @return Read-only (const) reference to the character. 3266 * @throw std::out_of_range If @a n is an invalid index. 3267 * 3268 * This function provides for safer data access. The parameter is 3269 * first checked that it is in the range of the string. The function 3270 * throws out_of_range if the check fails. 3271 */ 3272 const_reference 3273 at(size_type __n) const 3274 { 3275 if (__n >= this->size()) 3276 __throw_out_of_range_fmt(__N("basic_string::at: __n " 3277 "(which is %zu) >= this->size() " 3278 "(which is %zu)"), 3279 __n, this->size()); 3280 return _M_data()[__n]; 3281 } 3282 3283 /** 3284 * @brief Provides access to the data contained in the %string. 3285 * @param __n The index of the character to access. 3286 * @return Read/write reference to the character. 3287 * @throw std::out_of_range If @a n is an invalid index. 3288 * 3289 * This function provides for safer data access. The parameter is 3290 * first checked that it is in the range of the string. The function 3291 * throws out_of_range if the check fails. Success results in 3292 * unsharing the string. 3293 */ 3294 reference 3295 at(size_type __n) 3296 { 3297 if (__n >= size()) 3298 __throw_out_of_range_fmt(__N("basic_string::at: __n " 3299 "(which is %zu) >= this->size() " 3300 "(which is %zu)"), 3301 __n, this->size()); 3302 _M_leak(); 3303 return _M_data()[__n]; 3304 } 3305 3306#if __cplusplus >= 201103L 3307 /** 3308 * Returns a read/write reference to the data at the first 3309 * element of the %string. 3310 */ 3311 reference 3312 front() 3313 { return operator[](0); } 3314 3315 /** 3316 * Returns a read-only (constant) reference to the data at the first 3317 * element of the %string. 3318 */ 3319 const_reference 3320 front() const _GLIBCXX_NOEXCEPT 3321 { return operator[](0); } 3322 3323 /** 3324 * Returns a read/write reference to the data at the last 3325 * element of the %string. 3326 */ 3327 reference 3328 back() 3329 { return operator[](this->size() - 1); } 3330 3331 /** 3332 * Returns a read-only (constant) reference to the data at the 3333 * last element of the %string. 3334 */ 3335 const_reference 3336 back() const _GLIBCXX_NOEXCEPT 3337 { return operator[](this->size() - 1); } 3338#endif 3339 3340 // Modifiers: 3341 /** 3342 * @brief Append a string to this string. 3343 * @param __str The string to append. 3344 * @return Reference to this string. 3345 */ 3346 basic_string& 3347 operator+=(const basic_string& __str) 3348 { return this->append(__str); } 3349 3350 /** 3351 * @brief Append a C string. 3352 * @param __s The C string to append. 3353 * @return Reference to this string. 3354 */ 3355 basic_string& 3356 operator+=(const _CharT* __s) 3357 { return this->append(__s); } 3358 3359 /** 3360 * @brief Append a character. 3361 * @param __c The character to append. 3362 * @return Reference to this string. 3363 */ 3364 basic_string& 3365 operator+=(_CharT __c) 3366 { 3367 this->push_back(__c); 3368 return *this; 3369 } 3370 3371#if __cplusplus >= 201103L 3372 /** 3373 * @brief Append an initializer_list of characters. 3374 * @param __l The initializer_list of characters to be appended. 3375 * @return Reference to this string. 3376 */ 3377 basic_string& 3378 operator+=(initializer_list<_CharT> __l) 3379 { return this->append(__l.begin(), __l.size()); } 3380#endif // C++11 3381 3382 /** 3383 * @brief Append a string to this string. 3384 * @param __str The string to append. 3385 * @return Reference to this string. 3386 */ 3387 basic_string& 3388 append(const basic_string& __str); 3389 3390 /** 3391 * @brief Append a substring. 3392 * @param __str The string to append. 3393 * @param __pos Index of the first character of str to append. 3394 * @param __n The number of characters to append. 3395 * @return Reference to this string. 3396 * @throw std::out_of_range if @a __pos is not a valid index. 3397 * 3398 * This function appends @a __n characters from @a __str 3399 * starting at @a __pos to this string. If @a __n is is larger 3400 * than the number of available characters in @a __str, the 3401 * remainder of @a __str is appended. 3402 */ 3403 basic_string& 3404 append(const basic_string& __str, size_type __pos, size_type __n); 3405 3406 /** 3407 * @brief Append a C substring. 3408 * @param __s The C string to append. 3409 * @param __n The number of characters to append. 3410 * @return Reference to this string. 3411 */ 3412 basic_string& 3413 append(const _CharT* __s, size_type __n); 3414 3415 /** 3416 * @brief Append a C string. 3417 * @param __s The C string to append. 3418 * @return Reference to this string. 3419 */ 3420 basic_string& 3421 append(const _CharT* __s) 3422 { 3423 __glibcxx_requires_string(__s); 3424 return this->append(__s, traits_type::length(__s)); 3425 } 3426 3427 /** 3428 * @brief Append multiple characters. 3429 * @param __n The number of characters to append. 3430 * @param __c The character to use. 3431 * @return Reference to this string. 3432 * 3433 * Appends __n copies of __c to this string. 3434 */ 3435 basic_string& 3436 append(size_type __n, _CharT __c); 3437 3438#if __cplusplus >= 201103L 3439 /** 3440 * @brief Append an initializer_list of characters. 3441 * @param __l The initializer_list of characters to append. 3442 * @return Reference to this string. 3443 */ 3444 basic_string& 3445 append(initializer_list<_CharT> __l) 3446 { return this->append(__l.begin(), __l.size()); } 3447#endif // C++11 3448 3449 /** 3450 * @brief Append a range of characters. 3451 * @param __first Iterator referencing the first character to append. 3452 * @param __last Iterator marking the end of the range. 3453 * @return Reference to this string. 3454 * 3455 * Appends characters in the range [__first,__last) to this string. 3456 */ 3457 template<class _InputIterator> 3458 basic_string& 3459 append(_InputIterator __first, _InputIterator __last) 3460 { return this->replace(_M_iend(), _M_iend(), __first, __last); } 3461 3462 /** 3463 * @brief Append a single character. 3464 * @param __c Character to append. 3465 */ 3466 void 3467 push_back(_CharT __c) 3468 { 3469 const size_type __len = 1 + this->size(); 3470 if (__len > this->capacity() || _M_rep()->_M_is_shared()) 3471 this->reserve(__len); 3472 traits_type::assign(_M_data()[this->size()], __c); 3473 _M_rep()->_M_set_length_and_sharable(__len); 3474 } 3475 3476 /** 3477 * @brief Set value to contents of another string. 3478 * @param __str Source string to use. 3479 * @return Reference to this string. 3480 */ 3481 basic_string& 3482 assign(const basic_string& __str); 3483 3484#if __cplusplus >= 201103L 3485 /** 3486 * @brief Set value to contents of another string. 3487 * @param __str Source string to use. 3488 * @return Reference to this string. 3489 * 3490 * This function sets this string to the exact contents of @a __str. 3491 * @a __str is a valid, but unspecified string. 3492 */ 3493 // PR 58265, this should be noexcept. 3494 basic_string& 3495 assign(basic_string&& __str) 3496 { 3497 this->swap(__str); 3498 return *this; 3499 } 3500#endif // C++11 3501 3502 /** 3503 * @brief Set value to a substring of a string. 3504 * @param __str The string to use. 3505 * @param __pos Index of the first character of str. 3506 * @param __n Number of characters to use. 3507 * @return Reference to this string. 3508 * @throw std::out_of_range if @a pos is not a valid index. 3509 * 3510 * This function sets this string to the substring of @a __str 3511 * consisting of @a __n characters at @a __pos. If @a __n is 3512 * is larger than the number of available characters in @a 3513 * __str, the remainder of @a __str is used. 3514 */ 3515 basic_string& 3516 assign(const basic_string& __str, size_type __pos, size_type __n) 3517 { return this->assign(__str._M_data() 3518 + __str._M_check(__pos, "basic_string::assign"), 3519 __str._M_limit(__pos, __n)); } 3520 3521 /** 3522 * @brief Set value to a C substring. 3523 * @param __s The C string to use. 3524 * @param __n Number of characters to use. 3525 * @return Reference to this string. 3526 * 3527 * This function sets the value of this string to the first @a __n 3528 * characters of @a __s. If @a __n is is larger than the number of 3529 * available characters in @a __s, the remainder of @a __s is used. 3530 */ 3531 basic_string& 3532 assign(const _CharT* __s, size_type __n); 3533 3534 /** 3535 * @brief Set value to contents of a C string. 3536 * @param __s The C string to use. 3537 * @return Reference to this string. 3538 * 3539 * This function sets the value of this string to the value of @a __s. 3540 * The data is copied, so there is no dependence on @a __s once the 3541 * function returns. 3542 */ 3543 basic_string& 3544 assign(const _CharT* __s) 3545 { 3546 __glibcxx_requires_string(__s); 3547 return this->assign(__s, traits_type::length(__s)); 3548 } 3549 3550 /** 3551 * @brief Set value to multiple characters. 3552 * @param __n Length of the resulting string. 3553 * @param __c The character to use. 3554 * @return Reference to this string. 3555 * 3556 * This function sets the value of this string to @a __n copies of 3557 * character @a __c. 3558 */ 3559 basic_string& 3560 assign(size_type __n, _CharT __c) 3561 { return _M_replace_aux(size_type(0), this->size(), __n, __c); } 3562 3563 /** 3564 * @brief Set value to a range of characters. 3565 * @param __first Iterator referencing the first character to append. 3566 * @param __last Iterator marking the end of the range. 3567 * @return Reference to this string. 3568 * 3569 * Sets value of string to characters in the range [__first,__last). 3570 */ 3571 template<class _InputIterator> 3572 basic_string& 3573 assign(_InputIterator __first, _InputIterator __last) 3574 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); } 3575 3576#if __cplusplus >= 201103L 3577 /** 3578 * @brief Set value to an initializer_list of characters. 3579 * @param __l The initializer_list of characters to assign. 3580 * @return Reference to this string. 3581 */ 3582 basic_string& 3583 assign(initializer_list<_CharT> __l) 3584 { return this->assign(__l.begin(), __l.size()); } 3585#endif // C++11 3586 3587 /** 3588 * @brief Insert multiple characters. 3589 * @param __p Iterator referencing location in string to insert at. 3590 * @param __n Number of characters to insert 3591 * @param __c The character to insert. 3592 * @throw std::length_error If new length exceeds @c max_size(). 3593 * 3594 * Inserts @a __n copies of character @a __c starting at the 3595 * position referenced by iterator @a __p. If adding 3596 * characters causes the length to exceed max_size(), 3597 * length_error is thrown. The value of the string doesn't 3598 * change if an error is thrown. 3599 */ 3600 void 3601 insert(iterator __p, size_type __n, _CharT __c) 3602 { this->replace(__p, __p, __n, __c); } 3603 3604 /** 3605 * @brief Insert a range of characters. 3606 * @param __p Iterator referencing location in string to insert at. 3607 * @param __beg Start of range. 3608 * @param __end End of range. 3609 * @throw std::length_error If new length exceeds @c max_size(). 3610 * 3611 * Inserts characters in range [__beg,__end). If adding 3612 * characters causes the length to exceed max_size(), 3613 * length_error is thrown. The value of the string doesn't 3614 * change if an error is thrown. 3615 */ 3616 template<class _InputIterator> 3617 void 3618 insert(iterator __p, _InputIterator __beg, _InputIterator __end) 3619 { this->replace(__p, __p, __beg, __end); } 3620 3621#if __cplusplus >= 201103L 3622 /** 3623 * @brief Insert an initializer_list of characters. 3624 * @param __p Iterator referencing location in string to insert at. 3625 * @param __l The initializer_list of characters to insert. 3626 * @throw std::length_error If new length exceeds @c max_size(). 3627 */ 3628 void 3629 insert(iterator __p, initializer_list<_CharT> __l) 3630 { 3631 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend()); 3632 this->insert(__p - _M_ibegin(), __l.begin(), __l.size()); 3633 } 3634#endif // C++11 3635 3636 /** 3637 * @brief Insert value of a string. 3638 * @param __pos1 Iterator referencing location in string to insert at. 3639 * @param __str The string to insert. 3640 * @return Reference to this string. 3641 * @throw std::length_error If new length exceeds @c max_size(). 3642 * 3643 * Inserts value of @a __str starting at @a __pos1. If adding 3644 * characters causes the length to exceed max_size(), 3645 * length_error is thrown. The value of the string doesn't 3646 * change if an error is thrown. 3647 */ 3648 basic_string& 3649 insert(size_type __pos1, const basic_string& __str) 3650 { return this->insert(__pos1, __str, size_type(0), __str.size()); } 3651 3652 /** 3653 * @brief Insert a substring. 3654 * @param __pos1 Iterator referencing location in string to insert at. 3655 * @param __str The string to insert. 3656 * @param __pos2 Start of characters in str to insert. 3657 * @param __n Number of characters to insert. 3658 * @return Reference to this string. 3659 * @throw std::length_error If new length exceeds @c max_size(). 3660 * @throw std::out_of_range If @a pos1 > size() or 3661 * @a __pos2 > @a str.size(). 3662 * 3663 * Starting at @a pos1, insert @a __n character of @a __str 3664 * beginning with @a __pos2. If adding characters causes the 3665 * length to exceed max_size(), length_error is thrown. If @a 3666 * __pos1 is beyond the end of this string or @a __pos2 is 3667 * beyond the end of @a __str, out_of_range is thrown. The 3668 * value of the string doesn't change if an error is thrown. 3669 */ 3670 basic_string& 3671 insert(size_type __pos1, const basic_string& __str, 3672 size_type __pos2, size_type __n) 3673 { return this->insert(__pos1, __str._M_data() 3674 + __str._M_check(__pos2, "basic_string::insert"), 3675 __str._M_limit(__pos2, __n)); } 3676 3677 /** 3678 * @brief Insert a C substring. 3679 * @param __pos Iterator referencing location in string to insert at. 3680 * @param __s The C string to insert. 3681 * @param __n The number of characters to insert. 3682 * @return Reference to this string. 3683 * @throw std::length_error If new length exceeds @c max_size(). 3684 * @throw std::out_of_range If @a __pos is beyond the end of this 3685 * string. 3686 * 3687 * Inserts the first @a __n characters of @a __s starting at @a 3688 * __pos. If adding characters causes the length to exceed 3689 * max_size(), length_error is thrown. If @a __pos is beyond 3690 * end(), out_of_range is thrown. The value of the string 3691 * doesn't change if an error is thrown. 3692 */ 3693 basic_string& 3694 insert(size_type __pos, const _CharT* __s, size_type __n); 3695 3696 /** 3697 * @brief Insert a C string. 3698 * @param __pos Iterator referencing location in string to insert at. 3699 * @param __s The C string to insert. 3700 * @return Reference to this string. 3701 * @throw std::length_error If new length exceeds @c max_size(). 3702 * @throw std::out_of_range If @a pos is beyond the end of this 3703 * string. 3704 * 3705 * Inserts the first @a n characters of @a __s starting at @a __pos. If 3706 * adding characters causes the length to exceed max_size(), 3707 * length_error is thrown. If @a __pos is beyond end(), out_of_range is 3708 * thrown. The value of the string doesn't change if an error is 3709 * thrown. 3710 */ 3711 basic_string& 3712 insert(size_type __pos, const _CharT* __s) 3713 { 3714 __glibcxx_requires_string(__s); 3715 return this->insert(__pos, __s, traits_type::length(__s)); 3716 } 3717 3718 /** 3719 * @brief Insert multiple characters. 3720 * @param __pos Index in string to insert at. 3721 * @param __n Number of characters to insert 3722 * @param __c The character to insert. 3723 * @return Reference to this string. 3724 * @throw std::length_error If new length exceeds @c max_size(). 3725 * @throw std::out_of_range If @a __pos is beyond the end of this 3726 * string. 3727 * 3728 * Inserts @a __n copies of character @a __c starting at index 3729 * @a __pos. If adding characters causes the length to exceed 3730 * max_size(), length_error is thrown. If @a __pos > length(), 3731 * out_of_range is thrown. The value of the string doesn't 3732 * change if an error is thrown. 3733 */ 3734 basic_string& 3735 insert(size_type __pos, size_type __n, _CharT __c) 3736 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"), 3737 size_type(0), __n, __c); } 3738 3739 /** 3740 * @brief Insert one character. 3741 * @param __p Iterator referencing position in string to insert at. 3742 * @param __c The character to insert. 3743 * @return Iterator referencing newly inserted char. 3744 * @throw std::length_error If new length exceeds @c max_size(). 3745 * 3746 * Inserts character @a __c at position referenced by @a __p. 3747 * If adding character causes the length to exceed max_size(), 3748 * length_error is thrown. If @a __p is beyond end of string, 3749 * out_of_range is thrown. The value of the string doesn't 3750 * change if an error is thrown. 3751 */ 3752 iterator 3753 insert(iterator __p, _CharT __c) 3754 { 3755 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend()); 3756 const size_type __pos = __p - _M_ibegin(); 3757 _M_replace_aux(__pos, size_type(0), size_type(1), __c); 3758 _M_rep()->_M_set_leaked(); 3759 return iterator(_M_data() + __pos); 3760 } 3761 3762 /** 3763 * @brief Remove characters. 3764 * @param __pos Index of first character to remove (default 0). 3765 * @param __n Number of characters to remove (default remainder). 3766 * @return Reference to this string. 3767 * @throw std::out_of_range If @a pos is beyond the end of this 3768 * string. 3769 * 3770 * Removes @a __n characters from this string starting at @a 3771 * __pos. The length of the string is reduced by @a __n. If 3772 * there are < @a __n characters to remove, the remainder of 3773 * the string is truncated. If @a __p is beyond end of string, 3774 * out_of_range is thrown. The value of the string doesn't 3775 * change if an error is thrown. 3776 */ 3777 basic_string& 3778 erase(size_type __pos = 0, size_type __n = npos) 3779 { 3780 _M_mutate(_M_check(__pos, "basic_string::erase"), 3781 _M_limit(__pos, __n), size_type(0)); 3782 return *this; 3783 } 3784 3785 /** 3786 * @brief Remove one character. 3787 * @param __position Iterator referencing the character to remove. 3788 * @return iterator referencing same location after removal. 3789 * 3790 * Removes the character at @a __position from this string. The value 3791 * of the string doesn't change if an error is thrown. 3792 */ 3793 iterator 3794 erase(iterator __position) 3795 { 3796 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin() 3797 && __position < _M_iend()); 3798 const size_type __pos = __position - _M_ibegin(); 3799 _M_mutate(__pos, size_type(1), size_type(0)); 3800 _M_rep()->_M_set_leaked(); 3801 return iterator(_M_data() + __pos); 3802 } 3803 3804 /** 3805 * @brief Remove a range of characters. 3806 * @param __first Iterator referencing the first character to remove. 3807 * @param __last Iterator referencing the end of the range. 3808 * @return Iterator referencing location of first after removal. 3809 * 3810 * Removes the characters in the range [first,last) from this string. 3811 * The value of the string doesn't change if an error is thrown. 3812 */ 3813 iterator 3814 erase(iterator __first, iterator __last); 3815 3816#if __cplusplus >= 201103L 3817 /** 3818 * @brief Remove the last character. 3819 * 3820 * The string must be non-empty. 3821 */ 3822 void 3823 pop_back() // FIXME C++11: should be noexcept. 3824 { erase(size()-1, 1); } 3825#endif // C++11 3826 3827 /** 3828 * @brief Replace characters with value from another string. 3829 * @param __pos Index of first character to replace. 3830 * @param __n Number of characters to be replaced. 3831 * @param __str String to insert. 3832 * @return Reference to this string. 3833 * @throw std::out_of_range If @a pos is beyond the end of this 3834 * string. 3835 * @throw std::length_error If new length exceeds @c max_size(). 3836 * 3837 * Removes the characters in the range [__pos,__pos+__n) from 3838 * this string. In place, the value of @a __str is inserted. 3839 * If @a __pos is beyond end of string, out_of_range is thrown. 3840 * If the length of the result exceeds max_size(), length_error 3841 * is thrown. The value of the string doesn't change if an 3842 * error is thrown. 3843 */ 3844 basic_string& 3845 replace(size_type __pos, size_type __n, const basic_string& __str) 3846 { return this->replace(__pos, __n, __str._M_data(), __str.size()); } 3847 3848 /** 3849 * @brief Replace characters with value from another string. 3850 * @param __pos1 Index of first character to replace. 3851 * @param __n1 Number of characters to be replaced. 3852 * @param __str String to insert. 3853 * @param __pos2 Index of first character of str to use. 3854 * @param __n2 Number of characters from str to use. 3855 * @return Reference to this string. 3856 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 > 3857 * __str.size(). 3858 * @throw std::length_error If new length exceeds @c max_size(). 3859 * 3860 * Removes the characters in the range [__pos1,__pos1 + n) from this 3861 * string. In place, the value of @a __str is inserted. If @a __pos is 3862 * beyond end of string, out_of_range is thrown. If the length of the 3863 * result exceeds max_size(), length_error is thrown. The value of the 3864 * string doesn't change if an error is thrown. 3865 */ 3866 basic_string& 3867 replace(size_type __pos1, size_type __n1, const basic_string& __str, 3868 size_type __pos2, size_type __n2) 3869 { return this->replace(__pos1, __n1, __str._M_data() 3870 + __str._M_check(__pos2, "basic_string::replace"), 3871 __str._M_limit(__pos2, __n2)); } 3872 3873 /** 3874 * @brief Replace characters with value of a C substring. 3875 * @param __pos Index of first character to replace. 3876 * @param __n1 Number of characters to be replaced. 3877 * @param __s C string to insert. 3878 * @param __n2 Number of characters from @a s to use. 3879 * @return Reference to this string. 3880 * @throw std::out_of_range If @a pos1 > size(). 3881 * @throw std::length_error If new length exceeds @c max_size(). 3882 * 3883 * Removes the characters in the range [__pos,__pos + __n1) 3884 * from this string. In place, the first @a __n2 characters of 3885 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If 3886 * @a __pos is beyond end of string, out_of_range is thrown. If 3887 * the length of result exceeds max_size(), length_error is 3888 * thrown. The value of the string doesn't change if an error 3889 * is thrown. 3890 */ 3891 basic_string& 3892 replace(size_type __pos, size_type __n1, const _CharT* __s, 3893 size_type __n2); 3894 3895 /** 3896 * @brief Replace characters with value of a C string. 3897 * @param __pos Index of first character to replace. 3898 * @param __n1 Number of characters to be replaced. 3899 * @param __s C string to insert. 3900 * @return Reference to this string. 3901 * @throw std::out_of_range If @a pos > size(). 3902 * @throw std::length_error If new length exceeds @c max_size(). 3903 * 3904 * Removes the characters in the range [__pos,__pos + __n1) 3905 * from this string. In place, the characters of @a __s are 3906 * inserted. If @a __pos is beyond end of string, out_of_range 3907 * is thrown. If the length of result exceeds max_size(), 3908 * length_error is thrown. The value of the string doesn't 3909 * change if an error is thrown. 3910 */ 3911 basic_string& 3912 replace(size_type __pos, size_type __n1, const _CharT* __s) 3913 { 3914 __glibcxx_requires_string(__s); 3915 return this->replace(__pos, __n1, __s, traits_type::length(__s)); 3916 } 3917 3918 /** 3919 * @brief Replace characters with multiple characters. 3920 * @param __pos Index of first character to replace. 3921 * @param __n1 Number of characters to be replaced. 3922 * @param __n2 Number of characters to insert. 3923 * @param __c Character to insert. 3924 * @return Reference to this string. 3925 * @throw std::out_of_range If @a __pos > size(). 3926 * @throw std::length_error If new length exceeds @c max_size(). 3927 * 3928 * Removes the characters in the range [pos,pos + n1) from this 3929 * string. In place, @a __n2 copies of @a __c are inserted. 3930 * If @a __pos is beyond end of string, out_of_range is thrown. 3931 * If the length of result exceeds max_size(), length_error is 3932 * thrown. The value of the string doesn't change if an error 3933 * is thrown. 3934 */ 3935 basic_string& 3936 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c) 3937 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"), 3938 _M_limit(__pos, __n1), __n2, __c); } 3939 3940 /** 3941 * @brief Replace range of characters with string. 3942 * @param __i1 Iterator referencing start of range to replace. 3943 * @param __i2 Iterator referencing end of range to replace. 3944 * @param __str String value to insert. 3945 * @return Reference to this string. 3946 * @throw std::length_error If new length exceeds @c max_size(). 3947 * 3948 * Removes the characters in the range [__i1,__i2). In place, 3949 * the value of @a __str is inserted. If the length of result 3950 * exceeds max_size(), length_error is thrown. The value of 3951 * the string doesn't change if an error is thrown. 3952 */ 3953 basic_string& 3954 replace(iterator __i1, iterator __i2, const basic_string& __str) 3955 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); } 3956 3957 /** 3958 * @brief Replace range of characters with C substring. 3959 * @param __i1 Iterator referencing start of range to replace. 3960 * @param __i2 Iterator referencing end of range to replace. 3961 * @param __s C string value to insert. 3962 * @param __n Number of characters from s to insert. 3963 * @return Reference to this string. 3964 * @throw std::length_error If new length exceeds @c max_size(). 3965 * 3966 * Removes the characters in the range [__i1,__i2). In place, 3967 * the first @a __n characters of @a __s are inserted. If the 3968 * length of result exceeds max_size(), length_error is thrown. 3969 * The value of the string doesn't change if an error is 3970 * thrown. 3971 */ 3972 basic_string& 3973 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n) 3974 { 3975 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 3976 && __i2 <= _M_iend()); 3977 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n); 3978 } 3979 3980 /** 3981 * @brief Replace range of characters with C string. 3982 * @param __i1 Iterator referencing start of range to replace. 3983 * @param __i2 Iterator referencing end of range to replace. 3984 * @param __s C string value to insert. 3985 * @return Reference to this string. 3986 * @throw std::length_error If new length exceeds @c max_size(). 3987 * 3988 * Removes the characters in the range [__i1,__i2). In place, 3989 * the characters of @a __s are inserted. If the length of 3990 * result exceeds max_size(), length_error is thrown. The 3991 * value of the string doesn't change if an error is thrown. 3992 */ 3993 basic_string& 3994 replace(iterator __i1, iterator __i2, const _CharT* __s) 3995 { 3996 __glibcxx_requires_string(__s); 3997 return this->replace(__i1, __i2, __s, traits_type::length(__s)); 3998 } 3999 4000 /** 4001 * @brief Replace range of characters with multiple characters 4002 * @param __i1 Iterator referencing start of range to replace. 4003 * @param __i2 Iterator referencing end of range to replace. 4004 * @param __n Number of characters to insert. 4005 * @param __c Character to insert. 4006 * @return Reference to this string. 4007 * @throw std::length_error If new length exceeds @c max_size(). 4008 * 4009 * Removes the characters in the range [__i1,__i2). In place, 4010 * @a __n copies of @a __c are inserted. If the length of 4011 * result exceeds max_size(), length_error is thrown. The 4012 * value of the string doesn't change if an error is thrown. 4013 */ 4014 basic_string& 4015 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c) 4016 { 4017 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 4018 && __i2 <= _M_iend()); 4019 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c); 4020 } 4021 4022 /** 4023 * @brief Replace range of characters with range. 4024 * @param __i1 Iterator referencing start of range to replace. 4025 * @param __i2 Iterator referencing end of range to replace. 4026 * @param __k1 Iterator referencing start of range to insert. 4027 * @param __k2 Iterator referencing end of range to insert. 4028 * @return Reference to this string. 4029 * @throw std::length_error If new length exceeds @c max_size(). 4030 * 4031 * Removes the characters in the range [__i1,__i2). In place, 4032 * characters in the range [__k1,__k2) are inserted. If the 4033 * length of result exceeds max_size(), length_error is thrown. 4034 * The value of the string doesn't change if an error is 4035 * thrown. 4036 */ 4037 template<class _InputIterator> 4038 basic_string& 4039 replace(iterator __i1, iterator __i2, 4040 _InputIterator __k1, _InputIterator __k2) 4041 { 4042 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 4043 && __i2 <= _M_iend()); 4044 __glibcxx_requires_valid_range(__k1, __k2); 4045 typedef typename std::__is_integer<_InputIterator>::__type _Integral; 4046 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral()); 4047 } 4048 4049 // Specializations for the common case of pointer and iterator: 4050 // useful to avoid the overhead of temporary buffering in _M_replace. 4051 basic_string& 4052 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2) 4053 { 4054 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 4055 && __i2 <= _M_iend()); 4056 __glibcxx_requires_valid_range(__k1, __k2); 4057 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, 4058 __k1, __k2 - __k1); 4059 } 4060 4061 basic_string& 4062 replace(iterator __i1, iterator __i2, 4063 const _CharT* __k1, const _CharT* __k2) 4064 { 4065 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 4066 && __i2 <= _M_iend()); 4067 __glibcxx_requires_valid_range(__k1, __k2); 4068 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, 4069 __k1, __k2 - __k1); 4070 } 4071 4072 basic_string& 4073 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2) 4074 { 4075 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 4076 && __i2 <= _M_iend()); 4077 __glibcxx_requires_valid_range(__k1, __k2); 4078 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, 4079 __k1.base(), __k2 - __k1); 4080 } 4081 4082 basic_string& 4083 replace(iterator __i1, iterator __i2, 4084 const_iterator __k1, const_iterator __k2) 4085 { 4086 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 4087 && __i2 <= _M_iend()); 4088 __glibcxx_requires_valid_range(__k1, __k2); 4089 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, 4090 __k1.base(), __k2 - __k1); 4091 } 4092 4093#if __cplusplus >= 201103L 4094 /** 4095 * @brief Replace range of characters with initializer_list. 4096 * @param __i1 Iterator referencing start of range to replace. 4097 * @param __i2 Iterator referencing end of range to replace. 4098 * @param __l The initializer_list of characters to insert. 4099 * @return Reference to this string. 4100 * @throw std::length_error If new length exceeds @c max_size(). 4101 * 4102 * Removes the characters in the range [__i1,__i2). In place, 4103 * characters in the range [__k1,__k2) are inserted. If the 4104 * length of result exceeds max_size(), length_error is thrown. 4105 * The value of the string doesn't change if an error is 4106 * thrown. 4107 */ 4108 basic_string& replace(iterator __i1, iterator __i2, 4109 initializer_list<_CharT> __l) 4110 { return this->replace(__i1, __i2, __l.begin(), __l.end()); } 4111#endif // C++11 4112 4113 private: 4114 template<class _Integer> 4115 basic_string& 4116 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n, 4117 _Integer __val, __true_type) 4118 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); } 4119 4120 template<class _InputIterator> 4121 basic_string& 4122 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1, 4123 _InputIterator __k2, __false_type); 4124 4125 basic_string& 4126 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2, 4127 _CharT __c); 4128 4129 basic_string& 4130 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s, 4131 size_type __n2); 4132 4133 // _S_construct_aux is used to implement the 21.3.1 para 15 which 4134 // requires special behaviour if _InIter is an integral type 4135 template<class _InIterator> 4136 static _CharT* 4137 _S_construct_aux(_InIterator __beg, _InIterator __end, 4138 const _Alloc& __a, __false_type) 4139 { 4140 typedef typename iterator_traits<_InIterator>::iterator_category _Tag; 4141 return _S_construct(__beg, __end, __a, _Tag()); 4142 } 4143 4144 // _GLIBCXX_RESOLVE_LIB_DEFECTS 4145 // 438. Ambiguity in the "do the right thing" clause 4146 template<class _Integer> 4147 static _CharT* 4148 _S_construct_aux(_Integer __beg, _Integer __end, 4149 const _Alloc& __a, __true_type) 4150 { return _S_construct_aux_2(static_cast<size_type>(__beg), 4151 __end, __a); } 4152 4153 static _CharT* 4154 _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a) 4155 { return _S_construct(__req, __c, __a); } 4156 4157 template<class _InIterator> 4158 static _CharT* 4159 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a) 4160 { 4161 typedef typename std::__is_integer<_InIterator>::__type _Integral; 4162 return _S_construct_aux(__beg, __end, __a, _Integral()); 4163 } 4164 4165 // For Input Iterators, used in istreambuf_iterators, etc. 4166 template<class _InIterator> 4167 static _CharT* 4168 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a, 4169 input_iterator_tag); 4170 4171 // For forward_iterators up to random_access_iterators, used for 4172 // string::iterator, _CharT*, etc. 4173 template<class _FwdIterator> 4174 static _CharT* 4175 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a, 4176 forward_iterator_tag); 4177 4178 static _CharT* 4179 _S_construct(size_type __req, _CharT __c, const _Alloc& __a); 4180 4181 public: 4182 4183 /** 4184 * @brief Copy substring into C string. 4185 * @param __s C string to copy value into. 4186 * @param __n Number of characters to copy. 4187 * @param __pos Index of first character to copy. 4188 * @return Number of characters actually copied 4189 * @throw std::out_of_range If __pos > size(). 4190 * 4191 * Copies up to @a __n characters starting at @a __pos into the 4192 * C string @a __s. If @a __pos is %greater than size(), 4193 * out_of_range is thrown. 4194 */ 4195 size_type 4196 copy(_CharT* __s, size_type __n, size_type __pos = 0) const; 4197 4198 /** 4199 * @brief Swap contents with another string. 4200 * @param __s String to swap with. 4201 * 4202 * Exchanges the contents of this string with that of @a __s in constant 4203 * time. 4204 */ 4205 // PR 58265, this should be noexcept. 4206 void 4207 swap(basic_string& __s); 4208 4209 // String operations: 4210 /** 4211 * @brief Return const pointer to null-terminated contents. 4212 * 4213 * This is a handle to internal data. Do not modify or dire things may 4214 * happen. 4215 */ 4216 const _CharT* 4217 c_str() const _GLIBCXX_NOEXCEPT 4218 { return _M_data(); } 4219 4220 /** 4221 * @brief Return const pointer to contents. 4222 * 4223 * This is a handle to internal data. Do not modify or dire things may 4224 * happen. 4225 */ 4226 const _CharT* 4227 data() const _GLIBCXX_NOEXCEPT 4228 { return _M_data(); } 4229 4230 /** 4231 * @brief Return copy of allocator used to construct this string. 4232 */ 4233 allocator_type 4234 get_allocator() const _GLIBCXX_NOEXCEPT 4235 { return _M_dataplus; } 4236 4237 /** 4238 * @brief Find position of a C substring. 4239 * @param __s C string to locate. 4240 * @param __pos Index of character to search from. 4241 * @param __n Number of characters from @a s to search for. 4242 * @return Index of start of first occurrence. 4243 * 4244 * Starting from @a __pos, searches forward for the first @a 4245 * __n characters in @a __s within this string. If found, 4246 * returns the index where it begins. If not found, returns 4247 * npos. 4248 */ 4249 size_type 4250 find(const _CharT* __s, size_type __pos, size_type __n) const; 4251 4252 /** 4253 * @brief Find position of a string. 4254 * @param __str String to locate. 4255 * @param __pos Index of character to search from (default 0). 4256 * @return Index of start of first occurrence. 4257 * 4258 * Starting from @a __pos, searches forward for value of @a __str within 4259 * this string. If found, returns the index where it begins. If not 4260 * found, returns npos. 4261 */ 4262 size_type 4263 find(const basic_string& __str, size_type __pos = 0) const 4264 _GLIBCXX_NOEXCEPT 4265 { return this->find(__str.data(), __pos, __str.size()); } 4266 4267 /** 4268 * @brief Find position of a C string. 4269 * @param __s C string to locate. 4270 * @param __pos Index of character to search from (default 0). 4271 * @return Index of start of first occurrence. 4272 * 4273 * Starting from @a __pos, searches forward for the value of @a 4274 * __s within this string. If found, returns the index where 4275 * it begins. If not found, returns npos. 4276 */ 4277 size_type 4278 find(const _CharT* __s, size_type __pos = 0) const 4279 { 4280 __glibcxx_requires_string(__s); 4281 return this->find(__s, __pos, traits_type::length(__s)); 4282 } 4283 4284 /** 4285 * @brief Find position of a character. 4286 * @param __c Character to locate. 4287 * @param __pos Index of character to search from (default 0). 4288 * @return Index of first occurrence. 4289 * 4290 * Starting from @a __pos, searches forward for @a __c within 4291 * this string. If found, returns the index where it was 4292 * found. If not found, returns npos. 4293 */ 4294 size_type 4295 find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT; 4296 4297 /** 4298 * @brief Find last position of a string. 4299 * @param __str String to locate. 4300 * @param __pos Index of character to search back from (default end). 4301 * @return Index of start of last occurrence. 4302 * 4303 * Starting from @a __pos, searches backward for value of @a 4304 * __str within this string. If found, returns the index where 4305 * it begins. If not found, returns npos. 4306 */ 4307 size_type 4308 rfind(const basic_string& __str, size_type __pos = npos) const 4309 _GLIBCXX_NOEXCEPT 4310 { return this->rfind(__str.data(), __pos, __str.size()); } 4311 4312 /** 4313 * @brief Find last position of a C substring. 4314 * @param __s C string to locate. 4315 * @param __pos Index of character to search back from. 4316 * @param __n Number of characters from s to search for. 4317 * @return Index of start of last occurrence. 4318 * 4319 * Starting from @a __pos, searches backward for the first @a 4320 * __n characters in @a __s within this string. If found, 4321 * returns the index where it begins. If not found, returns 4322 * npos. 4323 */ 4324 size_type 4325 rfind(const _CharT* __s, size_type __pos, size_type __n) const; 4326 4327 /** 4328 * @brief Find last position of a C string. 4329 * @param __s C string to locate. 4330 * @param __pos Index of character to start search at (default end). 4331 * @return Index of start of last occurrence. 4332 * 4333 * Starting from @a __pos, searches backward for the value of 4334 * @a __s within this string. If found, returns the index 4335 * where it begins. If not found, returns npos. 4336 */ 4337 size_type 4338 rfind(const _CharT* __s, size_type __pos = npos) const 4339 { 4340 __glibcxx_requires_string(__s); 4341 return this->rfind(__s, __pos, traits_type::length(__s)); 4342 } 4343 4344 /** 4345 * @brief Find last position of a character. 4346 * @param __c Character to locate. 4347 * @param __pos Index of character to search back from (default end). 4348 * @return Index of last occurrence. 4349 * 4350 * Starting from @a __pos, searches backward for @a __c within 4351 * this string. If found, returns the index where it was 4352 * found. If not found, returns npos. 4353 */ 4354 size_type 4355 rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT; 4356 4357 /** 4358 * @brief Find position of a character of string. 4359 * @param __str String containing characters to locate. 4360 * @param __pos Index of character to search from (default 0). 4361 * @return Index of first occurrence. 4362 * 4363 * Starting from @a __pos, searches forward for one of the 4364 * characters of @a __str within this string. If found, 4365 * returns the index where it was found. If not found, returns 4366 * npos. 4367 */ 4368 size_type 4369 find_first_of(const basic_string& __str, size_type __pos = 0) const 4370 _GLIBCXX_NOEXCEPT 4371 { return this->find_first_of(__str.data(), __pos, __str.size()); } 4372 4373 /** 4374 * @brief Find position of a character of C substring. 4375 * @param __s String containing characters to locate. 4376 * @param __pos Index of character to search from. 4377 * @param __n Number of characters from s to search for. 4378 * @return Index of first occurrence. 4379 * 4380 * Starting from @a __pos, searches forward for one of the 4381 * first @a __n characters of @a __s within this string. If 4382 * found, returns the index where it was found. If not found, 4383 * returns npos. 4384 */ 4385 size_type 4386 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const; 4387 4388 /** 4389 * @brief Find position of a character of C string. 4390 * @param __s String containing characters to locate. 4391 * @param __pos Index of character to search from (default 0). 4392 * @return Index of first occurrence. 4393 * 4394 * Starting from @a __pos, searches forward for one of the 4395 * characters of @a __s within this string. If found, returns 4396 * the index where it was found. If not found, returns npos. 4397 */ 4398 size_type 4399 find_first_of(const _CharT* __s, size_type __pos = 0) const 4400 { 4401 __glibcxx_requires_string(__s); 4402 return this->find_first_of(__s, __pos, traits_type::length(__s)); 4403 } 4404 4405 /** 4406 * @brief Find position of a character. 4407 * @param __c Character to locate. 4408 * @param __pos Index of character to search from (default 0). 4409 * @return Index of first occurrence. 4410 * 4411 * Starting from @a __pos, searches forward for the character 4412 * @a __c within this string. If found, returns the index 4413 * where it was found. If not found, returns npos. 4414 * 4415 * Note: equivalent to find(__c, __pos). 4416 */ 4417 size_type 4418 find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT 4419 { return this->find(__c, __pos); } 4420 4421 /** 4422 * @brief Find last position of a character of string. 4423 * @param __str String containing characters to locate. 4424 * @param __pos Index of character to search back from (default end). 4425 * @return Index of last occurrence. 4426 * 4427 * Starting from @a __pos, searches backward for one of the 4428 * characters of @a __str within this string. If found, 4429 * returns the index where it was found. If not found, returns 4430 * npos. 4431 */ 4432 size_type 4433 find_last_of(const basic_string& __str, size_type __pos = npos) const 4434 _GLIBCXX_NOEXCEPT 4435 { return this->find_last_of(__str.data(), __pos, __str.size()); } 4436 4437 /** 4438 * @brief Find last position of a character of C substring. 4439 * @param __s C string containing characters to locate. 4440 * @param __pos Index of character to search back from. 4441 * @param __n Number of characters from s to search for. 4442 * @return Index of last occurrence. 4443 * 4444 * Starting from @a __pos, searches backward for one of the 4445 * first @a __n characters of @a __s within this string. If 4446 * found, returns the index where it was found. If not found, 4447 * returns npos. 4448 */ 4449 size_type 4450 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const; 4451 4452 /** 4453 * @brief Find last position of a character of C string. 4454 * @param __s C string containing characters to locate. 4455 * @param __pos Index of character to search back from (default end). 4456 * @return Index of last occurrence. 4457 * 4458 * Starting from @a __pos, searches backward for one of the 4459 * characters of @a __s within this string. If found, returns 4460 * the index where it was found. If not found, returns npos. 4461 */ 4462 size_type 4463 find_last_of(const _CharT* __s, size_type __pos = npos) const 4464 { 4465 __glibcxx_requires_string(__s); 4466 return this->find_last_of(__s, __pos, traits_type::length(__s)); 4467 } 4468 4469 /** 4470 * @brief Find last position of a character. 4471 * @param __c Character to locate. 4472 * @param __pos Index of character to search back from (default end). 4473 * @return Index of last occurrence. 4474 * 4475 * Starting from @a __pos, searches backward for @a __c within 4476 * this string. If found, returns the index where it was 4477 * found. If not found, returns npos. 4478 * 4479 * Note: equivalent to rfind(__c, __pos). 4480 */ 4481 size_type 4482 find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT 4483 { return this->rfind(__c, __pos); } 4484 4485 /** 4486 * @brief Find position of a character not in string. 4487 * @param __str String containing characters to avoid. 4488 * @param __pos Index of character to search from (default 0). 4489 * @return Index of first occurrence. 4490 * 4491 * Starting from @a __pos, searches forward for a character not contained 4492 * in @a __str within this string. If found, returns the index where it 4493 * was found. If not found, returns npos. 4494 */ 4495 size_type 4496 find_first_not_of(const basic_string& __str, size_type __pos = 0) const 4497 _GLIBCXX_NOEXCEPT 4498 { return this->find_first_not_of(__str.data(), __pos, __str.size()); } 4499 4500 /** 4501 * @brief Find position of a character not in C substring. 4502 * @param __s C string containing characters to avoid. 4503 * @param __pos Index of character to search from. 4504 * @param __n Number of characters from __s to consider. 4505 * @return Index of first occurrence. 4506 * 4507 * Starting from @a __pos, searches forward for a character not 4508 * contained in the first @a __n characters of @a __s within 4509 * this string. If found, returns the index where it was 4510 * found. If not found, returns npos. 4511 */ 4512 size_type 4513 find_first_not_of(const _CharT* __s, size_type __pos, 4514 size_type __n) const; 4515 4516 /** 4517 * @brief Find position of a character not in C string. 4518 * @param __s C string containing characters to avoid. 4519 * @param __pos Index of character to search from (default 0). 4520 * @return Index of first occurrence. 4521 * 4522 * Starting from @a __pos, searches forward for a character not 4523 * contained in @a __s within this string. If found, returns 4524 * the index where it was found. If not found, returns npos. 4525 */ 4526 size_type 4527 find_first_not_of(const _CharT* __s, size_type __pos = 0) const 4528 { 4529 __glibcxx_requires_string(__s); 4530 return this->find_first_not_of(__s, __pos, traits_type::length(__s)); 4531 } 4532 4533 /** 4534 * @brief Find position of a different character. 4535 * @param __c Character to avoid. 4536 * @param __pos Index of character to search from (default 0). 4537 * @return Index of first occurrence. 4538 * 4539 * Starting from @a __pos, searches forward for a character 4540 * other than @a __c within this string. If found, returns the 4541 * index where it was found. If not found, returns npos. 4542 */ 4543 size_type 4544 find_first_not_of(_CharT __c, size_type __pos = 0) const 4545 _GLIBCXX_NOEXCEPT; 4546 4547 /** 4548 * @brief Find last position of a character not in string. 4549 * @param __str String containing characters to avoid. 4550 * @param __pos Index of character to search back from (default end). 4551 * @return Index of last occurrence. 4552 * 4553 * Starting from @a __pos, searches backward for a character 4554 * not contained in @a __str within this string. If found, 4555 * returns the index where it was found. If not found, returns 4556 * npos. 4557 */ 4558 size_type 4559 find_last_not_of(const basic_string& __str, size_type __pos = npos) const 4560 _GLIBCXX_NOEXCEPT 4561 { return this->find_last_not_of(__str.data(), __pos, __str.size()); } 4562 4563 /** 4564 * @brief Find last position of a character not in C substring. 4565 * @param __s C string containing characters to avoid. 4566 * @param __pos Index of character to search back from. 4567 * @param __n Number of characters from s to consider. 4568 * @return Index of last occurrence. 4569 * 4570 * Starting from @a __pos, searches backward for a character not 4571 * contained in the first @a __n characters of @a __s within this string. 4572 * If found, returns the index where it was found. If not found, 4573 * returns npos. 4574 */ 4575 size_type 4576 find_last_not_of(const _CharT* __s, size_type __pos, 4577 size_type __n) const; 4578 /** 4579 * @brief Find last position of a character not in C string. 4580 * @param __s C string containing characters to avoid. 4581 * @param __pos Index of character to search back from (default end). 4582 * @return Index of last occurrence. 4583 * 4584 * Starting from @a __pos, searches backward for a character 4585 * not contained in @a __s within this string. If found, 4586 * returns the index where it was found. If not found, returns 4587 * npos. 4588 */ 4589 size_type 4590 find_last_not_of(const _CharT* __s, size_type __pos = npos) const 4591 { 4592 __glibcxx_requires_string(__s); 4593 return this->find_last_not_of(__s, __pos, traits_type::length(__s)); 4594 } 4595 4596 /** 4597 * @brief Find last position of a different character. 4598 * @param __c Character to avoid. 4599 * @param __pos Index of character to search back from (default end). 4600 * @return Index of last occurrence. 4601 * 4602 * Starting from @a __pos, searches backward for a character other than 4603 * @a __c within this string. If found, returns the index where it was 4604 * found. If not found, returns npos. 4605 */ 4606 size_type 4607 find_last_not_of(_CharT __c, size_type __pos = npos) const 4608 _GLIBCXX_NOEXCEPT; 4609 4610 /** 4611 * @brief Get a substring. 4612 * @param __pos Index of first character (default 0). 4613 * @param __n Number of characters in substring (default remainder). 4614 * @return The new string. 4615 * @throw std::out_of_range If __pos > size(). 4616 * 4617 * Construct and return a new string using the @a __n 4618 * characters starting at @a __pos. If the string is too 4619 * short, use the remainder of the characters. If @a __pos is 4620 * beyond the end of the string, out_of_range is thrown. 4621 */ 4622 basic_string 4623 substr(size_type __pos = 0, size_type __n = npos) const 4624 { return basic_string(*this, 4625 _M_check(__pos, "basic_string::substr"), __n); } 4626 4627 /** 4628 * @brief Compare to a string. 4629 * @param __str String to compare against. 4630 * @return Integer < 0, 0, or > 0. 4631 * 4632 * Returns an integer < 0 if this string is ordered before @a 4633 * __str, 0 if their values are equivalent, or > 0 if this 4634 * string is ordered after @a __str. Determines the effective 4635 * length rlen of the strings to compare as the smallest of 4636 * size() and str.size(). The function then compares the two 4637 * strings by calling traits::compare(data(), str.data(),rlen). 4638 * If the result of the comparison is nonzero returns it, 4639 * otherwise the shorter one is ordered first. 4640 */ 4641 int 4642 compare(const basic_string& __str) const 4643 { 4644 const size_type __size = this->size(); 4645 const size_type __osize = __str.size(); 4646 const size_type __len = std::min(__size, __osize); 4647 4648 int __r = traits_type::compare(_M_data(), __str.data(), __len); 4649 if (!__r) 4650 __r = _S_compare(__size, __osize); 4651 return __r; 4652 } 4653 4654 /** 4655 * @brief Compare substring to a string. 4656 * @param __pos Index of first character of substring. 4657 * @param __n Number of characters in substring. 4658 * @param __str String to compare against. 4659 * @return Integer < 0, 0, or > 0. 4660 * 4661 * Form the substring of this string from the @a __n characters 4662 * starting at @a __pos. Returns an integer < 0 if the 4663 * substring is ordered before @a __str, 0 if their values are 4664 * equivalent, or > 0 if the substring is ordered after @a 4665 * __str. Determines the effective length rlen of the strings 4666 * to compare as the smallest of the length of the substring 4667 * and @a __str.size(). The function then compares the two 4668 * strings by calling 4669 * traits::compare(substring.data(),str.data(),rlen). If the 4670 * result of the comparison is nonzero returns it, otherwise 4671 * the shorter one is ordered first. 4672 */ 4673 int 4674 compare(size_type __pos, size_type __n, const basic_string& __str) const; 4675 4676 /** 4677 * @brief Compare substring to a substring. 4678 * @param __pos1 Index of first character of substring. 4679 * @param __n1 Number of characters in substring. 4680 * @param __str String to compare against. 4681 * @param __pos2 Index of first character of substring of str. 4682 * @param __n2 Number of characters in substring of str. 4683 * @return Integer < 0, 0, or > 0. 4684 * 4685 * Form the substring of this string from the @a __n1 4686 * characters starting at @a __pos1. Form the substring of @a 4687 * __str from the @a __n2 characters starting at @a __pos2. 4688 * Returns an integer < 0 if this substring is ordered before 4689 * the substring of @a __str, 0 if their values are equivalent, 4690 * or > 0 if this substring is ordered after the substring of 4691 * @a __str. Determines the effective length rlen of the 4692 * strings to compare as the smallest of the lengths of the 4693 * substrings. The function then compares the two strings by 4694 * calling 4695 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen). 4696 * If the result of the comparison is nonzero returns it, 4697 * otherwise the shorter one is ordered first. 4698 */ 4699 int 4700 compare(size_type __pos1, size_type __n1, const basic_string& __str, 4701 size_type __pos2, size_type __n2) const; 4702 4703 /** 4704 * @brief Compare to a C string. 4705 * @param __s C string to compare against. 4706 * @return Integer < 0, 0, or > 0. 4707 * 4708 * Returns an integer < 0 if this string is ordered before @a __s, 0 if 4709 * their values are equivalent, or > 0 if this string is ordered after 4710 * @a __s. Determines the effective length rlen of the strings to 4711 * compare as the smallest of size() and the length of a string 4712 * constructed from @a __s. The function then compares the two strings 4713 * by calling traits::compare(data(),s,rlen). If the result of the 4714 * comparison is nonzero returns it, otherwise the shorter one is 4715 * ordered first. 4716 */ 4717 int 4718 compare(const _CharT* __s) const; 4719 4720 // _GLIBCXX_RESOLVE_LIB_DEFECTS 4721 // 5 String::compare specification questionable 4722 /** 4723 * @brief Compare substring to a C string. 4724 * @param __pos Index of first character of substring. 4725 * @param __n1 Number of characters in substring. 4726 * @param __s C string to compare against. 4727 * @return Integer < 0, 0, or > 0. 4728 * 4729 * Form the substring of this string from the @a __n1 4730 * characters starting at @a pos. Returns an integer < 0 if 4731 * the substring is ordered before @a __s, 0 if their values 4732 * are equivalent, or > 0 if the substring is ordered after @a 4733 * __s. Determines the effective length rlen of the strings to 4734 * compare as the smallest of the length of the substring and 4735 * the length of a string constructed from @a __s. The 4736 * function then compares the two string by calling 4737 * traits::compare(substring.data(),__s,rlen). If the result of 4738 * the comparison is nonzero returns it, otherwise the shorter 4739 * one is ordered first. 4740 */ 4741 int 4742 compare(size_type __pos, size_type __n1, const _CharT* __s) const; 4743 4744 /** 4745 * @brief Compare substring against a character %array. 4746 * @param __pos Index of first character of substring. 4747 * @param __n1 Number of characters in substring. 4748 * @param __s character %array to compare against. 4749 * @param __n2 Number of characters of s. 4750 * @return Integer < 0, 0, or > 0. 4751 * 4752 * Form the substring of this string from the @a __n1 4753 * characters starting at @a __pos. Form a string from the 4754 * first @a __n2 characters of @a __s. Returns an integer < 0 4755 * if this substring is ordered before the string from @a __s, 4756 * 0 if their values are equivalent, or > 0 if this substring 4757 * is ordered after the string from @a __s. Determines the 4758 * effective length rlen of the strings to compare as the 4759 * smallest of the length of the substring and @a __n2. The 4760 * function then compares the two strings by calling 4761 * traits::compare(substring.data(),s,rlen). If the result of 4762 * the comparison is nonzero returns it, otherwise the shorter 4763 * one is ordered first. 4764 * 4765 * NB: s must have at least n2 characters, '\\0' has 4766 * no special meaning. 4767 */ 4768 int 4769 compare(size_type __pos, size_type __n1, const _CharT* __s, 4770 size_type __n2) const; 4771 }; 4772#endif // !_GLIBCXX_USE_CXX11_ABI 4773 4774 // operator+ 4775 /** 4776 * @brief Concatenate two strings. 4777 * @param __lhs First string. 4778 * @param __rhs Last string. 4779 * @return New string with value of @a __lhs followed by @a __rhs. 4780 */ 4781 template<typename _CharT, typename _Traits, typename _Alloc> 4782 basic_string<_CharT, _Traits, _Alloc> 4783 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 4784 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 4785 { 4786 basic_string<_CharT, _Traits, _Alloc> __str(__lhs); 4787 __str.append(__rhs); 4788 return __str; 4789 } 4790 4791 /** 4792 * @brief Concatenate C string and string. 4793 * @param __lhs First string. 4794 * @param __rhs Last string. 4795 * @return New string with value of @a __lhs followed by @a __rhs. 4796 */ 4797 template<typename _CharT, typename _Traits, typename _Alloc> 4798 basic_string<_CharT,_Traits,_Alloc> 4799 operator+(const _CharT* __lhs, 4800 const basic_string<_CharT,_Traits,_Alloc>& __rhs); 4801 4802 /** 4803 * @brief Concatenate character and string. 4804 * @param __lhs First string. 4805 * @param __rhs Last string. 4806 * @return New string with @a __lhs followed by @a __rhs. 4807 */ 4808 template<typename _CharT, typename _Traits, typename _Alloc> 4809 basic_string<_CharT,_Traits,_Alloc> 4810 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs); 4811 4812 /** 4813 * @brief Concatenate string and C string. 4814 * @param __lhs First string. 4815 * @param __rhs Last string. 4816 * @return New string with @a __lhs followed by @a __rhs. 4817 */ 4818 template<typename _CharT, typename _Traits, typename _Alloc> 4819 inline basic_string<_CharT, _Traits, _Alloc> 4820 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 4821 const _CharT* __rhs) 4822 { 4823 basic_string<_CharT, _Traits, _Alloc> __str(__lhs); 4824 __str.append(__rhs); 4825 return __str; 4826 } 4827 4828 /** 4829 * @brief Concatenate string and character. 4830 * @param __lhs First string. 4831 * @param __rhs Last string. 4832 * @return New string with @a __lhs followed by @a __rhs. 4833 */ 4834 template<typename _CharT, typename _Traits, typename _Alloc> 4835 inline basic_string<_CharT, _Traits, _Alloc> 4836 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs) 4837 { 4838 typedef basic_string<_CharT, _Traits, _Alloc> __string_type; 4839 typedef typename __string_type::size_type __size_type; 4840 __string_type __str(__lhs); 4841 __str.append(__size_type(1), __rhs); 4842 return __str; 4843 } 4844 4845#if __cplusplus >= 201103L 4846 template<typename _CharT, typename _Traits, typename _Alloc> 4847 inline basic_string<_CharT, _Traits, _Alloc> 4848 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, 4849 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 4850 { return std::move(__lhs.append(__rhs)); } 4851 4852 template<typename _CharT, typename _Traits, typename _Alloc> 4853 inline basic_string<_CharT, _Traits, _Alloc> 4854 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 4855 basic_string<_CharT, _Traits, _Alloc>&& __rhs) 4856 { return std::move(__rhs.insert(0, __lhs)); } 4857 4858 template<typename _CharT, typename _Traits, typename _Alloc> 4859 inline basic_string<_CharT, _Traits, _Alloc> 4860 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, 4861 basic_string<_CharT, _Traits, _Alloc>&& __rhs) 4862 { 4863 const auto __size = __lhs.size() + __rhs.size(); 4864 const bool __cond = (__size > __lhs.capacity() 4865 && __size <= __rhs.capacity()); 4866 return __cond ? std::move(__rhs.insert(0, __lhs)) 4867 : std::move(__lhs.append(__rhs)); 4868 } 4869 4870 template<typename _CharT, typename _Traits, typename _Alloc> 4871 inline basic_string<_CharT, _Traits, _Alloc> 4872 operator+(const _CharT* __lhs, 4873 basic_string<_CharT, _Traits, _Alloc>&& __rhs) 4874 { return std::move(__rhs.insert(0, __lhs)); } 4875 4876 template<typename _CharT, typename _Traits, typename _Alloc> 4877 inline basic_string<_CharT, _Traits, _Alloc> 4878 operator+(_CharT __lhs, 4879 basic_string<_CharT, _Traits, _Alloc>&& __rhs) 4880 { return std::move(__rhs.insert(0, 1, __lhs)); } 4881 4882 template<typename _CharT, typename _Traits, typename _Alloc> 4883 inline basic_string<_CharT, _Traits, _Alloc> 4884 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, 4885 const _CharT* __rhs) 4886 { return std::move(__lhs.append(__rhs)); } 4887 4888 template<typename _CharT, typename _Traits, typename _Alloc> 4889 inline basic_string<_CharT, _Traits, _Alloc> 4890 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, 4891 _CharT __rhs) 4892 { return std::move(__lhs.append(1, __rhs)); } 4893#endif 4894 4895 // operator == 4896 /** 4897 * @brief Test equivalence of two strings. 4898 * @param __lhs First string. 4899 * @param __rhs Second string. 4900 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise. 4901 */ 4902 template<typename _CharT, typename _Traits, typename _Alloc> 4903 inline bool 4904 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 4905 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 4906 { return __lhs.compare(__rhs) == 0; } 4907 4908 template<typename _CharT> 4909 inline 4910 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type 4911 operator==(const basic_string<_CharT>& __lhs, 4912 const basic_string<_CharT>& __rhs) 4913 { return (__lhs.size() == __rhs.size() 4914 && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(), 4915 __lhs.size())); } 4916 4917 /** 4918 * @brief Test equivalence of C string and string. 4919 * @param __lhs C string. 4920 * @param __rhs String. 4921 * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise. 4922 */ 4923 template<typename _CharT, typename _Traits, typename _Alloc> 4924 inline bool 4925 operator==(const _CharT* __lhs, 4926 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 4927 { return __rhs.compare(__lhs) == 0; } 4928 4929 /** 4930 * @brief Test equivalence of string and C string. 4931 * @param __lhs String. 4932 * @param __rhs C string. 4933 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise. 4934 */ 4935 template<typename _CharT, typename _Traits, typename _Alloc> 4936 inline bool 4937 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 4938 const _CharT* __rhs) 4939 { return __lhs.compare(__rhs) == 0; } 4940 4941 // operator != 4942 /** 4943 * @brief Test difference of two strings. 4944 * @param __lhs First string. 4945 * @param __rhs Second string. 4946 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise. 4947 */ 4948 template<typename _CharT, typename _Traits, typename _Alloc> 4949 inline bool 4950 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 4951 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 4952 { return !(__lhs == __rhs); } 4953 4954 /** 4955 * @brief Test difference of C string and string. 4956 * @param __lhs C string. 4957 * @param __rhs String. 4958 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise. 4959 */ 4960 template<typename _CharT, typename _Traits, typename _Alloc> 4961 inline bool 4962 operator!=(const _CharT* __lhs, 4963 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 4964 { return !(__lhs == __rhs); } 4965 4966 /** 4967 * @brief Test difference of string and C string. 4968 * @param __lhs String. 4969 * @param __rhs C string. 4970 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise. 4971 */ 4972 template<typename _CharT, typename _Traits, typename _Alloc> 4973 inline bool 4974 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 4975 const _CharT* __rhs) 4976 { return !(__lhs == __rhs); } 4977 4978 // operator < 4979 /** 4980 * @brief Test if string precedes string. 4981 * @param __lhs First string. 4982 * @param __rhs Second string. 4983 * @return True if @a __lhs precedes @a __rhs. False otherwise. 4984 */ 4985 template<typename _CharT, typename _Traits, typename _Alloc> 4986 inline bool 4987 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 4988 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 4989 { return __lhs.compare(__rhs) < 0; } 4990 4991 /** 4992 * @brief Test if string precedes C string. 4993 * @param __lhs String. 4994 * @param __rhs C string. 4995 * @return True if @a __lhs precedes @a __rhs. False otherwise. 4996 */ 4997 template<typename _CharT, typename _Traits, typename _Alloc> 4998 inline bool 4999 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 5000 const _CharT* __rhs) 5001 { return __lhs.compare(__rhs) < 0; } 5002 5003 /** 5004 * @brief Test if C string precedes string. 5005 * @param __lhs C string. 5006 * @param __rhs String. 5007 * @return True if @a __lhs precedes @a __rhs. False otherwise. 5008 */ 5009 template<typename _CharT, typename _Traits, typename _Alloc> 5010 inline bool 5011 operator<(const _CharT* __lhs, 5012 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 5013 { return __rhs.compare(__lhs) > 0; } 5014 5015 // operator > 5016 /** 5017 * @brief Test if string follows string. 5018 * @param __lhs First string. 5019 * @param __rhs Second string. 5020 * @return True if @a __lhs follows @a __rhs. False otherwise. 5021 */ 5022 template<typename _CharT, typename _Traits, typename _Alloc> 5023 inline bool 5024 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 5025 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 5026 { return __lhs.compare(__rhs) > 0; } 5027 5028 /** 5029 * @brief Test if string follows C string. 5030 * @param __lhs String. 5031 * @param __rhs C string. 5032 * @return True if @a __lhs follows @a __rhs. False otherwise. 5033 */ 5034 template<typename _CharT, typename _Traits, typename _Alloc> 5035 inline bool 5036 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 5037 const _CharT* __rhs) 5038 { return __lhs.compare(__rhs) > 0; } 5039 5040 /** 5041 * @brief Test if C string follows string. 5042 * @param __lhs C string. 5043 * @param __rhs String. 5044 * @return True if @a __lhs follows @a __rhs. False otherwise. 5045 */ 5046 template<typename _CharT, typename _Traits, typename _Alloc> 5047 inline bool 5048 operator>(const _CharT* __lhs, 5049 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 5050 { return __rhs.compare(__lhs) < 0; } 5051 5052 // operator <= 5053 /** 5054 * @brief Test if string doesn't follow string. 5055 * @param __lhs First string. 5056 * @param __rhs Second string. 5057 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise. 5058 */ 5059 template<typename _CharT, typename _Traits, typename _Alloc> 5060 inline bool 5061 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 5062 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 5063 { return __lhs.compare(__rhs) <= 0; } 5064 5065 /** 5066 * @brief Test if string doesn't follow C string. 5067 * @param __lhs String. 5068 * @param __rhs C string. 5069 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise. 5070 */ 5071 template<typename _CharT, typename _Traits, typename _Alloc> 5072 inline bool 5073 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 5074 const _CharT* __rhs) 5075 { return __lhs.compare(__rhs) <= 0; } 5076 5077 /** 5078 * @brief Test if C string doesn't follow string. 5079 * @param __lhs C string. 5080 * @param __rhs String. 5081 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise. 5082 */ 5083 template<typename _CharT, typename _Traits, typename _Alloc> 5084 inline bool 5085 operator<=(const _CharT* __lhs, 5086 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 5087 { return __rhs.compare(__lhs) >= 0; } 5088 5089 // operator >= 5090 /** 5091 * @brief Test if string doesn't precede string. 5092 * @param __lhs First string. 5093 * @param __rhs Second string. 5094 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise. 5095 */ 5096 template<typename _CharT, typename _Traits, typename _Alloc> 5097 inline bool 5098 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 5099 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 5100 { return __lhs.compare(__rhs) >= 0; } 5101 5102 /** 5103 * @brief Test if string doesn't precede C string. 5104 * @param __lhs String. 5105 * @param __rhs C string. 5106 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise. 5107 */ 5108 template<typename _CharT, typename _Traits, typename _Alloc> 5109 inline bool 5110 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 5111 const _CharT* __rhs) 5112 { return __lhs.compare(__rhs) >= 0; } 5113 5114 /** 5115 * @brief Test if C string doesn't precede string. 5116 * @param __lhs C string. 5117 * @param __rhs String. 5118 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise. 5119 */ 5120 template<typename _CharT, typename _Traits, typename _Alloc> 5121 inline bool 5122 operator>=(const _CharT* __lhs, 5123 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 5124 { return __rhs.compare(__lhs) <= 0; } 5125 5126 /** 5127 * @brief Swap contents of two strings. 5128 * @param __lhs First string. 5129 * @param __rhs Second string. 5130 * 5131 * Exchanges the contents of @a __lhs and @a __rhs in constant time. 5132 */ 5133 template<typename _CharT, typename _Traits, typename _Alloc> 5134 inline void 5135 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs, 5136 basic_string<_CharT, _Traits, _Alloc>& __rhs) 5137 { __lhs.swap(__rhs); } 5138 5139 5140 /** 5141 * @brief Read stream into a string. 5142 * @param __is Input stream. 5143 * @param __str Buffer to store into. 5144 * @return Reference to the input stream. 5145 * 5146 * Stores characters from @a __is into @a __str until whitespace is 5147 * found, the end of the stream is encountered, or str.max_size() 5148 * is reached. If is.width() is non-zero, that is the limit on the 5149 * number of characters stored into @a __str. Any previous 5150 * contents of @a __str are erased. 5151 */ 5152 template<typename _CharT, typename _Traits, typename _Alloc> 5153 basic_istream<_CharT, _Traits>& 5154 operator>>(basic_istream<_CharT, _Traits>& __is, 5155 basic_string<_CharT, _Traits, _Alloc>& __str); 5156 5157 template<> 5158 basic_istream<char>& 5159 operator>>(basic_istream<char>& __is, basic_string<char>& __str); 5160 5161 /** 5162 * @brief Write string to a stream. 5163 * @param __os Output stream. 5164 * @param __str String to write out. 5165 * @return Reference to the output stream. 5166 * 5167 * Output characters of @a __str into os following the same rules as for 5168 * writing a C string. 5169 */ 5170 template<typename _CharT, typename _Traits, typename _Alloc> 5171 inline basic_ostream<_CharT, _Traits>& 5172 operator<<(basic_ostream<_CharT, _Traits>& __os, 5173 const basic_string<_CharT, _Traits, _Alloc>& __str) 5174 { 5175 // _GLIBCXX_RESOLVE_LIB_DEFECTS 5176 // 586. string inserter not a formatted function 5177 return __ostream_insert(__os, __str.data(), __str.size()); 5178 } 5179 5180 /** 5181 * @brief Read a line from stream into a string. 5182 * @param __is Input stream. 5183 * @param __str Buffer to store into. 5184 * @param __delim Character marking end of line. 5185 * @return Reference to the input stream. 5186 * 5187 * Stores characters from @a __is into @a __str until @a __delim is 5188 * found, the end of the stream is encountered, or str.max_size() 5189 * is reached. Any previous contents of @a __str are erased. If 5190 * @a __delim is encountered, it is extracted but not stored into 5191 * @a __str. 5192 */ 5193 template<typename _CharT, typename _Traits, typename _Alloc> 5194 basic_istream<_CharT, _Traits>& 5195 getline(basic_istream<_CharT, _Traits>& __is, 5196 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim); 5197 5198 /** 5199 * @brief Read a line from stream into a string. 5200 * @param __is Input stream. 5201 * @param __str Buffer to store into. 5202 * @return Reference to the input stream. 5203 * 5204 * Stores characters from is into @a __str until '\n' is 5205 * found, the end of the stream is encountered, or str.max_size() 5206 * is reached. Any previous contents of @a __str are erased. If 5207 * end of line is encountered, it is extracted but not stored into 5208 * @a __str. 5209 */ 5210 template<typename _CharT, typename _Traits, typename _Alloc> 5211 inline basic_istream<_CharT, _Traits>& 5212 getline(basic_istream<_CharT, _Traits>& __is, 5213 basic_string<_CharT, _Traits, _Alloc>& __str) 5214 { return std::getline(__is, __str, __is.widen('\n')); } 5215 5216#if __cplusplus >= 201103L 5217 /// Read a line from an rvalue stream into a string. 5218 template<typename _CharT, typename _Traits, typename _Alloc> 5219 inline basic_istream<_CharT, _Traits>& 5220 getline(basic_istream<_CharT, _Traits>&& __is, 5221 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim) 5222 { return std::getline(__is, __str, __delim); } 5223 5224 /// Read a line from an rvalue stream into a string. 5225 template<typename _CharT, typename _Traits, typename _Alloc> 5226 inline basic_istream<_CharT, _Traits>& 5227 getline(basic_istream<_CharT, _Traits>&& __is, 5228 basic_string<_CharT, _Traits, _Alloc>& __str) 5229 { return std::getline(__is, __str); } 5230#endif 5231 5232 template<> 5233 basic_istream<char>& 5234 getline(basic_istream<char>& __in, basic_string<char>& __str, 5235 char __delim); 5236 5237#ifdef _GLIBCXX_USE_WCHAR_T 5238 template<> 5239 basic_istream<wchar_t>& 5240 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str, 5241 wchar_t __delim); 5242#endif 5243 5244_GLIBCXX_END_NAMESPACE_VERSION 5245} // namespace 5246 5247#if __cplusplus >= 201103L && defined(_GLIBCXX_USE_C99) 5248 5249#include <ext/string_conversions.h> 5250 5251namespace std _GLIBCXX_VISIBILITY(default) 5252{ 5253_GLIBCXX_BEGIN_NAMESPACE_VERSION 5254_GLIBCXX_BEGIN_NAMESPACE_CXX11 5255 5256 // 21.4 Numeric Conversions [string.conversions]. 5257 inline int 5258 stoi(const string& __str, size_t* __idx = 0, int __base = 10) 5259 { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(), 5260 __idx, __base); } 5261 5262 inline long 5263 stol(const string& __str, size_t* __idx = 0, int __base = 10) 5264 { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(), 5265 __idx, __base); } 5266 5267 inline unsigned long 5268 stoul(const string& __str, size_t* __idx = 0, int __base = 10) 5269 { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(), 5270 __idx, __base); } 5271 5272 inline long long 5273 stoll(const string& __str, size_t* __idx = 0, int __base = 10) 5274 { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(), 5275 __idx, __base); } 5276 5277 inline unsigned long long 5278 stoull(const string& __str, size_t* __idx = 0, int __base = 10) 5279 { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(), 5280 __idx, __base); } 5281 5282 // NB: strtof vs strtod. 5283 inline float 5284 stof(const string& __str, size_t* __idx = 0) 5285 { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); } 5286 5287 inline double 5288 stod(const string& __str, size_t* __idx = 0) 5289 { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); } 5290 5291 inline long double 5292 stold(const string& __str, size_t* __idx = 0) 5293 { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); } 5294 5295 // NB: (v)snprintf vs sprintf. 5296 5297 // DR 1261. 5298 inline string 5299 to_string(int __val) 5300 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(int), 5301 "%d", __val); } 5302 5303 inline string 5304 to_string(unsigned __val) 5305 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 5306 4 * sizeof(unsigned), 5307 "%u", __val); } 5308 5309 inline string 5310 to_string(long __val) 5311 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(long), 5312 "%ld", __val); } 5313 5314 inline string 5315 to_string(unsigned long __val) 5316 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 5317 4 * sizeof(unsigned long), 5318 "%lu", __val); } 5319 5320 inline string 5321 to_string(long long __val) 5322 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 5323 4 * sizeof(long long), 5324 "%lld", __val); } 5325 5326 inline string 5327 to_string(unsigned long long __val) 5328 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 5329 4 * sizeof(unsigned long long), 5330 "%llu", __val); } 5331 5332 inline string 5333 to_string(float __val) 5334 { 5335 const int __n = 5336 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20; 5337 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n, 5338 "%f", __val); 5339 } 5340 5341 inline string 5342 to_string(double __val) 5343 { 5344 const int __n = 5345 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20; 5346 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n, 5347 "%f", __val); 5348 } 5349 5350 inline string 5351 to_string(long double __val) 5352 { 5353 const int __n = 5354 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20; 5355 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n, 5356 "%Lf", __val); 5357 } 5358 5359#ifdef _GLIBCXX_USE_WCHAR_T 5360 inline int 5361 stoi(const wstring& __str, size_t* __idx = 0, int __base = 10) 5362 { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(), 5363 __idx, __base); } 5364 5365 inline long 5366 stol(const wstring& __str, size_t* __idx = 0, int __base = 10) 5367 { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(), 5368 __idx, __base); } 5369 5370 inline unsigned long 5371 stoul(const wstring& __str, size_t* __idx = 0, int __base = 10) 5372 { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(), 5373 __idx, __base); } 5374 5375 inline long long 5376 stoll(const wstring& __str, size_t* __idx = 0, int __base = 10) 5377 { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(), 5378 __idx, __base); } 5379 5380 inline unsigned long long 5381 stoull(const wstring& __str, size_t* __idx = 0, int __base = 10) 5382 { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(), 5383 __idx, __base); } 5384 5385 // NB: wcstof vs wcstod. 5386 inline float 5387 stof(const wstring& __str, size_t* __idx = 0) 5388 { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); } 5389 5390 inline double 5391 stod(const wstring& __str, size_t* __idx = 0) 5392 { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); } 5393 5394 inline long double 5395 stold(const wstring& __str, size_t* __idx = 0) 5396 { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); } 5397 5398#ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF 5399 // DR 1261. 5400 inline wstring 5401 to_wstring(int __val) 5402 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int), 5403 L"%d", __val); } 5404 5405 inline wstring 5406 to_wstring(unsigned __val) 5407 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 5408 4 * sizeof(unsigned), 5409 L"%u", __val); } 5410 5411 inline wstring 5412 to_wstring(long __val) 5413 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long), 5414 L"%ld", __val); } 5415 5416 inline wstring 5417 to_wstring(unsigned long __val) 5418 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 5419 4 * sizeof(unsigned long), 5420 L"%lu", __val); } 5421 5422 inline wstring 5423 to_wstring(long long __val) 5424 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 5425 4 * sizeof(long long), 5426 L"%lld", __val); } 5427 5428 inline wstring 5429 to_wstring(unsigned long long __val) 5430 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 5431 4 * sizeof(unsigned long long), 5432 L"%llu", __val); } 5433 5434 inline wstring 5435 to_wstring(float __val) 5436 { 5437 const int __n = 5438 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20; 5439 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n, 5440 L"%f", __val); 5441 } 5442 5443 inline wstring 5444 to_wstring(double __val) 5445 { 5446 const int __n = 5447 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20; 5448 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n, 5449 L"%f", __val); 5450 } 5451 5452 inline wstring 5453 to_wstring(long double __val) 5454 { 5455 const int __n = 5456 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20; 5457 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n, 5458 L"%Lf", __val); 5459 } 5460#endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF 5461#endif 5462 5463_GLIBCXX_END_NAMESPACE_CXX11 5464_GLIBCXX_END_NAMESPACE_VERSION 5465} // namespace 5466 5467#endif /* C++11 && _GLIBCXX_USE_C99 ... */ 5468 5469#if __cplusplus >= 201103L 5470 5471#include <bits/functional_hash.h> 5472 5473namespace std _GLIBCXX_VISIBILITY(default) 5474{ 5475_GLIBCXX_BEGIN_NAMESPACE_VERSION 5476 5477 // DR 1182. 5478 5479#ifndef _GLIBCXX_COMPATIBILITY_CXX0X 5480 /// std::hash specialization for string. 5481 template<> 5482 struct hash<string> 5483 : public __hash_base<size_t, string> 5484 { 5485 size_t 5486 operator()(const string& __s) const noexcept 5487 { return std::_Hash_impl::hash(__s.data(), __s.length()); } 5488 }; 5489 5490 template<> 5491 struct __is_fast_hash<hash<string>> : std::false_type 5492 { }; 5493 5494#ifdef _GLIBCXX_USE_WCHAR_T 5495 /// std::hash specialization for wstring. 5496 template<> 5497 struct hash<wstring> 5498 : public __hash_base<size_t, wstring> 5499 { 5500 size_t 5501 operator()(const wstring& __s) const noexcept 5502 { return std::_Hash_impl::hash(__s.data(), 5503 __s.length() * sizeof(wchar_t)); } 5504 }; 5505 5506 template<> 5507 struct __is_fast_hash<hash<wstring>> : std::false_type 5508 { }; 5509#endif 5510#endif /* _GLIBCXX_COMPATIBILITY_CXX0X */ 5511 5512#ifdef _GLIBCXX_USE_C99_STDINT_TR1 5513 /// std::hash specialization for u16string. 5514 template<> 5515 struct hash<u16string> 5516 : public __hash_base<size_t, u16string> 5517 { 5518 size_t 5519 operator()(const u16string& __s) const noexcept 5520 { return std::_Hash_impl::hash(__s.data(), 5521 __s.length() * sizeof(char16_t)); } 5522 }; 5523 5524 template<> 5525 struct __is_fast_hash<hash<u16string>> : std::false_type 5526 { }; 5527 5528 /// std::hash specialization for u32string. 5529 template<> 5530 struct hash<u32string> 5531 : public __hash_base<size_t, u32string> 5532 { 5533 size_t 5534 operator()(const u32string& __s) const noexcept 5535 { return std::_Hash_impl::hash(__s.data(), 5536 __s.length() * sizeof(char32_t)); } 5537 }; 5538 5539 template<> 5540 struct __is_fast_hash<hash<u32string>> : std::false_type 5541 { }; 5542#endif 5543 5544#if __cplusplus > 201103L 5545 5546#define __cpp_lib_string_udls 201304 5547 5548 inline namespace literals 5549 { 5550 inline namespace string_literals 5551 { 5552 5553 _GLIBCXX_DEFAULT_ABI_TAG 5554 inline basic_string<char> 5555 operator""s(const char* __str, size_t __len) 5556 { return basic_string<char>{__str, __len}; } 5557 5558#ifdef _GLIBCXX_USE_WCHAR_T 5559 _GLIBCXX_DEFAULT_ABI_TAG 5560 inline basic_string<wchar_t> 5561 operator""s(const wchar_t* __str, size_t __len) 5562 { return basic_string<wchar_t>{__str, __len}; } 5563#endif 5564 5565#ifdef _GLIBCXX_USE_C99_STDINT_TR1 5566 _GLIBCXX_DEFAULT_ABI_TAG 5567 inline basic_string<char16_t> 5568 operator""s(const char16_t* __str, size_t __len) 5569 { return basic_string<char16_t>{__str, __len}; } 5570 5571 _GLIBCXX_DEFAULT_ABI_TAG 5572 inline basic_string<char32_t> 5573 operator""s(const char32_t* __str, size_t __len) 5574 { return basic_string<char32_t>{__str, __len}; } 5575#endif 5576 5577 } // inline namespace string_literals 5578 } // inline namespace literals 5579 5580#endif // __cplusplus > 201103L 5581 5582_GLIBCXX_END_NAMESPACE_VERSION 5583} // namespace std 5584 5585#endif // C++11 5586 5587#endif /* _BASIC_STRING_H */ 5588