From 37dc273148df0893053bf5cda0582a23f5c2b2d2 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Fri, 19 Aug 2016 02:39:32 +0900 Subject: 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 --- src/ansi.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/ansi.go') diff --git a/src/ansi.go b/src/ansi.go index 56831eee..0a525671 100644 --- a/src/ansi.go +++ b/src/ansi.go @@ -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 { -- cgit v1.2.3