diff options
Diffstat (limited to 'internal/engine')
| -rw-r--r-- | internal/engine/engine_test.go | 2 | ||||
| -rw-r--r-- | internal/engine/facts.go | 19 |
2 files changed, 21 insertions, 0 deletions
diff --git a/internal/engine/engine_test.go b/internal/engine/engine_test.go index 89ea6f0..85679dc 100644 --- a/internal/engine/engine_test.go +++ b/internal/engine/engine_test.go @@ -12,6 +12,7 @@ import ( "git.labunix.xyz/krino/internal/cond" "git.labunix.xyz/krino/internal/config" + "git.labunix.xyz/krino/internal/norm" ) // sandbox gives a test its own HOME with no XDG overrides and returns it. @@ -55,6 +56,7 @@ func (f fakeFacts) Now() time.Time { return func (f fakeFacts) ContentContains(cond.Options, []string) (int, error) { return -1, nil } func (f fakeFacts) Duplicate([]string) (string, bool, error) { return "", false, nil } func (f fakeFacts) Matched() (bool, bool) { return false, false } +func (f fakeFacts) Folded(subj string) norm.Folded { return norm.FoldMapped(subj) } var _ cond.Facts = fakeFacts{} diff --git a/internal/engine/facts.go b/internal/engine/facts.go index 6540fdd..d20a34d 100644 --- a/internal/engine/facts.go +++ b/internal/engine/facts.go @@ -179,6 +179,10 @@ type facts struct { // absolute, for a front end that offers to act on the other copy. dupOriginal string + // folded memoises the folded form of this file's name and path, which + // every name test of every rule would otherwise recompute. + folded map[string]norm.Folded + contentDone bool // extraction was attempted contentErr error // why it failed partialErr error // extract.ErrPartial: a keyword not found may be in the unread part @@ -192,6 +196,21 @@ func newFacts(run *matchRun, file scan.File) *facts { return &facts{run: run, file: file} } +// Folded is the file's name or path folded for comparison, worked out once +// however many name tests ask for it. facts belongs to one file and one +// goroutine, so no lock is needed. +func (f *facts) Folded(subj string) norm.Folded { + if v, ok := f.folded[subj]; ok { + return v + } + v := norm.FoldMapped(subj) + if f.folded == nil { + f.folded = make(map[string]norm.Folded, 2) + } + f.folded[subj] = v + return v +} + func (f *facts) Name() string { return f.file.Name } func (f *facts) Rel() string { return f.file.Rel } func (f *facts) Size() int64 { return f.file.Size } |
