summaryrefslogtreecommitdiffstats
path: root/helpers/general.go
diff options
context:
space:
mode:
authorCameron Moore <moorereason@gmail.com>2017-03-13 17:55:02 -0500
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2017-04-30 10:56:38 +0200
commitde7c32a1a880820252e922e0c9fcf69e109c0d1b (patch)
tree07d813f2617dd4a889aaebb885a9c1281a229960 /helpers/general.go
parent154e18ddb9ad205055d5bd4827c87f3f0daf499f (diff)
tpl: Add template function namespaces
This commit moves almost all of the template functions into separate packages under tpl/ and adds a namespace framework. All changes should be backward compatible for end users, as all existing function names in the template funcMap are left intact. Seq and DoArithmatic have been moved out of the helpers package and into template namespaces. Most of the tests involved have been refactored, and many new tests have been written. There's still work to do, but this is a big improvement. I got a little overzealous and added some new functions along the way: - strings.Contains - strings.ContainsAny - strings.HasSuffix - strings.TrimPrefix - strings.TrimSuffix Documentation is forthcoming. Fixes #3042
Diffstat (limited to 'helpers/general.go')
-rw-r--r--helpers/general.go194
1 files changed, 0 insertions, 194 deletions
diff --git a/helpers/general.go b/helpers/general.go
index ea3620119..4fd91133b 100644
--- a/helpers/general.go
+++ b/helpers/general.go
@@ -17,12 +17,10 @@ import (
"bytes"
"crypto/md5"
"encoding/hex"
- "errors"
"fmt"
"io"
"net"
"path/filepath"
- "reflect"
"strings"
"sync"
"unicode"
@@ -320,198 +318,6 @@ func IsWhitespace(r rune) bool {
return r == ' ' || r == '\t' || r == '\n' || r == '\r'
}
-// Seq creates a sequence of integers.
-// It's named and used as GNU's seq.
-// Examples:
-// 3 => 1, 2, 3
-// 1 2 4 => 1, 3
-// -3 => -1, -2, -3
-// 1 4 => 1, 2, 3, 4
-// 1 -2 => 1, 0, -1, -2
-func Seq(args ...interface{}) ([]int, error) {
- if len(args) < 1 || len(args) > 3 {
- return nil, errors.New("Seq, invalid number of args: 'first' 'increment' (optional) 'last' (optional)")
- }
-
- intArgs := cast.ToIntSlice(args)
-
- if len(intArgs) < 1 || len(intArgs) > 3 {
- return nil, errors.New("Invalid argument(s) to Seq")
- }
-
- var inc = 1
- var last int
- var first = intArgs[0]
-
- if len(intArgs) == 1 {
- last = first
- if last == 0 {
- return []int{}, nil
- } else if last > 0 {
- first = 1
- } else {
- first = -1
- inc = -1
- }
- } else if len(intArgs) == 2 {
- last = intArgs[1]
- if last < first {
- inc = -1
- }
- } else {
- inc = intArgs[1]
- last = intArgs[2]
- if inc == 0 {
- return nil, errors.New("'increment' must not be 0")
- }
- if first < last && inc < 0 {
- return nil, errors.New("'increment' must be > 0")
- }
- if first > last && inc > 0 {
- return nil, errors.New("'increment' must be < 0")
- }
- }
-
- // sanity check
- if last < -100000 {
- return nil, errors.New("size of result exceeds limit")
- }
- size := ((last - first) / inc) + 1
-
- // sanity check
- if size <= 0 || size > 2000 {
- return nil, errors.New("size of result exceeds limit")
- }
-
- seq := make([]int, size)
- val := first
- for i := 0; ; i++ {
- seq[i] = val
- val += inc
- if (inc < 0 && val < last) || (inc > 0 && val > last) {
- break
- }
- }
-
- return seq, nil
-}
-
-// DoArithmetic performs arithmetic operations (+,-,*,/) using reflection to
-// determine the type of the two terms.
-func DoArithmetic(a, b interface{}, op rune) (interface{}, error) {
- av := reflect.ValueOf(a)
- bv := reflect.ValueOf(b)
- var ai, bi int64
- var af, bf float64
- var au, bu uint64
- switch av.Kind() {
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- ai = av.Int()
- switch bv.Kind() {
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- bi = bv.Int()
- case reflect.Float32, reflect.Float64:
- af = float64(ai) // may overflow
- ai = 0
- bf = bv.Float()
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
- bu = bv.Uint()
- if ai >= 0 {
- au = uint64(ai)
- ai = 0
- } else {
- bi = int64(bu) // may overflow
- bu = 0
- }
- default:
- return nil, errors.New("Can't apply the operator to the values")
- }
- case reflect.Float32, reflect.Float64:
- af = av.Float()
- switch bv.Kind() {
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- bf = float64(bv.Int()) // may overflow
- case reflect.Float32, reflect.Float64:
- bf = bv.Float()
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
- bf = float64(bv.Uint()) // may overflow
- default:
- return nil, errors.New("Can't apply the operator to the values")
- }
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
- au = av.Uint()
- switch bv.Kind() {
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- bi = bv.Int()
- if bi >= 0 {
- bu = uint64(bi)
- bi = 0
- } else {
- ai = int64(au) // may overflow
- au = 0
- }
- case reflect.Float32, reflect.Float64:
- af = float64(au) // may overflow
- au = 0
- bf = bv.Float()
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
- bu = bv.Uint()
- default:
- return nil, errors.New("Can't apply the operator to the values")
- }
- case reflect.String:
- as := av.String()
- if bv.Kind() == reflect.String && op == '+' {
- bs := bv.String()
- return as + bs, nil
- }
- return nil, errors.New("Can't apply the operator to the values")
- default:
- return nil, errors.New("Can't apply the operator to the values")
- }
-
- switch op {
- case '+':
- if ai != 0 || bi != 0 {
- return ai + bi, nil
- } else if af != 0 || bf != 0 {
- return af + bf, nil
- } else if au != 0 || bu != 0 {
- return au + bu, nil
- }
- return 0, nil
- case '-':
- if ai != 0 || bi != 0 {
- return ai - bi, nil
- } else if af != 0 || bf != 0 {
- return af - bf, nil
- } else if au != 0 || bu != 0 {
- return au - bu, nil
- }
- return 0, nil
- case '*':
- if ai != 0 || bi != 0 {
- return ai * bi, nil
- } else if af != 0 || bf != 0 {
- return af * bf, nil
- } else if au != 0 || bu != 0 {
- return au * bu, nil
- }
- return 0, nil
- case '/':
- if bi != 0 {
- return ai / bi, nil
- } else if bf != 0 {
- return af / bf, nil
- } else if bu != 0 {
- return au / bu, nil
- }
- return nil, errors.New("Can't divide the value by 0")
- default:
- return nil, errors.New("There is no such an operation")
- }
-}
-
// NormalizeHugoFlags facilitates transitions of Hugo command-line flags,
// e.g. --baseUrl to --baseURL, --uglyUrls to --uglyURLs
func NormalizeHugoFlags(f *pflag.FlagSet, name string) pflag.NormalizedName {