github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/Unknwon/com/convert.go (about)

     1  // Copyright 2014 com authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"): you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    11  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    12  // License for the specific language governing permissions and limitations
    13  // under the License.
    14  
    15  package com
    16  
    17  import (
    18  	"fmt"
    19  	"strconv"
    20  )
    21  
    22  // Convert string to specify type.
    23  type StrTo string
    24  
    25  func (f StrTo) Exist() bool {
    26  	return string(f) != string(0x1E)
    27  }
    28  
    29  func (f StrTo) Uint8() (uint8, error) {
    30  	v, err := strconv.ParseUint(f.String(), 10, 8)
    31  	return uint8(v), err
    32  }
    33  
    34  func (f StrTo) Int() (int, error) {
    35  	v, err := strconv.ParseInt(f.String(), 10, 0)
    36  	return int(v), err
    37  }
    38  
    39  func (f StrTo) Int64() (int64, error) {
    40  	v, err := strconv.ParseInt(f.String(), 10, 64)
    41  	return int64(v), err
    42  }
    43  
    44  func (f StrTo) MustUint8() uint8 {
    45  	v, _ := f.Uint8()
    46  	return v
    47  }
    48  
    49  func (f StrTo) MustInt() int {
    50  	v, _ := f.Int()
    51  	return v
    52  }
    53  
    54  func (f StrTo) MustInt64() int64 {
    55  	v, _ := f.Int64()
    56  	return v
    57  }
    58  
    59  func (f StrTo) String() string {
    60  	if f.Exist() {
    61  		return string(f)
    62  	}
    63  	return ""
    64  }
    65  
    66  // Convert any type to string.
    67  func ToStr(value interface{}, args ...int) (s string) {
    68  	switch v := value.(type) {
    69  	case bool:
    70  		s = strconv.FormatBool(v)
    71  	case float32:
    72  		s = strconv.FormatFloat(float64(v), 'f', argInt(args).Get(0, -1), argInt(args).Get(1, 32))
    73  	case float64:
    74  		s = strconv.FormatFloat(v, 'f', argInt(args).Get(0, -1), argInt(args).Get(1, 64))
    75  	case int:
    76  		s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
    77  	case int8:
    78  		s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
    79  	case int16:
    80  		s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
    81  	case int32:
    82  		s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
    83  	case int64:
    84  		s = strconv.FormatInt(v, argInt(args).Get(0, 10))
    85  	case uint:
    86  		s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
    87  	case uint8:
    88  		s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
    89  	case uint16:
    90  		s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
    91  	case uint32:
    92  		s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
    93  	case uint64:
    94  		s = strconv.FormatUint(v, argInt(args).Get(0, 10))
    95  	case string:
    96  		s = v
    97  	case []byte:
    98  		s = string(v)
    99  	default:
   100  		s = fmt.Sprintf("%v", v)
   101  	}
   102  	return s
   103  }
   104  
   105  type argInt []int
   106  
   107  func (a argInt) Get(i int, args ...int) (r int) {
   108  	if i >= 0 && i < len(a) {
   109  		r = a[i]
   110  	} else if len(args) > 0 {
   111  		r = args[0]
   112  	}
   113  	return
   114  }
   115  
   116  // HexStr2int converts hex format string to decimal number.
   117  func HexStr2int(hexStr string) (int, error) {
   118  	num := 0
   119  	length := len(hexStr)
   120  	for i := 0; i < length; i++ {
   121  		char := hexStr[length-i-1]
   122  		factor := -1
   123  
   124  		switch {
   125  		case char >= '0' && char <= '9':
   126  			factor = int(char) - '0'
   127  		case char >= 'a' && char <= 'f':
   128  			factor = int(char) - 'a' + 10
   129  		default:
   130  			return -1, fmt.Errorf("invalid hex: %s", string(char))
   131  		}
   132  
   133  		num += factor * PowInt(16, i)
   134  	}
   135  	return num, nil
   136  }
   137  
   138  // Int2HexStr converts decimal number to hex format string.
   139  func Int2HexStr(num int) (hex string) {
   140  	if num == 0 {
   141  		return "0"
   142  	}
   143  
   144  	for num > 0 {
   145  		r := num % 16
   146  
   147  		c := "?"
   148  		if r >= 0 && r <= 9 {
   149  			c = string(r + '0')
   150  		} else {
   151  			c = string(r + 'a' - 10)
   152  		}
   153  		hex = c + hex
   154  		num = num / 16
   155  	}
   156  	return hex
   157  }