diff options
author | Junegunn Choi <junegunn.c@gmail.com> | 2017-07-15 12:28:29 +0900 |
---|---|---|
committer | Junegunn Choi <junegunn.c@gmail.com> | 2017-07-16 23:34:32 +0900 |
commit | d4f3d5a16423fbf039644f04516c052d1654326c (patch) | |
tree | 2cb3f9519a23e9e9c162d135636d9f99373deadc /src | |
parent | 7b5ccc45bc76f289fe2c48cf91e895b9ca99b1e2 (diff) |
Remove pointer indirection by changing Chunk definition
Diffstat (limited to 'src')
-rw-r--r-- | src/chunklist.go | 16 | ||||
-rw-r--r-- | src/chunklist_test.go | 4 | ||||
-rw-r--r-- | src/core.go | 16 | ||||
-rw-r--r-- | src/item.go | 6 | ||||
-rw-r--r-- | src/merger.go | 2 | ||||
-rw-r--r-- | src/pattern.go | 4 | ||||
-rw-r--r-- | src/pattern_test.go | 4 |
7 files changed, 29 insertions, 23 deletions
diff --git a/src/chunklist.go b/src/chunklist.go index a953fae9..1144274c 100644 --- a/src/chunklist.go +++ b/src/chunklist.go @@ -2,12 +2,12 @@ package fzf import "sync" -// Chunk is a list of Item pointers whose size has the upper limit of chunkSize -type Chunk []*Item // >>> []Item +// Chunk is a list of Items whose size has the upper limit of chunkSize +type Chunk []Item // ItemBuilder is a closure type that builds Item object from a pointer to a // string and an integer -type ItemBuilder func([]byte, int) *Item +type ItemBuilder func([]byte, int) Item // ChunkList is a list of Chunks type ChunkList struct { @@ -28,11 +28,11 @@ func NewChunkList(trans ItemBuilder) *ChunkList { func (c *Chunk) push(trans ItemBuilder, data []byte, index int) bool { item := trans(data, index) - if item != nil { - *c = append(*c, item) - return true + if item.Nil() { + return false } - return false + *c = append(*c, item) + return true } // IsFull returns true if the Chunk is full @@ -58,7 +58,7 @@ func (cl *ChunkList) Push(data []byte) bool { defer cl.mutex.Unlock() if len(cl.chunks) == 0 || cl.lastChunk().IsFull() { - newChunk := Chunk(make([]*Item, 0, chunkSize)) + newChunk := Chunk(make([]Item, 0, chunkSize)) cl.chunks = append(cl.chunks, &newChunk) } diff --git a/src/chunklist_test.go b/src/chunklist_test.go index 56559d88..983a7ed8 100644 --- a/src/chunklist_test.go +++ b/src/chunklist_test.go @@ -11,8 +11,8 @@ func TestChunkList(t *testing.T) { // FIXME global sortCriteria = []criterion{byScore, byLength} - cl := NewChunkList(func(s []byte, i int) *Item { - return &Item{text: util.ToChars(s), index: int32(i * 2)} + cl := NewChunkList(func(s []byte, i int) Item { + return Item{text: util.ToChars(s), index: int32(i * 2)} }) // Snapshot diff --git a/src/core.go b/src/core.go index a528dbb6..7e16dc35 100644 --- a/src/core.go +++ b/src/core.go @@ -91,27 +91,27 @@ func Run(opts *Options, revision string) { var chunkList *ChunkList header := make([]string, 0, opts.HeaderLines) if len(opts.WithNth) == 0 { - chunkList = NewChunkList(func(data []byte, index int) *Item { + chunkList = NewChunkList(func(data []byte, index int) Item { if len(header) < opts.HeaderLines { header = append(header, string(data)) eventBox.Set(EvtHeader, header) - return nil + return nilItem } chars, colors := ansiProcessor(data) - return &Item{ + return Item{ index: int32(index), trimLength: -1, text: chars, colors: colors} }) } else { - chunkList = NewChunkList(func(data []byte, index int) *Item { + chunkList = NewChunkList(func(data []byte, index int) Item { tokens := Tokenize(util.ToChars(data), opts.Delimiter) trans := Transform(tokens, opts.WithNth) if len(header) < opts.HeaderLines { header = append(header, string(joinTokens(trans))) eventBox.Set(EvtHeader, header) - return nil + return nilItem } textRunes := joinTokens(trans) item := Item{ @@ -123,7 +123,7 @@ func Run(opts *Options, revision string) { trimmed, colors := ansiProcessorRunes(textRunes) item.text = trimmed item.colors = colors - return &item + return item }) } @@ -168,8 +168,8 @@ func Run(opts *Options, revision string) { reader := Reader{ func(runes []byte) bool { item := chunkList.trans(runes, 0) - if item != nil { - if result, _, _ := pattern.MatchItem(item, false, slab); result != nil { + if !item.Nil() { + if result, _, _ := pattern.MatchItem(&item, false, slab); result != nil { opts.Printer(item.text.ToString()) found = true } diff --git a/src/item.go b/src/item.go index c67ac7ab..955c31d8 100644 --- a/src/item.go +++ b/src/item.go @@ -19,6 +19,12 @@ func (item *Item) Index() int32 { return item.index } +var nilItem = Item{index: -1} + +func (item *Item) Nil() bool { + return item.index < 0 +} + func (item *Item) TrimLength() int32 { if item.trimLength >= 0 { return item.trimLength diff --git a/src/merger.go b/src/merger.go index 2c7675d1..950ba04d 100644 --- a/src/merger.go +++ b/src/merger.go @@ -65,7 +65,7 @@ func (mg *Merger) Get(idx int) *Result { idx = mg.count - idx - 1 } chunk := (*mg.chunks)[idx/chunkSize] - return &Result{item: (*chunk)[idx%chunkSize]} + return &Result{item: &(*chunk)[idx%chunkSize]} } if mg.sorted { diff --git a/src/pattern.go b/src/pattern.go index 4614e079..05b03b9e 100644 --- a/src/pattern.go +++ b/src/pattern.go @@ -281,8 +281,8 @@ func (p *Pattern) matchChunk(chunk *Chunk, space []*Result, slab *util.Slab) []* matches := []*Result{} if space == nil { - for _, item := range *chunk { - if match, _, _ := p.MatchItem(item, false, slab); match != nil { + for idx := range *chunk { + if match, _, _ := p.MatchItem(&(*chunk)[idx], false, slab); match != nil { matches = append(matches, match) } } diff --git a/src/pattern_test.go b/src/pattern_test.go index 9d66c798..5722be4a 100644 --- a/src/pattern_test.go +++ b/src/pattern_test.go @@ -139,7 +139,7 @@ func TestOrigTextAndTransformed(t *testing.T) { origBytes := []byte("junegunn.choi") for _, extended := range []bool{false, true} { chunk := Chunk{ - &Item{ + Item{ text: util.RunesToChars([]rune("junegunn")), origText: &origBytes, transformed: trans}, @@ -152,7 +152,7 @@ func TestOrigTextAndTransformed(t *testing.T) { t.Error("Invalid match result", matches) } - match, offsets, pos := pattern.MatchItem(chunk[0], true, slab) + match, offsets, pos := pattern.MatchItem(&chunk[0], true, slab) if !(match.item.text.ToString() == "junegunn" && string(*match.item.origText) == "junegunn.choi" && offsets[0][0] == 0 && offsets[0][1] == 5 && |