Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentlaucsb committed Nov 9, 2019
2 parents 9ffab38 + 6c3859f commit 0102794
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 43 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ If a CSV file does not have column names, you can specify your own:
```cpp
std::vector<std::string> col_names = { ... };
CSVFormat format;
format.set_column_names(col_names);
format.column_names(col_names);
```

### Parsing an In-Memory String
Expand Down
4 changes: 2 additions & 2 deletions include/internal/csv_format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ namespace csv {
/** Tells the parser to throw an std::runtime_error if an
* invalid CSV sequence is found
*/
CONSTEXPR CSVFormat& strict_parsing(bool strict = true) {
this->strict = strict;
CONSTEXPR CSVFormat& strict_parsing(bool is_strict = true) {
this->strict = is_strict;
return *this;
}

Expand Down
9 changes: 3 additions & 6 deletions include/internal/csv_row.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ namespace csv {
*/
CSV_INLINE csv::string_view CSVRow::get_string_view(size_t n) const {
csv::string_view ret(this->row_str);

// First assume that field comprises entire row, then adjust accordingly
size_t beg = 0,
end = 0,
end = row_str.size(),
r_size = this->size();

if (n >= r_size)
Expand All @@ -36,11 +38,6 @@ namespace csv {
if (n != r_size - 1) end = this->split_at(n);
}
}

// Performance optimization
if (end == 0) {
end = row_str.size();
}

return ret.substr(
beg,
Expand Down
12 changes: 6 additions & 6 deletions include/internal/csv_row_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,10 @@ namespace csv {
col_names = this->buffer->col_names->get_col_names();
}

const size_t n_cols = col_names.size();
const size_t _n_cols = col_names.size();
std::string ret = "{";

for (size_t i = 0; i < n_cols; i++) {
for (size_t i = 0; i < _n_cols; i++) {
auto& col = col_names[i];
auto field = this->operator[](col);

Expand All @@ -218,7 +218,7 @@ namespace csv {
ret += '"' + internals::json_escape_string(field.get<csv::string_view>()) + '"';

// Do not add comma after last string
if (i + 1 < n_cols)
if (i + 1 < _n_cols)
ret += ',';
}

Expand All @@ -238,10 +238,10 @@ namespace csv {
if (subset.empty())
col_names = this->buffer->col_names->get_col_names();

const size_t n_cols = col_names.size();
const size_t _n_cols = col_names.size();
std::string ret = "[";

for (size_t i = 0; i < n_cols; i++) {
for (size_t i = 0; i < _n_cols; i++) {
auto field = this->operator[](col_names[i]);

// Add quotes around strings but not numbers
Expand All @@ -251,7 +251,7 @@ namespace csv {
ret += '"' + internals::json_escape_string(field.get<csv::string_view>()) + '"';

// Do not add comma after last string
if (i + 1 < n_cols)
if (i + 1 < _n_cols)
ret += ',';
}

Expand Down
25 changes: 11 additions & 14 deletions single_include/csv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2998,8 +2998,8 @@ namespace csv {
/** Tells the parser to throw an std::runtime_error if an
* invalid CSV sequence is found
*/
CONSTEXPR CSVFormat& strict_parsing(bool strict = true) {
this->strict = strict;
CONSTEXPR CSVFormat& strict_parsing(bool is_strict = true) {
this->strict = is_strict;
return *this;
}

Expand Down Expand Up @@ -5153,8 +5153,10 @@ namespace csv {
*/
CSV_INLINE csv::string_view CSVRow::get_string_view(size_t n) const {
csv::string_view ret(this->row_str);

// First assume that field comprises entire row, then adjust accordingly
size_t beg = 0,
end = 0,
end = row_str.size(),
r_size = this->size();

if (n >= r_size)
Expand All @@ -5172,11 +5174,6 @@ namespace csv {
if (n != r_size - 1) end = this->split_at(n);
}
}

// Performance optimization
if (end == 0) {
end = row_str.size();
}

return ret.substr(
beg,
Expand Down Expand Up @@ -5522,10 +5519,10 @@ namespace csv {
col_names = this->buffer->col_names->get_col_names();
}

const size_t n_cols = col_names.size();
const size_t _n_cols = col_names.size();
std::string ret = "{";

for (size_t i = 0; i < n_cols; i++) {
for (size_t i = 0; i < _n_cols; i++) {
auto& col = col_names[i];
auto field = this->operator[](col);

Expand All @@ -5539,7 +5536,7 @@ namespace csv {
ret += '"' + internals::json_escape_string(field.get<csv::string_view>()) + '"';

// Do not add comma after last string
if (i + 1 < n_cols)
if (i + 1 < _n_cols)
ret += ',';
}

Expand All @@ -5559,10 +5556,10 @@ namespace csv {
if (subset.empty())
col_names = this->buffer->col_names->get_col_names();

const size_t n_cols = col_names.size();
const size_t _n_cols = col_names.size();
std::string ret = "[";

for (size_t i = 0; i < n_cols; i++) {
for (size_t i = 0; i < _n_cols; i++) {
auto field = this->operator[](col_names[i]);

// Add quotes around strings but not numbers
Expand All @@ -5572,7 +5569,7 @@ namespace csv {
ret += '"' + internals::json_escape_string(field.get<csv::string_view>()) + '"';

// Do not add comma after last string
if (i + 1 < n_cols)
if (i + 1 < _n_cols)
ret += ',';
}

Expand Down
25 changes: 11 additions & 14 deletions single_include_test/csv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2998,8 +2998,8 @@ namespace csv {
/** Tells the parser to throw an std::runtime_error if an
* invalid CSV sequence is found
*/
CONSTEXPR CSVFormat& strict_parsing(bool strict = true) {
this->strict = strict;
CONSTEXPR CSVFormat& strict_parsing(bool is_strict = true) {
this->strict = is_strict;
return *this;
}

Expand Down Expand Up @@ -5153,8 +5153,10 @@ namespace csv {
*/
CSV_INLINE csv::string_view CSVRow::get_string_view(size_t n) const {
csv::string_view ret(this->row_str);

// First assume that field comprises entire row, then adjust accordingly
size_t beg = 0,
end = 0,
end = row_str.size(),
r_size = this->size();

if (n >= r_size)
Expand All @@ -5172,11 +5174,6 @@ namespace csv {
if (n != r_size - 1) end = this->split_at(n);
}
}

// Performance optimization
if (end == 0) {
end = row_str.size();
}

return ret.substr(
beg,
Expand Down Expand Up @@ -5522,10 +5519,10 @@ namespace csv {
col_names = this->buffer->col_names->get_col_names();
}

const size_t n_cols = col_names.size();
const size_t _n_cols = col_names.size();
std::string ret = "{";

for (size_t i = 0; i < n_cols; i++) {
for (size_t i = 0; i < _n_cols; i++) {
auto& col = col_names[i];
auto field = this->operator[](col);

Expand All @@ -5539,7 +5536,7 @@ namespace csv {
ret += '"' + internals::json_escape_string(field.get<csv::string_view>()) + '"';

// Do not add comma after last string
if (i + 1 < n_cols)
if (i + 1 < _n_cols)
ret += ',';
}

Expand All @@ -5559,10 +5556,10 @@ namespace csv {
if (subset.empty())
col_names = this->buffer->col_names->get_col_names();

const size_t n_cols = col_names.size();
const size_t _n_cols = col_names.size();
std::string ret = "[";

for (size_t i = 0; i < n_cols; i++) {
for (size_t i = 0; i < _n_cols; i++) {
auto field = this->operator[](col_names[i]);

// Add quotes around strings but not numbers
Expand All @@ -5572,7 +5569,7 @@ namespace csv {
ret += '"' + internals::json_escape_string(field.get<csv::string_view>()) + '"';

// Do not add comma after last string
if (i + 1 < n_cols)
if (i + 1 < _n_cols)
ret += ',';
}

Expand Down
24 changes: 24 additions & 0 deletions tests/test_read_csv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,4 +365,28 @@ TEST_CASE("Test read_row() CSVField - Power Status", "[read_row_csvf3]") {
REQUIRE(row[unit].get<std::string>() == "Beaver Valley 1");
}
}
}

// Reported in: https://github.com/vincentlaucsb/csv-parser/issues/56
TEST_CASE("Leading Empty Field Regression", "[empty_field_regression]") {
std::string csv_string(R"(category,subcategory,project name
,,foo-project
bar-category,,bar-project
)");
auto format = csv::CSVFormat();
csv::CSVReader reader(format);
reader.feed(csv_string);
reader.end_feed();

CSVRow first_row, second_row;
REQUIRE(reader.read_row(first_row));
REQUIRE(reader.read_row(second_row));

REQUIRE(first_row["category"] == "");
REQUIRE(first_row["subcategory"] == "");
REQUIRE(first_row["project name"] == "foo-project");

REQUIRE(second_row["category"] == "bar-category");
REQUIRE(second_row["subcategory"] == "");
REQUIRE(second_row["project name"] == "bar-project");
}

0 comments on commit 0102794

Please sign in to comment.