perf: avoid InBounds checks

This commit is contained in:
Jan De Dobbeleer 2021-12-18 19:30:31 +01:00 committed by Jan De Dobbeleer
parent 7805ee1a27
commit a88036fc69
9 changed files with 41 additions and 18 deletions

View file

@ -84,9 +84,9 @@ type RGB struct {
func NewRGBColor(ansiColor string) *RGB { func NewRGBColor(ansiColor string) *RGB {
colors := strings.Split(ansiColor, ";") colors := strings.Split(ansiColor, ";")
r, _ := strconv.Atoi(colors[0])
g, _ := strconv.Atoi(colors[1])
b, _ := strconv.Atoi(colors[2]) b, _ := strconv.Atoi(colors[2])
g, _ := strconv.Atoi(colors[1])
r, _ := strconv.Atoi(colors[0])
return &RGB{ return &RGB{
r: r, r: r,
g: g, g: g,
@ -365,6 +365,9 @@ func (ir *ImageRenderer) SavePNG(path string) error {
continue continue
} }
runes := []rune(ir.ansiString) runes := []rune(ir.ansiString)
if len(runes) == 0 {
continue
}
str := string(runes[0:1]) str := string(runes[0:1])
ir.ansiString = string(runes[1:]) ir.ansiString = string(runes[1:])
switch ir.style { switch ir.style {

View file

@ -87,7 +87,7 @@ func dirMatchesOneOf(env environmentInfo, dir string, regexes []string) bool {
for _, element := range regexes { for _, element := range regexes {
normalizedElement := strings.ReplaceAll(element, "\\\\", "/") normalizedElement := strings.ReplaceAll(element, "\\\\", "/")
if strings.HasPrefix(normalizedElement, "~") { if strings.HasPrefix(normalizedElement, "~") {
normalizedElement = normalizedHomeDir + normalizedElement[1:] normalizedElement = strings.Replace(normalizedElement, "~", normalizedHomeDir, 1)
} }
pattern := fmt.Sprintf("^%s$", normalizedElement) pattern := fmt.Sprintf("^%s$", normalizedElement)
goos := env.getRuntimeGOOS() goos := env.getRuntimeGOOS()

View file

@ -56,7 +56,7 @@ func (s *scm) init(props properties, env environmentInfo) {
func (s *scm) truncateBranch(branch string) string { func (s *scm) truncateBranch(branch string) string {
fullBranchPath := s.props.getBool(FullBranchPath, true) fullBranchPath := s.props.getBool(FullBranchPath, true)
maxLength := s.props.getInt(BranchMaxLength, 0) maxLength := s.props.getInt(BranchMaxLength, 0)
if !fullBranchPath && len(branch) > 0 { if !fullBranchPath && strings.Contains(branch, "/") {
index := strings.LastIndex(branch, "/") index := strings.LastIndex(branch, "/")
branch = branch[index+1:] branch = branch[index+1:]
} }

View file

@ -69,10 +69,13 @@ func (a *aws) getConfigFileInfo() {
continue continue
} }
if sectionActive && strings.HasPrefix(line, "region") { if sectionActive && strings.HasPrefix(line, "region") {
a.Region = strings.TrimSpace(strings.Split(line, "=")[1]) splitted := strings.Split(line, "=")
if len(splitted) >= 2 {
a.Region = strings.TrimSpace(splitted[1])
break break
} }
} }
}
if a.Profile == "" && a.Region != "" { if a.Profile == "" && a.Region != "" {
a.Profile = defaultUser a.Profile = defaultUser
} }

View file

@ -153,8 +153,10 @@ func (t *executiontime) formatDurationHouston() string {
// format milliseconds as a string with truncated trailing zeros // format milliseconds as a string with truncated trailing zeros
milliseconds = strconv.FormatFloat(float64(t.Ms%second)/second, 'f', -1, 64) milliseconds = strconv.FormatFloat(float64(t.Ms%second)/second, 'f', -1, 64)
// at this point milliseconds looks like "0.5". remove the leading "0" // at this point milliseconds looks like "0.5". remove the leading "0"
if len(milliseconds) >= 1 {
milliseconds = milliseconds[1:] milliseconds = milliseconds[1:]
} }
}
result := fmt.Sprintf("%02d:%02d:%02d%s", t.Ms/hour, t.Ms/minute%minutesPerHour, t.Ms%minute/second, milliseconds) result := fmt.Sprintf("%02d:%02d:%02d%s", t.Ms/hour, t.Ms/minute%minutesPerHour, t.Ms%minute/second, milliseconds)
return result return result
@ -175,8 +177,10 @@ func (t *executiontime) formatDurationAmarillo() string {
decimalResult := strconv.FormatFloat(decimalNumber, 'f', -1, 64) decimalResult := strconv.FormatFloat(decimalNumber, 'f', -1, 64)
// at this point decimalResult looks like "0.5" // at this point decimalResult looks like "0.5"
// remove the leading "0" and append // remove the leading "0" and append
if len(decimalResult) >= 1 {
result += decimalResult[1:] result += decimalResult[1:]
} }
}
result += "s" result += "s"
return result return result

View file

@ -247,24 +247,26 @@ func (g *git) setGitStatus() {
g.Staging = &GitStatus{} g.Staging = &GitStatus{}
output := g.getGitCommandOutput("status", "-unormal", "--branch", "--porcelain=2") output := g.getGitCommandOutput("status", "-unormal", "--branch", "--porcelain=2")
for _, line := range strings.Split(output, "\n") { 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] g.Hash = line[len(HASH) : len(HASH)+7]
continue continue
} }
if strings.HasPrefix(line, REF) { if strings.HasPrefix(line, REF) && len(line) > len(REF) {
g.Ref = line[len(REF):] g.Ref = line[len(REF):]
continue continue
} }
if strings.HasPrefix(line, UPSTREAM) { if strings.HasPrefix(line, UPSTREAM) && len(line) > len(UPSTREAM) {
g.Upstream = line[len(UPSTREAM):] g.Upstream = line[len(UPSTREAM):]
continue continue
} }
if strings.HasPrefix(line, BRANCHSTATUS) { if strings.HasPrefix(line, BRANCHSTATUS) && len(line) > len(BRANCHSTATUS) {
status := line[len(BRANCHSTATUS):] status := line[len(BRANCHSTATUS):]
splitted := strings.Split(status, " ") splitted := strings.Split(status, " ")
if len(splitted) >= 2 {
g.Ahead, _ = strconv.Atoi(splitted[0]) g.Ahead, _ = strconv.Atoi(splitted[0])
behind, _ := strconv.Atoi(splitted[1]) behind, _ := strconv.Atoi(splitted[1])
g.Behind = -behind g.Behind = -behind
}
continue continue
} }
addToStatus(line) addToStatus(line)

View file

@ -235,7 +235,11 @@ func (l *language) buildVersionURL(text string) string {
if n == 0 { if n == 0 {
return fmt.Sprintf(str, args...), nil 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) version, err := truncatingSprintf(l.versionURLTemplate, text, l.version.Major, l.version.Minor, l.version.Patch)
if err != nil { if err != nil {

View file

@ -2,6 +2,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
) )
@ -122,10 +123,14 @@ func (d *owm) setStatus() error {
if err != nil { if err != nil {
return err return err
} }
if len(q.Data) == 0 {
return errors.New("No data found")
}
id := q.Data[0].TypeID
d.Temperature = q.temperature.Value d.Temperature = q.temperature.Value
icon := "" icon := ""
switch q.Data[0].TypeID { switch id {
case "01n": case "01n":
fallthrough fallthrough
case "01d": case "01d":

View file

@ -206,7 +206,9 @@ func (pt *path) getLetterPath() string {
buffer.WriteString(fmt.Sprintf("%s%s", letter, separator)) buffer.WriteString(fmt.Sprintf("%s%s", letter, separator))
} }
if len(splitted) > 0 {
buffer.WriteString(splitted[len(splitted)-1]) buffer.WriteString(splitted[len(splitted)-1])
}
return buffer.String() return buffer.String()
} }