00001 // Components for manipulating sequences of characters -*- C++ -*- 00002 00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 00004 // Free Software Foundation, Inc. 00005 // 00006 // This file is part of the GNU ISO C++ Library. This library is free 00007 // software; you can redistribute it and/or modify it under the 00008 // terms of the GNU General Public License as published by the 00009 // Free Software Foundation; either version 2, or (at your option) 00010 // any later version. 00011 00012 // This library is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 00017 // You should have received a copy of the GNU General Public License along 00018 // with this library; see the file COPYING. If not, write to the Free 00019 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 00020 // USA. 00021 00022 // As a special exception, you may use this file as part of a free software 00023 // library without restriction. Specifically, if other files instantiate 00024 // templates or use macros or inline functions from this file, or you compile 00025 // this file and link it with other files to produce an executable, this 00026 // file does not by itself cause the resulting executable to be covered by 00027 // the GNU General Public License. This exception does not however 00028 // invalidate any other reasons why the executable file might be covered by 00029 // the GNU General Public License. 00030 00031 // 00032 // ISO C++ 14882: 21 Strings library 00033 // 00034 00035 /** @file basic_string.h 00036 * This is an internal header file, included by other library headers. 00037 * You should not attempt to use it directly. 00038 */ 00039 00040 #ifndef _BASIC_STRING_H 00041 #define _BASIC_STRING_H 1 00042 00043 #pragma GCC system_header 00044 00045 #include <bits/atomicity.h> 00046 #include <debug/debug.h> 00047 00048 namespace std 00049 { 00050 /** 00051 * @class basic_string basic_string.h <string> 00052 * @brief Managing sequences of characters and character-like objects. 00053 * 00054 * @ingroup Containers 00055 * @ingroup Sequences 00056 * 00057 * Meets the requirements of a <a href="tables.html#65">container</a>, a 00058 * <a href="tables.html#66">reversible container</a>, and a 00059 * <a href="tables.html#67">sequence</a>. Of the 00060 * <a href="tables.html#68">optional sequence requirements</a>, only 00061 * @c push_back, @c at, and array access are supported. 00062 * 00063 * @doctodo 00064 * 00065 * 00066 * @if maint 00067 * Documentation? What's that? 00068 * Nathan Myers <ncm@cantrip.org>. 00069 * 00070 * A string looks like this: 00071 * 00072 * @code 00073 * [_Rep] 00074 * _M_length 00075 * [basic_string<char_type>] _M_capacity 00076 * _M_dataplus _M_refcount 00077 * _M_p ----------------> unnamed array of char_type 00078 * @endcode 00079 * 00080 * Where the _M_p points to the first character in the string, and 00081 * you cast it to a pointer-to-_Rep and subtract 1 to get a 00082 * pointer to the header. 00083 * 00084 * This approach has the enormous advantage that a string object 00085 * requires only one allocation. All the ugliness is confined 00086 * within a single pair of inline functions, which each compile to 00087 * a single "add" instruction: _Rep::_M_data(), and 00088 * string::_M_rep(); and the allocation function which gets a 00089 * block of raw bytes and with room enough and constructs a _Rep 00090 * object at the front. 00091 * 00092 * The reason you want _M_data pointing to the character array and 00093 * not the _Rep is so that the debugger can see the string 00094 * contents. (Probably we should add a non-inline member to get 00095 * the _Rep for the debugger to use, so users can check the actual 00096 * string length.) 00097 * 00098 * Note that the _Rep object is a POD so that you can have a 00099 * static "empty string" _Rep object already "constructed" before 00100 * static constructors have run. The reference-count encoding is 00101 * chosen so that a 0 indicates one reference, so you never try to 00102 * destroy the empty-string _Rep object. 00103 * 00104 * All but the last paragraph is considered pretty conventional 00105 * for a C++ string implementation. 00106 * @endif 00107 */ 00108 // 21.3 Template class basic_string 00109 template<typename _CharT, typename _Traits, typename _Alloc> 00110 class basic_string 00111 { 00112 // Types: 00113 public: 00114 typedef _Traits traits_type; 00115 typedef typename _Traits::char_type value_type; 00116 typedef _Alloc allocator_type; 00117 typedef typename _Alloc::size_type size_type; 00118 typedef typename _Alloc::difference_type difference_type; 00119 typedef typename _Alloc::reference reference; 00120 typedef typename _Alloc::const_reference const_reference; 00121 typedef typename _Alloc::pointer pointer; 00122 typedef typename _Alloc::const_pointer const_pointer; 00123 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator; 00124 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string> 00125 const_iterator; 00126 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00127 typedef std::reverse_iterator<iterator> reverse_iterator; 00128 00129 private: 00130 // _Rep: string representation 00131 // Invariants: 00132 // 1. String really contains _M_length + 1 characters: due to 21.3.4 00133 // must be kept null-terminated. 00134 // 2. _M_capacity >= _M_length 00135 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT). 00136 // 3. _M_refcount has three states: 00137 // -1: leaked, one reference, no ref-copies allowed, non-const. 00138 // 0: one reference, non-const. 00139 // n>0: n + 1 references, operations require a lock, const. 00140 // 4. All fields==0 is an empty string, given the extra storage 00141 // beyond-the-end for a null terminator; thus, the shared 00142 // empty string representation needs no constructor. 00143 00144 struct _Rep_base 00145 { 00146 size_type _M_length; 00147 size_type _M_capacity; 00148 _Atomic_word _M_refcount; 00149 }; 00150 00151 struct _Rep : _Rep_base 00152 { 00153 // Types: 00154 typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc; 00155 00156 // (Public) Data members: 00157 00158 // The maximum number of individual char_type elements of an 00159 // individual string is determined by _S_max_size. This is the 00160 // value that will be returned by max_size(). (Whereas npos 00161 // is the maximum number of bytes the allocator can allocate.) 00162 // If one was to divvy up the theoretical largest size string, 00163 // with a terminating character and m _CharT elements, it'd 00164 // look like this: 00165 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT) 00166 // Solving for m: 00167 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1 00168 // In addition, this implementation quarters this amount. 00169 static const size_type _S_max_size; 00170 static const _CharT _S_terminal; 00171 00172 // The following storage is init'd to 0 by the linker, resulting 00173 // (carefully) in an empty string with one reference. 00174 static size_type _S_empty_rep_storage[]; 00175 00176 static _Rep& 00177 _S_empty_rep() 00178 { return *reinterpret_cast<_Rep*>(&_S_empty_rep_storage); } 00179 00180 bool 00181 _M_is_leaked() const 00182 { return this->_M_refcount < 0; } 00183 00184 bool 00185 _M_is_shared() const 00186 { return this->_M_refcount > 0; } 00187 00188 void 00189 _M_set_leaked() 00190 { this->_M_refcount = -1; } 00191 00192 void 00193 _M_set_sharable() 00194 { this->_M_refcount = 0; } 00195 00196 _CharT* 00197 _M_refdata() throw() 00198 { return reinterpret_cast<_CharT*>(this + 1); } 00199 00200 _CharT* 00201 _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2) 00202 { 00203 return (!_M_is_leaked() && __alloc1 == __alloc2) 00204 ? _M_refcopy() : _M_clone(__alloc1); 00205 } 00206 00207 // Create & Destroy 00208 static _Rep* 00209 _S_create(size_type, size_type, const _Alloc&); 00210 00211 void 00212 _M_dispose(const _Alloc& __a) 00213 { 00214 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING 00215 if (__builtin_expect(this != &_S_empty_rep(), false)) 00216 #endif 00217 if (__gnu_cxx::__exchange_and_add(&this->_M_refcount, -1) <= 0) 00218 _M_destroy(__a); 00219 } // XXX MT 00220 00221 void 00222 _M_destroy(const _Alloc&) throw(); 00223 00224 _CharT* 00225 _M_refcopy() throw() 00226 { 00227 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING 00228 if (__builtin_expect(this != &_S_empty_rep(), false)) 00229 #endif 00230 __gnu_cxx::__atomic_add(&this->_M_refcount, 1); 00231 return _M_refdata(); 00232 } // XXX MT 00233 00234 _CharT* 00235 _M_clone(const _Alloc&, size_type __res = 0); 00236 }; 00237 00238 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html 00239 struct _Alloc_hider : _Alloc 00240 { 00241 _Alloc_hider(_CharT* __dat, const _Alloc& __a) 00242 : _Alloc(__a), _M_p(__dat) { } 00243 00244 _CharT* _M_p; // The actual data. 00245 }; 00246 00247 public: 00248 // Data Members (public): 00249 // NB: This is an unsigned type, and thus represents the maximum 00250 // size that the allocator can hold. 00251 /// @var static const size_type npos 00252 /// Value returned by various member functions when they fail. 00253 static const size_type npos = static_cast<size_type>(-1); 00254 00255 private: 00256 // Data Members (private): 00257 mutable _Alloc_hider _M_dataplus; 00258 00259 _CharT* 00260 _M_data() const 00261 { return _M_dataplus._M_p; } 00262 00263 _CharT* 00264 _M_data(_CharT* __p) 00265 { return (_M_dataplus._M_p = __p); } 00266 00267 _Rep* 00268 _M_rep() const 00269 { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); } 00270 00271 // For the internal use we have functions similar to `begin'/`end' 00272 // but they do not call _M_leak. 00273 iterator 00274 _M_ibegin() const { return iterator(_M_data()); } 00275 00276 iterator 00277 _M_iend() const { return iterator(_M_data() + this->size()); } 00278 00279 void 00280 _M_leak() // for use in begin() & non-const op[] 00281 { 00282 if (!_M_rep()->_M_is_leaked()) 00283 _M_leak_hard(); 00284 } 00285 00286 size_type 00287 _M_check(size_type __pos, const char* __s) const 00288 { 00289 if (__pos > this->size()) 00290 __throw_out_of_range(__N(__s)); 00291 return __pos; 00292 } 00293 00294 // NB: _M_limit doesn't check for a bad __pos value. 00295 size_type 00296 _M_limit(size_type __pos, size_type __off) const 00297 { 00298 const bool __testoff = __off < this->size() - __pos; 00299 return __testoff ? __off : this->size() - __pos; 00300 } 00301 00302 // _S_copy_chars is a separate template to permit specialization 00303 // to optimize for the common case of pointers as iterators. 00304 template<class _Iterator> 00305 static void 00306 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2) 00307 { 00308 for (; __k1 != __k2; ++__k1, ++__p) 00309 traits_type::assign(*__p, *__k1); // These types are off. 00310 } 00311 00312 static void 00313 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) 00314 { _S_copy_chars(__p, __k1.base(), __k2.base()); } 00315 00316 static void 00317 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2) 00318 { _S_copy_chars(__p, __k1.base(), __k2.base()); } 00319 00320 static void 00321 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) 00322 { traits_type::copy(__p, __k1, __k2 - __k1); } 00323 00324 static void 00325 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2) 00326 { traits_type::copy(__p, __k1, __k2 - __k1); } 00327 00328 void 00329 _M_mutate(size_type __pos, size_type __len1, size_type __len2); 00330 00331 void 00332 _M_leak_hard(); 00333 00334 static _Rep& 00335 _S_empty_rep() 00336 { return _Rep::_S_empty_rep(); } 00337 00338 public: 00339 // Construct/copy/destroy: 00340 // NB: We overload ctors in some cases instead of using default 00341 // arguments, per 17.4.4.4 para. 2 item 2. 00342 00343 /** 00344 * @brief Default constructor creates an empty string. 00345 */ 00346 inline 00347 basic_string(); 00348 00349 /** 00350 * @brief Construct an empty string using allocator a. 00351 */ 00352 explicit 00353 basic_string(const _Alloc& __a); 00354 00355 // NB: per LWG issue 42, semantics different from IS: 00356 /** 00357 * @brief Construct string with copy of value of @a str. 00358 * @param str Source string. 00359 */ 00360 basic_string(const basic_string& __str); 00361 /** 00362 * @brief Construct string as copy of a substring. 00363 * @param str Source string. 00364 * @param pos Index of first character to copy from. 00365 * @param n Number of characters to copy (default remainder). 00366 */ 00367 basic_string(const basic_string& __str, size_type __pos, 00368 size_type __n = npos); 00369 /** 00370 * @brief Construct string as copy of a substring. 00371 * @param str Source string. 00372 * @param pos Index of first character to copy from. 00373 * @param n Number of characters to copy. 00374 * @param a Allocator to use. 00375 */ 00376 basic_string(const basic_string& __str, size_type __pos, 00377 size_type __n, const _Alloc& __a); 00378 00379 /** 00380 * @brief Construct string initialized by a character array. 00381 * @param s Source character array. 00382 * @param n Number of characters to copy. 00383 * @param a Allocator to use (default is default allocator). 00384 * 00385 * NB: s must have at least n characters, '\0' has no special 00386 * meaning. 00387 */ 00388 basic_string(const _CharT* __s, size_type __n, 00389 const _Alloc& __a = _Alloc()); 00390 /** 00391 * @brief Construct string as copy of a C string. 00392 * @param s Source C string. 00393 * @param a Allocator to use (default is default allocator). 00394 */ 00395 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc()); 00396 /** 00397 * @brief Construct string as multiple characters. 00398 * @param n Number of characters. 00399 * @param c Character to use. 00400 * @param a Allocator to use (default is default allocator). 00401 */ 00402 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc()); 00403 00404 /** 00405 * @brief Construct string as copy of a range. 00406 * @param beg Start of range. 00407 * @param end End of range. 00408 * @param a Allocator to use (default is default allocator). 00409 */ 00410 template<class _InputIterator> 00411 basic_string(_InputIterator __beg, _InputIterator __end, 00412 const _Alloc& __a = _Alloc()); 00413 00414 /** 00415 * @brief Destroy the string instance. 00416 */ 00417 ~basic_string() 00418 { _M_rep()->_M_dispose(this->get_allocator()); } 00419 00420 /** 00421 * @brief Assign the value of @a str to this string. 00422 * @param str Source string. 00423 */ 00424 basic_string& 00425 operator=(const basic_string& __str) 00426 { 00427 this->assign(__str); 00428 return *this; 00429 } 00430 00431 /** 00432 * @brief Copy contents of @a s into this string. 00433 * @param s Source null-terminated string. 00434 */ 00435 basic_string& 00436 operator=(const _CharT* __s) 00437 { 00438 this->assign(__s); 00439 return *this; 00440 } 00441 00442 /** 00443 * @brief Set value to string of length 1. 00444 * @param c Source character. 00445 * 00446 * Assigning to a character makes this string length 1 and 00447 * (*this)[0] == @a c. 00448 */ 00449 basic_string& 00450 operator=(_CharT __c) 00451 { 00452 this->assign(1, __c); 00453 return *this; 00454 } 00455 00456 // Iterators: 00457 /** 00458 * Returns a read/write iterator that points to the first character in 00459 * the %string. Unshares the string. 00460 */ 00461 iterator 00462 begin() 00463 { 00464 _M_leak(); 00465 return iterator(_M_data()); 00466 } 00467 00468 /** 00469 * Returns a read-only (constant) iterator that points to the first 00470 * character in the %string. 00471 */ 00472 const_iterator 00473 begin() const 00474 { return const_iterator(_M_data()); } 00475 00476 /** 00477 * Returns a read/write iterator that points one past the last 00478 * character in the %string. Unshares the string. 00479 */ 00480 iterator 00481 end() 00482 { 00483 _M_leak(); 00484 return iterator(_M_data() + this->size()); 00485 } 00486 00487 /** 00488 * Returns a read-only (constant) iterator that points one past the 00489 * last character in the %string. 00490 */ 00491 const_iterator 00492 end() const 00493 { return const_iterator(_M_data() + this->size()); } 00494 00495 /** 00496 * Returns a read/write reverse iterator that points to the last 00497 * character in the %string. Iteration is done in reverse element 00498 * order. Unshares the string. 00499 */ 00500 reverse_iterator 00501 rbegin() 00502 { return reverse_iterator(this->end()); } 00503 00504 /** 00505 * Returns a read-only (constant) reverse iterator that points 00506 * to the last character in the %string. Iteration is done in 00507 * reverse element order. 00508 */ 00509 const_reverse_iterator 00510 rbegin() const 00511 { return const_reverse_iterator(this->end()); } 00512 00513 /** 00514 * Returns a read/write reverse iterator that points to one before the 00515 * first character in the %string. Iteration is done in reverse 00516 * element order. Unshares the string. 00517 */ 00518 reverse_iterator 00519 rend() 00520 { return reverse_iterator(this->begin()); } 00521 00522 /** 00523 * Returns a read-only (constant) reverse iterator that points 00524 * to one before the first character in the %string. Iteration 00525 * is done in reverse element order. 00526 */ 00527 const_reverse_iterator 00528 rend() const 00529 { return const_reverse_iterator(this->begin()); } 00530 00531 public: 00532 // Capacity: 00533 /// Returns the number of characters in the string, not including any 00534 /// null-termination. 00535 size_type 00536 size() const { return _M_rep()->_M_length; } 00537 00538 /// Returns the number of characters in the string, not including any 00539 /// null-termination. 00540 size_type 00541 length() const { return _M_rep()->_M_length; } 00542 00543 /// Returns the size() of the largest possible %string. 00544 size_type 00545 max_size() const { return _Rep::_S_max_size; } 00546 00547 /** 00548 * @brief Resizes the %string to the specified number of characters. 00549 * @param n Number of characters the %string should contain. 00550 * @param c Character to fill any new elements. 00551 * 00552 * This function will %resize the %string to the specified 00553 * number of characters. If the number is smaller than the 00554 * %string's current size the %string is truncated, otherwise 00555 * the %string is extended and new elements are set to @a c. 00556 */ 00557 void 00558 resize(size_type __n, _CharT __c); 00559 00560 /** 00561 * @brief Resizes the %string to the specified number of characters. 00562 * @param n Number of characters the %string should contain. 00563 * 00564 * This function will resize the %string to the specified length. If 00565 * the new size is smaller than the %string's current size the %string 00566 * is truncated, otherwise the %string is extended and new characters 00567 * are default-constructed. For basic types such as char, this means 00568 * setting them to 0. 00569 */ 00570 void 00571 resize(size_type __n) { this->resize(__n, _CharT()); } 00572 00573 /** 00574 * Returns the total number of characters that the %string can hold 00575 * before needing to allocate more memory. 00576 */ 00577 size_type 00578 capacity() const { return _M_rep()->_M_capacity; } 00579 00580 /** 00581 * @brief Attempt to preallocate enough memory for specified number of 00582 * characters. 00583 * @param n Number of characters required. 00584 * @throw std::length_error If @a n exceeds @c max_size(). 00585 * 00586 * This function attempts to reserve enough memory for the 00587 * %string to hold the specified number of characters. If the 00588 * number requested is more than max_size(), length_error is 00589 * thrown. 00590 * 00591 * The advantage of this function is that if optimal code is a 00592 * necessity and the user can determine the string length that will be 00593 * required, the user can reserve the memory in %advance, and thus 00594 * prevent a possible reallocation of memory and copying of %string 00595 * data. 00596 */ 00597 void 00598 reserve(size_type __res_arg = 0); 00599 00600 /** 00601 * Erases the string, making it empty. 00602 */ 00603 void 00604 clear() { _M_mutate(0, this->size(), 0); } 00605 00606 /** 00607 * Returns true if the %string is empty. Equivalent to *this == "". 00608 */ 00609 bool 00610 empty() const { return this->size() == 0; } 00611 00612 // Element access: 00613 /** 00614 * @brief Subscript access to the data contained in the %string. 00615 * @param n The index of the character to access. 00616 * @return Read-only (constant) reference to the character. 00617 * 00618 * This operator allows for easy, array-style, data access. 00619 * Note that data access with this operator is unchecked and 00620 * out_of_range lookups are not defined. (For checked lookups 00621 * see at().) 00622 */ 00623 const_reference 00624 operator[] (size_type __pos) const 00625 { 00626 _GLIBCXX_DEBUG_ASSERT(__pos <= size()); 00627 return _M_data()[__pos]; 00628 } 00629 00630 /** 00631 * @brief Subscript access to the data contained in the %string. 00632 * @param n The index of the character to access. 00633 * @return Read/write reference to the character. 00634 * 00635 * This operator allows for easy, array-style, data access. 00636 * Note that data access with this operator is unchecked and 00637 * out_of_range lookups are not defined. (For checked lookups 00638 * see at().) Unshares the string. 00639 */ 00640 reference 00641 operator[](size_type __pos) 00642 { 00643 _GLIBCXX_DEBUG_ASSERT(__pos < size()); 00644 _M_leak(); 00645 return _M_data()[__pos]; 00646 } 00647 00648 /** 00649 * @brief Provides access to the data contained in the %string. 00650 * @param n The index of the character to access. 00651 * @return Read-only (const) reference to the character. 00652 * @throw std::out_of_range If @a n is an invalid index. 00653 * 00654 * This function provides for safer data access. The parameter is 00655 * first checked that it is in the range of the string. The function 00656 * throws out_of_range if the check fails. 00657 */ 00658 const_reference 00659 at(size_type __n) const 00660 { 00661 if (__n >= this->size()) 00662 __throw_out_of_range(__N("basic_string::at")); 00663 return _M_data()[__n]; 00664 } 00665 00666 /** 00667 * @brief Provides access to the data contained in the %string. 00668 * @param n The index of the character to access. 00669 * @return Read/write reference to the character. 00670 * @throw std::out_of_range If @a n is an invalid index. 00671 * 00672 * This function provides for safer data access. The parameter is 00673 * first checked that it is in the range of the string. The function 00674 * throws out_of_range if the check fails. Success results in 00675 * unsharing the string. 00676 */ 00677 reference 00678 at(size_type __n) 00679 { 00680 if (__n >= size()) 00681 __throw_out_of_range(__N("basic_string::at")); 00682 _M_leak(); 00683 return _M_data()[__n]; 00684 } 00685 00686 // Modifiers: 00687 /** 00688 * @brief Append a string to this string. 00689 * @param str The string to append. 00690 * @return Reference to this string. 00691 */ 00692 basic_string& 00693 operator+=(const basic_string& __str) { return this->append(__str); } 00694 00695 /** 00696 * @brief Append a C string. 00697 * @param s The C string to append. 00698 * @return Reference to this string. 00699 */ 00700 basic_string& 00701 operator+=(const _CharT* __s) { return this->append(__s); } 00702 00703 /** 00704 * @brief Append a character. 00705 * @param s The character to append. 00706 * @return Reference to this string. 00707 */ 00708 basic_string& 00709 operator+=(_CharT __c) { return this->append(size_type(1), __c); } 00710 00711 /** 00712 * @brief Append a string to this string. 00713 * @param str The string to append. 00714 * @return Reference to this string. 00715 */ 00716 basic_string& 00717 append(const basic_string& __str); 00718 00719 /** 00720 * @brief Append a substring. 00721 * @param str The string to append. 00722 * @param pos Index of the first character of str to append. 00723 * @param n The number of characters to append. 00724 * @return Reference to this string. 00725 * @throw std::out_of_range if @a pos is not a valid index. 00726 * 00727 * This function appends @a n characters from @a str starting at @a pos 00728 * to this string. If @a n is is larger than the number of available 00729 * characters in @a str, the remainder of @a str is appended. 00730 */ 00731 basic_string& 00732 append(const basic_string& __str, size_type __pos, size_type __n); 00733 00734 /** 00735 * @brief Append a C substring. 00736 * @param s The C string to append. 00737 * @param n The number of characters to append. 00738 * @return Reference to this string. 00739 */ 00740 basic_string& 00741 append(const _CharT* __s, size_type __n); 00742 00743 /** 00744 * @brief Append a C string. 00745 * @param s The C string to append. 00746 * @return Reference to this string. 00747 */ 00748 basic_string& 00749 append(const _CharT* __s) 00750 { 00751 __glibcxx_requires_string(__s); 00752 return this->append(__s, traits_type::length(__s)); 00753 } 00754 00755 /** 00756 * @brief Append multiple characters. 00757 * @param n The number of characters to append. 00758 * @param c The character to use. 00759 * @return Reference to this string. 00760 * 00761 * Appends n copies of c to this string. 00762 */ 00763 basic_string& 00764 append(size_type __n, _CharT __c) 00765 { return _M_replace_aux(this->size(), size_type(0), __n, __c); } 00766 00767 /** 00768 * @brief Append a range of characters. 00769 * @param first Iterator referencing the first character to append. 00770 * @param last Iterator marking the end of the range. 00771 * @return Reference to this string. 00772 * 00773 * Appends characters in the range [first,last) to this string. 00774 */ 00775 template<class _InputIterator> 00776 basic_string& 00777 append(_InputIterator __first, _InputIterator __last) 00778 { return this->replace(_M_iend(), _M_iend(), __first, __last); } 00779 00780 /** 00781 * @brief Append a single character. 00782 * @param c Character to append. 00783 */ 00784 void 00785 push_back(_CharT __c) 00786 { _M_replace_aux(this->size(), size_type(0), size_type(1), __c); } 00787 00788 /** 00789 * @brief Set value to contents of another string. 00790 * @param str Source string to use. 00791 * @return Reference to this string. 00792 */ 00793 basic_string& 00794 assign(const basic_string& __str); 00795 00796 /** 00797 * @brief Set value to a substring of a string. 00798 * @param str The string to use. 00799 * @param pos Index of the first character of str. 00800 * @param n Number of characters to use. 00801 * @return Reference to this string. 00802 * @throw std::out_of_range if @a pos is not a valid index. 00803 * 00804 * This function sets this string to the substring of @a str consisting 00805 * of @a n characters at @a pos. If @a n is is larger than the number 00806 * of available characters in @a str, the remainder of @a str is used. 00807 */ 00808 basic_string& 00809 assign(const basic_string& __str, size_type __pos, size_type __n) 00810 { return this->assign(__str._M_data() 00811 + __str._M_check(__pos, "basic_string::assign"), 00812 __str._M_limit(__pos, __n)); } 00813 00814 /** 00815 * @brief Set value to a C substring. 00816 * @param s The C string to use. 00817 * @param n Number of characters to use. 00818 * @return Reference to this string. 00819 * 00820 * This function sets the value of this string to the first @a n 00821 * characters of @a s. If @a n is is larger than the number of 00822 * available characters in @a s, the remainder of @a s is used. 00823 */ 00824 basic_string& 00825 assign(const _CharT* __s, size_type __n); 00826 00827 /** 00828 * @brief Set value to contents of a C string. 00829 * @param s The C string to use. 00830 * @return Reference to this string. 00831 * 00832 * This function sets the value of this string to the value of @a s. 00833 * The data is copied, so there is no dependence on @a s once the 00834 * function returns. 00835 */ 00836 basic_string& 00837 assign(const _CharT* __s) 00838 { 00839 __glibcxx_requires_string(__s); 00840 return this->assign(__s, traits_type::length(__s)); 00841 } 00842 00843 /** 00844 * @brief Set value to multiple characters. 00845 * @param n Length of the resulting string. 00846 * @param c The character to use. 00847 * @return Reference to this string. 00848 * 00849 * This function sets the value of this string to @a n copies of 00850 * character @a c. 00851 */ 00852 basic_string& 00853 assign(size_type __n, _CharT __c) 00854 { return _M_replace_aux(size_type(0), this->size(), __n, __c); } 00855 00856 /** 00857 * @brief Set value to a range of characters. 00858 * @param first Iterator referencing the first character to append. 00859 * @param last Iterator marking the end of the range. 00860 * @return Reference to this string. 00861 * 00862 * Sets value of string to characters in the range [first,last). 00863 */ 00864 template<class _InputIterator> 00865 basic_string& 00866 assign(_InputIterator __first, _InputIterator __last) 00867 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); } 00868 00869 /** 00870 * @brief Insert multiple characters. 00871 * @param p Iterator referencing location in string to insert at. 00872 * @param n Number of characters to insert 00873 * @param c The character to insert. 00874 * @throw std::length_error If new length exceeds @c max_size(). 00875 * 00876 * Inserts @a n copies of character @a c starting at the position 00877 * referenced by iterator @a p. If adding characters causes the length 00878 * to exceed max_size(), length_error is thrown. The value of the 00879 * string doesn't change if an error is thrown. 00880 */ 00881 void 00882 insert(iterator __p, size_type __n, _CharT __c) 00883 { this->replace(__p, __p, __n, __c); } 00884 00885 /** 00886 * @brief Insert a range of characters. 00887 * @param p Iterator referencing location in string to insert at. 00888 * @param beg Start of range. 00889 * @param end End of range. 00890 * @throw std::length_error If new length exceeds @c max_size(). 00891 * 00892 * Inserts characters in range [beg,end). If adding characters causes 00893 * the length to exceed max_size(), length_error is thrown. The value 00894 * of the string doesn't change if an error is thrown. 00895 */ 00896 template<class _InputIterator> 00897 void insert(iterator __p, _InputIterator __beg, _InputIterator __end) 00898 { this->replace(__p, __p, __beg, __end); } 00899 00900 /** 00901 * @brief Insert value of a string. 00902 * @param pos1 Iterator referencing location in string to insert at. 00903 * @param str The string to insert. 00904 * @return Reference to this string. 00905 * @throw std::length_error If new length exceeds @c max_size(). 00906 * 00907 * Inserts value of @a str starting at @a pos1. If adding characters 00908 * causes the length to exceed max_size(), length_error is thrown. The 00909 * value of the string doesn't change if an error is thrown. 00910 */ 00911 basic_string& 00912 insert(size_type __pos1, const basic_string& __str) 00913 { return this->insert(__pos1, __str, size_type(0), __str.size()); } 00914 00915 /** 00916 * @brief Insert a substring. 00917 * @param pos1 Iterator referencing location in string to insert at. 00918 * @param str The string to insert. 00919 * @param pos2 Start of characters in str to insert. 00920 * @param n Number of characters to insert. 00921 * @return Reference to this string. 00922 * @throw std::length_error If new length exceeds @c max_size(). 00923 * @throw std::out_of_range If @a pos1 > size() or 00924 * @a pos2 > @a str.size(). 00925 * 00926 * Starting at @a pos1, insert @a n character of @a str beginning with 00927 * @a pos2. If adding characters causes the length to exceed 00928 * max_size(), length_error is thrown. If @a pos1 is beyond the end of 00929 * this string or @a pos2 is beyond the end of @a str, out_of_range is 00930 * thrown. The value of the string doesn't change if an error is 00931 * thrown. 00932 */ 00933 basic_string& 00934 insert(size_type __pos1, const basic_string& __str, 00935 size_type __pos2, size_type __n) 00936 { return this->insert(__pos1, __str._M_data() 00937 + __str._M_check(__pos2, "basic_string::insert"), 00938 __str._M_limit(__pos2, __n)); } 00939 00940 /** 00941 * @brief Insert a C substring. 00942 * @param pos Iterator referencing location in string to insert at. 00943 * @param s The C string to insert. 00944 * @param n The number of characters to insert. 00945 * @return Reference to this string. 00946 * @throw std::length_error If new length exceeds @c max_size(). 00947 * @throw std::out_of_range If @a pos is beyond the end of this 00948 * string. 00949 * 00950 * Inserts the first @a n characters of @a s starting at @a pos. If 00951 * adding characters causes the length to exceed max_size(), 00952 * length_error is thrown. If @a pos is beyond end(), out_of_range is 00953 * thrown. The value of the string doesn't change if an error is 00954 * thrown. 00955 */ 00956 basic_string& 00957 insert(size_type __pos, const _CharT* __s, size_type __n); 00958 00959 /** 00960 * @brief Insert a C string. 00961 * @param pos Iterator referencing location in string to insert at. 00962 * @param s The C string to insert. 00963 * @return Reference to this string. 00964 * @throw std::length_error If new length exceeds @c max_size(). 00965 * @throw std::out_of_range If @a pos is beyond the end of this 00966 * string. 00967 * 00968 * Inserts the first @a n characters of @a s starting at @a pos. If 00969 * adding characters causes the length to exceed max_size(), 00970 * length_error is thrown. If @a pos is beyond end(), out_of_range is 00971 * thrown. The value of the string doesn't change if an error is 00972 * thrown. 00973 */ 00974 basic_string& 00975 insert(size_type __pos, const _CharT* __s) 00976 { 00977 __glibcxx_requires_string(__s); 00978 return this->insert(__pos, __s, traits_type::length(__s)); 00979 } 00980 00981 /** 00982 * @brief Insert multiple characters. 00983 * @param pos Index in string to insert at. 00984 * @param n Number of characters to insert 00985 * @param c The character to insert. 00986 * @return Reference to this string. 00987 * @throw std::length_error If new length exceeds @c max_size(). 00988 * @throw std::out_of_range If @a pos is beyond the end of this 00989 * string. 00990 * 00991 * Inserts @a n copies of character @a c starting at index @a pos. If 00992 * adding characters causes the length to exceed max_size(), 00993 * length_error is thrown. If @a pos > length(), out_of_range is 00994 * thrown. The value of the string doesn't change if an error is 00995 * thrown. 00996 */ 00997 basic_string& 00998 insert(size_type __pos, size_type __n, _CharT __c) 00999 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"), 01000 size_type(0), __n, __c); } 01001 01002 /** 01003 * @brief Insert one character. 01004 * @param p Iterator referencing position in string to insert at. 01005 * @param c The character to insert. 01006 * @return Iterator referencing newly inserted char. 01007 * @throw std::length_error If new length exceeds @c max_size(). 01008 * 01009 * Inserts character @a c at position referenced by @a p. If adding 01010 * character causes the length to exceed max_size(), length_error is 01011 * thrown. If @a p is beyond end of string, out_of_range is thrown. 01012 * The value of the string doesn't change if an error is thrown. 01013 */ 01014 iterator 01015 insert(iterator __p, _CharT __c) 01016 { 01017 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend()); 01018 const size_type __pos = __p - _M_ibegin(); 01019 _M_replace_aux(__pos, size_type(0), size_type(1), __c); 01020 _M_rep()->_M_set_leaked(); 01021 return this->_M_ibegin() + __pos; 01022 } 01023 01024 /** 01025 * @brief Remove characters. 01026 * @param pos Index of first character to remove (default 0). 01027 * @param n Number of characters to remove (default remainder). 01028 * @return Reference to this string. 01029 * @throw std::out_of_range If @a pos is beyond the end of this 01030 * string. 01031 * 01032 * Removes @a n characters from this string starting at @a pos. The 01033 * length of the string is reduced by @a n. If there are < @a n 01034 * characters to remove, the remainder of the string is truncated. If 01035 * @a p is beyond end of string, out_of_range is thrown. The value of 01036 * the string doesn't change if an error is thrown. 01037 */ 01038 basic_string& 01039 erase(size_type __pos = 0, size_type __n = npos) 01040 { return _M_replace_safe(_M_check(__pos, "basic_string::erase"), 01041 _M_limit(__pos, __n), NULL, size_type(0)); } 01042 01043 /** 01044 * @brief Remove one character. 01045 * @param position Iterator referencing the character to remove. 01046 * @return iterator referencing same location after removal. 01047 * 01048 * Removes the character at @a position from this string. The value 01049 * of the string doesn't change if an error is thrown. 01050 */ 01051 iterator 01052 erase(iterator __position) 01053 { 01054 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin() 01055 && __position < _M_iend()); 01056 const size_type __pos = __position - _M_ibegin(); 01057 _M_replace_safe(__pos, size_type(1), NULL, size_type(0)); 01058 _M_rep()->_M_set_leaked(); 01059 return _M_ibegin() + __pos; 01060 } 01061 01062 /** 01063 * @brief Remove a range of characters. 01064 * @param first Iterator referencing the first character to remove. 01065 * @param last Iterator referencing the end of the range. 01066 * @return Iterator referencing location of first after removal. 01067 * 01068 * Removes the characters in the range [first,last) from this string. 01069 * The value of the string doesn't change if an error is thrown. 01070 */ 01071 iterator 01072 erase(iterator __first, iterator __last) 01073 { 01074 _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last 01075 && __last <= _M_iend()); 01076 const size_type __pos = __first - _M_ibegin(); 01077 _M_replace_safe(__pos, __last - __first, NULL, size_type(0)); 01078 _M_rep()->_M_set_leaked(); 01079 return _M_ibegin() + __pos; 01080 } 01081 01082 /** 01083 * @brief Replace characters with value from another string. 01084 * @param pos Index of first character to replace. 01085 * @param n Number of characters to be replaced. 01086 * @param str String to insert. 01087 * @return Reference to this string. 01088 * @throw std::out_of_range If @a pos is beyond the end of this 01089 * string. 01090 * @throw std::length_error If new length exceeds @c max_size(). 01091 * 01092 * Removes the characters in the range [pos,pos+n) from this string. 01093 * In place, the value of @a str is inserted. If @a pos is beyond end 01094 * of string, out_of_range is thrown. If the length of the result 01095 * exceeds max_size(), length_error is thrown. The value of the string 01096 * doesn't change if an error is thrown. 01097 */ 01098 basic_string& 01099 replace(size_type __pos, size_type __n, const basic_string& __str) 01100 { return this->replace(__pos, __n, __str._M_data(), __str.size()); } 01101 01102 /** 01103 * @brief Replace characters with value from another string. 01104 * @param pos1 Index of first character to replace. 01105 * @param n1 Number of characters to be replaced. 01106 * @param str String to insert. 01107 * @param pos2 Index of first character of str to use. 01108 * @param n2 Number of characters from str to use. 01109 * @return Reference to this string. 01110 * @throw std::out_of_range If @a pos1 > size() or @a pos2 > 01111 * str.size(). 01112 * @throw std::length_error If new length exceeds @c max_size(). 01113 * 01114 * Removes the characters in the range [pos1,pos1 + n) from this 01115 * string. In place, the value of @a str is inserted. If @a pos is 01116 * beyond end of string, out_of_range is thrown. If the length of the 01117 * result exceeds max_size(), length_error is thrown. The value of the 01118 * string doesn't change if an error is thrown. 01119 */ 01120 basic_string& 01121 replace(size_type __pos1, size_type __n1, const basic_string& __str, 01122 size_type __pos2, size_type __n2) 01123 { return this->replace(__pos1, __n1, __str._M_data() 01124 + __str._M_check(__pos2, "basic_string::replace"), 01125 __str._M_limit(__pos2, __n2)); } 01126 01127 /** 01128 * @brief Replace characters with value of a C substring. 01129 * @param pos Index of first character to replace. 01130 * @param n1 Number of characters to be replaced. 01131 * @param str C string to insert. 01132 * @param n2 Number of characters from str to use. 01133 * @return Reference to this string. 01134 * @throw std::out_of_range If @a pos1 > size(). 01135 * @throw std::length_error If new length exceeds @c max_size(). 01136 * 01137 * Removes the characters in the range [pos,pos + n1) from this string. 01138 * In place, the first @a n2 characters of @a str are inserted, or all 01139 * of @a str if @a n2 is too large. If @a pos is beyond end of string, 01140 * out_of_range is thrown. If the length of result exceeds max_size(), 01141 * length_error is thrown. The value of the string doesn't change if 01142 * an error is thrown. 01143 */ 01144 basic_string& 01145 replace(size_type __pos, size_type __n1, const _CharT* __s, 01146 size_type __n2); 01147 01148 /** 01149 * @brief Replace characters with value of a C string. 01150 * @param pos Index of first character to replace. 01151 * @param n1 Number of characters to be replaced. 01152 * @param str C string to insert. 01153 * @return Reference to this string. 01154 * @throw std::out_of_range If @a pos > size(). 01155 * @throw std::length_error If new length exceeds @c max_size(). 01156 * 01157 * Removes the characters in the range [pos,pos + n1) from this string. 01158 * In place, the first @a n characters of @a str are inserted. If @a 01159 * pos is beyond end of string, out_of_range is thrown. If the length 01160 * of result exceeds max_size(), length_error is thrown. The value of 01161 * the string doesn't change if an error is thrown. 01162 */ 01163 basic_string& 01164 replace(size_type __pos, size_type __n1, const _CharT* __s) 01165 { 01166 __glibcxx_requires_string(__s); 01167 return this->replace(__pos, __n1, __s, traits_type::length(__s)); 01168 } 01169 01170 /** 01171 * @brief Replace characters with multiple characters. 01172 * @param pos Index of first character to replace. 01173 * @param n1 Number of characters to be replaced. 01174 * @param n2 Number of characters to insert. 01175 * @param c Character to insert. 01176 * @return Reference to this string. 01177 * @throw std::out_of_range If @a pos > size(). 01178 * @throw std::length_error If new length exceeds @c max_size(). 01179 * 01180 * Removes the characters in the range [pos,pos + n1) from this string. 01181 * In place, @a n2 copies of @a c are inserted. If @a pos is beyond 01182 * end of string, out_of_range is thrown. If the length of result 01183 * exceeds max_size(), length_error is thrown. The value of the string 01184 * doesn't change if an error is thrown. 01185 */ 01186 basic_string& 01187 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c) 01188 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"), 01189 _M_limit(__pos, __n1), __n2, __c); } 01190 01191 /** 01192 * @brief Replace range of characters with string. 01193 * @param i1 Iterator referencing start of range to replace. 01194 * @param i2 Iterator referencing end of range to replace. 01195 * @param str String value to insert. 01196 * @return Reference to this string. 01197 * @throw std::length_error If new length exceeds @c max_size(). 01198 * 01199 * Removes the characters in the range [i1,i2). In place, the value of 01200 * @a str is inserted. If the length of result exceeds max_size(), 01201 * length_error is thrown. The value of the string doesn't change if 01202 * an error is thrown. 01203 */ 01204 basic_string& 01205 replace(iterator __i1, iterator __i2, const basic_string& __str) 01206 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); } 01207 01208 /** 01209 * @brief Replace range of characters with C substring. 01210 * @param i1 Iterator referencing start of range to replace. 01211 * @param i2 Iterator referencing end of range to replace. 01212 * @param s C string value to insert. 01213 * @param n Number of characters from s to insert. 01214 * @return Reference to this string. 01215 * @throw std::length_error If new length exceeds @c max_size(). 01216 * 01217 * Removes the characters in the range [i1,i2). In place, the first @a 01218 * n characters of @a s are inserted. If the length of result exceeds 01219 * max_size(), length_error is thrown. The value of the string doesn't 01220 * change if an error is thrown. 01221 */ 01222 basic_string& 01223 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n) 01224 { 01225 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 01226 && __i2 <= _M_iend()); 01227 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n); 01228 } 01229 01230 /** 01231 * @brief Replace range of characters with C string. 01232 * @param i1 Iterator referencing start of range to replace. 01233 * @param i2 Iterator referencing end of range to replace. 01234 * @param s C string value to insert. 01235 * @return Reference to this string. 01236 * @throw std::length_error If new length exceeds @c max_size(). 01237 * 01238 * Removes the characters in the range [i1,i2). In place, the 01239 * characters of @a s are inserted. If the length of result exceeds 01240 * max_size(), length_error is thrown. The value of the string doesn't 01241 * change if an error is thrown. 01242 */ 01243 basic_string& 01244 replace(iterator __i1, iterator __i2, const _CharT* __s) 01245 { 01246 __glibcxx_requires_string(__s); 01247 return this->replace(__i1, __i2, __s, traits_type::length(__s)); 01248 } 01249 01250 /** 01251 * @brief Replace range of characters with multiple characters 01252 * @param i1 Iterator referencing start of range to replace. 01253 * @param i2 Iterator referencing end of range to replace. 01254 * @param n Number of characters to insert. 01255 * @param c Character to insert. 01256 * @return Reference to this string. 01257 * @throw std::length_error If new length exceeds @c max_size(). 01258 * 01259 * Removes the characters in the range [i1,i2). In place, @a n copies 01260 * of @a c are inserted. If the length of result exceeds max_size(), 01261 * length_error is thrown. The value of the string doesn't change if 01262 * an error is thrown. 01263 */ 01264 basic_string& 01265 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c) 01266 { 01267 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 01268 && __i2 <= _M_iend()); 01269 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c); 01270 } 01271 01272 /** 01273 * @brief Replace range of characters with range. 01274 * @param i1 Iterator referencing start of range to replace. 01275 * @param i2 Iterator referencing end of range to replace. 01276 * @param k1 Iterator referencing start of range to insert. 01277 * @param k2 Iterator referencing end of range to insert. 01278 * @return Reference to this string. 01279 * @throw std::length_error If new length exceeds @c max_size(). 01280 * 01281 * Removes the characters in the range [i1,i2). In place, characters 01282 * in the range [k1,k2) are inserted. If the length of result exceeds 01283 * max_size(), length_error is thrown. The value of the string doesn't 01284 * change if an error is thrown. 01285 */ 01286 template<class _InputIterator> 01287 basic_string& 01288 replace(iterator __i1, iterator __i2, 01289 _InputIterator __k1, _InputIterator __k2) 01290 { 01291 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 01292 && __i2 <= _M_iend()); 01293 __glibcxx_requires_valid_range(__k1, __k2); 01294 typedef typename _Is_integer<_InputIterator>::_Integral _Integral; 01295 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral()); 01296 } 01297 01298 // Specializations for the common case of pointer and iterator: 01299 // useful to avoid the overhead of temporary buffering in _M_replace. 01300 basic_string& 01301 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2) 01302 { 01303 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 01304 && __i2 <= _M_iend()); 01305 __glibcxx_requires_valid_range(__k1, __k2); 01306 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, 01307 __k1, __k2 - __k1); 01308 } 01309 01310 basic_string& 01311 replace(iterator __i1, iterator __i2, 01312 const _CharT* __k1, const _CharT* __k2) 01313 { 01314 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 01315 && __i2 <= _M_iend()); 01316 __glibcxx_requires_valid_range(__k1, __k2); 01317 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, 01318 __k1, __k2 - __k1); 01319 } 01320 01321 basic_string& 01322 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2) 01323 { 01324 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 01325 && __i2 <= _M_iend()); 01326 __glibcxx_requires_valid_range(__k1, __k2); 01327 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, 01328 __k1.base(), __k2 - __k1); 01329 } 01330 01331 basic_string& 01332 replace(iterator __i1, iterator __i2, 01333 const_iterator __k1, const_iterator __k2) 01334 { 01335 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 01336 && __i2 <= _M_iend()); 01337 __glibcxx_requires_valid_range(__k1, __k2); 01338 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, 01339 __k1.base(), __k2 - __k1); 01340 } 01341 01342 private: 01343 template<class _Integer> 01344 basic_string& 01345 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n, 01346 _Integer __val, __true_type) 01347 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); } 01348 01349 template<class _InputIterator> 01350 basic_string& 01351 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1, 01352 _InputIterator __k2, __false_type); 01353 01354 basic_string& 01355 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2, 01356 _CharT __c) 01357 { 01358 if (this->max_size() - (this->size() - __n1) < __n2) 01359 __throw_length_error(__N("basic_string::_M_replace_aux")); 01360 _M_mutate(__pos1, __n1, __n2); 01361 if (__n2 == 1) 01362 _M_data()[__pos1] = __c; 01363 else if (__n2) 01364 traits_type::assign(_M_data() + __pos1, __n2, __c); 01365 return *this; 01366 } 01367 01368 basic_string& 01369 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s, 01370 size_type __n2) 01371 { 01372 _M_mutate(__pos1, __n1, __n2); 01373 if (__n2 == 1) 01374 _M_data()[__pos1] = *__s; 01375 else if (__n2) 01376 traits_type::copy(_M_data() + __pos1, __s, __n2); 01377 return *this; 01378 } 01379 01380 // _S_construct_aux is used to implement the 21.3.1 para 15 which 01381 // requires special behaviour if _InIter is an integral type 01382 template<class _InIterator> 01383 static _CharT* 01384 _S_construct_aux(_InIterator __beg, _InIterator __end, 01385 const _Alloc& __a, __false_type) 01386 { 01387 typedef typename iterator_traits<_InIterator>::iterator_category _Tag; 01388 return _S_construct(__beg, __end, __a, _Tag()); 01389 } 01390 01391 template<class _InIterator> 01392 static _CharT* 01393 _S_construct_aux(_InIterator __beg, _InIterator __end, 01394 const _Alloc& __a, __true_type) 01395 { return _S_construct(static_cast<size_type>(__beg), 01396 static_cast<value_type>(__end), __a); } 01397 01398 template<class _InIterator> 01399 static _CharT* 01400 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a) 01401 { 01402 typedef typename _Is_integer<_InIterator>::_Integral _Integral; 01403 return _S_construct_aux(__beg, __end, __a, _Integral()); 01404 } 01405 01406 // For Input Iterators, used in istreambuf_iterators, etc. 01407 template<class _InIterator> 01408 static _CharT* 01409 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a, 01410 input_iterator_tag); 01411 01412 // For forward_iterators up to random_access_iterators, used for 01413 // string::iterator, _CharT*, etc. 01414 template<class _FwdIterator> 01415 static _CharT* 01416 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a, 01417 forward_iterator_tag); 01418 01419 static _CharT* 01420 _S_construct(size_type __req, _CharT __c, const _Alloc& __a); 01421 01422 public: 01423 01424 /** 01425 * @brief Copy substring into C string. 01426 * @param s C string to copy value into. 01427 * @param n Number of characters to copy. 01428 * @param pos Index of first character to copy. 01429 * @return Number of characters actually copied 01430 * @throw std::out_of_range If pos > size(). 01431 * 01432 * Copies up to @a n characters starting at @a pos into the C string @a 01433 * s. If @a pos is greater than size(), out_of_range is thrown. 01434 */ 01435 size_type 01436 copy(_CharT* __s, size_type __n, size_type __pos = 0) const; 01437 01438 /** 01439 * @brief Swap contents with another string. 01440 * @param s String to swap with. 01441 * 01442 * Exchanges the contents of this string with that of @a s in constant 01443 * time. 01444 */ 01445 void 01446 swap(basic_string& __s); 01447 01448 // String operations: 01449 /** 01450 * @brief Return const pointer to null-terminated contents. 01451 * 01452 * This is a handle to internal data. Do not modify or dire things may 01453 * happen. 01454 */ 01455 const _CharT* 01456 c_str() const { return _M_data(); } 01457 01458 /** 01459 * @brief Return const pointer to contents. 01460 * 01461 * This is a handle to internal data. Do not modify or dire things may 01462 * happen. 01463 */ 01464 const _CharT* 01465 data() const { return _M_data(); } 01466 01467 /** 01468 * @brief Return copy of allocator used to construct this string. 01469 */ 01470 allocator_type 01471 get_allocator() const { return _M_dataplus; } 01472 01473 /** 01474 * @brief Find position of a C substring. 01475 * @param s C string to locate. 01476 * @param pos Index of character to search from. 01477 * @param n Number of characters from @a s to search for. 01478 * @return Index of start of first occurrence. 01479 * 01480 * Starting from @a pos, searches forward for the first @a n characters 01481 * in @a s within this string. If found, returns the index where it 01482 * begins. If not found, returns npos. 01483 */ 01484 size_type 01485 find(const _CharT* __s, size_type __pos, size_type __n) const; 01486 01487 /** 01488 * @brief Find position of a string. 01489 * @param str String to locate. 01490 * @param pos Index of character to search from (default 0). 01491 * @return Index of start of first occurrence. 01492 * 01493 * Starting from @a pos, searches forward for value of @a str within 01494 * this string. If found, returns the index where it begins. If not 01495 * found, returns npos. 01496 */ 01497 size_type 01498 find(const basic_string& __str, size_type __pos = 0) const 01499 { return this->find(__str.data(), __pos, __str.size()); } 01500 01501 /** 01502 * @brief Find position of a C string. 01503 * @param s C string to locate. 01504 * @param pos Index of character to search from (default 0). 01505 * @return Index of start of first occurrence. 01506 * 01507 * Starting from @a pos, searches forward for the value of @a s within 01508 * this string. If found, returns the index where it begins. If not 01509 * found, returns npos. 01510 */ 01511 size_type 01512 find(const _CharT* __s, size_type __pos = 0) const 01513 { 01514 __glibcxx_requires_string(__s); 01515 return this->find(__s, __pos, traits_type::length(__s)); 01516 } 01517 01518 /** 01519 * @brief Find position of a character. 01520 * @param c Character to locate. 01521 * @param pos Index of character to search from (default 0). 01522 * @return Index of first occurrence. 01523 * 01524 * Starting from @a pos, searches forward for @a c within this string. 01525 * If found, returns the index where it was found. If not found, 01526 * returns npos. 01527 */ 01528 size_type 01529 find(_CharT __c, size_type __pos = 0) const; 01530 01531 /** 01532 * @brief Find last position of a string. 01533 * @param str String to locate. 01534 * @param pos Index of character to search back from (default end). 01535 * @return Index of start of last occurrence. 01536 * 01537 * Starting from @a pos, searches backward for value of @a str within 01538 * this string. If found, returns the index where it begins. If not 01539 * found, returns npos. 01540 */ 01541 size_type 01542 rfind(const basic_string& __str, size_type __pos = npos) const 01543 { return this->rfind(__str.data(), __pos, __str.size()); } 01544 01545 /** 01546 * @brief Find last position of a C substring. 01547 * @param s C string to locate. 01548 * @param pos Index of character to search back from. 01549 * @param n Number of characters from s to search for. 01550 * @return Index of start of last occurrence. 01551 * 01552 * Starting from @a pos, searches backward for the first @a n 01553 * characters in @a s within this string. If found, returns the index 01554 * where it begins. If not found, returns npos. 01555 */ 01556 size_type 01557 rfind(const _CharT* __s, size_type __pos, size_type __n) const; 01558 01559 /** 01560 * @brief Find last position of a C string. 01561 * @param s C string to locate. 01562 * @param pos Index of character to start search at (default 0). 01563 * @return Index of start of last occurrence. 01564 * 01565 * Starting from @a pos, searches backward for the value of @a s within 01566 * this string. If found, returns the index where it begins. If not 01567 * found, returns npos. 01568 */ 01569 size_type 01570 rfind(const _CharT* __s, size_type __pos = npos) const 01571 { 01572 __glibcxx_requires_string(__s); 01573 return this->rfind(__s, __pos, traits_type::length(__s)); 01574 } 01575 01576 /** 01577 * @brief Find last position of a character. 01578 * @param c Character to locate. 01579 * @param pos Index of character to search back from (default 0). 01580 * @return Index of last occurrence. 01581 * 01582 * Starting from @a pos, searches backward for @a c within this string. 01583 * If found, returns the index where it was found. If not found, 01584 * returns npos. 01585 */ 01586 size_type 01587 rfind(_CharT __c, size_type __pos = npos) const; 01588 01589 /** 01590 * @brief Find position of a character of string. 01591 * @param str String containing characters to locate. 01592 * @param pos Index of character to search from (default 0). 01593 * @return Index of first occurrence. 01594 * 01595 * Starting from @a pos, searches forward for one of the characters of 01596 * @a str within this string. If found, returns the index where it was 01597 * found. If not found, returns npos. 01598 */ 01599 size_type 01600 find_first_of(const basic_string& __str, size_type __pos = 0) const 01601 { return this->find_first_of(__str.data(), __pos, __str.size()); } 01602 01603 /** 01604 * @brief Find position of a character of C substring. 01605 * @param s String containing characters to locate. 01606 * @param pos Index of character to search from (default 0). 01607 * @param n Number of characters from s to search for. 01608 * @return Index of first occurrence. 01609 * 01610 * Starting from @a pos, searches forward for one of the first @a n 01611 * characters of @a s within this string. If found, returns the index 01612 * where it was found. If not found, returns npos. 01613 */ 01614 size_type 01615 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const; 01616 01617 /** 01618 * @brief Find position of a character of C string. 01619 * @param s String containing characters to locate. 01620 * @param pos Index of character to search from (default 0). 01621 * @return Index of first occurrence. 01622 * 01623 * Starting from @a pos, searches forward for one of the characters of 01624 * @a s within this string. If found, returns the index where it was 01625 * found. If not found, returns npos. 01626 */ 01627 size_type 01628 find_first_of(const _CharT* __s, size_type __pos = 0) const 01629 { 01630 __glibcxx_requires_string(__s); 01631 return this->find_first_of(__s, __pos, traits_type::length(__s)); 01632 } 01633 01634 /** 01635 * @brief Find position of a character. 01636 * @param c Character to locate. 01637 * @param pos Index of character to search from (default 0). 01638 * @return Index of first occurrence. 01639 * 01640 * Starting from @a pos, searches forward for the character @a c within 01641 * this string. If found, returns the index where it was found. If 01642 * not found, returns npos. 01643 * 01644 * Note: equivalent to find(c, pos). 01645 */ 01646 size_type 01647 find_first_of(_CharT __c, size_type __pos = 0) const 01648 { return this->find(__c, __pos); } 01649 01650 /** 01651 * @brief Find last position of a character of string. 01652 * @param str String containing characters to locate. 01653 * @param pos Index of character to search back from (default end). 01654 * @return Index of last occurrence. 01655 * 01656 * Starting from @a pos, searches backward for one of the characters of 01657 * @a str within this string. If found, returns the index where it was 01658 * found. If not found, returns npos. 01659 */ 01660 size_type 01661 find_last_of(const basic_string& __str, size_type __pos = npos) const 01662 { return this->find_last_of(__str.data(), __pos, __str.size()); } 01663 01664 /** 01665 * @brief Find last position of a character of C substring. 01666 * @param s C string containing characters to locate. 01667 * @param pos Index of character to search back from (default end). 01668 * @param n Number of characters from s to search for. 01669 * @return Index of last occurrence. 01670 * 01671 * Starting from @a pos, searches backward for one of the first @a n 01672 * characters of @a s within this string. If found, returns the index 01673 * where it was found. If not found, returns npos. 01674 */ 01675 size_type 01676 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const; 01677 01678 /** 01679 * @brief Find last position of a character of C string. 01680 * @param s C string containing characters to locate. 01681 * @param pos Index of character to search back from (default end). 01682 * @return Index of last occurrence. 01683 * 01684 * Starting from @a pos, searches backward for one of the characters of 01685 * @a s within this string. If found, returns the index where it was 01686 * found. If not found, returns npos. 01687 */ 01688 size_type 01689 find_last_of(const _CharT* __s, size_type __pos = npos) const 01690 { 01691 __glibcxx_requires_string(__s); 01692 return this->find_last_of(__s, __pos, traits_type::length(__s)); 01693 } 01694 01695 /** 01696 * @brief Find last position of a character. 01697 * @param c Character to locate. 01698 * @param pos Index of character to search back from (default 0). 01699 * @return Index of last occurrence. 01700 * 01701 * Starting from @a pos, searches backward for @a c within this string. 01702 * If found, returns the index where it was found. If not found, 01703 * returns npos. 01704 * 01705 * Note: equivalent to rfind(c, pos). 01706 */ 01707 size_type 01708 find_last_of(_CharT __c, size_type __pos = npos) const 01709 { return this->rfind(__c, __pos); } 01710 01711 /** 01712 * @brief Find position of a character not in string. 01713 * @param str String containing characters to avoid. 01714 * @param pos Index of character to search from (default 0). 01715 * @return Index of first occurrence. 01716 * 01717 * Starting from @a pos, searches forward for a character not contained 01718 * in @a str within this string. If found, returns the index where it 01719 * was found. If not found, returns npos. 01720 */ 01721 size_type 01722 find_first_not_of(const basic_string& __str, size_type __pos = 0) const 01723 { return this->find_first_not_of(__str.data(), __pos, __str.size()); } 01724 01725 /** 01726 * @brief Find position of a character not in C substring. 01727 * @param s C string containing characters to avoid. 01728 * @param pos Index of character to search from (default 0). 01729 * @param n Number of characters from s to consider. 01730 * @return Index of first occurrence. 01731 * 01732 * Starting from @a pos, searches forward for a character not contained 01733 * in the first @a n characters of @a s within this string. If found, 01734 * returns the index where it was found. If not found, returns npos. 01735 */ 01736 size_type 01737 find_first_not_of(const _CharT* __s, size_type __pos, 01738 size_type __n) const; 01739 01740 /** 01741 * @brief Find position of a character not in C string. 01742 * @param s C string containing characters to avoid. 01743 * @param pos Index of character to search from (default 0). 01744 * @return Index of first occurrence. 01745 * 01746 * Starting from @a pos, searches forward for a character not contained 01747 * in @a s within this string. If found, returns the index where it 01748 * was found. If not found, returns npos. 01749 */ 01750 size_type 01751 find_first_not_of(const _CharT* __s, size_type __pos = 0) const 01752 { 01753 __glibcxx_requires_string(__s); 01754 return this->find_first_not_of(__s, __pos, traits_type::length(__s)); 01755 } 01756 01757 /** 01758 * @brief Find position of a different character. 01759 * @param c Character to avoid. 01760 * @param pos Index of character to search from (default 0). 01761 * @return Index of first occurrence. 01762 * 01763 * Starting from @a pos, searches forward for a character other than @a c 01764 * within this string. If found, returns the index where it was found. 01765 * If not found, returns npos. 01766 */ 01767 size_type 01768 find_first_not_of(_CharT __c, size_type __pos = 0) const; 01769 01770 /** 01771 * @brief Find last position of a character not in string. 01772 * @param str String containing characters to avoid. 01773 * @param pos Index of character to search from (default 0). 01774 * @return Index of first occurrence. 01775 * 01776 * Starting from @a pos, searches backward for a character not 01777 * contained in @a str within this string. If found, returns the index 01778 * where it was found. If not found, returns npos. 01779 */ 01780 size_type 01781 find_last_not_of(const basic_string& __str, size_type __pos = npos) const 01782 { return this->find_last_not_of(__str.data(), __pos, __str.size()); } 01783 01784 /** 01785 * @brief Find last position of a character not in C substring. 01786 * @param s C string containing characters to avoid. 01787 * @param pos Index of character to search from (default 0). 01788 * @param n Number of characters from s to consider. 01789 * @return Index of first occurrence. 01790 * 01791 * Starting from @a pos, searches backward for a character not 01792 * contained in the first @a n characters of @a s within this string. 01793 * If found, returns the index where it was found. If not found, 01794 * returns npos. 01795 */ 01796 size_type 01797 find_last_not_of(const _CharT* __s, size_type __pos, 01798 size_type __n) const; 01799 /** 01800 * @brief Find position of a character not in C string. 01801 * @param s C string containing characters to avoid. 01802 * @param pos Index of character to search from (default 0). 01803 * @return Index of first occurrence. 01804 * 01805 * Starting from @a pos, searches backward for a character not 01806 * contained in @a s within this string. If found, returns the index 01807 * where it was found. If not found, returns npos. 01808 */ 01809 size_type 01810 find_last_not_of(const _CharT* __s, size_type __pos = npos) const 01811 { 01812 __glibcxx_requires_string(__s); 01813 return this->find_last_not_of(__s, __pos, traits_type::length(__s)); 01814 } 01815 01816 /** 01817 * @brief Find last position of a different character. 01818 * @param c Character to avoid. 01819 * @param pos Index of character to search from (default 0). 01820 * @return Index of first occurrence. 01821 * 01822 * Starting from @a pos, searches backward for a character other than 01823 * @a c within this string. If found, returns the index where it was 01824 * found. If not found, returns npos. 01825 */ 01826 size_type 01827 find_last_not_of(_CharT __c, size_type __pos = npos) const; 01828 01829 /** 01830 * @brief Get a substring. 01831 * @param pos Index of first character (default 0). 01832 * @param n Number of characters in substring (default remainder). 01833 * @return The new string. 01834 * @throw std::out_of_range If pos > size(). 01835 * 01836 * Construct and return a new string using the @a n characters starting 01837 * at @a pos. If the string is too short, use the remainder of the 01838 * characters. If @a pos is beyond the end of the string, out_of_range 01839 * is thrown. 01840 */ 01841 basic_string 01842 substr(size_type __pos = 0, size_type __n = npos) const 01843 { return basic_string(*this, 01844 _M_check(__pos, "basic_string::substr"), __n); } 01845 01846 /** 01847 * @brief Compare to a string. 01848 * @param str String to compare against. 01849 * @return Integer < 0, 0, or > 0. 01850 * 01851 * Returns an integer < 0 if this string is ordered before @a str, 0 if 01852 * their values are equivalent, or > 0 if this string is ordered after 01853 * @a str. If the lengths of @a str and this string are different, the 01854 * shorter one is ordered first. If they are the same, returns the 01855 * result of traits::compare(data(),str.data(),size()); 01856 */ 01857 int 01858 compare(const basic_string& __str) const 01859 { 01860 const size_type __size = this->size(); 01861 const size_type __osize = __str.size(); 01862 const size_type __len = std::min(__size, __osize); 01863 01864 int __r = traits_type::compare(_M_data(), __str.data(), __len); 01865 if (!__r) 01866 __r = __size - __osize; 01867 return __r; 01868 } 01869 01870 /** 01871 * @brief Compare substring to a string. 01872 * @param pos Index of first character of substring. 01873 * @param n Number of characters in substring. 01874 * @param str String to compare against. 01875 * @return Integer < 0, 0, or > 0. 01876 * 01877 * Form the substring of this string from the @a n characters starting 01878 * at @a pos. Returns an integer < 0 if the substring is ordered 01879 * before @a str, 0 if their values are equivalent, or > 0 if the 01880 * substring is ordered after @a str. If the lengths @a of str and the 01881 * substring are different, the shorter one is ordered first. If they 01882 * are the same, returns the result of 01883 * traits::compare(substring.data(),str.data(),size()); 01884 */ 01885 int 01886 compare(size_type __pos, size_type __n, const basic_string& __str) const; 01887 01888 /** 01889 * @brief Compare substring to a substring. 01890 * @param pos1 Index of first character of substring. 01891 * @param n1 Number of characters in substring. 01892 * @param str String to compare against. 01893 * @param pos2 Index of first character of substring of str. 01894 * @param n2 Number of characters in substring of str. 01895 * @return Integer < 0, 0, or > 0. 01896 * 01897 * Form the substring of this string from the @a n1 characters starting 01898 * at @a pos1. Form the substring of @a str from the @a n2 characters 01899 * starting at @a pos2. Returns an integer < 0 if this substring is 01900 * ordered before the substring of @a str, 0 if their values are 01901 * equivalent, or > 0 if this substring is ordered after the substring 01902 * of @a str. If the lengths of the substring of @a str and this 01903 * substring are different, the shorter one is ordered first. If they 01904 * are the same, returns the result of 01905 * traits::compare(substring.data(),str.substr(pos2,n2).data(),size()); 01906 */ 01907 int 01908 compare(size_type __pos1, size_type __n1, const basic_string& __str, 01909 size_type __pos2, size_type __n2) const; 01910 01911 /** 01912 * @brief Compare to a C string. 01913 * @param s C string to compare against. 01914 * @return Integer < 0, 0, or > 0. 01915 * 01916 * Returns an integer < 0 if this string is ordered before @a s, 0 if 01917 * their values are equivalent, or > 0 if this string is ordered after 01918 * @a s. If the lengths of @a s and this string are different, the 01919 * shorter one is ordered first. If they are the same, returns the 01920 * result of traits::compare(data(),s,size()); 01921 */ 01922 int 01923 compare(const _CharT* __s) const; 01924 01925 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01926 // 5 String::compare specification questionable 01927 /** 01928 * @brief Compare substring to a C string. 01929 * @param pos Index of first character of substring. 01930 * @param n1 Number of characters in substring. 01931 * @param s C string to compare against. 01932 * @return Integer < 0, 0, or > 0. 01933 * 01934 * Form the substring of this string from the @a n1 characters starting 01935 * at @a pos. Returns an integer < 0 if the substring is ordered 01936 * before @a s, 0 if their values are equivalent, or > 0 if the 01937 * substring is ordered after @a s. If the lengths of @a s and the 01938 * substring are different, the shorter one is ordered first. If they 01939 * are the same, returns the result of 01940 * traits::compare(substring.data(),s,size()); 01941 */ 01942 int 01943 compare(size_type __pos, size_type __n1, const _CharT* __s) const; 01944 01945 /** 01946 * @brief Compare substring against a character array. 01947 * @param pos1 Index of first character of substring. 01948 * @param n1 Number of characters in substring. 01949 * @param s character array to compare against. 01950 * @param n2 Number of characters of s. 01951 * @return Integer < 0, 0, or > 0. 01952 * 01953 * Form the substring of this string from the @a n1 characters starting 01954 * at @a pos1. Form a string from the first @a n2 characters of @a s. 01955 * Returns an integer < 0 if this substring is ordered before the string 01956 * from @a s, 0 if their values are equivalent, or > 0 if this substring 01957 * is ordered after the string from @a s. If the lengths of this 01958 * substring and @a n2 are different, the shorter one is ordered first. 01959 * If they are the same, returns the result of 01960 * traits::compare(substring.data(),s,size()); 01961 * 01962 * NB: s must have at least n2 characters, '\0' has no special 01963 * meaning. 01964 */ 01965 int 01966 compare(size_type __pos, size_type __n1, const _CharT* __s, 01967 size_type __n2) const; 01968 }; 01969 01970 template<typename _CharT, typename _Traits, typename _Alloc> 01971 inline basic_string<_CharT, _Traits, _Alloc>:: 01972 basic_string() 01973 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING 01974 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { } 01975 #else 01976 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()) { } 01977 #endif 01978 01979 // operator+ 01980 /** 01981 * @brief Concatenate two strings. 01982 * @param lhs First string. 01983 * @param rhs Last string. 01984 * @return New string with value of @a lhs followed by @a rhs. 01985 */ 01986 template<typename _CharT, typename _Traits, typename _Alloc> 01987 basic_string<_CharT, _Traits, _Alloc> 01988 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 01989 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 01990 { 01991 basic_string<_CharT, _Traits, _Alloc> __str(__lhs); 01992 __str.append(__rhs); 01993 return __str; 01994 } 01995 01996 /** 01997 * @brief Concatenate C string and string. 01998 * @param lhs First string. 01999 * @param rhs Last string. 02000 * @return New string with value of @a lhs followed by @a rhs. 02001 */ 02002 template<typename _CharT, typename _Traits, typename _Alloc> 02003 basic_string<_CharT,_Traits,_Alloc> 02004 operator+(const _CharT* __lhs, 02005 const basic_string<_CharT,_Traits,_Alloc>& __rhs); 02006 02007 /** 02008 * @brief Concatenate character and string. 02009 * @param lhs First string. 02010 * @param rhs Last string. 02011 * @return New string with @a lhs followed by @a rhs. 02012 */ 02013 template<typename _CharT, typename _Traits, typename _Alloc> 02014 basic_string<_CharT,_Traits,_Alloc> 02015 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs); 02016 02017 /** 02018 * @brief Concatenate string and C string. 02019 * @param lhs First string. 02020 * @param rhs Last string. 02021 * @return New string with @a lhs followed by @a rhs. 02022 */ 02023 template<typename _CharT, typename _Traits, typename _Alloc> 02024 inline basic_string<_CharT, _Traits, _Alloc> 02025 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02026 const _CharT* __rhs) 02027 { 02028 basic_string<_CharT, _Traits, _Alloc> __str(__lhs); 02029 __str.append(__rhs); 02030 return __str; 02031 } 02032 02033 /** 02034 * @brief Concatenate string and character. 02035 * @param lhs First string. 02036 * @param rhs Last string. 02037 * @return New string with @a lhs followed by @a rhs. 02038 */ 02039 template<typename _CharT, typename _Traits, typename _Alloc> 02040 inline basic_string<_CharT, _Traits, _Alloc> 02041 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs) 02042 { 02043 typedef basic_string<_CharT, _Traits, _Alloc> __string_type; 02044 typedef typename __string_type::size_type __size_type; 02045 __string_type __str(__lhs); 02046 __str.append(__size_type(1), __rhs); 02047 return __str; 02048 } 02049 02050 // operator == 02051 /** 02052 * @brief Test equivalence of two strings. 02053 * @param lhs First string. 02054 * @param rhs Second string. 02055 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise. 02056 */ 02057 template<typename _CharT, typename _Traits, typename _Alloc> 02058 inline bool 02059 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02060 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02061 { return __lhs.compare(__rhs) == 0; } 02062 02063 /** 02064 * @brief Test equivalence of C string and string. 02065 * @param lhs C string. 02066 * @param rhs String. 02067 * @return True if @a rhs.compare(@a lhs) == 0. False otherwise. 02068 */ 02069 template<typename _CharT, typename _Traits, typename _Alloc> 02070 inline bool 02071 operator==(const _CharT* __lhs, 02072 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02073 { return __rhs.compare(__lhs) == 0; } 02074 02075 /** 02076 * @brief Test equivalence of string and C string. 02077 * @param lhs String. 02078 * @param rhs C string. 02079 * @return True if @a lhs.compare(@a rhs) == 0. False otherwise. 02080 */ 02081 template<typename _CharT, typename _Traits, typename _Alloc> 02082 inline bool 02083 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02084 const _CharT* __rhs) 02085 { return __lhs.compare(__rhs) == 0; } 02086 02087 // operator != 02088 /** 02089 * @brief Test difference of two strings. 02090 * @param lhs First string. 02091 * @param rhs Second string. 02092 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise. 02093 */ 02094 template<typename _CharT, typename _Traits, typename _Alloc> 02095 inline bool 02096 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02097 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02098 { return __rhs.compare(__lhs) != 0; } 02099 02100 /** 02101 * @brief Test difference of C string and string. 02102 * @param lhs C string. 02103 * @param rhs String. 02104 * @return True if @a rhs.compare(@a lhs) != 0. False otherwise. 02105 */ 02106 template<typename _CharT, typename _Traits, typename _Alloc> 02107 inline bool 02108 operator!=(const _CharT* __lhs, 02109 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02110 { return __rhs.compare(__lhs) != 0; } 02111 02112 /** 02113 * @brief Test difference of string and C string. 02114 * @param lhs String. 02115 * @param rhs C string. 02116 * @return True if @a lhs.compare(@a rhs) != 0. False otherwise. 02117 */ 02118 template<typename _CharT, typename _Traits, typename _Alloc> 02119 inline bool 02120 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02121 const _CharT* __rhs) 02122 { return __lhs.compare(__rhs) != 0; } 02123 02124 // operator < 02125 /** 02126 * @brief Test if string precedes string. 02127 * @param lhs First string. 02128 * @param rhs Second string. 02129 * @return True if @a lhs precedes @a rhs. False otherwise. 02130 */ 02131 template<typename _CharT, typename _Traits, typename _Alloc> 02132 inline bool 02133 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02134 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02135 { return __lhs.compare(__rhs) < 0; } 02136 02137 /** 02138 * @brief Test if string precedes C string. 02139 * @param lhs String. 02140 * @param rhs C string. 02141 * @return True if @a lhs precedes @a rhs. False otherwise. 02142 */ 02143 template<typename _CharT, typename _Traits, typename _Alloc> 02144 inline bool 02145 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02146 const _CharT* __rhs) 02147 { return __lhs.compare(__rhs) < 0; } 02148 02149 /** 02150 * @brief Test if C string precedes string. 02151 * @param lhs C string. 02152 * @param rhs String. 02153 * @return True if @a lhs precedes @a rhs. False otherwise. 02154 */ 02155 template<typename _CharT, typename _Traits, typename _Alloc> 02156 inline bool 02157 operator<(const _CharT* __lhs, 02158 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02159 { return __rhs.compare(__lhs) > 0; } 02160 02161 // operator > 02162 /** 02163 * @brief Test if string follows string. 02164 * @param lhs First string. 02165 * @param rhs Second string. 02166 * @return True if @a lhs follows @a rhs. False otherwise. 02167 */ 02168 template<typename _CharT, typename _Traits, typename _Alloc> 02169 inline bool 02170 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02171 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02172 { return __lhs.compare(__rhs) > 0; } 02173 02174 /** 02175 * @brief Test if string follows C string. 02176 * @param lhs String. 02177 * @param rhs C string. 02178 * @return True if @a lhs follows @a rhs. False otherwise. 02179 */ 02180 template<typename _CharT, typename _Traits, typename _Alloc> 02181 inline bool 02182 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02183 const _CharT* __rhs) 02184 { return __lhs.compare(__rhs) > 0; } 02185 02186 /** 02187 * @brief Test if C string follows string. 02188 * @param lhs C string. 02189 * @param rhs String. 02190 * @return True if @a lhs follows @a rhs. False otherwise. 02191 */ 02192 template<typename _CharT, typename _Traits, typename _Alloc> 02193 inline bool 02194 operator>(const _CharT* __lhs, 02195 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02196 { return __rhs.compare(__lhs) < 0; } 02197 02198 // operator <= 02199 /** 02200 * @brief Test if string doesn't follow string. 02201 * @param lhs First string. 02202 * @param rhs Second string. 02203 * @return True if @a lhs doesn't follow @a rhs. False otherwise. 02204 */ 02205 template<typename _CharT, typename _Traits, typename _Alloc> 02206 inline bool 02207 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02208 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02209 { return __lhs.compare(__rhs) <= 0; } 02210 02211 /** 02212 * @brief Test if string doesn't follow C string. 02213 * @param lhs String. 02214 * @param rhs C string. 02215 * @return True if @a lhs doesn't follow @a rhs. False otherwise. 02216 */ 02217 template<typename _CharT, typename _Traits, typename _Alloc> 02218 inline bool 02219 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02220 const _CharT* __rhs) 02221 { return __lhs.compare(__rhs) <= 0; } 02222 02223 /** 02224 * @brief Test if C string doesn't follow string. 02225 * @param lhs C string. 02226 * @param rhs String. 02227 * @return True if @a lhs doesn't follow @a rhs. False otherwise. 02228 */ 02229 template<typename _CharT, typename _Traits, typename _Alloc> 02230 inline bool 02231 operator<=(const _CharT* __lhs, 02232 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02233 { return __rhs.compare(__lhs) >= 0; } 02234 02235 // operator >= 02236 /** 02237 * @brief Test if string doesn't precede string. 02238 * @param lhs First string. 02239 * @param rhs Second string. 02240 * @return True if @a lhs doesn't precede @a rhs. False otherwise. 02241 */ 02242 template<typename _CharT, typename _Traits, typename _Alloc> 02243 inline bool 02244 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02245 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02246 { return __lhs.compare(__rhs) >= 0; } 02247 02248 /** 02249 * @brief Test if string doesn't precede C string. 02250 * @param lhs String. 02251 * @param rhs C string. 02252 * @return True if @a lhs doesn't precede @a rhs. False otherwise. 02253 */ 02254 template<typename _CharT, typename _Traits, typename _Alloc> 02255 inline bool 02256 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02257 const _CharT* __rhs) 02258 { return __lhs.compare(__rhs) >= 0; } 02259 02260 /** 02261 * @brief Test if C string doesn't precede string. 02262 * @param lhs C string. 02263 * @param rhs String. 02264 * @return True if @a lhs doesn't precede @a rhs. False otherwise. 02265 */ 02266 template<typename _CharT, typename _Traits, typename _Alloc> 02267 inline bool 02268 operator>=(const _CharT* __lhs, 02269 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02270 { return __rhs.compare(__lhs) <= 0; } 02271 02272 /** 02273 * @brief Swap contents of two strings. 02274 * @param lhs First string. 02275 * @param rhs Second string. 02276 * 02277 * Exchanges the contents of @a lhs and @a rhs in constant time. 02278 */ 02279 template<typename _CharT, typename _Traits, typename _Alloc> 02280 inline void 02281 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs, 02282 basic_string<_CharT, _Traits, _Alloc>& __rhs) 02283 { __lhs.swap(__rhs); } 02284 02285 /** 02286 * @brief Read stream into a string. 02287 * @param is Input stream. 02288 * @param str Buffer to store into. 02289 * @return Reference to the input stream. 02290 * 02291 * Stores characters from @a is into @a str until whitespace is found, the 02292 * end of the stream is encountered, or str.max_size() is reached. If 02293 * is.width() is non-zero, that is the limit on the number of characters 02294 * stored into @a str. Any previous contents of @a str are erased. 02295 */ 02296 template<typename _CharT, typename _Traits, typename _Alloc> 02297 basic_istream<_CharT, _Traits>& 02298 operator>>(basic_istream<_CharT, _Traits>& __is, 02299 basic_string<_CharT, _Traits, _Alloc>& __str); 02300 02301 /** 02302 * @brief Write string to a stream. 02303 * @param os Output stream. 02304 * @param str String to write out. 02305 * @return Reference to the output stream. 02306 * 02307 * Output characters of @a str into os following the same rules as for 02308 * writing a C string. 02309 */ 02310 template<typename _CharT, typename _Traits, typename _Alloc> 02311 basic_ostream<_CharT, _Traits>& 02312 operator<<(basic_ostream<_CharT, _Traits>& __os, 02313 const basic_string<_CharT, _Traits, _Alloc>& __str); 02314 02315 /** 02316 * @brief Read a line from stream into a string. 02317 * @param is Input stream. 02318 * @param str Buffer to store into. 02319 * @param delim Character marking end of line. 02320 * @return Reference to the input stream. 02321 * 02322 * Stores characters from @a is into @a str until @a delim is found, the 02323 * end of the stream is encountered, or str.max_size() is reached. If 02324 * is.width() is non-zero, that is the limit on the number of characters 02325 * stored into @a str. Any previous contents of @a str are erased. If @a 02326 * delim was encountered, it is extracted but not stored into @a str. 02327 */ 02328 template<typename _CharT, typename _Traits, typename _Alloc> 02329 basic_istream<_CharT,_Traits>& 02330 getline(basic_istream<_CharT, _Traits>& __is, 02331 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim); 02332 02333 /** 02334 * @brief Read a line from stream into a string. 02335 * @param is Input stream. 02336 * @param str Buffer to store into. 02337 * @return Reference to the input stream. 02338 * 02339 * Stores characters from is into @a str until '\n' is found, the end of 02340 * the stream is encountered, or str.max_size() is reached. If is.width() 02341 * is non-zero, that is the limit on the number of characters stored into 02342 * @a str. Any previous contents of @a str are erased. If end of line was 02343 * encountered, it is extracted but not stored into @a str. 02344 */ 02345 template<typename _CharT, typename _Traits, typename _Alloc> 02346 inline basic_istream<_CharT,_Traits>& 02347 getline(basic_istream<_CharT, _Traits>& __is, 02348 basic_string<_CharT, _Traits, _Alloc>& __str); 02349 } // namespace std 02350 02351 #endif /* _BASIC_STRING_H */