From 703255e5f05fb68378a824e3871db56e2167014f Mon Sep 17 00:00:00 2001 From: gzatravkin <53317567+gzatravkin@users.noreply.github.com> Date: Sat, 20 Jul 2024 10:25:29 -0300 Subject: [PATCH 1/2] fix garbage --- AhoCorasick/AhoCorasick.cs | 2 +- AhoCorasick/Trie.cs | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/AhoCorasick/AhoCorasick.cs b/AhoCorasick/AhoCorasick.cs index dbe6fa2..53a6653 100644 --- a/AhoCorasick/AhoCorasick.cs +++ b/AhoCorasick/AhoCorasick.cs @@ -132,7 +132,7 @@ public void BuildFail(Trie node = null) var word = node.Word; for (int i = 1; i < word.Length && node.Fail == null; i++) - node.Fail = Trie.ExploreFailLink(word.Substring(i)); + node.Fail = Trie.ExploreFailLink(word, i, word.Length); foreach (var subNode in node.Next.Values) BuildFail(subNode); diff --git a/AhoCorasick/Trie.cs b/AhoCorasick/Trie.cs index b152296..1d7fc64 100644 --- a/AhoCorasick/Trie.cs +++ b/AhoCorasick/Trie.cs @@ -91,14 +91,16 @@ public virtual Trie Add(string word) /// /// The suffix. /// The failure node or null. - public virtual Trie ExploreFailLink(string word) + public virtual Trie ExploreFailLink(string word, int startIndex, int endIndex) { var node = this; - foreach (var c in word) + for (int i = startIndex; i < endIndex; i++) { - node.Next.TryGetValue(c, out node); - if (node == null) return null; + if (!node.Next.TryGetValue(word[i], out node)) + { + return null; + } } return node; From afb9977d4f31fbd64f87455c4d8c96e349026bc4 Mon Sep 17 00:00:00 2001 From: gzatravkin <53317567+gzatravkin@users.noreply.github.com> Date: Sat, 20 Jul 2024 10:31:06 -0300 Subject: [PATCH 2/2] update comment --- AhoCorasick/Trie.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/AhoCorasick/Trie.cs b/AhoCorasick/Trie.cs index 1d7fc64..710550e 100644 --- a/AhoCorasick/Trie.cs +++ b/AhoCorasick/Trie.cs @@ -87,10 +87,13 @@ public virtual Trie Add(string word) } /// - /// Finds the failure node for a specified suffix. + /// Finds the failure node for a specified suffix within the given range of indices. /// - /// The suffix. - /// The failure node or null. + /// The string containing the suffix. + /// The start index of the suffix within the string. + /// The end index (exclusive) of the suffix within the string. + /// The failure node or null if no failure node is found. + public virtual Trie ExploreFailLink(string word, int startIndex, int endIndex) { var node = this;