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

1658 lines
53KB

  1. // Components for manipulating sequences of characters -*- C++ -*-
  2. // Copyright (C) 1997-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/basic_string.tcc
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{string}
  23. */
  24. //
  25. // ISO C++ 14882: 21 Strings library
  26. //
  27. // Written by Jason Merrill based upon the specification by Takanori Adachi
  28. // in ANSI X3J16/94-0013R2. Rewritten by Nathan Myers to ISO-14882.
  29. // Non-reference-counted implementation written by Paolo Carlini and
  30. // updated by Jonathan Wakely for ISO-14882-2011.
  31. #ifndef _BASIC_STRING_TCC
  32. #define _BASIC_STRING_TCC 1
  33. #pragma GCC system_header
  34. #include <bits/cxxabi_forced.h>
  35. namespace std _GLIBCXX_VISIBILITY(default)
  36. {
  37. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  38. #if _GLIBCXX_USE_CXX11_ABI
  39. template<typename _CharT, typename _Traits, typename _Alloc>
  40. const typename basic_string<_CharT, _Traits, _Alloc>::size_type
  41. basic_string<_CharT, _Traits, _Alloc>::npos;
  42. template<typename _CharT, typename _Traits, typename _Alloc>
  43. void
  44. basic_string<_CharT, _Traits, _Alloc>::
  45. swap(basic_string& __s) _GLIBCXX_NOEXCEPT
  46. {
  47. if (this == &__s)
  48. return;
  49. _Alloc_traits::_S_on_swap(_M_get_allocator(), __s._M_get_allocator());
  50. if (_M_is_local())
  51. if (__s._M_is_local())
  52. {
  53. if (length() && __s.length())
  54. {
  55. _CharT __tmp_data[_S_local_capacity + 1];
  56. traits_type::copy(__tmp_data, __s._M_local_buf,
  57. _S_local_capacity + 1);
  58. traits_type::copy(__s._M_local_buf, _M_local_buf,
  59. _S_local_capacity + 1);
  60. traits_type::copy(_M_local_buf, __tmp_data,
  61. _S_local_capacity + 1);
  62. }
  63. else if (__s.length())
  64. {
  65. traits_type::copy(_M_local_buf, __s._M_local_buf,
  66. _S_local_capacity + 1);
  67. _M_length(__s.length());
  68. __s._M_set_length(0);
  69. return;
  70. }
  71. else if (length())
  72. {
  73. traits_type::copy(__s._M_local_buf, _M_local_buf,
  74. _S_local_capacity + 1);
  75. __s._M_length(length());
  76. _M_set_length(0);
  77. return;
  78. }
  79. }
  80. else
  81. {
  82. const size_type __tmp_capacity = __s._M_allocated_capacity;
  83. traits_type::copy(__s._M_local_buf, _M_local_buf,
  84. _S_local_capacity + 1);
  85. _M_data(__s._M_data());
  86. __s._M_data(__s._M_local_buf);
  87. _M_capacity(__tmp_capacity);
  88. }
  89. else
  90. {
  91. const size_type __tmp_capacity = _M_allocated_capacity;
  92. if (__s._M_is_local())
  93. {
  94. traits_type::copy(_M_local_buf, __s._M_local_buf,
  95. _S_local_capacity + 1);
  96. __s._M_data(_M_data());
  97. _M_data(_M_local_buf);
  98. }
  99. else
  100. {
  101. pointer __tmp_ptr = _M_data();
  102. _M_data(__s._M_data());
  103. __s._M_data(__tmp_ptr);
  104. _M_capacity(__s._M_allocated_capacity);
  105. }
  106. __s._M_capacity(__tmp_capacity);
  107. }
  108. const size_type __tmp_length = length();
  109. _M_length(__s.length());
  110. __s._M_length(__tmp_length);
  111. }
  112. template<typename _CharT, typename _Traits, typename _Alloc>
  113. typename basic_string<_CharT, _Traits, _Alloc>::pointer
  114. basic_string<_CharT, _Traits, _Alloc>::
  115. _M_create(size_type& __capacity, size_type __old_capacity)
  116. {
  117. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  118. // 83. String::npos vs. string::max_size()
  119. if (__capacity > max_size())
  120. std::__throw_length_error(__N("basic_string::_M_create"));
  121. // The below implements an exponential growth policy, necessary to
  122. // meet amortized linear time requirements of the library: see
  123. // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
  124. if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
  125. {
  126. __capacity = 2 * __old_capacity;
  127. // Never allocate a string bigger than max_size.
  128. if (__capacity > max_size())
  129. __capacity = max_size();
  130. }
  131. // NB: Need an array of char_type[__capacity], plus a terminating
  132. // null char_type() element.
  133. return _Alloc_traits::allocate(_M_get_allocator(), __capacity + 1);
  134. }
  135. // NB: This is the special case for Input Iterators, used in
  136. // istreambuf_iterators, etc.
  137. // Input Iterators have a cost structure very different from
  138. // pointers, calling for a different coding style.
  139. template<typename _CharT, typename _Traits, typename _Alloc>
  140. template<typename _InIterator>
  141. void
  142. basic_string<_CharT, _Traits, _Alloc>::
  143. _M_construct(_InIterator __beg, _InIterator __end,
  144. std::input_iterator_tag)
  145. {
  146. size_type __len = 0;
  147. size_type __capacity = size_type(_S_local_capacity);
  148. while (__beg != __end && __len < __capacity)
  149. {
  150. _M_data()[__len++] = *__beg;
  151. ++__beg;
  152. }
  153. __try
  154. {
  155. while (__beg != __end)
  156. {
  157. if (__len == __capacity)
  158. {
  159. // Allocate more space.
  160. __capacity = __len + 1;
  161. pointer __another = _M_create(__capacity, __len);
  162. this->_S_copy(__another, _M_data(), __len);
  163. _M_dispose();
  164. _M_data(__another);
  165. _M_capacity(__capacity);
  166. }
  167. _M_data()[__len++] = *__beg;
  168. ++__beg;
  169. }
  170. }
  171. __catch(...)
  172. {
  173. _M_dispose();
  174. __throw_exception_again;
  175. }
  176. _M_set_length(__len);
  177. }
  178. template<typename _CharT, typename _Traits, typename _Alloc>
  179. template<typename _InIterator>
  180. void
  181. basic_string<_CharT, _Traits, _Alloc>::
  182. _M_construct(_InIterator __beg, _InIterator __end,
  183. std::forward_iterator_tag)
  184. {
  185. // NB: Not required, but considered best practice.
  186. if (__gnu_cxx::__is_null_pointer(__beg) && __beg != __end)
  187. std::__throw_logic_error(__N("basic_string::"
  188. "_M_construct null not valid"));
  189. size_type __dnew = static_cast<size_type>(std::distance(__beg, __end));
  190. if (__dnew > size_type(_S_local_capacity))
  191. {
  192. _M_data(_M_create(__dnew, size_type(0)));
  193. _M_capacity(__dnew);
  194. }
  195. // Check for out_of_range and length_error exceptions.
  196. __try
  197. { this->_S_copy_chars(_M_data(), __beg, __end); }
  198. __catch(...)
  199. {
  200. _M_dispose();
  201. __throw_exception_again;
  202. }
  203. _M_set_length(__dnew);
  204. }
  205. template<typename _CharT, typename _Traits, typename _Alloc>
  206. void
  207. basic_string<_CharT, _Traits, _Alloc>::
  208. _M_construct(size_type __n, _CharT __c)
  209. {
  210. if (__n > size_type(_S_local_capacity))
  211. {
  212. _M_data(_M_create(__n, size_type(0)));
  213. _M_capacity(__n);
  214. }
  215. if (__n)
  216. this->_S_assign(_M_data(), __n, __c);
  217. _M_set_length(__n);
  218. }
  219. template<typename _CharT, typename _Traits, typename _Alloc>
  220. void
  221. basic_string<_CharT, _Traits, _Alloc>::
  222. _M_assign(const basic_string& __str)
  223. {
  224. if (this != &__str)
  225. {
  226. const size_type __rsize = __str.length();
  227. const size_type __capacity = capacity();
  228. if (__rsize > __capacity)
  229. {
  230. size_type __new_capacity = __rsize;
  231. pointer __tmp = _M_create(__new_capacity, __capacity);
  232. _M_dispose();
  233. _M_data(__tmp);
  234. _M_capacity(__new_capacity);
  235. }
  236. if (__rsize)
  237. this->_S_copy(_M_data(), __str._M_data(), __rsize);
  238. _M_set_length(__rsize);
  239. }
  240. }
  241. template<typename _CharT, typename _Traits, typename _Alloc>
  242. void
  243. basic_string<_CharT, _Traits, _Alloc>::
  244. reserve(size_type __res)
  245. {
  246. // Make sure we don't shrink below the current size.
  247. if (__res < length())
  248. __res = length();
  249. const size_type __capacity = capacity();
  250. if (__res != __capacity)
  251. {
  252. if (__res > __capacity
  253. || __res > size_type(_S_local_capacity))
  254. {
  255. pointer __tmp = _M_create(__res, __capacity);
  256. this->_S_copy(__tmp, _M_data(), length() + 1);
  257. _M_dispose();
  258. _M_data(__tmp);
  259. _M_capacity(__res);
  260. }
  261. else if (!_M_is_local())
  262. {
  263. this->_S_copy(_M_local_data(), _M_data(), length() + 1);
  264. _M_destroy(__capacity);
  265. _M_data(_M_local_data());
  266. }
  267. }
  268. }
  269. template<typename _CharT, typename _Traits, typename _Alloc>
  270. void
  271. basic_string<_CharT, _Traits, _Alloc>::
  272. _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
  273. size_type __len2)
  274. {
  275. const size_type __how_much = length() - __pos - __len1;
  276. size_type __new_capacity = length() + __len2 - __len1;
  277. pointer __r = _M_create(__new_capacity, capacity());
  278. if (__pos)
  279. this->_S_copy(__r, _M_data(), __pos);
  280. if (__s && __len2)
  281. this->_S_copy(__r + __pos, __s, __len2);
  282. if (__how_much)
  283. this->_S_copy(__r + __pos + __len2,
  284. _M_data() + __pos + __len1, __how_much);
  285. _M_dispose();
  286. _M_data(__r);
  287. _M_capacity(__new_capacity);
  288. }
  289. template<typename _CharT, typename _Traits, typename _Alloc>
  290. void
  291. basic_string<_CharT, _Traits, _Alloc>::
  292. _M_erase(size_type __pos, size_type __n)
  293. {
  294. const size_type __how_much = length() - __pos - __n;
  295. if (__how_much && __n)
  296. this->_S_move(_M_data() + __pos, _M_data() + __pos + __n, __how_much);
  297. _M_set_length(length() - __n);
  298. }
  299. template<typename _CharT, typename _Traits, typename _Alloc>
  300. void
  301. basic_string<_CharT, _Traits, _Alloc>::
  302. resize(size_type __n, _CharT __c)
  303. {
  304. const size_type __size = this->size();
  305. if (__size < __n)
  306. this->append(__n - __size, __c);
  307. else if (__n < __size)
  308. this->_M_set_length(__n);
  309. }
  310. template<typename _CharT, typename _Traits, typename _Alloc>
  311. basic_string<_CharT, _Traits, _Alloc>&
  312. basic_string<_CharT, _Traits, _Alloc>::
  313. _M_append(const _CharT* __s, size_type __n)
  314. {
  315. const size_type __len = __n + this->size();
  316. if (__len <= this->capacity())
  317. {
  318. if (__n)
  319. this->_S_copy(this->_M_data() + this->size(), __s, __n);
  320. }
  321. else
  322. this->_M_mutate(this->size(), size_type(0), __s, __n);
  323. this->_M_set_length(__len);
  324. return *this;
  325. }
  326. template<typename _CharT, typename _Traits, typename _Alloc>
  327. template<typename _InputIterator>
  328. basic_string<_CharT, _Traits, _Alloc>&
  329. basic_string<_CharT, _Traits, _Alloc>::
  330. _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
  331. _InputIterator __k1, _InputIterator __k2,
  332. std::__false_type)
  333. {
  334. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  335. // 2788. unintentionally require a default constructible allocator
  336. const basic_string __s(__k1, __k2, this->get_allocator());
  337. const size_type __n1 = __i2 - __i1;
  338. return _M_replace(__i1 - begin(), __n1, __s._M_data(),
  339. __s.size());
  340. }
  341. template<typename _CharT, typename _Traits, typename _Alloc>
  342. basic_string<_CharT, _Traits, _Alloc>&
  343. basic_string<_CharT, _Traits, _Alloc>::
  344. _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
  345. _CharT __c)
  346. {
  347. _M_check_length(__n1, __n2, "basic_string::_M_replace_aux");
  348. const size_type __old_size = this->size();
  349. const size_type __new_size = __old_size + __n2 - __n1;
  350. if (__new_size <= this->capacity())
  351. {
  352. pointer __p = this->_M_data() + __pos1;
  353. const size_type __how_much = __old_size - __pos1 - __n1;
  354. if (__how_much && __n1 != __n2)
  355. this->_S_move(__p + __n2, __p + __n1, __how_much);
  356. }
  357. else
  358. this->_M_mutate(__pos1, __n1, 0, __n2);
  359. if (__n2)
  360. this->_S_assign(this->_M_data() + __pos1, __n2, __c);
  361. this->_M_set_length(__new_size);
  362. return *this;
  363. }
  364. template<typename _CharT, typename _Traits, typename _Alloc>
  365. basic_string<_CharT, _Traits, _Alloc>&
  366. basic_string<_CharT, _Traits, _Alloc>::
  367. _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
  368. const size_type __len2)
  369. {
  370. _M_check_length(__len1, __len2, "basic_string::_M_replace");
  371. const size_type __old_size = this->size();
  372. const size_type __new_size = __old_size + __len2 - __len1;
  373. if (__new_size <= this->capacity())
  374. {
  375. pointer __p = this->_M_data() + __pos;
  376. const size_type __how_much = __old_size - __pos - __len1;
  377. if (_M_disjunct(__s))
  378. {
  379. if (__how_much && __len1 != __len2)
  380. this->_S_move(__p + __len2, __p + __len1, __how_much);
  381. if (__len2)
  382. this->_S_copy(__p, __s, __len2);
  383. }
  384. else
  385. {
  386. // Work in-place.
  387. if (__len2 && __len2 <= __len1)
  388. this->_S_move(__p, __s, __len2);
  389. if (__how_much && __len1 != __len2)
  390. this->_S_move(__p + __len2, __p + __len1, __how_much);
  391. if (__len2 > __len1)
  392. {
  393. if (__s + __len2 <= __p + __len1)
  394. this->_S_move(__p, __s, __len2);
  395. else if (__s >= __p + __len1)
  396. this->_S_copy(__p, __s + __len2 - __len1, __len2);
  397. else
  398. {
  399. const size_type __nleft = (__p + __len1) - __s;
  400. this->_S_move(__p, __s, __nleft);
  401. this->_S_copy(__p + __nleft, __p + __len2,
  402. __len2 - __nleft);
  403. }
  404. }
  405. }
  406. }
  407. else
  408. this->_M_mutate(__pos, __len1, __s, __len2);
  409. this->_M_set_length(__new_size);
  410. return *this;
  411. }
  412. template<typename _CharT, typename _Traits, typename _Alloc>
  413. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  414. basic_string<_CharT, _Traits, _Alloc>::
  415. copy(_CharT* __s, size_type __n, size_type __pos) const
  416. {
  417. _M_check(__pos, "basic_string::copy");
  418. __n = _M_limit(__pos, __n);
  419. __glibcxx_requires_string_len(__s, __n);
  420. if (__n)
  421. _S_copy(__s, _M_data() + __pos, __n);
  422. // 21.3.5.7 par 3: do not append null. (good.)
  423. return __n;
  424. }
  425. #else // !_GLIBCXX_USE_CXX11_ABI
  426. template<typename _CharT, typename _Traits, typename _Alloc>
  427. const typename basic_string<_CharT, _Traits, _Alloc>::size_type
  428. basic_string<_CharT, _Traits, _Alloc>::
  429. _Rep::_S_max_size = (((npos - sizeof(_Rep_base))/sizeof(_CharT)) - 1) / 4;
  430. template<typename _CharT, typename _Traits, typename _Alloc>
  431. const _CharT
  432. basic_string<_CharT, _Traits, _Alloc>::
  433. _Rep::_S_terminal = _CharT();
  434. template<typename _CharT, typename _Traits, typename _Alloc>
  435. const typename basic_string<_CharT, _Traits, _Alloc>::size_type
  436. basic_string<_CharT, _Traits, _Alloc>::npos;
  437. // Linker sets _S_empty_rep_storage to all 0s (one reference, empty string)
  438. // at static init time (before static ctors are run).
  439. template<typename _CharT, typename _Traits, typename _Alloc>
  440. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  441. basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_empty_rep_storage[
  442. (sizeof(_Rep_base) + sizeof(_CharT) + sizeof(size_type) - 1) /
  443. sizeof(size_type)];
  444. // NB: This is the special case for Input Iterators, used in
  445. // istreambuf_iterators, etc.
  446. // Input Iterators have a cost structure very different from
  447. // pointers, calling for a different coding style.
  448. template<typename _CharT, typename _Traits, typename _Alloc>
  449. template<typename _InIterator>
  450. _CharT*
  451. basic_string<_CharT, _Traits, _Alloc>::
  452. _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
  453. input_iterator_tag)
  454. {
  455. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  456. if (__beg == __end && __a == _Alloc())
  457. return _S_empty_rep()._M_refdata();
  458. #endif
  459. // Avoid reallocation for common case.
  460. _CharT __buf[128];
  461. size_type __len = 0;
  462. while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT))
  463. {
  464. __buf[__len++] = *__beg;
  465. ++__beg;
  466. }
  467. _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
  468. _M_copy(__r->_M_refdata(), __buf, __len);
  469. __try
  470. {
  471. while (__beg != __end)
  472. {
  473. if (__len == __r->_M_capacity)
  474. {
  475. // Allocate more space.
  476. _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
  477. _M_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
  478. __r->_M_destroy(__a);
  479. __r = __another;
  480. }
  481. __r->_M_refdata()[__len++] = *__beg;
  482. ++__beg;
  483. }
  484. }
  485. __catch(...)
  486. {
  487. __r->_M_destroy(__a);
  488. __throw_exception_again;
  489. }
  490. __r->_M_set_length_and_sharable(__len);
  491. return __r->_M_refdata();
  492. }
  493. template<typename _CharT, typename _Traits, typename _Alloc>
  494. template <typename _InIterator>
  495. _CharT*
  496. basic_string<_CharT, _Traits, _Alloc>::
  497. _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
  498. forward_iterator_tag)
  499. {
  500. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  501. if (__beg == __end && __a == _Alloc())
  502. return _S_empty_rep()._M_refdata();
  503. #endif
  504. // NB: Not required, but considered best practice.
  505. if (__gnu_cxx::__is_null_pointer(__beg) && __beg != __end)
  506. __throw_logic_error(__N("basic_string::_S_construct null not valid"));
  507. const size_type __dnew = static_cast<size_type>(std::distance(__beg,
  508. __end));
  509. // Check for out_of_range and length_error exceptions.
  510. _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
  511. __try
  512. { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
  513. __catch(...)
  514. {
  515. __r->_M_destroy(__a);
  516. __throw_exception_again;
  517. }
  518. __r->_M_set_length_and_sharable(__dnew);
  519. return __r->_M_refdata();
  520. }
  521. template<typename _CharT, typename _Traits, typename _Alloc>
  522. _CharT*
  523. basic_string<_CharT, _Traits, _Alloc>::
  524. _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
  525. {
  526. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  527. if (__n == 0 && __a == _Alloc())
  528. return _S_empty_rep()._M_refdata();
  529. #endif
  530. // Check for out_of_range and length_error exceptions.
  531. _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
  532. if (__n)
  533. _M_assign(__r->_M_refdata(), __n, __c);
  534. __r->_M_set_length_and_sharable(__n);
  535. return __r->_M_refdata();
  536. }
  537. template<typename _CharT, typename _Traits, typename _Alloc>
  538. basic_string<_CharT, _Traits, _Alloc>::
  539. basic_string(const basic_string& __str)
  540. : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(__str.get_allocator()),
  541. __str.get_allocator()),
  542. __str.get_allocator())
  543. { }
  544. template<typename _CharT, typename _Traits, typename _Alloc>
  545. basic_string<_CharT, _Traits, _Alloc>::
  546. basic_string(const _Alloc& __a)
  547. : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a)
  548. { }
  549. template<typename _CharT, typename _Traits, typename _Alloc>
  550. basic_string<_CharT, _Traits, _Alloc>::
  551. basic_string(const basic_string& __str, size_type __pos, const _Alloc& __a)
  552. : _M_dataplus(_S_construct(__str._M_data()
  553. + __str._M_check(__pos,
  554. "basic_string::basic_string"),
  555. __str._M_data() + __str._M_limit(__pos, npos)
  556. + __pos, __a), __a)
  557. { }
  558. template<typename _CharT, typename _Traits, typename _Alloc>
  559. basic_string<_CharT, _Traits, _Alloc>::
  560. basic_string(const basic_string& __str, size_type __pos, size_type __n)
  561. : _M_dataplus(_S_construct(__str._M_data()
  562. + __str._M_check(__pos,
  563. "basic_string::basic_string"),
  564. __str._M_data() + __str._M_limit(__pos, __n)
  565. + __pos, _Alloc()), _Alloc())
  566. { }
  567. template<typename _CharT, typename _Traits, typename _Alloc>
  568. basic_string<_CharT, _Traits, _Alloc>::
  569. basic_string(const basic_string& __str, size_type __pos,
  570. size_type __n, const _Alloc& __a)
  571. : _M_dataplus(_S_construct(__str._M_data()
  572. + __str._M_check(__pos,
  573. "basic_string::basic_string"),
  574. __str._M_data() + __str._M_limit(__pos, __n)
  575. + __pos, __a), __a)
  576. { }
  577. // TBD: DPG annotate
  578. template<typename _CharT, typename _Traits, typename _Alloc>
  579. basic_string<_CharT, _Traits, _Alloc>::
  580. basic_string(const _CharT* __s, size_type __n, const _Alloc& __a)
  581. : _M_dataplus(_S_construct(__s, __s + __n, __a), __a)
  582. { }
  583. template<typename _CharT, typename _Traits, typename _Alloc>
  584. basic_string<_CharT, _Traits, _Alloc>::
  585. basic_string(size_type __n, _CharT __c, const _Alloc& __a)
  586. : _M_dataplus(_S_construct(__n, __c, __a), __a)
  587. { }
  588. // TBD: DPG annotate
  589. template<typename _CharT, typename _Traits, typename _Alloc>
  590. template<typename _InputIterator>
  591. basic_string<_CharT, _Traits, _Alloc>::
  592. basic_string(_InputIterator __beg, _InputIterator __end, const _Alloc& __a)
  593. : _M_dataplus(_S_construct(__beg, __end, __a), __a)
  594. { }
  595. #if __cplusplus >= 201103L
  596. template<typename _CharT, typename _Traits, typename _Alloc>
  597. basic_string<_CharT, _Traits, _Alloc>::
  598. basic_string(initializer_list<_CharT> __l, const _Alloc& __a)
  599. : _M_dataplus(_S_construct(__l.begin(), __l.end(), __a), __a)
  600. { }
  601. #endif
  602. template<typename _CharT, typename _Traits, typename _Alloc>
  603. basic_string<_CharT, _Traits, _Alloc>&
  604. basic_string<_CharT, _Traits, _Alloc>::
  605. assign(const basic_string& __str)
  606. {
  607. if (_M_rep() != __str._M_rep())
  608. {
  609. // XXX MT
  610. const allocator_type __a = this->get_allocator();
  611. _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());
  612. _M_rep()->_M_dispose(__a);
  613. _M_data(__tmp);
  614. }
  615. return *this;
  616. }
  617. template<typename _CharT, typename _Traits, typename _Alloc>
  618. basic_string<_CharT, _Traits, _Alloc>&
  619. basic_string<_CharT, _Traits, _Alloc>::
  620. assign(const _CharT* __s, size_type __n)
  621. {
  622. __glibcxx_requires_string_len(__s, __n);
  623. _M_check_length(this->size(), __n, "basic_string::assign");
  624. if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
  625. return _M_replace_safe(size_type(0), this->size(), __s, __n);
  626. else
  627. {
  628. // Work in-place.
  629. const size_type __pos = __s - _M_data();
  630. if (__pos >= __n)
  631. _M_copy(_M_data(), __s, __n);
  632. else if (__pos)
  633. _M_move(_M_data(), __s, __n);
  634. _M_rep()->_M_set_length_and_sharable(__n);
  635. return *this;
  636. }
  637. }
  638. template<typename _CharT, typename _Traits, typename _Alloc>
  639. basic_string<_CharT, _Traits, _Alloc>&
  640. basic_string<_CharT, _Traits, _Alloc>::
  641. append(size_type __n, _CharT __c)
  642. {
  643. if (__n)
  644. {
  645. _M_check_length(size_type(0), __n, "basic_string::append");
  646. const size_type __len = __n + this->size();
  647. if (__len > this->capacity() || _M_rep()->_M_is_shared())
  648. this->reserve(__len);
  649. _M_assign(_M_data() + this->size(), __n, __c);
  650. _M_rep()->_M_set_length_and_sharable(__len);
  651. }
  652. return *this;
  653. }
  654. template<typename _CharT, typename _Traits, typename _Alloc>
  655. basic_string<_CharT, _Traits, _Alloc>&
  656. basic_string<_CharT, _Traits, _Alloc>::
  657. append(const _CharT* __s, size_type __n)
  658. {
  659. __glibcxx_requires_string_len(__s, __n);
  660. if (__n)
  661. {
  662. _M_check_length(size_type(0), __n, "basic_string::append");
  663. const size_type __len = __n + this->size();
  664. if (__len > this->capacity() || _M_rep()->_M_is_shared())
  665. {
  666. if (_M_disjunct(__s))
  667. this->reserve(__len);
  668. else
  669. {
  670. const size_type __off = __s - _M_data();
  671. this->reserve(__len);
  672. __s = _M_data() + __off;
  673. }
  674. }
  675. _M_copy(_M_data() + this->size(), __s, __n);
  676. _M_rep()->_M_set_length_and_sharable(__len);
  677. }
  678. return *this;
  679. }
  680. template<typename _CharT, typename _Traits, typename _Alloc>
  681. basic_string<_CharT, _Traits, _Alloc>&
  682. basic_string<_CharT, _Traits, _Alloc>::
  683. append(const basic_string& __str)
  684. {
  685. const size_type __size = __str.size();
  686. if (__size)
  687. {
  688. const size_type __len = __size + this->size();
  689. if (__len > this->capacity() || _M_rep()->_M_is_shared())
  690. this->reserve(__len);
  691. _M_copy(_M_data() + this->size(), __str._M_data(), __size);
  692. _M_rep()->_M_set_length_and_sharable(__len);
  693. }
  694. return *this;
  695. }
  696. template<typename _CharT, typename _Traits, typename _Alloc>
  697. basic_string<_CharT, _Traits, _Alloc>&
  698. basic_string<_CharT, _Traits, _Alloc>::
  699. append(const basic_string& __str, size_type __pos, size_type __n)
  700. {
  701. __str._M_check(__pos, "basic_string::append");
  702. __n = __str._M_limit(__pos, __n);
  703. if (__n)
  704. {
  705. const size_type __len = __n + this->size();
  706. if (__len > this->capacity() || _M_rep()->_M_is_shared())
  707. this->reserve(__len);
  708. _M_copy(_M_data() + this->size(), __str._M_data() + __pos, __n);
  709. _M_rep()->_M_set_length_and_sharable(__len);
  710. }
  711. return *this;
  712. }
  713. template<typename _CharT, typename _Traits, typename _Alloc>
  714. basic_string<_CharT, _Traits, _Alloc>&
  715. basic_string<_CharT, _Traits, _Alloc>::
  716. insert(size_type __pos, const _CharT* __s, size_type __n)
  717. {
  718. __glibcxx_requires_string_len(__s, __n);
  719. _M_check(__pos, "basic_string::insert");
  720. _M_check_length(size_type(0), __n, "basic_string::insert");
  721. if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
  722. return _M_replace_safe(__pos, size_type(0), __s, __n);
  723. else
  724. {
  725. // Work in-place.
  726. const size_type __off = __s - _M_data();
  727. _M_mutate(__pos, 0, __n);
  728. __s = _M_data() + __off;
  729. _CharT* __p = _M_data() + __pos;
  730. if (__s + __n <= __p)
  731. _M_copy(__p, __s, __n);
  732. else if (__s >= __p)
  733. _M_copy(__p, __s + __n, __n);
  734. else
  735. {
  736. const size_type __nleft = __p - __s;
  737. _M_copy(__p, __s, __nleft);
  738. _M_copy(__p + __nleft, __p + __n, __n - __nleft);
  739. }
  740. return *this;
  741. }
  742. }
  743. template<typename _CharT, typename _Traits, typename _Alloc>
  744. typename basic_string<_CharT, _Traits, _Alloc>::iterator
  745. basic_string<_CharT, _Traits, _Alloc>::
  746. erase(iterator __first, iterator __last)
  747. {
  748. _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
  749. && __last <= _M_iend());
  750. // NB: This isn't just an optimization (bail out early when
  751. // there is nothing to do, really), it's also a correctness
  752. // issue vs MT, see libstdc++/40518.
  753. const size_type __size = __last - __first;
  754. if (__size)
  755. {
  756. const size_type __pos = __first - _M_ibegin();
  757. _M_mutate(__pos, __size, size_type(0));
  758. _M_rep()->_M_set_leaked();
  759. return iterator(_M_data() + __pos);
  760. }
  761. else
  762. return __first;
  763. }
  764. template<typename _CharT, typename _Traits, typename _Alloc>
  765. basic_string<_CharT, _Traits, _Alloc>&
  766. basic_string<_CharT, _Traits, _Alloc>::
  767. replace(size_type __pos, size_type __n1, const _CharT* __s,
  768. size_type __n2)
  769. {
  770. __glibcxx_requires_string_len(__s, __n2);
  771. _M_check(__pos, "basic_string::replace");
  772. __n1 = _M_limit(__pos, __n1);
  773. _M_check_length(__n1, __n2, "basic_string::replace");
  774. bool __left;
  775. if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
  776. return _M_replace_safe(__pos, __n1, __s, __n2);
  777. else if ((__left = __s + __n2 <= _M_data() + __pos)
  778. || _M_data() + __pos + __n1 <= __s)
  779. {
  780. // Work in-place: non-overlapping case.
  781. size_type __off = __s - _M_data();
  782. __left ? __off : (__off += __n2 - __n1);
  783. _M_mutate(__pos, __n1, __n2);
  784. _M_copy(_M_data() + __pos, _M_data() + __off, __n2);
  785. return *this;
  786. }
  787. else
  788. {
  789. // Todo: overlapping case.
  790. const basic_string __tmp(__s, __n2);
  791. return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2);
  792. }
  793. }
  794. template<typename _CharT, typename _Traits, typename _Alloc>
  795. void
  796. basic_string<_CharT, _Traits, _Alloc>::_Rep::
  797. _M_destroy(const _Alloc& __a) throw ()
  798. {
  799. const size_type __size = sizeof(_Rep_base) +
  800. (this->_M_capacity + 1) * sizeof(_CharT);
  801. _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);
  802. }
  803. template<typename _CharT, typename _Traits, typename _Alloc>
  804. void
  805. basic_string<_CharT, _Traits, _Alloc>::
  806. _M_leak_hard()
  807. {
  808. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  809. if (_M_rep() == &_S_empty_rep())
  810. return;
  811. #endif
  812. if (_M_rep()->_M_is_shared())
  813. _M_mutate(0, 0, 0);
  814. _M_rep()->_M_set_leaked();
  815. }
  816. template<typename _CharT, typename _Traits, typename _Alloc>
  817. void
  818. basic_string<_CharT, _Traits, _Alloc>::
  819. _M_mutate(size_type __pos, size_type __len1, size_type __len2)
  820. {
  821. const size_type __old_size = this->size();
  822. const size_type __new_size = __old_size + __len2 - __len1;
  823. const size_type __how_much = __old_size - __pos - __len1;
  824. if (__new_size > this->capacity() || _M_rep()->_M_is_shared())
  825. {
  826. // Must reallocate.
  827. const allocator_type __a = get_allocator();
  828. _Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a);
  829. if (__pos)
  830. _M_copy(__r->_M_refdata(), _M_data(), __pos);
  831. if (__how_much)
  832. _M_copy(__r->_M_refdata() + __pos + __len2,
  833. _M_data() + __pos + __len1, __how_much);
  834. _M_rep()->_M_dispose(__a);
  835. _M_data(__r->_M_refdata());
  836. }
  837. else if (__how_much && __len1 != __len2)
  838. {
  839. // Work in-place.
  840. _M_move(_M_data() + __pos + __len2,
  841. _M_data() + __pos + __len1, __how_much);
  842. }
  843. _M_rep()->_M_set_length_and_sharable(__new_size);
  844. }
  845. template<typename _CharT, typename _Traits, typename _Alloc>
  846. void
  847. basic_string<_CharT, _Traits, _Alloc>::
  848. reserve(size_type __res)
  849. {
  850. if (__res != this->capacity() || _M_rep()->_M_is_shared())
  851. {
  852. // Make sure we don't shrink below the current size
  853. if (__res < this->size())
  854. __res = this->size();
  855. const allocator_type __a = get_allocator();
  856. _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size());
  857. _M_rep()->_M_dispose(__a);
  858. _M_data(__tmp);
  859. }
  860. }
  861. template<typename _CharT, typename _Traits, typename _Alloc>
  862. void
  863. basic_string<_CharT, _Traits, _Alloc>::
  864. swap(basic_string& __s)
  865. _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value)
  866. {
  867. if (_M_rep()->_M_is_leaked())
  868. _M_rep()->_M_set_sharable();
  869. if (__s._M_rep()->_M_is_leaked())
  870. __s._M_rep()->_M_set_sharable();
  871. if (this->get_allocator() == __s.get_allocator())
  872. {
  873. _CharT* __tmp = _M_data();
  874. _M_data(__s._M_data());
  875. __s._M_data(__tmp);
  876. }
  877. // The code below can usually be optimized away.
  878. else
  879. {
  880. const basic_string __tmp1(_M_ibegin(), _M_iend(),
  881. __s.get_allocator());
  882. const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
  883. this->get_allocator());
  884. *this = __tmp2;
  885. __s = __tmp1;
  886. }
  887. }
  888. template<typename _CharT, typename _Traits, typename _Alloc>
  889. typename basic_string<_CharT, _Traits, _Alloc>::_Rep*
  890. basic_string<_CharT, _Traits, _Alloc>::_Rep::
  891. _S_create(size_type __capacity, size_type __old_capacity,
  892. const _Alloc& __alloc)
  893. {
  894. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  895. // 83. String::npos vs. string::max_size()
  896. if (__capacity > _S_max_size)
  897. __throw_length_error(__N("basic_string::_S_create"));
  898. // The standard places no restriction on allocating more memory
  899. // than is strictly needed within this layer at the moment or as
  900. // requested by an explicit application call to reserve().
  901. // Many malloc implementations perform quite poorly when an
  902. // application attempts to allocate memory in a stepwise fashion
  903. // growing each allocation size by only 1 char. Additionally,
  904. // it makes little sense to allocate less linear memory than the
  905. // natural blocking size of the malloc implementation.
  906. // Unfortunately, we would need a somewhat low-level calculation
  907. // with tuned parameters to get this perfect for any particular
  908. // malloc implementation. Fortunately, generalizations about
  909. // common features seen among implementations seems to suffice.
  910. // __pagesize need not match the actual VM page size for good
  911. // results in practice, thus we pick a common value on the low
  912. // side. __malloc_header_size is an estimate of the amount of
  913. // overhead per memory allocation (in practice seen N * sizeof
  914. // (void*) where N is 0, 2 or 4). According to folklore,
  915. // picking this value on the high side is better than
  916. // low-balling it (especially when this algorithm is used with
  917. // malloc implementations that allocate memory blocks rounded up
  918. // to a size which is a power of 2).
  919. const size_type __pagesize = 4096;
  920. const size_type __malloc_header_size = 4 * sizeof(void*);
  921. // The below implements an exponential growth policy, necessary to
  922. // meet amortized linear time requirements of the library: see
  923. // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
  924. // It's active for allocations requiring an amount of memory above
  925. // system pagesize. This is consistent with the requirements of the
  926. // standard: http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html
  927. if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
  928. __capacity = 2 * __old_capacity;
  929. // NB: Need an array of char_type[__capacity], plus a terminating
  930. // null char_type() element, plus enough for the _Rep data structure.
  931. // Whew. Seemingly so needy, yet so elemental.
  932. size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
  933. const size_type __adj_size = __size + __malloc_header_size;
  934. if (__adj_size > __pagesize && __capacity > __old_capacity)
  935. {
  936. const size_type __extra = __pagesize - __adj_size % __pagesize;
  937. __capacity += __extra / sizeof(_CharT);
  938. // Never allocate a string bigger than _S_max_size.
  939. if (__capacity > _S_max_size)
  940. __capacity = _S_max_size;
  941. __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
  942. }
  943. // NB: Might throw, but no worries about a leak, mate: _Rep()
  944. // does not throw.
  945. void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
  946. _Rep *__p = new (__place) _Rep;
  947. __p->_M_capacity = __capacity;
  948. // ABI compatibility - 3.4.x set in _S_create both
  949. // _M_refcount and _M_length. All callers of _S_create
  950. // in basic_string.tcc then set just _M_length.
  951. // In 4.0.x and later both _M_refcount and _M_length
  952. // are initialized in the callers, unfortunately we can
  953. // have 3.4.x compiled code with _S_create callers inlined
  954. // calling 4.0.x+ _S_create.
  955. __p->_M_set_sharable();
  956. return __p;
  957. }
  958. template<typename _CharT, typename _Traits, typename _Alloc>
  959. _CharT*
  960. basic_string<_CharT, _Traits, _Alloc>::_Rep::
  961. _M_clone(const _Alloc& __alloc, size_type __res)
  962. {
  963. // Requested capacity of the clone.
  964. const size_type __requested_cap = this->_M_length + __res;
  965. _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity,
  966. __alloc);
  967. if (this->_M_length)
  968. _M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length);
  969. __r->_M_set_length_and_sharable(this->_M_length);
  970. return __r->_M_refdata();
  971. }
  972. template<typename _CharT, typename _Traits, typename _Alloc>
  973. void
  974. basic_string<_CharT, _Traits, _Alloc>::
  975. resize(size_type __n, _CharT __c)
  976. {
  977. const size_type __size = this->size();
  978. _M_check_length(__size, __n, "basic_string::resize");
  979. if (__size < __n)
  980. this->append(__n - __size, __c);
  981. else if (__n < __size)
  982. this->erase(__n);
  983. // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
  984. }
  985. template<typename _CharT, typename _Traits, typename _Alloc>
  986. template<typename _InputIterator>
  987. basic_string<_CharT, _Traits, _Alloc>&
  988. basic_string<_CharT, _Traits, _Alloc>::
  989. _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
  990. _InputIterator __k2, __false_type)
  991. {
  992. const basic_string __s(__k1, __k2);
  993. const size_type __n1 = __i2 - __i1;
  994. _M_check_length(__n1, __s.size(), "basic_string::_M_replace_dispatch");
  995. return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
  996. __s.size());
  997. }
  998. template<typename _CharT, typename _Traits, typename _Alloc>
  999. basic_string<_CharT, _Traits, _Alloc>&
  1000. basic_string<_CharT, _Traits, _Alloc>::
  1001. _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
  1002. _CharT __c)
  1003. {
  1004. _M_check_length(__n1, __n2, "basic_string::_M_replace_aux");
  1005. _M_mutate(__pos1, __n1, __n2);
  1006. if (__n2)
  1007. _M_assign(_M_data() + __pos1, __n2, __c);
  1008. return *this;
  1009. }
  1010. template<typename _CharT, typename _Traits, typename _Alloc>
  1011. basic_string<_CharT, _Traits, _Alloc>&
  1012. basic_string<_CharT, _Traits, _Alloc>::
  1013. _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
  1014. size_type __n2)
  1015. {
  1016. _M_mutate(__pos1, __n1, __n2);
  1017. if (__n2)
  1018. _M_copy(_M_data() + __pos1, __s, __n2);
  1019. return *this;
  1020. }
  1021. template<typename _CharT, typename _Traits, typename _Alloc>
  1022. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1023. basic_string<_CharT, _Traits, _Alloc>::
  1024. copy(_CharT* __s, size_type __n, size_type __pos) const
  1025. {
  1026. _M_check(__pos, "basic_string::copy");
  1027. __n = _M_limit(__pos, __n);
  1028. __glibcxx_requires_string_len(__s, __n);
  1029. if (__n)
  1030. _M_copy(__s, _M_data() + __pos, __n);
  1031. // 21.3.5.7 par 3: do not append null. (good.)
  1032. return __n;
  1033. }
  1034. #endif // !_GLIBCXX_USE_CXX11_ABI
  1035. template<typename _CharT, typename _Traits, typename _Alloc>
  1036. basic_string<_CharT, _Traits, _Alloc>
  1037. operator+(const _CharT* __lhs,
  1038. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  1039. {
  1040. __glibcxx_requires_string(__lhs);
  1041. typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
  1042. typedef typename __string_type::size_type __size_type;
  1043. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  1044. rebind<_CharT>::other _Char_alloc_type;
  1045. typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits;
  1046. const __size_type __len = _Traits::length(__lhs);
  1047. __string_type __str(_Alloc_traits::_S_select_on_copy(
  1048. __rhs.get_allocator()));
  1049. __str.reserve(__len + __rhs.size());
  1050. __str.append(__lhs, __len);
  1051. __str.append(__rhs);
  1052. return __str;
  1053. }
  1054. template<typename _CharT, typename _Traits, typename _Alloc>
  1055. basic_string<_CharT, _Traits, _Alloc>
  1056. operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  1057. {
  1058. typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
  1059. typedef typename __string_type::size_type __size_type;
  1060. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  1061. rebind<_CharT>::other _Char_alloc_type;
  1062. typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits;
  1063. __string_type __str(_Alloc_traits::_S_select_on_copy(
  1064. __rhs.get_allocator()));
  1065. const __size_type __len = __rhs.size();
  1066. __str.reserve(__len + 1);
  1067. __str.append(__size_type(1), __lhs);
  1068. __str.append(__rhs);
  1069. return __str;
  1070. }
  1071. template<typename _CharT, typename _Traits, typename _Alloc>
  1072. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1073. basic_string<_CharT, _Traits, _Alloc>::
  1074. find(const _CharT* __s, size_type __pos, size_type __n) const
  1075. _GLIBCXX_NOEXCEPT
  1076. {
  1077. __glibcxx_requires_string_len(__s, __n);
  1078. const size_type __size = this->size();
  1079. if (__n == 0)
  1080. return __pos <= __size ? __pos : npos;
  1081. if (__pos >= __size)
  1082. return npos;
  1083. const _CharT __elem0 = __s[0];
  1084. const _CharT* const __data = data();
  1085. const _CharT* __first = __data + __pos;
  1086. const _CharT* const __last = __data + __size;
  1087. size_type __len = __size - __pos;
  1088. while (__len >= __n)
  1089. {
  1090. // Find the first occurrence of __elem0:
  1091. __first = traits_type::find(__first, __len - __n + 1, __elem0);
  1092. if (!__first)
  1093. return npos;
  1094. // Compare the full strings from the first occurrence of __elem0.
  1095. // We already know that __first[0] == __s[0] but compare them again
  1096. // anyway because __s is probably aligned, which helps memcmp.
  1097. if (traits_type::compare(__first, __s, __n) == 0)
  1098. return __first - __data;
  1099. __len = __last - ++__first;
  1100. }
  1101. return npos;
  1102. }
  1103. template<typename _CharT, typename _Traits, typename _Alloc>
  1104. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1105. basic_string<_CharT, _Traits, _Alloc>::
  1106. find(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
  1107. {
  1108. size_type __ret = npos;
  1109. const size_type __size = this->size();
  1110. if (__pos < __size)
  1111. {
  1112. const _CharT* __data = _M_data();
  1113. const size_type __n = __size - __pos;
  1114. const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
  1115. if (__p)
  1116. __ret = __p - __data;
  1117. }
  1118. return __ret;
  1119. }
  1120. template<typename _CharT, typename _Traits, typename _Alloc>
  1121. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1122. basic_string<_CharT, _Traits, _Alloc>::
  1123. rfind(const _CharT* __s, size_type __pos, size_type __n) const
  1124. _GLIBCXX_NOEXCEPT
  1125. {
  1126. __glibcxx_requires_string_len(__s, __n);
  1127. const size_type __size = this->size();
  1128. if (__n <= __size)
  1129. {
  1130. __pos = std::min(size_type(__size - __n), __pos);
  1131. const _CharT* __data = _M_data();
  1132. do
  1133. {
  1134. if (traits_type::compare(__data + __pos, __s, __n) == 0)
  1135. return __pos;
  1136. }
  1137. while (__pos-- > 0);
  1138. }
  1139. return npos;
  1140. }
  1141. template<typename _CharT, typename _Traits, typename _Alloc>
  1142. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1143. basic_string<_CharT, _Traits, _Alloc>::
  1144. rfind(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
  1145. {
  1146. size_type __size = this->size();
  1147. if (__size)
  1148. {
  1149. if (--__size > __pos)
  1150. __size = __pos;
  1151. for (++__size; __size-- > 0; )
  1152. if (traits_type::eq(_M_data()[__size], __c))
  1153. return __size;
  1154. }
  1155. return npos;
  1156. }
  1157. template<typename _CharT, typename _Traits, typename _Alloc>
  1158. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1159. basic_string<_CharT, _Traits, _Alloc>::
  1160. find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
  1161. _GLIBCXX_NOEXCEPT
  1162. {
  1163. __glibcxx_requires_string_len(__s, __n);
  1164. for (; __n && __pos < this->size(); ++__pos)
  1165. {
  1166. const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
  1167. if (__p)
  1168. return __pos;
  1169. }
  1170. return npos;
  1171. }
  1172. template<typename _CharT, typename _Traits, typename _Alloc>
  1173. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1174. basic_string<_CharT, _Traits, _Alloc>::
  1175. find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
  1176. _GLIBCXX_NOEXCEPT
  1177. {
  1178. __glibcxx_requires_string_len(__s, __n);
  1179. size_type __size = this->size();
  1180. if (__size && __n)
  1181. {
  1182. if (--__size > __pos)
  1183. __size = __pos;
  1184. do
  1185. {
  1186. if (traits_type::find(__s, __n, _M_data()[__size]))
  1187. return __size;
  1188. }
  1189. while (__size-- != 0);
  1190. }
  1191. return npos;
  1192. }
  1193. template<typename _CharT, typename _Traits, typename _Alloc>
  1194. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1195. basic_string<_CharT, _Traits, _Alloc>::
  1196. find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
  1197. _GLIBCXX_NOEXCEPT
  1198. {
  1199. __glibcxx_requires_string_len(__s, __n);
  1200. for (; __pos < this->size(); ++__pos)
  1201. if (!traits_type::find(__s, __n, _M_data()[__pos]))
  1202. return __pos;
  1203. return npos;
  1204. }
  1205. template<typename _CharT, typename _Traits, typename _Alloc>
  1206. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1207. basic_string<_CharT, _Traits, _Alloc>::
  1208. find_first_not_of(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
  1209. {
  1210. for (; __pos < this->size(); ++__pos)
  1211. if (!traits_type::eq(_M_data()[__pos], __c))
  1212. return __pos;
  1213. return npos;
  1214. }
  1215. template<typename _CharT, typename _Traits, typename _Alloc>
  1216. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1217. basic_string<_CharT, _Traits, _Alloc>::
  1218. find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
  1219. _GLIBCXX_NOEXCEPT
  1220. {
  1221. __glibcxx_requires_string_len(__s, __n);
  1222. size_type __size = this->size();
  1223. if (__size)
  1224. {
  1225. if (--__size > __pos)
  1226. __size = __pos;
  1227. do
  1228. {
  1229. if (!traits_type::find(__s, __n, _M_data()[__size]))
  1230. return __size;
  1231. }
  1232. while (__size--);
  1233. }
  1234. return npos;
  1235. }
  1236. template<typename _CharT, typename _Traits, typename _Alloc>
  1237. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1238. basic_string<_CharT, _Traits, _Alloc>::
  1239. find_last_not_of(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
  1240. {
  1241. size_type __size = this->size();
  1242. if (__size)
  1243. {
  1244. if (--__size > __pos)
  1245. __size = __pos;
  1246. do
  1247. {
  1248. if (!traits_type::eq(_M_data()[__size], __c))
  1249. return __size;
  1250. }
  1251. while (__size--);
  1252. }
  1253. return npos;
  1254. }
  1255. template<typename _CharT, typename _Traits, typename _Alloc>
  1256. int
  1257. basic_string<_CharT, _Traits, _Alloc>::
  1258. compare(size_type __pos, size_type __n, const basic_string& __str) const
  1259. {
  1260. _M_check(__pos, "basic_string::compare");
  1261. __n = _M_limit(__pos, __n);
  1262. const size_type __osize = __str.size();
  1263. const size_type __len = std::min(__n, __osize);
  1264. int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
  1265. if (!__r)
  1266. __r = _S_compare(__n, __osize);
  1267. return __r;
  1268. }
  1269. template<typename _CharT, typename _Traits, typename _Alloc>
  1270. int
  1271. basic_string<_CharT, _Traits, _Alloc>::
  1272. compare(size_type __pos1, size_type __n1, const basic_string& __str,
  1273. size_type __pos2, size_type __n2) const
  1274. {
  1275. _M_check(__pos1, "basic_string::compare");
  1276. __str._M_check(__pos2, "basic_string::compare");
  1277. __n1 = _M_limit(__pos1, __n1);
  1278. __n2 = __str._M_limit(__pos2, __n2);
  1279. const size_type __len = std::min(__n1, __n2);
  1280. int __r = traits_type::compare(_M_data() + __pos1,
  1281. __str.data() + __pos2, __len);
  1282. if (!__r)
  1283. __r = _S_compare(__n1, __n2);
  1284. return __r;
  1285. }
  1286. template<typename _CharT, typename _Traits, typename _Alloc>
  1287. int
  1288. basic_string<_CharT, _Traits, _Alloc>::
  1289. compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT
  1290. {
  1291. __glibcxx_requires_string(__s);
  1292. const size_type __size = this->size();
  1293. const size_type __osize = traits_type::length(__s);
  1294. const size_type __len = std::min(__size, __osize);
  1295. int __r = traits_type::compare(_M_data(), __s, __len);
  1296. if (!__r)
  1297. __r = _S_compare(__size, __osize);
  1298. return __r;
  1299. }
  1300. template<typename _CharT, typename _Traits, typename _Alloc>
  1301. int
  1302. basic_string <_CharT, _Traits, _Alloc>::
  1303. compare(size_type __pos, size_type __n1, const _CharT* __s) const
  1304. {
  1305. __glibcxx_requires_string(__s);
  1306. _M_check(__pos, "basic_string::compare");
  1307. __n1 = _M_limit(__pos, __n1);
  1308. const size_type __osize = traits_type::length(__s);
  1309. const size_type __len = std::min(__n1, __osize);
  1310. int __r = traits_type::compare(_M_data() + __pos, __s, __len);
  1311. if (!__r)
  1312. __r = _S_compare(__n1, __osize);
  1313. return __r;
  1314. }
  1315. template<typename _CharT, typename _Traits, typename _Alloc>
  1316. int
  1317. basic_string <_CharT, _Traits, _Alloc>::
  1318. compare(size_type __pos, size_type __n1, const _CharT* __s,
  1319. size_type __n2) const
  1320. {
  1321. __glibcxx_requires_string_len(__s, __n2);
  1322. _M_check(__pos, "basic_string::compare");
  1323. __n1 = _M_limit(__pos, __n1);
  1324. const size_type __len = std::min(__n1, __n2);
  1325. int __r = traits_type::compare(_M_data() + __pos, __s, __len);
  1326. if (!__r)
  1327. __r = _S_compare(__n1, __n2);
  1328. return __r;
  1329. }
  1330. // 21.3.7.9 basic_string::getline and operators
  1331. template<typename _CharT, typename _Traits, typename _Alloc>
  1332. basic_istream<_CharT, _Traits>&
  1333. operator>>(basic_istream<_CharT, _Traits>& __in,
  1334. basic_string<_CharT, _Traits, _Alloc>& __str)
  1335. {
  1336. typedef basic_istream<_CharT, _Traits> __istream_type;
  1337. typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
  1338. typedef typename __istream_type::ios_base __ios_base;
  1339. typedef typename __istream_type::int_type __int_type;
  1340. typedef typename __string_type::size_type __size_type;
  1341. typedef ctype<_CharT> __ctype_type;
  1342. typedef typename __ctype_type::ctype_base __ctype_base;
  1343. __size_type __extracted = 0;
  1344. typename __ios_base::iostate __err = __ios_base::goodbit;
  1345. typename __istream_type::sentry __cerb(__in, false);
  1346. if (__cerb)
  1347. {
  1348. __try
  1349. {
  1350. // Avoid reallocation for common case.
  1351. __str.erase();
  1352. _CharT __buf[128];
  1353. __size_type __len = 0;
  1354. const streamsize __w = __in.width();
  1355. const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
  1356. : __str.max_size();
  1357. const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
  1358. const __int_type __eof = _Traits::eof();
  1359. __int_type __c = __in.rdbuf()->sgetc();
  1360. while (__extracted < __n
  1361. && !_Traits::eq_int_type(__c, __eof)
  1362. && !__ct.is(__ctype_base::space,
  1363. _Traits::to_char_type(__c)))
  1364. {
  1365. if (__len == sizeof(__buf) / sizeof(_CharT))
  1366. {
  1367. __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
  1368. __len = 0;
  1369. }
  1370. __buf[__len++] = _Traits::to_char_type(__c);
  1371. ++__extracted;
  1372. __c = __in.rdbuf()->snextc();
  1373. }
  1374. __str.append(__buf, __len);
  1375. if (_Traits::eq_int_type(__c, __eof))
  1376. __err |= __ios_base::eofbit;
  1377. __in.width(0);
  1378. }
  1379. __catch(__cxxabiv1::__forced_unwind&)
  1380. {
  1381. __in._M_setstate(__ios_base::badbit);
  1382. __throw_exception_again;
  1383. }
  1384. __catch(...)
  1385. {
  1386. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1387. // 91. Description of operator>> and getline() for string<>
  1388. // might cause endless loop
  1389. __in._M_setstate(__ios_base::badbit);
  1390. }
  1391. }
  1392. // 211. operator>>(istream&, string&) doesn't set failbit
  1393. if (!__extracted)
  1394. __err |= __ios_base::failbit;
  1395. if (__err)
  1396. __in.setstate(__err);
  1397. return __in;
  1398. }
  1399. template<typename _CharT, typename _Traits, typename _Alloc>
  1400. basic_istream<_CharT, _Traits>&
  1401. getline(basic_istream<_CharT, _Traits>& __in,
  1402. basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
  1403. {
  1404. typedef basic_istream<_CharT, _Traits> __istream_type;
  1405. typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
  1406. typedef typename __istream_type::ios_base __ios_base;
  1407. typedef typename __istream_type::int_type __int_type;
  1408. typedef typename __string_type::size_type __size_type;
  1409. __size_type __extracted = 0;
  1410. const __size_type __n = __str.max_size();
  1411. typename __ios_base::iostate __err = __ios_base::goodbit;
  1412. typename __istream_type::sentry __cerb(__in, true);
  1413. if (__cerb)
  1414. {
  1415. __try
  1416. {
  1417. __str.erase();
  1418. const __int_type __idelim = _Traits::to_int_type(__delim);
  1419. const __int_type __eof = _Traits::eof();
  1420. __int_type __c = __in.rdbuf()->sgetc();
  1421. while (__extracted < __n
  1422. && !_Traits::eq_int_type(__c, __eof)
  1423. && !_Traits::eq_int_type(__c, __idelim))
  1424. {
  1425. __str += _Traits::to_char_type(__c);
  1426. ++__extracted;
  1427. __c = __in.rdbuf()->snextc();
  1428. }
  1429. if (_Traits::eq_int_type(__c, __eof))
  1430. __err |= __ios_base::eofbit;
  1431. else if (_Traits::eq_int_type(__c, __idelim))
  1432. {
  1433. ++__extracted;
  1434. __in.rdbuf()->sbumpc();
  1435. }
  1436. else
  1437. __err |= __ios_base::failbit;
  1438. }
  1439. __catch(__cxxabiv1::__forced_unwind&)
  1440. {
  1441. __in._M_setstate(__ios_base::badbit);
  1442. __throw_exception_again;
  1443. }
  1444. __catch(...)
  1445. {
  1446. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1447. // 91. Description of operator>> and getline() for string<>
  1448. // might cause endless loop
  1449. __in._M_setstate(__ios_base::badbit);
  1450. }
  1451. }
  1452. if (!__extracted)
  1453. __err |= __ios_base::failbit;
  1454. if (__err)
  1455. __in.setstate(__err);
  1456. return __in;
  1457. }
  1458. // Inhibit implicit instantiations for required instantiations,
  1459. // which are defined via explicit instantiations elsewhere.
  1460. #if _GLIBCXX_EXTERN_TEMPLATE
  1461. // The explicit instantiation definitions in src/c++11/string-inst.cc and
  1462. // src/c++17/string-inst.cc only instantiate the members required for C++17
  1463. // and earlier standards (so not C++20's starts_with and ends_with).
  1464. // Suppress the explicit instantiation declarations for C++20, so C++20
  1465. // code will implicitly instantiate std::string and std::wstring as needed.
  1466. # if __cplusplus <= 201703L && _GLIBCXX_EXTERN_TEMPLATE > 0
  1467. extern template class basic_string<char>;
  1468. # elif ! _GLIBCXX_USE_CXX11_ABI
  1469. // Still need to prevent implicit instantiation of the COW empty rep,
  1470. // to ensure the definition in libstdc++.so is unique (PR 86138).
  1471. extern template basic_string<char>::size_type
  1472. basic_string<char>::_Rep::_S_empty_rep_storage[];
  1473. # endif
  1474. extern template
  1475. basic_istream<char>&
  1476. operator>>(basic_istream<char>&, string&);
  1477. extern template
  1478. basic_ostream<char>&
  1479. operator<<(basic_ostream<char>&, const string&);
  1480. extern template
  1481. basic_istream<char>&
  1482. getline(basic_istream<char>&, string&, char);
  1483. extern template
  1484. basic_istream<char>&
  1485. getline(basic_istream<char>&, string&);
  1486. #ifdef _GLIBCXX_USE_WCHAR_T
  1487. # if __cplusplus <= 201703L && _GLIBCXX_EXTERN_TEMPLATE > 0
  1488. extern template class basic_string<wchar_t>;
  1489. # elif ! _GLIBCXX_USE_CXX11_ABI
  1490. extern template basic_string<wchar_t>::size_type
  1491. basic_string<wchar_t>::_Rep::_S_empty_rep_storage[];
  1492. # endif
  1493. extern template
  1494. basic_istream<wchar_t>&
  1495. operator>>(basic_istream<wchar_t>&, wstring&);
  1496. extern template
  1497. basic_ostream<wchar_t>&
  1498. operator<<(basic_ostream<wchar_t>&, const wstring&);
  1499. extern template
  1500. basic_istream<wchar_t>&
  1501. getline(basic_istream<wchar_t>&, wstring&, wchar_t);
  1502. extern template
  1503. basic_istream<wchar_t>&
  1504. getline(basic_istream<wchar_t>&, wstring&);
  1505. #endif // _GLIBCXX_USE_WCHAR_T
  1506. #endif // _GLIBCXX_EXTERN_TEMPLATE
  1507. _GLIBCXX_END_NAMESPACE_VERSION
  1508. } // namespace std
  1509. #endif