basic_regex_creator.hpp 52 KB

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