github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/tools/strings.go (about) 1 package tools 2 3 import ( 4 "encoding/binary" 5 "reflect" 6 "strconv" 7 "strings" 8 "unsafe" 9 ) 10 11 /* 12 Creation Time: 2021 - Mar - 29 13 Created by: (ehsan) 14 Maintainers: 15 1. Ehsan N. Moosa (E2) 16 Auditor: Ehsan N. Moosa (E2) 17 Copyright Ronak Software Group 2020 18 */ 19 20 /* 21 Strings Builder helper functions 22 */ 23 24 func AppendStrInt(sb *strings.Builder, x int) { 25 var b [8]byte 26 binary.BigEndian.PutUint64(b[:], uint64(x)) 27 sb.Write(b[:]) 28 } 29 30 func AppendStrUInt(sb *strings.Builder, x uint) { 31 var b [8]byte 32 binary.BigEndian.PutUint64(b[:], uint64(x)) 33 sb.Write(b[:]) 34 } 35 36 func AppendStrInt64(sb *strings.Builder, x int64) { 37 var b [8]byte 38 binary.BigEndian.PutUint64(b[:], uint64(x)) 39 sb.Write(b[:]) 40 } 41 42 func AppendStrUInt64(sb *strings.Builder, x uint64) { 43 var b [8]byte 44 binary.BigEndian.PutUint64(b[:], x) 45 sb.Write(b[:]) 46 } 47 48 func AppendStrInt32(sb *strings.Builder, x int32) { 49 var b [4]byte 50 binary.BigEndian.PutUint32(b[:], uint32(x)) 51 sb.Write(b[:]) 52 } 53 54 func AppendStrUInt32(sb *strings.Builder, x uint32) { 55 var b [4]byte 56 binary.BigEndian.PutUint32(b[:], x) 57 sb.Write(b[:]) 58 } 59 60 /* 61 String Conversion helper functions 62 */ 63 64 func StrToFloat64(s string) float64 { 65 v, _ := strconv.ParseFloat(s, 64) 66 67 return v 68 } 69 70 func StrToFloat32(s string) float32 { 71 v, _ := strconv.ParseFloat(s, 32) 72 73 return float32(v) 74 } 75 76 func StrToInt64(s string) int64 { 77 v, _ := strconv.ParseInt(s, 10, 64) 78 79 return v 80 } 81 82 func StrToInt32(s string) int32 { 83 v, _ := strconv.ParseInt(s, 10, 32) 84 85 return int32(v) 86 } 87 88 func StrToUInt64(s string) uint64 { 89 v, _ := strconv.ParseInt(s, 10, 64) 90 91 return uint64(v) 92 } 93 94 func StrToUInt32(s string) uint32 { 95 v, _ := strconv.ParseInt(s, 10, 32) 96 97 return uint32(v) 98 } 99 100 func StrToInt(s string) int { 101 v, _ := strconv.ParseInt(s, 10, 32) 102 103 return int(v) 104 } 105 106 func Int64ToStr(x int64) string { 107 return strconv.FormatInt(x, 10) 108 } 109 110 func Int32ToStr(x int32) string { 111 return strconv.FormatInt(int64(x), 10) 112 } 113 114 func UInt64ToStr(x uint64) string { 115 return strconv.FormatUint(x, 10) 116 } 117 118 func UInt32ToStr(x uint32) string { 119 return strconv.FormatUint(uint64(x), 10) 120 } 121 122 func IntToStr(x int) string { 123 return strconv.FormatUint(uint64(x), 10) 124 } 125 126 // ByteToStr converts byte slice to a string without memory allocation. 127 // Note it may break if string and/or slice header will change 128 // in the future go versions. 129 func ByteToStr(bts []byte) string { 130 /* #nosec G103 */ 131 return *(*string)(unsafe.Pointer(&bts)) 132 } 133 134 // B2S is alias for ByteToStr 135 func B2S(bts []byte) string { 136 return ByteToStr(bts) 137 } 138 139 // StrToByte converts string to a byte slice without memory allocation. 140 // Note it may break if string and/or slice header will change 141 // in the future go versions. 142 func StrToByte(str string) (b []byte) { 143 /* #nosec G103 */ 144 bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) 145 /* #nosec G103 */ 146 sh := (*reflect.StringHeader)(unsafe.Pointer(&str)) 147 bh.Data = sh.Data 148 bh.Len = sh.Len 149 bh.Cap = sh.Len 150 151 return b 152 } 153 154 // S2B is alias for StrToByte 155 func S2B(str string) []byte { 156 return StrToByte(str) 157 }