basic_regex_creator.hpp 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  1. /*
  2. *
  3. * Copyright (c) 2004
  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 basic_regex_creator.cpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Declares template class basic_regex_creator which fills in
  16. * the data members of a regex_data object.
  17. */
  18. #ifndef BOOST_REGEX_V5_BASIC_REGEX_CREATOR_HPP
  19. #define BOOST_REGEX_V5_BASIC_REGEX_CREATOR_HPP
  20. #ifdef BOOST_REGEX_MSVC
  21. # pragma warning(push)
  22. #pragma warning(disable:4459)
  23. #if BOOST_REGEX_MSVC < 1910
  24. #pragma warning(disable:4800)
  25. #endif
  26. #endif
  27. #include <boost/regex/v5/basic_regex.hpp>
  28. #ifndef BOOST_REGEX_AS_MODULE
  29. #include <vector>
  30. #include <set>
  31. #endif
  32. namespace boost{
  33. namespace BOOST_REGEX_DETAIL_NS{
  34. template <class charT>
  35. struct digraph : public std::pair<charT, charT>
  36. {
  37. digraph() : std::pair<charT, charT>(charT(0), charT(0)){}
  38. digraph(charT c1) : std::pair<charT, charT>(c1, charT(0)){}
  39. digraph(charT c1, charT c2) : std::pair<charT, charT>(c1, c2)
  40. {}
  41. digraph(const digraph<charT>& d) : std::pair<charT, charT>(d.first, d.second){}
  42. digraph<charT>& operator=(const digraph<charT>&) = default;
  43. template <class Seq>
  44. digraph(const Seq& s) : std::pair<charT, charT>()
  45. {
  46. BOOST_REGEX_ASSERT(s.size() <= 2);
  47. BOOST_REGEX_ASSERT(s.size());
  48. this->first = s[0];
  49. this->second = (s.size() > 1) ? s[1] : 0;
  50. }
  51. };
  52. template <class charT, class traits>
  53. class basic_char_set
  54. {
  55. public:
  56. typedef digraph<charT> digraph_type;
  57. typedef typename traits::string_type string_type;
  58. typedef typename traits::char_class_type m_type;
  59. basic_char_set()
  60. {
  61. m_negate = false;
  62. m_has_digraphs = false;
  63. m_classes = 0;
  64. m_negated_classes = 0;
  65. m_empty = true;
  66. }
  67. void add_single(const digraph_type& s)
  68. {
  69. m_singles.insert(s);
  70. if(s.second)
  71. m_has_digraphs = true;
  72. m_empty = false;
  73. }
  74. void add_range(const digraph_type& first, const digraph_type& end)
  75. {
  76. m_ranges.push_back(first);
  77. m_ranges.push_back(end);
  78. if(first.second)
  79. {
  80. m_has_digraphs = true;
  81. add_single(first);
  82. }
  83. if(end.second)
  84. {
  85. m_has_digraphs = true;
  86. add_single(end);
  87. }
  88. m_empty = false;
  89. }
  90. void add_class(m_type m)
  91. {
  92. m_classes |= m;
  93. m_empty = false;
  94. }
  95. void add_negated_class(m_type m)
  96. {
  97. m_negated_classes |= m;
  98. m_empty = false;
  99. }
  100. void add_equivalent(const digraph_type& s)
  101. {
  102. m_equivalents.insert(s);
  103. if(s.second)
  104. {
  105. m_has_digraphs = true;
  106. add_single(s);
  107. }
  108. m_empty = false;
  109. }
  110. void negate()
  111. {
  112. m_negate = true;
  113. //m_empty = false;
  114. }
  115. //
  116. // accessor functions:
  117. //
  118. bool has_digraphs()const
  119. {
  120. return m_has_digraphs;
  121. }
  122. bool is_negated()const
  123. {
  124. return m_negate;
  125. }
  126. typedef typename std::vector<digraph_type>::const_iterator list_iterator;
  127. typedef typename std::set<digraph_type>::const_iterator set_iterator;
  128. set_iterator singles_begin()const
  129. {
  130. return m_singles.begin();
  131. }
  132. set_iterator singles_end()const
  133. {
  134. return m_singles.end();
  135. }
  136. list_iterator ranges_begin()const
  137. {
  138. return m_ranges.begin();
  139. }
  140. list_iterator ranges_end()const
  141. {
  142. return m_ranges.end();
  143. }
  144. set_iterator equivalents_begin()const
  145. {
  146. return m_equivalents.begin();
  147. }
  148. set_iterator equivalents_end()const
  149. {
  150. return m_equivalents.end();
  151. }
  152. m_type classes()const
  153. {
  154. return m_classes;
  155. }
  156. m_type negated_classes()const
  157. {
  158. return m_negated_classes;
  159. }
  160. bool empty()const
  161. {
  162. return m_empty;
  163. }
  164. private:
  165. std::set<digraph_type> m_singles; // a list of single characters to match
  166. std::vector<digraph_type> m_ranges; // a list of end points of our ranges
  167. bool m_negate; // true if the set is to be negated
  168. bool m_has_digraphs; // true if we have digraphs present
  169. m_type m_classes; // character classes to match
  170. m_type m_negated_classes; // negated character classes to match
  171. bool m_empty; // whether we've added anything yet
  172. std::set<digraph_type> m_equivalents; // a list of equivalence classes
  173. };
  174. template <class charT, class traits>
  175. class basic_regex_creator
  176. {
  177. public:
  178. basic_regex_creator(regex_data<charT, traits>* data);
  179. std::ptrdiff_t getoffset(void* addr)
  180. {
  181. return getoffset(addr, m_pdata->m_data.data());
  182. }
  183. std::ptrdiff_t getoffset(const void* addr, const void* base)
  184. {
  185. return static_cast<const char*>(addr) - static_cast<const char*>(base);
  186. }
  187. re_syntax_base* getaddress(std::ptrdiff_t off)
  188. {
  189. return getaddress(off, m_pdata->m_data.data());
  190. }
  191. re_syntax_base* getaddress(std::ptrdiff_t off, void* base)
  192. {
  193. return static_cast<re_syntax_base*>(static_cast<void*>(static_cast<char*>(base) + off));
  194. }
  195. void init(unsigned l_flags)
  196. {
  197. m_pdata->m_flags = l_flags;
  198. m_icase = l_flags & regex_constants::icase;
  199. }
  200. regbase::flag_type flags()
  201. {
  202. return m_pdata->m_flags;
  203. }
  204. void flags(regbase::flag_type f)
  205. {
  206. m_pdata->m_flags = f;
  207. if(m_icase != static_cast<bool>(f & regbase::icase))
  208. {
  209. m_icase = static_cast<bool>(f & regbase::icase);
  210. }
  211. }
  212. re_syntax_base* append_state(syntax_element_type t, std::size_t s);
  213. re_syntax_base* append_state(syntax_element_type t) { return append_state(t, sizeof(re_syntax_base)); }
  214. re_syntax_base* insert_state(std::ptrdiff_t pos, syntax_element_type t, std::size_t s);
  215. re_syntax_base* insert_state(std::ptrdiff_t pos, syntax_element_type t) { return insert_state(pos, t, sizeof(re_syntax_base)); }
  216. re_literal* append_literal(charT c);
  217. re_syntax_base* append_set(const basic_char_set<charT, traits>& char_set);
  218. re_syntax_base* append_set(const basic_char_set<charT, traits>& char_set, std::integral_constant<bool, false>*);
  219. re_syntax_base* append_set(const basic_char_set<charT, traits>& char_set, std::integral_constant<bool, true>*);
  220. void finalize(const charT* p1, const charT* p2);
  221. protected:
  222. regex_data<charT, traits>* m_pdata; // pointer to the basic_regex_data struct we are filling in
  223. const ::boost::regex_traits_wrapper<traits>&
  224. m_traits; // convenience reference to traits class
  225. re_syntax_base* m_last_state; // the last state we added
  226. bool m_icase; // true for case insensitive matches
  227. unsigned m_repeater_id; // the state_id of the next repeater
  228. bool m_has_backrefs; // true if there are actually any backrefs
  229. std::uintmax_t m_bad_repeats; // bitmask of repeats we can't deduce a startmap for;
  230. bool m_has_recursions; // set when we have recursive expressions to fixup
  231. std::vector<unsigned char> m_recursion_checks; // notes which recursions we've followed while analysing this expression
  232. typename traits::char_class_type m_word_mask; // mask used to determine if a character is a word character
  233. typename traits::char_class_type m_mask_space; // mask used to determine if a character is a word character
  234. typename traits::char_class_type m_lower_mask; // mask used to determine if a character is a lowercase character
  235. typename traits::char_class_type m_upper_mask; // mask used to determine if a character is an uppercase character
  236. typename traits::char_class_type m_alpha_mask; // mask used to determine if a character is an alphabetic character
  237. private:
  238. basic_regex_creator& operator=(const basic_regex_creator&);
  239. basic_regex_creator(const basic_regex_creator&);
  240. void fixup_pointers(re_syntax_base* state);
  241. void fixup_recursions(re_syntax_base* state);
  242. void create_startmaps(re_syntax_base* state);
  243. int calculate_backstep(re_syntax_base* state);
  244. void create_startmap(re_syntax_base* state, unsigned char* l_map, unsigned int* pnull, unsigned char mask, unsigned recursion_count = 0);
  245. unsigned get_restart_type(re_syntax_base* state);
  246. void set_all_masks(unsigned char* bits, unsigned char);
  247. bool is_bad_repeat(re_syntax_base* pt);
  248. void set_bad_repeat(re_syntax_base* pt);
  249. syntax_element_type get_repeat_type(re_syntax_base* state);
  250. void probe_leading_repeat(re_syntax_base* state);
  251. };
  252. template <class charT, class traits>
  253. basic_regex_creator<charT, traits>::basic_regex_creator(regex_data<charT, traits>* data)
  254. : m_pdata(data), m_traits(*(data->m_ptraits)), m_last_state(0), m_icase(false), m_repeater_id(0),
  255. m_has_backrefs(false), m_bad_repeats(0), m_has_recursions(false), m_word_mask(0), m_mask_space(0), m_lower_mask(0), m_upper_mask(0), m_alpha_mask(0)
  256. {
  257. m_pdata->m_data.clear();
  258. m_pdata->m_status = ::boost::regex_constants::error_ok;
  259. static const charT w = 'w';
  260. static const charT s = 's';
  261. static const charT l[5] = { 'l', 'o', 'w', 'e', 'r', };
  262. static const charT u[5] = { 'u', 'p', 'p', 'e', 'r', };
  263. static const charT a[5] = { 'a', 'l', 'p', 'h', 'a', };
  264. m_word_mask = m_traits.lookup_classname(&w, &w +1);
  265. m_mask_space = m_traits.lookup_classname(&s, &s +1);
  266. m_lower_mask = m_traits.lookup_classname(l, l + 5);
  267. m_upper_mask = m_traits.lookup_classname(u, u + 5);
  268. m_alpha_mask = m_traits.lookup_classname(a, a + 5);
  269. m_pdata->m_word_mask = m_word_mask;
  270. BOOST_REGEX_ASSERT(m_word_mask != 0);
  271. BOOST_REGEX_ASSERT(m_mask_space != 0);
  272. BOOST_REGEX_ASSERT(m_lower_mask != 0);
  273. BOOST_REGEX_ASSERT(m_upper_mask != 0);
  274. BOOST_REGEX_ASSERT(m_alpha_mask != 0);
  275. }
  276. template <class charT, class traits>
  277. re_syntax_base* basic_regex_creator<charT, traits>::append_state(syntax_element_type t, std::size_t s)
  278. {
  279. // if the state is a backref then make a note of it:
  280. if(t == syntax_element_backref)
  281. this->m_has_backrefs = true;
  282. // append a new state, start by aligning our last one:
  283. m_pdata->m_data.align();
  284. // set the offset to the next state in our last one:
  285. if(m_last_state)
  286. m_last_state->next.i = m_pdata->m_data.size() - getoffset(m_last_state);
  287. // now actually extend our data:
  288. m_last_state = static_cast<re_syntax_base*>(m_pdata->m_data.extend(s));
  289. // fill in boilerplate options in the new state:
  290. m_last_state->next.i = 0;
  291. m_last_state->type = t;
  292. return m_last_state;
  293. }
  294. template <class charT, class traits>
  295. re_syntax_base* basic_regex_creator<charT, traits>::insert_state(std::ptrdiff_t pos, syntax_element_type t, std::size_t s)
  296. {
  297. // append a new state, start by aligning our last one:
  298. m_pdata->m_data.align();
  299. // set the offset to the next state in our last one:
  300. if(m_last_state)
  301. m_last_state->next.i = m_pdata->m_data.size() - getoffset(m_last_state);
  302. // remember the last state position:
  303. std::ptrdiff_t off = getoffset(m_last_state) + s;
  304. // now actually insert our data:
  305. re_syntax_base* new_state = static_cast<re_syntax_base*>(m_pdata->m_data.insert(pos, s));
  306. // fill in boilerplate options in the new state:
  307. new_state->next.i = s;
  308. new_state->type = t;
  309. m_last_state = getaddress(off);
  310. return new_state;
  311. }
  312. template <class charT, class traits>
  313. re_literal* basic_regex_creator<charT, traits>::append_literal(charT c)
  314. {
  315. re_literal* result;
  316. // start by seeing if we have an existing re_literal we can extend:
  317. if((0 == m_last_state) || (m_last_state->type != syntax_element_literal))
  318. {
  319. // no existing re_literal, create a new one:
  320. result = static_cast<re_literal*>(append_state(syntax_element_literal, sizeof(re_literal) + sizeof(charT)));
  321. result->length = 1;
  322. *static_cast<charT*>(static_cast<void*>(result+1)) = m_traits.translate(c, m_icase);
  323. }
  324. else
  325. {
  326. // we have an existing re_literal, extend it:
  327. std::ptrdiff_t off = getoffset(m_last_state);
  328. m_pdata->m_data.extend(sizeof(charT));
  329. m_last_state = result = static_cast<re_literal*>(getaddress(off));
  330. charT* characters = static_cast<charT*>(static_cast<void*>(result+1));
  331. characters[result->length] = m_traits.translate(c, m_icase);
  332. result->length += 1;
  333. }
  334. return result;
  335. }
  336. template <class charT, class traits>
  337. inline re_syntax_base* basic_regex_creator<charT, traits>::append_set(
  338. const basic_char_set<charT, traits>& char_set)
  339. {
  340. typedef std::integral_constant<bool, (sizeof(charT) == 1) > truth_type;
  341. return char_set.has_digraphs()
  342. ? append_set(char_set, static_cast<std::integral_constant<bool, false>*>(0))
  343. : append_set(char_set, static_cast<truth_type*>(0));
  344. }
  345. template <class charT, class traits>
  346. re_syntax_base* basic_regex_creator<charT, traits>::append_set(
  347. const basic_char_set<charT, traits>& char_set, std::integral_constant<bool, false>*)
  348. {
  349. typedef typename traits::string_type string_type;
  350. typedef typename basic_char_set<charT, traits>::list_iterator item_iterator;
  351. typedef typename basic_char_set<charT, traits>::set_iterator set_iterator;
  352. typedef typename traits::char_class_type m_type;
  353. re_set_long<m_type>* result = static_cast<re_set_long<m_type>*>(append_state(syntax_element_long_set, sizeof(re_set_long<m_type>)));
  354. //
  355. // fill in the basics:
  356. //
  357. result->csingles = static_cast<unsigned int>(std::distance(char_set.singles_begin(), char_set.singles_end()));
  358. result->cranges = static_cast<unsigned int>(std::distance(char_set.ranges_begin(), char_set.ranges_end())) / 2;
  359. result->cequivalents = static_cast<unsigned int>(std::distance(char_set.equivalents_begin(), char_set.equivalents_end()));
  360. result->cclasses = char_set.classes();
  361. result->cnclasses = char_set.negated_classes();
  362. if(flags() & regbase::icase)
  363. {
  364. // adjust classes as needed:
  365. if(((result->cclasses & m_lower_mask) == m_lower_mask) || ((result->cclasses & m_upper_mask) == m_upper_mask))
  366. result->cclasses |= m_alpha_mask;
  367. if(((result->cnclasses & m_lower_mask) == m_lower_mask) || ((result->cnclasses & m_upper_mask) == m_upper_mask))
  368. result->cnclasses |= m_alpha_mask;
  369. }
  370. result->isnot = char_set.is_negated();
  371. result->singleton = !char_set.has_digraphs();
  372. //
  373. // remember where the state is for later:
  374. //
  375. std::ptrdiff_t offset = getoffset(result);
  376. //
  377. // now extend with all the singles:
  378. //
  379. item_iterator first, last;
  380. set_iterator sfirst, slast;
  381. sfirst = char_set.singles_begin();
  382. slast = char_set.singles_end();
  383. while(sfirst != slast)
  384. {
  385. charT* p = static_cast<charT*>(this->m_pdata->m_data.extend(sizeof(charT) * (sfirst->first == static_cast<charT>(0) ? 1 : sfirst->second ? 3 : 2)));
  386. p[0] = m_traits.translate(sfirst->first, m_icase);
  387. if(sfirst->first == static_cast<charT>(0))
  388. {
  389. p[0] = 0;
  390. }
  391. else if(sfirst->second)
  392. {
  393. p[1] = m_traits.translate(sfirst->second, m_icase);
  394. p[2] = 0;
  395. }
  396. else
  397. p[1] = 0;
  398. ++sfirst;
  399. }
  400. //
  401. // now extend with all the ranges:
  402. //
  403. first = char_set.ranges_begin();
  404. last = char_set.ranges_end();
  405. while(first != last)
  406. {
  407. // first grab the endpoints of the range:
  408. digraph<charT> c1 = *first;
  409. c1.first = this->m_traits.translate(c1.first, this->m_icase);
  410. c1.second = this->m_traits.translate(c1.second, this->m_icase);
  411. ++first;
  412. digraph<charT> c2 = *first;
  413. c2.first = this->m_traits.translate(c2.first, this->m_icase);
  414. c2.second = this->m_traits.translate(c2.second, this->m_icase);
  415. ++first;
  416. string_type s1, s2;
  417. // different actions now depending upon whether collation is turned on:
  418. if(flags() & regex_constants::collate)
  419. {
  420. // we need to transform our range into sort keys:
  421. charT a1[3] = { c1.first, c1.second, charT(0), };
  422. charT a2[3] = { c2.first, c2.second, charT(0), };
  423. s1 = this->m_traits.transform(a1, (a1[1] ? a1+2 : a1+1));
  424. s2 = this->m_traits.transform(a2, (a2[1] ? a2+2 : a2+1));
  425. if(s1.empty())
  426. s1 = string_type(1, charT(0));
  427. if(s2.empty())
  428. s2 = string_type(1, charT(0));
  429. }
  430. else
  431. {
  432. if(c1.second)
  433. {
  434. s1.insert(s1.end(), c1.first);
  435. s1.insert(s1.end(), c1.second);
  436. }
  437. else
  438. s1 = string_type(1, c1.first);
  439. if(c2.second)
  440. {
  441. s2.insert(s2.end(), c2.first);
  442. s2.insert(s2.end(), c2.second);
  443. }
  444. else
  445. s2.insert(s2.end(), c2.first);
  446. }
  447. if(s1 > s2)
  448. {
  449. // Oops error:
  450. return 0;
  451. }
  452. charT* p = static_cast<charT*>(this->m_pdata->m_data.extend(sizeof(charT) * (s1.size() + s2.size() + 2) ) );
  453. BOOST_REGEX_DETAIL_NS::copy(s1.begin(), s1.end(), p);
  454. p[s1.size()] = charT(0);
  455. p += s1.size() + 1;
  456. BOOST_REGEX_DETAIL_NS::copy(s2.begin(), s2.end(), p);
  457. p[s2.size()] = charT(0);
  458. }
  459. //
  460. // now process the equivalence classes:
  461. //
  462. sfirst = char_set.equivalents_begin();
  463. slast = char_set.equivalents_end();
  464. while(sfirst != slast)
  465. {
  466. string_type s;
  467. if(sfirst->second)
  468. {
  469. charT cs[3] = { sfirst->first, sfirst->second, charT(0), };
  470. s = m_traits.transform_primary(cs, cs+2);
  471. }
  472. else
  473. s = m_traits.transform_primary(&sfirst->first, &sfirst->first+1);
  474. if(s.empty())
  475. return 0; // invalid or unsupported equivalence class
  476. charT* p = static_cast<charT*>(this->m_pdata->m_data.extend(sizeof(charT) * (s.size()+1) ) );
  477. BOOST_REGEX_DETAIL_NS::copy(s.begin(), s.end(), p);
  478. p[s.size()] = charT(0);
  479. ++sfirst;
  480. }
  481. //
  482. // finally reset the address of our last state:
  483. //
  484. m_last_state = result = static_cast<re_set_long<m_type>*>(getaddress(offset));
  485. return result;
  486. }
  487. template<class T>
  488. inline bool char_less(T t1, T t2)
  489. {
  490. return t1 < t2;
  491. }
  492. inline bool char_less(char t1, char t2)
  493. {
  494. return static_cast<unsigned char>(t1) < static_cast<unsigned char>(t2);
  495. }
  496. inline bool char_less(signed char t1, signed char t2)
  497. {
  498. return static_cast<unsigned char>(t1) < static_cast<unsigned char>(t2);
  499. }
  500. template <class charT, class traits>
  501. re_syntax_base* basic_regex_creator<charT, traits>::append_set(
  502. const basic_char_set<charT, traits>& char_set, std::integral_constant<bool, true>*)
  503. {
  504. typedef typename traits::string_type string_type;
  505. typedef typename basic_char_set<charT, traits>::list_iterator item_iterator;
  506. typedef typename basic_char_set<charT, traits>::set_iterator set_iterator;
  507. re_set* result = static_cast<re_set*>(append_state(syntax_element_set, sizeof(re_set)));
  508. bool negate = char_set.is_negated();
  509. std::memset(result->_map, 0, sizeof(result->_map));
  510. //
  511. // handle singles first:
  512. //
  513. item_iterator first, last;
  514. set_iterator sfirst, slast;
  515. sfirst = char_set.singles_begin();
  516. slast = char_set.singles_end();
  517. while(sfirst != slast)
  518. {
  519. for(unsigned int i = 0; i < (1 << CHAR_BIT); ++i)
  520. {
  521. if(this->m_traits.translate(static_cast<charT>(i), this->m_icase)
  522. == this->m_traits.translate(sfirst->first, this->m_icase))
  523. result->_map[i] = true;
  524. }
  525. ++sfirst;
  526. }
  527. //
  528. // OK now handle ranges:
  529. //
  530. first = char_set.ranges_begin();
  531. last = char_set.ranges_end();
  532. while(first != last)
  533. {
  534. // first grab the endpoints of the range:
  535. charT c1 = this->m_traits.translate(first->first, this->m_icase);
  536. ++first;
  537. charT c2 = this->m_traits.translate(first->first, this->m_icase);
  538. ++first;
  539. // different actions now depending upon whether collation is turned on:
  540. if(flags() & regex_constants::collate)
  541. {
  542. // we need to transform our range into sort keys:
  543. charT c3[2] = { c1, charT(0), };
  544. string_type s1 = this->m_traits.transform(c3, c3+1);
  545. c3[0] = c2;
  546. string_type s2 = this->m_traits.transform(c3, c3+1);
  547. if(s1 > s2)
  548. {
  549. // Oops error:
  550. return 0;
  551. }
  552. BOOST_REGEX_ASSERT(c3[1] == charT(0));
  553. for(unsigned i = 0; i < (1u << CHAR_BIT); ++i)
  554. {
  555. c3[0] = static_cast<charT>(i);
  556. string_type s3 = this->m_traits.transform(c3, c3 +1);
  557. if((s1 <= s3) && (s3 <= s2))
  558. result->_map[i] = true;
  559. }
  560. }
  561. else
  562. {
  563. if(char_less(c2, c1))
  564. {
  565. // Oops error:
  566. return 0;
  567. }
  568. // everything in range matches:
  569. std::memset(result->_map + static_cast<unsigned char>(c1), true, static_cast<unsigned char>(1u) + static_cast<unsigned char>(static_cast<unsigned char>(c2) - static_cast<unsigned char>(c1)));
  570. }
  571. }
  572. //
  573. // and now the classes:
  574. //
  575. typedef typename traits::char_class_type m_type;
  576. m_type m = char_set.classes();
  577. if(flags() & regbase::icase)
  578. {
  579. // adjust m as needed:
  580. if(((m & m_lower_mask) == m_lower_mask) || ((m & m_upper_mask) == m_upper_mask))
  581. m |= m_alpha_mask;
  582. }
  583. if(m != 0)
  584. {
  585. for(unsigned i = 0; i < (1u << CHAR_BIT); ++i)
  586. {
  587. if(this->m_traits.isctype(static_cast<charT>(i), m))
  588. result->_map[i] = true;
  589. }
  590. }
  591. //
  592. // and now the negated classes:
  593. //
  594. m = char_set.negated_classes();
  595. if(flags() & regbase::icase)
  596. {
  597. // adjust m as needed:
  598. if(((m & m_lower_mask) == m_lower_mask) || ((m & m_upper_mask) == m_upper_mask))
  599. m |= m_alpha_mask;
  600. }
  601. if(m != 0)
  602. {
  603. for(unsigned i = 0; i < (1u << CHAR_BIT); ++i)
  604. {
  605. if(0 == this->m_traits.isctype(static_cast<charT>(i), m))
  606. result->_map[i] = true;
  607. }
  608. }
  609. //
  610. // now process the equivalence classes:
  611. //
  612. sfirst = char_set.equivalents_begin();
  613. slast = char_set.equivalents_end();
  614. while(sfirst != slast)
  615. {
  616. string_type s;
  617. BOOST_REGEX_ASSERT(static_cast<charT>(0) == sfirst->second);
  618. s = m_traits.transform_primary(&sfirst->first, &sfirst->first+1);
  619. if(s.empty())
  620. return 0; // invalid or unsupported equivalence class
  621. for(unsigned i = 0; i < (1u << CHAR_BIT); ++i)
  622. {
  623. charT c[2] = { (static_cast<charT>(i)), charT(0), };
  624. string_type s2 = this->m_traits.transform_primary(c, c+1);
  625. if(s == s2)
  626. result->_map[i] = true;
  627. }
  628. ++sfirst;
  629. }
  630. if(negate)
  631. {
  632. for(unsigned i = 0; i < (1u << CHAR_BIT); ++i)
  633. {
  634. result->_map[i] = !(result->_map[i]);
  635. }
  636. }
  637. return result;
  638. }
  639. template <class charT, class traits>
  640. void basic_regex_creator<charT, traits>::finalize(const charT* p1, const charT* p2)
  641. {
  642. if(this->m_pdata->m_status)
  643. return;
  644. // we've added all the states we need, now finish things off.
  645. // start by adding a terminating state:
  646. append_state(syntax_element_match);
  647. // extend storage to store original expression:
  648. std::ptrdiff_t len = p2 - p1;
  649. m_pdata->m_expression_len = len;
  650. charT* ps = static_cast<charT*>(m_pdata->m_data.extend(sizeof(charT) * (1 + (p2 - p1))));
  651. m_pdata->m_expression = ps;
  652. BOOST_REGEX_DETAIL_NS::copy(p1, p2, ps);
  653. ps[p2 - p1] = 0;
  654. // fill in our other data...
  655. // successful parsing implies a zero status:
  656. m_pdata->m_status = 0;
  657. // get the first state of the machine:
  658. m_pdata->m_first_state = static_cast<re_syntax_base*>(m_pdata->m_data.data());
  659. // fixup pointers in the machine:
  660. fixup_pointers(m_pdata->m_first_state);
  661. if(m_has_recursions)
  662. {
  663. m_pdata->m_has_recursions = true;
  664. fixup_recursions(m_pdata->m_first_state);
  665. if(this->m_pdata->m_status)
  666. return;
  667. }
  668. else
  669. m_pdata->m_has_recursions = false;
  670. // create nested startmaps:
  671. create_startmaps(m_pdata->m_first_state);
  672. // create main startmap:
  673. std::memset(m_pdata->m_startmap, 0, sizeof(m_pdata->m_startmap));
  674. m_pdata->m_can_be_null = 0;
  675. m_bad_repeats = 0;
  676. if(m_has_recursions)
  677. m_recursion_checks.assign(1 + m_pdata->m_mark_count, 0u);
  678. create_startmap(m_pdata->m_first_state, m_pdata->m_startmap, &(m_pdata->m_can_be_null), mask_all);
  679. // get the restart type:
  680. m_pdata->m_restart_type = get_restart_type(m_pdata->m_first_state);
  681. // optimise a leading repeat if there is one:
  682. probe_leading_repeat(m_pdata->m_first_state);
  683. }
  684. template <class charT, class traits>
  685. void basic_regex_creator<charT, traits>::fixup_pointers(re_syntax_base* state)
  686. {
  687. while(state)
  688. {
  689. switch(state->type)
  690. {
  691. case syntax_element_recurse:
  692. m_has_recursions = true;
  693. if(state->next.i)
  694. state->next.p = getaddress(state->next.i, state);
  695. else
  696. state->next.p = 0;
  697. break;
  698. case syntax_element_rep:
  699. case syntax_element_dot_rep:
  700. case syntax_element_char_rep:
  701. case syntax_element_short_set_rep:
  702. case syntax_element_long_set_rep:
  703. // set the state_id of this repeat:
  704. static_cast<re_repeat*>(state)->state_id = m_repeater_id++;
  705. BOOST_REGEX_FALLTHROUGH;
  706. case syntax_element_alt:
  707. std::memset(static_cast<re_alt*>(state)->_map, 0, sizeof(static_cast<re_alt*>(state)->_map));
  708. static_cast<re_alt*>(state)->can_be_null = 0;
  709. BOOST_REGEX_FALLTHROUGH;
  710. case syntax_element_jump:
  711. static_cast<re_jump*>(state)->alt.p = getaddress(static_cast<re_jump*>(state)->alt.i, state);
  712. BOOST_REGEX_FALLTHROUGH;
  713. default:
  714. if(state->next.i)
  715. state->next.p = getaddress(state->next.i, state);
  716. else
  717. state->next.p = 0;
  718. }
  719. state = state->next.p;
  720. }
  721. }
  722. template <class charT, class traits>
  723. void basic_regex_creator<charT, traits>::fixup_recursions(re_syntax_base* state)
  724. {
  725. re_syntax_base* base = state;
  726. while(state)
  727. {
  728. switch(state->type)
  729. {
  730. case syntax_element_assert_backref:
  731. {
  732. // just check that the index is valid:
  733. int idx = static_cast<const re_brace*>(state)->index;
  734. if(idx < 0)
  735. {
  736. idx = -idx-1;
  737. if(idx >= hash_value_mask)
  738. {
  739. idx = m_pdata->get_id(idx);
  740. if(idx <= 0)
  741. {
  742. // check of sub-expression that doesn't exist:
  743. if(0 == this->m_pdata->m_status) // update the error code if not already set
  744. this->m_pdata->m_status = boost::regex_constants::error_bad_pattern;
  745. //
  746. // clear the expression, we should be empty:
  747. //
  748. this->m_pdata->m_expression = 0;
  749. this->m_pdata->m_expression_len = 0;
  750. //
  751. // and throw if required:
  752. //
  753. if(0 == (this->flags() & regex_constants::no_except))
  754. {
  755. std::string message = "Encountered a forward reference to a marked sub-expression that does not exist.";
  756. boost::regex_error e(message, boost::regex_constants::error_bad_pattern, 0);
  757. e.raise();
  758. }
  759. }
  760. }
  761. }
  762. }
  763. break;
  764. case syntax_element_recurse:
  765. {
  766. bool ok = false;
  767. re_syntax_base* p = base;
  768. std::ptrdiff_t idx = static_cast<re_jump*>(state)->alt.i;
  769. if(idx >= hash_value_mask)
  770. {
  771. //
  772. // There may be more than one capture group with this hash, just do what Perl
  773. // does and recurse to the leftmost:
  774. //
  775. idx = m_pdata->get_id(static_cast<int>(idx));
  776. }
  777. if(idx < 0)
  778. {
  779. ok = false;
  780. }
  781. else
  782. {
  783. while(p)
  784. {
  785. if((p->type == syntax_element_startmark) && (static_cast<re_brace*>(p)->index == idx))
  786. {
  787. //
  788. // We've found the target of the recursion, set the jump target:
  789. //
  790. static_cast<re_jump*>(state)->alt.p = p;
  791. ok = true;
  792. //
  793. // Now scan the target for nested repeats:
  794. //
  795. p = p->next.p;
  796. int next_rep_id = 0;
  797. while(p)
  798. {
  799. switch(p->type)
  800. {
  801. case syntax_element_rep:
  802. case syntax_element_dot_rep:
  803. case syntax_element_char_rep:
  804. case syntax_element_short_set_rep:
  805. case syntax_element_long_set_rep:
  806. next_rep_id = static_cast<re_repeat*>(p)->state_id;
  807. break;
  808. case syntax_element_endmark:
  809. if(static_cast<const re_brace*>(p)->index == idx)
  810. next_rep_id = -1;
  811. break;
  812. default:
  813. break;
  814. }
  815. if(next_rep_id)
  816. break;
  817. p = p->next.p;
  818. }
  819. if(next_rep_id > 0)
  820. {
  821. static_cast<re_recurse*>(state)->state_id = next_rep_id - 1;
  822. }
  823. break;
  824. }
  825. p = p->next.p;
  826. }
  827. }
  828. if(!ok)
  829. {
  830. // recursion to sub-expression that doesn't exist:
  831. if(0 == this->m_pdata->m_status) // update the error code if not already set
  832. this->m_pdata->m_status = boost::regex_constants::error_bad_pattern;
  833. //
  834. // clear the expression, we should be empty:
  835. //
  836. this->m_pdata->m_expression = 0;
  837. this->m_pdata->m_expression_len = 0;
  838. //
  839. // and throw if required:
  840. //
  841. if(0 == (this->flags() & regex_constants::no_except))
  842. {
  843. std::string message = "Encountered a forward reference to a recursive sub-expression that does not exist.";
  844. boost::regex_error e(message, boost::regex_constants::error_bad_pattern, 0);
  845. e.raise();
  846. }
  847. }
  848. }
  849. break;
  850. default:
  851. break;
  852. }
  853. state = state->next.p;
  854. }
  855. }
  856. template <class charT, class traits>
  857. void basic_regex_creator<charT, traits>::create_startmaps(re_syntax_base* state)
  858. {
  859. // non-recursive implementation:
  860. // create the last map in the machine first, so that earlier maps
  861. // can make use of the result...
  862. //
  863. // This was originally a recursive implementation, but that caused stack
  864. // overflows with complex expressions on small stacks (think COM+).
  865. // start by saving the case setting:
  866. bool l_icase = m_icase;
  867. std::vector<std::pair<bool, re_syntax_base*> > v;
  868. while(state)
  869. {
  870. switch(state->type)
  871. {
  872. case syntax_element_toggle_case:
  873. // we need to track case changes here:
  874. m_icase = static_cast<re_case*>(state)->icase;
  875. state = state->next.p;
  876. continue;
  877. case syntax_element_alt:
  878. case syntax_element_rep:
  879. case syntax_element_dot_rep:
  880. case syntax_element_char_rep:
  881. case syntax_element_short_set_rep:
  882. case syntax_element_long_set_rep:
  883. // just push the state onto our stack for now:
  884. v.push_back(std::pair<bool, re_syntax_base*>(m_icase, state));
  885. state = state->next.p;
  886. break;
  887. case syntax_element_backstep:
  888. // we need to calculate how big the backstep is:
  889. static_cast<re_brace*>(state)->index
  890. = this->calculate_backstep(state->next.p);
  891. if(static_cast<re_brace*>(state)->index < 0)
  892. {
  893. // Oops error:
  894. if(0 == this->m_pdata->m_status) // update the error code if not already set
  895. this->m_pdata->m_status = boost::regex_constants::error_bad_pattern;
  896. //
  897. // clear the expression, we should be empty:
  898. //
  899. this->m_pdata->m_expression = 0;
  900. this->m_pdata->m_expression_len = 0;
  901. //
  902. // and throw if required:
  903. //
  904. if(0 == (this->flags() & regex_constants::no_except))
  905. {
  906. std::string message = "Invalid lookbehind assertion encountered in the regular expression.";
  907. boost::regex_error e(message, boost::regex_constants::error_bad_pattern, 0);
  908. e.raise();
  909. }
  910. }
  911. BOOST_REGEX_FALLTHROUGH;
  912. default:
  913. state = state->next.p;
  914. }
  915. }
  916. // now work through our list, building all the maps as we go:
  917. while(!v.empty())
  918. {
  919. // Initialize m_recursion_checks if we need it:
  920. if(m_has_recursions)
  921. m_recursion_checks.assign(1 + m_pdata->m_mark_count, 0u);
  922. const std::pair<bool, re_syntax_base*>& p = v.back();
  923. m_icase = p.first;
  924. state = p.second;
  925. v.pop_back();
  926. // Build maps:
  927. m_bad_repeats = 0;
  928. create_startmap(state->next.p, static_cast<re_alt*>(state)->_map, &static_cast<re_alt*>(state)->can_be_null, mask_take);
  929. m_bad_repeats = 0;
  930. if(m_has_recursions)
  931. m_recursion_checks.assign(1 + m_pdata->m_mark_count, 0u);
  932. create_startmap(static_cast<re_alt*>(state)->alt.p, static_cast<re_alt*>(state)->_map, &static_cast<re_alt*>(state)->can_be_null, mask_skip);
  933. // adjust the type of the state to allow for faster matching:
  934. state->type = this->get_repeat_type(state);
  935. }
  936. // restore case sensitivity:
  937. m_icase = l_icase;
  938. }
  939. template <class charT, class traits>
  940. int basic_regex_creator<charT, traits>::calculate_backstep(re_syntax_base* state)
  941. {
  942. typedef typename traits::char_class_type m_type;
  943. int result = 0;
  944. int last_alternative_result = -1;
  945. std::vector<std::tuple<int, re_syntax_base*>> stack;
  946. while(state)
  947. {
  948. switch(state->type)
  949. {
  950. case syntax_element_startmark:
  951. if((static_cast<re_brace*>(state)->index == -1)
  952. || (static_cast<re_brace*>(state)->index == -2))
  953. {
  954. state = static_cast<re_jump*>(state->next.p)->alt.p->next.p;
  955. continue;
  956. }
  957. else if(static_cast<re_brace*>(state)->index == -3)
  958. {
  959. state = state->next.p->next.p;
  960. continue;
  961. }
  962. break;
  963. case syntax_element_endmark:
  964. if ((static_cast<re_brace*>(state)->index == -1)
  965. || (static_cast<re_brace*>(state)->index == -2))
  966. {
  967. // We've finished the calculation, check against any previous alternatives:
  968. if (last_alternative_result >= 0)
  969. {
  970. if (last_alternative_result != result)
  971. return -1;
  972. }
  973. else
  974. last_alternative_result = result;
  975. if (stack.size())
  976. {
  977. // Skip to next alternative and calculate that as well:
  978. std::tie(result, state) = stack.back();
  979. stack.pop_back();
  980. continue;
  981. }
  982. else
  983. return result;
  984. }
  985. break;
  986. case syntax_element_literal:
  987. result += static_cast<re_literal*>(state)->length;
  988. break;
  989. case syntax_element_wild:
  990. case syntax_element_set:
  991. result += 1;
  992. break;
  993. case syntax_element_dot_rep:
  994. case syntax_element_char_rep:
  995. case syntax_element_short_set_rep:
  996. case syntax_element_backref:
  997. case syntax_element_rep:
  998. case syntax_element_combining:
  999. case syntax_element_long_set_rep:
  1000. case syntax_element_backstep:
  1001. {
  1002. re_repeat* rep = static_cast<re_repeat *>(state);
  1003. // adjust the type of the state to allow for faster matching:
  1004. state->type = this->get_repeat_type(state);
  1005. if((state->type == syntax_element_dot_rep)
  1006. || (state->type == syntax_element_char_rep)
  1007. || (state->type == syntax_element_short_set_rep))
  1008. {
  1009. if(rep->max != rep->min)
  1010. return -1;
  1011. if (static_cast<std::size_t>((std::numeric_limits<int>::max)() - result) < rep->min)
  1012. return -1; // protection against overflow, we can't calculate a backstep in this case and the expression is probably ill-formed.
  1013. result += static_cast<int>(rep->min);
  1014. state = rep->alt.p;
  1015. continue;
  1016. }
  1017. else if(state->type == syntax_element_long_set_rep)
  1018. {
  1019. BOOST_REGEX_ASSERT(rep->next.p->type == syntax_element_long_set);
  1020. if(static_cast<re_set_long<m_type>*>(rep->next.p)->singleton == 0)
  1021. return -1;
  1022. if(rep->max != rep->min)
  1023. return -1;
  1024. result += static_cast<int>(rep->min);
  1025. state = rep->alt.p;
  1026. continue;
  1027. }
  1028. }
  1029. return -1;
  1030. case syntax_element_long_set:
  1031. if(static_cast<re_set_long<m_type>*>(state)->singleton == 0)
  1032. return -1;
  1033. result += 1;
  1034. break;
  1035. case syntax_element_jump:
  1036. state = static_cast<re_jump*>(state)->alt.p;
  1037. continue;
  1038. case syntax_element_alt:
  1039. {
  1040. // Push the alternative if we haven't pushed too many already:
  1041. if(stack.size() > BOOST_REGEX_MAX_BLOCKS)
  1042. return -1;
  1043. stack.push_back(std::make_tuple(result, static_cast<re_alt*>(state)->alt.p));
  1044. // and take the first one:
  1045. state = state->next.p;
  1046. continue;
  1047. }
  1048. default:
  1049. break;
  1050. }
  1051. state = state->next.p;
  1052. }
  1053. return -1;
  1054. }
  1055. struct recursion_saver
  1056. {
  1057. std::vector<unsigned char> saved_state;
  1058. std::vector<unsigned char>* state;
  1059. recursion_saver(std::vector<unsigned char>* p) : saved_state(*p), state(p) {}
  1060. ~recursion_saver()
  1061. {
  1062. state->swap(saved_state);
  1063. }
  1064. };
  1065. template <class charT, class traits>
  1066. void basic_regex_creator<charT, traits>::create_startmap(re_syntax_base* state, unsigned char* l_map, unsigned int* pnull, unsigned char mask, unsigned recursion_count)
  1067. {
  1068. recursion_saver saved_recursions(&m_recursion_checks);
  1069. int not_last_jump = 1;
  1070. re_syntax_base* recursion_start = 0;
  1071. int recursion_sub = 0;
  1072. re_syntax_base* recursion_restart = 0;
  1073. // track case sensitivity:
  1074. bool l_icase = m_icase;
  1075. if (recursion_count > BOOST_REGEX_MAX_RECURSION_DEPTH)
  1076. {
  1077. // Oops error:
  1078. if (0 == this->m_pdata->m_status) // update the error code if not already set
  1079. this->m_pdata->m_status = boost::regex_constants::error_complexity;
  1080. //
  1081. // clear the expression, we should be empty:
  1082. //
  1083. this->m_pdata->m_expression = 0;
  1084. this->m_pdata->m_expression_len = 0;
  1085. //
  1086. // and throw if required:
  1087. //
  1088. if (0 == (this->flags() & regex_constants::no_except))
  1089. {
  1090. std::string message = "Expression complexity exceeded.";
  1091. boost::regex_error e(message, boost::regex_constants::error_complexity, 0);
  1092. e.raise();
  1093. }
  1094. return;
  1095. }
  1096. while(state)
  1097. {
  1098. switch(state->type)
  1099. {
  1100. case syntax_element_toggle_case:
  1101. l_icase = static_cast<re_case*>(state)->icase;
  1102. state = state->next.p;
  1103. break;
  1104. case syntax_element_literal:
  1105. {
  1106. // don't set anything in *pnull, set each element in l_map
  1107. // that could match the first character in the literal:
  1108. if(l_map)
  1109. {
  1110. l_map[0] |= mask_init;
  1111. charT first_char = *static_cast<charT*>(static_cast<void*>(static_cast<re_literal*>(state) + 1));
  1112. for(unsigned int i = 0; i < (1u << CHAR_BIT); ++i)
  1113. {
  1114. if(m_traits.translate(static_cast<charT>(i), l_icase) == first_char)
  1115. l_map[i] |= mask;
  1116. }
  1117. }
  1118. return;
  1119. }
  1120. case syntax_element_end_line:
  1121. {
  1122. // next character must be a line separator (if there is one):
  1123. if(l_map)
  1124. {
  1125. l_map[0] |= mask_init;
  1126. l_map[static_cast<unsigned>('\n')] |= mask;
  1127. l_map[static_cast<unsigned>('\r')] |= mask;
  1128. l_map[static_cast<unsigned>('\f')] |= mask;
  1129. l_map[0x85] |= mask;
  1130. }
  1131. // now figure out if we can match a NULL string at this point:
  1132. if(pnull)
  1133. create_startmap(state->next.p, 0, pnull, mask, ++recursion_count);
  1134. return;
  1135. }
  1136. case syntax_element_recurse:
  1137. {
  1138. BOOST_REGEX_ASSERT(static_cast<const re_jump*>(state)->alt.p->type == syntax_element_startmark);
  1139. recursion_sub = static_cast<re_brace*>(static_cast<const re_jump*>(state)->alt.p)->index;
  1140. if(m_recursion_checks[recursion_sub] & 1u)
  1141. {
  1142. // Infinite recursion!!
  1143. if(0 == this->m_pdata->m_status) // update the error code if not already set
  1144. this->m_pdata->m_status = boost::regex_constants::error_bad_pattern;
  1145. //
  1146. // clear the expression, we should be empty:
  1147. //
  1148. this->m_pdata->m_expression = 0;
  1149. this->m_pdata->m_expression_len = 0;
  1150. //
  1151. // and throw if required:
  1152. //
  1153. if(0 == (this->flags() & regex_constants::no_except))
  1154. {
  1155. std::string message = "Encountered an infinite recursion.";
  1156. boost::regex_error e(message, boost::regex_constants::error_bad_pattern, 0);
  1157. e.raise();
  1158. }
  1159. }
  1160. else if(recursion_start == 0)
  1161. {
  1162. recursion_start = state;
  1163. recursion_restart = state->next.p;
  1164. state = static_cast<re_jump*>(state)->alt.p;
  1165. m_recursion_checks[recursion_sub] |= 1u;
  1166. break;
  1167. }
  1168. m_recursion_checks[recursion_sub] |= 1u;
  1169. // can't handle nested recursion here...
  1170. BOOST_REGEX_FALLTHROUGH;
  1171. }
  1172. case syntax_element_backref:
  1173. // can be null, and any character can match:
  1174. if(pnull)
  1175. *pnull |= mask;
  1176. BOOST_REGEX_FALLTHROUGH;
  1177. case syntax_element_wild:
  1178. {
  1179. // can't be null, any character can match:
  1180. set_all_masks(l_map, mask);
  1181. return;
  1182. }
  1183. case syntax_element_accept:
  1184. case syntax_element_match:
  1185. {
  1186. // must be null, any character can match:
  1187. set_all_masks(l_map, mask);
  1188. if(pnull)
  1189. *pnull |= mask;
  1190. return;
  1191. }
  1192. case syntax_element_word_start:
  1193. {
  1194. // recurse, then AND with all the word characters:
  1195. create_startmap(state->next.p, l_map, pnull, mask, ++recursion_count);
  1196. if(l_map)
  1197. {
  1198. l_map[0] |= mask_init;
  1199. for(unsigned int i = 0; i < (1u << CHAR_BIT); ++i)
  1200. {
  1201. if(!m_traits.isctype(static_cast<charT>(i), m_word_mask))
  1202. l_map[i] &= static_cast<unsigned char>(~mask);
  1203. }
  1204. }
  1205. return;
  1206. }
  1207. case syntax_element_word_end:
  1208. {
  1209. // recurse, then AND with all the word characters:
  1210. create_startmap(state->next.p, l_map, pnull, mask, ++recursion_count);
  1211. if(l_map)
  1212. {
  1213. l_map[0] |= mask_init;
  1214. for(unsigned int i = 0; i < (1u << CHAR_BIT); ++i)
  1215. {
  1216. if(m_traits.isctype(static_cast<charT>(i), m_word_mask))
  1217. l_map[i] &= static_cast<unsigned char>(~mask);
  1218. }
  1219. }
  1220. return;
  1221. }
  1222. case syntax_element_buffer_end:
  1223. {
  1224. // we *must be null* :
  1225. if(pnull)
  1226. *pnull |= mask;
  1227. return;
  1228. }
  1229. case syntax_element_long_set:
  1230. if(l_map)
  1231. {
  1232. typedef typename traits::char_class_type m_type;
  1233. if(static_cast<re_set_long<m_type>*>(state)->singleton)
  1234. {
  1235. l_map[0] |= mask_init;
  1236. for(unsigned int i = 0; i < (1u << CHAR_BIT); ++i)
  1237. {
  1238. charT c = static_cast<charT>(i);
  1239. if(&c != re_is_set_member(&c, &c + 1, static_cast<re_set_long<m_type>*>(state), *m_pdata, l_icase))
  1240. l_map[i] |= mask;
  1241. }
  1242. }
  1243. else
  1244. set_all_masks(l_map, mask);
  1245. }
  1246. return;
  1247. case syntax_element_set:
  1248. if(l_map)
  1249. {
  1250. l_map[0] |= mask_init;
  1251. for(unsigned int i = 0; i < (1u << CHAR_BIT); ++i)
  1252. {
  1253. if(static_cast<re_set*>(state)->_map[
  1254. static_cast<unsigned char>(m_traits.translate(static_cast<charT>(i), l_icase))])
  1255. l_map[i] |= mask;
  1256. }
  1257. }
  1258. return;
  1259. case syntax_element_jump:
  1260. // take the jump:
  1261. state = static_cast<re_alt*>(state)->alt.p;
  1262. not_last_jump = -1;
  1263. break;
  1264. case syntax_element_alt:
  1265. case syntax_element_rep:
  1266. case syntax_element_dot_rep:
  1267. case syntax_element_char_rep:
  1268. case syntax_element_short_set_rep:
  1269. case syntax_element_long_set_rep:
  1270. {
  1271. re_alt* rep = static_cast<re_alt*>(state);
  1272. if(rep->_map[0] & mask_init)
  1273. {
  1274. if(l_map)
  1275. {
  1276. // copy previous results:
  1277. l_map[0] |= mask_init;
  1278. for(unsigned int i = 0; i <= UCHAR_MAX; ++i)
  1279. {
  1280. if(rep->_map[i] & mask_any)
  1281. l_map[i] |= mask;
  1282. }
  1283. }
  1284. if(pnull)
  1285. {
  1286. if(rep->can_be_null & mask_any)
  1287. *pnull |= mask;
  1288. }
  1289. }
  1290. else
  1291. {
  1292. // we haven't created a startmap for this alternative yet
  1293. // so take the union of the two options:
  1294. if(is_bad_repeat(state))
  1295. {
  1296. set_all_masks(l_map, mask);
  1297. if(pnull)
  1298. *pnull |= mask;
  1299. return;
  1300. }
  1301. set_bad_repeat(state);
  1302. create_startmap(state->next.p, l_map, pnull, mask, ++recursion_count);
  1303. if((state->type == syntax_element_alt)
  1304. || (static_cast<re_repeat*>(state)->min == 0)
  1305. || (not_last_jump == 0))
  1306. create_startmap(rep->alt.p, l_map, pnull, mask, ++recursion_count);
  1307. }
  1308. }
  1309. return;
  1310. case syntax_element_soft_buffer_end:
  1311. // match newline or null:
  1312. if(l_map)
  1313. {
  1314. l_map[0] |= mask_init;
  1315. l_map[static_cast<unsigned>('\n')] |= mask;
  1316. l_map[static_cast<unsigned>('\r')] |= mask;
  1317. }
  1318. if(pnull)
  1319. *pnull |= mask;
  1320. return;
  1321. case syntax_element_endmark:
  1322. // need to handle independent subs as a special case:
  1323. if(static_cast<re_brace*>(state)->index < 0)
  1324. {
  1325. // can be null, any character can match:
  1326. set_all_masks(l_map, mask);
  1327. if(pnull)
  1328. *pnull |= mask;
  1329. return;
  1330. }
  1331. else if(recursion_start && (recursion_sub != 0) && (recursion_sub == static_cast<re_brace*>(state)->index))
  1332. {
  1333. // recursion termination:
  1334. recursion_start = 0;
  1335. state = recursion_restart;
  1336. break;
  1337. }
  1338. //
  1339. // Normally we just go to the next state... but if this sub-expression is
  1340. // the target of a recursion, then we might be ending a recursion, in which
  1341. // case we should check whatever follows that recursion, as well as whatever
  1342. // follows this state:
  1343. //
  1344. if(m_pdata->m_has_recursions && static_cast<re_brace*>(state)->index)
  1345. {
  1346. bool ok = false;
  1347. re_syntax_base* p = m_pdata->m_first_state;
  1348. while(p)
  1349. {
  1350. if(p->type == syntax_element_recurse)
  1351. {
  1352. re_brace* p2 = static_cast<re_brace*>(static_cast<re_jump*>(p)->alt.p);
  1353. if((p2->type == syntax_element_startmark) && (p2->index == static_cast<re_brace*>(state)->index))
  1354. {
  1355. ok = true;
  1356. break;
  1357. }
  1358. }
  1359. p = p->next.p;
  1360. }
  1361. if(ok && ((m_recursion_checks[static_cast<re_brace*>(state)->index] & 2u) == 0))
  1362. {
  1363. m_recursion_checks[static_cast<re_brace*>(state)->index] |= 2u;
  1364. create_startmap(p->next.p, l_map, pnull, mask, ++recursion_count);
  1365. }
  1366. }
  1367. state = state->next.p;
  1368. break;
  1369. case syntax_element_commit:
  1370. set_all_masks(l_map, mask);
  1371. // Continue scanning so we can figure out whether we can be null:
  1372. state = state->next.p;
  1373. break;
  1374. case syntax_element_startmark:
  1375. // need to handle independent subs as a special case:
  1376. if(static_cast<re_brace*>(state)->index == -3)
  1377. {
  1378. state = state->next.p->next.p;
  1379. break;
  1380. }
  1381. BOOST_REGEX_FALLTHROUGH;
  1382. default:
  1383. state = state->next.p;
  1384. }
  1385. ++not_last_jump;
  1386. }
  1387. }
  1388. template <class charT, class traits>
  1389. unsigned basic_regex_creator<charT, traits>::get_restart_type(re_syntax_base* state)
  1390. {
  1391. //
  1392. // find out how the machine starts, so we can optimise the search:
  1393. //
  1394. while(state)
  1395. {
  1396. switch(state->type)
  1397. {
  1398. case syntax_element_startmark:
  1399. case syntax_element_endmark:
  1400. state = state->next.p;
  1401. continue;
  1402. case syntax_element_start_line:
  1403. return regbase::restart_line;
  1404. case syntax_element_word_start:
  1405. return regbase::restart_word;
  1406. case syntax_element_buffer_start:
  1407. return regbase::restart_buf;
  1408. case syntax_element_restart_continue:
  1409. return regbase::restart_continue;
  1410. default:
  1411. state = 0;
  1412. continue;
  1413. }
  1414. }
  1415. return regbase::restart_any;
  1416. }
  1417. template <class charT, class traits>
  1418. void basic_regex_creator<charT, traits>::set_all_masks(unsigned char* bits, unsigned char mask)
  1419. {
  1420. //
  1421. // set mask in all of bits elements,
  1422. // if bits[0] has mask_init not set then we can
  1423. // optimise this to a call to memset:
  1424. //
  1425. if(bits)
  1426. {
  1427. if(bits[0] == 0)
  1428. (std::memset)(bits, mask, 1u << CHAR_BIT);
  1429. else
  1430. {
  1431. for(unsigned i = 0; i < (1u << CHAR_BIT); ++i)
  1432. bits[i] |= mask;
  1433. }
  1434. bits[0] |= mask_init;
  1435. }
  1436. }
  1437. template <class charT, class traits>
  1438. bool basic_regex_creator<charT, traits>::is_bad_repeat(re_syntax_base* pt)
  1439. {
  1440. switch(pt->type)
  1441. {
  1442. case syntax_element_rep:
  1443. case syntax_element_dot_rep:
  1444. case syntax_element_char_rep:
  1445. case syntax_element_short_set_rep:
  1446. case syntax_element_long_set_rep:
  1447. {
  1448. unsigned state_id = static_cast<re_repeat*>(pt)->state_id;
  1449. if(state_id >= sizeof(m_bad_repeats) * CHAR_BIT)
  1450. return true; // run out of bits, assume we can't traverse this one.
  1451. static const std::uintmax_t one = 1uL;
  1452. return m_bad_repeats & (one << state_id);
  1453. }
  1454. default:
  1455. return false;
  1456. }
  1457. }
  1458. template <class charT, class traits>
  1459. void basic_regex_creator<charT, traits>::set_bad_repeat(re_syntax_base* pt)
  1460. {
  1461. switch(pt->type)
  1462. {
  1463. case syntax_element_rep:
  1464. case syntax_element_dot_rep:
  1465. case syntax_element_char_rep:
  1466. case syntax_element_short_set_rep:
  1467. case syntax_element_long_set_rep:
  1468. {
  1469. unsigned state_id = static_cast<re_repeat*>(pt)->state_id;
  1470. static const std::uintmax_t one = 1uL;
  1471. if(state_id <= sizeof(m_bad_repeats) * CHAR_BIT)
  1472. m_bad_repeats |= (one << state_id);
  1473. }
  1474. break;
  1475. default:
  1476. break;
  1477. }
  1478. }
  1479. template <class charT, class traits>
  1480. syntax_element_type basic_regex_creator<charT, traits>::get_repeat_type(re_syntax_base* state)
  1481. {
  1482. typedef typename traits::char_class_type m_type;
  1483. if(state->type == syntax_element_rep)
  1484. {
  1485. // check to see if we are repeating a single state:
  1486. if(state->next.p->next.p->next.p == static_cast<re_alt*>(state)->alt.p)
  1487. {
  1488. switch(state->next.p->type)
  1489. {
  1490. case BOOST_REGEX_DETAIL_NS::syntax_element_wild:
  1491. return BOOST_REGEX_DETAIL_NS::syntax_element_dot_rep;
  1492. case BOOST_REGEX_DETAIL_NS::syntax_element_literal:
  1493. return BOOST_REGEX_DETAIL_NS::syntax_element_char_rep;
  1494. case BOOST_REGEX_DETAIL_NS::syntax_element_set:
  1495. return BOOST_REGEX_DETAIL_NS::syntax_element_short_set_rep;
  1496. case BOOST_REGEX_DETAIL_NS::syntax_element_long_set:
  1497. if(static_cast<BOOST_REGEX_DETAIL_NS::re_set_long<m_type>*>(state->next.p)->singleton)
  1498. return BOOST_REGEX_DETAIL_NS::syntax_element_long_set_rep;
  1499. break;
  1500. default:
  1501. break;
  1502. }
  1503. }
  1504. }
  1505. return state->type;
  1506. }
  1507. template <class charT, class traits>
  1508. void basic_regex_creator<charT, traits>::probe_leading_repeat(re_syntax_base* state)
  1509. {
  1510. // enumerate our states, and see if we have a leading repeat
  1511. // for which failed search restarts can be optimized;
  1512. do
  1513. {
  1514. switch(state->type)
  1515. {
  1516. case syntax_element_startmark:
  1517. if(static_cast<re_brace*>(state)->index >= 0)
  1518. {
  1519. state = state->next.p;
  1520. continue;
  1521. }
  1522. #ifdef BOOST_REGEX_MSVC
  1523. # pragma warning(push)
  1524. #pragma warning(disable:6011)
  1525. #endif
  1526. if((static_cast<re_brace*>(state)->index == -1)
  1527. || (static_cast<re_brace*>(state)->index == -2))
  1528. {
  1529. // skip past the zero width assertion:
  1530. state = static_cast<const re_jump*>(state->next.p)->alt.p->next.p;
  1531. continue;
  1532. }
  1533. #ifdef BOOST_REGEX_MSVC
  1534. # pragma warning(pop)
  1535. #endif
  1536. if(static_cast<re_brace*>(state)->index == -3)
  1537. {
  1538. // Have to skip the leading jump state:
  1539. state = state->next.p->next.p;
  1540. continue;
  1541. }
  1542. return;
  1543. case syntax_element_endmark:
  1544. case syntax_element_start_line:
  1545. case syntax_element_end_line:
  1546. case syntax_element_word_boundary:
  1547. case syntax_element_within_word:
  1548. case syntax_element_word_start:
  1549. case syntax_element_word_end:
  1550. case syntax_element_buffer_start:
  1551. case syntax_element_buffer_end:
  1552. case syntax_element_restart_continue:
  1553. state = state->next.p;
  1554. break;
  1555. case syntax_element_dot_rep:
  1556. case syntax_element_char_rep:
  1557. case syntax_element_short_set_rep:
  1558. case syntax_element_long_set_rep:
  1559. if(this->m_has_backrefs == 0)
  1560. static_cast<re_repeat*>(state)->leading = true;
  1561. BOOST_REGEX_FALLTHROUGH;
  1562. default:
  1563. return;
  1564. }
  1565. }while(state);
  1566. }
  1567. } // namespace BOOST_REGEX_DETAIL_NS
  1568. } // namespace boost
  1569. #ifdef BOOST_REGEX_MSVC
  1570. # pragma warning(pop)
  1571. #endif
  1572. #endif