github.com/spi-ca/misc@v1.0.1/strutil/strconv.go (about) 1 package strutil 2 3 import ( 4 "bytes" 5 "math" 6 "strings" 7 ) 8 9 const ( 10 fingerNum = 10 11 zeroChr = '0' 12 ) 13 14 // Reverse is a function that returns a string in reverse order. 15 func Reverse(s string) string { 16 runes := []rune(s) 17 for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 { 18 runes[i], runes[j] = runes[j], runes[i] 19 } 20 return string(runes) 21 } 22 23 // FormatIntToStringReversed is a function that converts number to decimal string reversed order(LSB). 24 func FormatIntToStringReversed(number int) (out string) { 25 if number == 0 { 26 return "0" 27 } 28 var ( 29 buf strings.Builder 30 digit int 31 sign = number < 0 32 ) 33 if sign { 34 number = -number 35 } 36 for radix := 0; number > 0; radix++ { 37 digit, number = number%fingerNum, number/fingerNum 38 buf.WriteByte(zeroChr + byte(digit)) 39 } 40 if sign { 41 buf.WriteByte('-') 42 } 43 return buf.String() 44 } 45 46 // FormatIntToString is a function that converts number to decimal string(MSB). 47 func FormatIntToString(number int) (out string) { 48 if number == 0 { 49 return "0" 50 } 51 var ( 52 buf strings.Builder 53 digit int 54 sign = number < 0 55 ) 56 57 if sign { 58 number = -number 59 buf.WriteByte('-') 60 } 61 62 var ( 63 maxDigit = int(math.Log10(float64(number))) + 1 64 numBuf = make([]byte, maxDigit) 65 ) 66 67 for radix := 0; number > 0; radix++ { 68 digit, number = number%fingerNum, number/fingerNum 69 numBuf[maxDigit-radix-1] = zeroChr + byte(digit) 70 } 71 buf.Write(numBuf) 72 73 return buf.String() 74 } 75 76 // FormatIntToBytesReversed is a function that converts number to decimal byte array(ASCII code based) reversed order(LSB). 77 func FormatIntToBytesReversed(number int) (out []byte) { 78 if number == 0 { 79 return []byte("0") 80 } 81 var ( 82 buf bytes.Buffer 83 digit int 84 sign = number < 0 85 ) 86 87 if sign { 88 number = -number 89 } 90 for radix := 0; number > 0; radix++ { 91 digit, number = number%fingerNum, number/fingerNum 92 buf.WriteByte(zeroChr + byte(digit)) 93 } 94 95 if sign { 96 buf.WriteByte('-') 97 } 98 return buf.Bytes() 99 } 100 101 // FormatIntToBytes is a function that converts number to decimal byte array(ASCII code based,MSB). 102 func FormatIntToBytes(number int) (out []byte) { 103 if number == 0 { 104 return []byte("0") 105 } 106 107 var ( 108 digit int 109 sign = number < 0 110 maxDigit int 111 ) 112 113 if sign { 114 number = -number 115 maxDigit = int(math.Log10(float64(number))) + 2 116 } else { 117 maxDigit = int(math.Log10(float64(number))) + 1 118 } 119 120 out = make([]byte, maxDigit) 121 if sign { 122 out[0] = '-' 123 } 124 125 for radix := 0; number > 0; radix++ { 126 digit, number = number%fingerNum, number/fingerNum 127 out[maxDigit-radix-1] = zeroChr + byte(digit) 128 } 129 return 130 } 131 132 // ParseStringToInt is a function that converts a decimal string to integer. 133 func ParseStringToInt(in string) (number int) { 134 if len(in) == 0 { 135 return 136 } 137 138 var ( 139 out []uint8 140 sign = in[0] == '-' 141 ) 142 if sign { 143 out = make([]uint8, 0, len(in)-1) 144 in = in[1:] 145 } else { 146 out = make([]uint8, 0, len(in)) 147 } 148 for _, digit := range in { 149 if digit < '0' || digit > '9' { 150 continue 151 } 152 out = append(out, byte(digit-'0')) 153 } 154 scanned := len(out) 155 pow := 1 156 157 for i := scanned; i > 0; i-- { 158 number += pow * int(out[i-1]) 159 pow *= 10 160 } 161 162 if sign { 163 number = -number 164 } 165 return 166 } 167 168 // ParseBytesToInt is a function that converts a decimal byte array(ASCII based) to integer. 169 func ParseBytesToInt(in []byte) (number int) { 170 if len(in) == 0 { 171 return -1 172 } 173 174 var ( 175 out []uint8 176 sign = in[0] == '-' 177 ) 178 179 if sign { 180 out = make([]uint8, 0, len(in)-1) 181 in = in[1:] 182 } else { 183 out = make([]uint8, 0, len(in)) 184 } 185 186 for _, digit := range in { 187 if digit < '0' || digit > '9' { 188 continue 189 } 190 out = append(out, digit-'0') 191 } 192 scanned := len(out) 193 if scanned == 0 { 194 return -1 195 } 196 pow := 1 197 198 for i := scanned; i > 0; i-- { 199 number += pow * int(out[i-1]) 200 pow *= 10 201 } 202 203 if sign { 204 number = -number 205 } 206 return 207 } 208 209 // FormatUnsignedIntToStringReversed is a function that converts number to decimal string reversed order(LSB). 210 func FormatUnsignedIntToStringReversed(sign bool, number uint) (out string) { 211 if number == 0 { 212 return "0" 213 } 214 var ( 215 buf strings.Builder 216 digit uint 217 ) 218 219 for radix := 0; number > 0; radix++ { 220 digit, number = number%fingerNum, number/fingerNum 221 buf.WriteByte(zeroChr + byte(digit)) 222 } 223 if sign { 224 buf.WriteByte('-') 225 } 226 return buf.String() 227 } 228 229 // FormatUnsignedIntToString is a function that converts number to decimal string(MSB). 230 func FormatUnsignedIntToString(sign bool, number uint) (out string) { 231 if number == 0 { 232 return "0" 233 } 234 var ( 235 buf strings.Builder 236 digit uint 237 maxDigit = int(math.Log10(float64(number))) + 1 238 numBuf = make([]byte, maxDigit) 239 ) 240 if sign { 241 buf.WriteByte('-') 242 } 243 for radix := 0; number > 0; radix++ { 244 digit, number = number%fingerNum, number/fingerNum 245 numBuf[maxDigit-radix-1] = zeroChr + byte(digit) 246 } 247 248 buf.Write(numBuf) 249 250 return buf.String() 251 } 252 253 // FormatUnsignedIntToBytesReversed is a function that converts number to decimal byte array(ASCII code based) reversed order(LSB). 254 func FormatUnsignedIntToBytesReversed(sign bool, number uint) (out []byte) { 255 if number == 0 { 256 return []byte("0") 257 } 258 var ( 259 buf bytes.Buffer 260 digit uint 261 ) 262 for radix := 0; number > 0; radix++ { 263 digit, number = number%fingerNum, number/fingerNum 264 buf.WriteByte(zeroChr + byte(digit)) 265 } 266 if sign { 267 buf.WriteByte('-') 268 } 269 return buf.Bytes() 270 } 271 272 // FormatUnsignedIntToBytes is a function that converts number to decimal byte array(ASCII code based, MSB). 273 func FormatUnsignedIntToBytes(sign bool, number uint) (out []byte) { 274 if number == 0 { 275 return []byte("0") 276 } 277 var ( 278 digit uint 279 maxDigit = int(math.Log10(float64(number))) + 1 280 ) 281 282 if sign { 283 maxDigit++ 284 out = make([]byte, maxDigit) 285 out[0] = '-' 286 } else { 287 out = make([]byte, maxDigit) 288 } 289 290 for radix := 0; number > 0; radix++ { 291 digit, number = number%fingerNum, number/fingerNum 292 out[maxDigit-radix-1] = zeroChr + byte(digit) 293 } 294 return 295 }