From b70534600864b0ab1f8aff1d1cd748cf8182f4b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Insaurralde?= Date: Tue, 26 May 2026 07:26:42 +0200 Subject: [PATCH] perf(cli): compile ANSI strip regex once at package scope MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Promote the ANSI-stripping pattern in attestation_status.go to a package-level regexp.MustCompile so each call to removeAnsiCharactersFromBytes reuses the compiled matcher instead of recompiling on every invocation. Signed-off-by: Matías Insaurralde Chainloop-Trace-Sessions: 617492fc-fc21-43ad-9d49-01a7c53b9dde --- app/cli/cmd/attestation_status.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/cli/cmd/attestation_status.go b/app/cli/cmd/attestation_status.go index fa3c825fd..3920bb13f 100644 --- a/app/cli/cmd/attestation_status.go +++ b/app/cli/cmd/attestation_status.go @@ -307,10 +307,11 @@ func versionStringAttFinal(p *action.ProjectVersion) string { return p.Version } -// removeAnsiCharactersFromBytes removes ANSI escape codes from bytes slices. +// ansiPattern matches ANSI escape codes. // Credits to: https://github.com/acarl005/stripansi +var ansiPattern = regexp.MustCompile("[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))") + +// removeAnsiCharactersFromBytes removes ANSI escape codes from bytes slices. func removeAnsiCharactersFromBytes(input []byte) []byte { - const ansiPattern = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))" - re := regexp.MustCompile(ansiPattern) - return re.ReplaceAll(input, []byte("")) + return ansiPattern.ReplaceAll(input, nil) }