Use cast.ToIntE for int conversions in substr and slicestr - hugo - [fork] hugo port for 9front
HTML git clone git@git.drkhsh.at/hugo.git
DIR Log
DIR Files
DIR Refs
DIR Submodules
DIR README
DIR LICENSE
---
DIR commit dac9c0dae68ec0e918f0e648b11263b6d779dd42
DIR parent 5b51b3b9fb19d8d20e99d3c249c0052793abe50a
HTML Author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Date: Sat, 15 Aug 2015 15:47:16 +0200
Use cast.ToIntE for int conversions in substr and slicestr
It is less restrictive, and it is what is used in other template funcs.
Diffstat:
M tpl/template_funcs.go | 38 ++++++++-----------------------
M tpl/template_funcs_test.go | 6 +++---
2 files changed, 13 insertions(+), 31 deletions(-)
---
DIR diff --git 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")
DIR diff --git 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