github.com/zlyuancn/zstr@v0.0.0-20230412074414-14d6b645962f/simple.go (about) 1 /* 2 ------------------------------------------------- 3 Author : Zhang Fan 4 date: 2020/7/14 5 Description : 6 ------------------------------------------------- 7 */ 8 9 package zstr 10 11 import ( 12 "fmt" 13 "reflect" 14 "strconv" 15 "unsafe" 16 ) 17 18 func ToBytes(s string) []byte { 19 return StringToBytes(&s) 20 } 21 22 func ToBool(any interface{}) (bool, error) { 23 switch v := any.(type) { 24 case nil: 25 return false, nil 26 case bool: 27 return v, nil 28 } 29 s := anyToString(any) 30 switch s { 31 case "1", "t", "T", "true", "TRUE", "True", "y", "Y", "yes", "YES", "Yes", 32 "on", "ON", "On", "ok", "OK", "Ok", 33 "enabled", "ENABLED", "Enabled", 34 "open", "OPEN", "Open": 35 return true, nil 36 case "0", "f", "F", "false", "FALSE", "False", "n", "N", "no", "NO", "No", 37 "off", "OFF", "Off", "cancel", "CANCEL", "Cancel", 38 "disable", "DISABLE", "Disable", 39 "close", "CLOSE", "Close", 40 "", "nil", "Nil", "NIL", "null", "Null", "NULL", "none", "None", "NONE": 41 return false, nil 42 } 43 return false, fmt.Errorf("数据\"%s\"无法转换为bool", s) 44 } 45 func GetBool(any interface{}, def ...bool) bool { 46 if a, err := ToBool(any); err == nil { 47 return a 48 } 49 return len(def) > 0 && def[0] 50 } 51 52 func ToInt(any interface{}) (int, error) { 53 switch v := any.(type) { 54 case nil: 55 return 0, nil 56 case bool: 57 if v { 58 return 1, nil 59 } 60 return 0, nil 61 case int: 62 return v, nil 63 } 64 65 s := anyToString(any) 66 return strconv.Atoi(s) 67 } 68 func GetInt(any interface{}, def ...int) int { 69 if a, err := ToInt(any); err == nil { 70 return a 71 } 72 if len(def) > 0 { 73 return def[0] 74 } 75 return 0 76 } 77 func ToInt8(any interface{}) (int8, error) { 78 switch v := any.(type) { 79 case nil: 80 return 0, nil 81 case bool: 82 if v { 83 return 1, nil 84 } 85 return 0, nil 86 case int8: 87 return v, nil 88 } 89 90 s := anyToString(any) 91 n, err := strconv.ParseInt(s, 10, 8) 92 if err != nil { 93 return 0, err 94 } 95 return int8(n), nil 96 } 97 func GetInt8(any interface{}, def ...int8) int8 { 98 if a, err := ToInt8(any); err == nil { 99 return a 100 } 101 if len(def) > 0 { 102 return def[0] 103 } 104 return 0 105 } 106 func ToInt16(any interface{}) (int16, error) { 107 switch v := any.(type) { 108 case nil: 109 return 0, nil 110 case bool: 111 if v { 112 return 1, nil 113 } 114 return 0, nil 115 case int16: 116 return v, nil 117 } 118 119 s := anyToString(any) 120 n, err := strconv.ParseInt(s, 10, 16) 121 if err != nil { 122 return 0, err 123 } 124 return int16(n), nil 125 } 126 func GetInt16(any interface{}, def ...int16) int16 { 127 if a, err := ToInt16(any); err == nil { 128 return a 129 } 130 if len(def) > 0 { 131 return def[0] 132 } 133 return 0 134 } 135 func ToInt32(any interface{}) (int32, error) { 136 switch v := any.(type) { 137 case nil: 138 return 0, nil 139 case bool: 140 if v { 141 return 1, nil 142 } 143 return 0, nil 144 case int32: 145 return v, nil 146 } 147 148 s := anyToString(any) 149 n, err := strconv.ParseInt(s, 10, 32) 150 if err != nil { 151 return 0, err 152 } 153 return int32(n), nil 154 } 155 func GetInt32(any interface{}, def ...int32) int32 { 156 if a, err := ToInt32(any); err == nil { 157 return a 158 } 159 if len(def) > 0 { 160 return def[0] 161 } 162 return 0 163 } 164 func ToInt64(any interface{}) (int64, error) { 165 switch v := any.(type) { 166 case nil: 167 return 0, nil 168 case bool: 169 if v { 170 return 1, nil 171 } 172 return 0, nil 173 case int64: 174 return v, nil 175 } 176 177 s := anyToString(any) 178 return strconv.ParseInt(s, 10, 64) 179 } 180 func GetInt64(any interface{}, def ...int64) int64 { 181 if a, err := ToInt64(any); err == nil { 182 return a 183 } 184 if len(def) > 0 { 185 return def[0] 186 } 187 return 0 188 } 189 190 func ToUint(any interface{}) (uint, error) { 191 switch v := any.(type) { 192 case nil: 193 return 0, nil 194 case bool: 195 if v { 196 return 1, nil 197 } 198 return 0, nil 199 case uint: 200 return v, nil 201 } 202 203 s := anyToString(any) 204 n, err := strconv.ParseUint(s, 10, 64) 205 if err != nil { 206 return 0, err 207 } 208 return uint(n), err 209 } 210 func GetUint(any interface{}, def ...uint) uint { 211 if a, err := ToUint(any); err == nil { 212 return a 213 } 214 if len(def) > 0 { 215 return def[0] 216 } 217 return 0 218 } 219 func ToUint8(any interface{}) (uint8, error) { 220 switch v := any.(type) { 221 case nil: 222 return 0, nil 223 case bool: 224 if v { 225 return 1, nil 226 } 227 return 0, nil 228 case uint8: 229 return v, nil 230 } 231 232 s := anyToString(any) 233 n, err := strconv.ParseUint(s, 10, 8) 234 if err != nil { 235 return 0, err 236 } 237 return uint8(n), nil 238 } 239 func GetUint8(any interface{}, def ...uint8) uint8 { 240 if a, err := ToUint8(any); err == nil { 241 return a 242 } 243 if len(def) > 0 { 244 return def[0] 245 } 246 return 0 247 } 248 func ToUint16(any interface{}) (uint16, error) { 249 switch v := any.(type) { 250 case nil: 251 return 0, nil 252 case bool: 253 if v { 254 return 1, nil 255 } 256 return 0, nil 257 case uint16: 258 return v, nil 259 } 260 261 s := anyToString(any) 262 n, err := strconv.ParseUint(s, 10, 16) 263 if err != nil { 264 return 0, err 265 } 266 return uint16(n), nil 267 } 268 func GetUint16(any interface{}, def ...uint16) uint16 { 269 if a, err := ToUint16(any); err == nil { 270 return a 271 } 272 if len(def) > 0 { 273 return def[0] 274 } 275 return 0 276 } 277 func ToUint32(any interface{}) (uint32, error) { 278 switch v := any.(type) { 279 case nil: 280 return 0, nil 281 case bool: 282 if v { 283 return 1, nil 284 } 285 return 0, nil 286 case uint32: 287 return v, nil 288 } 289 290 s := anyToString(any) 291 n, err := strconv.ParseUint(s, 10, 32) 292 if err != nil { 293 return 0, err 294 } 295 return uint32(n), nil 296 } 297 func GetUint32(any interface{}, def ...uint32) uint32 { 298 if a, err := ToUint32(any); err == nil { 299 return a 300 } 301 if len(def) > 0 { 302 return def[0] 303 } 304 return 0 305 } 306 func ToUint64(any interface{}) (uint64, error) { 307 switch v := any.(type) { 308 case nil: 309 return 0, nil 310 case bool: 311 if v { 312 return 1, nil 313 } 314 return 0, nil 315 case uint64: 316 return v, nil 317 } 318 319 s := anyToString(any) 320 return strconv.ParseUint(s, 10, 64) 321 } 322 func GetUint64(any interface{}, def ...uint64) uint64 { 323 if a, err := ToUint64(any); err == nil { 324 return a 325 } 326 if len(def) > 0 { 327 return def[0] 328 } 329 return 0 330 } 331 332 func ToFloat32(any interface{}) (float32, error) { 333 switch v := any.(type) { 334 case nil: 335 return 0, nil 336 case bool: 337 if v { 338 return 1, nil 339 } 340 return 0, nil 341 case float32: 342 return v, nil 343 case float64: 344 return float32(v), nil 345 } 346 347 s := anyToString(any) 348 f, err := strconv.ParseFloat(s, 32) 349 if err != nil { 350 return 0, err 351 } 352 return float32(f), nil 353 } 354 func GetFloat32(any interface{}, def ...float32) float32 { 355 if a, err := ToFloat32(any); err == nil { 356 return float32(a) 357 } 358 if len(def) > 0 { 359 return def[0] 360 } 361 return 0 362 } 363 func ToFloat64(any interface{}) (float64, error) { 364 switch v := any.(type) { 365 case nil: 366 return 0, nil 367 case bool: 368 if v { 369 return 1, nil 370 } 371 return 0, nil 372 case float32: 373 return float64(v), nil 374 case float64: 375 return v, nil 376 } 377 378 s := anyToString(any) 379 return strconv.ParseFloat(s, 64) 380 } 381 func GetFloat64(any interface{}, def ...float64) float64 { 382 if a, err := ToFloat64(any); err == nil { 383 return a 384 } 385 if len(def) > 0 { 386 return def[0] 387 } 388 return 0 389 } 390 391 func ToString(a interface{}, nilToEmpty ...bool) string { 392 return anyToString(a, nilToEmpty...) 393 } 394 func GetString(a interface{}, nilToEmpty ...bool) string { 395 return anyToString(a, nilToEmpty...) 396 } 397 398 // string转bytes, 转换后的bytes禁止写, 否则产生运行故障 399 func StringToBytes(s *string) []byte { 400 sh := (*reflect.StringHeader)(unsafe.Pointer(s)) 401 bh := reflect.SliceHeader{ 402 Data: sh.Data, 403 Len: sh.Len, 404 Cap: sh.Len, 405 } 406 return *(*[]byte)(unsafe.Pointer(&bh)) 407 } 408 409 // bytes转string 410 func BytesToString(b []byte) *string { 411 return (*string)(unsafe.Pointer(&b)) 412 }