Debian Codegraph @

pkg/util/number.go

raw
1 package util
2
3 import "strings"
4
5 // hex is the base used for hexadecimal number parsing.
6 const hex int = 16
7
8 // StripNumberPrefix removes hexadecimal prefixes from number strings.
9 //
10 // It recognizes the following prefixes:
11 // - "#" (e.g., "#FF" becomes "FF")
12 // - "0x" or "0X" (e.g., "0xFF" becomes "FF")
13 //
14 // Parameters:
15 // - input: The number string that may contain a base prefix.
16 //
17 // Returns:
18 // - The input string with any recognized prefix removed.
19 // - The base (16 for hex prefixes, 0 if no prefix was found).
20 // A return value of 0 indicates that strconv should attempt to identify the base.
21 func StripNumberPrefix(input string) (string, int) {
22 if strings.HasPrefix(input, "#") {
23 return input[1:], hex
24 }
25
26 if strings.HasPrefix(input, "0x") || strings.HasPrefix(input, "0X") {
27 return input[2:], hex
28 }
29
30 return input, 0
31 }