Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

2967 lines
108KB

  1. // Versatile string -*- C++ -*-
  2. // Copyright (C) 2005-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 ext/vstring.h
  21. * This file is a GNU extension to the Standard C++ Library.
  22. */
  23. #ifndef _VSTRING_H
  24. #define _VSTRING_H 1
  25. #pragma GCC system_header
  26. #if __cplusplus >= 201103L
  27. #include <initializer_list>
  28. #endif
  29. #include <ext/vstring_util.h>
  30. #include <ext/rc_string_base.h>
  31. #include <ext/sso_string_base.h>
  32. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  33. {
  34. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  35. /**
  36. * @class __versa_string vstring.h
  37. * @brief Template class __versa_string.
  38. * @ingroup extensions
  39. *
  40. * Data structure managing sequences of characters and
  41. * character-like objects.
  42. */
  43. template<typename _CharT, typename _Traits, typename _Alloc,
  44. template <typename, typename, typename> class _Base>
  45. class __versa_string
  46. : private _Base<_CharT, _Traits, _Alloc>
  47. {
  48. typedef _Base<_CharT, _Traits, _Alloc> __vstring_base;
  49. typedef typename __vstring_base::_CharT_alloc_type _CharT_alloc_type;
  50. typedef __alloc_traits<_CharT_alloc_type> _CharT_alloc_traits;
  51. // Types:
  52. public:
  53. typedef _Traits traits_type;
  54. typedef typename _Traits::char_type value_type;
  55. typedef _Alloc allocator_type;
  56. typedef typename _CharT_alloc_type::size_type size_type;
  57. typedef typename _CharT_alloc_type::difference_type difference_type;
  58. typedef value_type& reference;
  59. typedef const value_type& const_reference;
  60. typedef typename _CharT_alloc_traits::pointer pointer;
  61. typedef typename _CharT_alloc_traits::const_pointer const_pointer;
  62. typedef __gnu_cxx::__normal_iterator<pointer, __versa_string> iterator;
  63. typedef __gnu_cxx::__normal_iterator<const_pointer, __versa_string>
  64. const_iterator;
  65. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  66. typedef std::reverse_iterator<iterator> reverse_iterator;
  67. // Data Member (public):
  68. /// Value returned by various member functions when they fail.
  69. static const size_type npos = static_cast<size_type>(-1);
  70. private:
  71. size_type
  72. _M_check(size_type __pos, const char* __s) const
  73. {
  74. if (__pos > this->size())
  75. std::__throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
  76. "this->size() (which is %zu)"),
  77. __s, __pos, this->size());
  78. return __pos;
  79. }
  80. void
  81. _M_check_length(size_type __n1, size_type __n2, const char* __s) const
  82. {
  83. if (this->max_size() - (this->size() - __n1) < __n2)
  84. std::__throw_length_error(__N(__s));
  85. }
  86. // NB: _M_limit doesn't check for a bad __pos value.
  87. size_type
  88. _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
  89. {
  90. const bool __testoff = __off < this->size() - __pos;
  91. return __testoff ? __off : this->size() - __pos;
  92. }
  93. // True if _Rep and source do not overlap.
  94. bool
  95. _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
  96. {
  97. return (std::less<const _CharT*>()(__s, this->_M_data())
  98. || std::less<const _CharT*>()(this->_M_data()
  99. + this->size(), __s));
  100. }
  101. // For the internal use we have functions similar to `begin'/`end'
  102. // but they do not call _M_leak.
  103. iterator
  104. _M_ibegin() const _GLIBCXX_NOEXCEPT
  105. { return iterator(this->_M_data()); }
  106. iterator
  107. _M_iend() const _GLIBCXX_NOEXCEPT
  108. { return iterator(this->_M_data() + this->_M_length()); }
  109. public:
  110. // Construct/copy/destroy:
  111. // NB: We overload ctors in some cases instead of using default
  112. // arguments, per 17.4.4.4 para. 2 item 2.
  113. /**
  114. * @brief Construct an empty string using allocator @a a.
  115. */
  116. explicit
  117. __versa_string(const _Alloc& __a = _Alloc()) _GLIBCXX_NOEXCEPT
  118. : __vstring_base(__a) { }
  119. // NB: per LWG issue 42, semantics different from IS:
  120. /**
  121. * @brief Construct string with copy of value of @a __str.
  122. * @param __str Source string.
  123. */
  124. __versa_string(const __versa_string& __str)
  125. : __vstring_base(__str) { }
  126. #if __cplusplus >= 201103L
  127. /**
  128. * @brief String move constructor.
  129. * @param __str Source string.
  130. *
  131. * The newly-constructed %string contains the exact contents of
  132. * @a __str. The contents of @a __str are a valid, but unspecified
  133. * string.
  134. */
  135. __versa_string(__versa_string&& __str) noexcept
  136. : __vstring_base(std::move(__str)) { }
  137. /**
  138. * @brief Construct string from an initializer list.
  139. * @param __l std::initializer_list of characters.
  140. * @param __a Allocator to use (default is default allocator).
  141. */
  142. __versa_string(std::initializer_list<_CharT> __l,
  143. const _Alloc& __a = _Alloc())
  144. : __vstring_base(__l.begin(), __l.end(), __a) { }
  145. #endif
  146. /**
  147. * @brief Construct string as copy of a substring.
  148. * @param __str Source string.
  149. * @param __pos Index of first character to copy from.
  150. * @param __n Number of characters to copy (default remainder).
  151. */
  152. __versa_string(const __versa_string& __str, size_type __pos,
  153. size_type __n = npos)
  154. : __vstring_base(__str._M_data()
  155. + __str._M_check(__pos,
  156. "__versa_string::__versa_string"),
  157. __str._M_data() + __str._M_limit(__pos, __n)
  158. + __pos, _Alloc()) { }
  159. /**
  160. * @brief Construct string as copy of a substring.
  161. * @param __str Source string.
  162. * @param __pos Index of first character to copy from.
  163. * @param __n Number of characters to copy.
  164. * @param __a Allocator to use.
  165. */
  166. __versa_string(const __versa_string& __str, size_type __pos,
  167. size_type __n, const _Alloc& __a)
  168. : __vstring_base(__str._M_data()
  169. + __str._M_check(__pos,
  170. "__versa_string::__versa_string"),
  171. __str._M_data() + __str._M_limit(__pos, __n)
  172. + __pos, __a) { }
  173. /**
  174. * @brief Construct string initialized by a character array.
  175. * @param __s Source character array.
  176. * @param __n Number of characters to copy.
  177. * @param __a Allocator to use (default is default allocator).
  178. *
  179. * NB: @a __s must have at least @a __n characters, '\\0' has no special
  180. * meaning.
  181. */
  182. __versa_string(const _CharT* __s, size_type __n,
  183. const _Alloc& __a = _Alloc())
  184. : __vstring_base(__s, __s + __n, __a) { }
  185. /**
  186. * @brief Construct string as copy of a C string.
  187. * @param __s Source C string.
  188. * @param __a Allocator to use (default is default allocator).
  189. */
  190. __versa_string(const _CharT* __s, const _Alloc& __a = _Alloc())
  191. : __vstring_base(__s, __s ? __s + traits_type::length(__s) :
  192. __s + npos, __a) { }
  193. /**
  194. * @brief Construct string as multiple characters.
  195. * @param __n Number of characters.
  196. * @param __c Character to use.
  197. * @param __a Allocator to use (default is default allocator).
  198. */
  199. __versa_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
  200. : __vstring_base(__n, __c, __a) { }
  201. /**
  202. * @brief Construct string as copy of a range.
  203. * @param __beg Start of range.
  204. * @param __end End of range.
  205. * @param __a Allocator to use (default is default allocator).
  206. */
  207. #if __cplusplus >= 201103L
  208. template<class _InputIterator,
  209. typename = std::_RequireInputIter<_InputIterator>>
  210. #else
  211. template<class _InputIterator>
  212. #endif
  213. __versa_string(_InputIterator __beg, _InputIterator __end,
  214. const _Alloc& __a = _Alloc())
  215. : __vstring_base(__beg, __end, __a) { }
  216. /**
  217. * @brief Destroy the string instance.
  218. */
  219. ~__versa_string() _GLIBCXX_NOEXCEPT { }
  220. /**
  221. * @brief Assign the value of @a str to this string.
  222. * @param __str Source string.
  223. */
  224. __versa_string&
  225. operator=(const __versa_string& __str)
  226. { return this->assign(__str); }
  227. #if __cplusplus >= 201103L
  228. /**
  229. * @brief String move assignment operator.
  230. * @param __str Source string.
  231. *
  232. * The contents of @a __str are moved into this string (without
  233. * copying). @a __str is a valid, but unspecified string.
  234. */
  235. __versa_string&
  236. operator=(__versa_string&& __str) noexcept
  237. {
  238. // NB: DR 1204.
  239. this->swap(__str);
  240. return *this;
  241. }
  242. /**
  243. * @brief Set value to string constructed from initializer list.
  244. * @param __l std::initializer_list.
  245. */
  246. __versa_string&
  247. operator=(std::initializer_list<_CharT> __l)
  248. {
  249. this->assign(__l.begin(), __l.end());
  250. return *this;
  251. }
  252. #endif
  253. /**
  254. * @brief Copy contents of @a __s into this string.
  255. * @param __s Source null-terminated string.
  256. */
  257. __versa_string&
  258. operator=(const _CharT* __s)
  259. { return this->assign(__s); }
  260. /**
  261. * @brief Set value to string of length 1.
  262. * @param __c Source character.
  263. *
  264. * Assigning to a character makes this string length 1 and
  265. * (*this)[0] == @a __c.
  266. */
  267. __versa_string&
  268. operator=(_CharT __c)
  269. {
  270. this->assign(1, __c);
  271. return *this;
  272. }
  273. // Iterators:
  274. /**
  275. * Returns a read/write iterator that points to the first character in
  276. * the %string. Unshares the string.
  277. */
  278. iterator
  279. begin() _GLIBCXX_NOEXCEPT
  280. {
  281. this->_M_leak();
  282. return iterator(this->_M_data());
  283. }
  284. /**
  285. * Returns a read-only (constant) iterator that points to the first
  286. * character in the %string.
  287. */
  288. const_iterator
  289. begin() const _GLIBCXX_NOEXCEPT
  290. { return const_iterator(this->_M_data()); }
  291. /**
  292. * Returns a read/write iterator that points one past the last
  293. * character in the %string. Unshares the string.
  294. */
  295. iterator
  296. end() _GLIBCXX_NOEXCEPT
  297. {
  298. this->_M_leak();
  299. return iterator(this->_M_data() + this->size());
  300. }
  301. /**
  302. * Returns a read-only (constant) iterator that points one past the
  303. * last character in the %string.
  304. */
  305. const_iterator
  306. end() const _GLIBCXX_NOEXCEPT
  307. { return const_iterator(this->_M_data() + this->size()); }
  308. /**
  309. * Returns a read/write reverse iterator that points to the last
  310. * character in the %string. Iteration is done in reverse element
  311. * order. Unshares the string.
  312. */
  313. reverse_iterator
  314. rbegin() _GLIBCXX_NOEXCEPT
  315. { return reverse_iterator(this->end()); }
  316. /**
  317. * Returns a read-only (constant) reverse iterator that points
  318. * to the last character in the %string. Iteration is done in
  319. * reverse element order.
  320. */
  321. const_reverse_iterator
  322. rbegin() const _GLIBCXX_NOEXCEPT
  323. { return const_reverse_iterator(this->end()); }
  324. /**
  325. * Returns a read/write reverse iterator that points to one before the
  326. * first character in the %string. Iteration is done in reverse
  327. * element order. Unshares the string.
  328. */
  329. reverse_iterator
  330. rend() _GLIBCXX_NOEXCEPT
  331. { return reverse_iterator(this->begin()); }
  332. /**
  333. * Returns a read-only (constant) reverse iterator that points
  334. * to one before the first character in the %string. Iteration
  335. * is done in reverse element order.
  336. */
  337. const_reverse_iterator
  338. rend() const _GLIBCXX_NOEXCEPT
  339. { return const_reverse_iterator(this->begin()); }
  340. #if __cplusplus >= 201103L
  341. /**
  342. * Returns a read-only (constant) iterator that points to the first
  343. * character in the %string.
  344. */
  345. const_iterator
  346. cbegin() const noexcept
  347. { return const_iterator(this->_M_data()); }
  348. /**
  349. * Returns a read-only (constant) iterator that points one past the
  350. * last character in the %string.
  351. */
  352. const_iterator
  353. cend() const noexcept
  354. { return const_iterator(this->_M_data() + this->size()); }
  355. /**
  356. * Returns a read-only (constant) reverse iterator that points
  357. * to the last character in the %string. Iteration is done in
  358. * reverse element order.
  359. */
  360. const_reverse_iterator
  361. crbegin() const noexcept
  362. { return const_reverse_iterator(this->end()); }
  363. /**
  364. * Returns a read-only (constant) reverse iterator that points
  365. * to one before the first character in the %string. Iteration
  366. * is done in reverse element order.
  367. */
  368. const_reverse_iterator
  369. crend() const noexcept
  370. { return const_reverse_iterator(this->begin()); }
  371. #endif
  372. public:
  373. // Capacity:
  374. /// Returns the number of characters in the string, not including any
  375. /// null-termination.
  376. size_type
  377. size() const _GLIBCXX_NOEXCEPT
  378. { return this->_M_length(); }
  379. /// Returns the number of characters in the string, not including any
  380. /// null-termination.
  381. size_type
  382. length() const _GLIBCXX_NOEXCEPT
  383. { return this->_M_length(); }
  384. /// Returns the size() of the largest possible %string.
  385. size_type
  386. max_size() const _GLIBCXX_NOEXCEPT
  387. { return this->_M_max_size(); }
  388. /**
  389. * @brief Resizes the %string to the specified number of characters.
  390. * @param __n Number of characters the %string should contain.
  391. * @param __c Character to fill any new elements.
  392. *
  393. * This function will %resize the %string to the specified
  394. * number of characters. If the number is smaller than the
  395. * %string's current size the %string is truncated, otherwise
  396. * the %string is extended and new elements are set to @a __c.
  397. */
  398. void
  399. resize(size_type __n, _CharT __c);
  400. /**
  401. * @brief Resizes the %string to the specified number of characters.
  402. * @param __n Number of characters the %string should contain.
  403. *
  404. * This function will resize the %string to the specified
  405. * length. If the new size is smaller than the %string's
  406. * current size the %string is truncated, otherwise the %string
  407. * is extended and new characters are default-constructed. For
  408. * basic types such as char, this means setting them to 0.
  409. */
  410. void
  411. resize(size_type __n)
  412. { this->resize(__n, _CharT()); }
  413. #if __cplusplus >= 201103L
  414. /// A non-binding request to reduce capacity() to size().
  415. void
  416. shrink_to_fit() noexcept
  417. {
  418. if (capacity() > size())
  419. {
  420. __try
  421. { this->reserve(0); }
  422. __catch(...)
  423. { }
  424. }
  425. }
  426. #endif
  427. /**
  428. * Returns the total number of characters that the %string can
  429. * hold before needing to allocate more memory.
  430. */
  431. size_type
  432. capacity() const _GLIBCXX_NOEXCEPT
  433. { return this->_M_capacity(); }
  434. /**
  435. * @brief Attempt to preallocate enough memory for specified number of
  436. * characters.
  437. * @param __res_arg Number of characters required.
  438. * @throw std::length_error If @a __res_arg exceeds @c max_size().
  439. *
  440. * This function attempts to reserve enough memory for the
  441. * %string to hold the specified number of characters. If the
  442. * number requested is more than max_size(), length_error is
  443. * thrown.
  444. *
  445. * The advantage of this function is that if optimal code is a
  446. * necessity and the user can determine the string length that
  447. * will be required, the user can reserve the memory in
  448. * %advance, and thus prevent a possible reallocation of memory
  449. * and copying of %string data.
  450. */
  451. void
  452. reserve(size_type __res_arg = 0)
  453. { this->_M_reserve(__res_arg); }
  454. /**
  455. * Erases the string, making it empty.
  456. */
  457. void
  458. clear() _GLIBCXX_NOEXCEPT
  459. { this->_M_clear(); }
  460. /**
  461. * Returns true if the %string is empty. Equivalent to
  462. * <code>*this == ""</code>.
  463. */
  464. _GLIBCXX_NODISCARD bool
  465. empty() const _GLIBCXX_NOEXCEPT
  466. { return this->size() == 0; }
  467. // Element access:
  468. /**
  469. * @brief Subscript access to the data contained in the %string.
  470. * @param __pos The index of the character to access.
  471. * @return Read-only (constant) reference to the character.
  472. *
  473. * This operator allows for easy, array-style, data access.
  474. * Note that data access with this operator is unchecked and
  475. * out_of_range lookups are not defined. (For checked lookups
  476. * see at().)
  477. */
  478. const_reference
  479. operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
  480. {
  481. __glibcxx_assert(__pos <= this->size());
  482. return this->_M_data()[__pos];
  483. }
  484. /**
  485. * @brief Subscript access to the data contained in the %string.
  486. * @param __pos The index of the character to access.
  487. * @return Read/write reference to the character.
  488. *
  489. * This operator allows for easy, array-style, data access.
  490. * Note that data access with this operator is unchecked and
  491. * out_of_range lookups are not defined. (For checked lookups
  492. * see at().) Unshares the string.
  493. */
  494. reference
  495. operator[](size_type __pos) _GLIBCXX_NOEXCEPT
  496. {
  497. // Allow pos == size() both in C++98 mode, as v3 extension,
  498. // and in C++11 mode.
  499. __glibcxx_assert(__pos <= this->size());
  500. // In pedantic mode be strict in C++98 mode.
  501. _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L
  502. || __pos < this->size());
  503. this->_M_leak();
  504. return this->_M_data()[__pos];
  505. }
  506. /**
  507. * @brief Provides access to the data contained in the %string.
  508. * @param __n The index of the character to access.
  509. * @return Read-only (const) reference to the character.
  510. * @throw std::out_of_range If @a __n is an invalid index.
  511. *
  512. * This function provides for safer data access. The parameter
  513. * is first checked that it is in the range of the string. The
  514. * function throws out_of_range if the check fails.
  515. */
  516. const_reference
  517. at(size_type __n) const
  518. {
  519. if (__n >= this->size())
  520. std::__throw_out_of_range_fmt(__N("__versa_string::at: __n "
  521. "(which is %zu) >= this->size() "
  522. "(which is %zu)"),
  523. __n, this->size());
  524. return this->_M_data()[__n];
  525. }
  526. /**
  527. * @brief Provides access to the data contained in the %string.
  528. * @param __n The index of the character to access.
  529. * @return Read/write reference to the character.
  530. * @throw std::out_of_range If @a __n is an invalid index.
  531. *
  532. * This function provides for safer data access. The parameter
  533. * is first checked that it is in the range of the string. The
  534. * function throws out_of_range if the check fails. Success
  535. * results in unsharing the string.
  536. */
  537. reference
  538. at(size_type __n)
  539. {
  540. if (__n >= this->size())
  541. std::__throw_out_of_range_fmt(__N("__versa_string::at: __n "
  542. "(which is %zu) >= this->size() "
  543. "(which is %zu)"),
  544. __n, this->size());
  545. this->_M_leak();
  546. return this->_M_data()[__n];
  547. }
  548. #if __cplusplus >= 201103L
  549. /**
  550. * Returns a read/write reference to the data at the first
  551. * element of the %string.
  552. */
  553. reference
  554. front() noexcept
  555. { return operator[](0); }
  556. /**
  557. * Returns a read-only (constant) reference to the data at the first
  558. * element of the %string.
  559. */
  560. const_reference
  561. front() const noexcept
  562. { return operator[](0); }
  563. /**
  564. * Returns a read/write reference to the data at the last
  565. * element of the %string.
  566. */
  567. reference
  568. back() noexcept
  569. { return operator[](this->size() - 1); }
  570. /**
  571. * Returns a read-only (constant) reference to the data at the
  572. * last element of the %string.
  573. */
  574. const_reference
  575. back() const noexcept
  576. { return operator[](this->size() - 1); }
  577. #endif
  578. // Modifiers:
  579. /**
  580. * @brief Append a string to this string.
  581. * @param __str The string to append.
  582. * @return Reference to this string.
  583. */
  584. __versa_string&
  585. operator+=(const __versa_string& __str)
  586. { return this->append(__str); }
  587. /**
  588. * @brief Append a C string.
  589. * @param __s The C string to append.
  590. * @return Reference to this string.
  591. */
  592. __versa_string&
  593. operator+=(const _CharT* __s)
  594. { return this->append(__s); }
  595. /**
  596. * @brief Append a character.
  597. * @param __c The character to append.
  598. * @return Reference to this string.
  599. */
  600. __versa_string&
  601. operator+=(_CharT __c)
  602. {
  603. this->push_back(__c);
  604. return *this;
  605. }
  606. #if __cplusplus >= 201103L
  607. /**
  608. * @brief Append an initializer_list of characters.
  609. * @param __l The initializer_list of characters to be appended.
  610. * @return Reference to this string.
  611. */
  612. __versa_string&
  613. operator+=(std::initializer_list<_CharT> __l)
  614. { return this->append(__l.begin(), __l.end()); }
  615. #endif // C++11
  616. /**
  617. * @brief Append a string to this string.
  618. * @param __str The string to append.
  619. * @return Reference to this string.
  620. */
  621. __versa_string&
  622. append(const __versa_string& __str)
  623. { return _M_append(__str._M_data(), __str.size()); }
  624. /**
  625. * @brief Append a substring.
  626. * @param __str The string to append.
  627. * @param __pos Index of the first character of str to append.
  628. * @param __n The number of characters to append.
  629. * @return Reference to this string.
  630. * @throw std::out_of_range if @a pos is not a valid index.
  631. *
  632. * This function appends @a __n characters from @a __str
  633. * starting at @a __pos to this string. If @a __n is is larger
  634. * than the number of available characters in @a __str, the
  635. * remainder of @a __str is appended.
  636. */
  637. __versa_string&
  638. append(const __versa_string& __str, size_type __pos, size_type __n)
  639. { return _M_append(__str._M_data()
  640. + __str._M_check(__pos, "__versa_string::append"),
  641. __str._M_limit(__pos, __n)); }
  642. /**
  643. * @brief Append a C substring.
  644. * @param __s The C string to append.
  645. * @param __n The number of characters to append.
  646. * @return Reference to this string.
  647. */
  648. __versa_string&
  649. append(const _CharT* __s, size_type __n)
  650. {
  651. __glibcxx_requires_string_len(__s, __n);
  652. _M_check_length(size_type(0), __n, "__versa_string::append");
  653. return _M_append(__s, __n);
  654. }
  655. /**
  656. * @brief Append a C string.
  657. * @param __s The C string to append.
  658. * @return Reference to this string.
  659. */
  660. __versa_string&
  661. append(const _CharT* __s)
  662. {
  663. __glibcxx_requires_string(__s);
  664. const size_type __n = traits_type::length(__s);
  665. _M_check_length(size_type(0), __n, "__versa_string::append");
  666. return _M_append(__s, __n);
  667. }
  668. /**
  669. * @brief Append multiple characters.
  670. * @param __n The number of characters to append.
  671. * @param __c The character to use.
  672. * @return Reference to this string.
  673. *
  674. * Appends n copies of c to this string.
  675. */
  676. __versa_string&
  677. append(size_type __n, _CharT __c)
  678. { return _M_replace_aux(this->size(), size_type(0), __n, __c); }
  679. #if __cplusplus >= 201103L
  680. /**
  681. * @brief Append an initializer_list of characters.
  682. * @param __l The initializer_list of characters to append.
  683. * @return Reference to this string.
  684. */
  685. __versa_string&
  686. append(std::initializer_list<_CharT> __l)
  687. { return this->append(__l.begin(), __l.end()); }
  688. #endif // C++11
  689. /**
  690. * @brief Append a range of characters.
  691. * @param __first Iterator referencing the first character to append.
  692. * @param __last Iterator marking the end of the range.
  693. * @return Reference to this string.
  694. *
  695. * Appends characters in the range [first,last) to this string.
  696. */
  697. #if __cplusplus >= 201103L
  698. template<class _InputIterator,
  699. typename = std::_RequireInputIter<_InputIterator>>
  700. #else
  701. template<class _InputIterator>
  702. #endif
  703. __versa_string&
  704. append(_InputIterator __first, _InputIterator __last)
  705. { return this->replace(_M_iend(), _M_iend(), __first, __last); }
  706. /**
  707. * @brief Append a single character.
  708. * @param __c Character to append.
  709. */
  710. void
  711. push_back(_CharT __c)
  712. {
  713. const size_type __size = this->size();
  714. if (__size + 1 > this->capacity() || this->_M_is_shared())
  715. this->_M_mutate(__size, size_type(0), 0, size_type(1));
  716. traits_type::assign(this->_M_data()[__size], __c);
  717. this->_M_set_length(__size + 1);
  718. }
  719. /**
  720. * @brief Set value to contents of another string.
  721. * @param __str Source string to use.
  722. * @return Reference to this string.
  723. */
  724. __versa_string&
  725. assign(const __versa_string& __str)
  726. {
  727. this->_M_assign(__str);
  728. return *this;
  729. }
  730. #if __cplusplus >= 201103L
  731. /**
  732. * @brief Set value to contents of another string.
  733. * @param __str Source string to use.
  734. * @return Reference to this string.
  735. *
  736. * This function sets this string to the exact contents of @a __str.
  737. * @a __str is a valid, but unspecified string.
  738. */
  739. __versa_string&
  740. assign(__versa_string&& __str) noexcept
  741. {
  742. this->swap(__str);
  743. return *this;
  744. }
  745. #endif // C++11
  746. /**
  747. * @brief Set value to a substring of a string.
  748. * @param __str The string to use.
  749. * @param __pos Index of the first character of str.
  750. * @param __n Number of characters to use.
  751. * @return Reference to this string.
  752. * @throw std::out_of_range if @a __pos is not a valid index.
  753. *
  754. * This function sets this string to the substring of @a __str
  755. * consisting of @a __n characters at @a __pos. If @a __n is
  756. * is larger than the number of available characters in @a
  757. * __str, the remainder of @a __str is used.
  758. */
  759. __versa_string&
  760. assign(const __versa_string& __str, size_type __pos, size_type __n)
  761. { return _M_replace(size_type(0), this->size(), __str._M_data()
  762. + __str._M_check(__pos, "__versa_string::assign"),
  763. __str._M_limit(__pos, __n)); }
  764. /**
  765. * @brief Set value to a C substring.
  766. * @param __s The C string to use.
  767. * @param __n Number of characters to use.
  768. * @return Reference to this string.
  769. *
  770. * This function sets the value of this string to the first @a
  771. * __n characters of @a __s. If @a __n is is larger than the
  772. * number of available characters in @a __s, the remainder of
  773. * @a __s is used.
  774. */
  775. __versa_string&
  776. assign(const _CharT* __s, size_type __n)
  777. {
  778. __glibcxx_requires_string_len(__s, __n);
  779. return _M_replace(size_type(0), this->size(), __s, __n);
  780. }
  781. /**
  782. * @brief Set value to contents of a C string.
  783. * @param __s The C string to use.
  784. * @return Reference to this string.
  785. *
  786. * This function sets the value of this string to the value of
  787. * @a __s. The data is copied, so there is no dependence on @a
  788. * __s once the function returns.
  789. */
  790. __versa_string&
  791. assign(const _CharT* __s)
  792. {
  793. __glibcxx_requires_string(__s);
  794. return _M_replace(size_type(0), this->size(), __s,
  795. traits_type::length(__s));
  796. }
  797. /**
  798. * @brief Set value to multiple characters.
  799. * @param __n Length of the resulting string.
  800. * @param __c The character to use.
  801. * @return Reference to this string.
  802. *
  803. * This function sets the value of this string to @a __n copies of
  804. * character @a __c.
  805. */
  806. __versa_string&
  807. assign(size_type __n, _CharT __c)
  808. { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
  809. /**
  810. * @brief Set value to a range of characters.
  811. * @param __first Iterator referencing the first character to append.
  812. * @param __last Iterator marking the end of the range.
  813. * @return Reference to this string.
  814. *
  815. * Sets value of string to characters in the range
  816. * [first,last).
  817. */
  818. #if __cplusplus >= 201103L
  819. template<class _InputIterator,
  820. typename = std::_RequireInputIter<_InputIterator>>
  821. #else
  822. template<class _InputIterator>
  823. #endif
  824. __versa_string&
  825. assign(_InputIterator __first, _InputIterator __last)
  826. { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
  827. #if __cplusplus >= 201103L
  828. /**
  829. * @brief Set value to an initializer_list of characters.
  830. * @param __l The initializer_list of characters to assign.
  831. * @return Reference to this string.
  832. */
  833. __versa_string&
  834. assign(std::initializer_list<_CharT> __l)
  835. { return this->assign(__l.begin(), __l.end()); }
  836. #endif // C++11
  837. #if __cplusplus >= 201103L
  838. /**
  839. * @brief Insert multiple characters.
  840. * @param __p Const_iterator referencing location in string to
  841. * insert at.
  842. * @param __n Number of characters to insert
  843. * @param __c The character to insert.
  844. * @return Iterator referencing the first inserted char.
  845. * @throw std::length_error If new length exceeds @c max_size().
  846. *
  847. * Inserts @a __n copies of character @a __c starting at the
  848. * position referenced by iterator @a __p. If adding
  849. * characters causes the length to exceed max_size(),
  850. * length_error is thrown. The value of the string doesn't
  851. * change if an error is thrown.
  852. */
  853. iterator
  854. insert(const_iterator __p, size_type __n, _CharT __c)
  855. {
  856. _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
  857. const size_type __pos = __p - _M_ibegin();
  858. this->replace(__p, __p, __n, __c);
  859. return iterator(this->_M_data() + __pos);
  860. }
  861. #else
  862. /**
  863. * @brief Insert multiple characters.
  864. * @param __p Iterator referencing location in string to insert at.
  865. * @param __n Number of characters to insert
  866. * @param __c The character to insert.
  867. * @throw std::length_error If new length exceeds @c max_size().
  868. *
  869. * Inserts @a __n copies of character @a __c starting at the
  870. * position referenced by iterator @a __p. If adding
  871. * characters causes the length to exceed max_size(),
  872. * length_error is thrown. The value of the string doesn't
  873. * change if an error is thrown.
  874. */
  875. void
  876. insert(iterator __p, size_type __n, _CharT __c)
  877. { this->replace(__p, __p, __n, __c); }
  878. #endif
  879. #if __cplusplus >= 201103L
  880. /**
  881. * @brief Insert a range of characters.
  882. * @param __p Const_iterator referencing location in string to
  883. * insert at.
  884. * @param __beg Start of range.
  885. * @param __end End of range.
  886. * @return Iterator referencing the first inserted char.
  887. * @throw std::length_error If new length exceeds @c max_size().
  888. *
  889. * Inserts characters in range [beg,end). If adding characters
  890. * causes the length to exceed max_size(), length_error is
  891. * thrown. The value of the string doesn't change if an error
  892. * is thrown.
  893. */
  894. template<class _InputIterator,
  895. typename = std::_RequireInputIter<_InputIterator>>
  896. iterator
  897. insert(const_iterator __p, _InputIterator __beg, _InputIterator __end)
  898. {
  899. _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
  900. const size_type __pos = __p - _M_ibegin();
  901. this->replace(__p, __p, __beg, __end);
  902. return iterator(this->_M_data() + __pos);
  903. }
  904. #else
  905. /**
  906. * @brief Insert a range of characters.
  907. * @param __p Iterator referencing location in string to insert at.
  908. * @param __beg Start of range.
  909. * @param __end End of range.
  910. * @throw std::length_error If new length exceeds @c max_size().
  911. *
  912. * Inserts characters in range [beg,end). If adding characters
  913. * causes the length to exceed max_size(), length_error is
  914. * thrown. The value of the string doesn't change if an error
  915. * is thrown.
  916. */
  917. template<class _InputIterator>
  918. void
  919. insert(iterator __p, _InputIterator __beg, _InputIterator __end)
  920. { this->replace(__p, __p, __beg, __end); }
  921. #endif
  922. #if __cplusplus >= 201103L
  923. /**
  924. * @brief Insert an initializer_list of characters.
  925. * @param __p Const_iterator referencing location in string to
  926. * insert at.
  927. * @param __l The initializer_list of characters to insert.
  928. * @return Iterator referencing the first inserted char.
  929. * @throw std::length_error If new length exceeds @c max_size().
  930. */
  931. iterator
  932. insert(const_iterator __p, std::initializer_list<_CharT> __l)
  933. { return this->insert(__p, __l.begin(), __l.end()); }
  934. #endif // C++11
  935. /**
  936. * @brief Insert value of a string.
  937. * @param __pos1 Iterator referencing location in string to insert at.
  938. * @param __str The string to insert.
  939. * @return Reference to this string.
  940. * @throw std::length_error If new length exceeds @c max_size().
  941. *
  942. * Inserts value of @a __str starting at @a __pos1. If adding
  943. * characters causes the length to exceed max_size(),
  944. * length_error is thrown. The value of the string doesn't
  945. * change if an error is thrown.
  946. */
  947. __versa_string&
  948. insert(size_type __pos1, const __versa_string& __str)
  949. { return this->replace(__pos1, size_type(0),
  950. __str._M_data(), __str.size()); }
  951. /**
  952. * @brief Insert a substring.
  953. * @param __pos1 Iterator referencing location in string to insert at.
  954. * @param __str The string to insert.
  955. * @param __pos2 Start of characters in str to insert.
  956. * @param __n Number of characters to insert.
  957. * @return Reference to this string.
  958. * @throw std::length_error If new length exceeds @c max_size().
  959. * @throw std::out_of_range If @a __pos1 > size() or
  960. * @a __pos2 > @a __str.size().
  961. *
  962. * Starting at @a __pos1, insert @a __n character of @a __str
  963. * beginning with @a __pos2. If adding characters causes the
  964. * length to exceed max_size(), length_error is thrown. If @a
  965. * __pos1 is beyond the end of this string or @a __pos2 is
  966. * beyond the end of @a __str, out_of_range is thrown. The
  967. * value of the string doesn't change if an error is thrown.
  968. */
  969. __versa_string&
  970. insert(size_type __pos1, const __versa_string& __str,
  971. size_type __pos2, size_type __n)
  972. { return this->replace(__pos1, size_type(0), __str._M_data()
  973. + __str._M_check(__pos2, "__versa_string::insert"),
  974. __str._M_limit(__pos2, __n)); }
  975. /**
  976. * @brief Insert a C substring.
  977. * @param __pos Iterator referencing location in string to insert at.
  978. * @param __s The C string to insert.
  979. * @param __n The number of characters to insert.
  980. * @return Reference to this string.
  981. * @throw std::length_error If new length exceeds @c max_size().
  982. * @throw std::out_of_range If @a __pos is beyond the end of this
  983. * string.
  984. *
  985. * Inserts the first @a __n characters of @a __s starting at @a
  986. * __pos. If adding characters causes the length to exceed
  987. * max_size(), length_error is thrown. If @a __pos is beyond
  988. * end(), out_of_range is thrown. The value of the string
  989. * doesn't change if an error is thrown.
  990. */
  991. __versa_string&
  992. insert(size_type __pos, const _CharT* __s, size_type __n)
  993. { return this->replace(__pos, size_type(0), __s, __n); }
  994. /**
  995. * @brief Insert a C string.
  996. * @param __pos Iterator referencing location in string to insert at.
  997. * @param __s The C string to insert.
  998. * @return Reference to this string.
  999. * @throw std::length_error If new length exceeds @c max_size().
  1000. * @throw std::out_of_range If @a __pos is beyond the end of this
  1001. * string.
  1002. *
  1003. * Inserts the first @a __n characters of @a __s starting at @a
  1004. * __pos. If adding characters causes the length to exceed
  1005. * max_size(), length_error is thrown. If @a __pos is beyond
  1006. * end(), out_of_range is thrown. The value of the string
  1007. * doesn't change if an error is thrown.
  1008. */
  1009. __versa_string&
  1010. insert(size_type __pos, const _CharT* __s)
  1011. {
  1012. __glibcxx_requires_string(__s);
  1013. return this->replace(__pos, size_type(0), __s,
  1014. traits_type::length(__s));
  1015. }
  1016. /**
  1017. * @brief Insert multiple characters.
  1018. * @param __pos Index in string to insert at.
  1019. * @param __n Number of characters to insert
  1020. * @param __c The character to insert.
  1021. * @return Reference to this string.
  1022. * @throw std::length_error If new length exceeds @c max_size().
  1023. * @throw std::out_of_range If @a __pos is beyond the end of this
  1024. * string.
  1025. *
  1026. * Inserts @a __n copies of character @a __c starting at index
  1027. * @a __pos. If adding characters causes the length to exceed
  1028. * max_size(), length_error is thrown. If @a __pos > length(),
  1029. * out_of_range is thrown. The value of the string doesn't
  1030. * change if an error is thrown.
  1031. */
  1032. __versa_string&
  1033. insert(size_type __pos, size_type __n, _CharT __c)
  1034. { return _M_replace_aux(_M_check(__pos, "__versa_string::insert"),
  1035. size_type(0), __n, __c); }
  1036. /**
  1037. * @brief Insert one character.
  1038. * @param __p Iterator referencing position in string to insert at.
  1039. * @param __c The character to insert.
  1040. * @return Iterator referencing newly inserted char.
  1041. * @throw std::length_error If new length exceeds @c max_size().
  1042. *
  1043. * Inserts character @a __c at position referenced by @a __p.
  1044. * If adding character causes the length to exceed max_size(),
  1045. * length_error is thrown. If @a __p is beyond end of string,
  1046. * out_of_range is thrown. The value of the string doesn't
  1047. * change if an error is thrown.
  1048. */
  1049. iterator
  1050. #if __cplusplus >= 201103L
  1051. insert(const_iterator __p, _CharT __c)
  1052. #else
  1053. insert(iterator __p, _CharT __c)
  1054. #endif
  1055. {
  1056. _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
  1057. const size_type __pos = __p - _M_ibegin();
  1058. _M_replace_aux(__pos, size_type(0), size_type(1), __c);
  1059. this->_M_set_leaked();
  1060. return iterator(this->_M_data() + __pos);
  1061. }
  1062. /**
  1063. * @brief Remove characters.
  1064. * @param __pos Index of first character to remove (default 0).
  1065. * @param __n Number of characters to remove (default remainder).
  1066. * @return Reference to this string.
  1067. * @throw std::out_of_range If @a __pos is beyond the end of this
  1068. * string.
  1069. *
  1070. * Removes @a __n characters from this string starting at @a
  1071. * __pos. The length of the string is reduced by @a __n. If
  1072. * there are < @a __n characters to remove, the remainder of
  1073. * the string is truncated. If @a __p is beyond end of string,
  1074. * out_of_range is thrown. The value of the string doesn't
  1075. * change if an error is thrown.
  1076. */
  1077. __versa_string&
  1078. erase(size_type __pos = 0, size_type __n = npos)
  1079. {
  1080. this->_M_erase(_M_check(__pos, "__versa_string::erase"),
  1081. _M_limit(__pos, __n));
  1082. return *this;
  1083. }
  1084. /**
  1085. * @brief Remove one character.
  1086. * @param __position Iterator referencing the character to remove.
  1087. * @return iterator referencing same location after removal.
  1088. *
  1089. * Removes the character at @a __position from this string. The
  1090. * value of the string doesn't change if an error is thrown.
  1091. */
  1092. iterator
  1093. #if __cplusplus >= 201103L
  1094. erase(const_iterator __position)
  1095. #else
  1096. erase(iterator __position)
  1097. #endif
  1098. {
  1099. _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
  1100. && __position < _M_iend());
  1101. const size_type __pos = __position - _M_ibegin();
  1102. this->_M_erase(__pos, size_type(1));
  1103. this->_M_set_leaked();
  1104. return iterator(this->_M_data() + __pos);
  1105. }
  1106. /**
  1107. * @brief Remove a range of characters.
  1108. * @param __first Iterator referencing the first character to remove.
  1109. * @param __last Iterator referencing the end of the range.
  1110. * @return Iterator referencing location of first after removal.
  1111. *
  1112. * Removes the characters in the range [first,last) from this
  1113. * string. The value of the string doesn't change if an error
  1114. * is thrown.
  1115. */
  1116. iterator
  1117. #if __cplusplus >= 201103L
  1118. erase(const_iterator __first, const_iterator __last)
  1119. #else
  1120. erase(iterator __first, iterator __last)
  1121. #endif
  1122. {
  1123. _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
  1124. && __last <= _M_iend());
  1125. const size_type __pos = __first - _M_ibegin();
  1126. this->_M_erase(__pos, __last - __first);
  1127. this->_M_set_leaked();
  1128. return iterator(this->_M_data() + __pos);
  1129. }
  1130. #if __cplusplus >= 201103L
  1131. /**
  1132. * @brief Remove the last character.
  1133. *
  1134. * The string must be non-empty.
  1135. */
  1136. void
  1137. pop_back()
  1138. { this->_M_erase(size()-1, 1); }
  1139. #endif // C++11
  1140. /**
  1141. * @brief Replace characters with value from another string.
  1142. * @param __pos Index of first character to replace.
  1143. * @param __n Number of characters to be replaced.
  1144. * @param __str String to insert.
  1145. * @return Reference to this string.
  1146. * @throw std::out_of_range If @a __pos is beyond the end of this
  1147. * string.
  1148. * @throw std::length_error If new length exceeds @c max_size().
  1149. *
  1150. * Removes the characters in the range [pos,pos+n) from this
  1151. * string. In place, the value of @a __str is inserted. If @a
  1152. * __pos is beyond end of string, out_of_range is thrown. If
  1153. * the length of the result exceeds max_size(), length_error is
  1154. * thrown. The value of the string doesn't change if an error
  1155. * is thrown.
  1156. */
  1157. __versa_string&
  1158. replace(size_type __pos, size_type __n, const __versa_string& __str)
  1159. { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
  1160. /**
  1161. * @brief Replace characters with value from another string.
  1162. * @param __pos1 Index of first character to replace.
  1163. * @param __n1 Number of characters to be replaced.
  1164. * @param __str String to insert.
  1165. * @param __pos2 Index of first character of str to use.
  1166. * @param __n2 Number of characters from str to use.
  1167. * @return Reference to this string.
  1168. * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
  1169. * str.size().
  1170. * @throw std::length_error If new length exceeds @c max_size().
  1171. *
  1172. * Removes the characters in the range [pos1,pos1 + n) from
  1173. * this string. In place, the value of @a __str is inserted.
  1174. * If @a __pos is beyond end of string, out_of_range is thrown.
  1175. * If the length of the result exceeds max_size(), length_error
  1176. * is thrown. The value of the string doesn't change if an
  1177. * error is thrown.
  1178. */
  1179. __versa_string&
  1180. replace(size_type __pos1, size_type __n1, const __versa_string& __str,
  1181. size_type __pos2, size_type __n2)
  1182. {
  1183. return this->replace(__pos1, __n1, __str._M_data()
  1184. + __str._M_check(__pos2,
  1185. "__versa_string::replace"),
  1186. __str._M_limit(__pos2, __n2));
  1187. }
  1188. /**
  1189. * @brief Replace characters with value of a C substring.
  1190. * @param __pos Index of first character to replace.
  1191. * @param __n1 Number of characters to be replaced.
  1192. * @param __s C string to insert.
  1193. * @param __n2 Number of characters from @a __s to use.
  1194. * @return Reference to this string.
  1195. * @throw std::out_of_range If @a __pos1 > size().
  1196. * @throw std::length_error If new length exceeds @c max_size().
  1197. *
  1198. * Removes the characters in the range [pos,pos + n1) from this
  1199. * string. In place, the first @a __n2 characters of @a __s
  1200. * are inserted, or all of @a __s if @a __n2 is too large. If
  1201. * @a __pos is beyond end of string, out_of_range is thrown.
  1202. * If the length of result exceeds max_size(), length_error is
  1203. * thrown. The value of the string doesn't change if an error
  1204. * is thrown.
  1205. */
  1206. __versa_string&
  1207. replace(size_type __pos, size_type __n1, const _CharT* __s,
  1208. size_type __n2)
  1209. {
  1210. __glibcxx_requires_string_len(__s, __n2);
  1211. return _M_replace(_M_check(__pos, "__versa_string::replace"),
  1212. _M_limit(__pos, __n1), __s, __n2);
  1213. }
  1214. /**
  1215. * @brief Replace characters with value of a C string.
  1216. * @param __pos Index of first character to replace.
  1217. * @param __n1 Number of characters to be replaced.
  1218. * @param __s C string to insert.
  1219. * @return Reference to this string.
  1220. * @throw std::out_of_range If @a __pos > size().
  1221. * @throw std::length_error If new length exceeds @c max_size().
  1222. *
  1223. * Removes the characters in the range [pos,pos + n1) from this
  1224. * string. In place, the characters of @a __s are inserted. If
  1225. * @a pos is beyond end of string, out_of_range is thrown. If
  1226. * the length of result exceeds max_size(), length_error is thrown.
  1227. * The value of the string doesn't change if an error is thrown.
  1228. */
  1229. __versa_string&
  1230. replace(size_type __pos, size_type __n1, const _CharT* __s)
  1231. {
  1232. __glibcxx_requires_string(__s);
  1233. return this->replace(__pos, __n1, __s, traits_type::length(__s));
  1234. }
  1235. /**
  1236. * @brief Replace characters with multiple characters.
  1237. * @param __pos Index of first character to replace.
  1238. * @param __n1 Number of characters to be replaced.
  1239. * @param __n2 Number of characters to insert.
  1240. * @param __c Character to insert.
  1241. * @return Reference to this string.
  1242. * @throw std::out_of_range If @a __pos > size().
  1243. * @throw std::length_error If new length exceeds @c max_size().
  1244. *
  1245. * Removes the characters in the range [pos,pos + n1) from this
  1246. * string. In place, @a __n2 copies of @a __c are inserted.
  1247. * If @a __pos is beyond end of string, out_of_range is thrown.
  1248. * If the length of result exceeds max_size(), length_error is
  1249. * thrown. The value of the string doesn't change if an error
  1250. * is thrown.
  1251. */
  1252. __versa_string&
  1253. replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
  1254. { return _M_replace_aux(_M_check(__pos, "__versa_string::replace"),
  1255. _M_limit(__pos, __n1), __n2, __c); }
  1256. /**
  1257. * @brief Replace range of characters with string.
  1258. * @param __i1 Iterator referencing start of range to replace.
  1259. * @param __i2 Iterator referencing end of range to replace.
  1260. * @param __str String value to insert.
  1261. * @return Reference to this string.
  1262. * @throw std::length_error If new length exceeds @c max_size().
  1263. *
  1264. * Removes the characters in the range [i1,i2). In place, the
  1265. * value of @a __str is inserted. If the length of result
  1266. * exceeds max_size(), length_error is thrown. The value of
  1267. * the string doesn't change if an error is thrown.
  1268. */
  1269. __versa_string&
  1270. #if __cplusplus >= 201103L
  1271. replace(const_iterator __i1, const_iterator __i2,
  1272. const __versa_string& __str)
  1273. #else
  1274. replace(iterator __i1, iterator __i2, const __versa_string& __str)
  1275. #endif
  1276. { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
  1277. /**
  1278. * @brief Replace range of characters with C substring.
  1279. * @param __i1 Iterator referencing start of range to replace.
  1280. * @param __i2 Iterator referencing end of range to replace.
  1281. * @param __s C string value to insert.
  1282. * @param __n Number of characters from s to insert.
  1283. * @return Reference to this string.
  1284. * @throw std::length_error If new length exceeds @c max_size().
  1285. *
  1286. * Removes the characters in the range [i1,i2). In place, the
  1287. * first @a n characters of @a __s are inserted. If the length
  1288. * of result exceeds max_size(), length_error is thrown. The
  1289. * value of the string doesn't change if an error is thrown.
  1290. */
  1291. __versa_string&
  1292. #if __cplusplus >= 201103L
  1293. replace(const_iterator __i1, const_iterator __i2,
  1294. const _CharT* __s, size_type __n)
  1295. #else
  1296. replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
  1297. #endif
  1298. {
  1299. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1300. && __i2 <= _M_iend());
  1301. return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
  1302. }
  1303. /**
  1304. * @brief Replace range of characters with C string.
  1305. * @param __i1 Iterator referencing start of range to replace.
  1306. * @param __i2 Iterator referencing end of range to replace.
  1307. * @param __s C string value to insert.
  1308. * @return Reference to this string.
  1309. * @throw std::length_error If new length exceeds @c max_size().
  1310. *
  1311. * Removes the characters in the range [i1,i2). In place, the
  1312. * characters of @a __s are inserted. If the length of result
  1313. * exceeds max_size(), length_error is thrown. The value of
  1314. * the string doesn't change if an error is thrown.
  1315. */
  1316. __versa_string&
  1317. #if __cplusplus >= 201103L
  1318. replace(const_iterator __i1, const_iterator __i2, const _CharT* __s)
  1319. #else
  1320. replace(iterator __i1, iterator __i2, const _CharT* __s)
  1321. #endif
  1322. {
  1323. __glibcxx_requires_string(__s);
  1324. return this->replace(__i1, __i2, __s, traits_type::length(__s));
  1325. }
  1326. /**
  1327. * @brief Replace range of characters with multiple characters
  1328. * @param __i1 Iterator referencing start of range to replace.
  1329. * @param __i2 Iterator referencing end of range to replace.
  1330. * @param __n Number of characters to insert.
  1331. * @param __c Character to insert.
  1332. * @return Reference to this string.
  1333. * @throw std::length_error If new length exceeds @c max_size().
  1334. *
  1335. * Removes the characters in the range [i1,i2). In place, @a
  1336. * __n copies of @a __c are inserted. If the length of result
  1337. * exceeds max_size(), length_error is thrown. The value of
  1338. * the string doesn't change if an error is thrown.
  1339. */
  1340. __versa_string&
  1341. #if __cplusplus >= 201103L
  1342. replace(const_iterator __i1, const_iterator __i2, size_type __n,
  1343. _CharT __c)
  1344. #else
  1345. replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
  1346. #endif
  1347. {
  1348. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1349. && __i2 <= _M_iend());
  1350. return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
  1351. }
  1352. /**
  1353. * @brief Replace range of characters with range.
  1354. * @param __i1 Iterator referencing start of range to replace.
  1355. * @param __i2 Iterator referencing end of range to replace.
  1356. * @param __k1 Iterator referencing start of range to insert.
  1357. * @param __k2 Iterator referencing end of range to insert.
  1358. * @return Reference to this string.
  1359. * @throw std::length_error If new length exceeds @c max_size().
  1360. *
  1361. * Removes the characters in the range [i1,i2). In place,
  1362. * characters in the range [k1,k2) are inserted. If the length
  1363. * of result exceeds max_size(), length_error is thrown. The
  1364. * value of the string doesn't change if an error is thrown.
  1365. */
  1366. #if __cplusplus >= 201103L
  1367. template<class _InputIterator,
  1368. typename = std::_RequireInputIter<_InputIterator>>
  1369. __versa_string&
  1370. replace(const_iterator __i1, const_iterator __i2,
  1371. _InputIterator __k1, _InputIterator __k2)
  1372. {
  1373. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1374. && __i2 <= _M_iend());
  1375. __glibcxx_requires_valid_range(__k1, __k2);
  1376. return this->_M_replace_dispatch(__i1, __i2, __k1, __k2,
  1377. std::__false_type());
  1378. }
  1379. #else
  1380. template<class _InputIterator>
  1381. __versa_string&
  1382. replace(iterator __i1, iterator __i2,
  1383. _InputIterator __k1, _InputIterator __k2)
  1384. {
  1385. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1386. && __i2 <= _M_iend());
  1387. __glibcxx_requires_valid_range(__k1, __k2);
  1388. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  1389. return this->_M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
  1390. }
  1391. #endif
  1392. // Specializations for the common case of pointer and iterator:
  1393. // useful to avoid the overhead of temporary buffering in _M_replace.
  1394. __versa_string&
  1395. #if __cplusplus >= 201103L
  1396. replace(const_iterator __i1, const_iterator __i2,
  1397. _CharT* __k1, _CharT* __k2)
  1398. #else
  1399. replace(iterator __i1, iterator __i2,
  1400. _CharT* __k1, _CharT* __k2)
  1401. #endif
  1402. {
  1403. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1404. && __i2 <= _M_iend());
  1405. __glibcxx_requires_valid_range(__k1, __k2);
  1406. return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
  1407. __k1, __k2 - __k1);
  1408. }
  1409. __versa_string&
  1410. #if __cplusplus >= 201103L
  1411. replace(const_iterator __i1, const_iterator __i2,
  1412. const _CharT* __k1, const _CharT* __k2)
  1413. #else
  1414. replace(iterator __i1, iterator __i2,
  1415. const _CharT* __k1, const _CharT* __k2)
  1416. #endif
  1417. {
  1418. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1419. && __i2 <= _M_iend());
  1420. __glibcxx_requires_valid_range(__k1, __k2);
  1421. return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
  1422. __k1, __k2 - __k1);
  1423. }
  1424. __versa_string&
  1425. #if __cplusplus >= 201103L
  1426. replace(const_iterator __i1, const_iterator __i2,
  1427. iterator __k1, iterator __k2)
  1428. #else
  1429. replace(iterator __i1, iterator __i2,
  1430. iterator __k1, iterator __k2)
  1431. #endif
  1432. {
  1433. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1434. && __i2 <= _M_iend());
  1435. __glibcxx_requires_valid_range(__k1, __k2);
  1436. return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
  1437. __k1.base(), __k2 - __k1);
  1438. }
  1439. __versa_string&
  1440. #if __cplusplus >= 201103L
  1441. replace(const_iterator __i1, const_iterator __i2,
  1442. const_iterator __k1, const_iterator __k2)
  1443. #else
  1444. replace(iterator __i1, iterator __i2,
  1445. const_iterator __k1, const_iterator __k2)
  1446. #endif
  1447. {
  1448. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1449. && __i2 <= _M_iend());
  1450. __glibcxx_requires_valid_range(__k1, __k2);
  1451. return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
  1452. __k1.base(), __k2 - __k1);
  1453. }
  1454. #if __cplusplus >= 201103L
  1455. /**
  1456. * @brief Replace range of characters with initializer_list.
  1457. * @param __i1 Iterator referencing start of range to replace.
  1458. * @param __i2 Iterator referencing end of range to replace.
  1459. * @param __l The initializer_list of characters to insert.
  1460. * @return Reference to this string.
  1461. * @throw std::length_error If new length exceeds @c max_size().
  1462. *
  1463. * Removes the characters in the range [i1,i2). In place,
  1464. * characters in the range [k1,k2) are inserted. If the length
  1465. * of result exceeds max_size(), length_error is thrown. The
  1466. * value of the string doesn't change if an error is thrown.
  1467. */
  1468. __versa_string&
  1469. replace(const_iterator __i1, const_iterator __i2,
  1470. std::initializer_list<_CharT> __l)
  1471. { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
  1472. #endif // C++11
  1473. private:
  1474. template<class _Integer>
  1475. __versa_string&
  1476. _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
  1477. _Integer __n, _Integer __val, std::__true_type)
  1478. { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
  1479. template<class _InputIterator>
  1480. __versa_string&
  1481. _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
  1482. _InputIterator __k1, _InputIterator __k2,
  1483. std::__false_type);
  1484. __versa_string&
  1485. _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
  1486. _CharT __c);
  1487. __versa_string&
  1488. _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
  1489. const size_type __len2);
  1490. __versa_string&
  1491. _M_append(const _CharT* __s, size_type __n);
  1492. public:
  1493. /**
  1494. * @brief Copy substring into C string.
  1495. * @param __s C string to copy value into.
  1496. * @param __n Number of characters to copy.
  1497. * @param __pos Index of first character to copy.
  1498. * @return Number of characters actually copied
  1499. * @throw std::out_of_range If pos > size().
  1500. *
  1501. * Copies up to @a __n characters starting at @a __pos into the
  1502. * C string @a s. If @a __pos is greater than size(),
  1503. * out_of_range is thrown.
  1504. */
  1505. size_type
  1506. copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
  1507. /**
  1508. * @brief Swap contents with another string.
  1509. * @param __s String to swap with.
  1510. *
  1511. * Exchanges the contents of this string with that of @a __s in
  1512. * constant time.
  1513. */
  1514. void
  1515. swap(__versa_string& __s) _GLIBCXX_NOEXCEPT
  1516. { this->_M_swap(__s); }
  1517. // String operations:
  1518. /**
  1519. * @brief Return const pointer to null-terminated contents.
  1520. *
  1521. * This is a handle to internal data. Do not modify or dire things may
  1522. * happen.
  1523. */
  1524. const _CharT*
  1525. c_str() const _GLIBCXX_NOEXCEPT
  1526. { return this->_M_data(); }
  1527. /**
  1528. * @brief Return const pointer to contents.
  1529. *
  1530. * This is a handle to internal data. Do not modify or dire things may
  1531. * happen.
  1532. */
  1533. const _CharT*
  1534. data() const _GLIBCXX_NOEXCEPT
  1535. { return this->_M_data(); }
  1536. /**
  1537. * @brief Return copy of allocator used to construct this string.
  1538. */
  1539. allocator_type
  1540. get_allocator() const _GLIBCXX_NOEXCEPT
  1541. { return allocator_type(this->_M_get_allocator()); }
  1542. /**
  1543. * @brief Find position of a C substring.
  1544. * @param __s C string to locate.
  1545. * @param __pos Index of character to search from.
  1546. * @param __n Number of characters from @a __s to search for.
  1547. * @return Index of start of first occurrence.
  1548. *
  1549. * Starting from @a __pos, searches forward for the first @a
  1550. * __n characters in @a __s within this string. If found,
  1551. * returns the index where it begins. If not found, returns
  1552. * npos.
  1553. */
  1554. size_type
  1555. find(const _CharT* __s, size_type __pos, size_type __n) const;
  1556. /**
  1557. * @brief Find position of a string.
  1558. * @param __str String to locate.
  1559. * @param __pos Index of character to search from (default 0).
  1560. * @return Index of start of first occurrence.
  1561. *
  1562. * Starting from @a __pos, searches forward for value of @a
  1563. * __str within this string. If found, returns the index where
  1564. * it begins. If not found, returns npos.
  1565. */
  1566. size_type
  1567. find(const __versa_string& __str, size_type __pos = 0) const
  1568. _GLIBCXX_NOEXCEPT
  1569. { return this->find(__str.data(), __pos, __str.size()); }
  1570. /**
  1571. * @brief Find position of a C string.
  1572. * @param __s C string to locate.
  1573. * @param __pos Index of character to search from (default 0).
  1574. * @return Index of start of first occurrence.
  1575. *
  1576. * Starting from @a __pos, searches forward for the value of @a
  1577. * __s within this string. If found, returns the index where
  1578. * it begins. If not found, returns npos.
  1579. */
  1580. size_type
  1581. find(const _CharT* __s, size_type __pos = 0) const
  1582. {
  1583. __glibcxx_requires_string(__s);
  1584. return this->find(__s, __pos, traits_type::length(__s));
  1585. }
  1586. /**
  1587. * @brief Find position of a character.
  1588. * @param __c Character to locate.
  1589. * @param __pos Index of character to search from (default 0).
  1590. * @return Index of first occurrence.
  1591. *
  1592. * Starting from @a __pos, searches forward for @a __c within
  1593. * this string. If found, returns the index where it was
  1594. * found. If not found, returns npos.
  1595. */
  1596. size_type
  1597. find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
  1598. /**
  1599. * @brief Find last position of a string.
  1600. * @param __str String to locate.
  1601. * @param __pos Index of character to search back from (default end).
  1602. * @return Index of start of last occurrence.
  1603. *
  1604. * Starting from @a __pos, searches backward for value of @a
  1605. * __str within this string. If found, returns the index where
  1606. * it begins. If not found, returns npos.
  1607. */
  1608. size_type
  1609. rfind(const __versa_string& __str, size_type __pos = npos) const
  1610. _GLIBCXX_NOEXCEPT
  1611. { return this->rfind(__str.data(), __pos, __str.size()); }
  1612. /**
  1613. * @brief Find last position of a C substring.
  1614. * @param __s C string to locate.
  1615. * @param __pos Index of character to search back from.
  1616. * @param __n Number of characters from s to search for.
  1617. * @return Index of start of last occurrence.
  1618. *
  1619. * Starting from @a __pos, searches backward for the first @a
  1620. * __n characters in @a __s within this string. If found,
  1621. * returns the index where it begins. If not found, returns
  1622. * npos.
  1623. */
  1624. size_type
  1625. rfind(const _CharT* __s, size_type __pos, size_type __n) const;
  1626. /**
  1627. * @brief Find last position of a C string.
  1628. * @param __s C string to locate.
  1629. * @param __pos Index of character to start search at (default end).
  1630. * @return Index of start of last occurrence.
  1631. *
  1632. * Starting from @a __pos, searches backward for the value of
  1633. * @a __s within this string. If found, returns the index
  1634. * where it begins. If not found, returns npos.
  1635. */
  1636. size_type
  1637. rfind(const _CharT* __s, size_type __pos = npos) const
  1638. {
  1639. __glibcxx_requires_string(__s);
  1640. return this->rfind(__s, __pos, traits_type::length(__s));
  1641. }
  1642. /**
  1643. * @brief Find last position of a character.
  1644. * @param __c Character to locate.
  1645. * @param __pos Index of character to search back from (default end).
  1646. * @return Index of last occurrence.
  1647. *
  1648. * Starting from @a __pos, searches backward for @a __c within
  1649. * this string. If found, returns the index where it was
  1650. * found. If not found, returns npos.
  1651. */
  1652. size_type
  1653. rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
  1654. /**
  1655. * @brief Find position of a character of string.
  1656. * @param __str String containing characters to locate.
  1657. * @param __pos Index of character to search from (default 0).
  1658. * @return Index of first occurrence.
  1659. *
  1660. * Starting from @a __pos, searches forward for one of the characters of
  1661. * @a __str within this string. If found, returns the index where it was
  1662. * found. If not found, returns npos.
  1663. */
  1664. size_type
  1665. find_first_of(const __versa_string& __str, size_type __pos = 0) const
  1666. _GLIBCXX_NOEXCEPT
  1667. { return this->find_first_of(__str.data(), __pos, __str.size()); }
  1668. /**
  1669. * @brief Find position of a character of C substring.
  1670. * @param __s String containing characters to locate.
  1671. * @param __pos Index of character to search from.
  1672. * @param __n Number of characters from s to search for.
  1673. * @return Index of first occurrence.
  1674. *
  1675. * Starting from @a __pos, searches forward for one of the
  1676. * first @a __n characters of @a __s within this string. If
  1677. * found, returns the index where it was found. If not found,
  1678. * returns npos.
  1679. */
  1680. size_type
  1681. find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
  1682. /**
  1683. * @brief Find position of a character of C string.
  1684. * @param __s String containing characters to locate.
  1685. * @param __pos Index of character to search from (default 0).
  1686. * @return Index of first occurrence.
  1687. *
  1688. * Starting from @a __pos, searches forward for one of the
  1689. * characters of @a __s within this string. If found, returns
  1690. * the index where it was found. If not found, returns npos.
  1691. */
  1692. size_type
  1693. find_first_of(const _CharT* __s, size_type __pos = 0) const
  1694. {
  1695. __glibcxx_requires_string(__s);
  1696. return this->find_first_of(__s, __pos, traits_type::length(__s));
  1697. }
  1698. /**
  1699. * @brief Find position of a character.
  1700. * @param __c Character to locate.
  1701. * @param __pos Index of character to search from (default 0).
  1702. * @return Index of first occurrence.
  1703. *
  1704. * Starting from @a __pos, searches forward for the character
  1705. * @a __c within this string. If found, returns the index
  1706. * where it was found. If not found, returns npos.
  1707. *
  1708. * Note: equivalent to find(c, pos).
  1709. */
  1710. size_type
  1711. find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
  1712. { return this->find(__c, __pos); }
  1713. /**
  1714. * @brief Find last position of a character of string.
  1715. * @param __str String containing characters to locate.
  1716. * @param __pos Index of character to search back from (default end).
  1717. * @return Index of last occurrence.
  1718. *
  1719. * Starting from @a __pos, searches backward for one of the
  1720. * characters of @a __str within this string. If found,
  1721. * returns the index where it was found. If not found, returns
  1722. * npos.
  1723. */
  1724. size_type
  1725. find_last_of(const __versa_string& __str, size_type __pos = npos) const
  1726. _GLIBCXX_NOEXCEPT
  1727. { return this->find_last_of(__str.data(), __pos, __str.size()); }
  1728. /**
  1729. * @brief Find last position of a character of C substring.
  1730. * @param __s C string containing characters to locate.
  1731. * @param __pos Index of character to search back from.
  1732. * @param __n Number of characters from s to search for.
  1733. * @return Index of last occurrence.
  1734. *
  1735. * Starting from @a __pos, searches backward for one of the
  1736. * first @a __n characters of @a __s within this string. If
  1737. * found, returns the index where it was found. If not found,
  1738. * returns npos.
  1739. */
  1740. size_type
  1741. find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
  1742. /**
  1743. * @brief Find last position of a character of C string.
  1744. * @param __s C string containing characters to locate.
  1745. * @param __pos Index of character to search back from (default end).
  1746. * @return Index of last occurrence.
  1747. *
  1748. * Starting from @a __pos, searches backward for one of the
  1749. * characters of @a __s within this string. If found, returns
  1750. * the index where it was found. If not found, returns npos.
  1751. */
  1752. size_type
  1753. find_last_of(const _CharT* __s, size_type __pos = npos) const
  1754. {
  1755. __glibcxx_requires_string(__s);
  1756. return this->find_last_of(__s, __pos, traits_type::length(__s));
  1757. }
  1758. /**
  1759. * @brief Find last position of a character.
  1760. * @param __c Character to locate.
  1761. * @param __pos Index of character to search back from (default end).
  1762. * @return Index of last occurrence.
  1763. *
  1764. * Starting from @a __pos, searches backward for @a __c within
  1765. * this string. If found, returns the index where it was
  1766. * found. If not found, returns npos.
  1767. *
  1768. * Note: equivalent to rfind(c, pos).
  1769. */
  1770. size_type
  1771. find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
  1772. { return this->rfind(__c, __pos); }
  1773. /**
  1774. * @brief Find position of a character not in string.
  1775. * @param __str String containing characters to avoid.
  1776. * @param __pos Index of character to search from (default 0).
  1777. * @return Index of first occurrence.
  1778. *
  1779. * Starting from @a __pos, searches forward for a character not
  1780. * contained in @a __str within this string. If found, returns
  1781. * the index where it was found. If not found, returns npos.
  1782. */
  1783. size_type
  1784. find_first_not_of(const __versa_string& __str, size_type __pos = 0) const
  1785. _GLIBCXX_NOEXCEPT
  1786. { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
  1787. /**
  1788. * @brief Find position of a character not in C substring.
  1789. * @param __s C string containing characters to avoid.
  1790. * @param __pos Index of character to search from.
  1791. * @param __n Number of characters from s to consider.
  1792. * @return Index of first occurrence.
  1793. *
  1794. * Starting from @a __pos, searches forward for a character not
  1795. * contained in the first @a __n characters of @a __s within
  1796. * this string. If found, returns the index where it was
  1797. * found. If not found, returns npos.
  1798. */
  1799. size_type
  1800. find_first_not_of(const _CharT* __s, size_type __pos,
  1801. size_type __n) const;
  1802. /**
  1803. * @brief Find position of a character not in C string.
  1804. * @param __s C string containing characters to avoid.
  1805. * @param __pos Index of character to search from (default 0).
  1806. * @return Index of first occurrence.
  1807. *
  1808. * Starting from @a __pos, searches forward for a character not
  1809. * contained in @a __s within this string. If found, returns
  1810. * the index where it was found. If not found, returns npos.
  1811. */
  1812. size_type
  1813. find_first_not_of(const _CharT* __s, size_type __pos = 0) const
  1814. {
  1815. __glibcxx_requires_string(__s);
  1816. return this->find_first_not_of(__s, __pos, traits_type::length(__s));
  1817. }
  1818. /**
  1819. * @brief Find position of a different character.
  1820. * @param __c Character to avoid.
  1821. * @param __pos Index of character to search from (default 0).
  1822. * @return Index of first occurrence.
  1823. *
  1824. * Starting from @a __pos, searches forward for a character
  1825. * other than @a __c within this string. If found, returns the
  1826. * index where it was found. If not found, returns npos.
  1827. */
  1828. size_type
  1829. find_first_not_of(_CharT __c, size_type __pos = 0) const
  1830. _GLIBCXX_NOEXCEPT;
  1831. /**
  1832. * @brief Find last position of a character not in string.
  1833. * @param __str String containing characters to avoid.
  1834. * @param __pos Index of character to search back from (default end).
  1835. * @return Index of last occurrence.
  1836. *
  1837. * Starting from @a __pos, searches backward for a character
  1838. * not contained in @a __str within this string. If found,
  1839. * returns the index where it was found. If not found, returns
  1840. * npos.
  1841. */
  1842. size_type
  1843. find_last_not_of(const __versa_string& __str,
  1844. size_type __pos = npos) const _GLIBCXX_NOEXCEPT
  1845. { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
  1846. /**
  1847. * @brief Find last position of a character not in C substring.
  1848. * @param __s C string containing characters to avoid.
  1849. * @param __pos Index of character to search back from.
  1850. * @param __n Number of characters from s to consider.
  1851. * @return Index of last occurrence.
  1852. *
  1853. * Starting from @a __pos, searches backward for a character
  1854. * not contained in the first @a __n characters of @a __s
  1855. * within this string. If found, returns the index where it
  1856. * was found. If not found, returns npos.
  1857. */
  1858. size_type
  1859. find_last_not_of(const _CharT* __s, size_type __pos,
  1860. size_type __n) const;
  1861. /**
  1862. * @brief Find last position of a character not in C string.
  1863. * @param __s C string containing characters to avoid.
  1864. * @param __pos Index of character to search back from (default end).
  1865. * @return Index of last occurrence.
  1866. *
  1867. * Starting from @a __pos, searches backward for a character
  1868. * not contained in @a __s within this string. If found,
  1869. * returns the index where it was found. If not found, returns
  1870. * npos.
  1871. */
  1872. size_type
  1873. find_last_not_of(const _CharT* __s, size_type __pos = npos) const
  1874. {
  1875. __glibcxx_requires_string(__s);
  1876. return this->find_last_not_of(__s, __pos, traits_type::length(__s));
  1877. }
  1878. /**
  1879. * @brief Find last position of a different character.
  1880. * @param __c Character to avoid.
  1881. * @param __pos Index of character to search back from (default end).
  1882. * @return Index of last occurrence.
  1883. *
  1884. * Starting from @a __pos, searches backward for a character
  1885. * other than @a __c within this string. If found, returns the
  1886. * index where it was found. If not found, returns npos.
  1887. */
  1888. size_type
  1889. find_last_not_of(_CharT __c, size_type __pos = npos) const
  1890. _GLIBCXX_NOEXCEPT;
  1891. /**
  1892. * @brief Get a substring.
  1893. * @param __pos Index of first character (default 0).
  1894. * @param __n Number of characters in substring (default remainder).
  1895. * @return The new string.
  1896. * @throw std::out_of_range If pos > size().
  1897. *
  1898. * Construct and return a new string using the @a __n
  1899. * characters starting at @a __pos. If the string is too
  1900. * short, use the remainder of the characters. If @a __pos is
  1901. * beyond the end of the string, out_of_range is thrown.
  1902. */
  1903. __versa_string
  1904. substr(size_type __pos = 0, size_type __n = npos) const
  1905. {
  1906. return __versa_string(*this, _M_check(__pos, "__versa_string::substr"),
  1907. __n);
  1908. }
  1909. /**
  1910. * @brief Compare to a string.
  1911. * @param __str String to compare against.
  1912. * @return Integer < 0, 0, or > 0.
  1913. *
  1914. * Returns an integer < 0 if this string is ordered before @a
  1915. * __str, 0 if their values are equivalent, or > 0 if this
  1916. * string is ordered after @a __str. Determines the effective
  1917. * length rlen of the strings to compare as the smallest of
  1918. * size() and str.size(). The function then compares the two
  1919. * strings by calling traits::compare(data(), str.data(),rlen).
  1920. * If the result of the comparison is nonzero returns it,
  1921. * otherwise the shorter one is ordered first.
  1922. */
  1923. int
  1924. compare(const __versa_string& __str) const
  1925. {
  1926. if (this->_M_compare(__str))
  1927. return 0;
  1928. const size_type __size = this->size();
  1929. const size_type __osize = __str.size();
  1930. const size_type __len = std::min(__size, __osize);
  1931. int __r = traits_type::compare(this->_M_data(), __str.data(), __len);
  1932. if (!__r)
  1933. __r = this->_S_compare(__size, __osize);
  1934. return __r;
  1935. }
  1936. /**
  1937. * @brief Compare substring to a string.
  1938. * @param __pos Index of first character of substring.
  1939. * @param __n Number of characters in substring.
  1940. * @param __str String to compare against.
  1941. * @return Integer < 0, 0, or > 0.
  1942. *
  1943. * Form the substring of this string from the @a __n characters
  1944. * starting at @a __pos. Returns an integer < 0 if the
  1945. * substring is ordered before @a __str, 0 if their values are
  1946. * equivalent, or > 0 if the substring is ordered after @a
  1947. * __str. Determines the effective length rlen of the strings
  1948. * to compare as the smallest of the length of the substring
  1949. * and @a __str.size(). The function then compares the two
  1950. * strings by calling
  1951. * traits::compare(substring.data(),str.data(),rlen). If the
  1952. * result of the comparison is nonzero returns it, otherwise
  1953. * the shorter one is ordered first.
  1954. */
  1955. int
  1956. compare(size_type __pos, size_type __n,
  1957. const __versa_string& __str) const;
  1958. /**
  1959. * @brief Compare substring to a substring.
  1960. * @param __pos1 Index of first character of substring.
  1961. * @param __n1 Number of characters in substring.
  1962. * @param __str String to compare against.
  1963. * @param __pos2 Index of first character of substring of str.
  1964. * @param __n2 Number of characters in substring of str.
  1965. * @return Integer < 0, 0, or > 0.
  1966. *
  1967. * Form the substring of this string from the @a __n1
  1968. * characters starting at @a __pos1. Form the substring of @a
  1969. * __str from the @a __n2 characters starting at @a __pos2.
  1970. * Returns an integer < 0 if this substring is ordered before
  1971. * the substring of @a __str, 0 if their values are equivalent,
  1972. * or > 0 if this substring is ordered after the substring of
  1973. * @a __str. Determines the effective length rlen of the
  1974. * strings to compare as the smallest of the lengths of the
  1975. * substrings. The function then compares the two strings by
  1976. * calling
  1977. * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
  1978. * If the result of the comparison is nonzero returns it,
  1979. * otherwise the shorter one is ordered first.
  1980. */
  1981. int
  1982. compare(size_type __pos1, size_type __n1, const __versa_string& __str,
  1983. size_type __pos2, size_type __n2) const;
  1984. /**
  1985. * @brief Compare to a C string.
  1986. * @param __s C string to compare against.
  1987. * @return Integer < 0, 0, or > 0.
  1988. *
  1989. * Returns an integer < 0 if this string is ordered before @a
  1990. * __s, 0 if their values are equivalent, or > 0 if this string
  1991. * is ordered after @a __s. Determines the effective length
  1992. * rlen of the strings to compare as the smallest of size() and
  1993. * the length of a string constructed from @a __s. The
  1994. * function then compares the two strings by calling
  1995. * traits::compare(data(),s,rlen). If the result of the
  1996. * comparison is nonzero returns it, otherwise the shorter one
  1997. * is ordered first.
  1998. */
  1999. int
  2000. compare(const _CharT* __s) const;
  2001. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  2002. // 5 String::compare specification questionable
  2003. /**
  2004. * @brief Compare substring to a C string.
  2005. * @param __pos Index of first character of substring.
  2006. * @param __n1 Number of characters in substring.
  2007. * @param __s C string to compare against.
  2008. * @return Integer < 0, 0, or > 0.
  2009. *
  2010. * Form the substring of this string from the @a __n1
  2011. * characters starting at @a __pos. Returns an integer < 0 if
  2012. * the substring is ordered before @a __s, 0 if their values
  2013. * are equivalent, or > 0 if the substring is ordered after @a
  2014. * __s. Determines the effective length rlen of the strings to
  2015. * compare as the smallest of the length of the substring and
  2016. * the length of a string constructed from @a __s. The
  2017. * function then compares the two string by calling
  2018. * traits::compare(substring.data(),s,rlen). If the result of
  2019. * the comparison is nonzero returns it, otherwise the shorter
  2020. * one is ordered first.
  2021. */
  2022. int
  2023. compare(size_type __pos, size_type __n1, const _CharT* __s) const;
  2024. /**
  2025. * @brief Compare substring against a character array.
  2026. * @param __pos Index of first character of substring.
  2027. * @param __n1 Number of characters in substring.
  2028. * @param __s character array to compare against.
  2029. * @param __n2 Number of characters of s.
  2030. * @return Integer < 0, 0, or > 0.
  2031. *
  2032. * Form the substring of this string from the @a __n1
  2033. * characters starting at @a __pos. Form a string from the
  2034. * first @a __n2 characters of @a __s. Returns an integer < 0
  2035. * if this substring is ordered before the string from @a __s,
  2036. * 0 if their values are equivalent, or > 0 if this substring
  2037. * is ordered after the string from @a __s. Determines the
  2038. * effective length rlen of the strings to compare as the
  2039. * smallest of the length of the substring and @a __n2. The
  2040. * function then compares the two strings by calling
  2041. * traits::compare(substring.data(),__s,rlen). If the result of
  2042. * the comparison is nonzero returns it, otherwise the shorter
  2043. * one is ordered first.
  2044. *
  2045. * NB: __s must have at least n2 characters, <em>\\0</em> has no special
  2046. * meaning.
  2047. */
  2048. int
  2049. compare(size_type __pos, size_type __n1, const _CharT* __s,
  2050. size_type __n2) const;
  2051. };
  2052. // operator+
  2053. /**
  2054. * @brief Concatenate two strings.
  2055. * @param __lhs First string.
  2056. * @param __rhs Last string.
  2057. * @return New string with value of @a __lhs followed by @a __rhs.
  2058. */
  2059. template<typename _CharT, typename _Traits, typename _Alloc,
  2060. template <typename, typename, typename> class _Base>
  2061. __versa_string<_CharT, _Traits, _Alloc, _Base>
  2062. operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2063. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs);
  2064. /**
  2065. * @brief Concatenate C string and string.
  2066. * @param __lhs First string.
  2067. * @param __rhs Last string.
  2068. * @return New string with value of @a __lhs followed by @a __rhs.
  2069. */
  2070. template<typename _CharT, typename _Traits, typename _Alloc,
  2071. template <typename, typename, typename> class _Base>
  2072. __versa_string<_CharT, _Traits, _Alloc, _Base>
  2073. operator+(const _CharT* __lhs,
  2074. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs);
  2075. /**
  2076. * @brief Concatenate character and string.
  2077. * @param __lhs First string.
  2078. * @param __rhs Last string.
  2079. * @return New string with @a __lhs followed by @a __rhs.
  2080. */
  2081. template<typename _CharT, typename _Traits, typename _Alloc,
  2082. template <typename, typename, typename> class _Base>
  2083. __versa_string<_CharT, _Traits, _Alloc, _Base>
  2084. operator+(_CharT __lhs,
  2085. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs);
  2086. /**
  2087. * @brief Concatenate string and C string.
  2088. * @param __lhs First string.
  2089. * @param __rhs Last string.
  2090. * @return New string with @a __lhs followed by @a __rhs.
  2091. */
  2092. template<typename _CharT, typename _Traits, typename _Alloc,
  2093. template <typename, typename, typename> class _Base>
  2094. __versa_string<_CharT, _Traits, _Alloc, _Base>
  2095. operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2096. const _CharT* __rhs);
  2097. /**
  2098. * @brief Concatenate string and character.
  2099. * @param __lhs First string.
  2100. * @param __rhs Last string.
  2101. * @return New string with @a __lhs followed by @a __rhs.
  2102. */
  2103. template<typename _CharT, typename _Traits, typename _Alloc,
  2104. template <typename, typename, typename> class _Base>
  2105. __versa_string<_CharT, _Traits, _Alloc, _Base>
  2106. operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2107. _CharT __rhs);
  2108. #if __cplusplus >= 201103L
  2109. template<typename _CharT, typename _Traits, typename _Alloc,
  2110. template <typename, typename, typename> class _Base>
  2111. inline __versa_string<_CharT, _Traits, _Alloc, _Base>
  2112. operator+(__versa_string<_CharT, _Traits, _Alloc, _Base>&& __lhs,
  2113. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2114. { return std::move(__lhs.append(__rhs)); }
  2115. template<typename _CharT, typename _Traits, typename _Alloc,
  2116. template <typename, typename, typename> class _Base>
  2117. inline __versa_string<_CharT, _Traits, _Alloc, _Base>
  2118. operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2119. __versa_string<_CharT, _Traits, _Alloc, _Base>&& __rhs)
  2120. { return std::move(__rhs.insert(0, __lhs)); }
  2121. template<typename _CharT, typename _Traits, typename _Alloc,
  2122. template <typename, typename, typename> class _Base>
  2123. inline __versa_string<_CharT, _Traits, _Alloc, _Base>
  2124. operator+(__versa_string<_CharT, _Traits, _Alloc, _Base>&& __lhs,
  2125. __versa_string<_CharT, _Traits, _Alloc, _Base>&& __rhs)
  2126. {
  2127. const auto __size = __lhs.size() + __rhs.size();
  2128. const bool __cond = (__size > __lhs.capacity()
  2129. && __size <= __rhs.capacity());
  2130. return __cond ? std::move(__rhs.insert(0, __lhs))
  2131. : std::move(__lhs.append(__rhs));
  2132. }
  2133. template<typename _CharT, typename _Traits, typename _Alloc,
  2134. template <typename, typename, typename> class _Base>
  2135. inline __versa_string<_CharT, _Traits, _Alloc, _Base>
  2136. operator+(const _CharT* __lhs,
  2137. __versa_string<_CharT, _Traits, _Alloc, _Base>&& __rhs)
  2138. { return std::move(__rhs.insert(0, __lhs)); }
  2139. template<typename _CharT, typename _Traits, typename _Alloc,
  2140. template <typename, typename, typename> class _Base>
  2141. inline __versa_string<_CharT, _Traits, _Alloc, _Base>
  2142. operator+(_CharT __lhs,
  2143. __versa_string<_CharT, _Traits, _Alloc, _Base>&& __rhs)
  2144. { return std::move(__rhs.insert(0, 1, __lhs)); }
  2145. template<typename _CharT, typename _Traits, typename _Alloc,
  2146. template <typename, typename, typename> class _Base>
  2147. inline __versa_string<_CharT, _Traits, _Alloc, _Base>
  2148. operator+(__versa_string<_CharT, _Traits, _Alloc, _Base>&& __lhs,
  2149. const _CharT* __rhs)
  2150. { return std::move(__lhs.append(__rhs)); }
  2151. template<typename _CharT, typename _Traits, typename _Alloc,
  2152. template <typename, typename, typename> class _Base>
  2153. inline __versa_string<_CharT, _Traits, _Alloc, _Base>
  2154. operator+(__versa_string<_CharT, _Traits, _Alloc, _Base>&& __lhs,
  2155. _CharT __rhs)
  2156. { return std::move(__lhs.append(1, __rhs)); }
  2157. #endif
  2158. // operator ==
  2159. /**
  2160. * @brief Test equivalence of two strings.
  2161. * @param __lhs First string.
  2162. * @param __rhs Second string.
  2163. * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
  2164. */
  2165. template<typename _CharT, typename _Traits, typename _Alloc,
  2166. template <typename, typename, typename> class _Base>
  2167. inline bool
  2168. operator==(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2169. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2170. { return __lhs.compare(__rhs) == 0; }
  2171. template<typename _CharT,
  2172. template <typename, typename, typename> class _Base>
  2173. inline typename __enable_if<std::__is_char<_CharT>::__value, bool>::__type
  2174. operator==(const __versa_string<_CharT, std::char_traits<_CharT>,
  2175. std::allocator<_CharT>, _Base>& __lhs,
  2176. const __versa_string<_CharT, std::char_traits<_CharT>,
  2177. std::allocator<_CharT>, _Base>& __rhs)
  2178. { return (__lhs.size() == __rhs.size()
  2179. && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
  2180. __lhs.size())); }
  2181. /**
  2182. * @brief Test equivalence of C string and string.
  2183. * @param __lhs C string.
  2184. * @param __rhs String.
  2185. * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
  2186. */
  2187. template<typename _CharT, typename _Traits, typename _Alloc,
  2188. template <typename, typename, typename> class _Base>
  2189. inline bool
  2190. operator==(const _CharT* __lhs,
  2191. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2192. { return __rhs.compare(__lhs) == 0; }
  2193. /**
  2194. * @brief Test equivalence of string and C string.
  2195. * @param __lhs String.
  2196. * @param __rhs C string.
  2197. * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
  2198. */
  2199. template<typename _CharT, typename _Traits, typename _Alloc,
  2200. template <typename, typename, typename> class _Base>
  2201. inline bool
  2202. operator==(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2203. const _CharT* __rhs)
  2204. { return __lhs.compare(__rhs) == 0; }
  2205. // operator !=
  2206. /**
  2207. * @brief Test difference of two strings.
  2208. * @param __lhs First string.
  2209. * @param __rhs Second string.
  2210. * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
  2211. */
  2212. template<typename _CharT, typename _Traits, typename _Alloc,
  2213. template <typename, typename, typename> class _Base>
  2214. inline bool
  2215. operator!=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2216. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2217. { return !(__lhs == __rhs); }
  2218. /**
  2219. * @brief Test difference of C string and string.
  2220. * @param __lhs C string.
  2221. * @param __rhs String.
  2222. * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
  2223. */
  2224. template<typename _CharT, typename _Traits, typename _Alloc,
  2225. template <typename, typename, typename> class _Base>
  2226. inline bool
  2227. operator!=(const _CharT* __lhs,
  2228. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2229. { return !(__lhs == __rhs); }
  2230. /**
  2231. * @brief Test difference of string and C string.
  2232. * @param __lhs String.
  2233. * @param __rhs C string.
  2234. * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
  2235. */
  2236. template<typename _CharT, typename _Traits, typename _Alloc,
  2237. template <typename, typename, typename> class _Base>
  2238. inline bool
  2239. operator!=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2240. const _CharT* __rhs)
  2241. { return !(__lhs == __rhs); }
  2242. // operator <
  2243. /**
  2244. * @brief Test if string precedes string.
  2245. * @param __lhs First string.
  2246. * @param __rhs Second string.
  2247. * @return True if @a __lhs precedes @a __rhs. False otherwise.
  2248. */
  2249. template<typename _CharT, typename _Traits, typename _Alloc,
  2250. template <typename, typename, typename> class _Base>
  2251. inline bool
  2252. operator<(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2253. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2254. { return __lhs.compare(__rhs) < 0; }
  2255. /**
  2256. * @brief Test if string precedes C string.
  2257. * @param __lhs String.
  2258. * @param __rhs C string.
  2259. * @return True if @a __lhs precedes @a __rhs. False otherwise.
  2260. */
  2261. template<typename _CharT, typename _Traits, typename _Alloc,
  2262. template <typename, typename, typename> class _Base>
  2263. inline bool
  2264. operator<(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2265. const _CharT* __rhs)
  2266. { return __lhs.compare(__rhs) < 0; }
  2267. /**
  2268. * @brief Test if C string precedes string.
  2269. * @param __lhs C string.
  2270. * @param __rhs String.
  2271. * @return True if @a __lhs precedes @a __rhs. False otherwise.
  2272. */
  2273. template<typename _CharT, typename _Traits, typename _Alloc,
  2274. template <typename, typename, typename> class _Base>
  2275. inline bool
  2276. operator<(const _CharT* __lhs,
  2277. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2278. { return __rhs.compare(__lhs) > 0; }
  2279. // operator >
  2280. /**
  2281. * @brief Test if string follows string.
  2282. * @param __lhs First string.
  2283. * @param __rhs Second string.
  2284. * @return True if @a __lhs follows @a __rhs. False otherwise.
  2285. */
  2286. template<typename _CharT, typename _Traits, typename _Alloc,
  2287. template <typename, typename, typename> class _Base>
  2288. inline bool
  2289. operator>(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2290. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2291. { return __lhs.compare(__rhs) > 0; }
  2292. /**
  2293. * @brief Test if string follows C string.
  2294. * @param __lhs String.
  2295. * @param __rhs C string.
  2296. * @return True if @a __lhs follows @a __rhs. False otherwise.
  2297. */
  2298. template<typename _CharT, typename _Traits, typename _Alloc,
  2299. template <typename, typename, typename> class _Base>
  2300. inline bool
  2301. operator>(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2302. const _CharT* __rhs)
  2303. { return __lhs.compare(__rhs) > 0; }
  2304. /**
  2305. * @brief Test if C string follows string.
  2306. * @param __lhs C string.
  2307. * @param __rhs String.
  2308. * @return True if @a __lhs follows @a __rhs. False otherwise.
  2309. */
  2310. template<typename _CharT, typename _Traits, typename _Alloc,
  2311. template <typename, typename, typename> class _Base>
  2312. inline bool
  2313. operator>(const _CharT* __lhs,
  2314. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2315. { return __rhs.compare(__lhs) < 0; }
  2316. // operator <=
  2317. /**
  2318. * @brief Test if string doesn't follow string.
  2319. * @param __lhs First string.
  2320. * @param __rhs Second string.
  2321. * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
  2322. */
  2323. template<typename _CharT, typename _Traits, typename _Alloc,
  2324. template <typename, typename, typename> class _Base>
  2325. inline bool
  2326. operator<=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2327. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2328. { return __lhs.compare(__rhs) <= 0; }
  2329. /**
  2330. * @brief Test if string doesn't follow C string.
  2331. * @param __lhs String.
  2332. * @param __rhs C string.
  2333. * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
  2334. */
  2335. template<typename _CharT, typename _Traits, typename _Alloc,
  2336. template <typename, typename, typename> class _Base>
  2337. inline bool
  2338. operator<=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2339. const _CharT* __rhs)
  2340. { return __lhs.compare(__rhs) <= 0; }
  2341. /**
  2342. * @brief Test if C string doesn't follow string.
  2343. * @param __lhs C string.
  2344. * @param __rhs String.
  2345. * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
  2346. */
  2347. template<typename _CharT, typename _Traits, typename _Alloc,
  2348. template <typename, typename, typename> class _Base>
  2349. inline bool
  2350. operator<=(const _CharT* __lhs,
  2351. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2352. { return __rhs.compare(__lhs) >= 0; }
  2353. // operator >=
  2354. /**
  2355. * @brief Test if string doesn't precede string.
  2356. * @param __lhs First string.
  2357. * @param __rhs Second string.
  2358. * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
  2359. */
  2360. template<typename _CharT, typename _Traits, typename _Alloc,
  2361. template <typename, typename, typename> class _Base>
  2362. inline bool
  2363. operator>=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2364. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2365. { return __lhs.compare(__rhs) >= 0; }
  2366. /**
  2367. * @brief Test if string doesn't precede C string.
  2368. * @param __lhs String.
  2369. * @param __rhs C string.
  2370. * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
  2371. */
  2372. template<typename _CharT, typename _Traits, typename _Alloc,
  2373. template <typename, typename, typename> class _Base>
  2374. inline bool
  2375. operator>=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2376. const _CharT* __rhs)
  2377. { return __lhs.compare(__rhs) >= 0; }
  2378. /**
  2379. * @brief Test if C string doesn't precede string.
  2380. * @param __lhs C string.
  2381. * @param __rhs String.
  2382. * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
  2383. */
  2384. template<typename _CharT, typename _Traits, typename _Alloc,
  2385. template <typename, typename, typename> class _Base>
  2386. inline bool
  2387. operator>=(const _CharT* __lhs,
  2388. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2389. { return __rhs.compare(__lhs) <= 0; }
  2390. /**
  2391. * @brief Swap contents of two strings.
  2392. * @param __lhs First string.
  2393. * @param __rhs Second string.
  2394. *
  2395. * Exchanges the contents of @a __lhs and @a __rhs in constant time.
  2396. */
  2397. template<typename _CharT, typename _Traits, typename _Alloc,
  2398. template <typename, typename, typename> class _Base>
  2399. inline void
  2400. swap(__versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2401. __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2402. { __lhs.swap(__rhs); }
  2403. _GLIBCXX_END_NAMESPACE_VERSION
  2404. } // namespace
  2405. namespace std _GLIBCXX_VISIBILITY(default)
  2406. {
  2407. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  2408. /**
  2409. * @brief Read stream into a string.
  2410. * @param __is Input stream.
  2411. * @param __str Buffer to store into.
  2412. * @return Reference to the input stream.
  2413. *
  2414. * Stores characters from @a __is into @a __str until whitespace is
  2415. * found, the end of the stream is encountered, or str.max_size()
  2416. * is reached. If is.width() is non-zero, that is the limit on the
  2417. * number of characters stored into @a __str. Any previous
  2418. * contents of @a __str are erased.
  2419. */
  2420. template<typename _CharT, typename _Traits, typename _Alloc,
  2421. template <typename, typename, typename> class _Base>
  2422. basic_istream<_CharT, _Traits>&
  2423. operator>>(basic_istream<_CharT, _Traits>& __is,
  2424. __gnu_cxx::__versa_string<_CharT, _Traits,
  2425. _Alloc, _Base>& __str);
  2426. /**
  2427. * @brief Write string to a stream.
  2428. * @param __os Output stream.
  2429. * @param __str String to write out.
  2430. * @return Reference to the output stream.
  2431. *
  2432. * Output characters of @a __str into os following the same rules as for
  2433. * writing a C string.
  2434. */
  2435. template<typename _CharT, typename _Traits, typename _Alloc,
  2436. template <typename, typename, typename> class _Base>
  2437. inline basic_ostream<_CharT, _Traits>&
  2438. operator<<(basic_ostream<_CharT, _Traits>& __os,
  2439. const __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc,
  2440. _Base>& __str)
  2441. {
  2442. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  2443. // 586. string inserter not a formatted function
  2444. return __ostream_insert(__os, __str.data(), __str.size());
  2445. }
  2446. /**
  2447. * @brief Read a line from stream into a string.
  2448. * @param __is Input stream.
  2449. * @param __str Buffer to store into.
  2450. * @param __delim Character marking end of line.
  2451. * @return Reference to the input stream.
  2452. *
  2453. * Stores characters from @a __is into @a __str until @a __delim is
  2454. * found, the end of the stream is encountered, or str.max_size()
  2455. * is reached. If is.width() is non-zero, that is the limit on the
  2456. * number of characters stored into @a __str. Any previous
  2457. * contents of @a __str are erased. If @a delim was encountered,
  2458. * it is extracted but not stored into @a __str.
  2459. */
  2460. template<typename _CharT, typename _Traits, typename _Alloc,
  2461. template <typename, typename, typename> class _Base>
  2462. basic_istream<_CharT, _Traits>&
  2463. getline(basic_istream<_CharT, _Traits>& __is,
  2464. __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base>& __str,
  2465. _CharT __delim);
  2466. /**
  2467. * @brief Read a line from stream into a string.
  2468. * @param __is Input stream.
  2469. * @param __str Buffer to store into.
  2470. * @return Reference to the input stream.
  2471. *
  2472. * Stores characters from is into @a __str until &apos;\n&apos; is
  2473. * found, the end of the stream is encountered, or str.max_size()
  2474. * is reached. If is.width() is non-zero, that is the limit on the
  2475. * number of characters stored into @a __str. Any previous
  2476. * contents of @a __str are erased. If end of line was
  2477. * encountered, it is extracted but not stored into @a __str.
  2478. */
  2479. template<typename _CharT, typename _Traits, typename _Alloc,
  2480. template <typename, typename, typename> class _Base>
  2481. inline basic_istream<_CharT, _Traits>&
  2482. getline(basic_istream<_CharT, _Traits>& __is,
  2483. __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base>& __str)
  2484. { return getline(__is, __str, __is.widen('\n')); }
  2485. _GLIBCXX_END_NAMESPACE_VERSION
  2486. } // namespace
  2487. #if __cplusplus >= 201103L
  2488. #include <ext/string_conversions.h>
  2489. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  2490. {
  2491. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  2492. #if _GLIBCXX_USE_C99_STDLIB
  2493. // 21.4 Numeric Conversions [string.conversions].
  2494. inline int
  2495. stoi(const __vstring& __str, std::size_t* __idx = 0, int __base = 10)
  2496. { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
  2497. __idx, __base); }
  2498. inline long
  2499. stol(const __vstring& __str, std::size_t* __idx = 0, int __base = 10)
  2500. { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
  2501. __idx, __base); }
  2502. inline unsigned long
  2503. stoul(const __vstring& __str, std::size_t* __idx = 0, int __base = 10)
  2504. { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
  2505. __idx, __base); }
  2506. inline long long
  2507. stoll(const __vstring& __str, std::size_t* __idx = 0, int __base = 10)
  2508. { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
  2509. __idx, __base); }
  2510. inline unsigned long long
  2511. stoull(const __vstring& __str, std::size_t* __idx, int __base = 10)
  2512. { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
  2513. __idx, __base); }
  2514. // NB: strtof vs strtod.
  2515. inline float
  2516. stof(const __vstring& __str, std::size_t* __idx = 0)
  2517. { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
  2518. inline double
  2519. stod(const __vstring& __str, std::size_t* __idx = 0)
  2520. { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
  2521. inline long double
  2522. stold(const __vstring& __str, std::size_t* __idx = 0)
  2523. { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
  2524. #endif // _GLIBCXX_USE_C99_STDLIB
  2525. #if _GLIBCXX_USE_C99_STDIO
  2526. // NB: (v)snprintf vs sprintf.
  2527. // DR 1261.
  2528. inline __vstring
  2529. to_string(int __val)
  2530. { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf, 4 * sizeof(int),
  2531. "%d", __val); }
  2532. inline __vstring
  2533. to_string(unsigned __val)
  2534. { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf,
  2535. 4 * sizeof(unsigned),
  2536. "%u", __val); }
  2537. inline __vstring
  2538. to_string(long __val)
  2539. { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf,
  2540. 4 * sizeof(long),
  2541. "%ld", __val); }
  2542. inline __vstring
  2543. to_string(unsigned long __val)
  2544. { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf,
  2545. 4 * sizeof(unsigned long),
  2546. "%lu", __val); }
  2547. inline __vstring
  2548. to_string(long long __val)
  2549. { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf,
  2550. 4 * sizeof(long long),
  2551. "%lld", __val); }
  2552. inline __vstring
  2553. to_string(unsigned long long __val)
  2554. { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf,
  2555. 4 * sizeof(unsigned long long),
  2556. "%llu", __val); }
  2557. inline __vstring
  2558. to_string(float __val)
  2559. {
  2560. const int __n = __numeric_traits<float>::__max_exponent10 + 20;
  2561. return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf, __n,
  2562. "%f", __val);
  2563. }
  2564. inline __vstring
  2565. to_string(double __val)
  2566. {
  2567. const int __n = __numeric_traits<double>::__max_exponent10 + 20;
  2568. return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf, __n,
  2569. "%f", __val);
  2570. }
  2571. inline __vstring
  2572. to_string(long double __val)
  2573. {
  2574. const int __n = __numeric_traits<long double>::__max_exponent10 + 20;
  2575. return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf, __n,
  2576. "%Lf", __val);
  2577. }
  2578. #endif // _GLIBCXX_USE_C99_STDIO
  2579. #if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
  2580. inline int
  2581. stoi(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10)
  2582. { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
  2583. __idx, __base); }
  2584. inline long
  2585. stol(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10)
  2586. { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
  2587. __idx, __base); }
  2588. inline unsigned long
  2589. stoul(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10)
  2590. { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
  2591. __idx, __base); }
  2592. inline long long
  2593. stoll(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10)
  2594. { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
  2595. __idx, __base); }
  2596. inline unsigned long long
  2597. stoull(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10)
  2598. { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
  2599. __idx, __base); }
  2600. // NB: wcstof vs wcstod.
  2601. inline float
  2602. stof(const __wvstring& __str, std::size_t* __idx = 0)
  2603. { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
  2604. inline double
  2605. stod(const __wvstring& __str, std::size_t* __idx = 0)
  2606. { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
  2607. inline long double
  2608. stold(const __wvstring& __str, std::size_t* __idx = 0)
  2609. { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
  2610. #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
  2611. // DR 1261.
  2612. inline __wvstring
  2613. to_wstring(int __val)
  2614. { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
  2615. 4 * sizeof(int),
  2616. L"%d", __val); }
  2617. inline __wvstring
  2618. to_wstring(unsigned __val)
  2619. { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
  2620. 4 * sizeof(unsigned),
  2621. L"%u", __val); }
  2622. inline __wvstring
  2623. to_wstring(long __val)
  2624. { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
  2625. 4 * sizeof(long),
  2626. L"%ld", __val); }
  2627. inline __wvstring
  2628. to_wstring(unsigned long __val)
  2629. { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
  2630. 4 * sizeof(unsigned long),
  2631. L"%lu", __val); }
  2632. inline __wvstring
  2633. to_wstring(long long __val)
  2634. { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
  2635. 4 * sizeof(long long),
  2636. L"%lld", __val); }
  2637. inline __wvstring
  2638. to_wstring(unsigned long long __val)
  2639. { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
  2640. 4 * sizeof(unsigned long long),
  2641. L"%llu", __val); }
  2642. inline __wvstring
  2643. to_wstring(float __val)
  2644. {
  2645. const int __n = __numeric_traits<float>::__max_exponent10 + 20;
  2646. return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf, __n,
  2647. L"%f", __val);
  2648. }
  2649. inline __wvstring
  2650. to_wstring(double __val)
  2651. {
  2652. const int __n = __numeric_traits<double>::__max_exponent10 + 20;
  2653. return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf, __n,
  2654. L"%f", __val);
  2655. }
  2656. inline __wvstring
  2657. to_wstring(long double __val)
  2658. {
  2659. const int __n = __numeric_traits<long double>::__max_exponent10 + 20;
  2660. return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf, __n,
  2661. L"%Lf", __val);
  2662. }
  2663. #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
  2664. #endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR
  2665. _GLIBCXX_END_NAMESPACE_VERSION
  2666. } // namespace
  2667. #endif
  2668. #if __cplusplus >= 201103L
  2669. #include <bits/functional_hash.h>
  2670. namespace std _GLIBCXX_VISIBILITY(default)
  2671. {
  2672. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  2673. /// std::hash specialization for __vstring.
  2674. template<>
  2675. struct hash<__gnu_cxx::__vstring>
  2676. : public __hash_base<size_t, __gnu_cxx::__vstring>
  2677. {
  2678. size_t
  2679. operator()(const __gnu_cxx::__vstring& __s) const noexcept
  2680. { return std::_Hash_impl::hash(__s.data(), __s.length()); }
  2681. };
  2682. #ifdef _GLIBCXX_USE_WCHAR_T
  2683. /// std::hash specialization for __wvstring.
  2684. template<>
  2685. struct hash<__gnu_cxx::__wvstring>
  2686. : public __hash_base<size_t, __gnu_cxx::__wvstring>
  2687. {
  2688. size_t
  2689. operator()(const __gnu_cxx::__wvstring& __s) const noexcept
  2690. { return std::_Hash_impl::hash(__s.data(),
  2691. __s.length() * sizeof(wchar_t)); }
  2692. };
  2693. #endif
  2694. /// std::hash specialization for __u16vstring.
  2695. template<>
  2696. struct hash<__gnu_cxx::__u16vstring>
  2697. : public __hash_base<size_t, __gnu_cxx::__u16vstring>
  2698. {
  2699. size_t
  2700. operator()(const __gnu_cxx::__u16vstring& __s) const noexcept
  2701. { return std::_Hash_impl::hash(__s.data(),
  2702. __s.length() * sizeof(char16_t)); }
  2703. };
  2704. /// std::hash specialization for __u32vstring.
  2705. template<>
  2706. struct hash<__gnu_cxx::__u32vstring>
  2707. : public __hash_base<size_t, __gnu_cxx::__u32vstring>
  2708. {
  2709. size_t
  2710. operator()(const __gnu_cxx::__u32vstring& __s) const noexcept
  2711. { return std::_Hash_impl::hash(__s.data(),
  2712. __s.length() * sizeof(char32_t)); }
  2713. };
  2714. _GLIBCXX_END_NAMESPACE_VERSION
  2715. } // namespace
  2716. #endif // C++11
  2717. #include <ext/vstring.tcc>
  2718. #endif /* _VSTRING_H */