infra
/
goutils
Archived
1
0
Fork 0

Compare commits

...

3 Commits

Author SHA1 Message Date
Sangbum Kim 08ec19535d template 함수 추가 2018-07-13 01:29:26 +09:00
Sangbum Kim c9fbc24e51 용량 계산 로직 수정 2018-07-06 14:19:56 +09:00
Sangbum Kim f673c30fb4 용량 컨버전 로직 추가 2018-07-06 14:03:30 +09:00
4 changed files with 159 additions and 1 deletions

View File

@ -2,9 +2,11 @@ package misc
import (
"strconv"
"fmt"
"math"
"image"
)
func mustParseInt(value string, bits int) int64 {
if parsed, err := strconv.ParseInt(value, 10, bits); err != nil {
panic(err)
@ -124,3 +126,69 @@ func FormatInt16(value int16) 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 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
}

23
tmpl/internal.go Normal file
View File

@ -0,0 +1,23 @@
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 Normal file
View File

@ -0,0 +1,66 @@
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
tmpl/stub.s Normal file
View File

@ -0,0 +1 @@
// make compiler happy