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