summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2017-08-18 03:04:11 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2017-08-18 03:04:11 +0900
commit931c78a70c3ff39f417898c1e97d21a82b47ec8d (patch)
tree2fe5a783132efccac8316a64f7d45001b83f7a19
parent8d23646fe6a7cc404824a6c88b3fe373fe38fb60 (diff)
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.
-rw-r--r--src/ansi.go28
1 files changed, 17 insertions, 11 deletions
diff --git a/src/ansi.go b/src/ansi.go
index 5856bd57..e1c85291 100644
--- a/src/ansi.go
+++ b/src/ansi.go
@@ -73,8 +73,6 @@ func extractColor(str string, state *ansiState, proc func(string, *ansiState) bo
runeCount := 0
for idx := 0; idx < len(str); {
idx += findAnsiStart(str[idx:])
-
- // No sign of ANSI code
if idx == len(str) {
break
}
@@ -117,22 +115,30 @@ func extractColor(str string, state *ansiState, proc func(string, *ansiState) bo
}
}
- rest := str[prevIdx:]
- if len(rest) > 0 {
+ var rest string
+ var trimmed string
+
+ if prevIdx == 0 {
+ // No ANSI code found
+ rest = str
+ trimmed = str
+ } else {
+ rest = str[prevIdx:]
output.WriteString(rest)
- if state != nil {
- // Update last offset
- runeCount += utf8.RuneCountInString(rest)
- (&offsets[len(offsets)-1]).offset[1] = int32(runeCount)
- }
+ trimmed = output.String()
+ }
+ if len(rest) > 0 && state != nil {
+ // Update last offset
+ runeCount += utf8.RuneCountInString(rest)
+ (&offsets[len(offsets)-1]).offset[1] = int32(runeCount)
}
if proc != nil {
proc(rest, state)
}
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 {