perl_matcher_non_recursive.hpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  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 idx, const re_syntax_base* p, Results* pr)
  119. : saved_state(14), recursion_id(idx), 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_complexity);
  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_stack);
  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 idx, 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>(idx, 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. // Move end forward by "desired", preferably without using distance or advance if we can
  667. // as these can be slow for some iterator types.
  668. std::size_t len = (desired == (std::numeric_limits<std::size_t>::max)()) ? 0u : ::boost::re_detail::distance(position, last);
  669. if(desired >= len)
  670. end = last;
  671. else
  672. std::advance(end, desired);
  673. BidiIterator origin(position);
  674. while((position != end) && (traits_inst.translate(*position, icase) == what))
  675. {
  676. ++position;
  677. }
  678. count = (unsigned)::boost::re_detail::distance(origin, position);
  679. }
  680. else
  681. {
  682. while((count < desired) && (position != last) && (traits_inst.translate(*position, icase) == what))
  683. {
  684. ++position;
  685. ++count;
  686. }
  687. }
  688. if(count < rep->min)
  689. return false;
  690. if(greedy)
  691. {
  692. if((rep->leading) && (count < rep->max))
  693. restart = position;
  694. // push backtrack info if available:
  695. if(count - rep->min)
  696. push_single_repeat(count, rep, position, saved_state_greedy_single_repeat);
  697. // jump to next state:
  698. pstate = rep->alt.p;
  699. return true;
  700. }
  701. else
  702. {
  703. // non-greedy, push state and return true if we can skip:
  704. if(count < rep->max)
  705. push_single_repeat(count, rep, position, saved_state_rep_char);
  706. pstate = rep->alt.p;
  707. return (position == last) ? (rep->can_be_null & mask_skip) : can_start(*position, rep->_map, mask_skip);
  708. }
  709. #ifdef __BORLANDC__
  710. #pragma option pop
  711. #endif
  712. #ifdef BOOST_MSVC
  713. #pragma warning(pop)
  714. #endif
  715. }
  716. template <class BidiIterator, class Allocator, class traits>
  717. bool perl_matcher<BidiIterator, Allocator, traits>::match_set_repeat()
  718. {
  719. #ifdef BOOST_MSVC
  720. #pragma warning(push)
  721. #pragma warning(disable:4127)
  722. #endif
  723. #ifdef __BORLANDC__
  724. #pragma option push -w-8008 -w-8066 -w-8004
  725. #endif
  726. const re_repeat* rep = static_cast<const re_repeat*>(pstate);
  727. const unsigned char* map = static_cast<const re_set*>(rep->next.p)->_map;
  728. std::size_t count = 0;
  729. //
  730. // start by working out how much we can skip:
  731. //
  732. bool greedy = (rep->greedy) && (!(m_match_flags & regex_constants::match_any) || m_independent);
  733. std::size_t desired = greedy ? rep->max : rep->min;
  734. if(::boost::is_random_access_iterator<BidiIterator>::value)
  735. {
  736. BidiIterator end = position;
  737. // Move end forward by "desired", preferably without using distance or advance if we can
  738. // as these can be slow for some iterator types.
  739. std::size_t len = (desired == (std::numeric_limits<std::size_t>::max)()) ? 0u : ::boost::re_detail::distance(position, last);
  740. if(desired >= len)
  741. end = last;
  742. else
  743. std::advance(end, desired);
  744. BidiIterator origin(position);
  745. while((position != end) && map[static_cast<unsigned char>(traits_inst.translate(*position, icase))])
  746. {
  747. ++position;
  748. }
  749. count = (unsigned)::boost::re_detail::distance(origin, position);
  750. }
  751. else
  752. {
  753. while((count < desired) && (position != last) && map[static_cast<unsigned char>(traits_inst.translate(*position, icase))])
  754. {
  755. ++position;
  756. ++count;
  757. }
  758. }
  759. if(count < rep->min)
  760. return false;
  761. if(greedy)
  762. {
  763. if((rep->leading) && (count < rep->max))
  764. restart = position;
  765. // push backtrack info if available:
  766. if(count - rep->min)
  767. push_single_repeat(count, rep, position, saved_state_greedy_single_repeat);
  768. // jump to next state:
  769. pstate = rep->alt.p;
  770. return true;
  771. }
  772. else
  773. {
  774. // non-greedy, push state and return true if we can skip:
  775. if(count < rep->max)
  776. push_single_repeat(count, rep, position, saved_state_rep_short_set);
  777. pstate = rep->alt.p;
  778. return (position == last) ? (rep->can_be_null & mask_skip) : can_start(*position, rep->_map, mask_skip);
  779. }
  780. #ifdef __BORLANDC__
  781. #pragma option pop
  782. #endif
  783. #ifdef BOOST_MSVC
  784. #pragma warning(pop)
  785. #endif
  786. }
  787. template <class BidiIterator, class Allocator, class traits>
  788. bool perl_matcher<BidiIterator, Allocator, traits>::match_long_set_repeat()
  789. {
  790. #ifdef BOOST_MSVC
  791. #pragma warning(push)
  792. #pragma warning(disable:4127)
  793. #endif
  794. #ifdef __BORLANDC__
  795. #pragma option push -w-8008 -w-8066 -w-8004
  796. #endif
  797. typedef typename traits::char_class_type m_type;
  798. const re_repeat* rep = static_cast<const re_repeat*>(pstate);
  799. const re_set_long<m_type>* set = static_cast<const re_set_long<m_type>*>(pstate->next.p);
  800. std::size_t count = 0;
  801. //
  802. // start by working out how much we can skip:
  803. //
  804. bool greedy = (rep->greedy) && (!(m_match_flags & regex_constants::match_any) || m_independent);
  805. std::size_t desired = greedy ? rep->max : rep->min;
  806. if(::boost::is_random_access_iterator<BidiIterator>::value)
  807. {
  808. BidiIterator end = position;
  809. // Move end forward by "desired", preferably without using distance or advance if we can
  810. // as these can be slow for some iterator types.
  811. std::size_t len = (desired == (std::numeric_limits<std::size_t>::max)()) ? 0u : ::boost::re_detail::distance(position, last);
  812. if(desired >= len)
  813. end = last;
  814. else
  815. std::advance(end, desired);
  816. BidiIterator origin(position);
  817. while((position != end) && (position != re_is_set_member(position, last, set, re.get_data(), icase)))
  818. {
  819. ++position;
  820. }
  821. count = (unsigned)::boost::re_detail::distance(origin, position);
  822. }
  823. else
  824. {
  825. while((count < desired) && (position != last) && (position != re_is_set_member(position, last, set, re.get_data(), icase)))
  826. {
  827. ++position;
  828. ++count;
  829. }
  830. }
  831. if(count < rep->min)
  832. return false;
  833. if(greedy)
  834. {
  835. if((rep->leading) && (count < rep->max))
  836. restart = position;
  837. // push backtrack info if available:
  838. if(count - rep->min)
  839. push_single_repeat(count, rep, position, saved_state_greedy_single_repeat);
  840. // jump to next state:
  841. pstate = rep->alt.p;
  842. return true;
  843. }
  844. else
  845. {
  846. // non-greedy, push state and return true if we can skip:
  847. if(count < rep->max)
  848. push_single_repeat(count, rep, position, saved_state_rep_long_set);
  849. pstate = rep->alt.p;
  850. return (position == last) ? (rep->can_be_null & mask_skip) : can_start(*position, rep->_map, mask_skip);
  851. }
  852. #ifdef __BORLANDC__
  853. #pragma option pop
  854. #endif
  855. #ifdef BOOST_MSVC
  856. #pragma warning(pop)
  857. #endif
  858. }
  859. template <class BidiIterator, class Allocator, class traits>
  860. bool perl_matcher<BidiIterator, Allocator, traits>::match_recursion()
  861. {
  862. BOOST_ASSERT(pstate->type == syntax_element_recurse);
  863. //
  864. // Backup call stack:
  865. //
  866. push_recursion_pop();
  867. //
  868. // Set new call stack:
  869. //
  870. if(recursion_stack.capacity() == 0)
  871. {
  872. recursion_stack.reserve(50);
  873. }
  874. recursion_stack.push_back(recursion_info<results_type>());
  875. recursion_stack.back().preturn_address = pstate->next.p;
  876. recursion_stack.back().results = *m_presult;
  877. if(static_cast<const re_recurse*>(pstate)->state_id > 0)
  878. {
  879. push_repeater_count(static_cast<const re_recurse*>(pstate)->state_id, &next_count);
  880. }
  881. pstate = static_cast<const re_jump*>(pstate)->alt.p;
  882. recursion_stack.back().idx = static_cast<const re_brace*>(pstate)->index;
  883. return true;
  884. }
  885. template <class BidiIterator, class Allocator, class traits>
  886. bool perl_matcher<BidiIterator, Allocator, traits>::match_endmark()
  887. {
  888. int index = static_cast<const re_brace*>(pstate)->index;
  889. icase = static_cast<const re_brace*>(pstate)->icase;
  890. if(index > 0)
  891. {
  892. if((m_match_flags & match_nosubs) == 0)
  893. {
  894. m_presult->set_second(position, index);
  895. }
  896. if(!recursion_stack.empty())
  897. {
  898. if(index == recursion_stack.back().idx)
  899. {
  900. pstate = recursion_stack.back().preturn_address;
  901. *m_presult = recursion_stack.back().results;
  902. push_recursion(recursion_stack.back().idx, recursion_stack.back().preturn_address, &recursion_stack.back().results);
  903. recursion_stack.pop_back();
  904. }
  905. }
  906. }
  907. else if((index < 0) && (index != -4))
  908. {
  909. // matched forward lookahead:
  910. pstate = 0;
  911. return true;
  912. }
  913. pstate = pstate->next.p;
  914. return true;
  915. }
  916. template <class BidiIterator, class Allocator, class traits>
  917. bool perl_matcher<BidiIterator, Allocator, traits>::match_match()
  918. {
  919. if(!recursion_stack.empty())
  920. {
  921. BOOST_ASSERT(0 == recursion_stack.back().idx);
  922. pstate = recursion_stack.back().preturn_address;
  923. *m_presult = recursion_stack.back().results;
  924. push_recursion(recursion_stack.back().idx, recursion_stack.back().preturn_address, &recursion_stack.back().results);
  925. recursion_stack.pop_back();
  926. return true;
  927. }
  928. if((m_match_flags & match_not_null) && (position == (*m_presult)[0].first))
  929. return false;
  930. if((m_match_flags & match_all) && (position != last))
  931. return false;
  932. if((m_match_flags & regex_constants::match_not_initial_null) && (position == search_base))
  933. return false;
  934. m_presult->set_second(position);
  935. pstate = 0;
  936. m_has_found_match = true;
  937. if((m_match_flags & match_posix) == match_posix)
  938. {
  939. m_result.maybe_assign(*m_presult);
  940. if((m_match_flags & match_any) == 0)
  941. return false;
  942. }
  943. #ifdef BOOST_REGEX_MATCH_EXTRA
  944. if(match_extra & m_match_flags)
  945. {
  946. for(unsigned i = 0; i < m_presult->size(); ++i)
  947. if((*m_presult)[i].matched)
  948. ((*m_presult)[i]).get_captures().push_back((*m_presult)[i]);
  949. }
  950. #endif
  951. return true;
  952. }
  953. /****************************************************************************
  954. Unwind and associated proceedures follow, these perform what normal stack
  955. unwinding does in the recursive implementation.
  956. ****************************************************************************/
  957. template <class BidiIterator, class Allocator, class traits>
  958. bool perl_matcher<BidiIterator, Allocator, traits>::unwind(bool have_match)
  959. {
  960. static unwind_proc_type const s_unwind_table[18] =
  961. {
  962. &perl_matcher<BidiIterator, Allocator, traits>::unwind_end,
  963. &perl_matcher<BidiIterator, Allocator, traits>::unwind_paren,
  964. &perl_matcher<BidiIterator, Allocator, traits>::unwind_recursion_stopper,
  965. &perl_matcher<BidiIterator, Allocator, traits>::unwind_assertion,
  966. &perl_matcher<BidiIterator, Allocator, traits>::unwind_alt,
  967. &perl_matcher<BidiIterator, Allocator, traits>::unwind_repeater_counter,
  968. &perl_matcher<BidiIterator, Allocator, traits>::unwind_extra_block,
  969. &perl_matcher<BidiIterator, Allocator, traits>::unwind_greedy_single_repeat,
  970. &perl_matcher<BidiIterator, Allocator, traits>::unwind_slow_dot_repeat,
  971. &perl_matcher<BidiIterator, Allocator, traits>::unwind_fast_dot_repeat,
  972. &perl_matcher<BidiIterator, Allocator, traits>::unwind_char_repeat,
  973. &perl_matcher<BidiIterator, Allocator, traits>::unwind_short_set_repeat,
  974. &perl_matcher<BidiIterator, Allocator, traits>::unwind_long_set_repeat,
  975. &perl_matcher<BidiIterator, Allocator, traits>::unwind_non_greedy_repeat,
  976. &perl_matcher<BidiIterator, Allocator, traits>::unwind_recursion,
  977. &perl_matcher<BidiIterator, Allocator, traits>::unwind_recursion_pop,
  978. };
  979. m_recursive_result = have_match;
  980. unwind_proc_type unwinder;
  981. bool cont;
  982. //
  983. // keep unwinding our stack until we have something to do:
  984. //
  985. do
  986. {
  987. unwinder = s_unwind_table[m_backup_state->state_id];
  988. cont = (this->*unwinder)(m_recursive_result);
  989. }while(cont);
  990. //
  991. // return true if we have more states to try:
  992. //
  993. return pstate ? true : false;
  994. }
  995. template <class BidiIterator, class Allocator, class traits>
  996. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_end(bool)
  997. {
  998. pstate = 0; // nothing left to search
  999. return false; // end of stack nothing more to search
  1000. }
  1001. template <class BidiIterator, class Allocator, class traits>
  1002. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_paren(bool have_match)
  1003. {
  1004. saved_matched_paren<BidiIterator>* pmp = static_cast<saved_matched_paren<BidiIterator>*>(m_backup_state);
  1005. // restore previous values if no match was found:
  1006. if(have_match == false)
  1007. {
  1008. m_presult->set_first(pmp->sub.first, pmp->index, pmp->index == 0);
  1009. m_presult->set_second(pmp->sub.second, pmp->index, pmp->sub.matched, pmp->index == 0);
  1010. }
  1011. #ifdef BOOST_REGEX_MATCH_EXTRA
  1012. //
  1013. // we have a match, push the capture information onto the stack:
  1014. //
  1015. else if(pmp->sub.matched && (match_extra & m_match_flags))
  1016. ((*m_presult)[pmp->index]).get_captures().push_back(pmp->sub);
  1017. #endif
  1018. // unwind stack:
  1019. m_backup_state = pmp+1;
  1020. boost::re_detail::inplace_destroy(pmp);
  1021. return true; // keep looking
  1022. }
  1023. template <class BidiIterator, class Allocator, class traits>
  1024. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_recursion_stopper(bool)
  1025. {
  1026. boost::re_detail::inplace_destroy(m_backup_state++);
  1027. pstate = 0; // nothing left to search
  1028. return false; // end of stack nothing more to search
  1029. }
  1030. template <class BidiIterator, class Allocator, class traits>
  1031. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_assertion(bool r)
  1032. {
  1033. saved_assertion<BidiIterator>* pmp = static_cast<saved_assertion<BidiIterator>*>(m_backup_state);
  1034. pstate = pmp->pstate;
  1035. position = pmp->position;
  1036. bool result = (r == pmp->positive);
  1037. m_recursive_result = pmp->positive ? r : !r;
  1038. boost::re_detail::inplace_destroy(pmp++);
  1039. m_backup_state = pmp;
  1040. return !result; // return false if the assertion was matched to stop search.
  1041. }
  1042. template <class BidiIterator, class Allocator, class traits>
  1043. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_alt(bool r)
  1044. {
  1045. saved_position<BidiIterator>* pmp = static_cast<saved_position<BidiIterator>*>(m_backup_state);
  1046. if(!r)
  1047. {
  1048. pstate = pmp->pstate;
  1049. position = pmp->position;
  1050. }
  1051. boost::re_detail::inplace_destroy(pmp++);
  1052. m_backup_state = pmp;
  1053. return r;
  1054. }
  1055. template <class BidiIterator, class Allocator, class traits>
  1056. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_repeater_counter(bool)
  1057. {
  1058. saved_repeater<BidiIterator>* pmp = static_cast<saved_repeater<BidiIterator>*>(m_backup_state);
  1059. boost::re_detail::inplace_destroy(pmp++);
  1060. m_backup_state = pmp;
  1061. return true; // keep looking
  1062. }
  1063. template <class BidiIterator, class Allocator, class traits>
  1064. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_extra_block(bool)
  1065. {
  1066. saved_extra_block* pmp = static_cast<saved_extra_block*>(m_backup_state);
  1067. void* condemmed = m_stack_base;
  1068. m_stack_base = pmp->base;
  1069. m_backup_state = pmp->end;
  1070. boost::re_detail::inplace_destroy(pmp);
  1071. put_mem_block(condemmed);
  1072. return true; // keep looking
  1073. }
  1074. template <class BidiIterator, class Allocator, class traits>
  1075. inline void perl_matcher<BidiIterator, Allocator, traits>::destroy_single_repeat()
  1076. {
  1077. saved_single_repeat<BidiIterator>* p = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  1078. boost::re_detail::inplace_destroy(p++);
  1079. m_backup_state = p;
  1080. }
  1081. template <class BidiIterator, class Allocator, class traits>
  1082. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_greedy_single_repeat(bool r)
  1083. {
  1084. saved_single_repeat<BidiIterator>* pmp = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  1085. // if we have a match, just discard this state:
  1086. if(r)
  1087. {
  1088. destroy_single_repeat();
  1089. return true;
  1090. }
  1091. const re_repeat* rep = pmp->rep;
  1092. std::size_t count = pmp->count;
  1093. BOOST_ASSERT(rep->next.p != 0);
  1094. BOOST_ASSERT(rep->alt.p != 0);
  1095. count -= rep->min;
  1096. if((m_match_flags & match_partial) && (position == last))
  1097. m_has_partial_match = true;
  1098. BOOST_ASSERT(count);
  1099. position = pmp->last_position;
  1100. // backtrack till we can skip out:
  1101. do
  1102. {
  1103. --position;
  1104. --count;
  1105. ++state_count;
  1106. }while(count && !can_start(*position, rep->_map, mask_skip));
  1107. // if we've hit base, destroy this state:
  1108. if(count == 0)
  1109. {
  1110. destroy_single_repeat();
  1111. if(!can_start(*position, rep->_map, mask_skip))
  1112. return true;
  1113. }
  1114. else
  1115. {
  1116. pmp->count = count + rep->min;
  1117. pmp->last_position = position;
  1118. }
  1119. pstate = rep->alt.p;
  1120. return false;
  1121. }
  1122. template <class BidiIterator, class Allocator, class traits>
  1123. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_slow_dot_repeat(bool r)
  1124. {
  1125. saved_single_repeat<BidiIterator>* pmp = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  1126. // if we have a match, just discard this state:
  1127. if(r)
  1128. {
  1129. destroy_single_repeat();
  1130. return true;
  1131. }
  1132. const re_repeat* rep = pmp->rep;
  1133. std::size_t count = pmp->count;
  1134. BOOST_ASSERT(rep->type == syntax_element_dot_rep);
  1135. BOOST_ASSERT(rep->next.p != 0);
  1136. BOOST_ASSERT(rep->alt.p != 0);
  1137. BOOST_ASSERT(rep->next.p->type == syntax_element_wild);
  1138. BOOST_ASSERT(count < rep->max);
  1139. pstate = rep->next.p;
  1140. position = pmp->last_position;
  1141. if(position != last)
  1142. {
  1143. // wind forward until we can skip out of the repeat:
  1144. do
  1145. {
  1146. if(!match_wild())
  1147. {
  1148. // failed repeat match, discard this state and look for another:
  1149. destroy_single_repeat();
  1150. return true;
  1151. }
  1152. ++count;
  1153. ++state_count;
  1154. pstate = rep->next.p;
  1155. }while((count < rep->max) && (position != last) && !can_start(*position, rep->_map, mask_skip));
  1156. }
  1157. if(position == last)
  1158. {
  1159. // can't repeat any more, remove the pushed state:
  1160. destroy_single_repeat();
  1161. if((m_match_flags & match_partial) && (position == last) && (position != search_base))
  1162. m_has_partial_match = true;
  1163. if(0 == (rep->can_be_null & mask_skip))
  1164. return true;
  1165. }
  1166. else if(count == rep->max)
  1167. {
  1168. // can't repeat any more, remove the pushed state:
  1169. destroy_single_repeat();
  1170. if(!can_start(*position, rep->_map, mask_skip))
  1171. return true;
  1172. }
  1173. else
  1174. {
  1175. pmp->count = count;
  1176. pmp->last_position = position;
  1177. }
  1178. pstate = rep->alt.p;
  1179. return false;
  1180. }
  1181. template <class BidiIterator, class Allocator, class traits>
  1182. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_fast_dot_repeat(bool r)
  1183. {
  1184. saved_single_repeat<BidiIterator>* pmp = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  1185. // if we have a match, just discard this state:
  1186. if(r)
  1187. {
  1188. destroy_single_repeat();
  1189. return true;
  1190. }
  1191. const re_repeat* rep = pmp->rep;
  1192. std::size_t count = pmp->count;
  1193. BOOST_ASSERT(count < rep->max);
  1194. position = pmp->last_position;
  1195. if(position != last)
  1196. {
  1197. // wind forward until we can skip out of the repeat:
  1198. do
  1199. {
  1200. ++position;
  1201. ++count;
  1202. ++state_count;
  1203. }while((count < rep->max) && (position != last) && !can_start(*position, rep->_map, mask_skip));
  1204. }
  1205. // remember where we got to if this is a leading repeat:
  1206. if((rep->leading) && (count < rep->max))
  1207. restart = position;
  1208. if(position == last)
  1209. {
  1210. // can't repeat any more, remove the pushed state:
  1211. destroy_single_repeat();
  1212. if((m_match_flags & match_partial) && (position == last) && (position != search_base))
  1213. m_has_partial_match = true;
  1214. if(0 == (rep->can_be_null & mask_skip))
  1215. return true;
  1216. }
  1217. else if(count == rep->max)
  1218. {
  1219. // can't repeat any more, remove the pushed state:
  1220. destroy_single_repeat();
  1221. if(!can_start(*position, rep->_map, mask_skip))
  1222. return true;
  1223. }
  1224. else
  1225. {
  1226. pmp->count = count;
  1227. pmp->last_position = position;
  1228. }
  1229. pstate = rep->alt.p;
  1230. return false;
  1231. }
  1232. template <class BidiIterator, class Allocator, class traits>
  1233. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_char_repeat(bool r)
  1234. {
  1235. saved_single_repeat<BidiIterator>* pmp = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  1236. // if we have a match, just discard this state:
  1237. if(r)
  1238. {
  1239. destroy_single_repeat();
  1240. return true;
  1241. }
  1242. const re_repeat* rep = pmp->rep;
  1243. std::size_t count = pmp->count;
  1244. pstate = rep->next.p;
  1245. const char_type what = *reinterpret_cast<const char_type*>(static_cast<const re_literal*>(pstate) + 1);
  1246. position = pmp->last_position;
  1247. BOOST_ASSERT(rep->type == syntax_element_char_rep);
  1248. BOOST_ASSERT(rep->next.p != 0);
  1249. BOOST_ASSERT(rep->alt.p != 0);
  1250. BOOST_ASSERT(rep->next.p->type == syntax_element_literal);
  1251. BOOST_ASSERT(count < rep->max);
  1252. if(position != last)
  1253. {
  1254. // wind forward until we can skip out of the repeat:
  1255. do
  1256. {
  1257. if(traits_inst.translate(*position, icase) != what)
  1258. {
  1259. // failed repeat match, discard this state and look for another:
  1260. destroy_single_repeat();
  1261. return true;
  1262. }
  1263. ++count;
  1264. ++ position;
  1265. ++state_count;
  1266. pstate = rep->next.p;
  1267. }while((count < rep->max) && (position != last) && !can_start(*position, rep->_map, mask_skip));
  1268. }
  1269. // remember where we got to if this is a leading repeat:
  1270. if((rep->leading) && (count < rep->max))
  1271. restart = position;
  1272. if(position == last)
  1273. {
  1274. // can't repeat any more, remove the pushed state:
  1275. destroy_single_repeat();
  1276. if((m_match_flags & match_partial) && (position == last) && (position != search_base))
  1277. m_has_partial_match = true;
  1278. if(0 == (rep->can_be_null & mask_skip))
  1279. return true;
  1280. }
  1281. else if(count == rep->max)
  1282. {
  1283. // can't repeat any more, remove the pushed state:
  1284. destroy_single_repeat();
  1285. if(!can_start(*position, rep->_map, mask_skip))
  1286. return true;
  1287. }
  1288. else
  1289. {
  1290. pmp->count = count;
  1291. pmp->last_position = position;
  1292. }
  1293. pstate = rep->alt.p;
  1294. return false;
  1295. }
  1296. template <class BidiIterator, class Allocator, class traits>
  1297. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_short_set_repeat(bool r)
  1298. {
  1299. saved_single_repeat<BidiIterator>* pmp = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  1300. // if we have a match, just discard this state:
  1301. if(r)
  1302. {
  1303. destroy_single_repeat();
  1304. return true;
  1305. }
  1306. const re_repeat* rep = pmp->rep;
  1307. std::size_t count = pmp->count;
  1308. pstate = rep->next.p;
  1309. const unsigned char* map = static_cast<const re_set*>(rep->next.p)->_map;
  1310. position = pmp->last_position;
  1311. BOOST_ASSERT(rep->type == syntax_element_short_set_rep);
  1312. BOOST_ASSERT(rep->next.p != 0);
  1313. BOOST_ASSERT(rep->alt.p != 0);
  1314. BOOST_ASSERT(rep->next.p->type == syntax_element_set);
  1315. BOOST_ASSERT(count < rep->max);
  1316. if(position != last)
  1317. {
  1318. // wind forward until we can skip out of the repeat:
  1319. do
  1320. {
  1321. if(!map[static_cast<unsigned char>(traits_inst.translate(*position, icase))])
  1322. {
  1323. // failed repeat match, discard this state and look for another:
  1324. destroy_single_repeat();
  1325. return true;
  1326. }
  1327. ++count;
  1328. ++ position;
  1329. ++state_count;
  1330. pstate = rep->next.p;
  1331. }while((count < rep->max) && (position != last) && !can_start(*position, rep->_map, mask_skip));
  1332. }
  1333. // remember where we got to if this is a leading repeat:
  1334. if((rep->leading) && (count < rep->max))
  1335. restart = position;
  1336. if(position == last)
  1337. {
  1338. // can't repeat any more, remove the pushed state:
  1339. destroy_single_repeat();
  1340. if((m_match_flags & match_partial) && (position == last) && (position != search_base))
  1341. m_has_partial_match = true;
  1342. if(0 == (rep->can_be_null & mask_skip))
  1343. return true;
  1344. }
  1345. else if(count == rep->max)
  1346. {
  1347. // can't repeat any more, remove the pushed state:
  1348. destroy_single_repeat();
  1349. if(!can_start(*position, rep->_map, mask_skip))
  1350. return true;
  1351. }
  1352. else
  1353. {
  1354. pmp->count = count;
  1355. pmp->last_position = position;
  1356. }
  1357. pstate = rep->alt.p;
  1358. return false;
  1359. }
  1360. template <class BidiIterator, class Allocator, class traits>
  1361. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_long_set_repeat(bool r)
  1362. {
  1363. typedef typename traits::char_class_type m_type;
  1364. saved_single_repeat<BidiIterator>* pmp = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  1365. // if we have a match, just discard this state:
  1366. if(r)
  1367. {
  1368. destroy_single_repeat();
  1369. return true;
  1370. }
  1371. const re_repeat* rep = pmp->rep;
  1372. std::size_t count = pmp->count;
  1373. pstate = rep->next.p;
  1374. const re_set_long<m_type>* set = static_cast<const re_set_long<m_type>*>(pstate);
  1375. position = pmp->last_position;
  1376. BOOST_ASSERT(rep->type == syntax_element_long_set_rep);
  1377. BOOST_ASSERT(rep->next.p != 0);
  1378. BOOST_ASSERT(rep->alt.p != 0);
  1379. BOOST_ASSERT(rep->next.p->type == syntax_element_long_set);
  1380. BOOST_ASSERT(count < rep->max);
  1381. if(position != last)
  1382. {
  1383. // wind forward until we can skip out of the repeat:
  1384. do
  1385. {
  1386. if(position == re_is_set_member(position, last, set, re.get_data(), icase))
  1387. {
  1388. // failed repeat match, discard this state and look for another:
  1389. destroy_single_repeat();
  1390. return true;
  1391. }
  1392. ++position;
  1393. ++count;
  1394. ++state_count;
  1395. pstate = rep->next.p;
  1396. }while((count < rep->max) && (position != last) && !can_start(*position, rep->_map, mask_skip));
  1397. }
  1398. // remember where we got to if this is a leading repeat:
  1399. if((rep->leading) && (count < rep->max))
  1400. restart = position;
  1401. if(position == last)
  1402. {
  1403. // can't repeat any more, remove the pushed state:
  1404. destroy_single_repeat();
  1405. if((m_match_flags & match_partial) && (position == last) && (position != search_base))
  1406. m_has_partial_match = true;
  1407. if(0 == (rep->can_be_null & mask_skip))
  1408. return true;
  1409. }
  1410. else if(count == rep->max)
  1411. {
  1412. // can't repeat any more, remove the pushed state:
  1413. destroy_single_repeat();
  1414. if(!can_start(*position, rep->_map, mask_skip))
  1415. return true;
  1416. }
  1417. else
  1418. {
  1419. pmp->count = count;
  1420. pmp->last_position = position;
  1421. }
  1422. pstate = rep->alt.p;
  1423. return false;
  1424. }
  1425. template <class BidiIterator, class Allocator, class traits>
  1426. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_non_greedy_repeat(bool r)
  1427. {
  1428. saved_position<BidiIterator>* pmp = static_cast<saved_position<BidiIterator>*>(m_backup_state);
  1429. if(!r)
  1430. {
  1431. position = pmp->position;
  1432. pstate = pmp->pstate;
  1433. ++(*next_count);
  1434. }
  1435. boost::re_detail::inplace_destroy(pmp++);
  1436. m_backup_state = pmp;
  1437. return r;
  1438. }
  1439. template <class BidiIterator, class Allocator, class traits>
  1440. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_recursion(bool r)
  1441. {
  1442. saved_recursion<results_type>* pmp = static_cast<saved_recursion<results_type>*>(m_backup_state);
  1443. if(!r)
  1444. {
  1445. recursion_stack.push_back(recursion_info<results_type>());
  1446. recursion_stack.back().idx = pmp->recursion_id;
  1447. recursion_stack.back().preturn_address = pmp->preturn_address;
  1448. recursion_stack.back().results = pmp->results;
  1449. }
  1450. boost::re_detail::inplace_destroy(pmp++);
  1451. m_backup_state = pmp;
  1452. return true;
  1453. }
  1454. template <class BidiIterator, class Allocator, class traits>
  1455. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_recursion_pop(bool r)
  1456. {
  1457. saved_state* pmp = static_cast<saved_state*>(m_backup_state);
  1458. if(!r)
  1459. {
  1460. recursion_stack.pop_back();
  1461. }
  1462. boost::re_detail::inplace_destroy(pmp++);
  1463. m_backup_state = pmp;
  1464. return true;
  1465. }
  1466. template <class BidiIterator, class Allocator, class traits>
  1467. void perl_matcher<BidiIterator, Allocator, traits>::push_recursion_pop()
  1468. {
  1469. saved_state* pmp = static_cast<saved_state*>(m_backup_state);
  1470. --pmp;
  1471. if(pmp < m_stack_base)
  1472. {
  1473. extend_stack();
  1474. pmp = static_cast<saved_state*>(m_backup_state);
  1475. --pmp;
  1476. }
  1477. (void) new (pmp)saved_state(15);
  1478. m_backup_state = pmp;
  1479. }
  1480. /*
  1481. template <class BidiIterator, class Allocator, class traits>
  1482. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_parenthesis_pop(bool r)
  1483. {
  1484. saved_state* pmp = static_cast<saved_state*>(m_backup_state);
  1485. if(!r)
  1486. {
  1487. --parenthesis_stack_position;
  1488. }
  1489. boost::re_detail::inplace_destroy(pmp++);
  1490. m_backup_state = pmp;
  1491. return true;
  1492. }
  1493. template <class BidiIterator, class Allocator, class traits>
  1494. void perl_matcher<BidiIterator, Allocator, traits>::push_parenthesis_pop()
  1495. {
  1496. saved_state* pmp = static_cast<saved_state*>(m_backup_state);
  1497. --pmp;
  1498. if(pmp < m_stack_base)
  1499. {
  1500. extend_stack();
  1501. pmp = static_cast<saved_state*>(m_backup_state);
  1502. --pmp;
  1503. }
  1504. (void) new (pmp)saved_state(16);
  1505. m_backup_state = pmp;
  1506. }
  1507. template <class BidiIterator, class Allocator, class traits>
  1508. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_parenthesis_push(bool r)
  1509. {
  1510. saved_position<BidiIterator>* pmp = static_cast<saved_position<BidiIterator>*>(m_backup_state);
  1511. if(!r)
  1512. {
  1513. parenthesis_stack[parenthesis_stack_position++] = pmp->position;
  1514. }
  1515. boost::re_detail::inplace_destroy(pmp++);
  1516. m_backup_state = pmp;
  1517. return true;
  1518. }
  1519. template <class BidiIterator, class Allocator, class traits>
  1520. inline void perl_matcher<BidiIterator, Allocator, traits>::push_parenthesis_push(BidiIterator p)
  1521. {
  1522. saved_position<BidiIterator>* pmp = static_cast<saved_position<BidiIterator>*>(m_backup_state);
  1523. --pmp;
  1524. if(pmp < m_stack_base)
  1525. {
  1526. extend_stack();
  1527. pmp = static_cast<saved_position<BidiIterator>*>(m_backup_state);
  1528. --pmp;
  1529. }
  1530. (void) new (pmp)saved_position<BidiIterator>(0, p, 17);
  1531. m_backup_state = pmp;
  1532. }
  1533. */
  1534. } // namespace re_detail
  1535. } // namespace boost
  1536. #ifdef BOOST_MSVC
  1537. # pragma warning(pop)
  1538. #endif
  1539. #ifdef BOOST_MSVC
  1540. #pragma warning(push)
  1541. #pragma warning(disable: 4103)
  1542. #endif
  1543. #ifdef BOOST_HAS_ABI_HEADERS
  1544. # include BOOST_ABI_SUFFIX
  1545. #endif
  1546. #ifdef BOOST_MSVC
  1547. #pragma warning(pop)
  1548. #endif
  1549. #endif