/Src/Dependencies/Boost/boost/xpressive/regex_algorithms.hpp

http://hadesmem.googlecode.com/ · C++ Header · 994 lines · 675 code · 114 blank · 205 comment · 114 complexity · 8747b640bac76f4370d6967723fcff61 MD5 · raw file

  1. ///////////////////////////////////////////////////////////////////////////////
  2. /// \file regex_algorithms.hpp
  3. /// Contains the regex_match(), regex_search() and regex_replace() algorithms.
  4. //
  5. // Copyright 2008 Eric Niebler. Distributed under the Boost
  6. // Software License, Version 1.0. (See accompanying file
  7. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. #ifndef BOOST_XPRESSIVE_ALGORITHMS_HPP_EAN_10_04_2005
  9. #define BOOST_XPRESSIVE_ALGORITHMS_HPP_EAN_10_04_2005
  10. // MS compatible compilers support #pragma once
  11. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  12. # pragma once
  13. #endif
  14. #include <string>
  15. #include <iterator>
  16. #include <boost/mpl/or.hpp>
  17. #include <boost/range/end.hpp>
  18. #include <boost/range/begin.hpp>
  19. #include <boost/mpl/identity.hpp>
  20. #include <boost/utility/enable_if.hpp>
  21. #include <boost/type_traits/add_const.hpp>
  22. #include <boost/type_traits/is_pointer.hpp>
  23. #include <boost/type_traits/remove_const.hpp>
  24. #include <boost/xpressive/match_results.hpp>
  25. #include <boost/xpressive/detail/detail_fwd.hpp>
  26. #include <boost/xpressive/detail/core/state.hpp>
  27. #include <boost/xpressive/detail/utility/save_restore.hpp>
  28. /// INTERNAL ONLY
  29. ///
  30. #define BOOST_XPR_NONDEDUCED_TYPE_(x) typename mpl::identity<x>::type
  31. namespace boost { namespace xpressive
  32. {
  33. ///////////////////////////////////////////////////////////////////////////////
  34. // regex_match
  35. ///////////////////////////////////////////////////////////////////////////////
  36. namespace detail
  37. {
  38. ///////////////////////////////////////////////////////////////////////////////
  39. // regex_match_impl
  40. template<typename BidiIter>
  41. inline bool regex_match_impl
  42. (
  43. BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) begin
  44. , BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) end
  45. , match_results<BidiIter> &what
  46. , basic_regex<BidiIter> const &re
  47. , regex_constants::match_flag_type flags = regex_constants::match_default
  48. )
  49. {
  50. typedef detail::core_access<BidiIter> access;
  51. BOOST_ASSERT(0 != re.regex_id());
  52. // the state object holds matching state and
  53. // is passed by reference to all the matchers
  54. detail::match_state<BidiIter> state(begin, end, what, *access::get_regex_impl(re), flags);
  55. state.flags_.match_all_ = true;
  56. state.sub_match(0).begin_ = begin;
  57. if(access::match(re, state))
  58. {
  59. access::set_prefix_suffix(what, begin, end);
  60. return true;
  61. }
  62. // handle partial matches
  63. else if(state.found_partial_match_ && 0 != (flags & regex_constants::match_partial))
  64. {
  65. state.set_partial_match();
  66. return true;
  67. }
  68. access::reset(what);
  69. return false;
  70. }
  71. } // namespace detail
  72. /// \brief See if a regex matches a sequence from beginning to end.
  73. ///
  74. /// Determines whether there is an exact match between the regular expression \c re,
  75. /// and all of the sequence <tt>[begin, end)</tt>.
  76. ///
  77. /// \pre Type \c BidiIter meets the requirements of a Bidirectional Iterator (24.1.4).
  78. /// \pre <tt>[begin,end)</tt> denotes a valid iterator range.
  79. /// \param begin The beginning of the sequence.
  80. /// \param end The end of the sequence.
  81. /// \param what The \c match_results struct into which the sub_matches will be written
  82. /// \param re The regular expression object to use
  83. /// \param flags Optional match flags, used to control how the expression is matched
  84. /// against the sequence. (See \c match_flag_type.)
  85. /// \return \c true if a match is found, \c false otherwise
  86. /// \throw regex_error on stack exhaustion
  87. template<typename BidiIter>
  88. inline bool regex_match
  89. (
  90. BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) begin
  91. , BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) end
  92. , match_results<BidiIter> &what
  93. , basic_regex<BidiIter> const &re
  94. , regex_constants::match_flag_type flags = regex_constants::match_default
  95. )
  96. {
  97. typedef detail::core_access<BidiIter> access;
  98. if(0 == re.regex_id())
  99. {
  100. access::reset(what);
  101. return false;
  102. }
  103. return detail::regex_match_impl(begin, end, what, re, flags);
  104. }
  105. /// \overload
  106. ///
  107. template<typename BidiIter>
  108. inline bool regex_match
  109. (
  110. BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) begin
  111. , BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) end
  112. , basic_regex<BidiIter> const &re
  113. , regex_constants::match_flag_type flags = regex_constants::match_default
  114. )
  115. {
  116. if(0 == re.regex_id())
  117. {
  118. return false;
  119. }
  120. // BUGBUG this is inefficient
  121. match_results<BidiIter> what;
  122. return detail::regex_match_impl(begin, end, what, re, flags);
  123. }
  124. /// \overload
  125. ///
  126. template<typename Char>
  127. inline bool regex_match
  128. (
  129. BOOST_XPR_NONDEDUCED_TYPE_(Char) *begin
  130. , match_results<Char *> &what
  131. , basic_regex<Char *> const &re
  132. , regex_constants::match_flag_type flags = regex_constants::match_default
  133. )
  134. {
  135. typedef detail::core_access<Char *> access;
  136. if(0 == re.regex_id())
  137. {
  138. access::reset(what);
  139. return false;
  140. }
  141. // BUGBUG this is inefficient
  142. typedef typename remove_const<Char>::type char_type;
  143. Char *end = begin + std::char_traits<char_type>::length(begin);
  144. return detail::regex_match_impl(begin, end, what, re, flags);
  145. }
  146. /// \overload
  147. ///
  148. template<typename BidiRange, typename BidiIter>
  149. inline bool regex_match
  150. (
  151. BidiRange &rng
  152. , match_results<BidiIter> &what
  153. , basic_regex<BidiIter> const &re
  154. , regex_constants::match_flag_type flags = regex_constants::match_default
  155. , typename disable_if<detail::is_char_ptr<BidiRange> >::type * = 0
  156. )
  157. {
  158. typedef detail::core_access<BidiIter> access;
  159. if(0 == re.regex_id())
  160. {
  161. access::reset(what);
  162. return false;
  163. }
  164. // Note that the result iterator of the range must be convertible
  165. // to BidiIter here.
  166. BidiIter begin = boost::begin(rng), end = boost::end(rng);
  167. return detail::regex_match_impl(begin, end, what, re, flags);
  168. }
  169. /// \overload
  170. ///
  171. template<typename BidiRange, typename BidiIter>
  172. inline bool regex_match
  173. (
  174. BidiRange const &rng
  175. , match_results<BidiIter> &what
  176. , basic_regex<BidiIter> const &re
  177. , regex_constants::match_flag_type flags = regex_constants::match_default
  178. , typename disable_if<detail::is_char_ptr<BidiRange> >::type * = 0
  179. )
  180. {
  181. typedef detail::core_access<BidiIter> access;
  182. if(0 == re.regex_id())
  183. {
  184. access::reset(what);
  185. return false;
  186. }
  187. // Note that the result iterator of the range must be convertible
  188. // to BidiIter here.
  189. BidiIter begin = boost::begin(rng), end = boost::end(rng);
  190. return detail::regex_match_impl(begin, end, what, re, flags);
  191. }
  192. /// \overload
  193. ///
  194. template<typename Char>
  195. inline bool regex_match
  196. (
  197. BOOST_XPR_NONDEDUCED_TYPE_(Char) *begin
  198. , basic_regex<Char *> const &re
  199. , regex_constants::match_flag_type flags = regex_constants::match_default
  200. )
  201. {
  202. if(0 == re.regex_id())
  203. {
  204. return false;
  205. }
  206. // BUGBUG this is inefficient
  207. match_results<Char *> what;
  208. typedef typename remove_const<Char>::type char_type;
  209. Char *end = begin + std::char_traits<char_type>::length(begin);
  210. return detail::regex_match_impl(begin, end, what, re, flags);
  211. }
  212. /// \overload
  213. ///
  214. template<typename BidiRange, typename BidiIter>
  215. inline bool regex_match
  216. (
  217. BidiRange &rng
  218. , basic_regex<BidiIter> const &re
  219. , regex_constants::match_flag_type flags = regex_constants::match_default
  220. , typename disable_if<detail::is_char_ptr<BidiRange> >::type * = 0
  221. )
  222. {
  223. if(0 == re.regex_id())
  224. {
  225. return false;
  226. }
  227. // BUGBUG this is inefficient
  228. match_results<BidiIter> what;
  229. // Note that the result iterator of the range must be convertible
  230. // to BidiIter here.
  231. BidiIter begin = boost::begin(rng), end = boost::end(rng);
  232. return detail::regex_match_impl(begin, end, what, re, flags);
  233. }
  234. /// \overload
  235. ///
  236. template<typename BidiRange, typename BidiIter>
  237. inline bool regex_match
  238. (
  239. BidiRange const &rng
  240. , basic_regex<BidiIter> const &re
  241. , regex_constants::match_flag_type flags = regex_constants::match_default
  242. , typename disable_if<detail::is_char_ptr<BidiRange> >::type * = 0
  243. )
  244. {
  245. if(0 == re.regex_id())
  246. {
  247. return false;
  248. }
  249. // BUGBUG this is inefficient
  250. match_results<BidiIter> what;
  251. // Note that the result iterator of the range must be convertible
  252. // to BidiIter here.
  253. BidiIter begin = boost::begin(rng), end = boost::end(rng);
  254. return detail::regex_match_impl(begin, end, what, re, flags);
  255. }
  256. ///////////////////////////////////////////////////////////////////////////////
  257. // regex_search
  258. ///////////////////////////////////////////////////////////////////////////////
  259. namespace detail
  260. {
  261. ///////////////////////////////////////////////////////////////////////////////
  262. // regex_search_impl
  263. template<typename BidiIter>
  264. inline bool regex_search_impl
  265. (
  266. match_state<BidiIter> &state
  267. , basic_regex<BidiIter> const &re
  268. , bool not_initial_null = false
  269. )
  270. {
  271. typedef core_access<BidiIter> access;
  272. typedef typename iterator_value<BidiIter>::type char_type;
  273. match_results<BidiIter> &what = *state.context_.results_ptr_;
  274. BOOST_ASSERT(0 != re.regex_id());
  275. bool const partial_ok = state.flags_.match_partial_;
  276. save_restore<bool> not_null(state.flags_.match_not_null_, state.flags_.match_not_null_ || not_initial_null);
  277. state.flags_.match_prev_avail_ = state.flags_.match_prev_avail_ || !state.bos();
  278. regex_impl<BidiIter> const &impl = *access::get_regex_impl(re);
  279. BidiIter const begin = state.cur_, end = state.end_;
  280. BidiIter &sub0begin = state.sub_match(0).begin_;
  281. sub0begin = state.cur_;
  282. // If match_continuous is set, we only need to check for a match at the current position
  283. if(state.flags_.match_continuous_)
  284. {
  285. if(access::match(re, state))
  286. {
  287. access::set_prefix_suffix(what, begin, end);
  288. return true;
  289. }
  290. // handle partial matches
  291. else if(partial_ok && state.found_partial_match_)
  292. {
  293. state.set_partial_match();
  294. return true;
  295. }
  296. }
  297. // If we have a finder, use it to find where a potential match can start
  298. else if(impl.finder_ && (!partial_ok || impl.finder_->ok_for_partial_matches()))
  299. {
  300. finder<BidiIter> const &find = *impl.finder_;
  301. if(find(state))
  302. {
  303. if(state.cur_ != begin)
  304. {
  305. not_null.restore();
  306. }
  307. do
  308. {
  309. sub0begin = state.cur_;
  310. if(access::match(re, state))
  311. {
  312. access::set_prefix_suffix(what, begin, end);
  313. return true;
  314. }
  315. // handle partial matches
  316. else if(partial_ok && state.found_partial_match_)
  317. {
  318. state.set_partial_match();
  319. return true;
  320. }
  321. BOOST_ASSERT(state.cur_ == sub0begin);
  322. not_null.restore();
  323. }
  324. while(state.cur_ != state.end_ && (++state.cur_, find(state)));
  325. }
  326. }
  327. // Otherwise, use brute force search at every position.
  328. else
  329. {
  330. for(;;)
  331. {
  332. if(access::match(re, state))
  333. {
  334. access::set_prefix_suffix(what, begin, end);
  335. return true;
  336. }
  337. // handle partial matches
  338. else if(partial_ok && state.found_partial_match_)
  339. {
  340. state.set_partial_match();
  341. return true;
  342. }
  343. else if(end == sub0begin)
  344. {
  345. break;
  346. }
  347. BOOST_ASSERT(state.cur_ == sub0begin);
  348. state.cur_ = ++sub0begin;
  349. not_null.restore();
  350. }
  351. }
  352. access::reset(what);
  353. return false;
  354. }
  355. } // namespace detail
  356. /// \brief Determines whether there is some sub-sequence within <tt>[begin,end)</tt>
  357. /// that matches the regular expression \c re.
  358. ///
  359. /// Determines whether there is some sub-sequence within <tt>[begin,end)</tt> that matches
  360. /// the regular expression \c re.
  361. ///
  362. /// \pre Type \c BidiIter meets the requirements of a Bidirectional Iterator (24.1.4).
  363. /// \pre <tt>[begin,end)</tt> denotes a valid iterator range.
  364. /// \param begin The beginning of the sequence
  365. /// \param end The end of the sequence
  366. /// \param what The \c match_results struct into which the sub_matches will be written
  367. /// \param re The regular expression object to use
  368. /// \param flags Optional match flags, used to control how the expression is matched against
  369. /// the sequence. (See \c match_flag_type.)
  370. /// \return \c true if a match is found, \c false otherwise
  371. /// \throw regex_error on stack exhaustion
  372. template<typename BidiIter>
  373. inline bool regex_search
  374. (
  375. BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) begin
  376. , BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) end
  377. , match_results<BidiIter> &what
  378. , basic_regex<BidiIter> const &re
  379. , regex_constants::match_flag_type flags = regex_constants::match_default
  380. )
  381. {
  382. typedef detail::core_access<BidiIter> access;
  383. // a default-constructed regex matches nothing
  384. if(0 == re.regex_id())
  385. {
  386. access::reset(what);
  387. return false;
  388. }
  389. // the state object holds matching state and
  390. // is passed by reference to all the matchers
  391. detail::match_state<BidiIter> state(begin, end, what, *access::get_regex_impl(re), flags);
  392. return detail::regex_search_impl(state, re);
  393. }
  394. /// \overload
  395. ///
  396. template<typename BidiIter>
  397. inline bool regex_search
  398. (
  399. BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) begin
  400. , BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) end
  401. , basic_regex<BidiIter> const &re
  402. , regex_constants::match_flag_type flags = regex_constants::match_default
  403. )
  404. {
  405. typedef detail::core_access<BidiIter> access;
  406. // a default-constructed regex matches nothing
  407. if(0 == re.regex_id())
  408. {
  409. return false;
  410. }
  411. // BUGBUG this is inefficient
  412. match_results<BidiIter> what;
  413. // the state object holds matching state and
  414. // is passed by reference to all the matchers
  415. detail::match_state<BidiIter> state(begin, end, what, *access::get_regex_impl(re), flags);
  416. return detail::regex_search_impl(state, re);
  417. }
  418. /// \overload
  419. ///
  420. template<typename Char>
  421. inline bool regex_search
  422. (
  423. BOOST_XPR_NONDEDUCED_TYPE_(Char) *begin
  424. , match_results<Char *> &what
  425. , basic_regex<Char *> const &re
  426. , regex_constants::match_flag_type flags = regex_constants::match_default
  427. )
  428. {
  429. typedef detail::core_access<Char *> access;
  430. // a default-constructed regex matches nothing
  431. if(0 == re.regex_id())
  432. {
  433. access::reset(what);
  434. return false;
  435. }
  436. // BUGBUG this is inefficient
  437. typedef typename remove_const<Char>::type char_type;
  438. Char *end = begin + std::char_traits<char_type>::length(begin);
  439. // the state object holds matching state and
  440. // is passed by reference to all the matchers
  441. detail::match_state<Char *> state(begin, end, what, *access::get_regex_impl(re), flags);
  442. return detail::regex_search_impl(state, re);
  443. }
  444. /// \overload
  445. ///
  446. template<typename BidiRange, typename BidiIter>
  447. inline bool regex_search
  448. (
  449. BidiRange &rng
  450. , match_results<BidiIter> &what
  451. , basic_regex<BidiIter> const &re
  452. , regex_constants::match_flag_type flags = regex_constants::match_default
  453. , typename disable_if<detail::is_char_ptr<BidiRange> >::type * = 0
  454. )
  455. {
  456. typedef detail::core_access<BidiIter> access;
  457. // a default-constructed regex matches nothing
  458. if(0 == re.regex_id())
  459. {
  460. access::reset(what);
  461. return false;
  462. }
  463. // Note that the result iterator of the range must be convertible
  464. // to BidiIter here.
  465. BidiIter begin = boost::begin(rng), end = boost::end(rng);
  466. // the state object holds matching state and
  467. // is passed by reference to all the matchers
  468. detail::match_state<BidiIter> state(begin, end, what, *access::get_regex_impl(re), flags);
  469. return detail::regex_search_impl(state, re);
  470. }
  471. /// \overload
  472. ///
  473. template<typename BidiRange, typename BidiIter>
  474. inline bool regex_search
  475. (
  476. BidiRange const &rng
  477. , match_results<BidiIter> &what
  478. , basic_regex<BidiIter> const &re
  479. , regex_constants::match_flag_type flags = regex_constants::match_default
  480. , typename disable_if<detail::is_char_ptr<BidiRange> >::type * = 0
  481. )
  482. {
  483. typedef detail::core_access<BidiIter> access;
  484. // a default-constructed regex matches nothing
  485. if(0 == re.regex_id())
  486. {
  487. access::reset(what);
  488. return false;
  489. }
  490. // Note that the result iterator of the range must be convertible
  491. // to BidiIter here.
  492. BidiIter begin = boost::begin(rng), end = boost::end(rng);
  493. // the state object holds matching state and
  494. // is passed by reference to all the matchers
  495. detail::match_state<BidiIter> state(begin, end, what, *access::get_regex_impl(re), flags);
  496. return detail::regex_search_impl(state, re);
  497. }
  498. /// \overload
  499. ///
  500. template<typename Char>
  501. inline bool regex_search
  502. (
  503. BOOST_XPR_NONDEDUCED_TYPE_(Char) *begin
  504. , basic_regex<Char *> const &re
  505. , regex_constants::match_flag_type flags = regex_constants::match_default
  506. )
  507. {
  508. typedef detail::core_access<Char *> access;
  509. // a default-constructed regex matches nothing
  510. if(0 == re.regex_id())
  511. {
  512. return false;
  513. }
  514. // BUGBUG this is inefficient
  515. match_results<Char *> what;
  516. // BUGBUG this is inefficient
  517. typedef typename remove_const<Char>::type char_type;
  518. Char *end = begin + std::char_traits<char_type>::length(begin);
  519. // the state object holds matching state and
  520. // is passed by reference to all the matchers
  521. detail::match_state<Char *> state(begin, end, what, *access::get_regex_impl(re), flags);
  522. return detail::regex_search_impl(state, re);
  523. }
  524. /// \overload
  525. ///
  526. template<typename BidiRange, typename BidiIter>
  527. inline bool regex_search
  528. (
  529. BidiRange &rng
  530. , basic_regex<BidiIter> const &re
  531. , regex_constants::match_flag_type flags = regex_constants::match_default
  532. , typename disable_if<detail::is_char_ptr<BidiRange> >::type * = 0
  533. )
  534. {
  535. typedef detail::core_access<BidiIter> access;
  536. // a default-constructed regex matches nothing
  537. if(0 == re.regex_id())
  538. {
  539. return false;
  540. }
  541. // BUGBUG this is inefficient
  542. match_results<BidiIter> what;
  543. // Note that the result iterator of the range must be convertible
  544. // to BidiIter here.
  545. BidiIter begin = boost::begin(rng), end = boost::end(rng);
  546. // the state object holds matching state and
  547. // is passed by reference to all the matchers
  548. detail::match_state<BidiIter> state(begin, end, what, *access::get_regex_impl(re), flags);
  549. return detail::regex_search_impl(state, re);
  550. }
  551. /// \overload
  552. ///
  553. template<typename BidiRange, typename BidiIter>
  554. inline bool regex_search
  555. (
  556. BidiRange const &rng
  557. , basic_regex<BidiIter> const &re
  558. , regex_constants::match_flag_type flags = regex_constants::match_default
  559. , typename disable_if<detail::is_char_ptr<BidiRange> >::type * = 0
  560. )
  561. {
  562. typedef detail::core_access<BidiIter> access;
  563. // a default-constructed regex matches nothing
  564. if(0 == re.regex_id())
  565. {
  566. return false;
  567. }
  568. // BUGBUG this is inefficient
  569. match_results<BidiIter> what;
  570. // Note that the result iterator of the range must be convertible
  571. // to BidiIter here.
  572. BidiIter begin = boost::begin(rng), end = boost::end(rng);
  573. // the state object holds matching state and
  574. // is passed by reference to all the matchers
  575. detail::match_state<BidiIter> state(begin, end, what, *access::get_regex_impl(re), flags);
  576. return detail::regex_search_impl(state, re);
  577. }
  578. ///////////////////////////////////////////////////////////////////////////////
  579. // regex_replace
  580. ///////////////////////////////////////////////////////////////////////////////
  581. namespace detail
  582. {
  583. ///////////////////////////////////////////////////////////////////////////////
  584. // regex_replace_impl
  585. template<typename OutIter, typename BidiIter, typename Formatter>
  586. inline OutIter regex_replace_impl
  587. (
  588. OutIter out
  589. , BidiIter begin
  590. , BidiIter end
  591. , basic_regex<BidiIter> const &re
  592. , Formatter const &format
  593. , regex_constants::match_flag_type flags = regex_constants::match_default
  594. )
  595. {
  596. using namespace regex_constants;
  597. typedef detail::core_access<BidiIter> access;
  598. BOOST_ASSERT(0 != re.regex_id());
  599. BidiIter cur = begin;
  600. match_results<BidiIter> what;
  601. detail::match_state<BidiIter> state(begin, end, what, *access::get_regex_impl(re), flags);
  602. bool const yes_copy = (0 == (flags & format_no_copy));
  603. if(detail::regex_search_impl(state, re))
  604. {
  605. if(yes_copy)
  606. {
  607. out = std::copy(cur, what[0].first, out);
  608. }
  609. out = what.format(out, format, flags);
  610. cur = state.cur_ = state.next_search_ = what[0].second;
  611. if(0 == (flags & format_first_only))
  612. {
  613. bool not_null = (0 == what.length());
  614. state.reset(what, *access::get_regex_impl(re));
  615. while(detail::regex_search_impl(state, re, not_null))
  616. {
  617. if(yes_copy)
  618. {
  619. out = std::copy(cur, what[0].first, out);
  620. }
  621. access::set_prefix_suffix(what, begin, end);
  622. out = what.format(out, format, flags);
  623. cur = state.cur_ = state.next_search_ = what[0].second;
  624. not_null = (0 == what.length());
  625. state.reset(what, *access::get_regex_impl(re));
  626. }
  627. }
  628. }
  629. if(yes_copy)
  630. {
  631. out = std::copy(cur, end, out);
  632. }
  633. return out;
  634. }
  635. } // namespace detail
  636. /// \brief Build an output sequence given an input sequence, a regex, and a format string or
  637. /// a formatter object, function, or expression.
  638. ///
  639. /// Constructs a \c regex_iterator object: <tt>regex_iterator\< BidiIter \> i(begin, end, re, flags)</tt>,
  640. /// and uses \c i to enumerate through all of the matches m of type <tt>match_results\< BidiIter \></tt> that
  641. /// occur within the sequence <tt>[begin, end)</tt>. If no such matches are found and <tt>!(flags \& format_no_copy)</tt>
  642. /// then calls <tt>std::copy(begin, end, out)</tt>. Otherwise, for each match found, if <tt>!(flags \& format_no_copy)</tt>
  643. /// calls <tt>std::copy(m.prefix().first, m.prefix().second, out)</tt>, and then calls <tt>m.format(out, format, flags)</tt>.
  644. /// Finally if <tt>!(flags \& format_no_copy)</tt> calls <tt>std::copy(last_m.suffix().first, last_m.suffix().second, out)</tt>
  645. /// where \c last_m is a copy of the last match found.
  646. ///
  647. /// If <tt>flags \& format_first_only</tt> is non-zero then only the first match found is replaced.
  648. ///
  649. /// \pre Type \c BidiIter meets the requirements of a Bidirectional Iterator (24.1.4).
  650. /// \pre Type \c OutIter meets the requirements of an Output Iterator (24.1.2).
  651. /// \pre Type \c Formatter models \c ForwardRange, <tt>Callable\<match_results\<BidiIter\> \></tt>,
  652. /// <tt>Callable\<match_results\<BidiIter\>, OutIter\></tt>, or
  653. /// <tt>Callable\<match_results\<BidiIter\>, OutIter, regex_constants::match_flag_type\></tt>;
  654. /// or else it is a null-terminated format string, or an expression template
  655. /// representing a formatter lambda expression.
  656. /// \pre <tt>[begin,end)</tt> denotes a valid iterator range.
  657. /// \param out An output iterator into which the output sequence is written.
  658. /// \param begin The beginning of the input sequence.
  659. /// \param end The end of the input sequence.
  660. /// \param re The regular expression object to use.
  661. /// \param format The format string used to format the replacement sequence,
  662. /// or a formatter function, function object, or expression.
  663. /// \param flags Optional match flags, used to control how the expression is matched against
  664. /// the sequence. (See \c match_flag_type.)
  665. /// \return The value of the output iterator after the output sequence has been written to it.
  666. /// \throw regex_error on stack exhaustion or invalid format string.
  667. template<typename OutIter, typename BidiIter, typename Formatter>
  668. inline OutIter regex_replace
  669. (
  670. OutIter out
  671. , BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) begin
  672. , BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) end
  673. , basic_regex<BidiIter> const &re
  674. , Formatter const &format
  675. , regex_constants::match_flag_type flags = regex_constants::match_default
  676. , typename disable_if<detail::is_char_ptr<Formatter> >::type * = 0
  677. )
  678. {
  679. // Default-constructed regexes match nothing
  680. if(0 == re.regex_id())
  681. {
  682. if((0 == (flags & regex_constants::format_no_copy)))
  683. {
  684. out = std::copy(begin, end, out);
  685. }
  686. return out;
  687. }
  688. return detail::regex_replace_impl(out, begin, end, re, format, flags);
  689. }
  690. /// \overload
  691. ///
  692. template<typename OutIter, typename BidiIter>
  693. inline OutIter regex_replace
  694. (
  695. OutIter out
  696. , BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) begin
  697. , BOOST_XPR_NONDEDUCED_TYPE_(BidiIter) end
  698. , basic_regex<BidiIter> const &re
  699. , typename iterator_value<BidiIter>::type const *format
  700. , regex_constants::match_flag_type flags = regex_constants::match_default
  701. )
  702. {
  703. // Default-constructed regexes match nothing
  704. if(0 == re.regex_id())
  705. {
  706. if((0 == (flags & regex_constants::format_no_copy)))
  707. {
  708. out = std::copy(begin, end, out);
  709. }
  710. return out;
  711. }
  712. return detail::regex_replace_impl(out, begin, end, re, format, flags);
  713. }
  714. /// \overload
  715. ///
  716. template<typename BidiContainer, typename BidiIter, typename Formatter>
  717. inline BidiContainer regex_replace
  718. (
  719. BidiContainer &str
  720. , basic_regex<BidiIter> const &re
  721. , Formatter const &format
  722. , regex_constants::match_flag_type flags = regex_constants::match_default
  723. , typename disable_if<mpl::or_<detail::is_char_ptr<BidiContainer>, detail::is_char_ptr<Formatter> > >::type * = 0
  724. )
  725. {
  726. BidiContainer result;
  727. // Note that the result iterator of the range must be convertible
  728. // to BidiIter here.
  729. BidiIter begin = boost::begin(str), end = boost::end(str);
  730. // Default-constructed regexes match nothing
  731. if(0 == re.regex_id())
  732. {
  733. if((0 == (flags & regex_constants::format_no_copy)))
  734. {
  735. std::copy(begin, end, std::back_inserter(result));
  736. }
  737. return result;
  738. }
  739. detail::regex_replace_impl(std::back_inserter(result), begin, end, re, format, flags);
  740. return result;
  741. }
  742. /// \overload
  743. ///
  744. template<typename BidiContainer, typename BidiIter, typename Formatter>
  745. inline BidiContainer regex_replace
  746. (
  747. BidiContainer const &str
  748. , basic_regex<BidiIter> const &re
  749. , Formatter const &format
  750. , regex_constants::match_flag_type flags = regex_constants::match_default
  751. , typename disable_if<mpl::or_<detail::is_char_ptr<BidiContainer>, detail::is_char_ptr<Formatter> > >::type * = 0
  752. )
  753. {
  754. BidiContainer result;
  755. // Note that the result iterator of the range must be convertible
  756. // to BidiIter here.
  757. BidiIter begin = boost::begin(str), end = boost::end(str);
  758. // Default-constructed regexes match nothing
  759. if(0 == re.regex_id())
  760. {
  761. if((0 == (flags & regex_constants::format_no_copy)))
  762. {
  763. std::copy(begin, end, std::back_inserter(result));
  764. }
  765. return result;
  766. }
  767. detail::regex_replace_impl(std::back_inserter(result), begin, end, re, format, flags);
  768. return result;
  769. }
  770. /// \overload
  771. ///
  772. template<typename Char, typename Formatter>
  773. inline std::basic_string<typename remove_const<Char>::type> regex_replace
  774. (
  775. BOOST_XPR_NONDEDUCED_TYPE_(Char) *str
  776. , basic_regex<Char *> const &re
  777. , Formatter const &format
  778. , regex_constants::match_flag_type flags = regex_constants::match_default
  779. , typename disable_if<detail::is_char_ptr<Formatter> >::type * = 0
  780. )
  781. {
  782. typedef typename remove_const<Char>::type char_type;
  783. std::basic_string<char_type> result;
  784. // Default-constructed regexes match nothing
  785. if(0 == re.regex_id())
  786. {
  787. if((0 == (flags & regex_constants::format_no_copy)))
  788. {
  789. result = str;
  790. }
  791. return result;
  792. }
  793. Char *end = str + std::char_traits<char_type>::length(str);
  794. detail::regex_replace_impl(std::back_inserter(result), str, end, re, format, flags);
  795. return result;
  796. }
  797. /// \overload
  798. ///
  799. template<typename BidiContainer, typename BidiIter>
  800. inline BidiContainer regex_replace
  801. (
  802. BidiContainer &str
  803. , basic_regex<BidiIter> const &re
  804. , typename iterator_value<BidiIter>::type const *format
  805. , regex_constants::match_flag_type flags = regex_constants::match_default
  806. , typename disable_if<detail::is_char_ptr<BidiContainer> >::type * = 0
  807. )
  808. {
  809. BidiContainer result;
  810. // Note that the result iterator of the range must be convertible
  811. // to BidiIter here.
  812. BidiIter begin = boost::begin(str), end = boost::end(str);
  813. // Default-constructed regexes match nothing
  814. if(0 == re.regex_id())
  815. {
  816. if((0 == (flags & regex_constants::format_no_copy)))
  817. {
  818. std::copy(begin, end, std::back_inserter(result));
  819. }
  820. return result;
  821. }
  822. detail::regex_replace_impl(std::back_inserter(result), begin, end, re, format, flags);
  823. return result;
  824. }
  825. /// \overload
  826. ///
  827. template<typename BidiContainer, typename BidiIter>
  828. inline BidiContainer regex_replace
  829. (
  830. BidiContainer const &str
  831. , basic_regex<BidiIter> const &re
  832. , typename iterator_value<BidiIter>::type const *format
  833. , regex_constants::match_flag_type flags = regex_constants::match_default
  834. , typename disable_if<detail::is_char_ptr<BidiContainer> >::type * = 0
  835. )
  836. {
  837. BidiContainer result;
  838. // Note that the result iterator of the range must be convertible
  839. // to BidiIter here.
  840. BidiIter begin = boost::begin(str), end = boost::end(str);
  841. // Default-constructed regexes match nothing
  842. if(0 == re.regex_id())
  843. {
  844. if((0 == (flags & regex_constants::format_no_copy)))
  845. {
  846. std::copy(begin, end, std::back_inserter(result));
  847. }
  848. return result;
  849. }
  850. detail::regex_replace_impl(std::back_inserter(result), begin, end, re, format, flags);
  851. return result;
  852. }
  853. /// \overload
  854. ///
  855. template<typename Char>
  856. inline std::basic_string<typename remove_const<Char>::type> regex_replace
  857. (
  858. BOOST_XPR_NONDEDUCED_TYPE_(Char) *str
  859. , basic_regex<Char *> const &re
  860. , typename add_const<Char>::type *format
  861. , regex_constants::match_flag_type flags = regex_constants::match_default
  862. )
  863. {
  864. typedef typename remove_const<Char>::type char_type;
  865. std::basic_string<char_type> result;
  866. // Default-constructed regexes match nothing
  867. if(0 == re.regex_id())
  868. {
  869. if((0 == (flags & regex_constants::format_no_copy)))
  870. {
  871. result = str;
  872. }
  873. return result;
  874. }
  875. Char *end = str + std::char_traits<char_type>::length(str);
  876. detail::regex_replace_impl(std::back_inserter(result), str, end, re, format, flags);
  877. return result;
  878. }
  879. }} // namespace boost::xpressive
  880. #endif