regex.h 95 KB

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