basic_string.tcc 53 KB

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