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

Function parseFloatString

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

Source from the content-addressed store, hash-verified

171}
172
173func parseFloatString(s string) float64 {
174 var hasDot, hasExp bool
175
176 // <a>
177 // <a>.<b>
178 // <a>.<b>e<c>
179 // <a>e<c>
180 var a, b, c, rest string
181
182 a, rest, hasDot = strings.Cut(s, ".")
183 if hasDot {
184 // <a>.<b>
185 // <a>.<b>e<c>
186 b, c, hasExp = cutAny(rest, "eE")
187 } else {
188 // <a>
189 // <a>e<c>
190 a, c, hasExp = cutAny(s, "eE")
191 }
192
193 var sb strings.Builder
194 sb.Grow(len(a) + len(b) + len(c) + 3)
195
196 if a == "" {
197 if hasDot && b == "" {
198 return math.NaN()
199 }
200 if hasExp && c == "" {
201 return math.NaN()
202 }
203 sb.WriteString("0")
204 } else {
205 a = trimLeadingZeros(a)
206 if !isAllDigits(a) {
207 return math.NaN()
208 }
209 sb.WriteString(a)
210 }
211
212 if hasDot {
213 sb.WriteString(".")
214 if b == "" {
215 sb.WriteString("0")
216 } else {
217 b = trimTrailingZeros(b)
218 if !isAllDigits(b) {
219 return math.NaN()
220 }
221 sb.WriteString(b)
222 }
223 }
224
225 if hasExp {
226 sb.WriteString("e")
227
228 c, negative := strings.CutPrefix(c, "-")
229 if negative {
230 sb.WriteString("-")

Callers 1

FromStringFunction · 0.85

Calls 8

cutAnyFunction · 0.85
lenFunction · 0.85
trimLeadingZerosFunction · 0.85
isAllDigitsFunction · 0.85
trimTrailingZerosFunction · 0.85
stringToFloat64Function · 0.85
GrowMethod · 0.80
StringMethod · 0.65

Tested by

no test coverage detected