vstring.h 95 KB

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