Micro-optimizations

- Make structs smaller
- Introduce Result struct and use it to represent matched items instead of
  reusing Item struct for that purpose
- Avoid unnecessary memory allocation
- Avoid growing slice from the initial capacity
- Code cleanup
This commit is contained in:
Junegunn Choi
2016-08-19 02:39:32 +09:00
parent f7f01d109e
commit 37dc273148
19 changed files with 236 additions and 570 deletions

View File

@@ -36,7 +36,7 @@ func init() {
ansiRegex = regexp.MustCompile("\x1b\\[[0-9;]*[mK]")
}
func extractColor(str string, state *ansiState, proc func(string, *ansiState) bool) (string, []ansiOffset, *ansiState) {
func extractColor(str string, state *ansiState, proc func(string, *ansiState) bool) (string, *[]ansiOffset, *ansiState) {
var offsets []ansiOffset
var output bytes.Buffer
@@ -84,7 +84,10 @@ func extractColor(str string, state *ansiState, proc func(string, *ansiState) bo
if proc != nil {
proc(rest, state)
}
return output.String(), offsets, state
if len(offsets) == 0 {
return output.String(), nil, state
}
return output.String(), &offsets, state
}
func interpretCode(ansiCode string, prevState *ansiState) *ansiState {