libstdc++
|
00001 // Components for manipulating sequences of characters -*- C++ -*- 00002 00003 // Copyright (C) 1997-2014 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file bits/basic_string.h 00026 * This is an internal header file, included by other library headers. 00027 * Do not attempt to use it directly. @headername{string} 00028 */ 00029 00030 // 00031 // ISO C++ 14882: 21 Strings library 00032 // 00033 00034 #ifndef _BASIC_STRING_H 00035 #define _BASIC_STRING_H 1 00036 00037 #pragma GCC system_header 00038 00039 #include <ext/atomicity.h> 00040 #include <debug/debug.h> 00041 #if __cplusplus >= 201103L 00042 #include <initializer_list> 00043 #endif 00044 00045 namespace std _GLIBCXX_VISIBILITY(default) 00046 { 00047 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00048 00049 /** 00050 * @class basic_string basic_string.h <string> 00051 * @brief Managing sequences of characters and character-like objects. 00052 * 00053 * @ingroup strings 00054 * @ingroup sequences 00055 * 00056 * @tparam _CharT Type of character 00057 * @tparam _Traits Traits for character type, defaults to 00058 * char_traits<_CharT>. 00059 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>. 00060 * 00061 * Meets the requirements of a <a href="tables.html#65">container</a>, a 00062 * <a href="tables.html#66">reversible container</a>, and a 00063 * <a href="tables.html#67">sequence</a>. Of the 00064 * <a href="tables.html#68">optional sequence requirements</a>, only 00065 * @c push_back, @c at, and @c %array access are supported. 00066 * 00067 * @doctodo 00068 * 00069 * 00070 * Documentation? What's that? 00071 * Nathan Myers <ncm@cantrip.org>. 00072 * 00073 * A string looks like this: 00074 * 00075 * @code 00076 * [_Rep] 00077 * _M_length 00078 * [basic_string<char_type>] _M_capacity 00079 * _M_dataplus _M_refcount 00080 * _M_p ----------------> unnamed array of char_type 00081 * @endcode 00082 * 00083 * Where the _M_p points to the first character in the string, and 00084 * you cast it to a pointer-to-_Rep and subtract 1 to get a 00085 * pointer to the header. 00086 * 00087 * This approach has the enormous advantage that a string object 00088 * requires only one allocation. All the ugliness is confined 00089 * within a single %pair of inline functions, which each compile to 00090 * a single @a add instruction: _Rep::_M_data(), and 00091 * string::_M_rep(); and the allocation function which gets a 00092 * block of raw bytes and with room enough and constructs a _Rep 00093 * object at the front. 00094 * 00095 * The reason you want _M_data pointing to the character %array and 00096 * not the _Rep is so that the debugger can see the string 00097 * contents. (Probably we should add a non-inline member to get 00098 * the _Rep for the debugger to use, so users can check the actual 00099 * string length.) 00100 * 00101 * Note that the _Rep object is a POD so that you can have a 00102 * static <em>empty string</em> _Rep object already @a constructed before 00103 * static constructors have run. The reference-count encoding is 00104 * chosen so that a 0 indicates one reference, so you never try to 00105 * destroy the empty-string _Rep object. 00106 * 00107 * All but the last paragraph is considered pretty conventional 00108 * for a C++ string implementation. 00109 */ 00110 // 21.3 Template class basic_string 00111 template<typename _CharT, typename _Traits, typename _Alloc> 00112 class basic_string 00113 { 00114 typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type; 00115 00116 // Types: 00117 public: 00118 typedef _Traits traits_type; 00119 typedef typename _Traits::char_type value_type; 00120 typedef _Alloc allocator_type; 00121 typedef typename _CharT_alloc_type::size_type size_type; 00122 typedef typename _CharT_alloc_type::difference_type difference_type; 00123 typedef typename _CharT_alloc_type::reference reference; 00124 typedef typename _CharT_alloc_type::const_reference const_reference; 00125 typedef typename _CharT_alloc_type::pointer pointer; 00126 typedef typename _CharT_alloc_type::const_pointer const_pointer; 00127 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator; 00128 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string> 00129 const_iterator; 00130 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00131 typedef std::reverse_iterator<iterator> reverse_iterator; 00132 00133 private: 00134 // _Rep: string representation 00135 // Invariants: 00136 // 1. String really contains _M_length + 1 characters: due to 21.3.4 00137 // must be kept null-terminated. 00138 // 2. _M_capacity >= _M_length 00139 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT). 00140 // 3. _M_refcount has three states: 00141 // -1: leaked, one reference, no ref-copies allowed, non-const. 00142 // 0: one reference, non-const. 00143 // n>0: n + 1 references, operations require a lock, const. 00144 // 4. All fields==0 is an empty string, given the extra storage 00145 // beyond-the-end for a null terminator; thus, the shared 00146 // empty string representation needs no constructor. 00147 00148 struct _Rep_base 00149 { 00150 size_type _M_length; 00151 size_type _M_capacity; 00152 _Atomic_word _M_refcount; 00153 }; 00154 00155 struct _Rep : _Rep_base 00156 { 00157 // Types: 00158 typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc; 00159 00160 // (Public) Data members: 00161 00162 // The maximum number of individual char_type elements of an 00163 // individual string is determined by _S_max_size. This is the 00164 // value that will be returned by max_size(). (Whereas npos 00165 // is the maximum number of bytes the allocator can allocate.) 00166 // If one was to divvy up the theoretical largest size string, 00167 // with a terminating character and m _CharT elements, it'd 00168 // look like this: 00169 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT) 00170 // Solving for m: 00171 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1 00172 // In addition, this implementation quarters this amount. 00173 static const size_type _S_max_size; 00174 static const _CharT _S_terminal; 00175 00176 // The following storage is init'd to 0 by the linker, resulting 00177 // (carefully) in an empty string with one reference. 00178 static size_type _S_empty_rep_storage[]; 00179 00180 static _Rep& 00181 _S_empty_rep() _GLIBCXX_NOEXCEPT 00182 { 00183 // NB: Mild hack to avoid strict-aliasing warnings. Note that 00184 // _S_empty_rep_storage is never modified and the punning should 00185 // be reasonably safe in this case. 00186 void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage); 00187 return *reinterpret_cast<_Rep*>(__p); 00188 } 00189 00190 bool 00191 _M_is_leaked() const _GLIBCXX_NOEXCEPT 00192 { return this->_M_refcount < 0; } 00193 00194 bool 00195 _M_is_shared() const _GLIBCXX_NOEXCEPT 00196 { return this->_M_refcount > 0; } 00197 00198 void 00199 _M_set_leaked() _GLIBCXX_NOEXCEPT 00200 { this->_M_refcount = -1; } 00201 00202 void 00203 _M_set_sharable() _GLIBCXX_NOEXCEPT 00204 { this->_M_refcount = 0; } 00205 00206 void 00207 _M_set_length_and_sharable(size_type __n) _GLIBCXX_NOEXCEPT 00208 { 00209 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 00210 if (__builtin_expect(this != &_S_empty_rep(), false)) 00211 #endif 00212 { 00213 this->_M_set_sharable(); // One reference. 00214 this->_M_length = __n; 00215 traits_type::assign(this->_M_refdata()[__n], _S_terminal); 00216 // grrr. (per 21.3.4) 00217 // You cannot leave those LWG people alone for a second. 00218 } 00219 } 00220 00221 _CharT* 00222 _M_refdata() throw() 00223 { return reinterpret_cast<_CharT*>(this + 1); } 00224 00225 _CharT* 00226 _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2) 00227 { 00228 return (!_M_is_leaked() && __alloc1 == __alloc2) 00229 ? _M_refcopy() : _M_clone(__alloc1); 00230 } 00231 00232 // Create & Destroy 00233 static _Rep* 00234 _S_create(size_type, size_type, const _Alloc&); 00235 00236 void 00237 _M_dispose(const _Alloc& __a) _GLIBCXX_NOEXCEPT 00238 { 00239 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 00240 if (__builtin_expect(this != &_S_empty_rep(), false)) 00241 #endif 00242 { 00243 // Be race-detector-friendly. For more info see bits/c++config. 00244 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount); 00245 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount, 00246 -1) <= 0) 00247 { 00248 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount); 00249 _M_destroy(__a); 00250 } 00251 } 00252 } // XXX MT 00253 00254 void 00255 _M_destroy(const _Alloc&) throw(); 00256 00257 _CharT* 00258 _M_refcopy() throw() 00259 { 00260 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 00261 if (__builtin_expect(this != &_S_empty_rep(), false)) 00262 #endif 00263 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1); 00264 return _M_refdata(); 00265 } // XXX MT 00266 00267 _CharT* 00268 _M_clone(const _Alloc&, size_type __res = 0); 00269 }; 00270 00271 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html 00272 struct _Alloc_hider : _Alloc 00273 { 00274 _Alloc_hider(_CharT* __dat, const _Alloc& __a) _GLIBCXX_NOEXCEPT 00275 : _Alloc(__a), _M_p(__dat) { } 00276 00277 _CharT* _M_p; // The actual data. 00278 }; 00279 00280 public: 00281 // Data Members (public): 00282 // NB: This is an unsigned type, and thus represents the maximum 00283 // size that the allocator can hold. 00284 /// Value returned by various member functions when they fail. 00285 static const size_type npos = static_cast<size_type>(-1); 00286 00287 private: 00288 // Data Members (private): 00289 mutable _Alloc_hider _M_dataplus; 00290 00291 _CharT* 00292 _M_data() const _GLIBCXX_NOEXCEPT 00293 { return _M_dataplus._M_p; } 00294 00295 _CharT* 00296 _M_data(_CharT* __p) _GLIBCXX_NOEXCEPT 00297 { return (_M_dataplus._M_p = __p); } 00298 00299 _Rep* 00300 _M_rep() const _GLIBCXX_NOEXCEPT 00301 { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); } 00302 00303 // For the internal use we have functions similar to `begin'/`end' 00304 // but they do not call _M_leak. 00305 iterator 00306 _M_ibegin() const _GLIBCXX_NOEXCEPT 00307 { return iterator(_M_data()); } 00308 00309 iterator 00310 _M_iend() const _GLIBCXX_NOEXCEPT 00311 { return iterator(_M_data() + this->size()); } 00312 00313 void 00314 _M_leak() // for use in begin() & non-const op[] 00315 { 00316 if (!_M_rep()->_M_is_leaked()) 00317 _M_leak_hard(); 00318 } 00319 00320 size_type 00321 _M_check(size_type __pos, const char* __s) const 00322 { 00323 if (__pos > this->size()) 00324 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > " 00325 "this->size() (which is %zu)"), 00326 __s, __pos, this->size()); 00327 return __pos; 00328 } 00329 00330 void 00331 _M_check_length(size_type __n1, size_type __n2, const char* __s) const 00332 { 00333 if (this->max_size() - (this->size() - __n1) < __n2) 00334 __throw_length_error(__N(__s)); 00335 } 00336 00337 // NB: _M_limit doesn't check for a bad __pos value. 00338 size_type 00339 _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT 00340 { 00341 const bool __testoff = __off < this->size() - __pos; 00342 return __testoff ? __off : this->size() - __pos; 00343 } 00344 00345 // True if _Rep and source do not overlap. 00346 bool 00347 _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT 00348 { 00349 return (less<const _CharT*>()(__s, _M_data()) 00350 || less<const _CharT*>()(_M_data() + this->size(), __s)); 00351 } 00352 00353 // When __n = 1 way faster than the general multichar 00354 // traits_type::copy/move/assign. 00355 static void 00356 _M_copy(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT 00357 { 00358 if (__n == 1) 00359 traits_type::assign(*__d, *__s); 00360 else 00361 traits_type::copy(__d, __s, __n); 00362 } 00363 00364 static void 00365 _M_move(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT 00366 { 00367 if (__n == 1) 00368 traits_type::assign(*__d, *__s); 00369 else 00370 traits_type::move(__d, __s, __n); 00371 } 00372 00373 static void 00374 _M_assign(_CharT* __d, size_type __n, _CharT __c) _GLIBCXX_NOEXCEPT 00375 { 00376 if (__n == 1) 00377 traits_type::assign(*__d, __c); 00378 else 00379 traits_type::assign(__d, __n, __c); 00380 } 00381 00382 // _S_copy_chars is a separate template to permit specialization 00383 // to optimize for the common case of pointers as iterators. 00384 template<class _Iterator> 00385 static void 00386 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2) 00387 _GLIBCXX_NOEXCEPT 00388 { 00389 for (; __k1 != __k2; ++__k1, ++__p) 00390 traits_type::assign(*__p, *__k1); // These types are off. 00391 } 00392 00393 static void 00394 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT 00395 { _S_copy_chars(__p, __k1.base(), __k2.base()); } 00396 00397 static void 00398 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2) 00399 _GLIBCXX_NOEXCEPT 00400 { _S_copy_chars(__p, __k1.base(), __k2.base()); } 00401 00402 static void 00403 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT 00404 { _M_copy(__p, __k1, __k2 - __k1); } 00405 00406 static void 00407 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2) 00408 _GLIBCXX_NOEXCEPT 00409 { _M_copy(__p, __k1, __k2 - __k1); } 00410 00411 static int 00412 _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT 00413 { 00414 const difference_type __d = difference_type(__n1 - __n2); 00415 00416 if (__d > __gnu_cxx::__numeric_traits<int>::__max) 00417 return __gnu_cxx::__numeric_traits<int>::__max; 00418 else if (__d < __gnu_cxx::__numeric_traits<int>::__min) 00419 return __gnu_cxx::__numeric_traits<int>::__min; 00420 else 00421 return int(__d); 00422 } 00423 00424 void 00425 _M_mutate(size_type __pos, size_type __len1, size_type __len2); 00426 00427 void 00428 _M_leak_hard(); 00429 00430 static _Rep& 00431 _S_empty_rep() _GLIBCXX_NOEXCEPT 00432 { return _Rep::_S_empty_rep(); } 00433 00434 public: 00435 // Construct/copy/destroy: 00436 // NB: We overload ctors in some cases instead of using default 00437 // arguments, per 17.4.4.4 para. 2 item 2. 00438 00439 /** 00440 * @brief Default constructor creates an empty string. 00441 */ 00442 basic_string() 00443 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 00444 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { } 00445 #else 00446 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()){ } 00447 #endif 00448 00449 /** 00450 * @brief Construct an empty string using allocator @a a. 00451 */ 00452 explicit 00453 basic_string(const _Alloc& __a); 00454 00455 // NB: per LWG issue 42, semantics different from IS: 00456 /** 00457 * @brief Construct string with copy of value of @a str. 00458 * @param __str Source string. 00459 */ 00460 basic_string(const basic_string& __str); 00461 /** 00462 * @brief Construct string as copy of a substring. 00463 * @param __str Source string. 00464 * @param __pos Index of first character to copy from. 00465 * @param __n Number of characters to copy (default remainder). 00466 */ 00467 basic_string(const basic_string& __str, size_type __pos, 00468 size_type __n = npos); 00469 /** 00470 * @brief Construct string as copy of a substring. 00471 * @param __str Source string. 00472 * @param __pos Index of first character to copy from. 00473 * @param __n Number of characters to copy. 00474 * @param __a Allocator to use. 00475 */ 00476 basic_string(const basic_string& __str, size_type __pos, 00477 size_type __n, const _Alloc& __a); 00478 00479 /** 00480 * @brief Construct string initialized by a character %array. 00481 * @param __s Source character %array. 00482 * @param __n Number of characters to copy. 00483 * @param __a Allocator to use (default is default allocator). 00484 * 00485 * NB: @a __s must have at least @a __n characters, '\\0' 00486 * has no special meaning. 00487 */ 00488 basic_string(const _CharT* __s, size_type __n, 00489 const _Alloc& __a = _Alloc()); 00490 /** 00491 * @brief Construct string as copy of a C string. 00492 * @param __s Source C string. 00493 * @param __a Allocator to use (default is default allocator). 00494 */ 00495 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc()); 00496 /** 00497 * @brief Construct string as multiple characters. 00498 * @param __n Number of characters. 00499 * @param __c Character to use. 00500 * @param __a Allocator to use (default is default allocator). 00501 */ 00502 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc()); 00503 00504 #if __cplusplus >= 201103L 00505 /** 00506 * @brief Move construct string. 00507 * @param __str Source string. 00508 * 00509 * The newly-created string contains the exact contents of @a __str. 00510 * @a __str is a valid, but unspecified string. 00511 **/ 00512 basic_string(basic_string&& __str) 00513 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 00514 noexcept // FIXME C++11: should always be noexcept. 00515 #endif 00516 : _M_dataplus(__str._M_dataplus) 00517 { 00518 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 00519 __str._M_data(_S_empty_rep()._M_refdata()); 00520 #else 00521 __str._M_data(_S_construct(size_type(), _CharT(), get_allocator())); 00522 #endif 00523 } 00524 00525 /** 00526 * @brief Construct string from an initializer %list. 00527 * @param __l std::initializer_list of characters. 00528 * @param __a Allocator to use (default is default allocator). 00529 */ 00530 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc()); 00531 #endif // C++11 00532 00533 /** 00534 * @brief Construct string as copy of a range. 00535 * @param __beg Start of range. 00536 * @param __end End of range. 00537 * @param __a Allocator to use (default is default allocator). 00538 */ 00539 template<class _InputIterator> 00540 basic_string(_InputIterator __beg, _InputIterator __end, 00541 const _Alloc& __a = _Alloc()); 00542 00543 /** 00544 * @brief Destroy the string instance. 00545 */ 00546 ~basic_string() _GLIBCXX_NOEXCEPT 00547 { _M_rep()->_M_dispose(this->get_allocator()); } 00548 00549 /** 00550 * @brief Assign the value of @a str to this string. 00551 * @param __str Source string. 00552 */ 00553 basic_string& 00554 operator=(const basic_string& __str) 00555 { return this->assign(__str); } 00556 00557 /** 00558 * @brief Copy contents of @a s into this string. 00559 * @param __s Source null-terminated string. 00560 */ 00561 basic_string& 00562 operator=(const _CharT* __s) 00563 { return this->assign(__s); } 00564 00565 /** 00566 * @brief Set value to string of length 1. 00567 * @param __c Source character. 00568 * 00569 * Assigning to a character makes this string length 1 and 00570 * (*this)[0] == @a c. 00571 */ 00572 basic_string& 00573 operator=(_CharT __c) 00574 { 00575 this->assign(1, __c); 00576 return *this; 00577 } 00578 00579 #if __cplusplus >= 201103L 00580 /** 00581 * @brief Move assign the value of @a str to this string. 00582 * @param __str Source string. 00583 * 00584 * The contents of @a str are moved into this string (without copying). 00585 * @a str is a valid, but unspecified string. 00586 **/ 00587 // PR 58265, this should be noexcept. 00588 basic_string& 00589 operator=(basic_string&& __str) 00590 { 00591 // NB: DR 1204. 00592 this->swap(__str); 00593 return *this; 00594 } 00595 00596 /** 00597 * @brief Set value to string constructed from initializer %list. 00598 * @param __l std::initializer_list. 00599 */ 00600 basic_string& 00601 operator=(initializer_list<_CharT> __l) 00602 { 00603 this->assign(__l.begin(), __l.size()); 00604 return *this; 00605 } 00606 #endif // C++11 00607 00608 // Iterators: 00609 /** 00610 * Returns a read/write iterator that points to the first character in 00611 * the %string. Unshares the string. 00612 */ 00613 iterator 00614 begin() // FIXME C++11: should be noexcept. 00615 { 00616 _M_leak(); 00617 return iterator(_M_data()); 00618 } 00619 00620 /** 00621 * Returns a read-only (constant) iterator that points to the first 00622 * character in the %string. 00623 */ 00624 const_iterator 00625 begin() const _GLIBCXX_NOEXCEPT 00626 { return const_iterator(_M_data()); } 00627 00628 /** 00629 * Returns a read/write iterator that points one past the last 00630 * character in the %string. Unshares the string. 00631 */ 00632 iterator 00633 end() // FIXME C++11: should be noexcept. 00634 { 00635 _M_leak(); 00636 return iterator(_M_data() + this->size()); 00637 } 00638 00639 /** 00640 * Returns a read-only (constant) iterator that points one past the 00641 * last character in the %string. 00642 */ 00643 const_iterator 00644 end() const _GLIBCXX_NOEXCEPT 00645 { return const_iterator(_M_data() + this->size()); } 00646 00647 /** 00648 * Returns a read/write reverse iterator that points to the last 00649 * character in the %string. Iteration is done in reverse element 00650 * order. Unshares the string. 00651 */ 00652 reverse_iterator 00653 rbegin() // FIXME C++11: should be noexcept. 00654 { return reverse_iterator(this->end()); } 00655 00656 /** 00657 * Returns a read-only (constant) reverse iterator that points 00658 * to the last character in the %string. Iteration is done in 00659 * reverse element order. 00660 */ 00661 const_reverse_iterator 00662 rbegin() const _GLIBCXX_NOEXCEPT 00663 { return const_reverse_iterator(this->end()); } 00664 00665 /** 00666 * Returns a read/write reverse iterator that points to one before the 00667 * first character in the %string. Iteration is done in reverse 00668 * element order. Unshares the string. 00669 */ 00670 reverse_iterator 00671 rend() // FIXME C++11: should be noexcept. 00672 { return reverse_iterator(this->begin()); } 00673 00674 /** 00675 * Returns a read-only (constant) reverse iterator that points 00676 * to one before the first character in the %string. Iteration 00677 * is done in reverse element order. 00678 */ 00679 const_reverse_iterator 00680 rend() const _GLIBCXX_NOEXCEPT 00681 { return const_reverse_iterator(this->begin()); } 00682 00683 #if __cplusplus >= 201103L 00684 /** 00685 * Returns a read-only (constant) iterator that points to the first 00686 * character in the %string. 00687 */ 00688 const_iterator 00689 cbegin() const noexcept 00690 { return const_iterator(this->_M_data()); } 00691 00692 /** 00693 * Returns a read-only (constant) iterator that points one past the 00694 * last character in the %string. 00695 */ 00696 const_iterator 00697 cend() const noexcept 00698 { return const_iterator(this->_M_data() + this->size()); } 00699 00700 /** 00701 * Returns a read-only (constant) reverse iterator that points 00702 * to the last character in the %string. Iteration is done in 00703 * reverse element order. 00704 */ 00705 const_reverse_iterator 00706 crbegin() const noexcept 00707 { return const_reverse_iterator(this->end()); } 00708 00709 /** 00710 * Returns a read-only (constant) reverse iterator that points 00711 * to one before the first character in the %string. Iteration 00712 * is done in reverse element order. 00713 */ 00714 const_reverse_iterator 00715 crend() const noexcept 00716 { return const_reverse_iterator(this->begin()); } 00717 #endif 00718 00719 public: 00720 // Capacity: 00721 /// Returns the number of characters in the string, not including any 00722 /// null-termination. 00723 size_type 00724 size() const _GLIBCXX_NOEXCEPT 00725 { return _M_rep()->_M_length; } 00726 00727 /// Returns the number of characters in the string, not including any 00728 /// null-termination. 00729 size_type 00730 length() const _GLIBCXX_NOEXCEPT 00731 { return _M_rep()->_M_length; } 00732 00733 /// Returns the size() of the largest possible %string. 00734 size_type 00735 max_size() const _GLIBCXX_NOEXCEPT 00736 { return _Rep::_S_max_size; } 00737 00738 /** 00739 * @brief Resizes the %string to the specified number of characters. 00740 * @param __n Number of characters the %string should contain. 00741 * @param __c Character to fill any new elements. 00742 * 00743 * This function will %resize the %string to the specified 00744 * number of characters. If the number is smaller than the 00745 * %string's current size the %string is truncated, otherwise 00746 * the %string is extended and new elements are %set to @a __c. 00747 */ 00748 void 00749 resize(size_type __n, _CharT __c); 00750 00751 /** 00752 * @brief Resizes the %string to the specified number of characters. 00753 * @param __n Number of characters the %string should contain. 00754 * 00755 * This function will resize the %string to the specified length. If 00756 * the new size is smaller than the %string's current size the %string 00757 * is truncated, otherwise the %string is extended and new characters 00758 * are default-constructed. For basic types such as char, this means 00759 * setting them to 0. 00760 */ 00761 void 00762 resize(size_type __n) 00763 { this->resize(__n, _CharT()); } 00764 00765 #if __cplusplus >= 201103L 00766 /// A non-binding request to reduce capacity() to size(). 00767 void 00768 shrink_to_fit() _GLIBCXX_NOEXCEPT 00769 { 00770 if (capacity() > size()) 00771 { 00772 __try 00773 { reserve(0); } 00774 __catch(...) 00775 { } 00776 } 00777 } 00778 #endif 00779 00780 /** 00781 * Returns the total number of characters that the %string can hold 00782 * before needing to allocate more memory. 00783 */ 00784 size_type 00785 capacity() const _GLIBCXX_NOEXCEPT 00786 { return _M_rep()->_M_capacity; } 00787 00788 /** 00789 * @brief Attempt to preallocate enough memory for specified number of 00790 * characters. 00791 * @param __res_arg Number of characters required. 00792 * @throw std::length_error If @a __res_arg exceeds @c max_size(). 00793 * 00794 * This function attempts to reserve enough memory for the 00795 * %string to hold the specified number of characters. If the 00796 * number requested is more than max_size(), length_error is 00797 * thrown. 00798 * 00799 * The advantage of this function is that if optimal code is a 00800 * necessity and the user can determine the string length that will be 00801 * required, the user can reserve the memory in %advance, and thus 00802 * prevent a possible reallocation of memory and copying of %string 00803 * data. 00804 */ 00805 void 00806 reserve(size_type __res_arg = 0); 00807 00808 /** 00809 * Erases the string, making it empty. 00810 */ 00811 // PR 56166: this should not throw. 00812 void 00813 clear() 00814 { _M_mutate(0, this->size(), 0); } 00815 00816 /** 00817 * Returns true if the %string is empty. Equivalent to 00818 * <code>*this == ""</code>. 00819 */ 00820 bool 00821 empty() const _GLIBCXX_NOEXCEPT 00822 { return this->size() == 0; } 00823 00824 // Element access: 00825 /** 00826 * @brief Subscript access to the data contained in the %string. 00827 * @param __pos The index of the character to access. 00828 * @return Read-only (constant) reference to the character. 00829 * 00830 * This operator allows for easy, array-style, data access. 00831 * Note that data access with this operator is unchecked and 00832 * out_of_range lookups are not defined. (For checked lookups 00833 * see at().) 00834 */ 00835 const_reference 00836 operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT 00837 { 00838 _GLIBCXX_DEBUG_ASSERT(__pos <= size()); 00839 return _M_data()[__pos]; 00840 } 00841 00842 /** 00843 * @brief Subscript access to the data contained in the %string. 00844 * @param __pos The index of the character to access. 00845 * @return Read/write reference to the character. 00846 * 00847 * This operator allows for easy, array-style, data access. 00848 * Note that data access with this operator is unchecked and 00849 * out_of_range lookups are not defined. (For checked lookups 00850 * see at().) Unshares the string. 00851 */ 00852 reference 00853 operator[](size_type __pos) 00854 { 00855 // Allow pos == size() both in C++98 mode, as v3 extension, 00856 // and in C++11 mode. 00857 _GLIBCXX_DEBUG_ASSERT(__pos <= size()); 00858 // In pedantic mode be strict in C++98 mode. 00859 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size()); 00860 _M_leak(); 00861 return _M_data()[__pos]; 00862 } 00863 00864 /** 00865 * @brief Provides access to the data contained in the %string. 00866 * @param __n The index of the character to access. 00867 * @return Read-only (const) reference to the character. 00868 * @throw std::out_of_range If @a n is an invalid index. 00869 * 00870 * This function provides for safer data access. The parameter is 00871 * first checked that it is in the range of the string. The function 00872 * throws out_of_range if the check fails. 00873 */ 00874 const_reference 00875 at(size_type __n) const 00876 { 00877 if (__n >= this->size()) 00878 __throw_out_of_range_fmt(__N("basic_string::at: __n " 00879 "(which is %zu) >= this->size() " 00880 "(which is %zu)"), 00881 __n, this->size()); 00882 return _M_data()[__n]; 00883 } 00884 00885 /** 00886 * @brief Provides access to the data contained in the %string. 00887 * @param __n The index of the character to access. 00888 * @return Read/write reference to the character. 00889 * @throw std::out_of_range If @a n is an invalid index. 00890 * 00891 * This function provides for safer data access. The parameter is 00892 * first checked that it is in the range of the string. The function 00893 * throws out_of_range if the check fails. Success results in 00894 * unsharing the string. 00895 */ 00896 reference 00897 at(size_type __n) 00898 { 00899 if (__n >= size()) 00900 __throw_out_of_range_fmt(__N("basic_string::at: __n " 00901 "(which is %zu) >= this->size() " 00902 "(which is %zu)"), 00903 __n, this->size()); 00904 _M_leak(); 00905 return _M_data()[__n]; 00906 } 00907 00908 #if __cplusplus >= 201103L 00909 /** 00910 * Returns a read/write reference to the data at the first 00911 * element of the %string. 00912 */ 00913 reference 00914 front() 00915 { return operator[](0); } 00916 00917 /** 00918 * Returns a read-only (constant) reference to the data at the first 00919 * element of the %string. 00920 */ 00921 const_reference 00922 front() const _GLIBCXX_NOEXCEPT 00923 { return operator[](0); } 00924 00925 /** 00926 * Returns a read/write reference to the data at the last 00927 * element of the %string. 00928 */ 00929 reference 00930 back() 00931 { return operator[](this->size() - 1); } 00932 00933 /** 00934 * Returns a read-only (constant) reference to the data at the 00935 * last element of the %string. 00936 */ 00937 const_reference 00938 back() const _GLIBCXX_NOEXCEPT 00939 { return operator[](this->size() - 1); } 00940 #endif 00941 00942 // Modifiers: 00943 /** 00944 * @brief Append a string to this string. 00945 * @param __str The string to append. 00946 * @return Reference to this string. 00947 */ 00948 basic_string& 00949 operator+=(const basic_string& __str) 00950 { return this->append(__str); } 00951 00952 /** 00953 * @brief Append a C string. 00954 * @param __s The C string to append. 00955 * @return Reference to this string. 00956 */ 00957 basic_string& 00958 operator+=(const _CharT* __s) 00959 { return this->append(__s); } 00960 00961 /** 00962 * @brief Append a character. 00963 * @param __c The character to append. 00964 * @return Reference to this string. 00965 */ 00966 basic_string& 00967 operator+=(_CharT __c) 00968 { 00969 this->push_back(__c); 00970 return *this; 00971 } 00972 00973 #if __cplusplus >= 201103L 00974 /** 00975 * @brief Append an initializer_list of characters. 00976 * @param __l The initializer_list of characters to be appended. 00977 * @return Reference to this string. 00978 */ 00979 basic_string& 00980 operator+=(initializer_list<_CharT> __l) 00981 { return this->append(__l.begin(), __l.size()); } 00982 #endif // C++11 00983 00984 /** 00985 * @brief Append a string to this string. 00986 * @param __str The string to append. 00987 * @return Reference to this string. 00988 */ 00989 basic_string& 00990 append(const basic_string& __str); 00991 00992 /** 00993 * @brief Append a substring. 00994 * @param __str The string to append. 00995 * @param __pos Index of the first character of str to append. 00996 * @param __n The number of characters to append. 00997 * @return Reference to this string. 00998 * @throw std::out_of_range if @a __pos is not a valid index. 00999 * 01000 * This function appends @a __n characters from @a __str 01001 * starting at @a __pos to this string. If @a __n is is larger 01002 * than the number of available characters in @a __str, the 01003 * remainder of @a __str is appended. 01004 */ 01005 basic_string& 01006 append(const basic_string& __str, size_type __pos, size_type __n); 01007 01008 /** 01009 * @brief Append a C substring. 01010 * @param __s The C string to append. 01011 * @param __n The number of characters to append. 01012 * @return Reference to this string. 01013 */ 01014 basic_string& 01015 append(const _CharT* __s, size_type __n); 01016 01017 /** 01018 * @brief Append a C string. 01019 * @param __s The C string to append. 01020 * @return Reference to this string. 01021 */ 01022 basic_string& 01023 append(const _CharT* __s) 01024 { 01025 __glibcxx_requires_string(__s); 01026 return this->append(__s, traits_type::length(__s)); 01027 } 01028 01029 /** 01030 * @brief Append multiple characters. 01031 * @param __n The number of characters to append. 01032 * @param __c The character to use. 01033 * @return Reference to this string. 01034 * 01035 * Appends __n copies of __c to this string. 01036 */ 01037 basic_string& 01038 append(size_type __n, _CharT __c); 01039 01040 #if __cplusplus >= 201103L 01041 /** 01042 * @brief Append an initializer_list of characters. 01043 * @param __l The initializer_list of characters to append. 01044 * @return Reference to this string. 01045 */ 01046 basic_string& 01047 append(initializer_list<_CharT> __l) 01048 { return this->append(__l.begin(), __l.size()); } 01049 #endif // C++11 01050 01051 /** 01052 * @brief Append a range of characters. 01053 * @param __first Iterator referencing the first character to append. 01054 * @param __last Iterator marking the end of the range. 01055 * @return Reference to this string. 01056 * 01057 * Appends characters in the range [__first,__last) to this string. 01058 */ 01059 template<class _InputIterator> 01060 basic_string& 01061 append(_InputIterator __first, _InputIterator __last) 01062 { return this->replace(_M_iend(), _M_iend(), __first, __last); } 01063 01064 /** 01065 * @brief Append a single character. 01066 * @param __c Character to append. 01067 */ 01068 void 01069 push_back(_CharT __c) 01070 { 01071 const size_type __len = 1 + this->size(); 01072 if (__len > this->capacity() || _M_rep()->_M_is_shared()) 01073 this->reserve(__len); 01074 traits_type::assign(_M_data()[this->size()], __c); 01075 _M_rep()->_M_set_length_and_sharable(__len); 01076 } 01077 01078 /** 01079 * @brief Set value to contents of another string. 01080 * @param __str Source string to use. 01081 * @return Reference to this string. 01082 */ 01083 basic_string& 01084 assign(const basic_string& __str); 01085 01086 #if __cplusplus >= 201103L 01087 /** 01088 * @brief Set value to contents of another string. 01089 * @param __str Source string to use. 01090 * @return Reference to this string. 01091 * 01092 * This function sets this string to the exact contents of @a __str. 01093 * @a __str is a valid, but unspecified string. 01094 */ 01095 // PR 58265, this should be noexcept. 01096 basic_string& 01097 assign(basic_string&& __str) 01098 { 01099 this->swap(__str); 01100 return *this; 01101 } 01102 #endif // C++11 01103 01104 /** 01105 * @brief Set value to a substring of a string. 01106 * @param __str The string to use. 01107 * @param __pos Index of the first character of str. 01108 * @param __n Number of characters to use. 01109 * @return Reference to this string. 01110 * @throw std::out_of_range if @a pos is not a valid index. 01111 * 01112 * This function sets this string to the substring of @a __str 01113 * consisting of @a __n characters at @a __pos. If @a __n is 01114 * is larger than the number of available characters in @a 01115 * __str, the remainder of @a __str is used. 01116 */ 01117 basic_string& 01118 assign(const basic_string& __str, size_type __pos, size_type __n) 01119 { return this->assign(__str._M_data() 01120 + __str._M_check(__pos, "basic_string::assign"), 01121 __str._M_limit(__pos, __n)); } 01122 01123 /** 01124 * @brief Set value to a C substring. 01125 * @param __s The C string to use. 01126 * @param __n Number of characters to use. 01127 * @return Reference to this string. 01128 * 01129 * This function sets the value of this string to the first @a __n 01130 * characters of @a __s. If @a __n is is larger than the number of 01131 * available characters in @a __s, the remainder of @a __s is used. 01132 */ 01133 basic_string& 01134 assign(const _CharT* __s, size_type __n); 01135 01136 /** 01137 * @brief Set value to contents of a C string. 01138 * @param __s The C string to use. 01139 * @return Reference to this string. 01140 * 01141 * This function sets the value of this string to the value of @a __s. 01142 * The data is copied, so there is no dependence on @a __s once the 01143 * function returns. 01144 */ 01145 basic_string& 01146 assign(const _CharT* __s) 01147 { 01148 __glibcxx_requires_string(__s); 01149 return this->assign(__s, traits_type::length(__s)); 01150 } 01151 01152 /** 01153 * @brief Set value to multiple characters. 01154 * @param __n Length of the resulting string. 01155 * @param __c The character to use. 01156 * @return Reference to this string. 01157 * 01158 * This function sets the value of this string to @a __n copies of 01159 * character @a __c. 01160 */ 01161 basic_string& 01162 assign(size_type __n, _CharT __c) 01163 { return _M_replace_aux(size_type(0), this->size(), __n, __c); } 01164 01165 /** 01166 * @brief Set value to a range of characters. 01167 * @param __first Iterator referencing the first character to append. 01168 * @param __last Iterator marking the end of the range. 01169 * @return Reference to this string. 01170 * 01171 * Sets value of string to characters in the range [__first,__last). 01172 */ 01173 template<class _InputIterator> 01174 basic_string& 01175 assign(_InputIterator __first, _InputIterator __last) 01176 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); } 01177 01178 #if __cplusplus >= 201103L 01179 /** 01180 * @brief Set value to an initializer_list of characters. 01181 * @param __l The initializer_list of characters to assign. 01182 * @return Reference to this string. 01183 */ 01184 basic_string& 01185 assign(initializer_list<_CharT> __l) 01186 { return this->assign(__l.begin(), __l.size()); } 01187 #endif // C++11 01188 01189 /** 01190 * @brief Insert multiple characters. 01191 * @param __p Iterator referencing location in string to insert at. 01192 * @param __n Number of characters to insert 01193 * @param __c The character to insert. 01194 * @throw std::length_error If new length exceeds @c max_size(). 01195 * 01196 * Inserts @a __n copies of character @a __c starting at the 01197 * position referenced by iterator @a __p. If adding 01198 * characters causes the length to exceed max_size(), 01199 * length_error is thrown. The value of the string doesn't 01200 * change if an error is thrown. 01201 */ 01202 void 01203 insert(iterator __p, size_type __n, _CharT __c) 01204 { this->replace(__p, __p, __n, __c); } 01205 01206 /** 01207 * @brief Insert a range of characters. 01208 * @param __p Iterator referencing location in string to insert at. 01209 * @param __beg Start of range. 01210 * @param __end End of range. 01211 * @throw std::length_error If new length exceeds @c max_size(). 01212 * 01213 * Inserts characters in range [__beg,__end). If adding 01214 * characters causes the length to exceed max_size(), 01215 * length_error is thrown. The value of the string doesn't 01216 * change if an error is thrown. 01217 */ 01218 template<class _InputIterator> 01219 void 01220 insert(iterator __p, _InputIterator __beg, _InputIterator __end) 01221 { this->replace(__p, __p, __beg, __end); } 01222 01223 #if __cplusplus >= 201103L 01224 /** 01225 * @brief Insert an initializer_list of characters. 01226 * @param __p Iterator referencing location in string to insert at. 01227 * @param __l The initializer_list of characters to insert. 01228 * @throw std::length_error If new length exceeds @c max_size(). 01229 */ 01230 void 01231 insert(iterator __p, initializer_list<_CharT> __l) 01232 { 01233 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend()); 01234 this->insert(__p - _M_ibegin(), __l.begin(), __l.size()); 01235 } 01236 #endif // C++11 01237 01238 /** 01239 * @brief Insert value of a string. 01240 * @param __pos1 Iterator referencing location in string to insert at. 01241 * @param __str The string to insert. 01242 * @return Reference to this string. 01243 * @throw std::length_error If new length exceeds @c max_size(). 01244 * 01245 * Inserts value of @a __str starting at @a __pos1. If adding 01246 * characters causes the length to exceed max_size(), 01247 * length_error is thrown. The value of the string doesn't 01248 * change if an error is thrown. 01249 */ 01250 basic_string& 01251 insert(size_type __pos1, const basic_string& __str) 01252 { return this->insert(__pos1, __str, size_type(0), __str.size()); } 01253 01254 /** 01255 * @brief Insert a substring. 01256 * @param __pos1 Iterator referencing location in string to insert at. 01257 * @param __str The string to insert. 01258 * @param __pos2 Start of characters in str to insert. 01259 * @param __n Number of characters to insert. 01260 * @return Reference to this string. 01261 * @throw std::length_error If new length exceeds @c max_size(). 01262 * @throw std::out_of_range If @a pos1 > size() or 01263 * @a __pos2 > @a str.size(). 01264 * 01265 * Starting at @a pos1, insert @a __n character of @a __str 01266 * beginning with @a __pos2. If adding characters causes the 01267 * length to exceed max_size(), length_error is thrown. If @a 01268 * __pos1 is beyond the end of this string or @a __pos2 is 01269 * beyond the end of @a __str, out_of_range is thrown. The 01270 * value of the string doesn't change if an error is thrown. 01271 */ 01272 basic_string& 01273 insert(size_type __pos1, const basic_string& __str, 01274 size_type __pos2, size_type __n) 01275 { return this->insert(__pos1, __str._M_data() 01276 + __str._M_check(__pos2, "basic_string::insert"), 01277 __str._M_limit(__pos2, __n)); } 01278 01279 /** 01280 * @brief Insert a C substring. 01281 * @param __pos Iterator referencing location in string to insert at. 01282 * @param __s The C string to insert. 01283 * @param __n The number of characters to insert. 01284 * @return Reference to this string. 01285 * @throw std::length_error If new length exceeds @c max_size(). 01286 * @throw std::out_of_range If @a __pos is beyond the end of this 01287 * string. 01288 * 01289 * Inserts the first @a __n characters of @a __s starting at @a 01290 * __pos. If adding characters causes the length to exceed 01291 * max_size(), length_error is thrown. If @a __pos is beyond 01292 * end(), out_of_range is thrown. The value of the string 01293 * doesn't change if an error is thrown. 01294 */ 01295 basic_string& 01296 insert(size_type __pos, const _CharT* __s, size_type __n); 01297 01298 /** 01299 * @brief Insert a C string. 01300 * @param __pos Iterator referencing location in string to insert at. 01301 * @param __s The C string to insert. 01302 * @return Reference to this string. 01303 * @throw std::length_error If new length exceeds @c max_size(). 01304 * @throw std::out_of_range If @a pos is beyond the end of this 01305 * string. 01306 * 01307 * Inserts the first @a n characters of @a __s starting at @a __pos. If 01308 * adding characters causes the length to exceed max_size(), 01309 * length_error is thrown. If @a __pos is beyond end(), out_of_range is 01310 * thrown. The value of the string doesn't change if an error is 01311 * thrown. 01312 */ 01313 basic_string& 01314 insert(size_type __pos, const _CharT* __s) 01315 { 01316 __glibcxx_requires_string(__s); 01317 return this->insert(__pos, __s, traits_type::length(__s)); 01318 } 01319 01320 /** 01321 * @brief Insert multiple characters. 01322 * @param __pos Index in string to insert at. 01323 * @param __n Number of characters to insert 01324 * @param __c The character to insert. 01325 * @return Reference to this string. 01326 * @throw std::length_error If new length exceeds @c max_size(). 01327 * @throw std::out_of_range If @a __pos is beyond the end of this 01328 * string. 01329 * 01330 * Inserts @a __n copies of character @a __c starting at index 01331 * @a __pos. If adding characters causes the length to exceed 01332 * max_size(), length_error is thrown. If @a __pos > length(), 01333 * out_of_range is thrown. The value of the string doesn't 01334 * change if an error is thrown. 01335 */ 01336 basic_string& 01337 insert(size_type __pos, size_type __n, _CharT __c) 01338 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"), 01339 size_type(0), __n, __c); } 01340 01341 /** 01342 * @brief Insert one character. 01343 * @param __p Iterator referencing position in string to insert at. 01344 * @param __c The character to insert. 01345 * @return Iterator referencing newly inserted char. 01346 * @throw std::length_error If new length exceeds @c max_size(). 01347 * 01348 * Inserts character @a __c at position referenced by @a __p. 01349 * If adding character causes the length to exceed max_size(), 01350 * length_error is thrown. If @a __p is beyond end of string, 01351 * out_of_range is thrown. The value of the string doesn't 01352 * change if an error is thrown. 01353 */ 01354 iterator 01355 insert(iterator __p, _CharT __c) 01356 { 01357 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend()); 01358 const size_type __pos = __p - _M_ibegin(); 01359 _M_replace_aux(__pos, size_type(0), size_type(1), __c); 01360 _M_rep()->_M_set_leaked(); 01361 return iterator(_M_data() + __pos); 01362 } 01363 01364 /** 01365 * @brief Remove characters. 01366 * @param __pos Index of first character to remove (default 0). 01367 * @param __n Number of characters to remove (default remainder). 01368 * @return Reference to this string. 01369 * @throw std::out_of_range If @a pos is beyond the end of this 01370 * string. 01371 * 01372 * Removes @a __n characters from this string starting at @a 01373 * __pos. The length of the string is reduced by @a __n. If 01374 * there are < @a __n characters to remove, the remainder of 01375 * the string is truncated. If @a __p is beyond end of string, 01376 * out_of_range is thrown. The value of the string doesn't 01377 * change if an error is thrown. 01378 */ 01379 basic_string& 01380 erase(size_type __pos = 0, size_type __n = npos) 01381 { 01382 _M_mutate(_M_check(__pos, "basic_string::erase"), 01383 _M_limit(__pos, __n), size_type(0)); 01384 return *this; 01385 } 01386 01387 /** 01388 * @brief Remove one character. 01389 * @param __position Iterator referencing the character to remove. 01390 * @return iterator referencing same location after removal. 01391 * 01392 * Removes the character at @a __position from this string. The value 01393 * of the string doesn't change if an error is thrown. 01394 */ 01395 iterator 01396 erase(iterator __position) 01397 { 01398 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin() 01399 && __position < _M_iend()); 01400 const size_type __pos = __position - _M_ibegin(); 01401 _M_mutate(__pos, size_type(1), size_type(0)); 01402 _M_rep()->_M_set_leaked(); 01403 return iterator(_M_data() + __pos); 01404 } 01405 01406 /** 01407 * @brief Remove a range of characters. 01408 * @param __first Iterator referencing the first character to remove. 01409 * @param __last Iterator referencing the end of the range. 01410 * @return Iterator referencing location of first after removal. 01411 * 01412 * Removes the characters in the range [first,last) from this string. 01413 * The value of the string doesn't change if an error is thrown. 01414 */ 01415 iterator 01416 erase(iterator __first, iterator __last); 01417 01418 #if __cplusplus >= 201103L 01419 /** 01420 * @brief Remove the last character. 01421 * 01422 * The string must be non-empty. 01423 */ 01424 void 01425 pop_back() // FIXME C++11: should be noexcept. 01426 { erase(size()-1, 1); } 01427 #endif // C++11 01428 01429 /** 01430 * @brief Replace characters with value from another string. 01431 * @param __pos Index of first character to replace. 01432 * @param __n Number of characters to be replaced. 01433 * @param __str String to insert. 01434 * @return Reference to this string. 01435 * @throw std::out_of_range If @a pos is beyond the end of this 01436 * string. 01437 * @throw std::length_error If new length exceeds @c max_size(). 01438 * 01439 * Removes the characters in the range [__pos,__pos+__n) from 01440 * this string. In place, the value of @a __str is inserted. 01441 * If @a __pos is beyond end of string, out_of_range is thrown. 01442 * If the length of the result exceeds max_size(), length_error 01443 * is thrown. The value of the string doesn't change if an 01444 * error is thrown. 01445 */ 01446 basic_string& 01447 replace(size_type __pos, size_type __n, const basic_string& __str) 01448 { return this->replace(__pos, __n, __str._M_data(), __str.size()); } 01449 01450 /** 01451 * @brief Replace characters with value from another string. 01452 * @param __pos1 Index of first character to replace. 01453 * @param __n1 Number of characters to be replaced. 01454 * @param __str String to insert. 01455 * @param __pos2 Index of first character of str to use. 01456 * @param __n2 Number of characters from str to use. 01457 * @return Reference to this string. 01458 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 > 01459 * __str.size(). 01460 * @throw std::length_error If new length exceeds @c max_size(). 01461 * 01462 * Removes the characters in the range [__pos1,__pos1 + n) from this 01463 * string. In place, the value of @a __str is inserted. If @a __pos is 01464 * beyond end of string, out_of_range is thrown. If the length of the 01465 * result exceeds max_size(), length_error is thrown. The value of the 01466 * string doesn't change if an error is thrown. 01467 */ 01468 basic_string& 01469 replace(size_type __pos1, size_type __n1, const basic_string& __str, 01470 size_type __pos2, size_type __n2) 01471 { return this->replace(__pos1, __n1, __str._M_data() 01472 + __str._M_check(__pos2, "basic_string::replace"), 01473 __str._M_limit(__pos2, __n2)); } 01474 01475 /** 01476 * @brief Replace characters with value of a C substring. 01477 * @param __pos Index of first character to replace. 01478 * @param __n1 Number of characters to be replaced. 01479 * @param __s C string to insert. 01480 * @param __n2 Number of characters from @a s to use. 01481 * @return Reference to this string. 01482 * @throw std::out_of_range If @a pos1 > size(). 01483 * @throw std::length_error If new length exceeds @c max_size(). 01484 * 01485 * Removes the characters in the range [__pos,__pos + __n1) 01486 * from this string. In place, the first @a __n2 characters of 01487 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If 01488 * @a __pos is beyond end of string, out_of_range is thrown. If 01489 * the length of result exceeds max_size(), length_error is 01490 * thrown. The value of the string doesn't change if an error 01491 * is thrown. 01492 */ 01493 basic_string& 01494 replace(size_type __pos, size_type __n1, const _CharT* __s, 01495 size_type __n2); 01496 01497 /** 01498 * @brief Replace characters with value of a C string. 01499 * @param __pos Index of first character to replace. 01500 * @param __n1 Number of characters to be replaced. 01501 * @param __s C string to insert. 01502 * @return Reference to this string. 01503 * @throw std::out_of_range If @a pos > size(). 01504 * @throw std::length_error If new length exceeds @c max_size(). 01505 * 01506 * Removes the characters in the range [__pos,__pos + __n1) 01507 * from this string. In place, the characters of @a __s are 01508 * inserted. If @a __pos is beyond end of string, out_of_range 01509 * is thrown. If the length of result exceeds max_size(), 01510 * length_error is thrown. The value of the string doesn't 01511 * change if an error is thrown. 01512 */ 01513 basic_string& 01514 replace(size_type __pos, size_type __n1, const _CharT* __s) 01515 { 01516 __glibcxx_requires_string(__s); 01517 return this->replace(__pos, __n1, __s, traits_type::length(__s)); 01518 } 01519 01520 /** 01521 * @brief Replace characters with multiple characters. 01522 * @param __pos Index of first character to replace. 01523 * @param __n1 Number of characters to be replaced. 01524 * @param __n2 Number of characters to insert. 01525 * @param __c Character to insert. 01526 * @return Reference to this string. 01527 * @throw std::out_of_range If @a __pos > size(). 01528 * @throw std::length_error If new length exceeds @c max_size(). 01529 * 01530 * Removes the characters in the range [pos,pos + n1) from this 01531 * string. In place, @a __n2 copies of @a __c are inserted. 01532 * If @a __pos is beyond end of string, out_of_range is thrown. 01533 * If the length of result exceeds max_size(), length_error is 01534 * thrown. The value of the string doesn't change if an error 01535 * is thrown. 01536 */ 01537 basic_string& 01538 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c) 01539 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"), 01540 _M_limit(__pos, __n1), __n2, __c); } 01541 01542 /** 01543 * @brief Replace range of characters with string. 01544 * @param __i1 Iterator referencing start of range to replace. 01545 * @param __i2 Iterator referencing end of range to replace. 01546 * @param __str String value to insert. 01547 * @return Reference to this string. 01548 * @throw std::length_error If new length exceeds @c max_size(). 01549 * 01550 * Removes the characters in the range [__i1,__i2). In place, 01551 * the value of @a __str is inserted. If the length of result 01552 * exceeds max_size(), length_error is thrown. The value of 01553 * the string doesn't change if an error is thrown. 01554 */ 01555 basic_string& 01556 replace(iterator __i1, iterator __i2, const basic_string& __str) 01557 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); } 01558 01559 /** 01560 * @brief Replace range of characters with C substring. 01561 * @param __i1 Iterator referencing start of range to replace. 01562 * @param __i2 Iterator referencing end of range to replace. 01563 * @param __s C string value to insert. 01564 * @param __n Number of characters from s to insert. 01565 * @return Reference to this string. 01566 * @throw std::length_error If new length exceeds @c max_size(). 01567 * 01568 * Removes the characters in the range [__i1,__i2). In place, 01569 * the first @a __n characters of @a __s are inserted. If the 01570 * length of result exceeds max_size(), length_error is thrown. 01571 * The value of the string doesn't change if an error is 01572 * thrown. 01573 */ 01574 basic_string& 01575 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n) 01576 { 01577 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 01578 && __i2 <= _M_iend()); 01579 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n); 01580 } 01581 01582 /** 01583 * @brief Replace range of characters with C string. 01584 * @param __i1 Iterator referencing start of range to replace. 01585 * @param __i2 Iterator referencing end of range to replace. 01586 * @param __s C string value to insert. 01587 * @return Reference to this string. 01588 * @throw std::length_error If new length exceeds @c max_size(). 01589 * 01590 * Removes the characters in the range [__i1,__i2). In place, 01591 * the characters of @a __s are inserted. If the length of 01592 * result exceeds max_size(), length_error is thrown. The 01593 * value of the string doesn't change if an error is thrown. 01594 */ 01595 basic_string& 01596 replace(iterator __i1, iterator __i2, const _CharT* __s) 01597 { 01598 __glibcxx_requires_string(__s); 01599 return this->replace(__i1, __i2, __s, traits_type::length(__s)); 01600 } 01601 01602 /** 01603 * @brief Replace range of characters with multiple characters 01604 * @param __i1 Iterator referencing start of range to replace. 01605 * @param __i2 Iterator referencing end of range to replace. 01606 * @param __n Number of characters to insert. 01607 * @param __c Character to insert. 01608 * @return Reference to this string. 01609 * @throw std::length_error If new length exceeds @c max_size(). 01610 * 01611 * Removes the characters in the range [__i1,__i2). In place, 01612 * @a __n copies of @a __c are inserted. If the length of 01613 * result exceeds max_size(), length_error is thrown. The 01614 * value of the string doesn't change if an error is thrown. 01615 */ 01616 basic_string& 01617 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c) 01618 { 01619 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 01620 && __i2 <= _M_iend()); 01621 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c); 01622 } 01623 01624 /** 01625 * @brief Replace range of characters with range. 01626 * @param __i1 Iterator referencing start of range to replace. 01627 * @param __i2 Iterator referencing end of range to replace. 01628 * @param __k1 Iterator referencing start of range to insert. 01629 * @param __k2 Iterator referencing end of range to insert. 01630 * @return Reference to this string. 01631 * @throw std::length_error If new length exceeds @c max_size(). 01632 * 01633 * Removes the characters in the range [__i1,__i2). In place, 01634 * characters in the range [__k1,__k2) are inserted. If the 01635 * length of result exceeds max_size(), length_error is thrown. 01636 * The value of the string doesn't change if an error is 01637 * thrown. 01638 */ 01639 template<class _InputIterator> 01640 basic_string& 01641 replace(iterator __i1, iterator __i2, 01642 _InputIterator __k1, _InputIterator __k2) 01643 { 01644 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 01645 && __i2 <= _M_iend()); 01646 __glibcxx_requires_valid_range(__k1, __k2); 01647 typedef typename std::__is_integer<_InputIterator>::__type _Integral; 01648 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral()); 01649 } 01650 01651 // Specializations for the common case of pointer and iterator: 01652 // useful to avoid the overhead of temporary buffering in _M_replace. 01653 basic_string& 01654 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2) 01655 { 01656 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 01657 && __i2 <= _M_iend()); 01658 __glibcxx_requires_valid_range(__k1, __k2); 01659 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, 01660 __k1, __k2 - __k1); 01661 } 01662 01663 basic_string& 01664 replace(iterator __i1, iterator __i2, 01665 const _CharT* __k1, const _CharT* __k2) 01666 { 01667 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 01668 && __i2 <= _M_iend()); 01669 __glibcxx_requires_valid_range(__k1, __k2); 01670 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, 01671 __k1, __k2 - __k1); 01672 } 01673 01674 basic_string& 01675 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2) 01676 { 01677 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 01678 && __i2 <= _M_iend()); 01679 __glibcxx_requires_valid_range(__k1, __k2); 01680 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, 01681 __k1.base(), __k2 - __k1); 01682 } 01683 01684 basic_string& 01685 replace(iterator __i1, iterator __i2, 01686 const_iterator __k1, const_iterator __k2) 01687 { 01688 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 01689 && __i2 <= _M_iend()); 01690 __glibcxx_requires_valid_range(__k1, __k2); 01691 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, 01692 __k1.base(), __k2 - __k1); 01693 } 01694 01695 #if __cplusplus >= 201103L 01696 /** 01697 * @brief Replace range of characters with initializer_list. 01698 * @param __i1 Iterator referencing start of range to replace. 01699 * @param __i2 Iterator referencing end of range to replace. 01700 * @param __l The initializer_list of characters to insert. 01701 * @return Reference to this string. 01702 * @throw std::length_error If new length exceeds @c max_size(). 01703 * 01704 * Removes the characters in the range [__i1,__i2). In place, 01705 * characters in the range [__k1,__k2) are inserted. If the 01706 * length of result exceeds max_size(), length_error is thrown. 01707 * The value of the string doesn't change if an error is 01708 * thrown. 01709 */ 01710 basic_string& replace(iterator __i1, iterator __i2, 01711 initializer_list<_CharT> __l) 01712 { return this->replace(__i1, __i2, __l.begin(), __l.end()); } 01713 #endif // C++11 01714 01715 private: 01716 template<class _Integer> 01717 basic_string& 01718 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n, 01719 _Integer __val, __true_type) 01720 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); } 01721 01722 template<class _InputIterator> 01723 basic_string& 01724 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1, 01725 _InputIterator __k2, __false_type); 01726 01727 basic_string& 01728 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2, 01729 _CharT __c); 01730 01731 basic_string& 01732 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s, 01733 size_type __n2); 01734 01735 // _S_construct_aux is used to implement the 21.3.1 para 15 which 01736 // requires special behaviour if _InIter is an integral type 01737 template<class _InIterator> 01738 static _CharT* 01739 _S_construct_aux(_InIterator __beg, _InIterator __end, 01740 const _Alloc& __a, __false_type) 01741 { 01742 typedef typename iterator_traits<_InIterator>::iterator_category _Tag; 01743 return _S_construct(__beg, __end, __a, _Tag()); 01744 } 01745 01746 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01747 // 438. Ambiguity in the "do the right thing" clause 01748 template<class _Integer> 01749 static _CharT* 01750 _S_construct_aux(_Integer __beg, _Integer __end, 01751 const _Alloc& __a, __true_type) 01752 { return _S_construct_aux_2(static_cast<size_type>(__beg), 01753 __end, __a); } 01754 01755 static _CharT* 01756 _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a) 01757 { return _S_construct(__req, __c, __a); } 01758 01759 template<class _InIterator> 01760 static _CharT* 01761 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a) 01762 { 01763 typedef typename std::__is_integer<_InIterator>::__type _Integral; 01764 return _S_construct_aux(__beg, __end, __a, _Integral()); 01765 } 01766 01767 // For Input Iterators, used in istreambuf_iterators, etc. 01768 template<class _InIterator> 01769 static _CharT* 01770 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a, 01771 input_iterator_tag); 01772 01773 // For forward_iterators up to random_access_iterators, used for 01774 // string::iterator, _CharT*, etc. 01775 template<class _FwdIterator> 01776 static _CharT* 01777 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a, 01778 forward_iterator_tag); 01779 01780 static _CharT* 01781 _S_construct(size_type __req, _CharT __c, const _Alloc& __a); 01782 01783 public: 01784 01785 /** 01786 * @brief Copy substring into C string. 01787 * @param __s C string to copy value into. 01788 * @param __n Number of characters to copy. 01789 * @param __pos Index of first character to copy. 01790 * @return Number of characters actually copied 01791 * @throw std::out_of_range If __pos > size(). 01792 * 01793 * Copies up to @a __n characters starting at @a __pos into the 01794 * C string @a __s. If @a __pos is %greater than size(), 01795 * out_of_range is thrown. 01796 */ 01797 size_type 01798 copy(_CharT* __s, size_type __n, size_type __pos = 0) const; 01799 01800 /** 01801 * @brief Swap contents with another string. 01802 * @param __s String to swap with. 01803 * 01804 * Exchanges the contents of this string with that of @a __s in constant 01805 * time. 01806 */ 01807 // PR 58265, this should be noexcept. 01808 void 01809 swap(basic_string& __s); 01810 01811 // String operations: 01812 /** 01813 * @brief Return const pointer to null-terminated contents. 01814 * 01815 * This is a handle to internal data. Do not modify or dire things may 01816 * happen. 01817 */ 01818 const _CharT* 01819 c_str() const _GLIBCXX_NOEXCEPT 01820 { return _M_data(); } 01821 01822 /** 01823 * @brief Return const pointer to contents. 01824 * 01825 * This is a handle to internal data. Do not modify or dire things may 01826 * happen. 01827 */ 01828 const _CharT* 01829 data() const _GLIBCXX_NOEXCEPT 01830 { return _M_data(); } 01831 01832 /** 01833 * @brief Return copy of allocator used to construct this string. 01834 */ 01835 allocator_type 01836 get_allocator() const _GLIBCXX_NOEXCEPT 01837 { return _M_dataplus; } 01838 01839 /** 01840 * @brief Find position of a C substring. 01841 * @param __s C string to locate. 01842 * @param __pos Index of character to search from. 01843 * @param __n Number of characters from @a s to search for. 01844 * @return Index of start of first occurrence. 01845 * 01846 * Starting from @a __pos, searches forward for the first @a 01847 * __n characters in @a __s within this string. If found, 01848 * returns the index where it begins. If not found, returns 01849 * npos. 01850 */ 01851 size_type 01852 find(const _CharT* __s, size_type __pos, size_type __n) const; 01853 01854 /** 01855 * @brief Find position of a string. 01856 * @param __str String to locate. 01857 * @param __pos Index of character to search from (default 0). 01858 * @return Index of start of first occurrence. 01859 * 01860 * Starting from @a __pos, searches forward for value of @a __str within 01861 * this string. If found, returns the index where it begins. If not 01862 * found, returns npos. 01863 */ 01864 size_type 01865 find(const basic_string& __str, size_type __pos = 0) const 01866 _GLIBCXX_NOEXCEPT 01867 { return this->find(__str.data(), __pos, __str.size()); } 01868 01869 /** 01870 * @brief Find position of a C string. 01871 * @param __s C string to locate. 01872 * @param __pos Index of character to search from (default 0). 01873 * @return Index of start of first occurrence. 01874 * 01875 * Starting from @a __pos, searches forward for the value of @a 01876 * __s within this string. If found, returns the index where 01877 * it begins. If not found, returns npos. 01878 */ 01879 size_type 01880 find(const _CharT* __s, size_type __pos = 0) const 01881 { 01882 __glibcxx_requires_string(__s); 01883 return this->find(__s, __pos, traits_type::length(__s)); 01884 } 01885 01886 /** 01887 * @brief Find position of a character. 01888 * @param __c Character to locate. 01889 * @param __pos Index of character to search from (default 0). 01890 * @return Index of first occurrence. 01891 * 01892 * Starting from @a __pos, searches forward for @a __c within 01893 * this string. If found, returns the index where it was 01894 * found. If not found, returns npos. 01895 */ 01896 size_type 01897 find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT; 01898 01899 /** 01900 * @brief Find last position of a string. 01901 * @param __str String to locate. 01902 * @param __pos Index of character to search back from (default end). 01903 * @return Index of start of last occurrence. 01904 * 01905 * Starting from @a __pos, searches backward for value of @a 01906 * __str within this string. If found, returns the index where 01907 * it begins. If not found, returns npos. 01908 */ 01909 size_type 01910 rfind(const basic_string& __str, size_type __pos = npos) const 01911 _GLIBCXX_NOEXCEPT 01912 { return this->rfind(__str.data(), __pos, __str.size()); } 01913 01914 /** 01915 * @brief Find last position of a C substring. 01916 * @param __s C string to locate. 01917 * @param __pos Index of character to search back from. 01918 * @param __n Number of characters from s to search for. 01919 * @return Index of start of last occurrence. 01920 * 01921 * Starting from @a __pos, searches backward for the first @a 01922 * __n characters in @a __s within this string. If found, 01923 * returns the index where it begins. If not found, returns 01924 * npos. 01925 */ 01926 size_type 01927 rfind(const _CharT* __s, size_type __pos, size_type __n) const; 01928 01929 /** 01930 * @brief Find last position of a C string. 01931 * @param __s C string to locate. 01932 * @param __pos Index of character to start search at (default end). 01933 * @return Index of start of last occurrence. 01934 * 01935 * Starting from @a __pos, searches backward for the value of 01936 * @a __s within this string. If found, returns the index 01937 * where it begins. If not found, returns npos. 01938 */ 01939 size_type 01940 rfind(const _CharT* __s, size_type __pos = npos) const 01941 { 01942 __glibcxx_requires_string(__s); 01943 return this->rfind(__s, __pos, traits_type::length(__s)); 01944 } 01945 01946 /** 01947 * @brief Find last position of a character. 01948 * @param __c Character to locate. 01949 * @param __pos Index of character to search back from (default end). 01950 * @return Index of last occurrence. 01951 * 01952 * Starting from @a __pos, searches backward for @a __c within 01953 * this string. If found, returns the index where it was 01954 * found. If not found, returns npos. 01955 */ 01956 size_type 01957 rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT; 01958 01959 /** 01960 * @brief Find position of a character of string. 01961 * @param __str String containing characters to locate. 01962 * @param __pos Index of character to search from (default 0). 01963 * @return Index of first occurrence. 01964 * 01965 * Starting from @a __pos, searches forward for one of the 01966 * characters of @a __str within this string. If found, 01967 * returns the index where it was found. If not found, returns 01968 * npos. 01969 */ 01970 size_type 01971 find_first_of(const basic_string& __str, size_type __pos = 0) const 01972 _GLIBCXX_NOEXCEPT 01973 { return this->find_first_of(__str.data(), __pos, __str.size()); } 01974 01975 /** 01976 * @brief Find position of a character of C substring. 01977 * @param __s String containing characters to locate. 01978 * @param __pos Index of character to search from. 01979 * @param __n Number of characters from s to search for. 01980 * @return Index of first occurrence. 01981 * 01982 * Starting from @a __pos, searches forward for one of the 01983 * first @a __n characters of @a __s within this string. If 01984 * found, returns the index where it was found. If not found, 01985 * returns npos. 01986 */ 01987 size_type 01988 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const; 01989 01990 /** 01991 * @brief Find position of a character of C string. 01992 * @param __s String containing characters to locate. 01993 * @param __pos Index of character to search from (default 0). 01994 * @return Index of first occurrence. 01995 * 01996 * Starting from @a __pos, searches forward for one of the 01997 * characters of @a __s within this string. If found, returns 01998 * the index where it was found. If not found, returns npos. 01999 */ 02000 size_type 02001 find_first_of(const _CharT* __s, size_type __pos = 0) const 02002 { 02003 __glibcxx_requires_string(__s); 02004 return this->find_first_of(__s, __pos, traits_type::length(__s)); 02005 } 02006 02007 /** 02008 * @brief Find position of a character. 02009 * @param __c Character to locate. 02010 * @param __pos Index of character to search from (default 0). 02011 * @return Index of first occurrence. 02012 * 02013 * Starting from @a __pos, searches forward for the character 02014 * @a __c within this string. If found, returns the index 02015 * where it was found. If not found, returns npos. 02016 * 02017 * Note: equivalent to find(__c, __pos). 02018 */ 02019 size_type 02020 find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT 02021 { return this->find(__c, __pos); } 02022 02023 /** 02024 * @brief Find last position of a character of string. 02025 * @param __str String containing characters to locate. 02026 * @param __pos Index of character to search back from (default end). 02027 * @return Index of last occurrence. 02028 * 02029 * Starting from @a __pos, searches backward for one of the 02030 * characters of @a __str within this string. If found, 02031 * returns the index where it was found. If not found, returns 02032 * npos. 02033 */ 02034 size_type 02035 find_last_of(const basic_string& __str, size_type __pos = npos) const 02036 _GLIBCXX_NOEXCEPT 02037 { return this->find_last_of(__str.data(), __pos, __str.size()); } 02038 02039 /** 02040 * @brief Find last position of a character of C substring. 02041 * @param __s C string containing characters to locate. 02042 * @param __pos Index of character to search back from. 02043 * @param __n Number of characters from s to search for. 02044 * @return Index of last occurrence. 02045 * 02046 * Starting from @a __pos, searches backward for one of the 02047 * first @a __n characters of @a __s within this string. If 02048 * found, returns the index where it was found. If not found, 02049 * returns npos. 02050 */ 02051 size_type 02052 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const; 02053 02054 /** 02055 * @brief Find last position of a character of C string. 02056 * @param __s C string containing characters to locate. 02057 * @param __pos Index of character to search back from (default end). 02058 * @return Index of last occurrence. 02059 * 02060 * Starting from @a __pos, searches backward for one of the 02061 * characters of @a __s within this string. If found, returns 02062 * the index where it was found. If not found, returns npos. 02063 */ 02064 size_type 02065 find_last_of(const _CharT* __s, size_type __pos = npos) const 02066 { 02067 __glibcxx_requires_string(__s); 02068 return this->find_last_of(__s, __pos, traits_type::length(__s)); 02069 } 02070 02071 /** 02072 * @brief Find last position of a character. 02073 * @param __c Character to locate. 02074 * @param __pos Index of character to search back from (default end). 02075 * @return Index of last occurrence. 02076 * 02077 * Starting from @a __pos, searches backward for @a __c within 02078 * this string. If found, returns the index where it was 02079 * found. If not found, returns npos. 02080 * 02081 * Note: equivalent to rfind(__c, __pos). 02082 */ 02083 size_type 02084 find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT 02085 { return this->rfind(__c, __pos); } 02086 02087 /** 02088 * @brief Find position of a character not in string. 02089 * @param __str String containing characters to avoid. 02090 * @param __pos Index of character to search from (default 0). 02091 * @return Index of first occurrence. 02092 * 02093 * Starting from @a __pos, searches forward for a character not contained 02094 * in @a __str within this string. If found, returns the index where it 02095 * was found. If not found, returns npos. 02096 */ 02097 size_type 02098 find_first_not_of(const basic_string& __str, size_type __pos = 0) const 02099 _GLIBCXX_NOEXCEPT 02100 { return this->find_first_not_of(__str.data(), __pos, __str.size()); } 02101 02102 /** 02103 * @brief Find position of a character not in C substring. 02104 * @param __s C string containing characters to avoid. 02105 * @param __pos Index of character to search from. 02106 * @param __n Number of characters from __s to consider. 02107 * @return Index of first occurrence. 02108 * 02109 * Starting from @a __pos, searches forward for a character not 02110 * contained in the first @a __n characters of @a __s within 02111 * this string. If found, returns the index where it was 02112 * found. If not found, returns npos. 02113 */ 02114 size_type 02115 find_first_not_of(const _CharT* __s, size_type __pos, 02116 size_type __n) const; 02117 02118 /** 02119 * @brief Find position of a character not in C string. 02120 * @param __s C string containing characters to avoid. 02121 * @param __pos Index of character to search from (default 0). 02122 * @return Index of first occurrence. 02123 * 02124 * Starting from @a __pos, searches forward for a character not 02125 * contained in @a __s within this string. If found, returns 02126 * the index where it was found. If not found, returns npos. 02127 */ 02128 size_type 02129 find_first_not_of(const _CharT* __s, size_type __pos = 0) const 02130 { 02131 __glibcxx_requires_string(__s); 02132 return this->find_first_not_of(__s, __pos, traits_type::length(__s)); 02133 } 02134 02135 /** 02136 * @brief Find position of a different character. 02137 * @param __c Character to avoid. 02138 * @param __pos Index of character to search from (default 0). 02139 * @return Index of first occurrence. 02140 * 02141 * Starting from @a __pos, searches forward for a character 02142 * other than @a __c within this string. If found, returns the 02143 * index where it was found. If not found, returns npos. 02144 */ 02145 size_type 02146 find_first_not_of(_CharT __c, size_type __pos = 0) const 02147 _GLIBCXX_NOEXCEPT; 02148 02149 /** 02150 * @brief Find last position of a character not in string. 02151 * @param __str String containing characters to avoid. 02152 * @param __pos Index of character to search back from (default end). 02153 * @return Index of last occurrence. 02154 * 02155 * Starting from @a __pos, searches backward for a character 02156 * not contained in @a __str within this string. If found, 02157 * returns the index where it was found. If not found, returns 02158 * npos. 02159 */ 02160 size_type 02161 find_last_not_of(const basic_string& __str, size_type __pos = npos) const 02162 _GLIBCXX_NOEXCEPT 02163 { return this->find_last_not_of(__str.data(), __pos, __str.size()); } 02164 02165 /** 02166 * @brief Find last position of a character not in C substring. 02167 * @param __s C string containing characters to avoid. 02168 * @param __pos Index of character to search back from. 02169 * @param __n Number of characters from s to consider. 02170 * @return Index of last occurrence. 02171 * 02172 * Starting from @a __pos, searches backward for a character not 02173 * contained in the first @a __n characters of @a __s within this string. 02174 * If found, returns the index where it was found. If not found, 02175 * returns npos. 02176 */ 02177 size_type 02178 find_last_not_of(const _CharT* __s, size_type __pos, 02179 size_type __n) const; 02180 /** 02181 * @brief Find last position of a character not in C string. 02182 * @param __s C string containing characters to avoid. 02183 * @param __pos Index of character to search back from (default end). 02184 * @return Index of last occurrence. 02185 * 02186 * Starting from @a __pos, searches backward for a character 02187 * not contained in @a __s within this string. If found, 02188 * returns the index where it was found. If not found, returns 02189 * npos. 02190 */ 02191 size_type 02192 find_last_not_of(const _CharT* __s, size_type __pos = npos) const 02193 { 02194 __glibcxx_requires_string(__s); 02195 return this->find_last_not_of(__s, __pos, traits_type::length(__s)); 02196 } 02197 02198 /** 02199 * @brief Find last position of a different character. 02200 * @param __c Character to avoid. 02201 * @param __pos Index of character to search back from (default end). 02202 * @return Index of last occurrence. 02203 * 02204 * Starting from @a __pos, searches backward for a character other than 02205 * @a __c within this string. If found, returns the index where it was 02206 * found. If not found, returns npos. 02207 */ 02208 size_type 02209 find_last_not_of(_CharT __c, size_type __pos = npos) const 02210 _GLIBCXX_NOEXCEPT; 02211 02212 /** 02213 * @brief Get a substring. 02214 * @param __pos Index of first character (default 0). 02215 * @param __n Number of characters in substring (default remainder). 02216 * @return The new string. 02217 * @throw std::out_of_range If __pos > size(). 02218 * 02219 * Construct and return a new string using the @a __n 02220 * characters starting at @a __pos. If the string is too 02221 * short, use the remainder of the characters. If @a __pos is 02222 * beyond the end of the string, out_of_range is thrown. 02223 */ 02224 basic_string 02225 substr(size_type __pos = 0, size_type __n = npos) const 02226 { return basic_string(*this, 02227 _M_check(__pos, "basic_string::substr"), __n); } 02228 02229 /** 02230 * @brief Compare to a string. 02231 * @param __str String to compare against. 02232 * @return Integer < 0, 0, or > 0. 02233 * 02234 * Returns an integer < 0 if this string is ordered before @a 02235 * __str, 0 if their values are equivalent, or > 0 if this 02236 * string is ordered after @a __str. Determines the effective 02237 * length rlen of the strings to compare as the smallest of 02238 * size() and str.size(). The function then compares the two 02239 * strings by calling traits::compare(data(), str.data(),rlen). 02240 * If the result of the comparison is nonzero returns it, 02241 * otherwise the shorter one is ordered first. 02242 */ 02243 int 02244 compare(const basic_string& __str) const 02245 { 02246 const size_type __size = this->size(); 02247 const size_type __osize = __str.size(); 02248 const size_type __len = std::min(__size, __osize); 02249 02250 int __r = traits_type::compare(_M_data(), __str.data(), __len); 02251 if (!__r) 02252 __r = _S_compare(__size, __osize); 02253 return __r; 02254 } 02255 02256 /** 02257 * @brief Compare substring to a string. 02258 * @param __pos Index of first character of substring. 02259 * @param __n Number of characters in substring. 02260 * @param __str String to compare against. 02261 * @return Integer < 0, 0, or > 0. 02262 * 02263 * Form the substring of this string from the @a __n characters 02264 * starting at @a __pos. Returns an integer < 0 if the 02265 * substring is ordered before @a __str, 0 if their values are 02266 * equivalent, or > 0 if the substring is ordered after @a 02267 * __str. Determines the effective length rlen of the strings 02268 * to compare as the smallest of the length of the substring 02269 * and @a __str.size(). The function then compares the two 02270 * strings by calling 02271 * traits::compare(substring.data(),str.data(),rlen). If the 02272 * result of the comparison is nonzero returns it, otherwise 02273 * the shorter one is ordered first. 02274 */ 02275 int 02276 compare(size_type __pos, size_type __n, const basic_string& __str) const; 02277 02278 /** 02279 * @brief Compare substring to a substring. 02280 * @param __pos1 Index of first character of substring. 02281 * @param __n1 Number of characters in substring. 02282 * @param __str String to compare against. 02283 * @param __pos2 Index of first character of substring of str. 02284 * @param __n2 Number of characters in substring of str. 02285 * @return Integer < 0, 0, or > 0. 02286 * 02287 * Form the substring of this string from the @a __n1 02288 * characters starting at @a __pos1. Form the substring of @a 02289 * __str from the @a __n2 characters starting at @a __pos2. 02290 * Returns an integer < 0 if this substring is ordered before 02291 * the substring of @a __str, 0 if their values are equivalent, 02292 * or > 0 if this substring is ordered after the substring of 02293 * @a __str. Determines the effective length rlen of the 02294 * strings to compare as the smallest of the lengths of the 02295 * substrings. The function then compares the two strings by 02296 * calling 02297 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen). 02298 * If the result of the comparison is nonzero returns it, 02299 * otherwise the shorter one is ordered first. 02300 */ 02301 int 02302 compare(size_type __pos1, size_type __n1, const basic_string& __str, 02303 size_type __pos2, size_type __n2) const; 02304 02305 /** 02306 * @brief Compare to a C string. 02307 * @param __s C string to compare against. 02308 * @return Integer < 0, 0, or > 0. 02309 * 02310 * Returns an integer < 0 if this string is ordered before @a __s, 0 if 02311 * their values are equivalent, or > 0 if this string is ordered after 02312 * @a __s. Determines the effective length rlen of the strings to 02313 * compare as the smallest of size() and the length of a string 02314 * constructed from @a __s. The function then compares the two strings 02315 * by calling traits::compare(data(),s,rlen). If the result of the 02316 * comparison is nonzero returns it, otherwise the shorter one is 02317 * ordered first. 02318 */ 02319 int 02320 compare(const _CharT* __s) const; 02321 02322 // _GLIBCXX_RESOLVE_LIB_DEFECTS 02323 // 5 String::compare specification questionable 02324 /** 02325 * @brief Compare substring to a C string. 02326 * @param __pos Index of first character of substring. 02327 * @param __n1 Number of characters in substring. 02328 * @param __s C string to compare against. 02329 * @return Integer < 0, 0, or > 0. 02330 * 02331 * Form the substring of this string from the @a __n1 02332 * characters starting at @a pos. Returns an integer < 0 if 02333 * the substring is ordered before @a __s, 0 if their values 02334 * are equivalent, or > 0 if the substring is ordered after @a 02335 * __s. Determines the effective length rlen of the strings to 02336 * compare as the smallest of the length of the substring and 02337 * the length of a string constructed from @a __s. The 02338 * function then compares the two string by calling 02339 * traits::compare(substring.data(),__s,rlen). If the result of 02340 * the comparison is nonzero returns it, otherwise the shorter 02341 * one is ordered first. 02342 */ 02343 int 02344 compare(size_type __pos, size_type __n1, const _CharT* __s) const; 02345 02346 /** 02347 * @brief Compare substring against a character %array. 02348 * @param __pos Index of first character of substring. 02349 * @param __n1 Number of characters in substring. 02350 * @param __s character %array to compare against. 02351 * @param __n2 Number of characters of s. 02352 * @return Integer < 0, 0, or > 0. 02353 * 02354 * Form the substring of this string from the @a __n1 02355 * characters starting at @a __pos. Form a string from the 02356 * first @a __n2 characters of @a __s. Returns an integer < 0 02357 * if this substring is ordered before the string from @a __s, 02358 * 0 if their values are equivalent, or > 0 if this substring 02359 * is ordered after the string from @a __s. Determines the 02360 * effective length rlen of the strings to compare as the 02361 * smallest of the length of the substring and @a __n2. The 02362 * function then compares the two strings by calling 02363 * traits::compare(substring.data(),s,rlen). If the result of 02364 * the comparison is nonzero returns it, otherwise the shorter 02365 * one is ordered first. 02366 * 02367 * NB: s must have at least n2 characters, '\\0' has 02368 * no special meaning. 02369 */ 02370 int 02371 compare(size_type __pos, size_type __n1, const _CharT* __s, 02372 size_type __n2) const; 02373 }; 02374 02375 // operator+ 02376 /** 02377 * @brief Concatenate two strings. 02378 * @param __lhs First string. 02379 * @param __rhs Last string. 02380 * @return New string with value of @a __lhs followed by @a __rhs. 02381 */ 02382 template<typename _CharT, typename _Traits, typename _Alloc> 02383 basic_string<_CharT, _Traits, _Alloc> 02384 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02385 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02386 { 02387 basic_string<_CharT, _Traits, _Alloc> __str(__lhs); 02388 __str.append(__rhs); 02389 return __str; 02390 } 02391 02392 /** 02393 * @brief Concatenate C string and string. 02394 * @param __lhs First string. 02395 * @param __rhs Last string. 02396 * @return New string with value of @a __lhs followed by @a __rhs. 02397 */ 02398 template<typename _CharT, typename _Traits, typename _Alloc> 02399 basic_string<_CharT,_Traits,_Alloc> 02400 operator+(const _CharT* __lhs, 02401 const basic_string<_CharT,_Traits,_Alloc>& __rhs); 02402 02403 /** 02404 * @brief Concatenate character and string. 02405 * @param __lhs First string. 02406 * @param __rhs Last string. 02407 * @return New string with @a __lhs followed by @a __rhs. 02408 */ 02409 template<typename _CharT, typename _Traits, typename _Alloc> 02410 basic_string<_CharT,_Traits,_Alloc> 02411 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs); 02412 02413 /** 02414 * @brief Concatenate string and C string. 02415 * @param __lhs First string. 02416 * @param __rhs Last string. 02417 * @return New string with @a __lhs followed by @a __rhs. 02418 */ 02419 template<typename _CharT, typename _Traits, typename _Alloc> 02420 inline basic_string<_CharT, _Traits, _Alloc> 02421 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02422 const _CharT* __rhs) 02423 { 02424 basic_string<_CharT, _Traits, _Alloc> __str(__lhs); 02425 __str.append(__rhs); 02426 return __str; 02427 } 02428 02429 /** 02430 * @brief Concatenate string and character. 02431 * @param __lhs First string. 02432 * @param __rhs Last string. 02433 * @return New string with @a __lhs followed by @a __rhs. 02434 */ 02435 template<typename _CharT, typename _Traits, typename _Alloc> 02436 inline basic_string<_CharT, _Traits, _Alloc> 02437 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs) 02438 { 02439 typedef basic_string<_CharT, _Traits, _Alloc> __string_type; 02440 typedef typename __string_type::size_type __size_type; 02441 __string_type __str(__lhs); 02442 __str.append(__size_type(1), __rhs); 02443 return __str; 02444 } 02445 02446 #if __cplusplus >= 201103L 02447 template<typename _CharT, typename _Traits, typename _Alloc> 02448 inline basic_string<_CharT, _Traits, _Alloc> 02449 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, 02450 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02451 { return std::move(__lhs.append(__rhs)); } 02452 02453 template<typename _CharT, typename _Traits, typename _Alloc> 02454 inline basic_string<_CharT, _Traits, _Alloc> 02455 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02456 basic_string<_CharT, _Traits, _Alloc>&& __rhs) 02457 { return std::move(__rhs.insert(0, __lhs)); } 02458 02459 template<typename _CharT, typename _Traits, typename _Alloc> 02460 inline basic_string<_CharT, _Traits, _Alloc> 02461 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, 02462 basic_string<_CharT, _Traits, _Alloc>&& __rhs) 02463 { 02464 const auto __size = __lhs.size() + __rhs.size(); 02465 const bool __cond = (__size > __lhs.capacity() 02466 && __size <= __rhs.capacity()); 02467 return __cond ? std::move(__rhs.insert(0, __lhs)) 02468 : std::move(__lhs.append(__rhs)); 02469 } 02470 02471 template<typename _CharT, typename _Traits, typename _Alloc> 02472 inline basic_string<_CharT, _Traits, _Alloc> 02473 operator+(const _CharT* __lhs, 02474 basic_string<_CharT, _Traits, _Alloc>&& __rhs) 02475 { return std::move(__rhs.insert(0, __lhs)); } 02476 02477 template<typename _CharT, typename _Traits, typename _Alloc> 02478 inline basic_string<_CharT, _Traits, _Alloc> 02479 operator+(_CharT __lhs, 02480 basic_string<_CharT, _Traits, _Alloc>&& __rhs) 02481 { return std::move(__rhs.insert(0, 1, __lhs)); } 02482 02483 template<typename _CharT, typename _Traits, typename _Alloc> 02484 inline basic_string<_CharT, _Traits, _Alloc> 02485 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, 02486 const _CharT* __rhs) 02487 { return std::move(__lhs.append(__rhs)); } 02488 02489 template<typename _CharT, typename _Traits, typename _Alloc> 02490 inline basic_string<_CharT, _Traits, _Alloc> 02491 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, 02492 _CharT __rhs) 02493 { return std::move(__lhs.append(1, __rhs)); } 02494 #endif 02495 02496 // operator == 02497 /** 02498 * @brief Test equivalence of two strings. 02499 * @param __lhs First string. 02500 * @param __rhs Second string. 02501 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise. 02502 */ 02503 template<typename _CharT, typename _Traits, typename _Alloc> 02504 inline bool 02505 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02506 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02507 { return __lhs.compare(__rhs) == 0; } 02508 02509 template<typename _CharT> 02510 inline 02511 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type 02512 operator==(const basic_string<_CharT>& __lhs, 02513 const basic_string<_CharT>& __rhs) 02514 { return (__lhs.size() == __rhs.size() 02515 && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(), 02516 __lhs.size())); } 02517 02518 /** 02519 * @brief Test equivalence of C string and string. 02520 * @param __lhs C string. 02521 * @param __rhs String. 02522 * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise. 02523 */ 02524 template<typename _CharT, typename _Traits, typename _Alloc> 02525 inline bool 02526 operator==(const _CharT* __lhs, 02527 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02528 { return __rhs.compare(__lhs) == 0; } 02529 02530 /** 02531 * @brief Test equivalence of string and C string. 02532 * @param __lhs String. 02533 * @param __rhs C string. 02534 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise. 02535 */ 02536 template<typename _CharT, typename _Traits, typename _Alloc> 02537 inline bool 02538 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02539 const _CharT* __rhs) 02540 { return __lhs.compare(__rhs) == 0; } 02541 02542 // operator != 02543 /** 02544 * @brief Test difference of two strings. 02545 * @param __lhs First string. 02546 * @param __rhs Second string. 02547 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise. 02548 */ 02549 template<typename _CharT, typename _Traits, typename _Alloc> 02550 inline bool 02551 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02552 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02553 { return !(__lhs == __rhs); } 02554 02555 /** 02556 * @brief Test difference of C string and string. 02557 * @param __lhs C string. 02558 * @param __rhs String. 02559 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise. 02560 */ 02561 template<typename _CharT, typename _Traits, typename _Alloc> 02562 inline bool 02563 operator!=(const _CharT* __lhs, 02564 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02565 { return !(__lhs == __rhs); } 02566 02567 /** 02568 * @brief Test difference of string and C string. 02569 * @param __lhs String. 02570 * @param __rhs C string. 02571 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise. 02572 */ 02573 template<typename _CharT, typename _Traits, typename _Alloc> 02574 inline bool 02575 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02576 const _CharT* __rhs) 02577 { return !(__lhs == __rhs); } 02578 02579 // operator < 02580 /** 02581 * @brief Test if string precedes string. 02582 * @param __lhs First string. 02583 * @param __rhs Second string. 02584 * @return True if @a __lhs precedes @a __rhs. False otherwise. 02585 */ 02586 template<typename _CharT, typename _Traits, typename _Alloc> 02587 inline bool 02588 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02589 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02590 { return __lhs.compare(__rhs) < 0; } 02591 02592 /** 02593 * @brief Test if string precedes C string. 02594 * @param __lhs String. 02595 * @param __rhs C string. 02596 * @return True if @a __lhs precedes @a __rhs. False otherwise. 02597 */ 02598 template<typename _CharT, typename _Traits, typename _Alloc> 02599 inline bool 02600 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02601 const _CharT* __rhs) 02602 { return __lhs.compare(__rhs) < 0; } 02603 02604 /** 02605 * @brief Test if C string precedes string. 02606 * @param __lhs C string. 02607 * @param __rhs String. 02608 * @return True if @a __lhs precedes @a __rhs. False otherwise. 02609 */ 02610 template<typename _CharT, typename _Traits, typename _Alloc> 02611 inline bool 02612 operator<(const _CharT* __lhs, 02613 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02614 { return __rhs.compare(__lhs) > 0; } 02615 02616 // operator > 02617 /** 02618 * @brief Test if string follows string. 02619 * @param __lhs First string. 02620 * @param __rhs Second string. 02621 * @return True if @a __lhs follows @a __rhs. False otherwise. 02622 */ 02623 template<typename _CharT, typename _Traits, typename _Alloc> 02624 inline bool 02625 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02626 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02627 { return __lhs.compare(__rhs) > 0; } 02628 02629 /** 02630 * @brief Test if string follows C string. 02631 * @param __lhs String. 02632 * @param __rhs C string. 02633 * @return True if @a __lhs follows @a __rhs. False otherwise. 02634 */ 02635 template<typename _CharT, typename _Traits, typename _Alloc> 02636 inline bool 02637 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02638 const _CharT* __rhs) 02639 { return __lhs.compare(__rhs) > 0; } 02640 02641 /** 02642 * @brief Test if C string follows string. 02643 * @param __lhs C string. 02644 * @param __rhs String. 02645 * @return True if @a __lhs follows @a __rhs. False otherwise. 02646 */ 02647 template<typename _CharT, typename _Traits, typename _Alloc> 02648 inline bool 02649 operator>(const _CharT* __lhs, 02650 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02651 { return __rhs.compare(__lhs) < 0; } 02652 02653 // operator <= 02654 /** 02655 * @brief Test if string doesn't follow string. 02656 * @param __lhs First string. 02657 * @param __rhs Second string. 02658 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise. 02659 */ 02660 template<typename _CharT, typename _Traits, typename _Alloc> 02661 inline bool 02662 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02663 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02664 { return __lhs.compare(__rhs) <= 0; } 02665 02666 /** 02667 * @brief Test if string doesn't follow C string. 02668 * @param __lhs String. 02669 * @param __rhs C string. 02670 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise. 02671 */ 02672 template<typename _CharT, typename _Traits, typename _Alloc> 02673 inline bool 02674 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02675 const _CharT* __rhs) 02676 { return __lhs.compare(__rhs) <= 0; } 02677 02678 /** 02679 * @brief Test if C string doesn't follow string. 02680 * @param __lhs C string. 02681 * @param __rhs String. 02682 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise. 02683 */ 02684 template<typename _CharT, typename _Traits, typename _Alloc> 02685 inline bool 02686 operator<=(const _CharT* __lhs, 02687 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02688 { return __rhs.compare(__lhs) >= 0; } 02689 02690 // operator >= 02691 /** 02692 * @brief Test if string doesn't precede string. 02693 * @param __lhs First string. 02694 * @param __rhs Second string. 02695 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise. 02696 */ 02697 template<typename _CharT, typename _Traits, typename _Alloc> 02698 inline bool 02699 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02700 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02701 { return __lhs.compare(__rhs) >= 0; } 02702 02703 /** 02704 * @brief Test if string doesn't precede C string. 02705 * @param __lhs String. 02706 * @param __rhs C string. 02707 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise. 02708 */ 02709 template<typename _CharT, typename _Traits, typename _Alloc> 02710 inline bool 02711 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 02712 const _CharT* __rhs) 02713 { return __lhs.compare(__rhs) >= 0; } 02714 02715 /** 02716 * @brief Test if C string doesn't precede string. 02717 * @param __lhs C string. 02718 * @param __rhs String. 02719 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise. 02720 */ 02721 template<typename _CharT, typename _Traits, typename _Alloc> 02722 inline bool 02723 operator>=(const _CharT* __lhs, 02724 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 02725 { return __rhs.compare(__lhs) <= 0; } 02726 02727 /** 02728 * @brief Swap contents of two strings. 02729 * @param __lhs First string. 02730 * @param __rhs Second string. 02731 * 02732 * Exchanges the contents of @a __lhs and @a __rhs in constant time. 02733 */ 02734 template<typename _CharT, typename _Traits, typename _Alloc> 02735 inline void 02736 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs, 02737 basic_string<_CharT, _Traits, _Alloc>& __rhs) 02738 { __lhs.swap(__rhs); } 02739 02740 /** 02741 * @brief Read stream into a string. 02742 * @param __is Input stream. 02743 * @param __str Buffer to store into. 02744 * @return Reference to the input stream. 02745 * 02746 * Stores characters from @a __is into @a __str until whitespace is 02747 * found, the end of the stream is encountered, or str.max_size() 02748 * is reached. If is.width() is non-zero, that is the limit on the 02749 * number of characters stored into @a __str. Any previous 02750 * contents of @a __str are erased. 02751 */ 02752 template<typename _CharT, typename _Traits, typename _Alloc> 02753 basic_istream<_CharT, _Traits>& 02754 operator>>(basic_istream<_CharT, _Traits>& __is, 02755 basic_string<_CharT, _Traits, _Alloc>& __str); 02756 02757 template<> 02758 basic_istream<char>& 02759 operator>>(basic_istream<char>& __is, basic_string<char>& __str); 02760 02761 /** 02762 * @brief Write string to a stream. 02763 * @param __os Output stream. 02764 * @param __str String to write out. 02765 * @return Reference to the output stream. 02766 * 02767 * Output characters of @a __str into os following the same rules as for 02768 * writing a C string. 02769 */ 02770 template<typename _CharT, typename _Traits, typename _Alloc> 02771 inline basic_ostream<_CharT, _Traits>& 02772 operator<<(basic_ostream<_CharT, _Traits>& __os, 02773 const basic_string<_CharT, _Traits, _Alloc>& __str) 02774 { 02775 // _GLIBCXX_RESOLVE_LIB_DEFECTS 02776 // 586. string inserter not a formatted function 02777 return __ostream_insert(__os, __str.data(), __str.size()); 02778 } 02779 02780 /** 02781 * @brief Read a line from stream into a string. 02782 * @param __is Input stream. 02783 * @param __str Buffer to store into. 02784 * @param __delim Character marking end of line. 02785 * @return Reference to the input stream. 02786 * 02787 * Stores characters from @a __is into @a __str until @a __delim is 02788 * found, the end of the stream is encountered, or str.max_size() 02789 * is reached. Any previous contents of @a __str are erased. If 02790 * @a __delim is encountered, it is extracted but not stored into 02791 * @a __str. 02792 */ 02793 template<typename _CharT, typename _Traits, typename _Alloc> 02794 basic_istream<_CharT, _Traits>& 02795 getline(basic_istream<_CharT, _Traits>& __is, 02796 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim); 02797 02798 /** 02799 * @brief Read a line from stream into a string. 02800 * @param __is Input stream. 02801 * @param __str Buffer to store into. 02802 * @return Reference to the input stream. 02803 * 02804 * Stores characters from is into @a __str until '\n' is 02805 * found, the end of the stream is encountered, or str.max_size() 02806 * is reached. Any previous contents of @a __str are erased. If 02807 * end of line is encountered, it is extracted but not stored into 02808 * @a __str. 02809 */ 02810 template<typename _CharT, typename _Traits, typename _Alloc> 02811 inline basic_istream<_CharT, _Traits>& 02812 getline(basic_istream<_CharT, _Traits>& __is, 02813 basic_string<_CharT, _Traits, _Alloc>& __str) 02814 { return std::getline(__is, __str, __is.widen('\n')); } 02815 02816 #if __cplusplus >= 201103L 02817 /// Read a line from an rvalue stream into a string. 02818 template<typename _CharT, typename _Traits, typename _Alloc> 02819 inline basic_istream<_CharT, _Traits>& 02820 getline(basic_istream<_CharT, _Traits>&& __is, 02821 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim) 02822 { return std::getline(__is, __str, __delim); } 02823 02824 /// Read a line from an rvalue stream into a string. 02825 template<typename _CharT, typename _Traits, typename _Alloc> 02826 inline basic_istream<_CharT, _Traits>& 02827 getline(basic_istream<_CharT, _Traits>&& __is, 02828 basic_string<_CharT, _Traits, _Alloc>& __str) 02829 { return std::getline(__is, __str); } 02830 #endif 02831 02832 template<> 02833 basic_istream<char>& 02834 getline(basic_istream<char>& __in, basic_string<char>& __str, 02835 char __delim); 02836 02837 #ifdef _GLIBCXX_USE_WCHAR_T 02838 template<> 02839 basic_istream<wchar_t>& 02840 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str, 02841 wchar_t __delim); 02842 #endif 02843 02844 _GLIBCXX_END_NAMESPACE_VERSION 02845 } // namespace 02846 02847 #if ((__cplusplus >= 201103L) && defined(_GLIBCXX_USE_C99) \ 02848 && !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF)) 02849 02850 #include <ext/string_conversions.h> 02851 02852 namespace std _GLIBCXX_VISIBILITY(default) 02853 { 02854 _GLIBCXX_BEGIN_NAMESPACE_VERSION 02855 02856 // 21.4 Numeric Conversions [string.conversions]. 02857 inline int 02858 stoi(const string& __str, size_t* __idx = 0, int __base = 10) 02859 { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(), 02860 __idx, __base); } 02861 02862 inline long 02863 stol(const string& __str, size_t* __idx = 0, int __base = 10) 02864 { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(), 02865 __idx, __base); } 02866 02867 inline unsigned long 02868 stoul(const string& __str, size_t* __idx = 0, int __base = 10) 02869 { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(), 02870 __idx, __base); } 02871 02872 inline long long 02873 stoll(const string& __str, size_t* __idx = 0, int __base = 10) 02874 { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(), 02875 __idx, __base); } 02876 02877 inline unsigned long long 02878 stoull(const string& __str, size_t* __idx = 0, int __base = 10) 02879 { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(), 02880 __idx, __base); } 02881 02882 // NB: strtof vs strtod. 02883 inline float 02884 stof(const string& __str, size_t* __idx = 0) 02885 { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); } 02886 02887 inline double 02888 stod(const string& __str, size_t* __idx = 0) 02889 { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); } 02890 02891 inline long double 02892 stold(const string& __str, size_t* __idx = 0) 02893 { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); } 02894 02895 // NB: (v)snprintf vs sprintf. 02896 02897 // DR 1261. 02898 inline string 02899 to_string(int __val) 02900 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(int), 02901 "%d", __val); } 02902 02903 inline string 02904 to_string(unsigned __val) 02905 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 02906 4 * sizeof(unsigned), 02907 "%u", __val); } 02908 02909 inline string 02910 to_string(long __val) 02911 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(long), 02912 "%ld", __val); } 02913 02914 inline string 02915 to_string(unsigned long __val) 02916 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 02917 4 * sizeof(unsigned long), 02918 "%lu", __val); } 02919 02920 inline string 02921 to_string(long long __val) 02922 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 02923 4 * sizeof(long long), 02924 "%lld", __val); } 02925 02926 inline string 02927 to_string(unsigned long long __val) 02928 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 02929 4 * sizeof(unsigned long long), 02930 "%llu", __val); } 02931 02932 inline string 02933 to_string(float __val) 02934 { 02935 const int __n = 02936 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20; 02937 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n, 02938 "%f", __val); 02939 } 02940 02941 inline string 02942 to_string(double __val) 02943 { 02944 const int __n = 02945 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20; 02946 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n, 02947 "%f", __val); 02948 } 02949 02950 inline string 02951 to_string(long double __val) 02952 { 02953 const int __n = 02954 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20; 02955 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n, 02956 "%Lf", __val); 02957 } 02958 02959 #ifdef _GLIBCXX_USE_WCHAR_T 02960 inline int 02961 stoi(const wstring& __str, size_t* __idx = 0, int __base = 10) 02962 { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(), 02963 __idx, __base); } 02964 02965 inline long 02966 stol(const wstring& __str, size_t* __idx = 0, int __base = 10) 02967 { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(), 02968 __idx, __base); } 02969 02970 inline unsigned long 02971 stoul(const wstring& __str, size_t* __idx = 0, int __base = 10) 02972 { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(), 02973 __idx, __base); } 02974 02975 inline long long 02976 stoll(const wstring& __str, size_t* __idx = 0, int __base = 10) 02977 { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(), 02978 __idx, __base); } 02979 02980 inline unsigned long long 02981 stoull(const wstring& __str, size_t* __idx = 0, int __base = 10) 02982 { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(), 02983 __idx, __base); } 02984 02985 // NB: wcstof vs wcstod. 02986 inline float 02987 stof(const wstring& __str, size_t* __idx = 0) 02988 { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); } 02989 02990 inline double 02991 stod(const wstring& __str, size_t* __idx = 0) 02992 { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); } 02993 02994 inline long double 02995 stold(const wstring& __str, size_t* __idx = 0) 02996 { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); } 02997 02998 // DR 1261. 02999 inline wstring 03000 to_wstring(int __val) 03001 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int), 03002 L"%d", __val); } 03003 03004 inline wstring 03005 to_wstring(unsigned __val) 03006 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 03007 4 * sizeof(unsigned), 03008 L"%u", __val); } 03009 03010 inline wstring 03011 to_wstring(long __val) 03012 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long), 03013 L"%ld", __val); } 03014 03015 inline wstring 03016 to_wstring(unsigned long __val) 03017 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 03018 4 * sizeof(unsigned long), 03019 L"%lu", __val); } 03020 03021 inline wstring 03022 to_wstring(long long __val) 03023 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 03024 4 * sizeof(long long), 03025 L"%lld", __val); } 03026 03027 inline wstring 03028 to_wstring(unsigned long long __val) 03029 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 03030 4 * sizeof(unsigned long long), 03031 L"%llu", __val); } 03032 03033 inline wstring 03034 to_wstring(float __val) 03035 { 03036 const int __n = 03037 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20; 03038 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n, 03039 L"%f", __val); 03040 } 03041 03042 inline wstring 03043 to_wstring(double __val) 03044 { 03045 const int __n = 03046 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20; 03047 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n, 03048 L"%f", __val); 03049 } 03050 03051 inline wstring 03052 to_wstring(long double __val) 03053 { 03054 const int __n = 03055 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20; 03056 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n, 03057 L"%Lf", __val); 03058 } 03059 #endif 03060 03061 _GLIBCXX_END_NAMESPACE_VERSION 03062 } // namespace 03063 03064 #endif /* C++11 && _GLIBCXX_USE_C99 ... */ 03065 03066 #if __cplusplus >= 201103L 03067 03068 #include <bits/functional_hash.h> 03069 03070 namespace std _GLIBCXX_VISIBILITY(default) 03071 { 03072 _GLIBCXX_BEGIN_NAMESPACE_VERSION 03073 03074 // DR 1182. 03075 03076 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X 03077 /// std::hash specialization for string. 03078 template<> 03079 struct hash<string> 03080 : public __hash_base<size_t, string> 03081 { 03082 size_t 03083 operator()(const string& __s) const noexcept 03084 { return std::_Hash_impl::hash(__s.data(), __s.length()); } 03085 }; 03086 03087 template<> 03088 struct __is_fast_hash<hash<string>> : std::false_type 03089 { }; 03090 03091 #ifdef _GLIBCXX_USE_WCHAR_T 03092 /// std::hash specialization for wstring. 03093 template<> 03094 struct hash<wstring> 03095 : public __hash_base<size_t, wstring> 03096 { 03097 size_t 03098 operator()(const wstring& __s) const noexcept 03099 { return std::_Hash_impl::hash(__s.data(), 03100 __s.length() * sizeof(wchar_t)); } 03101 }; 03102 03103 template<> 03104 struct __is_fast_hash<hash<wstring>> : std::false_type 03105 { }; 03106 #endif 03107 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */ 03108 03109 #ifdef _GLIBCXX_USE_C99_STDINT_TR1 03110 /// std::hash specialization for u16string. 03111 template<> 03112 struct hash<u16string> 03113 : public __hash_base<size_t, u16string> 03114 { 03115 size_t 03116 operator()(const u16string& __s) const noexcept 03117 { return std::_Hash_impl::hash(__s.data(), 03118 __s.length() * sizeof(char16_t)); } 03119 }; 03120 03121 template<> 03122 struct __is_fast_hash<hash<u16string>> : std::false_type 03123 { }; 03124 03125 /// std::hash specialization for u32string. 03126 template<> 03127 struct hash<u32string> 03128 : public __hash_base<size_t, u32string> 03129 { 03130 size_t 03131 operator()(const u32string& __s) const noexcept 03132 { return std::_Hash_impl::hash(__s.data(), 03133 __s.length() * sizeof(char32_t)); } 03134 }; 03135 03136 template<> 03137 struct __is_fast_hash<hash<u32string>> : std::false_type 03138 { }; 03139 #endif 03140 03141 #if __cplusplus > 201103L 03142 03143 #define __cpp_lib_string_udls 201304 03144 03145 inline namespace literals 03146 { 03147 inline namespace string_literals 03148 { 03149 03150 inline basic_string<char> 03151 operator""s(const char* __str, size_t __len) 03152 { return basic_string<char>{__str, __len}; } 03153 03154 #ifdef _GLIBCXX_USE_WCHAR_T 03155 inline basic_string<wchar_t> 03156 operator""s(const wchar_t* __str, size_t __len) 03157 { return basic_string<wchar_t>{__str, __len}; } 03158 #endif 03159 03160 #ifdef _GLIBCXX_USE_C99_STDINT_TR1 03161 inline basic_string<char16_t> 03162 operator""s(const char16_t* __str, size_t __len) 03163 { return basic_string<char16_t>{__str, __len}; } 03164 03165 inline basic_string<char32_t> 03166 operator""s(const char32_t* __str, size_t __len) 03167 { return basic_string<char32_t>{__str, __len}; } 03168 #endif 03169 03170 } // inline namespace string_literals 03171 } // inline namespace literals 03172 03173 #endif // __cplusplus > 201103L 03174 03175 _GLIBCXX_END_NAMESPACE_VERSION 03176 } // namespace std 03177 03178 #endif // C++11 03179 03180 #endif /* _BASIC_STRING_H */