regex.h 94 KB

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