Rename Substr to Slice
authorbep <bjorn.erik.pedersen@gmail.com>
Mon, 23 Mar 2015 11:18:34 +0000 (12:18 +0100)
committerbep <bjorn.erik.pedersen@gmail.com>
Mon, 23 Mar 2015 11:18:27 +0000 (12:18 +0100)
That is whas was implemented, not Substr.

Also make the API more similar to Go's internal slice by making both the start and end indices optional.

See #990

tpl/template.go
tpl/template_test.go

index 7e9f41f1f66c622e7d917ae36728e288f243e09c..1f899faf736b0291ee9a5eb6749cea32ec83b19d 100644 (file)
@@ -188,12 +188,25 @@ func compareGetFloat(a interface{}, b interface{}) (float64, float64) {
        return left, right
 }
 
-func Substr(a interface{}, pos, length int) (string, error) {
+// Slicing in Slice is done by specifying a half-open range with
+// two indices, lower and upper. 1 and 4 creates a slice including elements 1 through 3.
+// The start and/or end indices can be omitted by setting one or both of them to -1; they default to zero and the slice's length respectively
+func Slice(a interface{}, start, end int) (string, error) {
        aStr, err := cast.ToStringE(a)
        if err != nil {
                return "", err
        }
-       return aStr[pos:length], nil
+
+       if start != -1 && end != -1 {
+               return aStr[start:end], nil
+       } else if start == -1 && end == -1 {
+               return aStr[:], nil
+       } else if start == -1 {
+               return aStr[:end], nil
+       } else {
+               return aStr[start:], nil
+       }
+
 }
 
 func Split(a interface{}, delimiter string) ([]string, error) {
@@ -1324,7 +1337,7 @@ func init() {
                "lt":          Lt,
                "le":          Le,
                "in":          In,
-               "substr":      Substr,
+               "slice":       Slice,
                "split":       Split,
                "intersect":   Intersect,
                "isSet":       IsSet,
index dca4711d16480d8bd4055bb2b35ab221bd3c4b8b..55f739576a5368cd07519e67fab97e3d7c25468b 100644 (file)
@@ -276,7 +276,7 @@ func TestIn(t *testing.T) {
        }
 }
 
-func TestSubstr(t *testing.T) {
+func TestSlice(t *testing.T) {
        for i, this := range []struct {
                v1     interface{}
                v2     int
@@ -286,14 +286,17 @@ func TestSubstr(t *testing.T) {
                {"abc", 1, 2, "b"},
                {"abc", 1, 3, "bc"},
                {"abc", 0, 1, "a"},
+               {"abcdef", -1, -1, "abcdef"},
+               {"abcdef", -1, 2, "ab"},
+               {"abcdef", 2, -1, "cdef"},
                {123, 1, 3, "23"},
                {tstNoStringer{}, 0, 1, false},
        } {
-               result, err := Substr(this.v1, this.v2, this.v3)
+               result, err := Slice(this.v1, this.v2, this.v3)
 
                if b, ok := this.expect.(bool); ok && !b {
                        if err == nil {
-                               t.Errorf("[%d] Substr didn't return an expected error", i)
+                               t.Errorf("[%d] Slice didn't return an expected error", i)
                        }
                } else {
                        if err != nil {