Skip to content
Myr

ansi: speed up escape sequence parsing (#2927)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Charlie Vieth · committed by GitHub · parent 9d041aa · Browse files · 1 file+14 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+14 added−6 removed

  • other:+14 added−6 removed

1 file+14 added−6 removed

modified:src/ansi.go+14 added-6 removed
105 unmodified lines
106 // `\x1b[\\[()][0-9;:?]*[a-zA-Z@]`107 // ^ match starting here108 //109 i := 2 // prefix matched in nextAnsiEscapeSequence()110 for ; i < len(s) && (isNumeric(s[i]) || s[i] == ';' || s[i] == ':' || s[i] == '?'); i++ {111 }112 if i < len(s) {110 for ; i < len(s); i++ {111 c := s[i]112 switch c {113 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ';', ':', '?':114 // ok115 default:114 if 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == '@' {115 return i + 1116 if 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == '@' {117 return i + 1118 }119 return -1120 }121 }122 return -1123}124125func isCtrlSeqStart(c uint8) bool {122 return c == '\\' || c == '[' || c == '(' || c == ')'126 switch c {127 case '\\', '[', '(', ')':128 return true129 }130 return false131}132133// nextAnsiEscapeSequence returns the ANSI escape sequence and is equivalent to134// calling FindStringIndex() on the below regex (which was originally used):
294 unmodified lines