From dac9c0dae68ec0e918f0e648b11263b6d779dd42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sat, 15 Aug 2015 15:47:16 +0200 Subject: [PATCH] Use cast.ToIntE for int conversions in substr and slicestr It is less restrictive, and it is what is used in other template funcs. --- tpl/template_funcs.go | 38 ++++++++++---------------------------- tpl/template_funcs_test.go | 6 +++--- 2 files changed, 13 insertions(+), 31 deletions(-) diff --git a/tpl/template_funcs.go b/tpl/template_funcs.go index 4b9f70111..e8e2e6d86 100644 --- a/tpl/template_funcs.go +++ b/tpl/template_funcs.go @@ -128,24 +128,6 @@ func compareGetFloat(a interface{}, b interface{}) (float64, float64) { return left, right } -// Taken out from Substr, to be used by Slicestr too. -func toInt(v interface{}, message string) (int, error) { - switch i := v.(type) { - case int: - return i, nil - case int8: - return int(i), nil - case int16: - return int(i), nil - case int32: - return int(i), nil - case int64: - return int(i), nil - default: - return 0, errors.New(message) - } -} - // Slicing in Slicestr is done by specifying a half-open range with // two indices, start and end. 1 and 4 creates a slice including elements 1 through 3. // The end index can be omitted, it defaults to the string's length. @@ -160,13 +142,13 @@ func Slicestr(a interface{}, startEnd ...interface{}) (string, error) { argNum := len(startEnd) if argNum > 0 { - if argStart, err = toInt(startEnd[0], "start argument must be integer"); err != nil { - return "", err + if argStart, err = cast.ToIntE(startEnd[0]); err != nil { + return "", errors.New("start argument must be integer") } } if argNum > 1 { - if argEnd, err = toInt(startEnd[1], "end argument must be integer"); err != nil { - return "", err + if argEnd, err = cast.ToIntE(startEnd[1]); err != nil { + return "", errors.New("end argument must be integer") } } @@ -219,16 +201,16 @@ func Substr(a interface{}, nums ...interface{}) (string, error) { case 0: return "", errors.New("too less arguments") case 1: - if start, err = toInt(nums[0], "start argument must be integer"); err != nil { - return "", err + if start, err = cast.ToIntE(nums[0]); err != nil { + return "", errors.New("start argument must be integer") } length = len(asRunes) case 2: - if start, err = toInt(nums[0], "start argument must be integer"); err != nil { - return "", err + if start, err = cast.ToIntE(nums[0]); err != nil { + return "", errors.New("start argument must be integer") } - if length, err = toInt(nums[1], "length argument must be integer"); err != nil { - return "", err + if length, err = cast.ToIntE(nums[1]); err != nil { + return "", errors.New("length argument must be integer") } default: return "", errors.New("too many arguments") diff --git a/tpl/template_funcs_test.go b/tpl/template_funcs_test.go index 585702ae7..3dbf1b0b9 100644 --- a/tpl/template_funcs_test.go +++ b/tpl/template_funcs_test.go @@ -437,9 +437,9 @@ func TestSubstr(t *testing.T) { {123, 1, 3, "23"}, {1.2e3, 0, 4, "1200"}, {tstNoStringer{}, 0, 1, false}, - {"abcdef", 2.0, nil, false}, - {"abcdef", 2.0, 2, false}, - {"abcdef", 2, 2.0, false}, + {"abcdef", 2.0, nil, "cdef"}, + {"abcdef", 2.0, 2, "cd"}, + {"abcdef", 2, 2.0, "cd"}, {"ĀĀĀ", 1, 2, "ĀĀ"}, // # issue 1333 } { var result string