From: Ulya Trofimovich Date: Fri, 28 Aug 2015 22:15:40 +0000 (+0100) Subject: No need to NULL-terminate array of known size. X-Git-Tag: 0.15~86 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b5441f223475e842166d861539c8fca39bfb9991;p=re2c No need to NULL-terminate array of known size. For some reason (which I yet do not understand) re2c uses size of DFA state's "kernel" to determine whether a new state should be constructed or some already constructed state (of the same "kernel" size) will do. Since re2c knows "kernel" size for each state anyway, there's no need to NULL-terminate "kernel" array while iterating over it: we can use pre-calculated size instead. --- diff --git a/re2c/src/ir/dfa/dfa.cc b/re2c/src/ir/dfa/dfa.cc index eeccf157..91b53333 100644 --- a/re2c/src/ir/dfa/dfa.cc +++ b/re2c/src/ir/dfa/dfa.cc @@ -64,8 +64,9 @@ DFA::DFA(Ins *ins, uint32_t ni, uint32_t lb, uint32_t ub, const Char *rep) s->rule = NULL; - for (Ins ** iP = s->kernel, * i; (i = *iP); ++iP) + for (uint32_t k = 0; k < s->kCount; ++k) { + Ins * i = s->kernel[k]; if (i->i.tag == CHAR) { for (Ins *j = i + 1; j < (Ins*) i->i.link; ++j) @@ -152,33 +153,29 @@ void DFA::addState(State **a, State *s) State *DFA::findState(Ins **kernel, Ins ** kernel_end) { - Ins ** cP = kernel; - - for (Ins ** iP = kernel; iP < kernel_end; ++iP) + uint32_t kCount = 0; + for (Ins ** i = kernel; i < kernel_end; ++i) { - Ins * i = *iP; - if (i->i.tag == CHAR || i->i.tag == TERM || i->i.tag == CTXT) + Ins * ins = *i; + if (ins->i.tag == CHAR || ins->i.tag == TERM || ins->i.tag == CTXT) { - *cP++ = i; + kernel[kCount++] = ins; } else { - unmark(i); + unmark (ins); } } - const ptrdiff_t kCount = cP - kernel; - kernel[kCount] = NULL; - State * s; for (s = head; s; s = s->next) { if (s->kCount == kCount) { bool marked = true; - for (Ins ** iP = s->kernel, * i; marked && (i = *iP); ++iP) + for (uint32_t i = 0; marked && i < s->kCount; ++i) { - marked = isMarked (i); + marked = isMarked (s->kernel[i]); } if (marked) { @@ -192,15 +189,15 @@ State *DFA::findState(Ins **kernel, Ins ** kernel_end) s = new State; addState(tail, s); s->kCount = kCount; - s->kernel = new Ins * [kCount + 1]; - memcpy(s->kernel, kernel, (kCount + 1)*sizeof(Ins*)); + s->kernel = new Ins * [kCount]; + memcpy(s->kernel, kernel, kCount * sizeof (Ins *)); s->link = toDo; toDo = s; } - for (Ins ** iP = kernel, * i; (i = *iP); ++iP) + for (uint32_t i = 0; i < kCount; ++i) { - unmark(i); + unmark (kernel[i]); } return s; diff --git a/re2c/src/ir/dfa/state.h b/re2c/src/ir/dfa/state.h index 6171223e..41af7dfd 100644 --- a/re2c/src/ir/dfa/state.h +++ b/re2c/src/ir/dfa/state.h @@ -17,7 +17,7 @@ public: State * next; State * link; uint32_t depth; // for finding SCCs - ptrdiff_t kCount; + uint32_t kCount; Ins ** kernel; bool isPreCtxt;