Skip to content
Myr

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

9 files+49 added−51 removed

modified:src/cache_test.go+1 added-2 removed
2 unmodified lines
3import "testing"45func 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 lines
modified:src/chunklist_test.go+11 added-9 removed
9 unmodified lines
10func TestChunkList(t *testing.T) {11 // FIXME global12 sortCriteria = []criterion{byScore, byLength}1314 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 })1920 // Snapshot21 snapshot, count := cl.Snapshot()
17 unmodified lines
39 }4041 // 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 lines
68 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 }7576 cl.Push([]byte("hello"))77 cl.Push([]byte("world"))7877 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 line
23import "sync"45// Chunk is a list of Items whose size has the upper limit of chunkSize6type Chunk []Item6type Chunk struct {7 items [chunkSize]Item8 count int9}1011// ItemBuilder is a closure type that builds Item object from a pointer to a12// string and an integer10type ItemBuilder func([]byte, int) Item13type ItemBuilder func(*Item, []byte, int) bool1415// ChunkList is a list of Chunks16type ChunkList struct {17 chunks []*Chunk
11 unmodified lines
29 trans: trans}30}3132func (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}3940// IsFull returns true if the Chunk is full41func (c *Chunk) IsFull() bool {40 return len(*c) == chunkSize42 return c.count == chunkSize43}4445func (cl *ChunkList) lastChunk() *Chunk {46 return cl.chunks[len(cl.chunks)-1]
3 unmodified lines
50func 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}5657// Push adds the item to the list58func (cl *ChunkList) Push(data []byte) bool {59 cl.mutex.Lock()6061 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 }6465 if cl.lastChunk().push(cl.trans, data, cl.count) {66 cl.count++
24 unmodified lines
modified:src/core.go+13 added-12 removed
83 unmodified lines
84 // 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 }114115 // Reader
35 unmodified lines
151 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 lines
modified:src/item.go+1 added-5 removed
15 unmodified lines
16func (item *Item) Index() int32 {17 return item.text.Index18}1920var nilItem = Item{text: util.Chars{Index: -1}}20var minItem = Item{text: util.Chars{Index: -1}}2122func (item *Item) Nil() bool {23 return item.Index() < 024}2522func (item *Item) TrimLength() uint16 {23 return item.text.TrimLength()24}25
19 unmodified lines
modified:src/merger.go+2 added-2 removed
27 unmodified lines
28 tac: tac,29 count: 0}3031 for _, chunk := range *mg.chunks {32 mg.count += len(*chunk)32 mg.count += chunk.count33 }34 return &mg35}36
27 unmodified lines
64 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 }7071 if mg.sorted {72 return mg.mergedGet(idx)
49 unmodified lines
modified:src/pattern_test.go+6 added-7 removed
128 unmodified lines
129 trans := Transform(tokens, []Range{Range{1, 1}})130131 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 }145147 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 lines
modified:src/pattern.go+2 added-2 removed
276 unmodified lines
277func (p *Pattern) matchChunk(chunk *Chunk, space []Result, slab *util.Slab) []Result {278 matches := []Result{}279280 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 lines
modified:src/result.go+1 added-1 removed
83 unmodified lines
84 return result.item.Index()85}8687func 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}9091func (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