You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

6952 lines
244KB

  1. // Components for manipulating sequences of characters -*- C++ -*-
  2. // Copyright (C) 1997-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/basic_string.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{string}
  23. */
  24. //
  25. // ISO C++ 14882: 21 Strings library
  26. //
  27. #ifndef _BASIC_STRING_H
  28. #define _BASIC_STRING_H 1
  29. #pragma GCC system_header
  30. #include <ext/atomicity.h>
  31. #include <ext/alloc_traits.h>
  32. #include <debug/debug.h>
  33. #if __cplusplus >= 201103L
  34. #include <initializer_list>
  35. #endif
  36. #if __cplusplus >= 201703L
  37. # include <string_view>
  38. #endif
  39. namespace std _GLIBCXX_VISIBILITY(default)
  40. {
  41. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  42. #if _GLIBCXX_USE_CXX11_ABI
  43. _GLIBCXX_BEGIN_NAMESPACE_CXX11
  44. /**
  45. * @class basic_string basic_string.h <string>
  46. * @brief Managing sequences of characters and character-like objects.
  47. *
  48. * @ingroup strings
  49. * @ingroup sequences
  50. *
  51. * @tparam _CharT Type of character
  52. * @tparam _Traits Traits for character type, defaults to
  53. * char_traits<_CharT>.
  54. * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
  55. *
  56. * Meets the requirements of a <a href="tables.html#65">container</a>, a
  57. * <a href="tables.html#66">reversible container</a>, and a
  58. * <a href="tables.html#67">sequence</a>. Of the
  59. * <a href="tables.html#68">optional sequence requirements</a>, only
  60. * @c push_back, @c at, and @c %array access are supported.
  61. */
  62. template<typename _CharT, typename _Traits, typename _Alloc>
  63. class basic_string
  64. {
  65. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  66. rebind<_CharT>::other _Char_alloc_type;
  67. typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits;
  68. // Types:
  69. public:
  70. typedef _Traits traits_type;
  71. typedef typename _Traits::char_type value_type;
  72. typedef _Char_alloc_type allocator_type;
  73. typedef typename _Alloc_traits::size_type size_type;
  74. typedef typename _Alloc_traits::difference_type difference_type;
  75. typedef typename _Alloc_traits::reference reference;
  76. typedef typename _Alloc_traits::const_reference const_reference;
  77. typedef typename _Alloc_traits::pointer pointer;
  78. typedef typename _Alloc_traits::const_pointer const_pointer;
  79. typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
  80. typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
  81. const_iterator;
  82. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  83. typedef std::reverse_iterator<iterator> reverse_iterator;
  84. /// Value returned by various member functions when they fail.
  85. static const size_type npos = static_cast<size_type>(-1);
  86. protected:
  87. // type used for positions in insert, erase etc.
  88. #if __cplusplus < 201103L
  89. typedef iterator __const_iterator;
  90. #else
  91. typedef const_iterator __const_iterator;
  92. #endif
  93. private:
  94. #if __cplusplus >= 201703L
  95. // A helper type for avoiding boiler-plate.
  96. typedef basic_string_view<_CharT, _Traits> __sv_type;
  97. template<typename _Tp, typename _Res>
  98. using _If_sv = enable_if_t<
  99. __and_<is_convertible<const _Tp&, __sv_type>,
  100. __not_<is_convertible<const _Tp*, const basic_string*>>,
  101. __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
  102. _Res>;
  103. // Allows an implicit conversion to __sv_type.
  104. static __sv_type
  105. _S_to_string_view(__sv_type __svt) noexcept
  106. { return __svt; }
  107. // Wraps a string_view by explicit conversion and thus
  108. // allows to add an internal constructor that does not
  109. // participate in overload resolution when a string_view
  110. // is provided.
  111. struct __sv_wrapper
  112. {
  113. explicit __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
  114. __sv_type _M_sv;
  115. };
  116. /**
  117. * @brief Only internally used: Construct string from a string view
  118. * wrapper.
  119. * @param __svw string view wrapper.
  120. * @param __a Allocator to use.
  121. */
  122. explicit
  123. basic_string(__sv_wrapper __svw, const _Alloc& __a)
  124. : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
  125. #endif
  126. // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
  127. struct _Alloc_hider : allocator_type // TODO check __is_final
  128. {
  129. #if __cplusplus < 201103L
  130. _Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc())
  131. : allocator_type(__a), _M_p(__dat) { }
  132. #else
  133. _Alloc_hider(pointer __dat, const _Alloc& __a)
  134. : allocator_type(__a), _M_p(__dat) { }
  135. _Alloc_hider(pointer __dat, _Alloc&& __a = _Alloc())
  136. : allocator_type(std::move(__a)), _M_p(__dat) { }
  137. #endif
  138. pointer _M_p; // The actual data.
  139. };
  140. _Alloc_hider _M_dataplus;
  141. size_type _M_string_length;
  142. enum { _S_local_capacity = 15 / sizeof(_CharT) };
  143. union
  144. {
  145. _CharT _M_local_buf[_S_local_capacity + 1];
  146. size_type _M_allocated_capacity;
  147. };
  148. void
  149. _M_data(pointer __p)
  150. { _M_dataplus._M_p = __p; }
  151. void
  152. _M_length(size_type __length)
  153. { _M_string_length = __length; }
  154. pointer
  155. _M_data() const
  156. { return _M_dataplus._M_p; }
  157. pointer
  158. _M_local_data()
  159. {
  160. #if __cplusplus >= 201103L
  161. return std::pointer_traits<pointer>::pointer_to(*_M_local_buf);
  162. #else
  163. return pointer(_M_local_buf);
  164. #endif
  165. }
  166. const_pointer
  167. _M_local_data() const
  168. {
  169. #if __cplusplus >= 201103L
  170. return std::pointer_traits<const_pointer>::pointer_to(*_M_local_buf);
  171. #else
  172. return const_pointer(_M_local_buf);
  173. #endif
  174. }
  175. void
  176. _M_capacity(size_type __capacity)
  177. { _M_allocated_capacity = __capacity; }
  178. void
  179. _M_set_length(size_type __n)
  180. {
  181. _M_length(__n);
  182. traits_type::assign(_M_data()[__n], _CharT());
  183. }
  184. bool
  185. _M_is_local() const
  186. { return _M_data() == _M_local_data(); }
  187. // Create & Destroy
  188. pointer
  189. _M_create(size_type&, size_type);
  190. void
  191. _M_dispose()
  192. {
  193. if (!_M_is_local())
  194. _M_destroy(_M_allocated_capacity);
  195. }
  196. void
  197. _M_destroy(size_type __size) throw()
  198. { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size + 1); }
  199. // _M_construct_aux is used to implement the 21.3.1 para 15 which
  200. // requires special behaviour if _InIterator is an integral type
  201. template<typename _InIterator>
  202. void
  203. _M_construct_aux(_InIterator __beg, _InIterator __end,
  204. std::__false_type)
  205. {
  206. typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
  207. _M_construct(__beg, __end, _Tag());
  208. }
  209. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  210. // 438. Ambiguity in the "do the right thing" clause
  211. template<typename _Integer>
  212. void
  213. _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type)
  214. { _M_construct_aux_2(static_cast<size_type>(__beg), __end); }
  215. void
  216. _M_construct_aux_2(size_type __req, _CharT __c)
  217. { _M_construct(__req, __c); }
  218. template<typename _InIterator>
  219. void
  220. _M_construct(_InIterator __beg, _InIterator __end)
  221. {
  222. typedef typename std::__is_integer<_InIterator>::__type _Integral;
  223. _M_construct_aux(__beg, __end, _Integral());
  224. }
  225. // For Input Iterators, used in istreambuf_iterators, etc.
  226. template<typename _InIterator>
  227. void
  228. _M_construct(_InIterator __beg, _InIterator __end,
  229. std::input_iterator_tag);
  230. // For forward_iterators up to random_access_iterators, used for
  231. // string::iterator, _CharT*, etc.
  232. template<typename _FwdIterator>
  233. void
  234. _M_construct(_FwdIterator __beg, _FwdIterator __end,
  235. std::forward_iterator_tag);
  236. void
  237. _M_construct(size_type __req, _CharT __c);
  238. allocator_type&
  239. _M_get_allocator()
  240. { return _M_dataplus; }
  241. const allocator_type&
  242. _M_get_allocator() const
  243. { return _M_dataplus; }
  244. private:
  245. #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
  246. // The explicit instantiations in misc-inst.cc require this due to
  247. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063
  248. template<typename _Tp, bool _Requires =
  249. !__are_same<_Tp, _CharT*>::__value
  250. && !__are_same<_Tp, const _CharT*>::__value
  251. && !__are_same<_Tp, iterator>::__value
  252. && !__are_same<_Tp, const_iterator>::__value>
  253. struct __enable_if_not_native_iterator
  254. { typedef basic_string& __type; };
  255. template<typename _Tp>
  256. struct __enable_if_not_native_iterator<_Tp, false> { };
  257. #endif
  258. size_type
  259. _M_check(size_type __pos, const char* __s) const
  260. {
  261. if (__pos > this->size())
  262. __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
  263. "this->size() (which is %zu)"),
  264. __s, __pos, this->size());
  265. return __pos;
  266. }
  267. void
  268. _M_check_length(size_type __n1, size_type __n2, const char* __s) const
  269. {
  270. if (this->max_size() - (this->size() - __n1) < __n2)
  271. __throw_length_error(__N(__s));
  272. }
  273. // NB: _M_limit doesn't check for a bad __pos value.
  274. size_type
  275. _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
  276. {
  277. const bool __testoff = __off < this->size() - __pos;
  278. return __testoff ? __off : this->size() - __pos;
  279. }
  280. // True if _Rep and source do not overlap.
  281. bool
  282. _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
  283. {
  284. return (less<const _CharT*>()(__s, _M_data())
  285. || less<const _CharT*>()(_M_data() + this->size(), __s));
  286. }
  287. // When __n = 1 way faster than the general multichar
  288. // traits_type::copy/move/assign.
  289. static void
  290. _S_copy(_CharT* __d, const _CharT* __s, size_type __n)
  291. {
  292. if (__n == 1)
  293. traits_type::assign(*__d, *__s);
  294. else
  295. traits_type::copy(__d, __s, __n);
  296. }
  297. static void
  298. _S_move(_CharT* __d, const _CharT* __s, size_type __n)
  299. {
  300. if (__n == 1)
  301. traits_type::assign(*__d, *__s);
  302. else
  303. traits_type::move(__d, __s, __n);
  304. }
  305. static void
  306. _S_assign(_CharT* __d, size_type __n, _CharT __c)
  307. {
  308. if (__n == 1)
  309. traits_type::assign(*__d, __c);
  310. else
  311. traits_type::assign(__d, __n, __c);
  312. }
  313. // _S_copy_chars is a separate template to permit specialization
  314. // to optimize for the common case of pointers as iterators.
  315. template<class _Iterator>
  316. static void
  317. _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
  318. {
  319. for (; __k1 != __k2; ++__k1, (void)++__p)
  320. traits_type::assign(*__p, *__k1); // These types are off.
  321. }
  322. static void
  323. _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
  324. { _S_copy_chars(__p, __k1.base(), __k2.base()); }
  325. static void
  326. _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
  327. _GLIBCXX_NOEXCEPT
  328. { _S_copy_chars(__p, __k1.base(), __k2.base()); }
  329. static void
  330. _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
  331. { _S_copy(__p, __k1, __k2 - __k1); }
  332. static void
  333. _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
  334. _GLIBCXX_NOEXCEPT
  335. { _S_copy(__p, __k1, __k2 - __k1); }
  336. static int
  337. _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
  338. {
  339. const difference_type __d = difference_type(__n1 - __n2);
  340. if (__d > __gnu_cxx::__numeric_traits<int>::__max)
  341. return __gnu_cxx::__numeric_traits<int>::__max;
  342. else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
  343. return __gnu_cxx::__numeric_traits<int>::__min;
  344. else
  345. return int(__d);
  346. }
  347. void
  348. _M_assign(const basic_string&);
  349. void
  350. _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
  351. size_type __len2);
  352. void
  353. _M_erase(size_type __pos, size_type __n);
  354. public:
  355. // Construct/copy/destroy:
  356. // NB: We overload ctors in some cases instead of using default
  357. // arguments, per 17.4.4.4 para. 2 item 2.
  358. /**
  359. * @brief Default constructor creates an empty string.
  360. */
  361. basic_string()
  362. _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)
  363. : _M_dataplus(_M_local_data())
  364. { _M_set_length(0); }
  365. /**
  366. * @brief Construct an empty string using allocator @a a.
  367. */
  368. explicit
  369. basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT
  370. : _M_dataplus(_M_local_data(), __a)
  371. { _M_set_length(0); }
  372. /**
  373. * @brief Construct string with copy of value of @a __str.
  374. * @param __str Source string.
  375. */
  376. basic_string(const basic_string& __str)
  377. : _M_dataplus(_M_local_data(),
  378. _Alloc_traits::_S_select_on_copy(__str._M_get_allocator()))
  379. { _M_construct(__str._M_data(), __str._M_data() + __str.length()); }
  380. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  381. // 2583. no way to supply an allocator for basic_string(str, pos)
  382. /**
  383. * @brief Construct string as copy of a substring.
  384. * @param __str Source string.
  385. * @param __pos Index of first character to copy from.
  386. * @param __a Allocator to use.
  387. */
  388. basic_string(const basic_string& __str, size_type __pos,
  389. const _Alloc& __a = _Alloc())
  390. : _M_dataplus(_M_local_data(), __a)
  391. {
  392. const _CharT* __start = __str._M_data()
  393. + __str._M_check(__pos, "basic_string::basic_string");
  394. _M_construct(__start, __start + __str._M_limit(__pos, npos));
  395. }
  396. /**
  397. * @brief Construct string as copy of a substring.
  398. * @param __str Source string.
  399. * @param __pos Index of first character to copy from.
  400. * @param __n Number of characters to copy.
  401. */
  402. basic_string(const basic_string& __str, size_type __pos,
  403. size_type __n)
  404. : _M_dataplus(_M_local_data())
  405. {
  406. const _CharT* __start = __str._M_data()
  407. + __str._M_check(__pos, "basic_string::basic_string");
  408. _M_construct(__start, __start + __str._M_limit(__pos, __n));
  409. }
  410. /**
  411. * @brief Construct string as copy of a substring.
  412. * @param __str Source string.
  413. * @param __pos Index of first character to copy from.
  414. * @param __n Number of characters to copy.
  415. * @param __a Allocator to use.
  416. */
  417. basic_string(const basic_string& __str, size_type __pos,
  418. size_type __n, const _Alloc& __a)
  419. : _M_dataplus(_M_local_data(), __a)
  420. {
  421. const _CharT* __start
  422. = __str._M_data() + __str._M_check(__pos, "string::string");
  423. _M_construct(__start, __start + __str._M_limit(__pos, __n));
  424. }
  425. /**
  426. * @brief Construct string initialized by a character %array.
  427. * @param __s Source character %array.
  428. * @param __n Number of characters to copy.
  429. * @param __a Allocator to use (default is default allocator).
  430. *
  431. * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
  432. * has no special meaning.
  433. */
  434. basic_string(const _CharT* __s, size_type __n,
  435. const _Alloc& __a = _Alloc())
  436. : _M_dataplus(_M_local_data(), __a)
  437. { _M_construct(__s, __s + __n); }
  438. /**
  439. * @brief Construct string as copy of a C string.
  440. * @param __s Source C string.
  441. * @param __a Allocator to use (default is default allocator).
  442. */
  443. #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
  444. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  445. // 3076. basic_string CTAD ambiguity
  446. template<typename = _RequireAllocator<_Alloc>>
  447. #endif
  448. basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
  449. : _M_dataplus(_M_local_data(), __a)
  450. { _M_construct(__s, __s ? __s + traits_type::length(__s) : __s+npos); }
  451. /**
  452. * @brief Construct string as multiple characters.
  453. * @param __n Number of characters.
  454. * @param __c Character to use.
  455. * @param __a Allocator to use (default is default allocator).
  456. */
  457. #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
  458. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  459. // 3076. basic_string CTAD ambiguity
  460. template<typename = _RequireAllocator<_Alloc>>
  461. #endif
  462. basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
  463. : _M_dataplus(_M_local_data(), __a)
  464. { _M_construct(__n, __c); }
  465. #if __cplusplus >= 201103L
  466. /**
  467. * @brief Move construct string.
  468. * @param __str Source string.
  469. *
  470. * The newly-created string contains the exact contents of @a __str.
  471. * @a __str is a valid, but unspecified string.
  472. **/
  473. basic_string(basic_string&& __str) noexcept
  474. : _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator()))
  475. {
  476. if (__str._M_is_local())
  477. {
  478. traits_type::copy(_M_local_buf, __str._M_local_buf,
  479. _S_local_capacity + 1);
  480. }
  481. else
  482. {
  483. _M_data(__str._M_data());
  484. _M_capacity(__str._M_allocated_capacity);
  485. }
  486. // Must use _M_length() here not _M_set_length() because
  487. // basic_stringbuf relies on writing into unallocated capacity so
  488. // we mess up the contents if we put a '\0' in the string.
  489. _M_length(__str.length());
  490. __str._M_data(__str._M_local_data());
  491. __str._M_set_length(0);
  492. }
  493. /**
  494. * @brief Construct string from an initializer %list.
  495. * @param __l std::initializer_list of characters.
  496. * @param __a Allocator to use (default is default allocator).
  497. */
  498. basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
  499. : _M_dataplus(_M_local_data(), __a)
  500. { _M_construct(__l.begin(), __l.end()); }
  501. basic_string(const basic_string& __str, const _Alloc& __a)
  502. : _M_dataplus(_M_local_data(), __a)
  503. { _M_construct(__str.begin(), __str.end()); }
  504. basic_string(basic_string&& __str, const _Alloc& __a)
  505. noexcept(_Alloc_traits::_S_always_equal())
  506. : _M_dataplus(_M_local_data(), __a)
  507. {
  508. if (__str._M_is_local())
  509. {
  510. traits_type::copy(_M_local_buf, __str._M_local_buf,
  511. _S_local_capacity + 1);
  512. _M_length(__str.length());
  513. __str._M_set_length(0);
  514. }
  515. else if (_Alloc_traits::_S_always_equal()
  516. || __str.get_allocator() == __a)
  517. {
  518. _M_data(__str._M_data());
  519. _M_length(__str.length());
  520. _M_capacity(__str._M_allocated_capacity);
  521. __str._M_data(__str._M_local_buf);
  522. __str._M_set_length(0);
  523. }
  524. else
  525. _M_construct(__str.begin(), __str.end());
  526. }
  527. #endif // C++11
  528. /**
  529. * @brief Construct string as copy of a range.
  530. * @param __beg Start of range.
  531. * @param __end End of range.
  532. * @param __a Allocator to use (default is default allocator).
  533. */
  534. #if __cplusplus >= 201103L
  535. template<typename _InputIterator,
  536. typename = std::_RequireInputIter<_InputIterator>>
  537. #else
  538. template<typename _InputIterator>
  539. #endif
  540. basic_string(_InputIterator __beg, _InputIterator __end,
  541. const _Alloc& __a = _Alloc())
  542. : _M_dataplus(_M_local_data(), __a)
  543. { _M_construct(__beg, __end); }
  544. #if __cplusplus >= 201703L
  545. /**
  546. * @brief Construct string from a substring of a string_view.
  547. * @param __t Source object convertible to string view.
  548. * @param __pos The index of the first character to copy from __t.
  549. * @param __n The number of characters to copy from __t.
  550. * @param __a Allocator to use.
  551. */
  552. template<typename _Tp, typename = _If_sv<_Tp, void>>
  553. basic_string(const _Tp& __t, size_type __pos, size_type __n,
  554. const _Alloc& __a = _Alloc())
  555. : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
  556. /**
  557. * @brief Construct string from a string_view.
  558. * @param __t Source object convertible to string view.
  559. * @param __a Allocator to use (default is default allocator).
  560. */
  561. template<typename _Tp, typename = _If_sv<_Tp, void>>
  562. explicit
  563. basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
  564. : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
  565. #endif // C++17
  566. /**
  567. * @brief Destroy the string instance.
  568. */
  569. ~basic_string()
  570. { _M_dispose(); }
  571. /**
  572. * @brief Assign the value of @a str to this string.
  573. * @param __str Source string.
  574. */
  575. basic_string&
  576. operator=(const basic_string& __str)
  577. {
  578. return this->assign(__str);
  579. }
  580. /**
  581. * @brief Copy contents of @a s into this string.
  582. * @param __s Source null-terminated string.
  583. */
  584. basic_string&
  585. operator=(const _CharT* __s)
  586. { return this->assign(__s); }
  587. /**
  588. * @brief Set value to string of length 1.
  589. * @param __c Source character.
  590. *
  591. * Assigning to a character makes this string length 1 and
  592. * (*this)[0] == @a c.
  593. */
  594. basic_string&
  595. operator=(_CharT __c)
  596. {
  597. this->assign(1, __c);
  598. return *this;
  599. }
  600. #if __cplusplus >= 201103L
  601. /**
  602. * @brief Move assign the value of @a str to this string.
  603. * @param __str Source string.
  604. *
  605. * The contents of @a str are moved into this string (without copying).
  606. * @a str is a valid, but unspecified string.
  607. **/
  608. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  609. // 2063. Contradictory requirements for string move assignment
  610. basic_string&
  611. operator=(basic_string&& __str)
  612. noexcept(_Alloc_traits::_S_nothrow_move())
  613. {
  614. if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign()
  615. && !_Alloc_traits::_S_always_equal()
  616. && _M_get_allocator() != __str._M_get_allocator())
  617. {
  618. // Destroy existing storage before replacing allocator.
  619. _M_destroy(_M_allocated_capacity);
  620. _M_data(_M_local_data());
  621. _M_set_length(0);
  622. }
  623. // Replace allocator if POCMA is true.
  624. std::__alloc_on_move(_M_get_allocator(), __str._M_get_allocator());
  625. if (__str._M_is_local())
  626. {
  627. // We've always got room for a short string, just copy it.
  628. if (__str.size())
  629. this->_S_copy(_M_data(), __str._M_data(), __str.size());
  630. _M_set_length(__str.size());
  631. }
  632. else if (_Alloc_traits::_S_propagate_on_move_assign()
  633. || _Alloc_traits::_S_always_equal()
  634. || _M_get_allocator() == __str._M_get_allocator())
  635. {
  636. // Just move the allocated pointer, our allocator can free it.
  637. pointer __data = nullptr;
  638. size_type __capacity;
  639. if (!_M_is_local())
  640. {
  641. if (_Alloc_traits::_S_always_equal())
  642. {
  643. // __str can reuse our existing storage.
  644. __data = _M_data();
  645. __capacity = _M_allocated_capacity;
  646. }
  647. else // __str can't use it, so free it.
  648. _M_destroy(_M_allocated_capacity);
  649. }
  650. _M_data(__str._M_data());
  651. _M_length(__str.length());
  652. _M_capacity(__str._M_allocated_capacity);
  653. if (__data)
  654. {
  655. __str._M_data(__data);
  656. __str._M_capacity(__capacity);
  657. }
  658. else
  659. __str._M_data(__str._M_local_buf);
  660. }
  661. else // Need to do a deep copy
  662. assign(__str);
  663. __str.clear();
  664. return *this;
  665. }
  666. /**
  667. * @brief Set value to string constructed from initializer %list.
  668. * @param __l std::initializer_list.
  669. */
  670. basic_string&
  671. operator=(initializer_list<_CharT> __l)
  672. {
  673. this->assign(__l.begin(), __l.size());
  674. return *this;
  675. }
  676. #endif // C++11
  677. #if __cplusplus >= 201703L
  678. /**
  679. * @brief Set value to string constructed from a string_view.
  680. * @param __svt An object convertible to string_view.
  681. */
  682. template<typename _Tp>
  683. _If_sv<_Tp, basic_string&>
  684. operator=(const _Tp& __svt)
  685. { return this->assign(__svt); }
  686. /**
  687. * @brief Convert to a string_view.
  688. * @return A string_view.
  689. */
  690. operator __sv_type() const noexcept
  691. { return __sv_type(data(), size()); }
  692. #endif // C++17
  693. // Iterators:
  694. /**
  695. * Returns a read/write iterator that points to the first character in
  696. * the %string.
  697. */
  698. iterator
  699. begin() _GLIBCXX_NOEXCEPT
  700. { return iterator(_M_data()); }
  701. /**
  702. * Returns a read-only (constant) iterator that points to the first
  703. * character in the %string.
  704. */
  705. const_iterator
  706. begin() const _GLIBCXX_NOEXCEPT
  707. { return const_iterator(_M_data()); }
  708. /**
  709. * Returns a read/write iterator that points one past the last
  710. * character in the %string.
  711. */
  712. iterator
  713. end() _GLIBCXX_NOEXCEPT
  714. { return iterator(_M_data() + this->size()); }
  715. /**
  716. * Returns a read-only (constant) iterator that points one past the
  717. * last character in the %string.
  718. */
  719. const_iterator
  720. end() const _GLIBCXX_NOEXCEPT
  721. { return const_iterator(_M_data() + this->size()); }
  722. /**
  723. * Returns a read/write reverse iterator that points to the last
  724. * character in the %string. Iteration is done in reverse element
  725. * order.
  726. */
  727. reverse_iterator
  728. rbegin() _GLIBCXX_NOEXCEPT
  729. { return reverse_iterator(this->end()); }
  730. /**
  731. * Returns a read-only (constant) reverse iterator that points
  732. * to the last character in the %string. Iteration is done in
  733. * reverse element order.
  734. */
  735. const_reverse_iterator
  736. rbegin() const _GLIBCXX_NOEXCEPT
  737. { return const_reverse_iterator(this->end()); }
  738. /**
  739. * Returns a read/write reverse iterator that points to one before the
  740. * first character in the %string. Iteration is done in reverse
  741. * element order.
  742. */
  743. reverse_iterator
  744. rend() _GLIBCXX_NOEXCEPT
  745. { return reverse_iterator(this->begin()); }
  746. /**
  747. * Returns a read-only (constant) reverse iterator that points
  748. * to one before the first character in the %string. Iteration
  749. * is done in reverse element order.
  750. */
  751. const_reverse_iterator
  752. rend() const _GLIBCXX_NOEXCEPT
  753. { return const_reverse_iterator(this->begin()); }
  754. #if __cplusplus >= 201103L
  755. /**
  756. * Returns a read-only (constant) iterator that points to the first
  757. * character in the %string.
  758. */
  759. const_iterator
  760. cbegin() const noexcept
  761. { return const_iterator(this->_M_data()); }
  762. /**
  763. * Returns a read-only (constant) iterator that points one past the
  764. * last character in the %string.
  765. */
  766. const_iterator
  767. cend() const noexcept
  768. { return const_iterator(this->_M_data() + this->size()); }
  769. /**
  770. * Returns a read-only (constant) reverse iterator that points
  771. * to the last character in the %string. Iteration is done in
  772. * reverse element order.
  773. */
  774. const_reverse_iterator
  775. crbegin() const noexcept
  776. { return const_reverse_iterator(this->end()); }
  777. /**
  778. * Returns a read-only (constant) reverse iterator that points
  779. * to one before the first character in the %string. Iteration
  780. * is done in reverse element order.
  781. */
  782. const_reverse_iterator
  783. crend() const noexcept
  784. { return const_reverse_iterator(this->begin()); }
  785. #endif
  786. public:
  787. // Capacity:
  788. /// Returns the number of characters in the string, not including any
  789. /// null-termination.
  790. size_type
  791. size() const _GLIBCXX_NOEXCEPT
  792. { return _M_string_length; }
  793. /// Returns the number of characters in the string, not including any
  794. /// null-termination.
  795. size_type
  796. length() const _GLIBCXX_NOEXCEPT
  797. { return _M_string_length; }
  798. /// Returns the size() of the largest possible %string.
  799. size_type
  800. max_size() const _GLIBCXX_NOEXCEPT
  801. { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; }
  802. /**
  803. * @brief Resizes the %string to the specified number of characters.
  804. * @param __n Number of characters the %string should contain.
  805. * @param __c Character to fill any new elements.
  806. *
  807. * This function will %resize the %string to the specified
  808. * number of characters. If the number is smaller than the
  809. * %string's current size the %string is truncated, otherwise
  810. * the %string is extended and new elements are %set to @a __c.
  811. */
  812. void
  813. resize(size_type __n, _CharT __c);
  814. /**
  815. * @brief Resizes the %string to the specified number of characters.
  816. * @param __n Number of characters the %string should contain.
  817. *
  818. * This function will resize the %string to the specified length. If
  819. * the new size is smaller than the %string's current size the %string
  820. * is truncated, otherwise the %string is extended and new characters
  821. * are default-constructed. For basic types such as char, this means
  822. * setting them to 0.
  823. */
  824. void
  825. resize(size_type __n)
  826. { this->resize(__n, _CharT()); }
  827. #if __cplusplus >= 201103L
  828. /// A non-binding request to reduce capacity() to size().
  829. void
  830. shrink_to_fit() noexcept
  831. {
  832. #if __cpp_exceptions
  833. if (capacity() > size())
  834. {
  835. try
  836. { reserve(0); }
  837. catch(...)
  838. { }
  839. }
  840. #endif
  841. }
  842. #endif
  843. /**
  844. * Returns the total number of characters that the %string can hold
  845. * before needing to allocate more memory.
  846. */
  847. size_type
  848. capacity() const _GLIBCXX_NOEXCEPT
  849. {
  850. return _M_is_local() ? size_type(_S_local_capacity)
  851. : _M_allocated_capacity;
  852. }
  853. /**
  854. * @brief Attempt to preallocate enough memory for specified number of
  855. * characters.
  856. * @param __res_arg Number of characters required.
  857. * @throw std::length_error If @a __res_arg exceeds @c max_size().
  858. *
  859. * This function attempts to reserve enough memory for the
  860. * %string to hold the specified number of characters. If the
  861. * number requested is more than max_size(), length_error is
  862. * thrown.
  863. *
  864. * The advantage of this function is that if optimal code is a
  865. * necessity and the user can determine the string length that will be
  866. * required, the user can reserve the memory in %advance, and thus
  867. * prevent a possible reallocation of memory and copying of %string
  868. * data.
  869. */
  870. void
  871. reserve(size_type __res_arg = 0);
  872. /**
  873. * Erases the string, making it empty.
  874. */
  875. void
  876. clear() _GLIBCXX_NOEXCEPT
  877. { _M_set_length(0); }
  878. /**
  879. * Returns true if the %string is empty. Equivalent to
  880. * <code>*this == ""</code>.
  881. */
  882. _GLIBCXX_NODISCARD bool
  883. empty() const _GLIBCXX_NOEXCEPT
  884. { return this->size() == 0; }
  885. // Element access:
  886. /**
  887. * @brief Subscript access to the data contained in the %string.
  888. * @param __pos The index of the character to access.
  889. * @return Read-only (constant) reference to the character.
  890. *
  891. * This operator allows for easy, array-style, data access.
  892. * Note that data access with this operator is unchecked and
  893. * out_of_range lookups are not defined. (For checked lookups
  894. * see at().)
  895. */
  896. const_reference
  897. operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
  898. {
  899. __glibcxx_assert(__pos <= size());
  900. return _M_data()[__pos];
  901. }
  902. /**
  903. * @brief Subscript access to the data contained in the %string.
  904. * @param __pos The index of the character to access.
  905. * @return Read/write reference to the character.
  906. *
  907. * This operator allows for easy, array-style, data access.
  908. * Note that data access with this operator is unchecked and
  909. * out_of_range lookups are not defined. (For checked lookups
  910. * see at().)
  911. */
  912. reference
  913. operator[](size_type __pos)
  914. {
  915. // Allow pos == size() both in C++98 mode, as v3 extension,
  916. // and in C++11 mode.
  917. __glibcxx_assert(__pos <= size());
  918. // In pedantic mode be strict in C++98 mode.
  919. _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
  920. return _M_data()[__pos];
  921. }
  922. /**
  923. * @brief Provides access to the data contained in the %string.
  924. * @param __n The index of the character to access.
  925. * @return Read-only (const) reference to the character.
  926. * @throw std::out_of_range If @a n is an invalid index.
  927. *
  928. * This function provides for safer data access. The parameter is
  929. * first checked that it is in the range of the string. The function
  930. * throws out_of_range if the check fails.
  931. */
  932. const_reference
  933. at(size_type __n) const
  934. {
  935. if (__n >= this->size())
  936. __throw_out_of_range_fmt(__N("basic_string::at: __n "
  937. "(which is %zu) >= this->size() "
  938. "(which is %zu)"),
  939. __n, this->size());
  940. return _M_data()[__n];
  941. }
  942. /**
  943. * @brief Provides access to the data contained in the %string.
  944. * @param __n The index of the character to access.
  945. * @return Read/write reference to the character.
  946. * @throw std::out_of_range If @a n is an invalid index.
  947. *
  948. * This function provides for safer data access. The parameter is
  949. * first checked that it is in the range of the string. The function
  950. * throws out_of_range if the check fails.
  951. */
  952. reference
  953. at(size_type __n)
  954. {
  955. if (__n >= size())
  956. __throw_out_of_range_fmt(__N("basic_string::at: __n "
  957. "(which is %zu) >= this->size() "
  958. "(which is %zu)"),
  959. __n, this->size());
  960. return _M_data()[__n];
  961. }
  962. #if __cplusplus >= 201103L
  963. /**
  964. * Returns a read/write reference to the data at the first
  965. * element of the %string.
  966. */
  967. reference
  968. front() noexcept
  969. {
  970. __glibcxx_assert(!empty());
  971. return operator[](0);
  972. }
  973. /**
  974. * Returns a read-only (constant) reference to the data at the first
  975. * element of the %string.
  976. */
  977. const_reference
  978. front() const noexcept
  979. {
  980. __glibcxx_assert(!empty());
  981. return operator[](0);
  982. }
  983. /**
  984. * Returns a read/write reference to the data at the last
  985. * element of the %string.
  986. */
  987. reference
  988. back() noexcept
  989. {
  990. __glibcxx_assert(!empty());
  991. return operator[](this->size() - 1);
  992. }
  993. /**
  994. * Returns a read-only (constant) reference to the data at the
  995. * last element of the %string.
  996. */
  997. const_reference
  998. back() const noexcept
  999. {
  1000. __glibcxx_assert(!empty());
  1001. return operator[](this->size() - 1);
  1002. }
  1003. #endif
  1004. // Modifiers:
  1005. /**
  1006. * @brief Append a string to this string.
  1007. * @param __str The string to append.
  1008. * @return Reference to this string.
  1009. */
  1010. basic_string&
  1011. operator+=(const basic_string& __str)
  1012. { return this->append(__str); }
  1013. /**
  1014. * @brief Append a C string.
  1015. * @param __s The C string to append.
  1016. * @return Reference to this string.
  1017. */
  1018. basic_string&
  1019. operator+=(const _CharT* __s)
  1020. { return this->append(__s); }
  1021. /**
  1022. * @brief Append a character.
  1023. * @param __c The character to append.
  1024. * @return Reference to this string.
  1025. */
  1026. basic_string&
  1027. operator+=(_CharT __c)
  1028. {
  1029. this->push_back(__c);
  1030. return *this;
  1031. }
  1032. #if __cplusplus >= 201103L
  1033. /**
  1034. * @brief Append an initializer_list of characters.
  1035. * @param __l The initializer_list of characters to be appended.
  1036. * @return Reference to this string.
  1037. */
  1038. basic_string&
  1039. operator+=(initializer_list<_CharT> __l)
  1040. { return this->append(__l.begin(), __l.size()); }
  1041. #endif // C++11
  1042. #if __cplusplus >= 201703L
  1043. /**
  1044. * @brief Append a string_view.
  1045. * @param __svt An object convertible to string_view to be appended.
  1046. * @return Reference to this string.
  1047. */
  1048. template<typename _Tp>
  1049. _If_sv<_Tp, basic_string&>
  1050. operator+=(const _Tp& __svt)
  1051. { return this->append(__svt); }
  1052. #endif // C++17
  1053. /**
  1054. * @brief Append a string to this string.
  1055. * @param __str The string to append.
  1056. * @return Reference to this string.
  1057. */
  1058. basic_string&
  1059. append(const basic_string& __str)
  1060. { return _M_append(__str._M_data(), __str.size()); }
  1061. /**
  1062. * @brief Append a substring.
  1063. * @param __str The string to append.
  1064. * @param __pos Index of the first character of str to append.
  1065. * @param __n The number of characters to append.
  1066. * @return Reference to this string.
  1067. * @throw std::out_of_range if @a __pos is not a valid index.
  1068. *
  1069. * This function appends @a __n characters from @a __str
  1070. * starting at @a __pos to this string. If @a __n is is larger
  1071. * than the number of available characters in @a __str, the
  1072. * remainder of @a __str is appended.
  1073. */
  1074. basic_string&
  1075. append(const basic_string& __str, size_type __pos, size_type __n = npos)
  1076. { return _M_append(__str._M_data()
  1077. + __str._M_check(__pos, "basic_string::append"),
  1078. __str._M_limit(__pos, __n)); }
  1079. /**
  1080. * @brief Append a C substring.
  1081. * @param __s The C string to append.
  1082. * @param __n The number of characters to append.
  1083. * @return Reference to this string.
  1084. */
  1085. basic_string&
  1086. append(const _CharT* __s, size_type __n)
  1087. {
  1088. __glibcxx_requires_string_len(__s, __n);
  1089. _M_check_length(size_type(0), __n, "basic_string::append");
  1090. return _M_append(__s, __n);
  1091. }
  1092. /**
  1093. * @brief Append a C string.
  1094. * @param __s The C string to append.
  1095. * @return Reference to this string.
  1096. */
  1097. basic_string&
  1098. append(const _CharT* __s)
  1099. {
  1100. __glibcxx_requires_string(__s);
  1101. const size_type __n = traits_type::length(__s);
  1102. _M_check_length(size_type(0), __n, "basic_string::append");
  1103. return _M_append(__s, __n);
  1104. }
  1105. /**
  1106. * @brief Append multiple characters.
  1107. * @param __n The number of characters to append.
  1108. * @param __c The character to use.
  1109. * @return Reference to this string.
  1110. *
  1111. * Appends __n copies of __c to this string.
  1112. */
  1113. basic_string&
  1114. append(size_type __n, _CharT __c)
  1115. { return _M_replace_aux(this->size(), size_type(0), __n, __c); }
  1116. #if __cplusplus >= 201103L
  1117. /**
  1118. * @brief Append an initializer_list of characters.
  1119. * @param __l The initializer_list of characters to append.
  1120. * @return Reference to this string.
  1121. */
  1122. basic_string&
  1123. append(initializer_list<_CharT> __l)
  1124. { return this->append(__l.begin(), __l.size()); }
  1125. #endif // C++11
  1126. /**
  1127. * @brief Append a range of characters.
  1128. * @param __first Iterator referencing the first character to append.
  1129. * @param __last Iterator marking the end of the range.
  1130. * @return Reference to this string.
  1131. *
  1132. * Appends characters in the range [__first,__last) to this string.
  1133. */
  1134. #if __cplusplus >= 201103L
  1135. template<class _InputIterator,
  1136. typename = std::_RequireInputIter<_InputIterator>>
  1137. #else
  1138. template<class _InputIterator>
  1139. #endif
  1140. basic_string&
  1141. append(_InputIterator __first, _InputIterator __last)
  1142. { return this->replace(end(), end(), __first, __last); }
  1143. #if __cplusplus >= 201703L
  1144. /**
  1145. * @brief Append a string_view.
  1146. * @param __svt An object convertible to string_view to be appended.
  1147. * @return Reference to this string.
  1148. */
  1149. template<typename _Tp>
  1150. _If_sv<_Tp, basic_string&>
  1151. append(const _Tp& __svt)
  1152. {
  1153. __sv_type __sv = __svt;
  1154. return this->append(__sv.data(), __sv.size());
  1155. }
  1156. /**
  1157. * @brief Append a range of characters from a string_view.
  1158. * @param __svt An object convertible to string_view to be appended from.
  1159. * @param __pos The position in the string_view to append from.
  1160. * @param __n The number of characters to append from the string_view.
  1161. * @return Reference to this string.
  1162. */
  1163. template<typename _Tp>
  1164. _If_sv<_Tp, basic_string&>
  1165. append(const _Tp& __svt, size_type __pos, size_type __n = npos)
  1166. {
  1167. __sv_type __sv = __svt;
  1168. return _M_append(__sv.data()
  1169. + std::__sv_check(__sv.size(), __pos, "basic_string::append"),
  1170. std::__sv_limit(__sv.size(), __pos, __n));
  1171. }
  1172. #endif // C++17
  1173. /**
  1174. * @brief Append a single character.
  1175. * @param __c Character to append.
  1176. */
  1177. void
  1178. push_back(_CharT __c)
  1179. {
  1180. const size_type __size = this->size();
  1181. if (__size + 1 > this->capacity())
  1182. this->_M_mutate(__size, size_type(0), 0, size_type(1));
  1183. traits_type::assign(this->_M_data()[__size], __c);
  1184. this->_M_set_length(__size + 1);
  1185. }
  1186. /**
  1187. * @brief Set value to contents of another string.
  1188. * @param __str Source string to use.
  1189. * @return Reference to this string.
  1190. */
  1191. basic_string&
  1192. assign(const basic_string& __str)
  1193. {
  1194. #if __cplusplus >= 201103L
  1195. if (_Alloc_traits::_S_propagate_on_copy_assign())
  1196. {
  1197. if (!_Alloc_traits::_S_always_equal() && !_M_is_local()
  1198. && _M_get_allocator() != __str._M_get_allocator())
  1199. {
  1200. // Propagating allocator cannot free existing storage so must
  1201. // deallocate it before replacing current allocator.
  1202. if (__str.size() <= _S_local_capacity)
  1203. {
  1204. _M_destroy(_M_allocated_capacity);
  1205. _M_data(_M_local_data());
  1206. _M_set_length(0);
  1207. }
  1208. else
  1209. {
  1210. const auto __len = __str.size();
  1211. auto __alloc = __str._M_get_allocator();
  1212. // If this allocation throws there are no effects:
  1213. auto __ptr = _Alloc_traits::allocate(__alloc, __len + 1);
  1214. _M_destroy(_M_allocated_capacity);
  1215. _M_data(__ptr);
  1216. _M_capacity(__len);
  1217. _M_set_length(__len);
  1218. }
  1219. }
  1220. std::__alloc_on_copy(_M_get_allocator(), __str._M_get_allocator());
  1221. }
  1222. #endif
  1223. this->_M_assign(__str);
  1224. return *this;
  1225. }
  1226. #if __cplusplus >= 201103L
  1227. /**
  1228. * @brief Set value to contents of another string.
  1229. * @param __str Source string to use.
  1230. * @return Reference to this string.
  1231. *
  1232. * This function sets this string to the exact contents of @a __str.
  1233. * @a __str is a valid, but unspecified string.
  1234. */
  1235. basic_string&
  1236. assign(basic_string&& __str)
  1237. noexcept(_Alloc_traits::_S_nothrow_move())
  1238. {
  1239. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1240. // 2063. Contradictory requirements for string move assignment
  1241. return *this = std::move(__str);
  1242. }
  1243. #endif // C++11
  1244. /**
  1245. * @brief Set value to a substring of a string.
  1246. * @param __str The string to use.
  1247. * @param __pos Index of the first character of str.
  1248. * @param __n Number of characters to use.
  1249. * @return Reference to this string.
  1250. * @throw std::out_of_range if @a pos is not a valid index.
  1251. *
  1252. * This function sets this string to the substring of @a __str
  1253. * consisting of @a __n characters at @a __pos. If @a __n is
  1254. * is larger than the number of available characters in @a
  1255. * __str, the remainder of @a __str is used.
  1256. */
  1257. basic_string&
  1258. assign(const basic_string& __str, size_type __pos, size_type __n = npos)
  1259. { return _M_replace(size_type(0), this->size(), __str._M_data()
  1260. + __str._M_check(__pos, "basic_string::assign"),
  1261. __str._M_limit(__pos, __n)); }
  1262. /**
  1263. * @brief Set value to a C substring.
  1264. * @param __s The C string to use.
  1265. * @param __n Number of characters to use.
  1266. * @return Reference to this string.
  1267. *
  1268. * This function sets the value of this string to the first @a __n
  1269. * characters of @a __s. If @a __n is is larger than the number of
  1270. * available characters in @a __s, the remainder of @a __s is used.
  1271. */
  1272. basic_string&
  1273. assign(const _CharT* __s, size_type __n)
  1274. {
  1275. __glibcxx_requires_string_len(__s, __n);
  1276. return _M_replace(size_type(0), this->size(), __s, __n);
  1277. }
  1278. /**
  1279. * @brief Set value to contents of a C string.
  1280. * @param __s The C string to use.
  1281. * @return Reference to this string.
  1282. *
  1283. * This function sets the value of this string to the value of @a __s.
  1284. * The data is copied, so there is no dependence on @a __s once the
  1285. * function returns.
  1286. */
  1287. basic_string&
  1288. assign(const _CharT* __s)
  1289. {
  1290. __glibcxx_requires_string(__s);
  1291. return _M_replace(size_type(0), this->size(), __s,
  1292. traits_type::length(__s));
  1293. }
  1294. /**
  1295. * @brief Set value to multiple characters.
  1296. * @param __n Length of the resulting string.
  1297. * @param __c The character to use.
  1298. * @return Reference to this string.
  1299. *
  1300. * This function sets the value of this string to @a __n copies of
  1301. * character @a __c.
  1302. */
  1303. basic_string&
  1304. assign(size_type __n, _CharT __c)
  1305. { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
  1306. /**
  1307. * @brief Set value to a range of characters.
  1308. * @param __first Iterator referencing the first character to append.
  1309. * @param __last Iterator marking the end of the range.
  1310. * @return Reference to this string.
  1311. *
  1312. * Sets value of string to characters in the range [__first,__last).
  1313. */
  1314. #if __cplusplus >= 201103L
  1315. template<class _InputIterator,
  1316. typename = std::_RequireInputIter<_InputIterator>>
  1317. #else
  1318. template<class _InputIterator>
  1319. #endif
  1320. basic_string&
  1321. assign(_InputIterator __first, _InputIterator __last)
  1322. { return this->replace(begin(), end(), __first, __last); }
  1323. #if __cplusplus >= 201103L
  1324. /**
  1325. * @brief Set value to an initializer_list of characters.
  1326. * @param __l The initializer_list of characters to assign.
  1327. * @return Reference to this string.
  1328. */
  1329. basic_string&
  1330. assign(initializer_list<_CharT> __l)
  1331. { return this->assign(__l.begin(), __l.size()); }
  1332. #endif // C++11
  1333. #if __cplusplus >= 201703L
  1334. /**
  1335. * @brief Set value from a string_view.
  1336. * @param __svt The source object convertible to string_view.
  1337. * @return Reference to this string.
  1338. */
  1339. template<typename _Tp>
  1340. _If_sv<_Tp, basic_string&>
  1341. assign(const _Tp& __svt)
  1342. {
  1343. __sv_type __sv = __svt;
  1344. return this->assign(__sv.data(), __sv.size());
  1345. }
  1346. /**
  1347. * @brief Set value from a range of characters in a string_view.
  1348. * @param __svt The source object convertible to string_view.
  1349. * @param __pos The position in the string_view to assign from.
  1350. * @param __n The number of characters to assign.
  1351. * @return Reference to this string.
  1352. */
  1353. template<typename _Tp>
  1354. _If_sv<_Tp, basic_string&>
  1355. assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
  1356. {
  1357. __sv_type __sv = __svt;
  1358. return _M_replace(size_type(0), this->size(),
  1359. __sv.data()
  1360. + std::__sv_check(__sv.size(), __pos, "basic_string::assign"),
  1361. std::__sv_limit(__sv.size(), __pos, __n));
  1362. }
  1363. #endif // C++17
  1364. #if __cplusplus >= 201103L
  1365. /**
  1366. * @brief Insert multiple characters.
  1367. * @param __p Const_iterator referencing location in string to
  1368. * insert at.
  1369. * @param __n Number of characters to insert
  1370. * @param __c The character to insert.
  1371. * @return Iterator referencing the first inserted char.
  1372. * @throw std::length_error If new length exceeds @c max_size().
  1373. *
  1374. * Inserts @a __n copies of character @a __c starting at the
  1375. * position referenced by iterator @a __p. If adding
  1376. * characters causes the length to exceed max_size(),
  1377. * length_error is thrown. The value of the string doesn't
  1378. * change if an error is thrown.
  1379. */
  1380. iterator
  1381. insert(const_iterator __p, size_type __n, _CharT __c)
  1382. {
  1383. _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
  1384. const size_type __pos = __p - begin();
  1385. this->replace(__p, __p, __n, __c);
  1386. return iterator(this->_M_data() + __pos);
  1387. }
  1388. #else
  1389. /**
  1390. * @brief Insert multiple characters.
  1391. * @param __p Iterator referencing location in string to insert at.
  1392. * @param __n Number of characters to insert
  1393. * @param __c The character to insert.
  1394. * @throw std::length_error If new length exceeds @c max_size().
  1395. *
  1396. * Inserts @a __n copies of character @a __c starting at the
  1397. * position referenced by iterator @a __p. If adding
  1398. * characters causes the length to exceed max_size(),
  1399. * length_error is thrown. The value of the string doesn't
  1400. * change if an error is thrown.
  1401. */
  1402. void
  1403. insert(iterator __p, size_type __n, _CharT __c)
  1404. { this->replace(__p, __p, __n, __c); }
  1405. #endif
  1406. #if __cplusplus >= 201103L
  1407. /**
  1408. * @brief Insert a range of characters.
  1409. * @param __p Const_iterator referencing location in string to
  1410. * insert at.
  1411. * @param __beg Start of range.
  1412. * @param __end End of range.
  1413. * @return Iterator referencing the first inserted char.
  1414. * @throw std::length_error If new length exceeds @c max_size().
  1415. *
  1416. * Inserts characters in range [beg,end). If adding characters
  1417. * causes the length to exceed max_size(), length_error is
  1418. * thrown. The value of the string doesn't change if an error
  1419. * is thrown.
  1420. */
  1421. template<class _InputIterator,
  1422. typename = std::_RequireInputIter<_InputIterator>>
  1423. iterator
  1424. insert(const_iterator __p, _InputIterator __beg, _InputIterator __end)
  1425. {
  1426. _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
  1427. const size_type __pos = __p - begin();
  1428. this->replace(__p, __p, __beg, __end);
  1429. return iterator(this->_M_data() + __pos);
  1430. }
  1431. #else
  1432. /**
  1433. * @brief Insert a range of characters.
  1434. * @param __p Iterator referencing location in string to insert at.
  1435. * @param __beg Start of range.
  1436. * @param __end End of range.
  1437. * @throw std::length_error If new length exceeds @c max_size().
  1438. *
  1439. * Inserts characters in range [__beg,__end). If adding
  1440. * characters causes the length to exceed max_size(),
  1441. * length_error is thrown. The value of the string doesn't
  1442. * change if an error is thrown.
  1443. */
  1444. template<class _InputIterator>
  1445. void
  1446. insert(iterator __p, _InputIterator __beg, _InputIterator __end)
  1447. { this->replace(__p, __p, __beg, __end); }
  1448. #endif
  1449. #if __cplusplus >= 201103L
  1450. /**
  1451. * @brief Insert an initializer_list of characters.
  1452. * @param __p Iterator referencing location in string to insert at.
  1453. * @param __l The initializer_list of characters to insert.
  1454. * @throw std::length_error If new length exceeds @c max_size().
  1455. */
  1456. iterator
  1457. insert(const_iterator __p, initializer_list<_CharT> __l)
  1458. { return this->insert(__p, __l.begin(), __l.end()); }
  1459. #ifdef _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
  1460. // See PR libstdc++/83328
  1461. void
  1462. insert(iterator __p, initializer_list<_CharT> __l)
  1463. {
  1464. _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
  1465. this->insert(__p - begin(), __l.begin(), __l.size());
  1466. }
  1467. #endif
  1468. #endif // C++11
  1469. /**
  1470. * @brief Insert value of a string.
  1471. * @param __pos1 Position in string to insert at.
  1472. * @param __str The string to insert.
  1473. * @return Reference to this string.
  1474. * @throw std::length_error If new length exceeds @c max_size().
  1475. *
  1476. * Inserts value of @a __str starting at @a __pos1. If adding
  1477. * characters causes the length to exceed max_size(),
  1478. * length_error is thrown. The value of the string doesn't
  1479. * change if an error is thrown.
  1480. */
  1481. basic_string&
  1482. insert(size_type __pos1, const basic_string& __str)
  1483. { return this->replace(__pos1, size_type(0),
  1484. __str._M_data(), __str.size()); }
  1485. /**
  1486. * @brief Insert a substring.
  1487. * @param __pos1 Position in string to insert at.
  1488. * @param __str The string to insert.
  1489. * @param __pos2 Start of characters in str to insert.
  1490. * @param __n Number of characters to insert.
  1491. * @return Reference to this string.
  1492. * @throw std::length_error If new length exceeds @c max_size().
  1493. * @throw std::out_of_range If @a pos1 > size() or
  1494. * @a __pos2 > @a str.size().
  1495. *
  1496. * Starting at @a pos1, insert @a __n character of @a __str
  1497. * beginning with @a __pos2. If adding characters causes the
  1498. * length to exceed max_size(), length_error is thrown. If @a
  1499. * __pos1 is beyond the end of this string or @a __pos2 is
  1500. * beyond the end of @a __str, out_of_range is thrown. The
  1501. * value of the string doesn't change if an error is thrown.
  1502. */
  1503. basic_string&
  1504. insert(size_type __pos1, const basic_string& __str,
  1505. size_type __pos2, size_type __n = npos)
  1506. { return this->replace(__pos1, size_type(0), __str._M_data()
  1507. + __str._M_check(__pos2, "basic_string::insert"),
  1508. __str._M_limit(__pos2, __n)); }
  1509. /**
  1510. * @brief Insert a C substring.
  1511. * @param __pos Position in string to insert at.
  1512. * @param __s The C string to insert.
  1513. * @param __n The number of characters to insert.
  1514. * @return Reference to this string.
  1515. * @throw std::length_error If new length exceeds @c max_size().
  1516. * @throw std::out_of_range If @a __pos is beyond the end of this
  1517. * string.
  1518. *
  1519. * Inserts the first @a __n characters of @a __s starting at @a
  1520. * __pos. If adding characters causes the length to exceed
  1521. * max_size(), length_error is thrown. If @a __pos is beyond
  1522. * end(), out_of_range is thrown. The value of the string
  1523. * doesn't change if an error is thrown.
  1524. */
  1525. basic_string&
  1526. insert(size_type __pos, const _CharT* __s, size_type __n)
  1527. { return this->replace(__pos, size_type(0), __s, __n); }
  1528. /**
  1529. * @brief Insert a C string.
  1530. * @param __pos Position in string to insert at.
  1531. * @param __s The C string to insert.
  1532. * @return Reference to this string.
  1533. * @throw std::length_error If new length exceeds @c max_size().
  1534. * @throw std::out_of_range If @a pos is beyond the end of this
  1535. * string.
  1536. *
  1537. * Inserts the first @a n characters of @a __s starting at @a __pos. If
  1538. * adding characters causes the length to exceed max_size(),
  1539. * length_error is thrown. If @a __pos is beyond end(), out_of_range is
  1540. * thrown. The value of the string doesn't change if an error is
  1541. * thrown.
  1542. */
  1543. basic_string&
  1544. insert(size_type __pos, const _CharT* __s)
  1545. {
  1546. __glibcxx_requires_string(__s);
  1547. return this->replace(__pos, size_type(0), __s,
  1548. traits_type::length(__s));
  1549. }
  1550. /**
  1551. * @brief Insert multiple characters.
  1552. * @param __pos Index in string to insert at.
  1553. * @param __n Number of characters to insert
  1554. * @param __c The character to insert.
  1555. * @return Reference to this string.
  1556. * @throw std::length_error If new length exceeds @c max_size().
  1557. * @throw std::out_of_range If @a __pos is beyond the end of this
  1558. * string.
  1559. *
  1560. * Inserts @a __n copies of character @a __c starting at index
  1561. * @a __pos. If adding characters causes the length to exceed
  1562. * max_size(), length_error is thrown. If @a __pos > length(),
  1563. * out_of_range is thrown. The value of the string doesn't
  1564. * change if an error is thrown.
  1565. */
  1566. basic_string&
  1567. insert(size_type __pos, size_type __n, _CharT __c)
  1568. { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
  1569. size_type(0), __n, __c); }
  1570. /**
  1571. * @brief Insert one character.
  1572. * @param __p Iterator referencing position in string to insert at.
  1573. * @param __c The character to insert.
  1574. * @return Iterator referencing newly inserted char.
  1575. * @throw std::length_error If new length exceeds @c max_size().
  1576. *
  1577. * Inserts character @a __c at position referenced by @a __p.
  1578. * If adding character causes the length to exceed max_size(),
  1579. * length_error is thrown. If @a __p is beyond end of string,
  1580. * out_of_range is thrown. The value of the string doesn't
  1581. * change if an error is thrown.
  1582. */
  1583. iterator
  1584. insert(__const_iterator __p, _CharT __c)
  1585. {
  1586. _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
  1587. const size_type __pos = __p - begin();
  1588. _M_replace_aux(__pos, size_type(0), size_type(1), __c);
  1589. return iterator(_M_data() + __pos);
  1590. }
  1591. #if __cplusplus >= 201703L
  1592. /**
  1593. * @brief Insert a string_view.
  1594. * @param __pos Position in string to insert at.
  1595. * @param __svt The object convertible to string_view to insert.
  1596. * @return Reference to this string.
  1597. */
  1598. template<typename _Tp>
  1599. _If_sv<_Tp, basic_string&>
  1600. insert(size_type __pos, const _Tp& __svt)
  1601. {
  1602. __sv_type __sv = __svt;
  1603. return this->insert(__pos, __sv.data(), __sv.size());
  1604. }
  1605. /**
  1606. * @brief Insert a string_view.
  1607. * @param __pos1 Position in string to insert at.
  1608. * @param __svt The object convertible to string_view to insert from.
  1609. * @param __pos2 Start of characters in str to insert.
  1610. * @param __n The number of characters to insert.
  1611. * @return Reference to this string.
  1612. */
  1613. template<typename _Tp>
  1614. _If_sv<_Tp, basic_string&>
  1615. insert(size_type __pos1, const _Tp& __svt,
  1616. size_type __pos2, size_type __n = npos)
  1617. {
  1618. __sv_type __sv = __svt;
  1619. return this->replace(__pos1, size_type(0),
  1620. __sv.data()
  1621. + std::__sv_check(__sv.size(), __pos2, "basic_string::insert"),
  1622. std::__sv_limit(__sv.size(), __pos2, __n));
  1623. }
  1624. #endif // C++17
  1625. /**
  1626. * @brief Remove characters.
  1627. * @param __pos Index of first character to remove (default 0).
  1628. * @param __n Number of characters to remove (default remainder).
  1629. * @return Reference to this string.
  1630. * @throw std::out_of_range If @a pos is beyond the end of this
  1631. * string.
  1632. *
  1633. * Removes @a __n characters from this string starting at @a
  1634. * __pos. The length of the string is reduced by @a __n. If
  1635. * there are < @a __n characters to remove, the remainder of
  1636. * the string is truncated. If @a __p is beyond end of string,
  1637. * out_of_range is thrown. The value of the string doesn't
  1638. * change if an error is thrown.
  1639. */
  1640. basic_string&
  1641. erase(size_type __pos = 0, size_type __n = npos)
  1642. {
  1643. _M_check(__pos, "basic_string::erase");
  1644. if (__n == npos)
  1645. this->_M_set_length(__pos);
  1646. else if (__n != 0)
  1647. this->_M_erase(__pos, _M_limit(__pos, __n));
  1648. return *this;
  1649. }
  1650. /**
  1651. * @brief Remove one character.
  1652. * @param __position Iterator referencing the character to remove.
  1653. * @return iterator referencing same location after removal.
  1654. *
  1655. * Removes the character at @a __position from this string. The value
  1656. * of the string doesn't change if an error is thrown.
  1657. */
  1658. iterator
  1659. erase(__const_iterator __position)
  1660. {
  1661. _GLIBCXX_DEBUG_PEDASSERT(__position >= begin()
  1662. && __position < end());
  1663. const size_type __pos = __position - begin();
  1664. this->_M_erase(__pos, size_type(1));
  1665. return iterator(_M_data() + __pos);
  1666. }
  1667. /**
  1668. * @brief Remove a range of characters.
  1669. * @param __first Iterator referencing the first character to remove.
  1670. * @param __last Iterator referencing the end of the range.
  1671. * @return Iterator referencing location of first after removal.
  1672. *
  1673. * Removes the characters in the range [first,last) from this string.
  1674. * The value of the string doesn't change if an error is thrown.
  1675. */
  1676. iterator
  1677. erase(__const_iterator __first, __const_iterator __last)
  1678. {
  1679. _GLIBCXX_DEBUG_PEDASSERT(__first >= begin() && __first <= __last
  1680. && __last <= end());
  1681. const size_type __pos = __first - begin();
  1682. if (__last == end())
  1683. this->_M_set_length(__pos);
  1684. else
  1685. this->_M_erase(__pos, __last - __first);
  1686. return iterator(this->_M_data() + __pos);
  1687. }
  1688. #if __cplusplus >= 201103L
  1689. /**
  1690. * @brief Remove the last character.
  1691. *
  1692. * The string must be non-empty.
  1693. */
  1694. void
  1695. pop_back() noexcept
  1696. {
  1697. __glibcxx_assert(!empty());
  1698. _M_erase(size() - 1, 1);
  1699. }
  1700. #endif // C++11
  1701. /**
  1702. * @brief Replace characters with value from another string.
  1703. * @param __pos Index of first character to replace.
  1704. * @param __n Number of characters to be replaced.
  1705. * @param __str String to insert.
  1706. * @return Reference to this string.
  1707. * @throw std::out_of_range If @a pos is beyond the end of this
  1708. * string.
  1709. * @throw std::length_error If new length exceeds @c max_size().
  1710. *
  1711. * Removes the characters in the range [__pos,__pos+__n) from
  1712. * this string. In place, the value of @a __str is inserted.
  1713. * If @a __pos is beyond end of string, out_of_range is thrown.
  1714. * If the length of the result exceeds max_size(), length_error
  1715. * is thrown. The value of the string doesn't change if an
  1716. * error is thrown.
  1717. */
  1718. basic_string&
  1719. replace(size_type __pos, size_type __n, const basic_string& __str)
  1720. { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
  1721. /**
  1722. * @brief Replace characters with value from another string.
  1723. * @param __pos1 Index of first character to replace.
  1724. * @param __n1 Number of characters to be replaced.
  1725. * @param __str String to insert.
  1726. * @param __pos2 Index of first character of str to use.
  1727. * @param __n2 Number of characters from str to use.
  1728. * @return Reference to this string.
  1729. * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
  1730. * __str.size().
  1731. * @throw std::length_error If new length exceeds @c max_size().
  1732. *
  1733. * Removes the characters in the range [__pos1,__pos1 + n) from this
  1734. * string. In place, the value of @a __str is inserted. If @a __pos is
  1735. * beyond end of string, out_of_range is thrown. If the length of the
  1736. * result exceeds max_size(), length_error is thrown. The value of the
  1737. * string doesn't change if an error is thrown.
  1738. */
  1739. basic_string&
  1740. replace(size_type __pos1, size_type __n1, const basic_string& __str,
  1741. size_type __pos2, size_type __n2 = npos)
  1742. { return this->replace(__pos1, __n1, __str._M_data()
  1743. + __str._M_check(__pos2, "basic_string::replace"),
  1744. __str._M_limit(__pos2, __n2)); }
  1745. /**
  1746. * @brief Replace characters with value of a C substring.
  1747. * @param __pos Index of first character to replace.
  1748. * @param __n1 Number of characters to be replaced.
  1749. * @param __s C string to insert.
  1750. * @param __n2 Number of characters from @a s to use.
  1751. * @return Reference to this string.
  1752. * @throw std::out_of_range If @a pos1 > size().
  1753. * @throw std::length_error If new length exceeds @c max_size().
  1754. *
  1755. * Removes the characters in the range [__pos,__pos + __n1)
  1756. * from this string. In place, the first @a __n2 characters of
  1757. * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
  1758. * @a __pos is beyond end of string, out_of_range is thrown. If
  1759. * the length of result exceeds max_size(), length_error is
  1760. * thrown. The value of the string doesn't change if an error
  1761. * is thrown.
  1762. */
  1763. basic_string&
  1764. replace(size_type __pos, size_type __n1, const _CharT* __s,
  1765. size_type __n2)
  1766. {
  1767. __glibcxx_requires_string_len(__s, __n2);
  1768. return _M_replace(_M_check(__pos, "basic_string::replace"),
  1769. _M_limit(__pos, __n1), __s, __n2);
  1770. }
  1771. /**
  1772. * @brief Replace characters with value of a C string.
  1773. * @param __pos Index of first character to replace.
  1774. * @param __n1 Number of characters to be replaced.
  1775. * @param __s C string to insert.
  1776. * @return Reference to this string.
  1777. * @throw std::out_of_range If @a pos > size().
  1778. * @throw std::length_error If new length exceeds @c max_size().
  1779. *
  1780. * Removes the characters in the range [__pos,__pos + __n1)
  1781. * from this string. In place, the characters of @a __s are
  1782. * inserted. If @a __pos is beyond end of string, out_of_range
  1783. * is thrown. If the length of result exceeds max_size(),
  1784. * length_error is thrown. The value of the string doesn't
  1785. * change if an error is thrown.
  1786. */
  1787. basic_string&
  1788. replace(size_type __pos, size_type __n1, const _CharT* __s)
  1789. {
  1790. __glibcxx_requires_string(__s);
  1791. return this->replace(__pos, __n1, __s, traits_type::length(__s));
  1792. }
  1793. /**
  1794. * @brief Replace characters with multiple characters.
  1795. * @param __pos Index of first character to replace.
  1796. * @param __n1 Number of characters to be replaced.
  1797. * @param __n2 Number of characters to insert.
  1798. * @param __c Character to insert.
  1799. * @return Reference to this string.
  1800. * @throw std::out_of_range If @a __pos > size().
  1801. * @throw std::length_error If new length exceeds @c max_size().
  1802. *
  1803. * Removes the characters in the range [pos,pos + n1) from this
  1804. * string. In place, @a __n2 copies of @a __c are inserted.
  1805. * If @a __pos is beyond end of string, out_of_range is thrown.
  1806. * If the length of result exceeds max_size(), length_error is
  1807. * thrown. The value of the string doesn't change if an error
  1808. * is thrown.
  1809. */
  1810. basic_string&
  1811. replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
  1812. { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
  1813. _M_limit(__pos, __n1), __n2, __c); }
  1814. /**
  1815. * @brief Replace range of characters with string.
  1816. * @param __i1 Iterator referencing start of range to replace.
  1817. * @param __i2 Iterator referencing end of range to replace.
  1818. * @param __str String value to insert.
  1819. * @return Reference to this string.
  1820. * @throw std::length_error If new length exceeds @c max_size().
  1821. *
  1822. * Removes the characters in the range [__i1,__i2). In place,
  1823. * the value of @a __str is inserted. If the length of result
  1824. * exceeds max_size(), length_error is thrown. The value of
  1825. * the string doesn't change if an error is thrown.
  1826. */
  1827. basic_string&
  1828. replace(__const_iterator __i1, __const_iterator __i2,
  1829. const basic_string& __str)
  1830. { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
  1831. /**
  1832. * @brief Replace range of characters with C substring.
  1833. * @param __i1 Iterator referencing start of range to replace.
  1834. * @param __i2 Iterator referencing end of range to replace.
  1835. * @param __s C string value to insert.
  1836. * @param __n Number of characters from s to insert.
  1837. * @return Reference to this string.
  1838. * @throw std::length_error If new length exceeds @c max_size().
  1839. *
  1840. * Removes the characters in the range [__i1,__i2). In place,
  1841. * the first @a __n characters of @a __s are inserted. If the
  1842. * length of result exceeds max_size(), length_error is thrown.
  1843. * The value of the string doesn't change if an error is
  1844. * thrown.
  1845. */
  1846. basic_string&
  1847. replace(__const_iterator __i1, __const_iterator __i2,
  1848. const _CharT* __s, size_type __n)
  1849. {
  1850. _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
  1851. && __i2 <= end());
  1852. return this->replace(__i1 - begin(), __i2 - __i1, __s, __n);
  1853. }
  1854. /**
  1855. * @brief Replace range of characters with C string.
  1856. * @param __i1 Iterator referencing start of range to replace.
  1857. * @param __i2 Iterator referencing end of range to replace.
  1858. * @param __s C string value to insert.
  1859. * @return Reference to this string.
  1860. * @throw std::length_error If new length exceeds @c max_size().
  1861. *
  1862. * Removes the characters in the range [__i1,__i2). In place,
  1863. * the characters of @a __s are inserted. If the length of
  1864. * result exceeds max_size(), length_error is thrown. The
  1865. * value of the string doesn't change if an error is thrown.
  1866. */
  1867. basic_string&
  1868. replace(__const_iterator __i1, __const_iterator __i2, const _CharT* __s)
  1869. {
  1870. __glibcxx_requires_string(__s);
  1871. return this->replace(__i1, __i2, __s, traits_type::length(__s));
  1872. }
  1873. /**
  1874. * @brief Replace range of characters with multiple characters
  1875. * @param __i1 Iterator referencing start of range to replace.
  1876. * @param __i2 Iterator referencing end of range to replace.
  1877. * @param __n Number of characters to insert.
  1878. * @param __c Character to insert.
  1879. * @return Reference to this string.
  1880. * @throw std::length_error If new length exceeds @c max_size().
  1881. *
  1882. * Removes the characters in the range [__i1,__i2). In place,
  1883. * @a __n copies of @a __c are inserted. If the length of
  1884. * result exceeds max_size(), length_error is thrown. The
  1885. * value of the string doesn't change if an error is thrown.
  1886. */
  1887. basic_string&
  1888. replace(__const_iterator __i1, __const_iterator __i2, size_type __n,
  1889. _CharT __c)
  1890. {
  1891. _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
  1892. && __i2 <= end());
  1893. return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __c);
  1894. }
  1895. /**
  1896. * @brief Replace range of characters with range.
  1897. * @param __i1 Iterator referencing start of range to replace.
  1898. * @param __i2 Iterator referencing end of range to replace.
  1899. * @param __k1 Iterator referencing start of range to insert.
  1900. * @param __k2 Iterator referencing end of range to insert.
  1901. * @return Reference to this string.
  1902. * @throw std::length_error If new length exceeds @c max_size().
  1903. *
  1904. * Removes the characters in the range [__i1,__i2). In place,
  1905. * characters in the range [__k1,__k2) are inserted. If the
  1906. * length of result exceeds max_size(), length_error is thrown.
  1907. * The value of the string doesn't change if an error is
  1908. * thrown.
  1909. */
  1910. #if __cplusplus >= 201103L
  1911. template<class _InputIterator,
  1912. typename = std::_RequireInputIter<_InputIterator>>
  1913. basic_string&
  1914. replace(const_iterator __i1, const_iterator __i2,
  1915. _InputIterator __k1, _InputIterator __k2)
  1916. {
  1917. _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
  1918. && __i2 <= end());
  1919. __glibcxx_requires_valid_range(__k1, __k2);
  1920. return this->_M_replace_dispatch(__i1, __i2, __k1, __k2,
  1921. std::__false_type());
  1922. }
  1923. #else
  1924. template<class _InputIterator>
  1925. #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
  1926. typename __enable_if_not_native_iterator<_InputIterator>::__type
  1927. #else
  1928. basic_string&
  1929. #endif
  1930. replace(iterator __i1, iterator __i2,
  1931. _InputIterator __k1, _InputIterator __k2)
  1932. {
  1933. _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
  1934. && __i2 <= end());
  1935. __glibcxx_requires_valid_range(__k1, __k2);
  1936. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  1937. return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
  1938. }
  1939. #endif
  1940. // Specializations for the common case of pointer and iterator:
  1941. // useful to avoid the overhead of temporary buffering in _M_replace.
  1942. basic_string&
  1943. replace(__const_iterator __i1, __const_iterator __i2,
  1944. _CharT* __k1, _CharT* __k2)
  1945. {
  1946. _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
  1947. && __i2 <= end());
  1948. __glibcxx_requires_valid_range(__k1, __k2);
  1949. return this->replace(__i1 - begin(), __i2 - __i1,
  1950. __k1, __k2 - __k1);
  1951. }
  1952. basic_string&
  1953. replace(__const_iterator __i1, __const_iterator __i2,
  1954. const _CharT* __k1, const _CharT* __k2)
  1955. {
  1956. _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
  1957. && __i2 <= end());
  1958. __glibcxx_requires_valid_range(__k1, __k2);
  1959. return this->replace(__i1 - begin(), __i2 - __i1,
  1960. __k1, __k2 - __k1);
  1961. }
  1962. basic_string&
  1963. replace(__const_iterator __i1, __const_iterator __i2,
  1964. iterator __k1, iterator __k2)
  1965. {
  1966. _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
  1967. && __i2 <= end());
  1968. __glibcxx_requires_valid_range(__k1, __k2);
  1969. return this->replace(__i1 - begin(), __i2 - __i1,
  1970. __k1.base(), __k2 - __k1);
  1971. }
  1972. basic_string&
  1973. replace(__const_iterator __i1, __const_iterator __i2,
  1974. const_iterator __k1, const_iterator __k2)
  1975. {
  1976. _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
  1977. && __i2 <= end());
  1978. __glibcxx_requires_valid_range(__k1, __k2);
  1979. return this->replace(__i1 - begin(), __i2 - __i1,
  1980. __k1.base(), __k2 - __k1);
  1981. }
  1982. #if __cplusplus >= 201103L
  1983. /**
  1984. * @brief Replace range of characters with initializer_list.
  1985. * @param __i1 Iterator referencing start of range to replace.
  1986. * @param __i2 Iterator referencing end of range to replace.
  1987. * @param __l The initializer_list of characters to insert.
  1988. * @return Reference to this string.
  1989. * @throw std::length_error If new length exceeds @c max_size().
  1990. *
  1991. * Removes the characters in the range [__i1,__i2). In place,
  1992. * characters in the range [__k1,__k2) are inserted. If the
  1993. * length of result exceeds max_size(), length_error is thrown.
  1994. * The value of the string doesn't change if an error is
  1995. * thrown.
  1996. */
  1997. basic_string& replace(const_iterator __i1, const_iterator __i2,
  1998. initializer_list<_CharT> __l)
  1999. { return this->replace(__i1, __i2, __l.begin(), __l.size()); }
  2000. #endif // C++11
  2001. #if __cplusplus >= 201703L
  2002. /**
  2003. * @brief Replace range of characters with string_view.
  2004. * @param __pos The position to replace at.
  2005. * @param __n The number of characters to replace.
  2006. * @param __svt The object convertible to string_view to insert.
  2007. * @return Reference to this string.
  2008. */
  2009. template<typename _Tp>
  2010. _If_sv<_Tp, basic_string&>
  2011. replace(size_type __pos, size_type __n, const _Tp& __svt)
  2012. {
  2013. __sv_type __sv = __svt;
  2014. return this->replace(__pos, __n, __sv.data(), __sv.size());
  2015. }
  2016. /**
  2017. * @brief Replace range of characters with string_view.
  2018. * @param __pos1 The position to replace at.
  2019. * @param __n1 The number of characters to replace.
  2020. * @param __svt The object convertible to string_view to insert from.
  2021. * @param __pos2 The position in the string_view to insert from.
  2022. * @param __n2 The number of characters to insert.
  2023. * @return Reference to this string.
  2024. */
  2025. template<typename _Tp>
  2026. _If_sv<_Tp, basic_string&>
  2027. replace(size_type __pos1, size_type __n1, const _Tp& __svt,
  2028. size_type __pos2, size_type __n2 = npos)
  2029. {
  2030. __sv_type __sv = __svt;
  2031. return this->replace(__pos1, __n1,
  2032. __sv.data()
  2033. + std::__sv_check(__sv.size(), __pos2, "basic_string::replace"),
  2034. std::__sv_limit(__sv.size(), __pos2, __n2));
  2035. }
  2036. /**
  2037. * @brief Replace range of characters with string_view.
  2038. * @param __i1 An iterator referencing the start position
  2039. to replace at.
  2040. * @param __i2 An iterator referencing the end position
  2041. for the replace.
  2042. * @param __svt The object convertible to string_view to insert from.
  2043. * @return Reference to this string.
  2044. */
  2045. template<typename _Tp>
  2046. _If_sv<_Tp, basic_string&>
  2047. replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
  2048. {
  2049. __sv_type __sv = __svt;
  2050. return this->replace(__i1 - begin(), __i2 - __i1, __sv);
  2051. }
  2052. #endif // C++17
  2053. private:
  2054. template<class _Integer>
  2055. basic_string&
  2056. _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
  2057. _Integer __n, _Integer __val, __true_type)
  2058. { return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __val); }
  2059. template<class _InputIterator>
  2060. basic_string&
  2061. _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
  2062. _InputIterator __k1, _InputIterator __k2,
  2063. __false_type);
  2064. basic_string&
  2065. _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
  2066. _CharT __c);
  2067. basic_string&
  2068. _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
  2069. const size_type __len2);
  2070. basic_string&
  2071. _M_append(const _CharT* __s, size_type __n);
  2072. public:
  2073. /**
  2074. * @brief Copy substring into C string.
  2075. * @param __s C string to copy value into.
  2076. * @param __n Number of characters to copy.
  2077. * @param __pos Index of first character to copy.
  2078. * @return Number of characters actually copied
  2079. * @throw std::out_of_range If __pos > size().
  2080. *
  2081. * Copies up to @a __n characters starting at @a __pos into the
  2082. * C string @a __s. If @a __pos is %greater than size(),
  2083. * out_of_range is thrown.
  2084. */
  2085. size_type
  2086. copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
  2087. /**
  2088. * @brief Swap contents with another string.
  2089. * @param __s String to swap with.
  2090. *
  2091. * Exchanges the contents of this string with that of @a __s in constant
  2092. * time.
  2093. */
  2094. void
  2095. swap(basic_string& __s) _GLIBCXX_NOEXCEPT;
  2096. // String operations:
  2097. /**
  2098. * @brief Return const pointer to null-terminated contents.
  2099. *
  2100. * This is a handle to internal data. Do not modify or dire things may
  2101. * happen.
  2102. */
  2103. const _CharT*
  2104. c_str() const _GLIBCXX_NOEXCEPT
  2105. { return _M_data(); }
  2106. /**
  2107. * @brief Return const pointer to contents.
  2108. *
  2109. * This is a pointer to internal data. It is undefined to modify
  2110. * the contents through the returned pointer. To get a pointer that
  2111. * allows modifying the contents use @c &str[0] instead,
  2112. * (or in C++17 the non-const @c str.data() overload).
  2113. */
  2114. const _CharT*
  2115. data() const _GLIBCXX_NOEXCEPT
  2116. { return _M_data(); }
  2117. #if __cplusplus >= 201703L
  2118. /**
  2119. * @brief Return non-const pointer to contents.
  2120. *
  2121. * This is a pointer to the character sequence held by the string.
  2122. * Modifying the characters in the sequence is allowed.
  2123. */
  2124. _CharT*
  2125. data() noexcept
  2126. { return _M_data(); }
  2127. #endif
  2128. /**
  2129. * @brief Return copy of allocator used to construct this string.
  2130. */
  2131. allocator_type
  2132. get_allocator() const _GLIBCXX_NOEXCEPT
  2133. { return _M_get_allocator(); }
  2134. /**
  2135. * @brief Find position of a C substring.
  2136. * @param __s C string to locate.
  2137. * @param __pos Index of character to search from.
  2138. * @param __n Number of characters from @a s to search for.
  2139. * @return Index of start of first occurrence.
  2140. *
  2141. * Starting from @a __pos, searches forward for the first @a
  2142. * __n characters in @a __s within this string. If found,
  2143. * returns the index where it begins. If not found, returns
  2144. * npos.
  2145. */
  2146. size_type
  2147. find(const _CharT* __s, size_type __pos, size_type __n) const
  2148. _GLIBCXX_NOEXCEPT;
  2149. /**
  2150. * @brief Find position of a string.
  2151. * @param __str String to locate.
  2152. * @param __pos Index of character to search from (default 0).
  2153. * @return Index of start of first occurrence.
  2154. *
  2155. * Starting from @a __pos, searches forward for value of @a __str within
  2156. * this string. If found, returns the index where it begins. If not
  2157. * found, returns npos.
  2158. */
  2159. size_type
  2160. find(const basic_string& __str, size_type __pos = 0) const
  2161. _GLIBCXX_NOEXCEPT
  2162. { return this->find(__str.data(), __pos, __str.size()); }
  2163. #if __cplusplus >= 201703L
  2164. /**
  2165. * @brief Find position of a string_view.
  2166. * @param __svt The object convertible to string_view to locate.
  2167. * @param __pos Index of character to search from (default 0).
  2168. * @return Index of start of first occurrence.
  2169. */
  2170. template<typename _Tp>
  2171. _If_sv<_Tp, size_type>
  2172. find(const _Tp& __svt, size_type __pos = 0) const
  2173. noexcept(is_same<_Tp, __sv_type>::value)
  2174. {
  2175. __sv_type __sv = __svt;
  2176. return this->find(__sv.data(), __pos, __sv.size());
  2177. }
  2178. #endif // C++17
  2179. /**
  2180. * @brief Find position of a C string.
  2181. * @param __s C string to locate.
  2182. * @param __pos Index of character to search from (default 0).
  2183. * @return Index of start of first occurrence.
  2184. *
  2185. * Starting from @a __pos, searches forward for the value of @a
  2186. * __s within this string. If found, returns the index where
  2187. * it begins. If not found, returns npos.
  2188. */
  2189. size_type
  2190. find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
  2191. {
  2192. __glibcxx_requires_string(__s);
  2193. return this->find(__s, __pos, traits_type::length(__s));
  2194. }
  2195. /**
  2196. * @brief Find position of a character.
  2197. * @param __c Character to locate.
  2198. * @param __pos Index of character to search from (default 0).
  2199. * @return Index of first occurrence.
  2200. *
  2201. * Starting from @a __pos, searches forward for @a __c within
  2202. * this string. If found, returns the index where it was
  2203. * found. If not found, returns npos.
  2204. */
  2205. size_type
  2206. find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
  2207. /**
  2208. * @brief Find last position of a string.
  2209. * @param __str String to locate.
  2210. * @param __pos Index of character to search back from (default end).
  2211. * @return Index of start of last occurrence.
  2212. *
  2213. * Starting from @a __pos, searches backward for value of @a
  2214. * __str within this string. If found, returns the index where
  2215. * it begins. If not found, returns npos.
  2216. */
  2217. size_type
  2218. rfind(const basic_string& __str, size_type __pos = npos) const
  2219. _GLIBCXX_NOEXCEPT
  2220. { return this->rfind(__str.data(), __pos, __str.size()); }
  2221. #if __cplusplus >= 201703L
  2222. /**
  2223. * @brief Find last position of a string_view.
  2224. * @param __svt The object convertible to string_view to locate.
  2225. * @param __pos Index of character to search back from (default end).
  2226. * @return Index of start of last occurrence.
  2227. */
  2228. template<typename _Tp>
  2229. _If_sv<_Tp, size_type>
  2230. rfind(const _Tp& __svt, size_type __pos = npos) const
  2231. noexcept(is_same<_Tp, __sv_type>::value)
  2232. {
  2233. __sv_type __sv = __svt;
  2234. return this->rfind(__sv.data(), __pos, __sv.size());
  2235. }
  2236. #endif // C++17
  2237. /**
  2238. * @brief Find last position of a C substring.
  2239. * @param __s C string to locate.
  2240. * @param __pos Index of character to search back from.
  2241. * @param __n Number of characters from s to search for.
  2242. * @return Index of start of last occurrence.
  2243. *
  2244. * Starting from @a __pos, searches backward for the first @a
  2245. * __n characters in @a __s within this string. If found,
  2246. * returns the index where it begins. If not found, returns
  2247. * npos.
  2248. */
  2249. size_type
  2250. rfind(const _CharT* __s, size_type __pos, size_type __n) const
  2251. _GLIBCXX_NOEXCEPT;
  2252. /**
  2253. * @brief Find last position of a C string.
  2254. * @param __s C string to locate.
  2255. * @param __pos Index of character to start search at (default end).
  2256. * @return Index of start of last occurrence.
  2257. *
  2258. * Starting from @a __pos, searches backward for the value of
  2259. * @a __s within this string. If found, returns the index
  2260. * where it begins. If not found, returns npos.
  2261. */
  2262. size_type
  2263. rfind(const _CharT* __s, size_type __pos = npos) const
  2264. {
  2265. __glibcxx_requires_string(__s);
  2266. return this->rfind(__s, __pos, traits_type::length(__s));
  2267. }
  2268. /**
  2269. * @brief Find last position of a character.
  2270. * @param __c Character to locate.
  2271. * @param __pos Index of character to search back from (default end).
  2272. * @return Index of last occurrence.
  2273. *
  2274. * Starting from @a __pos, searches backward for @a __c within
  2275. * this string. If found, returns the index where it was
  2276. * found. If not found, returns npos.
  2277. */
  2278. size_type
  2279. rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
  2280. /**
  2281. * @brief Find position of a character of string.
  2282. * @param __str String containing characters to locate.
  2283. * @param __pos Index of character to search from (default 0).
  2284. * @return Index of first occurrence.
  2285. *
  2286. * Starting from @a __pos, searches forward for one of the
  2287. * characters of @a __str within this string. If found,
  2288. * returns the index where it was found. If not found, returns
  2289. * npos.
  2290. */
  2291. size_type
  2292. find_first_of(const basic_string& __str, size_type __pos = 0) const
  2293. _GLIBCXX_NOEXCEPT
  2294. { return this->find_first_of(__str.data(), __pos, __str.size()); }
  2295. #if __cplusplus >= 201703L
  2296. /**
  2297. * @brief Find position of a character of a string_view.
  2298. * @param __svt An object convertible to string_view containing
  2299. * characters to locate.
  2300. * @param __pos Index of character to search from (default 0).
  2301. * @return Index of first occurrence.
  2302. */
  2303. template<typename _Tp>
  2304. _If_sv<_Tp, size_type>
  2305. find_first_of(const _Tp& __svt, size_type __pos = 0) const
  2306. noexcept(is_same<_Tp, __sv_type>::value)
  2307. {
  2308. __sv_type __sv = __svt;
  2309. return this->find_first_of(__sv.data(), __pos, __sv.size());
  2310. }
  2311. #endif // C++17
  2312. /**
  2313. * @brief Find position of a character of C substring.
  2314. * @param __s String containing characters to locate.
  2315. * @param __pos Index of character to search from.
  2316. * @param __n Number of characters from s to search for.
  2317. * @return Index of first occurrence.
  2318. *
  2319. * Starting from @a __pos, searches forward for one of the
  2320. * first @a __n characters of @a __s within this string. If
  2321. * found, returns the index where it was found. If not found,
  2322. * returns npos.
  2323. */
  2324. size_type
  2325. find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
  2326. _GLIBCXX_NOEXCEPT;
  2327. /**
  2328. * @brief Find position of a character of C string.
  2329. * @param __s String containing characters to locate.
  2330. * @param __pos Index of character to search from (default 0).
  2331. * @return Index of first occurrence.
  2332. *
  2333. * Starting from @a __pos, searches forward for one of the
  2334. * characters of @a __s within this string. If found, returns
  2335. * the index where it was found. If not found, returns npos.
  2336. */
  2337. size_type
  2338. find_first_of(const _CharT* __s, size_type __pos = 0) const
  2339. _GLIBCXX_NOEXCEPT
  2340. {
  2341. __glibcxx_requires_string(__s);
  2342. return this->find_first_of(__s, __pos, traits_type::length(__s));
  2343. }
  2344. /**
  2345. * @brief Find position of a character.
  2346. * @param __c Character to locate.
  2347. * @param __pos Index of character to search from (default 0).
  2348. * @return Index of first occurrence.
  2349. *
  2350. * Starting from @a __pos, searches forward for the character
  2351. * @a __c within this string. If found, returns the index
  2352. * where it was found. If not found, returns npos.
  2353. *
  2354. * Note: equivalent to find(__c, __pos).
  2355. */
  2356. size_type
  2357. find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
  2358. { return this->find(__c, __pos); }
  2359. /**
  2360. * @brief Find last position of a character of string.
  2361. * @param __str String containing characters to locate.
  2362. * @param __pos Index of character to search back from (default end).
  2363. * @return Index of last occurrence.
  2364. *
  2365. * Starting from @a __pos, searches backward for one of the
  2366. * characters of @a __str within this string. If found,
  2367. * returns the index where it was found. If not found, returns
  2368. * npos.
  2369. */
  2370. size_type
  2371. find_last_of(const basic_string& __str, size_type __pos = npos) const
  2372. _GLIBCXX_NOEXCEPT
  2373. { return this->find_last_of(__str.data(), __pos, __str.size()); }
  2374. #if __cplusplus >= 201703L
  2375. /**
  2376. * @brief Find last position of a character of string.
  2377. * @param __svt An object convertible to string_view containing
  2378. * characters to locate.
  2379. * @param __pos Index of character to search back from (default end).
  2380. * @return Index of last occurrence.
  2381. */
  2382. template<typename _Tp>
  2383. _If_sv<_Tp, size_type>
  2384. find_last_of(const _Tp& __svt, size_type __pos = npos) const
  2385. noexcept(is_same<_Tp, __sv_type>::value)
  2386. {
  2387. __sv_type __sv = __svt;
  2388. return this->find_last_of(__sv.data(), __pos, __sv.size());
  2389. }
  2390. #endif // C++17
  2391. /**
  2392. * @brief Find last position of a character of C substring.
  2393. * @param __s C string containing characters to locate.
  2394. * @param __pos Index of character to search back from.
  2395. * @param __n Number of characters from s to search for.
  2396. * @return Index of last occurrence.
  2397. *
  2398. * Starting from @a __pos, searches backward for one of the
  2399. * first @a __n characters of @a __s within this string. If
  2400. * found, returns the index where it was found. If not found,
  2401. * returns npos.
  2402. */
  2403. size_type
  2404. find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
  2405. _GLIBCXX_NOEXCEPT;
  2406. /**
  2407. * @brief Find last position of a character of C string.
  2408. * @param __s C string containing characters to locate.
  2409. * @param __pos Index of character to search back from (default end).
  2410. * @return Index of last occurrence.
  2411. *
  2412. * Starting from @a __pos, searches backward for one of the
  2413. * characters of @a __s within this string. If found, returns
  2414. * the index where it was found. If not found, returns npos.
  2415. */
  2416. size_type
  2417. find_last_of(const _CharT* __s, size_type __pos = npos) const
  2418. _GLIBCXX_NOEXCEPT
  2419. {
  2420. __glibcxx_requires_string(__s);
  2421. return this->find_last_of(__s, __pos, traits_type::length(__s));
  2422. }
  2423. /**
  2424. * @brief Find last position of a character.
  2425. * @param __c Character to locate.
  2426. * @param __pos Index of character to search back from (default end).
  2427. * @return Index of last occurrence.
  2428. *
  2429. * Starting from @a __pos, searches backward for @a __c within
  2430. * this string. If found, returns the index where it was
  2431. * found. If not found, returns npos.
  2432. *
  2433. * Note: equivalent to rfind(__c, __pos).
  2434. */
  2435. size_type
  2436. find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
  2437. { return this->rfind(__c, __pos); }
  2438. /**
  2439. * @brief Find position of a character not in string.
  2440. * @param __str String containing characters to avoid.
  2441. * @param __pos Index of character to search from (default 0).
  2442. * @return Index of first occurrence.
  2443. *
  2444. * Starting from @a __pos, searches forward for a character not contained
  2445. * in @a __str within this string. If found, returns the index where it
  2446. * was found. If not found, returns npos.
  2447. */
  2448. size_type
  2449. find_first_not_of(const basic_string& __str, size_type __pos = 0) const
  2450. _GLIBCXX_NOEXCEPT
  2451. { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
  2452. #if __cplusplus >= 201703L
  2453. /**
  2454. * @brief Find position of a character not in a string_view.
  2455. * @param __svt A object convertible to string_view containing
  2456. * characters to avoid.
  2457. * @param __pos Index of character to search from (default 0).
  2458. * @return Index of first occurrence.
  2459. */
  2460. template<typename _Tp>
  2461. _If_sv<_Tp, size_type>
  2462. find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
  2463. noexcept(is_same<_Tp, __sv_type>::value)
  2464. {
  2465. __sv_type __sv = __svt;
  2466. return this->find_first_not_of(__sv.data(), __pos, __sv.size());
  2467. }
  2468. #endif // C++17
  2469. /**
  2470. * @brief Find position of a character not in C substring.
  2471. * @param __s C string containing characters to avoid.
  2472. * @param __pos Index of character to search from.
  2473. * @param __n Number of characters from __s to consider.
  2474. * @return Index of first occurrence.
  2475. *
  2476. * Starting from @a __pos, searches forward for a character not
  2477. * contained in the first @a __n characters of @a __s within
  2478. * this string. If found, returns the index where it was
  2479. * found. If not found, returns npos.
  2480. */
  2481. size_type
  2482. find_first_not_of(const _CharT* __s, size_type __pos,
  2483. size_type __n) const _GLIBCXX_NOEXCEPT;
  2484. /**
  2485. * @brief Find position of a character not in C string.
  2486. * @param __s C string containing characters to avoid.
  2487. * @param __pos Index of character to search from (default 0).
  2488. * @return Index of first occurrence.
  2489. *
  2490. * Starting from @a __pos, searches forward for a character not
  2491. * contained in @a __s within this string. If found, returns
  2492. * the index where it was found. If not found, returns npos.
  2493. */
  2494. size_type
  2495. find_first_not_of(const _CharT* __s, size_type __pos = 0) const
  2496. _GLIBCXX_NOEXCEPT
  2497. {
  2498. __glibcxx_requires_string(__s);
  2499. return this->find_first_not_of(__s, __pos, traits_type::length(__s));
  2500. }
  2501. /**
  2502. * @brief Find position of a different character.
  2503. * @param __c Character to avoid.
  2504. * @param __pos Index of character to search from (default 0).
  2505. * @return Index of first occurrence.
  2506. *
  2507. * Starting from @a __pos, searches forward for a character
  2508. * other than @a __c within this string. If found, returns the
  2509. * index where it was found. If not found, returns npos.
  2510. */
  2511. size_type
  2512. find_first_not_of(_CharT __c, size_type __pos = 0) const
  2513. _GLIBCXX_NOEXCEPT;
  2514. /**
  2515. * @brief Find last position of a character not in string.
  2516. * @param __str String containing characters to avoid.
  2517. * @param __pos Index of character to search back from (default end).
  2518. * @return Index of last occurrence.
  2519. *
  2520. * Starting from @a __pos, searches backward for a character
  2521. * not contained in @a __str within this string. If found,
  2522. * returns the index where it was found. If not found, returns
  2523. * npos.
  2524. */
  2525. size_type
  2526. find_last_not_of(const basic_string& __str, size_type __pos = npos) const
  2527. _GLIBCXX_NOEXCEPT
  2528. { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
  2529. #if __cplusplus >= 201703L
  2530. /**
  2531. * @brief Find last position of a character not in a string_view.
  2532. * @param __svt An object convertible to string_view containing
  2533. * characters to avoid.
  2534. * @param __pos Index of character to search back from (default end).
  2535. * @return Index of last occurrence.
  2536. */
  2537. template<typename _Tp>
  2538. _If_sv<_Tp, size_type>
  2539. find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
  2540. noexcept(is_same<_Tp, __sv_type>::value)
  2541. {
  2542. __sv_type __sv = __svt;
  2543. return this->find_last_not_of(__sv.data(), __pos, __sv.size());
  2544. }
  2545. #endif // C++17
  2546. /**
  2547. * @brief Find last position of a character not in C substring.
  2548. * @param __s C string containing characters to avoid.
  2549. * @param __pos Index of character to search back from.
  2550. * @param __n Number of characters from s to consider.
  2551. * @return Index of last occurrence.
  2552. *
  2553. * Starting from @a __pos, searches backward for a character not
  2554. * contained in the first @a __n characters of @a __s within this string.
  2555. * If found, returns the index where it was found. If not found,
  2556. * returns npos.
  2557. */
  2558. size_type
  2559. find_last_not_of(const _CharT* __s, size_type __pos,
  2560. size_type __n) const _GLIBCXX_NOEXCEPT;
  2561. /**
  2562. * @brief Find last position of a character not in C string.
  2563. * @param __s C string containing characters to avoid.
  2564. * @param __pos Index of character to search back from (default end).
  2565. * @return Index of last occurrence.
  2566. *
  2567. * Starting from @a __pos, searches backward for a character
  2568. * not contained in @a __s within this string. If found,
  2569. * returns the index where it was found. If not found, returns
  2570. * npos.
  2571. */
  2572. size_type
  2573. find_last_not_of(const _CharT* __s, size_type __pos = npos) const
  2574. _GLIBCXX_NOEXCEPT
  2575. {
  2576. __glibcxx_requires_string(__s);
  2577. return this->find_last_not_of(__s, __pos, traits_type::length(__s));
  2578. }
  2579. /**
  2580. * @brief Find last position of a different character.
  2581. * @param __c Character to avoid.
  2582. * @param __pos Index of character to search back from (default end).
  2583. * @return Index of last occurrence.
  2584. *
  2585. * Starting from @a __pos, searches backward for a character other than
  2586. * @a __c within this string. If found, returns the index where it was
  2587. * found. If not found, returns npos.
  2588. */
  2589. size_type
  2590. find_last_not_of(_CharT __c, size_type __pos = npos) const
  2591. _GLIBCXX_NOEXCEPT;
  2592. /**
  2593. * @brief Get a substring.
  2594. * @param __pos Index of first character (default 0).
  2595. * @param __n Number of characters in substring (default remainder).
  2596. * @return The new string.
  2597. * @throw std::out_of_range If __pos > size().
  2598. *
  2599. * Construct and return a new string using the @a __n
  2600. * characters starting at @a __pos. If the string is too
  2601. * short, use the remainder of the characters. If @a __pos is
  2602. * beyond the end of the string, out_of_range is thrown.
  2603. */
  2604. basic_string
  2605. substr(size_type __pos = 0, size_type __n = npos) const
  2606. { return basic_string(*this,
  2607. _M_check(__pos, "basic_string::substr"), __n); }
  2608. /**
  2609. * @brief Compare to a string.
  2610. * @param __str String to compare against.
  2611. * @return Integer < 0, 0, or > 0.
  2612. *
  2613. * Returns an integer < 0 if this string is ordered before @a
  2614. * __str, 0 if their values are equivalent, or > 0 if this
  2615. * string is ordered after @a __str. Determines the effective
  2616. * length rlen of the strings to compare as the smallest of
  2617. * size() and str.size(). The function then compares the two
  2618. * strings by calling traits::compare(data(), str.data(),rlen).
  2619. * If the result of the comparison is nonzero returns it,
  2620. * otherwise the shorter one is ordered first.
  2621. */
  2622. int
  2623. compare(const basic_string& __str) const
  2624. {
  2625. const size_type __size = this->size();
  2626. const size_type __osize = __str.size();
  2627. const size_type __len = std::min(__size, __osize);
  2628. int __r = traits_type::compare(_M_data(), __str.data(), __len);
  2629. if (!__r)
  2630. __r = _S_compare(__size, __osize);
  2631. return __r;
  2632. }
  2633. #if __cplusplus >= 201703L
  2634. /**
  2635. * @brief Compare to a string_view.
  2636. * @param __svt An object convertible to string_view to compare against.
  2637. * @return Integer < 0, 0, or > 0.
  2638. */
  2639. template<typename _Tp>
  2640. _If_sv<_Tp, int>
  2641. compare(const _Tp& __svt) const
  2642. noexcept(is_same<_Tp, __sv_type>::value)
  2643. {
  2644. __sv_type __sv = __svt;
  2645. const size_type __size = this->size();
  2646. const size_type __osize = __sv.size();
  2647. const size_type __len = std::min(__size, __osize);
  2648. int __r = traits_type::compare(_M_data(), __sv.data(), __len);
  2649. if (!__r)
  2650. __r = _S_compare(__size, __osize);
  2651. return __r;
  2652. }
  2653. /**
  2654. * @brief Compare to a string_view.
  2655. * @param __pos A position in the string to start comparing from.
  2656. * @param __n The number of characters to compare.
  2657. * @param __svt An object convertible to string_view to compare
  2658. * against.
  2659. * @return Integer < 0, 0, or > 0.
  2660. */
  2661. template<typename _Tp>
  2662. _If_sv<_Tp, int>
  2663. compare(size_type __pos, size_type __n, const _Tp& __svt) const
  2664. noexcept(is_same<_Tp, __sv_type>::value)
  2665. {
  2666. __sv_type __sv = __svt;
  2667. return __sv_type(*this).substr(__pos, __n).compare(__sv);
  2668. }
  2669. /**
  2670. * @brief Compare to a string_view.
  2671. * @param __pos1 A position in the string to start comparing from.
  2672. * @param __n1 The number of characters to compare.
  2673. * @param __svt An object convertible to string_view to compare
  2674. * against.
  2675. * @param __pos2 A position in the string_view to start comparing from.
  2676. * @param __n2 The number of characters to compare.
  2677. * @return Integer < 0, 0, or > 0.
  2678. */
  2679. template<typename _Tp>
  2680. _If_sv<_Tp, int>
  2681. compare(size_type __pos1, size_type __n1, const _Tp& __svt,
  2682. size_type __pos2, size_type __n2 = npos) const
  2683. noexcept(is_same<_Tp, __sv_type>::value)
  2684. {
  2685. __sv_type __sv = __svt;
  2686. return __sv_type(*this)
  2687. .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
  2688. }
  2689. #endif // C++17
  2690. /**
  2691. * @brief Compare substring to a string.
  2692. * @param __pos Index of first character of substring.
  2693. * @param __n Number of characters in substring.
  2694. * @param __str String to compare against.
  2695. * @return Integer < 0, 0, or > 0.
  2696. *
  2697. * Form the substring of this string from the @a __n characters
  2698. * starting at @a __pos. Returns an integer < 0 if the
  2699. * substring is ordered before @a __str, 0 if their values are
  2700. * equivalent, or > 0 if the substring is ordered after @a
  2701. * __str. Determines the effective length rlen of the strings
  2702. * to compare as the smallest of the length of the substring
  2703. * and @a __str.size(). The function then compares the two
  2704. * strings by calling
  2705. * traits::compare(substring.data(),str.data(),rlen). If the
  2706. * result of the comparison is nonzero returns it, otherwise
  2707. * the shorter one is ordered first.
  2708. */
  2709. int
  2710. compare(size_type __pos, size_type __n, const basic_string& __str) const;
  2711. /**
  2712. * @brief Compare substring to a substring.
  2713. * @param __pos1 Index of first character of substring.
  2714. * @param __n1 Number of characters in substring.
  2715. * @param __str String to compare against.
  2716. * @param __pos2 Index of first character of substring of str.
  2717. * @param __n2 Number of characters in substring of str.
  2718. * @return Integer < 0, 0, or > 0.
  2719. *
  2720. * Form the substring of this string from the @a __n1
  2721. * characters starting at @a __pos1. Form the substring of @a
  2722. * __str from the @a __n2 characters starting at @a __pos2.
  2723. * Returns an integer < 0 if this substring is ordered before
  2724. * the substring of @a __str, 0 if their values are equivalent,
  2725. * or > 0 if this substring is ordered after the substring of
  2726. * @a __str. Determines the effective length rlen of the
  2727. * strings to compare as the smallest of the lengths of the
  2728. * substrings. The function then compares the two strings by
  2729. * calling
  2730. * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
  2731. * If the result of the comparison is nonzero returns it,
  2732. * otherwise the shorter one is ordered first.
  2733. */
  2734. int
  2735. compare(size_type __pos1, size_type __n1, const basic_string& __str,
  2736. size_type __pos2, size_type __n2 = npos) const;
  2737. /**
  2738. * @brief Compare to a C string.
  2739. * @param __s C string to compare against.
  2740. * @return Integer < 0, 0, or > 0.
  2741. *
  2742. * Returns an integer < 0 if this string is ordered before @a __s, 0 if
  2743. * their values are equivalent, or > 0 if this string is ordered after
  2744. * @a __s. Determines the effective length rlen of the strings to
  2745. * compare as the smallest of size() and the length of a string
  2746. * constructed from @a __s. The function then compares the two strings
  2747. * by calling traits::compare(data(),s,rlen). If the result of the
  2748. * comparison is nonzero returns it, otherwise the shorter one is
  2749. * ordered first.
  2750. */
  2751. int
  2752. compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT;
  2753. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  2754. // 5 String::compare specification questionable
  2755. /**
  2756. * @brief Compare substring to a C string.
  2757. * @param __pos Index of first character of substring.
  2758. * @param __n1 Number of characters in substring.
  2759. * @param __s C string to compare against.
  2760. * @return Integer < 0, 0, or > 0.
  2761. *
  2762. * Form the substring of this string from the @a __n1
  2763. * characters starting at @a pos. Returns an integer < 0 if
  2764. * the substring is ordered before @a __s, 0 if their values
  2765. * are equivalent, or > 0 if the substring is ordered after @a
  2766. * __s. Determines the effective length rlen of the strings to
  2767. * compare as the smallest of the length of the substring and
  2768. * the length of a string constructed from @a __s. The
  2769. * function then compares the two string by calling
  2770. * traits::compare(substring.data(),__s,rlen). If the result of
  2771. * the comparison is nonzero returns it, otherwise the shorter
  2772. * one is ordered first.
  2773. */
  2774. int
  2775. compare(size_type __pos, size_type __n1, const _CharT* __s) const;
  2776. /**
  2777. * @brief Compare substring against a character %array.
  2778. * @param __pos Index of first character of substring.
  2779. * @param __n1 Number of characters in substring.
  2780. * @param __s character %array to compare against.
  2781. * @param __n2 Number of characters of s.
  2782. * @return Integer < 0, 0, or > 0.
  2783. *
  2784. * Form the substring of this string from the @a __n1
  2785. * characters starting at @a __pos. Form a string from the
  2786. * first @a __n2 characters of @a __s. Returns an integer < 0
  2787. * if this substring is ordered before the string from @a __s,
  2788. * 0 if their values are equivalent, or > 0 if this substring
  2789. * is ordered after the string from @a __s. Determines the
  2790. * effective length rlen of the strings to compare as the
  2791. * smallest of the length of the substring and @a __n2. The
  2792. * function then compares the two strings by calling
  2793. * traits::compare(substring.data(),s,rlen). If the result of
  2794. * the comparison is nonzero returns it, otherwise the shorter
  2795. * one is ordered first.
  2796. *
  2797. * NB: s must have at least n2 characters, &apos;\\0&apos; has
  2798. * no special meaning.
  2799. */
  2800. int
  2801. compare(size_type __pos, size_type __n1, const _CharT* __s,
  2802. size_type __n2) const;
  2803. #if __cplusplus > 201703L
  2804. bool
  2805. starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept
  2806. { return __sv_type(this->data(), this->size()).starts_with(__x); }
  2807. bool
  2808. starts_with(_CharT __x) const noexcept
  2809. { return __sv_type(this->data(), this->size()).starts_with(__x); }
  2810. bool
  2811. starts_with(const _CharT* __x) const noexcept
  2812. { return __sv_type(this->data(), this->size()).starts_with(__x); }
  2813. bool
  2814. ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept
  2815. { return __sv_type(this->data(), this->size()).ends_with(__x); }
  2816. bool
  2817. ends_with(_CharT __x) const noexcept
  2818. { return __sv_type(this->data(), this->size()).ends_with(__x); }
  2819. bool
  2820. ends_with(const _CharT* __x) const noexcept
  2821. { return __sv_type(this->data(), this->size()).ends_with(__x); }
  2822. #endif // C++20
  2823. // Allow basic_stringbuf::__xfer_bufptrs to call _M_length:
  2824. template<typename, typename, typename> friend class basic_stringbuf;
  2825. };
  2826. _GLIBCXX_END_NAMESPACE_CXX11
  2827. #else // !_GLIBCXX_USE_CXX11_ABI
  2828. // Reference-counted COW string implentation
  2829. /**
  2830. * @class basic_string basic_string.h <string>
  2831. * @brief Managing sequences of characters and character-like objects.
  2832. *
  2833. * @ingroup strings
  2834. * @ingroup sequences
  2835. *
  2836. * @tparam _CharT Type of character
  2837. * @tparam _Traits Traits for character type, defaults to
  2838. * char_traits<_CharT>.
  2839. * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
  2840. *
  2841. * Meets the requirements of a <a href="tables.html#65">container</a>, a
  2842. * <a href="tables.html#66">reversible container</a>, and a
  2843. * <a href="tables.html#67">sequence</a>. Of the
  2844. * <a href="tables.html#68">optional sequence requirements</a>, only
  2845. * @c push_back, @c at, and @c %array access are supported.
  2846. *
  2847. * @doctodo
  2848. *
  2849. *
  2850. * Documentation? What's that?
  2851. * Nathan Myers <ncm@cantrip.org>.
  2852. *
  2853. * A string looks like this:
  2854. *
  2855. * @code
  2856. * [_Rep]
  2857. * _M_length
  2858. * [basic_string<char_type>] _M_capacity
  2859. * _M_dataplus _M_refcount
  2860. * _M_p ----------------> unnamed array of char_type
  2861. * @endcode
  2862. *
  2863. * Where the _M_p points to the first character in the string, and
  2864. * you cast it to a pointer-to-_Rep and subtract 1 to get a
  2865. * pointer to the header.
  2866. *
  2867. * This approach has the enormous advantage that a string object
  2868. * requires only one allocation. All the ugliness is confined
  2869. * within a single %pair of inline functions, which each compile to
  2870. * a single @a add instruction: _Rep::_M_data(), and
  2871. * string::_M_rep(); and the allocation function which gets a
  2872. * block of raw bytes and with room enough and constructs a _Rep
  2873. * object at the front.
  2874. *
  2875. * The reason you want _M_data pointing to the character %array and
  2876. * not the _Rep is so that the debugger can see the string
  2877. * contents. (Probably we should add a non-inline member to get
  2878. * the _Rep for the debugger to use, so users can check the actual
  2879. * string length.)
  2880. *
  2881. * Note that the _Rep object is a POD so that you can have a
  2882. * static <em>empty string</em> _Rep object already @a constructed before
  2883. * static constructors have run. The reference-count encoding is
  2884. * chosen so that a 0 indicates one reference, so you never try to
  2885. * destroy the empty-string _Rep object.
  2886. *
  2887. * All but the last paragraph is considered pretty conventional
  2888. * for a C++ string implementation.
  2889. */
  2890. // 21.3 Template class basic_string
  2891. template<typename _CharT, typename _Traits, typename _Alloc>
  2892. class basic_string
  2893. {
  2894. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  2895. rebind<_CharT>::other _CharT_alloc_type;
  2896. typedef __gnu_cxx::__alloc_traits<_CharT_alloc_type> _CharT_alloc_traits;
  2897. // Types:
  2898. public:
  2899. typedef _Traits traits_type;
  2900. typedef typename _Traits::char_type value_type;
  2901. typedef _Alloc allocator_type;
  2902. typedef typename _CharT_alloc_type::size_type size_type;
  2903. typedef typename _CharT_alloc_type::difference_type difference_type;
  2904. #if __cplusplus < 201103L
  2905. typedef typename _CharT_alloc_type::reference reference;
  2906. typedef typename _CharT_alloc_type::const_reference const_reference;
  2907. #else
  2908. typedef value_type& reference;
  2909. typedef const value_type& const_reference;
  2910. #endif
  2911. typedef typename _CharT_alloc_traits::pointer pointer;
  2912. typedef typename _CharT_alloc_traits::const_pointer const_pointer;
  2913. typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
  2914. typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
  2915. const_iterator;
  2916. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  2917. typedef std::reverse_iterator<iterator> reverse_iterator;
  2918. protected:
  2919. // type used for positions in insert, erase etc.
  2920. typedef iterator __const_iterator;
  2921. private:
  2922. // _Rep: string representation
  2923. // Invariants:
  2924. // 1. String really contains _M_length + 1 characters: due to 21.3.4
  2925. // must be kept null-terminated.
  2926. // 2. _M_capacity >= _M_length
  2927. // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
  2928. // 3. _M_refcount has three states:
  2929. // -1: leaked, one reference, no ref-copies allowed, non-const.
  2930. // 0: one reference, non-const.
  2931. // n>0: n + 1 references, operations require a lock, const.
  2932. // 4. All fields==0 is an empty string, given the extra storage
  2933. // beyond-the-end for a null terminator; thus, the shared
  2934. // empty string representation needs no constructor.
  2935. struct _Rep_base
  2936. {
  2937. size_type _M_length;
  2938. size_type _M_capacity;
  2939. _Atomic_word _M_refcount;
  2940. };
  2941. struct _Rep : _Rep_base
  2942. {
  2943. // Types:
  2944. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  2945. rebind<char>::other _Raw_bytes_alloc;
  2946. // (Public) Data members:
  2947. // The maximum number of individual char_type elements of an
  2948. // individual string is determined by _S_max_size. This is the
  2949. // value that will be returned by max_size(). (Whereas npos
  2950. // is the maximum number of bytes the allocator can allocate.)
  2951. // If one was to divvy up the theoretical largest size string,
  2952. // with a terminating character and m _CharT elements, it'd
  2953. // look like this:
  2954. // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
  2955. // Solving for m:
  2956. // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
  2957. // In addition, this implementation quarters this amount.
  2958. static const size_type _S_max_size;
  2959. static const _CharT _S_terminal;
  2960. // The following storage is init'd to 0 by the linker, resulting
  2961. // (carefully) in an empty string with one reference.
  2962. static size_type _S_empty_rep_storage[];
  2963. static _Rep&
  2964. _S_empty_rep() _GLIBCXX_NOEXCEPT
  2965. {
  2966. // NB: Mild hack to avoid strict-aliasing warnings. Note that
  2967. // _S_empty_rep_storage is never modified and the punning should
  2968. // be reasonably safe in this case.
  2969. void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage);
  2970. return *reinterpret_cast<_Rep*>(__p);
  2971. }
  2972. bool
  2973. _M_is_leaked() const _GLIBCXX_NOEXCEPT
  2974. {
  2975. #if defined(__GTHREADS)
  2976. // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
  2977. // so we need to use an atomic load. However, _M_is_leaked
  2978. // predicate does not change concurrently (i.e. the string is either
  2979. // leaked or not), so a relaxed load is enough.
  2980. return __atomic_load_n(&this->_M_refcount, __ATOMIC_RELAXED) < 0;
  2981. #else
  2982. return this->_M_refcount < 0;
  2983. #endif
  2984. }
  2985. bool
  2986. _M_is_shared() const _GLIBCXX_NOEXCEPT
  2987. {
  2988. #if defined(__GTHREADS)
  2989. // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
  2990. // so we need to use an atomic load. Another thread can drop last
  2991. // but one reference concurrently with this check, so we need this
  2992. // load to be acquire to synchronize with release fetch_and_add in
  2993. // _M_dispose.
  2994. return __atomic_load_n(&this->_M_refcount, __ATOMIC_ACQUIRE) > 0;
  2995. #else
  2996. return this->_M_refcount > 0;
  2997. #endif
  2998. }
  2999. void
  3000. _M_set_leaked() _GLIBCXX_NOEXCEPT
  3001. { this->_M_refcount = -1; }
  3002. void
  3003. _M_set_sharable() _GLIBCXX_NOEXCEPT
  3004. { this->_M_refcount = 0; }
  3005. void
  3006. _M_set_length_and_sharable(size_type __n) _GLIBCXX_NOEXCEPT
  3007. {
  3008. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  3009. if (__builtin_expect(this != &_S_empty_rep(), false))
  3010. #endif
  3011. {
  3012. this->_M_set_sharable(); // One reference.
  3013. this->_M_length = __n;
  3014. traits_type::assign(this->_M_refdata()[__n], _S_terminal);
  3015. // grrr. (per 21.3.4)
  3016. // You cannot leave those LWG people alone for a second.
  3017. }
  3018. }
  3019. _CharT*
  3020. _M_refdata() throw()
  3021. { return reinterpret_cast<_CharT*>(this + 1); }
  3022. _CharT*
  3023. _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
  3024. {
  3025. return (!_M_is_leaked() && __alloc1 == __alloc2)
  3026. ? _M_refcopy() : _M_clone(__alloc1);
  3027. }
  3028. // Create & Destroy
  3029. static _Rep*
  3030. _S_create(size_type, size_type, const _Alloc&);
  3031. void
  3032. _M_dispose(const _Alloc& __a) _GLIBCXX_NOEXCEPT
  3033. {
  3034. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  3035. if (__builtin_expect(this != &_S_empty_rep(), false))
  3036. #endif
  3037. {
  3038. // Be race-detector-friendly. For more info see bits/c++config.
  3039. _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount);
  3040. // Decrement of _M_refcount is acq_rel, because:
  3041. // - all but last decrements need to release to synchronize with
  3042. // the last decrement that will delete the object.
  3043. // - the last decrement needs to acquire to synchronize with
  3044. // all the previous decrements.
  3045. // - last but one decrement needs to release to synchronize with
  3046. // the acquire load in _M_is_shared that will conclude that
  3047. // the object is not shared anymore.
  3048. if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
  3049. -1) <= 0)
  3050. {
  3051. _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount);
  3052. _M_destroy(__a);
  3053. }
  3054. }
  3055. } // XXX MT
  3056. void
  3057. _M_destroy(const _Alloc&) throw();
  3058. _CharT*
  3059. _M_refcopy() throw()
  3060. {
  3061. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  3062. if (__builtin_expect(this != &_S_empty_rep(), false))
  3063. #endif
  3064. __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
  3065. return _M_refdata();
  3066. } // XXX MT
  3067. _CharT*
  3068. _M_clone(const _Alloc&, size_type __res = 0);
  3069. };
  3070. // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
  3071. struct _Alloc_hider : _Alloc
  3072. {
  3073. _Alloc_hider(_CharT* __dat, const _Alloc& __a) _GLIBCXX_NOEXCEPT
  3074. : _Alloc(__a), _M_p(__dat) { }
  3075. _CharT* _M_p; // The actual data.
  3076. };
  3077. public:
  3078. // Data Members (public):
  3079. // NB: This is an unsigned type, and thus represents the maximum
  3080. // size that the allocator can hold.
  3081. /// Value returned by various member functions when they fail.
  3082. static const size_type npos = static_cast<size_type>(-1);
  3083. private:
  3084. // Data Members (private):
  3085. mutable _Alloc_hider _M_dataplus;
  3086. _CharT*
  3087. _M_data() const _GLIBCXX_NOEXCEPT
  3088. { return _M_dataplus._M_p; }
  3089. _CharT*
  3090. _M_data(_CharT* __p) _GLIBCXX_NOEXCEPT
  3091. { return (_M_dataplus._M_p = __p); }
  3092. _Rep*
  3093. _M_rep() const _GLIBCXX_NOEXCEPT
  3094. { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
  3095. // For the internal use we have functions similar to `begin'/`end'
  3096. // but they do not call _M_leak.
  3097. iterator
  3098. _M_ibegin() const _GLIBCXX_NOEXCEPT
  3099. { return iterator(_M_data()); }
  3100. iterator
  3101. _M_iend() const _GLIBCXX_NOEXCEPT
  3102. { return iterator(_M_data() + this->size()); }
  3103. void
  3104. _M_leak() // for use in begin() & non-const op[]
  3105. {
  3106. if (!_M_rep()->_M_is_leaked())
  3107. _M_leak_hard();
  3108. }
  3109. size_type
  3110. _M_check(size_type __pos, const char* __s) const
  3111. {
  3112. if (__pos > this->size())
  3113. __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
  3114. "this->size() (which is %zu)"),
  3115. __s, __pos, this->size());
  3116. return __pos;
  3117. }
  3118. void
  3119. _M_check_length(size_type __n1, size_type __n2, const char* __s) const
  3120. {
  3121. if (this->max_size() - (this->size() - __n1) < __n2)
  3122. __throw_length_error(__N(__s));
  3123. }
  3124. // NB: _M_limit doesn't check for a bad __pos value.
  3125. size_type
  3126. _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
  3127. {
  3128. const bool __testoff = __off < this->size() - __pos;
  3129. return __testoff ? __off : this->size() - __pos;
  3130. }
  3131. // True if _Rep and source do not overlap.
  3132. bool
  3133. _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
  3134. {
  3135. return (less<const _CharT*>()(__s, _M_data())
  3136. || less<const _CharT*>()(_M_data() + this->size(), __s));
  3137. }
  3138. // When __n = 1 way faster than the general multichar
  3139. // traits_type::copy/move/assign.
  3140. static void
  3141. _M_copy(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
  3142. {
  3143. if (__n == 1)
  3144. traits_type::assign(*__d, *__s);
  3145. else
  3146. traits_type::copy(__d, __s, __n);
  3147. }
  3148. static void
  3149. _M_move(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
  3150. {
  3151. if (__n == 1)
  3152. traits_type::assign(*__d, *__s);
  3153. else
  3154. traits_type::move(__d, __s, __n);
  3155. }
  3156. static void
  3157. _M_assign(_CharT* __d, size_type __n, _CharT __c) _GLIBCXX_NOEXCEPT
  3158. {
  3159. if (__n == 1)
  3160. traits_type::assign(*__d, __c);
  3161. else
  3162. traits_type::assign(__d, __n, __c);
  3163. }
  3164. // _S_copy_chars is a separate template to permit specialization
  3165. // to optimize for the common case of pointers as iterators.
  3166. template<class _Iterator>
  3167. static void
  3168. _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
  3169. {
  3170. for (; __k1 != __k2; ++__k1, (void)++__p)
  3171. traits_type::assign(*__p, *__k1); // These types are off.
  3172. }
  3173. static void
  3174. _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
  3175. { _S_copy_chars(__p, __k1.base(), __k2.base()); }
  3176. static void
  3177. _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
  3178. _GLIBCXX_NOEXCEPT
  3179. { _S_copy_chars(__p, __k1.base(), __k2.base()); }
  3180. static void
  3181. _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
  3182. { _M_copy(__p, __k1, __k2 - __k1); }
  3183. static void
  3184. _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
  3185. _GLIBCXX_NOEXCEPT
  3186. { _M_copy(__p, __k1, __k2 - __k1); }
  3187. static int
  3188. _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
  3189. {
  3190. const difference_type __d = difference_type(__n1 - __n2);
  3191. if (__d > __gnu_cxx::__numeric_traits<int>::__max)
  3192. return __gnu_cxx::__numeric_traits<int>::__max;
  3193. else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
  3194. return __gnu_cxx::__numeric_traits<int>::__min;
  3195. else
  3196. return int(__d);
  3197. }
  3198. void
  3199. _M_mutate(size_type __pos, size_type __len1, size_type __len2);
  3200. void
  3201. _M_leak_hard();
  3202. static _Rep&
  3203. _S_empty_rep() _GLIBCXX_NOEXCEPT
  3204. { return _Rep::_S_empty_rep(); }
  3205. #if __cplusplus >= 201703L
  3206. // A helper type for avoiding boiler-plate.
  3207. typedef basic_string_view<_CharT, _Traits> __sv_type;
  3208. template<typename _Tp, typename _Res>
  3209. using _If_sv = enable_if_t<
  3210. __and_<is_convertible<const _Tp&, __sv_type>,
  3211. __not_<is_convertible<const _Tp*, const basic_string*>>,
  3212. __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
  3213. _Res>;
  3214. // Allows an implicit conversion to __sv_type.
  3215. static __sv_type
  3216. _S_to_string_view(__sv_type __svt) noexcept
  3217. { return __svt; }
  3218. // Wraps a string_view by explicit conversion and thus
  3219. // allows to add an internal constructor that does not
  3220. // participate in overload resolution when a string_view
  3221. // is provided.
  3222. struct __sv_wrapper
  3223. {
  3224. explicit __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
  3225. __sv_type _M_sv;
  3226. };
  3227. /**
  3228. * @brief Only internally used: Construct string from a string view
  3229. * wrapper.
  3230. * @param __svw string view wrapper.
  3231. * @param __a Allocator to use.
  3232. */
  3233. explicit
  3234. basic_string(__sv_wrapper __svw, const _Alloc& __a)
  3235. : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
  3236. #endif
  3237. public:
  3238. // Construct/copy/destroy:
  3239. // NB: We overload ctors in some cases instead of using default
  3240. // arguments, per 17.4.4.4 para. 2 item 2.
  3241. /**
  3242. * @brief Default constructor creates an empty string.
  3243. */
  3244. basic_string()
  3245. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  3246. _GLIBCXX_NOEXCEPT
  3247. : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc())
  3248. #else
  3249. : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc())
  3250. #endif
  3251. { }
  3252. /**
  3253. * @brief Construct an empty string using allocator @a a.
  3254. */
  3255. explicit
  3256. basic_string(const _Alloc& __a);
  3257. // NB: per LWG issue 42, semantics different from IS:
  3258. /**
  3259. * @brief Construct string with copy of value of @a str.
  3260. * @param __str Source string.
  3261. */
  3262. basic_string(const basic_string& __str);
  3263. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  3264. // 2583. no way to supply an allocator for basic_string(str, pos)
  3265. /**
  3266. * @brief Construct string as copy of a substring.
  3267. * @param __str Source string.
  3268. * @param __pos Index of first character to copy from.
  3269. * @param __a Allocator to use.
  3270. */
  3271. basic_string(const basic_string& __str, size_type __pos,
  3272. const _Alloc& __a = _Alloc());
  3273. /**
  3274. * @brief Construct string as copy of a substring.
  3275. * @param __str Source string.
  3276. * @param __pos Index of first character to copy from.
  3277. * @param __n Number of characters to copy.
  3278. */
  3279. basic_string(const basic_string& __str, size_type __pos,
  3280. size_type __n);
  3281. /**
  3282. * @brief Construct string as copy of a substring.
  3283. * @param __str Source string.
  3284. * @param __pos Index of first character to copy from.
  3285. * @param __n Number of characters to copy.
  3286. * @param __a Allocator to use.
  3287. */
  3288. basic_string(const basic_string& __str, size_type __pos,
  3289. size_type __n, const _Alloc& __a);
  3290. /**
  3291. * @brief Construct string initialized by a character %array.
  3292. * @param __s Source character %array.
  3293. * @param __n Number of characters to copy.
  3294. * @param __a Allocator to use (default is default allocator).
  3295. *
  3296. * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
  3297. * has no special meaning.
  3298. */
  3299. basic_string(const _CharT* __s, size_type __n,
  3300. const _Alloc& __a = _Alloc());
  3301. /**
  3302. * @brief Construct string as copy of a C string.
  3303. * @param __s Source C string.
  3304. * @param __a Allocator to use (default is default allocator).
  3305. */
  3306. #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
  3307. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  3308. // 3076. basic_string CTAD ambiguity
  3309. template<typename = _RequireAllocator<_Alloc>>
  3310. #endif
  3311. basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
  3312. : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) :
  3313. __s + npos, __a), __a)
  3314. { }
  3315. /**
  3316. * @brief Construct string as multiple characters.
  3317. * @param __n Number of characters.
  3318. * @param __c Character to use.
  3319. * @param __a Allocator to use (default is default allocator).
  3320. */
  3321. basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
  3322. #if __cplusplus >= 201103L
  3323. /**
  3324. * @brief Move construct string.
  3325. * @param __str Source string.
  3326. *
  3327. * The newly-created string contains the exact contents of @a __str.
  3328. * @a __str is a valid, but unspecified string.
  3329. **/
  3330. basic_string(basic_string&& __str)
  3331. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  3332. noexcept // FIXME C++11: should always be noexcept.
  3333. #endif
  3334. : _M_dataplus(std::move(__str._M_dataplus))
  3335. {
  3336. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  3337. __str._M_data(_S_empty_rep()._M_refdata());
  3338. #else
  3339. __str._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
  3340. #endif
  3341. }
  3342. /**
  3343. * @brief Construct string from an initializer %list.
  3344. * @param __l std::initializer_list of characters.
  3345. * @param __a Allocator to use (default is default allocator).
  3346. */
  3347. basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc());
  3348. basic_string(const basic_string& __str, const _Alloc& __a)
  3349. : _M_dataplus(__str._M_rep()->_M_grab(__a, __str.get_allocator()), __a)
  3350. { }
  3351. basic_string(basic_string&& __str, const _Alloc& __a)
  3352. : _M_dataplus(__str._M_data(), __a)
  3353. {
  3354. if (__a == __str.get_allocator())
  3355. {
  3356. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  3357. __str._M_data(_S_empty_rep()._M_refdata());
  3358. #else
  3359. __str._M_data(_S_construct(size_type(), _CharT(), __a));
  3360. #endif
  3361. }
  3362. else
  3363. _M_dataplus._M_p = _S_construct(__str.begin(), __str.end(), __a);
  3364. }
  3365. #endif // C++11
  3366. /**
  3367. * @brief Construct string as copy of a range.
  3368. * @param __beg Start of range.
  3369. * @param __end End of range.
  3370. * @param __a Allocator to use (default is default allocator).
  3371. */
  3372. template<class _InputIterator>
  3373. basic_string(_InputIterator __beg, _InputIterator __end,
  3374. const _Alloc& __a = _Alloc());
  3375. #if __cplusplus >= 201703L
  3376. /**
  3377. * @brief Construct string from a substring of a string_view.
  3378. * @param __t Source object convertible to string view.
  3379. * @param __pos The index of the first character to copy from __t.
  3380. * @param __n The number of characters to copy from __t.
  3381. * @param __a Allocator to use.
  3382. */
  3383. template<typename _Tp, typename = _If_sv<_Tp, void>>
  3384. basic_string(const _Tp& __t, size_type __pos, size_type __n,
  3385. const _Alloc& __a = _Alloc())
  3386. : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
  3387. /**
  3388. * @brief Construct string from a string_view.
  3389. * @param __t Source object convertible to string view.
  3390. * @param __a Allocator to use (default is default allocator).
  3391. */
  3392. template<typename _Tp, typename = _If_sv<_Tp, void>>
  3393. explicit
  3394. basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
  3395. : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
  3396. #endif // C++17
  3397. /**
  3398. * @brief Destroy the string instance.
  3399. */
  3400. ~basic_string() _GLIBCXX_NOEXCEPT
  3401. { _M_rep()->_M_dispose(this->get_allocator()); }
  3402. /**
  3403. * @brief Assign the value of @a str to this string.
  3404. * @param __str Source string.
  3405. */
  3406. basic_string&
  3407. operator=(const basic_string& __str)
  3408. { return this->assign(__str); }
  3409. /**
  3410. * @brief Copy contents of @a s into this string.
  3411. * @param __s Source null-terminated string.
  3412. */
  3413. basic_string&
  3414. operator=(const _CharT* __s)
  3415. { return this->assign(__s); }
  3416. /**
  3417. * @brief Set value to string of length 1.
  3418. * @param __c Source character.
  3419. *
  3420. * Assigning to a character makes this string length 1 and
  3421. * (*this)[0] == @a c.
  3422. */
  3423. basic_string&
  3424. operator=(_CharT __c)
  3425. {
  3426. this->assign(1, __c);
  3427. return *this;
  3428. }
  3429. #if __cplusplus >= 201103L
  3430. /**
  3431. * @brief Move assign the value of @a str to this string.
  3432. * @param __str Source string.
  3433. *
  3434. * The contents of @a str are moved into this string (without copying).
  3435. * @a str is a valid, but unspecified string.
  3436. **/
  3437. basic_string&
  3438. operator=(basic_string&& __str)
  3439. _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value)
  3440. {
  3441. // NB: DR 1204.
  3442. this->swap(__str);
  3443. return *this;
  3444. }
  3445. /**
  3446. * @brief Set value to string constructed from initializer %list.
  3447. * @param __l std::initializer_list.
  3448. */
  3449. basic_string&
  3450. operator=(initializer_list<_CharT> __l)
  3451. {
  3452. this->assign(__l.begin(), __l.size());
  3453. return *this;
  3454. }
  3455. #endif // C++11
  3456. #if __cplusplus >= 201703L
  3457. /**
  3458. * @brief Set value to string constructed from a string_view.
  3459. * @param __svt An object convertible to string_view.
  3460. */
  3461. template<typename _Tp>
  3462. _If_sv<_Tp, basic_string&>
  3463. operator=(const _Tp& __svt)
  3464. { return this->assign(__svt); }
  3465. /**
  3466. * @brief Convert to a string_view.
  3467. * @return A string_view.
  3468. */
  3469. operator __sv_type() const noexcept
  3470. { return __sv_type(data(), size()); }
  3471. #endif // C++17
  3472. // Iterators:
  3473. /**
  3474. * Returns a read/write iterator that points to the first character in
  3475. * the %string. Unshares the string.
  3476. */
  3477. iterator
  3478. begin() // FIXME C++11: should be noexcept.
  3479. {
  3480. _M_leak();
  3481. return iterator(_M_data());
  3482. }
  3483. /**
  3484. * Returns a read-only (constant) iterator that points to the first
  3485. * character in the %string.
  3486. */
  3487. const_iterator
  3488. begin() const _GLIBCXX_NOEXCEPT
  3489. { return const_iterator(_M_data()); }
  3490. /**
  3491. * Returns a read/write iterator that points one past the last
  3492. * character in the %string. Unshares the string.
  3493. */
  3494. iterator
  3495. end() // FIXME C++11: should be noexcept.
  3496. {
  3497. _M_leak();
  3498. return iterator(_M_data() + this->size());
  3499. }
  3500. /**
  3501. * Returns a read-only (constant) iterator that points one past the
  3502. * last character in the %string.
  3503. */
  3504. const_iterator
  3505. end() const _GLIBCXX_NOEXCEPT
  3506. { return const_iterator(_M_data() + this->size()); }
  3507. /**
  3508. * Returns a read/write reverse iterator that points to the last
  3509. * character in the %string. Iteration is done in reverse element
  3510. * order. Unshares the string.
  3511. */
  3512. reverse_iterator
  3513. rbegin() // FIXME C++11: should be noexcept.
  3514. { return reverse_iterator(this->end()); }
  3515. /**
  3516. * Returns a read-only (constant) reverse iterator that points
  3517. * to the last character in the %string. Iteration is done in
  3518. * reverse element order.
  3519. */
  3520. const_reverse_iterator
  3521. rbegin() const _GLIBCXX_NOEXCEPT
  3522. { return const_reverse_iterator(this->end()); }
  3523. /**
  3524. * Returns a read/write reverse iterator that points to one before the
  3525. * first character in the %string. Iteration is done in reverse
  3526. * element order. Unshares the string.
  3527. */
  3528. reverse_iterator
  3529. rend() // FIXME C++11: should be noexcept.
  3530. { return reverse_iterator(this->begin()); }
  3531. /**
  3532. * Returns a read-only (constant) reverse iterator that points
  3533. * to one before the first character in the %string. Iteration
  3534. * is done in reverse element order.
  3535. */
  3536. const_reverse_iterator
  3537. rend() const _GLIBCXX_NOEXCEPT
  3538. { return const_reverse_iterator(this->begin()); }
  3539. #if __cplusplus >= 201103L
  3540. /**
  3541. * Returns a read-only (constant) iterator that points to the first
  3542. * character in the %string.
  3543. */
  3544. const_iterator
  3545. cbegin() const noexcept
  3546. { return const_iterator(this->_M_data()); }
  3547. /**
  3548. * Returns a read-only (constant) iterator that points one past the
  3549. * last character in the %string.
  3550. */
  3551. const_iterator
  3552. cend() const noexcept
  3553. { return const_iterator(this->_M_data() + this->size()); }
  3554. /**
  3555. * Returns a read-only (constant) reverse iterator that points
  3556. * to the last character in the %string. Iteration is done in
  3557. * reverse element order.
  3558. */
  3559. const_reverse_iterator
  3560. crbegin() const noexcept
  3561. { return const_reverse_iterator(this->end()); }
  3562. /**
  3563. * Returns a read-only (constant) reverse iterator that points
  3564. * to one before the first character in the %string. Iteration
  3565. * is done in reverse element order.
  3566. */
  3567. const_reverse_iterator
  3568. crend() const noexcept
  3569. { return const_reverse_iterator(this->begin()); }
  3570. #endif
  3571. public:
  3572. // Capacity:
  3573. /// Returns the number of characters in the string, not including any
  3574. /// null-termination.
  3575. size_type
  3576. size() const _GLIBCXX_NOEXCEPT
  3577. { return _M_rep()->_M_length; }
  3578. /// Returns the number of characters in the string, not including any
  3579. /// null-termination.
  3580. size_type
  3581. length() const _GLIBCXX_NOEXCEPT
  3582. { return _M_rep()->_M_length; }
  3583. /// Returns the size() of the largest possible %string.
  3584. size_type
  3585. max_size() const _GLIBCXX_NOEXCEPT
  3586. { return _Rep::_S_max_size; }
  3587. /**
  3588. * @brief Resizes the %string to the specified number of characters.
  3589. * @param __n Number of characters the %string should contain.
  3590. * @param __c Character to fill any new elements.
  3591. *
  3592. * This function will %resize the %string to the specified
  3593. * number of characters. If the number is smaller than the
  3594. * %string's current size the %string is truncated, otherwise
  3595. * the %string is extended and new elements are %set to @a __c.
  3596. */
  3597. void
  3598. resize(size_type __n, _CharT __c);
  3599. /**
  3600. * @brief Resizes the %string to the specified number of characters.
  3601. * @param __n Number of characters the %string should contain.
  3602. *
  3603. * This function will resize the %string to the specified length. If
  3604. * the new size is smaller than the %string's current size the %string
  3605. * is truncated, otherwise the %string is extended and new characters
  3606. * are default-constructed. For basic types such as char, this means
  3607. * setting them to 0.
  3608. */
  3609. void
  3610. resize(size_type __n)
  3611. { this->resize(__n, _CharT()); }
  3612. #if __cplusplus >= 201103L
  3613. /// A non-binding request to reduce capacity() to size().
  3614. void
  3615. shrink_to_fit() _GLIBCXX_NOEXCEPT
  3616. {
  3617. #if __cpp_exceptions
  3618. if (capacity() > size())
  3619. {
  3620. try
  3621. { reserve(0); }
  3622. catch(...)
  3623. { }
  3624. }
  3625. #endif
  3626. }
  3627. #endif
  3628. /**
  3629. * Returns the total number of characters that the %string can hold
  3630. * before needing to allocate more memory.
  3631. */
  3632. size_type
  3633. capacity() const _GLIBCXX_NOEXCEPT
  3634. { return _M_rep()->_M_capacity; }
  3635. /**
  3636. * @brief Attempt to preallocate enough memory for specified number of
  3637. * characters.
  3638. * @param __res_arg Number of characters required.
  3639. * @throw std::length_error If @a __res_arg exceeds @c max_size().
  3640. *
  3641. * This function attempts to reserve enough memory for the
  3642. * %string to hold the specified number of characters. If the
  3643. * number requested is more than max_size(), length_error is
  3644. * thrown.
  3645. *
  3646. * The advantage of this function is that if optimal code is a
  3647. * necessity and the user can determine the string length that will be
  3648. * required, the user can reserve the memory in %advance, and thus
  3649. * prevent a possible reallocation of memory and copying of %string
  3650. * data.
  3651. */
  3652. void
  3653. reserve(size_type __res_arg = 0);
  3654. /**
  3655. * Erases the string, making it empty.
  3656. */
  3657. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  3658. void
  3659. clear() _GLIBCXX_NOEXCEPT
  3660. {
  3661. if (_M_rep()->_M_is_shared())
  3662. {
  3663. _M_rep()->_M_dispose(this->get_allocator());
  3664. _M_data(_S_empty_rep()._M_refdata());
  3665. }
  3666. else
  3667. _M_rep()->_M_set_length_and_sharable(0);
  3668. }
  3669. #else
  3670. // PR 56166: this should not throw.
  3671. void
  3672. clear()
  3673. { _M_mutate(0, this->size(), 0); }
  3674. #endif
  3675. /**
  3676. * Returns true if the %string is empty. Equivalent to
  3677. * <code>*this == ""</code>.
  3678. */
  3679. _GLIBCXX_NODISCARD bool
  3680. empty() const _GLIBCXX_NOEXCEPT
  3681. { return this->size() == 0; }
  3682. // Element access:
  3683. /**
  3684. * @brief Subscript access to the data contained in the %string.
  3685. * @param __pos The index of the character to access.
  3686. * @return Read-only (constant) reference to the character.
  3687. *
  3688. * This operator allows for easy, array-style, data access.
  3689. * Note that data access with this operator is unchecked and
  3690. * out_of_range lookups are not defined. (For checked lookups
  3691. * see at().)
  3692. */
  3693. const_reference
  3694. operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
  3695. {
  3696. __glibcxx_assert(__pos <= size());
  3697. return _M_data()[__pos];
  3698. }
  3699. /**
  3700. * @brief Subscript access to the data contained in the %string.
  3701. * @param __pos The index of the character to access.
  3702. * @return Read/write reference to the character.
  3703. *
  3704. * This operator allows for easy, array-style, data access.
  3705. * Note that data access with this operator is unchecked and
  3706. * out_of_range lookups are not defined. (For checked lookups
  3707. * see at().) Unshares the string.
  3708. */
  3709. reference
  3710. operator[](size_type __pos)
  3711. {
  3712. // Allow pos == size() both in C++98 mode, as v3 extension,
  3713. // and in C++11 mode.
  3714. __glibcxx_assert(__pos <= size());
  3715. // In pedantic mode be strict in C++98 mode.
  3716. _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
  3717. _M_leak();
  3718. return _M_data()[__pos];
  3719. }
  3720. /**
  3721. * @brief Provides access to the data contained in the %string.
  3722. * @param __n The index of the character to access.
  3723. * @return Read-only (const) reference to the character.
  3724. * @throw std::out_of_range If @a n is an invalid index.
  3725. *
  3726. * This function provides for safer data access. The parameter is
  3727. * first checked that it is in the range of the string. The function
  3728. * throws out_of_range if the check fails.
  3729. */
  3730. const_reference
  3731. at(size_type __n) const
  3732. {
  3733. if (__n >= this->size())
  3734. __throw_out_of_range_fmt(__N("basic_string::at: __n "
  3735. "(which is %zu) >= this->size() "
  3736. "(which is %zu)"),
  3737. __n, this->size());
  3738. return _M_data()[__n];
  3739. }
  3740. /**
  3741. * @brief Provides access to the data contained in the %string.
  3742. * @param __n The index of the character to access.
  3743. * @return Read/write reference to the character.
  3744. * @throw std::out_of_range If @a n is an invalid index.
  3745. *
  3746. * This function provides for safer data access. The parameter is
  3747. * first checked that it is in the range of the string. The function
  3748. * throws out_of_range if the check fails. Success results in
  3749. * unsharing the string.
  3750. */
  3751. reference
  3752. at(size_type __n)
  3753. {
  3754. if (__n >= size())
  3755. __throw_out_of_range_fmt(__N("basic_string::at: __n "
  3756. "(which is %zu) >= this->size() "
  3757. "(which is %zu)"),
  3758. __n, this->size());
  3759. _M_leak();
  3760. return _M_data()[__n];
  3761. }
  3762. #if __cplusplus >= 201103L
  3763. /**
  3764. * Returns a read/write reference to the data at the first
  3765. * element of the %string.
  3766. */
  3767. reference
  3768. front()
  3769. {
  3770. __glibcxx_assert(!empty());
  3771. return operator[](0);
  3772. }
  3773. /**
  3774. * Returns a read-only (constant) reference to the data at the first
  3775. * element of the %string.
  3776. */
  3777. const_reference
  3778. front() const noexcept
  3779. {
  3780. __glibcxx_assert(!empty());
  3781. return operator[](0);
  3782. }
  3783. /**
  3784. * Returns a read/write reference to the data at the last
  3785. * element of the %string.
  3786. */
  3787. reference
  3788. back()
  3789. {
  3790. __glibcxx_assert(!empty());
  3791. return operator[](this->size() - 1);
  3792. }
  3793. /**
  3794. * Returns a read-only (constant) reference to the data at the
  3795. * last element of the %string.
  3796. */
  3797. const_reference
  3798. back() const noexcept
  3799. {
  3800. __glibcxx_assert(!empty());
  3801. return operator[](this->size() - 1);
  3802. }
  3803. #endif
  3804. // Modifiers:
  3805. /**
  3806. * @brief Append a string to this string.
  3807. * @param __str The string to append.
  3808. * @return Reference to this string.
  3809. */
  3810. basic_string&
  3811. operator+=(const basic_string& __str)
  3812. { return this->append(__str); }
  3813. /**
  3814. * @brief Append a C string.
  3815. * @param __s The C string to append.
  3816. * @return Reference to this string.
  3817. */
  3818. basic_string&
  3819. operator+=(const _CharT* __s)
  3820. { return this->append(__s); }
  3821. /**
  3822. * @brief Append a character.
  3823. * @param __c The character to append.
  3824. * @return Reference to this string.
  3825. */
  3826. basic_string&
  3827. operator+=(_CharT __c)
  3828. {
  3829. this->push_back(__c);
  3830. return *this;
  3831. }
  3832. #if __cplusplus >= 201103L
  3833. /**
  3834. * @brief Append an initializer_list of characters.
  3835. * @param __l The initializer_list of characters to be appended.
  3836. * @return Reference to this string.
  3837. */
  3838. basic_string&
  3839. operator+=(initializer_list<_CharT> __l)
  3840. { return this->append(__l.begin(), __l.size()); }
  3841. #endif // C++11
  3842. #if __cplusplus >= 201703L
  3843. /**
  3844. * @brief Append a string_view.
  3845. * @param __svt The object convertible to string_view to be appended.
  3846. * @return Reference to this string.
  3847. */
  3848. template<typename _Tp>
  3849. _If_sv<_Tp, basic_string&>
  3850. operator+=(const _Tp& __svt)
  3851. { return this->append(__svt); }
  3852. #endif // C++17
  3853. /**
  3854. * @brief Append a string to this string.
  3855. * @param __str The string to append.
  3856. * @return Reference to this string.
  3857. */
  3858. basic_string&
  3859. append(const basic_string& __str);
  3860. /**
  3861. * @brief Append a substring.
  3862. * @param __str The string to append.
  3863. * @param __pos Index of the first character of str to append.
  3864. * @param __n The number of characters to append.
  3865. * @return Reference to this string.
  3866. * @throw std::out_of_range if @a __pos is not a valid index.
  3867. *
  3868. * This function appends @a __n characters from @a __str
  3869. * starting at @a __pos to this string. If @a __n is is larger
  3870. * than the number of available characters in @a __str, the
  3871. * remainder of @a __str is appended.
  3872. */
  3873. basic_string&
  3874. append(const basic_string& __str, size_type __pos, size_type __n = npos);
  3875. /**
  3876. * @brief Append a C substring.
  3877. * @param __s The C string to append.
  3878. * @param __n The number of characters to append.
  3879. * @return Reference to this string.
  3880. */
  3881. basic_string&
  3882. append(const _CharT* __s, size_type __n);
  3883. /**
  3884. * @brief Append a C string.
  3885. * @param __s The C string to append.
  3886. * @return Reference to this string.
  3887. */
  3888. basic_string&
  3889. append(const _CharT* __s)
  3890. {
  3891. __glibcxx_requires_string(__s);
  3892. return this->append(__s, traits_type::length(__s));
  3893. }
  3894. /**
  3895. * @brief Append multiple characters.
  3896. * @param __n The number of characters to append.
  3897. * @param __c The character to use.
  3898. * @return Reference to this string.
  3899. *
  3900. * Appends __n copies of __c to this string.
  3901. */
  3902. basic_string&
  3903. append(size_type __n, _CharT __c);
  3904. #if __cplusplus >= 201103L
  3905. /**
  3906. * @brief Append an initializer_list of characters.
  3907. * @param __l The initializer_list of characters to append.
  3908. * @return Reference to this string.
  3909. */
  3910. basic_string&
  3911. append(initializer_list<_CharT> __l)
  3912. { return this->append(__l.begin(), __l.size()); }
  3913. #endif // C++11
  3914. /**
  3915. * @brief Append a range of characters.
  3916. * @param __first Iterator referencing the first character to append.
  3917. * @param __last Iterator marking the end of the range.
  3918. * @return Reference to this string.
  3919. *
  3920. * Appends characters in the range [__first,__last) to this string.
  3921. */
  3922. template<class _InputIterator>
  3923. basic_string&
  3924. append(_InputIterator __first, _InputIterator __last)
  3925. { return this->replace(_M_iend(), _M_iend(), __first, __last); }
  3926. #if __cplusplus >= 201703L
  3927. /**
  3928. * @brief Append a string_view.
  3929. * @param __svt The object convertible to string_view to be appended.
  3930. * @return Reference to this string.
  3931. */
  3932. template<typename _Tp>
  3933. _If_sv<_Tp, basic_string&>
  3934. append(const _Tp& __svt)
  3935. {
  3936. __sv_type __sv = __svt;
  3937. return this->append(__sv.data(), __sv.size());
  3938. }
  3939. /**
  3940. * @brief Append a range of characters from a string_view.
  3941. * @param __svt The object convertible to string_view to be appended
  3942. * from.
  3943. * @param __pos The position in the string_view to append from.
  3944. * @param __n The number of characters to append from the string_view.
  3945. * @return Reference to this string.
  3946. */
  3947. template<typename _Tp>
  3948. _If_sv<_Tp, basic_string&>
  3949. append(const _Tp& __svt, size_type __pos, size_type __n = npos)
  3950. {
  3951. __sv_type __sv = __svt;
  3952. return append(__sv.data()
  3953. + std::__sv_check(__sv.size(), __pos, "basic_string::append"),
  3954. std::__sv_limit(__sv.size(), __pos, __n));
  3955. }
  3956. #endif // C++17
  3957. /**
  3958. * @brief Append a single character.
  3959. * @param __c Character to append.
  3960. */
  3961. void
  3962. push_back(_CharT __c)
  3963. {
  3964. const size_type __len = 1 + this->size();
  3965. if (__len > this->capacity() || _M_rep()->_M_is_shared())
  3966. this->reserve(__len);
  3967. traits_type::assign(_M_data()[this->size()], __c);
  3968. _M_rep()->_M_set_length_and_sharable(__len);
  3969. }
  3970. /**
  3971. * @brief Set value to contents of another string.
  3972. * @param __str Source string to use.
  3973. * @return Reference to this string.
  3974. */
  3975. basic_string&
  3976. assign(const basic_string& __str);
  3977. #if __cplusplus >= 201103L
  3978. /**
  3979. * @brief Set value to contents of another string.
  3980. * @param __str Source string to use.
  3981. * @return Reference to this string.
  3982. *
  3983. * This function sets this string to the exact contents of @a __str.
  3984. * @a __str is a valid, but unspecified string.
  3985. */
  3986. basic_string&
  3987. assign(basic_string&& __str)
  3988. noexcept(allocator_traits<_Alloc>::is_always_equal::value)
  3989. {
  3990. this->swap(__str);
  3991. return *this;
  3992. }
  3993. #endif // C++11
  3994. /**
  3995. * @brief Set value to a substring of a string.
  3996. * @param __str The string to use.
  3997. * @param __pos Index of the first character of str.
  3998. * @param __n Number of characters to use.
  3999. * @return Reference to this string.
  4000. * @throw std::out_of_range if @a pos is not a valid index.
  4001. *
  4002. * This function sets this string to the substring of @a __str
  4003. * consisting of @a __n characters at @a __pos. If @a __n is
  4004. * is larger than the number of available characters in @a
  4005. * __str, the remainder of @a __str is used.
  4006. */
  4007. basic_string&
  4008. assign(const basic_string& __str, size_type __pos, size_type __n = npos)
  4009. { return this->assign(__str._M_data()
  4010. + __str._M_check(__pos, "basic_string::assign"),
  4011. __str._M_limit(__pos, __n)); }
  4012. /**
  4013. * @brief Set value to a C substring.
  4014. * @param __s The C string to use.
  4015. * @param __n Number of characters to use.
  4016. * @return Reference to this string.
  4017. *
  4018. * This function sets the value of this string to the first @a __n
  4019. * characters of @a __s. If @a __n is is larger than the number of
  4020. * available characters in @a __s, the remainder of @a __s is used.
  4021. */
  4022. basic_string&
  4023. assign(const _CharT* __s, size_type __n);
  4024. /**
  4025. * @brief Set value to contents of a C string.
  4026. * @param __s The C string to use.
  4027. * @return Reference to this string.
  4028. *
  4029. * This function sets the value of this string to the value of @a __s.
  4030. * The data is copied, so there is no dependence on @a __s once the
  4031. * function returns.
  4032. */
  4033. basic_string&
  4034. assign(const _CharT* __s)
  4035. {
  4036. __glibcxx_requires_string(__s);
  4037. return this->assign(__s, traits_type::length(__s));
  4038. }
  4039. /**
  4040. * @brief Set value to multiple characters.
  4041. * @param __n Length of the resulting string.
  4042. * @param __c The character to use.
  4043. * @return Reference to this string.
  4044. *
  4045. * This function sets the value of this string to @a __n copies of
  4046. * character @a __c.
  4047. */
  4048. basic_string&
  4049. assign(size_type __n, _CharT __c)
  4050. { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
  4051. /**
  4052. * @brief Set value to a range of characters.
  4053. * @param __first Iterator referencing the first character to append.
  4054. * @param __last Iterator marking the end of the range.
  4055. * @return Reference to this string.
  4056. *
  4057. * Sets value of string to characters in the range [__first,__last).
  4058. */
  4059. template<class _InputIterator>
  4060. basic_string&
  4061. assign(_InputIterator __first, _InputIterator __last)
  4062. { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
  4063. #if __cplusplus >= 201103L
  4064. /**
  4065. * @brief Set value to an initializer_list of characters.
  4066. * @param __l The initializer_list of characters to assign.
  4067. * @return Reference to this string.
  4068. */
  4069. basic_string&
  4070. assign(initializer_list<_CharT> __l)
  4071. { return this->assign(__l.begin(), __l.size()); }
  4072. #endif // C++11
  4073. #if __cplusplus >= 201703L
  4074. /**
  4075. * @brief Set value from a string_view.
  4076. * @param __svt The source object convertible to string_view.
  4077. * @return Reference to this string.
  4078. */
  4079. template<typename _Tp>
  4080. _If_sv<_Tp, basic_string&>
  4081. assign(const _Tp& __svt)
  4082. {
  4083. __sv_type __sv = __svt;
  4084. return this->assign(__sv.data(), __sv.size());
  4085. }
  4086. /**
  4087. * @brief Set value from a range of characters in a string_view.
  4088. * @param __svt The source object convertible to string_view.
  4089. * @param __pos The position in the string_view to assign from.
  4090. * @param __n The number of characters to assign.
  4091. * @return Reference to this string.
  4092. */
  4093. template<typename _Tp>
  4094. _If_sv<_Tp, basic_string&>
  4095. assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
  4096. {
  4097. __sv_type __sv = __svt;
  4098. return assign(__sv.data()
  4099. + std::__sv_check(__sv.size(), __pos, "basic_string::assign"),
  4100. std::__sv_limit(__sv.size(), __pos, __n));
  4101. }
  4102. #endif // C++17
  4103. /**
  4104. * @brief Insert multiple characters.
  4105. * @param __p Iterator referencing location in string to insert at.
  4106. * @param __n Number of characters to insert
  4107. * @param __c The character to insert.
  4108. * @throw std::length_error If new length exceeds @c max_size().
  4109. *
  4110. * Inserts @a __n copies of character @a __c starting at the
  4111. * position referenced by iterator @a __p. If adding
  4112. * characters causes the length to exceed max_size(),
  4113. * length_error is thrown. The value of the string doesn't
  4114. * change if an error is thrown.
  4115. */
  4116. void
  4117. insert(iterator __p, size_type __n, _CharT __c)
  4118. { this->replace(__p, __p, __n, __c); }
  4119. /**
  4120. * @brief Insert a range of characters.
  4121. * @param __p Iterator referencing location in string to insert at.
  4122. * @param __beg Start of range.
  4123. * @param __end End of range.
  4124. * @throw std::length_error If new length exceeds @c max_size().
  4125. *
  4126. * Inserts characters in range [__beg,__end). If adding
  4127. * characters causes the length to exceed max_size(),
  4128. * length_error is thrown. The value of the string doesn't
  4129. * change if an error is thrown.
  4130. */
  4131. template<class _InputIterator>
  4132. void
  4133. insert(iterator __p, _InputIterator __beg, _InputIterator __end)
  4134. { this->replace(__p, __p, __beg, __end); }
  4135. #if __cplusplus >= 201103L
  4136. /**
  4137. * @brief Insert an initializer_list of characters.
  4138. * @param __p Iterator referencing location in string to insert at.
  4139. * @param __l The initializer_list of characters to insert.
  4140. * @throw std::length_error If new length exceeds @c max_size().
  4141. */
  4142. void
  4143. insert(iterator __p, initializer_list<_CharT> __l)
  4144. {
  4145. _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
  4146. this->insert(__p - _M_ibegin(), __l.begin(), __l.size());
  4147. }
  4148. #endif // C++11
  4149. /**
  4150. * @brief Insert value of a string.
  4151. * @param __pos1 Position in string to insert at.
  4152. * @param __str The string to insert.
  4153. * @return Reference to this string.
  4154. * @throw std::length_error If new length exceeds @c max_size().
  4155. *
  4156. * Inserts value of @a __str starting at @a __pos1. If adding
  4157. * characters causes the length to exceed max_size(),
  4158. * length_error is thrown. The value of the string doesn't
  4159. * change if an error is thrown.
  4160. */
  4161. basic_string&
  4162. insert(size_type __pos1, const basic_string& __str)
  4163. { return this->insert(__pos1, __str, size_type(0), __str.size()); }
  4164. /**
  4165. * @brief Insert a substring.
  4166. * @param __pos1 Position in string to insert at.
  4167. * @param __str The string to insert.
  4168. * @param __pos2 Start of characters in str to insert.
  4169. * @param __n Number of characters to insert.
  4170. * @return Reference to this string.
  4171. * @throw std::length_error If new length exceeds @c max_size().
  4172. * @throw std::out_of_range If @a pos1 > size() or
  4173. * @a __pos2 > @a str.size().
  4174. *
  4175. * Starting at @a pos1, insert @a __n character of @a __str
  4176. * beginning with @a __pos2. If adding characters causes the
  4177. * length to exceed max_size(), length_error is thrown. If @a
  4178. * __pos1 is beyond the end of this string or @a __pos2 is
  4179. * beyond the end of @a __str, out_of_range is thrown. The
  4180. * value of the string doesn't change if an error is thrown.
  4181. */
  4182. basic_string&
  4183. insert(size_type __pos1, const basic_string& __str,
  4184. size_type __pos2, size_type __n = npos)
  4185. { return this->insert(__pos1, __str._M_data()
  4186. + __str._M_check(__pos2, "basic_string::insert"),
  4187. __str._M_limit(__pos2, __n)); }
  4188. /**
  4189. * @brief Insert a C substring.
  4190. * @param __pos Position in string to insert at.
  4191. * @param __s The C string to insert.
  4192. * @param __n The number of characters to insert.
  4193. * @return Reference to this string.
  4194. * @throw std::length_error If new length exceeds @c max_size().
  4195. * @throw std::out_of_range If @a __pos is beyond the end of this
  4196. * string.
  4197. *
  4198. * Inserts the first @a __n characters of @a __s starting at @a
  4199. * __pos. If adding characters causes the length to exceed
  4200. * max_size(), length_error is thrown. If @a __pos is beyond
  4201. * end(), out_of_range is thrown. The value of the string
  4202. * doesn't change if an error is thrown.
  4203. */
  4204. basic_string&
  4205. insert(size_type __pos, const _CharT* __s, size_type __n);
  4206. /**
  4207. * @brief Insert a C string.
  4208. * @param __pos Position in string to insert at.
  4209. * @param __s The C string to insert.
  4210. * @return Reference to this string.
  4211. * @throw std::length_error If new length exceeds @c max_size().
  4212. * @throw std::out_of_range If @a pos is beyond the end of this
  4213. * string.
  4214. *
  4215. * Inserts the first @a n characters of @a __s starting at @a __pos. If
  4216. * adding characters causes the length to exceed max_size(),
  4217. * length_error is thrown. If @a __pos is beyond end(), out_of_range is
  4218. * thrown. The value of the string doesn't change if an error is
  4219. * thrown.
  4220. */
  4221. basic_string&
  4222. insert(size_type __pos, const _CharT* __s)
  4223. {
  4224. __glibcxx_requires_string(__s);
  4225. return this->insert(__pos, __s, traits_type::length(__s));
  4226. }
  4227. /**
  4228. * @brief Insert multiple characters.
  4229. * @param __pos Index in string to insert at.
  4230. * @param __n Number of characters to insert
  4231. * @param __c The character to insert.
  4232. * @return Reference to this string.
  4233. * @throw std::length_error If new length exceeds @c max_size().
  4234. * @throw std::out_of_range If @a __pos is beyond the end of this
  4235. * string.
  4236. *
  4237. * Inserts @a __n copies of character @a __c starting at index
  4238. * @a __pos. If adding characters causes the length to exceed
  4239. * max_size(), length_error is thrown. If @a __pos > length(),
  4240. * out_of_range is thrown. The value of the string doesn't
  4241. * change if an error is thrown.
  4242. */
  4243. basic_string&
  4244. insert(size_type __pos, size_type __n, _CharT __c)
  4245. { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
  4246. size_type(0), __n, __c); }
  4247. /**
  4248. * @brief Insert one character.
  4249. * @param __p Iterator referencing position in string to insert at.
  4250. * @param __c The character to insert.
  4251. * @return Iterator referencing newly inserted char.
  4252. * @throw std::length_error If new length exceeds @c max_size().
  4253. *
  4254. * Inserts character @a __c at position referenced by @a __p.
  4255. * If adding character causes the length to exceed max_size(),
  4256. * length_error is thrown. If @a __p is beyond end of string,
  4257. * out_of_range is thrown. The value of the string doesn't
  4258. * change if an error is thrown.
  4259. */
  4260. iterator
  4261. insert(iterator __p, _CharT __c)
  4262. {
  4263. _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
  4264. const size_type __pos = __p - _M_ibegin();
  4265. _M_replace_aux(__pos, size_type(0), size_type(1), __c);
  4266. _M_rep()->_M_set_leaked();
  4267. return iterator(_M_data() + __pos);
  4268. }
  4269. #if __cplusplus >= 201703L
  4270. /**
  4271. * @brief Insert a string_view.
  4272. * @param __pos Position in string to insert at.
  4273. * @param __svt The object convertible to string_view to insert.
  4274. * @return Reference to this string.
  4275. */
  4276. template<typename _Tp>
  4277. _If_sv<_Tp, basic_string&>
  4278. insert(size_type __pos, const _Tp& __svt)
  4279. {
  4280. __sv_type __sv = __svt;
  4281. return this->insert(__pos, __sv.data(), __sv.size());
  4282. }
  4283. /**
  4284. * @brief Insert a string_view.
  4285. * @param __pos Position in string to insert at.
  4286. * @param __svt The object convertible to string_view to insert from.
  4287. * @param __pos Position in string_view to insert
  4288. * from.
  4289. * @param __n The number of characters to insert.
  4290. * @return Reference to this string.
  4291. */
  4292. template<typename _Tp>
  4293. _If_sv<_Tp, basic_string&>
  4294. insert(size_type __pos1, const _Tp& __svt,
  4295. size_type __pos2, size_type __n = npos)
  4296. {
  4297. __sv_type __sv = __svt;
  4298. return this->replace(__pos1, size_type(0), __sv.data()
  4299. + std::__sv_check(__sv.size(), __pos2, "basic_string::insert"),
  4300. std::__sv_limit(__sv.size(), __pos2, __n));
  4301. }
  4302. #endif // C++17
  4303. /**
  4304. * @brief Remove characters.
  4305. * @param __pos Index of first character to remove (default 0).
  4306. * @param __n Number of characters to remove (default remainder).
  4307. * @return Reference to this string.
  4308. * @throw std::out_of_range If @a pos is beyond the end of this
  4309. * string.
  4310. *
  4311. * Removes @a __n characters from this string starting at @a
  4312. * __pos. The length of the string is reduced by @a __n. If
  4313. * there are < @a __n characters to remove, the remainder of
  4314. * the string is truncated. If @a __p is beyond end of string,
  4315. * out_of_range is thrown. The value of the string doesn't
  4316. * change if an error is thrown.
  4317. */
  4318. basic_string&
  4319. erase(size_type __pos = 0, size_type __n = npos)
  4320. {
  4321. _M_mutate(_M_check(__pos, "basic_string::erase"),
  4322. _M_limit(__pos, __n), size_type(0));
  4323. return *this;
  4324. }
  4325. /**
  4326. * @brief Remove one character.
  4327. * @param __position Iterator referencing the character to remove.
  4328. * @return iterator referencing same location after removal.
  4329. *
  4330. * Removes the character at @a __position from this string. The value
  4331. * of the string doesn't change if an error is thrown.
  4332. */
  4333. iterator
  4334. erase(iterator __position)
  4335. {
  4336. _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
  4337. && __position < _M_iend());
  4338. const size_type __pos = __position - _M_ibegin();
  4339. _M_mutate(__pos, size_type(1), size_type(0));
  4340. _M_rep()->_M_set_leaked();
  4341. return iterator(_M_data() + __pos);
  4342. }
  4343. /**
  4344. * @brief Remove a range of characters.
  4345. * @param __first Iterator referencing the first character to remove.
  4346. * @param __last Iterator referencing the end of the range.
  4347. * @return Iterator referencing location of first after removal.
  4348. *
  4349. * Removes the characters in the range [first,last) from this string.
  4350. * The value of the string doesn't change if an error is thrown.
  4351. */
  4352. iterator
  4353. erase(iterator __first, iterator __last);
  4354. #if __cplusplus >= 201103L
  4355. /**
  4356. * @brief Remove the last character.
  4357. *
  4358. * The string must be non-empty.
  4359. */
  4360. void
  4361. pop_back() // FIXME C++11: should be noexcept.
  4362. {
  4363. __glibcxx_assert(!empty());
  4364. erase(size() - 1, 1);
  4365. }
  4366. #endif // C++11
  4367. /**
  4368. * @brief Replace characters with value from another string.
  4369. * @param __pos Index of first character to replace.
  4370. * @param __n Number of characters to be replaced.
  4371. * @param __str String to insert.
  4372. * @return Reference to this string.
  4373. * @throw std::out_of_range If @a pos is beyond the end of this
  4374. * string.
  4375. * @throw std::length_error If new length exceeds @c max_size().
  4376. *
  4377. * Removes the characters in the range [__pos,__pos+__n) from
  4378. * this string. In place, the value of @a __str is inserted.
  4379. * If @a __pos is beyond end of string, out_of_range is thrown.
  4380. * If the length of the result exceeds max_size(), length_error
  4381. * is thrown. The value of the string doesn't change if an
  4382. * error is thrown.
  4383. */
  4384. basic_string&
  4385. replace(size_type __pos, size_type __n, const basic_string& __str)
  4386. { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
  4387. /**
  4388. * @brief Replace characters with value from another string.
  4389. * @param __pos1 Index of first character to replace.
  4390. * @param __n1 Number of characters to be replaced.
  4391. * @param __str String to insert.
  4392. * @param __pos2 Index of first character of str to use.
  4393. * @param __n2 Number of characters from str to use.
  4394. * @return Reference to this string.
  4395. * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
  4396. * __str.size().
  4397. * @throw std::length_error If new length exceeds @c max_size().
  4398. *
  4399. * Removes the characters in the range [__pos1,__pos1 + n) from this
  4400. * string. In place, the value of @a __str is inserted. If @a __pos is
  4401. * beyond end of string, out_of_range is thrown. If the length of the
  4402. * result exceeds max_size(), length_error is thrown. The value of the
  4403. * string doesn't change if an error is thrown.
  4404. */
  4405. basic_string&
  4406. replace(size_type __pos1, size_type __n1, const basic_string& __str,
  4407. size_type __pos2, size_type __n2 = npos)
  4408. { return this->replace(__pos1, __n1, __str._M_data()
  4409. + __str._M_check(__pos2, "basic_string::replace"),
  4410. __str._M_limit(__pos2, __n2)); }
  4411. /**
  4412. * @brief Replace characters with value of a C substring.
  4413. * @param __pos Index of first character to replace.
  4414. * @param __n1 Number of characters to be replaced.
  4415. * @param __s C string to insert.
  4416. * @param __n2 Number of characters from @a s to use.
  4417. * @return Reference to this string.
  4418. * @throw std::out_of_range If @a pos1 > size().
  4419. * @throw std::length_error If new length exceeds @c max_size().
  4420. *
  4421. * Removes the characters in the range [__pos,__pos + __n1)
  4422. * from this string. In place, the first @a __n2 characters of
  4423. * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
  4424. * @a __pos is beyond end of string, out_of_range is thrown. If
  4425. * the length of result exceeds max_size(), length_error is
  4426. * thrown. The value of the string doesn't change if an error
  4427. * is thrown.
  4428. */
  4429. basic_string&
  4430. replace(size_type __pos, size_type __n1, const _CharT* __s,
  4431. size_type __n2);
  4432. /**
  4433. * @brief Replace characters with value of a C string.
  4434. * @param __pos Index of first character to replace.
  4435. * @param __n1 Number of characters to be replaced.
  4436. * @param __s C string to insert.
  4437. * @return Reference to this string.
  4438. * @throw std::out_of_range If @a pos > size().
  4439. * @throw std::length_error If new length exceeds @c max_size().
  4440. *
  4441. * Removes the characters in the range [__pos,__pos + __n1)
  4442. * from this string. In place, the characters of @a __s are
  4443. * inserted. If @a __pos is beyond end of string, out_of_range
  4444. * is thrown. If the length of result exceeds max_size(),
  4445. * length_error is thrown. The value of the string doesn't
  4446. * change if an error is thrown.
  4447. */
  4448. basic_string&
  4449. replace(size_type __pos, size_type __n1, const _CharT* __s)
  4450. {
  4451. __glibcxx_requires_string(__s);
  4452. return this->replace(__pos, __n1, __s, traits_type::length(__s));
  4453. }
  4454. /**
  4455. * @brief Replace characters with multiple characters.
  4456. * @param __pos Index of first character to replace.
  4457. * @param __n1 Number of characters to be replaced.
  4458. * @param __n2 Number of characters to insert.
  4459. * @param __c Character to insert.
  4460. * @return Reference to this string.
  4461. * @throw std::out_of_range If @a __pos > size().
  4462. * @throw std::length_error If new length exceeds @c max_size().
  4463. *
  4464. * Removes the characters in the range [pos,pos + n1) from this
  4465. * string. In place, @a __n2 copies of @a __c are inserted.
  4466. * If @a __pos is beyond end of string, out_of_range is thrown.
  4467. * If the length of result exceeds max_size(), length_error is
  4468. * thrown. The value of the string doesn't change if an error
  4469. * is thrown.
  4470. */
  4471. basic_string&
  4472. replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
  4473. { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
  4474. _M_limit(__pos, __n1), __n2, __c); }
  4475. /**
  4476. * @brief Replace range of characters with string.
  4477. * @param __i1 Iterator referencing start of range to replace.
  4478. * @param __i2 Iterator referencing end of range to replace.
  4479. * @param __str String value to insert.
  4480. * @return Reference to this string.
  4481. * @throw std::length_error If new length exceeds @c max_size().
  4482. *
  4483. * Removes the characters in the range [__i1,__i2). In place,
  4484. * the value of @a __str is inserted. If the length of result
  4485. * exceeds max_size(), length_error is thrown. The value of
  4486. * the string doesn't change if an error is thrown.
  4487. */
  4488. basic_string&
  4489. replace(iterator __i1, iterator __i2, const basic_string& __str)
  4490. { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
  4491. /**
  4492. * @brief Replace range of characters with C substring.
  4493. * @param __i1 Iterator referencing start of range to replace.
  4494. * @param __i2 Iterator referencing end of range to replace.
  4495. * @param __s C string value to insert.
  4496. * @param __n Number of characters from s to insert.
  4497. * @return Reference to this string.
  4498. * @throw std::length_error If new length exceeds @c max_size().
  4499. *
  4500. * Removes the characters in the range [__i1,__i2). In place,
  4501. * the first @a __n characters of @a __s are inserted. If the
  4502. * length of result exceeds max_size(), length_error is thrown.
  4503. * The value of the string doesn't change if an error is
  4504. * thrown.
  4505. */
  4506. basic_string&
  4507. replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
  4508. {
  4509. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  4510. && __i2 <= _M_iend());
  4511. return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
  4512. }
  4513. /**
  4514. * @brief Replace range of characters with C string.
  4515. * @param __i1 Iterator referencing start of range to replace.
  4516. * @param __i2 Iterator referencing end of range to replace.
  4517. * @param __s C string value to insert.
  4518. * @return Reference to this string.
  4519. * @throw std::length_error If new length exceeds @c max_size().
  4520. *
  4521. * Removes the characters in the range [__i1,__i2). In place,
  4522. * the characters of @a __s are inserted. If the length of
  4523. * result exceeds max_size(), length_error is thrown. The
  4524. * value of the string doesn't change if an error is thrown.
  4525. */
  4526. basic_string&
  4527. replace(iterator __i1, iterator __i2, const _CharT* __s)
  4528. {
  4529. __glibcxx_requires_string(__s);
  4530. return this->replace(__i1, __i2, __s, traits_type::length(__s));
  4531. }
  4532. /**
  4533. * @brief Replace range of characters with multiple characters
  4534. * @param __i1 Iterator referencing start of range to replace.
  4535. * @param __i2 Iterator referencing end of range to replace.
  4536. * @param __n Number of characters to insert.
  4537. * @param __c Character to insert.
  4538. * @return Reference to this string.
  4539. * @throw std::length_error If new length exceeds @c max_size().
  4540. *
  4541. * Removes the characters in the range [__i1,__i2). In place,
  4542. * @a __n copies of @a __c are inserted. If the length of
  4543. * result exceeds max_size(), length_error is thrown. The
  4544. * value of the string doesn't change if an error is thrown.
  4545. */
  4546. basic_string&
  4547. replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
  4548. {
  4549. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  4550. && __i2 <= _M_iend());
  4551. return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
  4552. }
  4553. /**
  4554. * @brief Replace range of characters with range.
  4555. * @param __i1 Iterator referencing start of range to replace.
  4556. * @param __i2 Iterator referencing end of range to replace.
  4557. * @param __k1 Iterator referencing start of range to insert.
  4558. * @param __k2 Iterator referencing end of range to insert.
  4559. * @return Reference to this string.
  4560. * @throw std::length_error If new length exceeds @c max_size().
  4561. *
  4562. * Removes the characters in the range [__i1,__i2). In place,
  4563. * characters in the range [__k1,__k2) are inserted. If the
  4564. * length of result exceeds max_size(), length_error is thrown.
  4565. * The value of the string doesn't change if an error is
  4566. * thrown.
  4567. */
  4568. template<class _InputIterator>
  4569. basic_string&
  4570. replace(iterator __i1, iterator __i2,
  4571. _InputIterator __k1, _InputIterator __k2)
  4572. {
  4573. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  4574. && __i2 <= _M_iend());
  4575. __glibcxx_requires_valid_range(__k1, __k2);
  4576. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  4577. return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
  4578. }
  4579. // Specializations for the common case of pointer and iterator:
  4580. // useful to avoid the overhead of temporary buffering in _M_replace.
  4581. basic_string&
  4582. replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
  4583. {
  4584. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  4585. && __i2 <= _M_iend());
  4586. __glibcxx_requires_valid_range(__k1, __k2);
  4587. return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
  4588. __k1, __k2 - __k1);
  4589. }
  4590. basic_string&
  4591. replace(iterator __i1, iterator __i2,
  4592. const _CharT* __k1, const _CharT* __k2)
  4593. {
  4594. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  4595. && __i2 <= _M_iend());
  4596. __glibcxx_requires_valid_range(__k1, __k2);
  4597. return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
  4598. __k1, __k2 - __k1);
  4599. }
  4600. basic_string&
  4601. replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
  4602. {
  4603. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  4604. && __i2 <= _M_iend());
  4605. __glibcxx_requires_valid_range(__k1, __k2);
  4606. return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
  4607. __k1.base(), __k2 - __k1);
  4608. }
  4609. basic_string&
  4610. replace(iterator __i1, iterator __i2,
  4611. const_iterator __k1, const_iterator __k2)
  4612. {
  4613. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  4614. && __i2 <= _M_iend());
  4615. __glibcxx_requires_valid_range(__k1, __k2);
  4616. return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
  4617. __k1.base(), __k2 - __k1);
  4618. }
  4619. #if __cplusplus >= 201103L
  4620. /**
  4621. * @brief Replace range of characters with initializer_list.
  4622. * @param __i1 Iterator referencing start of range to replace.
  4623. * @param __i2 Iterator referencing end of range to replace.
  4624. * @param __l The initializer_list of characters to insert.
  4625. * @return Reference to this string.
  4626. * @throw std::length_error If new length exceeds @c max_size().
  4627. *
  4628. * Removes the characters in the range [__i1,__i2). In place,
  4629. * characters in the range [__k1,__k2) are inserted. If the
  4630. * length of result exceeds max_size(), length_error is thrown.
  4631. * The value of the string doesn't change if an error is
  4632. * thrown.
  4633. */
  4634. basic_string& replace(iterator __i1, iterator __i2,
  4635. initializer_list<_CharT> __l)
  4636. { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
  4637. #endif // C++11
  4638. #if __cplusplus >= 201703L
  4639. /**
  4640. * @brief Replace range of characters with string_view.
  4641. * @param __pos The position to replace at.
  4642. * @param __n The number of characters to replace.
  4643. * @param __svt The object convertible to string_view to insert.
  4644. * @return Reference to this string.
  4645. */
  4646. template<typename _Tp>
  4647. _If_sv<_Tp, basic_string&>
  4648. replace(size_type __pos, size_type __n, const _Tp& __svt)
  4649. {
  4650. __sv_type __sv = __svt;
  4651. return this->replace(__pos, __n, __sv.data(), __sv.size());
  4652. }
  4653. /**
  4654. * @brief Replace range of characters with string_view.
  4655. * @param __pos1 The position to replace at.
  4656. * @param __n1 The number of characters to replace.
  4657. * @param __svt The object convertible to string_view to insert from.
  4658. * @param __pos2 The position in the string_view to insert from.
  4659. * @param __n2 The number of characters to insert.
  4660. * @return Reference to this string.
  4661. */
  4662. template<typename _Tp>
  4663. _If_sv<_Tp, basic_string&>
  4664. replace(size_type __pos1, size_type __n1, const _Tp& __svt,
  4665. size_type __pos2, size_type __n2 = npos)
  4666. {
  4667. __sv_type __sv = __svt;
  4668. return this->replace(__pos1, __n1,
  4669. __sv.data()
  4670. + std::__sv_check(__sv.size(), __pos2, "basic_string::replace"),
  4671. std::__sv_limit(__sv.size(), __pos2, __n2));
  4672. }
  4673. /**
  4674. * @brief Replace range of characters with string_view.
  4675. * @param __i1 An iterator referencing the start position
  4676. to replace at.
  4677. * @param __i2 An iterator referencing the end position
  4678. for the replace.
  4679. * @param __svt The object convertible to string_view to insert from.
  4680. * @return Reference to this string.
  4681. */
  4682. template<typename _Tp>
  4683. _If_sv<_Tp, basic_string&>
  4684. replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
  4685. {
  4686. __sv_type __sv = __svt;
  4687. return this->replace(__i1 - begin(), __i2 - __i1, __sv);
  4688. }
  4689. #endif // C++17
  4690. private:
  4691. template<class _Integer>
  4692. basic_string&
  4693. _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
  4694. _Integer __val, __true_type)
  4695. { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
  4696. template<class _InputIterator>
  4697. basic_string&
  4698. _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
  4699. _InputIterator __k2, __false_type);
  4700. basic_string&
  4701. _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
  4702. _CharT __c);
  4703. basic_string&
  4704. _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
  4705. size_type __n2);
  4706. // _S_construct_aux is used to implement the 21.3.1 para 15 which
  4707. // requires special behaviour if _InIter is an integral type
  4708. template<class _InIterator>
  4709. static _CharT*
  4710. _S_construct_aux(_InIterator __beg, _InIterator __end,
  4711. const _Alloc& __a, __false_type)
  4712. {
  4713. typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
  4714. return _S_construct(__beg, __end, __a, _Tag());
  4715. }
  4716. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  4717. // 438. Ambiguity in the "do the right thing" clause
  4718. template<class _Integer>
  4719. static _CharT*
  4720. _S_construct_aux(_Integer __beg, _Integer __end,
  4721. const _Alloc& __a, __true_type)
  4722. { return _S_construct_aux_2(static_cast<size_type>(__beg),
  4723. __end, __a); }
  4724. static _CharT*
  4725. _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
  4726. { return _S_construct(__req, __c, __a); }
  4727. template<class _InIterator>
  4728. static _CharT*
  4729. _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
  4730. {
  4731. typedef typename std::__is_integer<_InIterator>::__type _Integral;
  4732. return _S_construct_aux(__beg, __end, __a, _Integral());
  4733. }
  4734. // For Input Iterators, used in istreambuf_iterators, etc.
  4735. template<class _InIterator>
  4736. static _CharT*
  4737. _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
  4738. input_iterator_tag);
  4739. // For forward_iterators up to random_access_iterators, used for
  4740. // string::iterator, _CharT*, etc.
  4741. template<class _FwdIterator>
  4742. static _CharT*
  4743. _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
  4744. forward_iterator_tag);
  4745. static _CharT*
  4746. _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
  4747. public:
  4748. /**
  4749. * @brief Copy substring into C string.
  4750. * @param __s C string to copy value into.
  4751. * @param __n Number of characters to copy.
  4752. * @param __pos Index of first character to copy.
  4753. * @return Number of characters actually copied
  4754. * @throw std::out_of_range If __pos > size().
  4755. *
  4756. * Copies up to @a __n characters starting at @a __pos into the
  4757. * C string @a __s. If @a __pos is %greater than size(),
  4758. * out_of_range is thrown.
  4759. */
  4760. size_type
  4761. copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
  4762. /**
  4763. * @brief Swap contents with another string.
  4764. * @param __s String to swap with.
  4765. *
  4766. * Exchanges the contents of this string with that of @a __s in constant
  4767. * time.
  4768. */
  4769. void
  4770. swap(basic_string& __s)
  4771. _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value);
  4772. // String operations:
  4773. /**
  4774. * @brief Return const pointer to null-terminated contents.
  4775. *
  4776. * This is a handle to internal data. Do not modify or dire things may
  4777. * happen.
  4778. */
  4779. const _CharT*
  4780. c_str() const _GLIBCXX_NOEXCEPT
  4781. { return _M_data(); }
  4782. /**
  4783. * @brief Return const pointer to contents.
  4784. *
  4785. * This is a pointer to internal data. It is undefined to modify
  4786. * the contents through the returned pointer. To get a pointer that
  4787. * allows modifying the contents use @c &str[0] instead,
  4788. * (or in C++17 the non-const @c str.data() overload).
  4789. */
  4790. const _CharT*
  4791. data() const _GLIBCXX_NOEXCEPT
  4792. { return _M_data(); }
  4793. #if __cplusplus >= 201703L
  4794. /**
  4795. * @brief Return non-const pointer to contents.
  4796. *
  4797. * This is a pointer to the character sequence held by the string.
  4798. * Modifying the characters in the sequence is allowed.
  4799. */
  4800. _CharT*
  4801. data() noexcept
  4802. {
  4803. _M_leak();
  4804. return _M_data();
  4805. }
  4806. #endif
  4807. /**
  4808. * @brief Return copy of allocator used to construct this string.
  4809. */
  4810. allocator_type
  4811. get_allocator() const _GLIBCXX_NOEXCEPT
  4812. { return _M_dataplus; }
  4813. /**
  4814. * @brief Find position of a C substring.
  4815. * @param __s C string to locate.
  4816. * @param __pos Index of character to search from.
  4817. * @param __n Number of characters from @a s to search for.
  4818. * @return Index of start of first occurrence.
  4819. *
  4820. * Starting from @a __pos, searches forward for the first @a
  4821. * __n characters in @a __s within this string. If found,
  4822. * returns the index where it begins. If not found, returns
  4823. * npos.
  4824. */
  4825. size_type
  4826. find(const _CharT* __s, size_type __pos, size_type __n) const
  4827. _GLIBCXX_NOEXCEPT;
  4828. /**
  4829. * @brief Find position of a string.
  4830. * @param __str String to locate.
  4831. * @param __pos Index of character to search from (default 0).
  4832. * @return Index of start of first occurrence.
  4833. *
  4834. * Starting from @a __pos, searches forward for value of @a __str within
  4835. * this string. If found, returns the index where it begins. If not
  4836. * found, returns npos.
  4837. */
  4838. size_type
  4839. find(const basic_string& __str, size_type __pos = 0) const
  4840. _GLIBCXX_NOEXCEPT
  4841. { return this->find(__str.data(), __pos, __str.size()); }
  4842. /**
  4843. * @brief Find position of a C string.
  4844. * @param __s C string to locate.
  4845. * @param __pos Index of character to search from (default 0).
  4846. * @return Index of start of first occurrence.
  4847. *
  4848. * Starting from @a __pos, searches forward for the value of @a
  4849. * __s within this string. If found, returns the index where
  4850. * it begins. If not found, returns npos.
  4851. */
  4852. size_type
  4853. find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
  4854. {
  4855. __glibcxx_requires_string(__s);
  4856. return this->find(__s, __pos, traits_type::length(__s));
  4857. }
  4858. /**
  4859. * @brief Find position of a character.
  4860. * @param __c Character to locate.
  4861. * @param __pos Index of character to search from (default 0).
  4862. * @return Index of first occurrence.
  4863. *
  4864. * Starting from @a __pos, searches forward for @a __c within
  4865. * this string. If found, returns the index where it was
  4866. * found. If not found, returns npos.
  4867. */
  4868. size_type
  4869. find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
  4870. #if __cplusplus >= 201703L
  4871. /**
  4872. * @brief Find position of a string_view.
  4873. * @param __svt The object convertible to string_view to locate.
  4874. * @param __pos Index of character to search from (default 0).
  4875. * @return Index of start of first occurrence.
  4876. */
  4877. template<typename _Tp>
  4878. _If_sv<_Tp, size_type>
  4879. find(const _Tp& __svt, size_type __pos = 0) const
  4880. noexcept(is_same<_Tp, __sv_type>::value)
  4881. {
  4882. __sv_type __sv = __svt;
  4883. return this->find(__sv.data(), __pos, __sv.size());
  4884. }
  4885. #endif // C++17
  4886. /**
  4887. * @brief Find last position of a string.
  4888. * @param __str String to locate.
  4889. * @param __pos Index of character to search back from (default end).
  4890. * @return Index of start of last occurrence.
  4891. *
  4892. * Starting from @a __pos, searches backward for value of @a
  4893. * __str within this string. If found, returns the index where
  4894. * it begins. If not found, returns npos.
  4895. */
  4896. size_type
  4897. rfind(const basic_string& __str, size_type __pos = npos) const
  4898. _GLIBCXX_NOEXCEPT
  4899. { return this->rfind(__str.data(), __pos, __str.size()); }
  4900. /**
  4901. * @brief Find last position of a C substring.
  4902. * @param __s C string to locate.
  4903. * @param __pos Index of character to search back from.
  4904. * @param __n Number of characters from s to search for.
  4905. * @return Index of start of last occurrence.
  4906. *
  4907. * Starting from @a __pos, searches backward for the first @a
  4908. * __n characters in @a __s within this string. If found,
  4909. * returns the index where it begins. If not found, returns
  4910. * npos.
  4911. */
  4912. size_type
  4913. rfind(const _CharT* __s, size_type __pos, size_type __n) const
  4914. _GLIBCXX_NOEXCEPT;
  4915. /**
  4916. * @brief Find last position of a C string.
  4917. * @param __s C string to locate.
  4918. * @param __pos Index of character to start search at (default end).
  4919. * @return Index of start of last occurrence.
  4920. *
  4921. * Starting from @a __pos, searches backward for the value of
  4922. * @a __s within this string. If found, returns the index
  4923. * where it begins. If not found, returns npos.
  4924. */
  4925. size_type
  4926. rfind(const _CharT* __s, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
  4927. {
  4928. __glibcxx_requires_string(__s);
  4929. return this->rfind(__s, __pos, traits_type::length(__s));
  4930. }
  4931. /**
  4932. * @brief Find last position of a character.
  4933. * @param __c Character to locate.
  4934. * @param __pos Index of character to search back from (default end).
  4935. * @return Index of last occurrence.
  4936. *
  4937. * Starting from @a __pos, searches backward for @a __c within
  4938. * this string. If found, returns the index where it was
  4939. * found. If not found, returns npos.
  4940. */
  4941. size_type
  4942. rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
  4943. #if __cplusplus >= 201703L
  4944. /**
  4945. * @brief Find last position of a string_view.
  4946. * @param __svt The object convertible to string_view to locate.
  4947. * @param __pos Index of character to search back from (default end).
  4948. * @return Index of start of last occurrence.
  4949. */
  4950. template<typename _Tp>
  4951. _If_sv<_Tp, size_type>
  4952. rfind(const _Tp& __svt, size_type __pos = npos) const
  4953. noexcept(is_same<_Tp, __sv_type>::value)
  4954. {
  4955. __sv_type __sv = __svt;
  4956. return this->rfind(__sv.data(), __pos, __sv.size());
  4957. }
  4958. #endif // C++17
  4959. /**
  4960. * @brief Find position of a character of string.
  4961. * @param __str String containing characters to locate.
  4962. * @param __pos Index of character to search from (default 0).
  4963. * @return Index of first occurrence.
  4964. *
  4965. * Starting from @a __pos, searches forward for one of the
  4966. * characters of @a __str within this string. If found,
  4967. * returns the index where it was found. If not found, returns
  4968. * npos.
  4969. */
  4970. size_type
  4971. find_first_of(const basic_string& __str, size_type __pos = 0) const
  4972. _GLIBCXX_NOEXCEPT
  4973. { return this->find_first_of(__str.data(), __pos, __str.size()); }
  4974. /**
  4975. * @brief Find position of a character of C substring.
  4976. * @param __s String containing characters to locate.
  4977. * @param __pos Index of character to search from.
  4978. * @param __n Number of characters from s to search for.
  4979. * @return Index of first occurrence.
  4980. *
  4981. * Starting from @a __pos, searches forward for one of the
  4982. * first @a __n characters of @a __s within this string. If
  4983. * found, returns the index where it was found. If not found,
  4984. * returns npos.
  4985. */
  4986. size_type
  4987. find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
  4988. _GLIBCXX_NOEXCEPT;
  4989. /**
  4990. * @brief Find position of a character of C string.
  4991. * @param __s String containing characters to locate.
  4992. * @param __pos Index of character to search from (default 0).
  4993. * @return Index of first occurrence.
  4994. *
  4995. * Starting from @a __pos, searches forward for one of the
  4996. * characters of @a __s within this string. If found, returns
  4997. * the index where it was found. If not found, returns npos.
  4998. */
  4999. size_type
  5000. find_first_of(const _CharT* __s, size_type __pos = 0) const
  5001. _GLIBCXX_NOEXCEPT
  5002. {
  5003. __glibcxx_requires_string(__s);
  5004. return this->find_first_of(__s, __pos, traits_type::length(__s));
  5005. }
  5006. /**
  5007. * @brief Find position of a character.
  5008. * @param __c Character to locate.
  5009. * @param __pos Index of character to search from (default 0).
  5010. * @return Index of first occurrence.
  5011. *
  5012. * Starting from @a __pos, searches forward for the character
  5013. * @a __c within this string. If found, returns the index
  5014. * where it was found. If not found, returns npos.
  5015. *
  5016. * Note: equivalent to find(__c, __pos).
  5017. */
  5018. size_type
  5019. find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
  5020. { return this->find(__c, __pos); }
  5021. #if __cplusplus >= 201703L
  5022. /**
  5023. * @brief Find position of a character of a string_view.
  5024. * @param __svt An object convertible to string_view containing
  5025. * characters to locate.
  5026. * @param __pos Index of character to search from (default 0).
  5027. * @return Index of first occurrence.
  5028. */
  5029. template<typename _Tp>
  5030. _If_sv<_Tp, size_type>
  5031. find_first_of(const _Tp& __svt, size_type __pos = 0) const
  5032. noexcept(is_same<_Tp, __sv_type>::value)
  5033. {
  5034. __sv_type __sv = __svt;
  5035. return this->find_first_of(__sv.data(), __pos, __sv.size());
  5036. }
  5037. #endif // C++17
  5038. /**
  5039. * @brief Find last position of a character of string.
  5040. * @param __str String containing characters to locate.
  5041. * @param __pos Index of character to search back from (default end).
  5042. * @return Index of last occurrence.
  5043. *
  5044. * Starting from @a __pos, searches backward for one of the
  5045. * characters of @a __str within this string. If found,
  5046. * returns the index where it was found. If not found, returns
  5047. * npos.
  5048. */
  5049. size_type
  5050. find_last_of(const basic_string& __str, size_type __pos = npos) const
  5051. _GLIBCXX_NOEXCEPT
  5052. { return this->find_last_of(__str.data(), __pos, __str.size()); }
  5053. /**
  5054. * @brief Find last position of a character of C substring.
  5055. * @param __s C string containing characters to locate.
  5056. * @param __pos Index of character to search back from.
  5057. * @param __n Number of characters from s to search for.
  5058. * @return Index of last occurrence.
  5059. *
  5060. * Starting from @a __pos, searches backward for one of the
  5061. * first @a __n characters of @a __s within this string. If
  5062. * found, returns the index where it was found. If not found,
  5063. * returns npos.
  5064. */
  5065. size_type
  5066. find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
  5067. _GLIBCXX_NOEXCEPT;
  5068. /**
  5069. * @brief Find last position of a character of C string.
  5070. * @param __s C string containing characters to locate.
  5071. * @param __pos Index of character to search back from (default end).
  5072. * @return Index of last occurrence.
  5073. *
  5074. * Starting from @a __pos, searches backward for one of the
  5075. * characters of @a __s within this string. If found, returns
  5076. * the index where it was found. If not found, returns npos.
  5077. */
  5078. size_type
  5079. find_last_of(const _CharT* __s, size_type __pos = npos) const
  5080. _GLIBCXX_NOEXCEPT
  5081. {
  5082. __glibcxx_requires_string(__s);
  5083. return this->find_last_of(__s, __pos, traits_type::length(__s));
  5084. }
  5085. /**
  5086. * @brief Find last position of a character.
  5087. * @param __c Character to locate.
  5088. * @param __pos Index of character to search back from (default end).
  5089. * @return Index of last occurrence.
  5090. *
  5091. * Starting from @a __pos, searches backward for @a __c within
  5092. * this string. If found, returns the index where it was
  5093. * found. If not found, returns npos.
  5094. *
  5095. * Note: equivalent to rfind(__c, __pos).
  5096. */
  5097. size_type
  5098. find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
  5099. { return this->rfind(__c, __pos); }
  5100. #if __cplusplus >= 201703L
  5101. /**
  5102. * @brief Find last position of a character of string.
  5103. * @param __svt An object convertible to string_view containing
  5104. * characters to locate.
  5105. * @param __pos Index of character to search back from (default end).
  5106. * @return Index of last occurrence.
  5107. */
  5108. template<typename _Tp>
  5109. _If_sv<_Tp, size_type>
  5110. find_last_of(const _Tp& __svt, size_type __pos = npos) const
  5111. noexcept(is_same<_Tp, __sv_type>::value)
  5112. {
  5113. __sv_type __sv = __svt;
  5114. return this->find_last_of(__sv.data(), __pos, __sv.size());
  5115. }
  5116. #endif // C++17
  5117. /**
  5118. * @brief Find position of a character not in string.
  5119. * @param __str String containing characters to avoid.
  5120. * @param __pos Index of character to search from (default 0).
  5121. * @return Index of first occurrence.
  5122. *
  5123. * Starting from @a __pos, searches forward for a character not contained
  5124. * in @a __str within this string. If found, returns the index where it
  5125. * was found. If not found, returns npos.
  5126. */
  5127. size_type
  5128. find_first_not_of(const basic_string& __str, size_type __pos = 0) const
  5129. _GLIBCXX_NOEXCEPT
  5130. { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
  5131. /**
  5132. * @brief Find position of a character not in C substring.
  5133. * @param __s C string containing characters to avoid.
  5134. * @param __pos Index of character to search from.
  5135. * @param __n Number of characters from __s to consider.
  5136. * @return Index of first occurrence.
  5137. *
  5138. * Starting from @a __pos, searches forward for a character not
  5139. * contained in the first @a __n characters of @a __s within
  5140. * this string. If found, returns the index where it was
  5141. * found. If not found, returns npos.
  5142. */
  5143. size_type
  5144. find_first_not_of(const _CharT* __s, size_type __pos,
  5145. size_type __n) const _GLIBCXX_NOEXCEPT;
  5146. /**
  5147. * @brief Find position of a character not in C string.
  5148. * @param __s C string containing characters to avoid.
  5149. * @param __pos Index of character to search from (default 0).
  5150. * @return Index of first occurrence.
  5151. *
  5152. * Starting from @a __pos, searches forward for a character not
  5153. * contained in @a __s within this string. If found, returns
  5154. * the index where it was found. If not found, returns npos.
  5155. */
  5156. size_type
  5157. find_first_not_of(const _CharT* __s, size_type __pos = 0) const
  5158. _GLIBCXX_NOEXCEPT
  5159. {
  5160. __glibcxx_requires_string(__s);
  5161. return this->find_first_not_of(__s, __pos, traits_type::length(__s));
  5162. }
  5163. /**
  5164. * @brief Find position of a different character.
  5165. * @param __c Character to avoid.
  5166. * @param __pos Index of character to search from (default 0).
  5167. * @return Index of first occurrence.
  5168. *
  5169. * Starting from @a __pos, searches forward for a character
  5170. * other than @a __c within this string. If found, returns the
  5171. * index where it was found. If not found, returns npos.
  5172. */
  5173. size_type
  5174. find_first_not_of(_CharT __c, size_type __pos = 0) const
  5175. _GLIBCXX_NOEXCEPT;
  5176. #if __cplusplus >= 201703L
  5177. /**
  5178. * @brief Find position of a character not in a string_view.
  5179. * @param __svt An object convertible to string_view containing
  5180. * characters to avoid.
  5181. * @param __pos Index of character to search from (default 0).
  5182. * @return Index of first occurrence.
  5183. */
  5184. template<typename _Tp>
  5185. _If_sv<_Tp, size_type>
  5186. find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
  5187. noexcept(is_same<_Tp, __sv_type>::value)
  5188. {
  5189. __sv_type __sv = __svt;
  5190. return this->find_first_not_of(__sv.data(), __pos, __sv.size());
  5191. }
  5192. #endif // C++17
  5193. /**
  5194. * @brief Find last position of a character not in string.
  5195. * @param __str String containing characters to avoid.
  5196. * @param __pos Index of character to search back from (default end).
  5197. * @return Index of last occurrence.
  5198. *
  5199. * Starting from @a __pos, searches backward for a character
  5200. * not contained in @a __str within this string. If found,
  5201. * returns the index where it was found. If not found, returns
  5202. * npos.
  5203. */
  5204. size_type
  5205. find_last_not_of(const basic_string& __str, size_type __pos = npos) const
  5206. _GLIBCXX_NOEXCEPT
  5207. { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
  5208. /**
  5209. * @brief Find last position of a character not in C substring.
  5210. * @param __s C string containing characters to avoid.
  5211. * @param __pos Index of character to search back from.
  5212. * @param __n Number of characters from s to consider.
  5213. * @return Index of last occurrence.
  5214. *
  5215. * Starting from @a __pos, searches backward for a character not
  5216. * contained in the first @a __n characters of @a __s within this string.
  5217. * If found, returns the index where it was found. If not found,
  5218. * returns npos.
  5219. */
  5220. size_type
  5221. find_last_not_of(const _CharT* __s, size_type __pos,
  5222. size_type __n) const _GLIBCXX_NOEXCEPT;
  5223. /**
  5224. * @brief Find last position of a character not in C string.
  5225. * @param __s C string containing characters to avoid.
  5226. * @param __pos Index of character to search back from (default end).
  5227. * @return Index of last occurrence.
  5228. *
  5229. * Starting from @a __pos, searches backward for a character
  5230. * not contained in @a __s within this string. If found,
  5231. * returns the index where it was found. If not found, returns
  5232. * npos.
  5233. */
  5234. size_type
  5235. find_last_not_of(const _CharT* __s, size_type __pos = npos) const
  5236. _GLIBCXX_NOEXCEPT
  5237. {
  5238. __glibcxx_requires_string(__s);
  5239. return this->find_last_not_of(__s, __pos, traits_type::length(__s));
  5240. }
  5241. /**
  5242. * @brief Find last position of a different character.
  5243. * @param __c Character to avoid.
  5244. * @param __pos Index of character to search back from (default end).
  5245. * @return Index of last occurrence.
  5246. *
  5247. * Starting from @a __pos, searches backward for a character other than
  5248. * @a __c within this string. If found, returns the index where it was
  5249. * found. If not found, returns npos.
  5250. */
  5251. size_type
  5252. find_last_not_of(_CharT __c, size_type __pos = npos) const
  5253. _GLIBCXX_NOEXCEPT;
  5254. #if __cplusplus >= 201703L
  5255. /**
  5256. * @brief Find last position of a character not in a string_view.
  5257. * @param __svt An object convertible to string_view containing
  5258. * characters to avoid.
  5259. * @param __pos Index of character to search back from (default end).
  5260. * @return Index of last occurrence.
  5261. */
  5262. template<typename _Tp>
  5263. _If_sv<_Tp, size_type>
  5264. find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
  5265. noexcept(is_same<_Tp, __sv_type>::value)
  5266. {
  5267. __sv_type __sv = __svt;
  5268. return this->find_last_not_of(__sv.data(), __pos, __sv.size());
  5269. }
  5270. #endif // C++17
  5271. /**
  5272. * @brief Get a substring.
  5273. * @param __pos Index of first character (default 0).
  5274. * @param __n Number of characters in substring (default remainder).
  5275. * @return The new string.
  5276. * @throw std::out_of_range If __pos > size().
  5277. *
  5278. * Construct and return a new string using the @a __n
  5279. * characters starting at @a __pos. If the string is too
  5280. * short, use the remainder of the characters. If @a __pos is
  5281. * beyond the end of the string, out_of_range is thrown.
  5282. */
  5283. basic_string
  5284. substr(size_type __pos = 0, size_type __n = npos) const
  5285. { return basic_string(*this,
  5286. _M_check(__pos, "basic_string::substr"), __n); }
  5287. /**
  5288. * @brief Compare to a string.
  5289. * @param __str String to compare against.
  5290. * @return Integer < 0, 0, or > 0.
  5291. *
  5292. * Returns an integer < 0 if this string is ordered before @a
  5293. * __str, 0 if their values are equivalent, or > 0 if this
  5294. * string is ordered after @a __str. Determines the effective
  5295. * length rlen of the strings to compare as the smallest of
  5296. * size() and str.size(). The function then compares the two
  5297. * strings by calling traits::compare(data(), str.data(),rlen).
  5298. * If the result of the comparison is nonzero returns it,
  5299. * otherwise the shorter one is ordered first.
  5300. */
  5301. int
  5302. compare(const basic_string& __str) const
  5303. {
  5304. const size_type __size = this->size();
  5305. const size_type __osize = __str.size();
  5306. const size_type __len = std::min(__size, __osize);
  5307. int __r = traits_type::compare(_M_data(), __str.data(), __len);
  5308. if (!__r)
  5309. __r = _S_compare(__size, __osize);
  5310. return __r;
  5311. }
  5312. #if __cplusplus >= 201703L
  5313. /**
  5314. * @brief Compare to a string_view.
  5315. * @param __svt An object convertible to string_view to compare against.
  5316. * @return Integer < 0, 0, or > 0.
  5317. */
  5318. template<typename _Tp>
  5319. _If_sv<_Tp, int>
  5320. compare(const _Tp& __svt) const
  5321. noexcept(is_same<_Tp, __sv_type>::value)
  5322. {
  5323. __sv_type __sv = __svt;
  5324. const size_type __size = this->size();
  5325. const size_type __osize = __sv.size();
  5326. const size_type __len = std::min(__size, __osize);
  5327. int __r = traits_type::compare(_M_data(), __sv.data(), __len);
  5328. if (!__r)
  5329. __r = _S_compare(__size, __osize);
  5330. return __r;
  5331. }
  5332. /**
  5333. * @brief Compare to a string_view.
  5334. * @param __pos A position in the string to start comparing from.
  5335. * @param __n The number of characters to compare.
  5336. * @param __svt An object convertible to string_view to compare
  5337. * against.
  5338. * @return Integer < 0, 0, or > 0.
  5339. */
  5340. template<typename _Tp>
  5341. _If_sv<_Tp, int>
  5342. compare(size_type __pos, size_type __n, const _Tp& __svt) const
  5343. noexcept(is_same<_Tp, __sv_type>::value)
  5344. {
  5345. __sv_type __sv = __svt;
  5346. return __sv_type(*this).substr(__pos, __n).compare(__sv);
  5347. }
  5348. /**
  5349. * @brief Compare to a string_view.
  5350. * @param __pos1 A position in the string to start comparing from.
  5351. * @param __n1 The number of characters to compare.
  5352. * @param __svt An object convertible to string_view to compare
  5353. * against.
  5354. * @param __pos2 A position in the string_view to start comparing from.
  5355. * @param __n2 The number of characters to compare.
  5356. * @return Integer < 0, 0, or > 0.
  5357. */
  5358. template<typename _Tp>
  5359. _If_sv<_Tp, int>
  5360. compare(size_type __pos1, size_type __n1, const _Tp& __svt,
  5361. size_type __pos2, size_type __n2 = npos) const
  5362. noexcept(is_same<_Tp, __sv_type>::value)
  5363. {
  5364. __sv_type __sv = __svt;
  5365. return __sv_type(*this)
  5366. .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
  5367. }
  5368. #endif // C++17
  5369. /**
  5370. * @brief Compare substring to a string.
  5371. * @param __pos Index of first character of substring.
  5372. * @param __n Number of characters in substring.
  5373. * @param __str String to compare against.
  5374. * @return Integer < 0, 0, or > 0.
  5375. *
  5376. * Form the substring of this string from the @a __n characters
  5377. * starting at @a __pos. Returns an integer < 0 if the
  5378. * substring is ordered before @a __str, 0 if their values are
  5379. * equivalent, or > 0 if the substring is ordered after @a
  5380. * __str. Determines the effective length rlen of the strings
  5381. * to compare as the smallest of the length of the substring
  5382. * and @a __str.size(). The function then compares the two
  5383. * strings by calling
  5384. * traits::compare(substring.data(),str.data(),rlen). If the
  5385. * result of the comparison is nonzero returns it, otherwise
  5386. * the shorter one is ordered first.
  5387. */
  5388. int
  5389. compare(size_type __pos, size_type __n, const basic_string& __str) const;
  5390. /**
  5391. * @brief Compare substring to a substring.
  5392. * @param __pos1 Index of first character of substring.
  5393. * @param __n1 Number of characters in substring.
  5394. * @param __str String to compare against.
  5395. * @param __pos2 Index of first character of substring of str.
  5396. * @param __n2 Number of characters in substring of str.
  5397. * @return Integer < 0, 0, or > 0.
  5398. *
  5399. * Form the substring of this string from the @a __n1
  5400. * characters starting at @a __pos1. Form the substring of @a
  5401. * __str from the @a __n2 characters starting at @a __pos2.
  5402. * Returns an integer < 0 if this substring is ordered before
  5403. * the substring of @a __str, 0 if their values are equivalent,
  5404. * or > 0 if this substring is ordered after the substring of
  5405. * @a __str. Determines the effective length rlen of the
  5406. * strings to compare as the smallest of the lengths of the
  5407. * substrings. The function then compares the two strings by
  5408. * calling
  5409. * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
  5410. * If the result of the comparison is nonzero returns it,
  5411. * otherwise the shorter one is ordered first.
  5412. */
  5413. int
  5414. compare(size_type __pos1, size_type __n1, const basic_string& __str,
  5415. size_type __pos2, size_type __n2 = npos) const;
  5416. /**
  5417. * @brief Compare to a C string.
  5418. * @param __s C string to compare against.
  5419. * @return Integer < 0, 0, or > 0.
  5420. *
  5421. * Returns an integer < 0 if this string is ordered before @a __s, 0 if
  5422. * their values are equivalent, or > 0 if this string is ordered after
  5423. * @a __s. Determines the effective length rlen of the strings to
  5424. * compare as the smallest of size() and the length of a string
  5425. * constructed from @a __s. The function then compares the two strings
  5426. * by calling traits::compare(data(),s,rlen). If the result of the
  5427. * comparison is nonzero returns it, otherwise the shorter one is
  5428. * ordered first.
  5429. */
  5430. int
  5431. compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT;
  5432. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  5433. // 5 String::compare specification questionable
  5434. /**
  5435. * @brief Compare substring to a C string.
  5436. * @param __pos Index of first character of substring.
  5437. * @param __n1 Number of characters in substring.
  5438. * @param __s C string to compare against.
  5439. * @return Integer < 0, 0, or > 0.
  5440. *
  5441. * Form the substring of this string from the @a __n1
  5442. * characters starting at @a pos. Returns an integer < 0 if
  5443. * the substring is ordered before @a __s, 0 if their values
  5444. * are equivalent, or > 0 if the substring is ordered after @a
  5445. * __s. Determines the effective length rlen of the strings to
  5446. * compare as the smallest of the length of the substring and
  5447. * the length of a string constructed from @a __s. The
  5448. * function then compares the two string by calling
  5449. * traits::compare(substring.data(),__s,rlen). If the result of
  5450. * the comparison is nonzero returns it, otherwise the shorter
  5451. * one is ordered first.
  5452. */
  5453. int
  5454. compare(size_type __pos, size_type __n1, const _CharT* __s) const;
  5455. /**
  5456. * @brief Compare substring against a character %array.
  5457. * @param __pos Index of first character of substring.
  5458. * @param __n1 Number of characters in substring.
  5459. * @param __s character %array to compare against.
  5460. * @param __n2 Number of characters of s.
  5461. * @return Integer < 0, 0, or > 0.
  5462. *
  5463. * Form the substring of this string from the @a __n1
  5464. * characters starting at @a __pos. Form a string from the
  5465. * first @a __n2 characters of @a __s. Returns an integer < 0
  5466. * if this substring is ordered before the string from @a __s,
  5467. * 0 if their values are equivalent, or > 0 if this substring
  5468. * is ordered after the string from @a __s. Determines the
  5469. * effective length rlen of the strings to compare as the
  5470. * smallest of the length of the substring and @a __n2. The
  5471. * function then compares the two strings by calling
  5472. * traits::compare(substring.data(),s,rlen). If the result of
  5473. * the comparison is nonzero returns it, otherwise the shorter
  5474. * one is ordered first.
  5475. *
  5476. * NB: s must have at least n2 characters, &apos;\\0&apos; has
  5477. * no special meaning.
  5478. */
  5479. int
  5480. compare(size_type __pos, size_type __n1, const _CharT* __s,
  5481. size_type __n2) const;
  5482. #if __cplusplus > 201703L
  5483. bool
  5484. starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept
  5485. { return __sv_type(this->data(), this->size()).starts_with(__x); }
  5486. bool
  5487. starts_with(_CharT __x) const noexcept
  5488. { return __sv_type(this->data(), this->size()).starts_with(__x); }
  5489. bool
  5490. starts_with(const _CharT* __x) const noexcept
  5491. { return __sv_type(this->data(), this->size()).starts_with(__x); }
  5492. bool
  5493. ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept
  5494. { return __sv_type(this->data(), this->size()).ends_with(__x); }
  5495. bool
  5496. ends_with(_CharT __x) const noexcept
  5497. { return __sv_type(this->data(), this->size()).ends_with(__x); }
  5498. bool
  5499. ends_with(const _CharT* __x) const noexcept
  5500. { return __sv_type(this->data(), this->size()).ends_with(__x); }
  5501. #endif // C++20
  5502. # ifdef _GLIBCXX_TM_TS_INTERNAL
  5503. friend void
  5504. ::_txnal_cow_string_C1_for_exceptions(void* that, const char* s,
  5505. void* exc);
  5506. friend const char*
  5507. ::_txnal_cow_string_c_str(const void *that);
  5508. friend void
  5509. ::_txnal_cow_string_D1(void *that);
  5510. friend void
  5511. ::_txnal_cow_string_D1_commit(void *that);
  5512. # endif
  5513. };
  5514. #endif // !_GLIBCXX_USE_CXX11_ABI
  5515. #if __cpp_deduction_guides >= 201606
  5516. _GLIBCXX_BEGIN_NAMESPACE_CXX11
  5517. template<typename _InputIterator, typename _CharT
  5518. = typename iterator_traits<_InputIterator>::value_type,
  5519. typename _Allocator = allocator<_CharT>,
  5520. typename = _RequireInputIter<_InputIterator>,
  5521. typename = _RequireAllocator<_Allocator>>
  5522. basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
  5523. -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
  5524. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  5525. // 3075. basic_string needs deduction guides from basic_string_view
  5526. template<typename _CharT, typename _Traits,
  5527. typename _Allocator = allocator<_CharT>,
  5528. typename = _RequireAllocator<_Allocator>>
  5529. basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
  5530. -> basic_string<_CharT, _Traits, _Allocator>;
  5531. template<typename _CharT, typename _Traits,
  5532. typename _Allocator = allocator<_CharT>,
  5533. typename = _RequireAllocator<_Allocator>>
  5534. basic_string(basic_string_view<_CharT, _Traits>,
  5535. typename basic_string<_CharT, _Traits, _Allocator>::size_type,
  5536. typename basic_string<_CharT, _Traits, _Allocator>::size_type,
  5537. const _Allocator& = _Allocator())
  5538. -> basic_string<_CharT, _Traits, _Allocator>;
  5539. _GLIBCXX_END_NAMESPACE_CXX11
  5540. #endif
  5541. // operator+
  5542. /**
  5543. * @brief Concatenate two strings.
  5544. * @param __lhs First string.
  5545. * @param __rhs Last string.
  5546. * @return New string with value of @a __lhs followed by @a __rhs.
  5547. */
  5548. template<typename _CharT, typename _Traits, typename _Alloc>
  5549. basic_string<_CharT, _Traits, _Alloc>
  5550. operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5551. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5552. {
  5553. basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
  5554. __str.append(__rhs);
  5555. return __str;
  5556. }
  5557. /**
  5558. * @brief Concatenate C string and string.
  5559. * @param __lhs First string.
  5560. * @param __rhs Last string.
  5561. * @return New string with value of @a __lhs followed by @a __rhs.
  5562. */
  5563. template<typename _CharT, typename _Traits, typename _Alloc>
  5564. basic_string<_CharT,_Traits,_Alloc>
  5565. operator+(const _CharT* __lhs,
  5566. const basic_string<_CharT,_Traits,_Alloc>& __rhs);
  5567. /**
  5568. * @brief Concatenate character and string.
  5569. * @param __lhs First string.
  5570. * @param __rhs Last string.
  5571. * @return New string with @a __lhs followed by @a __rhs.
  5572. */
  5573. template<typename _CharT, typename _Traits, typename _Alloc>
  5574. basic_string<_CharT,_Traits,_Alloc>
  5575. operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
  5576. /**
  5577. * @brief Concatenate string and C string.
  5578. * @param __lhs First string.
  5579. * @param __rhs Last string.
  5580. * @return New string with @a __lhs followed by @a __rhs.
  5581. */
  5582. template<typename _CharT, typename _Traits, typename _Alloc>
  5583. inline basic_string<_CharT, _Traits, _Alloc>
  5584. operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5585. const _CharT* __rhs)
  5586. {
  5587. basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
  5588. __str.append(__rhs);
  5589. return __str;
  5590. }
  5591. /**
  5592. * @brief Concatenate string and character.
  5593. * @param __lhs First string.
  5594. * @param __rhs Last string.
  5595. * @return New string with @a __lhs followed by @a __rhs.
  5596. */
  5597. template<typename _CharT, typename _Traits, typename _Alloc>
  5598. inline basic_string<_CharT, _Traits, _Alloc>
  5599. operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
  5600. {
  5601. typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
  5602. typedef typename __string_type::size_type __size_type;
  5603. __string_type __str(__lhs);
  5604. __str.append(__size_type(1), __rhs);
  5605. return __str;
  5606. }
  5607. #if __cplusplus >= 201103L
  5608. template<typename _CharT, typename _Traits, typename _Alloc>
  5609. inline basic_string<_CharT, _Traits, _Alloc>
  5610. operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
  5611. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5612. { return std::move(__lhs.append(__rhs)); }
  5613. template<typename _CharT, typename _Traits, typename _Alloc>
  5614. inline basic_string<_CharT, _Traits, _Alloc>
  5615. operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5616. basic_string<_CharT, _Traits, _Alloc>&& __rhs)
  5617. { return std::move(__rhs.insert(0, __lhs)); }
  5618. template<typename _CharT, typename _Traits, typename _Alloc>
  5619. inline basic_string<_CharT, _Traits, _Alloc>
  5620. operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
  5621. basic_string<_CharT, _Traits, _Alloc>&& __rhs)
  5622. {
  5623. #if _GLIBCXX_USE_CXX11_ABI
  5624. using _Alloc_traits = allocator_traits<_Alloc>;
  5625. bool __use_rhs = false;
  5626. if _GLIBCXX17_CONSTEXPR (typename _Alloc_traits::is_always_equal{})
  5627. __use_rhs = true;
  5628. else if (__lhs.get_allocator() == __rhs.get_allocator())
  5629. __use_rhs = true;
  5630. if (__use_rhs)
  5631. #endif
  5632. {
  5633. const auto __size = __lhs.size() + __rhs.size();
  5634. if (__size > __lhs.capacity() && __size <= __rhs.capacity())
  5635. return std::move(__rhs.insert(0, __lhs));
  5636. }
  5637. return std::move(__lhs.append(__rhs));
  5638. }
  5639. template<typename _CharT, typename _Traits, typename _Alloc>
  5640. inline basic_string<_CharT, _Traits, _Alloc>
  5641. operator+(const _CharT* __lhs,
  5642. basic_string<_CharT, _Traits, _Alloc>&& __rhs)
  5643. { return std::move(__rhs.insert(0, __lhs)); }
  5644. template<typename _CharT, typename _Traits, typename _Alloc>
  5645. inline basic_string<_CharT, _Traits, _Alloc>
  5646. operator+(_CharT __lhs,
  5647. basic_string<_CharT, _Traits, _Alloc>&& __rhs)
  5648. { return std::move(__rhs.insert(0, 1, __lhs)); }
  5649. template<typename _CharT, typename _Traits, typename _Alloc>
  5650. inline basic_string<_CharT, _Traits, _Alloc>
  5651. operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
  5652. const _CharT* __rhs)
  5653. { return std::move(__lhs.append(__rhs)); }
  5654. template<typename _CharT, typename _Traits, typename _Alloc>
  5655. inline basic_string<_CharT, _Traits, _Alloc>
  5656. operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
  5657. _CharT __rhs)
  5658. { return std::move(__lhs.append(1, __rhs)); }
  5659. #endif
  5660. // operator ==
  5661. /**
  5662. * @brief Test equivalence of two strings.
  5663. * @param __lhs First string.
  5664. * @param __rhs Second string.
  5665. * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
  5666. */
  5667. template<typename _CharT, typename _Traits, typename _Alloc>
  5668. inline bool
  5669. operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5670. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5671. _GLIBCXX_NOEXCEPT
  5672. { return __lhs.compare(__rhs) == 0; }
  5673. template<typename _CharT>
  5674. inline
  5675. typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type
  5676. operator==(const basic_string<_CharT>& __lhs,
  5677. const basic_string<_CharT>& __rhs) _GLIBCXX_NOEXCEPT
  5678. { return (__lhs.size() == __rhs.size()
  5679. && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
  5680. __lhs.size())); }
  5681. /**
  5682. * @brief Test equivalence of string and C string.
  5683. * @param __lhs String.
  5684. * @param __rhs C string.
  5685. * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
  5686. */
  5687. template<typename _CharT, typename _Traits, typename _Alloc>
  5688. inline bool
  5689. operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5690. const _CharT* __rhs)
  5691. { return __lhs.compare(__rhs) == 0; }
  5692. #if __cpp_lib_three_way_comparison
  5693. /**
  5694. * @brief Three-way comparison of a string and a C string.
  5695. * @param __lhs A string.
  5696. * @param __rhs A null-terminated string.
  5697. * @return A value indicating whether `__lhs` is less than, equal to,
  5698. * greater than, or incomparable with `__rhs`.
  5699. */
  5700. template<typename _CharT, typename _Traits, typename _Alloc>
  5701. inline auto
  5702. operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5703. const basic_string<_CharT, _Traits, _Alloc>& __rhs) noexcept
  5704. -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
  5705. { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); }
  5706. /**
  5707. * @brief Three-way comparison of a string and a C string.
  5708. * @param __lhs A string.
  5709. * @param __rhs A null-terminated string.
  5710. * @return A value indicating whether `__lhs` is less than, equal to,
  5711. * greater than, or incomparable with `__rhs`.
  5712. */
  5713. template<typename _CharT, typename _Traits, typename _Alloc>
  5714. inline auto
  5715. operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5716. const _CharT* __rhs) noexcept
  5717. -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
  5718. { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); }
  5719. #else
  5720. /**
  5721. * @brief Test equivalence of C string and string.
  5722. * @param __lhs C string.
  5723. * @param __rhs String.
  5724. * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
  5725. */
  5726. template<typename _CharT, typename _Traits, typename _Alloc>
  5727. inline bool
  5728. operator==(const _CharT* __lhs,
  5729. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5730. { return __rhs.compare(__lhs) == 0; }
  5731. // operator !=
  5732. /**
  5733. * @brief Test difference of two strings.
  5734. * @param __lhs First string.
  5735. * @param __rhs Second string.
  5736. * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
  5737. */
  5738. template<typename _CharT, typename _Traits, typename _Alloc>
  5739. inline bool
  5740. operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5741. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5742. _GLIBCXX_NOEXCEPT
  5743. { return !(__lhs == __rhs); }
  5744. /**
  5745. * @brief Test difference of C string and string.
  5746. * @param __lhs C string.
  5747. * @param __rhs String.
  5748. * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
  5749. */
  5750. template<typename _CharT, typename _Traits, typename _Alloc>
  5751. inline bool
  5752. operator!=(const _CharT* __lhs,
  5753. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5754. { return !(__lhs == __rhs); }
  5755. /**
  5756. * @brief Test difference of string and C string.
  5757. * @param __lhs String.
  5758. * @param __rhs C string.
  5759. * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
  5760. */
  5761. template<typename _CharT, typename _Traits, typename _Alloc>
  5762. inline bool
  5763. operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5764. const _CharT* __rhs)
  5765. { return !(__lhs == __rhs); }
  5766. // operator <
  5767. /**
  5768. * @brief Test if string precedes string.
  5769. * @param __lhs First string.
  5770. * @param __rhs Second string.
  5771. * @return True if @a __lhs precedes @a __rhs. False otherwise.
  5772. */
  5773. template<typename _CharT, typename _Traits, typename _Alloc>
  5774. inline bool
  5775. operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5776. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5777. _GLIBCXX_NOEXCEPT
  5778. { return __lhs.compare(__rhs) < 0; }
  5779. /**
  5780. * @brief Test if string precedes C string.
  5781. * @param __lhs String.
  5782. * @param __rhs C string.
  5783. * @return True if @a __lhs precedes @a __rhs. False otherwise.
  5784. */
  5785. template<typename _CharT, typename _Traits, typename _Alloc>
  5786. inline bool
  5787. operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5788. const _CharT* __rhs)
  5789. { return __lhs.compare(__rhs) < 0; }
  5790. /**
  5791. * @brief Test if C string precedes string.
  5792. * @param __lhs C string.
  5793. * @param __rhs String.
  5794. * @return True if @a __lhs precedes @a __rhs. False otherwise.
  5795. */
  5796. template<typename _CharT, typename _Traits, typename _Alloc>
  5797. inline bool
  5798. operator<(const _CharT* __lhs,
  5799. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5800. { return __rhs.compare(__lhs) > 0; }
  5801. // operator >
  5802. /**
  5803. * @brief Test if string follows string.
  5804. * @param __lhs First string.
  5805. * @param __rhs Second string.
  5806. * @return True if @a __lhs follows @a __rhs. False otherwise.
  5807. */
  5808. template<typename _CharT, typename _Traits, typename _Alloc>
  5809. inline bool
  5810. operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5811. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5812. _GLIBCXX_NOEXCEPT
  5813. { return __lhs.compare(__rhs) > 0; }
  5814. /**
  5815. * @brief Test if string follows C string.
  5816. * @param __lhs String.
  5817. * @param __rhs C string.
  5818. * @return True if @a __lhs follows @a __rhs. False otherwise.
  5819. */
  5820. template<typename _CharT, typename _Traits, typename _Alloc>
  5821. inline bool
  5822. operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5823. const _CharT* __rhs)
  5824. { return __lhs.compare(__rhs) > 0; }
  5825. /**
  5826. * @brief Test if C string follows string.
  5827. * @param __lhs C string.
  5828. * @param __rhs String.
  5829. * @return True if @a __lhs follows @a __rhs. False otherwise.
  5830. */
  5831. template<typename _CharT, typename _Traits, typename _Alloc>
  5832. inline bool
  5833. operator>(const _CharT* __lhs,
  5834. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5835. { return __rhs.compare(__lhs) < 0; }
  5836. // operator <=
  5837. /**
  5838. * @brief Test if string doesn't follow string.
  5839. * @param __lhs First string.
  5840. * @param __rhs Second string.
  5841. * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
  5842. */
  5843. template<typename _CharT, typename _Traits, typename _Alloc>
  5844. inline bool
  5845. operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5846. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5847. _GLIBCXX_NOEXCEPT
  5848. { return __lhs.compare(__rhs) <= 0; }
  5849. /**
  5850. * @brief Test if string doesn't follow C string.
  5851. * @param __lhs String.
  5852. * @param __rhs C string.
  5853. * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
  5854. */
  5855. template<typename _CharT, typename _Traits, typename _Alloc>
  5856. inline bool
  5857. operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5858. const _CharT* __rhs)
  5859. { return __lhs.compare(__rhs) <= 0; }
  5860. /**
  5861. * @brief Test if C string doesn't follow string.
  5862. * @param __lhs C string.
  5863. * @param __rhs String.
  5864. * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
  5865. */
  5866. template<typename _CharT, typename _Traits, typename _Alloc>
  5867. inline bool
  5868. operator<=(const _CharT* __lhs,
  5869. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5870. { return __rhs.compare(__lhs) >= 0; }
  5871. // operator >=
  5872. /**
  5873. * @brief Test if string doesn't precede string.
  5874. * @param __lhs First string.
  5875. * @param __rhs Second string.
  5876. * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
  5877. */
  5878. template<typename _CharT, typename _Traits, typename _Alloc>
  5879. inline bool
  5880. operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5881. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5882. _GLIBCXX_NOEXCEPT
  5883. { return __lhs.compare(__rhs) >= 0; }
  5884. /**
  5885. * @brief Test if string doesn't precede C string.
  5886. * @param __lhs String.
  5887. * @param __rhs C string.
  5888. * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
  5889. */
  5890. template<typename _CharT, typename _Traits, typename _Alloc>
  5891. inline bool
  5892. operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5893. const _CharT* __rhs)
  5894. { return __lhs.compare(__rhs) >= 0; }
  5895. /**
  5896. * @brief Test if C string doesn't precede string.
  5897. * @param __lhs C string.
  5898. * @param __rhs String.
  5899. * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
  5900. */
  5901. template<typename _CharT, typename _Traits, typename _Alloc>
  5902. inline bool
  5903. operator>=(const _CharT* __lhs,
  5904. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5905. { return __rhs.compare(__lhs) <= 0; }
  5906. #endif // three-way comparison
  5907. /**
  5908. * @brief Swap contents of two strings.
  5909. * @param __lhs First string.
  5910. * @param __rhs Second string.
  5911. *
  5912. * Exchanges the contents of @a __lhs and @a __rhs in constant time.
  5913. */
  5914. template<typename _CharT, typename _Traits, typename _Alloc>
  5915. inline void
  5916. swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
  5917. basic_string<_CharT, _Traits, _Alloc>& __rhs)
  5918. _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
  5919. { __lhs.swap(__rhs); }
  5920. /**
  5921. * @brief Read stream into a string.
  5922. * @param __is Input stream.
  5923. * @param __str Buffer to store into.
  5924. * @return Reference to the input stream.
  5925. *
  5926. * Stores characters from @a __is into @a __str until whitespace is
  5927. * found, the end of the stream is encountered, or str.max_size()
  5928. * is reached. If is.width() is non-zero, that is the limit on the
  5929. * number of characters stored into @a __str. Any previous
  5930. * contents of @a __str are erased.
  5931. */
  5932. template<typename _CharT, typename _Traits, typename _Alloc>
  5933. basic_istream<_CharT, _Traits>&
  5934. operator>>(basic_istream<_CharT, _Traits>& __is,
  5935. basic_string<_CharT, _Traits, _Alloc>& __str);
  5936. template<>
  5937. basic_istream<char>&
  5938. operator>>(basic_istream<char>& __is, basic_string<char>& __str);
  5939. /**
  5940. * @brief Write string to a stream.
  5941. * @param __os Output stream.
  5942. * @param __str String to write out.
  5943. * @return Reference to the output stream.
  5944. *
  5945. * Output characters of @a __str into os following the same rules as for
  5946. * writing a C string.
  5947. */
  5948. template<typename _CharT, typename _Traits, typename _Alloc>
  5949. inline basic_ostream<_CharT, _Traits>&
  5950. operator<<(basic_ostream<_CharT, _Traits>& __os,
  5951. const basic_string<_CharT, _Traits, _Alloc>& __str)
  5952. {
  5953. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  5954. // 586. string inserter not a formatted function
  5955. return __ostream_insert(__os, __str.data(), __str.size());
  5956. }
  5957. /**
  5958. * @brief Read a line from stream into a string.
  5959. * @param __is Input stream.
  5960. * @param __str Buffer to store into.
  5961. * @param __delim Character marking end of line.
  5962. * @return Reference to the input stream.
  5963. *
  5964. * Stores characters from @a __is into @a __str until @a __delim is
  5965. * found, the end of the stream is encountered, or str.max_size()
  5966. * is reached. Any previous contents of @a __str are erased. If
  5967. * @a __delim is encountered, it is extracted but not stored into
  5968. * @a __str.
  5969. */
  5970. template<typename _CharT, typename _Traits, typename _Alloc>
  5971. basic_istream<_CharT, _Traits>&
  5972. getline(basic_istream<_CharT, _Traits>& __is,
  5973. basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
  5974. /**
  5975. * @brief Read a line from stream into a string.
  5976. * @param __is Input stream.
  5977. * @param __str Buffer to store into.
  5978. * @return Reference to the input stream.
  5979. *
  5980. * Stores characters from is into @a __str until &apos;\n&apos; is
  5981. * found, the end of the stream is encountered, or str.max_size()
  5982. * is reached. Any previous contents of @a __str are erased. If
  5983. * end of line is encountered, it is extracted but not stored into
  5984. * @a __str.
  5985. */
  5986. template<typename _CharT, typename _Traits, typename _Alloc>
  5987. inline basic_istream<_CharT, _Traits>&
  5988. getline(basic_istream<_CharT, _Traits>& __is,
  5989. basic_string<_CharT, _Traits, _Alloc>& __str)
  5990. { return std::getline(__is, __str, __is.widen('\n')); }
  5991. #if __cplusplus >= 201103L
  5992. /// Read a line from an rvalue stream into a string.
  5993. template<typename _CharT, typename _Traits, typename _Alloc>
  5994. inline basic_istream<_CharT, _Traits>&
  5995. getline(basic_istream<_CharT, _Traits>&& __is,
  5996. basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
  5997. { return std::getline(__is, __str, __delim); }
  5998. /// Read a line from an rvalue stream into a string.
  5999. template<typename _CharT, typename _Traits, typename _Alloc>
  6000. inline basic_istream<_CharT, _Traits>&
  6001. getline(basic_istream<_CharT, _Traits>&& __is,
  6002. basic_string<_CharT, _Traits, _Alloc>& __str)
  6003. { return std::getline(__is, __str); }
  6004. #endif
  6005. template<>
  6006. basic_istream<char>&
  6007. getline(basic_istream<char>& __in, basic_string<char>& __str,
  6008. char __delim);
  6009. #ifdef _GLIBCXX_USE_WCHAR_T
  6010. template<>
  6011. basic_istream<wchar_t>&
  6012. getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
  6013. wchar_t __delim);
  6014. #endif
  6015. _GLIBCXX_END_NAMESPACE_VERSION
  6016. } // namespace
  6017. #if __cplusplus >= 201103L
  6018. #include <ext/string_conversions.h>
  6019. #include <bits/charconv.h>
  6020. namespace std _GLIBCXX_VISIBILITY(default)
  6021. {
  6022. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  6023. _GLIBCXX_BEGIN_NAMESPACE_CXX11
  6024. #if _GLIBCXX_USE_C99_STDLIB
  6025. // 21.4 Numeric Conversions [string.conversions].
  6026. inline int
  6027. stoi(const string& __str, size_t* __idx = 0, int __base = 10)
  6028. { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
  6029. __idx, __base); }
  6030. inline long
  6031. stol(const string& __str, size_t* __idx = 0, int __base = 10)
  6032. { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
  6033. __idx, __base); }
  6034. inline unsigned long
  6035. stoul(const string& __str, size_t* __idx = 0, int __base = 10)
  6036. { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
  6037. __idx, __base); }
  6038. inline long long
  6039. stoll(const string& __str, size_t* __idx = 0, int __base = 10)
  6040. { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
  6041. __idx, __base); }
  6042. inline unsigned long long
  6043. stoull(const string& __str, size_t* __idx = 0, int __base = 10)
  6044. { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
  6045. __idx, __base); }
  6046. // NB: strtof vs strtod.
  6047. inline float
  6048. stof(const string& __str, size_t* __idx = 0)
  6049. { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
  6050. inline double
  6051. stod(const string& __str, size_t* __idx = 0)
  6052. { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
  6053. inline long double
  6054. stold(const string& __str, size_t* __idx = 0)
  6055. { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
  6056. #endif // _GLIBCXX_USE_C99_STDLIB
  6057. // DR 1261. Insufficent overloads for to_string / to_wstring
  6058. inline string
  6059. to_string(int __val)
  6060. {
  6061. const bool __neg = __val < 0;
  6062. const unsigned __uval = __neg ? (unsigned)~__val + 1u : __val;
  6063. const auto __len = __detail::__to_chars_len(__uval);
  6064. string __str(__neg + __len, '-');
  6065. __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
  6066. return __str;
  6067. }
  6068. inline string
  6069. to_string(unsigned __val)
  6070. {
  6071. string __str(__detail::__to_chars_len(__val), '\0');
  6072. __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);
  6073. return __str;
  6074. }
  6075. inline string
  6076. to_string(long __val)
  6077. {
  6078. const bool __neg = __val < 0;
  6079. const unsigned long __uval = __neg ? (unsigned long)~__val + 1ul : __val;
  6080. const auto __len = __detail::__to_chars_len(__uval);
  6081. string __str(__neg + __len, '-');
  6082. __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
  6083. return __str;
  6084. }
  6085. inline string
  6086. to_string(unsigned long __val)
  6087. {
  6088. string __str(__detail::__to_chars_len(__val), '\0');
  6089. __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);
  6090. return __str;
  6091. }
  6092. inline string
  6093. to_string(long long __val)
  6094. {
  6095. const bool __neg = __val < 0;
  6096. const unsigned long long __uval
  6097. = __neg ? (unsigned long long)~__val + 1ull : __val;
  6098. const auto __len = __detail::__to_chars_len(__uval);
  6099. string __str(__neg + __len, '-');
  6100. __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
  6101. return __str;
  6102. }
  6103. inline string
  6104. to_string(unsigned long long __val)
  6105. {
  6106. string __str(__detail::__to_chars_len(__val), '\0');
  6107. __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);
  6108. return __str;
  6109. }
  6110. #if _GLIBCXX_USE_C99_STDIO
  6111. // NB: (v)snprintf vs sprintf.
  6112. inline string
  6113. to_string(float __val)
  6114. {
  6115. const int __n =
  6116. __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
  6117. return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
  6118. "%f", __val);
  6119. }
  6120. inline string
  6121. to_string(double __val)
  6122. {
  6123. const int __n =
  6124. __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
  6125. return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
  6126. "%f", __val);
  6127. }
  6128. inline string
  6129. to_string(long double __val)
  6130. {
  6131. const int __n =
  6132. __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
  6133. return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
  6134. "%Lf", __val);
  6135. }
  6136. #endif // _GLIBCXX_USE_C99_STDIO
  6137. #if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
  6138. inline int
  6139. stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
  6140. { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
  6141. __idx, __base); }
  6142. inline long
  6143. stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
  6144. { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
  6145. __idx, __base); }
  6146. inline unsigned long
  6147. stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
  6148. { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
  6149. __idx, __base); }
  6150. inline long long
  6151. stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
  6152. { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
  6153. __idx, __base); }
  6154. inline unsigned long long
  6155. stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
  6156. { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
  6157. __idx, __base); }
  6158. // NB: wcstof vs wcstod.
  6159. inline float
  6160. stof(const wstring& __str, size_t* __idx = 0)
  6161. { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
  6162. inline double
  6163. stod(const wstring& __str, size_t* __idx = 0)
  6164. { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
  6165. inline long double
  6166. stold(const wstring& __str, size_t* __idx = 0)
  6167. { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
  6168. #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
  6169. // DR 1261.
  6170. inline wstring
  6171. to_wstring(int __val)
  6172. { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int),
  6173. L"%d", __val); }
  6174. inline wstring
  6175. to_wstring(unsigned __val)
  6176. { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
  6177. 4 * sizeof(unsigned),
  6178. L"%u", __val); }
  6179. inline wstring
  6180. to_wstring(long __val)
  6181. { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long),
  6182. L"%ld", __val); }
  6183. inline wstring
  6184. to_wstring(unsigned long __val)
  6185. { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
  6186. 4 * sizeof(unsigned long),
  6187. L"%lu", __val); }
  6188. inline wstring
  6189. to_wstring(long long __val)
  6190. { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
  6191. 4 * sizeof(long long),
  6192. L"%lld", __val); }
  6193. inline wstring
  6194. to_wstring(unsigned long long __val)
  6195. { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
  6196. 4 * sizeof(unsigned long long),
  6197. L"%llu", __val); }
  6198. inline wstring
  6199. to_wstring(float __val)
  6200. {
  6201. const int __n =
  6202. __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
  6203. return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
  6204. L"%f", __val);
  6205. }
  6206. inline wstring
  6207. to_wstring(double __val)
  6208. {
  6209. const int __n =
  6210. __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
  6211. return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
  6212. L"%f", __val);
  6213. }
  6214. inline wstring
  6215. to_wstring(long double __val)
  6216. {
  6217. const int __n =
  6218. __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
  6219. return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
  6220. L"%Lf", __val);
  6221. }
  6222. #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
  6223. #endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR
  6224. _GLIBCXX_END_NAMESPACE_CXX11
  6225. _GLIBCXX_END_NAMESPACE_VERSION
  6226. } // namespace
  6227. #endif /* C++11 */
  6228. #if __cplusplus >= 201103L
  6229. #include <bits/functional_hash.h>
  6230. namespace std _GLIBCXX_VISIBILITY(default)
  6231. {
  6232. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  6233. // DR 1182.
  6234. #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
  6235. /// std::hash specialization for string.
  6236. template<>
  6237. struct hash<string>
  6238. : public __hash_base<size_t, string>
  6239. {
  6240. size_t
  6241. operator()(const string& __s) const noexcept
  6242. { return std::_Hash_impl::hash(__s.data(), __s.length()); }
  6243. };
  6244. template<>
  6245. struct __is_fast_hash<hash<string>> : std::false_type
  6246. { };
  6247. #ifdef _GLIBCXX_USE_WCHAR_T
  6248. /// std::hash specialization for wstring.
  6249. template<>
  6250. struct hash<wstring>
  6251. : public __hash_base<size_t, wstring>
  6252. {
  6253. size_t
  6254. operator()(const wstring& __s) const noexcept
  6255. { return std::_Hash_impl::hash(__s.data(),
  6256. __s.length() * sizeof(wchar_t)); }
  6257. };
  6258. template<>
  6259. struct __is_fast_hash<hash<wstring>> : std::false_type
  6260. { };
  6261. #endif
  6262. #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
  6263. #ifdef _GLIBCXX_USE_CHAR8_T
  6264. /// std::hash specialization for u8string.
  6265. template<>
  6266. struct hash<u8string>
  6267. : public __hash_base<size_t, u8string>
  6268. {
  6269. size_t
  6270. operator()(const u8string& __s) const noexcept
  6271. { return std::_Hash_impl::hash(__s.data(),
  6272. __s.length() * sizeof(char8_t)); }
  6273. };
  6274. template<>
  6275. struct __is_fast_hash<hash<u8string>> : std::false_type
  6276. { };
  6277. #endif
  6278. /// std::hash specialization for u16string.
  6279. template<>
  6280. struct hash<u16string>
  6281. : public __hash_base<size_t, u16string>
  6282. {
  6283. size_t
  6284. operator()(const u16string& __s) const noexcept
  6285. { return std::_Hash_impl::hash(__s.data(),
  6286. __s.length() * sizeof(char16_t)); }
  6287. };
  6288. template<>
  6289. struct __is_fast_hash<hash<u16string>> : std::false_type
  6290. { };
  6291. /// std::hash specialization for u32string.
  6292. template<>
  6293. struct hash<u32string>
  6294. : public __hash_base<size_t, u32string>
  6295. {
  6296. size_t
  6297. operator()(const u32string& __s) const noexcept
  6298. { return std::_Hash_impl::hash(__s.data(),
  6299. __s.length() * sizeof(char32_t)); }
  6300. };
  6301. template<>
  6302. struct __is_fast_hash<hash<u32string>> : std::false_type
  6303. { };
  6304. #if __cplusplus >= 201402L
  6305. #define __cpp_lib_string_udls 201304
  6306. inline namespace literals
  6307. {
  6308. inline namespace string_literals
  6309. {
  6310. #pragma GCC diagnostic push
  6311. #pragma GCC diagnostic ignored "-Wliteral-suffix"
  6312. _GLIBCXX_DEFAULT_ABI_TAG
  6313. inline basic_string<char>
  6314. operator""s(const char* __str, size_t __len)
  6315. { return basic_string<char>{__str, __len}; }
  6316. #ifdef _GLIBCXX_USE_WCHAR_T
  6317. _GLIBCXX_DEFAULT_ABI_TAG
  6318. inline basic_string<wchar_t>
  6319. operator""s(const wchar_t* __str, size_t __len)
  6320. { return basic_string<wchar_t>{__str, __len}; }
  6321. #endif
  6322. #ifdef _GLIBCXX_USE_CHAR8_T
  6323. _GLIBCXX_DEFAULT_ABI_TAG
  6324. inline basic_string<char8_t>
  6325. operator""s(const char8_t* __str, size_t __len)
  6326. { return basic_string<char8_t>{__str, __len}; }
  6327. #endif
  6328. _GLIBCXX_DEFAULT_ABI_TAG
  6329. inline basic_string<char16_t>
  6330. operator""s(const char16_t* __str, size_t __len)
  6331. { return basic_string<char16_t>{__str, __len}; }
  6332. _GLIBCXX_DEFAULT_ABI_TAG
  6333. inline basic_string<char32_t>
  6334. operator""s(const char32_t* __str, size_t __len)
  6335. { return basic_string<char32_t>{__str, __len}; }
  6336. #pragma GCC diagnostic pop
  6337. } // inline namespace string_literals
  6338. } // inline namespace literals
  6339. #if __cplusplus >= 201703L
  6340. namespace __detail::__variant
  6341. {
  6342. template<typename> struct _Never_valueless_alt; // see <variant>
  6343. // Provide the strong exception-safety guarantee when emplacing a
  6344. // basic_string into a variant, but only if moving the string cannot throw.
  6345. template<typename _Tp, typename _Traits, typename _Alloc>
  6346. struct _Never_valueless_alt<std::basic_string<_Tp, _Traits, _Alloc>>
  6347. : __and_<
  6348. is_nothrow_move_constructible<std::basic_string<_Tp, _Traits, _Alloc>>,
  6349. is_nothrow_move_assignable<std::basic_string<_Tp, _Traits, _Alloc>>
  6350. >::type
  6351. { };
  6352. } // namespace __detail::__variant
  6353. #endif // C++17
  6354. #endif // C++14
  6355. _GLIBCXX_END_NAMESPACE_VERSION
  6356. } // namespace std
  6357. #endif // C++11
  6358. #endif /* _BASIC_STRING_H */