Skip to content

Commit be1c5ad

Browse files
committed
avoid copying an empty map if no captures
1 parent 8d67f28 commit be1c5ad

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

x/meg/meg.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ type captureFunc *func(string) error
3333
type captureMap map[captureFunc][]string
3434

3535
func (cm captureMap) clone() captureMap {
36+
if cm == nil {
37+
return nil
38+
}
3639
out := make(captureMap, len(cm))
3740
for k, v := range cm {
3841
out[k] = slices.Clone(v)
@@ -80,6 +83,10 @@ func Match[S ~[]T, T Matchable](s *MatchState, components S) (bool, error) {
8083
if s.kind == matchCode && s.code == c.Code() {
8184
cm := currentStates.captures[i]
8285
if s.capture != nil {
86+
if cm == nil {
87+
cm = make(captureMap)
88+
currentStates.captures[i] = cm
89+
}
8390
cm[s.capture] = append(cm[s.capture], c.Value())
8491
}
8592
nextStates = appendState(nextStates, s.next, currentStates.captures[i], listGeneration)
@@ -111,9 +118,6 @@ func appendState(arr statesAndCaptures, s *MatchState, c captureMap, listGenerat
111118
if s == nil || s.generation == listGeneration {
112119
return arr
113120
}
114-
if c == nil {
115-
c = make(captureMap)
116-
}
117121
s.generation = listGeneration
118122
if s.kind == split {
119123
arr = appendState(arr, s.next, c, listGeneration)

0 commit comments

Comments
 (0)