funcs: Add reverse

This commit is contained in:
Adnan Maolood 2021-05-12 17:10:19 -04:00
parent da78298c3e
commit 07a54e89dd

View file

@ -3,11 +3,13 @@ package main
import ( import (
"html/template" "html/template"
"path" "path"
"reflect"
"strings" "strings"
) )
var funcs = map[string]interface{}{ var funcs = map[string]interface{}{
"path": func() _path { return _path{} }, "path": func() _path { return _path{} },
"reverse": reverse,
"strings": func() _strings { return _strings{} }, "strings": func() _strings { return _strings{} },
"safeHTML": func(s string) template.HTML { return template.HTML(s) }, "safeHTML": func(s string) template.HTML { return template.HTML(s) },
"safeHTMLAttr": func(s string) template.HTMLAttr { return template.HTMLAttr(s) }, "safeHTMLAttr": func(s string) template.HTMLAttr { return template.HTMLAttr(s) },
@ -44,3 +46,15 @@ func (_strings) TrimPrefix(a, b string) string { return strings.TrimPrefi
func (_strings) TrimRight(a, b string) string { return strings.TrimRight(a, b) } func (_strings) TrimRight(a, b string) string { return strings.TrimRight(a, b) }
func (_strings) TrimSpace(s string) string { return strings.TrimSpace(s) } func (_strings) TrimSpace(s string) string { return strings.TrimSpace(s) }
func (_strings) TrimSuffix(a, b string) string { return strings.TrimSuffix(a, b) } func (_strings) TrimSuffix(a, b string) string { return strings.TrimSuffix(a, b) }
func reverse(s interface{}) interface{} {
v := reflect.ValueOf(s)
n := v.Len()
r := reflect.MakeSlice(v.Type(), n, n)
reflect.Copy(r, v)
swap := reflect.Swapper(r.Interface())
for i, j := 0, n-1; i < j; i, j = i+1, j-1 {
swap(i, j)
}
return r.Interface()
}