Compare commits
No commits in common. "develop" and "v0.1.0" have entirely different histories.
|
@ -2,11 +2,9 @@ package misc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"fmt"
|
|
||||||
"math"
|
|
||||||
"image"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func mustParseInt(value string, bits int) int64 {
|
func mustParseInt(value string, bits int) int64 {
|
||||||
if parsed, err := strconv.ParseInt(value, 10, bits); err != nil {
|
if parsed, err := strconv.ParseInt(value, 10, bits); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -126,69 +124,3 @@ func FormatInt16(value int16) string { return strconv.FormatInt(int64(value), 10
|
||||||
func FormatInt32(value int32) string { return strconv.FormatInt(int64(value), 10) }
|
func FormatInt32(value int32) string { return strconv.FormatInt(int64(value), 10) }
|
||||||
func FormatInt64(value int64) string { return strconv.FormatInt(value, 10) }
|
func FormatInt64(value int64) string { return strconv.FormatInt(value, 10) }
|
||||||
func FormatInt(value int) string { return strconv.FormatInt(int64(value), 10) }
|
func FormatInt(value int) string { return strconv.FormatInt(int64(value), 10) }
|
||||||
|
|
||||||
var (
|
|
||||||
sizesIEC = []string{
|
|
||||||
"B",
|
|
||||||
"KiB",
|
|
||||||
"MiB",
|
|
||||||
"GiB",
|
|
||||||
"TiB",
|
|
||||||
"PiB",
|
|
||||||
"EiB",
|
|
||||||
"ZiB",
|
|
||||||
"YiB",
|
|
||||||
}
|
|
||||||
sizes = []string{
|
|
||||||
"B",
|
|
||||||
"KB",
|
|
||||||
"MB",
|
|
||||||
"GB",
|
|
||||||
"TB",
|
|
||||||
"PB",
|
|
||||||
"EB",
|
|
||||||
"ZB",
|
|
||||||
"YB",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
func logn(n, b float64) float64 {
|
|
||||||
return math.Log(n) / math.Log(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
func humanateBytes(s uint64, base float64, sizes []string) string {
|
|
||||||
if s < 10 {
|
|
||||||
return fmt.Sprintf("%dB", s)
|
|
||||||
}
|
|
||||||
e := math.Floor(logn(float64(s), base))
|
|
||||||
suffix := sizes[int(e)]
|
|
||||||
val := float64(s) / math.Pow(base, math.Floor(e))
|
|
||||||
f := "%.0f"
|
|
||||||
if val < 10 {
|
|
||||||
f = "%.1f"
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprintf(f+"%s", val, suffix)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FileSize calculates the file size and generate user-friendly string.
|
|
||||||
func FileSizeIEC(s uint64) string {
|
|
||||||
return humanateBytes(s, 1024, sizesIEC)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FileSize calculates the file size and generate user-friendly string.
|
|
||||||
func FileSize(s uint64) string {
|
|
||||||
return humanateBytes(s, 1000, sizes)
|
|
||||||
}
|
|
||||||
func AspectRatio(srcRect image.Point, toResize uint64) image.Point {
|
|
||||||
w, h := int(toResize), getRatioSize(int(toResize), srcRect.Y, srcRect.X)
|
|
||||||
if srcRect.X < srcRect.Y {
|
|
||||||
w, h = getRatioSize(int(toResize), srcRect.X, srcRect.Y), int(toResize)
|
|
||||||
}
|
|
||||||
return image.Point{w, h}
|
|
||||||
}
|
|
||||||
|
|
||||||
func getRatioSize(a, b, c int) int {
|
|
||||||
d := a * b / c
|
|
||||||
return (d + 1) & -1
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,23 +0,0 @@
|
||||||
package tmpl
|
|
||||||
|
|
||||||
import (
|
|
||||||
"reflect"
|
|
||||||
_ "unsafe"
|
|
||||||
)
|
|
||||||
|
|
||||||
//go:linkname indirectInterface text/template.indirectInterface
|
|
||||||
//go:nosplit
|
|
||||||
func indirectInterface(v reflect.Value) reflect.Value
|
|
||||||
|
|
||||||
//go:linkname truth text/template.truth
|
|
||||||
//go:nosplit
|
|
||||||
func truth(arg reflect.Value) bool
|
|
||||||
|
|
||||||
//go:linkname isTrue text/template.isTrue
|
|
||||||
//go:nosplit
|
|
||||||
func isTrue(arg reflect.Value) (truth, ok bool)
|
|
||||||
|
|
||||||
func evalTrue(val reflect.Value) (truth bool) {
|
|
||||||
truth, _ = isTrue(val)
|
|
||||||
return
|
|
||||||
}
|
|
66
tmpl/map.go
66
tmpl/map.go
|
@ -1,66 +0,0 @@
|
||||||
package tmpl
|
|
||||||
|
|
||||||
import (
|
|
||||||
"reflect"
|
|
||||||
_ "unsafe"
|
|
||||||
)
|
|
||||||
|
|
||||||
type TmplMap struct {
|
|
||||||
reflect.Value
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m TmplMap) Keys() ([]reflect.Value) {
|
|
||||||
return m.MapKeys()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m TmplMap) MapWithDefaults(arg0 reflect.Value, args ...reflect.Value) (v reflect.Value) {
|
|
||||||
if iarg0 := indirectInterface(arg0); evalTrue(iarg0) {
|
|
||||||
if v = m.MapIndex(iarg0); evalTrue(v) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, v = range args {
|
|
||||||
if truth(v) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m TmplMap) Map(arg0 reflect.Value, args ...reflect.Value) (v reflect.Value) {
|
|
||||||
if iarg0 := indirectInterface(arg0); evalTrue(iarg0) {
|
|
||||||
if v = m.MapIndex(iarg0); evalTrue(v) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
argLen := len(args)
|
|
||||||
if argLen == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
keys, defaultValue := args[:argLen-1], args[argLen-1]
|
|
||||||
for _, k := range keys {
|
|
||||||
if ik := indirectInterface(k); !evalTrue(ik) {
|
|
||||||
} else if v = m.Value.MapIndex(ik); evalTrue(v) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
v = defaultValue
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func MakeMapFunc(arg ...reflect.Value) TmplMap {
|
|
||||||
if len(arg)%2 != 0 {
|
|
||||||
panic("bad makemap")
|
|
||||||
}
|
|
||||||
|
|
||||||
m := reflect.MakeMapWithSize(reflect.TypeOf(map[interface{}]interface{}{}), len(arg)/2)
|
|
||||||
for i := 0; i < len(arg); i += 2 {
|
|
||||||
k, v := indirectInterface(arg[i]), indirectInterface(arg[i+1])
|
|
||||||
if !(evalTrue(k) && evalTrue(v)) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
m.SetMapIndex(k, v)
|
|
||||||
}
|
|
||||||
return TmplMap{m}
|
|
||||||
}
|
|
|
@ -1 +0,0 @@
|
||||||
// make compiler happy
|
|
Reference in New Issue