Remove pointer indirection by changing Chunk definition
This commit is contained in:
parent
7b5ccc45bc
commit
d4f3d5a164
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
16
src/core.go
16
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
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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 &&
|
||||
|
Loading…
Reference in New Issue
Block a user