From 209d5e8e9089d33694e40faa81107e07ff4f171f Mon Sep 17 00:00:00 2001 From: Charlie Vieth Date: Thu, 25 Aug 2022 01:02:08 -0400 Subject: [PATCH] ansi: speed up escape sequence parsing (#2927) --- src/ansi.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/ansi.go b/src/ansi.go index f7910d7..ff12db0 100644 --- a/src/ansi.go +++ b/src/ansi.go @@ -107,19 +107,27 @@ func matchControlSequence(s string) int { // ^ match starting here // i := 2 // prefix matched in nextAnsiEscapeSequence() - for ; i < len(s) && (isNumeric(s[i]) || s[i] == ';' || s[i] == ':' || s[i] == '?'); i++ { - } - if i < len(s) { + for ; i < len(s); i++ { c := s[i] - if 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == '@' { - return i + 1 + switch c { + case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ';', ':', '?': + // ok + default: + if 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == '@' { + return i + 1 + } + return -1 } } return -1 } func isCtrlSeqStart(c uint8) bool { - return c == '\\' || c == '[' || c == '(' || c == ')' + switch c { + case '\\', '[', '(', ')': + return true + } + return false } // nextAnsiEscapeSequence returns the ANSI escape sequence and is equivalent to