MCPcopy Create free account
hub / github.com/microsoft/typescript-go / tryParseInt

Function tryParseInt

internal/jsnum/string.go:120–171  ·  view source on GitHub ↗
(s string)

Source from the content-addressed store, hash-verified

118var errUnknownPrefix = errors.New("unknown number prefix")
119
120func tryParseInt(s string) (Number, bool) {
121 var i int64
122 var err error
123 var hasIntResult bool
124
125 if len(s) > 2 {
126 prefix, rest := s[:2], s[2:]
127 switch prefix {
128 case "0b", "0B":
129 if !isAllBinaryDigits(rest) {
130 return NaN(), true
131 }
132 i, err = strconv.ParseInt(rest, 2, 64)
133 hasIntResult = true
134 case "0o", "0O":
135 if !isAllOctalDigits(rest) {
136 return NaN(), true
137 }
138 i, err = strconv.ParseInt(rest, 8, 64)
139 hasIntResult = true
140 case "0x", "0X":
141 if !isAllHexDigits(rest) {
142 return NaN(), true
143 }
144 i, err = strconv.ParseInt(rest, 16, 64)
145 hasIntResult = true
146 }
147 }
148
149 if !hasIntResult {
150 // StringToNumber does not parse leading zeros as octal.
151 s = trimLeadingZeros(s)
152 if !isAllDigits(s) {
153 return 0, false
154 }
155 i, err = strconv.ParseInt(s, 10, 64)
156 hasIntResult = true
157 }
158
159 if hasIntResult && err == nil {
160 return Number(i), true
161 }
162
163 // Using this to parse large integers.
164 bi, ok := new(big.Int).SetString(s, 0)
165 if !ok {
166 return NaN(), true
167 }
168
169 f, _ := bi.Float64()
170 return Number(f), true
171}
172
173func parseFloatString(s string) float64 {
174 var hasDot, hasExp bool

Callers 1

FromStringFunction · 0.85

Calls 9

lenFunction · 0.85
isAllBinaryDigitsFunction · 0.85
NaNFunction · 0.85
isAllOctalDigitsFunction · 0.85
isAllHexDigitsFunction · 0.85
trimLeadingZerosFunction · 0.85
isAllDigitsFunction · 0.85
NumberTypeAlias · 0.70
newFunction · 0.50

Tested by

no test coverage detected