vstring.h 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965
  1. // Versatile string -*- C++ -*-
  2. // Copyright (C) 2005-2022 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 ext/vstring.h
  21. * This file is a GNU extension to the Standard C++ Library.
  22. */
  23. #ifndef _VSTRING_H
  24. #define _VSTRING_H 1
  25. #pragma GCC system_header
  26. #if __cplusplus >= 201103L
  27. #include <initializer_list>
  28. #endif
  29. #include <ext/vstring_util.h>
  30. #include <ext/rc_string_base.h>
  31. #include <ext/sso_string_base.h>
  32. #include <bits/stl_algobase.h> // std::min
  33. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  34. {
  35. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  36. /**
  37. * @class __versa_string vstring.h
  38. * @brief Template class __versa_string.
  39. * @ingroup extensions
  40. *
  41. * Data structure managing sequences of characters and
  42. * character-like objects.
  43. */
  44. template<typename _CharT, typename _Traits, typename _Alloc,
  45. template <typename, typename, typename> class _Base>
  46. class __versa_string
  47. : private _Base<_CharT, _Traits, _Alloc>
  48. {
  49. typedef _Base<_CharT, _Traits, _Alloc> __vstring_base;
  50. typedef typename __vstring_base::_CharT_alloc_type _CharT_alloc_type;
  51. typedef __alloc_traits<_CharT_alloc_type> _CharT_alloc_traits;
  52. // Types:
  53. public:
  54. typedef _Traits traits_type;
  55. typedef typename _Traits::char_type value_type;
  56. typedef _Alloc allocator_type;
  57. typedef typename _CharT_alloc_type::size_type size_type;
  58. typedef typename _CharT_alloc_type::difference_type difference_type;
  59. typedef value_type& reference;
  60. typedef const value_type& const_reference;
  61. typedef typename _CharT_alloc_traits::pointer pointer;
  62. typedef typename _CharT_alloc_traits::const_pointer const_pointer;
  63. typedef __gnu_cxx::__normal_iterator<pointer, __versa_string> iterator;
  64. typedef __gnu_cxx::__normal_iterator<const_pointer, __versa_string>
  65. const_iterator;
  66. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  67. typedef std::reverse_iterator<iterator> reverse_iterator;
  68. // Data Member (public):
  69. /// Value returned by various member functions when they fail.
  70. static const size_type npos = static_cast<size_type>(-1);
  71. private:
  72. size_type
  73. _M_check(size_type __pos, const char* __s) const
  74. {
  75. if (__pos > this->size())
  76. std::__throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
  77. "this->size() (which is %zu)"),
  78. __s, __pos, this->size());
  79. return __pos;
  80. }
  81. void
  82. _M_check_length(size_type __n1, size_type __n2, const char* __s) const
  83. {
  84. if (this->max_size() - (this->size() - __n1) < __n2)
  85. std::__throw_length_error(__N(__s));
  86. }
  87. // NB: _M_limit doesn't check for a bad __pos value.
  88. size_type
  89. _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
  90. {
  91. const bool __testoff = __off < this->size() - __pos;
  92. return __testoff ? __off : this->size() - __pos;
  93. }
  94. // True if _Rep and source do not overlap.
  95. bool
  96. _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
  97. {
  98. return (std::less<const _CharT*>()(__s, this->_M_data())
  99. || std::less<const _CharT*>()(this->_M_data()
  100. + this->size(), __s));
  101. }
  102. // For the internal use we have functions similar to `begin'/`end'
  103. // but they do not call _M_leak.
  104. iterator
  105. _M_ibegin() const _GLIBCXX_NOEXCEPT
  106. { return iterator(this->_M_data()); }
  107. iterator
  108. _M_iend() const _GLIBCXX_NOEXCEPT
  109. { return iterator(this->_M_data() + this->_M_length()); }
  110. public:
  111. // Construct/copy/destroy:
  112. // NB: We overload ctors in some cases instead of using default
  113. // arguments, per 17.4.4.4 para. 2 item 2.
  114. /**
  115. * @brief Construct an empty string using allocator @a a.
  116. */
  117. explicit
  118. __versa_string(const _Alloc& __a = _Alloc()) _GLIBCXX_NOEXCEPT
  119. : __vstring_base(__a) { }
  120. // NB: per LWG issue 42, semantics different from IS:
  121. /**
  122. * @brief Construct string with copy of value of @a __str.
  123. * @param __str Source string.
  124. */
  125. __versa_string(const __versa_string& __str)
  126. : __vstring_base(__str) { }
  127. #if __cplusplus >= 201103L
  128. /**
  129. * @brief String move constructor.
  130. * @param __str Source string.
  131. *
  132. * The newly-constructed %string contains the exact contents of
  133. * @a __str. The contents of @a __str are a valid, but unspecified
  134. * string.
  135. */
  136. __versa_string(__versa_string&& __str) noexcept
  137. : __vstring_base(std::move(__str)) { }
  138. /**
  139. * @brief Construct string from an initializer list.
  140. * @param __l std::initializer_list of characters.
  141. * @param __a Allocator to use (default is default allocator).
  142. */
  143. __versa_string(std::initializer_list<_CharT> __l,
  144. const _Alloc& __a = _Alloc())
  145. : __vstring_base(__l.begin(), __l.end(), __a) { }
  146. #endif
  147. /**
  148. * @brief Construct string as copy of a substring.
  149. * @param __str Source string.
  150. * @param __pos Index of first character to copy from.
  151. * @param __n Number of characters to copy (default remainder).
  152. */
  153. __versa_string(const __versa_string& __str, size_type __pos,
  154. size_type __n = npos)
  155. : __vstring_base(__str._M_data()
  156. + __str._M_check(__pos,
  157. "__versa_string::__versa_string"),
  158. __str._M_data() + __str._M_limit(__pos, __n)
  159. + __pos, _Alloc()) { }
  160. /**
  161. * @brief Construct string as copy of a substring.
  162. * @param __str Source string.
  163. * @param __pos Index of first character to copy from.
  164. * @param __n Number of characters to copy.
  165. * @param __a Allocator to use.
  166. */
  167. __versa_string(const __versa_string& __str, size_type __pos,
  168. size_type __n, const _Alloc& __a)
  169. : __vstring_base(__str._M_data()
  170. + __str._M_check(__pos,
  171. "__versa_string::__versa_string"),
  172. __str._M_data() + __str._M_limit(__pos, __n)
  173. + __pos, __a) { }
  174. /**
  175. * @brief Construct string initialized by a character array.
  176. * @param __s Source character array.
  177. * @param __n Number of characters to copy.
  178. * @param __a Allocator to use (default is default allocator).
  179. *
  180. * NB: @a __s must have at least @a __n characters, '\\0' has no special
  181. * meaning.
  182. */
  183. __versa_string(const _CharT* __s, size_type __n,
  184. const _Alloc& __a = _Alloc())
  185. : __vstring_base(__s, __s + __n, __a) { }
  186. /**
  187. * @brief Construct string as copy of a C string.
  188. * @param __s Source C string.
  189. * @param __a Allocator to use (default is default allocator).
  190. */
  191. __versa_string(const _CharT* __s, const _Alloc& __a = _Alloc())
  192. : __vstring_base(__s, __s ? __s + traits_type::length(__s) :
  193. __s + npos, __a) { }
  194. /**
  195. * @brief Construct string as multiple characters.
  196. * @param __n Number of characters.
  197. * @param __c Character to use.
  198. * @param __a Allocator to use (default is default allocator).
  199. */
  200. __versa_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
  201. : __vstring_base(__n, __c, __a) { }
  202. /**
  203. * @brief Construct string as copy of a range.
  204. * @param __beg Start of range.
  205. * @param __end End of range.
  206. * @param __a Allocator to use (default is default allocator).
  207. */
  208. #if __cplusplus >= 201103L
  209. template<class _InputIterator,
  210. typename = std::_RequireInputIter<_InputIterator>>
  211. #else
  212. template<class _InputIterator>
  213. #endif
  214. __versa_string(_InputIterator __beg, _InputIterator __end,
  215. const _Alloc& __a = _Alloc())
  216. : __vstring_base(__beg, __end, __a) { }
  217. /**
  218. * @brief Destroy the string instance.
  219. */
  220. ~__versa_string() _GLIBCXX_NOEXCEPT { }
  221. /**
  222. * @brief Assign the value of @a str to this string.
  223. * @param __str Source string.
  224. */
  225. __versa_string&
  226. operator=(const __versa_string& __str)
  227. { return this->assign(__str); }
  228. #if __cplusplus >= 201103L
  229. /**
  230. * @brief String move assignment operator.
  231. * @param __str Source string.
  232. *
  233. * The contents of @a __str are moved into this string (without
  234. * copying). @a __str is a valid, but unspecified string.
  235. */
  236. __versa_string&
  237. operator=(__versa_string&& __str) noexcept
  238. {
  239. // NB: DR 1204.
  240. this->swap(__str);
  241. return *this;
  242. }
  243. /**
  244. * @brief Set value to string constructed from initializer list.
  245. * @param __l std::initializer_list.
  246. */
  247. __versa_string&
  248. operator=(std::initializer_list<_CharT> __l)
  249. {
  250. this->assign(__l.begin(), __l.end());
  251. return *this;
  252. }
  253. #endif
  254. /**
  255. * @brief Copy contents of @a __s into this string.
  256. * @param __s Source null-terminated string.
  257. */
  258. __versa_string&
  259. operator=(const _CharT* __s)
  260. { return this->assign(__s); }
  261. /**
  262. * @brief Set value to string of length 1.
  263. * @param __c Source character.
  264. *
  265. * Assigning to a character makes this string length 1 and
  266. * (*this)[0] == @a __c.
  267. */
  268. __versa_string&
  269. operator=(_CharT __c)
  270. {
  271. this->assign(1, __c);
  272. return *this;
  273. }
  274. // Iterators:
  275. /**
  276. * Returns a read/write iterator that points to the first character in
  277. * the %string. Unshares the string.
  278. */
  279. iterator
  280. begin() _GLIBCXX_NOEXCEPT
  281. {
  282. this->_M_leak();
  283. return iterator(this->_M_data());
  284. }
  285. /**
  286. * Returns a read-only (constant) iterator that points to the first
  287. * character in the %string.
  288. */
  289. const_iterator
  290. begin() const _GLIBCXX_NOEXCEPT
  291. { return const_iterator(this->_M_data()); }
  292. /**
  293. * Returns a read/write iterator that points one past the last
  294. * character in the %string. Unshares the string.
  295. */
  296. iterator
  297. end() _GLIBCXX_NOEXCEPT
  298. {
  299. this->_M_leak();
  300. return iterator(this->_M_data() + this->size());
  301. }
  302. /**
  303. * Returns a read-only (constant) iterator that points one past the
  304. * last character in the %string.
  305. */
  306. const_iterator
  307. end() const _GLIBCXX_NOEXCEPT
  308. { return const_iterator(this->_M_data() + this->size()); }
  309. /**
  310. * Returns a read/write reverse iterator that points to the last
  311. * character in the %string. Iteration is done in reverse element
  312. * order. Unshares the string.
  313. */
  314. reverse_iterator
  315. rbegin() _GLIBCXX_NOEXCEPT
  316. { return reverse_iterator(this->end()); }
  317. /**
  318. * Returns a read-only (constant) reverse iterator that points
  319. * to the last character in the %string. Iteration is done in
  320. * reverse element order.
  321. */
  322. const_reverse_iterator
  323. rbegin() const _GLIBCXX_NOEXCEPT
  324. { return const_reverse_iterator(this->end()); }
  325. /**
  326. * Returns a read/write reverse iterator that points to one before the
  327. * first character in the %string. Iteration is done in reverse
  328. * element order. Unshares the string.
  329. */
  330. reverse_iterator
  331. rend() _GLIBCXX_NOEXCEPT
  332. { return reverse_iterator(this->begin()); }
  333. /**
  334. * Returns a read-only (constant) reverse iterator that points
  335. * to one before the first character in the %string. Iteration
  336. * is done in reverse element order.
  337. */
  338. const_reverse_iterator
  339. rend() const _GLIBCXX_NOEXCEPT
  340. { return const_reverse_iterator(this->begin()); }
  341. #if __cplusplus >= 201103L
  342. /**
  343. * Returns a read-only (constant) iterator that points to the first
  344. * character in the %string.
  345. */
  346. const_iterator
  347. cbegin() const noexcept
  348. { return const_iterator(this->_M_data()); }
  349. /**
  350. * Returns a read-only (constant) iterator that points one past the
  351. * last character in the %string.
  352. */
  353. const_iterator
  354. cend() const noexcept
  355. { return const_iterator(this->_M_data() + this->size()); }
  356. /**
  357. * Returns a read-only (constant) reverse iterator that points
  358. * to the last character in the %string. Iteration is done in
  359. * reverse element order.
  360. */
  361. const_reverse_iterator
  362. crbegin() const noexcept
  363. { return const_reverse_iterator(this->end()); }
  364. /**
  365. * Returns a read-only (constant) reverse iterator that points
  366. * to one before the first character in the %string. Iteration
  367. * is done in reverse element order.
  368. */
  369. const_reverse_iterator
  370. crend() const noexcept
  371. { return const_reverse_iterator(this->begin()); }
  372. #endif
  373. public:
  374. // Capacity:
  375. /// Returns the number of characters in the string, not including any
  376. /// null-termination.
  377. size_type
  378. size() const _GLIBCXX_NOEXCEPT
  379. { return this->_M_length(); }
  380. /// Returns the number of characters in the string, not including any
  381. /// null-termination.
  382. size_type
  383. length() const _GLIBCXX_NOEXCEPT
  384. { return this->_M_length(); }
  385. /// Returns the size() of the largest possible %string.
  386. size_type
  387. max_size() const _GLIBCXX_NOEXCEPT
  388. { return this->_M_max_size(); }
  389. /**
  390. * @brief Resizes the %string to the specified number of characters.
  391. * @param __n Number of characters the %string should contain.
  392. * @param __c Character to fill any new elements.
  393. *
  394. * This function will %resize the %string to the specified
  395. * number of characters. If the number is smaller than the
  396. * %string's current size the %string is truncated, otherwise
  397. * the %string is extended and new elements are set to @a __c.
  398. */
  399. void
  400. resize(size_type __n, _CharT __c);
  401. /**
  402. * @brief Resizes the %string to the specified number of characters.
  403. * @param __n Number of characters the %string should contain.
  404. *
  405. * This function will resize the %string to the specified
  406. * length. If the new size is smaller than the %string's
  407. * current size the %string is truncated, otherwise the %string
  408. * is extended and new characters are default-constructed. For
  409. * basic types such as char, this means setting them to 0.
  410. */
  411. void
  412. resize(size_type __n)
  413. { this->resize(__n, _CharT()); }
  414. #if __cplusplus >= 201103L
  415. /// A non-binding request to reduce capacity() to size().
  416. void
  417. shrink_to_fit() noexcept
  418. {
  419. if (capacity() > size())
  420. {
  421. __try
  422. { this->reserve(0); }
  423. __catch(...)
  424. { }
  425. }
  426. }
  427. #endif
  428. /**
  429. * Returns the total number of characters that the %string can
  430. * hold before needing to allocate more memory.
  431. */
  432. size_type
  433. capacity() const _GLIBCXX_NOEXCEPT
  434. { return this->_M_capacity(); }
  435. /**
  436. * @brief Attempt to preallocate enough memory for specified number of
  437. * characters.
  438. * @param __res_arg Number of characters required.
  439. * @throw std::length_error If @a __res_arg exceeds @c max_size().
  440. *
  441. * This function attempts to reserve enough memory for the
  442. * %string to hold the specified number of characters. If the
  443. * number requested is more than max_size(), length_error is
  444. * thrown.
  445. *
  446. * The advantage of this function is that if optimal code is a
  447. * necessity and the user can determine the string length that
  448. * will be required, the user can reserve the memory in
  449. * %advance, and thus prevent a possible reallocation of memory
  450. * and copying of %string data.
  451. */
  452. void
  453. reserve(size_type __res_arg = 0)
  454. { this->_M_reserve(__res_arg); }
  455. /**
  456. * Erases the string, making it empty.
  457. */
  458. void
  459. clear() _GLIBCXX_NOEXCEPT
  460. { this->_M_clear(); }
  461. /**
  462. * Returns true if the %string is empty. Equivalent to
  463. * <code>*this == ""</code>.
  464. */
  465. _GLIBCXX_NODISCARD bool
  466. empty() const _GLIBCXX_NOEXCEPT
  467. { return this->size() == 0; }
  468. // Element access:
  469. /**
  470. * @brief Subscript access to the data contained in the %string.
  471. * @param __pos The index of the character to access.
  472. * @return Read-only (constant) reference to the character.
  473. *
  474. * This operator allows for easy, array-style, data access.
  475. * Note that data access with this operator is unchecked and
  476. * out_of_range lookups are not defined. (For checked lookups
  477. * see at().)
  478. */
  479. const_reference
  480. operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
  481. {
  482. __glibcxx_assert(__pos <= this->size());
  483. return this->_M_data()[__pos];
  484. }
  485. /**
  486. * @brief Subscript access to the data contained in the %string.
  487. * @param __pos The index of the character to access.
  488. * @return Read/write reference to the character.
  489. *
  490. * This operator allows for easy, array-style, data access.
  491. * Note that data access with this operator is unchecked and
  492. * out_of_range lookups are not defined. (For checked lookups
  493. * see at().) Unshares the string.
  494. */
  495. reference
  496. operator[](size_type __pos) _GLIBCXX_NOEXCEPT
  497. {
  498. // Allow pos == size() both in C++98 mode, as v3 extension,
  499. // and in C++11 mode.
  500. __glibcxx_assert(__pos <= this->size());
  501. // In pedantic mode be strict in C++98 mode.
  502. _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L
  503. || __pos < this->size());
  504. this->_M_leak();
  505. return this->_M_data()[__pos];
  506. }
  507. /**
  508. * @brief Provides access to the data contained in the %string.
  509. * @param __n The index of the character to access.
  510. * @return Read-only (const) reference to the character.
  511. * @throw std::out_of_range If @a __n is an invalid index.
  512. *
  513. * This function provides for safer data access. The parameter
  514. * is first checked that it is in the range of the string. The
  515. * function throws out_of_range if the check fails.
  516. */
  517. const_reference
  518. at(size_type __n) const
  519. {
  520. if (__n >= this->size())
  521. std::__throw_out_of_range_fmt(__N("__versa_string::at: __n "
  522. "(which is %zu) >= this->size() "
  523. "(which is %zu)"),
  524. __n, this->size());
  525. return this->_M_data()[__n];
  526. }
  527. /**
  528. * @brief Provides access to the data contained in the %string.
  529. * @param __n The index of the character to access.
  530. * @return Read/write reference to the character.
  531. * @throw std::out_of_range If @a __n is an invalid index.
  532. *
  533. * This function provides for safer data access. The parameter
  534. * is first checked that it is in the range of the string. The
  535. * function throws out_of_range if the check fails. Success
  536. * results in unsharing the string.
  537. */
  538. reference
  539. at(size_type __n)
  540. {
  541. if (__n >= this->size())
  542. std::__throw_out_of_range_fmt(__N("__versa_string::at: __n "
  543. "(which is %zu) >= this->size() "
  544. "(which is %zu)"),
  545. __n, this->size());
  546. this->_M_leak();
  547. return this->_M_data()[__n];
  548. }
  549. #if __cplusplus >= 201103L
  550. /**
  551. * Returns a read/write reference to the data at the first
  552. * element of the %string.
  553. */
  554. reference
  555. front() noexcept
  556. { return operator[](0); }
  557. /**
  558. * Returns a read-only (constant) reference to the data at the first
  559. * element of the %string.
  560. */
  561. const_reference
  562. front() const noexcept
  563. { return operator[](0); }
  564. /**
  565. * Returns a read/write reference to the data at the last
  566. * element of the %string.
  567. */
  568. reference
  569. back() noexcept
  570. { return operator[](this->size() - 1); }
  571. /**
  572. * Returns a read-only (constant) reference to the data at the
  573. * last element of the %string.
  574. */
  575. const_reference
  576. back() const noexcept
  577. { return operator[](this->size() - 1); }
  578. #endif
  579. // Modifiers:
  580. /**
  581. * @brief Append a string to this string.
  582. * @param __str The string to append.
  583. * @return Reference to this string.
  584. */
  585. __versa_string&
  586. operator+=(const __versa_string& __str)
  587. { return this->append(__str); }
  588. /**
  589. * @brief Append a C string.
  590. * @param __s The C string to append.
  591. * @return Reference to this string.
  592. */
  593. __versa_string&
  594. operator+=(const _CharT* __s)
  595. { return this->append(__s); }
  596. /**
  597. * @brief Append a character.
  598. * @param __c The character to append.
  599. * @return Reference to this string.
  600. */
  601. __versa_string&
  602. operator+=(_CharT __c)
  603. {
  604. this->push_back(__c);
  605. return *this;
  606. }
  607. #if __cplusplus >= 201103L
  608. /**
  609. * @brief Append an initializer_list of characters.
  610. * @param __l The initializer_list of characters to be appended.
  611. * @return Reference to this string.
  612. */
  613. __versa_string&
  614. operator+=(std::initializer_list<_CharT> __l)
  615. { return this->append(__l.begin(), __l.end()); }
  616. #endif // C++11
  617. /**
  618. * @brief Append a string to this string.
  619. * @param __str The string to append.
  620. * @return Reference to this string.
  621. */
  622. __versa_string&
  623. append(const __versa_string& __str)
  624. { return _M_append(__str._M_data(), __str.size()); }
  625. /**
  626. * @brief Append a substring.
  627. * @param __str The string to append.
  628. * @param __pos Index of the first character of str to append.
  629. * @param __n The number of characters to append.
  630. * @return Reference to this string.
  631. * @throw std::out_of_range if @a pos is not a valid index.
  632. *
  633. * This function appends @a __n characters from @a __str
  634. * starting at @a __pos to this string. If @a __n is is larger
  635. * than the number of available characters in @a __str, the
  636. * remainder of @a __str is appended.
  637. */
  638. __versa_string&
  639. append(const __versa_string& __str, size_type __pos, size_type __n)
  640. { return _M_append(__str._M_data()
  641. + __str._M_check(__pos, "__versa_string::append"),
  642. __str._M_limit(__pos, __n)); }
  643. /**
  644. * @brief Append a C substring.
  645. * @param __s The C string to append.
  646. * @param __n The number of characters to append.
  647. * @return Reference to this string.
  648. */
  649. __versa_string&
  650. append(const _CharT* __s, size_type __n)
  651. {
  652. __glibcxx_requires_string_len(__s, __n);
  653. _M_check_length(size_type(0), __n, "__versa_string::append");
  654. return _M_append(__s, __n);
  655. }
  656. /**
  657. * @brief Append a C string.
  658. * @param __s The C string to append.
  659. * @return Reference to this string.
  660. */
  661. __versa_string&
  662. append(const _CharT* __s)
  663. {
  664. __glibcxx_requires_string(__s);
  665. const size_type __n = traits_type::length(__s);
  666. _M_check_length(size_type(0), __n, "__versa_string::append");
  667. return _M_append(__s, __n);
  668. }
  669. /**
  670. * @brief Append multiple characters.
  671. * @param __n The number of characters to append.
  672. * @param __c The character to use.
  673. * @return Reference to this string.
  674. *
  675. * Appends n copies of c to this string.
  676. */
  677. __versa_string&
  678. append(size_type __n, _CharT __c)
  679. { return _M_replace_aux(this->size(), size_type(0), __n, __c); }
  680. #if __cplusplus >= 201103L
  681. /**
  682. * @brief Append an initializer_list of characters.
  683. * @param __l The initializer_list of characters to append.
  684. * @return Reference to this string.
  685. */
  686. __versa_string&
  687. append(std::initializer_list<_CharT> __l)
  688. { return this->append(__l.begin(), __l.end()); }
  689. #endif // C++11
  690. /**
  691. * @brief Append a range of characters.
  692. * @param __first Iterator referencing the first character to append.
  693. * @param __last Iterator marking the end of the range.
  694. * @return Reference to this string.
  695. *
  696. * Appends characters in the range [first,last) to this string.
  697. */
  698. #if __cplusplus >= 201103L
  699. template<class _InputIterator,
  700. typename = std::_RequireInputIter<_InputIterator>>
  701. #else
  702. template<class _InputIterator>
  703. #endif
  704. __versa_string&
  705. append(_InputIterator __first, _InputIterator __last)
  706. { return this->replace(_M_iend(), _M_iend(), __first, __last); }
  707. /**
  708. * @brief Append a single character.
  709. * @param __c Character to append.
  710. */
  711. void
  712. push_back(_CharT __c)
  713. {
  714. const size_type __size = this->size();
  715. if (__size + 1 > this->capacity() || this->_M_is_shared())
  716. this->_M_mutate(__size, size_type(0), 0, size_type(1));
  717. traits_type::assign(this->_M_data()[__size], __c);
  718. this->_M_set_length(__size + 1);
  719. }
  720. /**
  721. * @brief Set value to contents of another string.
  722. * @param __str Source string to use.
  723. * @return Reference to this string.
  724. */
  725. __versa_string&
  726. assign(const __versa_string& __str)
  727. {
  728. this->_M_assign(__str);
  729. return *this;
  730. }
  731. #if __cplusplus >= 201103L
  732. /**
  733. * @brief Set value to contents of another string.
  734. * @param __str Source string to use.
  735. * @return Reference to this string.
  736. *
  737. * This function sets this string to the exact contents of @a __str.
  738. * @a __str is a valid, but unspecified string.
  739. */
  740. __versa_string&
  741. assign(__versa_string&& __str) noexcept
  742. {
  743. this->swap(__str);
  744. return *this;
  745. }
  746. #endif // C++11
  747. /**
  748. * @brief Set value to a substring of a string.
  749. * @param __str The string to use.
  750. * @param __pos Index of the first character of str.
  751. * @param __n Number of characters to use.
  752. * @return Reference to this string.
  753. * @throw std::out_of_range if @a __pos is not a valid index.
  754. *
  755. * This function sets this string to the substring of @a __str
  756. * consisting of @a __n characters at @a __pos. If @a __n is
  757. * is larger than the number of available characters in @a
  758. * __str, the remainder of @a __str is used.
  759. */
  760. __versa_string&
  761. assign(const __versa_string& __str, size_type __pos, size_type __n)
  762. { return _M_replace(size_type(0), this->size(), __str._M_data()
  763. + __str._M_check(__pos, "__versa_string::assign"),
  764. __str._M_limit(__pos, __n)); }
  765. /**
  766. * @brief Set value to a C substring.
  767. * @param __s The C string to use.
  768. * @param __n Number of characters to use.
  769. * @return Reference to this string.
  770. *
  771. * This function sets the value of this string to the first @a
  772. * __n characters of @a __s. If @a __n is is larger than the
  773. * number of available characters in @a __s, the remainder of
  774. * @a __s is used.
  775. */
  776. __versa_string&
  777. assign(const _CharT* __s, size_type __n)
  778. {
  779. __glibcxx_requires_string_len(__s, __n);
  780. return _M_replace(size_type(0), this->size(), __s, __n);
  781. }
  782. /**
  783. * @brief Set value to contents of a C string.
  784. * @param __s The C string to use.
  785. * @return Reference to this string.
  786. *
  787. * This function sets the value of this string to the value of
  788. * @a __s. The data is copied, so there is no dependence on @a
  789. * __s once the function returns.
  790. */
  791. __versa_string&
  792. assign(const _CharT* __s)
  793. {
  794. __glibcxx_requires_string(__s);
  795. return _M_replace(size_type(0), this->size(), __s,
  796. traits_type::length(__s));
  797. }
  798. /**
  799. * @brief Set value to multiple characters.
  800. * @param __n Length of the resulting string.
  801. * @param __c The character to use.
  802. * @return Reference to this string.
  803. *
  804. * This function sets the value of this string to @a __n copies of
  805. * character @a __c.
  806. */
  807. __versa_string&
  808. assign(size_type __n, _CharT __c)
  809. { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
  810. /**
  811. * @brief Set value to a range of characters.
  812. * @param __first Iterator referencing the first character to append.
  813. * @param __last Iterator marking the end of the range.
  814. * @return Reference to this string.
  815. *
  816. * Sets value of string to characters in the range
  817. * [first,last).
  818. */
  819. #if __cplusplus >= 201103L
  820. template<class _InputIterator,
  821. typename = std::_RequireInputIter<_InputIterator>>
  822. #else
  823. template<class _InputIterator>
  824. #endif
  825. __versa_string&
  826. assign(_InputIterator __first, _InputIterator __last)
  827. { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
  828. #if __cplusplus >= 201103L
  829. /**
  830. * @brief Set value to an initializer_list of characters.
  831. * @param __l The initializer_list of characters to assign.
  832. * @return Reference to this string.
  833. */
  834. __versa_string&
  835. assign(std::initializer_list<_CharT> __l)
  836. { return this->assign(__l.begin(), __l.end()); }
  837. #endif // C++11
  838. #if __cplusplus >= 201103L
  839. /**
  840. * @brief Insert multiple characters.
  841. * @param __p Const_iterator referencing location in string to
  842. * insert at.
  843. * @param __n Number of characters to insert
  844. * @param __c The character to insert.
  845. * @return Iterator referencing the first inserted char.
  846. * @throw std::length_error If new length exceeds @c max_size().
  847. *
  848. * Inserts @a __n copies of character @a __c starting at the
  849. * position referenced by iterator @a __p. If adding
  850. * characters causes the length to exceed max_size(),
  851. * length_error is thrown. The value of the string doesn't
  852. * change if an error is thrown.
  853. */
  854. iterator
  855. insert(const_iterator __p, size_type __n, _CharT __c)
  856. {
  857. _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
  858. const size_type __pos = __p - _M_ibegin();
  859. this->replace(__p, __p, __n, __c);
  860. return iterator(this->_M_data() + __pos);
  861. }
  862. #else
  863. /**
  864. * @brief Insert multiple characters.
  865. * @param __p Iterator referencing location in string to insert at.
  866. * @param __n Number of characters to insert
  867. * @param __c The character to insert.
  868. * @throw std::length_error If new length exceeds @c max_size().
  869. *
  870. * Inserts @a __n copies of character @a __c starting at the
  871. * position referenced by iterator @a __p. If adding
  872. * characters causes the length to exceed max_size(),
  873. * length_error is thrown. The value of the string doesn't
  874. * change if an error is thrown.
  875. */
  876. void
  877. insert(iterator __p, size_type __n, _CharT __c)
  878. { this->replace(__p, __p, __n, __c); }
  879. #endif
  880. #if __cplusplus >= 201103L
  881. /**
  882. * @brief Insert a range of characters.
  883. * @param __p Const_iterator referencing location in string to
  884. * insert at.
  885. * @param __beg Start of range.
  886. * @param __end End of range.
  887. * @return Iterator referencing the first inserted char.
  888. * @throw std::length_error If new length exceeds @c max_size().
  889. *
  890. * Inserts characters in range [beg,end). If adding characters
  891. * causes the length to exceed max_size(), length_error is
  892. * thrown. The value of the string doesn't change if an error
  893. * is thrown.
  894. */
  895. template<class _InputIterator,
  896. typename = std::_RequireInputIter<_InputIterator>>
  897. iterator
  898. insert(const_iterator __p, _InputIterator __beg, _InputIterator __end)
  899. {
  900. _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
  901. const size_type __pos = __p - _M_ibegin();
  902. this->replace(__p, __p, __beg, __end);
  903. return iterator(this->_M_data() + __pos);
  904. }
  905. #else
  906. /**
  907. * @brief Insert a range of characters.
  908. * @param __p Iterator referencing location in string to insert at.
  909. * @param __beg Start of range.
  910. * @param __end End of range.
  911. * @throw std::length_error If new length exceeds @c max_size().
  912. *
  913. * Inserts characters in range [beg,end). If adding characters
  914. * causes the length to exceed max_size(), length_error is
  915. * thrown. The value of the string doesn't change if an error
  916. * is thrown.
  917. */
  918. template<class _InputIterator>
  919. void
  920. insert(iterator __p, _InputIterator __beg, _InputIterator __end)
  921. { this->replace(__p, __p, __beg, __end); }
  922. #endif
  923. #if __cplusplus >= 201103L
  924. /**
  925. * @brief Insert an initializer_list of characters.
  926. * @param __p Const_iterator referencing location in string to
  927. * insert at.
  928. * @param __l The initializer_list of characters to insert.
  929. * @return Iterator referencing the first inserted char.
  930. * @throw std::length_error If new length exceeds @c max_size().
  931. */
  932. iterator
  933. insert(const_iterator __p, std::initializer_list<_CharT> __l)
  934. { return this->insert(__p, __l.begin(), __l.end()); }
  935. #endif // C++11
  936. /**
  937. * @brief Insert value of a string.
  938. * @param __pos1 Iterator referencing location in string to insert at.
  939. * @param __str The string to insert.
  940. * @return Reference to this string.
  941. * @throw std::length_error If new length exceeds @c max_size().
  942. *
  943. * Inserts value of @a __str starting at @a __pos1. If adding
  944. * characters causes the length to exceed max_size(),
  945. * length_error is thrown. The value of the string doesn't
  946. * change if an error is thrown.
  947. */
  948. __versa_string&
  949. insert(size_type __pos1, const __versa_string& __str)
  950. { return this->replace(__pos1, size_type(0),
  951. __str._M_data(), __str.size()); }
  952. /**
  953. * @brief Insert a substring.
  954. * @param __pos1 Iterator referencing location in string to insert at.
  955. * @param __str The string to insert.
  956. * @param __pos2 Start of characters in str to insert.
  957. * @param __n Number of characters to insert.
  958. * @return Reference to this string.
  959. * @throw std::length_error If new length exceeds @c max_size().
  960. * @throw std::out_of_range If @a __pos1 > size() or
  961. * @a __pos2 > @a __str.size().
  962. *
  963. * Starting at @a __pos1, insert @a __n character of @a __str
  964. * beginning with @a __pos2. If adding characters causes the
  965. * length to exceed max_size(), length_error is thrown. If @a
  966. * __pos1 is beyond the end of this string or @a __pos2 is
  967. * beyond the end of @a __str, out_of_range is thrown. The
  968. * value of the string doesn't change if an error is thrown.
  969. */
  970. __versa_string&
  971. insert(size_type __pos1, const __versa_string& __str,
  972. size_type __pos2, size_type __n)
  973. { return this->replace(__pos1, size_type(0), __str._M_data()
  974. + __str._M_check(__pos2, "__versa_string::insert"),
  975. __str._M_limit(__pos2, __n)); }
  976. /**
  977. * @brief Insert a C substring.
  978. * @param __pos Iterator referencing location in string to insert at.
  979. * @param __s The C string to insert.
  980. * @param __n The number of characters to insert.
  981. * @return Reference to this string.
  982. * @throw std::length_error If new length exceeds @c max_size().
  983. * @throw std::out_of_range If @a __pos is beyond the end of this
  984. * string.
  985. *
  986. * Inserts the first @a __n characters of @a __s starting at @a
  987. * __pos. If adding characters causes the length to exceed
  988. * max_size(), length_error is thrown. If @a __pos is beyond
  989. * end(), out_of_range is thrown. The value of the string
  990. * doesn't change if an error is thrown.
  991. */
  992. __versa_string&
  993. insert(size_type __pos, const _CharT* __s, size_type __n)
  994. { return this->replace(__pos, size_type(0), __s, __n); }
  995. /**
  996. * @brief Insert a C string.
  997. * @param __pos Iterator referencing location in string to insert at.
  998. * @param __s The C string to insert.
  999. * @return Reference to this string.
  1000. * @throw std::length_error If new length exceeds @c max_size().
  1001. * @throw std::out_of_range If @a __pos is beyond the end of this
  1002. * string.
  1003. *
  1004. * Inserts the first @a __n characters of @a __s starting at @a
  1005. * __pos. If adding characters causes the length to exceed
  1006. * max_size(), length_error is thrown. If @a __pos is beyond
  1007. * end(), out_of_range is thrown. The value of the string
  1008. * doesn't change if an error is thrown.
  1009. */
  1010. __versa_string&
  1011. insert(size_type __pos, const _CharT* __s)
  1012. {
  1013. __glibcxx_requires_string(__s);
  1014. return this->replace(__pos, size_type(0), __s,
  1015. traits_type::length(__s));
  1016. }
  1017. /**
  1018. * @brief Insert multiple characters.
  1019. * @param __pos Index in string to insert at.
  1020. * @param __n Number of characters to insert
  1021. * @param __c The character to insert.
  1022. * @return Reference to this string.
  1023. * @throw std::length_error If new length exceeds @c max_size().
  1024. * @throw std::out_of_range If @a __pos is beyond the end of this
  1025. * string.
  1026. *
  1027. * Inserts @a __n copies of character @a __c starting at index
  1028. * @a __pos. If adding characters causes the length to exceed
  1029. * max_size(), length_error is thrown. If @a __pos > length(),
  1030. * out_of_range is thrown. The value of the string doesn't
  1031. * change if an error is thrown.
  1032. */
  1033. __versa_string&
  1034. insert(size_type __pos, size_type __n, _CharT __c)
  1035. { return _M_replace_aux(_M_check(__pos, "__versa_string::insert"),
  1036. size_type(0), __n, __c); }
  1037. /**
  1038. * @brief Insert one character.
  1039. * @param __p Iterator referencing position in string to insert at.
  1040. * @param __c The character to insert.
  1041. * @return Iterator referencing newly inserted char.
  1042. * @throw std::length_error If new length exceeds @c max_size().
  1043. *
  1044. * Inserts character @a __c at position referenced by @a __p.
  1045. * If adding character causes the length to exceed max_size(),
  1046. * length_error is thrown. If @a __p is beyond end of string,
  1047. * out_of_range is thrown. The value of the string doesn't
  1048. * change if an error is thrown.
  1049. */
  1050. iterator
  1051. #if __cplusplus >= 201103L
  1052. insert(const_iterator __p, _CharT __c)
  1053. #else
  1054. insert(iterator __p, _CharT __c)
  1055. #endif
  1056. {
  1057. _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
  1058. const size_type __pos = __p - _M_ibegin();
  1059. _M_replace_aux(__pos, size_type(0), size_type(1), __c);
  1060. this->_M_set_leaked();
  1061. return iterator(this->_M_data() + __pos);
  1062. }
  1063. /**
  1064. * @brief Remove characters.
  1065. * @param __pos Index of first character to remove (default 0).
  1066. * @param __n Number of characters to remove (default remainder).
  1067. * @return Reference to this string.
  1068. * @throw std::out_of_range If @a __pos is beyond the end of this
  1069. * string.
  1070. *
  1071. * Removes @a __n characters from this string starting at @a
  1072. * __pos. The length of the string is reduced by @a __n. If
  1073. * there are < @a __n characters to remove, the remainder of
  1074. * the string is truncated. If @a __p is beyond end of string,
  1075. * out_of_range is thrown. The value of the string doesn't
  1076. * change if an error is thrown.
  1077. */
  1078. __versa_string&
  1079. erase(size_type __pos = 0, size_type __n = npos)
  1080. {
  1081. this->_M_erase(_M_check(__pos, "__versa_string::erase"),
  1082. _M_limit(__pos, __n));
  1083. return *this;
  1084. }
  1085. /**
  1086. * @brief Remove one character.
  1087. * @param __position Iterator referencing the character to remove.
  1088. * @return iterator referencing same location after removal.
  1089. *
  1090. * Removes the character at @a __position from this string. The
  1091. * value of the string doesn't change if an error is thrown.
  1092. */
  1093. iterator
  1094. #if __cplusplus >= 201103L
  1095. erase(const_iterator __position)
  1096. #else
  1097. erase(iterator __position)
  1098. #endif
  1099. {
  1100. _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
  1101. && __position < _M_iend());
  1102. const size_type __pos = __position - _M_ibegin();
  1103. this->_M_erase(__pos, size_type(1));
  1104. this->_M_set_leaked();
  1105. return iterator(this->_M_data() + __pos);
  1106. }
  1107. /**
  1108. * @brief Remove a range of characters.
  1109. * @param __first Iterator referencing the first character to remove.
  1110. * @param __last Iterator referencing the end of the range.
  1111. * @return Iterator referencing location of first after removal.
  1112. *
  1113. * Removes the characters in the range [first,last) from this
  1114. * string. The value of the string doesn't change if an error
  1115. * is thrown.
  1116. */
  1117. iterator
  1118. #if __cplusplus >= 201103L
  1119. erase(const_iterator __first, const_iterator __last)
  1120. #else
  1121. erase(iterator __first, iterator __last)
  1122. #endif
  1123. {
  1124. _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
  1125. && __last <= _M_iend());
  1126. const size_type __pos = __first - _M_ibegin();
  1127. this->_M_erase(__pos, __last - __first);
  1128. this->_M_set_leaked();
  1129. return iterator(this->_M_data() + __pos);
  1130. }
  1131. #if __cplusplus >= 201103L
  1132. /**
  1133. * @brief Remove the last character.
  1134. *
  1135. * The string must be non-empty.
  1136. */
  1137. void
  1138. pop_back()
  1139. { this->_M_erase(size()-1, 1); }
  1140. #endif // C++11
  1141. /**
  1142. * @brief Replace characters with value from another string.
  1143. * @param __pos Index of first character to replace.
  1144. * @param __n Number of characters to be replaced.
  1145. * @param __str String to insert.
  1146. * @return Reference to this string.
  1147. * @throw std::out_of_range If @a __pos is beyond the end of this
  1148. * string.
  1149. * @throw std::length_error If new length exceeds @c max_size().
  1150. *
  1151. * Removes the characters in the range [pos,pos+n) from this
  1152. * string. In place, the value of @a __str is inserted. If @a
  1153. * __pos is beyond end of string, out_of_range is thrown. If
  1154. * the length of the result exceeds max_size(), length_error is
  1155. * thrown. The value of the string doesn't change if an error
  1156. * is thrown.
  1157. */
  1158. __versa_string&
  1159. replace(size_type __pos, size_type __n, const __versa_string& __str)
  1160. { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
  1161. /**
  1162. * @brief Replace characters with value from another string.
  1163. * @param __pos1 Index of first character to replace.
  1164. * @param __n1 Number of characters to be replaced.
  1165. * @param __str String to insert.
  1166. * @param __pos2 Index of first character of str to use.
  1167. * @param __n2 Number of characters from str to use.
  1168. * @return Reference to this string.
  1169. * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
  1170. * str.size().
  1171. * @throw std::length_error If new length exceeds @c max_size().
  1172. *
  1173. * Removes the characters in the range [pos1,pos1 + n) from
  1174. * this string. In place, the value of @a __str is inserted.
  1175. * If @a __pos is beyond end of string, out_of_range is thrown.
  1176. * If the length of the result exceeds max_size(), length_error
  1177. * is thrown. The value of the string doesn't change if an
  1178. * error is thrown.
  1179. */
  1180. __versa_string&
  1181. replace(size_type __pos1, size_type __n1, const __versa_string& __str,
  1182. size_type __pos2, size_type __n2)
  1183. {
  1184. return this->replace(__pos1, __n1, __str._M_data()
  1185. + __str._M_check(__pos2,
  1186. "__versa_string::replace"),
  1187. __str._M_limit(__pos2, __n2));
  1188. }
  1189. /**
  1190. * @brief Replace characters with value of a C substring.
  1191. * @param __pos Index of first character to replace.
  1192. * @param __n1 Number of characters to be replaced.
  1193. * @param __s C string to insert.
  1194. * @param __n2 Number of characters from @a __s to use.
  1195. * @return Reference to this string.
  1196. * @throw std::out_of_range If @a __pos1 > size().
  1197. * @throw std::length_error If new length exceeds @c max_size().
  1198. *
  1199. * Removes the characters in the range [pos,pos + n1) from this
  1200. * string. In place, the first @a __n2 characters of @a __s
  1201. * are inserted, or all of @a __s if @a __n2 is too large. If
  1202. * @a __pos is beyond end of string, out_of_range is thrown.
  1203. * If the length of result exceeds max_size(), length_error is
  1204. * thrown. The value of the string doesn't change if an error
  1205. * is thrown.
  1206. */
  1207. __versa_string&
  1208. replace(size_type __pos, size_type __n1, const _CharT* __s,
  1209. size_type __n2)
  1210. {
  1211. __glibcxx_requires_string_len(__s, __n2);
  1212. return _M_replace(_M_check(__pos, "__versa_string::replace"),
  1213. _M_limit(__pos, __n1), __s, __n2);
  1214. }
  1215. /**
  1216. * @brief Replace characters with value of a C string.
  1217. * @param __pos Index of first character to replace.
  1218. * @param __n1 Number of characters to be replaced.
  1219. * @param __s C string to insert.
  1220. * @return Reference to this string.
  1221. * @throw std::out_of_range If @a __pos > size().
  1222. * @throw std::length_error If new length exceeds @c max_size().
  1223. *
  1224. * Removes the characters in the range [pos,pos + n1) from this
  1225. * string. In place, the characters of @a __s are inserted. If
  1226. * @a pos is beyond end of string, out_of_range is thrown. If
  1227. * the length of result exceeds max_size(), length_error is thrown.
  1228. * The value of the string doesn't change if an error is thrown.
  1229. */
  1230. __versa_string&
  1231. replace(size_type __pos, size_type __n1, const _CharT* __s)
  1232. {
  1233. __glibcxx_requires_string(__s);
  1234. return this->replace(__pos, __n1, __s, traits_type::length(__s));
  1235. }
  1236. /**
  1237. * @brief Replace characters with multiple characters.
  1238. * @param __pos Index of first character to replace.
  1239. * @param __n1 Number of characters to be replaced.
  1240. * @param __n2 Number of characters to insert.
  1241. * @param __c Character to insert.
  1242. * @return Reference to this string.
  1243. * @throw std::out_of_range If @a __pos > size().
  1244. * @throw std::length_error If new length exceeds @c max_size().
  1245. *
  1246. * Removes the characters in the range [pos,pos + n1) from this
  1247. * string. In place, @a __n2 copies of @a __c are inserted.
  1248. * If @a __pos is beyond end of string, out_of_range is thrown.
  1249. * If the length of result exceeds max_size(), length_error is
  1250. * thrown. The value of the string doesn't change if an error
  1251. * is thrown.
  1252. */
  1253. __versa_string&
  1254. replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
  1255. { return _M_replace_aux(_M_check(__pos, "__versa_string::replace"),
  1256. _M_limit(__pos, __n1), __n2, __c); }
  1257. /**
  1258. * @brief Replace range of characters with string.
  1259. * @param __i1 Iterator referencing start of range to replace.
  1260. * @param __i2 Iterator referencing end of range to replace.
  1261. * @param __str String value to insert.
  1262. * @return Reference to this string.
  1263. * @throw std::length_error If new length exceeds @c max_size().
  1264. *
  1265. * Removes the characters in the range [i1,i2). In place, the
  1266. * value of @a __str is inserted. If the length of result
  1267. * exceeds max_size(), length_error is thrown. The value of
  1268. * the string doesn't change if an error is thrown.
  1269. */
  1270. __versa_string&
  1271. #if __cplusplus >= 201103L
  1272. replace(const_iterator __i1, const_iterator __i2,
  1273. const __versa_string& __str)
  1274. #else
  1275. replace(iterator __i1, iterator __i2, const __versa_string& __str)
  1276. #endif
  1277. { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
  1278. /**
  1279. * @brief Replace range of characters with C substring.
  1280. * @param __i1 Iterator referencing start of range to replace.
  1281. * @param __i2 Iterator referencing end of range to replace.
  1282. * @param __s C string value to insert.
  1283. * @param __n Number of characters from s to insert.
  1284. * @return Reference to this string.
  1285. * @throw std::length_error If new length exceeds @c max_size().
  1286. *
  1287. * Removes the characters in the range [i1,i2). In place, the
  1288. * first @a n characters of @a __s are inserted. If the length
  1289. * of result exceeds max_size(), length_error is thrown. The
  1290. * value of the string doesn't change if an error is thrown.
  1291. */
  1292. __versa_string&
  1293. #if __cplusplus >= 201103L
  1294. replace(const_iterator __i1, const_iterator __i2,
  1295. const _CharT* __s, size_type __n)
  1296. #else
  1297. replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
  1298. #endif
  1299. {
  1300. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1301. && __i2 <= _M_iend());
  1302. return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
  1303. }
  1304. /**
  1305. * @brief Replace range of characters with C string.
  1306. * @param __i1 Iterator referencing start of range to replace.
  1307. * @param __i2 Iterator referencing end of range to replace.
  1308. * @param __s C string value to insert.
  1309. * @return Reference to this string.
  1310. * @throw std::length_error If new length exceeds @c max_size().
  1311. *
  1312. * Removes the characters in the range [i1,i2). In place, the
  1313. * characters of @a __s are inserted. If the length of result
  1314. * exceeds max_size(), length_error is thrown. The value of
  1315. * the string doesn't change if an error is thrown.
  1316. */
  1317. __versa_string&
  1318. #if __cplusplus >= 201103L
  1319. replace(const_iterator __i1, const_iterator __i2, const _CharT* __s)
  1320. #else
  1321. replace(iterator __i1, iterator __i2, const _CharT* __s)
  1322. #endif
  1323. {
  1324. __glibcxx_requires_string(__s);
  1325. return this->replace(__i1, __i2, __s, traits_type::length(__s));
  1326. }
  1327. /**
  1328. * @brief Replace range of characters with multiple characters
  1329. * @param __i1 Iterator referencing start of range to replace.
  1330. * @param __i2 Iterator referencing end of range to replace.
  1331. * @param __n Number of characters to insert.
  1332. * @param __c Character to insert.
  1333. * @return Reference to this string.
  1334. * @throw std::length_error If new length exceeds @c max_size().
  1335. *
  1336. * Removes the characters in the range [i1,i2). In place, @a
  1337. * __n copies of @a __c are inserted. If the length of result
  1338. * exceeds max_size(), length_error is thrown. The value of
  1339. * the string doesn't change if an error is thrown.
  1340. */
  1341. __versa_string&
  1342. #if __cplusplus >= 201103L
  1343. replace(const_iterator __i1, const_iterator __i2, size_type __n,
  1344. _CharT __c)
  1345. #else
  1346. replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
  1347. #endif
  1348. {
  1349. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1350. && __i2 <= _M_iend());
  1351. return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
  1352. }
  1353. /**
  1354. * @brief Replace range of characters with range.
  1355. * @param __i1 Iterator referencing start of range to replace.
  1356. * @param __i2 Iterator referencing end of range to replace.
  1357. * @param __k1 Iterator referencing start of range to insert.
  1358. * @param __k2 Iterator referencing end of range to insert.
  1359. * @return Reference to this string.
  1360. * @throw std::length_error If new length exceeds @c max_size().
  1361. *
  1362. * Removes the characters in the range [i1,i2). In place,
  1363. * characters in the range [k1,k2) are inserted. If the length
  1364. * of result exceeds max_size(), length_error is thrown. The
  1365. * value of the string doesn't change if an error is thrown.
  1366. */
  1367. #if __cplusplus >= 201103L
  1368. template<class _InputIterator,
  1369. typename = std::_RequireInputIter<_InputIterator>>
  1370. __versa_string&
  1371. replace(const_iterator __i1, const_iterator __i2,
  1372. _InputIterator __k1, _InputIterator __k2)
  1373. {
  1374. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1375. && __i2 <= _M_iend());
  1376. __glibcxx_requires_valid_range(__k1, __k2);
  1377. return this->_M_replace_dispatch(__i1, __i2, __k1, __k2,
  1378. std::__false_type());
  1379. }
  1380. #else
  1381. template<class _InputIterator>
  1382. __versa_string&
  1383. replace(iterator __i1, iterator __i2,
  1384. _InputIterator __k1, _InputIterator __k2)
  1385. {
  1386. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1387. && __i2 <= _M_iend());
  1388. __glibcxx_requires_valid_range(__k1, __k2);
  1389. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  1390. return this->_M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
  1391. }
  1392. #endif
  1393. // Specializations for the common case of pointer and iterator:
  1394. // useful to avoid the overhead of temporary buffering in _M_replace.
  1395. __versa_string&
  1396. #if __cplusplus >= 201103L
  1397. replace(const_iterator __i1, const_iterator __i2,
  1398. _CharT* __k1, _CharT* __k2)
  1399. #else
  1400. replace(iterator __i1, iterator __i2,
  1401. _CharT* __k1, _CharT* __k2)
  1402. #endif
  1403. {
  1404. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1405. && __i2 <= _M_iend());
  1406. __glibcxx_requires_valid_range(__k1, __k2);
  1407. return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
  1408. __k1, __k2 - __k1);
  1409. }
  1410. __versa_string&
  1411. #if __cplusplus >= 201103L
  1412. replace(const_iterator __i1, const_iterator __i2,
  1413. const _CharT* __k1, const _CharT* __k2)
  1414. #else
  1415. replace(iterator __i1, iterator __i2,
  1416. const _CharT* __k1, const _CharT* __k2)
  1417. #endif
  1418. {
  1419. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1420. && __i2 <= _M_iend());
  1421. __glibcxx_requires_valid_range(__k1, __k2);
  1422. return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
  1423. __k1, __k2 - __k1);
  1424. }
  1425. __versa_string&
  1426. #if __cplusplus >= 201103L
  1427. replace(const_iterator __i1, const_iterator __i2,
  1428. iterator __k1, iterator __k2)
  1429. #else
  1430. replace(iterator __i1, iterator __i2,
  1431. iterator __k1, iterator __k2)
  1432. #endif
  1433. {
  1434. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1435. && __i2 <= _M_iend());
  1436. __glibcxx_requires_valid_range(__k1, __k2);
  1437. return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
  1438. __k1.base(), __k2 - __k1);
  1439. }
  1440. __versa_string&
  1441. #if __cplusplus >= 201103L
  1442. replace(const_iterator __i1, const_iterator __i2,
  1443. const_iterator __k1, const_iterator __k2)
  1444. #else
  1445. replace(iterator __i1, iterator __i2,
  1446. const_iterator __k1, const_iterator __k2)
  1447. #endif
  1448. {
  1449. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1450. && __i2 <= _M_iend());
  1451. __glibcxx_requires_valid_range(__k1, __k2);
  1452. return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
  1453. __k1.base(), __k2 - __k1);
  1454. }
  1455. #if __cplusplus >= 201103L
  1456. /**
  1457. * @brief Replace range of characters with initializer_list.
  1458. * @param __i1 Iterator referencing start of range to replace.
  1459. * @param __i2 Iterator referencing end of range to replace.
  1460. * @param __l The initializer_list of characters to insert.
  1461. * @return Reference to this string.
  1462. * @throw std::length_error If new length exceeds @c max_size().
  1463. *
  1464. * Removes the characters in the range [i1,i2). In place,
  1465. * characters in the range [k1,k2) are inserted. If the length
  1466. * of result exceeds max_size(), length_error is thrown. The
  1467. * value of the string doesn't change if an error is thrown.
  1468. */
  1469. __versa_string&
  1470. replace(const_iterator __i1, const_iterator __i2,
  1471. std::initializer_list<_CharT> __l)
  1472. { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
  1473. #endif // C++11
  1474. private:
  1475. template<class _Integer>
  1476. __versa_string&
  1477. _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
  1478. _Integer __n, _Integer __val, std::__true_type)
  1479. { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
  1480. template<class _InputIterator>
  1481. __versa_string&
  1482. _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
  1483. _InputIterator __k1, _InputIterator __k2,
  1484. std::__false_type);
  1485. __versa_string&
  1486. _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
  1487. _CharT __c);
  1488. __versa_string&
  1489. _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
  1490. const size_type __len2);
  1491. __versa_string&
  1492. _M_append(const _CharT* __s, size_type __n);
  1493. public:
  1494. /**
  1495. * @brief Copy substring into C string.
  1496. * @param __s C string to copy value into.
  1497. * @param __n Number of characters to copy.
  1498. * @param __pos Index of first character to copy.
  1499. * @return Number of characters actually copied
  1500. * @throw std::out_of_range If pos > size().
  1501. *
  1502. * Copies up to @a __n characters starting at @a __pos into the
  1503. * C string @a s. If @a __pos is greater than size(),
  1504. * out_of_range is thrown.
  1505. */
  1506. size_type
  1507. copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
  1508. /**
  1509. * @brief Swap contents with another string.
  1510. * @param __s String to swap with.
  1511. *
  1512. * Exchanges the contents of this string with that of @a __s in
  1513. * constant time.
  1514. */
  1515. void
  1516. swap(__versa_string& __s) _GLIBCXX_NOEXCEPT
  1517. { this->_M_swap(__s); }
  1518. // String operations:
  1519. /**
  1520. * @brief Return const pointer to null-terminated contents.
  1521. *
  1522. * This is a handle to internal data. Do not modify or dire things may
  1523. * happen.
  1524. */
  1525. const _CharT*
  1526. c_str() const _GLIBCXX_NOEXCEPT
  1527. { return this->_M_data(); }
  1528. /**
  1529. * @brief Return const pointer to contents.
  1530. *
  1531. * This is a handle to internal data. Do not modify or dire things may
  1532. * happen.
  1533. */
  1534. const _CharT*
  1535. data() const _GLIBCXX_NOEXCEPT
  1536. { return this->_M_data(); }
  1537. /**
  1538. * @brief Return copy of allocator used to construct this string.
  1539. */
  1540. allocator_type
  1541. get_allocator() const _GLIBCXX_NOEXCEPT
  1542. { return allocator_type(this->_M_get_allocator()); }
  1543. /**
  1544. * @brief Find position of a C substring.
  1545. * @param __s C string to locate.
  1546. * @param __pos Index of character to search from.
  1547. * @param __n Number of characters from @a __s to search for.
  1548. * @return Index of start of first occurrence.
  1549. *
  1550. * Starting from @a __pos, searches forward for the first @a
  1551. * __n characters in @a __s within this string. If found,
  1552. * returns the index where it begins. If not found, returns
  1553. * npos.
  1554. */
  1555. size_type
  1556. find(const _CharT* __s, size_type __pos, size_type __n) const;
  1557. /**
  1558. * @brief Find position of a string.
  1559. * @param __str String to locate.
  1560. * @param __pos Index of character to search from (default 0).
  1561. * @return Index of start of first occurrence.
  1562. *
  1563. * Starting from @a __pos, searches forward for value of @a
  1564. * __str within this string. If found, returns the index where
  1565. * it begins. If not found, returns npos.
  1566. */
  1567. size_type
  1568. find(const __versa_string& __str, size_type __pos = 0) const
  1569. _GLIBCXX_NOEXCEPT
  1570. { return this->find(__str.data(), __pos, __str.size()); }
  1571. /**
  1572. * @brief Find position of a C string.
  1573. * @param __s C string to locate.
  1574. * @param __pos Index of character to search from (default 0).
  1575. * @return Index of start of first occurrence.
  1576. *
  1577. * Starting from @a __pos, searches forward for the value of @a
  1578. * __s within this string. If found, returns the index where
  1579. * it begins. If not found, returns npos.
  1580. */
  1581. size_type
  1582. find(const _CharT* __s, size_type __pos = 0) const
  1583. {
  1584. __glibcxx_requires_string(__s);
  1585. return this->find(__s, __pos, traits_type::length(__s));
  1586. }
  1587. /**
  1588. * @brief Find position of a character.
  1589. * @param __c Character to locate.
  1590. * @param __pos Index of character to search from (default 0).
  1591. * @return Index of first occurrence.
  1592. *
  1593. * Starting from @a __pos, searches forward for @a __c within
  1594. * this string. If found, returns the index where it was
  1595. * found. If not found, returns npos.
  1596. */
  1597. size_type
  1598. find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
  1599. /**
  1600. * @brief Find last position of a string.
  1601. * @param __str String to locate.
  1602. * @param __pos Index of character to search back from (default end).
  1603. * @return Index of start of last occurrence.
  1604. *
  1605. * Starting from @a __pos, searches backward for value of @a
  1606. * __str within this string. If found, returns the index where
  1607. * it begins. If not found, returns npos.
  1608. */
  1609. size_type
  1610. rfind(const __versa_string& __str, size_type __pos = npos) const
  1611. _GLIBCXX_NOEXCEPT
  1612. { return this->rfind(__str.data(), __pos, __str.size()); }
  1613. /**
  1614. * @brief Find last position of a C substring.
  1615. * @param __s C string to locate.
  1616. * @param __pos Index of character to search back from.
  1617. * @param __n Number of characters from s to search for.
  1618. * @return Index of start of last occurrence.
  1619. *
  1620. * Starting from @a __pos, searches backward for the first @a
  1621. * __n characters in @a __s within this string. If found,
  1622. * returns the index where it begins. If not found, returns
  1623. * npos.
  1624. */
  1625. size_type
  1626. rfind(const _CharT* __s, size_type __pos, size_type __n) const;
  1627. /**
  1628. * @brief Find last position of a C string.
  1629. * @param __s C string to locate.
  1630. * @param __pos Index of character to start search at (default end).
  1631. * @return Index of start of last occurrence.
  1632. *
  1633. * Starting from @a __pos, searches backward for the value of
  1634. * @a __s within this string. If found, returns the index
  1635. * where it begins. If not found, returns npos.
  1636. */
  1637. size_type
  1638. rfind(const _CharT* __s, size_type __pos = npos) const
  1639. {
  1640. __glibcxx_requires_string(__s);
  1641. return this->rfind(__s, __pos, traits_type::length(__s));
  1642. }
  1643. /**
  1644. * @brief Find last position of a character.
  1645. * @param __c Character to locate.
  1646. * @param __pos Index of character to search back from (default end).
  1647. * @return Index of last occurrence.
  1648. *
  1649. * Starting from @a __pos, searches backward for @a __c within
  1650. * this string. If found, returns the index where it was
  1651. * found. If not found, returns npos.
  1652. */
  1653. size_type
  1654. rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
  1655. /**
  1656. * @brief Find position of a character of string.
  1657. * @param __str String containing characters to locate.
  1658. * @param __pos Index of character to search from (default 0).
  1659. * @return Index of first occurrence.
  1660. *
  1661. * Starting from @a __pos, searches forward for one of the characters of
  1662. * @a __str within this string. If found, returns the index where it was
  1663. * found. If not found, returns npos.
  1664. */
  1665. size_type
  1666. find_first_of(const __versa_string& __str, size_type __pos = 0) const
  1667. _GLIBCXX_NOEXCEPT
  1668. { return this->find_first_of(__str.data(), __pos, __str.size()); }
  1669. /**
  1670. * @brief Find position of a character of C substring.
  1671. * @param __s String containing characters to locate.
  1672. * @param __pos Index of character to search from.
  1673. * @param __n Number of characters from s to search for.
  1674. * @return Index of first occurrence.
  1675. *
  1676. * Starting from @a __pos, searches forward for one of the
  1677. * first @a __n characters of @a __s within this string. If
  1678. * found, returns the index where it was found. If not found,
  1679. * returns npos.
  1680. */
  1681. size_type
  1682. find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
  1683. /**
  1684. * @brief Find position of a character of C string.
  1685. * @param __s String containing characters to locate.
  1686. * @param __pos Index of character to search from (default 0).
  1687. * @return Index of first occurrence.
  1688. *
  1689. * Starting from @a __pos, searches forward for one of the
  1690. * characters of @a __s within this string. If found, returns
  1691. * the index where it was found. If not found, returns npos.
  1692. */
  1693. size_type
  1694. find_first_of(const _CharT* __s, size_type __pos = 0) const
  1695. {
  1696. __glibcxx_requires_string(__s);
  1697. return this->find_first_of(__s, __pos, traits_type::length(__s));
  1698. }
  1699. /**
  1700. * @brief Find position of a character.
  1701. * @param __c Character to locate.
  1702. * @param __pos Index of character to search from (default 0).
  1703. * @return Index of first occurrence.
  1704. *
  1705. * Starting from @a __pos, searches forward for the character
  1706. * @a __c within this string. If found, returns the index
  1707. * where it was found. If not found, returns npos.
  1708. *
  1709. * Note: equivalent to find(c, pos).
  1710. */
  1711. size_type
  1712. find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
  1713. { return this->find(__c, __pos); }
  1714. /**
  1715. * @brief Find last position of a character of string.
  1716. * @param __str String containing characters to locate.
  1717. * @param __pos Index of character to search back from (default end).
  1718. * @return Index of last occurrence.
  1719. *
  1720. * Starting from @a __pos, searches backward for one of the
  1721. * characters of @a __str within this string. If found,
  1722. * returns the index where it was found. If not found, returns
  1723. * npos.
  1724. */
  1725. size_type
  1726. find_last_of(const __versa_string& __str, size_type __pos = npos) const
  1727. _GLIBCXX_NOEXCEPT
  1728. { return this->find_last_of(__str.data(), __pos, __str.size()); }
  1729. /**
  1730. * @brief Find last position of a character of C substring.
  1731. * @param __s C string containing characters to locate.
  1732. * @param __pos Index of character to search back from.
  1733. * @param __n Number of characters from s to search for.
  1734. * @return Index of last occurrence.
  1735. *
  1736. * Starting from @a __pos, searches backward for one of the
  1737. * first @a __n characters of @a __s within this string. If
  1738. * found, returns the index where it was found. If not found,
  1739. * returns npos.
  1740. */
  1741. size_type
  1742. find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
  1743. /**
  1744. * @brief Find last position of a character of C string.
  1745. * @param __s C string containing characters to locate.
  1746. * @param __pos Index of character to search back from (default end).
  1747. * @return Index of last occurrence.
  1748. *
  1749. * Starting from @a __pos, searches backward for one of the
  1750. * characters of @a __s within this string. If found, returns
  1751. * the index where it was found. If not found, returns npos.
  1752. */
  1753. size_type
  1754. find_last_of(const _CharT* __s, size_type __pos = npos) const
  1755. {
  1756. __glibcxx_requires_string(__s);
  1757. return this->find_last_of(__s, __pos, traits_type::length(__s));
  1758. }
  1759. /**
  1760. * @brief Find last position of a character.
  1761. * @param __c Character to locate.
  1762. * @param __pos Index of character to search back from (default end).
  1763. * @return Index of last occurrence.
  1764. *
  1765. * Starting from @a __pos, searches backward for @a __c within
  1766. * this string. If found, returns the index where it was
  1767. * found. If not found, returns npos.
  1768. *
  1769. * Note: equivalent to rfind(c, pos).
  1770. */
  1771. size_type
  1772. find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
  1773. { return this->rfind(__c, __pos); }
  1774. /**
  1775. * @brief Find position of a character not in string.
  1776. * @param __str String containing characters to avoid.
  1777. * @param __pos Index of character to search from (default 0).
  1778. * @return Index of first occurrence.
  1779. *
  1780. * Starting from @a __pos, searches forward for a character not
  1781. * contained in @a __str within this string. If found, returns
  1782. * the index where it was found. If not found, returns npos.
  1783. */
  1784. size_type
  1785. find_first_not_of(const __versa_string& __str, size_type __pos = 0) const
  1786. _GLIBCXX_NOEXCEPT
  1787. { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
  1788. /**
  1789. * @brief Find position of a character not in C substring.
  1790. * @param __s C string containing characters to avoid.
  1791. * @param __pos Index of character to search from.
  1792. * @param __n Number of characters from s to consider.
  1793. * @return Index of first occurrence.
  1794. *
  1795. * Starting from @a __pos, searches forward for a character not
  1796. * contained in the first @a __n characters of @a __s within
  1797. * this string. If found, returns the index where it was
  1798. * found. If not found, returns npos.
  1799. */
  1800. size_type
  1801. find_first_not_of(const _CharT* __s, size_type __pos,
  1802. size_type __n) const;
  1803. /**
  1804. * @brief Find position of a character not in C string.
  1805. * @param __s C string containing characters to avoid.
  1806. * @param __pos Index of character to search from (default 0).
  1807. * @return Index of first occurrence.
  1808. *
  1809. * Starting from @a __pos, searches forward for a character not
  1810. * contained in @a __s within this string. If found, returns
  1811. * the index where it was found. If not found, returns npos.
  1812. */
  1813. size_type
  1814. find_first_not_of(const _CharT* __s, size_type __pos = 0) const
  1815. {
  1816. __glibcxx_requires_string(__s);
  1817. return this->find_first_not_of(__s, __pos, traits_type::length(__s));
  1818. }
  1819. /**
  1820. * @brief Find position of a different character.
  1821. * @param __c Character to avoid.
  1822. * @param __pos Index of character to search from (default 0).
  1823. * @return Index of first occurrence.
  1824. *
  1825. * Starting from @a __pos, searches forward for a character
  1826. * other than @a __c within this string. If found, returns the
  1827. * index where it was found. If not found, returns npos.
  1828. */
  1829. size_type
  1830. find_first_not_of(_CharT __c, size_type __pos = 0) const
  1831. _GLIBCXX_NOEXCEPT;
  1832. /**
  1833. * @brief Find last position of a character not in string.
  1834. * @param __str String containing characters to avoid.
  1835. * @param __pos Index of character to search back from (default end).
  1836. * @return Index of last occurrence.
  1837. *
  1838. * Starting from @a __pos, searches backward for a character
  1839. * not contained in @a __str within this string. If found,
  1840. * returns the index where it was found. If not found, returns
  1841. * npos.
  1842. */
  1843. size_type
  1844. find_last_not_of(const __versa_string& __str,
  1845. size_type __pos = npos) const _GLIBCXX_NOEXCEPT
  1846. { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
  1847. /**
  1848. * @brief Find last position of a character not in C substring.
  1849. * @param __s C string containing characters to avoid.
  1850. * @param __pos Index of character to search back from.
  1851. * @param __n Number of characters from s to consider.
  1852. * @return Index of last occurrence.
  1853. *
  1854. * Starting from @a __pos, searches backward for a character
  1855. * not contained in the first @a __n characters of @a __s
  1856. * within this string. If found, returns the index where it
  1857. * was found. If not found, returns npos.
  1858. */
  1859. size_type
  1860. find_last_not_of(const _CharT* __s, size_type __pos,
  1861. size_type __n) const;
  1862. /**
  1863. * @brief Find last position of a character not in C string.
  1864. * @param __s C string containing characters to avoid.
  1865. * @param __pos Index of character to search back from (default end).
  1866. * @return Index of last occurrence.
  1867. *
  1868. * Starting from @a __pos, searches backward for a character
  1869. * not contained in @a __s within this string. If found,
  1870. * returns the index where it was found. If not found, returns
  1871. * npos.
  1872. */
  1873. size_type
  1874. find_last_not_of(const _CharT* __s, size_type __pos = npos) const
  1875. {
  1876. __glibcxx_requires_string(__s);
  1877. return this->find_last_not_of(__s, __pos, traits_type::length(__s));
  1878. }
  1879. /**
  1880. * @brief Find last position of a different character.
  1881. * @param __c Character to avoid.
  1882. * @param __pos Index of character to search back from (default end).
  1883. * @return Index of last occurrence.
  1884. *
  1885. * Starting from @a __pos, searches backward for a character
  1886. * other than @a __c within this string. If found, returns the
  1887. * index where it was found. If not found, returns npos.
  1888. */
  1889. size_type
  1890. find_last_not_of(_CharT __c, size_type __pos = npos) const
  1891. _GLIBCXX_NOEXCEPT;
  1892. /**
  1893. * @brief Get a substring.
  1894. * @param __pos Index of first character (default 0).
  1895. * @param __n Number of characters in substring (default remainder).
  1896. * @return The new string.
  1897. * @throw std::out_of_range If pos > size().
  1898. *
  1899. * Construct and return a new string using the @a __n
  1900. * characters starting at @a __pos. If the string is too
  1901. * short, use the remainder of the characters. If @a __pos is
  1902. * beyond the end of the string, out_of_range is thrown.
  1903. */
  1904. __versa_string
  1905. substr(size_type __pos = 0, size_type __n = npos) const
  1906. {
  1907. return __versa_string(*this, _M_check(__pos, "__versa_string::substr"),
  1908. __n);
  1909. }
  1910. /**
  1911. * @brief Compare to a string.
  1912. * @param __str String to compare against.
  1913. * @return Integer < 0, 0, or > 0.
  1914. *
  1915. * Returns an integer < 0 if this string is ordered before @a
  1916. * __str, 0 if their values are equivalent, or > 0 if this
  1917. * string is ordered after @a __str. Determines the effective
  1918. * length rlen of the strings to compare as the smallest of
  1919. * size() and str.size(). The function then compares the two
  1920. * strings by calling traits::compare(data(), str.data(),rlen).
  1921. * If the result of the comparison is nonzero returns it,
  1922. * otherwise the shorter one is ordered first.
  1923. */
  1924. int
  1925. compare(const __versa_string& __str) const
  1926. {
  1927. if (this->_M_compare(__str))
  1928. return 0;
  1929. const size_type __size = this->size();
  1930. const size_type __osize = __str.size();
  1931. const size_type __len = std::min(__size, __osize);
  1932. int __r = traits_type::compare(this->_M_data(), __str.data(), __len);
  1933. if (!__r)
  1934. __r = this->_S_compare(__size, __osize);
  1935. return __r;
  1936. }
  1937. /**
  1938. * @brief Compare substring to a string.
  1939. * @param __pos Index of first character of substring.
  1940. * @param __n Number of characters in substring.
  1941. * @param __str String to compare against.
  1942. * @return Integer < 0, 0, or > 0.
  1943. *
  1944. * Form the substring of this string from the @a __n characters
  1945. * starting at @a __pos. Returns an integer < 0 if the
  1946. * substring is ordered before @a __str, 0 if their values are
  1947. * equivalent, or > 0 if the substring is ordered after @a
  1948. * __str. Determines the effective length rlen of the strings
  1949. * to compare as the smallest of the length of the substring
  1950. * and @a __str.size(). The function then compares the two
  1951. * strings by calling
  1952. * traits::compare(substring.data(),str.data(),rlen). If the
  1953. * result of the comparison is nonzero returns it, otherwise
  1954. * the shorter one is ordered first.
  1955. */
  1956. int
  1957. compare(size_type __pos, size_type __n,
  1958. const __versa_string& __str) const;
  1959. /**
  1960. * @brief Compare substring to a substring.
  1961. * @param __pos1 Index of first character of substring.
  1962. * @param __n1 Number of characters in substring.
  1963. * @param __str String to compare against.
  1964. * @param __pos2 Index of first character of substring of str.
  1965. * @param __n2 Number of characters in substring of str.
  1966. * @return Integer < 0, 0, or > 0.
  1967. *
  1968. * Form the substring of this string from the @a __n1
  1969. * characters starting at @a __pos1. Form the substring of @a
  1970. * __str from the @a __n2 characters starting at @a __pos2.
  1971. * Returns an integer < 0 if this substring is ordered before
  1972. * the substring of @a __str, 0 if their values are equivalent,
  1973. * or > 0 if this substring is ordered after the substring of
  1974. * @a __str. Determines the effective length rlen of the
  1975. * strings to compare as the smallest of the lengths of the
  1976. * substrings. The function then compares the two strings by
  1977. * calling
  1978. * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
  1979. * If the result of the comparison is nonzero returns it,
  1980. * otherwise the shorter one is ordered first.
  1981. */
  1982. int
  1983. compare(size_type __pos1, size_type __n1, const __versa_string& __str,
  1984. size_type __pos2, size_type __n2) const;
  1985. /**
  1986. * @brief Compare to a C string.
  1987. * @param __s C string to compare against.
  1988. * @return Integer < 0, 0, or > 0.
  1989. *
  1990. * Returns an integer < 0 if this string is ordered before @a
  1991. * __s, 0 if their values are equivalent, or > 0 if this string
  1992. * is ordered after @a __s. Determines the effective length
  1993. * rlen of the strings to compare as the smallest of size() and
  1994. * the length of a string constructed from @a __s. The
  1995. * function then compares the two strings by calling
  1996. * traits::compare(data(),s,rlen). If the result of the
  1997. * comparison is nonzero returns it, otherwise the shorter one
  1998. * is ordered first.
  1999. */
  2000. int
  2001. compare(const _CharT* __s) const;
  2002. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  2003. // 5 String::compare specification questionable
  2004. /**
  2005. * @brief Compare substring to a C string.
  2006. * @param __pos Index of first character of substring.
  2007. * @param __n1 Number of characters in substring.
  2008. * @param __s C string to compare against.
  2009. * @return Integer < 0, 0, or > 0.
  2010. *
  2011. * Form the substring of this string from the @a __n1
  2012. * characters starting at @a __pos. Returns an integer < 0 if
  2013. * the substring is ordered before @a __s, 0 if their values
  2014. * are equivalent, or > 0 if the substring is ordered after @a
  2015. * __s. Determines the effective length rlen of the strings to
  2016. * compare as the smallest of the length of the substring and
  2017. * the length of a string constructed from @a __s. The
  2018. * function then compares the two string by calling
  2019. * traits::compare(substring.data(),s,rlen). If the result of
  2020. * the comparison is nonzero returns it, otherwise the shorter
  2021. * one is ordered first.
  2022. */
  2023. int
  2024. compare(size_type __pos, size_type __n1, const _CharT* __s) const;
  2025. /**
  2026. * @brief Compare substring against a character array.
  2027. * @param __pos Index of first character of substring.
  2028. * @param __n1 Number of characters in substring.
  2029. * @param __s character array to compare against.
  2030. * @param __n2 Number of characters of s.
  2031. * @return Integer < 0, 0, or > 0.
  2032. *
  2033. * Form the substring of this string from the @a __n1
  2034. * characters starting at @a __pos. Form a string from the
  2035. * first @a __n2 characters of @a __s. Returns an integer < 0
  2036. * if this substring is ordered before the string from @a __s,
  2037. * 0 if their values are equivalent, or > 0 if this substring
  2038. * is ordered after the string from @a __s. Determines the
  2039. * effective length rlen of the strings to compare as the
  2040. * smallest of the length of the substring and @a __n2. The
  2041. * function then compares the two strings by calling
  2042. * traits::compare(substring.data(),__s,rlen). If the result of
  2043. * the comparison is nonzero returns it, otherwise the shorter
  2044. * one is ordered first.
  2045. *
  2046. * NB: __s must have at least n2 characters, <em>\\0</em> has no special
  2047. * meaning.
  2048. */
  2049. int
  2050. compare(size_type __pos, size_type __n1, const _CharT* __s,
  2051. size_type __n2) const;
  2052. };
  2053. // operator+
  2054. /**
  2055. * @brief Concatenate two strings.
  2056. * @param __lhs First string.
  2057. * @param __rhs Last string.
  2058. * @return New string with value of @a __lhs followed by @a __rhs.
  2059. */
  2060. template<typename _CharT, typename _Traits, typename _Alloc,
  2061. template <typename, typename, typename> class _Base>
  2062. __versa_string<_CharT, _Traits, _Alloc, _Base>
  2063. operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2064. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs);
  2065. /**
  2066. * @brief Concatenate C string and string.
  2067. * @param __lhs First string.
  2068. * @param __rhs Last string.
  2069. * @return New string with value of @a __lhs followed by @a __rhs.
  2070. */
  2071. template<typename _CharT, typename _Traits, typename _Alloc,
  2072. template <typename, typename, typename> class _Base>
  2073. __versa_string<_CharT, _Traits, _Alloc, _Base>
  2074. operator+(const _CharT* __lhs,
  2075. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs);
  2076. /**
  2077. * @brief Concatenate character and string.
  2078. * @param __lhs First string.
  2079. * @param __rhs Last string.
  2080. * @return New string with @a __lhs followed by @a __rhs.
  2081. */
  2082. template<typename _CharT, typename _Traits, typename _Alloc,
  2083. template <typename, typename, typename> class _Base>
  2084. __versa_string<_CharT, _Traits, _Alloc, _Base>
  2085. operator+(_CharT __lhs,
  2086. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs);
  2087. /**
  2088. * @brief Concatenate string and C string.
  2089. * @param __lhs First string.
  2090. * @param __rhs Last string.
  2091. * @return New string with @a __lhs followed by @a __rhs.
  2092. */
  2093. template<typename _CharT, typename _Traits, typename _Alloc,
  2094. template <typename, typename, typename> class _Base>
  2095. __versa_string<_CharT, _Traits, _Alloc, _Base>
  2096. operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2097. const _CharT* __rhs);
  2098. /**
  2099. * @brief Concatenate string and character.
  2100. * @param __lhs First string.
  2101. * @param __rhs Last string.
  2102. * @return New string with @a __lhs followed by @a __rhs.
  2103. */
  2104. template<typename _CharT, typename _Traits, typename _Alloc,
  2105. template <typename, typename, typename> class _Base>
  2106. __versa_string<_CharT, _Traits, _Alloc, _Base>
  2107. operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2108. _CharT __rhs);
  2109. #if __cplusplus >= 201103L
  2110. template<typename _CharT, typename _Traits, typename _Alloc,
  2111. template <typename, typename, typename> class _Base>
  2112. inline __versa_string<_CharT, _Traits, _Alloc, _Base>
  2113. operator+(__versa_string<_CharT, _Traits, _Alloc, _Base>&& __lhs,
  2114. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2115. { return std::move(__lhs.append(__rhs)); }
  2116. template<typename _CharT, typename _Traits, typename _Alloc,
  2117. template <typename, typename, typename> class _Base>
  2118. inline __versa_string<_CharT, _Traits, _Alloc, _Base>
  2119. operator+(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2120. __versa_string<_CharT, _Traits, _Alloc, _Base>&& __rhs)
  2121. { return std::move(__rhs.insert(0, __lhs)); }
  2122. template<typename _CharT, typename _Traits, typename _Alloc,
  2123. template <typename, typename, typename> class _Base>
  2124. inline __versa_string<_CharT, _Traits, _Alloc, _Base>
  2125. operator+(__versa_string<_CharT, _Traits, _Alloc, _Base>&& __lhs,
  2126. __versa_string<_CharT, _Traits, _Alloc, _Base>&& __rhs)
  2127. {
  2128. const auto __size = __lhs.size() + __rhs.size();
  2129. const bool __cond = (__size > __lhs.capacity()
  2130. && __size <= __rhs.capacity());
  2131. return __cond ? std::move(__rhs.insert(0, __lhs))
  2132. : std::move(__lhs.append(__rhs));
  2133. }
  2134. template<typename _CharT, typename _Traits, typename _Alloc,
  2135. template <typename, typename, typename> class _Base>
  2136. inline __versa_string<_CharT, _Traits, _Alloc, _Base>
  2137. operator+(const _CharT* __lhs,
  2138. __versa_string<_CharT, _Traits, _Alloc, _Base>&& __rhs)
  2139. { return std::move(__rhs.insert(0, __lhs)); }
  2140. template<typename _CharT, typename _Traits, typename _Alloc,
  2141. template <typename, typename, typename> class _Base>
  2142. inline __versa_string<_CharT, _Traits, _Alloc, _Base>
  2143. operator+(_CharT __lhs,
  2144. __versa_string<_CharT, _Traits, _Alloc, _Base>&& __rhs)
  2145. { return std::move(__rhs.insert(0, 1, __lhs)); }
  2146. template<typename _CharT, typename _Traits, typename _Alloc,
  2147. template <typename, typename, typename> class _Base>
  2148. inline __versa_string<_CharT, _Traits, _Alloc, _Base>
  2149. operator+(__versa_string<_CharT, _Traits, _Alloc, _Base>&& __lhs,
  2150. const _CharT* __rhs)
  2151. { return std::move(__lhs.append(__rhs)); }
  2152. template<typename _CharT, typename _Traits, typename _Alloc,
  2153. template <typename, typename, typename> class _Base>
  2154. inline __versa_string<_CharT, _Traits, _Alloc, _Base>
  2155. operator+(__versa_string<_CharT, _Traits, _Alloc, _Base>&& __lhs,
  2156. _CharT __rhs)
  2157. { return std::move(__lhs.append(1, __rhs)); }
  2158. #endif
  2159. // operator ==
  2160. /**
  2161. * @brief Test equivalence of two strings.
  2162. * @param __lhs First string.
  2163. * @param __rhs Second string.
  2164. * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
  2165. */
  2166. template<typename _CharT, typename _Traits, typename _Alloc,
  2167. template <typename, typename, typename> class _Base>
  2168. inline bool
  2169. operator==(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2170. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2171. { return __lhs.compare(__rhs) == 0; }
  2172. template<typename _CharT,
  2173. template <typename, typename, typename> class _Base>
  2174. inline typename __enable_if<std::__is_char<_CharT>::__value, bool>::__type
  2175. operator==(const __versa_string<_CharT, std::char_traits<_CharT>,
  2176. std::allocator<_CharT>, _Base>& __lhs,
  2177. const __versa_string<_CharT, std::char_traits<_CharT>,
  2178. std::allocator<_CharT>, _Base>& __rhs)
  2179. { return (__lhs.size() == __rhs.size()
  2180. && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
  2181. __lhs.size())); }
  2182. /**
  2183. * @brief Test equivalence of C string and string.
  2184. * @param __lhs C string.
  2185. * @param __rhs String.
  2186. * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
  2187. */
  2188. template<typename _CharT, typename _Traits, typename _Alloc,
  2189. template <typename, typename, typename> class _Base>
  2190. inline bool
  2191. operator==(const _CharT* __lhs,
  2192. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2193. { return __rhs.compare(__lhs) == 0; }
  2194. /**
  2195. * @brief Test equivalence of string and C string.
  2196. * @param __lhs String.
  2197. * @param __rhs C string.
  2198. * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
  2199. */
  2200. template<typename _CharT, typename _Traits, typename _Alloc,
  2201. template <typename, typename, typename> class _Base>
  2202. inline bool
  2203. operator==(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2204. const _CharT* __rhs)
  2205. { return __lhs.compare(__rhs) == 0; }
  2206. // operator !=
  2207. /**
  2208. * @brief Test difference of two strings.
  2209. * @param __lhs First string.
  2210. * @param __rhs Second string.
  2211. * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
  2212. */
  2213. template<typename _CharT, typename _Traits, typename _Alloc,
  2214. template <typename, typename, typename> class _Base>
  2215. inline bool
  2216. operator!=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2217. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2218. { return !(__lhs == __rhs); }
  2219. /**
  2220. * @brief Test difference of C string and string.
  2221. * @param __lhs C string.
  2222. * @param __rhs String.
  2223. * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
  2224. */
  2225. template<typename _CharT, typename _Traits, typename _Alloc,
  2226. template <typename, typename, typename> class _Base>
  2227. inline bool
  2228. operator!=(const _CharT* __lhs,
  2229. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2230. { return !(__lhs == __rhs); }
  2231. /**
  2232. * @brief Test difference of string and C string.
  2233. * @param __lhs String.
  2234. * @param __rhs C string.
  2235. * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
  2236. */
  2237. template<typename _CharT, typename _Traits, typename _Alloc,
  2238. template <typename, typename, typename> class _Base>
  2239. inline bool
  2240. operator!=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2241. const _CharT* __rhs)
  2242. { return !(__lhs == __rhs); }
  2243. // operator <
  2244. /**
  2245. * @brief Test if string precedes string.
  2246. * @param __lhs First string.
  2247. * @param __rhs Second string.
  2248. * @return True if @a __lhs precedes @a __rhs. False otherwise.
  2249. */
  2250. template<typename _CharT, typename _Traits, typename _Alloc,
  2251. template <typename, typename, typename> class _Base>
  2252. inline bool
  2253. operator<(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2254. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2255. { return __lhs.compare(__rhs) < 0; }
  2256. /**
  2257. * @brief Test if string precedes C string.
  2258. * @param __lhs String.
  2259. * @param __rhs C string.
  2260. * @return True if @a __lhs precedes @a __rhs. False otherwise.
  2261. */
  2262. template<typename _CharT, typename _Traits, typename _Alloc,
  2263. template <typename, typename, typename> class _Base>
  2264. inline bool
  2265. operator<(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2266. const _CharT* __rhs)
  2267. { return __lhs.compare(__rhs) < 0; }
  2268. /**
  2269. * @brief Test if C string precedes string.
  2270. * @param __lhs C string.
  2271. * @param __rhs String.
  2272. * @return True if @a __lhs precedes @a __rhs. False otherwise.
  2273. */
  2274. template<typename _CharT, typename _Traits, typename _Alloc,
  2275. template <typename, typename, typename> class _Base>
  2276. inline bool
  2277. operator<(const _CharT* __lhs,
  2278. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2279. { return __rhs.compare(__lhs) > 0; }
  2280. // operator >
  2281. /**
  2282. * @brief Test if string follows string.
  2283. * @param __lhs First string.
  2284. * @param __rhs Second string.
  2285. * @return True if @a __lhs follows @a __rhs. False otherwise.
  2286. */
  2287. template<typename _CharT, typename _Traits, typename _Alloc,
  2288. template <typename, typename, typename> class _Base>
  2289. inline bool
  2290. operator>(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2291. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2292. { return __lhs.compare(__rhs) > 0; }
  2293. /**
  2294. * @brief Test if string follows C string.
  2295. * @param __lhs String.
  2296. * @param __rhs C string.
  2297. * @return True if @a __lhs follows @a __rhs. False otherwise.
  2298. */
  2299. template<typename _CharT, typename _Traits, typename _Alloc,
  2300. template <typename, typename, typename> class _Base>
  2301. inline bool
  2302. operator>(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2303. const _CharT* __rhs)
  2304. { return __lhs.compare(__rhs) > 0; }
  2305. /**
  2306. * @brief Test if C string follows string.
  2307. * @param __lhs C string.
  2308. * @param __rhs String.
  2309. * @return True if @a __lhs follows @a __rhs. False otherwise.
  2310. */
  2311. template<typename _CharT, typename _Traits, typename _Alloc,
  2312. template <typename, typename, typename> class _Base>
  2313. inline bool
  2314. operator>(const _CharT* __lhs,
  2315. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2316. { return __rhs.compare(__lhs) < 0; }
  2317. // operator <=
  2318. /**
  2319. * @brief Test if string doesn't follow string.
  2320. * @param __lhs First string.
  2321. * @param __rhs Second string.
  2322. * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
  2323. */
  2324. template<typename _CharT, typename _Traits, typename _Alloc,
  2325. template <typename, typename, typename> class _Base>
  2326. inline bool
  2327. operator<=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2328. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2329. { return __lhs.compare(__rhs) <= 0; }
  2330. /**
  2331. * @brief Test if string doesn't follow C string.
  2332. * @param __lhs String.
  2333. * @param __rhs C string.
  2334. * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
  2335. */
  2336. template<typename _CharT, typename _Traits, typename _Alloc,
  2337. template <typename, typename, typename> class _Base>
  2338. inline bool
  2339. operator<=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2340. const _CharT* __rhs)
  2341. { return __lhs.compare(__rhs) <= 0; }
  2342. /**
  2343. * @brief Test if C string doesn't follow string.
  2344. * @param __lhs C string.
  2345. * @param __rhs String.
  2346. * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
  2347. */
  2348. template<typename _CharT, typename _Traits, typename _Alloc,
  2349. template <typename, typename, typename> class _Base>
  2350. inline bool
  2351. operator<=(const _CharT* __lhs,
  2352. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2353. { return __rhs.compare(__lhs) >= 0; }
  2354. // operator >=
  2355. /**
  2356. * @brief Test if string doesn't precede string.
  2357. * @param __lhs First string.
  2358. * @param __rhs Second string.
  2359. * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
  2360. */
  2361. template<typename _CharT, typename _Traits, typename _Alloc,
  2362. template <typename, typename, typename> class _Base>
  2363. inline bool
  2364. operator>=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2365. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2366. { return __lhs.compare(__rhs) >= 0; }
  2367. /**
  2368. * @brief Test if string doesn't precede C string.
  2369. * @param __lhs String.
  2370. * @param __rhs C string.
  2371. * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
  2372. */
  2373. template<typename _CharT, typename _Traits, typename _Alloc,
  2374. template <typename, typename, typename> class _Base>
  2375. inline bool
  2376. operator>=(const __versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2377. const _CharT* __rhs)
  2378. { return __lhs.compare(__rhs) >= 0; }
  2379. /**
  2380. * @brief Test if C string doesn't precede string.
  2381. * @param __lhs C string.
  2382. * @param __rhs String.
  2383. * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
  2384. */
  2385. template<typename _CharT, typename _Traits, typename _Alloc,
  2386. template <typename, typename, typename> class _Base>
  2387. inline bool
  2388. operator>=(const _CharT* __lhs,
  2389. const __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2390. { return __rhs.compare(__lhs) <= 0; }
  2391. /**
  2392. * @brief Swap contents of two strings.
  2393. * @param __lhs First string.
  2394. * @param __rhs Second string.
  2395. *
  2396. * Exchanges the contents of @a __lhs and @a __rhs in constant time.
  2397. */
  2398. template<typename _CharT, typename _Traits, typename _Alloc,
  2399. template <typename, typename, typename> class _Base>
  2400. inline void
  2401. swap(__versa_string<_CharT, _Traits, _Alloc, _Base>& __lhs,
  2402. __versa_string<_CharT, _Traits, _Alloc, _Base>& __rhs)
  2403. { __lhs.swap(__rhs); }
  2404. _GLIBCXX_END_NAMESPACE_VERSION
  2405. } // namespace
  2406. namespace std _GLIBCXX_VISIBILITY(default)
  2407. {
  2408. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  2409. /**
  2410. * @brief Read stream into a string.
  2411. * @param __is Input stream.
  2412. * @param __str Buffer to store into.
  2413. * @return Reference to the input stream.
  2414. *
  2415. * Stores characters from @a __is into @a __str until whitespace is
  2416. * found, the end of the stream is encountered, or str.max_size()
  2417. * is reached. If is.width() is non-zero, that is the limit on the
  2418. * number of characters stored into @a __str. Any previous
  2419. * contents of @a __str are erased.
  2420. */
  2421. template<typename _CharT, typename _Traits, typename _Alloc,
  2422. template <typename, typename, typename> class _Base>
  2423. basic_istream<_CharT, _Traits>&
  2424. operator>>(basic_istream<_CharT, _Traits>& __is,
  2425. __gnu_cxx::__versa_string<_CharT, _Traits,
  2426. _Alloc, _Base>& __str);
  2427. /**
  2428. * @brief Write string to a stream.
  2429. * @param __os Output stream.
  2430. * @param __str String to write out.
  2431. * @return Reference to the output stream.
  2432. *
  2433. * Output characters of @a __str into os following the same rules as for
  2434. * writing a C string.
  2435. */
  2436. template<typename _CharT, typename _Traits, typename _Alloc,
  2437. template <typename, typename, typename> class _Base>
  2438. inline basic_ostream<_CharT, _Traits>&
  2439. operator<<(basic_ostream<_CharT, _Traits>& __os,
  2440. const __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc,
  2441. _Base>& __str)
  2442. {
  2443. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  2444. // 586. string inserter not a formatted function
  2445. return __ostream_insert(__os, __str.data(), __str.size());
  2446. }
  2447. /**
  2448. * @brief Read a line from stream into a string.
  2449. * @param __is Input stream.
  2450. * @param __str Buffer to store into.
  2451. * @param __delim Character marking end of line.
  2452. * @return Reference to the input stream.
  2453. *
  2454. * Stores characters from @a __is into @a __str until @a __delim is
  2455. * found, the end of the stream is encountered, or str.max_size()
  2456. * is reached. If is.width() is non-zero, that is the limit on the
  2457. * number of characters stored into @a __str. Any previous
  2458. * contents of @a __str are erased. If @a delim was encountered,
  2459. * it is extracted but not stored into @a __str.
  2460. */
  2461. template<typename _CharT, typename _Traits, typename _Alloc,
  2462. template <typename, typename, typename> class _Base>
  2463. basic_istream<_CharT, _Traits>&
  2464. getline(basic_istream<_CharT, _Traits>& __is,
  2465. __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base>& __str,
  2466. _CharT __delim);
  2467. /**
  2468. * @brief Read a line from stream into a string.
  2469. * @param __is Input stream.
  2470. * @param __str Buffer to store into.
  2471. * @return Reference to the input stream.
  2472. *
  2473. * Stores characters from is into @a __str until &apos;\n&apos; is
  2474. * found, the end of the stream is encountered, or str.max_size()
  2475. * is reached. If is.width() is non-zero, that is the limit on the
  2476. * number of characters stored into @a __str. Any previous
  2477. * contents of @a __str are erased. If end of line was
  2478. * encountered, it is extracted but not stored into @a __str.
  2479. */
  2480. template<typename _CharT, typename _Traits, typename _Alloc,
  2481. template <typename, typename, typename> class _Base>
  2482. inline basic_istream<_CharT, _Traits>&
  2483. getline(basic_istream<_CharT, _Traits>& __is,
  2484. __gnu_cxx::__versa_string<_CharT, _Traits, _Alloc, _Base>& __str)
  2485. { return getline(__is, __str, __is.widen('\n')); }
  2486. _GLIBCXX_END_NAMESPACE_VERSION
  2487. } // namespace
  2488. #if __cplusplus >= 201103L
  2489. #include <ext/string_conversions.h>
  2490. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  2491. {
  2492. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  2493. #if _GLIBCXX_USE_C99_STDLIB
  2494. // 21.4 Numeric Conversions [string.conversions].
  2495. inline int
  2496. stoi(const __vstring& __str, std::size_t* __idx = 0, int __base = 10)
  2497. { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
  2498. __idx, __base); }
  2499. inline long
  2500. stol(const __vstring& __str, std::size_t* __idx = 0, int __base = 10)
  2501. { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
  2502. __idx, __base); }
  2503. inline unsigned long
  2504. stoul(const __vstring& __str, std::size_t* __idx = 0, int __base = 10)
  2505. { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
  2506. __idx, __base); }
  2507. inline long long
  2508. stoll(const __vstring& __str, std::size_t* __idx = 0, int __base = 10)
  2509. { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
  2510. __idx, __base); }
  2511. inline unsigned long long
  2512. stoull(const __vstring& __str, std::size_t* __idx, int __base = 10)
  2513. { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
  2514. __idx, __base); }
  2515. // NB: strtof vs strtod.
  2516. inline float
  2517. stof(const __vstring& __str, std::size_t* __idx = 0)
  2518. { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
  2519. inline double
  2520. stod(const __vstring& __str, std::size_t* __idx = 0)
  2521. { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
  2522. inline long double
  2523. stold(const __vstring& __str, std::size_t* __idx = 0)
  2524. { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
  2525. #endif // _GLIBCXX_USE_C99_STDLIB
  2526. #if _GLIBCXX_USE_C99_STDIO
  2527. // NB: (v)snprintf vs sprintf.
  2528. // DR 1261.
  2529. inline __vstring
  2530. to_string(int __val)
  2531. { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf, 4 * sizeof(int),
  2532. "%d", __val); }
  2533. inline __vstring
  2534. to_string(unsigned __val)
  2535. { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf,
  2536. 4 * sizeof(unsigned),
  2537. "%u", __val); }
  2538. inline __vstring
  2539. to_string(long __val)
  2540. { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf,
  2541. 4 * sizeof(long),
  2542. "%ld", __val); }
  2543. inline __vstring
  2544. to_string(unsigned long __val)
  2545. { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf,
  2546. 4 * sizeof(unsigned long),
  2547. "%lu", __val); }
  2548. inline __vstring
  2549. to_string(long long __val)
  2550. { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf,
  2551. 4 * sizeof(long long),
  2552. "%lld", __val); }
  2553. inline __vstring
  2554. to_string(unsigned long long __val)
  2555. { return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf,
  2556. 4 * sizeof(unsigned long long),
  2557. "%llu", __val); }
  2558. inline __vstring
  2559. to_string(float __val)
  2560. {
  2561. const int __n = __numeric_traits<float>::__max_exponent10 + 20;
  2562. return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf, __n,
  2563. "%f", __val);
  2564. }
  2565. inline __vstring
  2566. to_string(double __val)
  2567. {
  2568. const int __n = __numeric_traits<double>::__max_exponent10 + 20;
  2569. return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf, __n,
  2570. "%f", __val);
  2571. }
  2572. inline __vstring
  2573. to_string(long double __val)
  2574. {
  2575. const int __n = __numeric_traits<long double>::__max_exponent10 + 20;
  2576. return __gnu_cxx::__to_xstring<__vstring>(&std::vsnprintf, __n,
  2577. "%Lf", __val);
  2578. }
  2579. #endif // _GLIBCXX_USE_C99_STDIO
  2580. #if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
  2581. inline int
  2582. stoi(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10)
  2583. { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
  2584. __idx, __base); }
  2585. inline long
  2586. stol(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10)
  2587. { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
  2588. __idx, __base); }
  2589. inline unsigned long
  2590. stoul(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10)
  2591. { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
  2592. __idx, __base); }
  2593. inline long long
  2594. stoll(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10)
  2595. { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
  2596. __idx, __base); }
  2597. inline unsigned long long
  2598. stoull(const __wvstring& __str, std::size_t* __idx = 0, int __base = 10)
  2599. { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
  2600. __idx, __base); }
  2601. // NB: wcstof vs wcstod.
  2602. inline float
  2603. stof(const __wvstring& __str, std::size_t* __idx = 0)
  2604. { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
  2605. inline double
  2606. stod(const __wvstring& __str, std::size_t* __idx = 0)
  2607. { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
  2608. inline long double
  2609. stold(const __wvstring& __str, std::size_t* __idx = 0)
  2610. { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
  2611. #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
  2612. // DR 1261.
  2613. inline __wvstring
  2614. to_wstring(int __val)
  2615. { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
  2616. 4 * sizeof(int),
  2617. L"%d", __val); }
  2618. inline __wvstring
  2619. to_wstring(unsigned __val)
  2620. { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
  2621. 4 * sizeof(unsigned),
  2622. L"%u", __val); }
  2623. inline __wvstring
  2624. to_wstring(long __val)
  2625. { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
  2626. 4 * sizeof(long),
  2627. L"%ld", __val); }
  2628. inline __wvstring
  2629. to_wstring(unsigned long __val)
  2630. { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
  2631. 4 * sizeof(unsigned long),
  2632. L"%lu", __val); }
  2633. inline __wvstring
  2634. to_wstring(long long __val)
  2635. { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
  2636. 4 * sizeof(long long),
  2637. L"%lld", __val); }
  2638. inline __wvstring
  2639. to_wstring(unsigned long long __val)
  2640. { return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf,
  2641. 4 * sizeof(unsigned long long),
  2642. L"%llu", __val); }
  2643. inline __wvstring
  2644. to_wstring(float __val)
  2645. {
  2646. const int __n = __numeric_traits<float>::__max_exponent10 + 20;
  2647. return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf, __n,
  2648. L"%f", __val);
  2649. }
  2650. inline __wvstring
  2651. to_wstring(double __val)
  2652. {
  2653. const int __n = __numeric_traits<double>::__max_exponent10 + 20;
  2654. return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf, __n,
  2655. L"%f", __val);
  2656. }
  2657. inline __wvstring
  2658. to_wstring(long double __val)
  2659. {
  2660. const int __n = __numeric_traits<long double>::__max_exponent10 + 20;
  2661. return __gnu_cxx::__to_xstring<__wvstring>(&std::vswprintf, __n,
  2662. L"%Lf", __val);
  2663. }
  2664. #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
  2665. #endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR
  2666. _GLIBCXX_END_NAMESPACE_VERSION
  2667. } // namespace
  2668. #endif
  2669. #if __cplusplus >= 201103L
  2670. #include <bits/functional_hash.h>
  2671. namespace std _GLIBCXX_VISIBILITY(default)
  2672. {
  2673. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  2674. /// std::hash specialization for __vstring.
  2675. template<>
  2676. struct hash<__gnu_cxx::__vstring>
  2677. : public __hash_base<size_t, __gnu_cxx::__vstring>
  2678. {
  2679. size_t
  2680. operator()(const __gnu_cxx::__vstring& __s) const noexcept
  2681. { return std::_Hash_impl::hash(__s.data(), __s.length()); }
  2682. };
  2683. /// std::hash specialization for __wvstring.
  2684. template<>
  2685. struct hash<__gnu_cxx::__wvstring>
  2686. : public __hash_base<size_t, __gnu_cxx::__wvstring>
  2687. {
  2688. size_t
  2689. operator()(const __gnu_cxx::__wvstring& __s) const noexcept
  2690. { return std::_Hash_impl::hash(__s.data(),
  2691. __s.length() * sizeof(wchar_t)); }
  2692. };
  2693. /// std::hash specialization for __u16vstring.
  2694. template<>
  2695. struct hash<__gnu_cxx::__u16vstring>
  2696. : public __hash_base<size_t, __gnu_cxx::__u16vstring>
  2697. {
  2698. size_t
  2699. operator()(const __gnu_cxx::__u16vstring& __s) const noexcept
  2700. { return std::_Hash_impl::hash(__s.data(),
  2701. __s.length() * sizeof(char16_t)); }
  2702. };
  2703. /// std::hash specialization for __u32vstring.
  2704. template<>
  2705. struct hash<__gnu_cxx::__u32vstring>
  2706. : public __hash_base<size_t, __gnu_cxx::__u32vstring>
  2707. {
  2708. size_t
  2709. operator()(const __gnu_cxx::__u32vstring& __s) const noexcept
  2710. { return std::_Hash_impl::hash(__s.data(),
  2711. __s.length() * sizeof(char32_t)); }
  2712. };
  2713. _GLIBCXX_END_NAMESPACE_VERSION
  2714. } // namespace
  2715. #endif // C++11
  2716. #include <ext/vstring.tcc>
  2717. #endif /* _VSTRING_H */