| 1 | /* |
|---|
| 2 | * |
|---|
| 3 | * Copyright (c) 2002 |
|---|
| 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 perl_matcher_common.cpp |
|---|
| 15 | * VERSION see <boost/version.hpp> |
|---|
| 16 | * DESCRIPTION: Definitions of perl_matcher member functions that are |
|---|
| 17 | * common to both the recursive and non-recursive versions. |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #ifndef BOOST_REGEX_V4_PERL_MATCHER_COMMON_HPP |
|---|
| 21 | #define BOOST_REGEX_V4_PERL_MATCHER_COMMON_HPP |
|---|
| 22 | |
|---|
| 23 | #ifdef BOOST_HAS_ABI_HEADERS |
|---|
| 24 | # include BOOST_ABI_PREFIX |
|---|
| 25 | #endif |
|---|
| 26 | |
|---|
| 27 | #ifdef __BORLANDC__ |
|---|
| 28 | # pragma option push -w-8008 -w-8066 |
|---|
| 29 | #endif |
|---|
| 30 | |
|---|
| 31 | namespace boost{ |
|---|
| 32 | namespace re_detail{ |
|---|
| 33 | |
|---|
| 34 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 35 | void perl_matcher<BidiIterator, Allocator, traits>::construct_init(const basic_regex<char_type, traits>& e, match_flag_type f) |
|---|
| 36 | { |
|---|
| 37 | typedef typename regex_iterator_traits<BidiIterator>::iterator_category category; |
|---|
| 38 | typedef typename basic_regex<char_type, traits>::flag_type expression_flag_type; |
|---|
| 39 | |
|---|
| 40 | if(e.empty()) |
|---|
| 41 | { |
|---|
| 42 | // precondition failure: e is not a valid regex. |
|---|
| 43 | std::invalid_argument ex("Invalid regular expression object"); |
|---|
| 44 | boost::throw_exception(ex); |
|---|
| 45 | } |
|---|
| 46 | pstate = 0; |
|---|
| 47 | m_match_flags = f; |
|---|
| 48 | estimate_max_state_count(static_cast<category*>(0)); |
|---|
| 49 | expression_flag_type re_f = re.flags(); |
|---|
| 50 | icase = re_f & regex_constants::icase; |
|---|
| 51 | if(!(m_match_flags & (match_perl|match_posix))) |
|---|
| 52 | { |
|---|
| 53 | if((re_f & (regbase::main_option_type|regbase::no_perl_ex)) == 0) |
|---|
| 54 | m_match_flags |= match_perl; |
|---|
| 55 | else if((re_f & (regbase::main_option_type|regbase::emacs_ex)) == (regbase::basic_syntax_group|regbase::emacs_ex)) |
|---|
| 56 | m_match_flags |= match_perl; |
|---|
| 57 | else |
|---|
| 58 | m_match_flags |= match_posix; |
|---|
| 59 | } |
|---|
| 60 | if(m_match_flags & match_posix) |
|---|
| 61 | { |
|---|
| 62 | m_temp_match.reset(new match_results<BidiIterator, Allocator>()); |
|---|
| 63 | m_presult = m_temp_match.get(); |
|---|
| 64 | } |
|---|
| 65 | else |
|---|
| 66 | m_presult = &m_result; |
|---|
| 67 | #ifdef BOOST_REGEX_NON_RECURSIVE |
|---|
| 68 | m_stack_base = 0; |
|---|
| 69 | m_backup_state = 0; |
|---|
| 70 | #endif |
|---|
| 71 | // find the value to use for matching word boundaries: |
|---|
| 72 | m_word_mask = re.get_data().m_word_mask; |
|---|
| 73 | // find bitmask to use for matching '.': |
|---|
| 74 | match_any_mask = static_cast<unsigned char>((f & match_not_dot_newline) ? re_detail::test_not_newline : re_detail::test_newline); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 78 | void perl_matcher<BidiIterator, Allocator, traits>::estimate_max_state_count(std::random_access_iterator_tag*) |
|---|
| 79 | { |
|---|
| 80 | // |
|---|
| 81 | // How many states should we allow our machine to visit before giving up? |
|---|
| 82 | // This is a heuristic: it takes the greater of O(N^2) and O(NS^2) |
|---|
| 83 | // where N is the length of the string, and S is the number of states |
|---|
| 84 | // in the machine. It's tempting to up this to O(N^2S) or even O(N^2S^2) |
|---|
| 85 | // but these take unreasonably amounts of time to bale out in pathological |
|---|
| 86 | // cases. |
|---|
| 87 | // |
|---|
| 88 | // Calculate NS^2 first: |
|---|
| 89 | // |
|---|
| 90 | static const boost::uintmax_t k = 100000; |
|---|
| 91 | boost::uintmax_t dist = boost::re_detail::distance(base, last); |
|---|
| 92 | if(dist == 0) |
|---|
| 93 | dist = 1; |
|---|
| 94 | boost::uintmax_t states = re.size(); |
|---|
| 95 | if(states == 0) |
|---|
| 96 | states = 1; |
|---|
| 97 | states *= states; |
|---|
| 98 | if((std::numeric_limits<boost::uintmax_t>::max)() / dist < states) |
|---|
| 99 | { |
|---|
| 100 | max_state_count = (std::numeric_limits<boost::uintmax_t>::max)() - 2; |
|---|
| 101 | return; |
|---|
| 102 | } |
|---|
| 103 | states *= dist; |
|---|
| 104 | if((std::numeric_limits<boost::uintmax_t>::max)() - k < states) |
|---|
| 105 | { |
|---|
| 106 | max_state_count = (std::numeric_limits<boost::uintmax_t>::max)() - 2; |
|---|
| 107 | return; |
|---|
| 108 | } |
|---|
| 109 | states += k; |
|---|
| 110 | |
|---|
| 111 | max_state_count = states; |
|---|
| 112 | |
|---|
| 113 | // |
|---|
| 114 | // Now calculate N^2: |
|---|
| 115 | // |
|---|
| 116 | states = dist; |
|---|
| 117 | if((std::numeric_limits<boost::uintmax_t>::max)() / dist < states) |
|---|
| 118 | { |
|---|
| 119 | max_state_count = (std::numeric_limits<boost::uintmax_t>::max)() - 2; |
|---|
| 120 | return; |
|---|
| 121 | } |
|---|
| 122 | states *= dist; |
|---|
| 123 | if((std::numeric_limits<boost::uintmax_t>::max)() - k < states) |
|---|
| 124 | { |
|---|
| 125 | max_state_count = (std::numeric_limits<boost::uintmax_t>::max)() - 2; |
|---|
| 126 | return; |
|---|
| 127 | } |
|---|
| 128 | states += k; |
|---|
| 129 | // |
|---|
| 130 | // N^2 can be a very large number indeed, to prevent things getting out |
|---|
| 131 | // of control, cap the max states: |
|---|
| 132 | // |
|---|
| 133 | if(states > BOOST_REGEX_MAX_STATE_COUNT) |
|---|
| 134 | states = BOOST_REGEX_MAX_STATE_COUNT; |
|---|
| 135 | // |
|---|
| 136 | // If (the possibly capped) N^2 is larger than our first estimate, |
|---|
| 137 | // use this instead: |
|---|
| 138 | // |
|---|
| 139 | if(states > max_state_count) |
|---|
| 140 | max_state_count = states; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 144 | inline void perl_matcher<BidiIterator, Allocator, traits>::estimate_max_state_count(void*) |
|---|
| 145 | { |
|---|
| 146 | // we don't know how long the sequence is: |
|---|
| 147 | max_state_count = BOOST_REGEX_MAX_STATE_COUNT; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | #ifdef BOOST_REGEX_HAS_MS_STACK_GUARD |
|---|
| 151 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 152 | inline bool perl_matcher<BidiIterator, Allocator, traits>::protected_call( |
|---|
| 153 | protected_proc_type proc) |
|---|
| 154 | { |
|---|
| 155 | ::boost::re_detail::concrete_protected_call |
|---|
| 156 | <perl_matcher<BidiIterator, Allocator, traits> > |
|---|
| 157 | obj(this, proc); |
|---|
| 158 | return obj.execute(); |
|---|
| 159 | |
|---|
| 160 | } |
|---|
| 161 | #endif |
|---|
| 162 | |
|---|
| 163 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 164 | inline bool perl_matcher<BidiIterator, Allocator, traits>::match() |
|---|
| 165 | { |
|---|
| 166 | #ifdef BOOST_REGEX_HAS_MS_STACK_GUARD |
|---|
| 167 | return protected_call(&perl_matcher<BidiIterator, Allocator, traits>::match_imp); |
|---|
| 168 | #else |
|---|
| 169 | return match_imp(); |
|---|
| 170 | #endif |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 174 | bool perl_matcher<BidiIterator, Allocator, traits>::match_imp() |
|---|
| 175 | { |
|---|
| 176 | // initialise our stack if we are non-recursive: |
|---|
| 177 | #ifdef BOOST_REGEX_NON_RECURSIVE |
|---|
| 178 | save_state_init init(&m_stack_base, &m_backup_state); |
|---|
| 179 | used_block_count = BOOST_REGEX_MAX_BLOCKS; |
|---|
| 180 | #if !defined(BOOST_NO_EXCEPTIONS) |
|---|
| 181 | try{ |
|---|
| 182 | #endif |
|---|
| 183 | #endif |
|---|
| 184 | |
|---|
| 185 | // reset our state machine: |
|---|
| 186 | position = base; |
|---|
| 187 | search_base = base; |
|---|
| 188 | state_count = 0; |
|---|
| 189 | m_match_flags |= regex_constants::match_all; |
|---|
| 190 | m_presult->set_size((m_match_flags & match_nosubs) ? 1 : re.mark_count(), search_base, last); |
|---|
| 191 | m_presult->set_base(base); |
|---|
| 192 | if(m_match_flags & match_posix) |
|---|
| 193 | m_result = *m_presult; |
|---|
| 194 | verify_options(re.flags(), m_match_flags); |
|---|
| 195 | if(0 == match_prefix()) |
|---|
| 196 | return false; |
|---|
| 197 | return m_result[0].second == last; |
|---|
| 198 | |
|---|
| 199 | #if defined(BOOST_REGEX_NON_RECURSIVE) && !defined(BOOST_NO_EXCEPTIONS) |
|---|
| 200 | } |
|---|
| 201 | catch(...) |
|---|
| 202 | { |
|---|
| 203 | // unwind all pushed states, apart from anything else this |
|---|
| 204 | // ensures that all the states are correctly destructed |
|---|
| 205 | // not just the memory freed. |
|---|
| 206 | while(unwind(true)){} |
|---|
| 207 | throw; |
|---|
| 208 | } |
|---|
| 209 | #endif |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 213 | inline bool perl_matcher<BidiIterator, Allocator, traits>::find() |
|---|
| 214 | { |
|---|
| 215 | #ifdef BOOST_REGEX_HAS_MS_STACK_GUARD |
|---|
| 216 | return protected_call(&perl_matcher<BidiIterator, Allocator, traits>::find_imp); |
|---|
| 217 | #else |
|---|
| 218 | return find_imp(); |
|---|
| 219 | #endif |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 223 | bool perl_matcher<BidiIterator, Allocator, traits>::find_imp() |
|---|
| 224 | { |
|---|
| 225 | static matcher_proc_type const s_find_vtable[7] = |
|---|
| 226 | { |
|---|
| 227 | &perl_matcher<BidiIterator, Allocator, traits>::find_restart_any, |
|---|
| 228 | &perl_matcher<BidiIterator, Allocator, traits>::find_restart_word, |
|---|
| 229 | &perl_matcher<BidiIterator, Allocator, traits>::find_restart_line, |
|---|
| 230 | &perl_matcher<BidiIterator, Allocator, traits>::find_restart_buf, |
|---|
| 231 | &perl_matcher<BidiIterator, Allocator, traits>::match_prefix, |
|---|
| 232 | &perl_matcher<BidiIterator, Allocator, traits>::find_restart_lit, |
|---|
| 233 | &perl_matcher<BidiIterator, Allocator, traits>::find_restart_lit, |
|---|
| 234 | }; |
|---|
| 235 | |
|---|
| 236 | // initialise our stack if we are non-recursive: |
|---|
| 237 | #ifdef BOOST_REGEX_NON_RECURSIVE |
|---|
| 238 | save_state_init init(&m_stack_base, &m_backup_state); |
|---|
| 239 | used_block_count = BOOST_REGEX_MAX_BLOCKS; |
|---|
| 240 | #if !defined(BOOST_NO_EXCEPTIONS) |
|---|
| 241 | try{ |
|---|
| 242 | #endif |
|---|
| 243 | #endif |
|---|
| 244 | |
|---|
| 245 | state_count = 0; |
|---|
| 246 | if((m_match_flags & regex_constants::match_init) == 0) |
|---|
| 247 | { |
|---|
| 248 | // reset our state machine: |
|---|
| 249 | search_base = position = base; |
|---|
| 250 | pstate = re.get_first_state(); |
|---|
| 251 | m_presult->set_size((m_match_flags & match_nosubs) ? 1 : re.mark_count(), base, last); |
|---|
| 252 | m_presult->set_base(base); |
|---|
| 253 | m_match_flags |= regex_constants::match_init; |
|---|
| 254 | } |
|---|
| 255 | else |
|---|
| 256 | { |
|---|
| 257 | // start again: |
|---|
| 258 | search_base = position = m_result[0].second; |
|---|
| 259 | // If last match was null and match_not_null was not set then increment |
|---|
| 260 | // our start position, otherwise we go into an infinite loop: |
|---|
| 261 | if(((m_match_flags & match_not_null) == 0) && (m_result.length() == 0)) |
|---|
| 262 | { |
|---|
| 263 | if(position == last) |
|---|
| 264 | return false; |
|---|
| 265 | else |
|---|
| 266 | ++position; |
|---|
| 267 | } |
|---|
| 268 | // reset $` start: |
|---|
| 269 | m_presult->set_size((m_match_flags & match_nosubs) ? 1 : re.mark_count(), search_base, last); |
|---|
| 270 | //if((base != search_base) && (base == backstop)) |
|---|
| 271 | // m_match_flags |= match_prev_avail; |
|---|
| 272 | } |
|---|
| 273 | if(m_match_flags & match_posix) |
|---|
| 274 | { |
|---|
| 275 | m_result.set_size(re.mark_count(), base, last); |
|---|
| 276 | m_result.set_base(base); |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | verify_options(re.flags(), m_match_flags); |
|---|
| 280 | // find out what kind of expression we have: |
|---|
| 281 | unsigned type = (m_match_flags & match_continuous) ? |
|---|
| 282 | static_cast<unsigned int>(regbase::restart_continue) |
|---|
| 283 | : static_cast<unsigned int>(re.get_restart_type()); |
|---|
| 284 | |
|---|
| 285 | // call the appropriate search routine: |
|---|
| 286 | matcher_proc_type proc = s_find_vtable[type]; |
|---|
| 287 | return (this->*proc)(); |
|---|
| 288 | |
|---|
| 289 | #if defined(BOOST_REGEX_NON_RECURSIVE) && !defined(BOOST_NO_EXCEPTIONS) |
|---|
| 290 | } |
|---|
| 291 | catch(...) |
|---|
| 292 | { |
|---|
| 293 | // unwind all pushed states, apart from anything else this |
|---|
| 294 | // ensures that all the states are correctly destructed |
|---|
| 295 | // not just the memory freed. |
|---|
| 296 | while(unwind(true)){} |
|---|
| 297 | throw; |
|---|
| 298 | } |
|---|
| 299 | #endif |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 303 | bool perl_matcher<BidiIterator, Allocator, traits>::match_prefix() |
|---|
| 304 | { |
|---|
| 305 | m_has_partial_match = false; |
|---|
| 306 | m_has_found_match = false; |
|---|
| 307 | pstate = re.get_first_state(); |
|---|
| 308 | m_presult->set_first(position); |
|---|
| 309 | restart = position; |
|---|
| 310 | match_all_states(); |
|---|
| 311 | if(!m_has_found_match && m_has_partial_match && (m_match_flags & match_partial)) |
|---|
| 312 | { |
|---|
| 313 | m_has_found_match = true; |
|---|
| 314 | m_presult->set_second(last, 0, false); |
|---|
| 315 | position = last; |
|---|
| 316 | } |
|---|
| 317 | #ifdef BOOST_REGEX_MATCH_EXTRA |
|---|
| 318 | if(m_has_found_match && (match_extra & m_match_flags)) |
|---|
| 319 | { |
|---|
| 320 | // |
|---|
| 321 | // we have a match, reverse the capture information: |
|---|
| 322 | // |
|---|
| 323 | for(unsigned i = 0; i < m_presult->size(); ++i) |
|---|
| 324 | { |
|---|
| 325 | typename sub_match<BidiIterator>::capture_sequence_type & seq = ((*m_presult)[i]).get_captures(); |
|---|
| 326 | std::reverse(seq.begin(), seq.end()); |
|---|
| 327 | } |
|---|
| 328 | } |
|---|
| 329 | #endif |
|---|
| 330 | if(!m_has_found_match) |
|---|
| 331 | position = restart; // reset search postion |
|---|
| 332 | return m_has_found_match; |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 336 | bool perl_matcher<BidiIterator, Allocator, traits>::match_endmark() |
|---|
| 337 | { |
|---|
| 338 | int index = static_cast<const re_brace*>(pstate)->index; |
|---|
| 339 | if(index > 0) |
|---|
| 340 | { |
|---|
| 341 | if((m_match_flags & match_nosubs) == 0) |
|---|
| 342 | m_presult->set_second(position, index); |
|---|
| 343 | } |
|---|
| 344 | else if((index < 0) && (index != -4)) |
|---|
| 345 | { |
|---|
| 346 | // matched forward lookahead: |
|---|
| 347 | pstate = 0; |
|---|
| 348 | return true; |
|---|
| 349 | } |
|---|
| 350 | pstate = pstate->next.p; |
|---|
| 351 | return true; |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 355 | bool perl_matcher<BidiIterator, Allocator, traits>::match_literal() |
|---|
| 356 | { |
|---|
| 357 | unsigned int len = static_cast<const re_literal*>(pstate)->length; |
|---|
| 358 | const char_type* what = reinterpret_cast<const char_type*>(static_cast<const re_literal*>(pstate) + 1); |
|---|
| 359 | // |
|---|
| 360 | // compare string with what we stored in |
|---|
| 361 | // our records: |
|---|
| 362 | for(unsigned int i = 0; i < len; ++i, ++position) |
|---|
| 363 | { |
|---|
| 364 | if((position == last) || (traits_inst.translate(*position, icase) != what[i])) |
|---|
| 365 | return false; |
|---|
| 366 | } |
|---|
| 367 | pstate = pstate->next.p; |
|---|
| 368 | return true; |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 372 | bool perl_matcher<BidiIterator, Allocator, traits>::match_start_line() |
|---|
| 373 | { |
|---|
| 374 | if(position == backstop) |
|---|
| 375 | { |
|---|
| 376 | if((m_match_flags & match_prev_avail) == 0) |
|---|
| 377 | { |
|---|
| 378 | if((m_match_flags & match_not_bol) == 0) |
|---|
| 379 | { |
|---|
| 380 | pstate = pstate->next.p; |
|---|
| 381 | return true; |
|---|
| 382 | } |
|---|
| 383 | return false; |
|---|
| 384 | } |
|---|
| 385 | } |
|---|
| 386 | else if(m_match_flags & match_single_line) |
|---|
| 387 | return false; |
|---|
| 388 | |
|---|
| 389 | // check the previous value character: |
|---|
| 390 | BidiIterator t(position); |
|---|
| 391 | --t; |
|---|
| 392 | if(position != last) |
|---|
| 393 | { |
|---|
| 394 | if(is_separator(*t) && !((*t == static_cast<char_type>('\r')) && (*position == static_cast<char_type>('\n'))) ) |
|---|
| 395 | { |
|---|
| 396 | pstate = pstate->next.p; |
|---|
| 397 | return true; |
|---|
| 398 | } |
|---|
| 399 | } |
|---|
| 400 | else if(is_separator(*t)) |
|---|
| 401 | { |
|---|
| 402 | pstate = pstate->next.p; |
|---|
| 403 | return true; |
|---|
| 404 | } |
|---|
| 405 | return false; |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 409 | bool perl_matcher<BidiIterator, Allocator, traits>::match_end_line() |
|---|
| 410 | { |
|---|
| 411 | if(position != last) |
|---|
| 412 | { |
|---|
| 413 | if(m_match_flags & match_single_line) |
|---|
| 414 | return false; |
|---|
| 415 | // we're not yet at the end so *first is always valid: |
|---|
| 416 | if(is_separator(*position)) |
|---|
| 417 | { |
|---|
| 418 | if((position != backstop) || (m_match_flags & match_prev_avail)) |
|---|
| 419 | { |
|---|
| 420 | // check that we're not in the middle of \r\n sequence |
|---|
| 421 | BidiIterator t(position); |
|---|
| 422 | --t; |
|---|
| 423 | if((*t == static_cast<char_type>('\r')) && (*position == static_cast<char_type>('\n'))) |
|---|
| 424 | { |
|---|
| 425 | return false; |
|---|
| 426 | } |
|---|
| 427 | } |
|---|
| 428 | pstate = pstate->next.p; |
|---|
| 429 | return true; |
|---|
| 430 | } |
|---|
| 431 | } |
|---|
| 432 | else if((m_match_flags & match_not_eol) == 0) |
|---|
| 433 | { |
|---|
| 434 | pstate = pstate->next.p; |
|---|
| 435 | return true; |
|---|
| 436 | } |
|---|
| 437 | return false; |
|---|
| 438 | } |
|---|
| 439 | |
|---|
| 440 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 441 | bool perl_matcher<BidiIterator, Allocator, traits>::match_wild() |
|---|
| 442 | { |
|---|
| 443 | if(position == last) |
|---|
| 444 | return false; |
|---|
| 445 | if(is_separator(*position) && ((match_any_mask & static_cast<const re_dot*>(pstate)->mask) == 0)) |
|---|
| 446 | return false; |
|---|
| 447 | if((*position == char_type(0)) && (m_match_flags & match_not_dot_null)) |
|---|
| 448 | return false; |
|---|
| 449 | pstate = pstate->next.p; |
|---|
| 450 | ++position; |
|---|
| 451 | return true; |
|---|
| 452 | } |
|---|
| 453 | |
|---|
| 454 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 455 | bool perl_matcher<BidiIterator, Allocator, traits>::match_match() |
|---|
| 456 | { |
|---|
| 457 | if((m_match_flags & match_not_null) && (position == (*m_presult)[0].first)) |
|---|
| 458 | return false; |
|---|
| 459 | if((m_match_flags & match_all) && (position != last)) |
|---|
| 460 | return false; |
|---|
| 461 | if((m_match_flags & regex_constants::match_not_initial_null) && (position == search_base)) |
|---|
| 462 | return false; |
|---|
| 463 | m_presult->set_second(position); |
|---|
| 464 | pstate = 0; |
|---|
| 465 | m_has_found_match = true; |
|---|
| 466 | if((m_match_flags & match_posix) == match_posix) |
|---|
| 467 | { |
|---|
| 468 | m_result.maybe_assign(*m_presult); |
|---|
| 469 | if((m_match_flags & match_any) == 0) |
|---|
| 470 | return false; |
|---|
| 471 | } |
|---|
| 472 | #ifdef BOOST_REGEX_MATCH_EXTRA |
|---|
| 473 | if(match_extra & m_match_flags) |
|---|
| 474 | { |
|---|
| 475 | for(unsigned i = 0; i < m_presult->size(); ++i) |
|---|
| 476 | if((*m_presult)[i].matched) |
|---|
| 477 | ((*m_presult)[i]).get_captures().push_back((*m_presult)[i]); |
|---|
| 478 | } |
|---|
| 479 | #endif |
|---|
| 480 | return true; |
|---|
| 481 | } |
|---|
| 482 | |
|---|
| 483 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 484 | bool perl_matcher<BidiIterator, Allocator, traits>::match_word_boundary() |
|---|
| 485 | { |
|---|
| 486 | bool b; // indcates whether next character is a word character |
|---|
| 487 | if(position != last) |
|---|
| 488 | { |
|---|
| 489 | // prev and this character must be opposites: |
|---|
| 490 | #if defined(BOOST_REGEX_USE_C_LOCALE) && defined(__GNUC__) && (__GNUC__ == 2) && (__GNUC_MINOR__ < 95) |
|---|
| 491 | b = traits::isctype(*position, m_word_mask); |
|---|
| 492 | #else |
|---|
| 493 | b = traits_inst.isctype(*position, m_word_mask); |
|---|
| 494 | #endif |
|---|
| 495 | } |
|---|
| 496 | else |
|---|
| 497 | { |
|---|
| 498 | b = (m_match_flags & match_not_eow) ? true : false; |
|---|
| 499 | } |
|---|
| 500 | if((position == backstop) && ((m_match_flags & match_prev_avail) == 0)) |
|---|
| 501 | { |
|---|
| 502 | if(m_match_flags & match_not_bow) |
|---|
| 503 | b ^= true; |
|---|
| 504 | else |
|---|
| 505 | b ^= false; |
|---|
| 506 | } |
|---|
| 507 | else |
|---|
| 508 | { |
|---|
| 509 | --position; |
|---|
| 510 | b ^= traits_inst.isctype(*position, m_word_mask); |
|---|
| 511 | ++position; |
|---|
| 512 | } |
|---|
| 513 | if(b) |
|---|
| 514 | { |
|---|
| 515 | pstate = pstate->next.p; |
|---|
| 516 | return true; |
|---|
| 517 | } |
|---|
| 518 | return false; // no match if we get to here... |
|---|
| 519 | } |
|---|
| 520 | |
|---|
| 521 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 522 | bool perl_matcher<BidiIterator, Allocator, traits>::match_within_word() |
|---|
| 523 | { |
|---|
| 524 | if(position == last) |
|---|
| 525 | return false; |
|---|
| 526 | // both prev and this character must be m_word_mask: |
|---|
| 527 | if(traits_inst.isctype(*position, m_word_mask)) |
|---|
| 528 | { |
|---|
| 529 | bool b; |
|---|
| 530 | if((position == backstop) && ((m_match_flags & match_prev_avail) == 0)) |
|---|
| 531 | return false; |
|---|
| 532 | else |
|---|
| 533 | { |
|---|
| 534 | --position; |
|---|
| 535 | b = traits_inst.isctype(*position, m_word_mask); |
|---|
| 536 | ++position; |
|---|
| 537 | } |
|---|
| 538 | if(b) |
|---|
| 539 | { |
|---|
| 540 | pstate = pstate->next.p; |
|---|
| 541 | return true; |
|---|
| 542 | } |
|---|
| 543 | } |
|---|
| 544 | return false; |
|---|
| 545 | } |
|---|
| 546 | |
|---|
| 547 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 548 | bool perl_matcher<BidiIterator, Allocator, traits>::match_word_start() |
|---|
| 549 | { |
|---|
| 550 | if(position == last) |
|---|
| 551 | return false; // can't be starting a word if we're already at the end of input |
|---|
| 552 | if(!traits_inst.isctype(*position, m_word_mask)) |
|---|
| 553 | return false; // next character isn't a word character |
|---|
| 554 | if((position == backstop) && ((m_match_flags & match_prev_avail) == 0)) |
|---|
| 555 | { |
|---|
| 556 | if(m_match_flags & match_not_bow) |
|---|
| 557 | return false; // no previous input |
|---|
| 558 | } |
|---|
| 559 | else |
|---|
| 560 | { |
|---|
| 561 | // otherwise inside buffer: |
|---|
| 562 | BidiIterator t(position); |
|---|
| 563 | --t; |
|---|
| 564 | if(traits_inst.isctype(*t, m_word_mask)) |
|---|
| 565 | return false; // previous character not non-word |
|---|
| 566 | } |
|---|
| 567 | // OK we have a match: |
|---|
| 568 | pstate = pstate->next.p; |
|---|
| 569 | return true; |
|---|
| 570 | } |
|---|
| 571 | |
|---|
| 572 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 573 | bool perl_matcher<BidiIterator, Allocator, traits>::match_word_end() |
|---|
| 574 | { |
|---|
| 575 | if((position == backstop) && ((m_match_flags & match_prev_avail) == 0)) |
|---|
| 576 | return false; // start of buffer can't be end of word |
|---|
| 577 | BidiIterator t(position); |
|---|
| 578 | --t; |
|---|
| 579 | if(traits_inst.isctype(*t, m_word_mask) == false) |
|---|
| 580 | return false; // previous character wasn't a word character |
|---|
| 581 | |
|---|
| 582 | if(position == last) |
|---|
| 583 | { |
|---|
| 584 | if(m_match_flags & match_not_eow) |
|---|
| 585 | return false; // end of buffer but not end of word |
|---|
| 586 | } |
|---|
| 587 | else |
|---|
| 588 | { |
|---|
| 589 | // otherwise inside buffer: |
|---|
| 590 | if(traits_inst.isctype(*position, m_word_mask)) |
|---|
| 591 | return false; // next character is a word character |
|---|
| 592 | } |
|---|
| 593 | pstate = pstate->next.p; |
|---|
| 594 | return true; // if we fall through to here then we've succeeded |
|---|
| 595 | } |
|---|
| 596 | |
|---|
| 597 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 598 | bool perl_matcher<BidiIterator, Allocator, traits>::match_buffer_start() |
|---|
| 599 | { |
|---|
| 600 | if((position != backstop) || (m_match_flags & match_not_bob)) |
|---|
| 601 | return false; |
|---|
| 602 | // OK match: |
|---|
| 603 | pstate = pstate->next.p; |
|---|
| 604 | return true; |
|---|
| 605 | } |
|---|
| 606 | |
|---|
| 607 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 608 | bool perl_matcher<BidiIterator, Allocator, traits>::match_buffer_end() |
|---|
| 609 | { |
|---|
| 610 | if((position != last) || (m_match_flags & match_not_eob)) |
|---|
| 611 | return false; |
|---|
| 612 | // OK match: |
|---|
| 613 | pstate = pstate->next.p; |
|---|
| 614 | return true; |
|---|
| 615 | } |
|---|
| 616 | |
|---|
| 617 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 618 | bool perl_matcher<BidiIterator, Allocator, traits>::match_backref() |
|---|
| 619 | { |
|---|
| 620 | // |
|---|
| 621 | // Compare with what we previously matched. |
|---|
| 622 | // Note that this succeeds if the backref did not partisipate |
|---|
| 623 | // in the match, this is in line with ECMAScript, but not Perl |
|---|
| 624 | // or PCRE. |
|---|
| 625 | // |
|---|
| 626 | BidiIterator i = (*m_presult)[static_cast<const re_brace*>(pstate)->index].first; |
|---|
| 627 | BidiIterator j = (*m_presult)[static_cast<const re_brace*>(pstate)->index].second; |
|---|
| 628 | while(i != j) |
|---|
| 629 | { |
|---|
| 630 | if((position == last) || (traits_inst.translate(*position, icase) != traits_inst.translate(*i, icase))) |
|---|
| 631 | return false; |
|---|
| 632 | ++i; |
|---|
| 633 | ++position; |
|---|
| 634 | } |
|---|
| 635 | pstate = pstate->next.p; |
|---|
| 636 | return true; |
|---|
| 637 | } |
|---|
| 638 | |
|---|
| 639 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 640 | bool perl_matcher<BidiIterator, Allocator, traits>::match_long_set() |
|---|
| 641 | { |
|---|
| 642 | typedef typename traits::char_class_type char_class_type; |
|---|
| 643 | // let the traits class do the work: |
|---|
| 644 | if(position == last) |
|---|
| 645 | return false; |
|---|
| 646 | BidiIterator t = re_is_set_member(position, last, static_cast<const re_set_long<char_class_type>*>(pstate), re.get_data(), icase); |
|---|
| 647 | if(t != position) |
|---|
| 648 | { |
|---|
| 649 | pstate = pstate->next.p; |
|---|
| 650 | position = t; |
|---|
| 651 | return true; |
|---|
| 652 | } |
|---|
| 653 | return false; |
|---|
| 654 | } |
|---|
| 655 | |
|---|
| 656 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 657 | bool perl_matcher<BidiIterator, Allocator, traits>::match_set() |
|---|
| 658 | { |
|---|
| 659 | if(position == last) |
|---|
| 660 | return false; |
|---|
| 661 | if(static_cast<const re_set*>(pstate)->_map[static_cast<unsigned char>(traits_inst.translate(*position, icase))]) |
|---|
| 662 | { |
|---|
| 663 | pstate = pstate->next.p; |
|---|
| 664 | ++position; |
|---|
| 665 | return true; |
|---|
| 666 | } |
|---|
| 667 | return false; |
|---|
| 668 | } |
|---|
| 669 | |
|---|
| 670 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 671 | bool perl_matcher<BidiIterator, Allocator, traits>::match_jump() |
|---|
| 672 | { |
|---|
| 673 | pstate = static_cast<const re_jump*>(pstate)->alt.p; |
|---|
| 674 | return true; |
|---|
| 675 | } |
|---|
| 676 | |
|---|
| 677 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 678 | bool perl_matcher<BidiIterator, Allocator, traits>::match_combining() |
|---|
| 679 | { |
|---|
| 680 | if(position == last) |
|---|
| 681 | return false; |
|---|
| 682 | if(is_combining(traits_inst.translate(*position, icase))) |
|---|
| 683 | return false; |
|---|
| 684 | ++position; |
|---|
| 685 | while((position != last) && is_combining(traits_inst.translate(*position, icase))) |
|---|
| 686 | ++position; |
|---|
| 687 | pstate = pstate->next.p; |
|---|
| 688 | return true; |
|---|
| 689 | } |
|---|
| 690 | |
|---|
| 691 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 692 | bool perl_matcher<BidiIterator, Allocator, traits>::match_soft_buffer_end() |
|---|
| 693 | { |
|---|
| 694 | if(m_match_flags & match_not_eob) |
|---|
| 695 | return false; |
|---|
| 696 | BidiIterator p(position); |
|---|
| 697 | while((p != last) && is_separator(traits_inst.translate(*p, icase)))++p; |
|---|
| 698 | if(p != last) |
|---|
| 699 | return false; |
|---|
| 700 | pstate = pstate->next.p; |
|---|
| 701 | return true; |
|---|
| 702 | } |
|---|
| 703 | |
|---|
| 704 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 705 | bool perl_matcher<BidiIterator, Allocator, traits>::match_restart_continue() |
|---|
| 706 | { |
|---|
| 707 | if(position == search_base) |
|---|
| 708 | { |
|---|
| 709 | pstate = pstate->next.p; |
|---|
| 710 | return true; |
|---|
| 711 | } |
|---|
| 712 | return false; |
|---|
| 713 | } |
|---|
| 714 | |
|---|
| 715 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 716 | bool perl_matcher<BidiIterator, Allocator, traits>::match_backstep() |
|---|
| 717 | { |
|---|
| 718 | #ifdef BOOST_MSVC |
|---|
| 719 | #pragma warning(push) |
|---|
| 720 | #pragma warning(disable:4127) |
|---|
| 721 | #endif |
|---|
| 722 | if( ::boost::is_random_access_iterator<BidiIterator>::value) |
|---|
| 723 | { |
|---|
| 724 | std::ptrdiff_t maxlen = ::boost::re_detail::distance(backstop, position); |
|---|
| 725 | if(maxlen < static_cast<const re_brace*>(pstate)->index) |
|---|
| 726 | return false; |
|---|
| 727 | std::advance(position, -static_cast<const re_brace*>(pstate)->index); |
|---|
| 728 | } |
|---|
| 729 | else |
|---|
| 730 | { |
|---|
| 731 | int c = static_cast<const re_brace*>(pstate)->index; |
|---|
| 732 | while(c--) |
|---|
| 733 | { |
|---|
| 734 | if(position == backstop) |
|---|
| 735 | return false; |
|---|
| 736 | --position; |
|---|
| 737 | } |
|---|
| 738 | } |
|---|
| 739 | pstate = pstate->next.p; |
|---|
| 740 | return true; |
|---|
| 741 | #ifdef BOOST_MSVC |
|---|
| 742 | #pragma warning(pop) |
|---|
| 743 | #endif |
|---|
| 744 | } |
|---|
| 745 | |
|---|
| 746 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 747 | inline bool perl_matcher<BidiIterator, Allocator, traits>::match_assert_backref() |
|---|
| 748 | { |
|---|
| 749 | // return true if marked sub-expression N has been matched: |
|---|
| 750 | bool result = (*m_presult)[static_cast<const re_brace*>(pstate)->index].matched; |
|---|
| 751 | pstate = pstate->next.p; |
|---|
| 752 | return result; |
|---|
| 753 | } |
|---|
| 754 | |
|---|
| 755 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 756 | bool perl_matcher<BidiIterator, Allocator, traits>::match_toggle_case() |
|---|
| 757 | { |
|---|
| 758 | // change our case sensitivity: |
|---|
| 759 | this->icase = static_cast<const re_case*>(pstate)->icase; |
|---|
| 760 | pstate = pstate->next.p; |
|---|
| 761 | return true; |
|---|
| 762 | } |
|---|
| 763 | |
|---|
| 764 | |
|---|
| 765 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 766 | bool perl_matcher<BidiIterator, Allocator, traits>::find_restart_any() |
|---|
| 767 | { |
|---|
| 768 | #ifdef BOOST_MSVC |
|---|
| 769 | #pragma warning(push) |
|---|
| 770 | #pragma warning(disable:4127) |
|---|
| 771 | #endif |
|---|
| 772 | const unsigned char* _map = re.get_map(); |
|---|
| 773 | while(true) |
|---|
| 774 | { |
|---|
| 775 | // skip everything we can't match: |
|---|
| 776 | while((position != last) && !can_start(*position, _map, (unsigned char)mask_any) ) |
|---|
| 777 | ++position; |
|---|
| 778 | if(position == last) |
|---|
| 779 | { |
|---|
| 780 | // run out of characters, try a null match if possible: |
|---|
| 781 | if(re.can_be_null()) |
|---|
| 782 | return match_prefix(); |
|---|
| 783 | break; |
|---|
| 784 | } |
|---|
| 785 | // now try and obtain a match: |
|---|
| 786 | if(match_prefix()) |
|---|
| 787 | return true; |
|---|
| 788 | if(position == last) |
|---|
| 789 | return false; |
|---|
| 790 | ++position; |
|---|
| 791 | } |
|---|
| 792 | return false; |
|---|
| 793 | #ifdef BOOST_MSVC |
|---|
| 794 | #pragma warning(pop) |
|---|
| 795 | #endif |
|---|
| 796 | } |
|---|
| 797 | |
|---|
| 798 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 799 | bool perl_matcher<BidiIterator, Allocator, traits>::find_restart_word() |
|---|
| 800 | { |
|---|
| 801 | #ifdef BOOST_MSVC |
|---|
| 802 | #pragma warning(push) |
|---|
| 803 | #pragma warning(disable:4127) |
|---|
| 804 | #endif |
|---|
| 805 | // do search optimised for word starts: |
|---|
| 806 | const unsigned char* _map = re.get_map(); |
|---|
| 807 | if((m_match_flags & match_prev_avail) || (position != base)) |
|---|
| 808 | --position; |
|---|
| 809 | else if(match_prefix()) |
|---|
| 810 | return true; |
|---|
| 811 | do |
|---|
| 812 | { |
|---|
| 813 | while((position != last) && traits_inst.isctype(*position, m_word_mask)) |
|---|
| 814 | ++position; |
|---|
| 815 | while((position != last) && !traits_inst.isctype(*position, m_word_mask)) |
|---|
| 816 | ++position; |
|---|
| 817 | if(position == last) |
|---|
| 818 | break; |
|---|
| 819 | |
|---|
| 820 | if(can_start(*position, _map, (unsigned char)mask_any) ) |
|---|
| 821 | { |
|---|
| 822 | if(match_prefix()) |
|---|
| 823 | return true; |
|---|
| 824 | } |
|---|
| 825 | if(position == last) |
|---|
| 826 | break; |
|---|
| 827 | } while(true); |
|---|
| 828 | return false; |
|---|
| 829 | #ifdef BOOST_MSVC |
|---|
| 830 | #pragma warning(pop) |
|---|
| 831 | #endif |
|---|
| 832 | } |
|---|
| 833 | |
|---|
| 834 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 835 | bool perl_matcher<BidiIterator, Allocator, traits>::find_restart_line() |
|---|
| 836 | { |
|---|
| 837 | // do search optimised for line starts: |
|---|
| 838 | const unsigned char* _map = re.get_map(); |
|---|
| 839 | if(match_prefix()) |
|---|
| 840 | return true; |
|---|
| 841 | while(position != last) |
|---|
| 842 | { |
|---|
| 843 | while((position != last) && !is_separator(*position)) |
|---|
| 844 | ++position; |
|---|
| 845 | if(position == last) |
|---|
| 846 | return false; |
|---|
| 847 | ++position; |
|---|
| 848 | if(position == last) |
|---|
| 849 | { |
|---|
| 850 | if(re.can_be_null() && match_prefix()) |
|---|
| 851 | return true; |
|---|
| 852 | return false; |
|---|
| 853 | } |
|---|
| 854 | |
|---|
| 855 | if( can_start(*position, _map, (unsigned char)mask_any) ) |
|---|
| 856 | { |
|---|
| 857 | if(match_prefix()) |
|---|
| 858 | return true; |
|---|
| 859 | } |
|---|
| 860 | if(position == last) |
|---|
| 861 | return false; |
|---|
| 862 | //++position; |
|---|
| 863 | } |
|---|
| 864 | return false; |
|---|
| 865 | } |
|---|
| 866 | |
|---|
| 867 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 868 | bool perl_matcher<BidiIterator, Allocator, traits>::find_restart_buf() |
|---|
| 869 | { |
|---|
| 870 | if((position == base) && ((m_match_flags & match_not_bob) == 0)) |
|---|
| 871 | return match_prefix(); |
|---|
| 872 | return false; |
|---|
| 873 | } |
|---|
| 874 | |
|---|
| 875 | template <class BidiIterator, class Allocator, class traits> |
|---|
| 876 | bool perl_matcher<BidiIterator, Allocator, traits>::find_restart_lit() |
|---|
| 877 | { |
|---|
| 878 | #if 0 |
|---|
| 879 | if(position == last) |
|---|
| 880 | return false; // can't possibly match if we're at the end already |
|---|
| 881 | |
|---|
| 882 | unsigned type = (m_match_flags & match_continuous) ? |
|---|
| 883 | static_cast<unsigned int>(regbase::restart_continue) |
|---|
| 884 | : static_cast<unsigned int>(re.get_restart_type()); |
|---|
| 885 | |
|---|
| 886 | const kmp_info<char_type>* info = access::get_kmp(re); |
|---|
| 887 | int len = info->len; |
|---|
| 888 | const char_type* x = info->pstr; |
|---|
| 889 | int j = 0; |
|---|
| 890 | while (position != last) |
|---|
| 891 | { |
|---|
| 892 | while((j > -1) && (x[j] != traits_inst.translate(*position, icase))) |
|---|
| 893 | j = info->kmp_next[j]; |
|---|
| 894 | ++position; |
|---|
| 895 | ++j; |
|---|
| 896 | if(j >= len) |
|---|
| 897 | { |
|---|
| 898 | if(type == regbase::restart_fixed_lit) |
|---|
| 899 | { |
|---|
| 900 | std::advance(position, -j); |
|---|
| 901 | restart = position; |
|---|
| 902 | std::advance(restart, len); |
|---|
| 903 | m_result.set_first(position); |
|---|
| 904 | m_result.set_second(restart); |
|---|
| 905 | position = restart; |
|---|
| 906 | return true; |
|---|
| 907 | } |
|---|
| 908 | else |
|---|
| 909 | { |
|---|
| 910 | restart = position; |
|---|
| 911 | std::advance(position, -j); |
|---|
| 912 | if(match_prefix()) |
|---|
| 913 | return true; |
|---|
| 914 | else |
|---|
| 915 | { |
|---|
| 916 | for(int k = 0; (restart != position) && (k < j); ++k, --restart) |
|---|
| 917 | {} // dwa 10/20/2000 - warning suppression for MWCW |
|---|
| 918 | if(restart != last) |
|---|
| 919 | ++restart; |
|---|
| 920 | position = restart; |
|---|
| 921 | j = 0; //we could do better than this... |
|---|
| 922 | } |
|---|
| 923 | } |
|---|
| 924 | } |
|---|
| 925 | } |
|---|
| 926 | if((m_match_flags & match_partial) && (position == last) && j) |
|---|
| 927 | { |
|---|
| 928 | // we need to check for a partial match: |
|---|
| 929 | restart = position; |
|---|
| 930 | std::advance(position, -j); |
|---|
| 931 | return match_prefix(); |
|---|
| 932 | } |
|---|
| 933 | #endif |
|---|
| 934 | return false; |
|---|
| 935 | } |
|---|
| 936 | |
|---|
| 937 | } // namespace re_detail |
|---|
| 938 | |
|---|
| 939 | } // namespace boost |
|---|
| 940 | |
|---|
| 941 | #ifdef __BORLANDC__ |
|---|
| 942 | # pragma option pop |
|---|
| 943 | #endif |
|---|
| 944 | #ifdef BOOST_HAS_ABI_HEADERS |
|---|
| 945 | # include BOOST_ABI_SUFFIX |
|---|
| 946 | #endif |
|---|
| 947 | |
|---|
| 948 | #endif |
|---|
| 949 | |
|---|