Skip to content

Commit

Permalink
inf or nan parsing should ignore leading spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj committed Apr 12, 2019
1 parent b471640 commit 65f94b3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/support/strtod.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 == '-') {
Expand Down
7 changes: 7 additions & 0 deletions test/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 65f94b3

Please sign in to comment.