basic_regex_creator.hpp 52 KB

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