regex.h 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026
  1. // class template regex -*- C++ -*-
  2. // Copyright (C) 2010-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. /**
  21. * @file bits/regex.h
  22. * This is an internal header file, included by other library headers.
  23. * Do not attempt to use it directly. @headername{regex}
  24. */
  25. namespace std _GLIBCXX_VISIBILITY(default)
  26. {
  27. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  28. _GLIBCXX_BEGIN_NAMESPACE_CXX11
  29. template<typename, typename>
  30. class basic_regex;
  31. template<typename _Bi_iter, typename _Alloc>
  32. class match_results;
  33. _GLIBCXX_END_NAMESPACE_CXX11
  34. namespace __detail
  35. {
  36. enum class _RegexExecutorPolicy : int { _S_auto, _S_alternate };
  37. template<typename _BiIter, typename _Alloc,
  38. typename _CharT, typename _TraitsT>
  39. bool
  40. __regex_algo_impl(_BiIter __s, _BiIter __e,
  41. match_results<_BiIter, _Alloc>& __m,
  42. const basic_regex<_CharT, _TraitsT>& __re,
  43. regex_constants::match_flag_type __flags,
  44. _RegexExecutorPolicy __policy,
  45. bool __match_mode);
  46. template<typename, typename, typename, bool>
  47. class _Executor;
  48. template<typename _Tp>
  49. struct __is_contiguous_iter : false_type { };
  50. template<typename _Tp>
  51. struct __is_contiguous_iter<_Tp*> : true_type { };
  52. template<typename _Tp, typename _Cont>
  53. struct __is_contiguous_iter<__gnu_cxx::__normal_iterator<_Tp*, _Cont>>
  54. : true_type { };
  55. }
  56. _GLIBCXX_BEGIN_NAMESPACE_CXX11
  57. /**
  58. * @addtogroup regex
  59. * @{
  60. */
  61. /**
  62. * @brief Describes aspects of a regular expression.
  63. *
  64. * A regular expression traits class that satisfies the requirements of
  65. * section [28.7].
  66. *
  67. * The class %regex is parameterized around a set of related types and
  68. * functions used to complete the definition of its semantics. This class
  69. * satisfies the requirements of such a traits class.
  70. */
  71. template<typename _Ch_type>
  72. class regex_traits
  73. {
  74. public:
  75. typedef _Ch_type char_type;
  76. typedef std::basic_string<char_type> string_type;
  77. typedef std::locale locale_type;
  78. private:
  79. struct _RegexMask
  80. {
  81. typedef std::ctype_base::mask _BaseType;
  82. _BaseType _M_base;
  83. unsigned char _M_extended;
  84. static constexpr unsigned char _S_under = 1 << 0;
  85. static constexpr unsigned char _S_valid_mask = 0x1;
  86. constexpr _RegexMask(_BaseType __base = 0,
  87. unsigned char __extended = 0)
  88. : _M_base(__base), _M_extended(__extended)
  89. { }
  90. constexpr _RegexMask
  91. operator&(_RegexMask __other) const
  92. {
  93. return _RegexMask(_M_base & __other._M_base,
  94. _M_extended & __other._M_extended);
  95. }
  96. constexpr _RegexMask
  97. operator|(_RegexMask __other) const
  98. {
  99. return _RegexMask(_M_base | __other._M_base,
  100. _M_extended | __other._M_extended);
  101. }
  102. constexpr _RegexMask
  103. operator^(_RegexMask __other) const
  104. {
  105. return _RegexMask(_M_base ^ __other._M_base,
  106. _M_extended ^ __other._M_extended);
  107. }
  108. constexpr _RegexMask
  109. operator~() const
  110. { return _RegexMask(~_M_base, ~_M_extended); }
  111. _RegexMask&
  112. operator&=(_RegexMask __other)
  113. { return *this = (*this) & __other; }
  114. _RegexMask&
  115. operator|=(_RegexMask __other)
  116. { return *this = (*this) | __other; }
  117. _RegexMask&
  118. operator^=(_RegexMask __other)
  119. { return *this = (*this) ^ __other; }
  120. constexpr bool
  121. operator==(_RegexMask __other) const
  122. {
  123. return (_M_extended & _S_valid_mask)
  124. == (__other._M_extended & _S_valid_mask)
  125. && _M_base == __other._M_base;
  126. }
  127. #if __cpp_impl_three_way_comparison < 201907L
  128. constexpr bool
  129. operator!=(_RegexMask __other) const
  130. { return !((*this) == __other); }
  131. #endif
  132. };
  133. public:
  134. typedef _RegexMask char_class_type;
  135. public:
  136. /**
  137. * @brief Constructs a default traits object.
  138. */
  139. regex_traits() { }
  140. /**
  141. * @brief Gives the length of a C-style string starting at @p __p.
  142. *
  143. * @param __p a pointer to the start of a character sequence.
  144. *
  145. * @returns the number of characters between @p *__p and the first
  146. * default-initialized value of type @p char_type. In other words, uses
  147. * the C-string algorithm for determining the length of a sequence of
  148. * characters.
  149. */
  150. static std::size_t
  151. length(const char_type* __p)
  152. { return string_type::traits_type::length(__p); }
  153. /**
  154. * @brief Performs the identity translation.
  155. *
  156. * @param __c A character to the locale-specific character set.
  157. *
  158. * @returns __c.
  159. */
  160. char_type
  161. translate(char_type __c) const
  162. { return __c; }
  163. /**
  164. * @brief Translates a character into a case-insensitive equivalent.
  165. *
  166. * @param __c A character to the locale-specific character set.
  167. *
  168. * @returns the locale-specific lower-case equivalent of __c.
  169. * @throws std::bad_cast if the imbued locale does not support the ctype
  170. * facet.
  171. */
  172. char_type
  173. translate_nocase(char_type __c) const
  174. {
  175. typedef std::ctype<char_type> __ctype_type;
  176. const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
  177. return __fctyp.tolower(__c);
  178. }
  179. /**
  180. * @brief Gets a sort key for a character sequence.
  181. *
  182. * @param __first beginning of the character sequence.
  183. * @param __last one-past-the-end of the character sequence.
  184. *
  185. * Returns a sort key for the character sequence designated by the
  186. * iterator range [F1, F2) such that if the character sequence [G1, G2)
  187. * sorts before the character sequence [H1, H2) then
  188. * v.transform(G1, G2) < v.transform(H1, H2).
  189. *
  190. * What this really does is provide a more efficient way to compare a
  191. * string to multiple other strings in locales with fancy collation
  192. * rules and equivalence classes.
  193. *
  194. * @returns a locale-specific sort key equivalent to the input range.
  195. *
  196. * @throws std::bad_cast if the current locale does not have a collate
  197. * facet.
  198. */
  199. template<typename _Fwd_iter>
  200. string_type
  201. transform(_Fwd_iter __first, _Fwd_iter __last) const
  202. {
  203. typedef std::collate<char_type> __collate_type;
  204. const __collate_type& __fclt(use_facet<__collate_type>(_M_locale));
  205. string_type __s(__first, __last);
  206. return __fclt.transform(__s.data(), __s.data() + __s.size());
  207. }
  208. /**
  209. * @brief Gets a sort key for a character sequence, independent of case.
  210. *
  211. * @param __first beginning of the character sequence.
  212. * @param __last one-past-the-end of the character sequence.
  213. *
  214. * Effects: if typeid(use_facet<collate<_Ch_type> >) ==
  215. * typeid(collate_byname<_Ch_type>) and the form of the sort key
  216. * returned by collate_byname<_Ch_type>::transform(__first, __last)
  217. * is known and can be converted into a primary sort key
  218. * then returns that key, otherwise returns an empty string.
  219. *
  220. * @todo Implement this function correctly.
  221. */
  222. template<typename _Fwd_iter>
  223. string_type
  224. transform_primary(_Fwd_iter __first, _Fwd_iter __last) const
  225. {
  226. // TODO : this is not entirely correct.
  227. // This function requires extra support from the platform.
  228. //
  229. // Read http://gcc.gnu.org/ml/libstdc++/2013-09/msg00117.html and
  230. // http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2003/n1429.htm
  231. // for details.
  232. typedef std::ctype<char_type> __ctype_type;
  233. const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
  234. _GLIBCXX_STD_C::vector<char_type> __s(__first, __last);
  235. __fctyp.tolower(__s.data(), __s.data() + __s.size());
  236. return this->transform(__s.data(), __s.data() + __s.size());
  237. }
  238. /**
  239. * @brief Gets a collation element by name.
  240. *
  241. * @param __first beginning of the collation element name.
  242. * @param __last one-past-the-end of the collation element name.
  243. *
  244. * @returns a sequence of one or more characters that represents the
  245. * collating element consisting of the character sequence designated by
  246. * the iterator range [__first, __last). Returns an empty string if the
  247. * character sequence is not a valid collating element.
  248. */
  249. template<typename _Fwd_iter>
  250. string_type
  251. lookup_collatename(_Fwd_iter __first, _Fwd_iter __last) const;
  252. /**
  253. * @brief Maps one or more characters to a named character
  254. * classification.
  255. *
  256. * @param __first beginning of the character sequence.
  257. * @param __last one-past-the-end of the character sequence.
  258. * @param __icase ignores the case of the classification name.
  259. *
  260. * @returns an unspecified value that represents the character
  261. * classification named by the character sequence designated by
  262. * the iterator range [__first, __last). If @p icase is true,
  263. * the returned mask identifies the classification regardless of
  264. * the case of the characters to be matched (for example,
  265. * [[:lower:]] is the same as [[:alpha:]]), otherwise a
  266. * case-dependent classification is returned. The value
  267. * returned shall be independent of the case of the characters
  268. * in the character sequence. If the name is not recognized then
  269. * returns a value that compares equal to 0.
  270. *
  271. * At least the following names (or their wide-character equivalent) are
  272. * supported.
  273. * - d
  274. * - w
  275. * - s
  276. * - alnum
  277. * - alpha
  278. * - blank
  279. * - cntrl
  280. * - digit
  281. * - graph
  282. * - lower
  283. * - print
  284. * - punct
  285. * - space
  286. * - upper
  287. * - xdigit
  288. */
  289. template<typename _Fwd_iter>
  290. char_class_type
  291. lookup_classname(_Fwd_iter __first, _Fwd_iter __last,
  292. bool __icase = false) const;
  293. /**
  294. * @brief Determines if @p c is a member of an identified class.
  295. *
  296. * @param __c a character.
  297. * @param __f a class type (as returned from lookup_classname).
  298. *
  299. * @returns true if the character @p __c is a member of the classification
  300. * represented by @p __f, false otherwise.
  301. *
  302. * @throws std::bad_cast if the current locale does not have a ctype
  303. * facet.
  304. */
  305. bool
  306. isctype(_Ch_type __c, char_class_type __f) const;
  307. /**
  308. * @brief Converts a digit to an int.
  309. *
  310. * @param __ch a character representing a digit.
  311. * @param __radix the radix if the numeric conversion (limited to 8, 10,
  312. * or 16).
  313. *
  314. * @returns the value represented by the digit __ch in base radix if the
  315. * character __ch is a valid digit in base radix; otherwise returns -1.
  316. */
  317. int
  318. value(_Ch_type __ch, int __radix) const;
  319. /**
  320. * @brief Imbues the regex_traits object with a copy of a new locale.
  321. *
  322. * @param __loc A locale.
  323. *
  324. * @returns a copy of the previous locale in use by the regex_traits
  325. * object.
  326. *
  327. * @note Calling imbue with a different locale than the one currently in
  328. * use invalidates all cached data held by *this.
  329. */
  330. locale_type
  331. imbue(locale_type __loc)
  332. {
  333. std::swap(_M_locale, __loc);
  334. return __loc;
  335. }
  336. /**
  337. * @brief Gets a copy of the current locale in use by the regex_traits
  338. * object.
  339. */
  340. locale_type
  341. getloc() const
  342. { return _M_locale; }
  343. protected:
  344. locale_type _M_locale;
  345. };
  346. // [7.8] Class basic_regex
  347. /**
  348. * Objects of specializations of this class represent regular expressions
  349. * constructed from sequences of character type @p _Ch_type.
  350. *
  351. * Storage for the regular expression is allocated and deallocated as
  352. * necessary by the member functions of this class.
  353. */
  354. template<typename _Ch_type, typename _Rx_traits = regex_traits<_Ch_type>>
  355. class basic_regex
  356. {
  357. public:
  358. static_assert(is_same<_Ch_type, typename _Rx_traits::char_type>::value,
  359. "regex traits class must have the same char_type");
  360. // types:
  361. typedef _Ch_type value_type;
  362. typedef _Rx_traits traits_type;
  363. typedef typename traits_type::string_type string_type;
  364. typedef regex_constants::syntax_option_type flag_type;
  365. typedef typename traits_type::locale_type locale_type;
  366. /**
  367. * @name Constants
  368. * std [28.8.1](1)
  369. */
  370. ///@{
  371. static constexpr flag_type icase = regex_constants::icase;
  372. static constexpr flag_type nosubs = regex_constants::nosubs;
  373. static constexpr flag_type optimize = regex_constants::optimize;
  374. static constexpr flag_type collate = regex_constants::collate;
  375. static constexpr flag_type ECMAScript = regex_constants::ECMAScript;
  376. static constexpr flag_type basic = regex_constants::basic;
  377. static constexpr flag_type extended = regex_constants::extended;
  378. static constexpr flag_type awk = regex_constants::awk;
  379. static constexpr flag_type grep = regex_constants::grep;
  380. static constexpr flag_type egrep = regex_constants::egrep;
  381. #if __cplusplus >= 201703L || !defined __STRICT_ANSI__
  382. static constexpr flag_type multiline = regex_constants::multiline;
  383. #endif
  384. ///@}
  385. // [7.8.2] construct/copy/destroy
  386. /**
  387. * Constructs a basic regular expression that does not match any
  388. * character sequence.
  389. */
  390. basic_regex() noexcept
  391. : _M_flags(ECMAScript), _M_loc(), _M_automaton(nullptr)
  392. { }
  393. /**
  394. * @brief Constructs a basic regular expression from the
  395. * sequence [__p, __p + char_traits<_Ch_type>::length(__p))
  396. * interpreted according to the flags in @p __f.
  397. *
  398. * @param __p A pointer to the start of a C-style null-terminated string
  399. * containing a regular expression.
  400. * @param __f Flags indicating the syntax rules and options.
  401. *
  402. * @throws regex_error if @p __p is not a valid regular expression.
  403. */
  404. explicit
  405. basic_regex(const _Ch_type* __p, flag_type __f = ECMAScript)
  406. { _M_compile(__p, __p + _Rx_traits::length(__p), __f); }
  407. /**
  408. * @brief Constructs a basic regular expression from the sequence
  409. * [p, p + len) interpreted according to the flags in @p f.
  410. *
  411. * @param __p A pointer to the start of a string containing a regular
  412. * expression.
  413. * @param __len The length of the string containing the regular
  414. * expression.
  415. * @param __f Flags indicating the syntax rules and options.
  416. *
  417. * @throws regex_error if @p __p is not a valid regular expression.
  418. */
  419. basic_regex(const _Ch_type* __p, std::size_t __len,
  420. flag_type __f = ECMAScript)
  421. {
  422. __glibcxx_requires_string_len(__p, __len);
  423. _M_compile(__p, __p + __len, __f);
  424. }
  425. /**
  426. * @brief Copy-constructs a basic regular expression.
  427. *
  428. * @param __rhs A @p regex object.
  429. */
  430. basic_regex(const basic_regex& __rhs) = default;
  431. /**
  432. * @brief Move-constructs a basic regular expression.
  433. *
  434. * @param __rhs A @p regex object.
  435. */
  436. basic_regex(basic_regex&& __rhs) noexcept = default;
  437. /**
  438. * @brief Constructs a basic regular expression from the string
  439. * @p s interpreted according to the flags in @p f.
  440. *
  441. * @param __s A string containing a regular expression.
  442. * @param __f Flags indicating the syntax rules and options.
  443. *
  444. * @throws regex_error if @p __s is not a valid regular expression.
  445. */
  446. template<typename _Ch_traits, typename _Ch_alloc>
  447. explicit
  448. basic_regex(const std::basic_string<_Ch_type, _Ch_traits,
  449. _Ch_alloc>& __s,
  450. flag_type __f = ECMAScript)
  451. { _M_compile(__s.data(), __s.data() + __s.size(), __f); }
  452. /**
  453. * @brief Constructs a basic regular expression from the range
  454. * [first, last) interpreted according to the flags in @p f.
  455. *
  456. * @param __first The start of a range containing a valid regular
  457. * expression.
  458. * @param __last The end of a range containing a valid regular
  459. * expression.
  460. * @param __f The format flags of the regular expression.
  461. *
  462. * @throws regex_error if @p [__first, __last) is not a valid regular
  463. * expression.
  464. */
  465. template<typename _FwdIter>
  466. basic_regex(_FwdIter __first, _FwdIter __last,
  467. flag_type __f = ECMAScript)
  468. { this->assign(__first, __last, __f); }
  469. /**
  470. * @brief Constructs a basic regular expression from an initializer list.
  471. *
  472. * @param __l The initializer list.
  473. * @param __f The format flags of the regular expression.
  474. *
  475. * @throws regex_error if @p __l is not a valid regular expression.
  476. */
  477. basic_regex(initializer_list<_Ch_type> __l, flag_type __f = ECMAScript)
  478. { _M_compile(__l.begin(), __l.end(), __f); }
  479. /**
  480. * @brief Destroys a basic regular expression.
  481. */
  482. ~basic_regex()
  483. { }
  484. /**
  485. * @brief Assigns one regular expression to another.
  486. */
  487. basic_regex&
  488. operator=(const basic_regex&) = default;
  489. /**
  490. * @brief Move-assigns one regular expression to another.
  491. */
  492. basic_regex&
  493. operator=(basic_regex&&) = default;
  494. /**
  495. * @brief Replaces a regular expression with a new one constructed from
  496. * a C-style null-terminated string.
  497. *
  498. * @param __p A pointer to the start of a null-terminated C-style string
  499. * containing a regular expression.
  500. */
  501. basic_regex&
  502. operator=(const _Ch_type* __p)
  503. { return this->assign(__p); }
  504. /**
  505. * @brief Replaces a regular expression with a new one constructed from
  506. * an initializer list.
  507. *
  508. * @param __l The initializer list.
  509. *
  510. * @throws regex_error if @p __l is not a valid regular expression.
  511. */
  512. basic_regex&
  513. operator=(initializer_list<_Ch_type> __l)
  514. { return this->assign(__l); }
  515. /**
  516. * @brief Replaces a regular expression with a new one constructed from
  517. * a string.
  518. *
  519. * @param __s A pointer to a string containing a regular expression.
  520. */
  521. template<typename _Ch_traits, typename _Alloc>
  522. basic_regex&
  523. operator=(const basic_string<_Ch_type, _Ch_traits, _Alloc>& __s)
  524. { return this->assign(__s); }
  525. // [7.8.3] assign
  526. /**
  527. * @brief Assigns one regular expression to another.
  528. *
  529. * @param __rhs Another regular expression object.
  530. */
  531. basic_regex&
  532. assign(const basic_regex& __rhs) noexcept
  533. { return *this = __rhs; }
  534. /**
  535. * @brief Move-assigns one regular expression to another.
  536. *
  537. * @param __rhs Another regular expression object.
  538. */
  539. basic_regex&
  540. assign(basic_regex&& __rhs) noexcept
  541. { return *this = std::move(__rhs); }
  542. /**
  543. * @brief Assigns a new regular expression to a regex object from a
  544. * C-style null-terminated string containing a regular expression
  545. * pattern.
  546. *
  547. * @param __p A pointer to a C-style null-terminated string containing
  548. * a regular expression pattern.
  549. * @param __flags Syntax option flags.
  550. *
  551. * @throws regex_error if __p does not contain a valid regular
  552. * expression pattern interpreted according to @p __flags. If
  553. * regex_error is thrown, *this remains unchanged.
  554. */
  555. basic_regex&
  556. assign(const _Ch_type* __p, flag_type __flags = ECMAScript)
  557. {
  558. _M_compile(__p, __p + _Rx_traits::length(__p), __flags);
  559. return *this;
  560. }
  561. /**
  562. * @brief Assigns a new regular expression to a regex object from a
  563. * C-style string containing a regular expression pattern.
  564. *
  565. * @param __p A pointer to a C-style string containing a
  566. * regular expression pattern.
  567. * @param __len The length of the regular expression pattern string.
  568. * @param __flags Syntax option flags.
  569. *
  570. * @throws regex_error if p does not contain a valid regular
  571. * expression pattern interpreted according to @p __flags. If
  572. * regex_error is thrown, *this remains unchanged.
  573. */
  574. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  575. // 3296. Inconsistent default argument for basic_regex<>::assign
  576. basic_regex&
  577. assign(const _Ch_type* __p, size_t __len, flag_type __flags = ECMAScript)
  578. {
  579. _M_compile(__p, __p + __len, __flags);
  580. return *this;
  581. }
  582. /**
  583. * @brief Assigns a new regular expression to a regex object from a
  584. * string containing a regular expression pattern.
  585. *
  586. * @param __s A string containing a regular expression pattern.
  587. * @param __flags Syntax option flags.
  588. *
  589. * @throws regex_error if __s does not contain a valid regular
  590. * expression pattern interpreted according to @p __flags. If
  591. * regex_error is thrown, *this remains unchanged.
  592. */
  593. template<typename _Ch_traits, typename _Alloc>
  594. basic_regex&
  595. assign(const basic_string<_Ch_type, _Ch_traits, _Alloc>& __s,
  596. flag_type __flags = ECMAScript)
  597. {
  598. _M_compile(__s.data(), __s.data() + __s.size(), __flags);
  599. return *this;
  600. }
  601. /**
  602. * @brief Assigns a new regular expression to a regex object.
  603. *
  604. * @param __first The start of a range containing a valid regular
  605. * expression.
  606. * @param __last The end of a range containing a valid regular
  607. * expression.
  608. * @param __flags Syntax option flags.
  609. *
  610. * @throws regex_error if p does not contain a valid regular
  611. * expression pattern interpreted according to @p __flags. If
  612. * regex_error is thrown, the object remains unchanged.
  613. */
  614. template<typename _InputIterator>
  615. basic_regex&
  616. assign(_InputIterator __first, _InputIterator __last,
  617. flag_type __flags = ECMAScript)
  618. {
  619. #if __cpp_if_constexpr >= 201606L
  620. using _ValT = typename iterator_traits<_InputIterator>::value_type;
  621. if constexpr (__detail::__is_contiguous_iter<_InputIterator>::value
  622. && is_same_v<_ValT, value_type>)
  623. {
  624. __glibcxx_requires_valid_range(__first, __last);
  625. if constexpr (is_pointer_v<_InputIterator>)
  626. _M_compile(__first, __last, __flags);
  627. else // __normal_iterator<_T*, C>
  628. _M_compile(__first.base(), __last.base(), __flags);
  629. }
  630. else
  631. #endif
  632. this->assign(string_type(__first, __last), __flags);
  633. return *this;
  634. }
  635. /**
  636. * @brief Assigns a new regular expression to a regex object.
  637. *
  638. * @param __l An initializer list representing a regular expression.
  639. * @param __flags Syntax option flags.
  640. *
  641. * @throws regex_error if @p __l does not contain a valid
  642. * regular expression pattern interpreted according to @p
  643. * __flags. If regex_error is thrown, the object remains
  644. * unchanged.
  645. */
  646. basic_regex&
  647. assign(initializer_list<_Ch_type> __l, flag_type __flags = ECMAScript)
  648. {
  649. _M_compile(__l.begin(), __l.end(), __flags);
  650. return *this;
  651. }
  652. // [7.8.4] const operations
  653. /**
  654. * @brief Gets the number of marked subexpressions within the regular
  655. * expression.
  656. */
  657. unsigned int
  658. mark_count() const noexcept
  659. {
  660. if (_M_automaton)
  661. return _M_automaton->_M_sub_count() - 1;
  662. return 0;
  663. }
  664. /**
  665. * @brief Gets the flags used to construct the regular expression
  666. * or in the last call to assign().
  667. */
  668. flag_type
  669. flags() const noexcept
  670. { return _M_flags; }
  671. // [7.8.5] locale
  672. /**
  673. * @brief Imbues the regular expression object with the given locale.
  674. *
  675. * @param __loc A locale.
  676. */
  677. locale_type
  678. imbue(locale_type __loc)
  679. {
  680. std::swap(__loc, _M_loc);
  681. _M_automaton.reset();
  682. return __loc;
  683. }
  684. /**
  685. * @brief Gets the locale currently imbued in the regular expression
  686. * object.
  687. */
  688. locale_type
  689. getloc() const noexcept
  690. { return _M_loc; }
  691. // [7.8.6] swap
  692. /**
  693. * @brief Swaps the contents of two regular expression objects.
  694. *
  695. * @param __rhs Another regular expression object.
  696. */
  697. void
  698. swap(basic_regex& __rhs) noexcept
  699. {
  700. std::swap(_M_flags, __rhs._M_flags);
  701. std::swap(_M_loc, __rhs._M_loc);
  702. std::swap(_M_automaton, __rhs._M_automaton);
  703. }
  704. #ifdef _GLIBCXX_DEBUG
  705. void
  706. _M_dot(std::ostream& __ostr)
  707. { _M_automaton->_M_dot(__ostr); }
  708. #endif
  709. private:
  710. typedef std::shared_ptr<const __detail::_NFA<_Rx_traits>> _AutomatonPtr;
  711. void
  712. _M_compile(const _Ch_type* __first, const _Ch_type* __last,
  713. flag_type __f)
  714. {
  715. __detail::_Compiler<_Rx_traits> __c(__first, __last, _M_loc, __f);
  716. _M_automaton = __c._M_get_nfa();
  717. _M_flags = __f;
  718. }
  719. template<typename _Bp, typename _Ap, typename _Cp, typename _Rp>
  720. friend bool
  721. __detail::__regex_algo_impl(_Bp, _Bp, match_results<_Bp, _Ap>&,
  722. const basic_regex<_Cp, _Rp>&,
  723. regex_constants::match_flag_type,
  724. __detail::_RegexExecutorPolicy, bool);
  725. template<typename, typename, typename, bool>
  726. friend class __detail::_Executor;
  727. flag_type _M_flags;
  728. locale_type _M_loc;
  729. _AutomatonPtr _M_automaton;
  730. };
  731. #if ! __cpp_inline_variables
  732. template<typename _Ch, typename _Tr>
  733. constexpr regex_constants::syntax_option_type
  734. basic_regex<_Ch, _Tr>::icase;
  735. template<typename _Ch, typename _Tr>
  736. constexpr regex_constants::syntax_option_type
  737. basic_regex<_Ch, _Tr>::nosubs;
  738. template<typename _Ch, typename _Tr>
  739. constexpr regex_constants::syntax_option_type
  740. basic_regex<_Ch, _Tr>::optimize;
  741. template<typename _Ch, typename _Tr>
  742. constexpr regex_constants::syntax_option_type
  743. basic_regex<_Ch, _Tr>::collate;
  744. template<typename _Ch, typename _Tr>
  745. constexpr regex_constants::syntax_option_type
  746. basic_regex<_Ch, _Tr>::ECMAScript;
  747. template<typename _Ch, typename _Tr>
  748. constexpr regex_constants::syntax_option_type
  749. basic_regex<_Ch, _Tr>::basic;
  750. template<typename _Ch, typename _Tr>
  751. constexpr regex_constants::syntax_option_type
  752. basic_regex<_Ch, _Tr>::extended;
  753. template<typename _Ch, typename _Tr>
  754. constexpr regex_constants::syntax_option_type
  755. basic_regex<_Ch, _Tr>::awk;
  756. template<typename _Ch, typename _Tr>
  757. constexpr regex_constants::syntax_option_type
  758. basic_regex<_Ch, _Tr>::grep;
  759. template<typename _Ch, typename _Tr>
  760. constexpr regex_constants::syntax_option_type
  761. basic_regex<_Ch, _Tr>::egrep;
  762. #endif // ! C++17
  763. #if __cpp_deduction_guides >= 201606
  764. template<typename _ForwardIterator>
  765. basic_regex(_ForwardIterator, _ForwardIterator,
  766. regex_constants::syntax_option_type = {})
  767. -> basic_regex<typename iterator_traits<_ForwardIterator>::value_type>;
  768. #endif
  769. /** @brief Standard regular expressions. */
  770. typedef basic_regex<char> regex;
  771. #ifdef _GLIBCXX_USE_WCHAR_T
  772. /** @brief Standard wide-character regular expressions. */
  773. typedef basic_regex<wchar_t> wregex;
  774. #endif
  775. // [7.8.6] basic_regex swap
  776. /**
  777. * @brief Swaps the contents of two regular expression objects.
  778. * @param __lhs First regular expression.
  779. * @param __rhs Second regular expression.
  780. * @relates basic_regex
  781. */
  782. template<typename _Ch_type, typename _Rx_traits>
  783. inline void
  784. swap(basic_regex<_Ch_type, _Rx_traits>& __lhs,
  785. basic_regex<_Ch_type, _Rx_traits>& __rhs) noexcept
  786. { __lhs.swap(__rhs); }
  787. // C++11 28.9 [re.submatch] Class template sub_match
  788. /**
  789. * A sequence of characters matched by a particular marked sub-expression.
  790. *
  791. * An object of this class is essentially a pair of iterators marking a
  792. * matched subexpression within a regular expression pattern match. Such
  793. * objects can be converted to and compared with std::basic_string objects
  794. * of a similar base character type as the pattern matched by the regular
  795. * expression.
  796. *
  797. * The iterators that make up the pair are the usual half-open interval
  798. * referencing the actual original pattern matched.
  799. */
  800. template<typename _BiIter>
  801. class sub_match : public std::pair<_BiIter, _BiIter>
  802. {
  803. typedef iterator_traits<_BiIter> __iter_traits;
  804. public:
  805. typedef typename __iter_traits::value_type value_type;
  806. typedef typename __iter_traits::difference_type difference_type;
  807. typedef _BiIter iterator;
  808. typedef basic_string<value_type> string_type;
  809. bool matched;
  810. constexpr sub_match() noexcept : matched() { }
  811. /// Gets the length of the matching sequence.
  812. difference_type
  813. length() const noexcept
  814. { return this->matched ? std::distance(this->first, this->second) : 0; }
  815. /**
  816. * @brief Gets the matching sequence as a string.
  817. *
  818. * @returns the matching sequence as a string.
  819. *
  820. * This is the implicit conversion operator. It is identical to the
  821. * str() member function except that it will want to pop up in
  822. * unexpected places and cause a great deal of confusion and cursing
  823. * from the unwary.
  824. */
  825. operator string_type() const
  826. { return str(); }
  827. /**
  828. * @brief Gets the matching sequence as a string.
  829. *
  830. * @returns the matching sequence as a string.
  831. */
  832. string_type
  833. str() const
  834. {
  835. return this->matched
  836. ? string_type(this->first, this->second)
  837. : string_type();
  838. }
  839. /**
  840. * @brief Compares this and another matched sequence.
  841. *
  842. * @param __s Another matched sequence to compare to this one.
  843. *
  844. * @retval negative This matched sequence will collate before `__s`.
  845. * @retval zero This matched sequence is equivalent to `__s`.
  846. * @retval positive This matched sequence will collate after `__s`.
  847. */
  848. int
  849. compare(const sub_match& __s) const
  850. { return this->_M_str().compare(__s._M_str()); }
  851. /**
  852. * @{
  853. * @brief Compares this `sub_match` to a string.
  854. *
  855. * @param __s A string to compare to this `sub_match`.
  856. *
  857. * @retval negative This matched sequence will collate before `__s`.
  858. * @retval zero This matched sequence is equivalent to `__s`.
  859. * @retval positive This matched sequence will collate after `__s`.
  860. */
  861. int
  862. compare(const string_type& __s) const
  863. { return this->_M_str().compare(__s); }
  864. int
  865. compare(const value_type* __s) const
  866. { return this->_M_str().compare(__s); }
  867. /// @}
  868. /// @cond undocumented
  869. // Non-standard, used by comparison operators
  870. int
  871. _M_compare(const value_type* __s, size_t __n) const
  872. { return this->_M_str().compare({__s, __n}); }
  873. /// @endcond
  874. private:
  875. // Simplified basic_string_view for C++11
  876. struct __string_view
  877. {
  878. using traits_type = typename string_type::traits_type;
  879. __string_view() = default;
  880. __string_view(const value_type* __s, size_t __n) noexcept
  881. : _M_data(__s), _M_len(__n) { }
  882. __string_view(const value_type* __s) noexcept
  883. : _M_data(__s), _M_len(traits_type::length(__s)) { }
  884. __string_view(const string_type& __s) noexcept
  885. : _M_data(__s.data()), _M_len(__s.length()) { }
  886. int
  887. compare(__string_view __s) const noexcept
  888. {
  889. if (const size_t __n = std::min(_M_len, __s._M_len))
  890. if (int __ret = traits_type::compare(_M_data, __s._M_data, __n))
  891. return __ret;
  892. using __limits = __gnu_cxx::__int_traits<int>;
  893. const difference_type __diff = _M_len - __s._M_len;
  894. if (__diff > __limits::__max)
  895. return __limits::__max;
  896. if (__diff < __limits::__min)
  897. return __limits::__min;
  898. return static_cast<int>(__diff);
  899. }
  900. private:
  901. const value_type* _M_data = nullptr;
  902. size_t _M_len = 0;
  903. };
  904. // Create a __string_view over the iterator range.
  905. template<typename _Iter = _BiIter>
  906. __enable_if_t<__detail::__is_contiguous_iter<_Iter>::value,
  907. __string_view>
  908. _M_str() const noexcept
  909. {
  910. if (this->matched)
  911. if (size_t __len = this->second - this->first)
  912. return { std::__addressof(*this->first), __len };
  913. return {};
  914. }
  915. // Create a temporary string that can be converted to __string_view.
  916. template<typename _Iter = _BiIter>
  917. __enable_if_t<!__detail::__is_contiguous_iter<_Iter>::value,
  918. string_type>
  919. _M_str() const
  920. { return str(); }
  921. };
  922. /** @brief Standard regex submatch over a C-style null-terminated string. */
  923. typedef sub_match<const char*> csub_match;
  924. /** @brief Standard regex submatch over a standard string. */
  925. typedef sub_match<string::const_iterator> ssub_match;
  926. #ifdef _GLIBCXX_USE_WCHAR_T
  927. /** @brief Regex submatch over a C-style null-terminated wide string. */
  928. typedef sub_match<const wchar_t*> wcsub_match;
  929. /** @brief Regex submatch over a standard wide string. */
  930. typedef sub_match<wstring::const_iterator> wssub_match;
  931. #endif
  932. // [7.9.2] sub_match non-member operators
  933. /// @relates sub_match @{
  934. /**
  935. * @brief Tests the equivalence of two regular expression submatches.
  936. * @param __lhs First regular expression submatch.
  937. * @param __rhs Second regular expression submatch.
  938. * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
  939. */
  940. template<typename _BiIter>
  941. inline bool
  942. operator==(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
  943. { return __lhs.compare(__rhs) == 0; }
  944. #if __cpp_lib_three_way_comparison
  945. /**
  946. * @brief Three-way comparison of two regular expression submatches.
  947. * @param __lhs First regular expression submatch.
  948. * @param __rhs Second regular expression submatch.
  949. * @returns A value indicating whether `__lhs` is less than, equal to,
  950. * greater than, or incomparable with `__rhs`.
  951. */
  952. template<typename _BiIter>
  953. inline auto
  954. operator<=>(const sub_match<_BiIter>& __lhs,
  955. const sub_match<_BiIter>& __rhs)
  956. noexcept(__detail::__is_contiguous_iter<_BiIter>::value)
  957. {
  958. using _Tr = char_traits<typename iterator_traits<_BiIter>::value_type>;
  959. return __detail::__char_traits_cmp_cat<_Tr>(__lhs.compare(__rhs));
  960. }
  961. #else
  962. /**
  963. * @brief Tests the inequivalence of two regular expression submatches.
  964. * @param __lhs First regular expression submatch.
  965. * @param __rhs Second regular expression submatch.
  966. * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
  967. */
  968. template<typename _BiIter>
  969. inline bool
  970. operator!=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
  971. { return __lhs.compare(__rhs) != 0; }
  972. /**
  973. * @brief Tests the ordering of two regular expression submatches.
  974. * @param __lhs First regular expression submatch.
  975. * @param __rhs Second regular expression submatch.
  976. * @returns true if @a __lhs precedes @a __rhs, false otherwise.
  977. */
  978. template<typename _BiIter>
  979. inline bool
  980. operator<(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
  981. { return __lhs.compare(__rhs) < 0; }
  982. /**
  983. * @brief Tests the ordering of two regular expression submatches.
  984. * @param __lhs First regular expression submatch.
  985. * @param __rhs Second regular expression submatch.
  986. * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
  987. */
  988. template<typename _BiIter>
  989. inline bool
  990. operator<=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
  991. { return __lhs.compare(__rhs) <= 0; }
  992. /**
  993. * @brief Tests the ordering of two regular expression submatches.
  994. * @param __lhs First regular expression submatch.
  995. * @param __rhs Second regular expression submatch.
  996. * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
  997. */
  998. template<typename _BiIter>
  999. inline bool
  1000. operator>=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
  1001. { return __lhs.compare(__rhs) >= 0; }
  1002. /**
  1003. * @brief Tests the ordering of two regular expression submatches.
  1004. * @param __lhs First regular expression submatch.
  1005. * @param __rhs Second regular expression submatch.
  1006. * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
  1007. */
  1008. template<typename _BiIter>
  1009. inline bool
  1010. operator>(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
  1011. { return __lhs.compare(__rhs) > 0; }
  1012. #endif // three-way comparison
  1013. /// @cond undocumented
  1014. // Alias for a basic_string that can be compared to a sub_match.
  1015. template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
  1016. using __sub_match_string = basic_string<
  1017. typename iterator_traits<_Bi_iter>::value_type,
  1018. _Ch_traits, _Ch_alloc>;
  1019. /// @endcond
  1020. #if ! __cpp_lib_three_way_comparison
  1021. /**
  1022. * @brief Tests the equivalence of a string and a regular expression
  1023. * submatch.
  1024. * @param __lhs A string.
  1025. * @param __rhs A regular expression submatch.
  1026. * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
  1027. */
  1028. template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
  1029. inline bool
  1030. operator==(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
  1031. const sub_match<_Bi_iter>& __rhs)
  1032. { return __rhs._M_compare(__lhs.data(), __lhs.size()) == 0; }
  1033. /**
  1034. * @brief Tests the inequivalence of a string and a regular expression
  1035. * submatch.
  1036. * @param __lhs A string.
  1037. * @param __rhs A regular expression submatch.
  1038. * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
  1039. */
  1040. template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
  1041. inline bool
  1042. operator!=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
  1043. const sub_match<_Bi_iter>& __rhs)
  1044. { return !(__lhs == __rhs); }
  1045. /**
  1046. * @brief Tests the ordering of a string and a regular expression submatch.
  1047. * @param __lhs A string.
  1048. * @param __rhs A regular expression submatch.
  1049. * @returns true if @a __lhs precedes @a __rhs, false otherwise.
  1050. */
  1051. template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
  1052. inline bool
  1053. operator<(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
  1054. const sub_match<_Bi_iter>& __rhs)
  1055. { return __rhs._M_compare(__lhs.data(), __lhs.size()) > 0; }
  1056. /**
  1057. * @brief Tests the ordering of a string and a regular expression submatch.
  1058. * @param __lhs A string.
  1059. * @param __rhs A regular expression submatch.
  1060. * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
  1061. */
  1062. template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
  1063. inline bool
  1064. operator>(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
  1065. const sub_match<_Bi_iter>& __rhs)
  1066. { return __rhs < __lhs; }
  1067. /**
  1068. * @brief Tests the ordering of a string and a regular expression submatch.
  1069. * @param __lhs A string.
  1070. * @param __rhs A regular expression submatch.
  1071. * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
  1072. */
  1073. template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
  1074. inline bool
  1075. operator>=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
  1076. const sub_match<_Bi_iter>& __rhs)
  1077. { return !(__lhs < __rhs); }
  1078. /**
  1079. * @brief Tests the ordering of a string and a regular expression submatch.
  1080. * @param __lhs A string.
  1081. * @param __rhs A regular expression submatch.
  1082. * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
  1083. */
  1084. template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
  1085. inline bool
  1086. operator<=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
  1087. const sub_match<_Bi_iter>& __rhs)
  1088. { return !(__rhs < __lhs); }
  1089. #endif // three-way comparison
  1090. /**
  1091. * @brief Tests the equivalence of a regular expression submatch and a
  1092. * string.
  1093. * @param __lhs A regular expression submatch.
  1094. * @param __rhs A string.
  1095. * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
  1096. */
  1097. template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
  1098. inline bool
  1099. operator==(const sub_match<_Bi_iter>& __lhs,
  1100. const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
  1101. { return __lhs._M_compare(__rhs.data(), __rhs.size()) == 0; }
  1102. #if __cpp_lib_three_way_comparison
  1103. /**
  1104. * @brief Three-way comparison of a regular expression submatch and a string.
  1105. * @param __lhs A regular expression submatch.
  1106. * @param __rhs A string.
  1107. * @returns A value indicating whether `__lhs` is less than, equal to,
  1108. * greater than, or incomparable with `__rhs`.
  1109. */
  1110. template<typename _Bi_iter, typename _Ch_traits, typename _Alloc>
  1111. inline auto
  1112. operator<=>(const sub_match<_Bi_iter>& __lhs,
  1113. const __sub_match_string<_Bi_iter, _Ch_traits, _Alloc>& __rhs)
  1114. noexcept(__detail::__is_contiguous_iter<_Bi_iter>::value)
  1115. {
  1116. return __detail::__char_traits_cmp_cat<_Ch_traits>(
  1117. __lhs._M_compare(__rhs.data(), __rhs.size()));
  1118. }
  1119. #else
  1120. /**
  1121. * @brief Tests the inequivalence of a regular expression submatch and a
  1122. * string.
  1123. * @param __lhs A regular expression submatch.
  1124. * @param __rhs A string.
  1125. * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
  1126. */
  1127. template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
  1128. inline bool
  1129. operator!=(const sub_match<_Bi_iter>& __lhs,
  1130. const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
  1131. { return !(__lhs == __rhs); }
  1132. /**
  1133. * @brief Tests the ordering of a regular expression submatch and a string.
  1134. * @param __lhs A regular expression submatch.
  1135. * @param __rhs A string.
  1136. * @returns true if @a __lhs precedes @a __rhs, false otherwise.
  1137. */
  1138. template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
  1139. inline bool
  1140. operator<(const sub_match<_Bi_iter>& __lhs,
  1141. const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
  1142. { return __lhs._M_compare(__rhs.data(), __rhs.size()) < 0; }
  1143. /**
  1144. * @brief Tests the ordering of a regular expression submatch and a string.
  1145. * @param __lhs A regular expression submatch.
  1146. * @param __rhs A string.
  1147. * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
  1148. */
  1149. template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
  1150. inline bool
  1151. operator>(const sub_match<_Bi_iter>& __lhs,
  1152. const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
  1153. { return __rhs < __lhs; }
  1154. /**
  1155. * @brief Tests the ordering of a regular expression submatch and a string.
  1156. * @param __lhs A regular expression submatch.
  1157. * @param __rhs A string.
  1158. * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
  1159. */
  1160. template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
  1161. inline bool
  1162. operator>=(const sub_match<_Bi_iter>& __lhs,
  1163. const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
  1164. { return !(__lhs < __rhs); }
  1165. /**
  1166. * @brief Tests the ordering of a regular expression submatch and a string.
  1167. * @param __lhs A regular expression submatch.
  1168. * @param __rhs A string.
  1169. * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
  1170. */
  1171. template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
  1172. inline bool
  1173. operator<=(const sub_match<_Bi_iter>& __lhs,
  1174. const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
  1175. { return !(__rhs < __lhs); }
  1176. /**
  1177. * @brief Tests the equivalence of a C string and a regular expression
  1178. * submatch.
  1179. * @param __lhs A null-terminated string.
  1180. * @param __rhs A regular expression submatch.
  1181. * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
  1182. */
  1183. template<typename _Bi_iter>
  1184. inline bool
  1185. operator==(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
  1186. const sub_match<_Bi_iter>& __rhs)
  1187. { return __rhs.compare(__lhs) == 0; }
  1188. /**
  1189. * @brief Tests the inequivalence of a C string and a regular
  1190. * expression submatch.
  1191. * @param __lhs A null-terminated string.
  1192. * @param __rhs A regular expression submatch.
  1193. * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
  1194. */
  1195. template<typename _Bi_iter>
  1196. inline bool
  1197. operator!=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
  1198. const sub_match<_Bi_iter>& __rhs)
  1199. { return !(__lhs == __rhs); }
  1200. /**
  1201. * @brief Tests the ordering of a C string and a regular expression submatch.
  1202. * @param __lhs A null-terminated string.
  1203. * @param __rhs A regular expression submatch.
  1204. * @returns true if @a __lhs precedes @a __rhs, false otherwise.
  1205. */
  1206. template<typename _Bi_iter>
  1207. inline bool
  1208. operator<(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
  1209. const sub_match<_Bi_iter>& __rhs)
  1210. { return __rhs.compare(__lhs) > 0; }
  1211. /**
  1212. * @brief Tests the ordering of a C string and a regular expression submatch.
  1213. * @param __lhs A null-terminated string.
  1214. * @param __rhs A regular expression submatch.
  1215. * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
  1216. */
  1217. template<typename _Bi_iter>
  1218. inline bool
  1219. operator>(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
  1220. const sub_match<_Bi_iter>& __rhs)
  1221. { return __rhs < __lhs; }
  1222. /**
  1223. * @brief Tests the ordering of a C string and a regular expression submatch.
  1224. * @param __lhs A null-terminated string.
  1225. * @param __rhs A regular expression submatch.
  1226. * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
  1227. */
  1228. template<typename _Bi_iter>
  1229. inline bool
  1230. operator>=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
  1231. const sub_match<_Bi_iter>& __rhs)
  1232. { return !(__lhs < __rhs); }
  1233. /**
  1234. * @brief Tests the ordering of a C string and a regular expression submatch.
  1235. * @param __lhs A null-terminated string.
  1236. * @param __rhs A regular expression submatch.
  1237. * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
  1238. */
  1239. template<typename _Bi_iter>
  1240. inline bool
  1241. operator<=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
  1242. const sub_match<_Bi_iter>& __rhs)
  1243. { return !(__rhs < __lhs); }
  1244. #endif // three-way comparison
  1245. /**
  1246. * @brief Tests the equivalence of a regular expression submatch and a C
  1247. * string.
  1248. * @param __lhs A regular expression submatch.
  1249. * @param __rhs A null-terminated string.
  1250. * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
  1251. */
  1252. template<typename _Bi_iter>
  1253. inline bool
  1254. operator==(const sub_match<_Bi_iter>& __lhs,
  1255. typename iterator_traits<_Bi_iter>::value_type const* __rhs)
  1256. { return __lhs.compare(__rhs) == 0; }
  1257. #if __cpp_lib_three_way_comparison
  1258. /**
  1259. * @brief Three-way comparison of a regular expression submatch and a C
  1260. * string.
  1261. * @param __lhs A regular expression submatch.
  1262. * @param __rhs A null-terminated string.
  1263. * @returns A value indicating whether `__lhs` is less than, equal to,
  1264. * greater than, or incomparable with `__rhs`.
  1265. */
  1266. template<typename _Bi_iter>
  1267. inline auto
  1268. operator<=>(const sub_match<_Bi_iter>& __lhs,
  1269. typename iterator_traits<_Bi_iter>::value_type const* __rhs)
  1270. noexcept(__detail::__is_contiguous_iter<_Bi_iter>::value)
  1271. {
  1272. using _Tr = char_traits<typename iterator_traits<_Bi_iter>::value_type>;
  1273. return __detail::__char_traits_cmp_cat<_Tr>(__lhs.compare(__rhs));
  1274. }
  1275. #else
  1276. /**
  1277. * @brief Tests the inequivalence of a regular expression submatch and a
  1278. * string.
  1279. * @param __lhs A regular expression submatch.
  1280. * @param __rhs A null-terminated string.
  1281. * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
  1282. */
  1283. template<typename _Bi_iter>
  1284. inline bool
  1285. operator!=(const sub_match<_Bi_iter>& __lhs,
  1286. typename iterator_traits<_Bi_iter>::value_type const* __rhs)
  1287. { return !(__lhs == __rhs); }
  1288. /**
  1289. * @brief Tests the ordering of a regular expression submatch and a C string.
  1290. * @param __lhs A regular expression submatch.
  1291. * @param __rhs A null-terminated string.
  1292. * @returns true if @a __lhs precedes @a __rhs, false otherwise.
  1293. */
  1294. template<typename _Bi_iter>
  1295. inline bool
  1296. operator<(const sub_match<_Bi_iter>& __lhs,
  1297. typename iterator_traits<_Bi_iter>::value_type const* __rhs)
  1298. { return __lhs.compare(__rhs) < 0; }
  1299. /**
  1300. * @brief Tests the ordering of a regular expression submatch and a C string.
  1301. * @param __lhs A regular expression submatch.
  1302. * @param __rhs A null-terminated string.
  1303. * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
  1304. */
  1305. template<typename _Bi_iter>
  1306. inline bool
  1307. operator>(const sub_match<_Bi_iter>& __lhs,
  1308. typename iterator_traits<_Bi_iter>::value_type const* __rhs)
  1309. { return __rhs < __lhs; }
  1310. /**
  1311. * @brief Tests the ordering of a regular expression submatch and a C string.
  1312. * @param __lhs A regular expression submatch.
  1313. * @param __rhs A null-terminated string.
  1314. * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
  1315. */
  1316. template<typename _Bi_iter>
  1317. inline bool
  1318. operator>=(const sub_match<_Bi_iter>& __lhs,
  1319. typename iterator_traits<_Bi_iter>::value_type const* __rhs)
  1320. { return !(__lhs < __rhs); }
  1321. /**
  1322. * @brief Tests the ordering of a regular expression submatch and a C string.
  1323. * @param __lhs A regular expression submatch.
  1324. * @param __rhs A null-terminated string.
  1325. * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
  1326. */
  1327. template<typename _Bi_iter>
  1328. inline bool
  1329. operator<=(const sub_match<_Bi_iter>& __lhs,
  1330. typename iterator_traits<_Bi_iter>::value_type const* __rhs)
  1331. { return !(__rhs < __lhs); }
  1332. /**
  1333. * @brief Tests the equivalence of a character and a regular expression
  1334. * submatch.
  1335. * @param __lhs A character.
  1336. * @param __rhs A regular expression submatch.
  1337. * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
  1338. */
  1339. template<typename _Bi_iter>
  1340. inline bool
  1341. operator==(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
  1342. const sub_match<_Bi_iter>& __rhs)
  1343. { return __rhs._M_compare(std::__addressof(__lhs), 1) == 0; }
  1344. /**
  1345. * @brief Tests the inequivalence of a character and a regular expression
  1346. * submatch.
  1347. * @param __lhs A character.
  1348. * @param __rhs A regular expression submatch.
  1349. * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
  1350. */
  1351. template<typename _Bi_iter>
  1352. inline bool
  1353. operator!=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
  1354. const sub_match<_Bi_iter>& __rhs)
  1355. { return !(__lhs == __rhs); }
  1356. /**
  1357. * @brief Tests the ordering of a character and a regular expression
  1358. * submatch.
  1359. * @param __lhs A character.
  1360. * @param __rhs A regular expression submatch.
  1361. * @returns true if @a __lhs precedes @a __rhs, false otherwise.
  1362. */
  1363. template<typename _Bi_iter>
  1364. inline bool
  1365. operator<(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
  1366. const sub_match<_Bi_iter>& __rhs)
  1367. { return __rhs._M_compare(std::__addressof(__lhs), 1) > 0; }
  1368. /**
  1369. * @brief Tests the ordering of a character and a regular expression
  1370. * submatch.
  1371. * @param __lhs A character.
  1372. * @param __rhs A regular expression submatch.
  1373. * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
  1374. */
  1375. template<typename _Bi_iter>
  1376. inline bool
  1377. operator>(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
  1378. const sub_match<_Bi_iter>& __rhs)
  1379. { return __rhs < __lhs; }
  1380. /**
  1381. * @brief Tests the ordering of a character and a regular expression
  1382. * submatch.
  1383. * @param __lhs A character.
  1384. * @param __rhs A regular expression submatch.
  1385. * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
  1386. */
  1387. template<typename _Bi_iter>
  1388. inline bool
  1389. operator>=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
  1390. const sub_match<_Bi_iter>& __rhs)
  1391. { return !(__lhs < __rhs); }
  1392. /**
  1393. * @brief Tests the ordering of a character and a regular expression
  1394. * submatch.
  1395. * @param __lhs A character.
  1396. * @param __rhs A regular expression submatch.
  1397. * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
  1398. */
  1399. template<typename _Bi_iter>
  1400. inline bool
  1401. operator<=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
  1402. const sub_match<_Bi_iter>& __rhs)
  1403. { return !(__rhs < __lhs); }
  1404. #endif // three-way comparison
  1405. /**
  1406. * @brief Tests the equivalence of a regular expression submatch and a
  1407. * character.
  1408. * @param __lhs A regular expression submatch.
  1409. * @param __rhs A character.
  1410. * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
  1411. */
  1412. template<typename _Bi_iter>
  1413. inline bool
  1414. operator==(const sub_match<_Bi_iter>& __lhs,
  1415. typename iterator_traits<_Bi_iter>::value_type const& __rhs)
  1416. { return __lhs._M_compare(std::__addressof(__rhs), 1) == 0; }
  1417. #if __cpp_lib_three_way_comparison
  1418. /**
  1419. * @brief Three-way comparison of a regular expression submatch and a
  1420. * character.
  1421. * @param __lhs A regular expression submatch.
  1422. * @param __rhs A character.
  1423. * @returns A value indicating whether `__lhs` is less than, equal to,
  1424. * greater than, or incomparable with `__rhs`.
  1425. */
  1426. template<typename _Bi_iter>
  1427. inline auto
  1428. operator<=>(const sub_match<_Bi_iter>& __lhs,
  1429. typename iterator_traits<_Bi_iter>::value_type const& __rhs)
  1430. noexcept(__detail::__is_contiguous_iter<_Bi_iter>::value)
  1431. {
  1432. using _Tr = char_traits<typename iterator_traits<_Bi_iter>::value_type>;
  1433. return __detail::__char_traits_cmp_cat<_Tr>(
  1434. __lhs._M_compare(std::__addressof(__rhs), 1));
  1435. }
  1436. #else
  1437. /**
  1438. * @brief Tests the inequivalence of a regular expression submatch and a
  1439. * character.
  1440. * @param __lhs A regular expression submatch.
  1441. * @param __rhs A character.
  1442. * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
  1443. */
  1444. template<typename _Bi_iter>
  1445. inline bool
  1446. operator!=(const sub_match<_Bi_iter>& __lhs,
  1447. typename iterator_traits<_Bi_iter>::value_type const& __rhs)
  1448. { return !(__lhs == __rhs); }
  1449. /**
  1450. * @brief Tests the ordering of a regular expression submatch and a
  1451. * character.
  1452. * @param __lhs A regular expression submatch.
  1453. * @param __rhs A character.
  1454. * @returns true if @a __lhs precedes @a __rhs, false otherwise.
  1455. */
  1456. template<typename _Bi_iter>
  1457. inline bool
  1458. operator<(const sub_match<_Bi_iter>& __lhs,
  1459. typename iterator_traits<_Bi_iter>::value_type const& __rhs)
  1460. { return __lhs._M_compare(std::__addressof(__rhs), 1) < 0; }
  1461. /**
  1462. * @brief Tests the ordering of a regular expression submatch and a
  1463. * character.
  1464. * @param __lhs A regular expression submatch.
  1465. * @param __rhs A character.
  1466. * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
  1467. */
  1468. template<typename _Bi_iter>
  1469. inline bool
  1470. operator>(const sub_match<_Bi_iter>& __lhs,
  1471. typename iterator_traits<_Bi_iter>::value_type const& __rhs)
  1472. { return __rhs < __lhs; }
  1473. /**
  1474. * @brief Tests the ordering of a regular expression submatch and a
  1475. * character.
  1476. * @param __lhs A regular expression submatch.
  1477. * @param __rhs A character.
  1478. * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
  1479. */
  1480. template<typename _Bi_iter>
  1481. inline bool
  1482. operator>=(const sub_match<_Bi_iter>& __lhs,
  1483. typename iterator_traits<_Bi_iter>::value_type const& __rhs)
  1484. { return !(__lhs < __rhs); }
  1485. /**
  1486. * @brief Tests the ordering of a regular expression submatch and a
  1487. * character.
  1488. * @param __lhs A regular expression submatch.
  1489. * @param __rhs A character.
  1490. * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
  1491. */
  1492. template<typename _Bi_iter>
  1493. inline bool
  1494. operator<=(const sub_match<_Bi_iter>& __lhs,
  1495. typename iterator_traits<_Bi_iter>::value_type const& __rhs)
  1496. { return !(__rhs < __lhs); }
  1497. #endif // three-way comparison
  1498. /**
  1499. * @brief Inserts a matched string into an output stream.
  1500. *
  1501. * @param __os The output stream.
  1502. * @param __m A submatch string.
  1503. *
  1504. * @returns the output stream with the submatch string inserted.
  1505. */
  1506. template<typename _Ch_type, typename _Ch_traits, typename _Bi_iter>
  1507. inline
  1508. basic_ostream<_Ch_type, _Ch_traits>&
  1509. operator<<(basic_ostream<_Ch_type, _Ch_traits>& __os,
  1510. const sub_match<_Bi_iter>& __m)
  1511. { return __os << __m.str(); }
  1512. /// @} relates sub_match
  1513. // [7.10] Class template match_results
  1514. /**
  1515. * @brief The results of a match or search operation.
  1516. *
  1517. * A collection of character sequences representing the result of a regular
  1518. * expression match. Storage for the collection is allocated and freed as
  1519. * necessary by the member functions of class template match_results.
  1520. *
  1521. * This class satisfies the Sequence requirements, with the exception that
  1522. * only the operations defined for a const-qualified Sequence are supported.
  1523. *
  1524. * The sub_match object stored at index 0 represents sub-expression 0, i.e.
  1525. * the whole match. In this case the %sub_match member matched is always true.
  1526. * The sub_match object stored at index n denotes what matched the marked
  1527. * sub-expression n within the matched expression. If the sub-expression n
  1528. * participated in a regular expression match then the %sub_match member
  1529. * matched evaluates to true, and members first and second denote the range
  1530. * of characters [first, second) which formed that match. Otherwise matched
  1531. * is false, and members first and second point to the end of the sequence
  1532. * that was searched.
  1533. */
  1534. template<typename _Bi_iter,
  1535. typename _Alloc = allocator<sub_match<_Bi_iter> > >
  1536. class match_results
  1537. : private std::vector<sub_match<_Bi_iter>, _Alloc>
  1538. {
  1539. private:
  1540. /*
  1541. * The vector base is empty if this does not represent a match (!ready());
  1542. * Otherwise if it's a match failure, it contains 3 elements:
  1543. * [0] unmatched
  1544. * [1] prefix
  1545. * [2] suffix
  1546. * Otherwise it contains n+4 elements where n is the number of marked
  1547. * sub-expressions:
  1548. * [0] entire match
  1549. * [1] 1st marked subexpression
  1550. * ...
  1551. * [n] nth marked subexpression
  1552. * [n+1] unmatched
  1553. * [n+2] prefix
  1554. * [n+3] suffix
  1555. */
  1556. typedef std::vector<sub_match<_Bi_iter>, _Alloc> _Base_type;
  1557. // In debug mode _Base_type is the debug vector, this is the unsafe one:
  1558. typedef _GLIBCXX_STD_C::vector<sub_match<_Bi_iter>, _Alloc> _Unchecked;
  1559. typedef std::iterator_traits<_Bi_iter> __iter_traits;
  1560. typedef regex_constants::match_flag_type match_flag_type;
  1561. public:
  1562. /**
  1563. * @name 28.10 Public Types
  1564. */
  1565. ///@{
  1566. typedef sub_match<_Bi_iter> value_type;
  1567. typedef const value_type& const_reference;
  1568. typedef value_type& reference;
  1569. typedef typename _Base_type::const_iterator const_iterator;
  1570. typedef const_iterator iterator;
  1571. typedef typename __iter_traits::difference_type difference_type;
  1572. typedef typename allocator_traits<_Alloc>::size_type size_type;
  1573. typedef _Alloc allocator_type;
  1574. typedef typename __iter_traits::value_type char_type;
  1575. typedef std::basic_string<char_type> string_type;
  1576. ///@}
  1577. public:
  1578. /**
  1579. * @name 28.10.1 Construction, Copying, and Destruction
  1580. */
  1581. ///@{
  1582. /**
  1583. * @brief Constructs a default %match_results container.
  1584. * @post size() returns 0 and str() returns an empty string.
  1585. */
  1586. match_results() : match_results(_Alloc()) { }
  1587. /**
  1588. * @brief Constructs a default %match_results container.
  1589. * @post size() returns 0 and str() returns an empty string.
  1590. */
  1591. explicit
  1592. match_results(const _Alloc& __a) noexcept
  1593. : _Base_type(__a)
  1594. { }
  1595. /**
  1596. * @brief Copy constructs a %match_results.
  1597. */
  1598. match_results(const match_results&) = default;
  1599. /**
  1600. * @brief Move constructs a %match_results.
  1601. */
  1602. match_results(match_results&&) noexcept = default;
  1603. /**
  1604. * @brief Assigns rhs to *this.
  1605. */
  1606. match_results&
  1607. operator=(const match_results&) = default;
  1608. /**
  1609. * @brief Move-assigns rhs to *this.
  1610. */
  1611. match_results&
  1612. operator=(match_results&&) = default;
  1613. /**
  1614. * @brief Destroys a %match_results object.
  1615. */
  1616. ~match_results() = default;
  1617. ///@}
  1618. // 28.10.2, state:
  1619. /**
  1620. * @brief Indicates if the %match_results is ready.
  1621. * @retval true The object has a fully-established result state.
  1622. * @retval false The object is not ready.
  1623. */
  1624. bool ready() const noexcept { return !_Unchecked::empty(); }
  1625. /**
  1626. * @name 28.10.2 Size
  1627. */
  1628. ///@{
  1629. /**
  1630. * @brief Gets the number of matches and submatches.
  1631. *
  1632. * The number of matches for a given regular expression will be either 0
  1633. * if there was no match or mark_count() + 1 if a match was successful.
  1634. * Some matches may be empty.
  1635. *
  1636. * @returns the number of matches found.
  1637. */
  1638. size_type
  1639. size() const noexcept
  1640. { return _Unchecked::empty() ? 0 : _Unchecked::size() - 3; }
  1641. size_type
  1642. max_size() const noexcept
  1643. { return _Unchecked::max_size() - 3; }
  1644. /**
  1645. * @brief Indicates if the %match_results contains no results.
  1646. * @retval true The %match_results object is empty.
  1647. * @retval false The %match_results object is not empty.
  1648. */
  1649. _GLIBCXX_NODISCARD bool
  1650. empty() const noexcept
  1651. { return _Unchecked::size() <= 3; }
  1652. ///@}
  1653. /**
  1654. * @name 28.10.4 Element Access
  1655. */
  1656. ///@{
  1657. /**
  1658. * @brief Gets the length of the indicated submatch.
  1659. * @param __sub indicates the submatch.
  1660. * @pre ready() == true
  1661. *
  1662. * This function returns the length of the indicated submatch, or the
  1663. * length of the entire match if @p __sub is zero (the default).
  1664. */
  1665. difference_type
  1666. length(size_type __sub = 0) const
  1667. { return (*this)[__sub].length(); }
  1668. /**
  1669. * @brief Gets the offset of the beginning of the indicated submatch.
  1670. * @param __sub indicates the submatch.
  1671. * @pre ready() == true
  1672. *
  1673. * This function returns the offset from the beginning of the target
  1674. * sequence to the beginning of the submatch, unless the value of @p __sub
  1675. * is zero (the default), in which case this function returns the offset
  1676. * from the beginning of the target sequence to the beginning of the
  1677. * match.
  1678. */
  1679. difference_type
  1680. position(size_type __sub = 0) const
  1681. { return std::distance(_M_begin, (*this)[__sub].first); }
  1682. /**
  1683. * @brief Gets the match or submatch converted to a string type.
  1684. * @param __sub indicates the submatch.
  1685. * @pre ready() == true
  1686. *
  1687. * This function gets the submatch (or match, if @p __sub is
  1688. * zero) extracted from the target range and converted to the
  1689. * associated string type.
  1690. */
  1691. string_type
  1692. str(size_type __sub = 0) const
  1693. { return string_type((*this)[__sub]); }
  1694. /**
  1695. * @brief Gets a %sub_match reference for the match or submatch.
  1696. * @param __sub indicates the submatch.
  1697. * @pre ready() == true
  1698. *
  1699. * This function gets a reference to the indicated submatch, or
  1700. * the entire match if @p __sub is zero.
  1701. *
  1702. * If @p __sub >= size() then this function returns a %sub_match with a
  1703. * special value indicating no submatch.
  1704. */
  1705. const_reference
  1706. operator[](size_type __sub) const
  1707. {
  1708. __glibcxx_assert( ready() );
  1709. return __sub < size()
  1710. ? _Unchecked::operator[](__sub)
  1711. : _M_unmatched_sub();
  1712. }
  1713. /**
  1714. * @brief Gets a %sub_match representing the match prefix.
  1715. * @pre ready() == true
  1716. *
  1717. * This function gets a reference to a %sub_match object representing the
  1718. * part of the target range between the start of the target range and the
  1719. * start of the match.
  1720. */
  1721. const_reference
  1722. prefix() const
  1723. {
  1724. __glibcxx_assert( ready() );
  1725. return !empty() ? _M_prefix() : _M_unmatched_sub();
  1726. }
  1727. /**
  1728. * @brief Gets a %sub_match representing the match suffix.
  1729. * @pre ready() == true
  1730. *
  1731. * This function gets a reference to a %sub_match object representing the
  1732. * part of the target range between the end of the match and the end of
  1733. * the target range.
  1734. */
  1735. const_reference
  1736. suffix() const
  1737. {
  1738. __glibcxx_assert( ready() );
  1739. return !empty() ? _M_suffix() : _M_unmatched_sub();
  1740. }
  1741. /**
  1742. * @brief Gets an iterator to the start of the %sub_match collection.
  1743. */
  1744. const_iterator
  1745. begin() const noexcept
  1746. { return _Base_type::begin(); }
  1747. /**
  1748. * @brief Gets an iterator to the start of the %sub_match collection.
  1749. */
  1750. const_iterator
  1751. cbegin() const noexcept
  1752. { return this->begin(); }
  1753. /**
  1754. * @brief Gets an iterator to one-past-the-end of the collection.
  1755. */
  1756. const_iterator
  1757. end() const noexcept
  1758. { return _Base_type::end() - (_Base_type::empty() ? 0 : 3); }
  1759. /**
  1760. * @brief Gets an iterator to one-past-the-end of the collection.
  1761. */
  1762. const_iterator
  1763. cend() const noexcept
  1764. { return this->end(); }
  1765. ///@}
  1766. /**
  1767. * @name 28.10.5 Formatting
  1768. *
  1769. * These functions perform formatted substitution of the matched
  1770. * character sequences into their target. The format specifiers and
  1771. * escape sequences accepted by these functions are determined by
  1772. * their @p flags parameter as documented above.
  1773. */
  1774. ///@{
  1775. /**
  1776. * @pre ready() == true
  1777. */
  1778. template<typename _Out_iter>
  1779. _Out_iter
  1780. format(_Out_iter __out, const char_type* __fmt_first,
  1781. const char_type* __fmt_last,
  1782. match_flag_type __flags = regex_constants::format_default) const;
  1783. /**
  1784. * @pre ready() == true
  1785. */
  1786. template<typename _Out_iter, typename _St, typename _Sa>
  1787. _Out_iter
  1788. format(_Out_iter __out, const basic_string<char_type, _St, _Sa>& __fmt,
  1789. match_flag_type __flags = regex_constants::format_default) const
  1790. {
  1791. return format(__out, __fmt.data(), __fmt.data() + __fmt.size(),
  1792. __flags);
  1793. }
  1794. /**
  1795. * @pre ready() == true
  1796. */
  1797. template<typename _St, typename _Sa>
  1798. basic_string<char_type, _St, _Sa>
  1799. format(const basic_string<char_type, _St, _Sa>& __fmt,
  1800. match_flag_type __flags = regex_constants::format_default) const
  1801. {
  1802. basic_string<char_type, _St, _Sa> __result;
  1803. format(std::back_inserter(__result), __fmt, __flags);
  1804. return __result;
  1805. }
  1806. /**
  1807. * @pre ready() == true
  1808. */
  1809. string_type
  1810. format(const char_type* __fmt,
  1811. match_flag_type __flags = regex_constants::format_default) const
  1812. {
  1813. string_type __result;
  1814. format(std::back_inserter(__result),
  1815. __fmt,
  1816. __fmt + char_traits<char_type>::length(__fmt),
  1817. __flags);
  1818. return __result;
  1819. }
  1820. ///@}
  1821. /**
  1822. * @name 28.10.6 Allocator
  1823. */
  1824. ///@{
  1825. /**
  1826. * @brief Gets a copy of the allocator.
  1827. */
  1828. allocator_type
  1829. get_allocator() const noexcept
  1830. { return _Base_type::get_allocator(); }
  1831. ///@}
  1832. /**
  1833. * @name 28.10.7 Swap
  1834. */
  1835. ///@{
  1836. /**
  1837. * @brief Swaps the contents of two match_results.
  1838. */
  1839. void
  1840. swap(match_results& __that) noexcept
  1841. {
  1842. using std::swap;
  1843. _Base_type::swap(__that);
  1844. swap(_M_begin, __that._M_begin);
  1845. }
  1846. ///@}
  1847. private:
  1848. template<typename, typename, typename>
  1849. friend class regex_iterator;
  1850. /// @cond undocumented
  1851. template<typename, typename, typename, bool>
  1852. friend class __detail::_Executor;
  1853. template<typename _Bp, typename _Ap, typename _Cp, typename _Rp>
  1854. friend bool
  1855. __detail::__regex_algo_impl(_Bp, _Bp, match_results<_Bp, _Ap>&,
  1856. const basic_regex<_Cp, _Rp>&,
  1857. regex_constants::match_flag_type,
  1858. __detail::_RegexExecutorPolicy, bool);
  1859. // Reset contents to __size unmatched sub_match objects
  1860. // (plus additional objects for prefix, suffix and unmatched sub).
  1861. void
  1862. _M_resize(unsigned int __size)
  1863. { _Unchecked::assign(__size + 3, sub_match<_Bi_iter>{}); }
  1864. // Set state to a failed match for the given past-the-end iterator.
  1865. void
  1866. _M_establish_failed_match(_Bi_iter __end)
  1867. {
  1868. sub_match<_Bi_iter> __sm;
  1869. __sm.first = __sm.second = __end;
  1870. _Unchecked::assign(3, __sm);
  1871. }
  1872. const_reference
  1873. _M_unmatched_sub() const
  1874. { return _Unchecked::operator[](_Unchecked::size() - 3); }
  1875. sub_match<_Bi_iter>&
  1876. _M_unmatched_sub()
  1877. { return _Unchecked::operator[](_Unchecked::size() - 3); }
  1878. const_reference
  1879. _M_prefix() const
  1880. { return _Unchecked::operator[](_Unchecked::size() - 2); }
  1881. sub_match<_Bi_iter>&
  1882. _M_prefix()
  1883. { return _Unchecked::operator[](_Unchecked::size() - 2); }
  1884. const_reference
  1885. _M_suffix() const
  1886. { return _Unchecked::operator[](_Unchecked::size() - 1); }
  1887. sub_match<_Bi_iter>&
  1888. _M_suffix()
  1889. { return _Unchecked::operator[](_Unchecked::size() - 1); }
  1890. _Bi_iter _M_begin {};
  1891. /// @endcond
  1892. };
  1893. typedef match_results<const char*> cmatch;
  1894. typedef match_results<string::const_iterator> smatch;
  1895. #ifdef _GLIBCXX_USE_WCHAR_T
  1896. typedef match_results<const wchar_t*> wcmatch;
  1897. typedef match_results<wstring::const_iterator> wsmatch;
  1898. #endif
  1899. // match_results comparisons
  1900. /**
  1901. * @brief Compares two match_results for equality.
  1902. * @returns true if the two objects refer to the same match,
  1903. * false otherwise.
  1904. */
  1905. template<typename _Bi_iter, typename _Alloc>
  1906. inline bool
  1907. operator==(const match_results<_Bi_iter, _Alloc>& __m1,
  1908. const match_results<_Bi_iter, _Alloc>& __m2)
  1909. {
  1910. if (__m1.ready() != __m2.ready())
  1911. return false;
  1912. if (!__m1.ready()) // both are not ready
  1913. return true;
  1914. if (__m1.empty() != __m2.empty())
  1915. return false;
  1916. if (__m1.empty()) // both are empty
  1917. return true;
  1918. return __m1.prefix() == __m2.prefix()
  1919. && __m1.size() == __m2.size()
  1920. && std::equal(__m1.begin(), __m1.end(), __m2.begin())
  1921. && __m1.suffix() == __m2.suffix();
  1922. }
  1923. #if ! __cpp_lib_three_way_comparison
  1924. /**
  1925. * @brief Compares two match_results for inequality.
  1926. * @returns true if the two objects do not refer to the same match,
  1927. * false otherwise.
  1928. */
  1929. template<typename _Bi_iter, class _Alloc>
  1930. inline bool
  1931. operator!=(const match_results<_Bi_iter, _Alloc>& __m1,
  1932. const match_results<_Bi_iter, _Alloc>& __m2)
  1933. { return !(__m1 == __m2); }
  1934. #endif
  1935. // [7.10.6] match_results swap
  1936. /**
  1937. * @brief Swaps two match results.
  1938. * @param __lhs A match result.
  1939. * @param __rhs A match result.
  1940. *
  1941. * The contents of the two match_results objects are swapped.
  1942. */
  1943. template<typename _Bi_iter, typename _Alloc>
  1944. inline void
  1945. swap(match_results<_Bi_iter, _Alloc>& __lhs,
  1946. match_results<_Bi_iter, _Alloc>& __rhs) noexcept
  1947. { __lhs.swap(__rhs); }
  1948. _GLIBCXX_END_NAMESPACE_CXX11
  1949. // [28.11.2] Function template regex_match
  1950. /**
  1951. * @name Matching, Searching, and Replacing
  1952. */
  1953. ///@{
  1954. /**
  1955. * @brief Determines if there is a match between the regular expression @p e
  1956. * and all of the character sequence [first, last).
  1957. *
  1958. * @param __s Start of the character sequence to match.
  1959. * @param __e One-past-the-end of the character sequence to match.
  1960. * @param __m The match results.
  1961. * @param __re The regular expression.
  1962. * @param __flags Controls how the regular expression is matched.
  1963. *
  1964. * @retval true A match exists.
  1965. * @retval false Otherwise.
  1966. *
  1967. * @throws an exception of type regex_error.
  1968. */
  1969. template<typename _Bi_iter, typename _Alloc,
  1970. typename _Ch_type, typename _Rx_traits>
  1971. inline bool
  1972. regex_match(_Bi_iter __s,
  1973. _Bi_iter __e,
  1974. match_results<_Bi_iter, _Alloc>& __m,
  1975. const basic_regex<_Ch_type, _Rx_traits>& __re,
  1976. regex_constants::match_flag_type __flags
  1977. = regex_constants::match_default)
  1978. {
  1979. return __detail::__regex_algo_impl(__s, __e, __m, __re, __flags,
  1980. __detail::_RegexExecutorPolicy::_S_auto, true);
  1981. }
  1982. /**
  1983. * @brief Indicates if there is a match between the regular expression @p e
  1984. * and all of the character sequence [first, last).
  1985. *
  1986. * @param __first Beginning of the character sequence to match.
  1987. * @param __last One-past-the-end of the character sequence to match.
  1988. * @param __re The regular expression.
  1989. * @param __flags Controls how the regular expression is matched.
  1990. *
  1991. * @retval true A match exists.
  1992. * @retval false Otherwise.
  1993. *
  1994. * @throws an exception of type regex_error.
  1995. */
  1996. template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
  1997. inline bool
  1998. regex_match(_Bi_iter __first, _Bi_iter __last,
  1999. const basic_regex<_Ch_type, _Rx_traits>& __re,
  2000. regex_constants::match_flag_type __flags
  2001. = regex_constants::match_default)
  2002. {
  2003. match_results<_Bi_iter> __what;
  2004. return regex_match(__first, __last, __what, __re, __flags);
  2005. }
  2006. /**
  2007. * @brief Determines if there is a match between the regular expression @p e
  2008. * and a C-style null-terminated string.
  2009. *
  2010. * @param __s The C-style null-terminated string to match.
  2011. * @param __m The match results.
  2012. * @param __re The regular expression.
  2013. * @param __f Controls how the regular expression is matched.
  2014. *
  2015. * @retval true A match exists.
  2016. * @retval false Otherwise.
  2017. *
  2018. * @throws an exception of type regex_error.
  2019. */
  2020. template<typename _Ch_type, typename _Alloc, typename _Rx_traits>
  2021. inline bool
  2022. regex_match(const _Ch_type* __s,
  2023. match_results<const _Ch_type*, _Alloc>& __m,
  2024. const basic_regex<_Ch_type, _Rx_traits>& __re,
  2025. regex_constants::match_flag_type __f
  2026. = regex_constants::match_default)
  2027. { return regex_match(__s, __s + _Rx_traits::length(__s), __m, __re, __f); }
  2028. /**
  2029. * @brief Determines if there is a match between the regular expression @p e
  2030. * and a string.
  2031. *
  2032. * @param __s The string to match.
  2033. * @param __m The match results.
  2034. * @param __re The regular expression.
  2035. * @param __flags Controls how the regular expression is matched.
  2036. *
  2037. * @retval true A match exists.
  2038. * @retval false Otherwise.
  2039. *
  2040. * @throws an exception of type regex_error.
  2041. */
  2042. template<typename _Ch_traits, typename _Ch_alloc,
  2043. typename _Alloc, typename _Ch_type, typename _Rx_traits>
  2044. inline bool
  2045. regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
  2046. match_results<typename basic_string<_Ch_type,
  2047. _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
  2048. const basic_regex<_Ch_type, _Rx_traits>& __re,
  2049. regex_constants::match_flag_type __flags
  2050. = regex_constants::match_default)
  2051. { return regex_match(__s.begin(), __s.end(), __m, __re, __flags); }
  2052. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  2053. // 2329. regex_match() with match_results should forbid temporary strings
  2054. /// Prevent unsafe attempts to get match_results from a temporary string.
  2055. template<typename _Ch_traits, typename _Ch_alloc,
  2056. typename _Alloc, typename _Ch_type, typename _Rx_traits>
  2057. bool
  2058. regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>&&,
  2059. match_results<typename basic_string<_Ch_type,
  2060. _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>&,
  2061. const basic_regex<_Ch_type, _Rx_traits>&,
  2062. regex_constants::match_flag_type
  2063. = regex_constants::match_default) = delete;
  2064. /**
  2065. * @brief Indicates if there is a match between the regular expression @p e
  2066. * and a C-style null-terminated string.
  2067. *
  2068. * @param __s The C-style null-terminated string to match.
  2069. * @param __re The regular expression.
  2070. * @param __f Controls how the regular expression is matched.
  2071. *
  2072. * @retval true A match exists.
  2073. * @retval false Otherwise.
  2074. *
  2075. * @throws an exception of type regex_error.
  2076. */
  2077. template<typename _Ch_type, class _Rx_traits>
  2078. inline bool
  2079. regex_match(const _Ch_type* __s,
  2080. const basic_regex<_Ch_type, _Rx_traits>& __re,
  2081. regex_constants::match_flag_type __f
  2082. = regex_constants::match_default)
  2083. { return regex_match(__s, __s + _Rx_traits::length(__s), __re, __f); }
  2084. /**
  2085. * @brief Indicates if there is a match between the regular expression @p e
  2086. * and a string.
  2087. *
  2088. * @param __s [IN] The string to match.
  2089. * @param __re [IN] The regular expression.
  2090. * @param __flags [IN] Controls how the regular expression is matched.
  2091. *
  2092. * @retval true A match exists.
  2093. * @retval false Otherwise.
  2094. *
  2095. * @throws an exception of type regex_error.
  2096. */
  2097. template<typename _Ch_traits, typename _Str_allocator,
  2098. typename _Ch_type, typename _Rx_traits>
  2099. inline bool
  2100. regex_match(const basic_string<_Ch_type, _Ch_traits, _Str_allocator>& __s,
  2101. const basic_regex<_Ch_type, _Rx_traits>& __re,
  2102. regex_constants::match_flag_type __flags
  2103. = regex_constants::match_default)
  2104. { return regex_match(__s.begin(), __s.end(), __re, __flags); }
  2105. // [7.11.3] Function template regex_search
  2106. /**
  2107. * Searches for a regular expression within a range.
  2108. * @param __s [IN] The start of the string to search.
  2109. * @param __e [IN] One-past-the-end of the string to search.
  2110. * @param __m [OUT] The match results.
  2111. * @param __re [IN] The regular expression to search for.
  2112. * @param __flags [IN] Search policy flags.
  2113. * @retval true A match was found within the string.
  2114. * @retval false No match was found within the string, the content of %m is
  2115. * undefined.
  2116. *
  2117. * @throws an exception of type regex_error.
  2118. */
  2119. template<typename _Bi_iter, typename _Alloc,
  2120. typename _Ch_type, typename _Rx_traits>
  2121. inline bool
  2122. regex_search(_Bi_iter __s, _Bi_iter __e,
  2123. match_results<_Bi_iter, _Alloc>& __m,
  2124. const basic_regex<_Ch_type, _Rx_traits>& __re,
  2125. regex_constants::match_flag_type __flags
  2126. = regex_constants::match_default)
  2127. {
  2128. return __detail::__regex_algo_impl(__s, __e, __m, __re, __flags,
  2129. __detail::_RegexExecutorPolicy::_S_auto, false);
  2130. }
  2131. /**
  2132. * Searches for a regular expression within a range.
  2133. * @param __first [IN] The start of the string to search.
  2134. * @param __last [IN] One-past-the-end of the string to search.
  2135. * @param __re [IN] The regular expression to search for.
  2136. * @param __flags [IN] Search policy flags.
  2137. * @retval true A match was found within the string.
  2138. * @retval false No match was found within the string.
  2139. *
  2140. * @throws an exception of type regex_error.
  2141. */
  2142. template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
  2143. inline bool
  2144. regex_search(_Bi_iter __first, _Bi_iter __last,
  2145. const basic_regex<_Ch_type, _Rx_traits>& __re,
  2146. regex_constants::match_flag_type __flags
  2147. = regex_constants::match_default)
  2148. {
  2149. match_results<_Bi_iter> __what;
  2150. return regex_search(__first, __last, __what, __re, __flags);
  2151. }
  2152. /**
  2153. * @brief Searches for a regular expression within a C-string.
  2154. * @param __s [IN] A C-string to search for the regex.
  2155. * @param __m [OUT] The set of regex matches.
  2156. * @param __e [IN] The regex to search for in @p s.
  2157. * @param __f [IN] The search flags.
  2158. * @retval true A match was found within the string.
  2159. * @retval false No match was found within the string, the content of %m is
  2160. * undefined.
  2161. *
  2162. * @throws an exception of type regex_error.
  2163. */
  2164. template<typename _Ch_type, class _Alloc, class _Rx_traits>
  2165. inline bool
  2166. regex_search(const _Ch_type* __s,
  2167. match_results<const _Ch_type*, _Alloc>& __m,
  2168. const basic_regex<_Ch_type, _Rx_traits>& __e,
  2169. regex_constants::match_flag_type __f
  2170. = regex_constants::match_default)
  2171. { return regex_search(__s, __s + _Rx_traits::length(__s), __m, __e, __f); }
  2172. /**
  2173. * @brief Searches for a regular expression within a C-string.
  2174. * @param __s [IN] The C-string to search.
  2175. * @param __e [IN] The regular expression to search for.
  2176. * @param __f [IN] Search policy flags.
  2177. * @retval true A match was found within the string.
  2178. * @retval false No match was found within the string.
  2179. *
  2180. * @throws an exception of type regex_error.
  2181. */
  2182. template<typename _Ch_type, typename _Rx_traits>
  2183. inline bool
  2184. regex_search(const _Ch_type* __s,
  2185. const basic_regex<_Ch_type, _Rx_traits>& __e,
  2186. regex_constants::match_flag_type __f
  2187. = regex_constants::match_default)
  2188. { return regex_search(__s, __s + _Rx_traits::length(__s), __e, __f); }
  2189. /**
  2190. * @brief Searches for a regular expression within a string.
  2191. * @param __s [IN] The string to search.
  2192. * @param __e [IN] The regular expression to search for.
  2193. * @param __flags [IN] Search policy flags.
  2194. * @retval true A match was found within the string.
  2195. * @retval false No match was found within the string.
  2196. *
  2197. * @throws an exception of type regex_error.
  2198. */
  2199. template<typename _Ch_traits, typename _String_allocator,
  2200. typename _Ch_type, typename _Rx_traits>
  2201. inline bool
  2202. regex_search(const basic_string<_Ch_type, _Ch_traits,
  2203. _String_allocator>& __s,
  2204. const basic_regex<_Ch_type, _Rx_traits>& __e,
  2205. regex_constants::match_flag_type __flags
  2206. = regex_constants::match_default)
  2207. { return regex_search(__s.begin(), __s.end(), __e, __flags); }
  2208. /**
  2209. * @brief Searches for a regular expression within a string.
  2210. * @param __s [IN] A C++ string to search for the regex.
  2211. * @param __m [OUT] The set of regex matches.
  2212. * @param __e [IN] The regex to search for in @p s.
  2213. * @param __f [IN] The search flags.
  2214. * @retval true A match was found within the string.
  2215. * @retval false No match was found within the string, the content of %m is
  2216. * undefined.
  2217. *
  2218. * @throws an exception of type regex_error.
  2219. */
  2220. template<typename _Ch_traits, typename _Ch_alloc,
  2221. typename _Alloc, typename _Ch_type,
  2222. typename _Rx_traits>
  2223. inline bool
  2224. regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
  2225. match_results<typename basic_string<_Ch_type,
  2226. _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
  2227. const basic_regex<_Ch_type, _Rx_traits>& __e,
  2228. regex_constants::match_flag_type __f
  2229. = regex_constants::match_default)
  2230. { return regex_search(__s.begin(), __s.end(), __m, __e, __f); }
  2231. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  2232. // 2329. regex_search() with match_results should forbid temporary strings
  2233. /// Prevent unsafe attempts to get match_results from a temporary string.
  2234. template<typename _Ch_traits, typename _Ch_alloc,
  2235. typename _Alloc, typename _Ch_type,
  2236. typename _Rx_traits>
  2237. bool
  2238. regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>&&,
  2239. match_results<typename basic_string<_Ch_type,
  2240. _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>&,
  2241. const basic_regex<_Ch_type, _Rx_traits>&,
  2242. regex_constants::match_flag_type
  2243. = regex_constants::match_default) = delete;
  2244. // std [28.11.4] Function template regex_replace
  2245. template<typename _Out_iter, typename _Bi_iter,
  2246. typename _Rx_traits, typename _Ch_type>
  2247. _Out_iter
  2248. __regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
  2249. const basic_regex<_Ch_type, _Rx_traits>& __e,
  2250. const _Ch_type* __fmt, size_t __len,
  2251. regex_constants::match_flag_type __flags);
  2252. /**
  2253. * @brief Search for a regular expression within a range for multiple times,
  2254. and replace the matched parts through filling a format string.
  2255. * @param __out [OUT] The output iterator.
  2256. * @param __first [IN] The start of the string to search.
  2257. * @param __last [IN] One-past-the-end of the string to search.
  2258. * @param __e [IN] The regular expression to search for.
  2259. * @param __fmt [IN] The format string.
  2260. * @param __flags [IN] Search and replace policy flags.
  2261. *
  2262. * @returns __out
  2263. * @throws an exception of type regex_error.
  2264. */
  2265. template<typename _Out_iter, typename _Bi_iter,
  2266. typename _Rx_traits, typename _Ch_type,
  2267. typename _St, typename _Sa>
  2268. inline _Out_iter
  2269. regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
  2270. const basic_regex<_Ch_type, _Rx_traits>& __e,
  2271. const basic_string<_Ch_type, _St, _Sa>& __fmt,
  2272. regex_constants::match_flag_type __flags
  2273. = regex_constants::match_default)
  2274. {
  2275. return std::__regex_replace(__out, __first, __last, __e, __fmt.c_str(),
  2276. __fmt.length(), __flags);
  2277. }
  2278. /**
  2279. * @brief Search for a regular expression within a range for multiple times,
  2280. and replace the matched parts through filling a format C-string.
  2281. * @param __out [OUT] The output iterator.
  2282. * @param __first [IN] The start of the string to search.
  2283. * @param __last [IN] One-past-the-end of the string to search.
  2284. * @param __e [IN] The regular expression to search for.
  2285. * @param __fmt [IN] The format C-string.
  2286. * @param __flags [IN] Search and replace policy flags.
  2287. *
  2288. * @returns __out
  2289. * @throws an exception of type regex_error.
  2290. */
  2291. template<typename _Out_iter, typename _Bi_iter,
  2292. typename _Rx_traits, typename _Ch_type>
  2293. _Out_iter
  2294. regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
  2295. const basic_regex<_Ch_type, _Rx_traits>& __e,
  2296. const _Ch_type* __fmt,
  2297. regex_constants::match_flag_type __flags
  2298. = regex_constants::match_default)
  2299. {
  2300. return std::__regex_replace(__out, __first, __last, __e, __fmt,
  2301. char_traits<_Ch_type>::length(__fmt),
  2302. __flags);
  2303. }
  2304. /**
  2305. * @brief Search for a regular expression within a string for multiple times,
  2306. and replace the matched parts through filling a format string.
  2307. * @param __s [IN] The string to search and replace.
  2308. * @param __e [IN] The regular expression to search for.
  2309. * @param __fmt [IN] The format string.
  2310. * @param __flags [IN] Search and replace policy flags.
  2311. *
  2312. * @returns The string after replacing.
  2313. * @throws an exception of type regex_error.
  2314. */
  2315. template<typename _Rx_traits, typename _Ch_type,
  2316. typename _St, typename _Sa, typename _Fst, typename _Fsa>
  2317. inline basic_string<_Ch_type, _St, _Sa>
  2318. regex_replace(const basic_string<_Ch_type, _St, _Sa>& __s,
  2319. const basic_regex<_Ch_type, _Rx_traits>& __e,
  2320. const basic_string<_Ch_type, _Fst, _Fsa>& __fmt,
  2321. regex_constants::match_flag_type __flags
  2322. = regex_constants::match_default)
  2323. {
  2324. basic_string<_Ch_type, _St, _Sa> __result;
  2325. regex_replace(std::back_inserter(__result),
  2326. __s.begin(), __s.end(), __e, __fmt, __flags);
  2327. return __result;
  2328. }
  2329. /**
  2330. * @brief Search for a regular expression within a string for multiple times,
  2331. and replace the matched parts through filling a format C-string.
  2332. * @param __s [IN] The string to search and replace.
  2333. * @param __e [IN] The regular expression to search for.
  2334. * @param __fmt [IN] The format C-string.
  2335. * @param __flags [IN] Search and replace policy flags.
  2336. *
  2337. * @returns The string after replacing.
  2338. * @throws an exception of type regex_error.
  2339. */
  2340. template<typename _Rx_traits, typename _Ch_type,
  2341. typename _St, typename _Sa>
  2342. inline basic_string<_Ch_type, _St, _Sa>
  2343. regex_replace(const basic_string<_Ch_type, _St, _Sa>& __s,
  2344. const basic_regex<_Ch_type, _Rx_traits>& __e,
  2345. const _Ch_type* __fmt,
  2346. regex_constants::match_flag_type __flags
  2347. = regex_constants::match_default)
  2348. {
  2349. basic_string<_Ch_type, _St, _Sa> __result;
  2350. regex_replace(std::back_inserter(__result),
  2351. __s.begin(), __s.end(), __e, __fmt, __flags);
  2352. return __result;
  2353. }
  2354. /**
  2355. * @brief Search for a regular expression within a C-string for multiple
  2356. times, and replace the matched parts through filling a format string.
  2357. * @param __s [IN] The C-string to search and replace.
  2358. * @param __e [IN] The regular expression to search for.
  2359. * @param __fmt [IN] The format string.
  2360. * @param __flags [IN] Search and replace policy flags.
  2361. *
  2362. * @returns The string after replacing.
  2363. * @throws an exception of type regex_error.
  2364. */
  2365. template<typename _Rx_traits, typename _Ch_type,
  2366. typename _St, typename _Sa>
  2367. inline basic_string<_Ch_type>
  2368. regex_replace(const _Ch_type* __s,
  2369. const basic_regex<_Ch_type, _Rx_traits>& __e,
  2370. const basic_string<_Ch_type, _St, _Sa>& __fmt,
  2371. regex_constants::match_flag_type __flags
  2372. = regex_constants::match_default)
  2373. {
  2374. basic_string<_Ch_type> __result;
  2375. regex_replace(std::back_inserter(__result), __s,
  2376. __s + char_traits<_Ch_type>::length(__s),
  2377. __e, __fmt, __flags);
  2378. return __result;
  2379. }
  2380. /**
  2381. * @brief Search for a regular expression within a C-string for multiple
  2382. times, and replace the matched parts through filling a format C-string.
  2383. * @param __s [IN] The C-string to search and replace.
  2384. * @param __e [IN] The regular expression to search for.
  2385. * @param __fmt [IN] The format C-string.
  2386. * @param __flags [IN] Search and replace policy flags.
  2387. *
  2388. * @returns The string after replacing.
  2389. * @throws an exception of type regex_error.
  2390. */
  2391. template<typename _Rx_traits, typename _Ch_type>
  2392. inline basic_string<_Ch_type>
  2393. regex_replace(const _Ch_type* __s,
  2394. const basic_regex<_Ch_type, _Rx_traits>& __e,
  2395. const _Ch_type* __fmt,
  2396. regex_constants::match_flag_type __flags
  2397. = regex_constants::match_default)
  2398. {
  2399. basic_string<_Ch_type> __result;
  2400. regex_replace(std::back_inserter(__result), __s,
  2401. __s + char_traits<_Ch_type>::length(__s),
  2402. __e, __fmt, __flags);
  2403. return __result;
  2404. }
  2405. ///@}
  2406. _GLIBCXX_BEGIN_NAMESPACE_CXX11
  2407. // std [28.12] Class template regex_iterator
  2408. /**
  2409. * An iterator adaptor that will provide repeated calls of regex_search over
  2410. * a range until no more matches remain.
  2411. */
  2412. template<typename _Bi_iter,
  2413. typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
  2414. typename _Rx_traits = regex_traits<_Ch_type> >
  2415. class regex_iterator
  2416. {
  2417. public:
  2418. typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
  2419. typedef match_results<_Bi_iter> value_type;
  2420. typedef std::ptrdiff_t difference_type;
  2421. typedef const value_type* pointer;
  2422. typedef const value_type& reference;
  2423. typedef std::forward_iterator_tag iterator_category;
  2424. /**
  2425. * @brief Provides a singular iterator, useful for indicating
  2426. * one-past-the-end of a range.
  2427. */
  2428. regex_iterator() = default;
  2429. /**
  2430. * Constructs a %regex_iterator...
  2431. * @param __a [IN] The start of a text range to search.
  2432. * @param __b [IN] One-past-the-end of the text range to search.
  2433. * @param __re [IN] The regular expression to match.
  2434. * @param __m [IN] Policy flags for match rules.
  2435. */
  2436. regex_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
  2437. regex_constants::match_flag_type __m
  2438. = regex_constants::match_default)
  2439. : _M_begin(__a), _M_end(__b), _M_pregex(&__re), _M_flags(__m), _M_match()
  2440. {
  2441. if (!regex_search(_M_begin, _M_end, _M_match, *_M_pregex, _M_flags))
  2442. *this = regex_iterator();
  2443. }
  2444. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  2445. // 2332. regex_iterator should forbid temporary regexes
  2446. regex_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
  2447. regex_constants::match_flag_type
  2448. = regex_constants::match_default) = delete;
  2449. /// Copy constructs a %regex_iterator.
  2450. regex_iterator(const regex_iterator&) = default;
  2451. /// Copy assigns one %regex_iterator to another.
  2452. regex_iterator&
  2453. operator=(const regex_iterator&) = default;
  2454. ~regex_iterator() = default;
  2455. /**
  2456. * @brief Tests the equivalence of two regex iterators.
  2457. */
  2458. bool
  2459. operator==(const regex_iterator&) const noexcept;
  2460. /**
  2461. * @brief Tests the inequivalence of two regex iterators.
  2462. */
  2463. bool
  2464. operator!=(const regex_iterator& __rhs) const noexcept
  2465. { return !(*this == __rhs); }
  2466. /**
  2467. * @brief Dereferences a %regex_iterator.
  2468. */
  2469. const value_type&
  2470. operator*() const noexcept
  2471. { return _M_match; }
  2472. /**
  2473. * @brief Selects a %regex_iterator member.
  2474. */
  2475. const value_type*
  2476. operator->() const noexcept
  2477. { return &_M_match; }
  2478. /**
  2479. * @brief Increments a %regex_iterator.
  2480. */
  2481. regex_iterator&
  2482. operator++();
  2483. /**
  2484. * @brief Postincrements a %regex_iterator.
  2485. */
  2486. regex_iterator
  2487. operator++(int)
  2488. {
  2489. auto __tmp = *this;
  2490. ++(*this);
  2491. return __tmp;
  2492. }
  2493. private:
  2494. _Bi_iter _M_begin {};
  2495. _Bi_iter _M_end {};
  2496. const regex_type* _M_pregex = nullptr;
  2497. regex_constants::match_flag_type _M_flags {};
  2498. match_results<_Bi_iter> _M_match;
  2499. };
  2500. typedef regex_iterator<const char*> cregex_iterator;
  2501. typedef regex_iterator<string::const_iterator> sregex_iterator;
  2502. #ifdef _GLIBCXX_USE_WCHAR_T
  2503. typedef regex_iterator<const wchar_t*> wcregex_iterator;
  2504. typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
  2505. #endif
  2506. // [7.12.2] Class template regex_token_iterator
  2507. /**
  2508. * Iterates over submatches in a range (or @a splits a text string).
  2509. *
  2510. * The purpose of this iterator is to enumerate all, or all specified,
  2511. * matches of a regular expression within a text range. The dereferenced
  2512. * value of an iterator of this class is a std::sub_match object.
  2513. */
  2514. template<typename _Bi_iter,
  2515. typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
  2516. typename _Rx_traits = regex_traits<_Ch_type> >
  2517. class regex_token_iterator
  2518. {
  2519. public:
  2520. typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
  2521. typedef sub_match<_Bi_iter> value_type;
  2522. typedef std::ptrdiff_t difference_type;
  2523. typedef const value_type* pointer;
  2524. typedef const value_type& reference;
  2525. typedef std::forward_iterator_tag iterator_category;
  2526. public:
  2527. /**
  2528. * @brief Default constructs a %regex_token_iterator.
  2529. *
  2530. * A default-constructed %regex_token_iterator is a singular iterator
  2531. * that will compare equal to the one-past-the-end value for any
  2532. * iterator of the same type.
  2533. */
  2534. regex_token_iterator()
  2535. : _M_position(), _M_subs(), _M_suffix(), _M_n(0), _M_result(nullptr),
  2536. _M_has_m1(false)
  2537. { }
  2538. /**
  2539. * Constructs a %regex_token_iterator...
  2540. * @param __a [IN] The start of the text to search.
  2541. * @param __b [IN] One-past-the-end of the text to search.
  2542. * @param __re [IN] The regular expression to search for.
  2543. * @param __submatch [IN] Which submatch to return. There are some
  2544. * special values for this parameter:
  2545. * - -1 each enumerated subexpression does NOT
  2546. * match the regular expression (aka field
  2547. * splitting)
  2548. * - 0 the entire string matching the
  2549. * subexpression is returned for each match
  2550. * within the text.
  2551. * - >0 enumerates only the indicated
  2552. * subexpression from a match within the text.
  2553. * @param __m [IN] Policy flags for match rules.
  2554. */
  2555. regex_token_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
  2556. int __submatch = 0,
  2557. regex_constants::match_flag_type __m
  2558. = regex_constants::match_default)
  2559. : _M_position(__a, __b, __re, __m), _M_subs(1, __submatch), _M_n(0)
  2560. { _M_init(__a, __b); }
  2561. /**
  2562. * Constructs a %regex_token_iterator...
  2563. * @param __a [IN] The start of the text to search.
  2564. * @param __b [IN] One-past-the-end of the text to search.
  2565. * @param __re [IN] The regular expression to search for.
  2566. * @param __submatches [IN] A list of subexpressions to return for each
  2567. * regular expression match within the text.
  2568. * @param __m [IN] Policy flags for match rules.
  2569. */
  2570. regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
  2571. const regex_type& __re,
  2572. const std::vector<int>& __submatches,
  2573. regex_constants::match_flag_type __m
  2574. = regex_constants::match_default)
  2575. : _M_position(__a, __b, __re, __m), _M_subs(__submatches), _M_n(0)
  2576. { _M_init(__a, __b); }
  2577. /**
  2578. * Constructs a %regex_token_iterator...
  2579. * @param __a [IN] The start of the text to search.
  2580. * @param __b [IN] One-past-the-end of the text to search.
  2581. * @param __re [IN] The regular expression to search for.
  2582. * @param __submatches [IN] A list of subexpressions to return for each
  2583. * regular expression match within the text.
  2584. * @param __m [IN] Policy flags for match rules.
  2585. */
  2586. regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
  2587. const regex_type& __re,
  2588. initializer_list<int> __submatches,
  2589. regex_constants::match_flag_type __m
  2590. = regex_constants::match_default)
  2591. : _M_position(__a, __b, __re, __m), _M_subs(__submatches), _M_n(0)
  2592. { _M_init(__a, __b); }
  2593. /**
  2594. * Constructs a %regex_token_iterator...
  2595. * @param __a [IN] The start of the text to search.
  2596. * @param __b [IN] One-past-the-end of the text to search.
  2597. * @param __re [IN] The regular expression to search for.
  2598. * @param __submatches [IN] A list of subexpressions to return for each
  2599. * regular expression match within the text.
  2600. * @param __m [IN] Policy flags for match rules.
  2601. */
  2602. template<std::size_t _Nm>
  2603. regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
  2604. const regex_type& __re,
  2605. const int (&__submatches)[_Nm],
  2606. regex_constants::match_flag_type __m
  2607. = regex_constants::match_default)
  2608. : _M_position(__a, __b, __re, __m),
  2609. _M_subs(__submatches, __submatches + _Nm), _M_n(0)
  2610. { _M_init(__a, __b); }
  2611. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  2612. // 2332. regex_token_iterator should forbid temporary regexes
  2613. regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&, int = 0,
  2614. regex_constants::match_flag_type =
  2615. regex_constants::match_default) = delete;
  2616. regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
  2617. const std::vector<int>&,
  2618. regex_constants::match_flag_type =
  2619. regex_constants::match_default) = delete;
  2620. regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
  2621. initializer_list<int>,
  2622. regex_constants::match_flag_type =
  2623. regex_constants::match_default) = delete;
  2624. template <std::size_t _Nm>
  2625. regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
  2626. const int (&)[_Nm],
  2627. regex_constants::match_flag_type =
  2628. regex_constants::match_default) = delete;
  2629. /**
  2630. * @brief Copy constructs a %regex_token_iterator.
  2631. * @param __rhs [IN] A %regex_token_iterator to copy.
  2632. */
  2633. regex_token_iterator(const regex_token_iterator& __rhs)
  2634. : _M_position(__rhs._M_position), _M_subs(__rhs._M_subs),
  2635. _M_suffix(__rhs._M_suffix), _M_n(__rhs._M_n), _M_has_m1(__rhs._M_has_m1)
  2636. { _M_normalize_result(); }
  2637. /**
  2638. * @brief Assigns a %regex_token_iterator to another.
  2639. * @param __rhs [IN] A %regex_token_iterator to copy.
  2640. */
  2641. regex_token_iterator&
  2642. operator=(const regex_token_iterator& __rhs);
  2643. /**
  2644. * @brief Compares a %regex_token_iterator to another for equality.
  2645. */
  2646. bool
  2647. operator==(const regex_token_iterator& __rhs) const;
  2648. /**
  2649. * @brief Compares a %regex_token_iterator to another for inequality.
  2650. */
  2651. bool
  2652. operator!=(const regex_token_iterator& __rhs) const
  2653. { return !(*this == __rhs); }
  2654. /**
  2655. * @brief Dereferences a %regex_token_iterator.
  2656. */
  2657. const value_type&
  2658. operator*() const
  2659. { return *_M_result; }
  2660. /**
  2661. * @brief Selects a %regex_token_iterator member.
  2662. */
  2663. const value_type*
  2664. operator->() const
  2665. { return _M_result; }
  2666. /**
  2667. * @brief Increments a %regex_token_iterator.
  2668. */
  2669. regex_token_iterator&
  2670. operator++();
  2671. /**
  2672. * @brief Postincrements a %regex_token_iterator.
  2673. */
  2674. regex_token_iterator
  2675. operator++(int)
  2676. {
  2677. auto __tmp = *this;
  2678. ++(*this);
  2679. return __tmp;
  2680. }
  2681. private:
  2682. typedef regex_iterator<_Bi_iter, _Ch_type, _Rx_traits> _Position;
  2683. void
  2684. _M_init(_Bi_iter __a, _Bi_iter __b);
  2685. const value_type&
  2686. _M_current_match() const
  2687. {
  2688. if (_M_subs[_M_n] == -1)
  2689. return (*_M_position).prefix();
  2690. else
  2691. return (*_M_position)[_M_subs[_M_n]];
  2692. }
  2693. constexpr bool
  2694. _M_end_of_seq() const
  2695. { return _M_result == nullptr; }
  2696. // [28.12.2.2.4]
  2697. void
  2698. _M_normalize_result()
  2699. {
  2700. if (_M_position != _Position())
  2701. _M_result = &_M_current_match();
  2702. else if (_M_has_m1)
  2703. _M_result = &_M_suffix;
  2704. else
  2705. _M_result = nullptr;
  2706. }
  2707. _Position _M_position;
  2708. std::vector<int> _M_subs;
  2709. value_type _M_suffix;
  2710. std::size_t _M_n;
  2711. const value_type* _M_result;
  2712. // Show whether _M_subs contains -1
  2713. bool _M_has_m1;
  2714. };
  2715. /** @brief Token iterator for C-style NULL-terminated strings. */
  2716. typedef regex_token_iterator<const char*> cregex_token_iterator;
  2717. /** @brief Token iterator for standard strings. */
  2718. typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
  2719. #ifdef _GLIBCXX_USE_WCHAR_T
  2720. /** @brief Token iterator for C-style NULL-terminated wide strings. */
  2721. typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
  2722. /** @brief Token iterator for standard wide-character strings. */
  2723. typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
  2724. #endif
  2725. ///@} // group regex
  2726. _GLIBCXX_END_NAMESPACE_CXX11
  2727. _GLIBCXX_END_NAMESPACE_VERSION
  2728. } // namespace
  2729. #include <bits/regex.tcc>