| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 | // Copyright 2014 com authors//// Licensed under the Apache License, Version 2.0 (the "License"): you may// not use this file except in compliance with the License. You may obtain// a copy of the License at////     http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the// License for the specific language governing permissions and limitations// under the License.package comimport (	"fmt"	"strconv")// Convert string to specify type.type StrTo stringfunc (f StrTo) Exist() bool {	return string(f) != string(0x1E)}func (f StrTo) Uint8() (uint8, error) {	v, err := strconv.ParseUint(f.String(), 10, 8)	return uint8(v), err}func (f StrTo) Int() (int, error) {	v, err := strconv.ParseInt(f.String(), 10, 0)	return int(v), err}func (f StrTo) Int64() (int64, error) {	v, err := strconv.ParseInt(f.String(), 10, 64)	return int64(v), err}func (f StrTo) MustUint8() uint8 {	v, _ := f.Uint8()	return v}func (f StrTo) MustInt() int {	v, _ := f.Int()	return v}func (f StrTo) MustInt64() int64 {	v, _ := f.Int64()	return v}func (f StrTo) String() string {	if f.Exist() {		return string(f)	}	return ""}// Convert any type to string.func ToStr(value interface{}, args ...int) (s string) {	switch v := value.(type) {	case bool:		s = strconv.FormatBool(v)	case float32:		s = strconv.FormatFloat(float64(v), 'f', argInt(args).Get(0, -1), argInt(args).Get(1, 32))	case float64:		s = strconv.FormatFloat(v, 'f', argInt(args).Get(0, -1), argInt(args).Get(1, 64))	case int:		s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))	case int8:		s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))	case int16:		s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))	case int32:		s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))	case int64:		s = strconv.FormatInt(v, argInt(args).Get(0, 10))	case uint:		s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))	case uint8:		s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))	case uint16:		s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))	case uint32:		s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))	case uint64:		s = strconv.FormatUint(v, argInt(args).Get(0, 10))	case string:		s = v	case []byte:		s = string(v)	default:		s = fmt.Sprintf("%v", v)	}	return s}type argInt []intfunc (a argInt) Get(i int, args ...int) (r int) {	if i >= 0 && i < len(a) {		r = a[i]	} else if len(args) > 0 {		r = args[0]	}	return}// HexStr2int converts hex format string to decimal number.func HexStr2int(hexStr string) (int, error) {	num := 0	length := len(hexStr)	for i := 0; i < length; i++ {		char := hexStr[length-i-1]		factor := -1		switch {		case char >= '0' && char <= '9':			factor = int(char) - '0'		case char >= 'a' && char <= 'f':			factor = int(char) - 'a' + 10		default:			return -1, fmt.Errorf("invalid hex: %s", string(char))		}		num += factor * PowInt(16, i)	}	return num, nil}// Int2HexStr converts decimal number to hex format string.func Int2HexStr(num int) (hex string) {	if num == 0 {		return "0"	}	for num > 0 {		r := num % 16		c := "?"		if r >= 0 && r <= 9 {			c = string(r + '0')		} else {			c = string(r + 'a' - 10)		}		hex = c + hex		num = num / 16	}	return hex}
 |