Skip to content

Commit

Permalink
Fix parsing of NaN and Inf to only be that exact strings, rather than…
Browse files Browse the repository at this point in the history
… extensions

We don't want to parse NaNa, Inform or Info as doubles

Fixes tidyverse/readr#1319
  • Loading branch information
jimhester committed Nov 5, 2021
1 parent c5b115b commit 7fc1a1d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# vroom (development version)

* Values with only a `Inf` or `NaN` prefix but additional data afterwards, like
`Inform` or no longer inappropriately guessed as doubles (https://github.com/tidyverse/readr/issues/1319)

* Time types now support `%h` format to denote hour durations greater than 24, like readr (https://github.com/tidyverse/readr/issues/1312)

* Fix performance issue when materializing subsetted vectors (#378)
Expand Down
4 changes: 2 additions & 2 deletions src/vroom_dbl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ double bsd_strtod(const char* begin, const char* end, const char decimalMark) {
++p;

/* NaN */
if (end - p >= 3 && tolower(p[0]) == 'n' && tolower(p[1]) == 'a' &&
if (end - p == 3 && tolower(p[0]) == 'n' && tolower(p[1]) == 'a' &&
tolower(p[2]) == 'n') {
return NAN;
}
/* Inf */
if (end - p >= 3 && tolower(p[0]) == 'i' && tolower(p[1]) == 'n' &&
if (end - p == 3 && tolower(p[0]) == 'i' && tolower(p[1]) == 'n' &&
tolower(p[2]) == 'f') {
return sign == 1 ? -HUGE_VAL : HUGE_VAL;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-dbl.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@ test_that("NaN values are guessed and parsed as doubles (https://github.com/tidy
equals = tibble::tibble(x = c(NaN)))
})

test_that("Test that values with only a NaN prefix are _not_ parsed as doubles", {
test_vroom(I("x\nNaNa\n"), delim = ",", col_types = "?",
equals = tibble::tibble(x = c("NaNa")))
})

test_that("Inf and -Inf values are guessed and parsed as doubles (https://github.com/tidyverse/readr/issues/1283)", {
test_vroom(I("x\nInf\n-Inf\n+Inf"), delim = ",", col_types = "?",
equals = tibble::tibble(x = c(Inf, -Inf, Inf)))
})

test_that("Test that values with only a Inf prefix are _not_ parsed as doubles", {
test_vroom(I("x\nInfa\n-Infb\n+Infc"), delim = ",", col_types = "?",
equals = tibble::tibble(x = c("Infa", "-Infb", "+Infc")))
})

0 comments on commit 7fc1a1d

Please sign in to comment.