|
- package utils
-
- import (
- "encoding/binary"
- "encoding/json"
- "fmt"
- "math"
- "strconv"
- "strings"
- )
-
- const CODE62 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
- const CODE_LENTH = 62
-
- var EDOC = map[string]int{"0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9, "a": 10, "b": 11, "c": 12, "d": 13, "e": 14, "f": 15, "g": 16, "h": 17, "i": 18, "j": 19, "k": 20, "l": 21, "m": 22, "n": 23, "o": 24, "p": 25, "q": 26, "r": 27, "s": 28, "t": 29, "u": 30, "v": 31, "w": 32, "x": 33, "y": 34, "z": 35, "A": 36, "B": 37, "C": 38, "D": 39, "E": 40, "F": 41, "G": 42, "H": 43, "I": 44, "J": 45, "K": 46, "L": 47, "M": 48, "N": 49, "O": 50, "P": 51, "Q": 52, "R": 53, "S": 54, "T": 55, "U": 56, "V": 57, "W": 58, "X": 59, "Y": 60, "Z": 61}
-
- func Base62Encode(number int) string {
- if number == 0 {
- return "0"
- }
- result := make([]byte, 0)
- for number > 0 {
- round := number / CODE_LENTH
- remain := number % CODE_LENTH
- result = append(result, CODE62[remain])
- number = round
- }
- return string(result)
- }
-
- /**
- * 解码字符串为整数
- */
- func Base62Decode(str string) int {
- str = strings.TrimSpace(str)
- var result int = 0
- for index, char := range []byte(str) {
- result += EDOC[string(char)] * int(math.Pow(CODE_LENTH, float64(index)))
- }
- return result
- }
- func ToString(raw interface{}, e error) (res string) {
- if e != nil {
- return ""
- }
- return AnyToString(raw)
- }
-
- func ToInt64(raw interface{}, e error) int64 {
- if e != nil {
- return 0
- }
- return AnyToInt64(raw)
- }
-
- func AnyToBool(raw interface{}) bool {
- switch i := raw.(type) {
- case float32, float64, int, int64, uint, uint8, uint16, uint32, uint64, int8, int16, int32:
- return i != 0
- case []byte:
- return i != nil
- case string:
- if i == "false" {
- return false
- }
- return i != ""
- case error:
- return false
- case nil:
- return true
- }
- val := fmt.Sprint(raw)
- val = strings.TrimLeft(val, "&")
- if strings.TrimLeft(val, "{}") == "" {
- return false
- }
- if strings.TrimLeft(val, "[]") == "" {
- return false
- }
- // ptr type
- b, err := json.Marshal(raw)
- if err != nil {
- return false
- }
- if strings.TrimLeft(string(b), "\"\"") == "" {
- return false
- }
- if strings.TrimLeft(string(b), "{}") == "" {
- return false
- }
- return true
- }
-
- func AnyToInt64(raw interface{}) int64 {
- switch i := raw.(type) {
- case string:
- res, _ := strconv.ParseInt(i, 10, 64)
- return res
- case []byte:
- return BytesToInt64(i)
- case int:
- return int64(i)
- case int64:
- return i
- case uint:
- return int64(i)
- case uint8:
- return int64(i)
- case uint16:
- return int64(i)
- case uint32:
- return int64(i)
- case uint64:
- return int64(i)
- case int8:
- return int64(i)
- case int16:
- return int64(i)
- case int32:
- return int64(i)
- case float32:
- return int64(i)
- case float64:
- return int64(i)
- case error:
- return 0
- case bool:
- if i {
- return 1
- }
- return 0
- }
- return 0
- }
-
- func AnyToString(raw interface{}) string {
- switch i := raw.(type) {
- case []byte:
- return string(i)
- case int:
- return strconv.FormatInt(int64(i), 10)
- case int64:
- return strconv.FormatInt(i, 10)
- case float32:
- return Float64ToStr(float64(i))
- case float64:
- return Float64ToStr(i)
- case uint:
- return strconv.FormatInt(int64(i), 10)
- case uint8:
- return strconv.FormatInt(int64(i), 10)
- case uint16:
- return strconv.FormatInt(int64(i), 10)
- case uint32:
- return strconv.FormatInt(int64(i), 10)
- case uint64:
- return strconv.FormatInt(int64(i), 10)
- case int8:
- return strconv.FormatInt(int64(i), 10)
- case int16:
- return strconv.FormatInt(int64(i), 10)
- case int32:
- return strconv.FormatInt(int64(i), 10)
- case string:
- return i
- case error:
- return i.Error()
- case bool:
- return strconv.FormatBool(i)
- }
- return fmt.Sprintf("%#v", raw)
- }
-
- func AnyToFloat64(raw interface{}) float64 {
- switch i := raw.(type) {
- case []byte:
- f, _ := strconv.ParseFloat(string(i), 64)
- return f
- case int:
- return float64(i)
- case int64:
- return float64(i)
- case float32:
- return float64(i)
- case float64:
- return i
- case uint:
- return float64(i)
- case uint8:
- return float64(i)
- case uint16:
- return float64(i)
- case uint32:
- return float64(i)
- case uint64:
- return float64(i)
- case int8:
- return float64(i)
- case int16:
- return float64(i)
- case int32:
- return float64(i)
- case string:
- f, _ := strconv.ParseFloat(i, 64)
- return f
- case bool:
- if i {
- return 1
- }
- }
- return 0
- }
-
- func ToByte(raw interface{}, e error) []byte {
- if e != nil {
- return []byte{}
- }
- switch i := raw.(type) {
- case string:
- return []byte(i)
- case int:
- return Int64ToBytes(int64(i))
- case int64:
- return Int64ToBytes(i)
- case float32:
- return Float32ToByte(i)
- case float64:
- return Float64ToByte(i)
- case uint:
- return Int64ToBytes(int64(i))
- case uint8:
- return Int64ToBytes(int64(i))
- case uint16:
- return Int64ToBytes(int64(i))
- case uint32:
- return Int64ToBytes(int64(i))
- case uint64:
- return Int64ToBytes(int64(i))
- case int8:
- return Int64ToBytes(int64(i))
- case int16:
- return Int64ToBytes(int64(i))
- case int32:
- return Int64ToBytes(int64(i))
- case []byte:
- return i
- case error:
- return []byte(i.Error())
- case bool:
- if i {
- return []byte("true")
- }
- return []byte("false")
- }
- return []byte(fmt.Sprintf("%#v", raw))
- }
-
- func Int64ToBytes(i int64) []byte {
- var buf = make([]byte, 8)
- binary.BigEndian.PutUint64(buf, uint64(i))
- return buf
- }
-
- func BytesToInt64(buf []byte) int64 {
- return int64(binary.BigEndian.Uint64(buf))
- }
-
- func StrToInt(s string) int {
- res, _ := strconv.Atoi(s)
- return res
- }
-
- func StrToInt64(s string) int64 {
- res, _ := strconv.ParseInt(s, 10, 64)
- return res
- }
-
- func Float32ToByte(float float32) []byte {
- bits := math.Float32bits(float)
- bytes := make([]byte, 4)
- binary.LittleEndian.PutUint32(bytes, bits)
-
- return bytes
- }
-
- func ByteToFloat32(bytes []byte) float32 {
- bits := binary.LittleEndian.Uint32(bytes)
- return math.Float32frombits(bits)
- }
-
- func Float64ToByte(float float64) []byte {
- bits := math.Float64bits(float)
- bytes := make([]byte, 8)
- binary.LittleEndian.PutUint64(bytes, bits)
- return bytes
- }
-
- func ByteToFloat64(bytes []byte) float64 {
- bits := binary.LittleEndian.Uint64(bytes)
- return math.Float64frombits(bits)
- }
-
- func Float64ToStr(f float64) string {
- return strconv.FormatFloat(f, 'f', 2, 64)
- }
- func Float64ToStrPrec1(f float64) string {
- return strconv.FormatFloat(f, 'f', 1, 64)
- }
-
- func Float32ToStr(f float32) string {
- return Float64ToStr(float64(f))
- }
-
- func StrToFloat64(s string) float64 {
- res, err := strconv.ParseFloat(s, 64)
- if err != nil {
- return 0
- }
- return res
- }
-
- func StrToFloat32(s string) float32 {
- res, err := strconv.ParseFloat(s, 32)
- if err != nil {
- return 0
- }
- return float32(res)
- }
-
- func StrToBool(s string) bool {
- b, _ := strconv.ParseBool(s)
- return b
- }
-
- func BoolToStr(b bool) string {
- if b {
- return "true"
- }
- return "false"
- }
-
- func FloatToInt64(f float64) int64 {
- return int64(f)
- }
-
- func IntToStr(i int) string {
- return strconv.Itoa(i)
- }
-
- func Int64ToStr(i int64) string {
- return strconv.FormatInt(i, 10)
- }
|