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

     1  package num
     2  
     3  import (
     4  	"math/big"
     5  	"reflect"
     6  )
     7  
     8  // Signed is a constraint that permits any signed integer type.
     9  // If future releases of Go add new predeclared signed integer types,
    10  // this constraint will be modified to include them.
    11  type Signed interface {
    12  	~int | ~int8 | ~int16 | ~int32 | ~int64
    13  }
    14  
    15  // Unsigned is a constraint that permits any unsigned integer type.
    16  // If future releases of Go add new predeclared unsigned integer types,
    17  // this constraint will be modified to include them.
    18  type Unsigned interface {
    19  	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
    20  }
    21  
    22  // Integer is a constraint that permits any integer type.
    23  // If future releases of Go add new predeclared integer types,
    24  // this constraint will be modified to include them.
    25  type Integer interface {
    26  	Signed | Unsigned
    27  }
    28  
    29  // Float is a constraint that permits any floating-point type.
    30  // If future releases of Go add new predeclared floating-point types,
    31  // this constraint will be modified to include them.
    32  type Float interface {
    33  	~float32 | ~float64
    34  }
    35  
    36  // Complex is a constraint that permits any complex numeric type.
    37  // If future releases of Go add new predeclared complex numeric types,
    38  // this constraint will be modified to include them.
    39  type Complex interface {
    40  	~complex64 | ~complex128
    41  }
    42  
    43  // Ordered is a constraint that permits any ordered type: any type
    44  // that supports the operators < <= >= >.
    45  // If future releases of Go add new ordered types,
    46  // this constraint will be modified to include them.
    47  type Ordered interface {
    48  	Integer | Float | ~string
    49  }
    50  
    51  // NumberOfCPUBitsRelated The number of CPU bits is related
    52  // Deprecated: 不推荐使用
    53  type NumberOfCPUBitsRelated interface {
    54  	~int | ~uint | ~uintptr
    55  }
    56  
    57  // /*nil, */ int8, uint8, int16, uint16, int32, uint32, int64, uint64, int, uint, float32, float64 , bool, string
    58  // ~int8 | ~uint8 | ~int16 | ~uint16 | ~int32 | ~uint32 | ~int64 | ~uint64 | ~int | ~uint | ~float32 | ~float64 | ~bool | ~string
    59  // uintptr
    60  
    61  // BaseType 基础类型
    62  type BaseType interface {
    63  	Integer | Float | ~string | ~bool
    64  }
    65  
    66  // GenericType Series支持的所有类型
    67  // Deprecated: 不推荐使用
    68  type GenericType interface {
    69  	~bool | ~int32 | ~int64 | ~int | ~float32 | ~float64 | ~string
    70  }
    71  
    72  // StatType 可以统计的类型
    73  // Deprecated: 不推荐使用
    74  type StatType interface {
    75  	~int32 | ~int64 | ~float32 | ~float64
    76  }
    77  
    78  type BigFloat = big.Float // 预留将来可能扩展float
    79  
    80  // Deprecated: 不推荐使用
    81  type Number8 interface {
    82  	~int8 | ~uint8
    83  }
    84  
    85  // Deprecated: 不推荐使用
    86  type Number16 interface {
    87  	~int16 | ~uint16
    88  }
    89  
    90  // Deprecated: 不推荐使用
    91  type Number32 interface {
    92  	~int32 | ~uint32 | float32
    93  }
    94  
    95  // Deprecated: 不推荐使用
    96  type Number64 interface {
    97  	~int64 | ~uint64 | float64 | int | uint
    98  }
    99  
   100  // Deprecated: 已弃用
   101  type MoveType interface {
   102  	StatType | ~bool | ~string
   103  }
   104  
   105  // Number int和uint的长度取决于CPU是多少位
   106  type Number interface {
   107  	Integer | Float
   108  }
   109  
   110  // TypeDefault 设定泛型默认值, 0或者NaN
   111  func TypeDefault[T BaseType]() T {
   112  	var d any
   113  	var t T
   114  	switch any(t).(type) {
   115  	case int8, uint8, int16, uint16, uint32, uint64, int, uint, uintptr:
   116  		d = t
   117  	case int32:
   118  		d = Int32NaN
   119  	case int64:
   120  		d = Int64NaN
   121  	case float32:
   122  		d = __nilToFloat32
   123  	case float64:
   124  		d = __nilToFloat64
   125  	case bool:
   126  		d = BoolNaN
   127  	case string:
   128  		d = StringNaN
   129  	default:
   130  		d = t
   131  	}
   132  
   133  	return d.(T)
   134  }
   135  
   136  // any转number
   137  func valueToNumber[T Number](v any, nil2t T, bool2t func(b bool) T, string2t func(s string, v any) T) T {
   138  	switch val := v.(type) {
   139  	case nil: // 这个地方判断nil值
   140  		return nil2t
   141  	case int8:
   142  		return T(val)
   143  	case uint8:
   144  		return T(val)
   145  	case int16:
   146  		return T(val)
   147  	case uint16:
   148  		return T(val)
   149  	case int32:
   150  		return T(val)
   151  	case uint32:
   152  		return T(val)
   153  	case int64:
   154  		return T(val)
   155  	case uint64:
   156  		return T(val)
   157  	case int:
   158  		return T(val)
   159  	case uint:
   160  		return T(val)
   161  	case uintptr:
   162  		return T(val)
   163  	case float32:
   164  		return T(val)
   165  	case float64:
   166  		return T(val)
   167  	case bool:
   168  		return bool2t(val)
   169  	case string:
   170  		return string2t(val, v)
   171  	case *int8:
   172  		return T(*val)
   173  	case *uint8:
   174  		return T(*val)
   175  	case *int16:
   176  		return T(*val)
   177  	case *uint16:
   178  		return T(*val)
   179  	case *int32:
   180  		return T(*val)
   181  	case *uint32:
   182  		return T(*val)
   183  	case *int64:
   184  		return T(*val)
   185  	case *uint64:
   186  		return T(*val)
   187  	case *int:
   188  		return T(*val)
   189  	case *uint:
   190  		return T(*val)
   191  	case *uintptr:
   192  		return T(*val)
   193  	case *float32:
   194  		return T(*val)
   195  	case *float64:
   196  		return T(*val)
   197  	case *bool:
   198  		return bool2t(*val)
   199  	case *string:
   200  		return string2t(*val, v)
   201  	default:
   202  		panic(TypeError(v))
   203  	}
   204  	return T(0)
   205  }
   206  
   207  // any转number, 推导参数v的类型
   208  func __anyToNumber[T Number](v any) T {
   209  	switch val := v.(type) {
   210  	case nil: // 这个地方判断nil值
   211  		return TypeDefault[T]()
   212  	case int8:
   213  		return T(val)
   214  	case *int8:
   215  		return T(*val)
   216  	case uint8:
   217  		return T(val)
   218  	case *uint8:
   219  		return T(*val)
   220  	case int16:
   221  		return T(val)
   222  	case *int16:
   223  		return T(*val)
   224  	case uint16:
   225  		return T(val)
   226  	case *uint16:
   227  		return T(*val)
   228  	case int32:
   229  		return T(val)
   230  	case *int32:
   231  		return T(*val)
   232  	case uint32:
   233  		return T(val)
   234  	case *uint32:
   235  		return T(*val)
   236  	case int64:
   237  		return T(val)
   238  	case *int64:
   239  		return T(*val)
   240  	case uint64:
   241  		return T(val)
   242  	case *uint64:
   243  		return T(*val)
   244  	case int:
   245  		return T(val)
   246  	case *int:
   247  		return T(*val)
   248  	case uint:
   249  		return T(val)
   250  	case *uint:
   251  		return T(*val)
   252  	case uintptr:
   253  		return T(val)
   254  	case *uintptr:
   255  		return T(*val)
   256  	case float32:
   257  		return T(val)
   258  	case *float32:
   259  		return T(*val)
   260  	case float64:
   261  		return T(val)
   262  	case *float64:
   263  		return T(*val)
   264  	case bool:
   265  		return T(BoolToInt(val))
   266  	case *bool:
   267  		return T(BoolToInt(*val))
   268  	case string:
   269  		vt := ParseFloat64(val, v)
   270  		if Float64IsNaN(vt) {
   271  			td := T(0)
   272  			if !reflect.ValueOf(td).CanFloat() {
   273  				return td
   274  			}
   275  		}
   276  		return T(vt)
   277  	case *string:
   278  		vt := ParseFloat64(*val, v)
   279  		if Float64IsNaN(vt) {
   280  			td := T(0)
   281  			if !reflect.ValueOf(td).CanFloat() {
   282  				return td
   283  			}
   284  		}
   285  		return T(vt)
   286  	default:
   287  		panic(TypeError(v))
   288  	}
   289  	return T(0)
   290  }
   291  
   292  // AnyToGeneric any转其它类型, 推导T的类型
   293  // 支持3个方向: any到number, any到bool, any到string
   294  func AnyToGeneric[T BaseType](v any) T {
   295  	var d any
   296  	var to T
   297  	switch any(to).(type) {
   298  	case int8:
   299  		d = __anyToNumber[int8](v)
   300  	case uint8:
   301  		d = __anyToNumber[uint8](v)
   302  	case int16:
   303  		d = __anyToNumber[int16](v)
   304  	case uint16:
   305  		d = __anyToNumber[uint16](v)
   306  	case int32:
   307  		d = __anyToNumber[int32](v)
   308  	case uint32:
   309  		d = __anyToNumber[uint32](v)
   310  	case int64:
   311  		d = __anyToNumber[int64](v)
   312  	case uint64:
   313  		d = __anyToNumber[uint64](v)
   314  	case int:
   315  		d = __anyToNumber[int](v)
   316  	case uint:
   317  		d = __anyToNumber[uint](v)
   318  	case uintptr:
   319  		d = __anyToNumber[uintptr](v)
   320  	case float32:
   321  		d = __anyToNumber[float32](v)
   322  	case float64:
   323  		d = __anyToNumber[float64](v)
   324  	case bool:
   325  		d = AnyToBool(v)
   326  	case string:
   327  		d = AnyToString(v)
   328  	case []int8, []uint8, []int16, []uint16, []int32, []uint32, []int64, []uint64, []int, []uint, []uintptr, []float32, []float64:
   329  		// 什么也不处理, 给个默认值
   330  		d = to
   331  	case []bool:
   332  		d = to
   333  	case []string:
   334  		d = to
   335  	default:
   336  		panic(TypeError(v))
   337  	}
   338  	return d.(T)
   339  }
   340  
   341  // GenericParse 泛型解析
   342  func GenericParse[T BaseType](text string) T {
   343  	return AnyToGeneric[T](text)
   344  }