Skip to content

Commit

Permalink
Return NA values for invalid integer inputs
Browse files Browse the repository at this point in the history
I had meant to go back and do this, but never did.

Fixes #135
  • Loading branch information
jimhester committed Jun 13, 2019
1 parent 7a6173f commit 2b5473a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS.md
@@ -1,5 +1,7 @@
# vroom (development)

* The integer parser now returns NA values for invalid inputs (#135)

* The column created by `id` is now stored as an run length encoded Altrep
vector, which uses less memory and is much faster for large inputs. (#111)

Expand Down
15 changes: 14 additions & 1 deletion src/vroom_int.cc
Expand Up @@ -4,9 +4,13 @@
// A version of strtoi that doesn't need null terminated strings, to avoid
// needing to copy the data
int strtoi(const char* begin, const char* end) {
int val = 0;
double val = 0;
bool is_neg = false;

if (begin == end) {
return NA_INTEGER;
}

if (begin != end && *begin == '-') {
is_neg = true;
++begin;
Expand All @@ -16,6 +20,15 @@ int strtoi(const char* begin, const char* end) {
val = val * 10 + ((*begin++) - '0');
}

// If there is more than digits, return NA
if (begin != end) {
return NA_INTEGER;
}

if (val > INT_MAX) {
return NA_INTEGER;
}

return is_neg ? -val : val;
}

Expand Down

0 comments on commit 2b5473a

Please sign in to comment.