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