diff options
author | Norihiro Tanaka <noritnk@kcn.ne.jp> | 2014-10-05 20:03:25 -0700 |
---|---|---|
committer | Jim Meyering <meyering@fb.com> | 2014-10-05 20:03:25 -0700 |
commit | 01d9f718410b583ab17d5e0e14d1f9e31fd878ba (patch) | |
tree | 4c805d42c910185e70e2a09d64516156c2bd380a | |
parent | 00f3b29027df6af21db4b3989e25c67d982cc03d (diff) | |
download | grep-01d9f718410b583ab17d5e0e14d1f9e31fd878ba.tar.gz |
dfa: factor out a new nontrivial block of duplicated code
* src/dfa.c (State_transition): New macro.
(dfaexec_main): Use it twice.
-rw-r--r-- | src/dfa.c | 73 |
1 files changed, 32 insertions, 41 deletions
@@ -3348,26 +3348,38 @@ dfaexec_main (struct dfa *d, char const *begin, char *end, goto done; } - /* Can match with a multibyte character (and multi character - collating element). Transition table might be updated. */ - s = transit_state (d, s, &p, (unsigned char *) end); - - if (p[-1] == eol) - { - if ((char *) p > end) - { - p = NULL; - goto done; - } - - nlcount++; - - if (!allow_nl) - s = 0; - } + /* The following code is used twice. + Use a macro to avoid the risk that they diverge. */ +#define State_transition() \ + do { \ + /* Can match with a multibyte character (and multi-character \ + collating element). Transition table might be updated. */ \ + s = transit_state (d, s, &p, (unsigned char *) end); \ + \ + /* If previous character is newline after a transition \ + for ANYCHAR or MBCSET in non-UTF8 multibyte locales, \ + check whether current position is beyond the end of \ + the input buffer. Also, transit to initial state if \ + !ALLOW_NL, even if RE_DOT_NEWLINE is set. */ \ + if (p[-1] == eol) \ + { \ + if ((char *) p > end) \ + { \ + p = NULL; \ + goto done; \ + } \ + \ + nlcount++; \ + \ + if (!allow_nl) \ + s = 0; \ + } \ + \ + mbp = p; \ + trans = d->trans; \ + } while (0) - mbp = p; - trans = d->trans; + State_transition(); } } else @@ -3410,28 +3422,7 @@ dfaexec_main (struct dfa *d, char const *begin, char *end, s1 = s; if (multibyte) - { - /* Can match with a multibyte character (and multicharacter - collating element). Transition table might be updated. */ - s = transit_state (d, s, &p, (unsigned char *) end); - - if (p[-1] == eol) - { - if ((char *) p > end) - { - p = NULL; - goto done; - } - - nlcount++; - - if (!allow_nl) - s = 0; - } - - mbp = p; - trans = d->trans; - } + State_transition(); else s = d->fails[s][*p++]; continue; |