Remove special nilItem
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Junegunn Choi · committed by Junegunn Choi · parent 2069bbc · Browse files · 9 files·+49 added−51 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+49 added−51 removed
- other:+49 added−51 removed
Provenance
- Commit
0d171ba- Tree
e82b6b4- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
2069bbc
9 files changed+49 added−51 removed
modified:src/cache_test.go+1 added-2 removed
2 unmodified lines3 import "testing"4 5 func TestChunkCache(t *testing.T) {6 cache := NewChunkCache()7- chunk2 := make(Chunk, chunkSize)7 chunk1p := &Chunk{}9- chunk2p := &chunk28+ chunk2p := &Chunk{count: chunkSize}9 items1 := []Result{Result{}}10 items2 := []Result{Result{}, Result{}}11 cache.Add(chunk1p, "foo", items1)12 cache.Add(chunk2p, "foo", items1)27 unmodified linesmodified:src/chunklist_test.go+11 added-9 removed
9 unmodified lines10 func TestChunkList(t *testing.T) {11 // FIXME global12 sortCriteria = []criterion{byScore, byLength}13 14- cl := NewChunkList(func(s []byte, i int) Item {15- chars := util.ToChars(s)16- chars.Index = int32(i * 2)17- return Item{text: chars}14+ cl := NewChunkList(func(item *Item, s []byte, i int) bool {15+ item.text = util.ToChars(s)16+ item.text.Index = int32(i * 2)17+ return true18 })19 20 // Snapshot21 snapshot, count := cl.Snapshot()17 unmodified lines39 }40 41 // Check the content of the ChunkList42 chunk1 := snapshot[0]43- if len(*chunk1) != 2 {43+ if chunk1.count != 2 {44 t.Error("Snapshot should contain only two items")45 }46- if (*chunk1)[0].text.ToString() != "hello" || (*chunk1)[0].Index() != 0 ||47- (*chunk1)[1].text.ToString() != "world" || (*chunk1)[1].Index() != 2 {46+ if chunk1.items[0].text.ToString() != "hello" ||47+ chunk1.items[0].Index() != 0 ||48+ chunk1.items[1].text.ToString() != "world" ||49+ chunk1.items[1].Index() != 2 {50 t.Error("Invalid data")51 }52 if chunk1.IsFull() {53 t.Error("Chunk should not have been marked full yet")14 unmodified lines68 if len(snapshot) != 3 || !snapshot[0].IsFull() ||69 !snapshot[1].IsFull() || snapshot[2].IsFull() || count != chunkSize*2+2 {70 t.Error("Expected two full chunks and one more chunk")71 }70- if len(*snapshot[2]) != 2 {72+ if snapshot[2].count != 2 {73 t.Error("Unexpected number of items")74 }75 76 cl.Push([]byte("hello"))77 cl.Push([]byte("world"))78 77- lastChunkCount := len(*snapshot[len(snapshot)-1])79+ lastChunkCount := snapshot[len(snapshot)-1].count80 if lastChunkCount != 2 {81 t.Error("Unexpected number of items:", lastChunkCount)82 }83 }modified:src/chunklist.go+12 added-11 removed
1 unmodified line2 3 import "sync"4 5 // Chunk is a list of Items whose size has the upper limit of chunkSize6-type Chunk []Item6+type Chunk struct {7+ items [chunkSize]Item8+ count int9+}10 11 // ItemBuilder is a closure type that builds Item object from a pointer to a12 // string and an integer10-type ItemBuilder func([]byte, int) Item13+type ItemBuilder func(*Item, []byte, int) bool14 15 // ChunkList is a list of Chunks16 type ChunkList struct {17 chunks []*Chunk11 unmodified lines29 trans: trans}30 }31 32 func (c *Chunk) push(trans ItemBuilder, data []byte, index int) bool {30- item := trans(data, index)31- if item.Nil() {32- return false33+ if trans(&c.items[c.count], data, index) {34+ c.count++35+ return true36 }34- *c = append(*c, item)35- return true37+ return false38 }39 40 // IsFull returns true if the Chunk is full41 func (c *Chunk) IsFull() bool {40- return len(*c) == chunkSize42+ return c.count == chunkSize43 }44 45 func (cl *ChunkList) lastChunk() *Chunk {46 return cl.chunks[len(cl.chunks)-1]3 unmodified lines50 func CountItems(cs []*Chunk) int {51 if len(cs) == 0 {52 return 053 }52- return chunkSize*(len(cs)-1) + len(*(cs[len(cs)-1]))54+ return chunkSize*(len(cs)-1) + cs[len(cs)-1].count55 }56 57 // Push adds the item to the list58 func (cl *ChunkList) Push(data []byte) bool {59 cl.mutex.Lock()60 61 if len(cl.chunks) == 0 || cl.lastChunk().IsFull() {60- newChunk := Chunk(make([]Item, 0, chunkSize))61- cl.chunks = append(cl.chunks, &newChunk)62+ cl.chunks = append(cl.chunks, &Chunk{})63 }64 65 if cl.lastChunk().push(cl.trans, data, cl.count) {66 cl.count++24 unmodified linesmodified:src/core.go+13 added-12 removed
83 unmodified lines84 // Chunk list85 var chunkList *ChunkList86 header := make([]string, 0, opts.HeaderLines)87 if len(opts.WithNth) == 0 {88- chunkList = NewChunkList(func(data []byte, index int) Item {88+ chunkList = NewChunkList(func(item *Item, data []byte, index int) bool {89 if len(header) < opts.HeaderLines {90 header = append(header, string(data))91 eventBox.Set(EvtHeader, header)92- return nilItem92+ return false93 }94- chars, colors := ansiProcessor(data)95- chars.Index = int32(index)96- return Item{text: chars, colors: colors}94+ item.text, item.colors = ansiProcessor(data)95+ item.text.Index = int32(index)96+ return true97 })98 } else {99- chunkList = NewChunkList(func(data []byte, index int) Item {99+ chunkList = NewChunkList(func(item *Item, data []byte, index int) bool {100 tokens := Tokenize(string(data), opts.Delimiter)101 trans := Transform(tokens, opts.WithNth)102 transformed := joinTokens(trans)103 if len(header) < opts.HeaderLines {104 header = append(header, transformed)105 eventBox.Set(EvtHeader, header)106- return nilItem106+ return false107 }108- trimmed, colors := ansiProcessor([]byte(transformed))109- trimmed.Index = int32(index)110- return Item{text: trimmed, colors: colors, origText: &data}108+ item.text, item.colors = ansiProcessor([]byte(transformed))109+ item.text.Index = int32(index)110+ item.origText = &data111+ return true112 })113 }114 115 // Reader35 unmodified lines151 if streamingFilter {152 slab := util.MakeSlab(slab16Size, slab32Size)153 reader := Reader{154 func(runes []byte) bool {154- item := chunkList.trans(runes, 0)155- if !item.Nil() {155+ item := Item{}156+ if chunkList.trans(&item, runes, 0) {157 if result, _, _ := pattern.MatchItem(&item, false, slab); result != nil {158 opts.Printer(item.text.ToString())159 found = true160 }117 unmodified linesmodified:src/item.go+1 added-5 removed
15 unmodified lines16 func (item *Item) Index() int32 {17 return item.text.Index18 }19 20-var nilItem = Item{text: util.Chars{Index: -1}}20+var minItem = Item{text: util.Chars{Index: -1}}21 22-func (item *Item) Nil() bool {23- return item.Index() < 024-}25-22 func (item *Item) TrimLength() uint16 {23 return item.text.TrimLength()24 }25 19 unmodified linesmodified:src/merger.go+2 added-2 removed
27 unmodified lines28 tac: tac,29 count: 0}30 31 for _, chunk := range *mg.chunks {32- mg.count += len(*chunk)32+ mg.count += chunk.count33 }34 return &mg35 }36 27 unmodified lines64 if mg.tac {65 idx = mg.count - idx - 166 }67 chunk := (*mg.chunks)[idx/chunkSize]68- return Result{item: &(*chunk)[idx%chunkSize]}68+ return Result{item: &chunk.items[idx%chunkSize]}69 }70 71 if mg.sorted {72 return mg.mergedGet(idx)49 unmodified linesmodified:src/pattern_test.go+6 added-7 removed
128 unmodified lines129 trans := Transform(tokens, []Range{Range{1, 1}})130 131 origBytes := []byte("junegunn.choi")132 for _, extended := range []bool{false, true} {133- chunk := Chunk{134- Item{135- text: util.ToChars([]byte("junegunn")),136- origText: &origBytes,137- transformed: &trans},138- }133+ chunk := Chunk{count: 1}134+ chunk.items[0] = Item{135+ text: util.ToChars([]byte("junegunn")),136+ origText: &origBytes,137+ transformed: &trans}138 pattern.extended = extended139 matches := pattern.matchChunk(&chunk, nil, slab) // No cache140 if !(matches[0].item.text.ToString() == "junegunn" &&141 string(*matches[0].item.origText) == "junegunn.choi" &&142 reflect.DeepEqual(*matches[0].item.transformed, trans)) {143 t.Error("Invalid match result", matches)144 }145 147- match, offsets, pos := pattern.MatchItem(&chunk[0], true, slab)146+ match, offsets, pos := pattern.MatchItem(&chunk.items[0], true, slab)147 if !(match.item.text.ToString() == "junegunn" &&148 string(*match.item.origText) == "junegunn.choi" &&149 offsets[0][0] == 0 && offsets[0][1] == 5 &&150 reflect.DeepEqual(*match.item.transformed, trans)) {54 unmodified linesmodified:src/pattern.go+2 added-2 removed
276 unmodified lines277 func (p *Pattern) matchChunk(chunk *Chunk, space []Result, slab *util.Slab) []Result {278 matches := []Result{}279 280 if space == nil {281- for idx := range *chunk {282- if match, _, _ := p.MatchItem(&(*chunk)[idx], false, slab); match != nil {281+ for idx := 0; idx < chunk.count; idx++ {282+ if match, _, _ := p.MatchItem(&chunk.items[idx], false, slab); match != nil {283 matches = append(matches, *match)284 }285 }286 } else {116 unmodified linesmodified:src/result.go+1 added-1 removed
83 unmodified lines84 return result.item.Index()85 }86 87 func minRank() Result {88- return Result{item: &nilItem, points: [4]uint16{math.MaxUint16, 0, 0, 0}}88+ return Result{item: &minItem, points: [4]uint16{math.MaxUint16, 0, 0, 0}}89 }90 91 func (result *Result) colorOffsets(matchOffsets []Offset, theme *tui.ColorTheme, color tui.ColorPair, attr tui.Attr, current bool) []colorOffset {92 itemColors := result.item.Colors()147 unmodified lines