summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNorihiro Tanaka <noritnk@kcn.ne.jp>2014-10-05 20:03:25 -0700
committerJim Meyering <meyering@fb.com>2014-10-05 20:03:25 -0700
commit01d9f718410b583ab17d5e0e14d1f9e31fd878ba (patch)
tree4c805d42c910185e70e2a09d64516156c2bd380a
parent00f3b29027df6af21db4b3989e25c67d982cc03d (diff)
downloadgrep-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.c73
1 files changed, 32 insertions, 41 deletions
diff --git a/src/dfa.c b/src/dfa.c
index 7cbe247c..2e619826 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -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;