From 1c9d6f06eb4b4ea4dae440d270bbe6609e5d3200 Mon Sep 17 00:00:00 2001 From: ziyitony Date: Thu, 16 Apr 2020 19:46:06 +0900 Subject: [PATCH] update Validate() function to make it easy to understand --- trie_tree.go | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/trie_tree.go b/trie_tree.go index d17ea66..ccc0ff7 100644 --- a/trie_tree.go +++ b/trie_tree.go @@ -145,29 +145,24 @@ func (tree *Trie) Validate(text string) (bool, string) { Empty = "" ) var ( - parent = tree.Root - current *Node - runes = []rune(text) - length = len(runes) - left = 0 - found bool + runes = []rune(text) ) - for position := 0; position < len(runes); position++ { - current, found = parent.Children[runes[position]] + for left := 0; left < len(runes); left++ { + parent := tree.Root + for position := left; position < len(runes); position++ { + current, found := parent.Children[runes[position]] - if !found || (!current.IsPathEnd() && position == length-1) { - parent = tree.Root - position = left - left++ - continue - } + if !found { + break + } - if current.IsPathEnd() && left <= position { - return false, string(runes[left : position+1]) - } + if current.IsPathEnd() { + return false, string(runes[left : position+1]) + } - parent = current + parent = current + } } return true, Empty