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