Return Result by value from MatchItem

This commit is contained in:
Junegunn Choi
2026-02-28 15:02:06 +09:00
parent 2db14b4308
commit 8452c78cc8
2 changed files with 16 additions and 17 deletions

View File

@@ -260,7 +260,7 @@ func Run(opts *Options) (int, error) {
return false return false
} }
mutex.Lock() mutex.Lock()
if result, _, _ := pattern.MatchItem(&item, false, slab); result != nil { if result, _, _ := pattern.MatchItem(&item, false, slab); result.item != nil {
opts.Printer(transformer(&item)) opts.Printer(transformer(&item))
found = true found = true
} }

View File

@@ -316,14 +316,14 @@ func (p *Pattern) matchChunk(chunk *Chunk, space []Result, slab *util.Slab) []Re
// Huge code duplication for minimizing unnecessary map lookups // Huge code duplication for minimizing unnecessary map lookups
if space == nil { if space == nil {
for idx := startIdx; idx < chunk.count; idx++ { for idx := startIdx; idx < chunk.count; idx++ {
if match, _, _ := p.MatchItem(&chunk.items[idx], p.withPos, slab); match != nil { if match, _, _ := p.MatchItem(&chunk.items[idx], p.withPos, slab); match.item != nil {
matches = append(matches, *match) matches = append(matches, match)
} }
} }
} else { } else {
for _, result := range space { for _, result := range space {
if match, _, _ := p.MatchItem(result.item, p.withPos, slab); match != nil { if match, _, _ := p.MatchItem(result.item, p.withPos, slab); match.item != nil {
matches = append(matches, *match) matches = append(matches, match)
} }
} }
} }
@@ -335,8 +335,8 @@ func (p *Pattern) matchChunk(chunk *Chunk, space []Result, slab *util.Slab) []Re
if _, prs := p.denylist[chunk.items[idx].Index()]; prs { if _, prs := p.denylist[chunk.items[idx].Index()]; prs {
continue continue
} }
if match, _, _ := p.MatchItem(&chunk.items[idx], p.withPos, slab); match != nil { if match, _, _ := p.MatchItem(&chunk.items[idx], p.withPos, slab); match.item != nil {
matches = append(matches, *match) matches = append(matches, match)
} }
} }
} else { } else {
@@ -344,30 +344,29 @@ func (p *Pattern) matchChunk(chunk *Chunk, space []Result, slab *util.Slab) []Re
if _, prs := p.denylist[result.item.Index()]; prs { if _, prs := p.denylist[result.item.Index()]; prs {
continue continue
} }
if match, _, _ := p.MatchItem(result.item, p.withPos, slab); match != nil { if match, _, _ := p.MatchItem(result.item, p.withPos, slab); match.item != nil {
matches = append(matches, *match) matches = append(matches, match)
} }
} }
} }
return matches return matches
} }
// MatchItem returns true if the Item is a match // MatchItem returns the match result if the Item is a match.
func (p *Pattern) MatchItem(item *Item, withPos bool, slab *util.Slab) (*Result, []Offset, *[]int) { // A zero-value Result (with item == nil) indicates no match.
func (p *Pattern) MatchItem(item *Item, withPos bool, slab *util.Slab) (Result, []Offset, *[]int) {
if p.extended { if p.extended {
if offsets, bonus, pos := p.extendedMatch(item, withPos, slab); len(offsets) == len(p.termSets) { if offsets, bonus, pos := p.extendedMatch(item, withPos, slab); len(offsets) == len(p.termSets) {
result := buildResult(item, offsets, bonus) return buildResult(item, offsets, bonus), offsets, pos
return &result, offsets, pos
} }
return nil, nil, nil return Result{}, nil, nil
} }
offset, bonus, pos := p.basicMatch(item, withPos, slab) offset, bonus, pos := p.basicMatch(item, withPos, slab)
if sidx := offset[0]; sidx >= 0 { if sidx := offset[0]; sidx >= 0 {
offsets := []Offset{offset} offsets := []Offset{offset}
result := buildResult(item, offsets, bonus) return buildResult(item, offsets, bonus), offsets, pos
return &result, offsets, pos
} }
return nil, nil, nil return Result{}, nil, nil
} }
func (p *Pattern) basicMatch(item *Item, withPos bool, slab *util.Slab) (Offset, int, *[]int) { func (p *Pattern) basicMatch(item *Item, withPos bool, slab *util.Slab) (Offset, int, *[]int) {