Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion ZACLib/ZACLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,23 @@ namespace ZACLib {

std::vector<Search::Match> Search::Do(const ZAC_SV& input) const {
std::vector<Match> result;
if (trie.empty()) return result;

int state = 0;
for (size_t i = 0; i < input.size(); ++i) {
const auto c = static_cast<unsigned char>(input[i]);
state = trie[state].next[c];

// Allow matching even when Build() has not been called yet.
// In that case transitions can still be -1, so we fall back to
// the fail chain and finally the root instead of indexing trie[-1].
while (state != 0 && trie[state].next[c] == -1) {
state = trie[state].fail;
}
if (trie[state].next[c] != -1) {
state = trie[state].next[c];
} else {
state = 0;
}
Comment on lines +230 to +237
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic to handle state transitions is correct and successfully hardens the function against crashes. However, the implementation can be made more concise. The current while loop followed by an if-else block can be simplified into a more standard pattern for Aho-Corasick state transitions. This improves readability and maintainability.

            while (trie[state].next[c] == -1 && state != 0) {
                state = trie[state].fail;
            }
            state = trie[state].next[c];
            if (state == -1) {
                state = 0;
            }


if (trie[state].output_id != Node::kInvalidOutput) {
result.push_back(
Expand Down