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