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