basic_regex_creator.hpp 53 KB

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