perl_matcher_non_recursive.hpp 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  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. * specific to the non-recursive implementation.
  17. */
  18. #ifndef BOOST_REGEX_V4_PERL_MATCHER_NON_RECURSIVE_HPP
  19. #define BOOST_REGEX_V4_PERL_MATCHER_NON_RECURSIVE_HPP
  20. #include <new>
  21. #ifdef BOOST_MSVC
  22. #pragma warning(push)
  23. #pragma warning(disable: 4103)
  24. #endif
  25. #ifdef BOOST_HAS_ABI_HEADERS
  26. # include BOOST_ABI_PREFIX
  27. #endif
  28. #ifdef BOOST_MSVC
  29. #pragma warning(pop)
  30. #endif
  31. #ifdef BOOST_MSVC
  32. # pragma warning(push)
  33. # pragma warning(disable: 4800)
  34. #endif
  35. namespace boost{
  36. namespace re_detail{
  37. template <class T>
  38. inline void inplace_destroy(T* p)
  39. {
  40. (void)p; // warning suppression
  41. p->~T();
  42. }
  43. struct saved_state
  44. {
  45. union{
  46. unsigned int state_id;
  47. // this padding ensures correct alignment on 64-bit platforms:
  48. std::size_t padding1;
  49. std::ptrdiff_t padding2;
  50. void* padding3;
  51. };
  52. saved_state(unsigned i) : state_id(i) {}
  53. };
  54. template <class BidiIterator>
  55. struct saved_matched_paren : public saved_state
  56. {
  57. int index;
  58. sub_match<BidiIterator> sub;
  59. saved_matched_paren(int i, const sub_match<BidiIterator>& s) : saved_state(1), index(i), sub(s){};
  60. };
  61. template <class BidiIterator>
  62. struct saved_position : public saved_state
  63. {
  64. const re_syntax_base* pstate;
  65. BidiIterator position;
  66. saved_position(const re_syntax_base* ps, BidiIterator pos, int i) : saved_state(i), pstate(ps), position(pos){};
  67. };
  68. template <class BidiIterator>
  69. struct saved_assertion : public saved_position<BidiIterator>
  70. {
  71. bool positive;
  72. saved_assertion(bool p, const re_syntax_base* ps, BidiIterator pos)
  73. : saved_position<BidiIterator>(ps, pos, saved_type_assertion), positive(p){};
  74. };
  75. template <class BidiIterator>
  76. struct saved_repeater : public saved_state
  77. {
  78. repeater_count<BidiIterator> count;
  79. saved_repeater(int i, repeater_count<BidiIterator>** s, BidiIterator start)
  80. : saved_state(saved_state_repeater_count), count(i,s,start){}
  81. };
  82. struct saved_extra_block : public saved_state
  83. {
  84. saved_state *base, *end;
  85. saved_extra_block(saved_state* b, saved_state* e)
  86. : saved_state(saved_state_extra_block), base(b), end(e) {}
  87. };
  88. struct save_state_init
  89. {
  90. saved_state** stack;
  91. save_state_init(saved_state** base, saved_state** end)
  92. : stack(base)
  93. {
  94. *base = static_cast<saved_state*>(get_mem_block());
  95. *end = reinterpret_cast<saved_state*>(reinterpret_cast<char*>(*base)+BOOST_REGEX_BLOCKSIZE);
  96. --(*end);
  97. (void) new (*end)saved_state(0);
  98. BOOST_ASSERT(*end > *base);
  99. }
  100. ~save_state_init()
  101. {
  102. put_mem_block(*stack);
  103. *stack = 0;
  104. }
  105. };
  106. template <class BidiIterator>
  107. struct saved_single_repeat : public saved_state
  108. {
  109. std::size_t count;
  110. const re_repeat* rep;
  111. BidiIterator last_position;
  112. saved_single_repeat(std::size_t c, const re_repeat* r, BidiIterator lp, int arg_id)
  113. : saved_state(arg_id), count(c), rep(r), last_position(lp){}
  114. };
  115. template <class Results>
  116. struct saved_recursion : public saved_state
  117. {
  118. saved_recursion(int id, const re_syntax_base* p, Results* pr)
  119. : saved_state(14), recursion_id(id), preturn_address(p), results(*pr)
  120. {}
  121. int recursion_id;
  122. const re_syntax_base* preturn_address;
  123. Results results;
  124. };
  125. template <class BidiIterator, class Allocator, class traits>
  126. bool perl_matcher<BidiIterator, Allocator, traits>::match_all_states()
  127. {
  128. static matcher_proc_type const s_match_vtable[30] =
  129. {
  130. (&perl_matcher<BidiIterator, Allocator, traits>::match_startmark),
  131. &perl_matcher<BidiIterator, Allocator, traits>::match_endmark,
  132. &perl_matcher<BidiIterator, Allocator, traits>::match_literal,
  133. &perl_matcher<BidiIterator, Allocator, traits>::match_start_line,
  134. &perl_matcher<BidiIterator, Allocator, traits>::match_end_line,
  135. &perl_matcher<BidiIterator, Allocator, traits>::match_wild,
  136. &perl_matcher<BidiIterator, Allocator, traits>::match_match,
  137. &perl_matcher<BidiIterator, Allocator, traits>::match_word_boundary,
  138. &perl_matcher<BidiIterator, Allocator, traits>::match_within_word,
  139. &perl_matcher<BidiIterator, Allocator, traits>::match_word_start,
  140. &perl_matcher<BidiIterator, Allocator, traits>::match_word_end,
  141. &perl_matcher<BidiIterator, Allocator, traits>::match_buffer_start,
  142. &perl_matcher<BidiIterator, Allocator, traits>::match_buffer_end,
  143. &perl_matcher<BidiIterator, Allocator, traits>::match_backref,
  144. &perl_matcher<BidiIterator, Allocator, traits>::match_long_set,
  145. &perl_matcher<BidiIterator, Allocator, traits>::match_set,
  146. &perl_matcher<BidiIterator, Allocator, traits>::match_jump,
  147. &perl_matcher<BidiIterator, Allocator, traits>::match_alt,
  148. &perl_matcher<BidiIterator, Allocator, traits>::match_rep,
  149. &perl_matcher<BidiIterator, Allocator, traits>::match_combining,
  150. &perl_matcher<BidiIterator, Allocator, traits>::match_soft_buffer_end,
  151. &perl_matcher<BidiIterator, Allocator, traits>::match_restart_continue,
  152. // Although this next line *should* be evaluated at compile time, in practice
  153. // some compilers (VC++) emit run-time initialisation which breaks thread
  154. // safety, so use a dispatch function instead:
  155. //(::boost::is_random_access_iterator<BidiIterator>::value ? &perl_matcher<BidiIterator, Allocator, traits>::match_dot_repeat_fast : &perl_matcher<BidiIterator, Allocator, traits>::match_dot_repeat_slow),
  156. &perl_matcher<BidiIterator, Allocator, traits>::match_dot_repeat_dispatch,
  157. &perl_matcher<BidiIterator, Allocator, traits>::match_char_repeat,
  158. &perl_matcher<BidiIterator, Allocator, traits>::match_set_repeat,
  159. &perl_matcher<BidiIterator, Allocator, traits>::match_long_set_repeat,
  160. &perl_matcher<BidiIterator, Allocator, traits>::match_backstep,
  161. &perl_matcher<BidiIterator, Allocator, traits>::match_assert_backref,
  162. &perl_matcher<BidiIterator, Allocator, traits>::match_toggle_case,
  163. &perl_matcher<BidiIterator, Allocator, traits>::match_recursion,
  164. };
  165. push_recursion_stopper();
  166. do{
  167. while(pstate)
  168. {
  169. matcher_proc_type proc = s_match_vtable[pstate->type];
  170. ++state_count;
  171. if(!(this->*proc)())
  172. {
  173. if(state_count > max_state_count)
  174. raise_error(traits_inst, regex_constants::error_space);
  175. if((m_match_flags & match_partial) && (position == last) && (position != search_base))
  176. m_has_partial_match = true;
  177. bool successful_unwind = unwind(false);
  178. if((m_match_flags & match_partial) && (position == last) && (position != search_base))
  179. m_has_partial_match = true;
  180. if(false == successful_unwind)
  181. return m_recursive_result;
  182. }
  183. }
  184. }while(unwind(true));
  185. return m_recursive_result;
  186. }
  187. template <class BidiIterator, class Allocator, class traits>
  188. void perl_matcher<BidiIterator, Allocator, traits>::extend_stack()
  189. {
  190. if(used_block_count)
  191. {
  192. --used_block_count;
  193. saved_state* stack_base;
  194. saved_state* backup_state;
  195. stack_base = static_cast<saved_state*>(get_mem_block());
  196. backup_state = reinterpret_cast<saved_state*>(reinterpret_cast<char*>(stack_base)+BOOST_REGEX_BLOCKSIZE);
  197. saved_extra_block* block = static_cast<saved_extra_block*>(backup_state);
  198. --block;
  199. (void) new (block) saved_extra_block(m_stack_base, m_backup_state);
  200. m_stack_base = stack_base;
  201. m_backup_state = block;
  202. }
  203. else
  204. raise_error(traits_inst, regex_constants::error_size);
  205. }
  206. template <class BidiIterator, class Allocator, class traits>
  207. inline void perl_matcher<BidiIterator, Allocator, traits>::push_matched_paren(int index, const sub_match<BidiIterator>& sub)
  208. {
  209. //BOOST_ASSERT(index);
  210. saved_matched_paren<BidiIterator>* pmp = static_cast<saved_matched_paren<BidiIterator>*>(m_backup_state);
  211. --pmp;
  212. if(pmp < m_stack_base)
  213. {
  214. extend_stack();
  215. pmp = static_cast<saved_matched_paren<BidiIterator>*>(m_backup_state);
  216. --pmp;
  217. }
  218. (void) new (pmp)saved_matched_paren<BidiIterator>(index, sub);
  219. m_backup_state = pmp;
  220. }
  221. template <class BidiIterator, class Allocator, class traits>
  222. inline void perl_matcher<BidiIterator, Allocator, traits>::push_recursion_stopper()
  223. {
  224. saved_state* pmp = m_backup_state;
  225. --pmp;
  226. if(pmp < m_stack_base)
  227. {
  228. extend_stack();
  229. pmp = m_backup_state;
  230. --pmp;
  231. }
  232. (void) new (pmp)saved_state(saved_type_recurse);
  233. m_backup_state = pmp;
  234. }
  235. template <class BidiIterator, class Allocator, class traits>
  236. inline void perl_matcher<BidiIterator, Allocator, traits>::push_assertion(const re_syntax_base* ps, bool positive)
  237. {
  238. saved_assertion<BidiIterator>* pmp = static_cast<saved_assertion<BidiIterator>*>(m_backup_state);
  239. --pmp;
  240. if(pmp < m_stack_base)
  241. {
  242. extend_stack();
  243. pmp = static_cast<saved_assertion<BidiIterator>*>(m_backup_state);
  244. --pmp;
  245. }
  246. (void) new (pmp)saved_assertion<BidiIterator>(positive, ps, position);
  247. m_backup_state = pmp;
  248. }
  249. template <class BidiIterator, class Allocator, class traits>
  250. inline void perl_matcher<BidiIterator, Allocator, traits>::push_alt(const re_syntax_base* ps)
  251. {
  252. saved_position<BidiIterator>* pmp = static_cast<saved_position<BidiIterator>*>(m_backup_state);
  253. --pmp;
  254. if(pmp < m_stack_base)
  255. {
  256. extend_stack();
  257. pmp = static_cast<saved_position<BidiIterator>*>(m_backup_state);
  258. --pmp;
  259. }
  260. (void) new (pmp)saved_position<BidiIterator>(ps, position, saved_state_alt);
  261. m_backup_state = pmp;
  262. }
  263. template <class BidiIterator, class Allocator, class traits>
  264. inline void perl_matcher<BidiIterator, Allocator, traits>::push_non_greedy_repeat(const re_syntax_base* ps)
  265. {
  266. saved_position<BidiIterator>* pmp = static_cast<saved_position<BidiIterator>*>(m_backup_state);
  267. --pmp;
  268. if(pmp < m_stack_base)
  269. {
  270. extend_stack();
  271. pmp = static_cast<saved_position<BidiIterator>*>(m_backup_state);
  272. --pmp;
  273. }
  274. (void) new (pmp)saved_position<BidiIterator>(ps, position, saved_state_non_greedy_long_repeat);
  275. m_backup_state = pmp;
  276. }
  277. template <class BidiIterator, class Allocator, class traits>
  278. inline void perl_matcher<BidiIterator, Allocator, traits>::push_repeater_count(int i, repeater_count<BidiIterator>** s)
  279. {
  280. saved_repeater<BidiIterator>* pmp = static_cast<saved_repeater<BidiIterator>*>(m_backup_state);
  281. --pmp;
  282. if(pmp < m_stack_base)
  283. {
  284. extend_stack();
  285. pmp = static_cast<saved_repeater<BidiIterator>*>(m_backup_state);
  286. --pmp;
  287. }
  288. (void) new (pmp)saved_repeater<BidiIterator>(i, s, position);
  289. m_backup_state = pmp;
  290. }
  291. template <class BidiIterator, class Allocator, class traits>
  292. inline void perl_matcher<BidiIterator, Allocator, traits>::push_single_repeat(std::size_t c, const re_repeat* r, BidiIterator last_position, int state_id)
  293. {
  294. saved_single_repeat<BidiIterator>* pmp = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  295. --pmp;
  296. if(pmp < m_stack_base)
  297. {
  298. extend_stack();
  299. pmp = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  300. --pmp;
  301. }
  302. (void) new (pmp)saved_single_repeat<BidiIterator>(c, r, last_position, state_id);
  303. m_backup_state = pmp;
  304. }
  305. template <class BidiIterator, class Allocator, class traits>
  306. inline void perl_matcher<BidiIterator, Allocator, traits>::push_recursion(int id, const re_syntax_base* p, results_type* presults)
  307. {
  308. saved_recursion<results_type>* pmp = static_cast<saved_recursion<results_type>*>(m_backup_state);
  309. --pmp;
  310. if(pmp < m_stack_base)
  311. {
  312. extend_stack();
  313. pmp = static_cast<saved_recursion<results_type>*>(m_backup_state);
  314. --pmp;
  315. }
  316. (void) new (pmp)saved_recursion<results_type>(id, p, presults);
  317. m_backup_state = pmp;
  318. }
  319. template <class BidiIterator, class Allocator, class traits>
  320. bool perl_matcher<BidiIterator, Allocator, traits>::match_startmark()
  321. {
  322. int index = static_cast<const re_brace*>(pstate)->index;
  323. icase = static_cast<const re_brace*>(pstate)->icase;
  324. switch(index)
  325. {
  326. case 0:
  327. pstate = pstate->next.p;
  328. break;
  329. case -1:
  330. case -2:
  331. {
  332. // forward lookahead assert:
  333. const re_syntax_base* next_pstate = static_cast<const re_jump*>(pstate->next.p)->alt.p->next.p;
  334. pstate = pstate->next.p->next.p;
  335. push_assertion(next_pstate, index == -1);
  336. break;
  337. }
  338. case -3:
  339. {
  340. // independent sub-expression, currently this is always recursive:
  341. bool old_independent = m_independent;
  342. m_independent = true;
  343. const re_syntax_base* next_pstate = static_cast<const re_jump*>(pstate->next.p)->alt.p->next.p;
  344. pstate = pstate->next.p->next.p;
  345. bool r = match_all_states();
  346. pstate = next_pstate;
  347. m_independent = old_independent;
  348. #ifdef BOOST_REGEX_MATCH_EXTRA
  349. if(r && (m_match_flags & match_extra))
  350. {
  351. //
  352. // our captures have been stored in *m_presult
  353. // we need to unpack them, and insert them
  354. // back in the right order when we unwind the stack:
  355. //
  356. match_results<BidiIterator, Allocator> temp_match(*m_presult);
  357. unsigned i;
  358. for(i = 0; i < temp_match.size(); ++i)
  359. (*m_presult)[i].get_captures().clear();
  360. // match everything else:
  361. r = match_all_states();
  362. // now place the stored captures back:
  363. for(i = 0; i < temp_match.size(); ++i)
  364. {
  365. typedef typename sub_match<BidiIterator>::capture_sequence_type seq;
  366. seq& s1 = (*m_presult)[i].get_captures();
  367. const seq& s2 = temp_match[i].captures();
  368. s1.insert(
  369. s1.end(),
  370. s2.begin(),
  371. s2.end());
  372. }
  373. }
  374. #endif
  375. return r;
  376. }
  377. case -4:
  378. {
  379. // conditional expression:
  380. const re_alt* alt = static_cast<const re_alt*>(pstate->next.p);
  381. BOOST_ASSERT(alt->type == syntax_element_alt);
  382. pstate = alt->next.p;
  383. if(pstate->type == syntax_element_assert_backref)
  384. {
  385. if(!match_assert_backref())
  386. pstate = alt->alt.p;
  387. break;
  388. }
  389. else
  390. {
  391. // zero width assertion, have to match this recursively:
  392. BOOST_ASSERT(pstate->type == syntax_element_startmark);
  393. bool negated = static_cast<const re_brace*>(pstate)->index == -2;
  394. BidiIterator saved_position = position;
  395. const re_syntax_base* next_pstate = static_cast<const re_jump*>(pstate->next.p)->alt.p->next.p;
  396. pstate = pstate->next.p->next.p;
  397. bool r = match_all_states();
  398. position = saved_position;
  399. if(negated)
  400. r = !r;
  401. if(r)
  402. pstate = next_pstate;
  403. else
  404. pstate = alt->alt.p;
  405. break;
  406. }
  407. }
  408. case -5:
  409. {
  410. push_matched_paren(0, (*m_presult)[0]);
  411. m_presult->set_first(position, 0, true);
  412. pstate = pstate->next.p;
  413. break;
  414. }
  415. default:
  416. {
  417. BOOST_ASSERT(index > 0);
  418. if((m_match_flags & match_nosubs) == 0)
  419. {
  420. push_matched_paren(index, (*m_presult)[index]);
  421. m_presult->set_first(position, index);
  422. }
  423. pstate = pstate->next.p;
  424. break;
  425. }
  426. }
  427. return true;
  428. }
  429. template <class BidiIterator, class Allocator, class traits>
  430. bool perl_matcher<BidiIterator, Allocator, traits>::match_alt()
  431. {
  432. bool take_first, take_second;
  433. const re_alt* jmp = static_cast<const re_alt*>(pstate);
  434. // find out which of these two alternatives we need to take:
  435. if(position == last)
  436. {
  437. take_first = jmp->can_be_null & mask_take;
  438. take_second = jmp->can_be_null & mask_skip;
  439. }
  440. else
  441. {
  442. take_first = can_start(*position, jmp->_map, (unsigned char)mask_take);
  443. take_second = can_start(*position, jmp->_map, (unsigned char)mask_skip);
  444. }
  445. if(take_first)
  446. {
  447. // we can take the first alternative,
  448. // see if we need to push next alternative:
  449. if(take_second)
  450. {
  451. push_alt(jmp->alt.p);
  452. }
  453. pstate = pstate->next.p;
  454. return true;
  455. }
  456. if(take_second)
  457. {
  458. pstate = jmp->alt.p;
  459. return true;
  460. }
  461. return false; // neither option is possible
  462. }
  463. template <class BidiIterator, class Allocator, class traits>
  464. bool perl_matcher<BidiIterator, Allocator, traits>::match_rep()
  465. {
  466. #ifdef BOOST_MSVC
  467. #pragma warning(push)
  468. #pragma warning(disable:4127 4244)
  469. #endif
  470. #ifdef __BORLANDC__
  471. #pragma option push -w-8008 -w-8066 -w-8004
  472. #endif
  473. const re_repeat* rep = static_cast<const re_repeat*>(pstate);
  474. // find out which of these two alternatives we need to take:
  475. bool take_first, take_second;
  476. if(position == last)
  477. {
  478. take_first = rep->can_be_null & mask_take;
  479. take_second = rep->can_be_null & mask_skip;
  480. }
  481. else
  482. {
  483. take_first = can_start(*position, rep->_map, (unsigned char)mask_take);
  484. take_second = can_start(*position, rep->_map, (unsigned char)mask_skip);
  485. }
  486. if((m_backup_state->state_id != saved_state_repeater_count)
  487. || (static_cast<saved_repeater<BidiIterator>*>(m_backup_state)->count.get_id() != rep->state_id)
  488. || (next_count->get_id() != rep->state_id))
  489. {
  490. // we're moving to a different repeat from the last
  491. // one, so set up a counter object:
  492. push_repeater_count(rep->state_id, &next_count);
  493. }
  494. //
  495. // If we've had at least one repeat already, and the last one
  496. // matched the NULL string then set the repeat count to
  497. // maximum:
  498. //
  499. next_count->check_null_repeat(position, rep->max);
  500. if(next_count->get_count() < rep->min)
  501. {
  502. // we must take the repeat:
  503. if(take_first)
  504. {
  505. // increase the counter:
  506. ++(*next_count);
  507. pstate = rep->next.p;
  508. return true;
  509. }
  510. return false;
  511. }
  512. bool greedy = (rep->greedy) && (!(m_match_flags & regex_constants::match_any) || m_independent);
  513. if(greedy)
  514. {
  515. // try and take the repeat if we can:
  516. if((next_count->get_count() < rep->max) && take_first)
  517. {
  518. if(take_second)
  519. {
  520. // store position in case we fail:
  521. push_alt(rep->alt.p);
  522. }
  523. // increase the counter:
  524. ++(*next_count);
  525. pstate = rep->next.p;
  526. return true;
  527. }
  528. else if(take_second)
  529. {
  530. pstate = rep->alt.p;
  531. return true;
  532. }
  533. return false; // can't take anything, fail...
  534. }
  535. else // non-greedy
  536. {
  537. // try and skip the repeat if we can:
  538. if(take_second)
  539. {
  540. if((next_count->get_count() < rep->max) && take_first)
  541. {
  542. // store position in case we fail:
  543. push_non_greedy_repeat(rep->next.p);
  544. }
  545. pstate = rep->alt.p;
  546. return true;
  547. }
  548. if((next_count->get_count() < rep->max) && take_first)
  549. {
  550. // increase the counter:
  551. ++(*next_count);
  552. pstate = rep->next.p;
  553. return true;
  554. }
  555. }
  556. return false;
  557. #ifdef __BORLANDC__
  558. #pragma option pop
  559. #endif
  560. #ifdef BOOST_MSVC
  561. #pragma warning(pop)
  562. #endif
  563. }
  564. template <class BidiIterator, class Allocator, class traits>
  565. bool perl_matcher<BidiIterator, Allocator, traits>::match_dot_repeat_slow()
  566. {
  567. unsigned count = 0;
  568. const re_repeat* rep = static_cast<const re_repeat*>(pstate);
  569. re_syntax_base* psingle = rep->next.p;
  570. // match compulsary repeats first:
  571. while(count < rep->min)
  572. {
  573. pstate = psingle;
  574. if(!match_wild())
  575. return false;
  576. ++count;
  577. }
  578. bool greedy = (rep->greedy) && (!(m_match_flags & regex_constants::match_any) || m_independent);
  579. if(greedy)
  580. {
  581. // repeat for as long as we can:
  582. while(count < rep->max)
  583. {
  584. pstate = psingle;
  585. if(!match_wild())
  586. break;
  587. ++count;
  588. }
  589. // remember where we got to if this is a leading repeat:
  590. if((rep->leading) && (count < rep->max))
  591. restart = position;
  592. // push backtrack info if available:
  593. if(count - rep->min)
  594. push_single_repeat(count, rep, position, saved_state_greedy_single_repeat);
  595. // jump to next state:
  596. pstate = rep->alt.p;
  597. return true;
  598. }
  599. else
  600. {
  601. // non-greedy, push state and return true if we can skip:
  602. if(count < rep->max)
  603. push_single_repeat(count, rep, position, saved_state_rep_slow_dot);
  604. pstate = rep->alt.p;
  605. return (position == last) ? (rep->can_be_null & mask_skip) : can_start(*position, rep->_map, mask_skip);
  606. }
  607. }
  608. template <class BidiIterator, class Allocator, class traits>
  609. bool perl_matcher<BidiIterator, Allocator, traits>::match_dot_repeat_fast()
  610. {
  611. if(m_match_flags & match_not_dot_null)
  612. return match_dot_repeat_slow();
  613. if((static_cast<const re_dot*>(pstate->next.p)->mask & match_any_mask) == 0)
  614. return match_dot_repeat_slow();
  615. const re_repeat* rep = static_cast<const re_repeat*>(pstate);
  616. bool greedy = (rep->greedy) && (!(m_match_flags & regex_constants::match_any) || m_independent);
  617. unsigned count = static_cast<unsigned>((std::min)(static_cast<unsigned>(::boost::re_detail::distance(position, last)), static_cast<unsigned>(greedy ? rep->max : rep->min)));
  618. if(rep->min > count)
  619. {
  620. position = last;
  621. return false; // not enough text left to match
  622. }
  623. std::advance(position, count);
  624. if(greedy)
  625. {
  626. if((rep->leading) && (count < rep->max))
  627. restart = position;
  628. // push backtrack info if available:
  629. if(count - rep->min)
  630. push_single_repeat(count, rep, position, saved_state_greedy_single_repeat);
  631. // jump to next state:
  632. pstate = rep->alt.p;
  633. return true;
  634. }
  635. else
  636. {
  637. // non-greedy, push state and return true if we can skip:
  638. if(count < rep->max)
  639. push_single_repeat(count, rep, position, saved_state_rep_fast_dot);
  640. pstate = rep->alt.p;
  641. return (position == last) ? (rep->can_be_null & mask_skip) : can_start(*position, rep->_map, mask_skip);
  642. }
  643. }
  644. template <class BidiIterator, class Allocator, class traits>
  645. bool perl_matcher<BidiIterator, Allocator, traits>::match_char_repeat()
  646. {
  647. #ifdef BOOST_MSVC
  648. #pragma warning(push)
  649. #pragma warning(disable:4127)
  650. #endif
  651. #ifdef __BORLANDC__
  652. #pragma option push -w-8008 -w-8066 -w-8004
  653. #endif
  654. const re_repeat* rep = static_cast<const re_repeat*>(pstate);
  655. BOOST_ASSERT(1 == static_cast<const re_literal*>(rep->next.p)->length);
  656. const char_type what = *reinterpret_cast<const char_type*>(static_cast<const re_literal*>(rep->next.p) + 1);
  657. std::size_t count = 0;
  658. //
  659. // start by working out how much we can skip:
  660. //
  661. bool greedy = (rep->greedy) && (!(m_match_flags & regex_constants::match_any) || m_independent);
  662. std::size_t desired = greedy ? rep->max : rep->min;
  663. if(::boost::is_random_access_iterator<BidiIterator>::value)
  664. {
  665. BidiIterator end = position;
  666. std::advance(end, (std::min)((std::size_t)::boost::re_detail::distance(position, last), desired));
  667. BidiIterator origin(position);
  668. while((position != end) && (traits_inst.translate(*position, icase) == what))
  669. {
  670. ++position;
  671. }
  672. count = (unsigned)::boost::re_detail::distance(origin, position);
  673. }
  674. else
  675. {
  676. while((count < desired) && (position != last) && (traits_inst.translate(*position, icase) == what))
  677. {
  678. ++position;
  679. ++count;
  680. }
  681. }
  682. if(count < rep->min)
  683. return false;
  684. if(greedy)
  685. {
  686. if((rep->leading) && (count < rep->max))
  687. restart = position;
  688. // push backtrack info if available:
  689. if(count - rep->min)
  690. push_single_repeat(count, rep, position, saved_state_greedy_single_repeat);
  691. // jump to next state:
  692. pstate = rep->alt.p;
  693. return true;
  694. }
  695. else
  696. {
  697. // non-greedy, push state and return true if we can skip:
  698. if(count < rep->max)
  699. push_single_repeat(count, rep, position, saved_state_rep_char);
  700. pstate = rep->alt.p;
  701. return (position == last) ? (rep->can_be_null & mask_skip) : can_start(*position, rep->_map, mask_skip);
  702. }
  703. #ifdef __BORLANDC__
  704. #pragma option pop
  705. #endif
  706. #ifdef BOOST_MSVC
  707. #pragma warning(pop)
  708. #endif
  709. }
  710. template <class BidiIterator, class Allocator, class traits>
  711. bool perl_matcher<BidiIterator, Allocator, traits>::match_set_repeat()
  712. {
  713. #ifdef BOOST_MSVC
  714. #pragma warning(push)
  715. #pragma warning(disable:4127)
  716. #endif
  717. #ifdef __BORLANDC__
  718. #pragma option push -w-8008 -w-8066 -w-8004
  719. #endif
  720. const re_repeat* rep = static_cast<const re_repeat*>(pstate);
  721. const unsigned char* map = static_cast<const re_set*>(rep->next.p)->_map;
  722. std::size_t count = 0;
  723. //
  724. // start by working out how much we can skip:
  725. //
  726. bool greedy = (rep->greedy) && (!(m_match_flags & regex_constants::match_any) || m_independent);
  727. std::size_t desired = greedy ? rep->max : rep->min;
  728. if(::boost::is_random_access_iterator<BidiIterator>::value)
  729. {
  730. BidiIterator end = position;
  731. std::advance(end, (std::min)((std::size_t)::boost::re_detail::distance(position, last), desired));
  732. BidiIterator origin(position);
  733. while((position != end) && map[static_cast<unsigned char>(traits_inst.translate(*position, icase))])
  734. {
  735. ++position;
  736. }
  737. count = (unsigned)::boost::re_detail::distance(origin, position);
  738. }
  739. else
  740. {
  741. while((count < desired) && (position != last) && map[static_cast<unsigned char>(traits_inst.translate(*position, icase))])
  742. {
  743. ++position;
  744. ++count;
  745. }
  746. }
  747. if(count < rep->min)
  748. return false;
  749. if(greedy)
  750. {
  751. if((rep->leading) && (count < rep->max))
  752. restart = position;
  753. // push backtrack info if available:
  754. if(count - rep->min)
  755. push_single_repeat(count, rep, position, saved_state_greedy_single_repeat);
  756. // jump to next state:
  757. pstate = rep->alt.p;
  758. return true;
  759. }
  760. else
  761. {
  762. // non-greedy, push state and return true if we can skip:
  763. if(count < rep->max)
  764. push_single_repeat(count, rep, position, saved_state_rep_short_set);
  765. pstate = rep->alt.p;
  766. return (position == last) ? (rep->can_be_null & mask_skip) : can_start(*position, rep->_map, mask_skip);
  767. }
  768. #ifdef __BORLANDC__
  769. #pragma option pop
  770. #endif
  771. #ifdef BOOST_MSVC
  772. #pragma warning(pop)
  773. #endif
  774. }
  775. template <class BidiIterator, class Allocator, class traits>
  776. bool perl_matcher<BidiIterator, Allocator, traits>::match_long_set_repeat()
  777. {
  778. #ifdef BOOST_MSVC
  779. #pragma warning(push)
  780. #pragma warning(disable:4127)
  781. #endif
  782. #ifdef __BORLANDC__
  783. #pragma option push -w-8008 -w-8066 -w-8004
  784. #endif
  785. typedef typename traits::char_class_type mask_type;
  786. const re_repeat* rep = static_cast<const re_repeat*>(pstate);
  787. const re_set_long<mask_type>* set = static_cast<const re_set_long<mask_type>*>(pstate->next.p);
  788. std::size_t count = 0;
  789. //
  790. // start by working out how much we can skip:
  791. //
  792. bool greedy = (rep->greedy) && (!(m_match_flags & regex_constants::match_any) || m_independent);
  793. std::size_t desired = greedy ? rep->max : rep->min;
  794. if(::boost::is_random_access_iterator<BidiIterator>::value)
  795. {
  796. BidiIterator end = position;
  797. std::advance(end, (std::min)((std::size_t)::boost::re_detail::distance(position, last), desired));
  798. BidiIterator origin(position);
  799. while((position != end) && (position != re_is_set_member(position, last, set, re.get_data(), icase)))
  800. {
  801. ++position;
  802. }
  803. count = (unsigned)::boost::re_detail::distance(origin, position);
  804. }
  805. else
  806. {
  807. while((count < desired) && (position != last) && (position != re_is_set_member(position, last, set, re.get_data(), icase)))
  808. {
  809. ++position;
  810. ++count;
  811. }
  812. }
  813. if(count < rep->min)
  814. return false;
  815. if(greedy)
  816. {
  817. if((rep->leading) && (count < rep->max))
  818. restart = position;
  819. // push backtrack info if available:
  820. if(count - rep->min)
  821. push_single_repeat(count, rep, position, saved_state_greedy_single_repeat);
  822. // jump to next state:
  823. pstate = rep->alt.p;
  824. return true;
  825. }
  826. else
  827. {
  828. // non-greedy, push state and return true if we can skip:
  829. if(count < rep->max)
  830. push_single_repeat(count, rep, position, saved_state_rep_long_set);
  831. pstate = rep->alt.p;
  832. return (position == last) ? (rep->can_be_null & mask_skip) : can_start(*position, rep->_map, mask_skip);
  833. }
  834. #ifdef __BORLANDC__
  835. #pragma option pop
  836. #endif
  837. #ifdef BOOST_MSVC
  838. #pragma warning(pop)
  839. #endif
  840. }
  841. template <class BidiIterator, class Allocator, class traits>
  842. bool perl_matcher<BidiIterator, Allocator, traits>::match_recursion()
  843. {
  844. BOOST_ASSERT(pstate->type == syntax_element_recurse);
  845. //
  846. // Backup call stack:
  847. //
  848. push_recursion_pop();
  849. //
  850. // Set new call stack:
  851. //
  852. if(recursion_stack_position >= static_cast<int>(sizeof(recursion_stack)/sizeof(recursion_stack[0])))
  853. {
  854. return false;
  855. }
  856. recursion_stack[recursion_stack_position].preturn_address = pstate->next.p;
  857. recursion_stack[recursion_stack_position].results = *m_presult;
  858. pstate = static_cast<const re_jump*>(pstate)->alt.p;
  859. recursion_stack[recursion_stack_position].id = static_cast<const re_brace*>(pstate)->index;
  860. ++recursion_stack_position;
  861. //BOOST_ASSERT(recursion_stack[recursion_stack_position-1].id);
  862. return true;
  863. }
  864. template <class BidiIterator, class Allocator, class traits>
  865. bool perl_matcher<BidiIterator, Allocator, traits>::match_endmark()
  866. {
  867. int index = static_cast<const re_brace*>(pstate)->index;
  868. icase = static_cast<const re_brace*>(pstate)->icase;
  869. if(index > 0)
  870. {
  871. if((m_match_flags & match_nosubs) == 0)
  872. {
  873. m_presult->set_second(position, index);
  874. }
  875. if(recursion_stack_position)
  876. {
  877. if(index == recursion_stack[recursion_stack_position-1].id)
  878. {
  879. --recursion_stack_position;
  880. pstate = recursion_stack[recursion_stack_position].preturn_address;
  881. *m_presult = recursion_stack[recursion_stack_position].results;
  882. push_recursion(recursion_stack[recursion_stack_position].id, recursion_stack[recursion_stack_position].preturn_address, &recursion_stack[recursion_stack_position].results);
  883. }
  884. }
  885. }
  886. else if((index < 0) && (index != -4))
  887. {
  888. // matched forward lookahead:
  889. pstate = 0;
  890. return true;
  891. }
  892. pstate = pstate->next.p;
  893. return true;
  894. }
  895. template <class BidiIterator, class Allocator, class traits>
  896. bool perl_matcher<BidiIterator, Allocator, traits>::match_match()
  897. {
  898. if(recursion_stack_position)
  899. {
  900. BOOST_ASSERT(0 == recursion_stack[recursion_stack_position-1].id);
  901. --recursion_stack_position;
  902. pstate = recursion_stack[recursion_stack_position].preturn_address;
  903. *m_presult = recursion_stack[recursion_stack_position].results;
  904. push_recursion(recursion_stack[recursion_stack_position].id, recursion_stack[recursion_stack_position].preturn_address, &recursion_stack[recursion_stack_position].results);
  905. return true;
  906. }
  907. if((m_match_flags & match_not_null) && (position == (*m_presult)[0].first))
  908. return false;
  909. if((m_match_flags & match_all) && (position != last))
  910. return false;
  911. if((m_match_flags & regex_constants::match_not_initial_null) && (position == search_base))
  912. return false;
  913. m_presult->set_second(position);
  914. pstate = 0;
  915. m_has_found_match = true;
  916. if((m_match_flags & match_posix) == match_posix)
  917. {
  918. m_result.maybe_assign(*m_presult);
  919. if((m_match_flags & match_any) == 0)
  920. return false;
  921. }
  922. #ifdef BOOST_REGEX_MATCH_EXTRA
  923. if(match_extra & m_match_flags)
  924. {
  925. for(unsigned i = 0; i < m_presult->size(); ++i)
  926. if((*m_presult)[i].matched)
  927. ((*m_presult)[i]).get_captures().push_back((*m_presult)[i]);
  928. }
  929. #endif
  930. return true;
  931. }
  932. /****************************************************************************
  933. Unwind and associated proceedures follow, these perform what normal stack
  934. unwinding does in the recursive implementation.
  935. ****************************************************************************/
  936. template <class BidiIterator, class Allocator, class traits>
  937. bool perl_matcher<BidiIterator, Allocator, traits>::unwind(bool have_match)
  938. {
  939. static unwind_proc_type const s_unwind_table[18] =
  940. {
  941. &perl_matcher<BidiIterator, Allocator, traits>::unwind_end,
  942. &perl_matcher<BidiIterator, Allocator, traits>::unwind_paren,
  943. &perl_matcher<BidiIterator, Allocator, traits>::unwind_recursion_stopper,
  944. &perl_matcher<BidiIterator, Allocator, traits>::unwind_assertion,
  945. &perl_matcher<BidiIterator, Allocator, traits>::unwind_alt,
  946. &perl_matcher<BidiIterator, Allocator, traits>::unwind_repeater_counter,
  947. &perl_matcher<BidiIterator, Allocator, traits>::unwind_extra_block,
  948. &perl_matcher<BidiIterator, Allocator, traits>::unwind_greedy_single_repeat,
  949. &perl_matcher<BidiIterator, Allocator, traits>::unwind_slow_dot_repeat,
  950. &perl_matcher<BidiIterator, Allocator, traits>::unwind_fast_dot_repeat,
  951. &perl_matcher<BidiIterator, Allocator, traits>::unwind_char_repeat,
  952. &perl_matcher<BidiIterator, Allocator, traits>::unwind_short_set_repeat,
  953. &perl_matcher<BidiIterator, Allocator, traits>::unwind_long_set_repeat,
  954. &perl_matcher<BidiIterator, Allocator, traits>::unwind_non_greedy_repeat,
  955. &perl_matcher<BidiIterator, Allocator, traits>::unwind_recursion,
  956. &perl_matcher<BidiIterator, Allocator, traits>::unwind_recursion_pop,
  957. };
  958. m_recursive_result = have_match;
  959. unwind_proc_type unwinder;
  960. bool cont;
  961. //
  962. // keep unwinding our stack until we have something to do:
  963. //
  964. do
  965. {
  966. unwinder = s_unwind_table[m_backup_state->state_id];
  967. cont = (this->*unwinder)(m_recursive_result);
  968. }while(cont);
  969. //
  970. // return true if we have more states to try:
  971. //
  972. return pstate ? true : false;
  973. }
  974. template <class BidiIterator, class Allocator, class traits>
  975. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_end(bool)
  976. {
  977. pstate = 0; // nothing left to search
  978. return false; // end of stack nothing more to search
  979. }
  980. template <class BidiIterator, class Allocator, class traits>
  981. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_paren(bool have_match)
  982. {
  983. saved_matched_paren<BidiIterator>* pmp = static_cast<saved_matched_paren<BidiIterator>*>(m_backup_state);
  984. // restore previous values if no match was found:
  985. if(have_match == false)
  986. {
  987. m_presult->set_first(pmp->sub.first, pmp->index, pmp->index == 0);
  988. m_presult->set_second(pmp->sub.second, pmp->index, pmp->sub.matched, pmp->index == 0);
  989. }
  990. #ifdef BOOST_REGEX_MATCH_EXTRA
  991. //
  992. // we have a match, push the capture information onto the stack:
  993. //
  994. else if(pmp->sub.matched && (match_extra & m_match_flags))
  995. ((*m_presult)[pmp->index]).get_captures().push_back(pmp->sub);
  996. #endif
  997. // unwind stack:
  998. m_backup_state = pmp+1;
  999. boost::re_detail::inplace_destroy(pmp);
  1000. return true; // keep looking
  1001. }
  1002. template <class BidiIterator, class Allocator, class traits>
  1003. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_recursion_stopper(bool)
  1004. {
  1005. boost::re_detail::inplace_destroy(m_backup_state++);
  1006. pstate = 0; // nothing left to search
  1007. return false; // end of stack nothing more to search
  1008. }
  1009. template <class BidiIterator, class Allocator, class traits>
  1010. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_assertion(bool r)
  1011. {
  1012. saved_assertion<BidiIterator>* pmp = static_cast<saved_assertion<BidiIterator>*>(m_backup_state);
  1013. pstate = pmp->pstate;
  1014. position = pmp->position;
  1015. bool result = (r == pmp->positive);
  1016. m_recursive_result = pmp->positive ? r : !r;
  1017. boost::re_detail::inplace_destroy(pmp++);
  1018. m_backup_state = pmp;
  1019. return !result; // return false if the assertion was matched to stop search.
  1020. }
  1021. template <class BidiIterator, class Allocator, class traits>
  1022. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_alt(bool r)
  1023. {
  1024. saved_position<BidiIterator>* pmp = static_cast<saved_position<BidiIterator>*>(m_backup_state);
  1025. if(!r)
  1026. {
  1027. pstate = pmp->pstate;
  1028. position = pmp->position;
  1029. }
  1030. boost::re_detail::inplace_destroy(pmp++);
  1031. m_backup_state = pmp;
  1032. return r;
  1033. }
  1034. template <class BidiIterator, class Allocator, class traits>
  1035. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_repeater_counter(bool)
  1036. {
  1037. saved_repeater<BidiIterator>* pmp = static_cast<saved_repeater<BidiIterator>*>(m_backup_state);
  1038. boost::re_detail::inplace_destroy(pmp++);
  1039. m_backup_state = pmp;
  1040. return true; // keep looking
  1041. }
  1042. template <class BidiIterator, class Allocator, class traits>
  1043. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_extra_block(bool)
  1044. {
  1045. saved_extra_block* pmp = static_cast<saved_extra_block*>(m_backup_state);
  1046. void* condemmed = m_stack_base;
  1047. m_stack_base = pmp->base;
  1048. m_backup_state = pmp->end;
  1049. boost::re_detail::inplace_destroy(pmp);
  1050. put_mem_block(condemmed);
  1051. return true; // keep looking
  1052. }
  1053. template <class BidiIterator, class Allocator, class traits>
  1054. inline void perl_matcher<BidiIterator, Allocator, traits>::destroy_single_repeat()
  1055. {
  1056. saved_single_repeat<BidiIterator>* p = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  1057. boost::re_detail::inplace_destroy(p++);
  1058. m_backup_state = p;
  1059. }
  1060. template <class BidiIterator, class Allocator, class traits>
  1061. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_greedy_single_repeat(bool r)
  1062. {
  1063. saved_single_repeat<BidiIterator>* pmp = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  1064. // if we have a match, just discard this state:
  1065. if(r)
  1066. {
  1067. destroy_single_repeat();
  1068. return true;
  1069. }
  1070. const re_repeat* rep = pmp->rep;
  1071. std::size_t count = pmp->count;
  1072. BOOST_ASSERT(rep->next.p != 0);
  1073. BOOST_ASSERT(rep->alt.p != 0);
  1074. count -= rep->min;
  1075. if((m_match_flags & match_partial) && (position == last))
  1076. m_has_partial_match = true;
  1077. BOOST_ASSERT(count);
  1078. position = pmp->last_position;
  1079. // backtrack till we can skip out:
  1080. do
  1081. {
  1082. --position;
  1083. --count;
  1084. ++state_count;
  1085. }while(count && !can_start(*position, rep->_map, mask_skip));
  1086. // if we've hit base, destroy this state:
  1087. if(count == 0)
  1088. {
  1089. destroy_single_repeat();
  1090. if(!can_start(*position, rep->_map, mask_skip))
  1091. return true;
  1092. }
  1093. else
  1094. {
  1095. pmp->count = count + rep->min;
  1096. pmp->last_position = position;
  1097. }
  1098. pstate = rep->alt.p;
  1099. return false;
  1100. }
  1101. template <class BidiIterator, class Allocator, class traits>
  1102. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_slow_dot_repeat(bool r)
  1103. {
  1104. saved_single_repeat<BidiIterator>* pmp = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  1105. // if we have a match, just discard this state:
  1106. if(r)
  1107. {
  1108. destroy_single_repeat();
  1109. return true;
  1110. }
  1111. const re_repeat* rep = pmp->rep;
  1112. std::size_t count = pmp->count;
  1113. BOOST_ASSERT(rep->type == syntax_element_dot_rep);
  1114. BOOST_ASSERT(rep->next.p != 0);
  1115. BOOST_ASSERT(rep->alt.p != 0);
  1116. BOOST_ASSERT(rep->next.p->type == syntax_element_wild);
  1117. BOOST_ASSERT(count < rep->max);
  1118. pstate = rep->next.p;
  1119. position = pmp->last_position;
  1120. if(position != last)
  1121. {
  1122. // wind forward until we can skip out of the repeat:
  1123. do
  1124. {
  1125. if(!match_wild())
  1126. {
  1127. // failed repeat match, discard this state and look for another:
  1128. destroy_single_repeat();
  1129. return true;
  1130. }
  1131. ++count;
  1132. ++state_count;
  1133. pstate = rep->next.p;
  1134. }while((count < rep->max) && (position != last) && !can_start(*position, rep->_map, mask_skip));
  1135. }
  1136. if(position == last)
  1137. {
  1138. // can't repeat any more, remove the pushed state:
  1139. destroy_single_repeat();
  1140. if((m_match_flags & match_partial) && (position == last) && (position != search_base))
  1141. m_has_partial_match = true;
  1142. if(0 == (rep->can_be_null & mask_skip))
  1143. return true;
  1144. }
  1145. else if(count == rep->max)
  1146. {
  1147. // can't repeat any more, remove the pushed state:
  1148. destroy_single_repeat();
  1149. if(!can_start(*position, rep->_map, mask_skip))
  1150. return true;
  1151. }
  1152. else
  1153. {
  1154. pmp->count = count;
  1155. pmp->last_position = position;
  1156. }
  1157. pstate = rep->alt.p;
  1158. return false;
  1159. }
  1160. template <class BidiIterator, class Allocator, class traits>
  1161. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_fast_dot_repeat(bool r)
  1162. {
  1163. saved_single_repeat<BidiIterator>* pmp = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  1164. // if we have a match, just discard this state:
  1165. if(r)
  1166. {
  1167. destroy_single_repeat();
  1168. return true;
  1169. }
  1170. const re_repeat* rep = pmp->rep;
  1171. std::size_t count = pmp->count;
  1172. BOOST_ASSERT(count < rep->max);
  1173. position = pmp->last_position;
  1174. if(position != last)
  1175. {
  1176. // wind forward until we can skip out of the repeat:
  1177. do
  1178. {
  1179. ++position;
  1180. ++count;
  1181. ++state_count;
  1182. }while((count < rep->max) && (position != last) && !can_start(*position, rep->_map, mask_skip));
  1183. }
  1184. if(position == last)
  1185. {
  1186. // can't repeat any more, remove the pushed state:
  1187. destroy_single_repeat();
  1188. if((m_match_flags & match_partial) && (position == last) && (position != search_base))
  1189. m_has_partial_match = true;
  1190. if(0 == (rep->can_be_null & mask_skip))
  1191. return true;
  1192. }
  1193. else if(count == rep->max)
  1194. {
  1195. // can't repeat any more, remove the pushed state:
  1196. destroy_single_repeat();
  1197. if(!can_start(*position, rep->_map, mask_skip))
  1198. return true;
  1199. }
  1200. else
  1201. {
  1202. pmp->count = count;
  1203. pmp->last_position = position;
  1204. }
  1205. pstate = rep->alt.p;
  1206. return false;
  1207. }
  1208. template <class BidiIterator, class Allocator, class traits>
  1209. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_char_repeat(bool r)
  1210. {
  1211. saved_single_repeat<BidiIterator>* pmp = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  1212. // if we have a match, just discard this state:
  1213. if(r)
  1214. {
  1215. destroy_single_repeat();
  1216. return true;
  1217. }
  1218. const re_repeat* rep = pmp->rep;
  1219. std::size_t count = pmp->count;
  1220. pstate = rep->next.p;
  1221. const char_type what = *reinterpret_cast<const char_type*>(static_cast<const re_literal*>(pstate) + 1);
  1222. position = pmp->last_position;
  1223. BOOST_ASSERT(rep->type == syntax_element_char_rep);
  1224. BOOST_ASSERT(rep->next.p != 0);
  1225. BOOST_ASSERT(rep->alt.p != 0);
  1226. BOOST_ASSERT(rep->next.p->type == syntax_element_literal);
  1227. BOOST_ASSERT(count < rep->max);
  1228. if(position != last)
  1229. {
  1230. // wind forward until we can skip out of the repeat:
  1231. do
  1232. {
  1233. if(traits_inst.translate(*position, icase) != what)
  1234. {
  1235. // failed repeat match, discard this state and look for another:
  1236. destroy_single_repeat();
  1237. return true;
  1238. }
  1239. ++count;
  1240. ++ position;
  1241. ++state_count;
  1242. pstate = rep->next.p;
  1243. }while((count < rep->max) && (position != last) && !can_start(*position, rep->_map, mask_skip));
  1244. }
  1245. // remember where we got to if this is a leading repeat:
  1246. if((rep->leading) && (count < rep->max))
  1247. restart = position;
  1248. if(position == last)
  1249. {
  1250. // can't repeat any more, remove the pushed state:
  1251. destroy_single_repeat();
  1252. if((m_match_flags & match_partial) && (position == last) && (position != search_base))
  1253. m_has_partial_match = true;
  1254. if(0 == (rep->can_be_null & mask_skip))
  1255. return true;
  1256. }
  1257. else if(count == rep->max)
  1258. {
  1259. // can't repeat any more, remove the pushed state:
  1260. destroy_single_repeat();
  1261. if(!can_start(*position, rep->_map, mask_skip))
  1262. return true;
  1263. }
  1264. else
  1265. {
  1266. pmp->count = count;
  1267. pmp->last_position = position;
  1268. }
  1269. pstate = rep->alt.p;
  1270. return false;
  1271. }
  1272. template <class BidiIterator, class Allocator, class traits>
  1273. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_short_set_repeat(bool r)
  1274. {
  1275. saved_single_repeat<BidiIterator>* pmp = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  1276. // if we have a match, just discard this state:
  1277. if(r)
  1278. {
  1279. destroy_single_repeat();
  1280. return true;
  1281. }
  1282. const re_repeat* rep = pmp->rep;
  1283. std::size_t count = pmp->count;
  1284. pstate = rep->next.p;
  1285. const unsigned char* map = static_cast<const re_set*>(rep->next.p)->_map;
  1286. position = pmp->last_position;
  1287. BOOST_ASSERT(rep->type == syntax_element_short_set_rep);
  1288. BOOST_ASSERT(rep->next.p != 0);
  1289. BOOST_ASSERT(rep->alt.p != 0);
  1290. BOOST_ASSERT(rep->next.p->type == syntax_element_set);
  1291. BOOST_ASSERT(count < rep->max);
  1292. if(position != last)
  1293. {
  1294. // wind forward until we can skip out of the repeat:
  1295. do
  1296. {
  1297. if(!map[static_cast<unsigned char>(traits_inst.translate(*position, icase))])
  1298. {
  1299. // failed repeat match, discard this state and look for another:
  1300. destroy_single_repeat();
  1301. return true;
  1302. }
  1303. ++count;
  1304. ++ position;
  1305. ++state_count;
  1306. pstate = rep->next.p;
  1307. }while((count < rep->max) && (position != last) && !can_start(*position, rep->_map, mask_skip));
  1308. }
  1309. // remember where we got to if this is a leading repeat:
  1310. if((rep->leading) && (count < rep->max))
  1311. restart = position;
  1312. if(position == last)
  1313. {
  1314. // can't repeat any more, remove the pushed state:
  1315. destroy_single_repeat();
  1316. if((m_match_flags & match_partial) && (position == last) && (position != search_base))
  1317. m_has_partial_match = true;
  1318. if(0 == (rep->can_be_null & mask_skip))
  1319. return true;
  1320. }
  1321. else if(count == rep->max)
  1322. {
  1323. // can't repeat any more, remove the pushed state:
  1324. destroy_single_repeat();
  1325. if(!can_start(*position, rep->_map, mask_skip))
  1326. return true;
  1327. }
  1328. else
  1329. {
  1330. pmp->count = count;
  1331. pmp->last_position = position;
  1332. }
  1333. pstate = rep->alt.p;
  1334. return false;
  1335. }
  1336. template <class BidiIterator, class Allocator, class traits>
  1337. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_long_set_repeat(bool r)
  1338. {
  1339. typedef typename traits::char_class_type mask_type;
  1340. saved_single_repeat<BidiIterator>* pmp = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  1341. // if we have a match, just discard this state:
  1342. if(r)
  1343. {
  1344. destroy_single_repeat();
  1345. return true;
  1346. }
  1347. const re_repeat* rep = pmp->rep;
  1348. std::size_t count = pmp->count;
  1349. pstate = rep->next.p;
  1350. const re_set_long<mask_type>* set = static_cast<const re_set_long<mask_type>*>(pstate);
  1351. position = pmp->last_position;
  1352. BOOST_ASSERT(rep->type == syntax_element_long_set_rep);
  1353. BOOST_ASSERT(rep->next.p != 0);
  1354. BOOST_ASSERT(rep->alt.p != 0);
  1355. BOOST_ASSERT(rep->next.p->type == syntax_element_long_set);
  1356. BOOST_ASSERT(count < rep->max);
  1357. if(position != last)
  1358. {
  1359. // wind forward until we can skip out of the repeat:
  1360. do
  1361. {
  1362. if(position == re_is_set_member(position, last, set, re.get_data(), icase))
  1363. {
  1364. // failed repeat match, discard this state and look for another:
  1365. destroy_single_repeat();
  1366. return true;
  1367. }
  1368. ++position;
  1369. ++count;
  1370. ++state_count;
  1371. pstate = rep->next.p;
  1372. }while((count < rep->max) && (position != last) && !can_start(*position, rep->_map, mask_skip));
  1373. }
  1374. // remember where we got to if this is a leading repeat:
  1375. if((rep->leading) && (count < rep->max))
  1376. restart = position;
  1377. if(position == last)
  1378. {
  1379. // can't repeat any more, remove the pushed state:
  1380. destroy_single_repeat();
  1381. if((m_match_flags & match_partial) && (position == last) && (position != search_base))
  1382. m_has_partial_match = true;
  1383. if(0 == (rep->can_be_null & mask_skip))
  1384. return true;
  1385. }
  1386. else if(count == rep->max)
  1387. {
  1388. // can't repeat any more, remove the pushed state:
  1389. destroy_single_repeat();
  1390. if(!can_start(*position, rep->_map, mask_skip))
  1391. return true;
  1392. }
  1393. else
  1394. {
  1395. pmp->count = count;
  1396. pmp->last_position = position;
  1397. }
  1398. pstate = rep->alt.p;
  1399. return false;
  1400. }
  1401. template <class BidiIterator, class Allocator, class traits>
  1402. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_non_greedy_repeat(bool r)
  1403. {
  1404. saved_position<BidiIterator>* pmp = static_cast<saved_position<BidiIterator>*>(m_backup_state);
  1405. if(!r)
  1406. {
  1407. position = pmp->position;
  1408. pstate = pmp->pstate;
  1409. ++(*next_count);
  1410. }
  1411. boost::re_detail::inplace_destroy(pmp++);
  1412. m_backup_state = pmp;
  1413. return r;
  1414. }
  1415. template <class BidiIterator, class Allocator, class traits>
  1416. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_recursion(bool r)
  1417. {
  1418. saved_recursion<results_type>* pmp = static_cast<saved_recursion<results_type>*>(m_backup_state);
  1419. if(!r)
  1420. {
  1421. recursion_stack[recursion_stack_position].id = pmp->recursion_id;
  1422. recursion_stack[recursion_stack_position].preturn_address = pmp->preturn_address;
  1423. recursion_stack[recursion_stack_position].results = pmp->results;
  1424. ++recursion_stack_position;
  1425. }
  1426. boost::re_detail::inplace_destroy(pmp++);
  1427. m_backup_state = pmp;
  1428. return true;
  1429. }
  1430. template <class BidiIterator, class Allocator, class traits>
  1431. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_recursion_pop(bool r)
  1432. {
  1433. saved_state* pmp = static_cast<saved_state*>(m_backup_state);
  1434. if(!r)
  1435. {
  1436. --recursion_stack_position;
  1437. }
  1438. boost::re_detail::inplace_destroy(pmp++);
  1439. m_backup_state = pmp;
  1440. return true;
  1441. }
  1442. template <class BidiIterator, class Allocator, class traits>
  1443. void perl_matcher<BidiIterator, Allocator, traits>::push_recursion_pop()
  1444. {
  1445. saved_state* pmp = static_cast<saved_state*>(m_backup_state);
  1446. --pmp;
  1447. if(pmp < m_stack_base)
  1448. {
  1449. extend_stack();
  1450. pmp = static_cast<saved_state*>(m_backup_state);
  1451. --pmp;
  1452. }
  1453. (void) new (pmp)saved_state(15);
  1454. m_backup_state = pmp;
  1455. }
  1456. /*
  1457. template <class BidiIterator, class Allocator, class traits>
  1458. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_parenthesis_pop(bool r)
  1459. {
  1460. saved_state* pmp = static_cast<saved_state*>(m_backup_state);
  1461. if(!r)
  1462. {
  1463. --parenthesis_stack_position;
  1464. }
  1465. boost::re_detail::inplace_destroy(pmp++);
  1466. m_backup_state = pmp;
  1467. return true;
  1468. }
  1469. template <class BidiIterator, class Allocator, class traits>
  1470. void perl_matcher<BidiIterator, Allocator, traits>::push_parenthesis_pop()
  1471. {
  1472. saved_state* pmp = static_cast<saved_state*>(m_backup_state);
  1473. --pmp;
  1474. if(pmp < m_stack_base)
  1475. {
  1476. extend_stack();
  1477. pmp = static_cast<saved_state*>(m_backup_state);
  1478. --pmp;
  1479. }
  1480. (void) new (pmp)saved_state(16);
  1481. m_backup_state = pmp;
  1482. }
  1483. template <class BidiIterator, class Allocator, class traits>
  1484. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_parenthesis_push(bool r)
  1485. {
  1486. saved_position<BidiIterator>* pmp = static_cast<saved_position<BidiIterator>*>(m_backup_state);
  1487. if(!r)
  1488. {
  1489. parenthesis_stack[parenthesis_stack_position++] = pmp->position;
  1490. }
  1491. boost::re_detail::inplace_destroy(pmp++);
  1492. m_backup_state = pmp;
  1493. return true;
  1494. }
  1495. template <class BidiIterator, class Allocator, class traits>
  1496. inline void perl_matcher<BidiIterator, Allocator, traits>::push_parenthesis_push(BidiIterator p)
  1497. {
  1498. saved_position<BidiIterator>* pmp = static_cast<saved_position<BidiIterator>*>(m_backup_state);
  1499. --pmp;
  1500. if(pmp < m_stack_base)
  1501. {
  1502. extend_stack();
  1503. pmp = static_cast<saved_position<BidiIterator>*>(m_backup_state);
  1504. --pmp;
  1505. }
  1506. (void) new (pmp)saved_position<BidiIterator>(0, p, 17);
  1507. m_backup_state = pmp;
  1508. }
  1509. */
  1510. } // namespace re_detail
  1511. } // namespace boost
  1512. #ifdef BOOST_MSVC
  1513. # pragma warning(pop)
  1514. #endif
  1515. #ifdef BOOST_MSVC
  1516. #pragma warning(push)
  1517. #pragma warning(disable: 4103)
  1518. #endif
  1519. #ifdef BOOST_HAS_ABI_HEADERS
  1520. # include BOOST_ABI_SUFFIX
  1521. #endif
  1522. #ifdef BOOST_MSVC
  1523. #pragma warning(pop)
  1524. #endif
  1525. #endif