From 0f48f36b0fa00f2c51efa5e7272aa53d679da4c0 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Sun, 3 Nov 2024 20:25:05 +0700 Subject: [PATCH] Small optimizations and cleanups (#1830) --- colors.go | 13 +++++++------ complete.go | 7 ++++--- eval.go | 2 +- icons.go | 4 +--- misc.go | 31 ++++++++++++++++++------------- parse.go | 7 ++----- 6 files changed, 33 insertions(+), 31 deletions(-) diff --git a/colors.go b/colors.go index 2d5d8c9e..8643ce78 100644 --- a/colors.go +++ b/colors.go @@ -98,7 +98,7 @@ func parseEscapeSequence(s string) tcell.Style { func applyAnsiCodes(s string, st tcell.Style) tcell.Style { toks := strings.Split(s, ";") - var nums []int + nums := make([]int, 0, len(toks)) for _, tok := range toks { if tok == "" { nums = append(nums, 0) @@ -115,7 +115,8 @@ func applyAnsiCodes(s string, st tcell.Style) tcell.Style { // ECMA-48 details the standard // TODO: should we support turning off attributes? // Probably because this is used for previewers too - for i := 0; i < len(nums); i++ { + numslen := len(nums) + for i := 0; i < numslen; i++ { n := nums[i] switch { case n == 0: @@ -143,10 +144,10 @@ func applyAnsiCodes(s string, st tcell.Style) tcell.Style { case n >= 90 && n <= 97: st = st.Foreground(tcell.PaletteColor(n - 82)) case n == 38: - if i+3 <= len(nums) && nums[i+1] == 5 { + if i+3 <= numslen && nums[i+1] == 5 { st = st.Foreground(tcell.PaletteColor(nums[i+2])) i += 2 - } else if i+5 <= len(nums) && nums[i+1] == 2 { + } else if i+5 <= numslen && nums[i+1] == 2 { st = st.Foreground(tcell.NewRGBColor( int32(nums[i+2]), int32(nums[i+3]), @@ -160,10 +161,10 @@ func applyAnsiCodes(s string, st tcell.Style) tcell.Style { case n >= 100 && n <= 107: st = st.Background(tcell.PaletteColor(n - 92)) case n == 48: - if i+3 <= len(nums) && nums[i+1] == 5 { + if i+3 <= numslen && nums[i+1] == 5 { st = st.Background(tcell.PaletteColor(nums[i+2])) i += 2 - } else if i+5 <= len(nums) && nums[i+1] == 2 { + } else if i+5 <= numslen && nums[i+1] == 2 { st = st.Background(tcell.NewRGBColor( int32(nums[i+2]), int32(nums[i+3]), diff --git a/complete.go b/complete.go index cd962a5a..8317ecca 100644 --- a/complete.go +++ b/complete.go @@ -215,9 +215,9 @@ func matchExec(s string) (matches []string, longest []rune) { } } - sort.Strings(words) - if len(words) > 0 { + sort.Strings(words) + uniq := words[:1] for i := 1; i < len(words); i++ { if words[i] != words[i-1] { @@ -294,7 +294,8 @@ func matchFile(s string) (matches []string, longest []rune) { } func matchCmd(s string) (matches []string, longest []rune) { - words := gCmdWords + words := make([]string, 0, len(gCmdWords)+len(gOpts.cmds)) + words = append(words, gCmdWords...) for c := range gOpts.cmds { words = append(words, c) } diff --git a/eval.go b/eval.go index b755d016..7fc05cc1 100644 --- a/eval.go +++ b/eval.go @@ -326,7 +326,7 @@ func (e *setExpr) eval(app *app, args []string) { gOpts.promptfmt = e.val case "ratios": toks := strings.Split(e.val, ":") - var rats []int + rats := make([]int, 0, len(toks)) for _, s := range toks { n, err := strconv.Atoi(s) if err != nil { diff --git a/icons.go b/icons.go index 49a522ab..f9b4a871 100644 --- a/icons.go +++ b/icons.go @@ -105,9 +105,7 @@ func (im *iconMap) parseEnv(env string) { } func (im *iconMap) parseArray(arr []string) { - key := arr[0] - - key = replaceTilde(key) + key := replaceTilde(arr[0]) if filepath.IsAbs(key) { key = filepath.Clean(key) diff --git a/misc.go b/misc.go index 0908a759..85e735ed 100644 --- a/misc.go +++ b/misc.go @@ -26,7 +26,7 @@ func isRoot(name string) bool { return filepath.Dir(name) == name } func replaceTilde(s string) string { if strings.HasPrefix(s, "~") { - s = strings.Replace(s, "~", gUser.HomeDir, 1) + return gUser.HomeDir + s[1:] } return s } @@ -76,7 +76,7 @@ func runeSliceWidthLastRange(rs []rune, maxWidth int) []rune { } // This function is used to escape whitespaces and special characters with -// backlashes in a given string. +// backslashes in a given string. func escape(s string) string { buf := make([]rune, 0, len(s)) for _, r := range s { @@ -88,7 +88,7 @@ func escape(s string) string { return string(buf) } -// This function is used to remove backlashes that are used to escape +// This function is used to remove backslashes that are used to escape // whitespaces and special characters in a given string. func unescape(s string) string { esc := false @@ -139,8 +139,7 @@ func tokenize(s string) []string { buf = nil } } - toks = append(toks, string(buf)) - return toks + return append(toks, string(buf)) } // This function splits the first word of a string delimited by whitespace from @@ -198,15 +197,16 @@ func readArrays(r io.Reader, min_cols, max_cols int) ([][]string, error) { } return !squote && !dquote && unicode.IsSpace(r) }) + arrlen := len(arr) - if len(arr) < min_cols || len(arr) > max_cols { + if arrlen < min_cols || arrlen > max_cols { if min_cols == max_cols { return nil, fmt.Errorf("expected %d columns but found: %s", min_cols, s.Text()) } return nil, fmt.Errorf("expected %d~%d columns but found: %s", min_cols, max_cols, s.Text()) } - for i := 0; i < len(arr); i++ { + for i := 0; i < arrlen; i++ { squote, dquote = false, false buf := make([]rune, 0, len(arr[i])) for _, r := range arr[i] { @@ -257,7 +257,8 @@ func humanize(size int64) string { for _, s := range suffix { if curr < 10 { return fmt.Sprintf("%.1f%s", curr-0.0499, s) - } else if curr < 1000 { + } + if curr < 1000 { return fmt.Sprintf("%d%s", int(curr), s) } curr /= 1000 @@ -272,22 +273,24 @@ func humanize(size int64) string { // than 'foo10bar'. func naturalLess(s1, s2 string) bool { lo1, lo2, hi1, hi2 := 0, 0, 0, 0 + s1len := len(s1) + s2len := len(s2) for { - if hi1 >= len(s1) { - return hi2 != len(s2) + if hi1 >= s1len { + return hi2 != s2len } - if hi2 >= len(s2) { + if hi2 >= s2len { return false } isDigit1 := isDigit(s1[hi1]) isDigit2 := isDigit(s2[hi2]) - for lo1 = hi1; hi1 < len(s1) && isDigit(s1[hi1]) == isDigit1; hi1++ { + for lo1 = hi1; hi1 < s1len && isDigit(s1[hi1]) == isDigit1; hi1++ { } - for lo2 = hi2; hi2 < len(s2) && isDigit(s2[hi2]) == isDigit2; hi2++ { + for lo2 = hi2; hi2 < s2len && isDigit(s2[hi2]) == isDigit2; hi2++ { } if s1[lo1:hi1] == s2[lo2:hi2] { @@ -332,6 +335,7 @@ var ( reWordEnd = regexp.MustCompile(`(\pL|\pN)([^\pL\pN]|$)`) ) +// TODO: Remove when bumping to Go 1.21+ func min(a, b int) int { if a < b { return a @@ -339,6 +343,7 @@ func min(a, b int) int { return b } +// TODO: Remove when bumping to Go 1.21+ func max(a, b int) int { if a > b { return a diff --git a/parse.go b/parse.go index f6ba4e77..ceb3fdee 100644 --- a/parse.go +++ b/parse.go @@ -324,9 +324,6 @@ func (p *parser) parseExpr() expr { } func (p *parser) parse() bool { - if p.expr = p.parseExpr(); p.expr == nil { - return false - } - - return true + p.expr = p.parseExpr() + return p.expr != nil }