Skip to content

Commit

Permalink
Fix the edge case of exclusive leading spaces when trim_ws's FALSE
Browse files Browse the repository at this point in the history
  • Loading branch information
chainsawriot committed Jun 11, 2024
1 parent 72e70d6 commit 00bb734
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 0 additions & 2 deletions src/QiParsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ bsd_strtod(const char* begin, const char** endptr, const char decimal_mark) {
* Strip off leading blanks and check for a sign.
*/
p = begin;
while (p != *endptr && (*p == ' ' || *p == '\t'))
++p;
if (p != *endptr && *p == '-') {
sign = 1;
++p;
Expand Down
6 changes: 2 additions & 4 deletions tests/testthat/test-parsing.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ test_that("parse_guess() guess_max", {
test_that("parse_guess() trim_ws #32 or tidyverse/readr#1536", {
expect_equal(parse_guess(c(" 1", "2 ", " 3 "), trim_ws = TRUE), c(1, 2, 3))
expect_equal(parse_guess(c(" 1", "2 ", " 3 "), trim_ws = FALSE), c(" 1", "2 ", " 3 "))
## exclusive leading and trim_ws = FALSE, won't be parsed as numeric
expect_equal(parse_guess(c(" 1", " 2", " 3"), trim_ws = FALSE), c(" 1", " 2", " 3"))
expect_equal(parse_guess(c(" TRUE", "FALSE ", " T "), trim_ws = TRUE), c(TRUE, FALSE, TRUE))
## integration in type_convert()
x <- type_convert(data.frame(a = c("1 ", " 1"), b = c(" 2", " 2")), trim_ws = TRUE)
expect_equal(class(x$a), "numeric")
expect_equal(class(x$b), "numeric")
})
9 changes: 9 additions & 0 deletions tests/testthat/test-type-convert.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ test_that("skip behaviors, readr#1509 or minty#20", {
expect_error(minty::type_convert(text_only, col_types = list("-", "-")), NA)
})

test_that("type_convert() trim_ws #32 or tidyverse/readr#1536", {
## integration of guess_parse in type_convert()
x <- type_convert(data.frame(a = c("1 ", " 1"), b = c(" 2", " 2")), trim_ws = TRUE)
expect_equal(class(x$a), "numeric")
expect_equal(class(x$b), "numeric")
x <- type_convert(data.frame(a = c(" 1")), trim_ws = FALSE)
expect_equal(class(x$a), "character")
})

test_that("r_is_string_cpp11", {
expect_true(r_is_string_cpp11("a"))
expect_true(r_is_string_cpp11(c("a")))
Expand Down

0 comments on commit 00bb734

Please sign in to comment.