basic_regex_creator.hpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  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 mask_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(mask_type m)
  94. {
  95. m_classes |= m;
  96. m_empty = false;
  97. }
  98. void add_negated_class(mask_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. mask_type classes()const
  155. {
  156. return m_classes;
  157. }
  158. mask_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. mask_type m_classes; // character classes to match
  172. mask_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. typename traits::char_class_type m_word_mask; // mask used to determine if a character is a word character
  233. typename traits::char_class_type m_mask_space; // mask used to determine if a character is a word character
  234. typename traits::char_class_type m_lower_mask; // mask used to determine if a character is a lowercase character
  235. typename traits::char_class_type m_upper_mask; // mask used to determine if a character is an uppercase character
  236. typename traits::char_class_type m_alpha_mask; // mask used to determine if a character is an alphabetic character
  237. private:
  238. basic_regex_creator& operator=(const basic_regex_creator&);
  239. basic_regex_creator(const basic_regex_creator&);
  240. void fixup_pointers(re_syntax_base* state);
  241. void fixup_recursions(re_syntax_base* state);
  242. void create_startmaps(re_syntax_base* state);
  243. int calculate_backstep(re_syntax_base* state);
  244. void create_startmap(re_syntax_base* state, unsigned char* l_map, unsigned int* pnull, unsigned char mask);
  245. unsigned get_restart_type(re_syntax_base* state);
  246. void set_all_masks(unsigned char* bits, unsigned char);
  247. bool is_bad_repeat(re_syntax_base* pt);
  248. void set_bad_repeat(re_syntax_base* pt);
  249. syntax_element_type get_repeat_type(re_syntax_base* state);
  250. void probe_leading_repeat(re_syntax_base* state);
  251. };
  252. template <class charT, class traits>
  253. basic_regex_creator<charT, traits>::basic_regex_creator(regex_data<charT, traits>* data)
  254. : m_pdata(data), m_traits(*(data->m_ptraits)), m_last_state(0), m_repeater_id(0), m_has_backrefs(false), m_backrefs(0), m_has_recursions(false)
  255. {
  256. m_pdata->m_data.clear();
  257. m_pdata->m_status = ::boost::regex_constants::error_ok;
  258. static const charT w = 'w';
  259. static const charT s = 's';
  260. static const charT l[5] = { 'l', 'o', 'w', 'e', 'r', };
  261. static const charT u[5] = { 'u', 'p', 'p', 'e', 'r', };
  262. static const charT a[5] = { 'a', 'l', 'p', 'h', 'a', };
  263. m_word_mask = m_traits.lookup_classname(&w, &w +1);
  264. m_mask_space = m_traits.lookup_classname(&s, &s +1);
  265. m_lower_mask = m_traits.lookup_classname(l, l + 5);
  266. m_upper_mask = m_traits.lookup_classname(u, u + 5);
  267. m_alpha_mask = m_traits.lookup_classname(a, a + 5);
  268. m_pdata->m_word_mask = m_word_mask;
  269. BOOST_ASSERT(m_word_mask != 0);
  270. BOOST_ASSERT(m_mask_space != 0);
  271. BOOST_ASSERT(m_lower_mask != 0);
  272. BOOST_ASSERT(m_upper_mask != 0);
  273. BOOST_ASSERT(m_alpha_mask != 0);
  274. }
  275. template <class charT, class traits>
  276. re_syntax_base* basic_regex_creator<charT, traits>::append_state(syntax_element_type t, std::size_t s)
  277. {
  278. // if the state is a backref then make a note of it:
  279. if(t == syntax_element_backref)
  280. this->m_has_backrefs = true;
  281. // append a new state, start by aligning our last one:
  282. m_pdata->m_data.align();
  283. // set the offset to the next state in our last one:
  284. if(m_last_state)
  285. m_last_state->next.i = m_pdata->m_data.size() - getoffset(m_last_state);
  286. // now actually extent our data:
  287. m_last_state = static_cast<re_syntax_base*>(m_pdata->m_data.extend(s));
  288. // fill in boilerplate options in the new state:
  289. m_last_state->next.i = 0;
  290. m_last_state->type = t;
  291. return m_last_state;
  292. }
  293. template <class charT, class traits>
  294. re_syntax_base* basic_regex_creator<charT, traits>::insert_state(std::ptrdiff_t pos, syntax_element_type t, std::size_t s)
  295. {
  296. // append a new state, start by aligning our last one:
  297. m_pdata->m_data.align();
  298. // set the offset to the next state in our last one:
  299. if(m_last_state)
  300. m_last_state->next.i = m_pdata->m_data.size() - getoffset(m_last_state);
  301. // remember the last state position:
  302. std::ptrdiff_t off = getoffset(m_last_state) + s;
  303. // now actually insert our data:
  304. re_syntax_base* new_state = static_cast<re_syntax_base*>(m_pdata->m_data.insert(pos, s));
  305. // fill in boilerplate options in the new state:
  306. new_state->next.i = s;
  307. new_state->type = t;
  308. m_last_state = getaddress(off);
  309. return new_state;
  310. }
  311. template <class charT, class traits>
  312. re_literal* basic_regex_creator<charT, traits>::append_literal(charT c)
  313. {
  314. re_literal* result;
  315. // start by seeing if we have an existing re_literal we can extend:
  316. if((0 == m_last_state) || (m_last_state->type != syntax_element_literal))
  317. {
  318. // no existing re_literal, create a new one:
  319. result = static_cast<re_literal*>(append_state(syntax_element_literal, sizeof(re_literal) + sizeof(charT)));
  320. result->length = 1;
  321. *static_cast<charT*>(static_cast<void*>(result+1)) = m_traits.translate(c, m_icase);
  322. }
  323. else
  324. {
  325. // we have an existing re_literal, extend it:
  326. std::ptrdiff_t off = getoffset(m_last_state);
  327. m_pdata->m_data.extend(sizeof(charT));
  328. m_last_state = result = static_cast<re_literal*>(getaddress(off));
  329. charT* characters = static_cast<charT*>(static_cast<void*>(result+1));
  330. characters[result->length] = m_traits.translate(c, m_icase);
  331. ++(result->length);
  332. }
  333. return result;
  334. }
  335. template <class charT, class traits>
  336. inline re_syntax_base* basic_regex_creator<charT, traits>::append_set(
  337. const basic_char_set<charT, traits>& char_set)
  338. {
  339. typedef mpl::bool_< (sizeof(charT) == 1) > truth_type;
  340. return char_set.has_digraphs()
  341. ? append_set(char_set, static_cast<mpl::false_*>(0))
  342. : append_set(char_set, static_cast<truth_type*>(0));
  343. }
  344. template <class charT, class traits>
  345. re_syntax_base* basic_regex_creator<charT, traits>::append_set(
  346. const basic_char_set<charT, traits>& char_set, mpl::false_*)
  347. {
  348. typedef typename traits::string_type string_type;
  349. typedef typename basic_char_set<charT, traits>::list_iterator item_iterator;
  350. typedef typename traits::char_class_type mask_type;
  351. re_set_long<mask_type>* result = static_cast<re_set_long<mask_type>*>(append_state(syntax_element_long_set, sizeof(re_set_long<mask_type>)));
  352. //
  353. // fill in the basics:
  354. //
  355. result->csingles = static_cast<unsigned int>(::boost::re_detail::distance(char_set.singles_begin(), char_set.singles_end()));
  356. result->cranges = static_cast<unsigned int>(::boost::re_detail::distance(char_set.ranges_begin(), char_set.ranges_end())) / 2;
  357. result->cequivalents = static_cast<unsigned int>(::boost::re_detail::distance(char_set.equivalents_begin(), char_set.equivalents_end()));
  358. result->cclasses = char_set.classes();
  359. result->cnclasses = char_set.negated_classes();
  360. if(flags() & regbase::icase)
  361. {
  362. // adjust classes as needed:
  363. if(((result->cclasses & m_lower_mask) == m_lower_mask) || ((result->cclasses & m_upper_mask) == m_upper_mask))
  364. result->cclasses |= m_alpha_mask;
  365. if(((result->cnclasses & m_lower_mask) == m_lower_mask) || ((result->cnclasses & m_upper_mask) == m_upper_mask))
  366. result->cnclasses |= m_alpha_mask;
  367. }
  368. result->isnot = char_set.is_negated();
  369. result->singleton = !char_set.has_digraphs();
  370. //
  371. // remember where the state is for later:
  372. //
  373. std::ptrdiff_t offset = getoffset(result);
  374. //
  375. // now extend with all the singles:
  376. //
  377. item_iterator first, last;
  378. first = char_set.singles_begin();
  379. last = char_set.singles_end();
  380. while(first != last)
  381. {
  382. charT* p = static_cast<charT*>(this->m_pdata->m_data.extend(sizeof(charT) * (first->second ? 3 : 2)));
  383. p[0] = m_traits.translate(first->first, m_icase);
  384. if(first->second)
  385. {
  386. p[1] = m_traits.translate(first->second, m_icase);
  387. p[2] = 0;
  388. }
  389. else
  390. p[1] = 0;
  391. ++first;
  392. }
  393. //
  394. // now extend with all the ranges:
  395. //
  396. first = char_set.ranges_begin();
  397. last = char_set.ranges_end();
  398. while(first != last)
  399. {
  400. // first grab the endpoints of the range:
  401. digraph<charT> c1 = *first;
  402. c1.first = this->m_traits.translate(c1.first, this->m_icase);
  403. c1.second = this->m_traits.translate(c1.second, this->m_icase);
  404. ++first;
  405. digraph<charT> c2 = *first;
  406. c2.first = this->m_traits.translate(c2.first, this->m_icase);
  407. c2.second = this->m_traits.translate(c2.second, this->m_icase);
  408. ++first;
  409. string_type s1, s2;
  410. // different actions now depending upon whether collation is turned on:
  411. if(flags() & regex_constants::collate)
  412. {
  413. // we need to transform our range into sort keys:
  414. #if BOOST_WORKAROUND(__GNUC__, < 3)
  415. string_type in(3, charT(0));
  416. in[0] = c1.first;
  417. in[1] = c1.second;
  418. s1 = this->m_traits.transform(in.c_str(), (in[1] ? in.c_str()+2 : in.c_str()+1));
  419. in[0] = c2.first;
  420. in[1] = c2.second;
  421. s2 = this->m_traits.transform(in.c_str(), (in[1] ? in.c_str()+2 : in.c_str()+1));
  422. #else
  423. charT a1[3] = { c1.first, c1.second, charT(0), };
  424. charT a2[3] = { c2.first, c2.second, charT(0), };
  425. s1 = this->m_traits.transform(a1, (a1[1] ? a1+2 : a1+1));
  426. s2 = this->m_traits.transform(a2, (a2[1] ? a2+2 : a2+1));
  427. #endif
  428. if(s1.size() == 0)
  429. s1 = string_type(1, charT(0));
  430. if(s2.size() == 0)
  431. s2 = string_type(1, charT(0));
  432. }
  433. else
  434. {
  435. if(c1.second)
  436. {
  437. s1.insert(s1.end(), c1.first);
  438. s1.insert(s1.end(), c1.second);
  439. }
  440. else
  441. s1 = string_type(1, c1.first);
  442. if(c2.second)
  443. {
  444. s2.insert(s2.end(), c2.first);
  445. s2.insert(s2.end(), c2.second);
  446. }
  447. else
  448. s2.insert(s2.end(), c2.first);
  449. }
  450. if(s1 > s2)
  451. {
  452. // Oops error:
  453. return 0;
  454. }
  455. charT* p = static_cast<charT*>(this->m_pdata->m_data.extend(sizeof(charT) * (s1.size() + s2.size() + 2) ) );
  456. re_detail::copy(s1.begin(), s1.end(), p);
  457. p[s1.size()] = charT(0);
  458. p += s1.size() + 1;
  459. re_detail::copy(s2.begin(), s2.end(), p);
  460. p[s2.size()] = charT(0);
  461. }
  462. //
  463. // now process the equivalence classes:
  464. //
  465. first = char_set.equivalents_begin();
  466. last = char_set.equivalents_end();
  467. while(first != last)
  468. {
  469. string_type s;
  470. if(first->second)
  471. {
  472. #if BOOST_WORKAROUND(__GNUC__, < 3)
  473. string_type in(3, charT(0));
  474. in[0] = first->first;
  475. in[1] = first->second;
  476. s = m_traits.transform_primary(in.c_str(), in.c_str()+2);
  477. #else
  478. charT cs[3] = { first->first, first->second, charT(0), };
  479. s = m_traits.transform_primary(cs, cs+2);
  480. #endif
  481. }
  482. else
  483. s = m_traits.transform_primary(&first->first, &first->first+1);
  484. if(s.empty())
  485. return 0; // invalid or unsupported equivalence class
  486. charT* p = static_cast<charT*>(this->m_pdata->m_data.extend(sizeof(charT) * (s.size()+1) ) );
  487. re_detail::copy(s.begin(), s.end(), p);
  488. p[s.size()] = charT(0);
  489. ++first;
  490. }
  491. //
  492. // finally reset the address of our last state:
  493. //
  494. m_last_state = result = static_cast<re_set_long<mask_type>*>(getaddress(offset));
  495. return result;
  496. }
  497. namespace{
  498. template<class T>
  499. inline bool char_less(T t1, T t2)
  500. {
  501. return t1 < t2;
  502. }
  503. template<>
  504. inline bool char_less<char>(char t1, char t2)
  505. {
  506. return static_cast<unsigned char>(t1) < static_cast<unsigned char>(t2);
  507. }
  508. template<>
  509. inline bool char_less<signed char>(signed char t1, signed char t2)
  510. {
  511. return static_cast<unsigned char>(t1) < static_cast<unsigned char>(t2);
  512. }
  513. }
  514. template <class charT, class traits>
  515. re_syntax_base* basic_regex_creator<charT, traits>::append_set(
  516. const basic_char_set<charT, traits>& char_set, mpl::true_*)
  517. {
  518. typedef typename traits::string_type string_type;
  519. typedef typename basic_char_set<charT, traits>::list_iterator item_iterator;
  520. re_set* result = static_cast<re_set*>(append_state(syntax_element_set, sizeof(re_set)));
  521. bool negate = char_set.is_negated();
  522. std::memset(result->_map, 0, sizeof(result->_map));
  523. //
  524. // handle singles first:
  525. //
  526. item_iterator first, last;
  527. first = char_set.singles_begin();
  528. last = char_set.singles_end();
  529. while(first != last)
  530. {
  531. for(unsigned int i = 0; i < (1 << CHAR_BIT); ++i)
  532. {
  533. if(this->m_traits.translate(static_cast<charT>(i), this->m_icase)
  534. == this->m_traits.translate(first->first, this->m_icase))
  535. result->_map[i] = true;
  536. }
  537. ++first;
  538. }
  539. //
  540. // OK now handle ranges:
  541. //
  542. first = char_set.ranges_begin();
  543. last = char_set.ranges_end();
  544. while(first != last)
  545. {
  546. // first grab the endpoints of the range:
  547. charT c1 = this->m_traits.translate(first->first, this->m_icase);
  548. ++first;
  549. charT c2 = this->m_traits.translate(first->first, this->m_icase);
  550. ++first;
  551. // different actions now depending upon whether collation is turned on:
  552. if(flags() & regex_constants::collate)
  553. {
  554. // we need to transform our range into sort keys:
  555. charT c3[2] = { c1, charT(0), };
  556. string_type s1 = this->m_traits.transform(c3, c3+1);
  557. c3[0] = c2;
  558. string_type s2 = this->m_traits.transform(c3, c3+1);
  559. if(s1 > s2)
  560. {
  561. // Oops error:
  562. return 0;
  563. }
  564. BOOST_ASSERT(c3[1] == charT(0));
  565. for(unsigned i = 0; i < (1u << CHAR_BIT); ++i)
  566. {
  567. c3[0] = static_cast<charT>(i);
  568. string_type s3 = this->m_traits.transform(c3, c3 +1);
  569. if((s1 <= s3) && (s3 <= s2))
  570. result->_map[i] = true;
  571. }
  572. }
  573. else
  574. {
  575. if(char_less<charT>(c2, c1))
  576. {
  577. // Oops error:
  578. return 0;
  579. }
  580. // everything in range matches:
  581. std::memset(result->_map + static_cast<unsigned char>(c1), true, 1 + static_cast<unsigned char>(c2) - static_cast<unsigned char>(c1));
  582. }
  583. }
  584. //
  585. // and now the classes:
  586. //
  587. typedef typename traits::char_class_type mask_type;
  588. mask_type m = char_set.classes();
  589. if(flags() & regbase::icase)
  590. {
  591. // adjust m as needed:
  592. if(((m & m_lower_mask) == m_lower_mask) || ((m & m_upper_mask) == m_upper_mask))
  593. m |= m_alpha_mask;
  594. }
  595. if(m != 0)
  596. {
  597. for(unsigned i = 0; i < (1u << CHAR_BIT); ++i)
  598. {
  599. if(this->m_traits.isctype(static_cast<charT>(i), m))
  600. result->_map[i] = true;
  601. }
  602. }
  603. //
  604. // and now the negated classes:
  605. //
  606. m = char_set.negated_classes();
  607. if(flags() & regbase::icase)
  608. {
  609. // adjust m as needed:
  610. if(((m & m_lower_mask) == m_lower_mask) || ((m & m_upper_mask) == m_upper_mask))
  611. m |= m_alpha_mask;
  612. }
  613. if(m != 0)
  614. {
  615. for(unsigned i = 0; i < (1u << CHAR_BIT); ++i)
  616. {
  617. if(0 == this->m_traits.isctype(static_cast<charT>(i), m))
  618. result->_map[i] = true;
  619. }
  620. }
  621. //
  622. // now process the equivalence classes:
  623. //
  624. first = char_set.equivalents_begin();
  625. last = char_set.equivalents_end();
  626. while(first != last)
  627. {
  628. string_type s;
  629. BOOST_ASSERT(static_cast<charT>(0) == first->second);
  630. s = m_traits.transform_primary(&first->first, &first->first+1);
  631. if(s.empty())
  632. return 0; // invalid or unsupported equivalence class
  633. for(unsigned i = 0; i < (1u << CHAR_BIT); ++i)
  634. {
  635. charT c[2] = { (static_cast<charT>(i)), charT(0), };
  636. string_type s2 = this->m_traits.transform_primary(c, c+1);
  637. if(s == s2)
  638. result->_map[i] = true;
  639. }
  640. ++first;
  641. }
  642. if(negate)
  643. {
  644. for(unsigned i = 0; i < (1u << CHAR_BIT); ++i)
  645. {
  646. result->_map[i] = !(result->_map[i]);
  647. }
  648. }
  649. return result;
  650. }
  651. template <class charT, class traits>
  652. void basic_regex_creator<charT, traits>::finalize(const charT* p1, const charT* p2)
  653. {
  654. // we've added all the states we need, now finish things off.
  655. // start by adding a terminating state:
  656. append_state(syntax_element_match);
  657. // extend storage to store original expression:
  658. std::ptrdiff_t len = p2 - p1;
  659. m_pdata->m_expression_len = len;
  660. charT* ps = static_cast<charT*>(m_pdata->m_data.extend(sizeof(charT) * (1 + (p2 - p1))));
  661. m_pdata->m_expression = ps;
  662. re_detail::copy(p1, p2, ps);
  663. ps[p2 - p1] = 0;
  664. // fill in our other data...
  665. // successful parsing implies a zero status:
  666. m_pdata->m_status = 0;
  667. // get the first state of the machine:
  668. m_pdata->m_first_state = static_cast<re_syntax_base*>(m_pdata->m_data.data());
  669. // fixup pointers in the machine:
  670. fixup_pointers(m_pdata->m_first_state);
  671. if(m_has_recursions)
  672. {
  673. m_pdata->m_has_recursions = true;
  674. fixup_recursions(m_pdata->m_first_state);
  675. }
  676. else
  677. m_pdata->m_has_recursions = false;
  678. // create nested startmaps:
  679. create_startmaps(m_pdata->m_first_state);
  680. // create main startmap:
  681. std::memset(m_pdata->m_startmap, 0, sizeof(m_pdata->m_startmap));
  682. m_pdata->m_can_be_null = 0;
  683. m_bad_repeats = 0;
  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. // fall through:
  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. // fall through:
  716. case syntax_element_jump:
  717. static_cast<re_jump*>(state)->alt.p = getaddress(static_cast<re_jump*>(state)->alt.i, state);
  718. // fall through again:
  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 id = static_cast<const re_brace*>(state)->index;
  740. if(id < 0)
  741. {
  742. id = -id-1;
  743. if(id >= 10000)
  744. {
  745. id = m_pdata->get_id(id);
  746. if(id <= 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 = this->m_pdata->m_ptraits->error_string(boost::regex_constants::error_bad_pattern);
  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. int id = static_cast<re_jump*>(state)->alt.i;
  775. if(id > 10000)
  776. id = m_pdata->get_id(id);
  777. while(p)
  778. {
  779. if((p->type == syntax_element_startmark) && (static_cast<re_brace*>(p)->index == id))
  780. {
  781. static_cast<re_jump*>(state)->alt.p = p;
  782. ok = true;
  783. break;
  784. }
  785. p = p->next.p;
  786. }
  787. if(!ok)
  788. {
  789. // recursion to sub-expression that doesn't exist:
  790. if(0 == this->m_pdata->m_status) // update the error code if not already set
  791. this->m_pdata->m_status = boost::regex_constants::error_bad_pattern;
  792. //
  793. // clear the expression, we should be empty:
  794. //
  795. this->m_pdata->m_expression = 0;
  796. this->m_pdata->m_expression_len = 0;
  797. //
  798. // and throw if required:
  799. //
  800. if(0 == (this->flags() & regex_constants::no_except))
  801. {
  802. std::string message = this->m_pdata->m_ptraits->error_string(boost::regex_constants::error_bad_pattern);
  803. boost::regex_error e(message, boost::regex_constants::error_bad_pattern, 0);
  804. e.raise();
  805. }
  806. }
  807. }
  808. default:
  809. break;
  810. }
  811. state = state->next.p;
  812. }
  813. }
  814. template <class charT, class traits>
  815. void basic_regex_creator<charT, traits>::create_startmaps(re_syntax_base* state)
  816. {
  817. // non-recursive implementation:
  818. // create the last map in the machine first, so that earlier maps
  819. // can make use of the result...
  820. //
  821. // This was originally a recursive implementation, but that caused stack
  822. // overflows with complex expressions on small stacks (think COM+).
  823. // start by saving the case setting:
  824. bool l_icase = m_icase;
  825. std::vector<std::pair<bool, re_syntax_base*> > v;
  826. while(state)
  827. {
  828. switch(state->type)
  829. {
  830. case syntax_element_toggle_case:
  831. // we need to track case changes here:
  832. m_icase = static_cast<re_case*>(state)->icase;
  833. state = state->next.p;
  834. continue;
  835. case syntax_element_alt:
  836. case syntax_element_rep:
  837. case syntax_element_dot_rep:
  838. case syntax_element_char_rep:
  839. case syntax_element_short_set_rep:
  840. case syntax_element_long_set_rep:
  841. // just push the state onto our stack for now:
  842. v.push_back(std::pair<bool, re_syntax_base*>(m_icase, state));
  843. state = state->next.p;
  844. break;
  845. case syntax_element_backstep:
  846. // we need to calculate how big the backstep is:
  847. static_cast<re_brace*>(state)->index
  848. = this->calculate_backstep(state->next.p);
  849. if(static_cast<re_brace*>(state)->index < 0)
  850. {
  851. // Oops error:
  852. if(0 == this->m_pdata->m_status) // update the error code if not already set
  853. this->m_pdata->m_status = boost::regex_constants::error_bad_pattern;
  854. //
  855. // clear the expression, we should be empty:
  856. //
  857. this->m_pdata->m_expression = 0;
  858. this->m_pdata->m_expression_len = 0;
  859. //
  860. // and throw if required:
  861. //
  862. if(0 == (this->flags() & regex_constants::no_except))
  863. {
  864. std::string message = this->m_pdata->m_ptraits->error_string(boost::regex_constants::error_bad_pattern);
  865. boost::regex_error e(message, boost::regex_constants::error_bad_pattern, 0);
  866. e.raise();
  867. }
  868. }
  869. // fall through:
  870. default:
  871. state = state->next.p;
  872. }
  873. }
  874. // now work through our list, building all the maps as we go:
  875. while(v.size())
  876. {
  877. const std::pair<bool, re_syntax_base*>& p = v.back();
  878. m_icase = p.first;
  879. state = p.second;
  880. v.pop_back();
  881. // Build maps:
  882. m_bad_repeats = 0;
  883. create_startmap(state->next.p, static_cast<re_alt*>(state)->_map, &static_cast<re_alt*>(state)->can_be_null, mask_take);
  884. m_bad_repeats = 0;
  885. 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);
  886. // adjust the type of the state to allow for faster matching:
  887. state->type = this->get_repeat_type(state);
  888. }
  889. // restore case sensitivity:
  890. m_icase = l_icase;
  891. }
  892. template <class charT, class traits>
  893. int basic_regex_creator<charT, traits>::calculate_backstep(re_syntax_base* state)
  894. {
  895. typedef typename traits::char_class_type mask_type;
  896. int result = 0;
  897. while(state)
  898. {
  899. switch(state->type)
  900. {
  901. case syntax_element_startmark:
  902. if((static_cast<re_brace*>(state)->index == -1)
  903. || (static_cast<re_brace*>(state)->index == -2))
  904. {
  905. state = static_cast<re_jump*>(state->next.p)->alt.p->next.p;
  906. continue;
  907. }
  908. else if(static_cast<re_brace*>(state)->index == -3)
  909. {
  910. state = state->next.p->next.p;
  911. continue;
  912. }
  913. break;
  914. case syntax_element_endmark:
  915. if((static_cast<re_brace*>(state)->index == -1)
  916. || (static_cast<re_brace*>(state)->index == -2))
  917. return result;
  918. break;
  919. case syntax_element_literal:
  920. result += static_cast<re_literal*>(state)->length;
  921. break;
  922. case syntax_element_wild:
  923. case syntax_element_set:
  924. result += 1;
  925. break;
  926. case syntax_element_dot_rep:
  927. case syntax_element_char_rep:
  928. case syntax_element_short_set_rep:
  929. case syntax_element_backref:
  930. case syntax_element_rep:
  931. case syntax_element_combining:
  932. case syntax_element_long_set_rep:
  933. case syntax_element_backstep:
  934. {
  935. re_repeat* rep = static_cast<re_repeat *>(state);
  936. // adjust the type of the state to allow for faster matching:
  937. state->type = this->get_repeat_type(state);
  938. if((state->type == syntax_element_dot_rep)
  939. || (state->type == syntax_element_char_rep)
  940. || (state->type == syntax_element_short_set_rep))
  941. {
  942. if(rep->max != rep->min)
  943. return -1;
  944. result += static_cast<int>(rep->min);
  945. state = rep->alt.p;
  946. continue;
  947. }
  948. else if((state->type == syntax_element_long_set_rep))
  949. {
  950. BOOST_ASSERT(rep->next.p->type == syntax_element_long_set);
  951. if(static_cast<re_set_long<mask_type>*>(rep->next.p)->singleton == 0)
  952. return -1;
  953. if(rep->max != rep->min)
  954. return -1;
  955. result += static_cast<int>(rep->min);
  956. state = rep->alt.p;
  957. continue;
  958. }
  959. }
  960. return -1;
  961. case syntax_element_long_set:
  962. if(static_cast<re_set_long<mask_type>*>(state)->singleton == 0)
  963. return -1;
  964. result += 1;
  965. break;
  966. case syntax_element_jump:
  967. state = static_cast<re_jump*>(state)->alt.p;
  968. continue;
  969. default:
  970. break;
  971. }
  972. state = state->next.p;
  973. }
  974. return -1;
  975. }
  976. template <class charT, class traits>
  977. void basic_regex_creator<charT, traits>::create_startmap(re_syntax_base* state, unsigned char* l_map, unsigned int* pnull, unsigned char mask)
  978. {
  979. int not_last_jump = 1;
  980. // track case sensitivity:
  981. bool l_icase = m_icase;
  982. while(state)
  983. {
  984. switch(state->type)
  985. {
  986. case syntax_element_toggle_case:
  987. l_icase = static_cast<re_case*>(state)->icase;
  988. state = state->next.p;
  989. break;
  990. case syntax_element_literal:
  991. {
  992. // don't set anything in *pnull, set each element in l_map
  993. // that could match the first character in the literal:
  994. if(l_map)
  995. {
  996. l_map[0] |= mask_init;
  997. charT first_char = *static_cast<charT*>(static_cast<void*>(static_cast<re_literal*>(state) + 1));
  998. for(unsigned int i = 0; i < (1u << CHAR_BIT); ++i)
  999. {
  1000. if(m_traits.translate(static_cast<charT>(i), l_icase) == first_char)
  1001. l_map[i] |= mask;
  1002. }
  1003. }
  1004. return;
  1005. }
  1006. case syntax_element_end_line:
  1007. {
  1008. // next character must be a line separator (if there is one):
  1009. if(l_map)
  1010. {
  1011. l_map[0] |= mask_init;
  1012. l_map['\n'] |= mask;
  1013. l_map['\r'] |= mask;
  1014. l_map['\f'] |= mask;
  1015. l_map[0x85] |= mask;
  1016. }
  1017. // now figure out if we can match a NULL string at this point:
  1018. if(pnull)
  1019. create_startmap(state->next.p, 0, pnull, mask);
  1020. return;
  1021. }
  1022. case syntax_element_recurse:
  1023. case syntax_element_backref:
  1024. // can be null, and any character can match:
  1025. if(pnull)
  1026. *pnull |= mask;
  1027. // fall through:
  1028. case syntax_element_wild:
  1029. {
  1030. // can't be null, any character can match:
  1031. set_all_masks(l_map, mask);
  1032. return;
  1033. }
  1034. case syntax_element_match:
  1035. {
  1036. // must be null, any character can match:
  1037. set_all_masks(l_map, mask);
  1038. if(pnull)
  1039. *pnull |= mask;
  1040. return;
  1041. }
  1042. case syntax_element_word_start:
  1043. {
  1044. // recurse, then AND with all the word characters:
  1045. create_startmap(state->next.p, l_map, pnull, mask);
  1046. if(l_map)
  1047. {
  1048. l_map[0] |= mask_init;
  1049. for(unsigned int i = 0; i < (1u << CHAR_BIT); ++i)
  1050. {
  1051. if(!m_traits.isctype(static_cast<charT>(i), m_word_mask))
  1052. l_map[i] &= static_cast<unsigned char>(~mask);
  1053. }
  1054. }
  1055. return;
  1056. }
  1057. case syntax_element_word_end:
  1058. {
  1059. // recurse, then AND with all the word characters:
  1060. create_startmap(state->next.p, l_map, pnull, mask);
  1061. if(l_map)
  1062. {
  1063. l_map[0] |= mask_init;
  1064. for(unsigned int i = 0; i < (1u << CHAR_BIT); ++i)
  1065. {
  1066. if(m_traits.isctype(static_cast<charT>(i), m_word_mask))
  1067. l_map[i] &= static_cast<unsigned char>(~mask);
  1068. }
  1069. }
  1070. return;
  1071. }
  1072. case syntax_element_buffer_end:
  1073. {
  1074. // we *must be null* :
  1075. if(pnull)
  1076. *pnull |= mask;
  1077. return;
  1078. }
  1079. case syntax_element_long_set:
  1080. if(l_map)
  1081. {
  1082. typedef typename traits::char_class_type mask_type;
  1083. if(static_cast<re_set_long<mask_type>*>(state)->singleton)
  1084. {
  1085. l_map[0] |= mask_init;
  1086. for(unsigned int i = 0; i < (1u << CHAR_BIT); ++i)
  1087. {
  1088. charT c = static_cast<charT>(i);
  1089. if(&c != re_is_set_member(&c, &c + 1, static_cast<re_set_long<mask_type>*>(state), *m_pdata, m_icase))
  1090. l_map[i] |= mask;
  1091. }
  1092. }
  1093. else
  1094. set_all_masks(l_map, mask);
  1095. }
  1096. return;
  1097. case syntax_element_set:
  1098. if(l_map)
  1099. {
  1100. l_map[0] |= mask_init;
  1101. for(unsigned int i = 0; i < (1u << CHAR_BIT); ++i)
  1102. {
  1103. if(static_cast<re_set*>(state)->_map[
  1104. static_cast<unsigned char>(m_traits.translate(static_cast<charT>(i), l_icase))])
  1105. l_map[i] |= mask;
  1106. }
  1107. }
  1108. return;
  1109. case syntax_element_jump:
  1110. // take the jump:
  1111. state = static_cast<re_alt*>(state)->alt.p;
  1112. not_last_jump = -1;
  1113. break;
  1114. case syntax_element_alt:
  1115. case syntax_element_rep:
  1116. case syntax_element_dot_rep:
  1117. case syntax_element_char_rep:
  1118. case syntax_element_short_set_rep:
  1119. case syntax_element_long_set_rep:
  1120. {
  1121. re_alt* rep = static_cast<re_alt*>(state);
  1122. if(rep->_map[0] & mask_init)
  1123. {
  1124. if(l_map)
  1125. {
  1126. // copy previous results:
  1127. l_map[0] |= mask_init;
  1128. for(unsigned int i = 0; i <= UCHAR_MAX; ++i)
  1129. {
  1130. if(rep->_map[i] & mask_any)
  1131. l_map[i] |= mask;
  1132. }
  1133. }
  1134. if(pnull)
  1135. {
  1136. if(rep->can_be_null & mask_any)
  1137. *pnull |= mask;
  1138. }
  1139. }
  1140. else
  1141. {
  1142. // we haven't created a startmap for this alternative yet
  1143. // so take the union of the two options:
  1144. if(is_bad_repeat(state))
  1145. {
  1146. set_all_masks(l_map, mask);
  1147. if(pnull)
  1148. *pnull |= mask;
  1149. return;
  1150. }
  1151. set_bad_repeat(state);
  1152. create_startmap(state->next.p, l_map, pnull, mask);
  1153. if((state->type == syntax_element_alt)
  1154. || (static_cast<re_repeat*>(state)->min == 0)
  1155. || (not_last_jump == 0))
  1156. create_startmap(rep->alt.p, l_map, pnull, mask);
  1157. }
  1158. }
  1159. return;
  1160. case syntax_element_soft_buffer_end:
  1161. // match newline or null:
  1162. if(l_map)
  1163. {
  1164. l_map[0] |= mask_init;
  1165. l_map['\n'] |= mask;
  1166. l_map['\r'] |= mask;
  1167. }
  1168. if(pnull)
  1169. *pnull |= mask;
  1170. return;
  1171. case syntax_element_endmark:
  1172. // need to handle independent subs as a special case:
  1173. if(static_cast<re_brace*>(state)->index < 0)
  1174. {
  1175. // can be null, any character can match:
  1176. set_all_masks(l_map, mask);
  1177. if(pnull)
  1178. *pnull |= mask;
  1179. return;
  1180. }
  1181. else
  1182. {
  1183. state = state->next.p;
  1184. break;
  1185. }
  1186. case syntax_element_startmark:
  1187. // need to handle independent subs as a special case:
  1188. if(static_cast<re_brace*>(state)->index == -3)
  1189. {
  1190. state = state->next.p->next.p;
  1191. break;
  1192. }
  1193. // otherwise fall through:
  1194. default:
  1195. state = state->next.p;
  1196. }
  1197. ++not_last_jump;
  1198. }
  1199. }
  1200. template <class charT, class traits>
  1201. unsigned basic_regex_creator<charT, traits>::get_restart_type(re_syntax_base* state)
  1202. {
  1203. //
  1204. // find out how the machine starts, so we can optimise the search:
  1205. //
  1206. while(state)
  1207. {
  1208. switch(state->type)
  1209. {
  1210. case syntax_element_startmark:
  1211. case syntax_element_endmark:
  1212. state = state->next.p;
  1213. continue;
  1214. case syntax_element_start_line:
  1215. return regbase::restart_line;
  1216. case syntax_element_word_start:
  1217. return regbase::restart_word;
  1218. case syntax_element_buffer_start:
  1219. return regbase::restart_buf;
  1220. case syntax_element_restart_continue:
  1221. return regbase::restart_continue;
  1222. default:
  1223. state = 0;
  1224. continue;
  1225. }
  1226. }
  1227. return regbase::restart_any;
  1228. }
  1229. template <class charT, class traits>
  1230. void basic_regex_creator<charT, traits>::set_all_masks(unsigned char* bits, unsigned char mask)
  1231. {
  1232. //
  1233. // set mask in all of bits elements,
  1234. // if bits[0] has mask_init not set then we can
  1235. // optimise this to a call to memset:
  1236. //
  1237. if(bits)
  1238. {
  1239. if(bits[0] == 0)
  1240. (std::memset)(bits, mask, 1u << CHAR_BIT);
  1241. else
  1242. {
  1243. for(unsigned i = 0; i < (1u << CHAR_BIT); ++i)
  1244. bits[i] |= mask;
  1245. }
  1246. bits[0] |= mask_init;
  1247. }
  1248. }
  1249. template <class charT, class traits>
  1250. bool basic_regex_creator<charT, traits>::is_bad_repeat(re_syntax_base* pt)
  1251. {
  1252. switch(pt->type)
  1253. {
  1254. case syntax_element_rep:
  1255. case syntax_element_dot_rep:
  1256. case syntax_element_char_rep:
  1257. case syntax_element_short_set_rep:
  1258. case syntax_element_long_set_rep:
  1259. {
  1260. unsigned state_id = static_cast<re_repeat*>(pt)->state_id;
  1261. if(state_id > sizeof(m_bad_repeats) * CHAR_BIT)
  1262. return true; // run out of bits, assume we can't traverse this one.
  1263. static const boost::uintmax_t one = 1uL;
  1264. return m_bad_repeats & (one << state_id);
  1265. }
  1266. default:
  1267. return false;
  1268. }
  1269. }
  1270. template <class charT, class traits>
  1271. void basic_regex_creator<charT, traits>::set_bad_repeat(re_syntax_base* pt)
  1272. {
  1273. switch(pt->type)
  1274. {
  1275. case syntax_element_rep:
  1276. case syntax_element_dot_rep:
  1277. case syntax_element_char_rep:
  1278. case syntax_element_short_set_rep:
  1279. case syntax_element_long_set_rep:
  1280. {
  1281. unsigned state_id = static_cast<re_repeat*>(pt)->state_id;
  1282. static const boost::uintmax_t one = 1uL;
  1283. if(state_id <= sizeof(m_bad_repeats) * CHAR_BIT)
  1284. m_bad_repeats |= (one << state_id);
  1285. }
  1286. default:
  1287. break;
  1288. }
  1289. }
  1290. template <class charT, class traits>
  1291. syntax_element_type basic_regex_creator<charT, traits>::get_repeat_type(re_syntax_base* state)
  1292. {
  1293. typedef typename traits::char_class_type mask_type;
  1294. if(state->type == syntax_element_rep)
  1295. {
  1296. // check to see if we are repeating a single state:
  1297. if(state->next.p->next.p->next.p == static_cast<re_alt*>(state)->alt.p)
  1298. {
  1299. switch(state->next.p->type)
  1300. {
  1301. case re_detail::syntax_element_wild:
  1302. return re_detail::syntax_element_dot_rep;
  1303. case re_detail::syntax_element_literal:
  1304. return re_detail::syntax_element_char_rep;
  1305. case re_detail::syntax_element_set:
  1306. return re_detail::syntax_element_short_set_rep;
  1307. case re_detail::syntax_element_long_set:
  1308. if(static_cast<re_detail::re_set_long<mask_type>*>(state->next.p)->singleton)
  1309. return re_detail::syntax_element_long_set_rep;
  1310. break;
  1311. default:
  1312. break;
  1313. }
  1314. }
  1315. }
  1316. return state->type;
  1317. }
  1318. template <class charT, class traits>
  1319. void basic_regex_creator<charT, traits>::probe_leading_repeat(re_syntax_base* state)
  1320. {
  1321. // enumerate our states, and see if we have a leading repeat
  1322. // for which failed search restarts can be optimised;
  1323. do
  1324. {
  1325. switch(state->type)
  1326. {
  1327. case syntax_element_startmark:
  1328. if(static_cast<re_brace*>(state)->index >= 0)
  1329. {
  1330. state = state->next.p;
  1331. continue;
  1332. }
  1333. if((static_cast<re_brace*>(state)->index == -1)
  1334. || (static_cast<re_brace*>(state)->index == -2))
  1335. {
  1336. // skip past the zero width assertion:
  1337. state = static_cast<const re_jump*>(state->next.p)->alt.p->next.p;
  1338. continue;
  1339. }
  1340. if(static_cast<re_brace*>(state)->index == -3)
  1341. {
  1342. // Have to skip the leading jump state:
  1343. state = state->next.p->next.p;
  1344. continue;
  1345. }
  1346. return;
  1347. case syntax_element_endmark:
  1348. case syntax_element_start_line:
  1349. case syntax_element_end_line:
  1350. case syntax_element_word_boundary:
  1351. case syntax_element_within_word:
  1352. case syntax_element_word_start:
  1353. case syntax_element_word_end:
  1354. case syntax_element_buffer_start:
  1355. case syntax_element_buffer_end:
  1356. case syntax_element_restart_continue:
  1357. state = state->next.p;
  1358. break;
  1359. case syntax_element_dot_rep:
  1360. case syntax_element_char_rep:
  1361. case syntax_element_short_set_rep:
  1362. case syntax_element_long_set_rep:
  1363. if(this->m_has_backrefs == 0)
  1364. static_cast<re_repeat*>(state)->leading = true;
  1365. // fall through:
  1366. default:
  1367. return;
  1368. }
  1369. }while(state);
  1370. }
  1371. } // namespace re_detail
  1372. } // namespace boost
  1373. #ifdef BOOST_MSVC
  1374. # pragma warning(pop)
  1375. #endif
  1376. #ifdef BOOST_MSVC
  1377. #pragma warning(push)
  1378. #pragma warning(disable: 4103)
  1379. #endif
  1380. #ifdef BOOST_HAS_ABI_HEADERS
  1381. # include BOOST_ABI_SUFFIX
  1382. #endif
  1383. #ifdef BOOST_MSVC
  1384. #pragma warning(pop)
  1385. #endif
  1386. #endif