summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2025-01-10 17:43:13 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2025-01-10 17:43:13 +0900
commit14ef8e80519987962008b2da0c95907993d03865 (patch)
tree35ded2e2a4174f729310a5fdb432aa02bf6ae84e /src
parentcc1d9f124e543d59c09b21a8712870e25ba4f074 (diff)
downloadfzf-14ef8e80519987962008b2da0c95907993d03865.tar.gz
Support ANSI sequences with mixed ; and : delimiters (#4169)
`make bench` shows no loss of performance.
Diffstat (limited to 'src')
-rw-r--r--src/ansi.go24
-rw-r--r--src/ansi_test.go2
2 files changed, 10 insertions, 16 deletions
diff --git a/src/ansi.go b/src/ansi.go
index 638d7eff..bf00905e 100644
--- a/src/ansi.go
+++ b/src/ansi.go
@@ -310,20 +310,15 @@ func extractColor(str string, state *ansiState, proc func(string, *ansiState) bo
return trimmed, nil, state
}
-func parseAnsiCode(s string, delimiter byte) (int, byte, string) {
+func parseAnsiCode(s string) (int, string) {
var remaining string
var i int
- if delimiter == 0 {
- // Faster than strings.IndexAny(";:")
- i = strings.IndexByte(s, ';')
- if i < 0 {
- i = strings.IndexByte(s, ':')
- }
- } else {
- i = strings.IndexByte(s, delimiter)
+ // Faster than strings.IndexAny(";:")
+ i = strings.IndexByte(s, ';')
+ if i < 0 {
+ i = strings.IndexByte(s, ':')
}
if i >= 0 {
- delimiter = s[i]
remaining = s[i+1:]
s = s[:i]
}
@@ -335,14 +330,14 @@ func parseAnsiCode(s string, delimiter byte) (int, byte, string) {
for _, ch := range stringBytes(s) {
ch -= '0'
if ch > 9 {
- return -1, delimiter, remaining
+ return -1, remaining
}
code = code*10 + int(ch)
}
- return code, delimiter, remaining
+ return code, remaining
}
- return -1, delimiter, remaining
+ return -1, remaining
}
func interpretCode(ansiCode string, prevState *ansiState) ansiState {
@@ -378,11 +373,10 @@ func interpretCode(ansiCode string, prevState *ansiState) ansiState {
state256 := 0
ptr := &state.fg
- var delimiter byte
count := 0
for len(ansiCode) != 0 {
var num int
- if num, delimiter, ansiCode = parseAnsiCode(ansiCode, delimiter); num != -1 {
+ if num, ansiCode = parseAnsiCode(ansiCode); num != -1 {
count++
switch state256 {
case 0:
diff --git a/src/ansi_test.go b/src/ansi_test.go
index 55f7fc08..d70224f5 100644
--- a/src/ansi_test.go
+++ b/src/ansi_test.go
@@ -381,7 +381,7 @@ func TestParseAnsiCode(t *testing.T) {
{"-2", "", -1},
}
for _, x := range tests {
- n, _, s := parseAnsiCode(x.In, 0)
+ n, s := parseAnsiCode(x.In)
if n != x.N || s != x.Exp {
t.Fatalf("%q: got: (%d %q) want: (%d %q)", x.In, n, s, x.N, x.Exp)
}