Skip to content
Myr

Remove count field from ChunkList

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 487c8fe · Browse files · 3 files+17 added−25 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+17 added−25 removed

  • other:+17 added−25 removed

3 files+17 added−25 removed

modified:src/chunklist_test.go+2 added-5 removed
9 unmodified lines
10func TestChunkList(t *testing.T) {11 // FIXME global12 sortCriteria = []criterion{byScore, byLength}1314 cl := NewChunkList(func(item *Item, s []byte, i int) bool {14 cl := NewChunkList(func(item *Item, s []byte) bool {15 item.text = util.ToChars(s)16 item.text.Index = int32(i * 2)16 return true17 })1819 // Snapshot
22 unmodified lines
42 if chunk1.count != 2 {43 t.Error("Snapshot should contain only two items")44 }45 if chunk1.items[0].text.ToString() != "hello" ||47 chunk1.items[0].Index() != 0 ||48 chunk1.items[1].text.ToString() != "world" ||46 chunk1.items[1].text.ToString() != "world" {49 chunk1.items[1].Index() != 2 {47 t.Error("Invalid data")48 }49 if chunk1.IsFull() {50 t.Error("Chunk should not have been marked full yet")
30 unmodified lines
modified:src/chunklist.go+7 added-15 removed
6 unmodified lines
7 items [chunkSize]Item8 count int9}1011// ItemBuilder is a closure type that builds Item object from a pointer to a12// string and an integer13type ItemBuilder func(*Item, []byte, int) bool11// ItemBuilder is a closure type that builds Item object from byte array12type ItemBuilder func(*Item, []byte) bool1314// ChunkList is a list of Chunks15type ChunkList struct {16 chunks []*Chunk18 count int17 mutex sync.Mutex18 trans ItemBuilder19}2021// NewChunkList returns a new ChunkList22func NewChunkList(trans ItemBuilder) *ChunkList {23 return &ChunkList{24 chunks: []*Chunk{},27 count: 0,25 mutex: sync.Mutex{},26 trans: trans}27}2832func (c *Chunk) push(trans ItemBuilder, data []byte, index int) bool {33 if trans(&c.items[c.count], data, index) {29func (c *Chunk) push(trans ItemBuilder, data []byte) bool {30 if trans(&c.items[c.count], data) {31 c.count++32 return true33 }34 return false
23 unmodified lines
58 if len(cl.chunks) == 0 || cl.lastChunk().IsFull() {59 cl.chunks = append(cl.chunks, &Chunk{})60 }6165 if cl.lastChunk().push(cl.trans, data, cl.count) {66 cl.count++67 cl.mutex.Unlock()68 return true69 }62 ret := cl.lastChunk().push(cl.trans, data)63 cl.mutex.Unlock()71 return false64 return ret65}6667// Snapshot returns immutable snapshot of the ChunkList68func (cl *ChunkList) Snapshot() ([]*Chunk, int) {69 cl.mutex.Lock()7071 ret := make([]*Chunk, len(cl.chunks))79 count := cl.count72 copy(ret, cl.chunks)7374 // Duplicate the last chunk75 if cnt := len(ret); cnt > 0 {
1 unmodified line
77 ret[cnt-1] = &newChunk78 }7980 cl.mutex.Unlock()89 return ret, count81 return ret, CountItems(ret)82}
modified:src/core.go+8 added-5 removed
81 unmodified lines
82 }8384 // Chunk list85 var chunkList *ChunkList86 var itemIndex int3287 header := make([]string, 0, opts.HeaderLines)88 if len(opts.WithNth) == 0 {88 chunkList = NewChunkList(func(item *Item, data []byte, index int) bool {89 chunkList = NewChunkList(func(item *Item, data []byte) bool {90 if len(header) < opts.HeaderLines {91 header = append(header, string(data))92 eventBox.Set(EvtHeader, header)93 return false94 }95 item.text, item.colors = ansiProcessor(data)95 item.text.Index = int32(index)96 item.text.Index = itemIndex97 itemIndex++98 return true99 })100 } else {99 chunkList = NewChunkList(func(item *Item, data []byte, index int) bool {101 chunkList = NewChunkList(func(item *Item, data []byte) bool {102 tokens := Tokenize(string(data), opts.Delimiter)103 trans := Transform(tokens, opts.WithNth)104 transformed := joinTokens(trans)105 if len(header) < opts.HeaderLines {
1 unmodified line
107 eventBox.Set(EvtHeader, header)108 return false109 }110 item.text, item.colors = ansiProcessor([]byte(transformed))109 item.text.Index = int32(index)111 item.text.Index = itemIndex112 item.origText = &data113 itemIndex++114 return true115 })116 }117
37 unmodified lines
155 slab := util.MakeSlab(slab16Size, slab32Size)156 reader := NewReader(157 func(runes []byte) bool {158 item := Item{}156 if chunkList.trans(&item, runes, 0) {159 if chunkList.trans(&item, runes) {160 if result, _, _ := pattern.MatchItem(&item, false, slab); result != nil {161 opts.Printer(item.text.ToString())162 found = true163 }
117 unmodified lines