Skip to content

fread: parse quoted numeric fields containing leading or trailing whitespace - #7962

Open
lds295 wants to merge 3 commits into
Rdatatable:masterfrom
loganb7869:fix-fread-quoted-numbers-whitespace
Open

lds295 wants to merge 3 commits into
Rdatatable:masterfrom
loganb7869:fix-fread-quoted-numbers-whitespace

Conversation

@lds295

@lds295 lds295 commented Nov 17, 2026

Copy link
Copy Markdown

Summary

Fixes an issue where fread() incorrectly parsed numbers enclosed in quotation marks as character when leading or trailing whitespace was present inside the quotes (e.g. " 123" or "123 "), whereas base read.table(..., strip.white=TRUE) parses them as numeric.

Root Cause

During type detection and data reading in src/fread.c, fread advanced over the opening quote (ch++) directly onto whitespace without calling skip_white(). Because numeric parsers strictly require immediate signs or digits, they rejected the whitespace and defaulted the column type to CT_STRING.

Adding skip_white() after opening quotes and before closing quotes allows fread to skip inner whitespace around numbers, matching the behavior of unquoted numbers and read.table().

Example

tmp <- tempfile(fileext = ".csv")
writeLines(c(
  '"stringvar";"numvar"',
  '"abc";" 123"',
  '"def";" 456"',
  '"ghi";" 789"'
), tmp)

fread(tmp, sep=";", quote='"', strip.white=TRUE)
# Before: numvar was <character>
# After:  numvar is  <integer>

Checklist

  • Added unit tests in inst/tests/tests.Rraw
  • Added entry to NEWS.md
  • Tests pass

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants