basic_regex_creator.hpp 52 KB

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