1
0
mirror of https://git.sr.ht/~adnano/kiln synced 2024-12-04 19:08:22 +01:00

funcs: Add reverse

This commit is contained in:
adnano 2021-05-12 17:10:19 -04:00
parent 636a41126b
commit b6614b71da

@ -3,11 +3,13 @@ package main
import (
"html/template"
"path"
"reflect"
"strings"
)
var funcs = map[string]interface{}{
"path": func() _path { return _path{} },
"reverse": reverse,
"strings": func() _strings { return _strings{} },
"safeHTML": func(s string) template.HTML { return template.HTML(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) TrimSpace(s string) string { return strings.TrimSpace(s) }
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()
}