github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/segment/utils/numberutils.go (about) 1 /* 2 Copyright 2023. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package utils 18 19 import "strconv" 20 21 func GetNumberTypeAndVal(numstr string) (SS_IntUintFloatTypes, int64, uint64, float64) { 22 firstchar := numstr[0] 23 var numType SS_IntUintFloatTypes 24 var intVal int64 25 var uintVal uint64 26 var fltVal float64 27 var ok bool 28 29 //ToDo : Assume float values are specified using decimal point and do not contain e/E 30 //if strings.Contains(strings.ToLower(numstr), "e") { 31 // numstr = convertFloatToInt(numstr) 32 //} 33 if firstchar == '-' { 34 if numType, intVal, ok = getIntTypeAndVal(numstr); ok { 35 //fmt.Println("signed int", numType, intVal, numstr) 36 return numType, intVal, uintVal, fltVal 37 } else if numType, fltVal, ok = getFloatTypeAndVal(numstr); ok { 38 //fmt.Println("Float", numType, fltVal, numstr) 39 return numType, intVal, uintVal, fltVal 40 } 41 } else { 42 if numType, uintVal, ok = getUintTypeAndVal(numstr); ok { 43 //fmt.Println("Unsigned int", numType, uintVal, numstr) 44 return numType, intVal, uintVal, fltVal 45 } else if numType, fltVal, ok = getFloatTypeAndVal(numstr); ok { 46 //fmt.Println("Float", numType, fltVal, numstr) 47 return numType, intVal, uintVal, fltVal 48 } 49 } 50 return numType, intVal, uintVal, fltVal 51 } 52 func getFloatTypeAndVal(strnum string) (SS_IntUintFloatTypes, float64, bool) { 53 if fltval, err := strconv.ParseFloat(strnum, 64); err == nil { 54 //fmt.Printf("packRecord: got float=%v\n", valconv) 55 if fltval == 0 { 56 return SS_UINT8, fltval, true 57 } else { 58 return SS_FLOAT64, fltval, true 59 } 60 } else { 61 return -1, 0, false 62 } 63 } 64 65 func getUintTypeAndVal(strnum string) (SS_IntUintFloatTypes, uint64, bool) { 66 if bigval, err := strconv.ParseUint(strnum, 10, 64); err == nil { 67 switch { 68 case bigval == 0: 69 return SS_UINT8, bigval, true 70 case bigval <= 255: 71 return SS_UINT8, bigval, true 72 case bigval <= 65535: 73 return SS_UINT16, bigval, true 74 case bigval <= 4294967295: 75 return SS_UINT32, bigval, true 76 default: 77 return SS_UINT64, bigval, true 78 } 79 } 80 return -1, 0, false 81 } 82 83 func getIntTypeAndVal(strnum string) (SS_IntUintFloatTypes, int64, bool) { 84 if bigval, err := strconv.ParseInt(strnum, 10, 64); err == nil { 85 switch { 86 case bigval == 0: 87 return SS_UINT8, bigval, true 88 case bigval >= -128 && bigval <= 127: 89 return SS_INT8, bigval, true 90 case bigval >= -32768 && bigval <= 32767: 91 return SS_INT16, bigval, true 92 case bigval >= -2147483648 && bigval <= 2147483647: 93 return SS_INT32, bigval, true 94 default: 95 return SS_INT64, bigval, true 96 } 97 } 98 return -1, 0, false 99 }