cow_string.h 129 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655
  1. // Definition of gcc4-compatible Copy-on-Write basic_string -*- C++ -*-
  2. // Copyright (C) 1997-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/cow_string.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{string}
  23. *
  24. * Defines the reference-counted COW string implentation.
  25. */
  26. #ifndef _COW_STRING_H
  27. #define _COW_STRING_H 1
  28. #if ! _GLIBCXX_USE_CXX11_ABI
  29. #ifdef __cpp_lib_is_constant_evaluated
  30. // Support P1032R1 in C++20 (but not P0980R1 for COW strings).
  31. # define __cpp_lib_constexpr_string 201811L
  32. #elif __cplusplus >= 201703L && _GLIBCXX_HAVE_IS_CONSTANT_EVALUATED
  33. // Support P0426R1 changes to char_traits in C++17.
  34. # define __cpp_lib_constexpr_string 201611L
  35. #endif
  36. namespace std _GLIBCXX_VISIBILITY(default)
  37. {
  38. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  39. /**
  40. * @class basic_string basic_string.h <string>
  41. * @brief Managing sequences of characters and character-like objects.
  42. *
  43. * @ingroup strings
  44. * @ingroup sequences
  45. *
  46. * @tparam _CharT Type of character
  47. * @tparam _Traits Traits for character type, defaults to
  48. * char_traits<_CharT>.
  49. * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
  50. *
  51. * Meets the requirements of a <a href="tables.html#65">container</a>, a
  52. * <a href="tables.html#66">reversible container</a>, and a
  53. * <a href="tables.html#67">sequence</a>. Of the
  54. * <a href="tables.html#68">optional sequence requirements</a>, only
  55. * @c push_back, @c at, and @c %array access are supported.
  56. *
  57. * @doctodo
  58. *
  59. *
  60. * Documentation? What's that?
  61. * Nathan Myers <ncm@cantrip.org>.
  62. *
  63. * A string looks like this:
  64. *
  65. * @code
  66. * [_Rep]
  67. * _M_length
  68. * [basic_string<char_type>] _M_capacity
  69. * _M_dataplus _M_refcount
  70. * _M_p ----------------> unnamed array of char_type
  71. * @endcode
  72. *
  73. * Where the _M_p points to the first character in the string, and
  74. * you cast it to a pointer-to-_Rep and subtract 1 to get a
  75. * pointer to the header.
  76. *
  77. * This approach has the enormous advantage that a string object
  78. * requires only one allocation. All the ugliness is confined
  79. * within a single %pair of inline functions, which each compile to
  80. * a single @a add instruction: _Rep::_M_data(), and
  81. * string::_M_rep(); and the allocation function which gets a
  82. * block of raw bytes and with room enough and constructs a _Rep
  83. * object at the front.
  84. *
  85. * The reason you want _M_data pointing to the character %array and
  86. * not the _Rep is so that the debugger can see the string
  87. * contents. (Probably we should add a non-inline member to get
  88. * the _Rep for the debugger to use, so users can check the actual
  89. * string length.)
  90. *
  91. * Note that the _Rep object is a POD so that you can have a
  92. * static <em>empty string</em> _Rep object already @a constructed before
  93. * static constructors have run. The reference-count encoding is
  94. * chosen so that a 0 indicates one reference, so you never try to
  95. * destroy the empty-string _Rep object.
  96. *
  97. * All but the last paragraph is considered pretty conventional
  98. * for a Copy-On-Write C++ string implementation.
  99. */
  100. // 21.3 Template class basic_string
  101. template<typename _CharT, typename _Traits, typename _Alloc>
  102. class basic_string
  103. {
  104. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  105. rebind<_CharT>::other _CharT_alloc_type;
  106. typedef __gnu_cxx::__alloc_traits<_CharT_alloc_type> _CharT_alloc_traits;
  107. // Types:
  108. public:
  109. typedef _Traits traits_type;
  110. typedef typename _Traits::char_type value_type;
  111. typedef _Alloc allocator_type;
  112. typedef typename _CharT_alloc_traits::size_type size_type;
  113. typedef typename _CharT_alloc_traits::difference_type difference_type;
  114. #if __cplusplus < 201103L
  115. typedef typename _CharT_alloc_type::reference reference;
  116. typedef typename _CharT_alloc_type::const_reference const_reference;
  117. #else
  118. typedef value_type& reference;
  119. typedef const value_type& const_reference;
  120. #endif
  121. typedef typename _CharT_alloc_traits::pointer pointer;
  122. typedef typename _CharT_alloc_traits::const_pointer const_pointer;
  123. typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
  124. typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
  125. const_iterator;
  126. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  127. typedef std::reverse_iterator<iterator> reverse_iterator;
  128. protected:
  129. // type used for positions in insert, erase etc.
  130. typedef iterator __const_iterator;
  131. private:
  132. // _Rep: string representation
  133. // Invariants:
  134. // 1. String really contains _M_length + 1 characters: due to 21.3.4
  135. // must be kept null-terminated.
  136. // 2. _M_capacity >= _M_length
  137. // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
  138. // 3. _M_refcount has three states:
  139. // -1: leaked, one reference, no ref-copies allowed, non-const.
  140. // 0: one reference, non-const.
  141. // n>0: n + 1 references, operations require a lock, const.
  142. // 4. All fields==0 is an empty string, given the extra storage
  143. // beyond-the-end for a null terminator; thus, the shared
  144. // empty string representation needs no constructor.
  145. struct _Rep_base
  146. {
  147. size_type _M_length;
  148. size_type _M_capacity;
  149. _Atomic_word _M_refcount;
  150. };
  151. struct _Rep : _Rep_base
  152. {
  153. // Types:
  154. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  155. rebind<char>::other _Raw_bytes_alloc;
  156. // (Public) Data members:
  157. // The maximum number of individual char_type elements of an
  158. // individual string is determined by _S_max_size. This is the
  159. // value that will be returned by max_size(). (Whereas npos
  160. // is the maximum number of bytes the allocator can allocate.)
  161. // If one was to divvy up the theoretical largest size string,
  162. // with a terminating character and m _CharT elements, it'd
  163. // look like this:
  164. // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
  165. // Solving for m:
  166. // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
  167. // In addition, this implementation quarters this amount.
  168. static const size_type _S_max_size;
  169. static const _CharT _S_terminal;
  170. // The following storage is init'd to 0 by the linker, resulting
  171. // (carefully) in an empty string with one reference.
  172. static size_type _S_empty_rep_storage[];
  173. static _Rep&
  174. _S_empty_rep() _GLIBCXX_NOEXCEPT
  175. {
  176. // NB: Mild hack to avoid strict-aliasing warnings. Note that
  177. // _S_empty_rep_storage is never modified and the punning should
  178. // be reasonably safe in this case.
  179. void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage);
  180. return *reinterpret_cast<_Rep*>(__p);
  181. }
  182. bool
  183. _M_is_leaked() const _GLIBCXX_NOEXCEPT
  184. {
  185. #if defined(__GTHREADS)
  186. // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
  187. // so we need to use an atomic load. However, _M_is_leaked
  188. // predicate does not change concurrently (i.e. the string is either
  189. // leaked or not), so a relaxed load is enough.
  190. return __atomic_load_n(&this->_M_refcount, __ATOMIC_RELAXED) < 0;
  191. #else
  192. return this->_M_refcount < 0;
  193. #endif
  194. }
  195. bool
  196. _M_is_shared() const _GLIBCXX_NOEXCEPT
  197. {
  198. #if defined(__GTHREADS)
  199. // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
  200. // so we need to use an atomic load. Another thread can drop last
  201. // but one reference concurrently with this check, so we need this
  202. // load to be acquire to synchronize with release fetch_and_add in
  203. // _M_dispose.
  204. if (!__gnu_cxx::__is_single_threaded())
  205. return __atomic_load_n(&this->_M_refcount, __ATOMIC_ACQUIRE) > 0;
  206. #endif
  207. return this->_M_refcount > 0;
  208. }
  209. void
  210. _M_set_leaked() _GLIBCXX_NOEXCEPT
  211. { this->_M_refcount = -1; }
  212. void
  213. _M_set_sharable() _GLIBCXX_NOEXCEPT
  214. { this->_M_refcount = 0; }
  215. void
  216. _M_set_length_and_sharable(size_type __n) _GLIBCXX_NOEXCEPT
  217. {
  218. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  219. if (__builtin_expect(this != &_S_empty_rep(), false))
  220. #endif
  221. {
  222. this->_M_set_sharable(); // One reference.
  223. this->_M_length = __n;
  224. traits_type::assign(this->_M_refdata()[__n], _S_terminal);
  225. // grrr. (per 21.3.4)
  226. // You cannot leave those LWG people alone for a second.
  227. }
  228. }
  229. _CharT*
  230. _M_refdata() throw()
  231. { return reinterpret_cast<_CharT*>(this + 1); }
  232. _CharT*
  233. _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
  234. {
  235. return (!_M_is_leaked() && __alloc1 == __alloc2)
  236. ? _M_refcopy() : _M_clone(__alloc1);
  237. }
  238. // Create & Destroy
  239. static _Rep*
  240. _S_create(size_type, size_type, const _Alloc&);
  241. void
  242. _M_dispose(const _Alloc& __a) _GLIBCXX_NOEXCEPT
  243. {
  244. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  245. if (__builtin_expect(this != &_S_empty_rep(), false))
  246. #endif
  247. {
  248. // Be race-detector-friendly. For more info see bits/c++config.
  249. _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount);
  250. // Decrement of _M_refcount is acq_rel, because:
  251. // - all but last decrements need to release to synchronize with
  252. // the last decrement that will delete the object.
  253. // - the last decrement needs to acquire to synchronize with
  254. // all the previous decrements.
  255. // - last but one decrement needs to release to synchronize with
  256. // the acquire load in _M_is_shared that will conclude that
  257. // the object is not shared anymore.
  258. if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
  259. -1) <= 0)
  260. {
  261. _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount);
  262. _M_destroy(__a);
  263. }
  264. }
  265. } // XXX MT
  266. void
  267. _M_destroy(const _Alloc&) throw();
  268. _CharT*
  269. _M_refcopy() throw()
  270. {
  271. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  272. if (__builtin_expect(this != &_S_empty_rep(), false))
  273. #endif
  274. __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
  275. return _M_refdata();
  276. } // XXX MT
  277. _CharT*
  278. _M_clone(const _Alloc&, size_type __res = 0);
  279. };
  280. // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
  281. struct _Alloc_hider : _Alloc
  282. {
  283. _Alloc_hider(_CharT* __dat, const _Alloc& __a) _GLIBCXX_NOEXCEPT
  284. : _Alloc(__a), _M_p(__dat) { }
  285. _CharT* _M_p; // The actual data.
  286. };
  287. public:
  288. // Data Members (public):
  289. // NB: This is an unsigned type, and thus represents the maximum
  290. // size that the allocator can hold.
  291. /// Value returned by various member functions when they fail.
  292. static const size_type npos = static_cast<size_type>(-1);
  293. private:
  294. // Data Members (private):
  295. mutable _Alloc_hider _M_dataplus;
  296. _CharT*
  297. _M_data() const _GLIBCXX_NOEXCEPT
  298. { return _M_dataplus._M_p; }
  299. _CharT*
  300. _M_data(_CharT* __p) _GLIBCXX_NOEXCEPT
  301. { return (_M_dataplus._M_p = __p); }
  302. _Rep*
  303. _M_rep() const _GLIBCXX_NOEXCEPT
  304. { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
  305. // For the internal use we have functions similar to `begin'/`end'
  306. // but they do not call _M_leak.
  307. iterator
  308. _M_ibegin() const _GLIBCXX_NOEXCEPT
  309. { return iterator(_M_data()); }
  310. iterator
  311. _M_iend() const _GLIBCXX_NOEXCEPT
  312. { return iterator(_M_data() + this->size()); }
  313. void
  314. _M_leak() // for use in begin() & non-const op[]
  315. {
  316. if (!_M_rep()->_M_is_leaked())
  317. _M_leak_hard();
  318. }
  319. size_type
  320. _M_check(size_type __pos, const char* __s) const
  321. {
  322. if (__pos > this->size())
  323. __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
  324. "this->size() (which is %zu)"),
  325. __s, __pos, this->size());
  326. return __pos;
  327. }
  328. void
  329. _M_check_length(size_type __n1, size_type __n2, const char* __s) const
  330. {
  331. if (this->max_size() - (this->size() - __n1) < __n2)
  332. __throw_length_error(__N(__s));
  333. }
  334. // NB: _M_limit doesn't check for a bad __pos value.
  335. size_type
  336. _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
  337. {
  338. const bool __testoff = __off < this->size() - __pos;
  339. return __testoff ? __off : this->size() - __pos;
  340. }
  341. // True if _Rep and source do not overlap.
  342. bool
  343. _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
  344. {
  345. return (less<const _CharT*>()(__s, _M_data())
  346. || less<const _CharT*>()(_M_data() + this->size(), __s));
  347. }
  348. // When __n = 1 way faster than the general multichar
  349. // traits_type::copy/move/assign.
  350. static void
  351. _M_copy(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
  352. {
  353. if (__n == 1)
  354. traits_type::assign(*__d, *__s);
  355. else
  356. traits_type::copy(__d, __s, __n);
  357. }
  358. static void
  359. _M_move(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
  360. {
  361. if (__n == 1)
  362. traits_type::assign(*__d, *__s);
  363. else
  364. traits_type::move(__d, __s, __n);
  365. }
  366. static void
  367. _M_assign(_CharT* __d, size_type __n, _CharT __c) _GLIBCXX_NOEXCEPT
  368. {
  369. if (__n == 1)
  370. traits_type::assign(*__d, __c);
  371. else
  372. traits_type::assign(__d, __n, __c);
  373. }
  374. // _S_copy_chars is a separate template to permit specialization
  375. // to optimize for the common case of pointers as iterators.
  376. template<class _Iterator>
  377. static void
  378. _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
  379. {
  380. for (; __k1 != __k2; ++__k1, (void)++__p)
  381. traits_type::assign(*__p, *__k1); // These types are off.
  382. }
  383. static void
  384. _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
  385. { _S_copy_chars(__p, __k1.base(), __k2.base()); }
  386. static void
  387. _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
  388. _GLIBCXX_NOEXCEPT
  389. { _S_copy_chars(__p, __k1.base(), __k2.base()); }
  390. static void
  391. _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
  392. { _M_copy(__p, __k1, __k2 - __k1); }
  393. static void
  394. _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
  395. _GLIBCXX_NOEXCEPT
  396. { _M_copy(__p, __k1, __k2 - __k1); }
  397. static int
  398. _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
  399. {
  400. const difference_type __d = difference_type(__n1 - __n2);
  401. if (__d > __gnu_cxx::__numeric_traits<int>::__max)
  402. return __gnu_cxx::__numeric_traits<int>::__max;
  403. else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
  404. return __gnu_cxx::__numeric_traits<int>::__min;
  405. else
  406. return int(__d);
  407. }
  408. void
  409. _M_mutate(size_type __pos, size_type __len1, size_type __len2);
  410. void
  411. _M_leak_hard();
  412. static _Rep&
  413. _S_empty_rep() _GLIBCXX_NOEXCEPT
  414. { return _Rep::_S_empty_rep(); }
  415. #if __cplusplus >= 201703L
  416. // A helper type for avoiding boiler-plate.
  417. typedef basic_string_view<_CharT, _Traits> __sv_type;
  418. template<typename _Tp, typename _Res>
  419. using _If_sv = enable_if_t<
  420. __and_<is_convertible<const _Tp&, __sv_type>,
  421. __not_<is_convertible<const _Tp*, const basic_string*>>,
  422. __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
  423. _Res>;
  424. // Allows an implicit conversion to __sv_type.
  425. static __sv_type
  426. _S_to_string_view(__sv_type __svt) noexcept
  427. { return __svt; }
  428. // Wraps a string_view by explicit conversion and thus
  429. // allows to add an internal constructor that does not
  430. // participate in overload resolution when a string_view
  431. // is provided.
  432. struct __sv_wrapper
  433. {
  434. explicit __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
  435. __sv_type _M_sv;
  436. };
  437. /**
  438. * @brief Only internally used: Construct string from a string view
  439. * wrapper.
  440. * @param __svw string view wrapper.
  441. * @param __a Allocator to use.
  442. */
  443. explicit
  444. basic_string(__sv_wrapper __svw, const _Alloc& __a)
  445. : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
  446. #endif
  447. public:
  448. // Construct/copy/destroy:
  449. // NB: We overload ctors in some cases instead of using default
  450. // arguments, per 17.4.4.4 para. 2 item 2.
  451. /**
  452. * @brief Default constructor creates an empty string.
  453. */
  454. basic_string()
  455. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  456. _GLIBCXX_NOEXCEPT
  457. : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc())
  458. #else
  459. : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc())
  460. #endif
  461. { }
  462. /**
  463. * @brief Construct an empty string using allocator @a a.
  464. */
  465. explicit
  466. basic_string(const _Alloc& __a)
  467. : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a)
  468. { }
  469. // NB: per LWG issue 42, semantics different from IS:
  470. /**
  471. * @brief Construct string with copy of value of @a str.
  472. * @param __str Source string.
  473. */
  474. basic_string(const basic_string& __str)
  475. : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(__str.get_allocator()),
  476. __str.get_allocator()),
  477. __str.get_allocator())
  478. { }
  479. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  480. // 2583. no way to supply an allocator for basic_string(str, pos)
  481. /**
  482. * @brief Construct string as copy of a substring.
  483. * @param __str Source string.
  484. * @param __pos Index of first character to copy from.
  485. * @param __a Allocator to use.
  486. */
  487. basic_string(const basic_string& __str, size_type __pos,
  488. const _Alloc& __a = _Alloc());
  489. /**
  490. * @brief Construct string as copy of a substring.
  491. * @param __str Source string.
  492. * @param __pos Index of first character to copy from.
  493. * @param __n Number of characters to copy.
  494. */
  495. basic_string(const basic_string& __str, size_type __pos,
  496. size_type __n);
  497. /**
  498. * @brief Construct string as copy of a substring.
  499. * @param __str Source string.
  500. * @param __pos Index of first character to copy from.
  501. * @param __n Number of characters to copy.
  502. * @param __a Allocator to use.
  503. */
  504. basic_string(const basic_string& __str, size_type __pos,
  505. size_type __n, const _Alloc& __a);
  506. /**
  507. * @brief Construct string initialized by a character %array.
  508. * @param __s Source character %array.
  509. * @param __n Number of characters to copy.
  510. * @param __a Allocator to use (default is default allocator).
  511. *
  512. * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
  513. * has no special meaning.
  514. */
  515. basic_string(const _CharT* __s, size_type __n,
  516. const _Alloc& __a = _Alloc())
  517. : _M_dataplus(_S_construct(__s, __s + __n, __a), __a)
  518. { }
  519. /**
  520. * @brief Construct string as copy of a C string.
  521. * @param __s Source C string.
  522. * @param __a Allocator to use (default is default allocator).
  523. */
  524. #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
  525. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  526. // 3076. basic_string CTAD ambiguity
  527. template<typename = _RequireAllocator<_Alloc>>
  528. #endif
  529. basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
  530. : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) :
  531. __s + npos, __a), __a)
  532. { }
  533. /**
  534. * @brief Construct string as multiple characters.
  535. * @param __n Number of characters.
  536. * @param __c Character to use.
  537. * @param __a Allocator to use (default is default allocator).
  538. */
  539. basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
  540. : _M_dataplus(_S_construct(__n, __c, __a), __a)
  541. { }
  542. #if __cplusplus >= 201103L
  543. /**
  544. * @brief Move construct string.
  545. * @param __str Source string.
  546. *
  547. * The newly-created string contains the exact contents of @a __str.
  548. * @a __str is a valid, but unspecified string.
  549. */
  550. basic_string(basic_string&& __str) noexcept
  551. : _M_dataplus(std::move(__str._M_dataplus))
  552. {
  553. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  554. // Make __str use the shared empty string rep.
  555. __str._M_data(_S_empty_rep()._M_refdata());
  556. #else
  557. // Rather than allocate an empty string for the rvalue string,
  558. // just share ownership with it by incrementing the reference count.
  559. // If the rvalue string was the unique owner then there are exactly
  560. // two owners now.
  561. if (_M_rep()->_M_is_shared())
  562. __gnu_cxx::__atomic_add_dispatch(&_M_rep()->_M_refcount, 1);
  563. else
  564. _M_rep()->_M_refcount = 1;
  565. #endif
  566. }
  567. /**
  568. * @brief Construct string from an initializer %list.
  569. * @param __l std::initializer_list of characters.
  570. * @param __a Allocator to use (default is default allocator).
  571. */
  572. basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
  573. : _M_dataplus(_S_construct(__l.begin(), __l.end(), __a), __a)
  574. { }
  575. basic_string(const basic_string& __str, const _Alloc& __a)
  576. : _M_dataplus(__str._M_rep()->_M_grab(__a, __str.get_allocator()), __a)
  577. { }
  578. basic_string(basic_string&& __str, const _Alloc& __a)
  579. : _M_dataplus(__str._M_data(), __a)
  580. {
  581. if (__a == __str.get_allocator())
  582. {
  583. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  584. __str._M_data(_S_empty_rep()._M_refdata());
  585. #else
  586. __str._M_data(_S_construct(size_type(), _CharT(), __a));
  587. #endif
  588. }
  589. else
  590. _M_dataplus._M_p = _S_construct(__str.begin(), __str.end(), __a);
  591. }
  592. #endif // C++11
  593. #if __cplusplus >= 202100L
  594. basic_string(nullptr_t) = delete;
  595. basic_string& operator=(nullptr_t) = delete;
  596. #endif // C++23
  597. /**
  598. * @brief Construct string as copy of a range.
  599. * @param __beg Start of range.
  600. * @param __end End of range.
  601. * @param __a Allocator to use (default is default allocator).
  602. */
  603. template<class _InputIterator>
  604. basic_string(_InputIterator __beg, _InputIterator __end,
  605. const _Alloc& __a = _Alloc())
  606. : _M_dataplus(_S_construct(__beg, __end, __a), __a)
  607. { }
  608. #if __cplusplus >= 201703L
  609. /**
  610. * @brief Construct string from a substring of a string_view.
  611. * @param __t Source object convertible to string view.
  612. * @param __pos The index of the first character to copy from __t.
  613. * @param __n The number of characters to copy from __t.
  614. * @param __a Allocator to use.
  615. */
  616. template<typename _Tp,
  617. typename = enable_if_t<is_convertible_v<const _Tp&, __sv_type>>>
  618. basic_string(const _Tp& __t, size_type __pos, size_type __n,
  619. const _Alloc& __a = _Alloc())
  620. : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
  621. /**
  622. * @brief Construct string from a string_view.
  623. * @param __t Source object convertible to string view.
  624. * @param __a Allocator to use (default is default allocator).
  625. */
  626. template<typename _Tp, typename = _If_sv<_Tp, void>>
  627. explicit
  628. basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
  629. : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
  630. #endif // C++17
  631. /**
  632. * @brief Destroy the string instance.
  633. */
  634. ~basic_string() _GLIBCXX_NOEXCEPT
  635. { _M_rep()->_M_dispose(this->get_allocator()); }
  636. /**
  637. * @brief Assign the value of @a str to this string.
  638. * @param __str Source string.
  639. */
  640. basic_string&
  641. operator=(const basic_string& __str)
  642. { return this->assign(__str); }
  643. /**
  644. * @brief Copy contents of @a s into this string.
  645. * @param __s Source null-terminated string.
  646. */
  647. basic_string&
  648. operator=(const _CharT* __s)
  649. { return this->assign(__s); }
  650. /**
  651. * @brief Set value to string of length 1.
  652. * @param __c Source character.
  653. *
  654. * Assigning to a character makes this string length 1 and
  655. * (*this)[0] == @a c.
  656. */
  657. basic_string&
  658. operator=(_CharT __c)
  659. {
  660. this->assign(1, __c);
  661. return *this;
  662. }
  663. #if __cplusplus >= 201103L
  664. /**
  665. * @brief Move assign the value of @a str to this string.
  666. * @param __str Source string.
  667. *
  668. * The contents of @a str are moved into this string (without copying).
  669. * @a str is a valid, but unspecified string.
  670. */
  671. basic_string&
  672. operator=(basic_string&& __str)
  673. _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value)
  674. {
  675. // NB: DR 1204.
  676. this->swap(__str);
  677. return *this;
  678. }
  679. /**
  680. * @brief Set value to string constructed from initializer %list.
  681. * @param __l std::initializer_list.
  682. */
  683. basic_string&
  684. operator=(initializer_list<_CharT> __l)
  685. {
  686. this->assign(__l.begin(), __l.size());
  687. return *this;
  688. }
  689. #endif // C++11
  690. #if __cplusplus >= 201703L
  691. /**
  692. * @brief Set value to string constructed from a string_view.
  693. * @param __svt An object convertible to string_view.
  694. */
  695. template<typename _Tp>
  696. _If_sv<_Tp, basic_string&>
  697. operator=(const _Tp& __svt)
  698. { return this->assign(__svt); }
  699. /**
  700. * @brief Convert to a string_view.
  701. * @return A string_view.
  702. */
  703. operator __sv_type() const noexcept
  704. { return __sv_type(data(), size()); }
  705. #endif // C++17
  706. // Iterators:
  707. /**
  708. * Returns a read/write iterator that points to the first character in
  709. * the %string. Unshares the string.
  710. */
  711. iterator
  712. begin() // FIXME C++11: should be noexcept.
  713. {
  714. _M_leak();
  715. return iterator(_M_data());
  716. }
  717. /**
  718. * Returns a read-only (constant) iterator that points to the first
  719. * character in the %string.
  720. */
  721. const_iterator
  722. begin() const _GLIBCXX_NOEXCEPT
  723. { return const_iterator(_M_data()); }
  724. /**
  725. * Returns a read/write iterator that points one past the last
  726. * character in the %string. Unshares the string.
  727. */
  728. iterator
  729. end() // FIXME C++11: should be noexcept.
  730. {
  731. _M_leak();
  732. return iterator(_M_data() + this->size());
  733. }
  734. /**
  735. * Returns a read-only (constant) iterator that points one past the
  736. * last character in the %string.
  737. */
  738. const_iterator
  739. end() const _GLIBCXX_NOEXCEPT
  740. { return const_iterator(_M_data() + this->size()); }
  741. /**
  742. * Returns a read/write reverse iterator that points to the last
  743. * character in the %string. Iteration is done in reverse element
  744. * order. Unshares the string.
  745. */
  746. reverse_iterator
  747. rbegin() // FIXME C++11: should be noexcept.
  748. { return reverse_iterator(this->end()); }
  749. /**
  750. * Returns a read-only (constant) reverse iterator that points
  751. * to the last character in the %string. Iteration is done in
  752. * reverse element order.
  753. */
  754. const_reverse_iterator
  755. rbegin() const _GLIBCXX_NOEXCEPT
  756. { return const_reverse_iterator(this->end()); }
  757. /**
  758. * Returns a read/write reverse iterator that points to one before the
  759. * first character in the %string. Iteration is done in reverse
  760. * element order. Unshares the string.
  761. */
  762. reverse_iterator
  763. rend() // FIXME C++11: should be noexcept.
  764. { return reverse_iterator(this->begin()); }
  765. /**
  766. * Returns a read-only (constant) reverse iterator that points
  767. * to one before the first character in the %string. Iteration
  768. * is done in reverse element order.
  769. */
  770. const_reverse_iterator
  771. rend() const _GLIBCXX_NOEXCEPT
  772. { return const_reverse_iterator(this->begin()); }
  773. #if __cplusplus >= 201103L
  774. /**
  775. * Returns a read-only (constant) iterator that points to the first
  776. * character in the %string.
  777. */
  778. const_iterator
  779. cbegin() const noexcept
  780. { return const_iterator(this->_M_data()); }
  781. /**
  782. * Returns a read-only (constant) iterator that points one past the
  783. * last character in the %string.
  784. */
  785. const_iterator
  786. cend() const noexcept
  787. { return const_iterator(this->_M_data() + this->size()); }
  788. /**
  789. * Returns a read-only (constant) reverse iterator that points
  790. * to the last character in the %string. Iteration is done in
  791. * reverse element order.
  792. */
  793. const_reverse_iterator
  794. crbegin() const noexcept
  795. { return const_reverse_iterator(this->end()); }
  796. /**
  797. * Returns a read-only (constant) reverse iterator that points
  798. * to one before the first character in the %string. Iteration
  799. * is done in reverse element order.
  800. */
  801. const_reverse_iterator
  802. crend() const noexcept
  803. { return const_reverse_iterator(this->begin()); }
  804. #endif
  805. public:
  806. // Capacity:
  807. /// Returns the number of characters in the string, not including any
  808. /// null-termination.
  809. size_type
  810. size() const _GLIBCXX_NOEXCEPT
  811. { return _M_rep()->_M_length; }
  812. /// Returns the number of characters in the string, not including any
  813. /// null-termination.
  814. size_type
  815. length() const _GLIBCXX_NOEXCEPT
  816. { return _M_rep()->_M_length; }
  817. /// Returns the size() of the largest possible %string.
  818. size_type
  819. max_size() const _GLIBCXX_NOEXCEPT
  820. { return _Rep::_S_max_size; }
  821. /**
  822. * @brief Resizes the %string to the specified number of characters.
  823. * @param __n Number of characters the %string should contain.
  824. * @param __c Character to fill any new elements.
  825. *
  826. * This function will %resize the %string to the specified
  827. * number of characters. If the number is smaller than the
  828. * %string's current size the %string is truncated, otherwise
  829. * the %string is extended and new elements are %set to @a __c.
  830. */
  831. void
  832. resize(size_type __n, _CharT __c);
  833. /**
  834. * @brief Resizes the %string to the specified number of characters.
  835. * @param __n Number of characters the %string should contain.
  836. *
  837. * This function will resize the %string to the specified length. If
  838. * the new size is smaller than the %string's current size the %string
  839. * is truncated, otherwise the %string is extended and new characters
  840. * are default-constructed. For basic types such as char, this means
  841. * setting them to 0.
  842. */
  843. void
  844. resize(size_type __n)
  845. { this->resize(__n, _CharT()); }
  846. #if __cplusplus >= 201103L
  847. #pragma GCC diagnostic push
  848. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  849. /// A non-binding request to reduce capacity() to size().
  850. void
  851. shrink_to_fit() noexcept
  852. { reserve(); }
  853. #pragma GCC diagnostic pop
  854. #endif
  855. /**
  856. * Returns the total number of characters that the %string can hold
  857. * before needing to allocate more memory.
  858. */
  859. size_type
  860. capacity() const _GLIBCXX_NOEXCEPT
  861. { return _M_rep()->_M_capacity; }
  862. /**
  863. * @brief Attempt to preallocate enough memory for specified number of
  864. * characters.
  865. * @param __res_arg Number of characters required.
  866. * @throw std::length_error If @a __res_arg exceeds @c max_size().
  867. *
  868. * This function attempts to reserve enough memory for the
  869. * %string to hold the specified number of characters. If the
  870. * number requested is more than max_size(), length_error is
  871. * thrown.
  872. *
  873. * The advantage of this function is that if optimal code is a
  874. * necessity and the user can determine the string length that will be
  875. * required, the user can reserve the memory in %advance, and thus
  876. * prevent a possible reallocation of memory and copying of %string
  877. * data.
  878. */
  879. void
  880. reserve(size_type __res_arg);
  881. /// Equivalent to shrink_to_fit().
  882. #if __cplusplus > 201703L
  883. [[deprecated("use shrink_to_fit() instead")]]
  884. #endif
  885. void
  886. reserve();
  887. /**
  888. * Erases the string, making it empty.
  889. */
  890. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  891. void
  892. clear() _GLIBCXX_NOEXCEPT
  893. {
  894. if (_M_rep()->_M_is_shared())
  895. {
  896. _M_rep()->_M_dispose(this->get_allocator());
  897. _M_data(_S_empty_rep()._M_refdata());
  898. }
  899. else
  900. _M_rep()->_M_set_length_and_sharable(0);
  901. }
  902. #else
  903. // PR 56166: this should not throw.
  904. void
  905. clear()
  906. { _M_mutate(0, this->size(), 0); }
  907. #endif
  908. /**
  909. * Returns true if the %string is empty. Equivalent to
  910. * <code>*this == ""</code>.
  911. */
  912. _GLIBCXX_NODISCARD bool
  913. empty() const _GLIBCXX_NOEXCEPT
  914. { return this->size() == 0; }
  915. // Element access:
  916. /**
  917. * @brief Subscript access to the data contained in the %string.
  918. * @param __pos The index of the character to access.
  919. * @return Read-only (constant) reference to the character.
  920. *
  921. * This operator allows for easy, array-style, data access.
  922. * Note that data access with this operator is unchecked and
  923. * out_of_range lookups are not defined. (For checked lookups
  924. * see at().)
  925. */
  926. const_reference
  927. operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
  928. {
  929. __glibcxx_assert(__pos <= size());
  930. return _M_data()[__pos];
  931. }
  932. /**
  933. * @brief Subscript access to the data contained in the %string.
  934. * @param __pos The index of the character to access.
  935. * @return Read/write reference to the character.
  936. *
  937. * This operator allows for easy, array-style, data access.
  938. * Note that data access with this operator is unchecked and
  939. * out_of_range lookups are not defined. (For checked lookups
  940. * see at().) Unshares the string.
  941. */
  942. reference
  943. operator[](size_type __pos)
  944. {
  945. // Allow pos == size() both in C++98 mode, as v3 extension,
  946. // and in C++11 mode.
  947. __glibcxx_assert(__pos <= size());
  948. // In pedantic mode be strict in C++98 mode.
  949. _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
  950. _M_leak();
  951. return _M_data()[__pos];
  952. }
  953. /**
  954. * @brief Provides access to the data contained in the %string.
  955. * @param __n The index of the character to access.
  956. * @return Read-only (const) reference to the character.
  957. * @throw std::out_of_range If @a n is an invalid index.
  958. *
  959. * This function provides for safer data access. The parameter is
  960. * first checked that it is in the range of the string. The function
  961. * throws out_of_range if the check fails.
  962. */
  963. const_reference
  964. at(size_type __n) const
  965. {
  966. if (__n >= this->size())
  967. __throw_out_of_range_fmt(__N("basic_string::at: __n "
  968. "(which is %zu) >= this->size() "
  969. "(which is %zu)"),
  970. __n, this->size());
  971. return _M_data()[__n];
  972. }
  973. /**
  974. * @brief Provides access to the data contained in the %string.
  975. * @param __n The index of the character to access.
  976. * @return Read/write reference to the character.
  977. * @throw std::out_of_range If @a n is an invalid index.
  978. *
  979. * This function provides for safer data access. The parameter is
  980. * first checked that it is in the range of the string. The function
  981. * throws out_of_range if the check fails. Success results in
  982. * unsharing the string.
  983. */
  984. reference
  985. at(size_type __n)
  986. {
  987. if (__n >= size())
  988. __throw_out_of_range_fmt(__N("basic_string::at: __n "
  989. "(which is %zu) >= this->size() "
  990. "(which is %zu)"),
  991. __n, this->size());
  992. _M_leak();
  993. return _M_data()[__n];
  994. }
  995. #if __cplusplus >= 201103L
  996. /**
  997. * Returns a read/write reference to the data at the first
  998. * element of the %string.
  999. */
  1000. reference
  1001. front()
  1002. {
  1003. __glibcxx_assert(!empty());
  1004. return operator[](0);
  1005. }
  1006. /**
  1007. * Returns a read-only (constant) reference to the data at the first
  1008. * element of the %string.
  1009. */
  1010. const_reference
  1011. front() const noexcept
  1012. {
  1013. __glibcxx_assert(!empty());
  1014. return operator[](0);
  1015. }
  1016. /**
  1017. * Returns a read/write reference to the data at the last
  1018. * element of the %string.
  1019. */
  1020. reference
  1021. back()
  1022. {
  1023. __glibcxx_assert(!empty());
  1024. return operator[](this->size() - 1);
  1025. }
  1026. /**
  1027. * Returns a read-only (constant) reference to the data at the
  1028. * last element of the %string.
  1029. */
  1030. const_reference
  1031. back() const noexcept
  1032. {
  1033. __glibcxx_assert(!empty());
  1034. return operator[](this->size() - 1);
  1035. }
  1036. #endif
  1037. // Modifiers:
  1038. /**
  1039. * @brief Append a string to this string.
  1040. * @param __str The string to append.
  1041. * @return Reference to this string.
  1042. */
  1043. basic_string&
  1044. operator+=(const basic_string& __str)
  1045. { return this->append(__str); }
  1046. /**
  1047. * @brief Append a C string.
  1048. * @param __s The C string to append.
  1049. * @return Reference to this string.
  1050. */
  1051. basic_string&
  1052. operator+=(const _CharT* __s)
  1053. { return this->append(__s); }
  1054. /**
  1055. * @brief Append a character.
  1056. * @param __c The character to append.
  1057. * @return Reference to this string.
  1058. */
  1059. basic_string&
  1060. operator+=(_CharT __c)
  1061. {
  1062. this->push_back(__c);
  1063. return *this;
  1064. }
  1065. #if __cplusplus >= 201103L
  1066. /**
  1067. * @brief Append an initializer_list of characters.
  1068. * @param __l The initializer_list of characters to be appended.
  1069. * @return Reference to this string.
  1070. */
  1071. basic_string&
  1072. operator+=(initializer_list<_CharT> __l)
  1073. { return this->append(__l.begin(), __l.size()); }
  1074. #endif // C++11
  1075. #if __cplusplus >= 201703L
  1076. /**
  1077. * @brief Append a string_view.
  1078. * @param __svt The object convertible to string_view to be appended.
  1079. * @return Reference to this string.
  1080. */
  1081. template<typename _Tp>
  1082. _If_sv<_Tp, basic_string&>
  1083. operator+=(const _Tp& __svt)
  1084. { return this->append(__svt); }
  1085. #endif // C++17
  1086. /**
  1087. * @brief Append a string to this string.
  1088. * @param __str The string to append.
  1089. * @return Reference to this string.
  1090. */
  1091. basic_string&
  1092. append(const basic_string& __str);
  1093. /**
  1094. * @brief Append a substring.
  1095. * @param __str The string to append.
  1096. * @param __pos Index of the first character of str to append.
  1097. * @param __n The number of characters to append.
  1098. * @return Reference to this string.
  1099. * @throw std::out_of_range if @a __pos is not a valid index.
  1100. *
  1101. * This function appends @a __n characters from @a __str
  1102. * starting at @a __pos to this string. If @a __n is is larger
  1103. * than the number of available characters in @a __str, the
  1104. * remainder of @a __str is appended.
  1105. */
  1106. basic_string&
  1107. append(const basic_string& __str, size_type __pos, size_type __n = npos);
  1108. /**
  1109. * @brief Append a C substring.
  1110. * @param __s The C string to append.
  1111. * @param __n The number of characters to append.
  1112. * @return Reference to this string.
  1113. */
  1114. basic_string&
  1115. append(const _CharT* __s, size_type __n);
  1116. /**
  1117. * @brief Append a C string.
  1118. * @param __s The C string to append.
  1119. * @return Reference to this string.
  1120. */
  1121. basic_string&
  1122. append(const _CharT* __s)
  1123. {
  1124. __glibcxx_requires_string(__s);
  1125. return this->append(__s, traits_type::length(__s));
  1126. }
  1127. /**
  1128. * @brief Append multiple characters.
  1129. * @param __n The number of characters to append.
  1130. * @param __c The character to use.
  1131. * @return Reference to this string.
  1132. *
  1133. * Appends __n copies of __c to this string.
  1134. */
  1135. basic_string&
  1136. append(size_type __n, _CharT __c);
  1137. #if __cplusplus >= 201103L
  1138. /**
  1139. * @brief Append an initializer_list of characters.
  1140. * @param __l The initializer_list of characters to append.
  1141. * @return Reference to this string.
  1142. */
  1143. basic_string&
  1144. append(initializer_list<_CharT> __l)
  1145. { return this->append(__l.begin(), __l.size()); }
  1146. #endif // C++11
  1147. /**
  1148. * @brief Append a range of characters.
  1149. * @param __first Iterator referencing the first character to append.
  1150. * @param __last Iterator marking the end of the range.
  1151. * @return Reference to this string.
  1152. *
  1153. * Appends characters in the range [__first,__last) to this string.
  1154. */
  1155. template<class _InputIterator>
  1156. basic_string&
  1157. append(_InputIterator __first, _InputIterator __last)
  1158. { return this->replace(_M_iend(), _M_iend(), __first, __last); }
  1159. #if __cplusplus >= 201703L
  1160. /**
  1161. * @brief Append a string_view.
  1162. * @param __svt The object convertible to string_view to be appended.
  1163. * @return Reference to this string.
  1164. */
  1165. template<typename _Tp>
  1166. _If_sv<_Tp, basic_string&>
  1167. append(const _Tp& __svt)
  1168. {
  1169. __sv_type __sv = __svt;
  1170. return this->append(__sv.data(), __sv.size());
  1171. }
  1172. /**
  1173. * @brief Append a range of characters from a string_view.
  1174. * @param __svt The object convertible to string_view to be appended
  1175. * from.
  1176. * @param __pos The position in the string_view to append from.
  1177. * @param __n The number of characters to append from the string_view.
  1178. * @return Reference to this string.
  1179. */
  1180. template<typename _Tp>
  1181. _If_sv<_Tp, basic_string&>
  1182. append(const _Tp& __svt, size_type __pos, size_type __n = npos)
  1183. {
  1184. __sv_type __sv = __svt;
  1185. return append(__sv.data()
  1186. + std::__sv_check(__sv.size(), __pos, "basic_string::append"),
  1187. std::__sv_limit(__sv.size(), __pos, __n));
  1188. }
  1189. #endif // C++17
  1190. /**
  1191. * @brief Append a single character.
  1192. * @param __c Character to append.
  1193. */
  1194. void
  1195. push_back(_CharT __c)
  1196. {
  1197. const size_type __len = 1 + this->size();
  1198. if (__len > this->capacity() || _M_rep()->_M_is_shared())
  1199. this->reserve(__len);
  1200. traits_type::assign(_M_data()[this->size()], __c);
  1201. _M_rep()->_M_set_length_and_sharable(__len);
  1202. }
  1203. /**
  1204. * @brief Set value to contents of another string.
  1205. * @param __str Source string to use.
  1206. * @return Reference to this string.
  1207. */
  1208. basic_string&
  1209. assign(const basic_string& __str);
  1210. #if __cplusplus >= 201103L
  1211. /**
  1212. * @brief Set value to contents of another string.
  1213. * @param __str Source string to use.
  1214. * @return Reference to this string.
  1215. *
  1216. * This function sets this string to the exact contents of @a __str.
  1217. * @a __str is a valid, but unspecified string.
  1218. */
  1219. basic_string&
  1220. assign(basic_string&& __str)
  1221. noexcept(allocator_traits<_Alloc>::is_always_equal::value)
  1222. {
  1223. this->swap(__str);
  1224. return *this;
  1225. }
  1226. #endif // C++11
  1227. /**
  1228. * @brief Set value to a substring of a string.
  1229. * @param __str The string to use.
  1230. * @param __pos Index of the first character of str.
  1231. * @param __n Number of characters to use.
  1232. * @return Reference to this string.
  1233. * @throw std::out_of_range if @a pos is not a valid index.
  1234. *
  1235. * This function sets this string to the substring of @a __str
  1236. * consisting of @a __n characters at @a __pos. If @a __n is
  1237. * is larger than the number of available characters in @a
  1238. * __str, the remainder of @a __str is used.
  1239. */
  1240. basic_string&
  1241. assign(const basic_string& __str, size_type __pos, size_type __n = npos)
  1242. { return this->assign(__str._M_data()
  1243. + __str._M_check(__pos, "basic_string::assign"),
  1244. __str._M_limit(__pos, __n)); }
  1245. /**
  1246. * @brief Set value to a C substring.
  1247. * @param __s The C string to use.
  1248. * @param __n Number of characters to use.
  1249. * @return Reference to this string.
  1250. *
  1251. * This function sets the value of this string to the first @a __n
  1252. * characters of @a __s. If @a __n is is larger than the number of
  1253. * available characters in @a __s, the remainder of @a __s is used.
  1254. */
  1255. basic_string&
  1256. assign(const _CharT* __s, size_type __n);
  1257. /**
  1258. * @brief Set value to contents of a C string.
  1259. * @param __s The C string to use.
  1260. * @return Reference to this string.
  1261. *
  1262. * This function sets the value of this string to the value of @a __s.
  1263. * The data is copied, so there is no dependence on @a __s once the
  1264. * function returns.
  1265. */
  1266. basic_string&
  1267. assign(const _CharT* __s)
  1268. {
  1269. __glibcxx_requires_string(__s);
  1270. return this->assign(__s, traits_type::length(__s));
  1271. }
  1272. /**
  1273. * @brief Set value to multiple characters.
  1274. * @param __n Length of the resulting string.
  1275. * @param __c The character to use.
  1276. * @return Reference to this string.
  1277. *
  1278. * This function sets the value of this string to @a __n copies of
  1279. * character @a __c.
  1280. */
  1281. basic_string&
  1282. assign(size_type __n, _CharT __c)
  1283. { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
  1284. /**
  1285. * @brief Set value to a range of characters.
  1286. * @param __first Iterator referencing the first character to append.
  1287. * @param __last Iterator marking the end of the range.
  1288. * @return Reference to this string.
  1289. *
  1290. * Sets value of string to characters in the range [__first,__last).
  1291. */
  1292. template<class _InputIterator>
  1293. basic_string&
  1294. assign(_InputIterator __first, _InputIterator __last)
  1295. { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
  1296. #if __cplusplus >= 201103L
  1297. /**
  1298. * @brief Set value to an initializer_list of characters.
  1299. * @param __l The initializer_list of characters to assign.
  1300. * @return Reference to this string.
  1301. */
  1302. basic_string&
  1303. assign(initializer_list<_CharT> __l)
  1304. { return this->assign(__l.begin(), __l.size()); }
  1305. #endif // C++11
  1306. #if __cplusplus >= 201703L
  1307. /**
  1308. * @brief Set value from a string_view.
  1309. * @param __svt The source object convertible to string_view.
  1310. * @return Reference to this string.
  1311. */
  1312. template<typename _Tp>
  1313. _If_sv<_Tp, basic_string&>
  1314. assign(const _Tp& __svt)
  1315. {
  1316. __sv_type __sv = __svt;
  1317. return this->assign(__sv.data(), __sv.size());
  1318. }
  1319. /**
  1320. * @brief Set value from a range of characters in a string_view.
  1321. * @param __svt The source object convertible to string_view.
  1322. * @param __pos The position in the string_view to assign from.
  1323. * @param __n The number of characters to assign.
  1324. * @return Reference to this string.
  1325. */
  1326. template<typename _Tp>
  1327. _If_sv<_Tp, basic_string&>
  1328. assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
  1329. {
  1330. __sv_type __sv = __svt;
  1331. return assign(__sv.data()
  1332. + std::__sv_check(__sv.size(), __pos, "basic_string::assign"),
  1333. std::__sv_limit(__sv.size(), __pos, __n));
  1334. }
  1335. #endif // C++17
  1336. /**
  1337. * @brief Insert multiple characters.
  1338. * @param __p Iterator referencing location in string to insert at.
  1339. * @param __n Number of characters to insert
  1340. * @param __c The character to insert.
  1341. * @throw std::length_error If new length exceeds @c max_size().
  1342. *
  1343. * Inserts @a __n copies of character @a __c starting at the
  1344. * position referenced by iterator @a __p. If adding
  1345. * characters causes the length to exceed max_size(),
  1346. * length_error is thrown. The value of the string doesn't
  1347. * change if an error is thrown.
  1348. */
  1349. void
  1350. insert(iterator __p, size_type __n, _CharT __c)
  1351. { this->replace(__p, __p, __n, __c); }
  1352. /**
  1353. * @brief Insert a range of characters.
  1354. * @param __p Iterator referencing location in string to insert at.
  1355. * @param __beg Start of range.
  1356. * @param __end End of range.
  1357. * @throw std::length_error If new length exceeds @c max_size().
  1358. *
  1359. * Inserts characters in range [__beg,__end). If adding
  1360. * characters causes the length to exceed max_size(),
  1361. * length_error is thrown. The value of the string doesn't
  1362. * change if an error is thrown.
  1363. */
  1364. template<class _InputIterator>
  1365. void
  1366. insert(iterator __p, _InputIterator __beg, _InputIterator __end)
  1367. { this->replace(__p, __p, __beg, __end); }
  1368. #if __cplusplus >= 201103L
  1369. /**
  1370. * @brief Insert an initializer_list of characters.
  1371. * @param __p Iterator referencing location in string to insert at.
  1372. * @param __l The initializer_list of characters to insert.
  1373. * @throw std::length_error If new length exceeds @c max_size().
  1374. */
  1375. void
  1376. insert(iterator __p, initializer_list<_CharT> __l)
  1377. {
  1378. _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
  1379. this->insert(__p - _M_ibegin(), __l.begin(), __l.size());
  1380. }
  1381. #endif // C++11
  1382. /**
  1383. * @brief Insert value of a string.
  1384. * @param __pos1 Position in string to insert at.
  1385. * @param __str The string to insert.
  1386. * @return Reference to this string.
  1387. * @throw std::length_error If new length exceeds @c max_size().
  1388. *
  1389. * Inserts value of @a __str starting at @a __pos1. If adding
  1390. * characters causes the length to exceed max_size(),
  1391. * length_error is thrown. The value of the string doesn't
  1392. * change if an error is thrown.
  1393. */
  1394. basic_string&
  1395. insert(size_type __pos1, const basic_string& __str)
  1396. { return this->insert(__pos1, __str, size_type(0), __str.size()); }
  1397. /**
  1398. * @brief Insert a substring.
  1399. * @param __pos1 Position in string to insert at.
  1400. * @param __str The string to insert.
  1401. * @param __pos2 Start of characters in str to insert.
  1402. * @param __n Number of characters to insert.
  1403. * @return Reference to this string.
  1404. * @throw std::length_error If new length exceeds @c max_size().
  1405. * @throw std::out_of_range If @a pos1 > size() or
  1406. * @a __pos2 > @a str.size().
  1407. *
  1408. * Starting at @a pos1, insert @a __n character of @a __str
  1409. * beginning with @a __pos2. If adding characters causes the
  1410. * length to exceed max_size(), length_error is thrown. If @a
  1411. * __pos1 is beyond the end of this string or @a __pos2 is
  1412. * beyond the end of @a __str, out_of_range is thrown. The
  1413. * value of the string doesn't change if an error is thrown.
  1414. */
  1415. basic_string&
  1416. insert(size_type __pos1, const basic_string& __str,
  1417. size_type __pos2, size_type __n = npos)
  1418. { return this->insert(__pos1, __str._M_data()
  1419. + __str._M_check(__pos2, "basic_string::insert"),
  1420. __str._M_limit(__pos2, __n)); }
  1421. /**
  1422. * @brief Insert a C substring.
  1423. * @param __pos Position in string to insert at.
  1424. * @param __s The C string to insert.
  1425. * @param __n The number of characters to insert.
  1426. * @return Reference to this string.
  1427. * @throw std::length_error If new length exceeds @c max_size().
  1428. * @throw std::out_of_range If @a __pos is beyond the end of this
  1429. * string.
  1430. *
  1431. * Inserts the first @a __n characters of @a __s starting at @a
  1432. * __pos. If adding characters causes the length to exceed
  1433. * max_size(), length_error is thrown. If @a __pos is beyond
  1434. * end(), out_of_range is thrown. The value of the string
  1435. * doesn't change if an error is thrown.
  1436. */
  1437. basic_string&
  1438. insert(size_type __pos, const _CharT* __s, size_type __n);
  1439. /**
  1440. * @brief Insert a C string.
  1441. * @param __pos Position in string to insert at.
  1442. * @param __s The C string to insert.
  1443. * @return Reference to this string.
  1444. * @throw std::length_error If new length exceeds @c max_size().
  1445. * @throw std::out_of_range If @a pos is beyond the end of this
  1446. * string.
  1447. *
  1448. * Inserts the first @a n characters of @a __s starting at @a __pos. If
  1449. * adding characters causes the length to exceed max_size(),
  1450. * length_error is thrown. If @a __pos is beyond end(), out_of_range is
  1451. * thrown. The value of the string doesn't change if an error is
  1452. * thrown.
  1453. */
  1454. basic_string&
  1455. insert(size_type __pos, const _CharT* __s)
  1456. {
  1457. __glibcxx_requires_string(__s);
  1458. return this->insert(__pos, __s, traits_type::length(__s));
  1459. }
  1460. /**
  1461. * @brief Insert multiple characters.
  1462. * @param __pos Index in string to insert at.
  1463. * @param __n Number of characters to insert
  1464. * @param __c The character to insert.
  1465. * @return Reference to this string.
  1466. * @throw std::length_error If new length exceeds @c max_size().
  1467. * @throw std::out_of_range If @a __pos is beyond the end of this
  1468. * string.
  1469. *
  1470. * Inserts @a __n copies of character @a __c starting at index
  1471. * @a __pos. If adding characters causes the length to exceed
  1472. * max_size(), length_error is thrown. If @a __pos > length(),
  1473. * out_of_range is thrown. The value of the string doesn't
  1474. * change if an error is thrown.
  1475. */
  1476. basic_string&
  1477. insert(size_type __pos, size_type __n, _CharT __c)
  1478. { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
  1479. size_type(0), __n, __c); }
  1480. /**
  1481. * @brief Insert one character.
  1482. * @param __p Iterator referencing position in string to insert at.
  1483. * @param __c The character to insert.
  1484. * @return Iterator referencing newly inserted char.
  1485. * @throw std::length_error If new length exceeds @c max_size().
  1486. *
  1487. * Inserts character @a __c at position referenced by @a __p.
  1488. * If adding character causes the length to exceed max_size(),
  1489. * length_error is thrown. If @a __p is beyond end of string,
  1490. * out_of_range is thrown. The value of the string doesn't
  1491. * change if an error is thrown.
  1492. */
  1493. iterator
  1494. insert(iterator __p, _CharT __c)
  1495. {
  1496. _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
  1497. const size_type __pos = __p - _M_ibegin();
  1498. _M_replace_aux(__pos, size_type(0), size_type(1), __c);
  1499. _M_rep()->_M_set_leaked();
  1500. return iterator(_M_data() + __pos);
  1501. }
  1502. #if __cplusplus >= 201703L
  1503. /**
  1504. * @brief Insert a string_view.
  1505. * @param __pos Position in string to insert at.
  1506. * @param __svt The object convertible to string_view to insert.
  1507. * @return Reference to this string.
  1508. */
  1509. template<typename _Tp>
  1510. _If_sv<_Tp, basic_string&>
  1511. insert(size_type __pos, const _Tp& __svt)
  1512. {
  1513. __sv_type __sv = __svt;
  1514. return this->insert(__pos, __sv.data(), __sv.size());
  1515. }
  1516. /**
  1517. * @brief Insert a string_view.
  1518. * @param __pos1 Position in string to insert at.
  1519. * @param __svt The object convertible to string_view to insert from.
  1520. * @param __pos2 Position in string_view to insert from.
  1521. * @param __n The number of characters to insert.
  1522. * @return Reference to this string.
  1523. */
  1524. template<typename _Tp>
  1525. _If_sv<_Tp, basic_string&>
  1526. insert(size_type __pos1, const _Tp& __svt,
  1527. size_type __pos2, size_type __n = npos)
  1528. {
  1529. __sv_type __sv = __svt;
  1530. return this->replace(__pos1, size_type(0), __sv.data()
  1531. + std::__sv_check(__sv.size(), __pos2, "basic_string::insert"),
  1532. std::__sv_limit(__sv.size(), __pos2, __n));
  1533. }
  1534. #endif // C++17
  1535. /**
  1536. * @brief Remove characters.
  1537. * @param __pos Index of first character to remove (default 0).
  1538. * @param __n Number of characters to remove (default remainder).
  1539. * @return Reference to this string.
  1540. * @throw std::out_of_range If @a pos is beyond the end of this
  1541. * string.
  1542. *
  1543. * Removes @a __n characters from this string starting at @a
  1544. * __pos. The length of the string is reduced by @a __n. If
  1545. * there are < @a __n characters to remove, the remainder of
  1546. * the string is truncated. If @a __p is beyond end of string,
  1547. * out_of_range is thrown. The value of the string doesn't
  1548. * change if an error is thrown.
  1549. */
  1550. basic_string&
  1551. erase(size_type __pos = 0, size_type __n = npos)
  1552. {
  1553. _M_mutate(_M_check(__pos, "basic_string::erase"),
  1554. _M_limit(__pos, __n), size_type(0));
  1555. return *this;
  1556. }
  1557. /**
  1558. * @brief Remove one character.
  1559. * @param __position Iterator referencing the character to remove.
  1560. * @return iterator referencing same location after removal.
  1561. *
  1562. * Removes the character at @a __position from this string. The value
  1563. * of the string doesn't change if an error is thrown.
  1564. */
  1565. iterator
  1566. erase(iterator __position)
  1567. {
  1568. _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
  1569. && __position < _M_iend());
  1570. const size_type __pos = __position - _M_ibegin();
  1571. _M_mutate(__pos, size_type(1), size_type(0));
  1572. _M_rep()->_M_set_leaked();
  1573. return iterator(_M_data() + __pos);
  1574. }
  1575. /**
  1576. * @brief Remove a range of characters.
  1577. * @param __first Iterator referencing the first character to remove.
  1578. * @param __last Iterator referencing the end of the range.
  1579. * @return Iterator referencing location of first after removal.
  1580. *
  1581. * Removes the characters in the range [first,last) from this string.
  1582. * The value of the string doesn't change if an error is thrown.
  1583. */
  1584. iterator
  1585. erase(iterator __first, iterator __last);
  1586. #if __cplusplus >= 201103L
  1587. /**
  1588. * @brief Remove the last character.
  1589. *
  1590. * The string must be non-empty.
  1591. */
  1592. void
  1593. pop_back() // FIXME C++11: should be noexcept.
  1594. {
  1595. __glibcxx_assert(!empty());
  1596. erase(size() - 1, 1);
  1597. }
  1598. #endif // C++11
  1599. /**
  1600. * @brief Replace characters with value from another string.
  1601. * @param __pos Index of first character to replace.
  1602. * @param __n Number of characters to be replaced.
  1603. * @param __str String to insert.
  1604. * @return Reference to this string.
  1605. * @throw std::out_of_range If @a pos is beyond the end of this
  1606. * string.
  1607. * @throw std::length_error If new length exceeds @c max_size().
  1608. *
  1609. * Removes the characters in the range [__pos,__pos+__n) from
  1610. * this string. In place, the value of @a __str is inserted.
  1611. * If @a __pos is beyond end of string, out_of_range is thrown.
  1612. * If the length of the result exceeds max_size(), length_error
  1613. * is thrown. The value of the string doesn't change if an
  1614. * error is thrown.
  1615. */
  1616. basic_string&
  1617. replace(size_type __pos, size_type __n, const basic_string& __str)
  1618. { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
  1619. /**
  1620. * @brief Replace characters with value from another string.
  1621. * @param __pos1 Index of first character to replace.
  1622. * @param __n1 Number of characters to be replaced.
  1623. * @param __str String to insert.
  1624. * @param __pos2 Index of first character of str to use.
  1625. * @param __n2 Number of characters from str to use.
  1626. * @return Reference to this string.
  1627. * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
  1628. * __str.size().
  1629. * @throw std::length_error If new length exceeds @c max_size().
  1630. *
  1631. * Removes the characters in the range [__pos1,__pos1 + n) from this
  1632. * string. In place, the value of @a __str is inserted. If @a __pos is
  1633. * beyond end of string, out_of_range is thrown. If the length of the
  1634. * result exceeds max_size(), length_error is thrown. The value of the
  1635. * string doesn't change if an error is thrown.
  1636. */
  1637. basic_string&
  1638. replace(size_type __pos1, size_type __n1, const basic_string& __str,
  1639. size_type __pos2, size_type __n2 = npos)
  1640. { return this->replace(__pos1, __n1, __str._M_data()
  1641. + __str._M_check(__pos2, "basic_string::replace"),
  1642. __str._M_limit(__pos2, __n2)); }
  1643. /**
  1644. * @brief Replace characters with value of a C substring.
  1645. * @param __pos Index of first character to replace.
  1646. * @param __n1 Number of characters to be replaced.
  1647. * @param __s C string to insert.
  1648. * @param __n2 Number of characters from @a s to use.
  1649. * @return Reference to this string.
  1650. * @throw std::out_of_range If @a pos1 > size().
  1651. * @throw std::length_error If new length exceeds @c max_size().
  1652. *
  1653. * Removes the characters in the range [__pos,__pos + __n1)
  1654. * from this string. In place, the first @a __n2 characters of
  1655. * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
  1656. * @a __pos is beyond end of string, out_of_range is thrown. If
  1657. * the length of result exceeds max_size(), length_error is
  1658. * thrown. The value of the string doesn't change if an error
  1659. * is thrown.
  1660. */
  1661. basic_string&
  1662. replace(size_type __pos, size_type __n1, const _CharT* __s,
  1663. size_type __n2);
  1664. /**
  1665. * @brief Replace characters with value of a C string.
  1666. * @param __pos Index of first character to replace.
  1667. * @param __n1 Number of characters to be replaced.
  1668. * @param __s C string to insert.
  1669. * @return Reference to this string.
  1670. * @throw std::out_of_range If @a pos > size().
  1671. * @throw std::length_error If new length exceeds @c max_size().
  1672. *
  1673. * Removes the characters in the range [__pos,__pos + __n1)
  1674. * from this string. In place, the characters of @a __s are
  1675. * inserted. If @a __pos is beyond end of string, out_of_range
  1676. * is thrown. If the length of result exceeds max_size(),
  1677. * length_error is thrown. The value of the string doesn't
  1678. * change if an error is thrown.
  1679. */
  1680. basic_string&
  1681. replace(size_type __pos, size_type __n1, const _CharT* __s)
  1682. {
  1683. __glibcxx_requires_string(__s);
  1684. return this->replace(__pos, __n1, __s, traits_type::length(__s));
  1685. }
  1686. /**
  1687. * @brief Replace characters with multiple characters.
  1688. * @param __pos Index of first character to replace.
  1689. * @param __n1 Number of characters to be replaced.
  1690. * @param __n2 Number of characters to insert.
  1691. * @param __c Character to insert.
  1692. * @return Reference to this string.
  1693. * @throw std::out_of_range If @a __pos > size().
  1694. * @throw std::length_error If new length exceeds @c max_size().
  1695. *
  1696. * Removes the characters in the range [pos,pos + n1) from this
  1697. * string. In place, @a __n2 copies of @a __c are inserted.
  1698. * If @a __pos is beyond end of string, out_of_range is thrown.
  1699. * If the length of result exceeds max_size(), length_error is
  1700. * thrown. The value of the string doesn't change if an error
  1701. * is thrown.
  1702. */
  1703. basic_string&
  1704. replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
  1705. { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
  1706. _M_limit(__pos, __n1), __n2, __c); }
  1707. /**
  1708. * @brief Replace range of characters with string.
  1709. * @param __i1 Iterator referencing start of range to replace.
  1710. * @param __i2 Iterator referencing end of range to replace.
  1711. * @param __str String value to insert.
  1712. * @return Reference to this string.
  1713. * @throw std::length_error If new length exceeds @c max_size().
  1714. *
  1715. * Removes the characters in the range [__i1,__i2). In place,
  1716. * the value of @a __str is inserted. If the length of result
  1717. * exceeds max_size(), length_error is thrown. The value of
  1718. * the string doesn't change if an error is thrown.
  1719. */
  1720. basic_string&
  1721. replace(iterator __i1, iterator __i2, const basic_string& __str)
  1722. { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
  1723. /**
  1724. * @brief Replace range of characters with C substring.
  1725. * @param __i1 Iterator referencing start of range to replace.
  1726. * @param __i2 Iterator referencing end of range to replace.
  1727. * @param __s C string value to insert.
  1728. * @param __n Number of characters from s to insert.
  1729. * @return Reference to this string.
  1730. * @throw std::length_error If new length exceeds @c max_size().
  1731. *
  1732. * Removes the characters in the range [__i1,__i2). In place,
  1733. * the first @a __n characters of @a __s are inserted. If the
  1734. * length of result exceeds max_size(), length_error is thrown.
  1735. * The value of the string doesn't change if an error is
  1736. * thrown.
  1737. */
  1738. basic_string&
  1739. replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
  1740. {
  1741. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1742. && __i2 <= _M_iend());
  1743. return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
  1744. }
  1745. /**
  1746. * @brief Replace range of characters with C string.
  1747. * @param __i1 Iterator referencing start of range to replace.
  1748. * @param __i2 Iterator referencing end of range to replace.
  1749. * @param __s C string value to insert.
  1750. * @return Reference to this string.
  1751. * @throw std::length_error If new length exceeds @c max_size().
  1752. *
  1753. * Removes the characters in the range [__i1,__i2). In place,
  1754. * the characters of @a __s are inserted. If the length of
  1755. * result exceeds max_size(), length_error is thrown. The
  1756. * value of the string doesn't change if an error is thrown.
  1757. */
  1758. basic_string&
  1759. replace(iterator __i1, iterator __i2, const _CharT* __s)
  1760. {
  1761. __glibcxx_requires_string(__s);
  1762. return this->replace(__i1, __i2, __s, traits_type::length(__s));
  1763. }
  1764. /**
  1765. * @brief Replace range of characters with multiple characters
  1766. * @param __i1 Iterator referencing start of range to replace.
  1767. * @param __i2 Iterator referencing end of range to replace.
  1768. * @param __n Number of characters to insert.
  1769. * @param __c Character to insert.
  1770. * @return Reference to this string.
  1771. * @throw std::length_error If new length exceeds @c max_size().
  1772. *
  1773. * Removes the characters in the range [__i1,__i2). In place,
  1774. * @a __n copies of @a __c are inserted. If the length of
  1775. * result exceeds max_size(), length_error is thrown. The
  1776. * value of the string doesn't change if an error is thrown.
  1777. */
  1778. basic_string&
  1779. replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
  1780. {
  1781. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1782. && __i2 <= _M_iend());
  1783. return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
  1784. }
  1785. /**
  1786. * @brief Replace range of characters with range.
  1787. * @param __i1 Iterator referencing start of range to replace.
  1788. * @param __i2 Iterator referencing end of range to replace.
  1789. * @param __k1 Iterator referencing start of range to insert.
  1790. * @param __k2 Iterator referencing end of range to insert.
  1791. * @return Reference to this string.
  1792. * @throw std::length_error If new length exceeds @c max_size().
  1793. *
  1794. * Removes the characters in the range [__i1,__i2). In place,
  1795. * characters in the range [__k1,__k2) are inserted. If the
  1796. * length of result exceeds max_size(), length_error is thrown.
  1797. * The value of the string doesn't change if an error is
  1798. * thrown.
  1799. */
  1800. template<class _InputIterator>
  1801. basic_string&
  1802. replace(iterator __i1, iterator __i2,
  1803. _InputIterator __k1, _InputIterator __k2)
  1804. {
  1805. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1806. && __i2 <= _M_iend());
  1807. __glibcxx_requires_valid_range(__k1, __k2);
  1808. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  1809. return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
  1810. }
  1811. // Specializations for the common case of pointer and iterator:
  1812. // useful to avoid the overhead of temporary buffering in _M_replace.
  1813. basic_string&
  1814. replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
  1815. {
  1816. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1817. && __i2 <= _M_iend());
  1818. __glibcxx_requires_valid_range(__k1, __k2);
  1819. return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
  1820. __k1, __k2 - __k1);
  1821. }
  1822. basic_string&
  1823. replace(iterator __i1, iterator __i2,
  1824. const _CharT* __k1, const _CharT* __k2)
  1825. {
  1826. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1827. && __i2 <= _M_iend());
  1828. __glibcxx_requires_valid_range(__k1, __k2);
  1829. return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
  1830. __k1, __k2 - __k1);
  1831. }
  1832. basic_string&
  1833. replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
  1834. {
  1835. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1836. && __i2 <= _M_iend());
  1837. __glibcxx_requires_valid_range(__k1, __k2);
  1838. return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
  1839. __k1.base(), __k2 - __k1);
  1840. }
  1841. basic_string&
  1842. replace(iterator __i1, iterator __i2,
  1843. const_iterator __k1, const_iterator __k2)
  1844. {
  1845. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1846. && __i2 <= _M_iend());
  1847. __glibcxx_requires_valid_range(__k1, __k2);
  1848. return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
  1849. __k1.base(), __k2 - __k1);
  1850. }
  1851. #if __cplusplus >= 201103L
  1852. /**
  1853. * @brief Replace range of characters with initializer_list.
  1854. * @param __i1 Iterator referencing start of range to replace.
  1855. * @param __i2 Iterator referencing end of range to replace.
  1856. * @param __l The initializer_list of characters to insert.
  1857. * @return Reference to this string.
  1858. * @throw std::length_error If new length exceeds @c max_size().
  1859. *
  1860. * Removes the characters in the range [__i1,__i2). In place,
  1861. * characters in the range [__k1,__k2) are inserted. If the
  1862. * length of result exceeds max_size(), length_error is thrown.
  1863. * The value of the string doesn't change if an error is
  1864. * thrown.
  1865. */
  1866. basic_string& replace(iterator __i1, iterator __i2,
  1867. initializer_list<_CharT> __l)
  1868. { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
  1869. #endif // C++11
  1870. #if __cplusplus >= 201703L
  1871. /**
  1872. * @brief Replace range of characters with string_view.
  1873. * @param __pos The position to replace at.
  1874. * @param __n The number of characters to replace.
  1875. * @param __svt The object convertible to string_view to insert.
  1876. * @return Reference to this string.
  1877. */
  1878. template<typename _Tp>
  1879. _If_sv<_Tp, basic_string&>
  1880. replace(size_type __pos, size_type __n, const _Tp& __svt)
  1881. {
  1882. __sv_type __sv = __svt;
  1883. return this->replace(__pos, __n, __sv.data(), __sv.size());
  1884. }
  1885. /**
  1886. * @brief Replace range of characters with string_view.
  1887. * @param __pos1 The position to replace at.
  1888. * @param __n1 The number of characters to replace.
  1889. * @param __svt The object convertible to string_view to insert from.
  1890. * @param __pos2 The position in the string_view to insert from.
  1891. * @param __n2 The number of characters to insert.
  1892. * @return Reference to this string.
  1893. */
  1894. template<typename _Tp>
  1895. _If_sv<_Tp, basic_string&>
  1896. replace(size_type __pos1, size_type __n1, const _Tp& __svt,
  1897. size_type __pos2, size_type __n2 = npos)
  1898. {
  1899. __sv_type __sv = __svt;
  1900. return this->replace(__pos1, __n1,
  1901. __sv.data()
  1902. + std::__sv_check(__sv.size(), __pos2, "basic_string::replace"),
  1903. std::__sv_limit(__sv.size(), __pos2, __n2));
  1904. }
  1905. /**
  1906. * @brief Replace range of characters with string_view.
  1907. * @param __i1 An iterator referencing the start position
  1908. * to replace at.
  1909. * @param __i2 An iterator referencing the end position
  1910. * for the replace.
  1911. * @param __svt The object convertible to string_view to insert from.
  1912. * @return Reference to this string.
  1913. */
  1914. template<typename _Tp>
  1915. _If_sv<_Tp, basic_string&>
  1916. replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
  1917. {
  1918. __sv_type __sv = __svt;
  1919. return this->replace(__i1 - begin(), __i2 - __i1, __sv);
  1920. }
  1921. #endif // C++17
  1922. private:
  1923. template<class _Integer>
  1924. basic_string&
  1925. _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
  1926. _Integer __val, __true_type)
  1927. { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
  1928. template<class _InputIterator>
  1929. basic_string&
  1930. _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
  1931. _InputIterator __k2, __false_type);
  1932. basic_string&
  1933. _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
  1934. _CharT __c);
  1935. basic_string&
  1936. _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
  1937. size_type __n2);
  1938. // _S_construct_aux is used to implement the 21.3.1 para 15 which
  1939. // requires special behaviour if _InIter is an integral type
  1940. template<class _InIterator>
  1941. static _CharT*
  1942. _S_construct_aux(_InIterator __beg, _InIterator __end,
  1943. const _Alloc& __a, __false_type)
  1944. {
  1945. typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
  1946. return _S_construct(__beg, __end, __a, _Tag());
  1947. }
  1948. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1949. // 438. Ambiguity in the "do the right thing" clause
  1950. template<class _Integer>
  1951. static _CharT*
  1952. _S_construct_aux(_Integer __beg, _Integer __end,
  1953. const _Alloc& __a, __true_type)
  1954. { return _S_construct_aux_2(static_cast<size_type>(__beg),
  1955. __end, __a); }
  1956. static _CharT*
  1957. _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
  1958. { return _S_construct(__req, __c, __a); }
  1959. template<class _InIterator>
  1960. static _CharT*
  1961. _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
  1962. {
  1963. typedef typename std::__is_integer<_InIterator>::__type _Integral;
  1964. return _S_construct_aux(__beg, __end, __a, _Integral());
  1965. }
  1966. // For Input Iterators, used in istreambuf_iterators, etc.
  1967. template<class _InIterator>
  1968. static _CharT*
  1969. _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
  1970. input_iterator_tag);
  1971. // For forward_iterators up to random_access_iterators, used for
  1972. // string::iterator, _CharT*, etc.
  1973. template<class _FwdIterator>
  1974. static _CharT*
  1975. _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
  1976. forward_iterator_tag);
  1977. static _CharT*
  1978. _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
  1979. public:
  1980. /**
  1981. * @brief Copy substring into C string.
  1982. * @param __s C string to copy value into.
  1983. * @param __n Number of characters to copy.
  1984. * @param __pos Index of first character to copy.
  1985. * @return Number of characters actually copied
  1986. * @throw std::out_of_range If __pos > size().
  1987. *
  1988. * Copies up to @a __n characters starting at @a __pos into the
  1989. * C string @a __s. If @a __pos is %greater than size(),
  1990. * out_of_range is thrown.
  1991. */
  1992. size_type
  1993. copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
  1994. /**
  1995. * @brief Swap contents with another string.
  1996. * @param __s String to swap with.
  1997. *
  1998. * Exchanges the contents of this string with that of @a __s in constant
  1999. * time.
  2000. */
  2001. void
  2002. swap(basic_string& __s)
  2003. _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value);
  2004. // String operations:
  2005. /**
  2006. * @brief Return const pointer to null-terminated contents.
  2007. *
  2008. * This is a handle to internal data. Do not modify or dire things may
  2009. * happen.
  2010. */
  2011. const _CharT*
  2012. c_str() const _GLIBCXX_NOEXCEPT
  2013. { return _M_data(); }
  2014. /**
  2015. * @brief Return const pointer to contents.
  2016. *
  2017. * This is a pointer to internal data. It is undefined to modify
  2018. * the contents through the returned pointer. To get a pointer that
  2019. * allows modifying the contents use @c &str[0] instead,
  2020. * (or in C++17 the non-const @c str.data() overload).
  2021. */
  2022. const _CharT*
  2023. data() const _GLIBCXX_NOEXCEPT
  2024. { return _M_data(); }
  2025. #if __cplusplus >= 201703L
  2026. /**
  2027. * @brief Return non-const pointer to contents.
  2028. *
  2029. * This is a pointer to the character sequence held by the string.
  2030. * Modifying the characters in the sequence is allowed.
  2031. */
  2032. _CharT*
  2033. data() noexcept
  2034. {
  2035. _M_leak();
  2036. return _M_data();
  2037. }
  2038. #endif
  2039. /**
  2040. * @brief Return copy of allocator used to construct this string.
  2041. */
  2042. allocator_type
  2043. get_allocator() const _GLIBCXX_NOEXCEPT
  2044. { return _M_dataplus; }
  2045. /**
  2046. * @brief Find position of a C substring.
  2047. * @param __s C string to locate.
  2048. * @param __pos Index of character to search from.
  2049. * @param __n Number of characters from @a s to search for.
  2050. * @return Index of start of first occurrence.
  2051. *
  2052. * Starting from @a __pos, searches forward for the first @a
  2053. * __n characters in @a __s within this string. If found,
  2054. * returns the index where it begins. If not found, returns
  2055. * npos.
  2056. */
  2057. size_type
  2058. find(const _CharT* __s, size_type __pos, size_type __n) const
  2059. _GLIBCXX_NOEXCEPT;
  2060. /**
  2061. * @brief Find position of a string.
  2062. * @param __str String to locate.
  2063. * @param __pos Index of character to search from (default 0).
  2064. * @return Index of start of first occurrence.
  2065. *
  2066. * Starting from @a __pos, searches forward for value of @a __str within
  2067. * this string. If found, returns the index where it begins. If not
  2068. * found, returns npos.
  2069. */
  2070. size_type
  2071. find(const basic_string& __str, size_type __pos = 0) const
  2072. _GLIBCXX_NOEXCEPT
  2073. { return this->find(__str.data(), __pos, __str.size()); }
  2074. /**
  2075. * @brief Find position of a C string.
  2076. * @param __s C string to locate.
  2077. * @param __pos Index of character to search from (default 0).
  2078. * @return Index of start of first occurrence.
  2079. *
  2080. * Starting from @a __pos, searches forward for the value of @a
  2081. * __s within this string. If found, returns the index where
  2082. * it begins. If not found, returns npos.
  2083. */
  2084. size_type
  2085. find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
  2086. {
  2087. __glibcxx_requires_string(__s);
  2088. return this->find(__s, __pos, traits_type::length(__s));
  2089. }
  2090. /**
  2091. * @brief Find position of a character.
  2092. * @param __c Character to locate.
  2093. * @param __pos Index of character to search from (default 0).
  2094. * @return Index of first occurrence.
  2095. *
  2096. * Starting from @a __pos, searches forward for @a __c within
  2097. * this string. If found, returns the index where it was
  2098. * found. If not found, returns npos.
  2099. */
  2100. size_type
  2101. find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
  2102. #if __cplusplus >= 201703L
  2103. /**
  2104. * @brief Find position of a string_view.
  2105. * @param __svt The object convertible to string_view to locate.
  2106. * @param __pos Index of character to search from (default 0).
  2107. * @return Index of start of first occurrence.
  2108. */
  2109. template<typename _Tp>
  2110. _If_sv<_Tp, size_type>
  2111. find(const _Tp& __svt, size_type __pos = 0) const
  2112. noexcept(is_same<_Tp, __sv_type>::value)
  2113. {
  2114. __sv_type __sv = __svt;
  2115. return this->find(__sv.data(), __pos, __sv.size());
  2116. }
  2117. #endif // C++17
  2118. /**
  2119. * @brief Find last position of a string.
  2120. * @param __str String to locate.
  2121. * @param __pos Index of character to search back from (default end).
  2122. * @return Index of start of last occurrence.
  2123. *
  2124. * Starting from @a __pos, searches backward for value of @a
  2125. * __str within this string. If found, returns the index where
  2126. * it begins. If not found, returns npos.
  2127. */
  2128. size_type
  2129. rfind(const basic_string& __str, size_type __pos = npos) const
  2130. _GLIBCXX_NOEXCEPT
  2131. { return this->rfind(__str.data(), __pos, __str.size()); }
  2132. /**
  2133. * @brief Find last position of a C substring.
  2134. * @param __s C string to locate.
  2135. * @param __pos Index of character to search back from.
  2136. * @param __n Number of characters from s to search for.
  2137. * @return Index of start of last occurrence.
  2138. *
  2139. * Starting from @a __pos, searches backward for the first @a
  2140. * __n characters in @a __s within this string. If found,
  2141. * returns the index where it begins. If not found, returns
  2142. * npos.
  2143. */
  2144. size_type
  2145. rfind(const _CharT* __s, size_type __pos, size_type __n) const
  2146. _GLIBCXX_NOEXCEPT;
  2147. /**
  2148. * @brief Find last position of a C string.
  2149. * @param __s C string to locate.
  2150. * @param __pos Index of character to start search at (default end).
  2151. * @return Index of start of last occurrence.
  2152. *
  2153. * Starting from @a __pos, searches backward for the value of
  2154. * @a __s within this string. If found, returns the index
  2155. * where it begins. If not found, returns npos.
  2156. */
  2157. size_type
  2158. rfind(const _CharT* __s, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
  2159. {
  2160. __glibcxx_requires_string(__s);
  2161. return this->rfind(__s, __pos, traits_type::length(__s));
  2162. }
  2163. /**
  2164. * @brief Find last position of a character.
  2165. * @param __c Character to locate.
  2166. * @param __pos Index of character to search back from (default end).
  2167. * @return Index of last occurrence.
  2168. *
  2169. * Starting from @a __pos, searches backward for @a __c within
  2170. * this string. If found, returns the index where it was
  2171. * found. If not found, returns npos.
  2172. */
  2173. size_type
  2174. rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
  2175. #if __cplusplus >= 201703L
  2176. /**
  2177. * @brief Find last position of a string_view.
  2178. * @param __svt The object convertible to string_view to locate.
  2179. * @param __pos Index of character to search back from (default end).
  2180. * @return Index of start of last occurrence.
  2181. */
  2182. template<typename _Tp>
  2183. _If_sv<_Tp, size_type>
  2184. rfind(const _Tp& __svt, size_type __pos = npos) const
  2185. noexcept(is_same<_Tp, __sv_type>::value)
  2186. {
  2187. __sv_type __sv = __svt;
  2188. return this->rfind(__sv.data(), __pos, __sv.size());
  2189. }
  2190. #endif // C++17
  2191. /**
  2192. * @brief Find position of a character of string.
  2193. * @param __str String containing characters to locate.
  2194. * @param __pos Index of character to search from (default 0).
  2195. * @return Index of first occurrence.
  2196. *
  2197. * Starting from @a __pos, searches forward for one of the
  2198. * characters of @a __str within this string. If found,
  2199. * returns the index where it was found. If not found, returns
  2200. * npos.
  2201. */
  2202. size_type
  2203. find_first_of(const basic_string& __str, size_type __pos = 0) const
  2204. _GLIBCXX_NOEXCEPT
  2205. { return this->find_first_of(__str.data(), __pos, __str.size()); }
  2206. /**
  2207. * @brief Find position of a character of C substring.
  2208. * @param __s String containing characters to locate.
  2209. * @param __pos Index of character to search from.
  2210. * @param __n Number of characters from s to search for.
  2211. * @return Index of first occurrence.
  2212. *
  2213. * Starting from @a __pos, searches forward for one of the
  2214. * first @a __n characters of @a __s within this string. If
  2215. * found, returns the index where it was found. If not found,
  2216. * returns npos.
  2217. */
  2218. size_type
  2219. find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
  2220. _GLIBCXX_NOEXCEPT;
  2221. /**
  2222. * @brief Find position of a character of C string.
  2223. * @param __s String containing characters to locate.
  2224. * @param __pos Index of character to search from (default 0).
  2225. * @return Index of first occurrence.
  2226. *
  2227. * Starting from @a __pos, searches forward for one of the
  2228. * characters of @a __s within this string. If found, returns
  2229. * the index where it was found. If not found, returns npos.
  2230. */
  2231. size_type
  2232. find_first_of(const _CharT* __s, size_type __pos = 0) const
  2233. _GLIBCXX_NOEXCEPT
  2234. {
  2235. __glibcxx_requires_string(__s);
  2236. return this->find_first_of(__s, __pos, traits_type::length(__s));
  2237. }
  2238. /**
  2239. * @brief Find position of a character.
  2240. * @param __c Character to locate.
  2241. * @param __pos Index of character to search from (default 0).
  2242. * @return Index of first occurrence.
  2243. *
  2244. * Starting from @a __pos, searches forward for the character
  2245. * @a __c within this string. If found, returns the index
  2246. * where it was found. If not found, returns npos.
  2247. *
  2248. * Note: equivalent to find(__c, __pos).
  2249. */
  2250. size_type
  2251. find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
  2252. { return this->find(__c, __pos); }
  2253. #if __cplusplus >= 201703L
  2254. /**
  2255. * @brief Find position of a character of a string_view.
  2256. * @param __svt An object convertible to string_view containing
  2257. * characters to locate.
  2258. * @param __pos Index of character to search from (default 0).
  2259. * @return Index of first occurrence.
  2260. */
  2261. template<typename _Tp>
  2262. _If_sv<_Tp, size_type>
  2263. find_first_of(const _Tp& __svt, size_type __pos = 0) const
  2264. noexcept(is_same<_Tp, __sv_type>::value)
  2265. {
  2266. __sv_type __sv = __svt;
  2267. return this->find_first_of(__sv.data(), __pos, __sv.size());
  2268. }
  2269. #endif // C++17
  2270. /**
  2271. * @brief Find last position of a character of string.
  2272. * @param __str String containing characters to locate.
  2273. * @param __pos Index of character to search back from (default end).
  2274. * @return Index of last occurrence.
  2275. *
  2276. * Starting from @a __pos, searches backward for one of the
  2277. * characters of @a __str within this string. If found,
  2278. * returns the index where it was found. If not found, returns
  2279. * npos.
  2280. */
  2281. size_type
  2282. find_last_of(const basic_string& __str, size_type __pos = npos) const
  2283. _GLIBCXX_NOEXCEPT
  2284. { return this->find_last_of(__str.data(), __pos, __str.size()); }
  2285. /**
  2286. * @brief Find last position of a character of C substring.
  2287. * @param __s C string containing characters to locate.
  2288. * @param __pos Index of character to search back from.
  2289. * @param __n Number of characters from s to search for.
  2290. * @return Index of last occurrence.
  2291. *
  2292. * Starting from @a __pos, searches backward for one of the
  2293. * first @a __n characters of @a __s within this string. If
  2294. * found, returns the index where it was found. If not found,
  2295. * returns npos.
  2296. */
  2297. size_type
  2298. find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
  2299. _GLIBCXX_NOEXCEPT;
  2300. /**
  2301. * @brief Find last position of a character of C string.
  2302. * @param __s C string containing characters to locate.
  2303. * @param __pos Index of character to search back from (default end).
  2304. * @return Index of last occurrence.
  2305. *
  2306. * Starting from @a __pos, searches backward for one of the
  2307. * characters of @a __s within this string. If found, returns
  2308. * the index where it was found. If not found, returns npos.
  2309. */
  2310. size_type
  2311. find_last_of(const _CharT* __s, size_type __pos = npos) const
  2312. _GLIBCXX_NOEXCEPT
  2313. {
  2314. __glibcxx_requires_string(__s);
  2315. return this->find_last_of(__s, __pos, traits_type::length(__s));
  2316. }
  2317. /**
  2318. * @brief Find last position of a character.
  2319. * @param __c Character to locate.
  2320. * @param __pos Index of character to search back from (default end).
  2321. * @return Index of last occurrence.
  2322. *
  2323. * Starting from @a __pos, searches backward for @a __c within
  2324. * this string. If found, returns the index where it was
  2325. * found. If not found, returns npos.
  2326. *
  2327. * Note: equivalent to rfind(__c, __pos).
  2328. */
  2329. size_type
  2330. find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
  2331. { return this->rfind(__c, __pos); }
  2332. #if __cplusplus >= 201703L
  2333. /**
  2334. * @brief Find last position of a character of string.
  2335. * @param __svt An object convertible to string_view containing
  2336. * characters to locate.
  2337. * @param __pos Index of character to search back from (default end).
  2338. * @return Index of last occurrence.
  2339. */
  2340. template<typename _Tp>
  2341. _If_sv<_Tp, size_type>
  2342. find_last_of(const _Tp& __svt, size_type __pos = npos) const
  2343. noexcept(is_same<_Tp, __sv_type>::value)
  2344. {
  2345. __sv_type __sv = __svt;
  2346. return this->find_last_of(__sv.data(), __pos, __sv.size());
  2347. }
  2348. #endif // C++17
  2349. /**
  2350. * @brief Find position of a character not in string.
  2351. * @param __str String containing characters to avoid.
  2352. * @param __pos Index of character to search from (default 0).
  2353. * @return Index of first occurrence.
  2354. *
  2355. * Starting from @a __pos, searches forward for a character not contained
  2356. * in @a __str within this string. If found, returns the index where it
  2357. * was found. If not found, returns npos.
  2358. */
  2359. size_type
  2360. find_first_not_of(const basic_string& __str, size_type __pos = 0) const
  2361. _GLIBCXX_NOEXCEPT
  2362. { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
  2363. /**
  2364. * @brief Find position of a character not in C substring.
  2365. * @param __s C string containing characters to avoid.
  2366. * @param __pos Index of character to search from.
  2367. * @param __n Number of characters from __s to consider.
  2368. * @return Index of first occurrence.
  2369. *
  2370. * Starting from @a __pos, searches forward for a character not
  2371. * contained in the first @a __n characters of @a __s within
  2372. * this string. If found, returns the index where it was
  2373. * found. If not found, returns npos.
  2374. */
  2375. size_type
  2376. find_first_not_of(const _CharT* __s, size_type __pos,
  2377. size_type __n) const _GLIBCXX_NOEXCEPT;
  2378. /**
  2379. * @brief Find position of a character not in C string.
  2380. * @param __s C string containing characters to avoid.
  2381. * @param __pos Index of character to search from (default 0).
  2382. * @return Index of first occurrence.
  2383. *
  2384. * Starting from @a __pos, searches forward for a character not
  2385. * contained in @a __s within this string. If found, returns
  2386. * the index where it was found. If not found, returns npos.
  2387. */
  2388. size_type
  2389. find_first_not_of(const _CharT* __s, size_type __pos = 0) const
  2390. _GLIBCXX_NOEXCEPT
  2391. {
  2392. __glibcxx_requires_string(__s);
  2393. return this->find_first_not_of(__s, __pos, traits_type::length(__s));
  2394. }
  2395. /**
  2396. * @brief Find position of a different character.
  2397. * @param __c Character to avoid.
  2398. * @param __pos Index of character to search from (default 0).
  2399. * @return Index of first occurrence.
  2400. *
  2401. * Starting from @a __pos, searches forward for a character
  2402. * other than @a __c within this string. If found, returns the
  2403. * index where it was found. If not found, returns npos.
  2404. */
  2405. size_type
  2406. find_first_not_of(_CharT __c, size_type __pos = 0) const
  2407. _GLIBCXX_NOEXCEPT;
  2408. #if __cplusplus >= 201703L
  2409. /**
  2410. * @brief Find position of a character not in a string_view.
  2411. * @param __svt An object convertible to string_view containing
  2412. * characters to avoid.
  2413. * @param __pos Index of character to search from (default 0).
  2414. * @return Index of first occurrence.
  2415. */
  2416. template<typename _Tp>
  2417. _If_sv<_Tp, size_type>
  2418. find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
  2419. noexcept(is_same<_Tp, __sv_type>::value)
  2420. {
  2421. __sv_type __sv = __svt;
  2422. return this->find_first_not_of(__sv.data(), __pos, __sv.size());
  2423. }
  2424. #endif // C++17
  2425. /**
  2426. * @brief Find last position of a character not in string.
  2427. * @param __str String containing characters to avoid.
  2428. * @param __pos Index of character to search back from (default end).
  2429. * @return Index of last occurrence.
  2430. *
  2431. * Starting from @a __pos, searches backward for a character
  2432. * not contained in @a __str within this string. If found,
  2433. * returns the index where it was found. If not found, returns
  2434. * npos.
  2435. */
  2436. size_type
  2437. find_last_not_of(const basic_string& __str, size_type __pos = npos) const
  2438. _GLIBCXX_NOEXCEPT
  2439. { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
  2440. /**
  2441. * @brief Find last position of a character not in C substring.
  2442. * @param __s C string containing characters to avoid.
  2443. * @param __pos Index of character to search back from.
  2444. * @param __n Number of characters from s to consider.
  2445. * @return Index of last occurrence.
  2446. *
  2447. * Starting from @a __pos, searches backward for a character not
  2448. * contained in the first @a __n characters of @a __s within this string.
  2449. * If found, returns the index where it was found. If not found,
  2450. * returns npos.
  2451. */
  2452. size_type
  2453. find_last_not_of(const _CharT* __s, size_type __pos,
  2454. size_type __n) const _GLIBCXX_NOEXCEPT;
  2455. /**
  2456. * @brief Find last position of a character not in C string.
  2457. * @param __s C string containing characters to avoid.
  2458. * @param __pos Index of character to search back from (default end).
  2459. * @return Index of last occurrence.
  2460. *
  2461. * Starting from @a __pos, searches backward for a character
  2462. * not contained in @a __s within this string. If found,
  2463. * returns the index where it was found. If not found, returns
  2464. * npos.
  2465. */
  2466. size_type
  2467. find_last_not_of(const _CharT* __s, size_type __pos = npos) const
  2468. _GLIBCXX_NOEXCEPT
  2469. {
  2470. __glibcxx_requires_string(__s);
  2471. return this->find_last_not_of(__s, __pos, traits_type::length(__s));
  2472. }
  2473. /**
  2474. * @brief Find last position of a different character.
  2475. * @param __c Character to avoid.
  2476. * @param __pos Index of character to search back from (default end).
  2477. * @return Index of last occurrence.
  2478. *
  2479. * Starting from @a __pos, searches backward for a character other than
  2480. * @a __c within this string. If found, returns the index where it was
  2481. * found. If not found, returns npos.
  2482. */
  2483. size_type
  2484. find_last_not_of(_CharT __c, size_type __pos = npos) const
  2485. _GLIBCXX_NOEXCEPT;
  2486. #if __cplusplus >= 201703L
  2487. /**
  2488. * @brief Find last position of a character not in a string_view.
  2489. * @param __svt An object convertible to string_view containing
  2490. * characters to avoid.
  2491. * @param __pos Index of character to search back from (default end).
  2492. * @return Index of last occurrence.
  2493. */
  2494. template<typename _Tp>
  2495. _If_sv<_Tp, size_type>
  2496. find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
  2497. noexcept(is_same<_Tp, __sv_type>::value)
  2498. {
  2499. __sv_type __sv = __svt;
  2500. return this->find_last_not_of(__sv.data(), __pos, __sv.size());
  2501. }
  2502. #endif // C++17
  2503. /**
  2504. * @brief Get a substring.
  2505. * @param __pos Index of first character (default 0).
  2506. * @param __n Number of characters in substring (default remainder).
  2507. * @return The new string.
  2508. * @throw std::out_of_range If __pos > size().
  2509. *
  2510. * Construct and return a new string using the @a __n
  2511. * characters starting at @a __pos. If the string is too
  2512. * short, use the remainder of the characters. If @a __pos is
  2513. * beyond the end of the string, out_of_range is thrown.
  2514. */
  2515. basic_string
  2516. substr(size_type __pos = 0, size_type __n = npos) const
  2517. { return basic_string(*this,
  2518. _M_check(__pos, "basic_string::substr"), __n); }
  2519. /**
  2520. * @brief Compare to a string.
  2521. * @param __str String to compare against.
  2522. * @return Integer < 0, 0, or > 0.
  2523. *
  2524. * Returns an integer < 0 if this string is ordered before @a
  2525. * __str, 0 if their values are equivalent, or > 0 if this
  2526. * string is ordered after @a __str. Determines the effective
  2527. * length rlen of the strings to compare as the smallest of
  2528. * size() and str.size(). The function then compares the two
  2529. * strings by calling traits::compare(data(), str.data(),rlen).
  2530. * If the result of the comparison is nonzero returns it,
  2531. * otherwise the shorter one is ordered first.
  2532. */
  2533. int
  2534. compare(const basic_string& __str) const
  2535. {
  2536. const size_type __size = this->size();
  2537. const size_type __osize = __str.size();
  2538. const size_type __len = std::min(__size, __osize);
  2539. int __r = traits_type::compare(_M_data(), __str.data(), __len);
  2540. if (!__r)
  2541. __r = _S_compare(__size, __osize);
  2542. return __r;
  2543. }
  2544. #if __cplusplus >= 201703L
  2545. /**
  2546. * @brief Compare to a string_view.
  2547. * @param __svt An object convertible to string_view to compare against.
  2548. * @return Integer < 0, 0, or > 0.
  2549. */
  2550. template<typename _Tp>
  2551. _If_sv<_Tp, int>
  2552. compare(const _Tp& __svt) const
  2553. noexcept(is_same<_Tp, __sv_type>::value)
  2554. {
  2555. __sv_type __sv = __svt;
  2556. const size_type __size = this->size();
  2557. const size_type __osize = __sv.size();
  2558. const size_type __len = std::min(__size, __osize);
  2559. int __r = traits_type::compare(_M_data(), __sv.data(), __len);
  2560. if (!__r)
  2561. __r = _S_compare(__size, __osize);
  2562. return __r;
  2563. }
  2564. /**
  2565. * @brief Compare to a string_view.
  2566. * @param __pos A position in the string to start comparing from.
  2567. * @param __n The number of characters to compare.
  2568. * @param __svt An object convertible to string_view to compare
  2569. * against.
  2570. * @return Integer < 0, 0, or > 0.
  2571. */
  2572. template<typename _Tp>
  2573. _If_sv<_Tp, int>
  2574. compare(size_type __pos, size_type __n, const _Tp& __svt) const
  2575. noexcept(is_same<_Tp, __sv_type>::value)
  2576. {
  2577. __sv_type __sv = __svt;
  2578. return __sv_type(*this).substr(__pos, __n).compare(__sv);
  2579. }
  2580. /**
  2581. * @brief Compare to a string_view.
  2582. * @param __pos1 A position in the string to start comparing from.
  2583. * @param __n1 The number of characters to compare.
  2584. * @param __svt An object convertible to string_view to compare
  2585. * against.
  2586. * @param __pos2 A position in the string_view to start comparing from.
  2587. * @param __n2 The number of characters to compare.
  2588. * @return Integer < 0, 0, or > 0.
  2589. */
  2590. template<typename _Tp>
  2591. _If_sv<_Tp, int>
  2592. compare(size_type __pos1, size_type __n1, const _Tp& __svt,
  2593. size_type __pos2, size_type __n2 = npos) const
  2594. noexcept(is_same<_Tp, __sv_type>::value)
  2595. {
  2596. __sv_type __sv = __svt;
  2597. return __sv_type(*this)
  2598. .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
  2599. }
  2600. #endif // C++17
  2601. /**
  2602. * @brief Compare substring to a string.
  2603. * @param __pos Index of first character of substring.
  2604. * @param __n Number of characters in substring.
  2605. * @param __str String to compare against.
  2606. * @return Integer < 0, 0, or > 0.
  2607. *
  2608. * Form the substring of this string from the @a __n characters
  2609. * starting at @a __pos. Returns an integer < 0 if the
  2610. * substring is ordered before @a __str, 0 if their values are
  2611. * equivalent, or > 0 if the substring is ordered after @a
  2612. * __str. Determines the effective length rlen of the strings
  2613. * to compare as the smallest of the length of the substring
  2614. * and @a __str.size(). The function then compares the two
  2615. * strings by calling
  2616. * traits::compare(substring.data(),str.data(),rlen). If the
  2617. * result of the comparison is nonzero returns it, otherwise
  2618. * the shorter one is ordered first.
  2619. */
  2620. int
  2621. compare(size_type __pos, size_type __n, const basic_string& __str) const;
  2622. /**
  2623. * @brief Compare substring to a substring.
  2624. * @param __pos1 Index of first character of substring.
  2625. * @param __n1 Number of characters in substring.
  2626. * @param __str String to compare against.
  2627. * @param __pos2 Index of first character of substring of str.
  2628. * @param __n2 Number of characters in substring of str.
  2629. * @return Integer < 0, 0, or > 0.
  2630. *
  2631. * Form the substring of this string from the @a __n1
  2632. * characters starting at @a __pos1. Form the substring of @a
  2633. * __str from the @a __n2 characters starting at @a __pos2.
  2634. * Returns an integer < 0 if this substring is ordered before
  2635. * the substring of @a __str, 0 if their values are equivalent,
  2636. * or > 0 if this substring is ordered after the substring of
  2637. * @a __str. Determines the effective length rlen of the
  2638. * strings to compare as the smallest of the lengths of the
  2639. * substrings. The function then compares the two strings by
  2640. * calling
  2641. * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
  2642. * If the result of the comparison is nonzero returns it,
  2643. * otherwise the shorter one is ordered first.
  2644. */
  2645. int
  2646. compare(size_type __pos1, size_type __n1, const basic_string& __str,
  2647. size_type __pos2, size_type __n2 = npos) const;
  2648. /**
  2649. * @brief Compare to a C string.
  2650. * @param __s C string to compare against.
  2651. * @return Integer < 0, 0, or > 0.
  2652. *
  2653. * Returns an integer < 0 if this string is ordered before @a __s, 0 if
  2654. * their values are equivalent, or > 0 if this string is ordered after
  2655. * @a __s. Determines the effective length rlen of the strings to
  2656. * compare as the smallest of size() and the length of a string
  2657. * constructed from @a __s. The function then compares the two strings
  2658. * by calling traits::compare(data(),s,rlen). If the result of the
  2659. * comparison is nonzero returns it, otherwise the shorter one is
  2660. * ordered first.
  2661. */
  2662. int
  2663. compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT;
  2664. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  2665. // 5 String::compare specification questionable
  2666. /**
  2667. * @brief Compare substring to a C string.
  2668. * @param __pos Index of first character of substring.
  2669. * @param __n1 Number of characters in substring.
  2670. * @param __s C string to compare against.
  2671. * @return Integer < 0, 0, or > 0.
  2672. *
  2673. * Form the substring of this string from the @a __n1
  2674. * characters starting at @a pos. Returns an integer < 0 if
  2675. * the substring is ordered before @a __s, 0 if their values
  2676. * are equivalent, or > 0 if the substring is ordered after @a
  2677. * __s. Determines the effective length rlen of the strings to
  2678. * compare as the smallest of the length of the substring and
  2679. * the length of a string constructed from @a __s. The
  2680. * function then compares the two string by calling
  2681. * traits::compare(substring.data(),__s,rlen). If the result of
  2682. * the comparison is nonzero returns it, otherwise the shorter
  2683. * one is ordered first.
  2684. */
  2685. int
  2686. compare(size_type __pos, size_type __n1, const _CharT* __s) const;
  2687. /**
  2688. * @brief Compare substring against a character %array.
  2689. * @param __pos Index of first character of substring.
  2690. * @param __n1 Number of characters in substring.
  2691. * @param __s character %array to compare against.
  2692. * @param __n2 Number of characters of s.
  2693. * @return Integer < 0, 0, or > 0.
  2694. *
  2695. * Form the substring of this string from the @a __n1
  2696. * characters starting at @a __pos. Form a string from the
  2697. * first @a __n2 characters of @a __s. Returns an integer < 0
  2698. * if this substring is ordered before the string from @a __s,
  2699. * 0 if their values are equivalent, or > 0 if this substring
  2700. * is ordered after the string from @a __s. Determines the
  2701. * effective length rlen of the strings to compare as the
  2702. * smallest of the length of the substring and @a __n2. The
  2703. * function then compares the two strings by calling
  2704. * traits::compare(substring.data(),s,rlen). If the result of
  2705. * the comparison is nonzero returns it, otherwise the shorter
  2706. * one is ordered first.
  2707. *
  2708. * NB: s must have at least n2 characters, &apos;\\0&apos; has
  2709. * no special meaning.
  2710. */
  2711. int
  2712. compare(size_type __pos, size_type __n1, const _CharT* __s,
  2713. size_type __n2) const;
  2714. #if __cplusplus > 201703L
  2715. bool
  2716. starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept
  2717. { return __sv_type(this->data(), this->size()).starts_with(__x); }
  2718. bool
  2719. starts_with(_CharT __x) const noexcept
  2720. { return __sv_type(this->data(), this->size()).starts_with(__x); }
  2721. bool
  2722. starts_with(const _CharT* __x) const noexcept
  2723. { return __sv_type(this->data(), this->size()).starts_with(__x); }
  2724. bool
  2725. ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept
  2726. { return __sv_type(this->data(), this->size()).ends_with(__x); }
  2727. bool
  2728. ends_with(_CharT __x) const noexcept
  2729. { return __sv_type(this->data(), this->size()).ends_with(__x); }
  2730. bool
  2731. ends_with(const _CharT* __x) const noexcept
  2732. { return __sv_type(this->data(), this->size()).ends_with(__x); }
  2733. #endif // C++20
  2734. #if __cplusplus > 202011L
  2735. bool
  2736. contains(basic_string_view<_CharT, _Traits> __x) const noexcept
  2737. { return __sv_type(this->data(), this->size()).contains(__x); }
  2738. bool
  2739. contains(_CharT __x) const noexcept
  2740. { return __sv_type(this->data(), this->size()).contains(__x); }
  2741. bool
  2742. contains(const _CharT* __x) const noexcept
  2743. { return __sv_type(this->data(), this->size()).contains(__x); }
  2744. #endif // C++23
  2745. # ifdef _GLIBCXX_TM_TS_INTERNAL
  2746. friend void
  2747. ::_txnal_cow_string_C1_for_exceptions(void* that, const char* s,
  2748. void* exc);
  2749. friend const char*
  2750. ::_txnal_cow_string_c_str(const void *that);
  2751. friend void
  2752. ::_txnal_cow_string_D1(void *that);
  2753. friend void
  2754. ::_txnal_cow_string_D1_commit(void *that);
  2755. # endif
  2756. };
  2757. template<typename _CharT, typename _Traits, typename _Alloc>
  2758. const typename basic_string<_CharT, _Traits, _Alloc>::size_type
  2759. basic_string<_CharT, _Traits, _Alloc>::
  2760. _Rep::_S_max_size = (((npos - sizeof(_Rep_base))/sizeof(_CharT)) - 1) / 4;
  2761. template<typename _CharT, typename _Traits, typename _Alloc>
  2762. const _CharT
  2763. basic_string<_CharT, _Traits, _Alloc>::
  2764. _Rep::_S_terminal = _CharT();
  2765. template<typename _CharT, typename _Traits, typename _Alloc>
  2766. const typename basic_string<_CharT, _Traits, _Alloc>::size_type
  2767. basic_string<_CharT, _Traits, _Alloc>::npos;
  2768. // Linker sets _S_empty_rep_storage to all 0s (one reference, empty string)
  2769. // at static init time (before static ctors are run).
  2770. template<typename _CharT, typename _Traits, typename _Alloc>
  2771. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  2772. basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_empty_rep_storage[
  2773. (sizeof(_Rep_base) + sizeof(_CharT) + sizeof(size_type) - 1) /
  2774. sizeof(size_type)];
  2775. // NB: This is the special case for Input Iterators, used in
  2776. // istreambuf_iterators, etc.
  2777. // Input Iterators have a cost structure very different from
  2778. // pointers, calling for a different coding style.
  2779. template<typename _CharT, typename _Traits, typename _Alloc>
  2780. template<typename _InIterator>
  2781. _CharT*
  2782. basic_string<_CharT, _Traits, _Alloc>::
  2783. _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
  2784. input_iterator_tag)
  2785. {
  2786. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  2787. if (__beg == __end && __a == _Alloc())
  2788. return _S_empty_rep()._M_refdata();
  2789. #endif
  2790. // Avoid reallocation for common case.
  2791. _CharT __buf[128];
  2792. size_type __len = 0;
  2793. while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT))
  2794. {
  2795. __buf[__len++] = *__beg;
  2796. ++__beg;
  2797. }
  2798. _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
  2799. _M_copy(__r->_M_refdata(), __buf, __len);
  2800. __try
  2801. {
  2802. while (__beg != __end)
  2803. {
  2804. if (__len == __r->_M_capacity)
  2805. {
  2806. // Allocate more space.
  2807. _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
  2808. _M_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
  2809. __r->_M_destroy(__a);
  2810. __r = __another;
  2811. }
  2812. __r->_M_refdata()[__len++] = *__beg;
  2813. ++__beg;
  2814. }
  2815. }
  2816. __catch(...)
  2817. {
  2818. __r->_M_destroy(__a);
  2819. __throw_exception_again;
  2820. }
  2821. __r->_M_set_length_and_sharable(__len);
  2822. return __r->_M_refdata();
  2823. }
  2824. template<typename _CharT, typename _Traits, typename _Alloc>
  2825. template <typename _InIterator>
  2826. _CharT*
  2827. basic_string<_CharT, _Traits, _Alloc>::
  2828. _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
  2829. forward_iterator_tag)
  2830. {
  2831. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  2832. if (__beg == __end && __a == _Alloc())
  2833. return _S_empty_rep()._M_refdata();
  2834. #endif
  2835. // NB: Not required, but considered best practice.
  2836. if (__gnu_cxx::__is_null_pointer(__beg) && __beg != __end)
  2837. __throw_logic_error(__N("basic_string::_S_construct null not valid"));
  2838. const size_type __dnew = static_cast<size_type>(std::distance(__beg,
  2839. __end));
  2840. // Check for out_of_range and length_error exceptions.
  2841. _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
  2842. __try
  2843. { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
  2844. __catch(...)
  2845. {
  2846. __r->_M_destroy(__a);
  2847. __throw_exception_again;
  2848. }
  2849. __r->_M_set_length_and_sharable(__dnew);
  2850. return __r->_M_refdata();
  2851. }
  2852. template<typename _CharT, typename _Traits, typename _Alloc>
  2853. _CharT*
  2854. basic_string<_CharT, _Traits, _Alloc>::
  2855. _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
  2856. {
  2857. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  2858. if (__n == 0 && __a == _Alloc())
  2859. return _S_empty_rep()._M_refdata();
  2860. #endif
  2861. // Check for out_of_range and length_error exceptions.
  2862. _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
  2863. if (__n)
  2864. _M_assign(__r->_M_refdata(), __n, __c);
  2865. __r->_M_set_length_and_sharable(__n);
  2866. return __r->_M_refdata();
  2867. }
  2868. template<typename _CharT, typename _Traits, typename _Alloc>
  2869. basic_string<_CharT, _Traits, _Alloc>::
  2870. basic_string(const basic_string& __str, size_type __pos, const _Alloc& __a)
  2871. : _M_dataplus(_S_construct(__str._M_data()
  2872. + __str._M_check(__pos,
  2873. "basic_string::basic_string"),
  2874. __str._M_data() + __str._M_limit(__pos, npos)
  2875. + __pos, __a), __a)
  2876. { }
  2877. template<typename _CharT, typename _Traits, typename _Alloc>
  2878. basic_string<_CharT, _Traits, _Alloc>::
  2879. basic_string(const basic_string& __str, size_type __pos, size_type __n)
  2880. : _M_dataplus(_S_construct(__str._M_data()
  2881. + __str._M_check(__pos,
  2882. "basic_string::basic_string"),
  2883. __str._M_data() + __str._M_limit(__pos, __n)
  2884. + __pos, _Alloc()), _Alloc())
  2885. { }
  2886. template<typename _CharT, typename _Traits, typename _Alloc>
  2887. basic_string<_CharT, _Traits, _Alloc>::
  2888. basic_string(const basic_string& __str, size_type __pos,
  2889. size_type __n, const _Alloc& __a)
  2890. : _M_dataplus(_S_construct(__str._M_data()
  2891. + __str._M_check(__pos,
  2892. "basic_string::basic_string"),
  2893. __str._M_data() + __str._M_limit(__pos, __n)
  2894. + __pos, __a), __a)
  2895. { }
  2896. template<typename _CharT, typename _Traits, typename _Alloc>
  2897. basic_string<_CharT, _Traits, _Alloc>&
  2898. basic_string<_CharT, _Traits, _Alloc>::
  2899. assign(const basic_string& __str)
  2900. {
  2901. if (_M_rep() != __str._M_rep())
  2902. {
  2903. // XXX MT
  2904. const allocator_type __a = this->get_allocator();
  2905. _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());
  2906. _M_rep()->_M_dispose(__a);
  2907. _M_data(__tmp);
  2908. }
  2909. return *this;
  2910. }
  2911. template<typename _CharT, typename _Traits, typename _Alloc>
  2912. basic_string<_CharT, _Traits, _Alloc>&
  2913. basic_string<_CharT, _Traits, _Alloc>::
  2914. assign(const _CharT* __s, size_type __n)
  2915. {
  2916. __glibcxx_requires_string_len(__s, __n);
  2917. _M_check_length(this->size(), __n, "basic_string::assign");
  2918. if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
  2919. return _M_replace_safe(size_type(0), this->size(), __s, __n);
  2920. else
  2921. {
  2922. // Work in-place.
  2923. const size_type __pos = __s - _M_data();
  2924. if (__pos >= __n)
  2925. _M_copy(_M_data(), __s, __n);
  2926. else if (__pos)
  2927. _M_move(_M_data(), __s, __n);
  2928. _M_rep()->_M_set_length_and_sharable(__n);
  2929. return *this;
  2930. }
  2931. }
  2932. template<typename _CharT, typename _Traits, typename _Alloc>
  2933. basic_string<_CharT, _Traits, _Alloc>&
  2934. basic_string<_CharT, _Traits, _Alloc>::
  2935. append(size_type __n, _CharT __c)
  2936. {
  2937. if (__n)
  2938. {
  2939. _M_check_length(size_type(0), __n, "basic_string::append");
  2940. const size_type __len = __n + this->size();
  2941. if (__len > this->capacity() || _M_rep()->_M_is_shared())
  2942. this->reserve(__len);
  2943. _M_assign(_M_data() + this->size(), __n, __c);
  2944. _M_rep()->_M_set_length_and_sharable(__len);
  2945. }
  2946. return *this;
  2947. }
  2948. template<typename _CharT, typename _Traits, typename _Alloc>
  2949. basic_string<_CharT, _Traits, _Alloc>&
  2950. basic_string<_CharT, _Traits, _Alloc>::
  2951. append(const _CharT* __s, size_type __n)
  2952. {
  2953. __glibcxx_requires_string_len(__s, __n);
  2954. if (__n)
  2955. {
  2956. _M_check_length(size_type(0), __n, "basic_string::append");
  2957. const size_type __len = __n + this->size();
  2958. if (__len > this->capacity() || _M_rep()->_M_is_shared())
  2959. {
  2960. if (_M_disjunct(__s))
  2961. this->reserve(__len);
  2962. else
  2963. {
  2964. const size_type __off = __s - _M_data();
  2965. this->reserve(__len);
  2966. __s = _M_data() + __off;
  2967. }
  2968. }
  2969. _M_copy(_M_data() + this->size(), __s, __n);
  2970. _M_rep()->_M_set_length_and_sharable(__len);
  2971. }
  2972. return *this;
  2973. }
  2974. template<typename _CharT, typename _Traits, typename _Alloc>
  2975. basic_string<_CharT, _Traits, _Alloc>&
  2976. basic_string<_CharT, _Traits, _Alloc>::
  2977. append(const basic_string& __str)
  2978. {
  2979. const size_type __size = __str.size();
  2980. if (__size)
  2981. {
  2982. const size_type __len = __size + this->size();
  2983. if (__len > this->capacity() || _M_rep()->_M_is_shared())
  2984. this->reserve(__len);
  2985. _M_copy(_M_data() + this->size(), __str._M_data(), __size);
  2986. _M_rep()->_M_set_length_and_sharable(__len);
  2987. }
  2988. return *this;
  2989. }
  2990. template<typename _CharT, typename _Traits, typename _Alloc>
  2991. basic_string<_CharT, _Traits, _Alloc>&
  2992. basic_string<_CharT, _Traits, _Alloc>::
  2993. append(const basic_string& __str, size_type __pos, size_type __n)
  2994. {
  2995. __str._M_check(__pos, "basic_string::append");
  2996. __n = __str._M_limit(__pos, __n);
  2997. if (__n)
  2998. {
  2999. const size_type __len = __n + this->size();
  3000. if (__len > this->capacity() || _M_rep()->_M_is_shared())
  3001. this->reserve(__len);
  3002. _M_copy(_M_data() + this->size(), __str._M_data() + __pos, __n);
  3003. _M_rep()->_M_set_length_and_sharable(__len);
  3004. }
  3005. return *this;
  3006. }
  3007. template<typename _CharT, typename _Traits, typename _Alloc>
  3008. basic_string<_CharT, _Traits, _Alloc>&
  3009. basic_string<_CharT, _Traits, _Alloc>::
  3010. insert(size_type __pos, const _CharT* __s, size_type __n)
  3011. {
  3012. __glibcxx_requires_string_len(__s, __n);
  3013. _M_check(__pos, "basic_string::insert");
  3014. _M_check_length(size_type(0), __n, "basic_string::insert");
  3015. if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
  3016. return _M_replace_safe(__pos, size_type(0), __s, __n);
  3017. else
  3018. {
  3019. // Work in-place.
  3020. const size_type __off = __s - _M_data();
  3021. _M_mutate(__pos, 0, __n);
  3022. __s = _M_data() + __off;
  3023. _CharT* __p = _M_data() + __pos;
  3024. if (__s + __n <= __p)
  3025. _M_copy(__p, __s, __n);
  3026. else if (__s >= __p)
  3027. _M_copy(__p, __s + __n, __n);
  3028. else
  3029. {
  3030. const size_type __nleft = __p - __s;
  3031. _M_copy(__p, __s, __nleft);
  3032. _M_copy(__p + __nleft, __p + __n, __n - __nleft);
  3033. }
  3034. return *this;
  3035. }
  3036. }
  3037. template<typename _CharT, typename _Traits, typename _Alloc>
  3038. typename basic_string<_CharT, _Traits, _Alloc>::iterator
  3039. basic_string<_CharT, _Traits, _Alloc>::
  3040. erase(iterator __first, iterator __last)
  3041. {
  3042. _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
  3043. && __last <= _M_iend());
  3044. // NB: This isn't just an optimization (bail out early when
  3045. // there is nothing to do, really), it's also a correctness
  3046. // issue vs MT, see libstdc++/40518.
  3047. const size_type __size = __last - __first;
  3048. if (__size)
  3049. {
  3050. const size_type __pos = __first - _M_ibegin();
  3051. _M_mutate(__pos, __size, size_type(0));
  3052. _M_rep()->_M_set_leaked();
  3053. return iterator(_M_data() + __pos);
  3054. }
  3055. else
  3056. return __first;
  3057. }
  3058. template<typename _CharT, typename _Traits, typename _Alloc>
  3059. basic_string<_CharT, _Traits, _Alloc>&
  3060. basic_string<_CharT, _Traits, _Alloc>::
  3061. replace(size_type __pos, size_type __n1, const _CharT* __s,
  3062. size_type __n2)
  3063. {
  3064. __glibcxx_requires_string_len(__s, __n2);
  3065. _M_check(__pos, "basic_string::replace");
  3066. __n1 = _M_limit(__pos, __n1);
  3067. _M_check_length(__n1, __n2, "basic_string::replace");
  3068. bool __left;
  3069. if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
  3070. return _M_replace_safe(__pos, __n1, __s, __n2);
  3071. else if ((__left = __s + __n2 <= _M_data() + __pos)
  3072. || _M_data() + __pos + __n1 <= __s)
  3073. {
  3074. // Work in-place: non-overlapping case.
  3075. size_type __off = __s - _M_data();
  3076. __left ? __off : (__off += __n2 - __n1);
  3077. _M_mutate(__pos, __n1, __n2);
  3078. _M_copy(_M_data() + __pos, _M_data() + __off, __n2);
  3079. return *this;
  3080. }
  3081. else
  3082. {
  3083. // Todo: overlapping case.
  3084. const basic_string __tmp(__s, __n2);
  3085. return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2);
  3086. }
  3087. }
  3088. template<typename _CharT, typename _Traits, typename _Alloc>
  3089. void
  3090. basic_string<_CharT, _Traits, _Alloc>::_Rep::
  3091. _M_destroy(const _Alloc& __a) throw ()
  3092. {
  3093. const size_type __size = sizeof(_Rep_base)
  3094. + (this->_M_capacity + 1) * sizeof(_CharT);
  3095. _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);
  3096. }
  3097. template<typename _CharT, typename _Traits, typename _Alloc>
  3098. void
  3099. basic_string<_CharT, _Traits, _Alloc>::
  3100. _M_leak_hard()
  3101. {
  3102. // No need to create a new copy of an empty string when a non-const
  3103. // reference/pointer/iterator into it is obtained. Modifying the
  3104. // trailing null character is undefined, so the ref/pointer/iterator
  3105. // is effectively const anyway.
  3106. if (this->empty())
  3107. return;
  3108. if (_M_rep()->_M_is_shared())
  3109. _M_mutate(0, 0, 0);
  3110. _M_rep()->_M_set_leaked();
  3111. }
  3112. template<typename _CharT, typename _Traits, typename _Alloc>
  3113. void
  3114. basic_string<_CharT, _Traits, _Alloc>::
  3115. _M_mutate(size_type __pos, size_type __len1, size_type __len2)
  3116. {
  3117. const size_type __old_size = this->size();
  3118. const size_type __new_size = __old_size + __len2 - __len1;
  3119. const size_type __how_much = __old_size - __pos - __len1;
  3120. if (__new_size > this->capacity() || _M_rep()->_M_is_shared())
  3121. {
  3122. // Must reallocate.
  3123. const allocator_type __a = get_allocator();
  3124. _Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a);
  3125. if (__pos)
  3126. _M_copy(__r->_M_refdata(), _M_data(), __pos);
  3127. if (__how_much)
  3128. _M_copy(__r->_M_refdata() + __pos + __len2,
  3129. _M_data() + __pos + __len1, __how_much);
  3130. _M_rep()->_M_dispose(__a);
  3131. _M_data(__r->_M_refdata());
  3132. }
  3133. else if (__how_much && __len1 != __len2)
  3134. {
  3135. // Work in-place.
  3136. _M_move(_M_data() + __pos + __len2,
  3137. _M_data() + __pos + __len1, __how_much);
  3138. }
  3139. _M_rep()->_M_set_length_and_sharable(__new_size);
  3140. }
  3141. template<typename _CharT, typename _Traits, typename _Alloc>
  3142. void
  3143. basic_string<_CharT, _Traits, _Alloc>::
  3144. reserve(size_type __res)
  3145. {
  3146. const size_type __capacity = capacity();
  3147. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  3148. // 2968. Inconsistencies between basic_string reserve and
  3149. // vector/unordered_map/unordered_set reserve functions
  3150. // P0966 reserve should not shrink
  3151. if (__res <= __capacity)
  3152. {
  3153. if (!_M_rep()->_M_is_shared())
  3154. return;
  3155. // unshare, but keep same capacity
  3156. __res = __capacity;
  3157. }
  3158. const allocator_type __a = get_allocator();
  3159. _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size());
  3160. _M_rep()->_M_dispose(__a);
  3161. _M_data(__tmp);
  3162. }
  3163. template<typename _CharT, typename _Traits, typename _Alloc>
  3164. void
  3165. basic_string<_CharT, _Traits, _Alloc>::
  3166. swap(basic_string& __s)
  3167. _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value)
  3168. {
  3169. if (_M_rep()->_M_is_leaked())
  3170. _M_rep()->_M_set_sharable();
  3171. if (__s._M_rep()->_M_is_leaked())
  3172. __s._M_rep()->_M_set_sharable();
  3173. if (this->get_allocator() == __s.get_allocator())
  3174. {
  3175. _CharT* __tmp = _M_data();
  3176. _M_data(__s._M_data());
  3177. __s._M_data(__tmp);
  3178. }
  3179. // The code below can usually be optimized away.
  3180. else
  3181. {
  3182. const basic_string __tmp1(_M_ibegin(), _M_iend(),
  3183. __s.get_allocator());
  3184. const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
  3185. this->get_allocator());
  3186. *this = __tmp2;
  3187. __s = __tmp1;
  3188. }
  3189. }
  3190. template<typename _CharT, typename _Traits, typename _Alloc>
  3191. typename basic_string<_CharT, _Traits, _Alloc>::_Rep*
  3192. basic_string<_CharT, _Traits, _Alloc>::_Rep::
  3193. _S_create(size_type __capacity, size_type __old_capacity,
  3194. const _Alloc& __alloc)
  3195. {
  3196. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  3197. // 83. String::npos vs. string::max_size()
  3198. if (__capacity > _S_max_size)
  3199. __throw_length_error(__N("basic_string::_S_create"));
  3200. // The standard places no restriction on allocating more memory
  3201. // than is strictly needed within this layer at the moment or as
  3202. // requested by an explicit application call to reserve(n).
  3203. // Many malloc implementations perform quite poorly when an
  3204. // application attempts to allocate memory in a stepwise fashion
  3205. // growing each allocation size by only 1 char. Additionally,
  3206. // it makes little sense to allocate less linear memory than the
  3207. // natural blocking size of the malloc implementation.
  3208. // Unfortunately, we would need a somewhat low-level calculation
  3209. // with tuned parameters to get this perfect for any particular
  3210. // malloc implementation. Fortunately, generalizations about
  3211. // common features seen among implementations seems to suffice.
  3212. // __pagesize need not match the actual VM page size for good
  3213. // results in practice, thus we pick a common value on the low
  3214. // side. __malloc_header_size is an estimate of the amount of
  3215. // overhead per memory allocation (in practice seen N * sizeof
  3216. // (void*) where N is 0, 2 or 4). According to folklore,
  3217. // picking this value on the high side is better than
  3218. // low-balling it (especially when this algorithm is used with
  3219. // malloc implementations that allocate memory blocks rounded up
  3220. // to a size which is a power of 2).
  3221. const size_type __pagesize = 4096;
  3222. const size_type __malloc_header_size = 4 * sizeof(void*);
  3223. // The below implements an exponential growth policy, necessary to
  3224. // meet amortized linear time requirements of the library: see
  3225. // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
  3226. // It's active for allocations requiring an amount of memory above
  3227. // system pagesize. This is consistent with the requirements of the
  3228. // standard: http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html
  3229. if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
  3230. __capacity = 2 * __old_capacity;
  3231. // NB: Need an array of char_type[__capacity], plus a terminating
  3232. // null char_type() element, plus enough for the _Rep data structure.
  3233. // Whew. Seemingly so needy, yet so elemental.
  3234. size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
  3235. const size_type __adj_size = __size + __malloc_header_size;
  3236. if (__adj_size > __pagesize && __capacity > __old_capacity)
  3237. {
  3238. const size_type __extra = __pagesize - __adj_size % __pagesize;
  3239. __capacity += __extra / sizeof(_CharT);
  3240. // Never allocate a string bigger than _S_max_size.
  3241. if (__capacity > _S_max_size)
  3242. __capacity = _S_max_size;
  3243. __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
  3244. }
  3245. // NB: Might throw, but no worries about a leak, mate: _Rep()
  3246. // does not throw.
  3247. void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
  3248. _Rep *__p = new (__place) _Rep;
  3249. __p->_M_capacity = __capacity;
  3250. // ABI compatibility - 3.4.x set in _S_create both
  3251. // _M_refcount and _M_length. All callers of _S_create
  3252. // in basic_string.tcc then set just _M_length.
  3253. // In 4.0.x and later both _M_refcount and _M_length
  3254. // are initialized in the callers, unfortunately we can
  3255. // have 3.4.x compiled code with _S_create callers inlined
  3256. // calling 4.0.x+ _S_create.
  3257. __p->_M_set_sharable();
  3258. return __p;
  3259. }
  3260. template<typename _CharT, typename _Traits, typename _Alloc>
  3261. _CharT*
  3262. basic_string<_CharT, _Traits, _Alloc>::_Rep::
  3263. _M_clone(const _Alloc& __alloc, size_type __res)
  3264. {
  3265. // Requested capacity of the clone.
  3266. const size_type __requested_cap = this->_M_length + __res;
  3267. _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity,
  3268. __alloc);
  3269. if (this->_M_length)
  3270. _M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length);
  3271. __r->_M_set_length_and_sharable(this->_M_length);
  3272. return __r->_M_refdata();
  3273. }
  3274. template<typename _CharT, typename _Traits, typename _Alloc>
  3275. void
  3276. basic_string<_CharT, _Traits, _Alloc>::
  3277. resize(size_type __n, _CharT __c)
  3278. {
  3279. const size_type __size = this->size();
  3280. _M_check_length(__size, __n, "basic_string::resize");
  3281. if (__size < __n)
  3282. this->append(__n - __size, __c);
  3283. else if (__n < __size)
  3284. this->erase(__n);
  3285. // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
  3286. }
  3287. template<typename _CharT, typename _Traits, typename _Alloc>
  3288. template<typename _InputIterator>
  3289. basic_string<_CharT, _Traits, _Alloc>&
  3290. basic_string<_CharT, _Traits, _Alloc>::
  3291. _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
  3292. _InputIterator __k2, __false_type)
  3293. {
  3294. const basic_string __s(__k1, __k2);
  3295. const size_type __n1 = __i2 - __i1;
  3296. _M_check_length(__n1, __s.size(), "basic_string::_M_replace_dispatch");
  3297. return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
  3298. __s.size());
  3299. }
  3300. template<typename _CharT, typename _Traits, typename _Alloc>
  3301. basic_string<_CharT, _Traits, _Alloc>&
  3302. basic_string<_CharT, _Traits, _Alloc>::
  3303. _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
  3304. _CharT __c)
  3305. {
  3306. _M_check_length(__n1, __n2, "basic_string::_M_replace_aux");
  3307. _M_mutate(__pos1, __n1, __n2);
  3308. if (__n2)
  3309. _M_assign(_M_data() + __pos1, __n2, __c);
  3310. return *this;
  3311. }
  3312. template<typename _CharT, typename _Traits, typename _Alloc>
  3313. basic_string<_CharT, _Traits, _Alloc>&
  3314. basic_string<_CharT, _Traits, _Alloc>::
  3315. _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
  3316. size_type __n2)
  3317. {
  3318. _M_mutate(__pos1, __n1, __n2);
  3319. if (__n2)
  3320. _M_copy(_M_data() + __pos1, __s, __n2);
  3321. return *this;
  3322. }
  3323. template<typename _CharT, typename _Traits, typename _Alloc>
  3324. void
  3325. basic_string<_CharT, _Traits, _Alloc>::
  3326. reserve()
  3327. {
  3328. #if __cpp_exceptions
  3329. if (length() < capacity() || _M_rep()->_M_is_shared())
  3330. try
  3331. {
  3332. const allocator_type __a = get_allocator();
  3333. _CharT* __tmp = _M_rep()->_M_clone(__a);
  3334. _M_rep()->_M_dispose(__a);
  3335. _M_data(__tmp);
  3336. }
  3337. catch (const __cxxabiv1::__forced_unwind&)
  3338. { throw; }
  3339. catch (...)
  3340. { /* swallow the exception */ }
  3341. #endif
  3342. }
  3343. template<typename _CharT, typename _Traits, typename _Alloc>
  3344. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  3345. basic_string<_CharT, _Traits, _Alloc>::
  3346. copy(_CharT* __s, size_type __n, size_type __pos) const
  3347. {
  3348. _M_check(__pos, "basic_string::copy");
  3349. __n = _M_limit(__pos, __n);
  3350. __glibcxx_requires_string_len(__s, __n);
  3351. if (__n)
  3352. _M_copy(__s, _M_data() + __pos, __n);
  3353. // 21.3.5.7 par 3: do not append null. (good.)
  3354. return __n;
  3355. }
  3356. _GLIBCXX_END_NAMESPACE_VERSION
  3357. } // namespace std
  3358. #endif // ! _GLIBCXX_USE_CXX11_ABI
  3359. #endif // _COW_STRING_H