|
libstdc++
|
00001 // Components for manipulating sequences of characters -*- C++ -*- 00002 00003 // Copyright (C) 1997-2015 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 <ext/alloc_traits.h> 00041 #include <debug/debug.h> 00042 #if __cplusplus >= 201103L 00043 #include <initializer_list> 00044 #endif 00045 00046 namespace std _GLIBCXX_VISIBILITY(default) 00047 { 00048 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00049 00050 #if _GLIBCXX_USE_CXX11_ABI 00051 _GLIBCXX_BEGIN_NAMESPACE_CXX11 00052 /** 00053 * @class basic_string basic_string.h <string> 00054 * @brief Managing sequences of characters and character-like objects. 00055 * 00056 * @ingroup strings 00057 * @ingroup sequences 00058 * 00059 * @tparam _CharT Type of character 00060 * @tparam _Traits Traits for character type, defaults to 00061 * char_traits<_CharT>. 00062 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>. 00063 * 00064 * Meets the requirements of a <a href="tables.html#65">container</a>, a 00065 * <a href="tables.html#66">reversible container</a>, and a 00066 * <a href="tables.html#67">sequence</a>. Of the 00067 * <a href="tables.html#68">optional sequence requirements</a>, only 00068 * @c push_back, @c at, and @c %array access are supported. 00069 */ 00070 template<typename _CharT, typename _Traits, typename _Alloc> 00071 class basic_string 00072 { 00073 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template 00074 rebind<_CharT>::other _Char_alloc_type; 00075 typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits; 00076 00077 // Types: 00078 public: 00079 typedef _Traits traits_type; 00080 typedef typename _Traits::char_type value_type; 00081 typedef _Char_alloc_type allocator_type; 00082 typedef typename _Alloc_traits::size_type size_type; 00083 typedef typename _Alloc_traits::difference_type difference_type; 00084 typedef typename _Alloc_traits::reference reference; 00085 typedef typename _Alloc_traits::const_reference const_reference; 00086 typedef typename _Alloc_traits::pointer pointer; 00087 typedef typename _Alloc_traits::const_pointer const_pointer; 00088 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator; 00089 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string> 00090 const_iterator; 00091 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00092 typedef std::reverse_iterator<iterator> reverse_iterator; 00093 00094 /// Value returned by various member functions when they fail. 00095 static const size_type npos = static_cast<size_type>(-1); 00096 00097 private: 00098 // type used for positions in insert, erase etc. 00099 #if __cplusplus < 201103L 00100 typedef iterator __const_iterator; 00101 #else 00102 typedef const_iterator __const_iterator; 00103 #endif 00104 00105 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html 00106 struct _Alloc_hider : allocator_type // TODO check __is_final 00107 { 00108 _Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc()) 00109 : allocator_type(__a), _M_p(__dat) { } 00110 00111 pointer _M_p; // The actual data. 00112 }; 00113 00114 _Alloc_hider _M_dataplus; 00115 size_type _M_string_length; 00116 00117 enum { _S_local_capacity = 15 / sizeof(_CharT) }; 00118 00119 union 00120 { 00121 _CharT _M_local_buf[_S_local_capacity + 1]; 00122 size_type _M_allocated_capacity; 00123 }; 00124 00125 void 00126 _M_data(pointer __p) 00127 { _M_dataplus._M_p = __p; } 00128 00129 void 00130 _M_length(size_type __length) 00131 { _M_string_length = __length; } 00132 00133 pointer 00134 _M_data() const 00135 { return _M_dataplus._M_p; } 00136 00137 pointer 00138 _M_local_data() 00139 { 00140 #if __cplusplus >= 201103L 00141 return std::pointer_traits<pointer>::pointer_to(*_M_local_buf); 00142 #else 00143 return pointer(_M_local_buf); 00144 #endif 00145 } 00146 00147 const_pointer 00148 _M_local_data() const 00149 { 00150 #if __cplusplus >= 201103L 00151 return std::pointer_traits<const_pointer>::pointer_to(*_M_local_buf); 00152 #else 00153 return const_pointer(_M_local_buf); 00154 #endif 00155 } 00156 00157 void 00158 _M_capacity(size_type __capacity) 00159 { _M_allocated_capacity = __capacity; } 00160 00161 void 00162 _M_set_length(size_type __n) 00163 { 00164 _M_length(__n); 00165 traits_type::assign(_M_data()[__n], _CharT()); 00166 } 00167 00168 bool 00169 _M_is_local() const 00170 { return _M_data() == _M_local_data(); } 00171 00172 // Create & Destroy 00173 pointer 00174 _M_create(size_type&, size_type); 00175 00176 void 00177 _M_dispose() 00178 { 00179 if (!_M_is_local()) 00180 _M_destroy(_M_allocated_capacity); 00181 } 00182 00183 void 00184 _M_destroy(size_type __size) throw() 00185 { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size + 1); } 00186 00187 // _M_construct_aux is used to implement the 21.3.1 para 15 which 00188 // requires special behaviour if _InIterator is an integral type 00189 template<typename _InIterator> 00190 void 00191 _M_construct_aux(_InIterator __beg, _InIterator __end, 00192 std::__false_type) 00193 { 00194 typedef typename iterator_traits<_InIterator>::iterator_category _Tag; 00195 _M_construct(__beg, __end, _Tag()); 00196 } 00197 00198 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00199 // 438. Ambiguity in the "do the right thing" clause 00200 template<typename _Integer> 00201 void 00202 _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type) 00203 { _M_construct_aux_2(static_cast<size_type>(__beg), __end); } 00204 00205 void 00206 _M_construct_aux_2(size_type __req, _CharT __c) 00207 { _M_construct(__req, __c); } 00208 00209 template<typename _InIterator> 00210 void 00211 _M_construct(_InIterator __beg, _InIterator __end) 00212 { 00213 typedef typename std::__is_integer<_InIterator>::__type _Integral; 00214 _M_construct_aux(__beg, __end, _Integral()); 00215 } 00216 00217 // For Input Iterators, used in istreambuf_iterators, etc. 00218 template<typename _InIterator> 00219 void 00220 _M_construct(_InIterator __beg, _InIterator __end, 00221 std::input_iterator_tag); 00222 00223 // For forward_iterators up to random_access_iterators, used for 00224 // string::iterator, _CharT*, etc. 00225 template<typename _FwdIterator> 00226 void 00227 _M_construct(_FwdIterator __beg, _FwdIterator __end, 00228 std::forward_iterator_tag); 00229 00230 void 00231 _M_construct(size_type __req, _CharT __c); 00232 00233 allocator_type& 00234 _M_get_allocator() 00235 { return _M_dataplus; } 00236 00237 const allocator_type& 00238 _M_get_allocator() const 00239 { return _M_dataplus; } 00240 00241 private: 00242 00243 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST 00244 // The explicit instantiations in misc-inst.cc require this due to 00245 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063 00246 template<typename _Tp, bool _Requires = 00247 !__are_same<_Tp, _CharT*>::__value 00248 && !__are_same<_Tp, const _CharT*>::__value 00249 && !__are_same<_Tp, iterator>::__value 00250 && !__are_same<_Tp, const_iterator>::__value> 00251 struct __enable_if_not_native_iterator 00252 { typedef basic_string& __type; }; 00253 template<typename _Tp> 00254 struct __enable_if_not_native_iterator<_Tp, false> { }; 00255 #endif 00256 00257 size_type 00258 _M_check(size_type __pos, const char* __s) const 00259 { 00260 if (__pos > this->size()) 00261 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > " 00262 "this->size() (which is %zu)"), 00263 __s, __pos, this->size()); 00264 return __pos; 00265 } 00266 00267 void 00268 _M_check_length(size_type __n1, size_type __n2, const char* __s) const 00269 { 00270 if (this->max_size() - (this->size() - __n1) < __n2) 00271 __throw_length_error(__N(__s)); 00272 } 00273 00274 00275 // NB: _M_limit doesn't check for a bad __pos value. 00276 size_type 00277 _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT 00278 { 00279 const bool __testoff = __off < this->size() - __pos; 00280 return __testoff ? __off : this->size() - __pos; 00281 } 00282 00283 // True if _Rep and source do not overlap. 00284 bool 00285 _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT 00286 { 00287 return (less<const _CharT*>()(__s, _M_data()) 00288 || less<const _CharT*>()(_M_data() + this->size(), __s)); 00289 } 00290 00291 // When __n = 1 way faster than the general multichar 00292 // traits_type::copy/move/assign. 00293 static void 00294 _S_copy(_CharT* __d, const _CharT* __s, size_type __n) 00295 { 00296 if (__n == 1) 00297 traits_type::assign(*__d, *__s); 00298 else 00299 traits_type::copy(__d, __s, __n); 00300 } 00301 00302 static void 00303 _S_move(_CharT* __d, const _CharT* __s, size_type __n) 00304 { 00305 if (__n == 1) 00306 traits_type::assign(*__d, *__s); 00307 else 00308 traits_type::move(__d, __s, __n); 00309 } 00310 00311 static void 00312 _S_assign(_CharT* __d, size_type __n, _CharT __c) 00313 { 00314 if (__n == 1) 00315 traits_type::assign(*__d, __c); 00316 else 00317 traits_type::assign(__d, __n, __c); 00318 } 00319 00320 // _S_copy_chars is a separate template to permit specialization 00321 // to optimize for the common case of pointers as iterators. 00322 template<class _Iterator> 00323 static void 00324 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2) 00325 { 00326 for (; __k1 != __k2; ++__k1, ++__p) 00327 traits_type::assign(*__p, *__k1); // These types are off. 00328 } 00329 00330 static void 00331 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT 00332 { _S_copy_chars(__p, __k1.base(), __k2.base()); } 00333 00334 static void 00335 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2) 00336 _GLIBCXX_NOEXCEPT 00337 { _S_copy_chars(__p, __k1.base(), __k2.base()); } 00338 00339 static void 00340 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT 00341 { _S_copy(__p, __k1, __k2 - __k1); } 00342 00343 static void 00344 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2) 00345 _GLIBCXX_NOEXCEPT 00346 { _S_copy(__p, __k1, __k2 - __k1); } 00347 00348 static int 00349 _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT 00350 { 00351 const difference_type __d = difference_type(__n1 - __n2); 00352 00353 if (__d > __gnu_cxx::__numeric_traits<int>::__max) 00354 return __gnu_cxx::__numeric_traits<int>::__max; 00355 else if (__d < __gnu_cxx::__numeric_traits<int>::__min) 00356 return __gnu_cxx::__numeric_traits<int>::__min; 00357 else 00358 return int(__d); 00359 } 00360 00361 void 00362 _M_assign(const basic_string& __rcs); 00363 00364 void 00365 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s, 00366 size_type __len2); 00367 00368 void 00369 _M_erase(size_type __pos, size_type __n); 00370 00371 public: 00372 // Construct/copy/destroy: 00373 // NB: We overload ctors in some cases instead of using default 00374 // arguments, per 17.4.4.4 para. 2 item 2. 00375 00376 /** 00377 * @brief Default constructor creates an empty string. 00378 */ 00379 basic_string() 00380 #if __cplusplus >= 201103L 00381 noexcept(is_nothrow_default_constructible<_Alloc>::value) 00382 #endif 00383 : _M_dataplus(_M_local_data()) 00384 { _M_set_length(0); } 00385 00386 /** 00387 * @brief Construct an empty string using allocator @a a. 00388 */ 00389 explicit 00390 basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT 00391 : _M_dataplus(_M_local_data(), __a) 00392 { _M_set_length(0); } 00393 00394 /** 00395 * @brief Construct string with copy of value of @a __str. 00396 * @param __str Source string. 00397 */ 00398 basic_string(const basic_string& __str) 00399 : _M_dataplus(_M_local_data(), 00400 _Alloc_traits::_S_select_on_copy(__str._M_get_allocator())) 00401 { _M_construct(__str._M_data(), __str._M_data() + __str.length()); } 00402 00403 /** 00404 * @brief Construct string as copy of a substring. 00405 * @param __str Source string. 00406 * @param __pos Index of first character to copy from. 00407 * @param __n Number of characters to copy (default remainder). 00408 */ 00409 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00410 // 2402. [this constructor] shouldn't use Allocator() 00411 basic_string(const basic_string& __str, size_type __pos, 00412 size_type __n = npos) 00413 : _M_dataplus(_M_local_data()) 00414 { 00415 const _CharT* __start = __str._M_data() 00416 + __str._M_check(__pos, "basic_string::basic_string"); 00417 _M_construct(__start, __start + __str._M_limit(__pos, __n)); 00418 } 00419 00420 /** 00421 * @brief Construct string as copy of a substring. 00422 * @param __str Source string. 00423 * @param __pos Index of first character to copy from. 00424 * @param __n Number of characters to copy (default remainder). 00425 * @param __a Allocator to use. 00426 */ 00427 basic_string(const basic_string& __str, size_type __pos, 00428 size_type __n, const _Alloc& __a) 00429 : _M_dataplus(_M_local_data(), __a) 00430 { 00431 const _CharT* __start 00432 = __str._M_data() + __str._M_check(__pos, "string::string"); 00433 _M_construct(__start, __start + __str._M_limit(__pos, __n)); 00434 } 00435 00436 /** 00437 * @brief Construct string initialized by a character %array. 00438 * @param __s Source character %array. 00439 * @param __n Number of characters to copy. 00440 * @param __a Allocator to use (default is default allocator). 00441 * 00442 * NB: @a __s must have at least @a __n characters, '\\0' 00443 * has no special meaning. 00444 */ 00445 basic_string(const _CharT* __s, size_type __n, 00446 const _Alloc& __a = _Alloc()) 00447 : _M_dataplus(_M_local_data(), __a) 00448 { _M_construct(__s, __s + __n); } 00449 00450 /** 00451 * @brief Construct string as copy of a C string. 00452 * @param __s Source C string. 00453 * @param __a Allocator to use (default is default allocator). 00454 */ 00455 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc()) 00456 : _M_dataplus(_M_local_data(), __a) 00457 { _M_construct(__s, __s ? __s + traits_type::length(__s) : __s+npos); } 00458 00459 /** 00460 * @brief Construct string as multiple characters. 00461 * @param __n Number of characters. 00462 * @param __c Character to use. 00463 * @param __a Allocator to use (default is default allocator). 00464 */ 00465 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc()) 00466 : _M_dataplus(_M_local_data(), __a) 00467 { _M_construct(__n, __c); } 00468 00469 #if __cplusplus >= 201103L 00470 /** 00471 * @brief Move construct string. 00472 * @param __str Source string. 00473 * 00474 * The newly-created string contains the exact contents of @a __str. 00475 * @a __str is a valid, but unspecified string. 00476 **/ 00477 basic_string(basic_string&& __str) noexcept 00478 : _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator())) 00479 { 00480 if (__str._M_is_local()) 00481 { 00482 traits_type::copy(_M_local_buf, __str._M_local_buf, 00483 _S_local_capacity + 1); 00484 } 00485 else 00486 { 00487 _M_data(__str._M_data()); 00488 _M_capacity(__str._M_allocated_capacity); 00489 } 00490 00491 // Must use _M_length() here not _M_set_length() because 00492 // basic_stringbuf relies on writing into unallocated capacity so 00493 // we mess up the contents if we put a '\0' in the string. 00494 _M_length(__str.length()); 00495 __str._M_data(__str._M_local_data()); 00496 __str._M_set_length(0); 00497 } 00498 00499 /** 00500 * @brief Construct string from an initializer %list. 00501 * @param __l std::initializer_list of characters. 00502 * @param __a Allocator to use (default is default allocator). 00503 */ 00504 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc()) 00505 : _M_dataplus(_M_local_data(), __a) 00506 { _M_construct(__l.begin(), __l.end()); } 00507 00508 basic_string(const basic_string& __str, const _Alloc& __a) 00509 : _M_dataplus(_M_local_data(), __a) 00510 { _M_construct(__str.begin(), __str.end()); } 00511 00512 basic_string(basic_string&& __str, const _Alloc& __a) 00513 noexcept(_Alloc_traits::_S_always_equal()) 00514 : _M_dataplus(_M_local_data(), __a) 00515 { 00516 if (__str._M_is_local()) 00517 { 00518 traits_type::copy(_M_local_buf, __str._M_local_buf, 00519 _S_local_capacity + 1); 00520 _M_length(__str.length()); 00521 __str._M_set_length(0); 00522 } 00523 else if (_Alloc_traits::_S_always_equal() 00524 || __str.get_allocator() == __a) 00525 { 00526 _M_data(__str._M_data()); 00527 _M_length(__str.length()); 00528 _M_capacity(__str._M_allocated_capacity); 00529 __str._M_data(__str._M_local_buf); 00530 __str._M_set_length(0); 00531 } 00532 else 00533 _M_construct(__str.begin(), __str.end()); 00534 } 00535 00536 #endif // C++11 00537 00538 /** 00539 * @brief Construct string as copy of a range. 00540 * @param __beg Start of range. 00541 * @param __end End of range. 00542 * @param __a Allocator to use (default is default allocator). 00543 */ 00544 #if __cplusplus >= 201103L 00545 template<typename _InputIterator, 00546 typename = std::_RequireInputIter<_InputIterator>> 00547 #else 00548 template<typename _InputIterator> 00549 #endif 00550 basic_string(_InputIterator __beg, _InputIterator __end, 00551 const _Alloc& __a = _Alloc()) 00552 : _M_dataplus(_M_local_data(), __a) 00553 { _M_construct(__beg, __end); } 00554 00555 /** 00556 * @brief Destroy the string instance. 00557 */ 00558 ~basic_string() 00559 { _M_dispose(); } 00560 00561 /** 00562 * @brief Assign the value of @a str to this string. 00563 * @param __str Source string. 00564 */ 00565 basic_string& 00566 operator=(const basic_string& __str) 00567 { 00568 #if __cplusplus >= 201103L 00569 if (_Alloc_traits::_S_propagate_on_copy_assign()) 00570 { 00571 if (!_Alloc_traits::_S_always_equal() && !_M_is_local() 00572 && _M_get_allocator() != __str._M_get_allocator()) 00573 { 00574 // Propagating allocator cannot free existing storage so must 00575 // deallocate it before replacing current allocator. 00576 if (__str.size() <= _S_local_capacity) 00577 { 00578 _M_destroy(_M_allocated_capacity); 00579 _M_data(_M_local_data()); 00580 _M_set_length(0); 00581 } 00582 else 00583 { 00584 const auto __len = __str.size(); 00585 auto __alloc = __str._M_get_allocator(); 00586 // If this allocation throws there are no effects: 00587 auto __ptr = _Alloc_traits::allocate(__alloc, __len + 1); 00588 _M_destroy(_M_allocated_capacity); 00589 _M_data(__ptr); 00590 _M_capacity(__len); 00591 _M_set_length(__len); 00592 } 00593 } 00594 std::__alloc_on_copy(_M_get_allocator(), __str._M_get_allocator()); 00595 } 00596 #endif 00597 return this->assign(__str); 00598 } 00599 00600 /** 00601 * @brief Copy contents of @a s into this string. 00602 * @param __s Source null-terminated string. 00603 */ 00604 basic_string& 00605 operator=(const _CharT* __s) 00606 { return this->assign(__s); } 00607 00608 /** 00609 * @brief Set value to string of length 1. 00610 * @param __c Source character. 00611 * 00612 * Assigning to a character makes this string length 1 and 00613 * (*this)[0] == @a c. 00614 */ 00615 basic_string& 00616 operator=(_CharT __c) 00617 { 00618 this->assign(1, __c); 00619 return *this; 00620 } 00621 00622 #if __cplusplus >= 201103L 00623 /** 00624 * @brief Move assign the value of @a str to this string. 00625 * @param __str Source string. 00626 * 00627 * The contents of @a str are moved into this string (without copying). 00628 * @a str is a valid, but unspecified string. 00629 **/ 00630 // PR 58265, this should be noexcept. 00631 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00632 // 2063. Contradictory requirements for string move assignment 00633 basic_string& 00634 operator=(basic_string&& __str) 00635 noexcept(_Alloc_traits::_S_nothrow_move()) 00636 { 00637 if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign() 00638 && !_Alloc_traits::_S_always_equal() 00639 && _M_get_allocator() != __str._M_get_allocator()) 00640 { 00641 // Destroy existing storage before replacing allocator. 00642 _M_destroy(_M_allocated_capacity); 00643 _M_data(_M_local_data()); 00644 _M_set_length(0); 00645 } 00646 // Replace allocator if POCMA is true. 00647 std::__alloc_on_move(_M_get_allocator(), __str._M_get_allocator()); 00648 00649 if (!__str._M_is_local() 00650 && (_Alloc_traits::_S_propagate_on_move_assign() 00651 || _Alloc_traits::_S_always_equal())) 00652 { 00653 pointer __data = nullptr; 00654 size_type __capacity; 00655 if (!_M_is_local()) 00656 { 00657 if (_Alloc_traits::_S_always_equal()) 00658 { 00659 __data = _M_data(); 00660 __capacity = _M_allocated_capacity; 00661 } 00662 else 00663 _M_destroy(_M_allocated_capacity); 00664 } 00665 00666 _M_data(__str._M_data()); 00667 _M_length(__str.length()); 00668 _M_capacity(__str._M_allocated_capacity); 00669 if (__data) 00670 { 00671 __str._M_data(__data); 00672 __str._M_capacity(__capacity); 00673 } 00674 else 00675 __str._M_data(__str._M_local_buf); 00676 } 00677 else 00678 assign(__str); 00679 __str.clear(); 00680 return *this; 00681 } 00682 00683 /** 00684 * @brief Set value to string constructed from initializer %list. 00685 * @param __l std::initializer_list. 00686 */ 00687 basic_string& 00688 operator=(initializer_list<_CharT> __l) 00689 { 00690 this->assign(__l.begin(), __l.size()); 00691 return *this; 00692 } 00693 #endif // C++11 00694 00695 // Iterators: 00696 /** 00697 * Returns a read/write iterator that points to the first character in 00698 * the %string. 00699 */ 00700 iterator 00701 begin() _GLIBCXX_NOEXCEPT 00702 { return iterator(_M_data()); } 00703 00704 /** 00705 * Returns a read-only (constant) iterator that points to the first 00706 * character in the %string. 00707 */ 00708 const_iterator 00709 begin() const _GLIBCXX_NOEXCEPT 00710 { return const_iterator(_M_data()); } 00711 00712 /** 00713 * Returns a read/write iterator that points one past the last 00714 * character in the %string. 00715 */ 00716 iterator 00717 end() _GLIBCXX_NOEXCEPT 00718 { return iterator(_M_data() + this->size()); } 00719 00720 /** 00721 * Returns a read-only (constant) iterator that points one past the 00722 * last character in the %string. 00723 */ 00724 const_iterator 00725 end() const _GLIBCXX_NOEXCEPT 00726 { return const_iterator(_M_data() + this->size()); } 00727 00728 /** 00729 * Returns a read/write reverse iterator that points to the last 00730 * character in the %string. Iteration is done in reverse element 00731 * order. 00732 */ 00733 reverse_iterator 00734 rbegin() _GLIBCXX_NOEXCEPT 00735 { return reverse_iterator(this->end()); } 00736 00737 /** 00738 * Returns a read-only (constant) reverse iterator that points 00739 * to the last character in the %string. Iteration is done in 00740 * reverse element order. 00741 */ 00742 const_reverse_iterator 00743 rbegin() const _GLIBCXX_NOEXCEPT 00744 { return const_reverse_iterator(this->end()); } 00745 00746 /** 00747 * Returns a read/write reverse iterator that points to one before the 00748 * first character in the %string. Iteration is done in reverse 00749 * element order. 00750 */ 00751 reverse_iterator 00752 rend() _GLIBCXX_NOEXCEPT 00753 { return reverse_iterator(this->begin()); } 00754 00755 /** 00756 * Returns a read-only (constant) reverse iterator that points 00757 * to one before the first character in the %string. Iteration 00758 * is done in reverse element order. 00759 */ 00760 const_reverse_iterator 00761 rend() const _GLIBCXX_NOEXCEPT 00762 { return const_reverse_iterator(this->begin()); } 00763 00764 #if __cplusplus >= 201103L 00765 /** 00766 * Returns a read-only (constant) iterator that points to the first 00767 * character in the %string. 00768 */ 00769 const_iterator 00770 cbegin() const noexcept 00771 { return const_iterator(this->_M_data()); } 00772 00773 /** 00774 * Returns a read-only (constant) iterator that points one past the 00775 * last character in the %string. 00776 */ 00777 const_iterator 00778 cend() const noexcept 00779 { return const_iterator(this->_M_data() + this->size()); } 00780 00781 /** 00782 * Returns a read-only (constant) reverse iterator that points 00783 * to the last character in the %string. Iteration is done in 00784 * reverse element order. 00785 */ 00786 const_reverse_iterator 00787 crbegin() const noexcept 00788 { return const_reverse_iterator(this->end()); } 00789 00790 /** 00791 * Returns a read-only (constant) reverse iterator that points 00792 * to one before the first character in the %string. Iteration 00793 * is done in reverse element order. 00794 */ 00795 const_reverse_iterator 00796 crend() const noexcept 00797 { return const_reverse_iterator(this->begin()); } 00798 #endif 00799 00800 public: 00801 // Capacity: 00802 /// Returns the number of characters in the string, not including any 00803 /// null-termination. 00804 size_type 00805 size() const _GLIBCXX_NOEXCEPT 00806 { return _M_string_length; } 00807 00808 /// Returns the number of characters in the string, not including any 00809 /// null-termination. 00810 size_type 00811 length() const _GLIBCXX_NOEXCEPT 00812 { return _M_string_length; } 00813 00814 /// Returns the size() of the largest possible %string. 00815 size_type 00816 max_size() const _GLIBCXX_NOEXCEPT 00817 { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; } 00818 00819 /** 00820 * @brief Resizes the %string to the specified number of characters. 00821 * @param __n Number of characters the %string should contain. 00822 * @param __c Character to fill any new elements. 00823 * 00824 * This function will %resize the %string to the specified 00825 * number of characters. If the number is smaller than the 00826 * %string's current size the %string is truncated, otherwise 00827 * the %string is extended and new elements are %set to @a __c. 00828 */ 00829 void 00830 resize(size_type __n, _CharT __c); 00831 00832 /** 00833 * @brief Resizes the %string to the specified number of characters. 00834 * @param __n Number of characters the %string should contain. 00835 * 00836 * This function will resize the %string to the specified length. If 00837 * the new size is smaller than the %string's current size the %string 00838 * is truncated, otherwise the %string is extended and new characters 00839 * are default-constructed. For basic types such as char, this means 00840 * setting them to 0. 00841 */ 00842 void 00843 resize(size_type __n) 00844 { this->resize(__n, _CharT()); } 00845 00846 #if __cplusplus >= 201103L 00847 /// A non-binding request to reduce capacity() to size(). 00848 void 00849 shrink_to_fit() noexcept 00850 { 00851 #if __cpp_exceptions 00852 if (capacity() > size()) 00853 { 00854 try 00855 { reserve(0); } 00856 catch(...) 00857 { } 00858 } 00859 #endif 00860 } 00861 #endif 00862 00863 /** 00864 * Returns the total number of characters that the %string can hold 00865 * before needing to allocate more memory. 00866 */ 00867 size_type 00868 capacity() const _GLIBCXX_NOEXCEPT 00869 { 00870 return _M_is_local() ? size_type(_S_local_capacity) 00871 : _M_allocated_capacity; 00872 } 00873 00874 /** 00875 * @brief Attempt to preallocate enough memory for specified number of 00876 * characters. 00877 * @param __res_arg Number of characters required. 00878 * @throw std::length_error If @a __res_arg exceeds @c max_size(). 00879 * 00880 * This function attempts to reserve enough memory for the 00881 * %string to hold the specified number of characters. If the 00882 * number requested is more than max_size(), length_error is 00883 * thrown. 00884 * 00885 * The advantage of this function is that if optimal code is a 00886 * necessity and the user can determine the string length that will be 00887 * required, the user can reserve the memory in %advance, and thus 00888 * prevent a possible reallocation of memory and copying of %string 00889 * data. 00890 */ 00891 void 00892 reserve(size_type __res_arg = 0); 00893 00894 /** 00895 * Erases the string, making it empty. 00896 */ 00897 void 00898 clear() _GLIBCXX_NOEXCEPT 00899 { _M_set_length(0); } 00900 00901 /** 00902 * Returns true if the %string is empty. Equivalent to 00903 * <code>*this == ""</code>. 00904 */ 00905 bool 00906 empty() const _GLIBCXX_NOEXCEPT 00907 { return this->size() == 0; } 00908 00909 // Element access: 00910 /** 00911 * @brief Subscript access to the data contained in the %string. 00912 * @param __pos The index of the character to access. 00913 * @return Read-only (constant) reference to the character. 00914 * 00915 * This operator allows for easy, array-style, data access. 00916 * Note that data access with this operator is unchecked and 00917 * out_of_range lookups are not defined. (For checked lookups 00918 * see at().) 00919 */ 00920 const_reference 00921 operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT 00922 { 00923 _GLIBCXX_DEBUG_ASSERT(__pos <= size()); 00924 return _M_data()[__pos]; 00925 } 00926 00927 /** 00928 * @brief Subscript access to the data contained in the %string. 00929 * @param __pos The index of the character to access. 00930 * @return Read/write reference to the character. 00931 * 00932 * This operator allows for easy, array-style, data access. 00933 * Note that data access with this operator is unchecked and 00934 * out_of_range lookups are not defined. (For checked lookups 00935 * see at().) 00936 */ 00937 reference 00938 operator[](size_type __pos) 00939 { 00940 // Allow pos == size() both in C++98 mode, as v3 extension, 00941 // and in C++11 mode. 00942 _GLIBCXX_DEBUG_ASSERT(__pos <= size()); 00943 // In pedantic mode be strict in C++98 mode. 00944 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size()); 00945 return _M_data()[__pos]; 00946 } 00947 00948 /** 00949 * @brief Provides access to the data contained in the %string. 00950 * @param __n The index of the character to access. 00951 * @return Read-only (const) reference to the character. 00952 * @throw std::out_of_range If @a n is an invalid index. 00953 * 00954 * This function provides for safer data access. The parameter is 00955 * first checked that it is in the range of the string. The function 00956 * throws out_of_range if the check fails. 00957 */ 00958 const_reference 00959 at(size_type __n) const 00960 { 00961 if (__n >= this->size()) 00962 __throw_out_of_range_fmt(__N("basic_string::at: __n " 00963 "(which is %zu) >= this->size() " 00964 "(which is %zu)"), 00965 __n, this->size()); 00966 return _M_data()[__n]; 00967 } 00968 00969 /** 00970 * @brief Provides access to the data contained in the %string. 00971 * @param __n The index of the character to access. 00972 * @return Read/write reference to the character. 00973 * @throw std::out_of_range If @a n is an invalid index. 00974 * 00975 * This function provides for safer data access. The parameter is 00976 * first checked that it is in the range of the string. The function 00977 * throws out_of_range if the check fails. 00978 */ 00979 reference 00980 at(size_type __n) 00981 { 00982 if (__n >= size()) 00983 __throw_out_of_range_fmt(__N("basic_string::at: __n " 00984 "(which is %zu) >= this->size() " 00985 "(which is %zu)"), 00986 __n, this->size()); 00987 return _M_data()[__n]; 00988 } 00989 00990 #if __cplusplus >= 201103L 00991 /** 00992 * Returns a read/write reference to the data at the first 00993 * element of the %string. 00994 */ 00995 reference 00996 front() noexcept 00997 { return operator[](0); } 00998 00999 /** 01000 * Returns a read-only (constant) reference to the data at the first 01001 * element of the %string. 01002 */ 01003 const_reference 01004 front() const noexcept 01005 { return operator[](0); } 01006 01007 /** 01008 * Returns a read/write reference to the data at the last 01009 * element of the %string. 01010 */ 01011 reference 01012 back() noexcept 01013 { return operator[](this->size() - 1); } 01014 01015 /** 01016 * Returns a read-only (constant) reference to the data at the 01017 * last element of the %string. 01018 */ 01019 const_reference 01020 back() const noexcept 01021 { return operator[](this->size() - 1); } 01022 #endif 01023 01024 // Modifiers: 01025 /** 01026 * @brief Append a string to this string. 01027 * @param __str The string to append. 01028 * @return Reference to this string. 01029 */ 01030 basic_string& 01031 operator+=(const basic_string& __str) 01032 { return this->append(__str); } 01033 01034 /** 01035 * @brief Append a C string. 01036 * @param __s The C string to append. 01037 * @return Reference to this string. 01038 */ 01039 basic_string& 01040 operator+=(const _CharT* __s) 01041 { return this->append(__s); } 01042 01043 /** 01044 * @brief Append a character. 01045 * @param __c The character to append. 01046 * @return Reference to this string. 01047 */ 01048 basic_string& 01049 operator+=(_CharT __c) 01050 { 01051 this->push_back(__c); 01052 return *this; 01053 } 01054 01055 #if __cplusplus >= 201103L 01056 /** 01057 * @brief Append an initializer_list of characters. 01058 * @param __l The initializer_list of characters to be appended. 01059 * @return Reference to this string. 01060 */ 01061 basic_string& 01062 operator+=(initializer_list<_CharT> __l) 01063 { return this->append(__l.begin(), __l.size()); } 01064 #endif // C++11 01065 01066 /** 01067 * @brief Append a string to this string. 01068 * @param __str The string to append. 01069 * @return Reference to this string. 01070 */ 01071 basic_string& 01072 append(const basic_string& __str) 01073 { return _M_append(__str._M_data(), __str.size()); } 01074 01075 /** 01076 * @brief Append a substring. 01077 * @param __str The string to append. 01078 * @param __pos Index of the first character of str to append. 01079 * @param __n The number of characters to append. 01080 * @return Reference to this string. 01081 * @throw std::out_of_range if @a __pos is not a valid index. 01082 * 01083 * This function appends @a __n characters from @a __str 01084 * starting at @a __pos to this string. If @a __n is is larger 01085 * than the number of available characters in @a __str, the 01086 * remainder of @a __str is appended. 01087 */ 01088 basic_string& 01089 append(const basic_string& __str, size_type __pos, size_type __n) 01090 { return _M_append(__str._M_data() 01091 + __str._M_check(__pos, "basic_string::append"), 01092 __str._M_limit(__pos, __n)); } 01093 01094 /** 01095 * @brief Append a C substring. 01096 * @param __s The C string to append. 01097 * @param __n The number of characters to append. 01098 * @return Reference to this string. 01099 */ 01100 basic_string& 01101 append(const _CharT* __s, size_type __n) 01102 { 01103 __glibcxx_requires_string_len(__s, __n); 01104 _M_check_length(size_type(0), __n, "basic_string::append"); 01105 return _M_append(__s, __n); 01106 } 01107 01108 /** 01109 * @brief Append a C string. 01110 * @param __s The C string to append. 01111 * @return Reference to this string. 01112 */ 01113 basic_string& 01114 append(const _CharT* __s) 01115 { 01116 __glibcxx_requires_string(__s); 01117 const size_type __n = traits_type::length(__s); 01118 _M_check_length(size_type(0), __n, "basic_string::append"); 01119 return _M_append(__s, __n); 01120 } 01121 01122 /** 01123 * @brief Append multiple characters. 01124 * @param __n The number of characters to append. 01125 * @param __c The character to use. 01126 * @return Reference to this string. 01127 * 01128 * Appends __n copies of __c to this string. 01129 */ 01130 basic_string& 01131 append(size_type __n, _CharT __c) 01132 { return _M_replace_aux(this->size(), size_type(0), __n, __c); } 01133 01134 #if __cplusplus >= 201103L 01135 /** 01136 * @brief Append an initializer_list of characters. 01137 * @param __l The initializer_list of characters to append. 01138 * @return Reference to this string. 01139 */ 01140 basic_string& 01141 append(initializer_list<_CharT> __l) 01142 { return this->append(__l.begin(), __l.size()); } 01143 #endif // C++11 01144 01145 /** 01146 * @brief Append a range of characters. 01147 * @param __first Iterator referencing the first character to append. 01148 * @param __last Iterator marking the end of the range. 01149 * @return Reference to this string. 01150 * 01151 * Appends characters in the range [__first,__last) to this string. 01152 */ 01153 #if __cplusplus >= 201103L 01154 template<class _InputIterator, 01155 typename = std::_RequireInputIter<_InputIterator>> 01156 #else 01157 template<class _InputIterator> 01158 #endif 01159 basic_string& 01160 append(_InputIterator __first, _InputIterator __last) 01161 { return this->replace(end(), end(), __first, __last); } 01162 01163 /** 01164 * @brief Append a single character. 01165 * @param __c Character to append. 01166 */ 01167 void 01168 push_back(_CharT __c) 01169 { 01170 const size_type __size = this->size(); 01171 if (__size + 1 > this->capacity()) 01172 this->_M_mutate(__size, size_type(0), 0, size_type(1)); 01173 traits_type::assign(this->_M_data()[__size], __c); 01174 this->_M_set_length(__size + 1); 01175 } 01176 01177 /** 01178 * @brief Set value to contents of another string. 01179 * @param __str Source string to use. 01180 * @return Reference to this string. 01181 */ 01182 basic_string& 01183 assign(const basic_string& __str) 01184 { 01185 this->_M_assign(__str); 01186 return *this; 01187 } 01188 01189 #if __cplusplus >= 201103L 01190 /** 01191 * @brief Set value to contents of another string. 01192 * @param __str Source string to use. 01193 * @return Reference to this string. 01194 * 01195 * This function sets this string to the exact contents of @a __str. 01196 * @a __str is a valid, but unspecified string. 01197 */ 01198 basic_string& 01199 assign(basic_string&& __str) 01200 noexcept(_Alloc_traits::_S_nothrow_move()) 01201 { 01202 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01203 // 2063. Contradictory requirements for string move assignment 01204 return *this = std::move(__str); 01205 } 01206 #endif // C++11 01207 01208 /** 01209 * @brief Set value to a substring of a string. 01210 * @param __str The string to use. 01211 * @param __pos Index of the first character of str. 01212 * @param __n Number of characters to use. 01213 * @return Reference to this string. 01214 * @throw std::out_of_range if @a pos is not a valid index. 01215 * 01216 * This function sets this string to the substring of @a __str 01217 * consisting of @a __n characters at @a __pos. If @a __n is 01218 * is larger than the number of available characters in @a 01219 * __str, the remainder of @a __str is used. 01220 */ 01221 basic_string& 01222 assign(const basic_string& __str, size_type __pos, size_type __n) 01223 { return _M_replace(size_type(0), this->size(), __str._M_data() 01224 + __str._M_check(__pos, "basic_string::assign"), 01225 __str._M_limit(__pos, __n)); } 01226 01227 /** 01228 * @brief Set value to a C substring. 01229 * @param __s The C string to use. 01230 * @param __n Number of characters to use. 01231 * @return Reference to this string. 01232 * 01233 * This function sets the value of this string to the first @a __n 01234 * characters of @a __s. If @a __n is is larger than the number of 01235 * available characters in @a __s, the remainder of @a __s is used. 01236 */ 01237 basic_string& 01238 assign(const _CharT* __s, size_type __n) 01239 { 01240 __glibcxx_requires_string_len(__s, __n); 01241 return _M_replace(size_type(0), this->size(), __s, __n); 01242 } 01243 01244 /** 01245 * @brief Set value to contents of a C string. 01246 * @param __s The C string to use. 01247 * @return Reference to this string. 01248 * 01249 * This function sets the value of this string to the value of @a __s. 01250 * The data is copied, so there is no dependence on @a __s once the 01251 * function returns. 01252 */ 01253 basic_string& 01254 assign(const _CharT* __s) 01255 { 01256 __glibcxx_requires_string(__s); 01257 return _M_replace(size_type(0), this->size(), __s, 01258 traits_type::length(__s)); 01259 } 01260 01261 /** 01262 * @brief Set value to multiple characters. 01263 * @param __n Length of the resulting string. 01264 * @param __c The character to use. 01265 * @return Reference to this string. 01266 * 01267 * This function sets the value of this string to @a __n copies of 01268 * character @a __c. 01269 */ 01270 basic_string& 01271 assign(size_type __n, _CharT __c) 01272 { return _M_replace_aux(size_type(0), this->size(), __n, __c); } 01273 01274 /** 01275 * @brief Set value to a range of characters. 01276 * @param __first Iterator referencing the first character to append. 01277 * @param __last Iterator marking the end of the range. 01278 * @return Reference to this string. 01279 * 01280 * Sets value of string to characters in the range [__first,__last). 01281 */ 01282 #if __cplusplus >= 201103L 01283 template<class _InputIterator, 01284 typename = std::_RequireInputIter<_InputIterator>> 01285 #else 01286 template<class _InputIterator> 01287 #endif 01288 basic_string& 01289 assign(_InputIterator __first, _InputIterator __last) 01290 { return this->replace(begin(), end(), __first, __last); } 01291 01292 #if __cplusplus >= 201103L 01293 /** 01294 * @brief Set value to an initializer_list of characters. 01295 * @param __l The initializer_list of characters to assign. 01296 * @return Reference to this string. 01297 */ 01298 basic_string& 01299 assign(initializer_list<_CharT> __l) 01300 { return this->assign(__l.begin(), __l.size()); } 01301 #endif // C++11 01302 01303 #if __cplusplus >= 201103L 01304 /** 01305 * @brief Insert multiple characters. 01306 * @param __p Const_iterator referencing location in string to 01307 * insert at. 01308 * @param __n Number of characters to insert 01309 * @param __c The character to insert. 01310 * @return Iterator referencing the first inserted char. 01311 * @throw std::length_error If new length exceeds @c max_size(). 01312 * 01313 * Inserts @a __n copies of character @a __c starting at the 01314 * position referenced by iterator @a __p. If adding 01315 * characters causes the length to exceed max_size(), 01316 * length_error is thrown. The value of the string doesn't 01317 * change if an error is thrown. 01318 */ 01319 iterator 01320 insert(const_iterator __p, size_type __n, _CharT __c) 01321 { 01322 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end()); 01323 const size_type __pos = __p - begin(); 01324 this->replace(__p, __p, __n, __c); 01325 return iterator(this->_M_data() + __pos); 01326 } 01327 #else 01328 /** 01329 * @brief Insert multiple characters. 01330 * @param __p Iterator referencing location in string to insert at. 01331 * @param __n Number of characters to insert 01332 * @param __c The character to insert. 01333 * @throw std::length_error If new length exceeds @c max_size(). 01334 * 01335 * Inserts @a __n copies of character @a __c starting at the 01336 * position referenced by iterator @a __p. If adding 01337 * characters causes the length to exceed max_size(), 01338 * length_error is thrown. The value of the string doesn't 01339 * change if an error is thrown. 01340 */ 01341 void 01342 insert(iterator __p, size_type __n, _CharT __c) 01343 { this->replace(__p, __p, __n, __c); } 01344 #endif 01345 01346 #if __cplusplus >= 201103L 01347 /** 01348 * @brief Insert a range of characters. 01349 * @param __p Const_iterator referencing location in string to 01350 * insert at. 01351 * @param __beg Start of range. 01352 * @param __end End of range. 01353 * @return Iterator referencing the first inserted char. 01354 * @throw std::length_error If new length exceeds @c max_size(). 01355 * 01356 * Inserts characters in range [beg,end). If adding characters 01357 * causes the length to exceed max_size(), length_error is 01358 * thrown. The value of the string doesn't change if an error 01359 * is thrown. 01360 */ 01361 template<class _InputIterator, 01362 typename = std::_RequireInputIter<_InputIterator>> 01363 iterator 01364 insert(const_iterator __p, _InputIterator __beg, _InputIterator __end) 01365 { 01366 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end()); 01367 const size_type __pos = __p - begin(); 01368 this->replace(__p, __p, __beg, __end); 01369 return iterator(this->_M_data() + __pos); 01370 } 01371 #else 01372 /** 01373 * @brief Insert a range of characters. 01374 * @param __p Iterator referencing location in string to insert at. 01375 * @param __beg Start of range. 01376 * @param __end End of range. 01377 * @throw std::length_error If new length exceeds @c max_size(). 01378 * 01379 * Inserts characters in range [__beg,__end). If adding 01380 * characters causes the length to exceed max_size(), 01381 * length_error is thrown. The value of the string doesn't 01382 * change if an error is thrown. 01383 */ 01384 template<class _InputIterator> 01385 void 01386 insert(iterator __p, _InputIterator __beg, _InputIterator __end) 01387 { this->replace(__p, __p, __beg, __end); } 01388 #endif 01389 01390 #if __cplusplus >= 201103L 01391 /** 01392 * @brief Insert an initializer_list of characters. 01393 * @param __p Iterator referencing location in string to insert at. 01394 * @param __l The initializer_list of characters to insert. 01395 * @throw std::length_error If new length exceeds @c max_size(). 01396 */ 01397 void 01398 insert(iterator __p, initializer_list<_CharT> __l) 01399 { 01400 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end()); 01401 this->insert(__p - begin(), __l.begin(), __l.size()); 01402 } 01403 #endif // C++11 01404 01405 /** 01406 * @brief Insert value of a string. 01407 * @param __pos1 Iterator referencing location in string to insert at. 01408 * @param __str The string to insert. 01409 * @return Reference to this string. 01410 * @throw std::length_error If new length exceeds @c max_size(). 01411 * 01412 * Inserts value of @a __str starting at @a __pos1. If adding 01413 * characters causes the length to exceed max_size(), 01414 * length_error is thrown. The value of the string doesn't 01415 * change if an error is thrown. 01416 */ 01417 basic_string& 01418 insert(size_type __pos1, const basic_string& __str) 01419 { return this->replace(__pos1, size_type(0), 01420 __str._M_data(), __str.size()); } 01421 01422 /** 01423 * @brief Insert a substring. 01424 * @param __pos1 Iterator referencing location in string to insert at. 01425 * @param __str The string to insert. 01426 * @param __pos2 Start of characters in str to insert. 01427 * @param __n Number of characters to insert. 01428 * @return Reference to this string. 01429 * @throw std::length_error If new length exceeds @c max_size(). 01430 * @throw std::out_of_range If @a pos1 > size() or 01431 * @a __pos2 > @a str.size(). 01432 * 01433 * Starting at @a pos1, insert @a __n character of @a __str 01434 * beginning with @a __pos2. If adding characters causes the 01435 * length to exceed max_size(), length_error is thrown. If @a 01436 * __pos1 is beyond the end of this string or @a __pos2 is 01437 * beyond the end of @a __str, out_of_range is thrown. The 01438 * value of the string doesn't change if an error is thrown. 01439 */ 01440 basic_string& 01441 insert(size_type __pos1, const basic_string& __str, 01442 size_type __pos2, size_type __n) 01443 { return this->replace(__pos1, size_type(0), __str._M_data() 01444 + __str._M_check(__pos2, "basic_string::insert"), 01445 __str._M_limit(__pos2, __n)); } 01446 01447 /** 01448 * @brief Insert a C substring. 01449 * @param __pos Iterator referencing location in string to insert at. 01450 * @param __s The C string to insert. 01451 * @param __n The number of characters to insert. 01452 * @return Reference to this string. 01453 * @throw std::length_error If new length exceeds @c max_size(). 01454 * @throw std::out_of_range If @a __pos is beyond the end of this 01455 * string. 01456 * 01457 * Inserts the first @a __n characters of @a __s starting at @a 01458 * __pos. If adding characters causes the length to exceed 01459 * max_size(), length_error is thrown. If @a __pos is beyond 01460 * end(), out_of_range is thrown. The value of the string 01461 * doesn't change if an error is thrown. 01462 */ 01463 basic_string& 01464 insert(size_type __pos, const _CharT* __s, size_type __n) 01465 { return this->replace(__pos, size_type(0), __s, __n); } 01466 01467 /** 01468 * @brief Insert a C string. 01469 * @param __pos Iterator referencing location in string to insert at. 01470 * @param __s The C string to insert. 01471 * @return Reference to this string. 01472 * @throw std::length_error If new length exceeds @c max_size(). 01473 * @throw std::out_of_range If @a pos is beyond the end of this 01474 * string. 01475 * 01476 * Inserts the first @a n characters of @a __s starting at @a __pos. If 01477 * adding characters causes the length to exceed max_size(), 01478 * length_error is thrown. If @a __pos is beyond end(), out_of_range is 01479 * thrown. The value of the string doesn't change if an error is 01480 * thrown. 01481 */ 01482 basic_string& 01483 insert(size_type __pos, const _CharT* __s) 01484 { 01485 __glibcxx_requires_string(__s); 01486 return this->replace(__pos, size_type(0), __s, 01487 traits_type::length(__s)); 01488 } 01489 01490 /** 01491 * @brief Insert multiple characters. 01492 * @param __pos Index in string to insert at. 01493 * @param __n Number of characters to insert 01494 * @param __c The character to insert. 01495 * @return Reference to this string. 01496 * @throw std::length_error If new length exceeds @c max_size(). 01497 * @throw std::out_of_range If @a __pos is beyond the end of this 01498 * string. 01499 * 01500 * Inserts @a __n copies of character @a __c starting at index 01501 * @a __pos. If adding characters causes the length to exceed 01502 * max_size(), length_error is thrown. If @a __pos > length(), 01503 * out_of_range is thrown. The value of the string doesn't 01504 * change if an error is thrown. 01505 */ 01506 basic_string& 01507 insert(size_type __pos, size_type __n, _CharT __c) 01508 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"), 01509 size_type(0), __n, __c); } 01510 01511 /** 01512 * @brief Insert one character. 01513 * @param __p Iterator referencing position in string to insert at. 01514 * @param __c The character to insert. 01515 * @return Iterator referencing newly inserted char. 01516 * @throw std::length_error If new length exceeds @c max_size(). 01517 * 01518 * Inserts character @a __c at position referenced by @a __p. 01519 * If adding character causes the length to exceed max_size(), 01520 * length_error is thrown. If @a __p is beyond end of string, 01521 * out_of_range is thrown. The value of the string doesn't 01522 * change if an error is thrown. 01523 */ 01524 iterator 01525 insert(__const_iterator __p, _CharT __c) 01526 { 01527 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end()); 01528 const size_type __pos = __p - begin(); 01529 _M_replace_aux(__pos, size_type(0), size_type(1), __c); 01530 return iterator(_M_data() + __pos); 01531 } 01532 01533 /** 01534 * @brief Remove characters. 01535 * @param __pos Index of first character to remove (default 0). 01536 * @param __n Number of characters to remove (default remainder). 01537 * @return Reference to this string. 01538 * @throw std::out_of_range If @a pos is beyond the end of this 01539 * string. 01540 * 01541 * Removes @a __n characters from this string starting at @a 01542 * __pos. The length of the string is reduced by @a __n. If 01543 * there are < @a __n characters to remove, the remainder of 01544 * the string is truncated. If @a __p is beyond end of string, 01545 * out_of_range is thrown. The value of the string doesn't 01546 * change if an error is thrown. 01547 */ 01548 basic_string& 01549 erase(size_type __pos = 0, size_type __n = npos) 01550 { 01551 this->_M_erase(_M_check(__pos, "basic_string::erase"), 01552 _M_limit(__pos, __n)); 01553 return *this; 01554 } 01555 01556 /** 01557 * @brief Remove one character. 01558 * @param __position Iterator referencing the character to remove. 01559 * @return iterator referencing same location after removal. 01560 * 01561 * Removes the character at @a __position from this string. The value 01562 * of the string doesn't change if an error is thrown. 01563 */ 01564 iterator 01565 erase(__const_iterator __position) 01566 { 01567 _GLIBCXX_DEBUG_PEDASSERT(__position >= begin() 01568 && __position < end()); 01569 const size_type __pos = __position - begin(); 01570 this->_M_erase(__pos, size_type(1)); 01571 return iterator(_M_data() + __pos); 01572 } 01573 01574 /** 01575 * @brief Remove a range of characters. 01576 * @param __first Iterator referencing the first character to remove. 01577 * @param __last Iterator referencing the end of the range. 01578 * @return Iterator referencing location of first after removal. 01579 * 01580 * Removes the characters in the range [first,last) from this string. 01581 * The value of the string doesn't change if an error is thrown. 01582 */ 01583 iterator 01584 erase(__const_iterator __first, __const_iterator __last) 01585 { 01586 _GLIBCXX_DEBUG_PEDASSERT(__first >= begin() && __first <= __last 01587 && __last <= end()); 01588 const size_type __pos = __first - begin(); 01589 this->_M_erase(__pos, __last - __first); 01590 return iterator(this->_M_data() + __pos); 01591 } 01592 01593 #if __cplusplus >= 201103L 01594 /** 01595 * @brief Remove the last character. 01596 * 01597 * The string must be non-empty. 01598 */ 01599 void 01600 pop_back() noexcept 01601 { _M_erase(size()-1, 1); } 01602 #endif // C++11 01603 01604 /** 01605 * @brief Replace characters with value from another string. 01606 * @param __pos Index of first character to replace. 01607 * @param __n Number of characters to be replaced. 01608 * @param __str String to insert. 01609 * @return Reference to this string. 01610 * @throw std::out_of_range If @a pos is beyond the end of this 01611 * string. 01612 * @throw std::length_error If new length exceeds @c max_size(). 01613 * 01614 * Removes the characters in the range [__pos,__pos+__n) from 01615 * this string. In place, the value of @a __str is inserted. 01616 * If @a __pos is beyond end of string, out_of_range is thrown. 01617 * If the length of the result exceeds max_size(), length_error 01618 * is thrown. The value of the string doesn't change if an 01619 * error is thrown. 01620 */ 01621 basic_string& 01622 replace(size_type __pos, size_type __n, const basic_string& __str) 01623 { return this->replace(__pos, __n, __str._M_data(), __str.size()); } 01624 01625 /** 01626 * @brief Replace characters with value from another string. 01627 * @param __pos1 Index of first character to replace. 01628 * @param __n1 Number of characters to be replaced. 01629 * @param __str String to insert. 01630 * @param __pos2 Index of first character of str to use. 01631 * @param __n2 Number of characters from str to use. 01632 * @return Reference to this string. 01633 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 > 01634 * __str.size(). 01635 * @throw std::length_error If new length exceeds @c max_size(). 01636 * 01637 * Removes the characters in the range [__pos1,__pos1 + n) from this 01638 * string. In place, the value of @a __str is inserted. If @a __pos is 01639 * beyond end of string, out_of_range is thrown. If the length of the 01640 * result exceeds max_size(), length_error is thrown. The value of the 01641 * string doesn't change if an error is thrown. 01642 */ 01643 basic_string& 01644 replace(size_type __pos1, size_type __n1, const basic_string& __str, 01645 size_type __pos2, size_type __n2) 01646 { return this->replace(__pos1, __n1, __str._M_data() 01647 + __str._M_check(__pos2, "basic_string::replace"), 01648 __str._M_limit(__pos2, __n2)); } 01649 01650 /** 01651 * @brief Replace characters with value of a C substring. 01652 * @param __pos Index of first character to replace. 01653 * @param __n1 Number of characters to be replaced. 01654 * @param __s C string to insert. 01655 * @param __n2 Number of characters from @a s to use. 01656 * @return Reference to this string. 01657 * @throw std::out_of_range If @a pos1 > size(). 01658 * @throw std::length_error If new length exceeds @c max_size(). 01659 * 01660 * Removes the characters in the range [__pos,__pos + __n1) 01661 * from this string. In place, the first @a __n2 characters of 01662 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If 01663 * @a __pos is beyond end of string, out_of_range is thrown. If 01664 * the length of result exceeds max_size(), length_error is 01665 * thrown. The value of the string doesn't change if an error 01666 * is thrown. 01667 */ 01668 basic_string& 01669 replace(size_type __pos, size_type __n1, const _CharT* __s, 01670 size_type __n2) 01671 { 01672 __glibcxx_requires_string_len(__s, __n2); 01673 return _M_replace(_M_check(__pos, "basic_string::replace"), 01674 _M_limit(__pos, __n1), __s, __n2); 01675 } 01676 01677 /** 01678 * @brief Replace characters with value of a C string. 01679 * @param __pos Index of first character to replace. 01680 * @param __n1 Number of characters to be replaced. 01681 * @param __s C string to insert. 01682 * @return Reference to this string. 01683 * @throw std::out_of_range If @a pos > size(). 01684 * @throw std::length_error If new length exceeds @c max_size(). 01685 * 01686 * Removes the characters in the range [__pos,__pos + __n1) 01687 * from this string. In place, the characters of @a __s are 01688 * inserted. If @a __pos is beyond end of string, out_of_range 01689 * is thrown. If the length of result exceeds max_size(), 01690 * length_error is thrown. The value of the string doesn't 01691 * change if an error is thrown. 01692 */ 01693 basic_string& 01694 replace(size_type __pos, size_type __n1, const _CharT* __s) 01695 { 01696 __glibcxx_requires_string(__s); 01697 return this->replace(__pos, __n1, __s, traits_type::length(__s)); 01698 } 01699 01700 /** 01701 * @brief Replace characters with multiple characters. 01702 * @param __pos Index of first character to replace. 01703 * @param __n1 Number of characters to be replaced. 01704 * @param __n2 Number of characters to insert. 01705 * @param __c Character to insert. 01706 * @return Reference to this string. 01707 * @throw std::out_of_range If @a __pos > size(). 01708 * @throw std::length_error If new length exceeds @c max_size(). 01709 * 01710 * Removes the characters in the range [pos,pos + n1) from this 01711 * string. In place, @a __n2 copies of @a __c are inserted. 01712 * If @a __pos is beyond end of string, out_of_range is thrown. 01713 * If the length of result exceeds max_size(), length_error is 01714 * thrown. The value of the string doesn't change if an error 01715 * is thrown. 01716 */ 01717 basic_string& 01718 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c) 01719 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"), 01720 _M_limit(__pos, __n1), __n2, __c); } 01721 01722 /** 01723 * @brief Replace range of characters with string. 01724 * @param __i1 Iterator referencing start of range to replace. 01725 * @param __i2 Iterator referencing end of range to replace. 01726 * @param __str String value to insert. 01727 * @return Reference to this string. 01728 * @throw std::length_error If new length exceeds @c max_size(). 01729 * 01730 * Removes the characters in the range [__i1,__i2). In place, 01731 * the value of @a __str is inserted. If the length of result 01732 * exceeds max_size(), length_error is thrown. The value of 01733 * the string doesn't change if an error is thrown. 01734 */ 01735 basic_string& 01736 replace(__const_iterator __i1, __const_iterator __i2, 01737 const basic_string& __str) 01738 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); } 01739 01740 /** 01741 * @brief Replace range of characters with C substring. 01742 * @param __i1 Iterator referencing start of range to replace. 01743 * @param __i2 Iterator referencing end of range to replace. 01744 * @param __s C string value to insert. 01745 * @param __n Number of characters from s to insert. 01746 * @return Reference to this string. 01747 * @throw std::length_error If new length exceeds @c max_size(). 01748 * 01749 * Removes the characters in the range [__i1,__i2). In place, 01750 * the first @a __n characters of @a __s are inserted. If the 01751 * length of result exceeds max_size(), length_error is thrown. 01752 * The value of the string doesn't change if an error is 01753 * thrown. 01754 */ 01755 basic_string& 01756 replace(__const_iterator __i1, __const_iterator __i2, 01757 const _CharT* __s, size_type __n) 01758 { 01759 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 01760 && __i2 <= end()); 01761 return this->replace(__i1 - begin(), __i2 - __i1, __s, __n); 01762 } 01763 01764 /** 01765 * @brief Replace range of characters with C string. 01766 * @param __i1 Iterator referencing start of range to replace. 01767 * @param __i2 Iterator referencing end of range to replace. 01768 * @param __s C string value to insert. 01769 * @return Reference to this string. 01770 * @throw std::length_error If new length exceeds @c max_size(). 01771 * 01772 * Removes the characters in the range [__i1,__i2). In place, 01773 * the characters of @a __s are inserted. If the length of 01774 * result exceeds max_size(), length_error is thrown. The 01775 * value of the string doesn't change if an error is thrown. 01776 */ 01777 basic_string& 01778 replace(__const_iterator __i1, __const_iterator __i2, const _CharT* __s) 01779 { 01780 __glibcxx_requires_string(__s); 01781 return this->replace(__i1, __i2, __s, traits_type::length(__s)); 01782 } 01783 01784 /** 01785 * @brief Replace range of characters with multiple characters 01786 * @param __i1 Iterator referencing start of range to replace. 01787 * @param __i2 Iterator referencing end of range to replace. 01788 * @param __n Number of characters to insert. 01789 * @param __c Character to insert. 01790 * @return Reference to this string. 01791 * @throw std::length_error If new length exceeds @c max_size(). 01792 * 01793 * Removes the characters in the range [__i1,__i2). In place, 01794 * @a __n copies of @a __c are inserted. If the length of 01795 * result exceeds max_size(), length_error is thrown. The 01796 * value of the string doesn't change if an error is thrown. 01797 */ 01798 basic_string& 01799 replace(__const_iterator __i1, __const_iterator __i2, size_type __n, 01800 _CharT __c) 01801 { 01802 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 01803 && __i2 <= end()); 01804 return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __c); 01805 } 01806 01807 /** 01808 * @brief Replace range of characters with range. 01809 * @param __i1 Iterator referencing start of range to replace. 01810 * @param __i2 Iterator referencing end of range to replace. 01811 * @param __k1 Iterator referencing start of range to insert. 01812 * @param __k2 Iterator referencing end of range to insert. 01813 * @return Reference to this string. 01814 * @throw std::length_error If new length exceeds @c max_size(). 01815 * 01816 * Removes the characters in the range [__i1,__i2). In place, 01817 * characters in the range [__k1,__k2) are inserted. If the 01818 * length of result exceeds max_size(), length_error is thrown. 01819 * The value of the string doesn't change if an error is 01820 * thrown. 01821 */ 01822 #if __cplusplus >= 201103L 01823 template<class _InputIterator, 01824 typename = std::_RequireInputIter<_InputIterator>> 01825 basic_string& 01826 replace(const_iterator __i1, const_iterator __i2, 01827 _InputIterator __k1, _InputIterator __k2) 01828 { 01829 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 01830 && __i2 <= end()); 01831 __glibcxx_requires_valid_range(__k1, __k2); 01832 return this->_M_replace_dispatch(__i1, __i2, __k1, __k2, 01833 std::__false_type()); 01834 } 01835 #else 01836 template<class _InputIterator> 01837 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST 01838 typename __enable_if_not_native_iterator<_InputIterator>::__type 01839 #else 01840 basic_string& 01841 #endif 01842 replace(iterator __i1, iterator __i2, 01843 _InputIterator __k1, _InputIterator __k2) 01844 { 01845 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 01846 && __i2 <= end()); 01847 __glibcxx_requires_valid_range(__k1, __k2); 01848 typedef typename std::__is_integer<_InputIterator>::__type _Integral; 01849 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral()); 01850 } 01851 #endif 01852 01853 // Specializations for the common case of pointer and iterator: 01854 // useful to avoid the overhead of temporary buffering in _M_replace. 01855 basic_string& 01856 replace(__const_iterator __i1, __const_iterator __i2, 01857 _CharT* __k1, _CharT* __k2) 01858 { 01859 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 01860 && __i2 <= end()); 01861 __glibcxx_requires_valid_range(__k1, __k2); 01862 return this->replace(__i1 - begin(), __i2 - __i1, 01863 __k1, __k2 - __k1); 01864 } 01865 01866 basic_string& 01867 replace(__const_iterator __i1, __const_iterator __i2, 01868 const _CharT* __k1, const _CharT* __k2) 01869 { 01870 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 01871 && __i2 <= end()); 01872 __glibcxx_requires_valid_range(__k1, __k2); 01873 return this->replace(__i1 - begin(), __i2 - __i1, 01874 __k1, __k2 - __k1); 01875 } 01876 01877 basic_string& 01878 replace(__const_iterator __i1, __const_iterator __i2, 01879 iterator __k1, iterator __k2) 01880 { 01881 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 01882 && __i2 <= end()); 01883 __glibcxx_requires_valid_range(__k1, __k2); 01884 return this->replace(__i1 - begin(), __i2 - __i1, 01885 __k1.base(), __k2 - __k1); 01886 } 01887 01888 basic_string& 01889 replace(__const_iterator __i1, __const_iterator __i2, 01890 const_iterator __k1, const_iterator __k2) 01891 { 01892 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 01893 && __i2 <= end()); 01894 __glibcxx_requires_valid_range(__k1, __k2); 01895 return this->replace(__i1 - begin(), __i2 - __i1, 01896 __k1.base(), __k2 - __k1); 01897 } 01898 01899 #if __cplusplus >= 201103L 01900 /** 01901 * @brief Replace range of characters with initializer_list. 01902 * @param __i1 Iterator referencing start of range to replace. 01903 * @param __i2 Iterator referencing end of range to replace. 01904 * @param __l The initializer_list of characters to insert. 01905 * @return Reference to this string. 01906 * @throw std::length_error If new length exceeds @c max_size(). 01907 * 01908 * Removes the characters in the range [__i1,__i2). In place, 01909 * characters in the range [__k1,__k2) are inserted. If the 01910 * length of result exceeds max_size(), length_error is thrown. 01911 * The value of the string doesn't change if an error is 01912 * thrown. 01913 */ 01914 basic_string& replace(const_iterator __i1, const_iterator __i2, 01915 initializer_list<_CharT> __l) 01916 { return this->replace(__i1, __i2, __l.begin(), __l.end()); } 01917 #endif // C++11 01918 01919 private: 01920 template<class _Integer> 01921 basic_string& 01922 _M_replace_dispatch(const_iterator __i1, const_iterator __i2, 01923 _Integer __n, _Integer __val, __true_type) 01924 { return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __val); } 01925 01926 template<class _InputIterator> 01927 basic_string& 01928 _M_replace_dispatch(const_iterator __i1, const_iterator __i2, 01929 _InputIterator __k1, _InputIterator __k2, 01930 __false_type); 01931 01932 basic_string& 01933 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2, 01934 _CharT __c); 01935 01936 basic_string& 01937 _M_replace(size_type __pos, size_type __len1, const _CharT* __s, 01938 const size_type __len2); 01939 01940 basic_string& 01941 _M_append(const _CharT* __s, size_type __n); 01942 01943 public: 01944 01945 /** 01946 * @brief Copy substring into C string. 01947 * @param __s C string to copy value into. 01948 * @param __n Number of characters to copy. 01949 * @param __pos Index of first character to copy. 01950 * @return Number of characters actually copied 01951 * @throw std::out_of_range If __pos > size(). 01952 * 01953 * Copies up to @a __n characters starting at @a __pos into the 01954 * C string @a __s. If @a __pos is %greater than size(), 01955 * out_of_range is thrown. 01956 */ 01957 size_type 01958 copy(_CharT* __s, size_type __n, size_type __pos = 0) const; 01959 01960 /** 01961 * @brief Swap contents with another string. 01962 * @param __s String to swap with. 01963 * 01964 * Exchanges the contents of this string with that of @a __s in constant 01965 * time. 01966 */ 01967 void 01968 swap(basic_string& __s) _GLIBCXX_NOEXCEPT; 01969 01970 // String operations: 01971 /** 01972 * @brief Return const pointer to null-terminated contents. 01973 * 01974 * This is a handle to internal data. Do not modify or dire things may 01975 * happen. 01976 */ 01977 const _CharT* 01978 c_str() const _GLIBCXX_NOEXCEPT 01979 { return _M_data(); } 01980 01981 /** 01982 * @brief Return const pointer to contents. 01983 * 01984 * This is a handle to internal data. Do not modify or dire things may 01985 * happen. 01986 */ 01987 const _CharT* 01988 data() const _GLIBCXX_NOEXCEPT 01989 { return _M_data(); } 01990 01991 /** 01992 * @brief Return copy of allocator used to construct this string. 01993 */ 01994 allocator_type 01995 get_allocator() const _GLIBCXX_NOEXCEPT 01996 { return _M_get_allocator(); } 01997 01998 /** 01999 * @brief Find position of a C substring. 02000 * @param __s C string to locate. 02001 * @param __pos Index of character to search from. 02002 * @param __n Number of characters from @a s to search for. 02003 * @return Index of start of first occurrence. 02004 * 02005 * Starting from @a __pos, searches forward for the first @a 02006 * __n characters in @a __s within this string. If found, 02007 * returns the index where it begins. If not found, returns 02008 * npos. 02009 */ 02010 size_type 02011 find(const _CharT* __s, size_type __pos, size_type __n) const; 02012 02013 /** 02014 * @brief Find position of a string. 02015 * @param __str String to locate. 02016 * @param __pos Index of character to search from (default 0). 02017 * @return Index of start of first occurrence. 02018 * 02019 * Starting from @a __pos, searches forward for value of @a __str within 02020 * this string. If found, returns the index where it begins. If not 02021 * found, returns npos. 02022 */ 02023 size_type 02024 find(const basic_string& __str, size_type __pos = 0) const 02025 _GLIBCXX_NOEXCEPT 02026 { return this->find(__str.data(), __pos, __str.size()); } 02027 02028 /** 02029 * @brief Find position of a C string. 02030 * @param __s C string to locate. 02031 * @param __pos Index of character to search from (default 0). 02032 * @return Index of start of first occurrence. 02033 * 02034 * Starting from @a __pos, searches forward for the value of @a 02035 * __s within this string. If found, returns the index where 02036 * it begins. If not found, returns npos. 02037 */ 02038 size_type 02039 find(const _CharT* __s, size_type __pos = 0) const 02040 { 02041 __glibcxx_requires_string(__s); 02042 return this->find(__s, __pos, traits_type::length(__s)); 02043 } 02044 02045 /** 02046 * @brief Find position of a character. 02047 * @param __c Character to locate. 02048 * @param __pos Index of character to search from (default 0). 02049 * @return Index of first occurrence. 02050 * 02051 * Starting from @a __pos, searches forward for @a __c within 02052 * this string. If found, returns the index where it was 02053 * found. If not found, returns npos. 02054 */ 02055 size_type 02056 find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT; 02057 02058 /** 02059 * @brief Find last position of a string. 02060 * @param __str String to locate. 02061 * @param __pos Index of character to search back from (default end). 02062 * @return Index of start of last occurrence. 02063 * 02064 * Starting from @a __pos, searches backward for value of @a 02065 * __str within this string. If found, returns the index where 02066 * it begins. If not found, returns npos. 02067 */ 02068 size_type 02069 rfind(const basic_string& __str, size_type __pos = npos) const 02070 _GLIBCXX_NOEXCEPT 02071 { return this->rfind(__str.data(), __pos, __str.size()); } 02072 02073 /** 02074 * @brief Find last position of a C substring. 02075 * @param __s C string to locate. 02076 * @param __pos Index of character to search back from. 02077 * @param __n Number of characters from s to search for. 02078 * @return Index of start of last occurrence. 02079 * 02080 * Starting from @a __pos, searches backward for the first @a 02081 * __n characters in @a __s within this string. If found, 02082 * returns the index where it begins. If not found, returns 02083 * npos. 02084 */ 02085 size_type 02086 rfind(const _CharT* __s, size_type __pos, size_type __n) const; 02087 02088 /** 02089 * @brief Find last position of a C string. 02090 * @param __s C string to locate. 02091 * @param __pos Index of character to start search at (default end). 02092 * @return Index of start of last occurrence. 02093 * 02094 * Starting from @a __pos, searches backward for the value of 02095 * @a __s within this string. If found, returns the index 02096 * where it begins. If not found, returns npos. 02097 */ 02098 size_type 02099 rfind(const _CharT* __s, size_type __pos = npos) const 02100 { 02101 __glibcxx_requires_string(__s); 02102 return this->rfind(__s, __pos, traits_type::length(__s)); 02103 } 02104 02105 /** 02106 * @brief Find last position of a character. 02107 * @param __c Character to locate. 02108 * @param __pos Index of character to search back from (default end). 02109 * @return Index of last occurrence. 02110 * 02111 * Starting from @a __pos, searches backward for @a __c within 02112 * this string. If found, returns the index where it was 02113 * found. If not found, returns npos. 02114 */ 02115 size_type 02116 rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT; 02117 02118 /** 02119 * @brief Find position of a character of string. 02120 * @param __str String containing characters to locate. 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 one of the 02125 * characters of @a __str within this string. If found, 02126 * returns the index where it was found. If not found, returns 02127 * npos. 02128 */ 02129 size_type 02130 find_first_of(const basic_string& __str, size_type __pos = 0) const 02131 _GLIBCXX_NOEXCEPT 02132 { return this->find_first_of(__str.data(), __pos, __str.size()); } 02133 02134 /** 02135 * @brief Find position of a character of C substring. 02136 * @param __s String containing characters to locate. 02137 * @param __pos Index of character to search from. 02138 * @param __n Number of characters from s to search for. 02139 * @return Index of first occurrence. 02140 * 02141 * Starting from @a __pos, searches forward for one of the 02142 * first @a __n characters of @a __s within this string. If 02143 * found, returns the index where it was found. If not found, 02144 * returns npos. 02145 */ 02146 size_type 02147 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const; 02148 02149 /** 02150 * @brief Find position of a character of C string. 02151 * @param __s String containing characters to locate. 02152 * @param __pos Index of character to search from (default 0). 02153 * @return Index of first occurrence. 02154 * 02155 * Starting from @a __pos, searches forward for one of the 02156 * characters of @a __s within this string. If found, returns 02157 * the index where it was found. If not found, returns npos. 02158 */ 02159 size_type 02160 find_first_of(const _CharT* __s, size_type __pos = 0) const 02161 { 02162 __glibcxx_requires_string(__s); 02163 return this->find_first_of(__s, __pos, traits_type::length(__s)); 02164 } 02165 02166 /** 02167 * @brief Find position of a character. 02168 * @param __c Character to locate. 02169 * @param __pos Index of character to search from (default 0). 02170 * @return Index of first occurrence. 02171 * 02172 * Starting from @a __pos, searches forward for the character 02173 * @a __c within this string. If found, returns the index 02174 * where it was found. If not found, returns npos. 02175 * 02176 * Note: equivalent to find(__c, __pos). 02177 */ 02178 size_type 02179 find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT 02180 { return this->find(__c, __pos); } 02181 02182 /** 02183 * @brief Find last position of a character of string. 02184 * @param __str String containing characters to locate. 02185 * @param __pos Index of character to search back from (default end). 02186 * @return Index of last occurrence. 02187 * 02188 * Starting from @a __pos, searches backward for one of the 02189 * characters of @a __str within this string. If found, 02190 * returns the index where it was found. If not found, returns 02191 * npos. 02192 */ 02193 size_type 02194 find_last_of(const basic_string& __str, size_type __pos = npos) const 02195 _GLIBCXX_NOEXCEPT 02196 { return this->find_last_of(__str.data(), __pos, __str.size()); } 02197 02198 /** 02199 * @brief Find last position of a character of C substring. 02200 * @param __s C string containing characters to locate. 02201 * @param __pos Index of character to search back from. 02202 * @param __n Number of characters from s to search for. 02203 * @return Index of last occurrence. 02204 * 02205 * Starting from @a __pos, searches backward for one of the 02206 * first @a __n characters of @a __s within this string. If 02207 * found, returns the index where it was found. If not found, 02208 * returns npos. 02209 */ 02210 size_type 02211 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const; 02212 02213 /** 02214 * @brief Find last position of a character of C string. 02215 * @param __s C string containing characters to locate. 02216 * @param __pos Index of character to search back from (default end). 02217 * @return Index of last occurrence. 02218 * 02219 * Starting from @a __pos, searches backward for one of the 02220 * characters of @a __s within this string. If found, returns 02221 * the index where it was found. If not found, returns npos. 02222 */ 02223 size_type 02224 find_last_of(const _CharT* __s, size_type __pos = npos) const 02225 { 02226 __glibcxx_requires_string(__s); 02227 return this->find_last_of(__s, __pos, traits_type::length(__s)); 02228 } 02229 02230 /** 02231 * @brief Find last position of a character. 02232 * @param __c Character to locate. 02233 * @param __pos Index of character to search back from (default end). 02234 * @return Index of last occurrence. 02235 * 02236 * Starting from @a __pos, searches backward for @a __c within 02237 * this string. If found, returns the index where it was 02238 * found. If not found, returns npos. 02239 * 02240 * Note: equivalent to rfind(__c, __pos). 02241 */ 02242 size_type 02243 find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT 02244 { return this->rfind(__c, __pos); } 02245 02246 /** 02247 * @brief Find position of a character not in string. 02248 * @param __str String containing characters to avoid. 02249 * @param __pos Index of character to search from (default 0). 02250 * @return Index of first occurrence. 02251 * 02252 * Starting from @a __pos, searches forward for a character not contained 02253 * in @a __str within this string. If found, returns the index where it 02254 * was found. If not found, returns npos. 02255 */ 02256 size_type 02257 find_first_not_of(const basic_string& __str, size_type __pos = 0) const 02258 _GLIBCXX_NOEXCEPT 02259 { return this->find_first_not_of(__str.data(), __pos, __str.size()); } 02260 02261 /** 02262 * @brief Find position of a character not in C substring. 02263 * @param __s C string containing characters to avoid. 02264 * @param __pos Index of character to search from. 02265 * @param __n Number of characters from __s to consider. 02266 * @return Index of first occurrence. 02267 * 02268 * Starting from @a __pos, searches forward for a character not 02269 * contained in the first @a __n characters of @a __s within 02270 * this string. If found, returns the index where it was 02271 * found. If not found, returns npos. 02272 */ 02273 size_type 02274 find_first_not_of(const _CharT* __s, size_type __pos, 02275 size_type __n) const; 02276 02277 /** 02278 * @brief Find position of a character not in C string. 02279 * @param __s C string containing characters to avoid. 02280 * @param __pos Index of character to search from (default 0). 02281 * @return Index of first occurrence. 02282 * 02283 * Starting from @a __pos, searches forward for a character not 02284 * contained in @a __s within this string. If found, returns 02285 * the index where it was found. If not found, returns npos. 02286 */ 02287 size_type 02288 find_first_not_of(const _CharT* __s, size_type __pos = 0) const 02289 { 02290 __glibcxx_requires_string(__s); 02291 return this->find_first_not_of(__s, __pos, traits_type::length(__s)); 02292 } 02293 02294 /** 02295 * @brief Find position of a different character. 02296 * @param __c Character to avoid. 02297 * @param __pos Index of character to search from (default 0). 02298 * @return Index of first occurrence. 02299 * 02300 * Starting from @a __pos, searches forward for a character 02301 * other than @a __c within this string. If found, returns the 02302 * index where it was found. If not found, returns npos. 02303 */ 02304 size_type 02305 find_first_not_of(_CharT __c, size_type __pos = 0) const 02306 _GLIBCXX_NOEXCEPT; 02307 02308 /** 02309 * @brief Find last position of a character not in string. 02310 * @param __str String containing characters to avoid. 02311 * @param __pos Index of character to search back from (default end). 02312 * @return Index of last occurrence. 02313 * 02314 * Starting from @a __pos, searches backward for a character 02315 * not contained in @a __str within this string. If found, 02316 * returns the index where it was found. If not found, returns 02317 * npos. 02318 */ 02319 size_type 02320 find_last_not_of(const basic_string& __str, size_type __pos = npos) const 02321 _GLIBCXX_NOEXCEPT 02322 { return this->find_last_not_of(__str.data(), __pos, __str.size()); } 02323 02324 /** 02325 * @brief Find last position of a character not in C substring. 02326 * @param __s C string containing characters to avoid. 02327 * @param __pos Index of character to search back from. 02328 * @param __n Number of characters from s to consider. 02329 * @return Index of last occurrence. 02330 * 02331 * Starting from @a __pos, searches backward for a character not 02332 * contained in the first @a __n characters of @a __s within this string. 02333 * If found, returns the index where it was found. If not found, 02334 * returns npos. 02335 */ 02336 size_type 02337 find_last_not_of(const _CharT* __s, size_type __pos, 02338 size_type __n) const; 02339 /** 02340 * @brief Find last position of a character not in C string. 02341 * @param __s C string containing characters to avoid. 02342 * @param __pos Index of character to search back from (default end). 02343 * @return Index of last occurrence. 02344 * 02345 * Starting from @a __pos, searches backward for a character 02346 * not contained in @a __s within this string. If found, 02347 * returns the index where it was found. If not found, returns 02348 * npos. 02349 */ 02350 size_type 02351 find_last_not_of(const _CharT* __s, size_type __pos = npos) const 02352 { 02353 __glibcxx_requires_string(__s); 02354 return this->find_last_not_of(__s, __pos, traits_type::length(__s)); 02355 } 02356 02357 /** 02358 * @brief Find last position of a different character. 02359 * @param __c Character to avoid. 02360 * @param __pos Index of character to search back from (default end). 02361 * @return Index of last occurrence. 02362 * 02363 * Starting from @a __pos, searches backward for a character other than 02364 * @a __c within this string. If found, returns the index where it was 02365 * found. If not found, returns npos. 02366 */ 02367 size_type 02368 find_last_not_of(_CharT __c, size_type __pos = npos) const 02369 _GLIBCXX_NOEXCEPT; 02370 02371 /** 02372 * @brief Get a substring. 02373 * @param __pos Index of first character (default 0). 02374 * @param __n Number of characters in substring (default remainder). 02375 * @return The new string. 02376 * @throw std::out_of_range If __pos > size(). 02377 * 02378 * Construct and return a new string using the @a __n 02379 * characters starting at @a __pos. If the string is too 02380 * short, use the remainder of the characters. If @a __pos is 02381 * beyond the end of the string, out_of_range is thrown. 02382 */ 02383 basic_string 02384 substr(size_type __pos = 0, size_type __n = npos) const 02385 { return basic_string(*this, 02386 _M_check(__pos, "basic_string::substr"), __n); } 02387 02388 /** 02389 * @brief Compare to a string. 02390 * @param __str String to compare against. 02391 * @return Integer < 0, 0, or > 0. 02392 * 02393 * Returns an integer < 0 if this string is ordered before @a 02394 * __str, 0 if their values are equivalent, or > 0 if this 02395 * string is ordered after @a __str. Determines the effective 02396 * length rlen of the strings to compare as the smallest of 02397 * size() and str.size(). The function then compares the two 02398 * strings by calling traits::compare(data(), str.data(),rlen). 02399 * If the result of the comparison is nonzero returns it, 02400 * otherwise the shorter one is ordered first. 02401 */ 02402 int 02403 compare(const basic_string& __str) const 02404 { 02405 const size_type __size = this->size(); 02406 const size_type __osize = __str.size(); 02407 const size_type __len = std::min(__size, __osize); 02408 02409 int __r = traits_type::compare(_M_data(), __str.data(), __len); 02410 if (!__r) 02411 __r = _S_compare(__size, __osize); 02412 return __r; 02413 } 02414 02415 /** 02416 * @brief Compare substring to a string. 02417 * @param __pos Index of first character of substring. 02418 * @param __n Number of characters in substring. 02419 * @param __str String to compare against. 02420 * @return Integer < 0, 0, or > 0. 02421 * 02422 * Form the substring of this string from the @a __n characters 02423 * starting at @a __pos. Returns an integer < 0 if the 02424 * substring is ordered before @a __str, 0 if their values are 02425 * equivalent, or > 0 if the substring is ordered after @a 02426 * __str. Determines the effective length rlen of the strings 02427 * to compare as the smallest of the length of the substring 02428 * and @a __str.size(). The function then compares the two 02429 * strings by calling 02430 * traits::compare(substring.data(),str.data(),rlen). If the 02431 * result of the comparison is nonzero returns it, otherwise 02432 * the shorter one is ordered first. 02433 */ 02434 int 02435 compare(size_type __pos, size_type __n, const basic_string& __str) const; 02436 02437 /** 02438 * @brief Compare substring to a substring. 02439 * @param __pos1 Index of first character of substring. 02440 * @param __n1 Number of characters in substring. 02441 * @param __str String to compare against. 02442 * @param __pos2 Index of first character of substring of str. 02443 * @param __n2 Number of characters in substring of str. 02444 * @return Integer < 0, 0, or > 0. 02445 * 02446 * Form the substring of this string from the @a __n1 02447 * characters starting at @a __pos1. Form the substring of @a 02448 * __str from the @a __n2 characters starting at @a __pos2. 02449 * Returns an integer < 0 if this substring is ordered before 02450 * the substring of @a __str, 0 if their values are equivalent, 02451 * or > 0 if this substring is ordered after the substring of 02452 * @a __str. Determines the effective length rlen of the 02453 * strings to compare as the smallest of the lengths of the 02454 * substrings. The function then compares the two strings by 02455 * calling 02456 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen). 02457 * If the result of the comparison is nonzero returns it, 02458 * otherwise the shorter one is ordered first. 02459 */ 02460 int 02461 compare(size_type __pos1, size_type __n1, const basic_string& __str, 02462 size_type __pos2, size_type __n2) const; 02463 02464 /** 02465 * @brief Compare to a C string. 02466 * @param __s C string to compare against. 02467 * @return Integer < 0, 0, or > 0. 02468 * 02469 * Returns an integer < 0 if this string is ordered before @a __s, 0 if 02470 * their values are equivalent, or > 0 if this string is ordered after 02471 * @a __s. Determines the effective length rlen of the strings to 02472 * compare as the smallest of size() and the length of a string 02473 * constructed from @a __s. The function then compares the two strings 02474 * by calling traits::compare(data(),s,rlen). If the result of the 02475 * comparison is nonzero returns it, otherwise the shorter one is 02476 * ordered first. 02477 */ 02478 int 02479 compare(const _CharT* __s) const; 02480 02481 // _GLIBCXX_RESOLVE_LIB_DEFECTS 02482 // 5 String::compare specification questionable 02483 /** 02484 * @brief Compare substring to a C string. 02485 * @param __pos Index of first character of substring. 02486 * @param __n1 Number of characters in substring. 02487 * @param __s C string to compare against. 02488 * @return Integer < 0, 0, or > 0. 02489 * 02490 * Form the substring of this string from the @a __n1 02491 * characters starting at @a pos. Returns an integer < 0 if 02492 * the substring is ordered before @a __s, 0 if their values 02493 * are equivalent, or > 0 if the substring is ordered after @a 02494 * __s. Determines the effective length rlen of the strings to 02495 * compare as the smallest of the length of the substring and 02496 * the length of a string constructed from @a __s. The 02497 * function then compares the two string by calling 02498 * traits::compare(substring.data(),__s,rlen). If the result of 02499 * the comparison is nonzero returns it, otherwise the shorter 02500 * one is ordered first. 02501 */ 02502 int 02503 compare(size_type __pos, size_type __n1, const _CharT* __s) const; 02504 02505 /** 02506 * @brief Compare substring against a character %array. 02507 * @param __pos Index of first character of substring. 02508 * @param __n1 Number of characters in substring. 02509 * @param __s character %array to compare against. 02510 * @param __n2 Number of characters of s. 02511 * @return Integer < 0, 0, or > 0. 02512 * 02513 * Form the substring of this string from the @a __n1 02514 * characters starting at @a __pos. Form a string from the 02515 * first @a __n2 characters of @a __s. Returns an integer < 0 02516 * if this substring is ordered before the string from @a __s, 02517 * 0 if their values are equivalent, or > 0 if this substring 02518 * is ordered after the string from @a __s. Determines the 02519 * effective length rlen of the strings to compare as the 02520 * smallest of the length of the substring and @a __n2. The 02521 * function then compares the two strings by calling 02522 * traits::compare(substring.data(),s,rlen). If the result of 02523 * the comparison is nonzero returns it, otherwise the shorter 02524 * one is ordered first. 02525 * 02526 * NB: s must have at least n2 characters, '\\0' has 02527 * no special meaning. 02528 */ 02529 int 02530 compare(size_type __pos, size_type __n1, const _CharT* __s, 02531 size_type __n2) const; 02532 02533 // Allow basic_stringbuf::__xfer_bufptrs to call _M_length: 02534 template<typename, typename, typename> friend class basic_stringbuf; 02535 }; 02536 _GLIBCXX_END_NAMESPACE_CXX11 02537 #else // !_GLIBCXX_USE_CXX11_ABI 02538 // Reference-counted COW string implentation 02539 02540 /** 02541 * @class basic_string basic_string.h <string> 02542 * @brief Managing sequences of characters and character-like objects. 02543 * 02544 * @ingroup strings 02545 * @ingroup sequences 02546 * 02547 * @tparam _CharT Type of character 02548 * @tparam _Traits Traits for character type, defaults to 02549 * char_traits<_CharT>. 02550 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>. 02551 * 02552 * Meets the requirements of a <a href="tables.html#65">container</a>, a 02553 * <a href="tables.html#66">reversible container</a>, and a 02554 * <a href="tables.html#67">sequence</a>. Of the 02555 * <a href="tables.html#68">optional sequence requirements</a>, only 02556 * @c push_back, @c at, and @c %array access are supported. 02557 * 02558 * @doctodo 02559 * 02560 * 02561 * Documentation? What's that? 02562 * Nathan Myers <ncm@cantrip.org>. 02563 * 02564 * A string looks like this: 02565 * 02566 * @code 02567 * [_Rep] 02568 * _M_length 02569 * [basic_string<char_type>] _M_capacity 02570 * _M_dataplus _M_refcount 02571 * _M_p ----------------> unnamed array of char_type 02572 * @endcode 02573 * 02574 * Where the _M_p points to the first character in the string, and 02575 * you cast it to a pointer-to-_Rep and subtract 1 to get a 02576 * pointer to the header. 02577 * 02578 * This approach has the enormous advantage that a string object 02579 * requires only one allocation. All the ugliness is confined 02580 * within a single %pair of inline functions, which each compile to 02581 * a single @a add instruction: _Rep::_M_data(), and 02582 * string::_M_rep(); and the allocation function which gets a 02583 * block of raw bytes and with room enough and constructs a _Rep 02584 * object at the front. 02585 * 02586 * The reason you want _M_data pointing to the character %array and 02587 * not the _Rep is so that the debugger can see the string 02588 * contents. (Probably we should add a non-inline member to get 02589 * the _Rep for the debugger to use, so users can check the actual 02590 * string length.) 02591 * 02592 * Note that the _Rep object is a POD so that you can have a 02593 * static <em>empty string</em> _Rep object already @a constructed before 02594 * static constructors have run. The reference-count encoding is 02595 * chosen so that a 0 indicates one reference, so you never try to 02596 * destroy the empty-string _Rep object. 02597 * 02598 * All but the last paragraph is considered pretty conventional 02599 * for a C++ string implementation. 02600 */ 02601 // 21.3 Template class basic_string 02602 template<typename _CharT, typename _Traits, typename _Alloc> 02603 class basic_string 02604 { 02605 typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type; 02606 02607 // Types: 02608 public: 02609 typedef _Traits traits_type; 02610 typedef typename _Traits::char_type value_type; 02611 typedef _Alloc allocator_type; 02612 typedef typename _CharT_alloc_type::size_type size_type; 02613 typedef typename _CharT_alloc_type::difference_type difference_type; 02614 typedef typename _CharT_alloc_type::reference reference; 02615 typedef typename _CharT_alloc_type::const_reference const_reference; 02616 typedef typename _CharT_alloc_type::pointer pointer; 02617 typedef typename _CharT_alloc_type::const_pointer const_pointer; 02618 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator; 02619 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string> 02620 const_iterator; 02621 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 02622 typedef std::reverse_iterator<iterator> reverse_iterator; 02623 02624 private: 02625 // _Rep: string representation 02626 // Invariants: 02627 // 1. String really contains _M_length + 1 characters: due to 21.3.4 02628 // must be kept null-terminated. 02629 // 2. _M_capacity >= _M_length 02630 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT). 02631 // 3. _M_refcount has three states: 02632 // -1: leaked, one reference, no ref-copies allowed, non-const. 02633 // 0: one reference, non-const. 02634 // n>0: n + 1 references, operations require a lock, const. 02635 // 4. All fields==0 is an empty string, given the extra storage 02636 // beyond-the-end for a null terminator; thus, the shared 02637 // empty string representation needs no constructor. 02638 02639 struct _Rep_base 02640 { 02641 size_type _M_length; 02642 size_type _M_capacity; 02643 _Atomic_word _M_refcount; 02644 }; 02645 02646 struct _Rep : _Rep_base 02647 { 02648 // Types: 02649 typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc; 02650 02651 // (Public) Data members: 02652 02653 // The maximum number of individual char_type elements of an 02654 // individual string is determined by _S_max_size. This is the 02655 // value that will be returned by max_size(). (Whereas npos 02656 // is the maximum number of bytes the allocator can allocate.) 02657 // If one was to divvy up the theoretical largest size string, 02658 // with a terminating character and m _CharT elements, it'd 02659 // look like this: 02660 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT) 02661 // Solving for m: 02662 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1 02663 // In addition, this implementation quarters this amount. 02664 static const size_type _S_max_size; 02665 static const _CharT _S_terminal; 02666 02667 // The following storage is init'd to 0 by the linker, resulting 02668 // (carefully) in an empty string with one reference. 02669 static size_type _S_empty_rep_storage[]; 02670 02671 static _Rep& 02672 _S_empty_rep() _GLIBCXX_NOEXCEPT 02673 { 02674 // NB: Mild hack to avoid strict-aliasing warnings. Note that 02675 // _S_empty_rep_storage is never modified and the punning should 02676 // be reasonably safe in this case. 02677 void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage); 02678 return *reinterpret_cast<_Rep*>(__p); 02679 } 02680 02681 bool 02682 _M_is_leaked() const _GLIBCXX_NOEXCEPT 02683 { return this->_M_refcount < 0; } 02684 02685 bool 02686 _M_is_shared() const _GLIBCXX_NOEXCEPT 02687 { return this->_M_refcount > 0; } 02688 02689 void 02690 _M_set_leaked() _GLIBCXX_NOEXCEPT 02691 { this->_M_refcount = -1; } 02692 02693 void 02694 _M_set_sharable() _GLIBCXX_NOEXCEPT 02695 { this->_M_refcount = 0; } 02696 02697 void 02698 _M_set_length_and_sharable(size_type __n) _GLIBCXX_NOEXCEPT 02699 { 02700 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 02701 if (__builtin_expect(this != &_S_empty_rep(), false)) 02702 #endif 02703 { 02704 this->_M_set_sharable(); // One reference. 02705 this->_M_length = __n; 02706 traits_type::assign(this->_M_refdata()[__n], _S_terminal); 02707 // grrr. (per 21.3.4) 02708 // You cannot leave those LWG people alone for a second. 02709 } 02710 } 02711 02712 _CharT* 02713 _M_refdata() throw() 02714 { return reinterpret_cast<_CharT*>(this + 1); } 02715 02716 _CharT* 02717 _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2) 02718 { 02719 return (!_M_is_leaked() && __alloc1 == __alloc2) 02720 ? _M_refcopy() : _M_clone(__alloc1); 02721 } 02722 02723 // Create & Destroy 02724 static _Rep* 02725 _S_create(size_type, size_type, const _Alloc&); 02726 02727 void 02728 _M_dispose(const _Alloc& __a) _GLIBCXX_NOEXCEPT 02729 { 02730 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 02731 if (__builtin_expect(this != &_S_empty_rep(), false)) 02732 #endif 02733 { 02734 // Be race-detector-friendly. For more info see bits/c++config. 02735 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount); 02736 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount, 02737 -1) <= 0) 02738 { 02739 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount); 02740 _M_destroy(__a); 02741 } 02742 } 02743 } // XXX MT 02744 02745 void 02746 _M_destroy(const _Alloc&) throw(); 02747 02748 _CharT* 02749 _M_refcopy() throw() 02750 { 02751 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 02752 if (__builtin_expect(this != &_S_empty_rep(), false)) 02753 #endif 02754 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1); 02755 return _M_refdata(); 02756 } // XXX MT 02757 02758 _CharT* 02759 _M_clone(const _Alloc&, size_type __res = 0); 02760 }; 02761 02762 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html 02763 struct _Alloc_hider : _Alloc 02764 { 02765 _Alloc_hider(_CharT* __dat, const _Alloc& __a) _GLIBCXX_NOEXCEPT 02766 : _Alloc(__a), _M_p(__dat) { } 02767 02768 _CharT* _M_p; // The actual data. 02769 }; 02770 02771 public: 02772 // Data Members (public): 02773 // NB: This is an unsigned type, and thus represents the maximum 02774 // size that the allocator can hold. 02775 /// Value returned by various member functions when they fail. 02776 static const size_type npos = static_cast<size_type>(-1); 02777 02778 private: 02779 // Data Members (private): 02780 mutable _Alloc_hider _M_dataplus; 02781 02782 _CharT* 02783 _M_data() const _GLIBCXX_NOEXCEPT 02784 { return _M_dataplus._M_p; } 02785 02786 _CharT* 02787 _M_data(_CharT* __p) _GLIBCXX_NOEXCEPT 02788 { return (_M_dataplus._M_p = __p); } 02789 02790 _Rep* 02791 _M_rep() const _GLIBCXX_NOEXCEPT 02792 { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); } 02793 02794 // For the internal use we have functions similar to `begin'/`end' 02795 // but they do not call _M_leak. 02796 iterator 02797 _M_ibegin() const _GLIBCXX_NOEXCEPT 02798 { return iterator(_M_data()); } 02799 02800 iterator 02801 _M_iend() const _GLIBCXX_NOEXCEPT 02802 { return iterator(_M_data() + this->size()); } 02803 02804 void 02805 _M_leak() // for use in begin() & non-const op[] 02806 { 02807 if (!_M_rep()->_M_is_leaked()) 02808 _M_leak_hard(); 02809 } 02810 02811 size_type 02812 _M_check(size_type __pos, const char* __s) const 02813 { 02814 if (__pos > this->size()) 02815 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > " 02816 "this->size() (which is %zu)"), 02817 __s, __pos, this->size()); 02818 return __pos; 02819 } 02820 02821 void 02822 _M_check_length(size_type __n1, size_type __n2, const char* __s) const 02823 { 02824 if (this->max_size() - (this->size() - __n1) < __n2) 02825 __throw_length_error(__N(__s)); 02826 } 02827 02828 // NB: _M_limit doesn't check for a bad __pos value. 02829 size_type 02830 _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT 02831 { 02832 const bool __testoff = __off < this->size() - __pos; 02833 return __testoff ? __off : this->size() - __pos; 02834 } 02835 02836 // True if _Rep and source do not overlap. 02837 bool 02838 _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT 02839 { 02840 return (less<const _CharT*>()(__s, _M_data()) 02841 || less<const _CharT*>()(_M_data() + this->size(), __s)); 02842 } 02843 02844 // When __n = 1 way faster than the general multichar 02845 // traits_type::copy/move/assign. 02846 static void 02847 _M_copy(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT 02848 { 02849 if (__n == 1) 02850 traits_type::assign(*__d, *__s); 02851 else 02852 traits_type::copy(__d, __s, __n); 02853 } 02854 02855 static void 02856 _M_move(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT 02857 { 02858 if (__n == 1) 02859 traits_type::assign(*__d, *__s); 02860 else 02861 traits_type::move(__d, __s, __n); 02862 } 02863 02864 static void 02865 _M_assign(_CharT* __d, size_type __n, _CharT __c) _GLIBCXX_NOEXCEPT 02866 { 02867 if (__n == 1) 02868 traits_type::assign(*__d, __c); 02869 else 02870 traits_type::assign(__d, __n, __c); 02871 } 02872 02873 // _S_copy_chars is a separate template to permit specialization 02874 // to optimize for the common case of pointers as iterators. 02875 template<class _Iterator> 02876 static void 02877 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2) 02878 { 02879 for (; __k1 != __k2; ++__k1, ++__p) 02880 traits_type::assign(*__p, *__k1); // These types are off. 02881 } 02882 02883 static void 02884 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT 02885 { _S_copy_chars(__p, __k1.base(), __k2.base()); } 02886 02887 static void 02888 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2) 02889 _GLIBCXX_NOEXCEPT 02890 { _S_copy_chars(__p, __k1.base(), __k2.base()); } 02891 02892 static void 02893 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT 02894 { _M_copy(__p, __k1, __k2 - __k1); } 02895 02896 static void 02897 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2) 02898 _GLIBCXX_NOEXCEPT 02899 { _M_copy(__p, __k1, __k2 - __k1); } 02900 02901 static int 02902 _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT 02903 { 02904 const difference_type __d = difference_type(__n1 - __n2); 02905 02906 if (__d > __gnu_cxx::__numeric_traits<int>::__max) 02907 return __gnu_cxx::__numeric_traits<int>::__max; 02908 else if (__d < __gnu_cxx::__numeric_traits<int>::__min) 02909 return __gnu_cxx::__numeric_traits<int>::__min; 02910 else 02911 return int(__d); 02912 } 02913 02914 void 02915 _M_mutate(size_type __pos, size_type __len1, size_type __len2); 02916 02917 void 02918 _M_leak_hard(); 02919 02920 static _Rep& 02921 _S_empty_rep() _GLIBCXX_NOEXCEPT 02922 { return _Rep::_S_empty_rep(); } 02923 02924 public: 02925 // Construct/copy/destroy: 02926 // NB: We overload ctors in some cases instead of using default 02927 // arguments, per 17.4.4.4 para. 2 item 2. 02928 02929 /** 02930 * @brief Default constructor creates an empty string. 02931 */ 02932 basic_string() 02933 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 02934 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { } 02935 #else 02936 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()){ } 02937 #endif 02938 02939 /** 02940 * @brief Construct an empty string using allocator @a a. 02941 */ 02942 explicit 02943 basic_string(const _Alloc& __a); 02944 02945 // NB: per LWG issue 42, semantics different from IS: 02946 /** 02947 * @brief Construct string with copy of value of @a str. 02948 * @param __str Source string. 02949 */ 02950 basic_string(const basic_string& __str); 02951 /** 02952 * @brief Construct string as copy of a substring. 02953 * @param __str Source string. 02954 * @param __pos Index of first character to copy from. 02955 * @param __n Number of characters to copy (default remainder). 02956 */ 02957 basic_string(const basic_string& __str, size_type __pos, 02958 size_type __n = npos); 02959 /** 02960 * @brief Construct string as copy of a substring. 02961 * @param __str Source string. 02962 * @param __pos Index of first character to copy from. 02963 * @param __n Number of characters to copy. 02964 * @param __a Allocator to use. 02965 */ 02966 basic_string(const basic_string& __str, size_type __pos, 02967 size_type __n, const _Alloc& __a); 02968 02969 /** 02970 * @brief Construct string initialized by a character %array. 02971 * @param __s Source character %array. 02972 * @param __n Number of characters to copy. 02973 * @param __a Allocator to use (default is default allocator). 02974 * 02975 * NB: @a __s must have at least @a __n characters, '\\0' 02976 * has no special meaning. 02977 */ 02978 basic_string(const _CharT* __s, size_type __n, 02979 const _Alloc& __a = _Alloc()); 02980 /** 02981 * @brief Construct string as copy of a C string. 02982 * @param __s Source C string. 02983 * @param __a Allocator to use (default is default allocator). 02984 */ 02985 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc()); 02986 /** 02987 * @brief Construct string as multiple characters. 02988 * @param __n Number of characters. 02989 * @param __c Character to use. 02990 * @param __a Allocator to use (default is default allocator). 02991 */ 02992 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc()); 02993 02994 #if __cplusplus >= 201103L 02995 /** 02996 * @brief Move construct string. 02997 * @param __str Source string. 02998 * 02999 * The newly-created string contains the exact contents of @a __str. 03000 * @a __str is a valid, but unspecified string. 03001 **/ 03002 basic_string(basic_string&& __str) 03003 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 03004 noexcept // FIXME C++11: should always be noexcept. 03005 #endif 03006 : _M_dataplus(__str._M_dataplus) 03007 { 03008 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 03009 __str._M_data(_S_empty_rep()._M_refdata()); 03010 #else 03011 __str._M_data(_S_construct(size_type(), _CharT(), get_allocator())); 03012 #endif 03013 } 03014 03015 /** 03016 * @brief Construct string from an initializer %list. 03017 * @param __l std::initializer_list of characters. 03018 * @param __a Allocator to use (default is default allocator). 03019 */ 03020 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc()); 03021 #endif // C++11 03022 03023 /** 03024 * @brief Construct string as copy of a range. 03025 * @param __beg Start of range. 03026 * @param __end End of range. 03027 * @param __a Allocator to use (default is default allocator). 03028 */ 03029 template<class _InputIterator> 03030 basic_string(_InputIterator __beg, _InputIterator __end, 03031 const _Alloc& __a = _Alloc()); 03032 03033 /** 03034 * @brief Destroy the string instance. 03035 */ 03036 ~basic_string() _GLIBCXX_NOEXCEPT 03037 { _M_rep()->_M_dispose(this->get_allocator()); } 03038 03039 /** 03040 * @brief Assign the value of @a str to this string. 03041 * @param __str Source string. 03042 */ 03043 basic_string& 03044 operator=(const basic_string& __str) 03045 { return this->assign(__str); } 03046 03047 /** 03048 * @brief Copy contents of @a s into this string. 03049 * @param __s Source null-terminated string. 03050 */ 03051 basic_string& 03052 operator=(const _CharT* __s) 03053 { return this->assign(__s); } 03054 03055 /** 03056 * @brief Set value to string of length 1. 03057 * @param __c Source character. 03058 * 03059 * Assigning to a character makes this string length 1 and 03060 * (*this)[0] == @a c. 03061 */ 03062 basic_string& 03063 operator=(_CharT __c) 03064 { 03065 this->assign(1, __c); 03066 return *this; 03067 } 03068 03069 #if __cplusplus >= 201103L 03070 /** 03071 * @brief Move assign the value of @a str to this string. 03072 * @param __str Source string. 03073 * 03074 * The contents of @a str are moved into this string (without copying). 03075 * @a str is a valid, but unspecified string. 03076 **/ 03077 // PR 58265, this should be noexcept. 03078 basic_string& 03079 operator=(basic_string&& __str) 03080 { 03081 // NB: DR 1204. 03082 this->swap(__str); 03083 return *this; 03084 } 03085 03086 /** 03087 * @brief Set value to string constructed from initializer %list. 03088 * @param __l std::initializer_list. 03089 */ 03090 basic_string& 03091 operator=(initializer_list<_CharT> __l) 03092 { 03093 this->assign(__l.begin(), __l.size()); 03094 return *this; 03095 } 03096 #endif // C++11 03097 03098 // Iterators: 03099 /** 03100 * Returns a read/write iterator that points to the first character in 03101 * the %string. Unshares the string. 03102 */ 03103 iterator 03104 begin() // FIXME C++11: should be noexcept. 03105 { 03106 _M_leak(); 03107 return iterator(_M_data()); 03108 } 03109 03110 /** 03111 * Returns a read-only (constant) iterator that points to the first 03112 * character in the %string. 03113 */ 03114 const_iterator 03115 begin() const _GLIBCXX_NOEXCEPT 03116 { return const_iterator(_M_data()); } 03117 03118 /** 03119 * Returns a read/write iterator that points one past the last 03120 * character in the %string. Unshares the string. 03121 */ 03122 iterator 03123 end() // FIXME C++11: should be noexcept. 03124 { 03125 _M_leak(); 03126 return iterator(_M_data() + this->size()); 03127 } 03128 03129 /** 03130 * Returns a read-only (constant) iterator that points one past the 03131 * last character in the %string. 03132 */ 03133 const_iterator 03134 end() const _GLIBCXX_NOEXCEPT 03135 { return const_iterator(_M_data() + this->size()); } 03136 03137 /** 03138 * Returns a read/write reverse iterator that points to the last 03139 * character in the %string. Iteration is done in reverse element 03140 * order. Unshares the string. 03141 */ 03142 reverse_iterator 03143 rbegin() // FIXME C++11: should be noexcept. 03144 { return reverse_iterator(this->end()); } 03145 03146 /** 03147 * Returns a read-only (constant) reverse iterator that points 03148 * to the last character in the %string. Iteration is done in 03149 * reverse element order. 03150 */ 03151 const_reverse_iterator 03152 rbegin() const _GLIBCXX_NOEXCEPT 03153 { return const_reverse_iterator(this->end()); } 03154 03155 /** 03156 * Returns a read/write reverse iterator that points to one before the 03157 * first character in the %string. Iteration is done in reverse 03158 * element order. Unshares the string. 03159 */ 03160 reverse_iterator 03161 rend() // FIXME C++11: should be noexcept. 03162 { return reverse_iterator(this->begin()); } 03163 03164 /** 03165 * Returns a read-only (constant) reverse iterator that points 03166 * to one before the first character in the %string. Iteration 03167 * is done in reverse element order. 03168 */ 03169 const_reverse_iterator 03170 rend() const _GLIBCXX_NOEXCEPT 03171 { return const_reverse_iterator(this->begin()); } 03172 03173 #if __cplusplus >= 201103L 03174 /** 03175 * Returns a read-only (constant) iterator that points to the first 03176 * character in the %string. 03177 */ 03178 const_iterator 03179 cbegin() const noexcept 03180 { return const_iterator(this->_M_data()); } 03181 03182 /** 03183 * Returns a read-only (constant) iterator that points one past the 03184 * last character in the %string. 03185 */ 03186 const_iterator 03187 cend() const noexcept 03188 { return const_iterator(this->_M_data() + this->size()); } 03189 03190 /** 03191 * Returns a read-only (constant) reverse iterator that points 03192 * to the last character in the %string. Iteration is done in 03193 * reverse element order. 03194 */ 03195 const_reverse_iterator 03196 crbegin() const noexcept 03197 { return const_reverse_iterator(this->end()); } 03198 03199 /** 03200 * Returns a read-only (constant) reverse iterator that points 03201 * to one before the first character in the %string. Iteration 03202 * is done in reverse element order. 03203 */ 03204 const_reverse_iterator 03205 crend() const noexcept 03206 { return const_reverse_iterator(this->begin()); } 03207 #endif 03208 03209 public: 03210 // Capacity: 03211 /// Returns the number of characters in the string, not including any 03212 /// null-termination. 03213 size_type 03214 size() const _GLIBCXX_NOEXCEPT 03215 { return _M_rep()->_M_length; } 03216 03217 /// Returns the number of characters in the string, not including any 03218 /// null-termination. 03219 size_type 03220 length() const _GLIBCXX_NOEXCEPT 03221 { return _M_rep()->_M_length; } 03222 03223 /// Returns the size() of the largest possible %string. 03224 size_type 03225 max_size() const _GLIBCXX_NOEXCEPT 03226 { return _Rep::_S_max_size; } 03227 03228 /** 03229 * @brief Resizes the %string to the specified number of characters. 03230 * @param __n Number of characters the %string should contain. 03231 * @param __c Character to fill any new elements. 03232 * 03233 * This function will %resize the %string to the specified 03234 * number of characters. If the number is smaller than the 03235 * %string's current size the %string is truncated, otherwise 03236 * the %string is extended and new elements are %set to @a __c. 03237 */ 03238 void 03239 resize(size_type __n, _CharT __c); 03240 03241 /** 03242 * @brief Resizes the %string to the specified number of characters. 03243 * @param __n Number of characters the %string should contain. 03244 * 03245 * This function will resize the %string to the specified length. If 03246 * the new size is smaller than the %string's current size the %string 03247 * is truncated, otherwise the %string is extended and new characters 03248 * are default-constructed. For basic types such as char, this means 03249 * setting them to 0. 03250 */ 03251 void 03252 resize(size_type __n) 03253 { this->resize(__n, _CharT()); } 03254 03255 #if __cplusplus >= 201103L 03256 /// A non-binding request to reduce capacity() to size(). 03257 void 03258 shrink_to_fit() _GLIBCXX_NOEXCEPT 03259 { 03260 #if __cpp_exceptions 03261 if (capacity() > size()) 03262 { 03263 try 03264 { reserve(0); } 03265 catch(...) 03266 { } 03267 } 03268 #endif 03269 } 03270 #endif 03271 03272 /** 03273 * Returns the total number of characters that the %string can hold 03274 * before needing to allocate more memory. 03275 */ 03276 size_type 03277 capacity() const _GLIBCXX_NOEXCEPT 03278 { return _M_rep()->_M_capacity; } 03279 03280 /** 03281 * @brief Attempt to preallocate enough memory for specified number of 03282 * characters. 03283 * @param __res_arg Number of characters required. 03284 * @throw std::length_error If @a __res_arg exceeds @c max_size(). 03285 * 03286 * This function attempts to reserve enough memory for the 03287 * %string to hold the specified number of characters. If the 03288 * number requested is more than max_size(), length_error is 03289 * thrown. 03290 * 03291 * The advantage of this function is that if optimal code is a 03292 * necessity and the user can determine the string length that will be 03293 * required, the user can reserve the memory in %advance, and thus 03294 * prevent a possible reallocation of memory and copying of %string 03295 * data. 03296 */ 03297 void 03298 reserve(size_type __res_arg = 0); 03299 03300 /** 03301 * Erases the string, making it empty. 03302 */ 03303 // PR 56166: this should not throw. 03304 void 03305 clear() 03306 { _M_mutate(0, this->size(), 0); } 03307 03308 /** 03309 * Returns true if the %string is empty. Equivalent to 03310 * <code>*this == ""</code>. 03311 */ 03312 bool 03313 empty() const _GLIBCXX_NOEXCEPT 03314 { return this->size() == 0; } 03315 03316 // Element access: 03317 /** 03318 * @brief Subscript access to the data contained in the %string. 03319 * @param __pos The index of the character to access. 03320 * @return Read-only (constant) reference to the character. 03321 * 03322 * This operator allows for easy, array-style, data access. 03323 * Note that data access with this operator is unchecked and 03324 * out_of_range lookups are not defined. (For checked lookups 03325 * see at().) 03326 */ 03327 const_reference 03328 operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT 03329 { 03330 _GLIBCXX_DEBUG_ASSERT(__pos <= size()); 03331 return _M_data()[__pos]; 03332 } 03333 03334 /** 03335 * @brief Subscript access to the data contained in the %string. 03336 * @param __pos The index of the character to access. 03337 * @return Read/write reference to the character. 03338 * 03339 * This operator allows for easy, array-style, data access. 03340 * Note that data access with this operator is unchecked and 03341 * out_of_range lookups are not defined. (For checked lookups 03342 * see at().) Unshares the string. 03343 */ 03344 reference 03345 operator[](size_type __pos) 03346 { 03347 // Allow pos == size() both in C++98 mode, as v3 extension, 03348 // and in C++11 mode. 03349 _GLIBCXX_DEBUG_ASSERT(__pos <= size()); 03350 // In pedantic mode be strict in C++98 mode. 03351 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size()); 03352 _M_leak(); 03353 return _M_data()[__pos]; 03354 } 03355 03356 /** 03357 * @brief Provides access to the data contained in the %string. 03358 * @param __n The index of the character to access. 03359 * @return Read-only (const) reference to the character. 03360 * @throw std::out_of_range If @a n is an invalid index. 03361 * 03362 * This function provides for safer data access. The parameter is 03363 * first checked that it is in the range of the string. The function 03364 * throws out_of_range if the check fails. 03365 */ 03366 const_reference 03367 at(size_type __n) const 03368 { 03369 if (__n >= this->size()) 03370 __throw_out_of_range_fmt(__N("basic_string::at: __n " 03371 "(which is %zu) >= this->size() " 03372 "(which is %zu)"), 03373 __n, this->size()); 03374 return _M_data()[__n]; 03375 } 03376 03377 /** 03378 * @brief Provides access to the data contained in the %string. 03379 * @param __n The index of the character to access. 03380 * @return Read/write reference to the character. 03381 * @throw std::out_of_range If @a n is an invalid index. 03382 * 03383 * This function provides for safer data access. The parameter is 03384 * first checked that it is in the range of the string. The function 03385 * throws out_of_range if the check fails. Success results in 03386 * unsharing the string. 03387 */ 03388 reference 03389 at(size_type __n) 03390 { 03391 if (__n >= size()) 03392 __throw_out_of_range_fmt(__N("basic_string::at: __n " 03393 "(which is %zu) >= this->size() " 03394 "(which is %zu)"), 03395 __n, this->size()); 03396 _M_leak(); 03397 return _M_data()[__n]; 03398 } 03399 03400 #if __cplusplus >= 201103L 03401 /** 03402 * Returns a read/write reference to the data at the first 03403 * element of the %string. 03404 */ 03405 reference 03406 front() 03407 { return operator[](0); } 03408 03409 /** 03410 * Returns a read-only (constant) reference to the data at the first 03411 * element of the %string. 03412 */ 03413 const_reference 03414 front() const _GLIBCXX_NOEXCEPT 03415 { return operator[](0); } 03416 03417 /** 03418 * Returns a read/write reference to the data at the last 03419 * element of the %string. 03420 */ 03421 reference 03422 back() 03423 { return operator[](this->size() - 1); } 03424 03425 /** 03426 * Returns a read-only (constant) reference to the data at the 03427 * last element of the %string. 03428 */ 03429 const_reference 03430 back() const _GLIBCXX_NOEXCEPT 03431 { return operator[](this->size() - 1); } 03432 #endif 03433 03434 // Modifiers: 03435 /** 03436 * @brief Append a string to this string. 03437 * @param __str The string to append. 03438 * @return Reference to this string. 03439 */ 03440 basic_string& 03441 operator+=(const basic_string& __str) 03442 { return this->append(__str); } 03443 03444 /** 03445 * @brief Append a C string. 03446 * @param __s The C string to append. 03447 * @return Reference to this string. 03448 */ 03449 basic_string& 03450 operator+=(const _CharT* __s) 03451 { return this->append(__s); } 03452 03453 /** 03454 * @brief Append a character. 03455 * @param __c The character to append. 03456 * @return Reference to this string. 03457 */ 03458 basic_string& 03459 operator+=(_CharT __c) 03460 { 03461 this->push_back(__c); 03462 return *this; 03463 } 03464 03465 #if __cplusplus >= 201103L 03466 /** 03467 * @brief Append an initializer_list of characters. 03468 * @param __l The initializer_list of characters to be appended. 03469 * @return Reference to this string. 03470 */ 03471 basic_string& 03472 operator+=(initializer_list<_CharT> __l) 03473 { return this->append(__l.begin(), __l.size()); } 03474 #endif // C++11 03475 03476 /** 03477 * @brief Append a string to this string. 03478 * @param __str The string to append. 03479 * @return Reference to this string. 03480 */ 03481 basic_string& 03482 append(const basic_string& __str); 03483 03484 /** 03485 * @brief Append a substring. 03486 * @param __str The string to append. 03487 * @param __pos Index of the first character of str to append. 03488 * @param __n The number of characters to append. 03489 * @return Reference to this string. 03490 * @throw std::out_of_range if @a __pos is not a valid index. 03491 * 03492 * This function appends @a __n characters from @a __str 03493 * starting at @a __pos to this string. If @a __n is is larger 03494 * than the number of available characters in @a __str, the 03495 * remainder of @a __str is appended. 03496 */ 03497 basic_string& 03498 append(const basic_string& __str, size_type __pos, size_type __n); 03499 03500 /** 03501 * @brief Append a C substring. 03502 * @param __s The C string to append. 03503 * @param __n The number of characters to append. 03504 * @return Reference to this string. 03505 */ 03506 basic_string& 03507 append(const _CharT* __s, size_type __n); 03508 03509 /** 03510 * @brief Append a C string. 03511 * @param __s The C string to append. 03512 * @return Reference to this string. 03513 */ 03514 basic_string& 03515 append(const _CharT* __s) 03516 { 03517 __glibcxx_requires_string(__s); 03518 return this->append(__s, traits_type::length(__s)); 03519 } 03520 03521 /** 03522 * @brief Append multiple characters. 03523 * @param __n The number of characters to append. 03524 * @param __c The character to use. 03525 * @return Reference to this string. 03526 * 03527 * Appends __n copies of __c to this string. 03528 */ 03529 basic_string& 03530 append(size_type __n, _CharT __c); 03531 03532 #if __cplusplus >= 201103L 03533 /** 03534 * @brief Append an initializer_list of characters. 03535 * @param __l The initializer_list of characters to append. 03536 * @return Reference to this string. 03537 */ 03538 basic_string& 03539 append(initializer_list<_CharT> __l) 03540 { return this->append(__l.begin(), __l.size()); } 03541 #endif // C++11 03542 03543 /** 03544 * @brief Append a range of characters. 03545 * @param __first Iterator referencing the first character to append. 03546 * @param __last Iterator marking the end of the range. 03547 * @return Reference to this string. 03548 * 03549 * Appends characters in the range [__first,__last) to this string. 03550 */ 03551 template<class _InputIterator> 03552 basic_string& 03553 append(_InputIterator __first, _InputIterator __last) 03554 { return this->replace(_M_iend(), _M_iend(), __first, __last); } 03555 03556 /** 03557 * @brief Append a single character. 03558 * @param __c Character to append. 03559 */ 03560 void 03561 push_back(_CharT __c) 03562 { 03563 const size_type __len = 1 + this->size(); 03564 if (__len > this->capacity() || _M_rep()->_M_is_shared()) 03565 this->reserve(__len); 03566 traits_type::assign(_M_data()[this->size()], __c); 03567 _M_rep()->_M_set_length_and_sharable(__len); 03568 } 03569 03570 /** 03571 * @brief Set value to contents of another string. 03572 * @param __str Source string to use. 03573 * @return Reference to this string. 03574 */ 03575 basic_string& 03576 assign(const basic_string& __str); 03577 03578 #if __cplusplus >= 201103L 03579 /** 03580 * @brief Set value to contents of another string. 03581 * @param __str Source string to use. 03582 * @return Reference to this string. 03583 * 03584 * This function sets this string to the exact contents of @a __str. 03585 * @a __str is a valid, but unspecified string. 03586 */ 03587 // PR 58265, this should be noexcept. 03588 basic_string& 03589 assign(basic_string&& __str) 03590 { 03591 this->swap(__str); 03592 return *this; 03593 } 03594 #endif // C++11 03595 03596 /** 03597 * @brief Set value to a substring of a string. 03598 * @param __str The string to use. 03599 * @param __pos Index of the first character of str. 03600 * @param __n Number of characters to use. 03601 * @return Reference to this string. 03602 * @throw std::out_of_range if @a pos is not a valid index. 03603 * 03604 * This function sets this string to the substring of @a __str 03605 * consisting of @a __n characters at @a __pos. If @a __n is 03606 * is larger than the number of available characters in @a 03607 * __str, the remainder of @a __str is used. 03608 */ 03609 basic_string& 03610 assign(const basic_string& __str, size_type __pos, size_type __n) 03611 { return this->assign(__str._M_data() 03612 + __str._M_check(__pos, "basic_string::assign"), 03613 __str._M_limit(__pos, __n)); } 03614 03615 /** 03616 * @brief Set value to a C substring. 03617 * @param __s The C string to use. 03618 * @param __n Number of characters to use. 03619 * @return Reference to this string. 03620 * 03621 * This function sets the value of this string to the first @a __n 03622 * characters of @a __s. If @a __n is is larger than the number of 03623 * available characters in @a __s, the remainder of @a __s is used. 03624 */ 03625 basic_string& 03626 assign(const _CharT* __s, size_type __n); 03627 03628 /** 03629 * @brief Set value to contents of a C string. 03630 * @param __s The C string to use. 03631 * @return Reference to this string. 03632 * 03633 * This function sets the value of this string to the value of @a __s. 03634 * The data is copied, so there is no dependence on @a __s once the 03635 * function returns. 03636 */ 03637 basic_string& 03638 assign(const _CharT* __s) 03639 { 03640 __glibcxx_requires_string(__s); 03641 return this->assign(__s, traits_type::length(__s)); 03642 } 03643 03644 /** 03645 * @brief Set value to multiple characters. 03646 * @param __n Length of the resulting string. 03647 * @param __c The character to use. 03648 * @return Reference to this string. 03649 * 03650 * This function sets the value of this string to @a __n copies of 03651 * character @a __c. 03652 */ 03653 basic_string& 03654 assign(size_type __n, _CharT __c) 03655 { return _M_replace_aux(size_type(0), this->size(), __n, __c); } 03656 03657 /** 03658 * @brief Set value to a range of characters. 03659 * @param __first Iterator referencing the first character to append. 03660 * @param __last Iterator marking the end of the range. 03661 * @return Reference to this string. 03662 * 03663 * Sets value of string to characters in the range [__first,__last). 03664 */ 03665 template<class _InputIterator> 03666 basic_string& 03667 assign(_InputIterator __first, _InputIterator __last) 03668 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); } 03669 03670 #if __cplusplus >= 201103L 03671 /** 03672 * @brief Set value to an initializer_list of characters. 03673 * @param __l The initializer_list of characters to assign. 03674 * @return Reference to this string. 03675 */ 03676 basic_string& 03677 assign(initializer_list<_CharT> __l) 03678 { return this->assign(__l.begin(), __l.size()); } 03679 #endif // C++11 03680 03681 /** 03682 * @brief Insert multiple characters. 03683 * @param __p Iterator referencing location in string to insert at. 03684 * @param __n Number of characters to insert 03685 * @param __c The character to insert. 03686 * @throw std::length_error If new length exceeds @c max_size(). 03687 * 03688 * Inserts @a __n copies of character @a __c starting at the 03689 * position referenced by iterator @a __p. If adding 03690 * characters causes the length to exceed max_size(), 03691 * length_error is thrown. The value of the string doesn't 03692 * change if an error is thrown. 03693 */ 03694 void 03695 insert(iterator __p, size_type __n, _CharT __c) 03696 { this->replace(__p, __p, __n, __c); } 03697 03698 /** 03699 * @brief Insert a range of characters. 03700 * @param __p Iterator referencing location in string to insert at. 03701 * @param __beg Start of range. 03702 * @param __end End of range. 03703 * @throw std::length_error If new length exceeds @c max_size(). 03704 * 03705 * Inserts characters in range [__beg,__end). If adding 03706 * characters causes the length to exceed max_size(), 03707 * length_error is thrown. The value of the string doesn't 03708 * change if an error is thrown. 03709 */ 03710 template<class _InputIterator> 03711 void 03712 insert(iterator __p, _InputIterator __beg, _InputIterator __end) 03713 { this->replace(__p, __p, __beg, __end); } 03714 03715 #if __cplusplus >= 201103L 03716 /** 03717 * @brief Insert an initializer_list of characters. 03718 * @param __p Iterator referencing location in string to insert at. 03719 * @param __l The initializer_list of characters to insert. 03720 * @throw std::length_error If new length exceeds @c max_size(). 03721 */ 03722 void 03723 insert(iterator __p, initializer_list<_CharT> __l) 03724 { 03725 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend()); 03726 this->insert(__p - _M_ibegin(), __l.begin(), __l.size()); 03727 } 03728 #endif // C++11 03729 03730 /** 03731 * @brief Insert value of a string. 03732 * @param __pos1 Iterator referencing location in string to insert at. 03733 * @param __str The string to insert. 03734 * @return Reference to this string. 03735 * @throw std::length_error If new length exceeds @c max_size(). 03736 * 03737 * Inserts value of @a __str starting at @a __pos1. If adding 03738 * characters causes the length to exceed max_size(), 03739 * length_error is thrown. The value of the string doesn't 03740 * change if an error is thrown. 03741 */ 03742 basic_string& 03743 insert(size_type __pos1, const basic_string& __str) 03744 { return this->insert(__pos1, __str, size_type(0), __str.size()); } 03745 03746 /** 03747 * @brief Insert a substring. 03748 * @param __pos1 Iterator referencing location in string to insert at. 03749 * @param __str The string to insert. 03750 * @param __pos2 Start of characters in str to insert. 03751 * @param __n Number of characters to insert. 03752 * @return Reference to this string. 03753 * @throw std::length_error If new length exceeds @c max_size(). 03754 * @throw std::out_of_range If @a pos1 > size() or 03755 * @a __pos2 > @a str.size(). 03756 * 03757 * Starting at @a pos1, insert @a __n character of @a __str 03758 * beginning with @a __pos2. If adding characters causes the 03759 * length to exceed max_size(), length_error is thrown. If @a 03760 * __pos1 is beyond the end of this string or @a __pos2 is 03761 * beyond the end of @a __str, out_of_range is thrown. The 03762 * value of the string doesn't change if an error is thrown. 03763 */ 03764 basic_string& 03765 insert(size_type __pos1, const basic_string& __str, 03766 size_type __pos2, size_type __n) 03767 { return this->insert(__pos1, __str._M_data() 03768 + __str._M_check(__pos2, "basic_string::insert"), 03769 __str._M_limit(__pos2, __n)); } 03770 03771 /** 03772 * @brief Insert a C substring. 03773 * @param __pos Iterator referencing location in string to insert at. 03774 * @param __s The C string to insert. 03775 * @param __n The number of characters to insert. 03776 * @return Reference to this string. 03777 * @throw std::length_error If new length exceeds @c max_size(). 03778 * @throw std::out_of_range If @a __pos is beyond the end of this 03779 * string. 03780 * 03781 * Inserts the first @a __n characters of @a __s starting at @a 03782 * __pos. If adding characters causes the length to exceed 03783 * max_size(), length_error is thrown. If @a __pos is beyond 03784 * end(), out_of_range is thrown. The value of the string 03785 * doesn't change if an error is thrown. 03786 */ 03787 basic_string& 03788 insert(size_type __pos, const _CharT* __s, size_type __n); 03789 03790 /** 03791 * @brief Insert a C string. 03792 * @param __pos Iterator referencing location in string to insert at. 03793 * @param __s The C string to insert. 03794 * @return Reference to this string. 03795 * @throw std::length_error If new length exceeds @c max_size(). 03796 * @throw std::out_of_range If @a pos is beyond the end of this 03797 * string. 03798 * 03799 * Inserts the first @a n characters of @a __s starting at @a __pos. If 03800 * adding characters causes the length to exceed max_size(), 03801 * length_error is thrown. If @a __pos is beyond end(), out_of_range is 03802 * thrown. The value of the string doesn't change if an error is 03803 * thrown. 03804 */ 03805 basic_string& 03806 insert(size_type __pos, const _CharT* __s) 03807 { 03808 __glibcxx_requires_string(__s); 03809 return this->insert(__pos, __s, traits_type::length(__s)); 03810 } 03811 03812 /** 03813 * @brief Insert multiple characters. 03814 * @param __pos Index in string to insert at. 03815 * @param __n Number of characters to insert 03816 * @param __c The character to insert. 03817 * @return Reference to this string. 03818 * @throw std::length_error If new length exceeds @c max_size(). 03819 * @throw std::out_of_range If @a __pos is beyond the end of this 03820 * string. 03821 * 03822 * Inserts @a __n copies of character @a __c starting at index 03823 * @a __pos. If adding characters causes the length to exceed 03824 * max_size(), length_error is thrown. If @a __pos > length(), 03825 * out_of_range is thrown. The value of the string doesn't 03826 * change if an error is thrown. 03827 */ 03828 basic_string& 03829 insert(size_type __pos, size_type __n, _CharT __c) 03830 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"), 03831 size_type(0), __n, __c); } 03832 03833 /** 03834 * @brief Insert one character. 03835 * @param __p Iterator referencing position in string to insert at. 03836 * @param __c The character to insert. 03837 * @return Iterator referencing newly inserted char. 03838 * @throw std::length_error If new length exceeds @c max_size(). 03839 * 03840 * Inserts character @a __c at position referenced by @a __p. 03841 * If adding character causes the length to exceed max_size(), 03842 * length_error is thrown. If @a __p is beyond end of string, 03843 * out_of_range is thrown. The value of the string doesn't 03844 * change if an error is thrown. 03845 */ 03846 iterator 03847 insert(iterator __p, _CharT __c) 03848 { 03849 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend()); 03850 const size_type __pos = __p - _M_ibegin(); 03851 _M_replace_aux(__pos, size_type(0), size_type(1), __c); 03852 _M_rep()->_M_set_leaked(); 03853 return iterator(_M_data() + __pos); 03854 } 03855 03856 /** 03857 * @brief Remove characters. 03858 * @param __pos Index of first character to remove (default 0). 03859 * @param __n Number of characters to remove (default remainder). 03860 * @return Reference to this string. 03861 * @throw std::out_of_range If @a pos is beyond the end of this 03862 * string. 03863 * 03864 * Removes @a __n characters from this string starting at @a 03865 * __pos. The length of the string is reduced by @a __n. If 03866 * there are < @a __n characters to remove, the remainder of 03867 * the string is truncated. If @a __p is beyond end of string, 03868 * out_of_range is thrown. The value of the string doesn't 03869 * change if an error is thrown. 03870 */ 03871 basic_string& 03872 erase(size_type __pos = 0, size_type __n = npos) 03873 { 03874 _M_mutate(_M_check(__pos, "basic_string::erase"), 03875 _M_limit(__pos, __n), size_type(0)); 03876 return *this; 03877 } 03878 03879 /** 03880 * @brief Remove one character. 03881 * @param __position Iterator referencing the character to remove. 03882 * @return iterator referencing same location after removal. 03883 * 03884 * Removes the character at @a __position from this string. The value 03885 * of the string doesn't change if an error is thrown. 03886 */ 03887 iterator 03888 erase(iterator __position) 03889 { 03890 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin() 03891 && __position < _M_iend()); 03892 const size_type __pos = __position - _M_ibegin(); 03893 _M_mutate(__pos, size_type(1), size_type(0)); 03894 _M_rep()->_M_set_leaked(); 03895 return iterator(_M_data() + __pos); 03896 } 03897 03898 /** 03899 * @brief Remove a range of characters. 03900 * @param __first Iterator referencing the first character to remove. 03901 * @param __last Iterator referencing the end of the range. 03902 * @return Iterator referencing location of first after removal. 03903 * 03904 * Removes the characters in the range [first,last) from this string. 03905 * The value of the string doesn't change if an error is thrown. 03906 */ 03907 iterator 03908 erase(iterator __first, iterator __last); 03909 03910 #if __cplusplus >= 201103L 03911 /** 03912 * @brief Remove the last character. 03913 * 03914 * The string must be non-empty. 03915 */ 03916 void 03917 pop_back() // FIXME C++11: should be noexcept. 03918 { erase(size()-1, 1); } 03919 #endif // C++11 03920 03921 /** 03922 * @brief Replace characters with value from another string. 03923 * @param __pos Index of first character to replace. 03924 * @param __n Number of characters to be replaced. 03925 * @param __str String to insert. 03926 * @return Reference to this string. 03927 * @throw std::out_of_range If @a pos is beyond the end of this 03928 * string. 03929 * @throw std::length_error If new length exceeds @c max_size(). 03930 * 03931 * Removes the characters in the range [__pos,__pos+__n) from 03932 * this string. In place, the value of @a __str is inserted. 03933 * If @a __pos is beyond end of string, out_of_range is thrown. 03934 * If the length of the result exceeds max_size(), length_error 03935 * is thrown. The value of the string doesn't change if an 03936 * error is thrown. 03937 */ 03938 basic_string& 03939 replace(size_type __pos, size_type __n, const basic_string& __str) 03940 { return this->replace(__pos, __n, __str._M_data(), __str.size()); } 03941 03942 /** 03943 * @brief Replace characters with value from another string. 03944 * @param __pos1 Index of first character to replace. 03945 * @param __n1 Number of characters to be replaced. 03946 * @param __str String to insert. 03947 * @param __pos2 Index of first character of str to use. 03948 * @param __n2 Number of characters from str to use. 03949 * @return Reference to this string. 03950 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 > 03951 * __str.size(). 03952 * @throw std::length_error If new length exceeds @c max_size(). 03953 * 03954 * Removes the characters in the range [__pos1,__pos1 + n) from this 03955 * string. In place, the value of @a __str is inserted. If @a __pos is 03956 * beyond end of string, out_of_range is thrown. If the length of the 03957 * result exceeds max_size(), length_error is thrown. The value of the 03958 * string doesn't change if an error is thrown. 03959 */ 03960 basic_string& 03961 replace(size_type __pos1, size_type __n1, const basic_string& __str, 03962 size_type __pos2, size_type __n2) 03963 { return this->replace(__pos1, __n1, __str._M_data() 03964 + __str._M_check(__pos2, "basic_string::replace"), 03965 __str._M_limit(__pos2, __n2)); } 03966 03967 /** 03968 * @brief Replace characters with value of a C substring. 03969 * @param __pos Index of first character to replace. 03970 * @param __n1 Number of characters to be replaced. 03971 * @param __s C string to insert. 03972 * @param __n2 Number of characters from @a s to use. 03973 * @return Reference to this string. 03974 * @throw std::out_of_range If @a pos1 > size(). 03975 * @throw std::length_error If new length exceeds @c max_size(). 03976 * 03977 * Removes the characters in the range [__pos,__pos + __n1) 03978 * from this string. In place, the first @a __n2 characters of 03979 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If 03980 * @a __pos is beyond end of string, out_of_range is thrown. If 03981 * the length of result exceeds max_size(), length_error is 03982 * thrown. The value of the string doesn't change if an error 03983 * is thrown. 03984 */ 03985 basic_string& 03986 replace(size_type __pos, size_type __n1, const _CharT* __s, 03987 size_type __n2); 03988 03989 /** 03990 * @brief Replace characters with value of a C string. 03991 * @param __pos Index of first character to replace. 03992 * @param __n1 Number of characters to be replaced. 03993 * @param __s C string to insert. 03994 * @return Reference to this string. 03995 * @throw std::out_of_range If @a pos > size(). 03996 * @throw std::length_error If new length exceeds @c max_size(). 03997 * 03998 * Removes the characters in the range [__pos,__pos + __n1) 03999 * from this string. In place, the characters of @a __s are 04000 * inserted. If @a __pos is beyond end of string, out_of_range 04001 * is thrown. If the length of result exceeds max_size(), 04002 * length_error is thrown. The value of the string doesn't 04003 * change if an error is thrown. 04004 */ 04005 basic_string& 04006 replace(size_type __pos, size_type __n1, const _CharT* __s) 04007 { 04008 __glibcxx_requires_string(__s); 04009 return this->replace(__pos, __n1, __s, traits_type::length(__s)); 04010 } 04011 04012 /** 04013 * @brief Replace characters with multiple characters. 04014 * @param __pos Index of first character to replace. 04015 * @param __n1 Number of characters to be replaced. 04016 * @param __n2 Number of characters to insert. 04017 * @param __c Character to insert. 04018 * @return Reference to this string. 04019 * @throw std::out_of_range If @a __pos > size(). 04020 * @throw std::length_error If new length exceeds @c max_size(). 04021 * 04022 * Removes the characters in the range [pos,pos + n1) from this 04023 * string. In place, @a __n2 copies of @a __c are inserted. 04024 * If @a __pos is beyond end of string, out_of_range is thrown. 04025 * If the length of result exceeds max_size(), length_error is 04026 * thrown. The value of the string doesn't change if an error 04027 * is thrown. 04028 */ 04029 basic_string& 04030 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c) 04031 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"), 04032 _M_limit(__pos, __n1), __n2, __c); } 04033 04034 /** 04035 * @brief Replace range of characters with string. 04036 * @param __i1 Iterator referencing start of range to replace. 04037 * @param __i2 Iterator referencing end of range to replace. 04038 * @param __str String value to insert. 04039 * @return Reference to this string. 04040 * @throw std::length_error If new length exceeds @c max_size(). 04041 * 04042 * Removes the characters in the range [__i1,__i2). In place, 04043 * the value of @a __str is inserted. If the length of result 04044 * exceeds max_size(), length_error is thrown. The value of 04045 * the string doesn't change if an error is thrown. 04046 */ 04047 basic_string& 04048 replace(iterator __i1, iterator __i2, const basic_string& __str) 04049 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); } 04050 04051 /** 04052 * @brief Replace range of characters with C substring. 04053 * @param __i1 Iterator referencing start of range to replace. 04054 * @param __i2 Iterator referencing end of range to replace. 04055 * @param __s C string value to insert. 04056 * @param __n Number of characters from s to insert. 04057 * @return Reference to this string. 04058 * @throw std::length_error If new length exceeds @c max_size(). 04059 * 04060 * Removes the characters in the range [__i1,__i2). In place, 04061 * the first @a __n characters of @a __s are inserted. If the 04062 * length of result exceeds max_size(), length_error is thrown. 04063 * The value of the string doesn't change if an error is 04064 * thrown. 04065 */ 04066 basic_string& 04067 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n) 04068 { 04069 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 04070 && __i2 <= _M_iend()); 04071 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n); 04072 } 04073 04074 /** 04075 * @brief Replace range of characters with C string. 04076 * @param __i1 Iterator referencing start of range to replace. 04077 * @param __i2 Iterator referencing end of range to replace. 04078 * @param __s C string value to insert. 04079 * @return Reference to this string. 04080 * @throw std::length_error If new length exceeds @c max_size(). 04081 * 04082 * Removes the characters in the range [__i1,__i2). In place, 04083 * the characters of @a __s are inserted. If the length of 04084 * result exceeds max_size(), length_error is thrown. The 04085 * value of the string doesn't change if an error is thrown. 04086 */ 04087 basic_string& 04088 replace(iterator __i1, iterator __i2, const _CharT* __s) 04089 { 04090 __glibcxx_requires_string(__s); 04091 return this->replace(__i1, __i2, __s, traits_type::length(__s)); 04092 } 04093 04094 /** 04095 * @brief Replace range of characters with multiple characters 04096 * @param __i1 Iterator referencing start of range to replace. 04097 * @param __i2 Iterator referencing end of range to replace. 04098 * @param __n Number of characters to insert. 04099 * @param __c Character to insert. 04100 * @return Reference to this string. 04101 * @throw std::length_error If new length exceeds @c max_size(). 04102 * 04103 * Removes the characters in the range [__i1,__i2). In place, 04104 * @a __n copies of @a __c are inserted. If the length of 04105 * result exceeds max_size(), length_error is thrown. The 04106 * value of the string doesn't change if an error is thrown. 04107 */ 04108 basic_string& 04109 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c) 04110 { 04111 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 04112 && __i2 <= _M_iend()); 04113 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c); 04114 } 04115 04116 /** 04117 * @brief Replace range of characters with range. 04118 * @param __i1 Iterator referencing start of range to replace. 04119 * @param __i2 Iterator referencing end of range to replace. 04120 * @param __k1 Iterator referencing start of range to insert. 04121 * @param __k2 Iterator referencing end of range to insert. 04122 * @return Reference to this string. 04123 * @throw std::length_error If new length exceeds @c max_size(). 04124 * 04125 * Removes the characters in the range [__i1,__i2). In place, 04126 * characters in the range [__k1,__k2) are inserted. If the 04127 * length of result exceeds max_size(), length_error is thrown. 04128 * The value of the string doesn't change if an error is 04129 * thrown. 04130 */ 04131 template<class _InputIterator> 04132 basic_string& 04133 replace(iterator __i1, iterator __i2, 04134 _InputIterator __k1, _InputIterator __k2) 04135 { 04136 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 04137 && __i2 <= _M_iend()); 04138 __glibcxx_requires_valid_range(__k1, __k2); 04139 typedef typename std::__is_integer<_InputIterator>::__type _Integral; 04140 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral()); 04141 } 04142 04143 // Specializations for the common case of pointer and iterator: 04144 // useful to avoid the overhead of temporary buffering in _M_replace. 04145 basic_string& 04146 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2) 04147 { 04148 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 04149 && __i2 <= _M_iend()); 04150 __glibcxx_requires_valid_range(__k1, __k2); 04151 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, 04152 __k1, __k2 - __k1); 04153 } 04154 04155 basic_string& 04156 replace(iterator __i1, iterator __i2, 04157 const _CharT* __k1, const _CharT* __k2) 04158 { 04159 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 04160 && __i2 <= _M_iend()); 04161 __glibcxx_requires_valid_range(__k1, __k2); 04162 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, 04163 __k1, __k2 - __k1); 04164 } 04165 04166 basic_string& 04167 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2) 04168 { 04169 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 04170 && __i2 <= _M_iend()); 04171 __glibcxx_requires_valid_range(__k1, __k2); 04172 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, 04173 __k1.base(), __k2 - __k1); 04174 } 04175 04176 basic_string& 04177 replace(iterator __i1, iterator __i2, 04178 const_iterator __k1, const_iterator __k2) 04179 { 04180 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2 04181 && __i2 <= _M_iend()); 04182 __glibcxx_requires_valid_range(__k1, __k2); 04183 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, 04184 __k1.base(), __k2 - __k1); 04185 } 04186 04187 #if __cplusplus >= 201103L 04188 /** 04189 * @brief Replace range of characters with initializer_list. 04190 * @param __i1 Iterator referencing start of range to replace. 04191 * @param __i2 Iterator referencing end of range to replace. 04192 * @param __l The initializer_list of characters to insert. 04193 * @return Reference to this string. 04194 * @throw std::length_error If new length exceeds @c max_size(). 04195 * 04196 * Removes the characters in the range [__i1,__i2). In place, 04197 * characters in the range [__k1,__k2) are inserted. If the 04198 * length of result exceeds max_size(), length_error is thrown. 04199 * The value of the string doesn't change if an error is 04200 * thrown. 04201 */ 04202 basic_string& replace(iterator __i1, iterator __i2, 04203 initializer_list<_CharT> __l) 04204 { return this->replace(__i1, __i2, __l.begin(), __l.end()); } 04205 #endif // C++11 04206 04207 private: 04208 template<class _Integer> 04209 basic_string& 04210 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n, 04211 _Integer __val, __true_type) 04212 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); } 04213 04214 template<class _InputIterator> 04215 basic_string& 04216 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1, 04217 _InputIterator __k2, __false_type); 04218 04219 basic_string& 04220 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2, 04221 _CharT __c); 04222 04223 basic_string& 04224 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s, 04225 size_type __n2); 04226 04227 // _S_construct_aux is used to implement the 21.3.1 para 15 which 04228 // requires special behaviour if _InIter is an integral type 04229 template<class _InIterator> 04230 static _CharT* 04231 _S_construct_aux(_InIterator __beg, _InIterator __end, 04232 const _Alloc& __a, __false_type) 04233 { 04234 typedef typename iterator_traits<_InIterator>::iterator_category _Tag; 04235 return _S_construct(__beg, __end, __a, _Tag()); 04236 } 04237 04238 // _GLIBCXX_RESOLVE_LIB_DEFECTS 04239 // 438. Ambiguity in the "do the right thing" clause 04240 template<class _Integer> 04241 static _CharT* 04242 _S_construct_aux(_Integer __beg, _Integer __end, 04243 const _Alloc& __a, __true_type) 04244 { return _S_construct_aux_2(static_cast<size_type>(__beg), 04245 __end, __a); } 04246 04247 static _CharT* 04248 _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a) 04249 { return _S_construct(__req, __c, __a); } 04250 04251 template<class _InIterator> 04252 static _CharT* 04253 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a) 04254 { 04255 typedef typename std::__is_integer<_InIterator>::__type _Integral; 04256 return _S_construct_aux(__beg, __end, __a, _Integral()); 04257 } 04258 04259 // For Input Iterators, used in istreambuf_iterators, etc. 04260 template<class _InIterator> 04261 static _CharT* 04262 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a, 04263 input_iterator_tag); 04264 04265 // For forward_iterators up to random_access_iterators, used for 04266 // string::iterator, _CharT*, etc. 04267 template<class _FwdIterator> 04268 static _CharT* 04269 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a, 04270 forward_iterator_tag); 04271 04272 static _CharT* 04273 _S_construct(size_type __req, _CharT __c, const _Alloc& __a); 04274 04275 public: 04276 04277 /** 04278 * @brief Copy substring into C string. 04279 * @param __s C string to copy value into. 04280 * @param __n Number of characters to copy. 04281 * @param __pos Index of first character to copy. 04282 * @return Number of characters actually copied 04283 * @throw std::out_of_range If __pos > size(). 04284 * 04285 * Copies up to @a __n characters starting at @a __pos into the 04286 * C string @a __s. If @a __pos is %greater than size(), 04287 * out_of_range is thrown. 04288 */ 04289 size_type 04290 copy(_CharT* __s, size_type __n, size_type __pos = 0) const; 04291 04292 /** 04293 * @brief Swap contents with another string. 04294 * @param __s String to swap with. 04295 * 04296 * Exchanges the contents of this string with that of @a __s in constant 04297 * time. 04298 */ 04299 // PR 58265, this should be noexcept. 04300 void 04301 swap(basic_string& __s); 04302 04303 // String operations: 04304 /** 04305 * @brief Return const pointer to null-terminated contents. 04306 * 04307 * This is a handle to internal data. Do not modify or dire things may 04308 * happen. 04309 */ 04310 const _CharT* 04311 c_str() const _GLIBCXX_NOEXCEPT 04312 { return _M_data(); } 04313 04314 /** 04315 * @brief Return const pointer to contents. 04316 * 04317 * This is a handle to internal data. Do not modify or dire things may 04318 * happen. 04319 */ 04320 const _CharT* 04321 data() const _GLIBCXX_NOEXCEPT 04322 { return _M_data(); } 04323 04324 /** 04325 * @brief Return copy of allocator used to construct this string. 04326 */ 04327 allocator_type 04328 get_allocator() const _GLIBCXX_NOEXCEPT 04329 { return _M_dataplus; } 04330 04331 /** 04332 * @brief Find position of a C substring. 04333 * @param __s C string to locate. 04334 * @param __pos Index of character to search from. 04335 * @param __n Number of characters from @a s to search for. 04336 * @return Index of start of first occurrence. 04337 * 04338 * Starting from @a __pos, searches forward for the first @a 04339 * __n characters in @a __s within this string. If found, 04340 * returns the index where it begins. If not found, returns 04341 * npos. 04342 */ 04343 size_type 04344 find(const _CharT* __s, size_type __pos, size_type __n) const; 04345 04346 /** 04347 * @brief Find position of a string. 04348 * @param __str String to locate. 04349 * @param __pos Index of character to search from (default 0). 04350 * @return Index of start of first occurrence. 04351 * 04352 * Starting from @a __pos, searches forward for value of @a __str within 04353 * this string. If found, returns the index where it begins. If not 04354 * found, returns npos. 04355 */ 04356 size_type 04357 find(const basic_string& __str, size_type __pos = 0) const 04358 _GLIBCXX_NOEXCEPT 04359 { return this->find(__str.data(), __pos, __str.size()); } 04360 04361 /** 04362 * @brief Find position of a C string. 04363 * @param __s C string to locate. 04364 * @param __pos Index of character to search from (default 0). 04365 * @return Index of start of first occurrence. 04366 * 04367 * Starting from @a __pos, searches forward for the value of @a 04368 * __s within this string. If found, returns the index where 04369 * it begins. If not found, returns npos. 04370 */ 04371 size_type 04372 find(const _CharT* __s, size_type __pos = 0) const 04373 { 04374 __glibcxx_requires_string(__s); 04375 return this->find(__s, __pos, traits_type::length(__s)); 04376 } 04377 04378 /** 04379 * @brief Find position of a character. 04380 * @param __c Character to locate. 04381 * @param __pos Index of character to search from (default 0). 04382 * @return Index of first occurrence. 04383 * 04384 * Starting from @a __pos, searches forward for @a __c within 04385 * this string. If found, returns the index where it was 04386 * found. If not found, returns npos. 04387 */ 04388 size_type 04389 find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT; 04390 04391 /** 04392 * @brief Find last position of a string. 04393 * @param __str String to locate. 04394 * @param __pos Index of character to search back from (default end). 04395 * @return Index of start of last occurrence. 04396 * 04397 * Starting from @a __pos, searches backward for value of @a 04398 * __str within this string. If found, returns the index where 04399 * it begins. If not found, returns npos. 04400 */ 04401 size_type 04402 rfind(const basic_string& __str, size_type __pos = npos) const 04403 _GLIBCXX_NOEXCEPT 04404 { return this->rfind(__str.data(), __pos, __str.size()); } 04405 04406 /** 04407 * @brief Find last position of a C substring. 04408 * @param __s C string to locate. 04409 * @param __pos Index of character to search back from. 04410 * @param __n Number of characters from s to search for. 04411 * @return Index of start of last occurrence. 04412 * 04413 * Starting from @a __pos, searches backward for the first @a 04414 * __n characters in @a __s within this string. If found, 04415 * returns the index where it begins. If not found, returns 04416 * npos. 04417 */ 04418 size_type 04419 rfind(const _CharT* __s, size_type __pos, size_type __n) const; 04420 04421 /** 04422 * @brief Find last position of a C string. 04423 * @param __s C string to locate. 04424 * @param __pos Index of character to start search at (default end). 04425 * @return Index of start of last occurrence. 04426 * 04427 * Starting from @a __pos, searches backward for the value of 04428 * @a __s within this string. If found, returns the index 04429 * where it begins. If not found, returns npos. 04430 */ 04431 size_type 04432 rfind(const _CharT* __s, size_type __pos = npos) const 04433 { 04434 __glibcxx_requires_string(__s); 04435 return this->rfind(__s, __pos, traits_type::length(__s)); 04436 } 04437 04438 /** 04439 * @brief Find last position of a character. 04440 * @param __c Character to locate. 04441 * @param __pos Index of character to search back from (default end). 04442 * @return Index of last occurrence. 04443 * 04444 * Starting from @a __pos, searches backward for @a __c within 04445 * this string. If found, returns the index where it was 04446 * found. If not found, returns npos. 04447 */ 04448 size_type 04449 rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT; 04450 04451 /** 04452 * @brief Find position of a character of string. 04453 * @param __str String containing characters to locate. 04454 * @param __pos Index of character to search from (default 0). 04455 * @return Index of first occurrence. 04456 * 04457 * Starting from @a __pos, searches forward for one of the 04458 * characters of @a __str within this string. If found, 04459 * returns the index where it was found. If not found, returns 04460 * npos. 04461 */ 04462 size_type 04463 find_first_of(const basic_string& __str, size_type __pos = 0) const 04464 _GLIBCXX_NOEXCEPT 04465 { return this->find_first_of(__str.data(), __pos, __str.size()); } 04466 04467 /** 04468 * @brief Find position of a character of C substring. 04469 * @param __s String containing characters to locate. 04470 * @param __pos Index of character to search from. 04471 * @param __n Number of characters from s to search for. 04472 * @return Index of first occurrence. 04473 * 04474 * Starting from @a __pos, searches forward for one of the 04475 * first @a __n characters of @a __s within this string. If 04476 * found, returns the index where it was found. If not found, 04477 * returns npos. 04478 */ 04479 size_type 04480 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const; 04481 04482 /** 04483 * @brief Find position of a character of C string. 04484 * @param __s String containing characters to locate. 04485 * @param __pos Index of character to search from (default 0). 04486 * @return Index of first occurrence. 04487 * 04488 * Starting from @a __pos, searches forward for one of the 04489 * characters of @a __s within this string. If found, returns 04490 * the index where it was found. If not found, returns npos. 04491 */ 04492 size_type 04493 find_first_of(const _CharT* __s, size_type __pos = 0) const 04494 { 04495 __glibcxx_requires_string(__s); 04496 return this->find_first_of(__s, __pos, traits_type::length(__s)); 04497 } 04498 04499 /** 04500 * @brief Find position of a character. 04501 * @param __c Character to locate. 04502 * @param __pos Index of character to search from (default 0). 04503 * @return Index of first occurrence. 04504 * 04505 * Starting from @a __pos, searches forward for the character 04506 * @a __c within this string. If found, returns the index 04507 * where it was found. If not found, returns npos. 04508 * 04509 * Note: equivalent to find(__c, __pos). 04510 */ 04511 size_type 04512 find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT 04513 { return this->find(__c, __pos); } 04514 04515 /** 04516 * @brief Find last position of a character of string. 04517 * @param __str String containing characters to locate. 04518 * @param __pos Index of character to search back from (default end). 04519 * @return Index of last occurrence. 04520 * 04521 * Starting from @a __pos, searches backward for one of the 04522 * characters of @a __str within this string. If found, 04523 * returns the index where it was found. If not found, returns 04524 * npos. 04525 */ 04526 size_type 04527 find_last_of(const basic_string& __str, size_type __pos = npos) const 04528 _GLIBCXX_NOEXCEPT 04529 { return this->find_last_of(__str.data(), __pos, __str.size()); } 04530 04531 /** 04532 * @brief Find last position of a character of C substring. 04533 * @param __s C string containing characters to locate. 04534 * @param __pos Index of character to search back from. 04535 * @param __n Number of characters from s to search for. 04536 * @return Index of last occurrence. 04537 * 04538 * Starting from @a __pos, searches backward for one of the 04539 * first @a __n characters of @a __s within this string. If 04540 * found, returns the index where it was found. If not found, 04541 * returns npos. 04542 */ 04543 size_type 04544 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const; 04545 04546 /** 04547 * @brief Find last position of a character of C string. 04548 * @param __s C string containing characters to locate. 04549 * @param __pos Index of character to search back from (default end). 04550 * @return Index of last occurrence. 04551 * 04552 * Starting from @a __pos, searches backward for one of the 04553 * characters of @a __s within this string. If found, returns 04554 * the index where it was found. If not found, returns npos. 04555 */ 04556 size_type 04557 find_last_of(const _CharT* __s, size_type __pos = npos) const 04558 { 04559 __glibcxx_requires_string(__s); 04560 return this->find_last_of(__s, __pos, traits_type::length(__s)); 04561 } 04562 04563 /** 04564 * @brief Find last position of a character. 04565 * @param __c Character to locate. 04566 * @param __pos Index of character to search back from (default end). 04567 * @return Index of last occurrence. 04568 * 04569 * Starting from @a __pos, searches backward for @a __c within 04570 * this string. If found, returns the index where it was 04571 * found. If not found, returns npos. 04572 * 04573 * Note: equivalent to rfind(__c, __pos). 04574 */ 04575 size_type 04576 find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT 04577 { return this->rfind(__c, __pos); } 04578 04579 /** 04580 * @brief Find position of a character not in string. 04581 * @param __str String containing characters to avoid. 04582 * @param __pos Index of character to search from (default 0). 04583 * @return Index of first occurrence. 04584 * 04585 * Starting from @a __pos, searches forward for a character not contained 04586 * in @a __str within this string. If found, returns the index where it 04587 * was found. If not found, returns npos. 04588 */ 04589 size_type 04590 find_first_not_of(const basic_string& __str, size_type __pos = 0) const 04591 _GLIBCXX_NOEXCEPT 04592 { return this->find_first_not_of(__str.data(), __pos, __str.size()); } 04593 04594 /** 04595 * @brief Find position of a character not in C substring. 04596 * @param __s C string containing characters to avoid. 04597 * @param __pos Index of character to search from. 04598 * @param __n Number of characters from __s to consider. 04599 * @return Index of first occurrence. 04600 * 04601 * Starting from @a __pos, searches forward for a character not 04602 * contained in the first @a __n characters of @a __s within 04603 * this string. If found, returns the index where it was 04604 * found. If not found, returns npos. 04605 */ 04606 size_type 04607 find_first_not_of(const _CharT* __s, size_type __pos, 04608 size_type __n) const; 04609 04610 /** 04611 * @brief Find position of a character not in C string. 04612 * @param __s C string containing characters to avoid. 04613 * @param __pos Index of character to search from (default 0). 04614 * @return Index of first occurrence. 04615 * 04616 * Starting from @a __pos, searches forward for a character not 04617 * contained in @a __s within this string. If found, returns 04618 * the index where it was found. If not found, returns npos. 04619 */ 04620 size_type 04621 find_first_not_of(const _CharT* __s, size_type __pos = 0) const 04622 { 04623 __glibcxx_requires_string(__s); 04624 return this->find_first_not_of(__s, __pos, traits_type::length(__s)); 04625 } 04626 04627 /** 04628 * @brief Find position of a different character. 04629 * @param __c Character to avoid. 04630 * @param __pos Index of character to search from (default 0). 04631 * @return Index of first occurrence. 04632 * 04633 * Starting from @a __pos, searches forward for a character 04634 * other than @a __c within this string. If found, returns the 04635 * index where it was found. If not found, returns npos. 04636 */ 04637 size_type 04638 find_first_not_of(_CharT __c, size_type __pos = 0) const 04639 _GLIBCXX_NOEXCEPT; 04640 04641 /** 04642 * @brief Find last position of a character not in string. 04643 * @param __str String containing characters to avoid. 04644 * @param __pos Index of character to search back from (default end). 04645 * @return Index of last occurrence. 04646 * 04647 * Starting from @a __pos, searches backward for a character 04648 * not contained in @a __str within this string. If found, 04649 * returns the index where it was found. If not found, returns 04650 * npos. 04651 */ 04652 size_type 04653 find_last_not_of(const basic_string& __str, size_type __pos = npos) const 04654 _GLIBCXX_NOEXCEPT 04655 { return this->find_last_not_of(__str.data(), __pos, __str.size()); } 04656 04657 /** 04658 * @brief Find last position of a character not in C substring. 04659 * @param __s C string containing characters to avoid. 04660 * @param __pos Index of character to search back from. 04661 * @param __n Number of characters from s to consider. 04662 * @return Index of last occurrence. 04663 * 04664 * Starting from @a __pos, searches backward for a character not 04665 * contained in the first @a __n characters of @a __s within this string. 04666 * If found, returns the index where it was found. If not found, 04667 * returns npos. 04668 */ 04669 size_type 04670 find_last_not_of(const _CharT* __s, size_type __pos, 04671 size_type __n) const; 04672 /** 04673 * @brief Find last position of a character not in C string. 04674 * @param __s C string containing characters to avoid. 04675 * @param __pos Index of character to search back from (default end). 04676 * @return Index of last occurrence. 04677 * 04678 * Starting from @a __pos, searches backward for a character 04679 * not contained in @a __s within this string. If found, 04680 * returns the index where it was found. If not found, returns 04681 * npos. 04682 */ 04683 size_type 04684 find_last_not_of(const _CharT* __s, size_type __pos = npos) const 04685 { 04686 __glibcxx_requires_string(__s); 04687 return this->find_last_not_of(__s, __pos, traits_type::length(__s)); 04688 } 04689 04690 /** 04691 * @brief Find last position of a different character. 04692 * @param __c Character to avoid. 04693 * @param __pos Index of character to search back from (default end). 04694 * @return Index of last occurrence. 04695 * 04696 * Starting from @a __pos, searches backward for a character other than 04697 * @a __c within this string. If found, returns the index where it was 04698 * found. If not found, returns npos. 04699 */ 04700 size_type 04701 find_last_not_of(_CharT __c, size_type __pos = npos) const 04702 _GLIBCXX_NOEXCEPT; 04703 04704 /** 04705 * @brief Get a substring. 04706 * @param __pos Index of first character (default 0). 04707 * @param __n Number of characters in substring (default remainder). 04708 * @return The new string. 04709 * @throw std::out_of_range If __pos > size(). 04710 * 04711 * Construct and return a new string using the @a __n 04712 * characters starting at @a __pos. If the string is too 04713 * short, use the remainder of the characters. If @a __pos is 04714 * beyond the end of the string, out_of_range is thrown. 04715 */ 04716 basic_string 04717 substr(size_type __pos = 0, size_type __n = npos) const 04718 { return basic_string(*this, 04719 _M_check(__pos, "basic_string::substr"), __n); } 04720 04721 /** 04722 * @brief Compare to a string. 04723 * @param __str String to compare against. 04724 * @return Integer < 0, 0, or > 0. 04725 * 04726 * Returns an integer < 0 if this string is ordered before @a 04727 * __str, 0 if their values are equivalent, or > 0 if this 04728 * string is ordered after @a __str. Determines the effective 04729 * length rlen of the strings to compare as the smallest of 04730 * size() and str.size(). The function then compares the two 04731 * strings by calling traits::compare(data(), str.data(),rlen). 04732 * If the result of the comparison is nonzero returns it, 04733 * otherwise the shorter one is ordered first. 04734 */ 04735 int 04736 compare(const basic_string& __str) const 04737 { 04738 const size_type __size = this->size(); 04739 const size_type __osize = __str.size(); 04740 const size_type __len = std::min(__size, __osize); 04741 04742 int __r = traits_type::compare(_M_data(), __str.data(), __len); 04743 if (!__r) 04744 __r = _S_compare(__size, __osize); 04745 return __r; 04746 } 04747 04748 /** 04749 * @brief Compare substring to a string. 04750 * @param __pos Index of first character of substring. 04751 * @param __n Number of characters in substring. 04752 * @param __str String to compare against. 04753 * @return Integer < 0, 0, or > 0. 04754 * 04755 * Form the substring of this string from the @a __n characters 04756 * starting at @a __pos. Returns an integer < 0 if the 04757 * substring is ordered before @a __str, 0 if their values are 04758 * equivalent, or > 0 if the substring is ordered after @a 04759 * __str. Determines the effective length rlen of the strings 04760 * to compare as the smallest of the length of the substring 04761 * and @a __str.size(). The function then compares the two 04762 * strings by calling 04763 * traits::compare(substring.data(),str.data(),rlen). If the 04764 * result of the comparison is nonzero returns it, otherwise 04765 * the shorter one is ordered first. 04766 */ 04767 int 04768 compare(size_type __pos, size_type __n, const basic_string& __str) const; 04769 04770 /** 04771 * @brief Compare substring to a substring. 04772 * @param __pos1 Index of first character of substring. 04773 * @param __n1 Number of characters in substring. 04774 * @param __str String to compare against. 04775 * @param __pos2 Index of first character of substring of str. 04776 * @param __n2 Number of characters in substring of str. 04777 * @return Integer < 0, 0, or > 0. 04778 * 04779 * Form the substring of this string from the @a __n1 04780 * characters starting at @a __pos1. Form the substring of @a 04781 * __str from the @a __n2 characters starting at @a __pos2. 04782 * Returns an integer < 0 if this substring is ordered before 04783 * the substring of @a __str, 0 if their values are equivalent, 04784 * or > 0 if this substring is ordered after the substring of 04785 * @a __str. Determines the effective length rlen of the 04786 * strings to compare as the smallest of the lengths of the 04787 * substrings. The function then compares the two strings by 04788 * calling 04789 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen). 04790 * If the result of the comparison is nonzero returns it, 04791 * otherwise the shorter one is ordered first. 04792 */ 04793 int 04794 compare(size_type __pos1, size_type __n1, const basic_string& __str, 04795 size_type __pos2, size_type __n2) const; 04796 04797 /** 04798 * @brief Compare to a C string. 04799 * @param __s C string to compare against. 04800 * @return Integer < 0, 0, or > 0. 04801 * 04802 * Returns an integer < 0 if this string is ordered before @a __s, 0 if 04803 * their values are equivalent, or > 0 if this string is ordered after 04804 * @a __s. Determines the effective length rlen of the strings to 04805 * compare as the smallest of size() and the length of a string 04806 * constructed from @a __s. The function then compares the two strings 04807 * by calling traits::compare(data(),s,rlen). If the result of the 04808 * comparison is nonzero returns it, otherwise the shorter one is 04809 * ordered first. 04810 */ 04811 int 04812 compare(const _CharT* __s) const; 04813 04814 // _GLIBCXX_RESOLVE_LIB_DEFECTS 04815 // 5 String::compare specification questionable 04816 /** 04817 * @brief Compare substring to a C string. 04818 * @param __pos Index of first character of substring. 04819 * @param __n1 Number of characters in substring. 04820 * @param __s C string to compare against. 04821 * @return Integer < 0, 0, or > 0. 04822 * 04823 * Form the substring of this string from the @a __n1 04824 * characters starting at @a pos. Returns an integer < 0 if 04825 * the substring is ordered before @a __s, 0 if their values 04826 * are equivalent, or > 0 if the substring is ordered after @a 04827 * __s. Determines the effective length rlen of the strings to 04828 * compare as the smallest of the length of the substring and 04829 * the length of a string constructed from @a __s. The 04830 * function then compares the two string by calling 04831 * traits::compare(substring.data(),__s,rlen). If the result of 04832 * the comparison is nonzero returns it, otherwise the shorter 04833 * one is ordered first. 04834 */ 04835 int 04836 compare(size_type __pos, size_type __n1, const _CharT* __s) const; 04837 04838 /** 04839 * @brief Compare substring against a character %array. 04840 * @param __pos Index of first character of substring. 04841 * @param __n1 Number of characters in substring. 04842 * @param __s character %array to compare against. 04843 * @param __n2 Number of characters of s. 04844 * @return Integer < 0, 0, or > 0. 04845 * 04846 * Form the substring of this string from the @a __n1 04847 * characters starting at @a __pos. Form a string from the 04848 * first @a __n2 characters of @a __s. Returns an integer < 0 04849 * if this substring is ordered before the string from @a __s, 04850 * 0 if their values are equivalent, or > 0 if this substring 04851 * is ordered after the string from @a __s. Determines the 04852 * effective length rlen of the strings to compare as the 04853 * smallest of the length of the substring and @a __n2. The 04854 * function then compares the two strings by calling 04855 * traits::compare(substring.data(),s,rlen). If the result of 04856 * the comparison is nonzero returns it, otherwise the shorter 04857 * one is ordered first. 04858 * 04859 * NB: s must have at least n2 characters, '\\0' has 04860 * no special meaning. 04861 */ 04862 int 04863 compare(size_type __pos, size_type __n1, const _CharT* __s, 04864 size_type __n2) const; 04865 }; 04866 #endif // !_GLIBCXX_USE_CXX11_ABI 04867 04868 // operator+ 04869 /** 04870 * @brief Concatenate two strings. 04871 * @param __lhs First string. 04872 * @param __rhs Last string. 04873 * @return New string with value of @a __lhs followed by @a __rhs. 04874 */ 04875 template<typename _CharT, typename _Traits, typename _Alloc> 04876 basic_string<_CharT, _Traits, _Alloc> 04877 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 04878 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 04879 { 04880 basic_string<_CharT, _Traits, _Alloc> __str(__lhs); 04881 __str.append(__rhs); 04882 return __str; 04883 } 04884 04885 /** 04886 * @brief Concatenate C string and string. 04887 * @param __lhs First string. 04888 * @param __rhs Last string. 04889 * @return New string with value of @a __lhs followed by @a __rhs. 04890 */ 04891 template<typename _CharT, typename _Traits, typename _Alloc> 04892 basic_string<_CharT,_Traits,_Alloc> 04893 operator+(const _CharT* __lhs, 04894 const basic_string<_CharT,_Traits,_Alloc>& __rhs); 04895 04896 /** 04897 * @brief Concatenate character and string. 04898 * @param __lhs First string. 04899 * @param __rhs Last string. 04900 * @return New string with @a __lhs followed by @a __rhs. 04901 */ 04902 template<typename _CharT, typename _Traits, typename _Alloc> 04903 basic_string<_CharT,_Traits,_Alloc> 04904 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs); 04905 04906 /** 04907 * @brief Concatenate string and C string. 04908 * @param __lhs First string. 04909 * @param __rhs Last string. 04910 * @return New string with @a __lhs followed by @a __rhs. 04911 */ 04912 template<typename _CharT, typename _Traits, typename _Alloc> 04913 inline basic_string<_CharT, _Traits, _Alloc> 04914 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 04915 const _CharT* __rhs) 04916 { 04917 basic_string<_CharT, _Traits, _Alloc> __str(__lhs); 04918 __str.append(__rhs); 04919 return __str; 04920 } 04921 04922 /** 04923 * @brief Concatenate string and character. 04924 * @param __lhs First string. 04925 * @param __rhs Last string. 04926 * @return New string with @a __lhs followed by @a __rhs. 04927 */ 04928 template<typename _CharT, typename _Traits, typename _Alloc> 04929 inline basic_string<_CharT, _Traits, _Alloc> 04930 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs) 04931 { 04932 typedef basic_string<_CharT, _Traits, _Alloc> __string_type; 04933 typedef typename __string_type::size_type __size_type; 04934 __string_type __str(__lhs); 04935 __str.append(__size_type(1), __rhs); 04936 return __str; 04937 } 04938 04939 #if __cplusplus >= 201103L 04940 template<typename _CharT, typename _Traits, typename _Alloc> 04941 inline basic_string<_CharT, _Traits, _Alloc> 04942 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, 04943 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 04944 { return std::move(__lhs.append(__rhs)); } 04945 04946 template<typename _CharT, typename _Traits, typename _Alloc> 04947 inline basic_string<_CharT, _Traits, _Alloc> 04948 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 04949 basic_string<_CharT, _Traits, _Alloc>&& __rhs) 04950 { return std::move(__rhs.insert(0, __lhs)); } 04951 04952 template<typename _CharT, typename _Traits, typename _Alloc> 04953 inline basic_string<_CharT, _Traits, _Alloc> 04954 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, 04955 basic_string<_CharT, _Traits, _Alloc>&& __rhs) 04956 { 04957 const auto __size = __lhs.size() + __rhs.size(); 04958 const bool __cond = (__size > __lhs.capacity() 04959 && __size <= __rhs.capacity()); 04960 return __cond ? std::move(__rhs.insert(0, __lhs)) 04961 : std::move(__lhs.append(__rhs)); 04962 } 04963 04964 template<typename _CharT, typename _Traits, typename _Alloc> 04965 inline basic_string<_CharT, _Traits, _Alloc> 04966 operator+(const _CharT* __lhs, 04967 basic_string<_CharT, _Traits, _Alloc>&& __rhs) 04968 { return std::move(__rhs.insert(0, __lhs)); } 04969 04970 template<typename _CharT, typename _Traits, typename _Alloc> 04971 inline basic_string<_CharT, _Traits, _Alloc> 04972 operator+(_CharT __lhs, 04973 basic_string<_CharT, _Traits, _Alloc>&& __rhs) 04974 { return std::move(__rhs.insert(0, 1, __lhs)); } 04975 04976 template<typename _CharT, typename _Traits, typename _Alloc> 04977 inline basic_string<_CharT, _Traits, _Alloc> 04978 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, 04979 const _CharT* __rhs) 04980 { return std::move(__lhs.append(__rhs)); } 04981 04982 template<typename _CharT, typename _Traits, typename _Alloc> 04983 inline basic_string<_CharT, _Traits, _Alloc> 04984 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, 04985 _CharT __rhs) 04986 { return std::move(__lhs.append(1, __rhs)); } 04987 #endif 04988 04989 // operator == 04990 /** 04991 * @brief Test equivalence of two strings. 04992 * @param __lhs First string. 04993 * @param __rhs Second string. 04994 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise. 04995 */ 04996 template<typename _CharT, typename _Traits, typename _Alloc> 04997 inline bool 04998 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 04999 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 05000 _GLIBCXX_NOEXCEPT 05001 { return __lhs.compare(__rhs) == 0; } 05002 05003 template<typename _CharT> 05004 inline 05005 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type 05006 operator==(const basic_string<_CharT>& __lhs, 05007 const basic_string<_CharT>& __rhs) _GLIBCXX_NOEXCEPT 05008 { return (__lhs.size() == __rhs.size() 05009 && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(), 05010 __lhs.size())); } 05011 05012 /** 05013 * @brief Test equivalence of C string and string. 05014 * @param __lhs C string. 05015 * @param __rhs String. 05016 * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise. 05017 */ 05018 template<typename _CharT, typename _Traits, typename _Alloc> 05019 inline bool 05020 operator==(const _CharT* __lhs, 05021 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 05022 { return __rhs.compare(__lhs) == 0; } 05023 05024 /** 05025 * @brief Test equivalence of string and C string. 05026 * @param __lhs String. 05027 * @param __rhs C string. 05028 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise. 05029 */ 05030 template<typename _CharT, typename _Traits, typename _Alloc> 05031 inline bool 05032 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 05033 const _CharT* __rhs) 05034 { return __lhs.compare(__rhs) == 0; } 05035 05036 // operator != 05037 /** 05038 * @brief Test difference of two strings. 05039 * @param __lhs First string. 05040 * @param __rhs Second string. 05041 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise. 05042 */ 05043 template<typename _CharT, typename _Traits, typename _Alloc> 05044 inline bool 05045 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 05046 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 05047 _GLIBCXX_NOEXCEPT 05048 { return !(__lhs == __rhs); } 05049 05050 /** 05051 * @brief Test difference of C string and string. 05052 * @param __lhs C string. 05053 * @param __rhs String. 05054 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise. 05055 */ 05056 template<typename _CharT, typename _Traits, typename _Alloc> 05057 inline bool 05058 operator!=(const _CharT* __lhs, 05059 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 05060 { return !(__lhs == __rhs); } 05061 05062 /** 05063 * @brief Test difference of string and C string. 05064 * @param __lhs String. 05065 * @param __rhs C string. 05066 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise. 05067 */ 05068 template<typename _CharT, typename _Traits, typename _Alloc> 05069 inline bool 05070 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 05071 const _CharT* __rhs) 05072 { return !(__lhs == __rhs); } 05073 05074 // operator < 05075 /** 05076 * @brief Test if string precedes string. 05077 * @param __lhs First string. 05078 * @param __rhs Second string. 05079 * @return True if @a __lhs precedes @a __rhs. False otherwise. 05080 */ 05081 template<typename _CharT, typename _Traits, typename _Alloc> 05082 inline bool 05083 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 05084 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 05085 _GLIBCXX_NOEXCEPT 05086 { return __lhs.compare(__rhs) < 0; } 05087 05088 /** 05089 * @brief Test if string precedes C string. 05090 * @param __lhs String. 05091 * @param __rhs C string. 05092 * @return True if @a __lhs precedes @a __rhs. False otherwise. 05093 */ 05094 template<typename _CharT, typename _Traits, typename _Alloc> 05095 inline bool 05096 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 05097 const _CharT* __rhs) 05098 { return __lhs.compare(__rhs) < 0; } 05099 05100 /** 05101 * @brief Test if C string precedes string. 05102 * @param __lhs C string. 05103 * @param __rhs String. 05104 * @return True if @a __lhs precedes @a __rhs. False otherwise. 05105 */ 05106 template<typename _CharT, typename _Traits, typename _Alloc> 05107 inline bool 05108 operator<(const _CharT* __lhs, 05109 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 05110 { return __rhs.compare(__lhs) > 0; } 05111 05112 // operator > 05113 /** 05114 * @brief Test if string follows string. 05115 * @param __lhs First string. 05116 * @param __rhs Second string. 05117 * @return True if @a __lhs follows @a __rhs. False otherwise. 05118 */ 05119 template<typename _CharT, typename _Traits, typename _Alloc> 05120 inline bool 05121 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 05122 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 05123 _GLIBCXX_NOEXCEPT 05124 { return __lhs.compare(__rhs) > 0; } 05125 05126 /** 05127 * @brief Test if string follows C string. 05128 * @param __lhs String. 05129 * @param __rhs C string. 05130 * @return True if @a __lhs follows @a __rhs. False otherwise. 05131 */ 05132 template<typename _CharT, typename _Traits, typename _Alloc> 05133 inline bool 05134 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 05135 const _CharT* __rhs) 05136 { return __lhs.compare(__rhs) > 0; } 05137 05138 /** 05139 * @brief Test if C string follows string. 05140 * @param __lhs C string. 05141 * @param __rhs String. 05142 * @return True if @a __lhs follows @a __rhs. False otherwise. 05143 */ 05144 template<typename _CharT, typename _Traits, typename _Alloc> 05145 inline bool 05146 operator>(const _CharT* __lhs, 05147 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 05148 { return __rhs.compare(__lhs) < 0; } 05149 05150 // operator <= 05151 /** 05152 * @brief Test if string doesn't follow string. 05153 * @param __lhs First string. 05154 * @param __rhs Second string. 05155 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise. 05156 */ 05157 template<typename _CharT, typename _Traits, typename _Alloc> 05158 inline bool 05159 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 05160 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 05161 _GLIBCXX_NOEXCEPT 05162 { return __lhs.compare(__rhs) <= 0; } 05163 05164 /** 05165 * @brief Test if string doesn't follow C string. 05166 * @param __lhs String. 05167 * @param __rhs C string. 05168 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise. 05169 */ 05170 template<typename _CharT, typename _Traits, typename _Alloc> 05171 inline bool 05172 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 05173 const _CharT* __rhs) 05174 { return __lhs.compare(__rhs) <= 0; } 05175 05176 /** 05177 * @brief Test if C string doesn't follow string. 05178 * @param __lhs C string. 05179 * @param __rhs String. 05180 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise. 05181 */ 05182 template<typename _CharT, typename _Traits, typename _Alloc> 05183 inline bool 05184 operator<=(const _CharT* __lhs, 05185 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 05186 { return __rhs.compare(__lhs) >= 0; } 05187 05188 // operator >= 05189 /** 05190 * @brief Test if string doesn't precede string. 05191 * @param __lhs First string. 05192 * @param __rhs Second string. 05193 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise. 05194 */ 05195 template<typename _CharT, typename _Traits, typename _Alloc> 05196 inline bool 05197 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 05198 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 05199 _GLIBCXX_NOEXCEPT 05200 { return __lhs.compare(__rhs) >= 0; } 05201 05202 /** 05203 * @brief Test if string doesn't precede C string. 05204 * @param __lhs String. 05205 * @param __rhs C string. 05206 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise. 05207 */ 05208 template<typename _CharT, typename _Traits, typename _Alloc> 05209 inline bool 05210 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, 05211 const _CharT* __rhs) 05212 { return __lhs.compare(__rhs) >= 0; } 05213 05214 /** 05215 * @brief Test if C string doesn't precede string. 05216 * @param __lhs C string. 05217 * @param __rhs String. 05218 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise. 05219 */ 05220 template<typename _CharT, typename _Traits, typename _Alloc> 05221 inline bool 05222 operator>=(const _CharT* __lhs, 05223 const basic_string<_CharT, _Traits, _Alloc>& __rhs) 05224 { return __rhs.compare(__lhs) <= 0; } 05225 05226 /** 05227 * @brief Swap contents of two strings. 05228 * @param __lhs First string. 05229 * @param __rhs Second string. 05230 * 05231 * Exchanges the contents of @a __lhs and @a __rhs in constant time. 05232 */ 05233 template<typename _CharT, typename _Traits, typename _Alloc> 05234 inline void 05235 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs, 05236 basic_string<_CharT, _Traits, _Alloc>& __rhs) 05237 #if __cplusplus >= 201103L 05238 noexcept(noexcept(__lhs.swap(__rhs))) 05239 #endif 05240 { __lhs.swap(__rhs); } 05241 05242 05243 /** 05244 * @brief Read stream into a string. 05245 * @param __is Input stream. 05246 * @param __str Buffer to store into. 05247 * @return Reference to the input stream. 05248 * 05249 * Stores characters from @a __is into @a __str until whitespace is 05250 * found, the end of the stream is encountered, or str.max_size() 05251 * is reached. If is.width() is non-zero, that is the limit on the 05252 * number of characters stored into @a __str. Any previous 05253 * contents of @a __str are erased. 05254 */ 05255 template<typename _CharT, typename _Traits, typename _Alloc> 05256 basic_istream<_CharT, _Traits>& 05257 operator>>(basic_istream<_CharT, _Traits>& __is, 05258 basic_string<_CharT, _Traits, _Alloc>& __str); 05259 05260 template<> 05261 basic_istream<char>& 05262 operator>>(basic_istream<char>& __is, basic_string<char>& __str); 05263 05264 /** 05265 * @brief Write string to a stream. 05266 * @param __os Output stream. 05267 * @param __str String to write out. 05268 * @return Reference to the output stream. 05269 * 05270 * Output characters of @a __str into os following the same rules as for 05271 * writing a C string. 05272 */ 05273 template<typename _CharT, typename _Traits, typename _Alloc> 05274 inline basic_ostream<_CharT, _Traits>& 05275 operator<<(basic_ostream<_CharT, _Traits>& __os, 05276 const basic_string<_CharT, _Traits, _Alloc>& __str) 05277 { 05278 // _GLIBCXX_RESOLVE_LIB_DEFECTS 05279 // 586. string inserter not a formatted function 05280 return __ostream_insert(__os, __str.data(), __str.size()); 05281 } 05282 05283 /** 05284 * @brief Read a line from stream into a string. 05285 * @param __is Input stream. 05286 * @param __str Buffer to store into. 05287 * @param __delim Character marking end of line. 05288 * @return Reference to the input stream. 05289 * 05290 * Stores characters from @a __is into @a __str until @a __delim is 05291 * found, the end of the stream is encountered, or str.max_size() 05292 * is reached. Any previous contents of @a __str are erased. If 05293 * @a __delim is encountered, it is extracted but not stored into 05294 * @a __str. 05295 */ 05296 template<typename _CharT, typename _Traits, typename _Alloc> 05297 basic_istream<_CharT, _Traits>& 05298 getline(basic_istream<_CharT, _Traits>& __is, 05299 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim); 05300 05301 /** 05302 * @brief Read a line from stream into a string. 05303 * @param __is Input stream. 05304 * @param __str Buffer to store into. 05305 * @return Reference to the input stream. 05306 * 05307 * Stores characters from is into @a __str until '\n' is 05308 * found, the end of the stream is encountered, or str.max_size() 05309 * is reached. Any previous contents of @a __str are erased. If 05310 * end of line is encountered, it is extracted but not stored into 05311 * @a __str. 05312 */ 05313 template<typename _CharT, typename _Traits, typename _Alloc> 05314 inline basic_istream<_CharT, _Traits>& 05315 getline(basic_istream<_CharT, _Traits>& __is, 05316 basic_string<_CharT, _Traits, _Alloc>& __str) 05317 { return std::getline(__is, __str, __is.widen('\n')); } 05318 05319 #if __cplusplus >= 201103L 05320 /// Read a line from an rvalue stream into a string. 05321 template<typename _CharT, typename _Traits, typename _Alloc> 05322 inline basic_istream<_CharT, _Traits>& 05323 getline(basic_istream<_CharT, _Traits>&& __is, 05324 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim) 05325 { return std::getline(__is, __str, __delim); } 05326 05327 /// Read a line from an rvalue stream into a string. 05328 template<typename _CharT, typename _Traits, typename _Alloc> 05329 inline basic_istream<_CharT, _Traits>& 05330 getline(basic_istream<_CharT, _Traits>&& __is, 05331 basic_string<_CharT, _Traits, _Alloc>& __str) 05332 { return std::getline(__is, __str); } 05333 #endif 05334 05335 template<> 05336 basic_istream<char>& 05337 getline(basic_istream<char>& __in, basic_string<char>& __str, 05338 char __delim); 05339 05340 #ifdef _GLIBCXX_USE_WCHAR_T 05341 template<> 05342 basic_istream<wchar_t>& 05343 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str, 05344 wchar_t __delim); 05345 #endif 05346 05347 _GLIBCXX_END_NAMESPACE_VERSION 05348 } // namespace 05349 05350 #if __cplusplus >= 201103L && defined(_GLIBCXX_USE_C99) 05351 05352 #include <ext/string_conversions.h> 05353 05354 namespace std _GLIBCXX_VISIBILITY(default) 05355 { 05356 _GLIBCXX_BEGIN_NAMESPACE_VERSION 05357 _GLIBCXX_BEGIN_NAMESPACE_CXX11 05358 05359 // 21.4 Numeric Conversions [string.conversions]. 05360 inline int 05361 stoi(const string& __str, size_t* __idx = 0, int __base = 10) 05362 { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(), 05363 __idx, __base); } 05364 05365 inline long 05366 stol(const string& __str, size_t* __idx = 0, int __base = 10) 05367 { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(), 05368 __idx, __base); } 05369 05370 inline unsigned long 05371 stoul(const string& __str, size_t* __idx = 0, int __base = 10) 05372 { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(), 05373 __idx, __base); } 05374 05375 inline long long 05376 stoll(const string& __str, size_t* __idx = 0, int __base = 10) 05377 { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(), 05378 __idx, __base); } 05379 05380 inline unsigned long long 05381 stoull(const string& __str, size_t* __idx = 0, int __base = 10) 05382 { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(), 05383 __idx, __base); } 05384 05385 // NB: strtof vs strtod. 05386 inline float 05387 stof(const string& __str, size_t* __idx = 0) 05388 { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); } 05389 05390 inline double 05391 stod(const string& __str, size_t* __idx = 0) 05392 { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); } 05393 05394 inline long double 05395 stold(const string& __str, size_t* __idx = 0) 05396 { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); } 05397 05398 // NB: (v)snprintf vs sprintf. 05399 05400 // DR 1261. 05401 inline string 05402 to_string(int __val) 05403 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(int), 05404 "%d", __val); } 05405 05406 inline string 05407 to_string(unsigned __val) 05408 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 05409 4 * sizeof(unsigned), 05410 "%u", __val); } 05411 05412 inline string 05413 to_string(long __val) 05414 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(long), 05415 "%ld", __val); } 05416 05417 inline string 05418 to_string(unsigned long __val) 05419 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 05420 4 * sizeof(unsigned long), 05421 "%lu", __val); } 05422 05423 inline string 05424 to_string(long long __val) 05425 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 05426 4 * sizeof(long long), 05427 "%lld", __val); } 05428 05429 inline string 05430 to_string(unsigned long long __val) 05431 { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 05432 4 * sizeof(unsigned long long), 05433 "%llu", __val); } 05434 05435 inline string 05436 to_string(float __val) 05437 { 05438 const int __n = 05439 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20; 05440 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n, 05441 "%f", __val); 05442 } 05443 05444 inline string 05445 to_string(double __val) 05446 { 05447 const int __n = 05448 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20; 05449 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n, 05450 "%f", __val); 05451 } 05452 05453 inline string 05454 to_string(long double __val) 05455 { 05456 const int __n = 05457 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20; 05458 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n, 05459 "%Lf", __val); 05460 } 05461 05462 #ifdef _GLIBCXX_USE_WCHAR_T 05463 inline int 05464 stoi(const wstring& __str, size_t* __idx = 0, int __base = 10) 05465 { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(), 05466 __idx, __base); } 05467 05468 inline long 05469 stol(const wstring& __str, size_t* __idx = 0, int __base = 10) 05470 { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(), 05471 __idx, __base); } 05472 05473 inline unsigned long 05474 stoul(const wstring& __str, size_t* __idx = 0, int __base = 10) 05475 { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(), 05476 __idx, __base); } 05477 05478 inline long long 05479 stoll(const wstring& __str, size_t* __idx = 0, int __base = 10) 05480 { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(), 05481 __idx, __base); } 05482 05483 inline unsigned long long 05484 stoull(const wstring& __str, size_t* __idx = 0, int __base = 10) 05485 { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(), 05486 __idx, __base); } 05487 05488 // NB: wcstof vs wcstod. 05489 inline float 05490 stof(const wstring& __str, size_t* __idx = 0) 05491 { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); } 05492 05493 inline double 05494 stod(const wstring& __str, size_t* __idx = 0) 05495 { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); } 05496 05497 inline long double 05498 stold(const wstring& __str, size_t* __idx = 0) 05499 { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); } 05500 05501 #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF 05502 // DR 1261. 05503 inline wstring 05504 to_wstring(int __val) 05505 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int), 05506 L"%d", __val); } 05507 05508 inline wstring 05509 to_wstring(unsigned __val) 05510 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 05511 4 * sizeof(unsigned), 05512 L"%u", __val); } 05513 05514 inline wstring 05515 to_wstring(long __val) 05516 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long), 05517 L"%ld", __val); } 05518 05519 inline wstring 05520 to_wstring(unsigned long __val) 05521 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 05522 4 * sizeof(unsigned long), 05523 L"%lu", __val); } 05524 05525 inline wstring 05526 to_wstring(long long __val) 05527 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 05528 4 * sizeof(long long), 05529 L"%lld", __val); } 05530 05531 inline wstring 05532 to_wstring(unsigned long long __val) 05533 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 05534 4 * sizeof(unsigned long long), 05535 L"%llu", __val); } 05536 05537 inline wstring 05538 to_wstring(float __val) 05539 { 05540 const int __n = 05541 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20; 05542 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n, 05543 L"%f", __val); 05544 } 05545 05546 inline wstring 05547 to_wstring(double __val) 05548 { 05549 const int __n = 05550 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20; 05551 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n, 05552 L"%f", __val); 05553 } 05554 05555 inline wstring 05556 to_wstring(long double __val) 05557 { 05558 const int __n = 05559 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20; 05560 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n, 05561 L"%Lf", __val); 05562 } 05563 #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF 05564 #endif 05565 05566 _GLIBCXX_END_NAMESPACE_CXX11 05567 _GLIBCXX_END_NAMESPACE_VERSION 05568 } // namespace 05569 05570 #endif /* C++11 && _GLIBCXX_USE_C99 ... */ 05571 05572 #if __cplusplus >= 201103L 05573 05574 #include <bits/functional_hash.h> 05575 05576 namespace std _GLIBCXX_VISIBILITY(default) 05577 { 05578 _GLIBCXX_BEGIN_NAMESPACE_VERSION 05579 05580 // DR 1182. 05581 05582 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X 05583 /// std::hash specialization for string. 05584 template<> 05585 struct hash<string> 05586 : public __hash_base<size_t, string> 05587 { 05588 size_t 05589 operator()(const string& __s) const noexcept 05590 { return std::_Hash_impl::hash(__s.data(), __s.length()); } 05591 }; 05592 05593 template<> 05594 struct __is_fast_hash<hash<string>> : std::false_type 05595 { }; 05596 05597 #ifdef _GLIBCXX_USE_WCHAR_T 05598 /// std::hash specialization for wstring. 05599 template<> 05600 struct hash<wstring> 05601 : public __hash_base<size_t, wstring> 05602 { 05603 size_t 05604 operator()(const wstring& __s) const noexcept 05605 { return std::_Hash_impl::hash(__s.data(), 05606 __s.length() * sizeof(wchar_t)); } 05607 }; 05608 05609 template<> 05610 struct __is_fast_hash<hash<wstring>> : std::false_type 05611 { }; 05612 #endif 05613 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */ 05614 05615 #ifdef _GLIBCXX_USE_C99_STDINT_TR1 05616 /// std::hash specialization for u16string. 05617 template<> 05618 struct hash<u16string> 05619 : public __hash_base<size_t, u16string> 05620 { 05621 size_t 05622 operator()(const u16string& __s) const noexcept 05623 { return std::_Hash_impl::hash(__s.data(), 05624 __s.length() * sizeof(char16_t)); } 05625 }; 05626 05627 template<> 05628 struct __is_fast_hash<hash<u16string>> : std::false_type 05629 { }; 05630 05631 /// std::hash specialization for u32string. 05632 template<> 05633 struct hash<u32string> 05634 : public __hash_base<size_t, u32string> 05635 { 05636 size_t 05637 operator()(const u32string& __s) const noexcept 05638 { return std::_Hash_impl::hash(__s.data(), 05639 __s.length() * sizeof(char32_t)); } 05640 }; 05641 05642 template<> 05643 struct __is_fast_hash<hash<u32string>> : std::false_type 05644 { }; 05645 #endif 05646 05647 _GLIBCXX_END_NAMESPACE_VERSION 05648 05649 #if __cplusplus > 201103L 05650 05651 #define __cpp_lib_string_udls 201304 05652 05653 inline namespace literals 05654 { 05655 inline namespace string_literals 05656 { 05657 _GLIBCXX_BEGIN_NAMESPACE_VERSION 05658 05659 _GLIBCXX_DEFAULT_ABI_TAG 05660 inline basic_string<char> 05661 operator""s(const char* __str, size_t __len) 05662 { return basic_string<char>{__str, __len}; } 05663 05664 #ifdef _GLIBCXX_USE_WCHAR_T 05665 _GLIBCXX_DEFAULT_ABI_TAG 05666 inline basic_string<wchar_t> 05667 operator""s(const wchar_t* __str, size_t __len) 05668 { return basic_string<wchar_t>{__str, __len}; } 05669 #endif 05670 05671 #ifdef _GLIBCXX_USE_C99_STDINT_TR1 05672 _GLIBCXX_DEFAULT_ABI_TAG 05673 inline basic_string<char16_t> 05674 operator""s(const char16_t* __str, size_t __len) 05675 { return basic_string<char16_t>{__str, __len}; } 05676 05677 _GLIBCXX_DEFAULT_ABI_TAG 05678 inline basic_string<char32_t> 05679 operator""s(const char32_t* __str, size_t __len) 05680 { return basic_string<char32_t>{__str, __len}; } 05681 #endif 05682 05683 _GLIBCXX_END_NAMESPACE_VERSION 05684 } // inline namespace string_literals 05685 } // inline namespace literals 05686 05687 #endif // __cplusplus > 201103L 05688 05689 } // namespace std 05690 05691 #endif // C++11 05692 05693 #endif /* _BASIC_STRING_H */