Skip to content

Commit 2d7629e

Browse files
committed
Made parseTo2DArray() const; improved compile-time example to include escaped quotes.
1 parent c0acb9a commit 2d7629e

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ using namespace std::string_view_literals;
7878
constexpr std::string_view data = "\"abc\",def\n5,6"sv;
7979
constexpr std::size_t columns = 2, rows = 2;
8080
81-
Csv::Parser parser;
81+
constexpr Csv::Parser parser;
8282
8383
// parse into std::array<std::array<CellStringReference, rows>, columns>
8484
constexpr auto matrix = parser.parseTo2DArray<columns, rows>(data);

csv_parser/csv_parser.h

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ namespace Csv {
5757
* - Line ending format inside strings is preserved.
5858
* - getOriginalStringView() methods may return escaped double-quotes; string_views are read-only and we
5959
* cannot touch the original CSV data; use getCleanString() methods if you need unescaped data.
60-
*
61-
* Ambiguity notice:
62-
* Empty cell (Invalid QVariant) and empty string (empty std::string) are serialized the same by Excel.
63-
* We read them as Invalid QVariants, but QVariant::toByteArray() will give empty std::strings as expected.
6460
*/
6561
class Parser {
6662
public:
@@ -93,7 +89,7 @@ class Parser {
9389
/// \return std::array<std::array<Cell, rows>, columns>
9490
/// \throws ParseError
9591
template<std::size_t columns, std::size_t rows, typename Cell = CellStringReference>
96-
constexpr auto parseTo2DArray(std::string_view data);
92+
constexpr auto parseTo2DArray(std::string_view data) const;
9793

9894

9995
private:
@@ -428,29 +424,32 @@ template<typename Vector2D>
428424
constexpr void Parser::parseTo(std::string_view data, Vector2D& values) const
429425
{
430426
Vector2D parsed_values;
431-
parse(data, [&](std::size_t row, std::size_t column, std::string_view cell_data, CellTypeHint hint) {
432-
if (parsed_values.size() < (column + 1)) {
433-
parsed_values.resize(column + 1);
434-
}
435-
if (parsed_values[column].size() < (row + 1)) {
436-
parsed_values[column].resize(row + 1);
427+
parse(data,
428+
[&parsed_values](std::size_t row, std::size_t column, std::string_view cell_data, CellTypeHint hint)
429+
{
430+
if (parsed_values.size() < (column + 1)) {
431+
parsed_values.resize(column + 1);
432+
}
433+
if (parsed_values[column].size() < (row + 1)) {
434+
parsed_values[column].resize(row + 1);
435+
}
436+
parsed_values[column][row] = typename Vector2D::value_type::value_type(cell_data, hint);
437437
}
438-
parsed_values[column][row] = typename Vector2D::value_type::value_type(cell_data, hint);
439-
});
438+
);
440439
std::swap(values, parsed_values);
441440
}
442441

443442

444443

445444
template<std::size_t columns, std::size_t rows, typename Cell>
446-
constexpr auto Parser::parseTo2DArray(std::string_view data)
445+
constexpr auto Parser::parseTo2DArray(std::string_view data) const
447446
{
448447
std::array<std::array<Cell, rows>, columns> matrix;
449448

450449
parse(data,
451450
[&matrix](std::size_t row, std::size_t column,
452451
std::string_view cell_data, Csv::CellTypeHint hint)
453-
constexpr mutable
452+
constexpr
454453
{
455454
matrix[column][row] = Cell(cell_data, hint);
456455
}

examples/example_compiletime.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,23 @@ int main()
1414
{
1515
using namespace std::string_view_literals;
1616

17-
constexpr std::string_view data = "\"abc\",def\n5,6"sv;
17+
constexpr std::string_view data =
18+
R"(abc,5
19+
,"with ""quote inside"
20+
)"sv;
1821
constexpr std::size_t columns = 2, rows = 2;
1922

20-
Csv::Parser parser;
23+
constexpr Csv::Parser parser;
2124

2225
// parse into std::array<std::array<CellStringReference, rows>, columns>
2326
constexpr auto matrix = parser.parseTo2DArray<columns, rows>(data);
2427

2528
// Verify the data at compile time.
2629
static_assert(matrix[0][0].getOriginalStringView() == "abc"sv);
27-
static_assert(matrix[1][0].getOriginalStringView() == "def"sv);
28-
static_assert(matrix[0][1].getOriginalStringView() == "5"sv);
29-
static_assert(matrix[1][1].getOriginalStringView() == "6"sv);
30+
static_assert(matrix[1][0].getOriginalStringView() == "5"sv);
31+
static_assert(matrix[0][1].getOriginalStringView().empty());
32+
static_assert(matrix[1][1].getCleanStringBuffer<"with \"\"quote inside"sv.size()>().getStringView()
33+
== "with \"quote inside"sv);
3034

3135
return EXIT_SUCCESS;
3236
}

tests/test_csv_parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ TEST_CASE("CsvParser", "[csv][parser]")
524524

525525
SECTION("supports constexpr") {
526526
constexpr std::string_view data = "\"abc\",def\n\"with \"\"quote inside\",6"sv;
527-
Csv::Parser parser;
527+
constexpr Csv::Parser parser;
528528

529529
// parse into std::array<std::array<CellStringReference, rows>, columns>
530530
constexpr auto matrix = parser.parseTo2DArray<2, 2>(data);

0 commit comments

Comments
 (0)