refactor: append, build variable and type switch (#4940)

* refactor: append, build variable and type switch

* fix: remove redundant space.
This commit is contained in:
Bo-Yi Wu 2019-05-28 23:45:54 +08:00 committed by GitHub
parent 31557b1274
commit 743697a549
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 42 additions and 46 deletions

View file

@ -465,41 +465,41 @@ func Subtract(left interface{}, right interface{}) interface{} {
var rleft, rright int64
var fleft, fright float64
var isInt = true
switch left := left.(type) {
switch v := left.(type) {
case int:
rleft = int64(left)
rleft = int64(v)
case int8:
rleft = int64(left)
rleft = int64(v)
case int16:
rleft = int64(left)
rleft = int64(v)
case int32:
rleft = int64(left)
rleft = int64(v)
case int64:
rleft = left
rleft = v
case float32:
fleft = float64(left)
fleft = float64(v)
isInt = false
case float64:
fleft = left
fleft = v
isInt = false
}
switch right := right.(type) {
switch v := right.(type) {
case int:
rright = int64(right)
rright = int64(v)
case int8:
rright = int64(right)
rright = int64(v)
case int16:
rright = int64(right)
rright = int64(v)
case int32:
rright = int64(right)
rright = int64(v)
case int64:
rright = right
rright = v
case float32:
fright = float64(right)
fright = float64(v)
isInt = false
case float64:
fright = right
fright = v
isInt = false
}

View file

@ -306,21 +306,21 @@ func TestFileSize(t *testing.T) {
func TestSubtract(t *testing.T) {
toFloat64 := func(n interface{}) float64 {
switch n := n.(type) {
switch v := n.(type) {
case int:
return float64(n)
return float64(v)
case int8:
return float64(n)
return float64(v)
case int16:
return float64(n)
return float64(v)
case int32:
return float64(n)
return float64(v)
case int64:
return float64(n)
return float64(v)
case float32:
return float64(n)
return float64(v)
case float64:
return n
return v
default:
return 0.0
}

View file

@ -156,8 +156,7 @@ func NewFuncMap() []template.FuncMap {
var path []string
index := strings.LastIndex(str, "/")
if index != -1 && index != len(str) {
path = append(path, str[0:index+1])
path = append(path, str[index+1:])
path = append(path, str[0:index+1], str[index+1:])
} else {
path = append(path, str)
}
@ -330,10 +329,10 @@ func ToUTF8(content string) string {
return res
}
// ReplaceLeft replaces all prefixes 'old' in 's' with 'new'.
func ReplaceLeft(s, old, new string) string {
oldLen, newLen, i, n := len(old), len(new), 0, 0
for ; i < len(s) && strings.HasPrefix(s[i:], old); n++ {
// ReplaceLeft replaces all prefixes 'oldS' in 's' with 'newS'.
func ReplaceLeft(s, oldS, newS string) string {
oldLen, newLen, i, n := len(oldS), len(newS), 0, 0
for ; i < len(s) && strings.HasPrefix(s[i:], oldS); n++ {
i += oldLen
}
@ -348,7 +347,7 @@ func ReplaceLeft(s, old, new string) string {
j := 0
for ; j < n*newLen; j += newLen {
copy(replacement[j:j+newLen], new)
copy(replacement[j:j+newLen], newS)
}
copy(replacement[j:], s[i:])