From 8452c78cc8af7fa5071a2aefc6bd32c5c191d3e7 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sat, 28 Feb 2026 15:02:06 +0900 Subject: [PATCH] Return Result by value from MatchItem --- src/core.go | 2 +- src/pattern.go | 31 +++++++++++++++---------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/core.go b/src/core.go index 67d4a059..34803a0c 100644 --- a/src/core.go +++ b/src/core.go @@ -260,7 +260,7 @@ func Run(opts *Options) (int, error) { return false } 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)) found = true } diff --git a/src/pattern.go b/src/pattern.go index 4e34c62f..688ef433 100644 --- a/src/pattern.go +++ b/src/pattern.go @@ -316,14 +316,14 @@ func (p *Pattern) matchChunk(chunk *Chunk, space []Result, slab *util.Slab) []Re // Huge code duplication for minimizing unnecessary map lookups if space == nil { for idx := startIdx; idx < chunk.count; idx++ { - if match, _, _ := p.MatchItem(&chunk.items[idx], p.withPos, slab); match != nil { - matches = append(matches, *match) + if match, _, _ := p.MatchItem(&chunk.items[idx], p.withPos, slab); match.item != nil { + matches = append(matches, match) } } } else { for _, result := range space { - if match, _, _ := p.MatchItem(result.item, p.withPos, slab); match != nil { - matches = append(matches, *match) + if match, _, _ := p.MatchItem(result.item, p.withPos, slab); match.item != nil { + 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 { continue } - if match, _, _ := p.MatchItem(&chunk.items[idx], p.withPos, slab); match != nil { - matches = append(matches, *match) + if match, _, _ := p.MatchItem(&chunk.items[idx], p.withPos, slab); match.item != nil { + matches = append(matches, match) } } } 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 { continue } - if match, _, _ := p.MatchItem(result.item, p.withPos, slab); match != nil { - matches = append(matches, *match) + if match, _, _ := p.MatchItem(result.item, p.withPos, slab); match.item != nil { + matches = append(matches, match) } } } return matches } -// MatchItem returns true if the Item is a match -func (p *Pattern) MatchItem(item *Item, withPos bool, slab *util.Slab) (*Result, []Offset, *[]int) { +// MatchItem returns the match result if the Item is a match. +// 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 offsets, bonus, pos := p.extendedMatch(item, withPos, slab); len(offsets) == len(p.termSets) { - result := buildResult(item, offsets, bonus) - return &result, offsets, pos + return buildResult(item, offsets, bonus), offsets, pos } - return nil, nil, nil + return Result{}, nil, nil } offset, bonus, pos := p.basicMatch(item, withPos, slab) if sidx := offset[0]; sidx >= 0 { offsets := []Offset{offset} - result := buildResult(item, offsets, bonus) - return &result, offsets, pos + return buildResult(item, offsets, bonus), offsets, pos } - return nil, nil, nil + return Result{}, nil, nil } func (p *Pattern) basicMatch(item *Item, withPos bool, slab *util.Slab) (Offset, int, *[]int) {