Debian Codegraph @

strconv/int.go

raw
1 package strconv
2
3 import (
4 "math"
5 )
6
7 // ParseInt parses a byte-slice and returns the integer it represents.
8 // If an invalid character is encountered, it will stop there.
9 func ParseInt(b []byte) (int64, int) {
10 i := 0
11 neg := false
12 if len(b) > 0 && (b[0] == '+' || b[0] == '-') {
13 neg = b[0] == '-'
14 i++
15 }
16 start := i
17 n := uint64(0)
18 for i < len(b) {
19 c := b[i]
20 if '0' <= c && c <= '9' {
21 if uint64(-math.MinInt64)/10 < n || uint64(-math.MinInt64)-uint64(c-'0') < n*10 {
22 return 0, 0
23 }
24 n *= 10
25 n += uint64(c - '0')
26 } else {
27 break
28 }
29 i++
30 }
31 if i == start {
32 return 0, 0
33 }
34 if !neg && uint64(math.MaxInt64) < n {
35 return 0, 0
36 } else if neg {
37 return -int64(n), i
38 }
39 return int64(n), i
40 }
41
42 // ParseUint parses a byte-slice and returns the integer it represents.
43 // If an invalid character is encountered, it will stop there.
44 func ParseUint(b []byte) (uint64, int) {
45 i := 0
46 n := uint64(0)
47 for i < len(b) {
48 c := b[i]
49 if '0' <= c && c <= '9' {
50 if math.MaxUint64/10 < n || math.MaxUint64-uint64(c-'0') < n*10 {
51 return 0, 0
52 }
53 n *= 10
54 n += uint64(c - '0')
55 } else {
56 break
57 }
58 i++
59 }
60 return n, i
61 }
62
63 // AppendInt will append an int64.
64 func AppendInt(b []byte, num int64) []byte {
65 if num == 0 {
66 return append(b, '0')
67 } else if num == -9223372036854775808 {
68 return append(b, "-9223372036854775808"...)
69 }
70
71 // resize byte slice
72 i, n := len(b), LenInt(num)
73 if cap(b) < i+n {
74 b = append(b, make([]byte, n)...)
75 } else {
76 b = b[:i+n]
77 }
78
79 // print sign
80 if num < 0 {
81 num = -num
82 b[i] = '-'
83 }
84 i += n - 1
85
86 // print number
87 for num != 0 {
88 b[i] = byte(num%10) + '0'
89 num /= 10
90 i--
91 }
92 return b
93 }
94
95 // LenInt returns the written length of an integer.
96 func LenInt(i int64) int {
97 if i < 0 {
98 if i == -9223372036854775808 {
99 return 20
100 }
101 return 1 + LenUint(uint64(-i))
102 }
103 return LenUint(uint64(i))
104 }
105
106 func LenUint(i uint64) int {
107 switch {
108 case i < 10:
109 return 1
110 case i < 100:
111 return 2
112 case i < 1000:
113 return 3
114 case i < 10000:
115 return 4
116 case i < 100000:
117 return 5
118 case i < 1000000:
119 return 6
120 case i < 10000000:
121 return 7
122 case i < 100000000:
123 return 8
124 case i < 1000000000:
125 return 9
126 case i < 10000000000:
127 return 10
128 case i < 100000000000:
129 return 11
130 case i < 1000000000000:
131 return 12
132 case i < 10000000000000:
133 return 13
134 case i < 100000000000000:
135 return 14
136 case i < 1000000000000000:
137 return 15
138 case i < 10000000000000000:
139 return 16
140 case i < 100000000000000000:
141 return 17
142 case i < 1000000000000000000:
143 return 18
144 case i < 10000000000000000000:
145 return 19
146 }
147 return 20
148 }
149
150 var int64pow10 = []int64{
151 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 10000000000000000, 100000000000000000, 1000000000000000000,
152 }