Short-circuit ANSI processing if no ANSI codes are found
Rework of 656963e
. Makes --ansi processing around 20% faster on plain
strings without ANSI codes.
This commit is contained in:
parent
8d23646fe6
commit
931c78a70c
22
src/ansi.go
22
src/ansi.go
@ -73,8 +73,6 @@ func extractColor(str string, state *ansiState, proc func(string, *ansiState) bo
|
|||||||
runeCount := 0
|
runeCount := 0
|
||||||
for idx := 0; idx < len(str); {
|
for idx := 0; idx < len(str); {
|
||||||
idx += findAnsiStart(str[idx:])
|
idx += findAnsiStart(str[idx:])
|
||||||
|
|
||||||
// No sign of ANSI code
|
|
||||||
if idx == len(str) {
|
if idx == len(str) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -117,22 +115,30 @@ func extractColor(str string, state *ansiState, proc func(string, *ansiState) bo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rest := str[prevIdx:]
|
var rest string
|
||||||
if len(rest) > 0 {
|
var trimmed string
|
||||||
|
|
||||||
|
if prevIdx == 0 {
|
||||||
|
// No ANSI code found
|
||||||
|
rest = str
|
||||||
|
trimmed = str
|
||||||
|
} else {
|
||||||
|
rest = str[prevIdx:]
|
||||||
output.WriteString(rest)
|
output.WriteString(rest)
|
||||||
if state != nil {
|
trimmed = output.String()
|
||||||
|
}
|
||||||
|
if len(rest) > 0 && state != nil {
|
||||||
// Update last offset
|
// Update last offset
|
||||||
runeCount += utf8.RuneCountInString(rest)
|
runeCount += utf8.RuneCountInString(rest)
|
||||||
(&offsets[len(offsets)-1]).offset[1] = int32(runeCount)
|
(&offsets[len(offsets)-1]).offset[1] = int32(runeCount)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if proc != nil {
|
if proc != nil {
|
||||||
proc(rest, state)
|
proc(rest, state)
|
||||||
}
|
}
|
||||||
if len(offsets) == 0 {
|
if len(offsets) == 0 {
|
||||||
return output.String(), nil, state
|
return trimmed, nil, state
|
||||||
}
|
}
|
||||||
return output.String(), &offsets, state
|
return trimmed, &offsets, state
|
||||||
}
|
}
|
||||||
|
|
||||||
func interpretCode(ansiCode string, prevState *ansiState) *ansiState {
|
func interpretCode(ansiCode string, prevState *ansiState) *ansiState {
|
||||||
|
Loading…
Reference in New Issue
Block a user