From a88036fc691ee18763edb5d249c503c9656efa55 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Sat, 18 Dec 2021 19:30:31 +0100 Subject: [PATCH] perf: avoid InBounds checks --- src/image.go | 7 +++++-- src/regex.go | 2 +- src/scm.go | 2 +- src/segment_aws.go | 7 +++++-- src/segment_executiontime.go | 8 ++++++-- src/segment_git.go | 16 +++++++++------- src/segment_language.go | 6 +++++- src/segment_owm.go | 7 ++++++- src/segment_path.go | 4 +++- 9 files changed, 41 insertions(+), 18 deletions(-) diff --git a/src/image.go b/src/image.go index 7c8cfb73..ab84c3e4 100644 --- a/src/image.go +++ b/src/image.go @@ -84,9 +84,9 @@ type RGB struct { func NewRGBColor(ansiColor string) *RGB { colors := strings.Split(ansiColor, ";") - r, _ := strconv.Atoi(colors[0]) - g, _ := strconv.Atoi(colors[1]) b, _ := strconv.Atoi(colors[2]) + g, _ := strconv.Atoi(colors[1]) + r, _ := strconv.Atoi(colors[0]) return &RGB{ r: r, g: g, @@ -365,6 +365,9 @@ func (ir *ImageRenderer) SavePNG(path string) error { continue } runes := []rune(ir.ansiString) + if len(runes) == 0 { + continue + } str := string(runes[0:1]) ir.ansiString = string(runes[1:]) switch ir.style { diff --git a/src/regex.go b/src/regex.go index 524d987b..738c8438 100644 --- a/src/regex.go +++ b/src/regex.go @@ -87,7 +87,7 @@ func dirMatchesOneOf(env environmentInfo, dir string, regexes []string) bool { for _, element := range regexes { normalizedElement := strings.ReplaceAll(element, "\\\\", "/") if strings.HasPrefix(normalizedElement, "~") { - normalizedElement = normalizedHomeDir + normalizedElement[1:] + normalizedElement = strings.Replace(normalizedElement, "~", normalizedHomeDir, 1) } pattern := fmt.Sprintf("^%s$", normalizedElement) goos := env.getRuntimeGOOS() diff --git a/src/scm.go b/src/scm.go index 6dd34969..b3aa581e 100644 --- a/src/scm.go +++ b/src/scm.go @@ -56,7 +56,7 @@ func (s *scm) init(props properties, env environmentInfo) { func (s *scm) truncateBranch(branch string) string { fullBranchPath := s.props.getBool(FullBranchPath, true) maxLength := s.props.getInt(BranchMaxLength, 0) - if !fullBranchPath && len(branch) > 0 { + if !fullBranchPath && strings.Contains(branch, "/") { index := strings.LastIndex(branch, "/") branch = branch[index+1:] } diff --git a/src/segment_aws.go b/src/segment_aws.go index 0290c37c..6d856454 100644 --- a/src/segment_aws.go +++ b/src/segment_aws.go @@ -69,8 +69,11 @@ func (a *aws) getConfigFileInfo() { continue } if sectionActive && strings.HasPrefix(line, "region") { - a.Region = strings.TrimSpace(strings.Split(line, "=")[1]) - break + splitted := strings.Split(line, "=") + if len(splitted) >= 2 { + a.Region = strings.TrimSpace(splitted[1]) + break + } } } if a.Profile == "" && a.Region != "" { diff --git a/src/segment_executiontime.go b/src/segment_executiontime.go index 34eda796..680942ed 100644 --- a/src/segment_executiontime.go +++ b/src/segment_executiontime.go @@ -153,7 +153,9 @@ func (t *executiontime) formatDurationHouston() string { // format milliseconds as a string with truncated trailing zeros milliseconds = strconv.FormatFloat(float64(t.Ms%second)/second, 'f', -1, 64) // at this point milliseconds looks like "0.5". remove the leading "0" - milliseconds = milliseconds[1:] + if len(milliseconds) >= 1 { + milliseconds = milliseconds[1:] + } } result := fmt.Sprintf("%02d:%02d:%02d%s", t.Ms/hour, t.Ms/minute%minutesPerHour, t.Ms%minute/second, milliseconds) @@ -175,7 +177,9 @@ func (t *executiontime) formatDurationAmarillo() string { decimalResult := strconv.FormatFloat(decimalNumber, 'f', -1, 64) // at this point decimalResult looks like "0.5" // remove the leading "0" and append - result += decimalResult[1:] + if len(decimalResult) >= 1 { + result += decimalResult[1:] + } } result += "s" diff --git a/src/segment_git.go b/src/segment_git.go index b73978be..df492a65 100644 --- a/src/segment_git.go +++ b/src/segment_git.go @@ -247,24 +247,26 @@ func (g *git) setGitStatus() { g.Staging = &GitStatus{} output := g.getGitCommandOutput("status", "-unormal", "--branch", "--porcelain=2") for _, line := range strings.Split(output, "\n") { - if strings.HasPrefix(line, HASH) { + if strings.HasPrefix(line, HASH) && len(line) >= len(HASH)+7 { g.Hash = line[len(HASH) : len(HASH)+7] continue } - if strings.HasPrefix(line, REF) { + if strings.HasPrefix(line, REF) && len(line) > len(REF) { g.Ref = line[len(REF):] continue } - if strings.HasPrefix(line, UPSTREAM) { + if strings.HasPrefix(line, UPSTREAM) && len(line) > len(UPSTREAM) { g.Upstream = line[len(UPSTREAM):] continue } - if strings.HasPrefix(line, BRANCHSTATUS) { + if strings.HasPrefix(line, BRANCHSTATUS) && len(line) > len(BRANCHSTATUS) { status := line[len(BRANCHSTATUS):] splitted := strings.Split(status, " ") - g.Ahead, _ = strconv.Atoi(splitted[0]) - behind, _ := strconv.Atoi(splitted[1]) - g.Behind = -behind + if len(splitted) >= 2 { + g.Ahead, _ = strconv.Atoi(splitted[0]) + behind, _ := strconv.Atoi(splitted[1]) + g.Behind = -behind + } continue } addToStatus(line) diff --git a/src/segment_language.go b/src/segment_language.go index 95b64680..95024540 100644 --- a/src/segment_language.go +++ b/src/segment_language.go @@ -235,7 +235,11 @@ func (l *language) buildVersionURL(text string) string { if n == 0 { return fmt.Sprintf(str, args...), nil } - return fmt.Sprintf(str, args[:n]...), nil + arguments := make([]interface{}, 0, n) + for i := 0; i < n; i++ { + arguments = append(arguments, args[i]) + } + return fmt.Sprintf(str, arguments...), nil } version, err := truncatingSprintf(l.versionURLTemplate, text, l.version.Major, l.version.Minor, l.version.Patch) if err != nil { diff --git a/src/segment_owm.go b/src/segment_owm.go index ca1928f3..eba76435 100644 --- a/src/segment_owm.go +++ b/src/segment_owm.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "errors" "fmt" ) @@ -122,10 +123,14 @@ func (d *owm) setStatus() error { if err != nil { return err } + if len(q.Data) == 0 { + return errors.New("No data found") + } + id := q.Data[0].TypeID d.Temperature = q.temperature.Value icon := "" - switch q.Data[0].TypeID { + switch id { case "01n": fallthrough case "01d": diff --git a/src/segment_path.go b/src/segment_path.go index 19a2e30c..d87a8bfa 100644 --- a/src/segment_path.go +++ b/src/segment_path.go @@ -206,7 +206,9 @@ func (pt *path) getLetterPath() string { buffer.WriteString(fmt.Sprintf("%s%s", letter, separator)) } - buffer.WriteString(splitted[len(splitted)-1]) + if len(splitted) > 0 { + buffer.WriteString(splitted[len(splitted)-1]) + } return buffer.String() }