Skip to content

Commit 121c465

Browse files
committed
Simplify value.str() and .num() logic
Slight speedup if anything, but possibly just noise
1 parent 343871b commit 121c465

1 file changed

Lines changed: 14 additions & 20 deletions

File tree

interp/value.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,43 +81,37 @@ func (v value) boolean() bool {
8181
// format if a number value. Integers are a special case and don't
8282
// use floatFormat.
8383
func (v value) str(floatFormat string) string {
84-
switch v.typ {
85-
case typeNum:
86-
if math.IsNaN(v.n) {
84+
if v.typ == typeNum {
85+
switch {
86+
case math.IsNaN(v.n):
8787
return "nan"
88-
} else if math.IsInf(v.n, 0) {
88+
case math.IsInf(v.n, 0):
8989
if v.n < 0 {
9090
return "-inf"
9191
} else {
9292
return "inf"
9393
}
94-
} else if v.n == float64(int(v.n)) {
94+
case v.n == float64(int(v.n)):
9595
return strconv.Itoa(int(v.n))
96-
} else {
96+
default:
9797
return fmt.Sprintf(floatFormat, v.n)
9898
}
99-
case typeStr, typeNumStr:
100-
return v.s
101-
default:
102-
return ""
10399
}
100+
// For typeStr and typeStrNum we already have the string, for
101+
// typeNull v.s == "".
102+
return v.s
104103
}
105104

106105
// Return value's number value, converting from string if necessary
107106
func (v value) num() float64 {
108-
switch v.typ {
109-
case typeNum:
110-
return v.n
111-
case typeStr:
107+
if v.typ == typeStr {
112108
// Ensure string starts with a float and convert it
113109
return parseFloatPrefix(v.s)
114-
case typeNumStr:
115-
// If it's a numeric string, we already have the float value
116-
// from the numStr() call
117-
return v.n
118-
default:
119-
return 0
120110
}
111+
// Handle case for typeNum and typeStrNum. If it's a numeric
112+
// string, we already have the float value from the numStr()
113+
// call. For typeNull v.n == 0.
114+
return v.n
121115
}
122116

123117
// Like strconv.ParseFloat, but parses at the start of string and

0 commit comments

Comments
 (0)