195 lines
4.8 KiB
Go
195 lines
4.8 KiB
Go
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)
|
|
} else {
|
|
return parsed
|
|
}
|
|
}
|
|
func mustParseUint(value string, bits int) uint64 {
|
|
if parsed, err := strconv.ParseUint(value, 10, bits); err != nil {
|
|
panic(err)
|
|
} else {
|
|
return parsed
|
|
}
|
|
}
|
|
|
|
func ParseUint8(value string) (ret *uint8) {
|
|
|
|
if parsed, err := strconv.ParseUint(value, 10, 8); err == nil {
|
|
ret = new(uint8)
|
|
*ret = uint8(parsed)
|
|
}
|
|
return
|
|
}
|
|
func ParseUint16(value string) (ret *uint16) {
|
|
|
|
if parsed, err := strconv.ParseUint(value, 10, 16); err == nil {
|
|
ret = new(uint16)
|
|
*ret = uint16(parsed)
|
|
}
|
|
return
|
|
}
|
|
func ParseUint32(value string) (ret *uint32) {
|
|
|
|
if parsed, err := strconv.ParseUint(value, 10, 32); err == nil {
|
|
ret = new(uint32)
|
|
*ret = uint32(parsed)
|
|
}
|
|
return
|
|
}
|
|
func ParseUint64(value string) (ret *uint64) {
|
|
|
|
if parsed, err := strconv.ParseUint(value, 10, 64); err == nil {
|
|
ret = new(uint64)
|
|
*ret = uint64(parsed)
|
|
}
|
|
return
|
|
}
|
|
func ParseUint(value string) (ret *uint) {
|
|
|
|
if parsed, err := strconv.ParseUint(value, 10, 0); err == nil {
|
|
ret = new(uint)
|
|
*ret = uint(parsed)
|
|
}
|
|
return
|
|
}
|
|
|
|
func ParseInt8(value string) (ret *int8) {
|
|
|
|
if parsed, err := strconv.ParseInt(value, 10, 8); err == nil {
|
|
ret = new(int8)
|
|
*ret = int8(parsed)
|
|
}
|
|
return
|
|
}
|
|
func ParseInt16(value string) (ret *int16) {
|
|
|
|
if parsed, err := strconv.ParseInt(value, 10, 16); err == nil {
|
|
ret = new(int16)
|
|
*ret = int16(parsed)
|
|
}
|
|
return
|
|
}
|
|
func ParseInt32(value string) (ret *int32) {
|
|
|
|
if parsed, err := strconv.ParseInt(value, 10, 32); err == nil {
|
|
ret = new(int32)
|
|
*ret = int32(parsed)
|
|
}
|
|
return
|
|
}
|
|
func ParseInt64(value string) (ret *int64) {
|
|
|
|
if parsed, err := strconv.ParseInt(value, 10, 64); err == nil {
|
|
ret = new(int64)
|
|
*ret = int64(parsed)
|
|
}
|
|
return
|
|
}
|
|
func ParseInt(value string) (ret *int) {
|
|
if parsed, err := strconv.ParseInt(value, 10, 0); err == nil {
|
|
ret = new(int)
|
|
*ret = int(parsed)
|
|
}
|
|
return
|
|
}
|
|
|
|
func ParseUint8Must(value string) uint8 { return uint8(mustParseUint(value, 8)) }
|
|
func ParseUint16Must(value string) uint16 { return uint16(mustParseUint(value, 16)) }
|
|
func ParseUint32Must(value string) uint32 { return uint32(mustParseUint(value, 32)) }
|
|
func ParseUint64Must(value string) uint64 { return mustParseUint(value, 64) }
|
|
func ParseUintMust(value string) uint { return uint(mustParseUint(value, 0)) }
|
|
|
|
func ParseInt8Must(value string) int8 { return int8(mustParseInt(value, 8)) }
|
|
func ParseInt16Must(value string) int16 { return int16(mustParseInt(value, 16)) }
|
|
func ParseInt32Must(value string) int32 { return int32(mustParseInt(value, 32)) }
|
|
func ParseInt64Must(value string) int64 { return mustParseInt(value, 64) }
|
|
func ParseIntMust(value string) int { return int(mustParseInt(value, 0)) }
|
|
|
|
func FormatUint8(value uint8) string { return strconv.FormatUint(uint64(value), 10) }
|
|
func FormatUint16(value uint16) string { return strconv.FormatUint(uint64(value), 10) }
|
|
func FormatUint32(value uint32) string { return strconv.FormatUint(uint64(value), 10) }
|
|
func FormatUint64(value uint64) string { return strconv.FormatUint(value, 10) }
|
|
func FormatUint(value uint) string { return strconv.FormatUint(uint64(value), 10) }
|
|
|
|
func FormatInt8(value int8) string { return strconv.FormatInt(int64(value), 10) }
|
|
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
|
|
}
|