mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-10-30 09:23:09 +00:00
funcs: Add reverse
This commit is contained in:
parent
da78298c3e
commit
07a54e89dd
14
funcs.go
14
funcs.go
|
@ -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()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue