From 65f94b35e37ec6dc268bcdc4fbabecef0c14ce45 Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Fri, 12 Apr 2019 08:17:11 -0400 Subject: [PATCH] inf or nan parsing should ignore leading spaces --- src/support/strtod.c | 17 +++++++++-------- test/parse.jl | 7 +++++++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/support/strtod.c b/src/support/strtod.c index 047d404d90cda..225f2dbd4c933 100644 --- a/src/support/strtod.c +++ b/src/support/strtod.c @@ -118,9 +118,16 @@ JL_DLLEXPORT double jl_strtod_c(const char *nptr, char **endptr) decimal_point_pos = NULL; + p = nptr; + + /* parse leading spaces */ + while (isspace((unsigned char)*p)) { + p++; + } + /* Parse infinities and nans */ - val = parse_inf_or_nan(nptr, endptr); - if (*endptr != nptr) + val = parse_inf_or_nan(p, endptr); + if (*endptr != p) return val; /* Set errno to zero, so that we can distinguish zero results @@ -130,12 +137,6 @@ JL_DLLEXPORT double jl_strtod_c(const char *nptr, char **endptr) /* We process the optional sign manually, then pass the remainder to the system strtod. This ensures that the result of an underflow has the correct sign. */ - p = nptr; - - /* parse leading spaces */ - while (isspace((unsigned char)*p)) { - p++; - } /* Process leading sign, if present */ if (*p == '-') { diff --git a/test/parse.jl b/test/parse.jl index 45e13a95b7f90..95c3bb492ffc5 100644 --- a/test/parse.jl +++ b/test/parse.jl @@ -338,3 +338,10 @@ end # Ensure dotting binary doesn't break dotting unary @test Meta.parse(".~[1,2]") == Expr(:call, :.~, Expr(:vect, 1, 2)) end + +@testset "inf and nan parsing" begin + for (v,vs) in ((NaN,"nan"), (Inf,"inf"), (Inf,"infinity")), sbefore in ("", " "), safter in ("", " "), sign in (+, -), case in (lowercase, uppercase) + s = case(string(sbefore, sign, vs, safter)) + @test isequal(parse(Float64, s), sign(v)) + end +end