Skip to content

Commit

Permalink
Fix values with number and dash data type are parsed as integer inste…
Browse files Browse the repository at this point in the history
…ad of string (#206)

* Fix parsing numbers with dash to string

* Fix single include libs

* Renamed neg_allowed to is_negative

It is unnecessary to determine if the minus sign is allowed separately from whitespace.

---------

Co-authored-by: Slobodan Klentikov <[email protected]>
Co-authored-by: Vincent La <[email protected]>
  • Loading branch information
3 people authored Mar 30, 2024
1 parent 5417c3d commit a6ce8b2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
10 changes: 5 additions & 5 deletions include/internal/data_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,9 @@ namespace csv {
return DataType::CSV_NULL;

bool ws_allowed = true,
neg_allowed = true,
dot_allowed = true,
digit_allowed = true,
is_negative = false,
has_digit = false,
prob_float = false;

Expand All @@ -268,12 +268,12 @@ namespace csv {
}
break;
case '-':
if (!neg_allowed) {
if (!ws_allowed) {
// Ex: '510-123-4567'
return DataType::CSV_STRING;
}

neg_allowed = false;
is_negative = true;
break;
case '.':
if (!dot_allowed) {
Expand All @@ -297,7 +297,7 @@ namespace csv {

return _process_potential_exponential(
in.substr(exponent_start_idx),
neg_allowed ? integral_part + decimal_part : -(integral_part + decimal_part),
is_negative ? -(integral_part + decimal_part) : integral_part + decimal_part,
out
);
}
Expand Down Expand Up @@ -331,7 +331,7 @@ namespace csv {
if (has_digit) {
long double number = integral_part + decimal_part;
if (out) {
*out = neg_allowed ? number : -number;
*out = is_negative ? -number : number;
}

return prob_float ? DataType::CSV_DOUBLE : _determine_integral_type(number);
Expand Down
2 changes: 1 addition & 1 deletion single_include/csv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5328,7 +5328,7 @@ namespace csv {
}
break;
case '-':
if (!neg_allowed) {
if (!ws_allowed) {
// Ex: '510-123-4567'
return DataType::CSV_STRING;
}
Expand Down
2 changes: 1 addition & 1 deletion single_include_test/csv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5328,7 +5328,7 @@ namespace csv {
}
break;
case '-':
if (!neg_allowed) {
if (!ws_allowed) {
// Ex: '510-123-4567'
return DataType::CSV_STRING;
}
Expand Down
8 changes: 7 additions & 1 deletion tests/test_data_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,10 @@ TEST_CASE("Parse Scientific Notation Malformed", "[sci_notation]") {
SECTION("Butchered Parsing Attempt") {
REQUIRE(data_type(butchered) == DataType::CSV_STRING);
}
}
}

TEST_CASE( "Parse numbers with dash as string", "[regression_double]" ) {
std::string s("510-123-4567");
long double out;
REQUIRE(data_type(s, &out) == DataType::CSV_STRING);
}

0 comments on commit a6ce8b2

Please sign in to comment.