From 9792ef33d2e109b57eb58f851405ec84107cdc48 Mon Sep 17 00:00:00 2001 From: Samuel Hawksby-Robinson Date: Wed, 6 Jan 2021 16:59:11 +0000 Subject: [PATCH] Made all compiled regex a field of TodoFinder This prevents the application from needing to constantly recompiling the same regex patterns over and over --- main.go | 6 +++++- todo_finder.go | 46 +++++++++++++++++++++++++++------------------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/main.go b/main.go index db80cf8..00e6d0c 100644 --- a/main.go +++ b/main.go @@ -15,8 +15,12 @@ const ( func main() { tf := NewTodoFinder() + err := tf.Init() + if err != nil { + panic(err) + } - err := tf.FindInDir(statusDir) + err = tf.FindInDir(statusDir) if err != nil { panic(err) } diff --git a/todo_finder.go b/todo_finder.go index 2a429e0..d7ad31f 100644 --- a/todo_finder.go +++ b/todo_finder.go @@ -16,6 +16,10 @@ type node struct { } type TodoFinder struct { + entityTracker *entityTracker + todoRegex *regexp.Regexp + lineRegex *regexp.Regexp + FoundTable []*todo foundTree *node @@ -33,27 +37,31 @@ func NewTodoFinder() TodoFinder { } } +func (tf *TodoFinder) Init() (err error) { + tf.todoRegex, err = regexp.Compile(tf.buildRegexPattern()) + if err != nil { + return err + } + + tf.lineRegex, err = regexp.Compile("\n") + if err != nil { + return err + } + + tf.entityTracker, err = NewEntityTracker() + if err != nil { + return err + } + + return nil +} + func (tf *TodoFinder) AddTodo(t *todo) { tf.FoundTable = append(tf.FoundTable, t) tf.foundTree.AddToTree(t.Path(), t) } func (tf *TodoFinder) FindInDir(dir string) error { - r, err := regexp.Compile(tf.buildRegexPattern()) - if err != nil { - return err - } - - lineSplitter, err := regexp.Compile("\n") - if err != nil { - return err - } - - et, err := NewEntityTracker() - if err != nil { - return err - } - files, err := ioutil.ReadDir(dir) if err != nil { return err @@ -82,11 +90,11 @@ func (tf *TodoFinder) FindInDir(dir string) error { return err } - lines := lineSplitter.Split(string(file), -1) + lines := tf.lineRegex.Split(string(file), -1) for i, l := range lines { - et.Track(l) + tf.entityTracker.Track(l) - results := r.FindSubmatch([]byte(l)) + results := tf.todoRegex.FindSubmatch([]byte(l)) if results == nil { if len(l) < 3 { tf.openTodo = nil @@ -107,7 +115,7 @@ func (tf *TodoFinder) FindInDir(dir string) error { Filepath: filepath, Description: string(results[1]), LineNumber: i + 1, - RelatedFuncOrType: et.Current(), + RelatedFuncOrType: tf.entityTracker.Current(), } tf.AddTodo(td) tf.openTodo = td