Skip to content

Commit

Permalink
Parse numbers with leading zeros as character.
Browse files Browse the repository at this point in the history
Fixes #266
  • Loading branch information
hadley committed Sep 22, 2015
1 parent b128cc5 commit ec04976
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# readr 0.1.1.9000

* Numbers with leading zeros now default to being parsed as text, rather than
as integers/doubles. Override with `col_integer()` or `col_double()` (#266).

* `read_fwf()` is now much more careful with new lines. The last column
can now be ragged: the width of the last field is silently extended until
it hits the next line break (#146). If a line is too short, you'll get
Expand Down
10 changes: 10 additions & 0 deletions src/CollectorGuess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ bool isLogical(const std::string& x, LocaleInfo* pLocale) {
bool isInteger(const std::string& x, LocaleInfo* pLocale) {
int res = 0;
std::string::const_iterator begin = x.begin(), end = x.end();
if (x[0] == '0' && x.size() > 1)
return false;

return parseInt(begin, end, res) && begin == end;
}
Expand All @@ -55,6 +57,10 @@ bool canParseNumber(CharacterVector x, LocaleInfo* pLocale) {
std::string xstr = std::string(x[i]);
std::string::const_iterator begin = xstr.begin(), end = xstr.end();

// Leading zero not followed by decimal mark
if (xstr[0] == '0' && xstr.size() > 1 && xstr[1] != pLocale->decimalMark_)
return false;

bool ok = parseNumber(pLocale->decimalMark_, pLocale->groupingMark_,
begin, end, tmp);
if (!ok)
Expand All @@ -69,6 +75,10 @@ bool canParseNumber(CharacterVector x, LocaleInfo* pLocale) {
}

bool isDouble(const std::string& x, LocaleInfo* pLocale) {
// Leading zero not followed by decimal mark
if (x[0] == '0' && x.size() > 1 && x[1] != pLocale->decimalMark_)
return false;

double res = 0;
std::string::const_iterator begin = x.begin(), end = x.end();

Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-parsing-numeric.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ test_that("leading/trailing ws ignored when parsing", {
})


# Leading zeros -----------------------------------------------------------

test_that("leading zeros are not numbers", {
expect_equal(collector_guess("0"), "integer")
expect_equal(collector_guess("0."), "double")
expect_equal(collector_guess("0001"), "character")
})

# Flexible number parsing -------------------------------------------------
es_MX <- locale("es", decimal_mark = ",")

Expand Down

0 comments on commit ec04976

Please sign in to comment.