basic_string.tcc 52 KB

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