1 // Versatile string -*- C++ -*- 2 3 // Copyright (C) 2005, 2006, 2007, 2008, 2009 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 ext/vstring.h 26 * This file is a GNU extension to the Standard C++ Library. 27 */ 28 29 #ifndef _VSTRING_H 30 #define _VSTRING_H 1 31 32 #pragma GCC system_header 33 34 #include <initializer_list> 35 #include <ext/vstring_util.h> 36 #include <ext/rc_string_base.h> 37 #include <ext/sso_string_base.h> 38 39 #if __google_stl_debug_string && !defined(_GLIBCXX_DEBUG) 40 # undef _GLIBCXX_DEBUG_ASSERT 41 # undef _GLIBCXX_DEBUG_PEDASSERT 42 // Perform additional checks (but only in this file). 43 # define _GLIBCXX_DEBUG_ASSERT(_Condition) \ 44 if (! (_Condition)) { \ 45 char buf[512]; \ 46 __builtin_snprintf(buf, sizeof(buf), \ 47 "%s:%d: %s: Assertion '%s' failed.\n", \ 48 __FILE__, __LINE__, __func__, # _Condition); \ 49 std::__throw_runtime_error(buf); \ 50 } 51 # define _GLIBCXX_DEBUG_PEDASSERT(_Condition) _GLIBCXX_DEBUG_ASSERT(_Condition) 52 #endif 53 54 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) 55 56 /** 57 * @class __versa_string vstring.h 58 * @brief Managing sequences of characters and character-like objects. 59 */ 60 61 // Template class __versa_string 62 template<typename _CharT, typename _Traits, typename _Alloc, 63 template <typename, typename, typename> class _Base> 64 class __versa_string 65 : private _Base<_CharT, _Traits, _Alloc> 66 { 67 typedef _Base<_CharT, _Traits, _Alloc> __vstring_base; 68 typedef typename __vstring_base::_CharT_alloc_type _CharT_alloc_type; 69 70 // Types: 71 public: 72 typedef _Traits traits_type; 73 typedef typename _Traits::char_type value_type; 74 typedef _Alloc allocator_type; 75 typedef typename _CharT_alloc_type::size_type size_type; 76 typedef typename _CharT_alloc_type::difference_type difference_type; 77 typedef typename _CharT_alloc_type::reference reference; 78 typedef typename _CharT_alloc_type::const_reference const_reference; 79 typedef typename _CharT_alloc_type::pointer pointer; 80 typedef typename _CharT_alloc_type::const_pointer const_pointer; 81 typedef __gnu_cxx::__normal_iterator<pointer, __versa_string> iterator; 82 typedef __gnu_cxx::__normal_iterator<const_pointer, __versa_string> 83 const_iterator; 84 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 85 typedef std::reverse_iterator<iterator> reverse_iterator; 86 87 // Data Member (public): 88 /// Value returned by various member functions when they fail. 89 static const size_type npos = static_cast<size_type>(-1); 90 91 private: 92 size_type 93 _M_check(size_type __pos, const char* __s) const 94 { 95 if (__pos > this->size()) 96 std::__throw_out_of_range(__N(__s)); 97 return __pos; 98 } 99 100 void 101 _M_check_length(size_type __n1, size_type __n2, const char* __s) const 102 { 103 if (this->max_size() - (this->size() - __n1) < __n2) 104 std::__throw_length_error(__N(__s)); 105 } 106 107 // NB: _M_limit doesn't check for a bad __pos value. 108 size_type 109 _M_limit(size_type __pos, size_type __off) const 110 { 111 const bool __testoff = __off < this->size() - __pos; 112 return __testoff ? __off : this->size() - __pos; 113 } 114 115 // True if _Rep and source do not overlap. 116 bool 117 _M_disjunct(const _CharT* __s) const 118 { 119 return (std::less<const _CharT*>()(__s, this->_M_data()) 120 || std::less<const _CharT*>()(this->_M_data() 121 + this->size(), __s)); 122 } 123 124 // For the internal use we have functions similar to `begin'/`end' 125 // but they do not call _M_leak. 126 iterator 127 _M_ibegin() const 128 { return iterator(this->_M_data()); } 129 130 iterator 131 _M_iend() const 132 { return iterator(this->_M_data() + this->_M_length()); } 133 134 public: 135 // Construct/copy/destroy: 136 // NB: We overload ctors in some cases instead of using default 137 // arguments, per 17.4.4.4 para. 2 item 2. 138 139 /** 140 * @brief Default constructor creates an empty string. 141 */ 142 __versa_string() 143 : __vstring_base() { } 144 145 /** 146 * @brief Construct an empty string using allocator @a a. 147 */ 148 explicit 149 __versa_string(const _Alloc& __a) 150 : __vstring_base(__a) { } 151 152 // NB: per LWG issue 42, semantics different from IS: 153 /** 154 * @brief Construct string with copy of value of @a str. 155 * @param __str Source string. 156 */ 157 __versa_string(const __versa_string& __str) 158 : __vstring_base(__str) { } 159 160 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 161 /** 162 * @brief String move constructor. 163 * @param __str Source string. 164 * 165 * The newly-constructed %string contains the exact contents of 166 * @a str. The contents of @a str are a valid, but unspecified 167 * string. 168 */ 169 __versa_string(__versa_string&& __str) 170 : __vstring_base(std::move(__str)) { } 171 172 /** 173 * @brief Construct string from an initializer list. 174 * @param __l std::initializer_list of characters. 175 * @param __a Allocator to use (default is default allocator). 176 */ 177 __versa_string(std::initializer_list<_CharT> __l, 178 const _Alloc& __a = _Alloc()) 179 : __vstring_base(__l.begin(), __l.end(), __a) { } 180 #endif 181 182 /** 183 * @brief Construct string as copy of a substring. 184 * @param __str Source string. 185 * @param __pos Index of first character to copy from. 186 * @param __n Number of characters to copy (default remainder). 187 */ 188 __versa_string(const __versa_string& __str, size_type __pos, 189 size_type __n = npos) 190 : __vstring_base(__str._M_data() 191 + __str._M_check(__pos, 192 "__versa_string::__versa_string"), 193 __str._M_data() + __str._M_limit(__pos, __n) 194 + __pos, _Alloc()) { } 195 196 /** 197 * @brief Construct string as copy of a substring. 198 * @param __str Source string. 199 * @param __pos Index of first character to copy from. 200 * @param __n Number of characters to copy. 201 * @param __a Allocator to use. 202 */ 203 __versa_string(const __versa_string& __str, size_type __pos, 204 size_type __n, const _Alloc& __a) 205 : __vstring_base(__str._M_data() 206 + __str._M_check(__pos, 207 "__versa_string::__versa_string"), 208 __str._M_data() + __str._M_limit(__pos, __n) 209 + __pos, __a) { } 210 211 /** 212 * @brief Construct string initialized by a character array. 213 * @param __s Source character array. 214 * @param __n Number of characters to copy. 215 * @param __a Allocator to use (default is default allocator). 216 * 217 * NB: @a __s must have at least @a __n characters, '\\0' has no special 218 * meaning. 219 */ 220 __versa_string(const _CharT* __s, size_type __n, 221 const _Alloc& __a = _Alloc()) 222 : __vstring_base(__s, __s + __n, __a) { } 223 224 /** 225 * @brief Construct string as copy of a C string. 226 * @param __s Source C string. 227 * @param __a Allocator to use (default is default allocator). 228 */ 229 __versa_string(const _CharT* __s, const _Alloc& __a = _Alloc()) 230 : __vstring_base(__s, __s ? __s + traits_type::length(__s) : 231 __s + npos, __a) { } 232 233 /** 234 * @brief Construct string as multiple characters. 235 * @param __n Number of characters. 236 * @param __c Character to use. 237 * @param __a Allocator to use (default is default allocator). 238 */ 239 __versa_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc()) 240 : __vstring_base(__n, __c, __a) { } 241 242 /** 243 * @brief Construct string as copy of a range. 244 * @param __beg Start of range. 245 * @param __end End of range. 246 * @param __a Allocator to use (default is default allocator). 247 */ 248 template<class _InputIterator> 249 __versa_string(_InputIterator __beg, _InputIterator __end, 250 const _Alloc& __a = _Alloc()) 251 : __vstring_base(__beg, __end, __a) { } 252 253 /** 254 * @brief Destroy the string instance. 255 */ 256 ~__versa_string() { } 257 258 /** 259 * @brief Assign the value of @a str to this string. 260 * @param __str Source string. 261 */ 262 __versa_string& 263 operator=(const __versa_string& __str) 264 { return this->assign(__str); } 265 266 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 267 /** 268 * @brief String move assignment operator. 269 * @param __str Source string. 270 * 271 * The contents of @a __str are moved into this string (without 272 * copying). @a __str is a valid, but unspecified string. 273 */ 274 __versa_string& 275 operator=(__versa_string&& __str) 276 { 277 if (this != &__str) 278 this->swap(__str); 279 return *this; 280 } 281 282 /** 283 * @brief Set value to string constructed from initializer list. 284 * @param __l std::initializer_list. 285 */ 286 __versa_string& 287 operator=(std::initializer_list<_CharT> __l) 288 { 289 this->assign(__l.begin(), __l.end()); 290 return *this; 291 } 292 #endif 293 294 /** 295 * @brief Copy contents of @a __s into this string. 296 * @param __s Source null-terminated string. 297 */ 298 __versa_string& 299 operator=(const _CharT* __s) 300 { return this->assign(__s); } 301 302 /** 303 * @brief Set value to string of length 1. 304 * @param __c Source character. 305 * 306 * Assigning to a character makes this string length 1 and 307 * (*this)[0] == @a __c. 308 */ 309 __versa_string& 310 operator=(_CharT __c) 311 { 312 this->assign(1, __c); 313 return *this; 314 } 315 316 // Iterators: 317 /** 318 * Returns a read/write iterator that points to the first character in 319 * the %string. Unshares the string. 320 */ 321 iterator 322 begin() 323 { 324 this->_M_leak(); 325 return iterator(this->_M_data()); 326 } 327 328 /** 329 * Returns a read-only (constant) iterator that points to the first 330 * character in the %string. 331 */ 332 const_iterator 333 begin() const 334 { return const_iterator(this->_M_data()); } 335 336 /** 337 * Returns a read/write iterator that points one past the last 338 * character in the %string. Unshares the string. 339 */ 340 iterator 341 end() 342 { 343 this->_M_leak(); 344 return iterator(this->_M_data() + this->size()); 345 } 346 347 /** 348 * Returns a read-only (constant) iterator that points one past the 349 * last character in the %string. 350 */ 351 const_iterator 352 end() const 353 { return const_iterator(this->_M_data() + this->size()); } 354 355 /** 356 * Returns a read/write reverse iterator that points to the last 357 * character in the %string. Iteration is done in reverse element 358 * order. Unshares the string. 359 */ 360 reverse_iterator 361 rbegin() 362 { return reverse_iterator(this->end()); } 363 364 /** 365 * Returns a read-only (constant) reverse iterator that points 366 * to the last character in the %string. Iteration is done in 367 * reverse element order. 368 */ 369 const_reverse_iterator 370 rbegin() const 371 { return const_reverse_iterator(this->end()); } 372 373 /** 374 * Returns a read/write reverse iterator that points to one before the 375 * first character in the %string. Iteration is done in reverse 376 * element order. Unshares the string. 377 */ 378 reverse_iterator 379 rend() 380 { return reverse_iterator(this->begin()); } 381 382 /** 383 * Returns a read-only (constant) reverse iterator that points 384 * to one before the first character in the %string. Iteration 385 * is done in reverse element order. 386 */ 387 const_reverse_iterator 388 rend() const 389 { return const_reverse_iterator(this->begin()); } 390 391 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 392 /** 393 * Returns a read-only (constant) iterator that points to the first 394 * character in the %string. 395 */ 396 const_iterator 397 cbegin() const 398 { return const_iterator(this->_M_data()); } 399 400 /** 401 * Returns a read-only (constant) iterator that points one past the 402 * last character in the %string. 403 */ 404 const_iterator 405 cend() const 406 { return const_iterator(this->_M_data() + this->size()); } 407 408 /** 409 * Returns a read-only (constant) reverse iterator that points 410 * to the last character in the %string. Iteration is done in 411 * reverse element order. 412 */ 413 const_reverse_iterator 414 crbegin() const 415 { return const_reverse_iterator(this->end()); } 416 417 /** 418 * Returns a read-only (constant) reverse iterator that points 419 * to one before the first character in the %string. Iteration 420 * is done in reverse element order. 421 */ 422 const_reverse_iterator 423 crend() const 424 { return const_reverse_iterator(this->begin()); } 425 #endif 426 427 public: 428 // Capacity: 429 /// Returns the number of characters in the string, not including any 430 /// null-termination. 431 size_type 432 size() const 433 { return this->_M_length(); } 434 435 /// Returns the number of characters in the string, not including any 436 /// null-termination. 437 size_type 438 length() const 439 { return this->_M_length(); } 440 441 /// Returns the size() of the largest possible %string. 442 size_type 443 max_size() const 444 { return this->_M_max_size(); } 445 446 /** 447 * @brief Resizes the %string to the specified number of characters. 448 * @param __n Number of characters the %string should contain. 449 * @param __c Character to fill any new elements. 450 * 451 * This function will %resize the %string to the specified 452 * number of characters. If the number is smaller than the 453 * %string's current size the %string is truncated, otherwise 454 * the %string is extended and new elements are set to @a __c. 455 */ 456 void 457 resize(size_type __n, _CharT __c); 458 459 /** 460 * @brief Resizes the %string to the specified number of characters. 461 * @param __n Number of characters the %string should contain. 462 * 463 * This function will resize the %string to the specified 464 * length. If the new size is smaller than the %string's 465 * current size the %string is truncated, otherwise the %string 466 * is extended and new characters are default-constructed. For 467 * basic types such as char, this means setting them to 0. 468 */ 469 void 470 resize(size_type __n) 471 { this->resize(__n, _CharT()); } 472 473 /** 474 * Returns the total number of characters that the %string can 475 * hold before needing to allocate more memory. 476 */ 477 size_type 478 capacity() const 479 { return this->_M_capacity(); } 480 481 /** 482 * @brief Attempt to preallocate enough memory for specified number of 483 * characters. 484 * @param __res_arg Number of characters required. 485 * @throw std::length_error If @a __res_arg exceeds @c max_size(). 486 * 487 * This function attempts to reserve enough memory for the 488 * %string to hold the specified number of characters. If the 489 * number requested is more than max_size(), length_error is 490 * thrown. 491 * 492 * The advantage of this function is that if optimal code is a 493 * necessity and the user can determine the string length that 494 * will be required, the user can reserve the memory in 495 * %advance, and thus prevent a possible reallocation of memory 496 * and copying of %string data. 497 */ 498 void 499 reserve(size_type __res_arg = 0) 500 { this->_M_reserve(__res_arg); } 501 502 /** 503 * Erases the string, making it empty. 504 */ 505 void 506 clear() 507 { this->_M_clear(); } 508 509 /** 510 * Returns true if the %string is empty. Equivalent to *this == "". 511 */ 512 bool 513 empty() const 514 { return this->size() == 0; } 515 516 // Element access: 517 /** 518 * @brief Subscript access to the data contained in the %string. 519 * @param __pos The index of the character to access. 520 * @return Read-only (constant) reference to the character. 521 * 522 * This operator allows for easy, array-style, data access. 523 * Note that data access with this operator is unchecked and 524 * out_of_range lookups are not defined. (For checked lookups 525 * see at().) 526 */ 527 const_reference 528 operator[] (size_type __pos) const 529 { 530 _GLIBCXX_DEBUG_ASSERT(__pos <= this->size()); 531 return this->_M_data()[__pos]; 532 } 533 534 /** 535 * @brief Subscript access to the data contained in the %string. 536 * @param __pos The index of the character to access. 537 * @return Read/write reference to the character. 538 * 539 * This operator allows for easy, array-style, data access. 540 * Note that data access with this operator is unchecked and 541 * out_of_range lookups are not defined. (For checked lookups 542 * see at().) Unshares the string. 543 */ 544 reference 545 operator[](size_type __pos) 546 { 547 // allow pos == size() as v3 extension: 548 _GLIBCXX_DEBUG_ASSERT(__pos <= this->size()); 549 // but be strict in pedantic mode: 550 _GLIBCXX_DEBUG_PEDASSERT(__pos < this->size()); 551 this->_M_leak(); 552 return this->_M_data()[__pos]; 553 } 554 555 /** 556 * @brief Provides access to the data contained in the %string. 557 * @param __n The index of the character to access. 558 * @return Read-only (const) reference to the character. 559 * @throw std::out_of_range If @a __n is an invalid index. 560 * 561 * This function provides for safer data access. The parameter 562 * is first checked that it is in the range of the string. The 563 * function throws out_of_range if the check fails. 564 */ 565 const_reference 566 at(size_type __n) const 567 { 568 if (__n >= this->size()) 569 std::__throw_out_of_range(__N("__versa_string::at")); 570 return this->_M_data()[__n]; 571 } 572 573 /** 574 * @brief Provides access to the data contained in the %string. 575 * @param __n The index of the character to access. 576 * @return Read/write reference to the character. 577 * @throw std::out_of_range If @a __n is an invalid index. 578 * 579 * This function provides for safer data access. The parameter 580 * is first checked that it is in the range of the string. The 581 * function throws out_of_range if the check fails. Success 582 * results in unsharing the string. 583 */ 584 reference 585 at(size_type __n) 586 { 587 if (__n >= this->size()) 588 std::__throw_out_of_range(__N("__versa_string::at")); 589 this->_M_leak(); 590 return this->_M_data()[__n]; 591 } 592 593 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 594 /** 595 * Returns a read/write reference to the data at the first 596 * element of the %string. 597 */ 598 reference 599 front() 600 { return *begin(); } 601 602 /** 603 * Returns a read-only (constant) reference to the data at the first 604 * element of the %string. 605 */ 606 const_reference 607 front() const 608 { return *begin(); } 609 610 /** 611 * Returns a read/write reference to the data at the last 612 * element of the %string. 613 */ 614 reference 615 back() 616 { return *(end() - 1); } 617 618 /** 619 * Returns a read-only (constant) reference to the data at the 620 * last element of the %string. 621 */ 622 const_reference 623 back() const 624 { return *(end() - 1); } 625 #endif 626 627 // Modifiers: 628 /** 629 * @brief Append a string to this string. 630 * @param __str The string to append. 631 * @return Reference to this string. 632 */ 633 __versa_string& 634 operator+=(const __versa_string& __str) 635 { return this->append(__str); } 636 637 /** 638 * @brief Append a C string. 639 * @param __s The C string to append. 640 * @return Reference to this string. 641 */ 642 __versa_string& 643 operator+=(const _CharT* __s) 644 { return this->append(__s); } 645 646 /** 647 * @brief Append a character. 648 * @param __c The character to append. 649 * @return Reference to this string. 650 */ 651 __versa_string& 652 operator+=(_CharT __c) 653 { 654 this->push_back(__c); 655 return *this; 656 } 657 658 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 659 /** 660 * @brief Append an initializer_list of characters. 661 * @param __l The initializer_list of characters to be appended. 662 * @return Reference to this string. 663 */ 664 __versa_string& 665 operator+=(std::initializer_list<_CharT> __l) 666 { return this->append(__l.begin(), __l.end()); } 667 #endif // __GXX_EXPERIMENTAL_CXX0X__ 668 669 /** 670 * @brief Append a string to this string. 671 * @param __str The string to append. 672 * @return Reference to this string. 673 */ 674 __versa_string& 675 append(const __versa_string& __str) 676 { return _M_append(__str._M_data(), __str.size()); } 677 678 /** 679 * @brief Append a substring. 680 * @param __str The string to append. 681 * @param __pos Index of the first character of str to append. 682 * @param __n The number of characters to append. 683 * @return Reference to this string. 684 * @throw std::out_of_range if @a pos is not a valid index. 685 * 686 * This function appends @a __n characters from @a __str 687 * starting at @a __pos to this string. If @a __n is is larger 688 * than the number of available characters in @a __str, the 689 * remainder of @a __str is appended. 690 */ 691 __versa_string& 692 append(const __versa_string& __str, size_type __pos, size_type __n) 693 { return _M_append(__str._M_data() 694 + __str._M_check(__pos, "__versa_string::append"), 695 __str._M_limit(__pos, __n)); } 696 697 /** 698 * @brief Append a C substring. 699 * @param __s The C string to append. 700 * @param __n The number of characters to append. 701 * @return Reference to this string. 702 */ 703 __versa_string& 704 append(const _CharT* __s, size_type __n) 705 { 706 __glibcxx_requires_string_len(__s, __n); 707 _M_check_length(size_type(0), __n, "__versa_string::append"); 708 return _M_append(__s, __n); 709 } 710 711 /** 712 * @brief Append a C string. 713 * @param __s The C string to append. 714 * @return Reference to this string. 715 */ 716 __versa_string& 717 append(const _CharT* __s) 718 { 719 __glibcxx_requires_string(__s); 720 const size_type __n = traits_type::length(__s); 721 _M_check_length(size_type(0), __n, "__versa_string::append"); 722 return _M_append(__s, __n); 723 } 724 725 /** 726 * @brief Append multiple characters. 727 * @param __n The number of characters to append. 728 * @param __c The character to use. 729 * @return Reference to this string. 730 * 731 * Appends n copies of c to this string. 732 */ 733 __versa_string& 734 append(size_type __n, _CharT __c) 735 { return _M_replace_aux(this->size(), size_type(0), __n, __c); } 736 737 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 738 /** 739 * @brief Append an initializer_list of characters. 740 * @param __l The initializer_list of characters to append. 741 * @return Reference to this string. 742 */ 743 __versa_string& 744 append(std::initializer_list<_CharT> __l) 745 { return this->append(__l.begin(), __l.end()); } 746 #endif // __GXX_EXPERIMENTAL_CXX0X__ 747 748 /** 749 * @brief Append a range of characters. 750 * @param __first Iterator referencing the first character to append. 751 * @param __last Iterator marking the end of the range. 752 * @return Reference to this string. 753 * 754 * Appends characters in the range [first,last) to this string. 755 */ 756 template<class _InputIterator> 757 __versa_string& 758 append(_InputIterator __first, _InputIterator __last) 759 { return this->replace(_M_iend(), _M_iend(), __first, __last); } 760 761 /** 762 * @brief Append a single character. 763 * @param __c Character to append. 764 */ 765 void 766 push_back(_CharT __c) 767 { 768 const size_type __size = this->size(); 769 if (__size + 1 > this->capacity() || this->_M_is_shared()) 770 this->_M_mutate(__size, size_type(0), 0, size_type(1)); 771 traits_type::assign(this->_M_data()[__size], __c); 772 this->_M_set_length(__size + 1); 773 } 774 775 /** 776 * @brief Set value to contents of another string. 777 * @param __str Source string to use. 778 * @return Reference to this string. 779 */ 780 __versa_string& 781 assign(const __versa_string& __str) 782 { 783 this->_M_assign(__str); 784 return *this; 785 } 786 787 /** 788 * @brief Set value to a substring of a string. 789 * @param __str The string to use. 790 * @param __pos Index of the first character of str. 791 * @param __n Number of characters to use. 792 * @return Reference to this string. 793 * @throw std::out_of_range if @a __pos is not a valid index. 794 * 795 * This function sets this string to the substring of @a __str 796 * consisting of @a __n characters at @a __pos. If @a __n is 797 * is larger than the number of available characters in @a 798 * __str, the remainder of @a __str is used. 799 */ 800 __versa_string& 801 assign(const __versa_string& __str, size_type __pos, size_type __n) 802 { return _M_replace(size_type(0), this->size(), __str._M_data() 803 + __str._M_check(__pos, "__versa_string::assign"), 804 __str._M_limit(__pos, __n)); } 805 806 /** 807 * @brief Set value to a C substring. 808 * @param __s The C string to use. 809 * @param __n Number of characters to use. 810 * @return Reference to this string. 811 * 812 * This function sets the value of this string to the first @a 813 * __n characters of @a __s. If @a __n is is larger than the 814 * number of available characters in @a __s, the remainder of 815 * @a __s is used. 816 */ 817 __versa_string& 818 assign(const _CharT* __s, size_type __n) 819 { 820 __glibcxx_requires_string_len(__s, __n); 821 return _M_replace(size_type(0), this->size(), __s, __n); 822 } 823 824 /** 825 * @brief Set value to contents of a C string. 826 * @param __s The C string to use. 827 * @return Reference to this string. 828 * 829 * This function sets the value of this string to the value of 830 * @a __s. The data is copied, so there is no dependence on @a 831 * __s once the function returns. 832 */ 833 __versa_string& 834 assign(const _CharT* __s) 835 { 836 __glibcxx_requires_string(__s); 837 return _M_replace(size_type(0), this->size(), __s, 838 traits_type::length(__s)); 839 } 840 841 /** 842 * @brief Set value to multiple characters. 843 * @param __n Length of the resulting string. 844 * @param __c The character to use. 845 * @return Reference to this string. 846 * 847 * This function sets the value of this string to @a __n copies of 848 * character @a __c. 849 */ 850 __versa_string& 851 assign(size_type __n, _CharT __c) 852 { return _M_replace_aux(size_type(0), this->size(), __n, __c); } 853 854 /** 855 * @brief Set value to a range of characters. 856 * @param __first Iterator referencing the first character to append. 857 * @param __last Iterator marking the end of the range. 858 * @return Reference to this string. 859 * 860 * Sets value of string to characters in the range 861 * [first,last). 862 */ 863 template<class _InputIterator> 864 __versa_string& 865 assign(_InputIterator __first, _InputIterator __last) 866 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); } 867 868 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 869 /** 870 * @brief Set value to an initializer_list of characters. 871 * @param __l The initializer_list of characters to assign. 872 * @return Reference to this string. 873 */ 874 __versa_string& 875 assign(std::initializer_list<_CharT> __l) 876 { return this->assign(__l.begin(), __l.end()); } 877 #endif // __GXX_EXPERIMENTAL_CXX0X__ 878 879 /** 880 * @brief Insert multiple characters. 881 * @param __p Iterator referencing location in string to insert at. 882 * @param __n Number of characters to insert 883 * @param __c The character to insert. 884 * @throw std::length_error If new length exceeds @c max_size(). 885 * 886 * Inserts @a __n copies of character @a __c starting at the 887 * position referenced by iterator @a __p. If adding 888 * characters causes the length to exceed max_size(), 889 * length_error is thrown. The value of the string doesn't 890 * change if an error is thrown. 891 */ 892 void 893 insert(iterator __p, size_type __n, _CharT __c) 894 { this->replace(__p, __p, __n, __c); } 895 896 /** 897 * @brief Insert a range of characters. 898 * @param __p Iterator referencing location in string to insert at. 899 * @param __beg Start of range. 900 * @param __end End of range. 901 * @throw std::length_error If new length exceeds @c max_size(). 902 * 903 * Inserts characters in range [beg,end). If adding characters 904 * causes the length to exceed max_size(), length_error is 905 * thrown. The value of the string doesn't change if an error 906 * is thrown. 907 */ 908 template<class _InputIterator> 909 void 910 insert(iterator __p, _InputIterator __beg, _InputIterator __end) 911 { this->replace(__p, __p, __beg, __end); } 912 913 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 914 /** 915 * @brief Insert an initializer_list of characters. 916 * @param __p Iterator referencing location in string to insert at. 917 * @param __l The initializer_list of characters to insert. 918 * @throw std::length_error If new length exceeds @c max_size(). 919 */ 920 void 921 insert(iterator __p, std::initializer_list<_CharT> __l) 922 { this->insert(__p, __l.begin(), __l.end()); } 923 #endif // __GXX_EXPERIMENTAL_CXX0X__ 924 925 /** 926 * @brief Insert value of a string. 927 * @param __pos1 Iterator referencing location in string to insert at. 928 * @param __str The string to insert. 929 * @return Reference to this string. 930 * @throw std::length_error If new length exceeds @c max_size(). 931 * 932 * Inserts value of @a __str starting at @a __pos1. If adding 933 * characters causes the length to exceed max_size(), 934 * length_error is thrown. The value of the string doesn't 935 * change if an error is thrown. 936 */ 937 __versa_string& 938 insert(size_type __pos1, const __versa_string& __str) 939 { return this->replace(__pos1, size_type(0), 940 __str._M_data(), __str.size()); } 941 942 /** 943 * @brief Insert a substring. 944 * @param __pos1 Iterator referencing location in string to insert at. 945 * @param __str The string to insert. 946 * @param __pos2 Start of characters in str to insert. 947 * @param __n Number of characters to insert. 948 * @return Reference to this string. 949 * @throw std::length_error If new length exceeds @c max_size(). 950 * @throw std::out_of_range If @a __pos1 > size() or 951 * @a __pos2 > @a __str.size(). 952 * 953 * Starting at @a __pos1, insert @a __n character of @a __str 954 * beginning with @a __pos2. If adding characters causes the 955 * length to exceed max_size(), length_error is thrown. If @a 956 * __pos1 is beyond the end of this string or @a __pos2 is 957 * beyond the end of @a __str, out_of_range is thrown. The 958 * value of the string doesn't change if an error is thrown. 959 */ 960 __versa_string& 961 insert(size_type __pos1, const __versa_string& __str, 962 size_type __pos2, size_type __n) 963 { return this->replace(__pos1, size_type(0), __str._M_data() 964 + __str._M_check(__pos2, "__versa_string::insert"), 965 __str._M_limit(__pos2, __n)); } 966 967 /** 968 * @brief Insert a C substring. 969 * @param __pos Iterator referencing location in string to insert at. 970 * @param __s The C string to insert. 971 * @param __n The number of characters to insert. 972 * @return Reference to this string. 973 * @throw std::length_error If new length exceeds @c max_size(). 974 * @throw std::out_of_range If @a __pos is beyond the end of this 975 * string. 976 * 977 * Inserts the first @a __n characters of @a __s starting at @a 978 * __pos. If adding characters causes the length to exceed 979 * max_size(), length_error is thrown. If @a __pos is beyond 980 * end(), out_of_range is thrown. The value of the string 981 * doesn't change if an error is thrown. 982 */ 983 __versa_string& 984 insert(size_type __pos, const _CharT* __s, size_type __n) 985 { return this->replace(__pos, size_type(0), __s, __n); } 986 987 /** 988 * @brief Insert a C string. 989 * @param __pos Iterator referencing location in string to insert at. 990 * @param __s The C string to insert. 991 * @return Reference to this string. 992 * @throw std::length_error If new length exceeds @c max_size(). 993 * @throw std::out_of_range If @a __pos is beyond the end of this 994 * string. 995 * 996 * Inserts the first @a __n characters of @a __s starting at @a 997 * __pos. If adding characters causes the length to exceed 998 * max_size(), length_error is thrown. If @a __pos is beyond 999 * end(), out_of_range is thrown. The value of the string 1000 * doesn't change if an error is thrown. 1001 */ 1002 __versa_string& 1003 insert(size_type __pos, const _CharT* __s) 1004 { 1005 __glibcxx_requires_string(__s); 1006 return this->replace(__pos, size_type(0), __s, 1007 traits_type::length(__s)); 1008 } 1009 1010 /** 1011 * @brief Insert multiple characters. 1012 * @param __pos Index in string to insert at. 1013 * @param __n Number of characters to insert 1014 * @param __c The character to insert. 1015 * @return Reference to this string. 1016 * @throw std::length_error If new length exceeds @c max_size(). 1017 * @throw std::out_of_range If @a __pos is beyond the end of this 1018 * string. 1019 * 1020 * Inserts @a __n copies of character @a __c starting at index 1021 * @a __pos. If adding characters causes the length to exceed 1022 * max_size(), length_error is thrown. If @a __pos > length(), 1023 * out_of_range is thrown. The value of the string doesn't 1024 * change if an error is thrown. 1025 */ 1026 __versa_string& 1027 insert(size_type __pos, size_type __n, _CharT __c) 1028 { return _M_replace_aux(_M_check(__pos, "__versa_string::insert"), 1029 size_type(0), __n, __c); } 1030 1031 /** 1032 * @brief Insert one character. 1033 * @param __p Iterator referencing position in string to insert at. 1034 * @param __c The character to insert. 1035 * @return Iterator referencing newly inserted char. 1036 * @throw std::length_error If new length exceeds @c max_size(). 1037 * 1038 * Inserts character @a __c at position referenced by @a __p. 1039 * If adding character causes the length to exceed max_size(), 1040 * length_error is thrown. If @a __p is beyond end of string, 1041 * out_of_range is thrown. The value of the string doesn't 1042 * change if an error is thrown. 1043 */ 1044 iterator 1045 insert(iterator __p, _CharT __c) 1046 { 1047 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend()); 1048 const size_type __pos = __p - _M_ibegin(); 1049 _M_replace_aux(__pos, size_type(0), size_type(1), __c); 1050 this->_M_set_leaked(); 1051 return iterator(this->_M_data() + __pos); 1052 } 1053 1054 /** 1055 * @brief Remove characters. 1056 * @param __pos Index of first character to remove (default 0). 1057 * @param __n Number of characters to remove (default remainder). 1058 * @return Reference to this string. 1059 * @throw std::out_of_range If @a __pos is beyond the end of this 1060 * string. 1061 * 1062 * Removes @a __n characters from this string starting at @a 1063 * __pos. The length of the string is reduced by @a __n. If 1064 * there are < @a __n characters to remove, the remainder of 1065 * the string is truncated. If @a __p is beyond end of string, 1066 * out_of_range is thrown. The value of the string doesn't 1067 * change if an error is thrown. 1068 */ 1069 __versa_string& 1070 erase(size_type __pos = 0, size_type __n = npos) 1071 { 1072 this->_M_erase(_M_check(__pos, "__versa_string::erase"), 1073 _M_limit(__pos, __n)); 1074 return *this; 1075 } 1076 1077 /** 1078 * @brief Remove one character. 1079 * @param __position Iterator referencing the character to remove. 1080 * @return iterator referencing same location after removal. 1081 * 1082 * Removes the character at @a __position from this string. The 1083 * value of the string doesn't change if an error is thrown. 1084 */ 1085 iterator 1086 erase(iterator __position) 1087 { 1088 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin() 1089 && __position < _M_iend()); 1090 const size_type __pos = __position - _M_ibegin(); 1091 this->_M_erase(__pos, size_type(1)); 1092 this->_M_set_leaked(); 1093 return iterator(this->_M_data() + __pos); 1094 } 1095 1096 /** 1097 * @brief Remove a range of characters. 1098 * @param __first Iterator referencing the first character to remove. 1099 * @param __last Iterator referencing the end of the range. 1100 * @return Iterator referencing location of first after removal. 1101 * 1102 * Removes the characters in the range [first,last) from this 1103 * string. The value of the string doesn't change if an error 1104 * is thrown. 1105 */ 1106 iterator 1107 erase(iterator __first, iterator __last) 1108 { 1109 _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last 1110 && __last <= _M_iend()); 1111 const size_type __pos = __first - _M_ibegin(); 1112 this->_M_erase(__pos, __last - __first); 1113 this->_M_set_leaked(); 1114 return iterator(this->_M_data() + __pos); 1115 } 1116 1117 /** 1118 * @brief Replace characters with value from another string. 1119 * @param __pos Index of first character to replace. 1120 * @param __n Number of characters to be replaced. 1121 * @param __str String to insert. 1122 * @return Reference to this string. 1123 * @throw std::out_of_range If @a __pos is beyond the end of this 1124 * string. 1125 * @throw std::length_error If new length exceeds @c max_size(). 1126 * 1127 * Removes the characters in the range [pos,pos+n) from this 1128 * string. In place, the value of @a __str is inserted. If @a 1129 * __pos is beyond end of string, out_of_range is thrown. If 1130 * the length of the result exceeds max_size(), length_error is 1131 * thrown. The value of the string doesn't change if an error 1132 * is thrown. 1133 */ 1134 __versa_string& 1135 replace(size_type __pos, size_type __n, const __versa_string& __str) 1136 { return this->replace(__pos, __n, __str._M_data(), __str.size()); } 1137 1138 /** 1139 * @brief Replace characters with value from another string. 1140 * @param __pos1 Index of first character to replace. 1141 * @param __n1 Number of characters to be replaced. 1142 * @param __str String to insert. 1143 * @param __pos2 Index of first character of str to use. 1144 * @param __n2 Number of characters from str to use. 1145 * @return Reference to this string. 1146 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 > 1147 * str.size(). 1148 * @throw std::length_error If new length exceeds @c max_size(). 1149 * 1150 * Removes the characters in the range [pos1,pos1 + n) from 1151 * this string. In place, the value of @a __str is inserted. 1152 * If @a __pos is beyond end of string, out_of_range is thrown. 1153 * If the length of the result exceeds max_size(), length_error 1154 * is thrown. The value of the string doesn't change if an 1155 * error is thrown. 1156 */ 1157 __versa_string& 1158 replace(size_type __pos1, size_type __n1, const __versa_string& __str, 1159 size_type __pos2, size_type __n2) 1160 { 1161 return this->replace(__pos1, __n1, __str._M_data() 1162 + __str._M_check(__pos2, 1163 "__versa_string::replace"), 1164 __str._M_limit(__pos2, __n2)); 1165 } 1166 1167 /** 1168 * @brief Replace characters with value of a C substring. 1169 * @param __pos Index of first character to replace. 1170 * @param __n1 Number of characters to be replaced. 1171 * @param __s C string to insert. 1172 * @param __n2 Number of characters from @a __s to use. 1173 * @return Reference to this string. 1174 * @throw std::out_of_range If @a __pos1 > size(). 1175 * @throw std::length_error If new length exceeds @c max_size(). 1176 * 1177 * Removes the characters in the range [pos,pos + n1) from this 1178 * string. In place, the first @a __n2 characters of @a __s 1179 * are inserted, or all of @a __s if @a __n2 is too large. If 1180 * @a __pos is beyond end of string, out_of_range is thrown. 1181 * If the length of result exceeds max_size(), length_error is 1182 * thrown. The value of the string doesn't change if an error 1183 * is thrown. 1184 */ 1185 __versa_string& 1186 replace(size_type __pos, size_type __n1, const _CharT* __s, 1187 size_type __n2) 1188 { 1189 __glibcxx_requires_string_len(__s, __n2); 1190 return _M_replace(_M_check(__pos, "__versa_string::replace"), 1191 _M_limit(__pos, __n1), __s, __n2); 1192 } 1193 1194 /** 1195 * @brief Replace characters with value of a C string. 1196 * @param __pos Index of first character to replace. 1197 * @param __n1 Number of characters to be replaced. 1198 * @param __s C string to insert. 1199 * @return Reference to this string. 1200 * @throw std::out_of_range If @a __pos > size(). 1201 * @throw std::length_error If new length exceeds @c max_size(). 1202 * 1203 * Removes the characters in the range [pos,pos + n1) from this 1204 * string. In place, the first @a __n characters of @a __s are 1205 * inserted. If @a pos is beyond end of string, out_of_range 1206 * is thrown. If the length of result exceeds max_size(), 1207 * length_error is thrown. The value of the string doesn't 1208 * change if an error is thrown. 1209 */ 1210 __versa_string& 1211 replace(size_type __pos, size_type __n1, const _CharT* __s) 1212 { 1213 __glibcxx_requires_string(__s); 1214 return this->replace(__pos, __n1, __s, traits_type::length(__s)); 1215 } 1216 1217 /** 1218 * @brief Replace characters with multiple characters. 1219 * @param __pos Index of first character to replace. 1220 * @param __n1 Number of characters to be replaced. 1221 * @param __n2 Number of characters to insert. 1222 * @param __c Character to insert. 1223 * @return Reference to this string. 1224 * @throw std::out_of_range If @a __pos > size(). 1225 * @throw std::length_error If new length exceeds @c max_size(). 1226 * 1227 * Removes the characters in the range [pos,pos + n1) from this 1228 * string. In place, @a __n2 copies of @a __c are inserted. 1229 * If @a __pos is beyond end of string, out_of_range is thrown. 1230 * If the length of result exceeds max_size(), length_error is 1231 * thrown. The value of the string doesn't change if an error 1232 * is thrown. 1233 */ 1234 __versa_string& 1235 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c) 1236 { return _M_replace_aux(_M_check(__pos, "__versa_string::replace"), 1237 _M_limit(__pos, __n1), __n2, __c); } 1238 1239 /** 1240 * @brief Replace range of characters with string. 1241 * @param __i1 Iterator referencing start of range to replace. 1242 * @param __i2 Iterator referencing end of range to replace. 1243 * @param __str String value to insert. 1244 * @return Reference to this string. 1245 * @throw std::length_error If new length exceeds @c max_size(). 1246 * 1247 * Removes the characters in the range [i1,i2). In place, the 1248 * value of @a __str is inserted. If the length of result 1249 * exceeds max_size(), length_error is thrown. The value of 1250 * the string doesn't change if an error is thrown. 1251 */ 1252 __versa_string& 1253 replace(iterator __i1, iterator __i2, const __versa_string& __str) 1254 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); } 1255 1256 /** 1257 * @brief Replace range of characters with C substring. 1258 * @param __i1 Iterator referencing start of range to replace. 1259 * @param __i2 Iterator referencing end of range to replace. 1260 * @param __s C string value to insert. 1261 * @param __n Number of characters from s to insert. 1262 * @return Reference to this string. 1263 * @throw std::length_error If new length exceeds @c max_size(). 1264 * 1265 * Removes the characters in the range [i1,i2). In place, the 1266 * first @a n characters of @a __s are inserted. If the length 1267 * of result exceeds max_size(), length_error is thrown. The 1268 * value of the string doesn't change if an error is thrown. 1269 */ 1270 __versa_string& 1271 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n) 1272 { 1273 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 1274 && __i2 <= _M_iend()); 1275 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n); 1276 } 1277 1278 /** 1279 * @brief Replace range of characters with C string. 1280 * @param __i1 Iterator referencing start of range to replace. 1281 * @param __i2 Iterator referencing end of range to replace. 1282 * @param __s C string value to insert. 1283 * @return Reference to this string. 1284 * @throw std::length_error If new length exceeds @c max_size(). 1285 * 1286 * Removes the characters in the range [i1,i2). In place, the 1287 * characters of @a __s are inserted. If the length of result 1288 * exceeds max_size(), length_error is thrown. The value of 1289 * the string doesn't change if an error is thrown. 1290 */ 1291 __versa_string& 1292 replace(iterator __i1, iterator __i2, const _CharT* __s) 1293 { 1294 __glibcxx_requires_string(__s); 1295 return this->replace(__i1, __i2, __s, traits_type::length(__s)); 1296 } 1297 1298 /** 1299 * @brief Replace range of characters with multiple characters 1300 * @param __i1 Iterator referencing start of range to replace. 1301 * @param __i2 Iterator referencing end of range to replace. 1302 * @param __n Number of characters to insert. 1303 * @param __c Character to insert. 1304 * @return Reference to this string. 1305 * @throw std::length_error If new length exceeds @c max_size(). 1306 * 1307 * Removes the characters in the range [i1,i2). In place, @a 1308 * __n copies of @a __c are inserted. If the length of result 1309 * exceeds max_size(), length_error is thrown. The value of 1310 * the string doesn't change if an error is thrown. 1311 */ 1312 __versa_string& 1313 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c) 1314 { 1315 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 1316 && __i2 <= _M_iend()); 1317 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c); 1318 } 1319 1320 /** 1321 * @brief Replace range of characters with range. 1322 * @param __i1 Iterator referencing start of range to replace. 1323 * @param __i2 Iterator referencing end of range to replace. 1324 * @param __k1 Iterator referencing start of range to insert. 1325 * @param __k2 Iterator referencing end of range to insert. 1326 * @return Reference to this string. 1327 * @throw std::length_error If new length exceeds @c max_size(). 1328 * 1329 * Removes the characters in the range [i1,i2). In place, 1330 * characters in the range [k1,k2) are inserted. If the length 1331 * of result exceeds max_size(), length_error is thrown. The 1332 * value of the string doesn't change if an error is thrown. 1333 */ 1334 template<class _InputIterator> 1335 __versa_string& 1336 replace(iterator __i1, iterator __i2, 1337 _InputIterator __k1, _InputIterator __k2) 1338 { 1339 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 1340 && __i2 <= _M_iend()); 1341 __glibcxx_requires_valid_range(__k1, __k2); 1342 typedef typename std::__is_integer<_InputIterator>::__type _Integral; 1343 return this->_M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral()); 1344 } 1345 1346 // Specializations for the common case of pointer and iterator: 1347 // useful to avoid the overhead of temporary buffering in _M_replace. 1348 __versa_string& 1349 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2) 1350 { 1351 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 1352 && __i2 <= _M_iend()); 1353 __glibcxx_requires_valid_range(__k1, __k2); 1354 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, 1355 __k1, __k2 - __k1); 1356 } 1357 1358 __versa_string& 1359 replace(iterator __i1, iterator __i2, 1360 const _CharT* __k1, const _CharT* __k2) 1361 { 1362 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 1363 && __i2 <= _M_iend()); 1364 __glibcxx_requires_valid_range(__k1, __k2); 1365 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, 1366 __k1, __k2 - __k1); 1367 } 1368 1369 __versa_string& 1370 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2) 1371 { 1372 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 1373 && __i2 <= _M_iend()); 1374 __glibcxx_requires_valid_range(__k1, __k2); 1375 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, 1376 __k1.base(), __k2 - __k1); 1377 } 1378 1379 __versa_string& 1380 replace(iterator __i1, iterator __i2, 1381 const_iterator __k1, const_iterator __k2) 1382 { 1383 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 1384 && __i2 <= _M_iend()); 1385 __glibcxx_requires_valid_range(__k1, __k2); 1386 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, 1387 __k1.base(), __k2 - __k1); 1388 } 1389 1390 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 1391 /** 1392 * @brief Replace range of characters with initializer_list. 1393 * @param __i1 Iterator referencing start of range to replace. 1394 * @param __i2 Iterator referencing end of range to replace. 1395 * @param __l The initializer_list of characters to insert. 1396 * @return Reference to this string. 1397 * @throw std::length_error If new length exceeds @c max_size(). 1398 * 1399 * Removes the characters in the range [i1,i2). In place, 1400 * characters in the range [k1,k2) are inserted. If the length 1401 * of result exceeds max_size(), length_error is thrown. The 1402 * value of the string doesn't change if an error is thrown. 1403 */ 1404 __versa_string& replace(iterator __i1, iterator __i2, 1405 std::initializer_list<_CharT> __l) 1406 { return this->replace(__i1, __i2, __l.begin(), __l.end()); } 1407 #endif // __GXX_EXPERIMENTAL_CXX0X__ 1408 1409 private: 1410 template<class _Integer> 1411 __versa_string& 1412 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n, 1413 _Integer __val, std::__true_type) 1414 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); } 1415 1416 template<class _InputIterator> 1417 __versa_string& 1418 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1, 1419 _InputIterator __k2, std::__false_type); 1420 1421 __versa_string& 1422 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2, 1423 _CharT __c); 1424 1425 __versa_string& 1426 _M_replace(size_type __pos, size_type __len1, const _CharT* __s, 1427 const size_type __len2); 1428 1429 __versa_string& 1430 _M_append(const _CharT* __s, size_type __n); 1431 1432 public: 1433 1434 /** 1435 * @brief Copy substring into C string. 1436 * @param __s C string to copy value into. 1437 * @param __n Number of characters to copy. 1438 * @param __pos Index of first character to copy. 1439 * @return Number of characters actually copied 1440 * @throw std::out_of_range If pos > size(). 1441 * 1442 * Copies up to @a __n characters starting at @a __pos into the 1443 * C string @a s. If @a __pos is greater than size(), 1444 * out_of_range is thrown. 1445 */ 1446 size_type 1447 copy(_CharT* __s, size_type __n, size_type __pos = 0) const; 1448 1449 /** 1450 * @brief Swap contents with another string. 1451 * @param __s String to swap with. 1452 * 1453 * Exchanges the contents of this string with that of @a __s in 1454 * constant time. 1455 */ 1456 void 1457 swap(__versa_string& __s) 1458 { this->_M_swap(__s); } 1459 1460 // String operations: 1461 /** 1462 * @brief Return const pointer to null-terminated contents. 1463 * 1464 * This is a handle to internal data. Do not modify or dire things may 1465 * happen. 1466 */ 1467 const _CharT* 1468 c_str() const 1469 { return this->_M_data(); } 1470 1471 /** 1472 * @brief Return const pointer to contents. 1473 * 1474 * This is a handle to internal data. Do not modify or dire things may 1475 * happen. 1476 */ 1477 const _CharT* 1478 data() const 1479 { return this->_M_data(); } 1480 1481 /** 1482 * @brief Return copy of allocator used to construct this string. 1483 */ 1484 allocator_type 1485 get_allocator() const 1486 { return allocator_type(this->_M_get_allocator()); } 1487 1488 /** 1489 * @brief Find position of a C substring. 1490 * @param __s C string to locate. 1491 * @param __pos Index of character to search from. 1492 * @param __n Number of characters from @a __s to search for. 1493 * @return Index of start of first occurrence. 1494 * 1495 * Starting from @a __pos, searches forward for the first @a 1496 * __n characters in @a __s within this string. If found, 1497 * returns the index where it begins. If not found, returns 1498 * npos. 1499 */ 1500 size_type 1501 find(const _CharT* __s, size_type __pos, size_type __n) const; 1502 1503 /** 1504 * @brief Find position of a string. 1505 * @param __str String to locate. 1506 * @param __pos Index of character to search from (default 0). 1507 * @return Index of start of first occurrence. 1508 * 1509 * Starting from @a __pos, searches forward for value of @a 1510 * __str within this string. If found, returns the index where 1511 * it begins. If not found, returns npos. 1512 */ 1513 size_type 1514 find(const __versa_string& __str, size_type __pos = 0) const 1515 { return this->find(__str.data(), __pos, __str.size()); } 1516 1517 /** 1518 * @brief Find position of a C string. 1519 * @param __s C string to locate. 1520 * @param __pos Index of character to search from (default 0). 1521 * @return Index of start of first occurrence. 1522 * 1523 * Starting from @a __pos, searches forward for the value of @a 1524 * __s within this string. If found, returns the index where 1525 * it begins. If not found, returns npos. 1526 */ 1527 size_type 1528 find(const _CharT* __s, size_type __pos = 0) const 1529 { 1530 __glibcxx_requires_string(__s); 1531 return this->find(__s, __pos, traits_type::length(__s)); 1532 } 1533 1534 /** 1535 * @brief Find position of a character. 1536 * @param __c Character to locate. 1537 * @param __pos Index of character to search from (default 0). 1538 * @return Index of first occurrence. 1539 * 1540 * Starting from @a __pos, searches forward for @a __c within 1541 * this string. If found, returns the index where it was 1542 * found. If not found, returns npos. 1543 */ 1544 size_type 1545 find(_CharT __c, size_type __pos = 0) const; 1546 1547 /** 1548 * @brief Find last position of a string. 1549 * @param __str String to locate. 1550 * @param __pos Index of character to search back from (default end). 1551 * @return Index of start of last occurrence. 1552 * 1553 * Starting from @a __pos, searches backward for value of @a 1554 * __str within this string. If found, returns the index where 1555 * it begins. If not found, returns npos. 1556 */ 1557 size_type 1558 rfind(const __versa_string& __str, size_type __pos = npos) const 1559 { return this->rfind(__str.data(), __pos, __str.size()); } 1560 1561 /** 1562 * @brief Find last position of a C substring. 1563 * @param __s C string to locate. 1564 * @param __pos Index of character to search back from. 1565 * @param __n Number of characters from s to search for. 1566 * @return Index of start of last occurrence. 1567 * 1568 * Starting from @a __pos, searches backward for the first @a 1569 * __n characters in @a __s within this string. If found, 1570 * returns the index where it begins. If not found, returns 1571 * npos. 1572 */ 1573 size_type 1574 rfind(const _CharT* __s, size_type __pos, size_type __n) const; 1575 1576 /** 1577 * @brief Find last position of a C string. 1578 * @param __s C string to locate. 1579 * @param __pos Index of character to start search at (default end). 1580 * @return Index of start of last occurrence. 1581 * 1582 * Starting from @a __pos, searches backward for the value of 1583 * @a __s within this string. If found, returns the index 1584 * where it begins. If not found, returns npos. 1585 */ 1586 size_type 1587 rfind(const _CharT* __s, size_type __pos = npos) const 1588 { 1589 __glibcxx_requires_string(__s); 1590 return this->rfind(__s, __pos, traits_type::length(__s)); 1591 } 1592 1593 /** 1594 * @brief Find last position of a character. 1595 * @param __c Character to locate. 1596 * @param __pos Index of character to search back from (default end). 1597 * @return Index of last occurrence. 1598 * 1599 * Starting from @a __pos, searches backward for @a __c within 1600 * this string. If found, returns the index where it was 1601 * found. If not found, returns npos. 1602 */ 1603 size_type 1604 rfind(_CharT __c, size_type __pos = npos) const; 1605 1606 /** 1607 * @brief Find position of a character of string. 1608 * @param __str String containing characters to locate. 1609 * @param __pos Index of character to search from (default 0). 1610 * @return Index of first occurrence. 1611 * 1612 * Starting from @a __pos, searches forward for one of the characters of 1613 * @a __str within this string. If found, returns the index where it was 1614 * found. If not found, returns npos. 1615 */ 1616 size_type 1617 find_first_of(const __versa_string& __str, size_type __pos = 0) const 1618 { return this->find_first_of(__str.data(), __pos, __str.size()); } 1619 1620 /** 1621 * @brief Find position of a character of C substring. 1622 * @param __s String containing characters to locate. 1623 * @param __pos Index of character to search from. 1624 * @param __n Number of characters from s to search for. 1625 * @return Index of first occurrence. 1626 * 1627 * Starting from @a __pos, searches forward for one of the 1628 * first @a __n characters of @a __s within this string. If 1629 * found, returns the index where it was found. If not found, 1630 * returns npos. 1631 */ 1632 size_type 1633 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const; 1634 1635 /** 1636 * @brief Find position of a character of C string. 1637 * @param __s String containing characters to locate. 1638 * @param __pos Index of character to search from (default 0). 1639 * @return Index of first occurrence. 1640 * 1641 * Starting from @a __pos, searches forward for one of the 1642 * characters of @a __s within this string. If found, returns 1643 * the index where it was found. If not found, returns npos. 1644 */ 1645 size_type 1646 find_first_of(const _CharT* __s, size_type __pos = 0) const 1647 { 1648 __glibcxx_requires_string(__s); 1649 return this->find_first_of(__s, __pos, traits_type::length(__s)); 1650 } 1651 1652 /** 1653 * @brief Find position of a character. 1654 * @param __c Character to locate. 1655 * @param __pos Index of character to search from (default 0). 1656 * @return Index of first occurrence. 1657 * 1658 * Starting from @a __pos, searches forward for the character 1659 * @a __c within this string. If found, returns the index 1660 * where it was found. If not found, returns npos. 1661 * 1662 * Note: equivalent to find(c, pos). 1663 */ 1664 size_type 1665 find_first_of(_CharT __c, size_type __pos = 0) const 1666 { return this->find(__c, __pos); } 1667 1668 /** 1669 * @brief Find last position of a character of string. 1670 * @param __str String containing characters to locate. 1671 * @param __pos Index of character to search back from (default end). 1672 * @return Index of last occurrence. 1673 * 1674 * Starting from @a __pos, searches backward for one of the 1675 * characters of @a __str within this string. If found, 1676 * returns the index where it was found. If not found, returns 1677 * npos. 1678 */ 1679 size_type 1680 find_last_of(const __versa_string& __str, size_type __pos = npos) const 1681 { return this->find_last_of(__str.data(), __pos, __str.size()); } 1682 1683 /** 1684 * @brief Find last position of a character of C substring. 1685 * @param __s C string containing characters to locate. 1686 * @param __pos Index of character to search back from. 1687 * @param __n Number of characters from s to search for. 1688 * @return Index of last occurrence. 1689 * 1690 * Starting from @a __pos, searches backward for one of the 1691 * first @a __n characters of @a __s within this string. If 1692 * found, returns the index where it was found. If not found, 1693 * returns npos. 1694 */ 1695 size_type 1696 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const; 1697 1698 /** 1699 * @brief Find last position of a character of C string. 1700 * @param __s C string containing characters to locate. 1701 * @param __pos Index of character to search back from (default end). 1702 * @return Index of last occurrence. 1703 * 1704 * Starting from @a __pos, searches backward for one of the 1705 * characters of @a __s within this string. If found, returns 1706 * the index where it was found. If not found, returns npos. 1707 */ 1708 size_type 1709 find_last_of(const _CharT* __s, size_type __pos = npos) const 1710 { 1711 __glibcxx_requires_string(__s); 1712 return this->find_last_of(__s, __pos, traits_type::length(__s)); 1713 } 1714 1715 /** 1716 * @brief Find last position of a character. 1717 * @param __c Character to locate. 1718 * @param __pos Index of character to search back from (default end). 1719 * @return Index of last occurrence. 1720 * 1721 * Starting from @a __pos, searches backward for @a __c within 1722 * this string. If found, returns the index where it was 1723 * found. If not found, returns npos. 1724 * 1725 * Note: equivalent to rfind(c, pos). 1726 */ 1727 size_type 1728 find_last_of(_CharT __c, size_type __pos = npos) const 1729 { return this->rfind(__c, __pos); } 1730 1731 /** 1732 * @brief Find position of a character not in string. 1733 * @param __str String containing characters to avoid. 1734 * @param __pos Index of character to search from (default 0). 1735 * @return Index of first occurrence. 1736 * 1737 * Starting from @a __pos, searches forward for a character not 1738 * contained in @a __str within this string. If found, returns 1739 * the index where it was found. If not found, returns npos. 1740 */ 1741 size_type 1742 find_first_not_of(const __versa_string& __str, size_type __pos = 0) const 1743 { return this->find_first_not_of(__str.data(), __pos, __str.size()); } 1744 1745 /** 1746 * @brief Find position of a character not in C substring. 1747 * @param __s C string containing characters to avoid. 1748 * @param __pos Index of character to search from. 1749 * @param __n Number of characters from s to consider. 1750 * @return Index of first occurrence. 1751 * 1752 * Starting from @a __pos, searches forward for a character not 1753 * contained in the first @a __n characters of @a __s within 1754 * this string. If found, returns the index where it was 1755 * found. If not found, returns npos. 1756 */ 1757 size_type 1758 find_first_not_of(const _CharT* __s, size_type __pos, 1759 size_type __n) const; 1760 1761 /** 1762 * @brief Find position of a character not in C string. 1763 * @param __s C string containing characters to avoid. 1764 * @param __pos Index of character to search from (default 0). 1765 * @return Index of first occurrence. 1766 * 1767 * Starting from @a __pos, searches forward for a character not 1768 * contained in @a __s within this string. If found, returns 1769 * the index where it was found. If not found, returns npos. 1770 */ 1771 size_type 1772 find_first_not_of(const _CharT* __s, size_type __pos = 0) const 1773 { 1774 __glibcxx_requires_string(__s); 1775 return this->find_first_not_of(__s, __pos, traits_type::length(__s)); 1776 } 1777 1778 /** 1779 * @brief Find position of a different character. 1780 * @param __c Character to avoid. 1781 * @param __pos Index of character to search from (default 0). 1782 * @return Index of first occurrence. 1783 * 1784 * Starting from @a __pos, searches forward for a character 1785 * other than @a __c within this string. If found, returns the 1786 * index where it was found. If not found, returns npos. 1787 */ 1788 size_type 1789 find_first_not_of(_CharT __c, size_type __pos = 0) const; 1790 1791 /** 1792 * @brief Find last position of a character not in string. 1793 * @param __str String containing characters to avoid. 1794 * @param __pos Index of character to search back from (default end). 1795 * @return Index of last occurrence. 1796 * 1797 * Starting from @a __pos, searches backward for a character 1798 * not contained in @a __str within this string. If found, 1799 * returns the index where it was found. If not found, returns 1800 * npos. 1801 */ 1802 size_type 1803 find_last_not_of(const __versa_string& __str, 1804 size_type __pos = npos) const 1805 { return this->find_last_not_of(__str.data(), __pos, __str.size()); } 1806 1807 /** 1808 * @brief Find last position of a character not in C substring. 1809 * @param __s C string containing characters to avoid. 1810 * @param __pos Index of character to search back from. 1811 * @param __n Number of characters from s to consider. 1812 * @return Index of last occurrence. 1813 * 1814 * Starting from @a __pos, searches backward for a character 1815 * not contained in the first @a __n characters of @a __s 1816 * within this string. If found, returns the index where it 1817 * was found. If not found, returns npos. 1818 */ 1819 size_type 1820 find_last_not_of(const _CharT* __s, size_type __pos, 1821 size_type __n) const; 1822 /** 1823 * @brief Find last position of a character not in C string. 1824 * @param __s C string containing characters to avoid. 1825 * @param __pos Index of character to search back from (default end). 1826 * @return Index of last occurrence. 1827 * 1828 * Starting from @a __pos, searches backward for a character 1829 * not contained in @a __s within this string. If found, 1830 * returns the index where it was found. If not found, returns 1831 * npos. 1832 */ 1833 size_type 1834 find_last_not_of(const _CharT* __s, size_type __pos = npos) const 1835 { 1836 __glibcxx_requires_string(__s); 1837 return this->find_last_not_of(__s, __pos, traits_type::length(__s)); 1838 } 1839 1840 /** 1841 * @brief Find last position of a different character. 1842 * @param __c Character to avoid. 1843 * @param __pos Index of character to search back from (default end). 1844 * @return Index of last occurrence. 1845 * 1846 * Starting from @a __pos, searches backward for a character 1847 * other than @a __c within this string. If found, returns the 1848 * index where it was found. If not found, returns npos. 1849 */ 1850 size_type 1851 find_last_not_of(_CharT __c, size_type __pos = npos) const; 1852 1853 /** 1854 * @brief Get a substring. 1855 * @param __pos Index of first character (default 0). 1856 * @param __n Number of characters in substring (default remainder). 1857 * @return The new string. 1858 * @throw std::out_of_range If pos > size(). 1859 * 1860 * Construct and return a new string using the @a __n 1861 * characters starting at @a __pos. If the string is too 1862 * short, use the remainder of the characters. If @a __pos is 1863 * beyond the end of the string, out_of_range is thrown. 1864 */ 1865 __versa_string 1866 substr(size_type __pos = 0, size_type __n = npos) const 1867 { 1868 return __versa_string(*this, _M_check(__pos, "__versa_string::substr"), 1869 __n); 1870 } 1871 1872 /** 1873 * @brief Compare to a string. 1874 * @param __str String to compare against. 1875 * @return Integer < 0, 0, or > 0. 1876 * 1877 * Returns an integer < 0 if this string is ordered before @a 1878 * __str, 0 if their values are equivalent, or > 0 if this 1879 * string is ordered after @a __str. Determines the effective 1880 * length rlen of the strings to compare as the smallest of 1881 * size() and str.size(). The function then compares the two 1882 * strings by calling traits::compare(data(), str.data(),rlen). 1883 * If the result of the comparison is nonzero returns it, 1884 * otherwise the shorter one is ordered first. 1885 */ 1886 int 1887 compare(const __versa_string& __str) const 1888 { 1889 if (this->_M_compare(__str)) 1890 return 0; 1891 1892 const size_type __size = this->size(); 1893 const size_type __osize = __str.size(); 1894 const size_type __len = std::min(__size, __osize); 1895 1896 int __r = traits_type::compare(this->_M_data(), __str.data(), __len); 1897 if (!__r) 1898 __r = this->_S_compare(__size, __osize); 1899 return __r; 1900 } 1901 1902 /** 1903 * @brief Compare substring to a string. 1904 * @param __pos Index of first character of substring. 1905 * @param __n Number of characters in substring. 1906 * @param __str String to compare against. 1907 * @return Integer < 0, 0, or > 0. 1908 * 1909 * Form the substring of this string from the @a __n characters 1910 * starting at @a __pos. Returns an integer < 0 if the 1911 * substring is ordered before @a __str, 0 if their values are 1912 * equivalent, or > 0 if the substring is ordered after @a 1913 * __str. Determines the effective length rlen of the strings 1914 * to compare as the smallest of the length of the substring 1915 * and @a __str.size(). The function then compares the two 1916 * strings by calling 1917 * traits::compare(substring.data(),str.data(),rlen). If the 1918 * result of the comparison is nonzero returns it, otherwise 1919 * the shorter one is ordered first. 1920 */ 1921 int 1922 compare(size_type __pos, size_type __n, 1923 const __versa_string& __str) const; 1924 1925 /** 1926 * @brief Compare substring to a substring. 1927 * @param __pos1 Index of first character of substring. 1928 * @param __n1 Number of characters in substring. 1929 * @param __str String to compare against. 1930 * @param __pos2 Index of first character of substring of str. 1931 * @param __n2 Number of characters in substring of str. 1932 * @return Integer < 0, 0, or > 0. 1933 * 1934 * Form the substring of this string from the @a __n1 1935 * characters starting at @a __pos1. Form the substring of @a 1936 * __str from the @a __n2 characters starting at @a __pos2. 1937 * Returns an integer < 0 if this substring is ordered before 1938 * the substring of @a __str, 0 if their values are equivalent, 1939 * or > 0 if this substring is ordered after the substring of 1940 * @a __str. Determines the effective length rlen of the 1941 * strings to compare as the smallest of the lengths of the 1942 * substrings. The function then compares the two strings by 1943 * calling 1944 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen). 1945 * If the result of the comparison is nonzero returns it, 1946 * otherwise the shorter one is ordered first. 1947 */ 1948 int 1949 compare(size_type __pos1, size_type __n1, const __versa_string& __str, 1950 size_type __pos2, size_type __n2) const; 1951 1952 /** 1953 * @brief Compare to a C string. 1954 * @param __s C string to compare against. 1955 * @return Integer < 0, 0, or > 0. 1956 * 1957 * Returns an integer < 0 if this string is ordered before @a 1958 * __s, 0 if their values are equivalent, or > 0 if this string 1959 * is ordered after @a __s. Determines the effective length 1960 * rlen of the strings to compare as the smallest of size() and 1961 * the length of a string constructed from @a __s. The 1962 * function then compares the two strings by calling 1963 * traits::compare(data(),s,rlen). If the result of the 1964 * comparison is nonzero returns it, otherwise the shorter one 1965 * is ordered first. 1966 */ 1967 int 1968 compare(const _CharT* __s) const; 1969 1970 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1971 // 5 String::compare specification questionable 1972 /** 1973 * @brief Compare substring to a C string. 1974 * @param __pos Index of first character of substring. 1975 * @param __n1 Number of characters in substring. 1976 * @param __s C string to compare against. 1977 * @return Integer < 0, 0, or > 0. 1978 * 1979 * Form the substring of this string from the @a __n1 1980 * characters starting at @a __pos. Returns an integer < 0 if 1981 * the substring is ordered before @a __s, 0 if their values 1982 * are equivalent, or > 0 if the substring is ordered after @a 1983 * __s. Determines the effective length rlen of the strings to 1984 * compare as the smallest of the length of the substring and 1985 * the length of a string constructed from @a __s. The 1986 * function then compares the two string by calling 1987 * traits::compare(substring.data(),s,rlen). If the result of 1988 * the comparison is nonzero returns it, otherwise the shorter 1989 * one is ordered first. 1990 */ 1991 int 1992 compare(size_type __pos, size_type __n1, const _CharT* __s) const; 1993 1994 /** 1995 * @brief Compare substring against a character array. 1996 * @param __pos1 Index of first character of substring. 1997 * @param __n1 Number of characters in substring. 1998 * @param __s character array to compare against. 1999 * @param __n2 Number of characters of s. 2000 * @return Integer < 0, 0, or > 0. 2001 * 2002 * Form the substring of this string from the @a __n1 2003 * characters starting at @a __pos1. Form a string from the 2004 * first @a __n2 characters of @a __s. Returns an integer < 0 2005 * if this substring is ordered before the string from @a __s, 2006 * 0 if their values are equivalent, or > 0 if this substring 2007 * is ordered after the string from @a __s. Determines the 2008 * effective length rlen of the strings to compare as the 2009 * smallest of the length of the substring and @a __n2. The 2010 * function then compares the two strings by calling 2011 * traits::compare(substring.data(),s,rlen). If the result of 2012 * the comparison is nonzero returns it, otherwise the shorter 2013 * one is ordered first. 2014 * 2015 * NB: s must have at least n2 characters, '\\0' has no special 2016 * meaning. 2017 */ 2018 int 2019 compare(size_type __pos, size_type __n1, const _CharT* __s, 2020 size_type __n2) const; 2021 }; 2022 2023 // operator+ 2024 /** 2025 * @brief Concatenate two strings. 2026 * @param __lhs First string. 2027 * @param __rhs Last string. 2028 * @return New string with value of @a __lhs followed by @a __rhs. 2029 */ 2030 template<typename _CharT, typename _Traits, typename _Alloc, 2031 template <typename, typename, typename> class _Base> 2032 __versa_string<_CharT, _Traits, _Alloc, _Base> 2033 operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs, 2034 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs); 2035 2036 /** 2037 * @brief Concatenate C string and string. 2038 * @param __lhs First string. 2039 * @param __rhs Last string. 2040 * @return New string with value of @a __lhs followed by @a __rhs. 2041 */ 2042 template<typename _CharT, typename _Traits, typename _Alloc, 2043 template <typename, typename, typename> class _Base> 2044 __versa_string<_CharT, _Traits, _Alloc, _Base> 2045 operator+(const _CharT* __lhs, 2046 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs); 2047 2048 /** 2049 * @brief Concatenate character and string. 2050 * @param __lhs First string. 2051 * @param __rhs Last string. 2052 * @return New string with @a __lhs followed by @a __rhs. 2053 */ 2054 template<typename _CharT, typename _Traits, typename _Alloc, 2055 template <typename, typename, typename> class _Base> 2056 __versa_string<_CharT, _Traits, _Alloc, _Base> 2057 operator+(_CharT __lhs, 2058 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs); 2059 2060 /** 2061 * @brief Concatenate string and C string. 2062 * @param __lhs First string. 2063 * @param __rhs Last string. 2064 * @return New string with @a __lhs followed by @a __rhs. 2065 */ 2066 template<typename _CharT, typename _Traits, typename _Alloc, 2067 template <typename, typename, typename> class _Base> 2068 __versa_string<_CharT, _Traits, _Alloc, _Base> 2069 operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs, 2070 const _CharT* __rhs); 2071 2072 /** 2073 * @brief Concatenate string and character. 2074 * @param __lhs First string. 2075 * @param __rhs Last string. 2076 * @return New string with @a __lhs followed by @a __rhs. 2077 */ 2078 template<typename _CharT, typename _Traits, typename _Alloc, 2079 template <typename, typename, typename> class _Base> 2080 __versa_string<_CharT, _Traits, _Alloc, _Base> 2081 operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs, 2082 _CharT __rhs); 2083 2084 // operator == 2085 /** 2086 * @brief Test equivalence of two strings. 2087 * @param __lhs First string. 2088 * @param __rhs Second string. 2089 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise. 2090 */ 2091 template<typename _CharT, typename _Traits, typename _Alloc, 2092 template <typename, typename, typename> class _Base> 2093 inline bool 2094 operator==(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs, 2095 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs) 2096 { return __lhs.compare(__rhs) == 0; } 2097 2098 template<typename _CharT, 2099 template <typename, typename, typename> class _Base> 2100 inline typename __enable_if<std::__is_char<_CharT>::__value, bool>::__type 2101 operator==(const __versa_string<_CharT, std::char_traits<_CharT>, 2102 std::allocator<_CharT>, _Base>& __lhs, 2103 const __versa_string<_CharT, std::char_traits<_CharT>, 2104 std::allocator<_CharT>, _Base>& __rhs) 2105 { return (__lhs.size() == __rhs.size() 2106 && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(), 2107 __lhs.size())); } 2108 2109 /** 2110 * @brief Test equivalence of C string and string. 2111 * @param __lhs C string. 2112 * @param __rhs String. 2113 * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise. 2114 */ 2115 template<typename _CharT, typename _Traits, typename _Alloc, 2116 template <typename, typename, typename> class _Base> 2117 inline bool 2118 operator==(const _CharT* __lhs, 2119 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs) 2120 { return __rhs.compare(__lhs) == 0; } 2121 2122 /** 2123 * @brief Test equivalence of string and C string. 2124 * @param __lhs String. 2125 * @param __rhs C string. 2126 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise. 2127 */ 2128 template<typename _CharT, typename _Traits, typename _Alloc, 2129 template <typename, typename, typename> class _Base> 2130 inline bool 2131 operator==(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs, 2132 const _CharT* __rhs) 2133 { return __lhs.compare(__rhs) == 0; } 2134 2135 // operator != 2136 /** 2137 * @brief Test difference of two strings. 2138 * @param __lhs First string. 2139 * @param __rhs Second string. 2140 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise. 2141 */ 2142 template<typename _CharT, typename _Traits, typename _Alloc, 2143 template <typename, typename, typename> class _Base> 2144 inline bool 2145 operator!=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs, 2146 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs) 2147 { return !(__lhs == __rhs); } 2148 2149 /** 2150 * @brief Test difference of C string and string. 2151 * @param __lhs C string. 2152 * @param __rhs String. 2153 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise. 2154 */ 2155 template<typename _CharT, typename _Traits, typename _Alloc, 2156 template <typename, typename, typename> class _Base> 2157 inline bool 2158 operator!=(const _CharT* __lhs, 2159 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs) 2160 { return !(__lhs == __rhs); } 2161 2162 /** 2163 * @brief Test difference of string and C string. 2164 * @param __lhs String. 2165 * @param __rhs C string. 2166 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise. 2167 */ 2168 template<typename _CharT, typename _Traits, typename _Alloc, 2169 template <typename, typename, typename> class _Base> 2170 inline bool 2171 operator!=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs, 2172 const _CharT* __rhs) 2173 { return !(__lhs == __rhs); } 2174 2175 // operator < 2176 /** 2177 * @brief Test if string precedes string. 2178 * @param __lhs First string. 2179 * @param __rhs Second string. 2180 * @return True if @a __lhs precedes @a __rhs. False otherwise. 2181 */ 2182 template<typename _CharT, typename _Traits, typename _Alloc, 2183 template <typename, typename, typename> class _Base> 2184 inline bool 2185 operator<(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs, 2186 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs) 2187 { return __lhs.compare(__rhs) < 0; } 2188 2189 /** 2190 * @brief Test if string precedes C string. 2191 * @param __lhs String. 2192 * @param __rhs C string. 2193 * @return True if @a __lhs precedes @a __rhs. False otherwise. 2194 */ 2195 template<typename _CharT, typename _Traits, typename _Alloc, 2196 template <typename, typename, typename> class _Base> 2197 inline bool 2198 operator<(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs, 2199 const _CharT* __rhs) 2200 { return __lhs.compare(__rhs) < 0; } 2201 2202 /** 2203 * @brief Test if C string precedes string. 2204 * @param __lhs C string. 2205 * @param __rhs String. 2206 * @return True if @a __lhs precedes @a __rhs. False otherwise. 2207 */ 2208 template<typename _CharT, typename _Traits, typename _Alloc, 2209 template <typename, typename, typename> class _Base> 2210 inline bool 2211 operator<(const _CharT* __lhs, 2212 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs) 2213 { return __rhs.compare(__lhs) > 0; } 2214 2215 // operator > 2216 /** 2217 * @brief Test if string follows string. 2218 * @param __lhs First string. 2219 * @param __rhs Second string. 2220 * @return True if @a __lhs follows @a __rhs. False otherwise. 2221 */ 2222 template<typename _CharT, typename _Traits, typename _Alloc, 2223 template <typename, typename, typename> class _Base> 2224 inline bool 2225 operator>(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs, 2226 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs) 2227 { return __lhs.compare(__rhs) > 0; } 2228 2229 /** 2230 * @brief Test if string follows C string. 2231 * @param __lhs String. 2232 * @param __rhs C string. 2233 * @return True if @a __lhs follows @a __rhs. False otherwise. 2234 */ 2235 template<typename _CharT, typename _Traits, typename _Alloc, 2236 template <typename, typename, typename> class _Base> 2237 inline bool 2238 operator>(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs, 2239 const _CharT* __rhs) 2240 { return __lhs.compare(__rhs) > 0; } 2241 2242 /** 2243 * @brief Test if C string follows string. 2244 * @param __lhs C string. 2245 * @param __rhs String. 2246 * @return True if @a __lhs follows @a __rhs. False otherwise. 2247 */ 2248 template<typename _CharT, typename _Traits, typename _Alloc, 2249 template <typename, typename, typename> class _Base> 2250 inline bool 2251 operator>(const _CharT* __lhs, 2252 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs) 2253 { return __rhs.compare(__lhs) < 0; } 2254 2255 // operator <= 2256 /** 2257 * @brief Test if string doesn't follow string. 2258 * @param __lhs First string. 2259 * @param __rhs Second string. 2260 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise. 2261 */ 2262 template<typename _CharT, typename _Traits, typename _Alloc, 2263 template <typename, typename, typename> class _Base> 2264 inline bool 2265 operator<=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs, 2266 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs) 2267 { return __lhs.compare(__rhs) <= 0; } 2268 2269 /** 2270 * @brief Test if string doesn't follow C string. 2271 * @param __lhs String. 2272 * @param __rhs C string. 2273 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise. 2274 */ 2275 template<typename _CharT, typename _Traits, typename _Alloc, 2276 template <typename, typename, typename> class _Base> 2277 inline bool 2278 operator<=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs, 2279 const _CharT* __rhs) 2280 { return __lhs.compare(__rhs) <= 0; } 2281 2282 /** 2283 * @brief Test if C string doesn't follow string. 2284 * @param __lhs C string. 2285 * @param __rhs String. 2286 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise. 2287 */ 2288 template<typename _CharT, typename _Traits, typename _Alloc, 2289 template <typename, typename, typename> class _Base> 2290 inline bool 2291 operator<=(const _CharT* __lhs, 2292 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs) 2293 { return __rhs.compare(__lhs) >= 0; } 2294 2295 // operator >= 2296 /** 2297 * @brief Test if string doesn't precede string. 2298 * @param __lhs First string. 2299 * @param __rhs Second string. 2300 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise. 2301 */ 2302 template<typename _CharT, typename _Traits, typename _Alloc, 2303 template <typename, typename, typename> class _Base> 2304 inline bool 2305 operator>=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs, 2306 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs) 2307 { return __lhs.compare(__rhs) >= 0; } 2308 2309 /** 2310 * @brief Test if string doesn't precede C string. 2311 * @param __lhs String. 2312 * @param __rhs C string. 2313 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise. 2314 */ 2315 template<typename _CharT, typename _Traits, typename _Alloc, 2316 template <typename, typename, typename> class _Base> 2317 inline bool 2318 operator>=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs, 2319 const _CharT* __rhs) 2320 { return __lhs.compare(__rhs) >= 0; } 2321 2322 /** 2323 * @brief Test if C string doesn't precede string. 2324 * @param __lhs C string. 2325 * @param __rhs String. 2326 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise. 2327 */ 2328 template<typename _CharT, typename _Traits, typename _Alloc, 2329 template <typename, typename, typename> class _Base> 2330 inline bool 2331 operator>=(const _CharT* __lhs, 2332 const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs) 2333 { return __rhs.compare(__lhs) <= 0; } 2334 2335 /** 2336 * @brief Swap contents of two strings. 2337 * @param __lhs First string. 2338 * @param __rhs Second string. 2339 * 2340 * Exchanges the contents of @a __lhs and @a __rhs in constant time. 2341 */ 2342 template<typename _CharT, typename _Traits, typename _Alloc, 2343 template <typename, typename, typename> class _Base> 2344 inline void 2345 swap(__versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs, 2346 __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs) 2347 { __lhs.swap(__rhs); } 2348 2349 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 2350 template<typename _CharT, typename _Traits, typename _Alloc, 2351 template <typename, typename, typename> class _Base> 2352 inline void 2353 swap(__versa_string<_CharT, _Traits, _Alloc, _Base>&& __lhs, 2354 __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs) 2355 { __lhs.swap(__rhs); } 2356 2357 template<typename _CharT, typename _Traits, typename _Alloc, 2358 template <typename, typename, typename> class _Base> 2359 inline void 2360 swap(__versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs, 2361 __versa_string<_CharT, _Traits, _Alloc, _Base>&& __rhs) 2362 { __lhs.swap(__rhs); } 2363 #endif 2364 2365 _GLIBCXX_END_NAMESPACE 2366 2367 _GLIBCXX_BEGIN_NAMESPACE(std) 2368 2369 /** 2370 * @brief Read stream into a string. 2371 * @param __is Input stream. 2372 * @param __str Buffer to store into. 2373 * @return Reference to the input stream. 2374 * 2375 * Stores characters from @a __is into @a __str until whitespace is 2376 * found, the end of the stream is encountered, or str.max_size() 2377 * is reached. If is.width() is non-zero, that is the limit on the 2378 * number of characters stored into @a __str. Any previous 2379 * contents of @a __str are erased. 2380 */ 2381 template<typename _CharT, typename _Traits, typename _Alloc, 2382 template <typename, typename, typename> class _Base> 2383 basic_istream<_CharT, _Traits>& 2384 operator>>(basic_istream<_CharT, _Traits>& __is, 2385 __gnu_cxx::__versa_string<_CharT, _Traits, 2386 _Alloc, _Base>& __str); 2387 2388 /** 2389 * @brief Write string to a stream. 2390 * @param __os Output stream. 2391 * @param __str String to write out. 2392 * @return Reference to the output stream. 2393 * 2394 * Output characters of @a __str into os following the same rules as for 2395 * writing a C string. 2396 */ 2397 template<typename _CharT, typename _Traits, typename _Alloc, 2398 template <typename, typename, typename> class _Base> 2399 inline basic_ostream<_CharT, _Traits>& 2400 operator<<(basic_ostream<_CharT, _Traits>& __os, 2401 const __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, 2402 _Base>& __str) 2403 { 2404 // _GLIBCXX_RESOLVE_LIB_DEFECTS 2405 // 586. string inserter not a formatted function 2406 return __ostream_insert(__os, __str.data(), __str.size()); 2407 } 2408 2409 /** 2410 * @brief Read a line from stream into a string. 2411 * @param __is Input stream. 2412 * @param __str Buffer to store into. 2413 * @param __delim Character marking end of line. 2414 * @return Reference to the input stream. 2415 * 2416 * Stores characters from @a __is into @a __str until @a __delim is 2417 * found, the end of the stream is encountered, or str.max_size() 2418 * is reached. If is.width() is non-zero, that is the limit on the 2419 * number of characters stored into @a __str. Any previous 2420 * contents of @a __str are erased. If @a delim was encountered, 2421 * it is extracted but not stored into @a __str. 2422 */ 2423 template<typename _CharT, typename _Traits, typename _Alloc, 2424 template <typename, typename, typename> class _Base> 2425 basic_istream<_CharT, _Traits>& 2426 getline(basic_istream<_CharT, _Traits>& __is, 2427 __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base>& __str, 2428 _CharT __delim); 2429 2430 /** 2431 * @brief Read a line from stream into a string. 2432 * @param __is Input stream. 2433 * @param __str Buffer to store into. 2434 * @return Reference to the input stream. 2435 * 2436 * Stores characters from is into @a __str until '\n' is found, the 2437 * end of the stream is encountered, or str.max_size() is reached. 2438 * If is.width() is non-zero, that is the limit on the number of 2439 * characters stored into @a __str. Any previous contents of @a 2440 * __str are erased. If end of line was encountered, it is 2441 * extracted but not stored into @a __str. 2442 */ 2443 template<typename _CharT, typename _Traits, typename _Alloc, 2444 template <typename, typename, typename> class _Base> 2445 inline basic_istream<_CharT, _Traits>& 2446 getline(basic_istream<_CharT, _Traits>& __is, 2447 __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base>& __str) 2448 { return getline(__is, __str, __is.widen('\n')); } 2449 2450 _GLIBCXX_END_NAMESPACE 2451 2452 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) && defined(_GLIBCXX_USE_C99)) 2453 2454 #include <ext/string_conversions.h> 2455 2456 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) 2457 2458 // 21.4 Numeric Conversions [string.conversions]. 2459 inline int 2460 stoi(const __vstring& __str, std::size_t* __idx = 0, int __base = 10) 2461 { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(), 2462 __idx, __base); } 2463 2464 inline long 2465 stol(const __vstring& __str, std::size_t* __idx = 0, int __base = 10) 2466 { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(), 2467 __idx, __base); } 2468 2469 inline unsigned long 2470 stoul(const __vstring& __str, std::size_t* __idx = 0, int __base = 10) 2471 { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(), 2472 __idx, __base); } 2473 2474 inline long long 2475 stoll(const __vstring& __str, std::size_t* __idx = 0, int __base = 10) 2476 { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(), 2477 __idx, __base); } 2478 2479 inline unsigned long long 2480 stoull(const __vstring& __str, std::size_t* __idx, int __base = 10) 2481 { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(), 2482 __idx, __base); } 2483 2484 // NB: strtof vs strtod. 2485 inline float 2486 stof(const __vstring& __str, std::size_t* __idx = 0) 2487 { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); } 2488 2489 inline double 2490 stod(const __vstring& __str, std::size_t* __idx = 0) 2491 { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); } 2492 2493 inline long double 2494 stold(const __vstring& __str, std::size_t* __idx = 0) 2495 { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); } 2496 2497 // NB: (v)snprintf vs sprintf. 2498 inline __vstring 2499 to_string(long long __val) 2500 { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf, 2501 4 * sizeof(long long), 2502 "%lld", __val); } 2503 2504 inline __vstring 2505 to_string(unsigned long long __val) 2506 { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf, 2507 4 * sizeof(unsigned long long), 2508 "%llu", __val); } 2509 2510 inline __vstring 2511 to_string(long double __val) 2512 { 2513 const int __n = __numeric_traits<long double>::__max_exponent10 + 20; 2514 return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf, __n, 2515 "%Lf", __val); 2516 } 2517 2518 #ifdef _GLIBCXX_USE_WCHAR_T 2519 inline int 2520 stoi(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10) 2521 { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(), 2522 __idx, __base); } 2523 2524 inline long 2525 stol(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10) 2526 { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(), 2527 __idx, __base); } 2528 2529 inline unsigned long 2530 stoul(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10) 2531 { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(), 2532 __idx, __base); } 2533 2534 inline long long 2535 stoll(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10) 2536 { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(), 2537 __idx, __base); } 2538 2539 inline unsigned long long 2540 stoull(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10) 2541 { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(), 2542 __idx, __base); } 2543 2544 // NB: wcstof vs wcstod. 2545 inline float 2546 stof(const __wvstring& __str, std::size_t* __idx = 0) 2547 { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); } 2548 2549 inline double 2550 stod(const __wvstring& __str, std::size_t* __idx = 0) 2551 { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); } 2552 2553 inline long double 2554 stold(const __wvstring& __str, std::size_t* __idx = 0) 2555 { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); } 2556 2557 inline __wvstring 2558 to_wstring(long long __val) 2559 { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf, 2560 4 * sizeof(long long), 2561 L"%lld", __val); } 2562 2563 inline __wvstring 2564 to_wstring(unsigned long long __val) 2565 { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf, 2566 4 * sizeof(unsigned long long), 2567 L"%llu", __val); } 2568 2569 inline __wvstring 2570 to_wstring(long double __val) 2571 { 2572 const int __n = __numeric_traits<long double>::__max_exponent10 + 20; 2573 return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf, __n, 2574 L"%Lf", __val); 2575 } 2576 #endif 2577 2578 _GLIBCXX_END_NAMESPACE 2579 2580 #endif 2581 2582 #ifndef _GLIBCXX_EXPORT_TEMPLATE 2583 # include "vstring.tcc" 2584 #endif 2585 2586 #if __google_stl_debug_string && !defined(_GLIBCXX_DEBUG) 2587 // Undo our defines, so they don't affect anything else. 2588 # undef _GLIBCXX_DEBUG_ASSERT 2589 # undef _GLIBCXX_DEBUG_PEDASSERT 2590 # define _GLIBCXX_DEBUG_ASSERT(_Condition) 2591 # define _GLIBCXX_DEBUG_PEDASSERT(_Condition) 2592 #endif 2593 2594 #endif /* _VSTRING_H */ 2595