You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2983 lines
100KB

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