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)
13 * LOCATION: see http://www.boost.org for most recent version.
14 * FILE basic_regex_creator.cpp
15 * VERSION see <boost/version.hpp>
16 * DESCRIPTION: Declares template class basic_regex_creator which fills in
17 * the data members of a regex_data object.
20 #ifndef BOOST_REGEX_V4_BASIC_REGEX_CREATOR_HPP
21 #define BOOST_REGEX_V4_BASIC_REGEX_CREATOR_HPP
25 #pragma warning(disable: 4103)
27 #ifdef BOOST_HAS_ABI_HEADERS
28 # include BOOST_ABI_PREFIX
35 # pragma warning(push)
36 # pragma warning(disable: 4800)
41 namespace BOOST_REGEX_DETAIL_NS{
43 template <class charT>
44 struct digraph : public std::pair<charT, charT>
46 digraph() : std::pair<charT, charT>(0, 0){}
47 digraph(charT c1) : std::pair<charT, charT>(c1, 0){}
48 digraph(charT c1, charT c2) : std::pair<charT, charT>(c1, c2)
50 digraph(const digraph<charT>& d) : std::pair<charT, charT>(d.first, d.second){}
52 digraph(const Seq& s) : std::pair<charT, charT>()
54 BOOST_ASSERT(s.size() <= 2);
55 BOOST_ASSERT(s.size());
57 this->second = (s.size() > 1) ? s[1] : 0;
61 template <class charT, class traits>
65 typedef digraph<charT> digraph_type;
66 typedef typename traits::string_type string_type;
67 typedef typename traits::char_class_type m_type;
72 m_has_digraphs = false;
74 m_negated_classes = 0;
78 void add_single(const digraph_type& s)
82 m_has_digraphs = true;
85 void add_range(const digraph_type& first, const digraph_type& end)
87 m_ranges.push_back(first);
88 m_ranges.push_back(end);
91 m_has_digraphs = true;
96 m_has_digraphs = true;
101 void add_class(m_type m)
106 void add_negated_class(m_type m)
108 m_negated_classes |= m;
111 void add_equivalent(const digraph_type& s)
113 m_equivalents.insert(s);
116 m_has_digraphs = true;
128 // accessor functions:
130 bool has_digraphs()const
132 return m_has_digraphs;
134 bool is_negated()const
138 typedef typename std::vector<digraph_type>::const_iterator list_iterator;
139 typedef typename std::set<digraph_type>::const_iterator set_iterator;
140 set_iterator singles_begin()const
142 return m_singles.begin();
144 set_iterator singles_end()const
146 return m_singles.end();
148 list_iterator ranges_begin()const
150 return m_ranges.begin();
152 list_iterator ranges_end()const
154 return m_ranges.end();
156 set_iterator equivalents_begin()const
158 return m_equivalents.begin();
160 set_iterator equivalents_end()const
162 return m_equivalents.end();
164 m_type classes()const
168 m_type negated_classes()const
170 return m_negated_classes;
177 std::set<digraph_type> m_singles; // a list of single characters to match
178 std::vector<digraph_type> m_ranges; // a list of end points of our ranges
179 bool m_negate; // true if the set is to be negated
180 bool m_has_digraphs; // true if we have digraphs present
181 m_type m_classes; // character classes to match
182 m_type m_negated_classes; // negated character classes to match
183 bool m_empty; // whether we've added anything yet
184 std::set<digraph_type> m_equivalents; // a list of equivalence classes
187 template <class charT, class traits>
188 class basic_regex_creator
191 basic_regex_creator(regex_data<charT, traits>* data);
192 std::ptrdiff_t getoffset(void* addr)
194 return getoffset(addr, m_pdata->m_data.data());
196 std::ptrdiff_t getoffset(const void* addr, const void* base)
198 return static_cast<const char*>(addr) - static_cast<const char*>(base);
200 re_syntax_base* getaddress(std::ptrdiff_t off)
202 return getaddress(off, m_pdata->m_data.data());
204 re_syntax_base* getaddress(std::ptrdiff_t off, void* base)
206 return static_cast<re_syntax_base*>(static_cast<void*>(static_cast<char*>(base) + off));
208 void init(unsigned l_flags)
210 m_pdata->m_flags = l_flags;
211 m_icase = l_flags & regex_constants::icase;
213 regbase::flag_type flags()
215 return m_pdata->m_flags;
217 void flags(regbase::flag_type f)
219 m_pdata->m_flags = f;
220 if(m_icase != static_cast<bool>(f & regbase::icase))
222 m_icase = static_cast<bool>(f & regbase::icase);
225 re_syntax_base* append_state(syntax_element_type t, std::size_t s = sizeof(re_syntax_base));
226 re_syntax_base* insert_state(std::ptrdiff_t pos, syntax_element_type t, std::size_t s = sizeof(re_syntax_base));
227 re_literal* append_literal(charT c);
228 re_syntax_base* append_set(const basic_char_set<charT, traits>& char_set);
229 re_syntax_base* append_set(const basic_char_set<charT, traits>& char_set, mpl::false_*);
230 re_syntax_base* append_set(const basic_char_set<charT, traits>& char_set, mpl::true_*);
231 void finalize(const charT* p1, const charT* p2);
233 regex_data<charT, traits>* m_pdata; // pointer to the basic_regex_data struct we are filling in
234 const ::boost::regex_traits_wrapper<traits>&
235 m_traits; // convenience reference to traits class
236 re_syntax_base* m_last_state; // the last state we added
237 bool m_icase; // true for case insensitive matches
238 unsigned m_repeater_id; // the state_id of the next repeater
239 bool m_has_backrefs; // true if there are actually any backrefs
240 unsigned m_backrefs; // bitmask of permitted backrefs
241 boost::uintmax_t m_bad_repeats; // bitmask of repeats we can't deduce a startmap for;
242 bool m_has_recursions; // set when we have recursive expresisons to fixup
243 std::vector<unsigned char> m_recursion_checks; // notes which recursions we've followed while analysing this expression
244 typename traits::char_class_type m_word_mask; // mask used to determine if a character is a word character
245 typename traits::char_class_type m_mask_space; // mask used to determine if a character is a word character
246 typename traits::char_class_type m_lower_mask; // mask used to determine if a character is a lowercase character
247 typename traits::char_class_type m_upper_mask; // mask used to determine if a character is an uppercase character
248 typename traits::char_class_type m_alpha_mask; // mask used to determine if a character is an alphabetic character
250 basic_regex_creator& operator=(const basic_regex_creator&);
251 basic_regex_creator(const basic_regex_creator&);
253 void fixup_pointers(re_syntax_base* state);
254 void fixup_recursions(re_syntax_base* state);
255 void create_startmaps(re_syntax_base* state);
256 int calculate_backstep(re_syntax_base* state);
257 void create_startmap(re_syntax_base* state, unsigned char* l_map, unsigned int* pnull, unsigned char mask);
258 unsigned get_restart_type(re_syntax_base* state);
259 void set_all_masks(unsigned char* bits, unsigned char);
260 bool is_bad_repeat(re_syntax_base* pt);
261 void set_bad_repeat(re_syntax_base* pt);
262 syntax_element_type get_repeat_type(re_syntax_base* state);
263 void probe_leading_repeat(re_syntax_base* state);
266 template <class charT, class traits>
267 basic_regex_creator<charT, traits>::basic_regex_creator(regex_data<charT, traits>* data)
268 : 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)
270 m_pdata->m_data.clear();
271 m_pdata->m_status = ::boost::regex_constants::error_ok;
272 static const charT w = 'w';
273 static const charT s = 's';
274 static const charT l[5] = { 'l', 'o', 'w', 'e', 'r', };
275 static const charT u[5] = { 'u', 'p', 'p', 'e', 'r', };
276 static const charT a[5] = { 'a', 'l', 'p', 'h', 'a', };
277 m_word_mask = m_traits.lookup_classname(&w, &w +1);
278 m_mask_space = m_traits.lookup_classname(&s, &s +1);
279 m_lower_mask = m_traits.lookup_classname(l, l + 5);
280 m_upper_mask = m_traits.lookup_classname(u, u + 5);
281 m_alpha_mask = m_traits.lookup_classname(a, a + 5);
282 m_pdata->m_word_mask = m_word_mask;
283 BOOST_ASSERT(m_word_mask != 0);
284 BOOST_ASSERT(m_mask_space != 0);
285 BOOST_ASSERT(m_lower_mask != 0);
286 BOOST_ASSERT(m_upper_mask != 0);
287 BOOST_ASSERT(m_alpha_mask != 0);
290 template <class charT, class traits>
291 re_syntax_base* basic_regex_creator<charT, traits>::append_state(syntax_element_type t, std::size_t s)
293 // if the state is a backref then make a note of it:
294 if(t == syntax_element_backref)
295 this->m_has_backrefs = true;
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:
300 m_last_state->next.i = m_pdata->m_data.size() - getoffset(m_last_state);
301 // now actually extent our data:
302 m_last_state = static_cast<re_syntax_base*>(m_pdata->m_data.extend(s));
303 // fill in boilerplate options in the new state:
304 m_last_state->next.i = 0;
305 m_last_state->type = t;
309 template <class charT, class traits>
310 re_syntax_base* basic_regex_creator<charT, traits>::insert_state(std::ptrdiff_t pos, syntax_element_type t, std::size_t s)
312 // append a new state, start by aligning our last one:
313 m_pdata->m_data.align();
314 // set the offset to the next state in our last one:
316 m_last_state->next.i = m_pdata->m_data.size() - getoffset(m_last_state);
317 // remember the last state position:
318 std::ptrdiff_t off = getoffset(m_last_state) + s;
319 // now actually insert our data:
320 re_syntax_base* new_state = static_cast<re_syntax_base*>(m_pdata->m_data.insert(pos, s));
321 // fill in boilerplate options in the new state:
322 new_state->next.i = s;
324 m_last_state = getaddress(off);
328 template <class charT, class traits>
329 re_literal* basic_regex_creator<charT, traits>::append_literal(charT c)
332 // start by seeing if we have an existing re_literal we can extend:
333 if((0 == m_last_state) || (m_last_state->type != syntax_element_literal))
335 // no existing re_literal, create a new one:
336 result = static_cast<re_literal*>(append_state(syntax_element_literal, sizeof(re_literal) + sizeof(charT)));
338 *static_cast<charT*>(static_cast<void*>(result+1)) = m_traits.translate(c, m_icase);
342 // we have an existing re_literal, extend it:
343 std::ptrdiff_t off = getoffset(m_last_state);
344 m_pdata->m_data.extend(sizeof(charT));
345 m_last_state = result = static_cast<re_literal*>(getaddress(off));
346 charT* characters = static_cast<charT*>(static_cast<void*>(result+1));
347 characters[result->length] = m_traits.translate(c, m_icase);
353 template <class charT, class traits>
354 inline re_syntax_base* basic_regex_creator<charT, traits>::append_set(
355 const basic_char_set<charT, traits>& char_set)
357 typedef mpl::bool_< (sizeof(charT) == 1) > truth_type;
358 return char_set.has_digraphs()
359 ? append_set(char_set, static_cast<mpl::false_*>(0))
360 : append_set(char_set, static_cast<truth_type*>(0));
363 template <class charT, class traits>
364 re_syntax_base* basic_regex_creator<charT, traits>::append_set(
365 const basic_char_set<charT, traits>& char_set, mpl::false_*)
367 typedef typename traits::string_type string_type;
368 typedef typename basic_char_set<charT, traits>::list_iterator item_iterator;
369 typedef typename basic_char_set<charT, traits>::set_iterator set_iterator;
370 typedef typename traits::char_class_type m_type;
372 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>)));
374 // fill in the basics:
376 result->csingles = static_cast<unsigned int>(::boost::BOOST_REGEX_DETAIL_NS::distance(char_set.singles_begin(), char_set.singles_end()));
377 result->cranges = static_cast<unsigned int>(::boost::BOOST_REGEX_DETAIL_NS::distance(char_set.ranges_begin(), char_set.ranges_end())) / 2;
378 result->cequivalents = static_cast<unsigned int>(::boost::BOOST_REGEX_DETAIL_NS::distance(char_set.equivalents_begin(), char_set.equivalents_end()));
379 result->cclasses = char_set.classes();
380 result->cnclasses = char_set.negated_classes();
381 if(flags() & regbase::icase)
383 // adjust classes as needed:
384 if(((result->cclasses & m_lower_mask) == m_lower_mask) || ((result->cclasses & m_upper_mask) == m_upper_mask))
385 result->cclasses |= m_alpha_mask;
386 if(((result->cnclasses & m_lower_mask) == m_lower_mask) || ((result->cnclasses & m_upper_mask) == m_upper_mask))
387 result->cnclasses |= m_alpha_mask;
390 result->isnot = char_set.is_negated();
391 result->singleton = !char_set.has_digraphs();
393 // remember where the state is for later:
395 std::ptrdiff_t offset = getoffset(result);
397 // now extend with all the singles:
399 item_iterator first, last;
400 set_iterator sfirst, slast;
401 sfirst = char_set.singles_begin();
402 slast = char_set.singles_end();
403 while(sfirst != slast)
405 charT* p = static_cast<charT*>(this->m_pdata->m_data.extend(sizeof(charT) * (sfirst->first == static_cast<charT>(0) ? 1 : sfirst->second ? 3 : 2)));
406 p[0] = m_traits.translate(sfirst->first, m_icase);
407 if(sfirst->first == static_cast<charT>(0))
411 else if(sfirst->second)
413 p[1] = m_traits.translate(sfirst->second, m_icase);
421 // now extend with all the ranges:
423 first = char_set.ranges_begin();
424 last = char_set.ranges_end();
427 // first grab the endpoints of the range:
428 digraph<charT> c1 = *first;
429 c1.first = this->m_traits.translate(c1.first, this->m_icase);
430 c1.second = this->m_traits.translate(c1.second, this->m_icase);
432 digraph<charT> c2 = *first;
433 c2.first = this->m_traits.translate(c2.first, this->m_icase);
434 c2.second = this->m_traits.translate(c2.second, this->m_icase);
437 // different actions now depending upon whether collation is turned on:
438 if(flags() & regex_constants::collate)
440 // we need to transform our range into sort keys:
441 charT a1[3] = { c1.first, c1.second, charT(0), };
442 charT a2[3] = { c2.first, c2.second, charT(0), };
443 s1 = this->m_traits.transform(a1, (a1[1] ? a1+2 : a1+1));
444 s2 = this->m_traits.transform(a2, (a2[1] ? a2+2 : a2+1));
446 s1 = string_type(1, charT(0));
448 s2 = string_type(1, charT(0));
454 s1.insert(s1.end(), c1.first);
455 s1.insert(s1.end(), c1.second);
458 s1 = string_type(1, c1.first);
461 s2.insert(s2.end(), c2.first);
462 s2.insert(s2.end(), c2.second);
465 s2.insert(s2.end(), c2.first);
472 charT* p = static_cast<charT*>(this->m_pdata->m_data.extend(sizeof(charT) * (s1.size() + s2.size() + 2) ) );
473 BOOST_REGEX_DETAIL_NS::copy(s1.begin(), s1.end(), p);
474 p[s1.size()] = charT(0);
476 BOOST_REGEX_DETAIL_NS::copy(s2.begin(), s2.end(), p);
477 p[s2.size()] = charT(0);
480 // now process the equivalence classes:
482 sfirst = char_set.equivalents_begin();
483 slast = char_set.equivalents_end();
484 while(sfirst != slast)
489 charT cs[3] = { sfirst->first, sfirst->second, charT(0), };
490 s = m_traits.transform_primary(cs, cs+2);
493 s = m_traits.transform_primary(&sfirst->first, &sfirst->first+1);
495 return 0; // invalid or unsupported equivalence class
496 charT* p = static_cast<charT*>(this->m_pdata->m_data.extend(sizeof(charT) * (s.size()+1) ) );
497 BOOST_REGEX_DETAIL_NS::copy(s.begin(), s.end(), p);
498 p[s.size()] = charT(0);
502 // finally reset the address of our last state:
504 m_last_state = result = static_cast<re_set_long<m_type>*>(getaddress(offset));
509 inline bool char_less(T t1, T t2)
513 inline bool char_less(char t1, char t2)
515 return static_cast<unsigned char>(t1) < static_cast<unsigned char>(t2);
517 inline bool char_less(signed char t1, signed char t2)
519 return static_cast<unsigned char>(t1) < static_cast<unsigned char>(t2);
522 template <class charT, class traits>
523 re_syntax_base* basic_regex_creator<charT, traits>::append_set(
524 const basic_char_set<charT, traits>& char_set, mpl::true_*)
526 typedef typename traits::string_type string_type;
527 typedef typename basic_char_set<charT, traits>::list_iterator item_iterator;
528 typedef typename basic_char_set<charT, traits>::set_iterator set_iterator;
530 re_set* result = static_cast<re_set*>(append_state(syntax_element_set, sizeof(re_set)));
531 bool negate = char_set.is_negated();
532 std::memset(result->_map, 0, sizeof(result->_map));
534 // handle singles first:
536 item_iterator first, last;
537 set_iterator sfirst, slast;
538 sfirst = char_set.singles_begin();
539 slast = char_set.singles_end();
540 while(sfirst != slast)
542 for(unsigned int i = 0; i < (1 << CHAR_BIT); ++i)
544 if(this->m_traits.translate(static_cast<charT>(i), this->m_icase)
545 == this->m_traits.translate(sfirst->first, this->m_icase))
546 result->_map[i] = true;
551 // OK now handle ranges:
553 first = char_set.ranges_begin();
554 last = char_set.ranges_end();
557 // first grab the endpoints of the range:
558 charT c1 = this->m_traits.translate(first->first, this->m_icase);
560 charT c2 = this->m_traits.translate(first->first, this->m_icase);
562 // different actions now depending upon whether collation is turned on:
563 if(flags() & regex_constants::collate)
565 // we need to transform our range into sort keys:
566 charT c3[2] = { c1, charT(0), };
567 string_type s1 = this->m_traits.transform(c3, c3+1);
569 string_type s2 = this->m_traits.transform(c3, c3+1);
575 BOOST_ASSERT(c3[1] == charT(0));
576 for(unsigned i = 0; i < (1u << CHAR_BIT); ++i)
578 c3[0] = static_cast<charT>(i);
579 string_type s3 = this->m_traits.transform(c3, c3 +1);
580 if((s1 <= s3) && (s3 <= s2))
581 result->_map[i] = true;
586 if(char_less(c2, c1))
591 // everything in range matches:
592 std::memset(result->_map + static_cast<unsigned char>(c1), true, 1 + static_cast<unsigned char>(c2) - static_cast<unsigned char>(c1));
596 // and now the classes:
598 typedef typename traits::char_class_type m_type;
599 m_type m = char_set.classes();
600 if(flags() & regbase::icase)
602 // adjust m as needed:
603 if(((m & m_lower_mask) == m_lower_mask) || ((m & m_upper_mask) == m_upper_mask))
608 for(unsigned i = 0; i < (1u << CHAR_BIT); ++i)
610 if(this->m_traits.isctype(static_cast<charT>(i), m))
611 result->_map[i] = true;
615 // and now the negated classes:
617 m = char_set.negated_classes();
618 if(flags() & regbase::icase)
620 // adjust m as needed:
621 if(((m & m_lower_mask) == m_lower_mask) || ((m & m_upper_mask) == m_upper_mask))
626 for(unsigned i = 0; i < (1u << CHAR_BIT); ++i)
628 if(0 == this->m_traits.isctype(static_cast<charT>(i), m))
629 result->_map[i] = true;
633 // now process the equivalence classes:
635 sfirst = char_set.equivalents_begin();
636 slast = char_set.equivalents_end();
637 while(sfirst != slast)
640 BOOST_ASSERT(static_cast<charT>(0) == sfirst->second);
641 s = m_traits.transform_primary(&sfirst->first, &sfirst->first+1);
643 return 0; // invalid or unsupported equivalence class
644 for(unsigned i = 0; i < (1u << CHAR_BIT); ++i)
646 charT c[2] = { (static_cast<charT>(i)), charT(0), };
647 string_type s2 = this->m_traits.transform_primary(c, c+1);
649 result->_map[i] = true;
655 for(unsigned i = 0; i < (1u << CHAR_BIT); ++i)
657 result->_map[i] = !(result->_map[i]);
663 template <class charT, class traits>
664 void basic_regex_creator<charT, traits>::finalize(const charT* p1, const charT* p2)
666 if(this->m_pdata->m_status)
668 // we've added all the states we need, now finish things off.
669 // start by adding a terminating state:
670 append_state(syntax_element_match);
671 // extend storage to store original expression:
672 std::ptrdiff_t len = p2 - p1;
673 m_pdata->m_expression_len = len;
674 charT* ps = static_cast<charT*>(m_pdata->m_data.extend(sizeof(charT) * (1 + (p2 - p1))));
675 m_pdata->m_expression = ps;
676 BOOST_REGEX_DETAIL_NS::copy(p1, p2, ps);
678 // fill in our other data...
679 // successful parsing implies a zero status:
680 m_pdata->m_status = 0;
681 // get the first state of the machine:
682 m_pdata->m_first_state = static_cast<re_syntax_base*>(m_pdata->m_data.data());
683 // fixup pointers in the machine:
684 fixup_pointers(m_pdata->m_first_state);
687 m_pdata->m_has_recursions = true;
688 fixup_recursions(m_pdata->m_first_state);
689 if(this->m_pdata->m_status)
693 m_pdata->m_has_recursions = false;
694 // create nested startmaps:
695 create_startmaps(m_pdata->m_first_state);
696 // create main startmap:
697 std::memset(m_pdata->m_startmap, 0, sizeof(m_pdata->m_startmap));
698 m_pdata->m_can_be_null = 0;
702 m_recursion_checks.assign(1 + m_pdata->m_mark_count, 0u);
703 create_startmap(m_pdata->m_first_state, m_pdata->m_startmap, &(m_pdata->m_can_be_null), mask_all);
704 // get the restart type:
705 m_pdata->m_restart_type = get_restart_type(m_pdata->m_first_state);
706 // optimise a leading repeat if there is one:
707 probe_leading_repeat(m_pdata->m_first_state);
710 template <class charT, class traits>
711 void basic_regex_creator<charT, traits>::fixup_pointers(re_syntax_base* state)
717 case syntax_element_recurse:
718 m_has_recursions = true;
720 state->next.p = getaddress(state->next.i, state);
724 case syntax_element_rep:
725 case syntax_element_dot_rep:
726 case syntax_element_char_rep:
727 case syntax_element_short_set_rep:
728 case syntax_element_long_set_rep:
729 // set the state_id of this repeat:
730 static_cast<re_repeat*>(state)->state_id = m_repeater_id++;
732 case syntax_element_alt:
733 std::memset(static_cast<re_alt*>(state)->_map, 0, sizeof(static_cast<re_alt*>(state)->_map));
734 static_cast<re_alt*>(state)->can_be_null = 0;
736 case syntax_element_jump:
737 static_cast<re_jump*>(state)->alt.p = getaddress(static_cast<re_jump*>(state)->alt.i, state);
741 state->next.p = getaddress(state->next.i, state);
745 state = state->next.p;
749 template <class charT, class traits>
750 void basic_regex_creator<charT, traits>::fixup_recursions(re_syntax_base* state)
752 re_syntax_base* base = state;
757 case syntax_element_assert_backref:
759 // just check that the index is valid:
760 int idx = static_cast<const re_brace*>(state)->index;
766 idx = m_pdata->get_id(idx);
769 // check of sub-expression that doesn't exist:
770 if(0 == this->m_pdata->m_status) // update the error code if not already set
771 this->m_pdata->m_status = boost::regex_constants::error_bad_pattern;
773 // clear the expression, we should be empty:
775 this->m_pdata->m_expression = 0;
776 this->m_pdata->m_expression_len = 0;
778 // and throw if required:
780 if(0 == (this->flags() & regex_constants::no_except))
782 std::string message = "Encountered a forward reference to a marked sub-expression that does not exist.";
783 boost::regex_error e(message, boost::regex_constants::error_bad_pattern, 0);
791 case syntax_element_recurse:
794 re_syntax_base* p = base;
795 std::ptrdiff_t idx = static_cast<re_jump*>(state)->alt.i;
799 // There may be more than one capture group with this hash, just do what Perl
800 // does and recurse to the leftmost:
802 idx = m_pdata->get_id(static_cast<int>(idx));
812 if((p->type == syntax_element_startmark) && (static_cast<re_brace*>(p)->index == idx))
815 // We've found the target of the recursion, set the jump target:
817 static_cast<re_jump*>(state)->alt.p = p;
820 // Now scan the target for nested repeats:
828 case syntax_element_rep:
829 case syntax_element_dot_rep:
830 case syntax_element_char_rep:
831 case syntax_element_short_set_rep:
832 case syntax_element_long_set_rep:
833 next_rep_id = static_cast<re_repeat*>(p)->state_id;
835 case syntax_element_endmark:
836 if(static_cast<const re_brace*>(p)->index == idx)
848 static_cast<re_recurse*>(state)->state_id = next_rep_id - 1;
858 // recursion to sub-expression that doesn't exist:
859 if(0 == this->m_pdata->m_status) // update the error code if not already set
860 this->m_pdata->m_status = boost::regex_constants::error_bad_pattern;
862 // clear the expression, we should be empty:
864 this->m_pdata->m_expression = 0;
865 this->m_pdata->m_expression_len = 0;
867 // and throw if required:
869 if(0 == (this->flags() & regex_constants::no_except))
871 std::string message = "Encountered a forward reference to a recursive sub-expression that does not exist.";
872 boost::regex_error e(message, boost::regex_constants::error_bad_pattern, 0);
881 state = state->next.p;
885 template <class charT, class traits>
886 void basic_regex_creator<charT, traits>::create_startmaps(re_syntax_base* state)
888 // non-recursive implementation:
889 // create the last map in the machine first, so that earlier maps
890 // can make use of the result...
892 // This was originally a recursive implementation, but that caused stack
893 // overflows with complex expressions on small stacks (think COM+).
895 // start by saving the case setting:
896 bool l_icase = m_icase;
897 std::vector<std::pair<bool, re_syntax_base*> > v;
903 case syntax_element_toggle_case:
904 // we need to track case changes here:
905 m_icase = static_cast<re_case*>(state)->icase;
906 state = state->next.p;
908 case syntax_element_alt:
909 case syntax_element_rep:
910 case syntax_element_dot_rep:
911 case syntax_element_char_rep:
912 case syntax_element_short_set_rep:
913 case syntax_element_long_set_rep:
914 // just push the state onto our stack for now:
915 v.push_back(std::pair<bool, re_syntax_base*>(m_icase, state));
916 state = state->next.p;
918 case syntax_element_backstep:
919 // we need to calculate how big the backstep is:
920 static_cast<re_brace*>(state)->index
921 = this->calculate_backstep(state->next.p);
922 if(static_cast<re_brace*>(state)->index < 0)
925 if(0 == this->m_pdata->m_status) // update the error code if not already set
926 this->m_pdata->m_status = boost::regex_constants::error_bad_pattern;
928 // clear the expression, we should be empty:
930 this->m_pdata->m_expression = 0;
931 this->m_pdata->m_expression_len = 0;
933 // and throw if required:
935 if(0 == (this->flags() & regex_constants::no_except))
937 std::string message = "Invalid lookbehind assertion encountered in the regular expression.";
938 boost::regex_error e(message, boost::regex_constants::error_bad_pattern, 0);
944 state = state->next.p;
948 // now work through our list, building all the maps as we go:
951 // Initialize m_recursion_checks if we need it:
953 m_recursion_checks.assign(1 + m_pdata->m_mark_count, 0u);
955 const std::pair<bool, re_syntax_base*>& p = v.back();
962 create_startmap(state->next.p, static_cast<re_alt*>(state)->_map, &static_cast<re_alt*>(state)->can_be_null, mask_take);
966 m_recursion_checks.assign(1 + m_pdata->m_mark_count, 0u);
967 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);
968 // adjust the type of the state to allow for faster matching:
969 state->type = this->get_repeat_type(state);
971 // restore case sensitivity:
975 template <class charT, class traits>
976 int basic_regex_creator<charT, traits>::calculate_backstep(re_syntax_base* state)
978 typedef typename traits::char_class_type m_type;
984 case syntax_element_startmark:
985 if((static_cast<re_brace*>(state)->index == -1)
986 || (static_cast<re_brace*>(state)->index == -2))
988 state = static_cast<re_jump*>(state->next.p)->alt.p->next.p;
991 else if(static_cast<re_brace*>(state)->index == -3)
993 state = state->next.p->next.p;
997 case syntax_element_endmark:
998 if((static_cast<re_brace*>(state)->index == -1)
999 || (static_cast<re_brace*>(state)->index == -2))
1002 case syntax_element_literal:
1003 result += static_cast<re_literal*>(state)->length;
1005 case syntax_element_wild:
1006 case syntax_element_set:
1009 case syntax_element_dot_rep:
1010 case syntax_element_char_rep:
1011 case syntax_element_short_set_rep:
1012 case syntax_element_backref:
1013 case syntax_element_rep:
1014 case syntax_element_combining:
1015 case syntax_element_long_set_rep:
1016 case syntax_element_backstep:
1018 re_repeat* rep = static_cast<re_repeat *>(state);
1019 // adjust the type of the state to allow for faster matching:
1020 state->type = this->get_repeat_type(state);
1021 if((state->type == syntax_element_dot_rep)
1022 || (state->type == syntax_element_char_rep)
1023 || (state->type == syntax_element_short_set_rep))
1025 if(rep->max != rep->min)
1027 result += static_cast<int>(rep->min);
1031 else if(state->type == syntax_element_long_set_rep)
1033 BOOST_ASSERT(rep->next.p->type == syntax_element_long_set);
1034 if(static_cast<re_set_long<m_type>*>(rep->next.p)->singleton == 0)
1036 if(rep->max != rep->min)
1038 result += static_cast<int>(rep->min);
1044 case syntax_element_long_set:
1045 if(static_cast<re_set_long<m_type>*>(state)->singleton == 0)
1049 case syntax_element_jump:
1050 state = static_cast<re_jump*>(state)->alt.p;
1052 case syntax_element_alt:
1054 int r1 = calculate_backstep(state->next.p);
1055 int r2 = calculate_backstep(static_cast<re_alt*>(state)->alt.p);
1056 if((r1 < 0) || (r1 != r2))
1063 state = state->next.p;
1068 template <class charT, class traits>
1069 void basic_regex_creator<charT, traits>::create_startmap(re_syntax_base* state, unsigned char* l_map, unsigned int* pnull, unsigned char mask)
1071 int not_last_jump = 1;
1072 re_syntax_base* recursion_start = 0;
1073 int recursion_sub = 0;
1074 re_syntax_base* recursion_restart = 0;
1076 // track case sensitivity:
1077 bool l_icase = m_icase;
1083 case syntax_element_toggle_case:
1084 l_icase = static_cast<re_case*>(state)->icase;
1085 state = state->next.p;
1087 case syntax_element_literal:
1089 // don't set anything in *pnull, set each element in l_map
1090 // that could match the first character in the literal:
1093 l_map[0] |= mask_init;
1094 charT first_char = *static_cast<charT*>(static_cast<void*>(static_cast<re_literal*>(state) + 1));
1095 for(unsigned int i = 0; i < (1u << CHAR_BIT); ++i)
1097 if(m_traits.translate(static_cast<charT>(i), l_icase) == first_char)
1103 case syntax_element_end_line:
1105 // next character must be a line separator (if there is one):
1108 l_map[0] |= mask_init;
1109 l_map[static_cast<unsigned>('\n')] |= mask;
1110 l_map[static_cast<unsigned>('\r')] |= mask;
1111 l_map[static_cast<unsigned>('\f')] |= mask;
1112 l_map[0x85] |= mask;
1114 // now figure out if we can match a NULL string at this point:
1116 create_startmap(state->next.p, 0, pnull, mask);
1119 case syntax_element_recurse:
1121 BOOST_ASSERT(static_cast<const re_jump*>(state)->alt.p->type == syntax_element_startmark);
1122 recursion_sub = static_cast<re_brace*>(static_cast<const re_jump*>(state)->alt.p)->index;
1123 if(m_recursion_checks[recursion_sub] & 1u)
1125 // Infinite recursion!!
1126 if(0 == this->m_pdata->m_status) // update the error code if not already set
1127 this->m_pdata->m_status = boost::regex_constants::error_bad_pattern;
1129 // clear the expression, we should be empty:
1131 this->m_pdata->m_expression = 0;
1132 this->m_pdata->m_expression_len = 0;
1134 // and throw if required:
1136 if(0 == (this->flags() & regex_constants::no_except))
1138 std::string message = "Encountered an infinite recursion.";
1139 boost::regex_error e(message, boost::regex_constants::error_bad_pattern, 0);
1143 else if(recursion_start == 0)
1145 recursion_start = state;
1146 recursion_restart = state->next.p;
1147 state = static_cast<re_jump*>(state)->alt.p;
1148 m_recursion_checks[recursion_sub] |= 1u;
1151 m_recursion_checks[recursion_sub] |= 1u;
1152 // can't handle nested recursion here...
1155 case syntax_element_backref:
1156 // can be null, and any character can match:
1160 case syntax_element_wild:
1162 // can't be null, any character can match:
1163 set_all_masks(l_map, mask);
1166 case syntax_element_accept:
1167 case syntax_element_match:
1169 // must be null, any character can match:
1170 set_all_masks(l_map, mask);
1175 case syntax_element_word_start:
1177 // recurse, then AND with all the word characters:
1178 create_startmap(state->next.p, l_map, pnull, mask);
1181 l_map[0] |= mask_init;
1182 for(unsigned int i = 0; i < (1u << CHAR_BIT); ++i)
1184 if(!m_traits.isctype(static_cast<charT>(i), m_word_mask))
1185 l_map[i] &= static_cast<unsigned char>(~mask);
1190 case syntax_element_word_end:
1192 // recurse, then AND with all the word characters:
1193 create_startmap(state->next.p, l_map, pnull, mask);
1196 l_map[0] |= mask_init;
1197 for(unsigned int i = 0; i < (1u << CHAR_BIT); ++i)
1199 if(m_traits.isctype(static_cast<charT>(i), m_word_mask))
1200 l_map[i] &= static_cast<unsigned char>(~mask);
1205 case syntax_element_buffer_end:
1207 // we *must be null* :
1212 case syntax_element_long_set:
1215 typedef typename traits::char_class_type m_type;
1216 if(static_cast<re_set_long<m_type>*>(state)->singleton)
1218 l_map[0] |= mask_init;
1219 for(unsigned int i = 0; i < (1u << CHAR_BIT); ++i)
1221 charT c = static_cast<charT>(i);
1222 if(&c != re_is_set_member(&c, &c + 1, static_cast<re_set_long<m_type>*>(state), *m_pdata, l_icase))
1227 set_all_masks(l_map, mask);
1230 case syntax_element_set:
1233 l_map[0] |= mask_init;
1234 for(unsigned int i = 0; i < (1u << CHAR_BIT); ++i)
1236 if(static_cast<re_set*>(state)->_map[
1237 static_cast<unsigned char>(m_traits.translate(static_cast<charT>(i), l_icase))])
1242 case syntax_element_jump:
1244 state = static_cast<re_alt*>(state)->alt.p;
1247 case syntax_element_alt:
1248 case syntax_element_rep:
1249 case syntax_element_dot_rep:
1250 case syntax_element_char_rep:
1251 case syntax_element_short_set_rep:
1252 case syntax_element_long_set_rep:
1254 re_alt* rep = static_cast<re_alt*>(state);
1255 if(rep->_map[0] & mask_init)
1259 // copy previous results:
1260 l_map[0] |= mask_init;
1261 for(unsigned int i = 0; i <= UCHAR_MAX; ++i)
1263 if(rep->_map[i] & mask_any)
1269 if(rep->can_be_null & mask_any)
1275 // we haven't created a startmap for this alternative yet
1276 // so take the union of the two options:
1277 if(is_bad_repeat(state))
1279 set_all_masks(l_map, mask);
1284 set_bad_repeat(state);
1285 create_startmap(state->next.p, l_map, pnull, mask);
1286 if((state->type == syntax_element_alt)
1287 || (static_cast<re_repeat*>(state)->min == 0)
1288 || (not_last_jump == 0))
1289 create_startmap(rep->alt.p, l_map, pnull, mask);
1293 case syntax_element_soft_buffer_end:
1294 // match newline or null:
1297 l_map[0] |= mask_init;
1298 l_map[static_cast<unsigned>('\n')] |= mask;
1299 l_map[static_cast<unsigned>('\r')] |= mask;
1304 case syntax_element_endmark:
1305 // need to handle independent subs as a special case:
1306 if(static_cast<re_brace*>(state)->index < 0)
1308 // can be null, any character can match:
1309 set_all_masks(l_map, mask);
1314 else if(recursion_start && (recursion_sub != 0) && (recursion_sub == static_cast<re_brace*>(state)->index))
1316 // recursion termination:
1317 recursion_start = 0;
1318 state = recursion_restart;
1323 // Normally we just go to the next state... but if this sub-expression is
1324 // the target of a recursion, then we might be ending a recursion, in which
1325 // case we should check whatever follows that recursion, as well as whatever
1326 // follows this state:
1328 if(m_pdata->m_has_recursions && static_cast<re_brace*>(state)->index)
1331 re_syntax_base* p = m_pdata->m_first_state;
1334 if(p->type == syntax_element_recurse)
1336 re_brace* p2 = static_cast<re_brace*>(static_cast<re_jump*>(p)->alt.p);
1337 if((p2->type == syntax_element_startmark) && (p2->index == static_cast<re_brace*>(state)->index))
1345 if(ok && ((m_recursion_checks[static_cast<re_brace*>(state)->index] & 2u) == 0))
1347 m_recursion_checks[static_cast<re_brace*>(state)->index] |= 2u;
1348 create_startmap(p->next.p, l_map, pnull, mask);
1351 state = state->next.p;
1354 case syntax_element_commit:
1355 set_all_masks(l_map, mask);
1356 // Continue scanning so we can figure out whether we can be null:
1357 state = state->next.p;
1359 case syntax_element_startmark:
1360 // need to handle independent subs as a special case:
1361 if(static_cast<re_brace*>(state)->index == -3)
1363 state = state->next.p->next.p;
1368 state = state->next.p;
1374 template <class charT, class traits>
1375 unsigned basic_regex_creator<charT, traits>::get_restart_type(re_syntax_base* state)
1378 // find out how the machine starts, so we can optimise the search:
1384 case syntax_element_startmark:
1385 case syntax_element_endmark:
1386 state = state->next.p;
1388 case syntax_element_start_line:
1389 return regbase::restart_line;
1390 case syntax_element_word_start:
1391 return regbase::restart_word;
1392 case syntax_element_buffer_start:
1393 return regbase::restart_buf;
1394 case syntax_element_restart_continue:
1395 return regbase::restart_continue;
1401 return regbase::restart_any;
1404 template <class charT, class traits>
1405 void basic_regex_creator<charT, traits>::set_all_masks(unsigned char* bits, unsigned char mask)
1408 // set mask in all of bits elements,
1409 // if bits[0] has mask_init not set then we can
1410 // optimise this to a call to memset:
1415 (std::memset)(bits, mask, 1u << CHAR_BIT);
1418 for(unsigned i = 0; i < (1u << CHAR_BIT); ++i)
1421 bits[0] |= mask_init;
1425 template <class charT, class traits>
1426 bool basic_regex_creator<charT, traits>::is_bad_repeat(re_syntax_base* pt)
1430 case syntax_element_rep:
1431 case syntax_element_dot_rep:
1432 case syntax_element_char_rep:
1433 case syntax_element_short_set_rep:
1434 case syntax_element_long_set_rep:
1436 unsigned state_id = static_cast<re_repeat*>(pt)->state_id;
1437 if(state_id >= sizeof(m_bad_repeats) * CHAR_BIT)
1438 return true; // run out of bits, assume we can't traverse this one.
1439 static const boost::uintmax_t one = 1uL;
1440 return m_bad_repeats & (one << state_id);
1447 template <class charT, class traits>
1448 void basic_regex_creator<charT, traits>::set_bad_repeat(re_syntax_base* pt)
1452 case syntax_element_rep:
1453 case syntax_element_dot_rep:
1454 case syntax_element_char_rep:
1455 case syntax_element_short_set_rep:
1456 case syntax_element_long_set_rep:
1458 unsigned state_id = static_cast<re_repeat*>(pt)->state_id;
1459 static const boost::uintmax_t one = 1uL;
1460 if(state_id <= sizeof(m_bad_repeats) * CHAR_BIT)
1461 m_bad_repeats |= (one << state_id);
1469 template <class charT, class traits>
1470 syntax_element_type basic_regex_creator<charT, traits>::get_repeat_type(re_syntax_base* state)
1472 typedef typename traits::char_class_type m_type;
1473 if(state->type == syntax_element_rep)
1475 // check to see if we are repeating a single state:
1476 if(state->next.p->next.p->next.p == static_cast<re_alt*>(state)->alt.p)
1478 switch(state->next.p->type)
1480 case BOOST_REGEX_DETAIL_NS::syntax_element_wild:
1481 return BOOST_REGEX_DETAIL_NS::syntax_element_dot_rep;
1482 case BOOST_REGEX_DETAIL_NS::syntax_element_literal:
1483 return BOOST_REGEX_DETAIL_NS::syntax_element_char_rep;
1484 case BOOST_REGEX_DETAIL_NS::syntax_element_set:
1485 return BOOST_REGEX_DETAIL_NS::syntax_element_short_set_rep;
1486 case BOOST_REGEX_DETAIL_NS::syntax_element_long_set:
1487 if(static_cast<BOOST_REGEX_DETAIL_NS::re_set_long<m_type>*>(state->next.p)->singleton)
1488 return BOOST_REGEX_DETAIL_NS::syntax_element_long_set_rep;
1498 template <class charT, class traits>
1499 void basic_regex_creator<charT, traits>::probe_leading_repeat(re_syntax_base* state)
1501 // enumerate our states, and see if we have a leading repeat
1502 // for which failed search restarts can be optimised;
1507 case syntax_element_startmark:
1508 if(static_cast<re_brace*>(state)->index >= 0)
1510 state = state->next.p;
1513 if((static_cast<re_brace*>(state)->index == -1)
1514 || (static_cast<re_brace*>(state)->index == -2))
1516 // skip past the zero width assertion:
1517 state = static_cast<const re_jump*>(state->next.p)->alt.p->next.p;
1520 if(static_cast<re_brace*>(state)->index == -3)
1522 // Have to skip the leading jump state:
1523 state = state->next.p->next.p;
1527 case syntax_element_endmark:
1528 case syntax_element_start_line:
1529 case syntax_element_end_line:
1530 case syntax_element_word_boundary:
1531 case syntax_element_within_word:
1532 case syntax_element_word_start:
1533 case syntax_element_word_end:
1534 case syntax_element_buffer_start:
1535 case syntax_element_buffer_end:
1536 case syntax_element_restart_continue:
1537 state = state->next.p;
1539 case syntax_element_dot_rep:
1540 case syntax_element_char_rep:
1541 case syntax_element_short_set_rep:
1542 case syntax_element_long_set_rep:
1543 if(this->m_has_backrefs == 0)
1544 static_cast<re_repeat*>(state)->leading = true;
1553 } // namespace BOOST_REGEX_DETAIL_NS
1555 } // namespace boost
1558 # pragma warning(pop)
1562 #pragma warning(push)
1563 #pragma warning(disable: 4103)
1565 #ifdef BOOST_HAS_ABI_HEADERS
1566 # include BOOST_ABI_SUFFIX
1569 #pragma warning(pop)