Csv::Parser (csv-parser)
Compile-time and runtime CSV parser written in C++17
Features
- Header-only.
- Requires only standard C++ (C++17).
- Supports reading CSV data from
std::string_view
during compilation (using constexpr
). - Fully supports RFC 4180, including quoted values, escaped quotes, and newlines in field values.
- Liberal in terms of accepting not-quite-standard CSV files, but detects errors when needed.
- Supports Excel CSV variations.
- Supports reading data as different types (string, double, empty field) (runtime only).
- Extensively tested using Catch2.
Usage Examples
Runtime Parsing into 2D std::vector
Example:
#include "csv_parser.h"
using namespace std::string_view_literals;
std::string_view data = "abc,def\n5,6"sv;
std::vector<std::vector<Csv::CellReference>> cell_refs;
Csv::Parser parser;
try {
parser.parseTo2DVector(data, cell_refs);
}
catch(Csv::ParseError& ex) {
std::cerr << "CSV parse error: " << ex.what() << std::endl;
return EXIT_FAILURE;
}
assert(cell_refs.size() == 2);
assert(cell_refs[0].size() == 2);
assert(cell_refs[1].size() == 2);
assert(cell_refs[0][0].getType() == Csv::CellType::String);
assert(cell_refs[1][0].getType() == Csv::CellType::String);
assert(cell_refs[0][1].getType() == Csv::CellType::Double);
assert(cell_refs[1][1].getType() == Csv::CellType::Double);
std::cout << "Column 0, row 0: " << cell_refs[0][0].getCleanString().value() << std::endl;
std::cout << "Column 1, row 0: " << cell_refs[1][0].getCleanString().value() << std::endl;
std::cout << "Column 0, row 1: " << cell_refs[0][1].getDouble().value() << std::endl;
std::cout << "Column 1, row 1: " << cell_refs[1][1].getDouble().value() << std::endl;
Runtime Parsing of Numeric Matrix Into 1D Vector With Row-Major Order
Example:
#include "csv_parser.h"
using namespace std::string_view_literals;
std::string_view data = "11,12,13\n21,22,23"sv;
std::vector<double> matrix_data;
Csv::Parser parser;
Csv::MatrixInformation info;
try {
info = parser.parseToVectorRowMajor(data, matrix_data);
}
catch(Csv::ParseError& ex) {
std::cerr << "CSV parse error: " << ex.what() << std::endl;
return EXIT_FAILURE;
}
assert(matrix_data.size() == 3 * 2);
assert(info.getColumns() == 3);
assert(info.getRows() == 2);
std::cout << "Row 0, column 0: " << matrix_data[0] << std::endl;
std::cout << "Row 0, column 1: " << matrix_data[1] << std::endl;
std::cout << "Row 0, column 2: " << matrix_data[2] << std::endl;
std::cout << "Row 1, column 0: " << matrix_data[info.matrixIndex(1, 0)] << std::endl;
std::cout << "Row 1, column 1: " << matrix_data[info.matrixIndex(1, 1)] << std::endl;
std::cout << "Row 1, column 2: " << matrix_data[info.matrixIndex(1, 2)] << std::endl;
return EXIT_SUCCESS;
Compile-Time Parsing
Currently, parsing at compile-time has some restrictions:
- Only string_views are supported for output (no doubles).
- To collapse consecutive double-quotes in strings, a compile-time-allocated buffer has to be used.
One (possibly useful) consequence of compile-time parsing is that a parse error also causes a compilation error.
Example:
#include "csv_parser.h"
using namespace std::string_view_literals;
constexpr std::string_view data =
R"(abc, "def"
"with ""quote inside",6)";
constexpr std::size_t columns = 2, rows = 2;
constexpr Csv::Parser parser;
constexpr auto matrix = parser.parseTo2DArray<rows, columns>(data);
static_assert(matrix[0][0].getOriginalStringView() == "abc"sv);
static_assert(matrix[1][0].getOriginalStringView() == "def"sv);
static_assert(matrix[1][1].getOriginalStringView() == "6"sv);
constexpr auto buffer_size = matrix[0][1].getRequiredBufferSize();
constexpr auto buffer = matrix[0][1].getCleanStringBuffer<buffer_size>();
static_assert(buffer.getStringView() == R"(with "quote inside)"sv);
Copyright
Copyright: Alexander Shaduri ashaduri@gmail.com
License: Zlib