From ca4bdfb4bd61e1cb9991146ac5b6bafbf5391072 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sun, 11 Jan 2015 01:47:46 +0900 Subject: [PATCH] Fix Transform result cache to speed up subsequent searches --- src/item.go | 2 +- src/pattern.go | 22 ++++++++++++---------- src/pattern_test.go | 14 ++++++++++---- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/item.go b/src/item.go index 60355b4..9f90b8d 100644 --- a/src/item.go +++ b/src/item.go @@ -10,9 +10,9 @@ type Offset [2]int32 type Item struct { text *string origText *string + transformed *Transformed offsets []Offset rank Rank - transformed *Transformed } type Rank struct { diff --git a/src/pattern.go b/src/pattern.go index 93dbaf9..a936411 100644 --- a/src/pattern.go +++ b/src/pattern.go @@ -229,16 +229,22 @@ func (p *Pattern) Match(chunk *Chunk) []*Item { return matches } +func dupItem(item *Item, offsets []Offset) *Item { + return &Item{ + text: item.text, + origText: item.origText, + transformed: item.transformed, + offsets: offsets, + rank: Rank{0, 0, item.rank.index}} +} + func (p *Pattern) fuzzyMatch(chunk *Chunk) []*Item { matches := []*Item{} for _, item := range *chunk { input := p.prepareInput(item) if sidx, eidx := p.iter(FuzzyMatch, input, p.text); sidx >= 0 { - matches = append(matches, &Item{ - text: item.text, - origText: item.origText, - offsets: []Offset{Offset{int32(sidx), int32(eidx)}}, - rank: Rank{0, 0, item.rank.index}}) + matches = append(matches, + dupItem(item, []Offset{Offset{int32(sidx), int32(eidx)}})) } } return matches @@ -262,11 +268,7 @@ func (p *Pattern) extendedMatch(chunk *Chunk) []*Item { } } if len(offsets) == len(p.terms) { - matches = append(matches, &Item{ - text: item.text, - origText: item.origText, - offsets: offsets, - rank: Rank{0, 0, item.rank.index}}) + matches = append(matches, dupItem(item, offsets)) } } return matches diff --git a/src/pattern_test.go b/src/pattern_test.go index a776e30..2635b6c 100644 --- a/src/pattern_test.go +++ b/src/pattern_test.go @@ -86,19 +86,25 @@ func TestCaseSensitivity(t *testing.T) { } } -func TestOrigText(t *testing.T) { +func TestOrigTextAndTransformed(t *testing.T) { strptr := func(str string) *string { return &str } - pattern := BuildPattern(MODE_EXTENDED, CASE_SMART, []Range{}, nil, []rune("jg")) + tokens := Tokenize(strptr("junegunn"), nil) + trans := Transform(tokens, []Range{Range{1, 1}}) + for _, fun := range []func(*Chunk) []*Item{pattern.fuzzyMatch, pattern.extendedMatch} { chunk := Chunk{ - &Item{text: strptr("junegunn"), origText: strptr("junegunn.choi")}, + &Item{ + text: strptr("junegunn"), + origText: strptr("junegunn.choi"), + transformed: trans}, } matches := fun(&chunk) if *matches[0].text != "junegunn" || *matches[0].origText != "junegunn.choi" || - matches[0].offsets[0][0] != 0 || matches[0].offsets[0][1] != 5 { + matches[0].offsets[0][0] != 0 || matches[0].offsets[0][1] != 5 || + matches[0].transformed != trans { t.Error("Invalid match result", matches) } }