basic_string.tcc 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665
  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. // TBD: DPG annotate
  584. template<typename _CharT, typename _Traits, typename _Alloc>
  585. basic_string<_CharT, _Traits, _Alloc>::
  586. basic_string(const _CharT* __s, const _Alloc& __a)
  587. : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) :
  588. __s + npos, __a), __a)
  589. { }
  590. template<typename _CharT, typename _Traits, typename _Alloc>
  591. basic_string<_CharT, _Traits, _Alloc>::
  592. basic_string(size_type __n, _CharT __c, const _Alloc& __a)
  593. : _M_dataplus(_S_construct(__n, __c, __a), __a)
  594. { }
  595. // TBD: DPG annotate
  596. template<typename _CharT, typename _Traits, typename _Alloc>
  597. template<typename _InputIterator>
  598. basic_string<_CharT, _Traits, _Alloc>::
  599. basic_string(_InputIterator __beg, _InputIterator __end, const _Alloc& __a)
  600. : _M_dataplus(_S_construct(__beg, __end, __a), __a)
  601. { }
  602. #if __cplusplus >= 201103L
  603. template<typename _CharT, typename _Traits, typename _Alloc>
  604. basic_string<_CharT, _Traits, _Alloc>::
  605. basic_string(initializer_list<_CharT> __l, const _Alloc& __a)
  606. : _M_dataplus(_S_construct(__l.begin(), __l.end(), __a), __a)
  607. { }
  608. #endif
  609. template<typename _CharT, typename _Traits, typename _Alloc>
  610. basic_string<_CharT, _Traits, _Alloc>&
  611. basic_string<_CharT, _Traits, _Alloc>::
  612. assign(const basic_string& __str)
  613. {
  614. if (_M_rep() != __str._M_rep())
  615. {
  616. // XXX MT
  617. const allocator_type __a = this->get_allocator();
  618. _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());
  619. _M_rep()->_M_dispose(__a);
  620. _M_data(__tmp);
  621. }
  622. return *this;
  623. }
  624. template<typename _CharT, typename _Traits, typename _Alloc>
  625. basic_string<_CharT, _Traits, _Alloc>&
  626. basic_string<_CharT, _Traits, _Alloc>::
  627. assign(const _CharT* __s, size_type __n)
  628. {
  629. __glibcxx_requires_string_len(__s, __n);
  630. _M_check_length(this->size(), __n, "basic_string::assign");
  631. if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
  632. return _M_replace_safe(size_type(0), this->size(), __s, __n);
  633. else
  634. {
  635. // Work in-place.
  636. const size_type __pos = __s - _M_data();
  637. if (__pos >= __n)
  638. _M_copy(_M_data(), __s, __n);
  639. else if (__pos)
  640. _M_move(_M_data(), __s, __n);
  641. _M_rep()->_M_set_length_and_sharable(__n);
  642. return *this;
  643. }
  644. }
  645. template<typename _CharT, typename _Traits, typename _Alloc>
  646. basic_string<_CharT, _Traits, _Alloc>&
  647. basic_string<_CharT, _Traits, _Alloc>::
  648. append(size_type __n, _CharT __c)
  649. {
  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. this->reserve(__len);
  656. _M_assign(_M_data() + this->size(), __n, __c);
  657. _M_rep()->_M_set_length_and_sharable(__len);
  658. }
  659. return *this;
  660. }
  661. template<typename _CharT, typename _Traits, typename _Alloc>
  662. basic_string<_CharT, _Traits, _Alloc>&
  663. basic_string<_CharT, _Traits, _Alloc>::
  664. append(const _CharT* __s, size_type __n)
  665. {
  666. __glibcxx_requires_string_len(__s, __n);
  667. if (__n)
  668. {
  669. _M_check_length(size_type(0), __n, "basic_string::append");
  670. const size_type __len = __n + this->size();
  671. if (__len > this->capacity() || _M_rep()->_M_is_shared())
  672. {
  673. if (_M_disjunct(__s))
  674. this->reserve(__len);
  675. else
  676. {
  677. const size_type __off = __s - _M_data();
  678. this->reserve(__len);
  679. __s = _M_data() + __off;
  680. }
  681. }
  682. _M_copy(_M_data() + this->size(), __s, __n);
  683. _M_rep()->_M_set_length_and_sharable(__len);
  684. }
  685. return *this;
  686. }
  687. template<typename _CharT, typename _Traits, typename _Alloc>
  688. basic_string<_CharT, _Traits, _Alloc>&
  689. basic_string<_CharT, _Traits, _Alloc>::
  690. append(const basic_string& __str)
  691. {
  692. const size_type __size = __str.size();
  693. if (__size)
  694. {
  695. const size_type __len = __size + 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(), __size);
  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. append(const basic_string& __str, size_type __pos, size_type __n)
  707. {
  708. __str._M_check(__pos, "basic_string::append");
  709. __n = __str._M_limit(__pos, __n);
  710. if (__n)
  711. {
  712. const size_type __len = __n + this->size();
  713. if (__len > this->capacity() || _M_rep()->_M_is_shared())
  714. this->reserve(__len);
  715. _M_copy(_M_data() + this->size(), __str._M_data() + __pos, __n);
  716. _M_rep()->_M_set_length_and_sharable(__len);
  717. }
  718. return *this;
  719. }
  720. template<typename _CharT, typename _Traits, typename _Alloc>
  721. basic_string<_CharT, _Traits, _Alloc>&
  722. basic_string<_CharT, _Traits, _Alloc>::
  723. insert(size_type __pos, const _CharT* __s, size_type __n)
  724. {
  725. __glibcxx_requires_string_len(__s, __n);
  726. _M_check(__pos, "basic_string::insert");
  727. _M_check_length(size_type(0), __n, "basic_string::insert");
  728. if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
  729. return _M_replace_safe(__pos, size_type(0), __s, __n);
  730. else
  731. {
  732. // Work in-place.
  733. const size_type __off = __s - _M_data();
  734. _M_mutate(__pos, 0, __n);
  735. __s = _M_data() + __off;
  736. _CharT* __p = _M_data() + __pos;
  737. if (__s + __n <= __p)
  738. _M_copy(__p, __s, __n);
  739. else if (__s >= __p)
  740. _M_copy(__p, __s + __n, __n);
  741. else
  742. {
  743. const size_type __nleft = __p - __s;
  744. _M_copy(__p, __s, __nleft);
  745. _M_copy(__p + __nleft, __p + __n, __n - __nleft);
  746. }
  747. return *this;
  748. }
  749. }
  750. template<typename _CharT, typename _Traits, typename _Alloc>
  751. typename basic_string<_CharT, _Traits, _Alloc>::iterator
  752. basic_string<_CharT, _Traits, _Alloc>::
  753. erase(iterator __first, iterator __last)
  754. {
  755. _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
  756. && __last <= _M_iend());
  757. // NB: This isn't just an optimization (bail out early when
  758. // there is nothing to do, really), it's also a correctness
  759. // issue vs MT, see libstdc++/40518.
  760. const size_type __size = __last - __first;
  761. if (__size)
  762. {
  763. const size_type __pos = __first - _M_ibegin();
  764. _M_mutate(__pos, __size, size_type(0));
  765. _M_rep()->_M_set_leaked();
  766. return iterator(_M_data() + __pos);
  767. }
  768. else
  769. return __first;
  770. }
  771. template<typename _CharT, typename _Traits, typename _Alloc>
  772. basic_string<_CharT, _Traits, _Alloc>&
  773. basic_string<_CharT, _Traits, _Alloc>::
  774. replace(size_type __pos, size_type __n1, const _CharT* __s,
  775. size_type __n2)
  776. {
  777. __glibcxx_requires_string_len(__s, __n2);
  778. _M_check(__pos, "basic_string::replace");
  779. __n1 = _M_limit(__pos, __n1);
  780. _M_check_length(__n1, __n2, "basic_string::replace");
  781. bool __left;
  782. if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
  783. return _M_replace_safe(__pos, __n1, __s, __n2);
  784. else if ((__left = __s + __n2 <= _M_data() + __pos)
  785. || _M_data() + __pos + __n1 <= __s)
  786. {
  787. // Work in-place: non-overlapping case.
  788. size_type __off = __s - _M_data();
  789. __left ? __off : (__off += __n2 - __n1);
  790. _M_mutate(__pos, __n1, __n2);
  791. _M_copy(_M_data() + __pos, _M_data() + __off, __n2);
  792. return *this;
  793. }
  794. else
  795. {
  796. // Todo: overlapping case.
  797. const basic_string __tmp(__s, __n2);
  798. return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2);
  799. }
  800. }
  801. template<typename _CharT, typename _Traits, typename _Alloc>
  802. void
  803. basic_string<_CharT, _Traits, _Alloc>::_Rep::
  804. _M_destroy(const _Alloc& __a) throw ()
  805. {
  806. const size_type __size = sizeof(_Rep_base) +
  807. (this->_M_capacity + 1) * sizeof(_CharT);
  808. _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);
  809. }
  810. template<typename _CharT, typename _Traits, typename _Alloc>
  811. void
  812. basic_string<_CharT, _Traits, _Alloc>::
  813. _M_leak_hard()
  814. {
  815. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  816. if (_M_rep() == &_S_empty_rep())
  817. return;
  818. #endif
  819. if (_M_rep()->_M_is_shared())
  820. _M_mutate(0, 0, 0);
  821. _M_rep()->_M_set_leaked();
  822. }
  823. template<typename _CharT, typename _Traits, typename _Alloc>
  824. void
  825. basic_string<_CharT, _Traits, _Alloc>::
  826. _M_mutate(size_type __pos, size_type __len1, size_type __len2)
  827. {
  828. const size_type __old_size = this->size();
  829. const size_type __new_size = __old_size + __len2 - __len1;
  830. const size_type __how_much = __old_size - __pos - __len1;
  831. if (__new_size > this->capacity() || _M_rep()->_M_is_shared())
  832. {
  833. // Must reallocate.
  834. const allocator_type __a = get_allocator();
  835. _Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a);
  836. if (__pos)
  837. _M_copy(__r->_M_refdata(), _M_data(), __pos);
  838. if (__how_much)
  839. _M_copy(__r->_M_refdata() + __pos + __len2,
  840. _M_data() + __pos + __len1, __how_much);
  841. _M_rep()->_M_dispose(__a);
  842. _M_data(__r->_M_refdata());
  843. }
  844. else if (__how_much && __len1 != __len2)
  845. {
  846. // Work in-place.
  847. _M_move(_M_data() + __pos + __len2,
  848. _M_data() + __pos + __len1, __how_much);
  849. }
  850. _M_rep()->_M_set_length_and_sharable(__new_size);
  851. }
  852. template<typename _CharT, typename _Traits, typename _Alloc>
  853. void
  854. basic_string<_CharT, _Traits, _Alloc>::
  855. reserve(size_type __res)
  856. {
  857. if (__res != this->capacity() || _M_rep()->_M_is_shared())
  858. {
  859. // Make sure we don't shrink below the current size
  860. if (__res < this->size())
  861. __res = this->size();
  862. const allocator_type __a = get_allocator();
  863. _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size());
  864. _M_rep()->_M_dispose(__a);
  865. _M_data(__tmp);
  866. }
  867. }
  868. template<typename _CharT, typename _Traits, typename _Alloc>
  869. void
  870. basic_string<_CharT, _Traits, _Alloc>::
  871. swap(basic_string& __s)
  872. _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value)
  873. {
  874. if (_M_rep()->_M_is_leaked())
  875. _M_rep()->_M_set_sharable();
  876. if (__s._M_rep()->_M_is_leaked())
  877. __s._M_rep()->_M_set_sharable();
  878. if (this->get_allocator() == __s.get_allocator())
  879. {
  880. _CharT* __tmp = _M_data();
  881. _M_data(__s._M_data());
  882. __s._M_data(__tmp);
  883. }
  884. // The code below can usually be optimized away.
  885. else
  886. {
  887. const basic_string __tmp1(_M_ibegin(), _M_iend(),
  888. __s.get_allocator());
  889. const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
  890. this->get_allocator());
  891. *this = __tmp2;
  892. __s = __tmp1;
  893. }
  894. }
  895. template<typename _CharT, typename _Traits, typename _Alloc>
  896. typename basic_string<_CharT, _Traits, _Alloc>::_Rep*
  897. basic_string<_CharT, _Traits, _Alloc>::_Rep::
  898. _S_create(size_type __capacity, size_type __old_capacity,
  899. const _Alloc& __alloc)
  900. {
  901. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  902. // 83. String::npos vs. string::max_size()
  903. if (__capacity > _S_max_size)
  904. __throw_length_error(__N("basic_string::_S_create"));
  905. // The standard places no restriction on allocating more memory
  906. // than is strictly needed within this layer at the moment or as
  907. // requested by an explicit application call to reserve().
  908. // Many malloc implementations perform quite poorly when an
  909. // application attempts to allocate memory in a stepwise fashion
  910. // growing each allocation size by only 1 char. Additionally,
  911. // it makes little sense to allocate less linear memory than the
  912. // natural blocking size of the malloc implementation.
  913. // Unfortunately, we would need a somewhat low-level calculation
  914. // with tuned parameters to get this perfect for any particular
  915. // malloc implementation. Fortunately, generalizations about
  916. // common features seen among implementations seems to suffice.
  917. // __pagesize need not match the actual VM page size for good
  918. // results in practice, thus we pick a common value on the low
  919. // side. __malloc_header_size is an estimate of the amount of
  920. // overhead per memory allocation (in practice seen N * sizeof
  921. // (void*) where N is 0, 2 or 4). According to folklore,
  922. // picking this value on the high side is better than
  923. // low-balling it (especially when this algorithm is used with
  924. // malloc implementations that allocate memory blocks rounded up
  925. // to a size which is a power of 2).
  926. const size_type __pagesize = 4096;
  927. const size_type __malloc_header_size = 4 * sizeof(void*);
  928. // The below implements an exponential growth policy, necessary to
  929. // meet amortized linear time requirements of the library: see
  930. // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
  931. // It's active for allocations requiring an amount of memory above
  932. // system pagesize. This is consistent with the requirements of the
  933. // standard: http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html
  934. if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
  935. __capacity = 2 * __old_capacity;
  936. // NB: Need an array of char_type[__capacity], plus a terminating
  937. // null char_type() element, plus enough for the _Rep data structure.
  938. // Whew. Seemingly so needy, yet so elemental.
  939. size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
  940. const size_type __adj_size = __size + __malloc_header_size;
  941. if (__adj_size > __pagesize && __capacity > __old_capacity)
  942. {
  943. const size_type __extra = __pagesize - __adj_size % __pagesize;
  944. __capacity += __extra / sizeof(_CharT);
  945. // Never allocate a string bigger than _S_max_size.
  946. if (__capacity > _S_max_size)
  947. __capacity = _S_max_size;
  948. __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
  949. }
  950. // NB: Might throw, but no worries about a leak, mate: _Rep()
  951. // does not throw.
  952. void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
  953. _Rep *__p = new (__place) _Rep;
  954. __p->_M_capacity = __capacity;
  955. // ABI compatibility - 3.4.x set in _S_create both
  956. // _M_refcount and _M_length. All callers of _S_create
  957. // in basic_string.tcc then set just _M_length.
  958. // In 4.0.x and later both _M_refcount and _M_length
  959. // are initialized in the callers, unfortunately we can
  960. // have 3.4.x compiled code with _S_create callers inlined
  961. // calling 4.0.x+ _S_create.
  962. __p->_M_set_sharable();
  963. return __p;
  964. }
  965. template<typename _CharT, typename _Traits, typename _Alloc>
  966. _CharT*
  967. basic_string<_CharT, _Traits, _Alloc>::_Rep::
  968. _M_clone(const _Alloc& __alloc, size_type __res)
  969. {
  970. // Requested capacity of the clone.
  971. const size_type __requested_cap = this->_M_length + __res;
  972. _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity,
  973. __alloc);
  974. if (this->_M_length)
  975. _M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length);
  976. __r->_M_set_length_and_sharable(this->_M_length);
  977. return __r->_M_refdata();
  978. }
  979. template<typename _CharT, typename _Traits, typename _Alloc>
  980. void
  981. basic_string<_CharT, _Traits, _Alloc>::
  982. resize(size_type __n, _CharT __c)
  983. {
  984. const size_type __size = this->size();
  985. _M_check_length(__size, __n, "basic_string::resize");
  986. if (__size < __n)
  987. this->append(__n - __size, __c);
  988. else if (__n < __size)
  989. this->erase(__n);
  990. // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
  991. }
  992. template<typename _CharT, typename _Traits, typename _Alloc>
  993. template<typename _InputIterator>
  994. basic_string<_CharT, _Traits, _Alloc>&
  995. basic_string<_CharT, _Traits, _Alloc>::
  996. _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
  997. _InputIterator __k2, __false_type)
  998. {
  999. const basic_string __s(__k1, __k2);
  1000. const size_type __n1 = __i2 - __i1;
  1001. _M_check_length(__n1, __s.size(), "basic_string::_M_replace_dispatch");
  1002. return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
  1003. __s.size());
  1004. }
  1005. template<typename _CharT, typename _Traits, typename _Alloc>
  1006. basic_string<_CharT, _Traits, _Alloc>&
  1007. basic_string<_CharT, _Traits, _Alloc>::
  1008. _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
  1009. _CharT __c)
  1010. {
  1011. _M_check_length(__n1, __n2, "basic_string::_M_replace_aux");
  1012. _M_mutate(__pos1, __n1, __n2);
  1013. if (__n2)
  1014. _M_assign(_M_data() + __pos1, __n2, __c);
  1015. return *this;
  1016. }
  1017. template<typename _CharT, typename _Traits, typename _Alloc>
  1018. basic_string<_CharT, _Traits, _Alloc>&
  1019. basic_string<_CharT, _Traits, _Alloc>::
  1020. _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
  1021. size_type __n2)
  1022. {
  1023. _M_mutate(__pos1, __n1, __n2);
  1024. if (__n2)
  1025. _M_copy(_M_data() + __pos1, __s, __n2);
  1026. return *this;
  1027. }
  1028. template<typename _CharT, typename _Traits, typename _Alloc>
  1029. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1030. basic_string<_CharT, _Traits, _Alloc>::
  1031. copy(_CharT* __s, size_type __n, size_type __pos) const
  1032. {
  1033. _M_check(__pos, "basic_string::copy");
  1034. __n = _M_limit(__pos, __n);
  1035. __glibcxx_requires_string_len(__s, __n);
  1036. if (__n)
  1037. _M_copy(__s, _M_data() + __pos, __n);
  1038. // 21.3.5.7 par 3: do not append null. (good.)
  1039. return __n;
  1040. }
  1041. #endif // !_GLIBCXX_USE_CXX11_ABI
  1042. template<typename _CharT, typename _Traits, typename _Alloc>
  1043. basic_string<_CharT, _Traits, _Alloc>
  1044. operator+(const _CharT* __lhs,
  1045. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  1046. {
  1047. __glibcxx_requires_string(__lhs);
  1048. typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
  1049. typedef typename __string_type::size_type __size_type;
  1050. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  1051. rebind<_CharT>::other _Char_alloc_type;
  1052. typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits;
  1053. const __size_type __len = _Traits::length(__lhs);
  1054. __string_type __str(_Alloc_traits::_S_select_on_copy(
  1055. __rhs.get_allocator()));
  1056. __str.reserve(__len + __rhs.size());
  1057. __str.append(__lhs, __len);
  1058. __str.append(__rhs);
  1059. return __str;
  1060. }
  1061. template<typename _CharT, typename _Traits, typename _Alloc>
  1062. basic_string<_CharT, _Traits, _Alloc>
  1063. operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  1064. {
  1065. typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
  1066. typedef typename __string_type::size_type __size_type;
  1067. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  1068. rebind<_CharT>::other _Char_alloc_type;
  1069. typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits;
  1070. __string_type __str(_Alloc_traits::_S_select_on_copy(
  1071. __rhs.get_allocator()));
  1072. const __size_type __len = __rhs.size();
  1073. __str.reserve(__len + 1);
  1074. __str.append(__size_type(1), __lhs);
  1075. __str.append(__rhs);
  1076. return __str;
  1077. }
  1078. template<typename _CharT, typename _Traits, typename _Alloc>
  1079. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1080. basic_string<_CharT, _Traits, _Alloc>::
  1081. find(const _CharT* __s, size_type __pos, size_type __n) const
  1082. _GLIBCXX_NOEXCEPT
  1083. {
  1084. __glibcxx_requires_string_len(__s, __n);
  1085. const size_type __size = this->size();
  1086. if (__n == 0)
  1087. return __pos <= __size ? __pos : npos;
  1088. if (__pos >= __size)
  1089. return npos;
  1090. const _CharT __elem0 = __s[0];
  1091. const _CharT* const __data = data();
  1092. const _CharT* __first = __data + __pos;
  1093. const _CharT* const __last = __data + __size;
  1094. size_type __len = __size - __pos;
  1095. while (__len >= __n)
  1096. {
  1097. // Find the first occurrence of __elem0:
  1098. __first = traits_type::find(__first, __len - __n + 1, __elem0);
  1099. if (!__first)
  1100. return npos;
  1101. // Compare the full strings from the first occurrence of __elem0.
  1102. // We already know that __first[0] == __s[0] but compare them again
  1103. // anyway because __s is probably aligned, which helps memcmp.
  1104. if (traits_type::compare(__first, __s, __n) == 0)
  1105. return __first - __data;
  1106. __len = __last - ++__first;
  1107. }
  1108. return npos;
  1109. }
  1110. template<typename _CharT, typename _Traits, typename _Alloc>
  1111. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1112. basic_string<_CharT, _Traits, _Alloc>::
  1113. find(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
  1114. {
  1115. size_type __ret = npos;
  1116. const size_type __size = this->size();
  1117. if (__pos < __size)
  1118. {
  1119. const _CharT* __data = _M_data();
  1120. const size_type __n = __size - __pos;
  1121. const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
  1122. if (__p)
  1123. __ret = __p - __data;
  1124. }
  1125. return __ret;
  1126. }
  1127. template<typename _CharT, typename _Traits, typename _Alloc>
  1128. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1129. basic_string<_CharT, _Traits, _Alloc>::
  1130. rfind(const _CharT* __s, size_type __pos, size_type __n) const
  1131. _GLIBCXX_NOEXCEPT
  1132. {
  1133. __glibcxx_requires_string_len(__s, __n);
  1134. const size_type __size = this->size();
  1135. if (__n <= __size)
  1136. {
  1137. __pos = std::min(size_type(__size - __n), __pos);
  1138. const _CharT* __data = _M_data();
  1139. do
  1140. {
  1141. if (traits_type::compare(__data + __pos, __s, __n) == 0)
  1142. return __pos;
  1143. }
  1144. while (__pos-- > 0);
  1145. }
  1146. return npos;
  1147. }
  1148. template<typename _CharT, typename _Traits, typename _Alloc>
  1149. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1150. basic_string<_CharT, _Traits, _Alloc>::
  1151. rfind(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
  1152. {
  1153. size_type __size = this->size();
  1154. if (__size)
  1155. {
  1156. if (--__size > __pos)
  1157. __size = __pos;
  1158. for (++__size; __size-- > 0; )
  1159. if (traits_type::eq(_M_data()[__size], __c))
  1160. return __size;
  1161. }
  1162. return npos;
  1163. }
  1164. template<typename _CharT, typename _Traits, typename _Alloc>
  1165. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1166. basic_string<_CharT, _Traits, _Alloc>::
  1167. find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
  1168. _GLIBCXX_NOEXCEPT
  1169. {
  1170. __glibcxx_requires_string_len(__s, __n);
  1171. for (; __n && __pos < this->size(); ++__pos)
  1172. {
  1173. const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
  1174. if (__p)
  1175. return __pos;
  1176. }
  1177. return npos;
  1178. }
  1179. template<typename _CharT, typename _Traits, typename _Alloc>
  1180. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1181. basic_string<_CharT, _Traits, _Alloc>::
  1182. find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
  1183. _GLIBCXX_NOEXCEPT
  1184. {
  1185. __glibcxx_requires_string_len(__s, __n);
  1186. size_type __size = this->size();
  1187. if (__size && __n)
  1188. {
  1189. if (--__size > __pos)
  1190. __size = __pos;
  1191. do
  1192. {
  1193. if (traits_type::find(__s, __n, _M_data()[__size]))
  1194. return __size;
  1195. }
  1196. while (__size-- != 0);
  1197. }
  1198. return npos;
  1199. }
  1200. template<typename _CharT, typename _Traits, typename _Alloc>
  1201. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1202. basic_string<_CharT, _Traits, _Alloc>::
  1203. find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
  1204. _GLIBCXX_NOEXCEPT
  1205. {
  1206. __glibcxx_requires_string_len(__s, __n);
  1207. for (; __pos < this->size(); ++__pos)
  1208. if (!traits_type::find(__s, __n, _M_data()[__pos]))
  1209. return __pos;
  1210. return npos;
  1211. }
  1212. template<typename _CharT, typename _Traits, typename _Alloc>
  1213. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1214. basic_string<_CharT, _Traits, _Alloc>::
  1215. find_first_not_of(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
  1216. {
  1217. for (; __pos < this->size(); ++__pos)
  1218. if (!traits_type::eq(_M_data()[__pos], __c))
  1219. return __pos;
  1220. return npos;
  1221. }
  1222. template<typename _CharT, typename _Traits, typename _Alloc>
  1223. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1224. basic_string<_CharT, _Traits, _Alloc>::
  1225. find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
  1226. _GLIBCXX_NOEXCEPT
  1227. {
  1228. __glibcxx_requires_string_len(__s, __n);
  1229. size_type __size = this->size();
  1230. if (__size)
  1231. {
  1232. if (--__size > __pos)
  1233. __size = __pos;
  1234. do
  1235. {
  1236. if (!traits_type::find(__s, __n, _M_data()[__size]))
  1237. return __size;
  1238. }
  1239. while (__size--);
  1240. }
  1241. return npos;
  1242. }
  1243. template<typename _CharT, typename _Traits, typename _Alloc>
  1244. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1245. basic_string<_CharT, _Traits, _Alloc>::
  1246. find_last_not_of(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
  1247. {
  1248. size_type __size = this->size();
  1249. if (__size)
  1250. {
  1251. if (--__size > __pos)
  1252. __size = __pos;
  1253. do
  1254. {
  1255. if (!traits_type::eq(_M_data()[__size], __c))
  1256. return __size;
  1257. }
  1258. while (__size--);
  1259. }
  1260. return npos;
  1261. }
  1262. template<typename _CharT, typename _Traits, typename _Alloc>
  1263. int
  1264. basic_string<_CharT, _Traits, _Alloc>::
  1265. compare(size_type __pos, size_type __n, const basic_string& __str) const
  1266. {
  1267. _M_check(__pos, "basic_string::compare");
  1268. __n = _M_limit(__pos, __n);
  1269. const size_type __osize = __str.size();
  1270. const size_type __len = std::min(__n, __osize);
  1271. int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
  1272. if (!__r)
  1273. __r = _S_compare(__n, __osize);
  1274. return __r;
  1275. }
  1276. template<typename _CharT, typename _Traits, typename _Alloc>
  1277. int
  1278. basic_string<_CharT, _Traits, _Alloc>::
  1279. compare(size_type __pos1, size_type __n1, const basic_string& __str,
  1280. size_type __pos2, size_type __n2) const
  1281. {
  1282. _M_check(__pos1, "basic_string::compare");
  1283. __str._M_check(__pos2, "basic_string::compare");
  1284. __n1 = _M_limit(__pos1, __n1);
  1285. __n2 = __str._M_limit(__pos2, __n2);
  1286. const size_type __len = std::min(__n1, __n2);
  1287. int __r = traits_type::compare(_M_data() + __pos1,
  1288. __str.data() + __pos2, __len);
  1289. if (!__r)
  1290. __r = _S_compare(__n1, __n2);
  1291. return __r;
  1292. }
  1293. template<typename _CharT, typename _Traits, typename _Alloc>
  1294. int
  1295. basic_string<_CharT, _Traits, _Alloc>::
  1296. compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT
  1297. {
  1298. __glibcxx_requires_string(__s);
  1299. const size_type __size = this->size();
  1300. const size_type __osize = traits_type::length(__s);
  1301. const size_type __len = std::min(__size, __osize);
  1302. int __r = traits_type::compare(_M_data(), __s, __len);
  1303. if (!__r)
  1304. __r = _S_compare(__size, __osize);
  1305. return __r;
  1306. }
  1307. template<typename _CharT, typename _Traits, typename _Alloc>
  1308. int
  1309. basic_string <_CharT, _Traits, _Alloc>::
  1310. compare(size_type __pos, size_type __n1, const _CharT* __s) const
  1311. {
  1312. __glibcxx_requires_string(__s);
  1313. _M_check(__pos, "basic_string::compare");
  1314. __n1 = _M_limit(__pos, __n1);
  1315. const size_type __osize = traits_type::length(__s);
  1316. const size_type __len = std::min(__n1, __osize);
  1317. int __r = traits_type::compare(_M_data() + __pos, __s, __len);
  1318. if (!__r)
  1319. __r = _S_compare(__n1, __osize);
  1320. return __r;
  1321. }
  1322. template<typename _CharT, typename _Traits, typename _Alloc>
  1323. int
  1324. basic_string <_CharT, _Traits, _Alloc>::
  1325. compare(size_type __pos, size_type __n1, const _CharT* __s,
  1326. size_type __n2) const
  1327. {
  1328. __glibcxx_requires_string_len(__s, __n2);
  1329. _M_check(__pos, "basic_string::compare");
  1330. __n1 = _M_limit(__pos, __n1);
  1331. const size_type __len = std::min(__n1, __n2);
  1332. int __r = traits_type::compare(_M_data() + __pos, __s, __len);
  1333. if (!__r)
  1334. __r = _S_compare(__n1, __n2);
  1335. return __r;
  1336. }
  1337. // 21.3.7.9 basic_string::getline and operators
  1338. template<typename _CharT, typename _Traits, typename _Alloc>
  1339. basic_istream<_CharT, _Traits>&
  1340. operator>>(basic_istream<_CharT, _Traits>& __in,
  1341. basic_string<_CharT, _Traits, _Alloc>& __str)
  1342. {
  1343. typedef basic_istream<_CharT, _Traits> __istream_type;
  1344. typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
  1345. typedef typename __istream_type::ios_base __ios_base;
  1346. typedef typename __istream_type::int_type __int_type;
  1347. typedef typename __string_type::size_type __size_type;
  1348. typedef ctype<_CharT> __ctype_type;
  1349. typedef typename __ctype_type::ctype_base __ctype_base;
  1350. __size_type __extracted = 0;
  1351. typename __ios_base::iostate __err = __ios_base::goodbit;
  1352. typename __istream_type::sentry __cerb(__in, false);
  1353. if (__cerb)
  1354. {
  1355. __try
  1356. {
  1357. // Avoid reallocation for common case.
  1358. __str.erase();
  1359. _CharT __buf[128];
  1360. __size_type __len = 0;
  1361. const streamsize __w = __in.width();
  1362. const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
  1363. : __str.max_size();
  1364. const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
  1365. const __int_type __eof = _Traits::eof();
  1366. __int_type __c = __in.rdbuf()->sgetc();
  1367. while (__extracted < __n
  1368. && !_Traits::eq_int_type(__c, __eof)
  1369. && !__ct.is(__ctype_base::space,
  1370. _Traits::to_char_type(__c)))
  1371. {
  1372. if (__len == sizeof(__buf) / sizeof(_CharT))
  1373. {
  1374. __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
  1375. __len = 0;
  1376. }
  1377. __buf[__len++] = _Traits::to_char_type(__c);
  1378. ++__extracted;
  1379. __c = __in.rdbuf()->snextc();
  1380. }
  1381. __str.append(__buf, __len);
  1382. if (_Traits::eq_int_type(__c, __eof))
  1383. __err |= __ios_base::eofbit;
  1384. __in.width(0);
  1385. }
  1386. __catch(__cxxabiv1::__forced_unwind&)
  1387. {
  1388. __in._M_setstate(__ios_base::badbit);
  1389. __throw_exception_again;
  1390. }
  1391. __catch(...)
  1392. {
  1393. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1394. // 91. Description of operator>> and getline() for string<>
  1395. // might cause endless loop
  1396. __in._M_setstate(__ios_base::badbit);
  1397. }
  1398. }
  1399. // 211. operator>>(istream&, string&) doesn't set failbit
  1400. if (!__extracted)
  1401. __err |= __ios_base::failbit;
  1402. if (__err)
  1403. __in.setstate(__err);
  1404. return __in;
  1405. }
  1406. template<typename _CharT, typename _Traits, typename _Alloc>
  1407. basic_istream<_CharT, _Traits>&
  1408. getline(basic_istream<_CharT, _Traits>& __in,
  1409. basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
  1410. {
  1411. typedef basic_istream<_CharT, _Traits> __istream_type;
  1412. typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
  1413. typedef typename __istream_type::ios_base __ios_base;
  1414. typedef typename __istream_type::int_type __int_type;
  1415. typedef typename __string_type::size_type __size_type;
  1416. __size_type __extracted = 0;
  1417. const __size_type __n = __str.max_size();
  1418. typename __ios_base::iostate __err = __ios_base::goodbit;
  1419. typename __istream_type::sentry __cerb(__in, true);
  1420. if (__cerb)
  1421. {
  1422. __try
  1423. {
  1424. __str.erase();
  1425. const __int_type __idelim = _Traits::to_int_type(__delim);
  1426. const __int_type __eof = _Traits::eof();
  1427. __int_type __c = __in.rdbuf()->sgetc();
  1428. while (__extracted < __n
  1429. && !_Traits::eq_int_type(__c, __eof)
  1430. && !_Traits::eq_int_type(__c, __idelim))
  1431. {
  1432. __str += _Traits::to_char_type(__c);
  1433. ++__extracted;
  1434. __c = __in.rdbuf()->snextc();
  1435. }
  1436. if (_Traits::eq_int_type(__c, __eof))
  1437. __err |= __ios_base::eofbit;
  1438. else if (_Traits::eq_int_type(__c, __idelim))
  1439. {
  1440. ++__extracted;
  1441. __in.rdbuf()->sbumpc();
  1442. }
  1443. else
  1444. __err |= __ios_base::failbit;
  1445. }
  1446. __catch(__cxxabiv1::__forced_unwind&)
  1447. {
  1448. __in._M_setstate(__ios_base::badbit);
  1449. __throw_exception_again;
  1450. }
  1451. __catch(...)
  1452. {
  1453. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1454. // 91. Description of operator>> and getline() for string<>
  1455. // might cause endless loop
  1456. __in._M_setstate(__ios_base::badbit);
  1457. }
  1458. }
  1459. if (!__extracted)
  1460. __err |= __ios_base::failbit;
  1461. if (__err)
  1462. __in.setstate(__err);
  1463. return __in;
  1464. }
  1465. // Inhibit implicit instantiations for required instantiations,
  1466. // which are defined via explicit instantiations elsewhere.
  1467. #if _GLIBCXX_EXTERN_TEMPLATE
  1468. // The explicit instantiation definitions in src/c++11/string-inst.cc and
  1469. // src/c++17/string-inst.cc only instantiate the members required for C++17
  1470. // and earlier standards (so not C++20's starts_with and ends_with).
  1471. // Suppress the explicit instantiation declarations for C++20, so C++20
  1472. // code will implicitly instantiate std::string and std::wstring as needed.
  1473. # if __cplusplus <= 201703L && _GLIBCXX_EXTERN_TEMPLATE > 0
  1474. extern template class basic_string<char>;
  1475. # elif ! _GLIBCXX_USE_CXX11_ABI
  1476. // Still need to prevent implicit instantiation of the COW empty rep,
  1477. // to ensure the definition in libstdc++.so is unique (PR 86138).
  1478. extern template basic_string<char>::size_type
  1479. basic_string<char>::_Rep::_S_empty_rep_storage[];
  1480. # endif
  1481. extern template
  1482. basic_istream<char>&
  1483. operator>>(basic_istream<char>&, string&);
  1484. extern template
  1485. basic_ostream<char>&
  1486. operator<<(basic_ostream<char>&, const string&);
  1487. extern template
  1488. basic_istream<char>&
  1489. getline(basic_istream<char>&, string&, char);
  1490. extern template
  1491. basic_istream<char>&
  1492. getline(basic_istream<char>&, string&);
  1493. #ifdef _GLIBCXX_USE_WCHAR_T
  1494. # if __cplusplus <= 201703L && _GLIBCXX_EXTERN_TEMPLATE > 0
  1495. extern template class basic_string<wchar_t>;
  1496. # elif ! _GLIBCXX_USE_CXX11_ABI
  1497. extern template basic_string<wchar_t>::size_type
  1498. basic_string<wchar_t>::_Rep::_S_empty_rep_storage[];
  1499. # endif
  1500. extern template
  1501. basic_istream<wchar_t>&
  1502. operator>>(basic_istream<wchar_t>&, wstring&);
  1503. extern template
  1504. basic_ostream<wchar_t>&
  1505. operator<<(basic_ostream<wchar_t>&, const wstring&);
  1506. extern template
  1507. basic_istream<wchar_t>&
  1508. getline(basic_istream<wchar_t>&, wstring&, wchar_t);
  1509. extern template
  1510. basic_istream<wchar_t>&
  1511. getline(basic_istream<wchar_t>&, wstring&);
  1512. #endif // _GLIBCXX_USE_WCHAR_T
  1513. #endif // _GLIBCXX_EXTERN_TEMPLATE
  1514. _GLIBCXX_END_NAMESPACE_VERSION
  1515. } // namespace std
  1516. #endif