| ... | ... | @@ -30,6 +30,7 @@ |
| 30 | 30 | // - Does not handle denormals |
| 31 | 31 | |
| 32 | 32 | const std = @import("../std.zig"); |
| 33 | const ascii = std.ascii; |
| 33 | 34 | |
| 34 | 35 | const max_digits = 25; |
| 35 | 36 | |
| ... | ... | @@ -190,14 +191,6 @@ const ParseResult = enum { |
| 190 | 191 | MinusInf, |
| 191 | 192 | }; |
| 192 | 193 | |
| 193 | | inline fn isDigit(c: u8) bool { |
| 194 | | return c >= '0' and c <= '9'; |
| 195 | | } |
| 196 | | |
| 197 | | inline fn isSpace(c: u8) bool { |
| 198 | | return (c >= 0x09 and c <= 0x13) or c == 0x20; |
| 199 | | } |
| 200 | | |
| 201 | 194 | fn parseRepr(s: []const u8, n: *FloatRepr) !ParseResult { |
| 202 | 195 | var digit_index: usize = 0; |
| 203 | 196 | var negative = false; |
| ... | ... | @@ -207,52 +200,49 @@ fn parseRepr(s: []const u8, n: *FloatRepr) !ParseResult { |
| 207 | 200 | var state = State.MaybeSign; |
| 208 | 201 | |
| 209 | 202 | var i: usize = 0; |
| 210 | | loop: while (i < s.len) { |
| 203 | while (i < s.len) { |
| 211 | 204 | const c = s[i]; |
| 212 | 205 | |
| 213 | 206 | switch (state) { |
| 214 | | State.MaybeSign => { |
| 215 | | state = State.LeadingMantissaZeros; |
| 207 | .MaybeSign => { |
| 208 | state = .LeadingMantissaZeros; |
| 216 | 209 | |
| 217 | 210 | if (c == '+') { |
| 218 | 211 | i += 1; |
| 219 | 212 | } else if (c == '-') { |
| 220 | 213 | n.negative = true; |
| 221 | 214 | i += 1; |
| 222 | | } else if (isDigit(c) or c == '.') { |
| 215 | } else if (ascii.isDigit(c) or c == '.') { |
| 223 | 216 | // continue |
| 224 | 217 | } else { |
| 225 | 218 | return error.InvalidCharacter; |
| 226 | 219 | } |
| 227 | 220 | }, |
| 228 | | |
| 229 | | State.LeadingMantissaZeros => { |
| 221 | .LeadingMantissaZeros => { |
| 230 | 222 | if (c == '0') { |
| 231 | 223 | i += 1; |
| 232 | 224 | } else if (c == '.') { |
| 233 | 225 | i += 1; |
| 234 | | state = State.LeadingFractionalZeros; |
| 226 | state = .LeadingFractionalZeros; |
| 235 | 227 | } else { |
| 236 | | state = State.MantissaIntegral; |
| 228 | state = .MantissaIntegral; |
| 237 | 229 | } |
| 238 | 230 | }, |
| 239 | | |
| 240 | | State.LeadingFractionalZeros => { |
| 231 | .LeadingFractionalZeros => { |
| 241 | 232 | if (c == '0') { |
| 242 | 233 | i += 1; |
| 243 | 234 | if (n.exponent > std.math.minInt(i32)) { |
| 244 | 235 | n.exponent -= 1; |
| 245 | 236 | } |
| 246 | 237 | } else { |
| 247 | | state = State.MantissaFractional; |
| 238 | state = .MantissaFractional; |
| 248 | 239 | } |
| 249 | 240 | }, |
| 250 | | |
| 251 | | State.MantissaIntegral => { |
| 252 | | if (isDigit(c)) { |
| 241 | .MantissaIntegral => { |
| 242 | if (ascii.isDigit(c)) { |
| 253 | 243 | if (digit_index < max_digits) { |
| 254 | 244 | n.mantissa *%= 10; |
| 255 | | n.mantissa += s[i] - '0'; |
| 245 | n.mantissa += c - '0'; |
| 256 | 246 | digit_index += 1; |
| 257 | 247 | } else if (n.exponent < std.math.maxInt(i32)) { |
| 258 | 248 | n.exponent += 1; |
| ... | ... | @@ -261,14 +251,13 @@ fn parseRepr(s: []const u8, n: *FloatRepr) !ParseResult { |
| 261 | 251 | i += 1; |
| 262 | 252 | } else if (c == '.') { |
| 263 | 253 | i += 1; |
| 264 | | state = State.MantissaFractional; |
| 254 | state = .MantissaFractional; |
| 265 | 255 | } else { |
| 266 | | state = State.MantissaFractional; |
| 256 | state = .MantissaFractional; |
| 267 | 257 | } |
| 268 | 258 | }, |
| 269 | | |
| 270 | | State.MantissaFractional => { |
| 271 | | if (isDigit(c)) { |
| 259 | .MantissaFractional => { |
| 260 | if (ascii.isDigit(c)) { |
| 272 | 261 | if (digit_index < max_digits) { |
| 273 | 262 | n.mantissa *%= 10; |
| 274 | 263 | n.mantissa += c - '0'; |
| ... | ... | @@ -279,13 +268,12 @@ fn parseRepr(s: []const u8, n: *FloatRepr) !ParseResult { |
| 279 | 268 | i += 1; |
| 280 | 269 | } else if (c == 'e' or c == 'E') { |
| 281 | 270 | i += 1; |
| 282 | | state = State.ExponentSign; |
| 271 | state = .ExponentSign; |
| 283 | 272 | } else { |
| 284 | | state = State.ExponentSign; |
| 273 | state = .ExponentSign; |
| 285 | 274 | } |
| 286 | 275 | }, |
| 287 | | |
| 288 | | State.ExponentSign => { |
| 276 | .ExponentSign => { |
| 289 | 277 | if (c == '+') { |
| 290 | 278 | i += 1; |
| 291 | 279 | } else if (c == '-') { |
| ... | ... | @@ -293,20 +281,18 @@ fn parseRepr(s: []const u8, n: *FloatRepr) !ParseResult { |
| 293 | 281 | i += 1; |
| 294 | 282 | } |
| 295 | 283 | |
| 296 | | state = State.LeadingExponentZeros; |
| 284 | state = .LeadingExponentZeros; |
| 297 | 285 | }, |
| 298 | | |
| 299 | | State.LeadingExponentZeros => { |
| 286 | .LeadingExponentZeros => { |
| 300 | 287 | if (c == '0') { |
| 301 | 288 | i += 1; |
| 302 | 289 | } else { |
| 303 | | state = State.Exponent; |
| 290 | state = .Exponent; |
| 304 | 291 | } |
| 305 | 292 | }, |
| 306 | | |
| 307 | | State.Exponent => { |
| 308 | | if (isDigit(c)) { |
| 309 | | if (exponent < std.math.maxInt(i32)) { |
| 293 | .Exponent => { |
| 294 | if (ascii.isDigit(c)) { |
| 295 | if (exponent < std.math.maxInt(i32) / 10) { |
| 310 | 296 | exponent *= 10; |
| 311 | 297 | exponent += @intCast(i32, c - '0'); |
| 312 | 298 | } |
| ... | ... | @@ -323,29 +309,21 @@ fn parseRepr(s: []const u8, n: *FloatRepr) !ParseResult { |
| 323 | 309 | n.exponent += exponent; |
| 324 | 310 | |
| 325 | 311 | if (n.mantissa == 0) { |
| 326 | | return if (n.negative) ParseResult.MinusZero else ParseResult.PlusZero; |
| 312 | return if (n.negative) .MinusZero else .PlusZero; |
| 327 | 313 | } else if (n.exponent > 309) { |
| 328 | | return if (n.negative) ParseResult.MinusInf else ParseResult.PlusInf; |
| 314 | return if (n.negative) .MinusInf else .PlusInf; |
| 329 | 315 | } else if (n.exponent < -328) { |
| 330 | | return if (n.negative) ParseResult.MinusZero else ParseResult.PlusZero; |
| 316 | return if (n.negative) .MinusZero else .PlusZero; |
| 331 | 317 | } |
| 332 | 318 | |
| 333 | | return ParseResult.Ok; |
| 334 | | } |
| 335 | | |
| 336 | | inline fn isLower(c: u8) bool { |
| 337 | | return c -% 'a' < 26; |
| 338 | | } |
| 339 | | |
| 340 | | inline fn toUpper(c: u8) u8 { |
| 341 | | return if (isLower(c)) (c & 0x5f) else c; |
| 319 | return .Ok; |
| 342 | 320 | } |
| 343 | 321 | |
| 344 | 322 | fn caseInEql(a: []const u8, b: []const u8) bool { |
| 345 | 323 | if (a.len != b.len) return false; |
| 346 | 324 | |
| 347 | 325 | for (a) |_, i| { |
| 348 | | if (toUpper(a[i]) != toUpper(b[i])) { |
| 326 | if (ascii.toUpper(a[i]) != ascii.toUpper(b[i])) { |
| 349 | 327 | return false; |
| 350 | 328 | } |
| 351 | 329 | } |
| ... | ... | @@ -373,11 +351,11 @@ pub fn parseFloat(comptime T: type, s: []const u8) !T { |
| 373 | 351 | }; |
| 374 | 352 | |
| 375 | 353 | return switch (try parseRepr(s, &r)) { |
| 376 | | ParseResult.Ok => convertRepr(T, r), |
| 377 | | ParseResult.PlusZero => 0.0, |
| 378 | | ParseResult.MinusZero => -@as(T, 0.0), |
| 379 | | ParseResult.PlusInf => std.math.inf(T), |
| 380 | | ParseResult.MinusInf => -std.math.inf(T), |
| 354 | .Ok => convertRepr(T, r), |
| 355 | .PlusZero => 0.0, |
| 356 | .MinusZero => -@as(T, 0.0), |
| 357 | .PlusInf => std.math.inf(T), |
| 358 | .MinusInf => -std.math.inf(T), |
| 381 | 359 | }; |
| 382 | 360 | } |
| 383 | 361 | |
| ... | ... | @@ -396,26 +374,28 @@ test "fmt.parseFloat" { |
| 396 | 374 | testing.expectError(error.InvalidCharacter, parseFloat(T, "1abc")); |
| 397 | 375 | |
| 398 | 376 | expectEqual(try parseFloat(T, "0"), 0.0); |
| 399 | | expectEqual((try parseFloat(T, "0")), 0.0); |
| 400 | | expectEqual((try parseFloat(T, "+0")), 0.0); |
| 401 | | expectEqual((try parseFloat(T, "-0")), 0.0); |
| 377 | expectEqual(try parseFloat(T, "0"), 0.0); |
| 378 | expectEqual(try parseFloat(T, "+0"), 0.0); |
| 379 | expectEqual(try parseFloat(T, "-0"), 0.0); |
| 402 | 380 | |
| 403 | | expectEqual((try parseFloat(T, "0e0")), 0); |
| 404 | | expectEqual((try parseFloat(T, "2e3")), 2000.0); |
| 405 | | expectEqual((try parseFloat(T, "1e0")), 1.0); |
| 406 | | expectEqual((try parseFloat(T, "-2e3")), -2000.0); |
| 407 | | expectEqual((try parseFloat(T, "-1e0")), -1.0); |
| 408 | | expectEqual((try parseFloat(T, "1.234e3")), 1234); |
| 381 | expectEqual(try parseFloat(T, "0e0"), 0); |
| 382 | expectEqual(try parseFloat(T, "2e3"), 2000.0); |
| 383 | expectEqual(try parseFloat(T, "1e0"), 1.0); |
| 384 | expectEqual(try parseFloat(T, "-2e3"), -2000.0); |
| 385 | expectEqual(try parseFloat(T, "-1e0"), -1.0); |
| 386 | expectEqual(try parseFloat(T, "1.234e3"), 1234); |
| 409 | 387 | |
| 410 | 388 | expect(approxEq(T, try parseFloat(T, "3.141"), 3.141, epsilon)); |
| 411 | 389 | expect(approxEq(T, try parseFloat(T, "-3.141"), -3.141, epsilon)); |
| 412 | 390 | |
| 413 | | expectEqual((try parseFloat(T, "1e-700")), 0); |
| 414 | | expectEqual((try parseFloat(T, "1e+700")), std.math.inf(T)); |
| 391 | expectEqual(try parseFloat(T, "1e-700"), 0); |
| 392 | expectEqual(try parseFloat(T, "1e+700"), std.math.inf(T)); |
| 415 | 393 | |
| 416 | 394 | expectEqual(@bitCast(Z, try parseFloat(T, "nAn")), @bitCast(Z, std.math.nan(T))); |
| 417 | | expectEqual((try parseFloat(T, "inF")), std.math.inf(T)); |
| 418 | | expectEqual((try parseFloat(T, "-INF")), -std.math.inf(T)); |
| 395 | expectEqual(try parseFloat(T, "inF"), std.math.inf(T)); |
| 396 | expectEqual(try parseFloat(T, "-INF"), -std.math.inf(T)); |
| 397 | |
| 398 | expectEqual(try parseFloat(T, "0.4e0066999999999999999999999999999999999999999999999999999"), std.math.inf(T)); |
| 419 | 399 | |
| 420 | 400 | if (T != f16) { |
| 421 | 401 | expect(approxEq(T, try parseFloat(T, "1e-2"), 0.01, epsilon)); |