Return Result by value from MatchItem
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 2db14b4 · Browse files · 2 files·+16 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+16 added−17 removed
- other:+16 added−17 removed
Provenance
- Commit
8452c78- Tree
eefb5bd- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
2db14b4
2 files changed+16 added−17 removed
modified:src/core.go+1 added-1 removed
258 unmodified lines259 if item.Index() < headerLines {260 return false261 }262 mutex.Lock()263- if result, _, _ := pattern.MatchItem(&item, false, slab); result != nil {263+ if result, _, _ := pattern.MatchItem(&item, false, slab); result.item != nil {264 opts.Printer(transformer(&item))265 found = true266 }267 mutex.Unlock()322 unmodified linesmodified:src/pattern.go+15 added-16 removed
314 unmodified lines315 if len(p.denylist) == 0 {316 // Huge code duplication for minimizing unnecessary map lookups317 if space == nil {318 for idx := startIdx; idx < chunk.count; idx++ {319- if match, _, _ := p.MatchItem(&chunk.items[idx], p.withPos, slab); match != nil {320- matches = append(matches, *match)319+ if match, _, _ := p.MatchItem(&chunk.items[idx], p.withPos, slab); match.item != nil {320+ matches = append(matches, match)321 }322 }323 } else {324 for _, result := range space {325- if match, _, _ := p.MatchItem(result.item, p.withPos, slab); match != nil {326- matches = append(matches, *match)325+ if match, _, _ := p.MatchItem(result.item, p.withPos, slab); match.item != nil {326+ matches = append(matches, match)327 }328 }329 }330 return matches3 unmodified lines334 for idx := startIdx; idx < chunk.count; idx++ {335 if _, prs := p.denylist[chunk.items[idx].Index()]; prs {336 continue337 }338- if match, _, _ := p.MatchItem(&chunk.items[idx], p.withPos, slab); match != nil {339- matches = append(matches, *match)338+ if match, _, _ := p.MatchItem(&chunk.items[idx], p.withPos, slab); match.item != nil {339+ matches = append(matches, match)340 }341 }342 } else {343 for _, result := range space {344 if _, prs := p.denylist[result.item.Index()]; prs {345 continue346 }347- if match, _, _ := p.MatchItem(result.item, p.withPos, slab); match != nil {348- matches = append(matches, *match)347+ if match, _, _ := p.MatchItem(result.item, p.withPos, slab); match.item != nil {348+ matches = append(matches, match)349 }350 }351 }352 return matches353 }354 355-// MatchItem returns true if the Item is a match356-func (p *Pattern) MatchItem(item *Item, withPos bool, slab *util.Slab) (*Result, []Offset, *[]int) {355+// MatchItem returns the match result if the Item is a match.356+// A zero-value Result (with item == nil) indicates no match.357+func (p *Pattern) MatchItem(item *Item, withPos bool, slab *util.Slab) (Result, []Offset, *[]int) {358 if p.extended {359 if offsets, bonus, pos := p.extendedMatch(item, withPos, slab); len(offsets) == len(p.termSets) {359- result := buildResult(item, offsets, bonus)360- return &result, offsets, pos360+ return buildResult(item, offsets, bonus), offsets, pos361 }362- return nil, nil, nil362+ return Result{}, nil, nil363 }364 offset, bonus, pos := p.basicMatch(item, withPos, slab)365 if sidx := offset[0]; sidx >= 0 {366 offsets := []Offset{offset}367- result := buildResult(item, offsets, bonus)368- return &result, offsets, pos367+ return buildResult(item, offsets, bonus), offsets, pos368 }370- return nil, nil, nil369+ return Result{}, nil, nil370 }371 372 func (p *Pattern) basicMatch(item *Item, withPos bool, slab *util.Slab) (Offset, int, *[]int) {373 var input []Token97 unmodified lines