]> git.ipfire.org Git - thirdparty/vectorscan.git/commitdiff
getMatches: simplify
authorJustin Viiret <justin.viiret@intel.com>
Wed, 8 Mar 2017 05:27:41 +0000 (16:27 +1100)
committerMatthew Barr <matthew.barr@intel.com>
Wed, 26 Apr 2017 05:17:03 +0000 (15:17 +1000)
util/ng_find_matches.cpp

index 0bc0cc93231288ad1c52298d0583a2c6bc94fad0..13efa05c71a3974bedb2df004b790e5e47d42eb4 100644 (file)
@@ -868,59 +868,61 @@ bool canReach(const NGHolder &g, const NFAEdge &e, struct fmstate &state) {
 }
 
 static
-void getMatches(const NGHolder &g, MatchSet &matches, struct fmstate &state,
-                bool allowEodMatches) {
-    flat_set<NFAVertex> accepts {g.accept, g.acceptEod};
-
-    for (auto v : accepts) {
-        bool eod = v == g.acceptEod;
-        if (eod && !allowEodMatches) {
-            continue;
-        }
+void getAcceptMatches(const NGHolder &g, MatchSet &matches,
+                      struct fmstate &state, NFAVertex accept_vertex) {
+    assert(accept_vertex == g.accept || accept_vertex == g.acceptEod);
 
-        auto active_states = eod ? state.states.getAcceptEodStates(state.gc) :
-                                   state.states.getAcceptStates(state.gc);
+    const bool eod = accept_vertex == g.acceptEod;
+    auto active_states = eod ? state.states.getAcceptEodStates(state.gc)
+                             : state.states.getAcceptStates(state.gc);
 
-        DEBUG_PRINTF("Number of active states: %zu\n", active_states.size());
+    DEBUG_PRINTF("Number of active states: %zu\n", active_states.size());
 
-        for (const auto &cur : active_states) {
-            auto u = state.vertices[cur.idx];
+    for (const auto &cur : active_states) {
+        auto u = state.vertices[cur.idx];
 
-            // we can't accept anything from startDs in between UTF-8 codepoints
-            if (state.utf8 && u == g.startDs && !isUtf8CodePoint(state.cur)) {
-                continue;
-            }
+        // we can't accept anything from startDs in between UTF-8 codepoints
+        if (state.utf8 && u == g.startDs && !isUtf8CodePoint(state.cur)) {
+            continue;
+        }
 
-            const auto &reports =
-                    eod ?
-                        state.gc.vertex_eod_reports_by_level[cur.level][u] :
-                    state.gc.vertex_reports_by_level[cur.level][u];
+        const auto &reports =
+            eod ? state.gc.vertex_eod_reports_by_level[cur.level][u]
+                : state.gc.vertex_reports_by_level[cur.level][u];
 
-            NFAEdge e = edge(u, v, g);
+        NFAEdge e = edge(u, accept_vertex, g);
 
-            // we assume edge assertions only exist at level 0
-            if (e && !canReach(g, e, state)) {
-                continue;
-            }
+        // we assume edge assertions only exist at level 0
+        if (e && !canReach(g, e, state)) {
+            continue;
+        }
 
-            DEBUG_PRINTF("%smatch found at %zu\n",
-                         eod ? "eod " : "", state.offset);
+        DEBUG_PRINTF("%smatch found at %zu\n", eod ? "eod " : "", state.offset);
 
-            assert(!reports.empty());
-            for (const auto &report_id : reports) {
-                const Report &ri = state.rm.getReport(report_id);
+        assert(!reports.empty());
+        for (const auto &report_id : reports) {
+            const Report &ri = state.rm.getReport(report_id);
 
-                DEBUG_PRINTF("report %u has offset adjustment %d\n",
-                             report_id, ri.offsetAdjust);
-                DEBUG_PRINTF("match from (i:%zu,l:%u,t:%u): (%zu,%zu)\n",
-                             cur.idx, cur.level, cur.type, cur.som,
-                             state.offset + ri.offsetAdjust);
-                matches.emplace(cur.som, state.offset + ri.offsetAdjust);
-            }
+            DEBUG_PRINTF("report %u has offset adjustment %d\n", report_id,
+                         ri.offsetAdjust);
+            DEBUG_PRINTF("match from (i:%zu,l:%u,t:%u): (%zu,%zu)\n", cur.idx,
+                         cur.level, cur.type, cur.som,
+                         state.offset + ri.offsetAdjust);
+            matches.emplace(cur.som, state.offset + ri.offsetAdjust);
         }
     }
 }
 
+
+static
+void getMatches(const NGHolder &g, MatchSet &matches, struct fmstate &state,
+                bool allowEodMatches) {
+    getAcceptMatches(g, matches, state, g.accept);
+    if (allowEodMatches) {
+        getAcceptMatches(g, matches, state, g.acceptEod);
+    }
+}
+
 static
 void step(const NGHolder &g, fmstate &state, StateSet::WorkingData &wd) {
     state.next.reset();