basic_string.tcc 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655
  1. // Components for manipulating sequences of characters -*- C++ -*-
  2. // Copyright (C) 1997-2019 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. _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value)
  871. {
  872. if (_M_rep()->_M_is_leaked())
  873. _M_rep()->_M_set_sharable();
  874. if (__s._M_rep()->_M_is_leaked())
  875. __s._M_rep()->_M_set_sharable();
  876. if (this->get_allocator() == __s.get_allocator())
  877. {
  878. _CharT* __tmp = _M_data();
  879. _M_data(__s._M_data());
  880. __s._M_data(__tmp);
  881. }
  882. // The code below can usually be optimized away.
  883. else
  884. {
  885. const basic_string __tmp1(_M_ibegin(), _M_iend(),
  886. __s.get_allocator());
  887. const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
  888. this->get_allocator());
  889. *this = __tmp2;
  890. __s = __tmp1;
  891. }
  892. }
  893. template<typename _CharT, typename _Traits, typename _Alloc>
  894. typename basic_string<_CharT, _Traits, _Alloc>::_Rep*
  895. basic_string<_CharT, _Traits, _Alloc>::_Rep::
  896. _S_create(size_type __capacity, size_type __old_capacity,
  897. const _Alloc& __alloc)
  898. {
  899. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  900. // 83. String::npos vs. string::max_size()
  901. if (__capacity > _S_max_size)
  902. __throw_length_error(__N("basic_string::_S_create"));
  903. // The standard places no restriction on allocating more memory
  904. // than is strictly needed within this layer at the moment or as
  905. // requested by an explicit application call to reserve().
  906. // Many malloc implementations perform quite poorly when an
  907. // application attempts to allocate memory in a stepwise fashion
  908. // growing each allocation size by only 1 char. Additionally,
  909. // it makes little sense to allocate less linear memory than the
  910. // natural blocking size of the malloc implementation.
  911. // Unfortunately, we would need a somewhat low-level calculation
  912. // with tuned parameters to get this perfect for any particular
  913. // malloc implementation. Fortunately, generalizations about
  914. // common features seen among implementations seems to suffice.
  915. // __pagesize need not match the actual VM page size for good
  916. // results in practice, thus we pick a common value on the low
  917. // side. __malloc_header_size is an estimate of the amount of
  918. // overhead per memory allocation (in practice seen N * sizeof
  919. // (void*) where N is 0, 2 or 4). According to folklore,
  920. // picking this value on the high side is better than
  921. // low-balling it (especially when this algorithm is used with
  922. // malloc implementations that allocate memory blocks rounded up
  923. // to a size which is a power of 2).
  924. const size_type __pagesize = 4096;
  925. const size_type __malloc_header_size = 4 * sizeof(void*);
  926. // The below implements an exponential growth policy, necessary to
  927. // meet amortized linear time requirements of the library: see
  928. // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
  929. // It's active for allocations requiring an amount of memory above
  930. // system pagesize. This is consistent with the requirements of the
  931. // standard: http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html
  932. if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
  933. __capacity = 2 * __old_capacity;
  934. // NB: Need an array of char_type[__capacity], plus a terminating
  935. // null char_type() element, plus enough for the _Rep data structure.
  936. // Whew. Seemingly so needy, yet so elemental.
  937. size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
  938. const size_type __adj_size = __size + __malloc_header_size;
  939. if (__adj_size > __pagesize && __capacity > __old_capacity)
  940. {
  941. const size_type __extra = __pagesize - __adj_size % __pagesize;
  942. __capacity += __extra / sizeof(_CharT);
  943. // Never allocate a string bigger than _S_max_size.
  944. if (__capacity > _S_max_size)
  945. __capacity = _S_max_size;
  946. __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
  947. }
  948. // NB: Might throw, but no worries about a leak, mate: _Rep()
  949. // does not throw.
  950. void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
  951. _Rep *__p = new (__place) _Rep;
  952. __p->_M_capacity = __capacity;
  953. // ABI compatibility - 3.4.x set in _S_create both
  954. // _M_refcount and _M_length. All callers of _S_create
  955. // in basic_string.tcc then set just _M_length.
  956. // In 4.0.x and later both _M_refcount and _M_length
  957. // are initialized in the callers, unfortunately we can
  958. // have 3.4.x compiled code with _S_create callers inlined
  959. // calling 4.0.x+ _S_create.
  960. __p->_M_set_sharable();
  961. return __p;
  962. }
  963. template<typename _CharT, typename _Traits, typename _Alloc>
  964. _CharT*
  965. basic_string<_CharT, _Traits, _Alloc>::_Rep::
  966. _M_clone(const _Alloc& __alloc, size_type __res)
  967. {
  968. // Requested capacity of the clone.
  969. const size_type __requested_cap = this->_M_length + __res;
  970. _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity,
  971. __alloc);
  972. if (this->_M_length)
  973. _M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length);
  974. __r->_M_set_length_and_sharable(this->_M_length);
  975. return __r->_M_refdata();
  976. }
  977. template<typename _CharT, typename _Traits, typename _Alloc>
  978. void
  979. basic_string<_CharT, _Traits, _Alloc>::
  980. resize(size_type __n, _CharT __c)
  981. {
  982. const size_type __size = this->size();
  983. _M_check_length(__size, __n, "basic_string::resize");
  984. if (__size < __n)
  985. this->append(__n - __size, __c);
  986. else if (__n < __size)
  987. this->erase(__n);
  988. // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
  989. }
  990. template<typename _CharT, typename _Traits, typename _Alloc>
  991. template<typename _InputIterator>
  992. basic_string<_CharT, _Traits, _Alloc>&
  993. basic_string<_CharT, _Traits, _Alloc>::
  994. _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
  995. _InputIterator __k2, __false_type)
  996. {
  997. const basic_string __s(__k1, __k2);
  998. const size_type __n1 = __i2 - __i1;
  999. _M_check_length(__n1, __s.size(), "basic_string::_M_replace_dispatch");
  1000. return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
  1001. __s.size());
  1002. }
  1003. template<typename _CharT, typename _Traits, typename _Alloc>
  1004. basic_string<_CharT, _Traits, _Alloc>&
  1005. basic_string<_CharT, _Traits, _Alloc>::
  1006. _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
  1007. _CharT __c)
  1008. {
  1009. _M_check_length(__n1, __n2, "basic_string::_M_replace_aux");
  1010. _M_mutate(__pos1, __n1, __n2);
  1011. if (__n2)
  1012. _M_assign(_M_data() + __pos1, __n2, __c);
  1013. return *this;
  1014. }
  1015. template<typename _CharT, typename _Traits, typename _Alloc>
  1016. basic_string<_CharT, _Traits, _Alloc>&
  1017. basic_string<_CharT, _Traits, _Alloc>::
  1018. _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
  1019. size_type __n2)
  1020. {
  1021. _M_mutate(__pos1, __n1, __n2);
  1022. if (__n2)
  1023. _M_copy(_M_data() + __pos1, __s, __n2);
  1024. return *this;
  1025. }
  1026. template<typename _CharT, typename _Traits, typename _Alloc>
  1027. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1028. basic_string<_CharT, _Traits, _Alloc>::
  1029. copy(_CharT* __s, size_type __n, size_type __pos) const
  1030. {
  1031. _M_check(__pos, "basic_string::copy");
  1032. __n = _M_limit(__pos, __n);
  1033. __glibcxx_requires_string_len(__s, __n);
  1034. if (__n)
  1035. _M_copy(__s, _M_data() + __pos, __n);
  1036. // 21.3.5.7 par 3: do not append null. (good.)
  1037. return __n;
  1038. }
  1039. #endif // !_GLIBCXX_USE_CXX11_ABI
  1040. template<typename _CharT, typename _Traits, typename _Alloc>
  1041. basic_string<_CharT, _Traits, _Alloc>
  1042. operator+(const _CharT* __lhs,
  1043. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  1044. {
  1045. __glibcxx_requires_string(__lhs);
  1046. typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
  1047. typedef typename __string_type::size_type __size_type;
  1048. const __size_type __len = _Traits::length(__lhs);
  1049. __string_type __str;
  1050. __str.reserve(__len + __rhs.size());
  1051. __str.append(__lhs, __len);
  1052. __str.append(__rhs);
  1053. return __str;
  1054. }
  1055. template<typename _CharT, typename _Traits, typename _Alloc>
  1056. basic_string<_CharT, _Traits, _Alloc>
  1057. operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  1058. {
  1059. typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
  1060. typedef typename __string_type::size_type __size_type;
  1061. __string_type __str;
  1062. const __size_type __len = __rhs.size();
  1063. __str.reserve(__len + 1);
  1064. __str.append(__size_type(1), __lhs);
  1065. __str.append(__rhs);
  1066. return __str;
  1067. }
  1068. template<typename _CharT, typename _Traits, typename _Alloc>
  1069. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1070. basic_string<_CharT, _Traits, _Alloc>::
  1071. find(const _CharT* __s, size_type __pos, size_type __n) const
  1072. _GLIBCXX_NOEXCEPT
  1073. {
  1074. __glibcxx_requires_string_len(__s, __n);
  1075. const size_type __size = this->size();
  1076. if (__n == 0)
  1077. return __pos <= __size ? __pos : npos;
  1078. if (__pos >= __size)
  1079. return npos;
  1080. const _CharT __elem0 = __s[0];
  1081. const _CharT* const __data = data();
  1082. const _CharT* __first = __data + __pos;
  1083. const _CharT* const __last = __data + __size;
  1084. size_type __len = __size - __pos;
  1085. while (__len >= __n)
  1086. {
  1087. // Find the first occurrence of __elem0:
  1088. __first = traits_type::find(__first, __len - __n + 1, __elem0);
  1089. if (!__first)
  1090. return npos;
  1091. // Compare the full strings from the first occurrence of __elem0.
  1092. // We already know that __first[0] == __s[0] but compare them again
  1093. // anyway because __s is probably aligned, which helps memcmp.
  1094. if (traits_type::compare(__first, __s, __n) == 0)
  1095. return __first - __data;
  1096. __len = __last - ++__first;
  1097. }
  1098. return npos;
  1099. }
  1100. template<typename _CharT, typename _Traits, typename _Alloc>
  1101. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1102. basic_string<_CharT, _Traits, _Alloc>::
  1103. find(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
  1104. {
  1105. size_type __ret = npos;
  1106. const size_type __size = this->size();
  1107. if (__pos < __size)
  1108. {
  1109. const _CharT* __data = _M_data();
  1110. const size_type __n = __size - __pos;
  1111. const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
  1112. if (__p)
  1113. __ret = __p - __data;
  1114. }
  1115. return __ret;
  1116. }
  1117. template<typename _CharT, typename _Traits, typename _Alloc>
  1118. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1119. basic_string<_CharT, _Traits, _Alloc>::
  1120. rfind(const _CharT* __s, size_type __pos, size_type __n) const
  1121. _GLIBCXX_NOEXCEPT
  1122. {
  1123. __glibcxx_requires_string_len(__s, __n);
  1124. const size_type __size = this->size();
  1125. if (__n <= __size)
  1126. {
  1127. __pos = std::min(size_type(__size - __n), __pos);
  1128. const _CharT* __data = _M_data();
  1129. do
  1130. {
  1131. if (traits_type::compare(__data + __pos, __s, __n) == 0)
  1132. return __pos;
  1133. }
  1134. while (__pos-- > 0);
  1135. }
  1136. return npos;
  1137. }
  1138. template<typename _CharT, typename _Traits, typename _Alloc>
  1139. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1140. basic_string<_CharT, _Traits, _Alloc>::
  1141. rfind(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
  1142. {
  1143. size_type __size = this->size();
  1144. if (__size)
  1145. {
  1146. if (--__size > __pos)
  1147. __size = __pos;
  1148. for (++__size; __size-- > 0; )
  1149. if (traits_type::eq(_M_data()[__size], __c))
  1150. return __size;
  1151. }
  1152. return npos;
  1153. }
  1154. template<typename _CharT, typename _Traits, typename _Alloc>
  1155. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1156. basic_string<_CharT, _Traits, _Alloc>::
  1157. find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
  1158. _GLIBCXX_NOEXCEPT
  1159. {
  1160. __glibcxx_requires_string_len(__s, __n);
  1161. for (; __n && __pos < this->size(); ++__pos)
  1162. {
  1163. const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
  1164. if (__p)
  1165. return __pos;
  1166. }
  1167. return npos;
  1168. }
  1169. template<typename _CharT, typename _Traits, typename _Alloc>
  1170. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1171. basic_string<_CharT, _Traits, _Alloc>::
  1172. find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
  1173. _GLIBCXX_NOEXCEPT
  1174. {
  1175. __glibcxx_requires_string_len(__s, __n);
  1176. size_type __size = this->size();
  1177. if (__size && __n)
  1178. {
  1179. if (--__size > __pos)
  1180. __size = __pos;
  1181. do
  1182. {
  1183. if (traits_type::find(__s, __n, _M_data()[__size]))
  1184. return __size;
  1185. }
  1186. while (__size-- != 0);
  1187. }
  1188. return npos;
  1189. }
  1190. template<typename _CharT, typename _Traits, typename _Alloc>
  1191. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1192. basic_string<_CharT, _Traits, _Alloc>::
  1193. find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
  1194. _GLIBCXX_NOEXCEPT
  1195. {
  1196. __glibcxx_requires_string_len(__s, __n);
  1197. for (; __pos < this->size(); ++__pos)
  1198. if (!traits_type::find(__s, __n, _M_data()[__pos]))
  1199. return __pos;
  1200. return npos;
  1201. }
  1202. template<typename _CharT, typename _Traits, typename _Alloc>
  1203. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1204. basic_string<_CharT, _Traits, _Alloc>::
  1205. find_first_not_of(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
  1206. {
  1207. for (; __pos < this->size(); ++__pos)
  1208. if (!traits_type::eq(_M_data()[__pos], __c))
  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_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
  1216. _GLIBCXX_NOEXCEPT
  1217. {
  1218. __glibcxx_requires_string_len(__s, __n);
  1219. size_type __size = this->size();
  1220. if (__size)
  1221. {
  1222. if (--__size > __pos)
  1223. __size = __pos;
  1224. do
  1225. {
  1226. if (!traits_type::find(__s, __n, _M_data()[__size]))
  1227. return __size;
  1228. }
  1229. while (__size--);
  1230. }
  1231. return npos;
  1232. }
  1233. template<typename _CharT, typename _Traits, typename _Alloc>
  1234. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  1235. basic_string<_CharT, _Traits, _Alloc>::
  1236. find_last_not_of(_CharT __c, size_type __pos) const _GLIBCXX_NOEXCEPT
  1237. {
  1238. size_type __size = this->size();
  1239. if (__size)
  1240. {
  1241. if (--__size > __pos)
  1242. __size = __pos;
  1243. do
  1244. {
  1245. if (!traits_type::eq(_M_data()[__size], __c))
  1246. return __size;
  1247. }
  1248. while (__size--);
  1249. }
  1250. return npos;
  1251. }
  1252. template<typename _CharT, typename _Traits, typename _Alloc>
  1253. int
  1254. basic_string<_CharT, _Traits, _Alloc>::
  1255. compare(size_type __pos, size_type __n, const basic_string& __str) const
  1256. {
  1257. _M_check(__pos, "basic_string::compare");
  1258. __n = _M_limit(__pos, __n);
  1259. const size_type __osize = __str.size();
  1260. const size_type __len = std::min(__n, __osize);
  1261. int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
  1262. if (!__r)
  1263. __r = _S_compare(__n, __osize);
  1264. return __r;
  1265. }
  1266. template<typename _CharT, typename _Traits, typename _Alloc>
  1267. int
  1268. basic_string<_CharT, _Traits, _Alloc>::
  1269. compare(size_type __pos1, size_type __n1, const basic_string& __str,
  1270. size_type __pos2, size_type __n2) const
  1271. {
  1272. _M_check(__pos1, "basic_string::compare");
  1273. __str._M_check(__pos2, "basic_string::compare");
  1274. __n1 = _M_limit(__pos1, __n1);
  1275. __n2 = __str._M_limit(__pos2, __n2);
  1276. const size_type __len = std::min(__n1, __n2);
  1277. int __r = traits_type::compare(_M_data() + __pos1,
  1278. __str.data() + __pos2, __len);
  1279. if (!__r)
  1280. __r = _S_compare(__n1, __n2);
  1281. return __r;
  1282. }
  1283. template<typename _CharT, typename _Traits, typename _Alloc>
  1284. int
  1285. basic_string<_CharT, _Traits, _Alloc>::
  1286. compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT
  1287. {
  1288. __glibcxx_requires_string(__s);
  1289. const size_type __size = this->size();
  1290. const size_type __osize = traits_type::length(__s);
  1291. const size_type __len = std::min(__size, __osize);
  1292. int __r = traits_type::compare(_M_data(), __s, __len);
  1293. if (!__r)
  1294. __r = _S_compare(__size, __osize);
  1295. return __r;
  1296. }
  1297. template<typename _CharT, typename _Traits, typename _Alloc>
  1298. int
  1299. basic_string <_CharT, _Traits, _Alloc>::
  1300. compare(size_type __pos, size_type __n1, const _CharT* __s) const
  1301. {
  1302. __glibcxx_requires_string(__s);
  1303. _M_check(__pos, "basic_string::compare");
  1304. __n1 = _M_limit(__pos, __n1);
  1305. const size_type __osize = traits_type::length(__s);
  1306. const size_type __len = std::min(__n1, __osize);
  1307. int __r = traits_type::compare(_M_data() + __pos, __s, __len);
  1308. if (!__r)
  1309. __r = _S_compare(__n1, __osize);
  1310. return __r;
  1311. }
  1312. template<typename _CharT, typename _Traits, typename _Alloc>
  1313. int
  1314. basic_string <_CharT, _Traits, _Alloc>::
  1315. compare(size_type __pos, size_type __n1, const _CharT* __s,
  1316. size_type __n2) const
  1317. {
  1318. __glibcxx_requires_string_len(__s, __n2);
  1319. _M_check(__pos, "basic_string::compare");
  1320. __n1 = _M_limit(__pos, __n1);
  1321. const size_type __len = std::min(__n1, __n2);
  1322. int __r = traits_type::compare(_M_data() + __pos, __s, __len);
  1323. if (!__r)
  1324. __r = _S_compare(__n1, __n2);
  1325. return __r;
  1326. }
  1327. // 21.3.7.9 basic_string::getline and operators
  1328. template<typename _CharT, typename _Traits, typename _Alloc>
  1329. basic_istream<_CharT, _Traits>&
  1330. operator>>(basic_istream<_CharT, _Traits>& __in,
  1331. basic_string<_CharT, _Traits, _Alloc>& __str)
  1332. {
  1333. typedef basic_istream<_CharT, _Traits> __istream_type;
  1334. typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
  1335. typedef typename __istream_type::ios_base __ios_base;
  1336. typedef typename __istream_type::int_type __int_type;
  1337. typedef typename __string_type::size_type __size_type;
  1338. typedef ctype<_CharT> __ctype_type;
  1339. typedef typename __ctype_type::ctype_base __ctype_base;
  1340. __size_type __extracted = 0;
  1341. typename __ios_base::iostate __err = __ios_base::goodbit;
  1342. typename __istream_type::sentry __cerb(__in, false);
  1343. if (__cerb)
  1344. {
  1345. __try
  1346. {
  1347. // Avoid reallocation for common case.
  1348. __str.erase();
  1349. _CharT __buf[128];
  1350. __size_type __len = 0;
  1351. const streamsize __w = __in.width();
  1352. const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
  1353. : __str.max_size();
  1354. const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
  1355. const __int_type __eof = _Traits::eof();
  1356. __int_type __c = __in.rdbuf()->sgetc();
  1357. while (__extracted < __n
  1358. && !_Traits::eq_int_type(__c, __eof)
  1359. && !__ct.is(__ctype_base::space,
  1360. _Traits::to_char_type(__c)))
  1361. {
  1362. if (__len == sizeof(__buf) / sizeof(_CharT))
  1363. {
  1364. __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
  1365. __len = 0;
  1366. }
  1367. __buf[__len++] = _Traits::to_char_type(__c);
  1368. ++__extracted;
  1369. __c = __in.rdbuf()->snextc();
  1370. }
  1371. __str.append(__buf, __len);
  1372. if (_Traits::eq_int_type(__c, __eof))
  1373. __err |= __ios_base::eofbit;
  1374. __in.width(0);
  1375. }
  1376. __catch(__cxxabiv1::__forced_unwind&)
  1377. {
  1378. __in._M_setstate(__ios_base::badbit);
  1379. __throw_exception_again;
  1380. }
  1381. __catch(...)
  1382. {
  1383. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1384. // 91. Description of operator>> and getline() for string<>
  1385. // might cause endless loop
  1386. __in._M_setstate(__ios_base::badbit);
  1387. }
  1388. }
  1389. // 211. operator>>(istream&, string&) doesn't set failbit
  1390. if (!__extracted)
  1391. __err |= __ios_base::failbit;
  1392. if (__err)
  1393. __in.setstate(__err);
  1394. return __in;
  1395. }
  1396. template<typename _CharT, typename _Traits, typename _Alloc>
  1397. basic_istream<_CharT, _Traits>&
  1398. getline(basic_istream<_CharT, _Traits>& __in,
  1399. basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
  1400. {
  1401. typedef basic_istream<_CharT, _Traits> __istream_type;
  1402. typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
  1403. typedef typename __istream_type::ios_base __ios_base;
  1404. typedef typename __istream_type::int_type __int_type;
  1405. typedef typename __string_type::size_type __size_type;
  1406. __size_type __extracted = 0;
  1407. const __size_type __n = __str.max_size();
  1408. typename __ios_base::iostate __err = __ios_base::goodbit;
  1409. typename __istream_type::sentry __cerb(__in, true);
  1410. if (__cerb)
  1411. {
  1412. __try
  1413. {
  1414. __str.erase();
  1415. const __int_type __idelim = _Traits::to_int_type(__delim);
  1416. const __int_type __eof = _Traits::eof();
  1417. __int_type __c = __in.rdbuf()->sgetc();
  1418. while (__extracted < __n
  1419. && !_Traits::eq_int_type(__c, __eof)
  1420. && !_Traits::eq_int_type(__c, __idelim))
  1421. {
  1422. __str += _Traits::to_char_type(__c);
  1423. ++__extracted;
  1424. __c = __in.rdbuf()->snextc();
  1425. }
  1426. if (_Traits::eq_int_type(__c, __eof))
  1427. __err |= __ios_base::eofbit;
  1428. else if (_Traits::eq_int_type(__c, __idelim))
  1429. {
  1430. ++__extracted;
  1431. __in.rdbuf()->sbumpc();
  1432. }
  1433. else
  1434. __err |= __ios_base::failbit;
  1435. }
  1436. __catch(__cxxabiv1::__forced_unwind&)
  1437. {
  1438. __in._M_setstate(__ios_base::badbit);
  1439. __throw_exception_again;
  1440. }
  1441. __catch(...)
  1442. {
  1443. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1444. // 91. Description of operator>> and getline() for string<>
  1445. // might cause endless loop
  1446. __in._M_setstate(__ios_base::badbit);
  1447. }
  1448. }
  1449. if (!__extracted)
  1450. __err |= __ios_base::failbit;
  1451. if (__err)
  1452. __in.setstate(__err);
  1453. return __in;
  1454. }
  1455. // Inhibit implicit instantiations for required instantiations,
  1456. // which are defined via explicit instantiations elsewhere.
  1457. #if _GLIBCXX_EXTERN_TEMPLATE
  1458. // The explicit instantiations definitions in src/c++11/string-inst.cc
  1459. // are compiled as C++14, so the new C++17 members aren't instantiated.
  1460. // Until those definitions are compiled as C++17 suppress the declaration,
  1461. // so C++17 code will implicitly instantiate std::string and std::wstring
  1462. // as needed.
  1463. # if __cplusplus <= 201703L && _GLIBCXX_EXTERN_TEMPLATE > 0
  1464. extern template class basic_string<char>;
  1465. # elif ! _GLIBCXX_USE_CXX11_ABI
  1466. // Still need to prevent implicit instantiation of the COW empty rep,
  1467. // to ensure the definition in libstdc++.so is unique (PR 86138).
  1468. extern template basic_string<char>::size_type
  1469. basic_string<char>::_Rep::_S_empty_rep_storage[];
  1470. # endif
  1471. extern template
  1472. basic_istream<char>&
  1473. operator>>(basic_istream<char>&, string&);
  1474. extern template
  1475. basic_ostream<char>&
  1476. operator<<(basic_ostream<char>&, const string&);
  1477. extern template
  1478. basic_istream<char>&
  1479. getline(basic_istream<char>&, string&, char);
  1480. extern template
  1481. basic_istream<char>&
  1482. getline(basic_istream<char>&, string&);
  1483. #ifdef _GLIBCXX_USE_WCHAR_T
  1484. # if __cplusplus <= 201703L && _GLIBCXX_EXTERN_TEMPLATE > 0
  1485. extern template class basic_string<wchar_t>;
  1486. # elif ! _GLIBCXX_USE_CXX11_ABI
  1487. extern template basic_string<wchar_t>::size_type
  1488. basic_string<wchar_t>::_Rep::_S_empty_rep_storage[];
  1489. # endif
  1490. extern template
  1491. basic_istream<wchar_t>&
  1492. operator>>(basic_istream<wchar_t>&, wstring&);
  1493. extern template
  1494. basic_ostream<wchar_t>&
  1495. operator<<(basic_ostream<wchar_t>&, const wstring&);
  1496. extern template
  1497. basic_istream<wchar_t>&
  1498. getline(basic_istream<wchar_t>&, wstring&, wchar_t);
  1499. extern template
  1500. basic_istream<wchar_t>&
  1501. getline(basic_istream<wchar_t>&, wstring&);
  1502. #endif // _GLIBCXX_USE_WCHAR_T
  1503. #endif // _GLIBCXX_EXTERN_TEMPLATE
  1504. _GLIBCXX_END_NAMESPACE_VERSION
  1505. } // namespace std
  1506. #endif