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