Skip to content

Commit 41a93c4

Browse files
perf(cli): compile ANSI strip regex once at package scope (#3150)
1 parent 1f43056 commit 41a93c4

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

app/cli/cmd/attestation_status.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,11 @@ func versionStringAttFinal(p *action.ProjectVersion) string {
307307
return p.Version
308308
}
309309

310-
// removeAnsiCharactersFromBytes removes ANSI escape codes from bytes slices.
310+
// ansiPattern matches ANSI escape codes.
311311
// Credits to: https://github.com/acarl005/stripansi
312+
var ansiPattern = regexp.MustCompile("[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))")
313+
314+
// removeAnsiCharactersFromBytes removes ANSI escape codes from bytes slices.
312315
func removeAnsiCharactersFromBytes(input []byte) []byte {
313-
const ansiPattern = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))"
314-
re := regexp.MustCompile(ansiPattern)
315-
return re.ReplaceAll(input, []byte(""))
316+
return ansiPattern.ReplaceAll(input, nil)
316317
}

0 commit comments

Comments
 (0)