regex.h 96 KB

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