perl_matcher_common.hpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /*
  2. *
  3. * Copyright (c) 2002
  4. * John Maddock
  5. *
  6. * Use, modification and distribution are subject to the
  7. * Boost Software License, Version 1.0. (See accompanying file
  8. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. *
  10. */
  11. /*
  12. * LOCATION: see http://www.boost.org for most recent version.
  13. * FILE perl_matcher_common.cpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Definitions of perl_matcher member functions that are
  16. * common to both the recursive and non-recursive versions.
  17. */
  18. #ifndef BOOST_REGEX_V5_PERL_MATCHER_COMMON_HPP
  19. #define BOOST_REGEX_V5_PERL_MATCHER_COMMON_HPP
  20. #include <boost/regex/config.hpp>
  21. #ifndef BOOST_REGEX_STANDALONE
  22. #include <boost/config.hpp>
  23. #if defined(BOOST_HAS_PRAGMA_ONCE)
  24. #pragma once
  25. #include <boost/regex/v5/perl_matcher.hpp>
  26. #endif
  27. #endif
  28. #include <boost/regex/v5/basic_regex.hpp>
  29. #include <boost/regex/v5/match_flags.hpp>
  30. #include <boost/regex/v5/match_results.hpp>
  31. #ifdef BOOST_REGEX_MSVC
  32. # pragma warning(push)
  33. #pragma warning(disable:4459)
  34. #if BOOST_REGEX_MSVC < 1910
  35. #pragma warning(disable:4800)
  36. #endif
  37. #endif
  38. namespace boost{
  39. namespace BOOST_REGEX_DETAIL_NS{
  40. #ifdef BOOST_REGEX_MSVC
  41. # pragma warning(push)
  42. #pragma warning(disable:26812)
  43. #endif
  44. template <class BidiIterator, class Allocator, class traits>
  45. void perl_matcher<BidiIterator, Allocator, traits>::construct_init(const basic_regex<char_type, traits>& e, match_flag_type f)
  46. {
  47. typedef typename std::iterator_traits<BidiIterator>::iterator_category category;
  48. typedef typename basic_regex<char_type, traits>::flag_type expression_flag_type;
  49. if(e.empty())
  50. {
  51. // precondition failure: e is not a valid regex.
  52. BOOST_REGEX_DETAIL_THROW(std::invalid_argument("Invalid regular expression object"));
  53. }
  54. pstate = 0;
  55. m_match_flags = f;
  56. estimate_max_state_count(static_cast<category*>(0));
  57. expression_flag_type re_f = re.flags();
  58. icase = re_f & regex_constants::icase;
  59. if(!(m_match_flags & (match_perl|match_posix)))
  60. {
  61. if((re_f & (regbase::main_option_type|regbase::no_perl_ex)) == 0)
  62. m_match_flags |= match_perl;
  63. else if((re_f & (regbase::main_option_type|regbase::emacs_ex)) == (regbase::basic_syntax_group|regbase::emacs_ex))
  64. m_match_flags |= match_perl;
  65. else if((re_f & (regbase::main_option_type|regbase::literal)) == (regbase::literal))
  66. m_match_flags |= match_perl;
  67. else
  68. m_match_flags |= match_posix;
  69. }
  70. if(m_match_flags & match_posix)
  71. {
  72. m_temp_match.reset(new match_results<BidiIterator, Allocator>());
  73. m_presult = m_temp_match.get();
  74. }
  75. else
  76. m_presult = &m_result;
  77. m_stack_base = 0;
  78. m_backup_state = 0;
  79. // find the value to use for matching word boundaries:
  80. m_word_mask = re.get_data().m_word_mask;
  81. // find bitmask to use for matching '.':
  82. match_any_mask = static_cast<unsigned char>((f & match_not_dot_newline) ? BOOST_REGEX_DETAIL_NS::test_not_newline : BOOST_REGEX_DETAIL_NS::test_newline);
  83. // Disable match_any if requested in the state machine:
  84. if(e.get_data().m_disable_match_any)
  85. {
  86. if (m_match_flags & match_posix)
  87. BOOST_REGEX_DETAIL_THROW(std::logic_error("Invalid regex for POSIX-style matching"));
  88. m_match_flags &= regex_constants::match_not_any;
  89. }
  90. }
  91. #ifdef BOOST_REGEX_MSVC
  92. # pragma warning(pop)
  93. #endif
  94. template <class BidiIterator, class Allocator, class traits>
  95. void perl_matcher<BidiIterator, Allocator, traits>::estimate_max_state_count(std::random_access_iterator_tag*)
  96. {
  97. //
  98. // How many states should we allow our machine to visit before giving up?
  99. // This is a heuristic: it takes the greater of O(N^2) and O(NS^2)
  100. // where N is the length of the string, and S is the number of states
  101. // in the machine. It's tempting to up this to O(N^2S) or even O(N^2S^2)
  102. // but these take unreasonably amounts of time to bale out in pathological
  103. // cases.
  104. //
  105. // Calculate NS^2 first:
  106. //
  107. static const std::ptrdiff_t k = 100000;
  108. std::ptrdiff_t dist = std::distance(base, last);
  109. if(dist == 0)
  110. dist = 1;
  111. std::ptrdiff_t states = re.size();
  112. if(states == 0)
  113. states = 1;
  114. if ((std::numeric_limits<std::ptrdiff_t>::max)() / states < states)
  115. {
  116. max_state_count = (std::min)((std::ptrdiff_t)BOOST_REGEX_MAX_STATE_COUNT, (std::numeric_limits<std::ptrdiff_t>::max)() - 2);
  117. return;
  118. }
  119. states *= states;
  120. if((std::numeric_limits<std::ptrdiff_t>::max)() / dist < states)
  121. {
  122. max_state_count = (std::min)((std::ptrdiff_t)BOOST_REGEX_MAX_STATE_COUNT, (std::numeric_limits<std::ptrdiff_t>::max)() - 2);
  123. return;
  124. }
  125. states *= dist;
  126. if((std::numeric_limits<std::ptrdiff_t>::max)() - k < states)
  127. {
  128. max_state_count = (std::min)((std::ptrdiff_t)BOOST_REGEX_MAX_STATE_COUNT, (std::numeric_limits<std::ptrdiff_t>::max)() - 2);
  129. return;
  130. }
  131. states += k;
  132. max_state_count = states;
  133. //
  134. // Now calculate N^2:
  135. //
  136. states = dist;
  137. if((std::numeric_limits<std::ptrdiff_t>::max)() / dist < states)
  138. {
  139. max_state_count = (std::min)((std::ptrdiff_t)BOOST_REGEX_MAX_STATE_COUNT, (std::numeric_limits<std::ptrdiff_t>::max)() - 2);
  140. return;
  141. }
  142. states *= dist;
  143. if((std::numeric_limits<std::ptrdiff_t>::max)() - k < states)
  144. {
  145. max_state_count = (std::min)((std::ptrdiff_t)BOOST_REGEX_MAX_STATE_COUNT, (std::numeric_limits<std::ptrdiff_t>::max)() - 2);
  146. return;
  147. }
  148. states += k;
  149. //
  150. // N^2 can be a very large number indeed, to prevent things getting out
  151. // of control, cap the max states:
  152. //
  153. if(states > BOOST_REGEX_MAX_STATE_COUNT)
  154. states = BOOST_REGEX_MAX_STATE_COUNT;
  155. //
  156. // If (the possibly capped) N^2 is larger than our first estimate,
  157. // use this instead:
  158. //
  159. if(states > max_state_count)
  160. max_state_count = states;
  161. }
  162. template <class BidiIterator, class Allocator, class traits>
  163. inline void perl_matcher<BidiIterator, Allocator, traits>::estimate_max_state_count(void*)
  164. {
  165. // we don't know how long the sequence is:
  166. max_state_count = BOOST_REGEX_MAX_STATE_COUNT;
  167. }
  168. template <class BidiIterator, class Allocator, class traits>
  169. inline bool perl_matcher<BidiIterator, Allocator, traits>::match()
  170. {
  171. return match_imp();
  172. }
  173. template <class BidiIterator, class Allocator, class traits>
  174. bool perl_matcher<BidiIterator, Allocator, traits>::match_imp()
  175. {
  176. // initialise our stack if we are non-recursive:
  177. save_state_init init(&m_stack_base, &m_backup_state);
  178. used_block_count = BOOST_REGEX_MAX_BLOCKS;
  179. #if !defined(BOOST_NO_EXCEPTIONS)
  180. try{
  181. #endif
  182. // reset our state machine:
  183. position = base;
  184. search_base = base;
  185. state_count = 0;
  186. m_match_flags |= regex_constants::match_all;
  187. m_presult->set_size((m_match_flags & match_nosubs) ? 1u : static_cast<typename results_type::size_type>(1u + re.mark_count()), search_base, last);
  188. m_presult->set_base(base);
  189. m_presult->set_named_subs(this->re.get_named_subs());
  190. if(m_match_flags & match_posix)
  191. m_result = *m_presult;
  192. verify_options(re.flags(), m_match_flags);
  193. if(0 == match_prefix())
  194. return false;
  195. return (m_result[0].second == last) && (m_result[0].first == base);
  196. #if !defined(BOOST_NO_EXCEPTIONS)
  197. }
  198. catch(...)
  199. {
  200. // unwind all pushed states, apart from anything else this
  201. // ensures that all the states are correctly destructed
  202. // not just the memory freed.
  203. while(unwind(true)){}
  204. throw;
  205. }
  206. #endif
  207. }
  208. template <class BidiIterator, class Allocator, class traits>
  209. inline bool perl_matcher<BidiIterator, Allocator, traits>::find()
  210. {
  211. return find_imp();
  212. }
  213. template <class BidiIterator, class Allocator, class traits>
  214. bool perl_matcher<BidiIterator, Allocator, traits>::find_imp()
  215. {
  216. static matcher_proc_type const s_find_vtable[7] =
  217. {
  218. &perl_matcher<BidiIterator, Allocator, traits>::find_restart_any,
  219. &perl_matcher<BidiIterator, Allocator, traits>::find_restart_word,
  220. &perl_matcher<BidiIterator, Allocator, traits>::find_restart_line,
  221. &perl_matcher<BidiIterator, Allocator, traits>::find_restart_buf,
  222. &perl_matcher<BidiIterator, Allocator, traits>::match_prefix,
  223. &perl_matcher<BidiIterator, Allocator, traits>::find_restart_lit,
  224. &perl_matcher<BidiIterator, Allocator, traits>::find_restart_lit,
  225. };
  226. // initialise our stack if we are non-recursive:
  227. save_state_init init(&m_stack_base, &m_backup_state);
  228. used_block_count = BOOST_REGEX_MAX_BLOCKS;
  229. #if !defined(BOOST_NO_EXCEPTIONS)
  230. try{
  231. #endif
  232. state_count = 0;
  233. if((m_match_flags & regex_constants::match_init) == 0)
  234. {
  235. // reset our state machine:
  236. search_base = position = base;
  237. pstate = re.get_first_state();
  238. m_presult->set_size((m_match_flags & match_nosubs) ? 1u : static_cast<typename results_type::size_type>(1u + re.mark_count()), base, last);
  239. m_presult->set_base(base);
  240. m_presult->set_named_subs(this->re.get_named_subs());
  241. m_match_flags |= regex_constants::match_init;
  242. }
  243. else
  244. {
  245. // start again:
  246. search_base = position = m_result[0].second;
  247. // If last match was null and match_not_null was not set then increment
  248. // our start position, otherwise we go into an infinite loop:
  249. if(((m_match_flags & match_not_null) == 0) && (m_result.length() == 0))
  250. {
  251. if(position == last)
  252. return false;
  253. else
  254. ++position;
  255. }
  256. // reset $` start:
  257. m_presult->set_size((m_match_flags & match_nosubs) ? 1u : static_cast<typename results_type::size_type>(1u + re.mark_count()), search_base, last);
  258. //if((base != search_base) && (base == backstop))
  259. // m_match_flags |= match_prev_avail;
  260. }
  261. if(m_match_flags & match_posix)
  262. {
  263. m_result.set_size(static_cast<typename results_type::size_type>(1u + re.mark_count()), base, last);
  264. m_result.set_base(base);
  265. }
  266. verify_options(re.flags(), m_match_flags);
  267. // find out what kind of expression we have:
  268. unsigned type = (m_match_flags & match_continuous) ?
  269. static_cast<unsigned int>(regbase::restart_continue)
  270. : static_cast<unsigned int>(re.get_restart_type());
  271. // call the appropriate search routine:
  272. matcher_proc_type proc = s_find_vtable[type];
  273. return (this->*proc)();
  274. #if !defined(BOOST_NO_EXCEPTIONS)
  275. }
  276. catch(...)
  277. {
  278. // unwind all pushed states, apart from anything else this
  279. // ensures that all the states are correctly destructed
  280. // not just the memory freed.
  281. while(unwind(true)){}
  282. throw;
  283. }
  284. #endif
  285. }
  286. template <class BidiIterator, class Allocator, class traits>
  287. bool perl_matcher<BidiIterator, Allocator, traits>::match_prefix()
  288. {
  289. m_has_partial_match = false;
  290. m_has_found_match = false;
  291. pstate = re.get_first_state();
  292. m_presult->set_first(position);
  293. restart = position;
  294. match_all_states();
  295. if(!m_has_found_match && m_has_partial_match && (m_match_flags & match_partial))
  296. {
  297. m_has_found_match = true;
  298. m_presult->set_second(last, 0, false);
  299. position = last;
  300. if((m_match_flags & match_posix) == match_posix)
  301. {
  302. m_result.maybe_assign(*m_presult);
  303. }
  304. }
  305. #ifdef BOOST_REGEX_MATCH_EXTRA
  306. if(m_has_found_match && (match_extra & m_match_flags))
  307. {
  308. //
  309. // we have a match, reverse the capture information:
  310. //
  311. for(unsigned i = 0; i < m_presult->size(); ++i)
  312. {
  313. typename sub_match<BidiIterator>::capture_sequence_type & seq = ((*m_presult)[i]).get_captures();
  314. std::reverse(seq.begin(), seq.end());
  315. }
  316. }
  317. #endif
  318. if(!m_has_found_match)
  319. position = restart; // reset search postion
  320. return m_has_found_match;
  321. }
  322. template <class BidiIterator, class Allocator, class traits>
  323. bool perl_matcher<BidiIterator, Allocator, traits>::match_literal()
  324. {
  325. unsigned int len = static_cast<const re_literal*>(pstate)->length;
  326. const char_type* what = reinterpret_cast<const char_type*>(static_cast<const re_literal*>(pstate) + 1);
  327. //
  328. // compare string with what we stored in
  329. // our records:
  330. for(unsigned int i = 0; i < len; ++i, ++position)
  331. {
  332. if((position == last) || (traits_inst.translate(*position, icase) != what[i]))
  333. return false;
  334. }
  335. pstate = pstate->next.p;
  336. return true;
  337. }
  338. template <class BidiIterator, class Allocator, class traits>
  339. bool perl_matcher<BidiIterator, Allocator, traits>::match_start_line()
  340. {
  341. if(position == backstop)
  342. {
  343. if((m_match_flags & match_prev_avail) == 0)
  344. {
  345. if((m_match_flags & match_not_bol) == 0)
  346. {
  347. pstate = pstate->next.p;
  348. return true;
  349. }
  350. return false;
  351. }
  352. }
  353. else if(m_match_flags & match_single_line)
  354. return false;
  355. // check the previous value character:
  356. BidiIterator t(position);
  357. --t;
  358. if(position != last)
  359. {
  360. if(is_separator(*t) && !((*t == static_cast<char_type>('\r')) && (*position == static_cast<char_type>('\n'))) )
  361. {
  362. pstate = pstate->next.p;
  363. return true;
  364. }
  365. }
  366. else if(is_separator(*t))
  367. {
  368. pstate = pstate->next.p;
  369. return true;
  370. }
  371. return false;
  372. }
  373. template <class BidiIterator, class Allocator, class traits>
  374. bool perl_matcher<BidiIterator, Allocator, traits>::match_end_line()
  375. {
  376. if(position != last)
  377. {
  378. if(m_match_flags & match_single_line)
  379. return false;
  380. // we're not yet at the end so *first is always valid:
  381. if(is_separator(*position))
  382. {
  383. if((position != backstop) || (m_match_flags & match_prev_avail))
  384. {
  385. // check that we're not in the middle of \r\n sequence
  386. BidiIterator t(position);
  387. --t;
  388. if((*t == static_cast<char_type>('\r')) && (*position == static_cast<char_type>('\n')))
  389. {
  390. return false;
  391. }
  392. }
  393. pstate = pstate->next.p;
  394. return true;
  395. }
  396. }
  397. else if((m_match_flags & match_not_eol) == 0)
  398. {
  399. pstate = pstate->next.p;
  400. return true;
  401. }
  402. return false;
  403. }
  404. template <class BidiIterator, class Allocator, class traits>
  405. bool perl_matcher<BidiIterator, Allocator, traits>::match_wild()
  406. {
  407. if(position == last)
  408. return false;
  409. if(is_separator(*position) && ((match_any_mask & static_cast<const re_dot*>(pstate)->mask) == 0))
  410. return false;
  411. if((*position == char_type(0)) && (m_match_flags & match_not_dot_null))
  412. return false;
  413. pstate = pstate->next.p;
  414. ++position;
  415. return true;
  416. }
  417. template <class BidiIterator, class Allocator, class traits>
  418. bool perl_matcher<BidiIterator, Allocator, traits>::match_word_boundary()
  419. {
  420. bool b; // indcates whether next character is a word character
  421. if(position != last)
  422. {
  423. // prev and this character must be opposites:
  424. b = traits_inst.isctype(*position, m_word_mask);
  425. }
  426. else
  427. {
  428. if (m_match_flags & match_not_eow)
  429. return false;
  430. b = false;
  431. }
  432. if((position == backstop) && ((m_match_flags & match_prev_avail) == 0))
  433. {
  434. if(m_match_flags & match_not_bow)
  435. return false;
  436. else
  437. b ^= false;
  438. }
  439. else
  440. {
  441. --position;
  442. b ^= traits_inst.isctype(*position, m_word_mask);
  443. ++position;
  444. }
  445. if(b)
  446. {
  447. pstate = pstate->next.p;
  448. return true;
  449. }
  450. return false; // no match if we get to here...
  451. }
  452. template <class BidiIterator, class Allocator, class traits>
  453. bool perl_matcher<BidiIterator, Allocator, traits>::match_within_word()
  454. {
  455. bool b = !match_word_boundary();
  456. if(b)
  457. pstate = pstate->next.p;
  458. return b;
  459. /*
  460. if(position == last)
  461. return false;
  462. // both prev and this character must be m_word_mask:
  463. bool prev = traits_inst.isctype(*position, m_word_mask);
  464. {
  465. bool b;
  466. if((position == backstop) && ((m_match_flags & match_prev_avail) == 0))
  467. return false;
  468. else
  469. {
  470. --position;
  471. b = traits_inst.isctype(*position, m_word_mask);
  472. ++position;
  473. }
  474. if(b == prev)
  475. {
  476. pstate = pstate->next.p;
  477. return true;
  478. }
  479. }
  480. return false;
  481. */
  482. }
  483. template <class BidiIterator, class Allocator, class traits>
  484. bool perl_matcher<BidiIterator, Allocator, traits>::match_word_start()
  485. {
  486. if(position == last)
  487. return false; // can't be starting a word if we're already at the end of input
  488. if(!traits_inst.isctype(*position, m_word_mask))
  489. return false; // next character isn't a word character
  490. if((position == backstop) && ((m_match_flags & match_prev_avail) == 0))
  491. {
  492. if(m_match_flags & match_not_bow)
  493. return false; // no previous input
  494. }
  495. else
  496. {
  497. // otherwise inside buffer:
  498. BidiIterator t(position);
  499. --t;
  500. if(traits_inst.isctype(*t, m_word_mask))
  501. return false; // previous character not non-word
  502. }
  503. // OK we have a match:
  504. pstate = pstate->next.p;
  505. return true;
  506. }
  507. template <class BidiIterator, class Allocator, class traits>
  508. bool perl_matcher<BidiIterator, Allocator, traits>::match_word_end()
  509. {
  510. if((position == backstop) && ((m_match_flags & match_prev_avail) == 0))
  511. return false; // start of buffer can't be end of word
  512. BidiIterator t(position);
  513. --t;
  514. if(traits_inst.isctype(*t, m_word_mask) == false)
  515. return false; // previous character wasn't a word character
  516. if(position == last)
  517. {
  518. if(m_match_flags & match_not_eow)
  519. return false; // end of buffer but not end of word
  520. }
  521. else
  522. {
  523. // otherwise inside buffer:
  524. if(traits_inst.isctype(*position, m_word_mask))
  525. return false; // next character is a word character
  526. }
  527. pstate = pstate->next.p;
  528. return true; // if we fall through to here then we've succeeded
  529. }
  530. template <class BidiIterator, class Allocator, class traits>
  531. bool perl_matcher<BidiIterator, Allocator, traits>::match_buffer_start()
  532. {
  533. if((position != backstop) || (m_match_flags & match_not_bob))
  534. return false;
  535. // OK match:
  536. pstate = pstate->next.p;
  537. return true;
  538. }
  539. template <class BidiIterator, class Allocator, class traits>
  540. bool perl_matcher<BidiIterator, Allocator, traits>::match_buffer_end()
  541. {
  542. if((position != last) || (m_match_flags & match_not_eob))
  543. return false;
  544. // OK match:
  545. pstate = pstate->next.p;
  546. return true;
  547. }
  548. template <class BidiIterator, class Allocator, class traits>
  549. bool perl_matcher<BidiIterator, Allocator, traits>::match_backref()
  550. {
  551. //
  552. // Compare with what we previously matched.
  553. // Note that this succeeds if the backref did not partisipate
  554. // in the match, this is in line with ECMAScript, but not Perl
  555. // or PCRE.
  556. //
  557. int index = static_cast<const re_brace*>(pstate)->index;
  558. if(index >= hash_value_mask)
  559. {
  560. named_subexpressions::range_type r = re.get_data().equal_range(index);
  561. BOOST_REGEX_ASSERT(r.first != r.second);
  562. do
  563. {
  564. index = r.first->index;
  565. ++r.first;
  566. }while((r.first != r.second) && ((*m_presult)[index].matched != true));
  567. }
  568. if((m_match_flags & match_perl) && !(*m_presult)[index].matched)
  569. return false;
  570. BidiIterator i = (*m_presult)[index].first;
  571. BidiIterator j = (*m_presult)[index].second;
  572. while(i != j)
  573. {
  574. if((position == last) || (traits_inst.translate(*position, icase) != traits_inst.translate(*i, icase)))
  575. return false;
  576. ++i;
  577. ++position;
  578. }
  579. pstate = pstate->next.p;
  580. return true;
  581. }
  582. template <class BidiIterator, class Allocator, class traits>
  583. bool perl_matcher<BidiIterator, Allocator, traits>::match_long_set()
  584. {
  585. typedef typename traits::char_class_type char_class_type;
  586. // let the traits class do the work:
  587. if(position == last)
  588. return false;
  589. BidiIterator t = re_is_set_member(position, last, static_cast<const re_set_long<char_class_type>*>(pstate), re.get_data(), icase);
  590. if(t != position)
  591. {
  592. pstate = pstate->next.p;
  593. position = t;
  594. return true;
  595. }
  596. return false;
  597. }
  598. template <class BidiIterator, class Allocator, class traits>
  599. bool perl_matcher<BidiIterator, Allocator, traits>::match_set()
  600. {
  601. if(position == last)
  602. return false;
  603. if(static_cast<const re_set*>(pstate)->_map[static_cast<unsigned char>(traits_inst.translate(*position, icase))])
  604. {
  605. pstate = pstate->next.p;
  606. ++position;
  607. return true;
  608. }
  609. return false;
  610. }
  611. template <class BidiIterator, class Allocator, class traits>
  612. bool perl_matcher<BidiIterator, Allocator, traits>::match_jump()
  613. {
  614. pstate = static_cast<const re_jump*>(pstate)->alt.p;
  615. return true;
  616. }
  617. template <class BidiIterator, class Allocator, class traits>
  618. bool perl_matcher<BidiIterator, Allocator, traits>::match_combining()
  619. {
  620. if(position == last)
  621. return false;
  622. if(is_combining(traits_inst.translate(*position, icase)))
  623. return false;
  624. ++position;
  625. while((position != last) && is_combining(traits_inst.translate(*position, icase)))
  626. ++position;
  627. pstate = pstate->next.p;
  628. return true;
  629. }
  630. template <class BidiIterator, class Allocator, class traits>
  631. bool perl_matcher<BidiIterator, Allocator, traits>::match_soft_buffer_end()
  632. {
  633. if(m_match_flags & match_not_eob)
  634. return false;
  635. BidiIterator p(position);
  636. while((p != last) && is_separator(traits_inst.translate(*p, icase)))++p;
  637. if(p != last)
  638. return false;
  639. pstate = pstate->next.p;
  640. return true;
  641. }
  642. template <class BidiIterator, class Allocator, class traits>
  643. bool perl_matcher<BidiIterator, Allocator, traits>::match_restart_continue()
  644. {
  645. if(position == search_base)
  646. {
  647. pstate = pstate->next.p;
  648. return true;
  649. }
  650. return false;
  651. }
  652. template <class BidiIterator, class Allocator, class traits>
  653. bool perl_matcher<BidiIterator, Allocator, traits>::match_backstep()
  654. {
  655. #ifdef BOOST_REGEX_MSVC
  656. #pragma warning(push)
  657. #pragma warning(disable:4127)
  658. #endif
  659. if( ::boost::is_random_access_iterator<BidiIterator>::value)
  660. {
  661. std::ptrdiff_t maxlen = std::distance(backstop, position);
  662. if(maxlen < static_cast<const re_brace*>(pstate)->index)
  663. return false;
  664. std::advance(position, -static_cast<const re_brace*>(pstate)->index);
  665. }
  666. else
  667. {
  668. int c = static_cast<const re_brace*>(pstate)->index;
  669. while(c--)
  670. {
  671. if(position == backstop)
  672. return false;
  673. --position;
  674. }
  675. }
  676. pstate = pstate->next.p;
  677. return true;
  678. #ifdef BOOST_REGEX_MSVC
  679. #pragma warning(pop)
  680. #endif
  681. }
  682. template <class BidiIterator, class Allocator, class traits>
  683. inline bool perl_matcher<BidiIterator, Allocator, traits>::match_assert_backref()
  684. {
  685. // return true if marked sub-expression N has been matched:
  686. int index = static_cast<const re_brace*>(pstate)->index;
  687. bool result = false;
  688. if(index == 9999)
  689. {
  690. // Magic value for a (DEFINE) block:
  691. return false;
  692. }
  693. else if(index > 0)
  694. {
  695. // Have we matched subexpression "index"?
  696. // Check if index is a hash value:
  697. if(index >= hash_value_mask)
  698. {
  699. named_subexpressions::range_type r = re.get_data().equal_range(index);
  700. while(r.first != r.second)
  701. {
  702. if((*m_presult)[r.first->index].matched)
  703. {
  704. result = true;
  705. break;
  706. }
  707. ++r.first;
  708. }
  709. }
  710. else
  711. {
  712. result = (*m_presult)[index].matched;
  713. }
  714. pstate = pstate->next.p;
  715. }
  716. else
  717. {
  718. // Have we recursed into subexpression "index"?
  719. // If index == 0 then check for any recursion at all, otherwise for recursion to -index-1.
  720. int idx = -(index+1);
  721. if(idx >= hash_value_mask)
  722. {
  723. named_subexpressions::range_type r = re.get_data().equal_range(idx);
  724. int stack_index = recursion_stack.empty() ? -1 : recursion_stack.back().idx;
  725. while(r.first != r.second)
  726. {
  727. result |= (stack_index == r.first->index);
  728. if(result)break;
  729. ++r.first;
  730. }
  731. }
  732. else
  733. {
  734. result = !recursion_stack.empty() && ((recursion_stack.back().idx == idx) || (index == 0));
  735. }
  736. pstate = pstate->next.p;
  737. }
  738. return result;
  739. }
  740. template <class BidiIterator, class Allocator, class traits>
  741. bool perl_matcher<BidiIterator, Allocator, traits>::match_fail()
  742. {
  743. // Just force a backtrack:
  744. return false;
  745. }
  746. template <class BidiIterator, class Allocator, class traits>
  747. bool perl_matcher<BidiIterator, Allocator, traits>::match_accept()
  748. {
  749. if(!recursion_stack.empty())
  750. {
  751. return skip_until_paren(recursion_stack.back().idx);
  752. }
  753. else
  754. {
  755. return skip_until_paren(INT_MAX);
  756. }
  757. }
  758. template <class BidiIterator, class Allocator, class traits>
  759. bool perl_matcher<BidiIterator, Allocator, traits>::find_restart_any()
  760. {
  761. #ifdef BOOST_REGEX_MSVC
  762. #pragma warning(push)
  763. #pragma warning(disable:4127)
  764. #endif
  765. const unsigned char* _map = re.get_map();
  766. while(true)
  767. {
  768. // skip everything we can't match:
  769. while((position != last) && !can_start(*position, _map, (unsigned char)mask_any) )
  770. ++position;
  771. if(position == last)
  772. {
  773. // run out of characters, try a null match if possible:
  774. if(re.can_be_null())
  775. return match_prefix();
  776. break;
  777. }
  778. // now try and obtain a match:
  779. if(match_prefix())
  780. return true;
  781. if(position == last)
  782. return false;
  783. ++position;
  784. }
  785. return false;
  786. #ifdef BOOST_REGEX_MSVC
  787. #pragma warning(pop)
  788. #endif
  789. }
  790. template <class BidiIterator, class Allocator, class traits>
  791. bool perl_matcher<BidiIterator, Allocator, traits>::find_restart_word()
  792. {
  793. #ifdef BOOST_REGEX_MSVC
  794. #pragma warning(push)
  795. #pragma warning(disable:4127)
  796. #endif
  797. // do search optimised for word starts:
  798. const unsigned char* _map = re.get_map();
  799. if((m_match_flags & match_prev_avail) || (position != base))
  800. --position;
  801. else if(match_prefix())
  802. return true;
  803. do
  804. {
  805. while((position != last) && traits_inst.isctype(*position, m_word_mask))
  806. ++position;
  807. while((position != last) && !traits_inst.isctype(*position, m_word_mask))
  808. ++position;
  809. if(position == last)
  810. break;
  811. if(can_start(*position, _map, (unsigned char)mask_any) )
  812. {
  813. if(match_prefix())
  814. return true;
  815. }
  816. if(position == last)
  817. break;
  818. } while(true);
  819. return false;
  820. #ifdef BOOST_REGEX_MSVC
  821. #pragma warning(pop)
  822. #endif
  823. }
  824. template <class BidiIterator, class Allocator, class traits>
  825. bool perl_matcher<BidiIterator, Allocator, traits>::find_restart_line()
  826. {
  827. // do search optimised for line starts:
  828. const unsigned char* _map = re.get_map();
  829. if(match_prefix())
  830. return true;
  831. while(position != last)
  832. {
  833. while((position != last) && !is_separator(*position))
  834. ++position;
  835. if(position == last)
  836. return false;
  837. ++position;
  838. if(position == last)
  839. {
  840. if(re.can_be_null() && match_prefix())
  841. return true;
  842. return false;
  843. }
  844. if( can_start(*position, _map, (unsigned char)mask_any) )
  845. {
  846. if(match_prefix())
  847. return true;
  848. }
  849. if(position == last)
  850. return false;
  851. //++position;
  852. }
  853. return false;
  854. }
  855. template <class BidiIterator, class Allocator, class traits>
  856. bool perl_matcher<BidiIterator, Allocator, traits>::find_restart_buf()
  857. {
  858. if((position == base) && ((m_match_flags & match_not_bob) == 0))
  859. return match_prefix();
  860. return false;
  861. }
  862. template <class BidiIterator, class Allocator, class traits>
  863. bool perl_matcher<BidiIterator, Allocator, traits>::find_restart_lit()
  864. {
  865. return false;
  866. }
  867. } // namespace BOOST_REGEX_DETAIL_NS
  868. } // namespace boost
  869. #ifdef BOOST_REGEX_MSVC
  870. # pragma warning(pop)
  871. #endif
  872. #endif