gitee.com/quant1x/num@v0.3.2/type_strings.go (about)

     1  package num
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/quant1x/gox/exception"
     6  	"gitee.com/quant1x/gox/logger"
     7  	"reflect"
     8  	"strconv"
     9  	"strings"
    10  )
    11  
    12  const (
    13  	StringNaN    = "NaN"   // 字符串NaN
    14  	Nil2String   = "NaN"   // nil指针转string
    15  	True2String  = "true"  // true转string
    16  	False2String = "false" // false转string
    17  )
    18  
    19  var (
    20  	// PossibleNaOfString 有可能出现的NaN字符串的全部选项
    21  	PossibleNaOfString = []string{"NA", "NaN", "nan", "<nil>"}
    22  )
    23  
    24  // IsEmpty Code to test if string is empty
    25  func IsEmpty(s string) bool {
    26  	if strings.TrimSpace(s) == "" {
    27  		return true
    28  	} else {
    29  		return false
    30  	}
    31  }
    32  
    33  // StringIsNaN 判断字符串是否NaN
    34  func StringIsNaN(s string) bool {
    35  	s = strings.TrimSpace(s)
    36  	if strings.ToLower(s) == "nan" {
    37  		return true
    38  	}
    39  	return false
    40  }
    41  
    42  func __anyToString(v any) string {
    43  	switch val := v.(type) {
    44  	case nil:
    45  		return Nil2String
    46  	case int8:
    47  		return strconv.FormatInt(int64(val), 10)
    48  	case uint8:
    49  		return strconv.FormatUint(uint64(val), 10)
    50  	case int16:
    51  		return strconv.FormatInt(int64(val), 10)
    52  	case uint16:
    53  		return strconv.FormatUint(uint64(val), 10)
    54  	case int32:
    55  		return strconv.FormatInt(int64(val), 10)
    56  	case uint32:
    57  		return strconv.FormatUint(uint64(val), 10)
    58  	case int64:
    59  		return strconv.FormatInt(int64(val), 10)
    60  	case uint64:
    61  		return strconv.FormatUint(uint64(val), 10)
    62  	case int:
    63  		return strconv.Itoa(val)
    64  	case uint:
    65  		return strconv.FormatUint(uint64(val), 10)
    66  	case uintptr:
    67  		return strconv.FormatUint(uint64(val), 10)
    68  	case float32:
    69  		return strconv.FormatFloat(float64(val), 'G', -1, 32)
    70  	case float64:
    71  		return strconv.FormatFloat(float64(val), 'G', -1, 64)
    72  	case bool:
    73  		return BoolToString(val)
    74  	case string:
    75  		return val
    76  	default:
    77  		logger.Errorf("%s, error=The type is not recognized\n", v)
    78  		_ = v.(string) // Intentionally panic
    79  		return Nil2String
    80  	}
    81  }
    82  
    83  func __printString(v any) string {
    84  	switch val := v.(type) {
    85  	case nil:
    86  		return Nil2String
    87  	case int8:
    88  		return strconv.FormatInt(int64(val), 10)
    89  	case uint8:
    90  		return strconv.FormatUint(uint64(val), 10)
    91  	case int16:
    92  		return strconv.FormatInt(int64(val), 10)
    93  	case uint16:
    94  		return strconv.FormatUint(uint64(val), 10)
    95  	case int32:
    96  		return strconv.FormatInt(int64(val), 10)
    97  	case uint32:
    98  		return strconv.FormatUint(uint64(val), 10)
    99  	case int64:
   100  		return strconv.FormatInt(int64(val), 10)
   101  	case uint64:
   102  		return strconv.FormatUint(uint64(val), 10)
   103  	case int:
   104  		return strconv.Itoa(val)
   105  	case uint:
   106  		return strconv.FormatUint(uint64(val), 10)
   107  	case uintptr:
   108  		return strconv.FormatUint(uint64(val), 10)
   109  	case float32: // python 8位, go 8位 ?
   110  		//return fmt.Sprintf("%.7f", val)
   111  		return strconv.FormatFloat(float64(val), 'G', -1, 32)
   112  	case float64: // python 16位, go 16位 ?
   113  		//return fmt.Sprintf("%.15f", val)
   114  		return strconv.FormatFloat(float64(val), 'G', -1, 64)
   115  	case bool:
   116  		return BoolToString(val)
   117  	case string:
   118  		return val
   119  	default:
   120  		logger.Errorf("%s, error=The type is not recognized\n", v)
   121  		_ = v.(string) // Intentionally panic
   122  		return Nil2String
   123  	}
   124  }
   125  
   126  // AnyToString any转string
   127  func AnyToString(v any) string {
   128  	if vv, ok := ExtractValueFromPointer(v); ok {
   129  		v = vv
   130  	}
   131  
   132  	s := __anyToString(v)
   133  	return s
   134  }
   135  
   136  func PrintString(v any) string {
   137  	if vv, ok := ExtractValueFromPointer(v); ok {
   138  		v = vv
   139  	}
   140  
   141  	s := __printString(v)
   142  	return s
   143  }
   144  
   145  func slice_any_to_string[T BaseType](s []T) []string {
   146  	count := len(s)
   147  	if count == 0 {
   148  		return []string{}
   149  	}
   150  	d := make([]string, count)
   151  	for idx, iv := range s {
   152  		d[idx] = AnyToGeneric[string](iv)
   153  	}
   154  	return d
   155  }
   156  
   157  // SliceToString any输入只能是一维slice或者数组
   158  func SliceToString(v any) []string {
   159  	switch values := v.(type) {
   160  	case []int8:
   161  		return slice_any_to_string(values)
   162  	case []uint8:
   163  		return slice_any_to_string(values)
   164  	case []int16:
   165  		return slice_any_to_string(values)
   166  	case []uint16:
   167  		return slice_any_to_string(values)
   168  	case []int32:
   169  		return slice_any_to_string(values)
   170  	case []uint32:
   171  		return slice_any_to_string(values)
   172  	case []int64:
   173  		return slice_any_to_string(values)
   174  	case []uint64:
   175  		return slice_any_to_string(values)
   176  	case []int:
   177  		return slice_any_to_string(values)
   178  	case []uint:
   179  		return slice_any_to_string(values)
   180  	case []float32:
   181  		return slice_any_to_string(values)
   182  	case []float64:
   183  		return slice_any_to_string(values)
   184  	case []bool:
   185  		return slice_any_to_string(values)
   186  	case []string:
   187  		return values
   188  	default:
   189  		vv := reflect.ValueOf(v)
   190  		vk := vv.Kind()
   191  		panic(exception.New(errorFloat64Base+0, fmt.Sprintf("Unsupported type: %s", vk.String())))
   192  	}
   193  	return []string{}
   194  }