amuz.es/src/infra/goutils@v0.1.3/misc/converse.go (about)

     1  package misc
     2  
     3  import (
     4  	"strconv"
     5  	"fmt"
     6  	"math"
     7  	"image"
     8  )
     9  
    10  func mustParseInt(value string, bits int) int64 {
    11  	if parsed, err := strconv.ParseInt(value, 10, bits); err != nil {
    12  		panic(err)
    13  	} else {
    14  		return parsed
    15  	}
    16  }
    17  func mustParseUint(value string, bits int) uint64 {
    18  	if parsed, err := strconv.ParseUint(value, 10, bits); err != nil {
    19  		panic(err)
    20  	} else {
    21  		return parsed
    22  	}
    23  }
    24  
    25  func ParseUint8(value string) (ret *uint8) {
    26  
    27  	if parsed, err := strconv.ParseUint(value, 10, 8); err == nil {
    28  		ret = new(uint8)
    29  		*ret = uint8(parsed)
    30  	}
    31  	return
    32  }
    33  func ParseUint16(value string) (ret *uint16) {
    34  
    35  	if parsed, err := strconv.ParseUint(value, 10, 16); err == nil {
    36  		ret = new(uint16)
    37  		*ret = uint16(parsed)
    38  	}
    39  	return
    40  }
    41  func ParseUint32(value string) (ret *uint32) {
    42  
    43  	if parsed, err := strconv.ParseUint(value, 10, 32); err == nil {
    44  		ret = new(uint32)
    45  		*ret = uint32(parsed)
    46  	}
    47  	return
    48  }
    49  func ParseUint64(value string) (ret *uint64) {
    50  
    51  	if parsed, err := strconv.ParseUint(value, 10, 64); err == nil {
    52  		ret = new(uint64)
    53  		*ret = uint64(parsed)
    54  	}
    55  	return
    56  }
    57  func ParseUint(value string) (ret *uint) {
    58  
    59  	if parsed, err := strconv.ParseUint(value, 10, 0); err == nil {
    60  		ret = new(uint)
    61  		*ret = uint(parsed)
    62  	}
    63  	return
    64  }
    65  
    66  func ParseInt8(value string) (ret *int8) {
    67  
    68  	if parsed, err := strconv.ParseInt(value, 10, 8); err == nil {
    69  		ret = new(int8)
    70  		*ret = int8(parsed)
    71  	}
    72  	return
    73  }
    74  func ParseInt16(value string) (ret *int16) {
    75  
    76  	if parsed, err := strconv.ParseInt(value, 10, 16); err == nil {
    77  		ret = new(int16)
    78  		*ret = int16(parsed)
    79  	}
    80  	return
    81  }
    82  func ParseInt32(value string) (ret *int32) {
    83  
    84  	if parsed, err := strconv.ParseInt(value, 10, 32); err == nil {
    85  		ret = new(int32)
    86  		*ret = int32(parsed)
    87  	}
    88  	return
    89  }
    90  func ParseInt64(value string) (ret *int64) {
    91  
    92  	if parsed, err := strconv.ParseInt(value, 10, 64); err == nil {
    93  		ret = new(int64)
    94  		*ret = int64(parsed)
    95  	}
    96  	return
    97  }
    98  func ParseInt(value string) (ret *int) {
    99  	if parsed, err := strconv.ParseInt(value, 10, 0); err == nil {
   100  		ret = new(int)
   101  		*ret = int(parsed)
   102  	}
   103  	return
   104  }
   105  
   106  func ParseUint8Must(value string) uint8   { return uint8(mustParseUint(value, 8)) }
   107  func ParseUint16Must(value string) uint16 { return uint16(mustParseUint(value, 16)) }
   108  func ParseUint32Must(value string) uint32 { return uint32(mustParseUint(value, 32)) }
   109  func ParseUint64Must(value string) uint64 { return mustParseUint(value, 64) }
   110  func ParseUintMust(value string) uint     { return uint(mustParseUint(value, 0)) }
   111  
   112  func ParseInt8Must(value string) int8   { return int8(mustParseInt(value, 8)) }
   113  func ParseInt16Must(value string) int16 { return int16(mustParseInt(value, 16)) }
   114  func ParseInt32Must(value string) int32 { return int32(mustParseInt(value, 32)) }
   115  func ParseInt64Must(value string) int64 { return mustParseInt(value, 64) }
   116  func ParseIntMust(value string) int     { return int(mustParseInt(value, 0)) }
   117  
   118  func FormatUint8(value uint8) string   { return strconv.FormatUint(uint64(value), 10) }
   119  func FormatUint16(value uint16) string { return strconv.FormatUint(uint64(value), 10) }
   120  func FormatUint32(value uint32) string { return strconv.FormatUint(uint64(value), 10) }
   121  func FormatUint64(value uint64) string { return strconv.FormatUint(value, 10) }
   122  func FormatUint(value uint) string     { return strconv.FormatUint(uint64(value), 10) }
   123  
   124  func FormatInt8(value int8) string   { return strconv.FormatInt(int64(value), 10) }
   125  func FormatInt16(value int16) string { return strconv.FormatInt(int64(value), 10) }
   126  func FormatInt32(value int32) string { return strconv.FormatInt(int64(value), 10) }
   127  func FormatInt64(value int64) string { return strconv.FormatInt(value, 10) }
   128  func FormatInt(value int) string     { return strconv.FormatInt(int64(value), 10) }
   129  
   130  var (
   131  	sizesIEC = []string{
   132  		"B",
   133  		"KiB",
   134  		"MiB",
   135  		"GiB",
   136  		"TiB",
   137  		"PiB",
   138  		"EiB",
   139  		"ZiB",
   140  		"YiB",
   141  	}
   142  	sizes = []string{
   143  		"B",
   144  		"KB",
   145  		"MB",
   146  		"GB",
   147  		"TB",
   148  		"PB",
   149  		"EB",
   150  		"ZB",
   151  		"YB",
   152  	}
   153  )
   154  
   155  func logn(n, b float64) float64 {
   156  	return math.Log(n) / math.Log(b)
   157  }
   158  
   159  func humanateBytes(s uint64, base float64, sizes []string) string {
   160  	if s < 10 {
   161  		return fmt.Sprintf("%dB", s)
   162  	}
   163  	e := math.Floor(logn(float64(s), base))
   164  	suffix := sizes[int(e)]
   165  	val := float64(s) / math.Pow(base, math.Floor(e))
   166  	f := "%.0f"
   167  	if val < 10 {
   168  		f = "%.1f"
   169  	}
   170  
   171  	return fmt.Sprintf(f+"%s", val, suffix)
   172  }
   173  
   174  // FileSize calculates the file size and generate user-friendly string.
   175  func FileSizeIEC(s uint64) string {
   176  	return humanateBytes(s, 1024, sizesIEC)
   177  }
   178  
   179  // FileSize calculates the file size and generate user-friendly string.
   180  func FileSize(s uint64) string {
   181  	return humanateBytes(s, 1000, sizes)
   182  }
   183  func AspectRatio(srcRect image.Point, toResize uint64) image.Point {
   184  	w, h := int(toResize), getRatioSize(int(toResize), srcRect.Y, srcRect.X)
   185  	if srcRect.X < srcRect.Y {
   186  		w, h = getRatioSize(int(toResize), srcRect.X, srcRect.Y), int(toResize)
   187  	}
   188  	return image.Point{w, h}
   189  }
   190  
   191  func getRatioSize(a, b, c int) int {
   192  	d := a * b / c
   193  	return (d + 1) & -1
   194  }