Skip to content
Myr

Support ANSI sequences with mixed ; and : delimiters (#4169)

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

`make bench` shows no loss of performance.

authored by Junegunn Choi · committed by Junegunn Choi · parent cc1d9f1 · Browse files · 2 files+10 added−16 removed
This commit

State

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

Diff stats

2 files+10 added−16 removed

  • other:+10 added−16 removed

2 files+10 added−16 removed

modified:src/ansi_test.go+1 added-1 removed
379 unmodified lines
380 {"12;a", "a", 12},381 {"-2", "", -1},382 }383 for _, x := range tests {384 n, _, s := parseAnsiCode(x.In, 0)384 n, s := parseAnsiCode(x.In)385 if n != x.N || s != x.Exp {386 t.Fatalf("%q: got: (%d %q) want: (%d %q)", x.In, n, s, x.N, x.Exp)387 }388 }
43 unmodified lines
modified:src/ansi.go+9 added-15 removed
308 unmodified lines
309 }310 return trimmed, nil, state311}312313func parseAnsiCode(s string, delimiter byte) (int, byte, string) {313func parseAnsiCode(s string) (int, string) {314 var remaining string315 var i int316 if delimiter == 0 {317 // Faster than strings.IndexAny(";:")318 i = strings.IndexByte(s, ';')319 if i < 0 {320 i = strings.IndexByte(s, ':')316 // Faster than strings.IndexAny(";:")317 i = strings.IndexByte(s, ';')318 if i < 0 {319 i = strings.IndexByte(s, ':')321 }322 } else {323 i = strings.IndexByte(s, delimiter)320 }321 if i >= 0 {326 delimiter = s[i]322 remaining = s[i+1:]323 s = s[:i]324 }325
3 unmodified lines
329 code := 0330 for _, ch := range stringBytes(s) {331 ch -= '0'332 if ch > 9 {338 return -1, delimiter, remaining333 return -1, remaining334 }335 code = code*10 + int(ch)336 }342 return code, delimiter, remaining337 return code, remaining338 }339345 return -1, delimiter, remaining340 return -1, remaining341}342343func interpretCode(ansiCode string, prevState *ansiState) ansiState {344 var state ansiState
27 unmodified lines
372373 state256 := 0374 ptr := &state.fg375381 var delimiter byte376 count := 0377 for len(ansiCode) != 0 {378 var num int385 if num, delimiter, ansiCode = parseAnsiCode(ansiCode, delimiter); num != -1 {379 if num, ansiCode = parseAnsiCode(ansiCode); num != -1 {380 count++381 switch state256 {382 case 0:383 switch num {
90 unmodified lines