csv-parser: Csv::Parser (csv-parser) (original) (raw)

Compile-time and runtime CSV parser written in Modern C++

GitHub release (latest SemVer) GitHub Language

Features

API Reference

Automatically generated API reference describes the public API in detail.

Usage Examples

Runtime Parsing into 2D std::vector

Example:

using namespace std::string_view_literals;

std::string_view data = "abc,def\n5,6"sv;

std::vector<std::vectorCsv::CellReference> cell_refs;

try {

}

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;

Exception thrown on CSV parse error.

Definition csv_error.h:25

The main CSV parser class.

Definition csv_parser.h:70

constexpr void parseTo2DVector(std::string_view data, Vector2D &values) const

Parse CSV string data into a vector of columns.

Definition csv_parser.h:484

Main include file for the library.

Runtime Parsing of Numeric Matrix Into 1D Vector With Row-Major Order

Example:

using namespace std::string_view_literals;

std::string_view data = "11,12,13\n21,22,23"sv;

std::vector matrix_data;

try {

}

std::cerr << "CSV parse error: " << ex.what() << std::endl;

return EXIT_FAILURE;

}

assert(matrix_data.size() == 3 * 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;

Matrix information (dimensions, order).

Definition csv_matrix.h:32

constexpr std::size_t getRows() const

Get number of rows in matrix.

Definition csv_matrix.h:64

constexpr std::size_t getColumns() const

Get number of columns in matrix.

Definition csv_matrix.h:78

static constexpr std::size_t matrixIndex(std::size_t row, std::size_t column, std::size_t rows, std::size_t columns, MatrixOrder order)

Get index (offset) in flat-matrix vector.

Definition csv_matrix.h:42

constexpr MatrixInformation parseToVectorRowMajor(std::string_view data, Vector &values) const

Parse CSV string data into a flat matrix in row-major format (A11, A12, A13, A21, ....

Definition csv_parser.h:527

Compile-Time Parsing Into 2D std::array

Currently, parsing at compile-time has some restrictions:

One (possibly useful) consequence of compile-time parsing is that a parse error also causes a compilation error.

Example:

using namespace std::string_view_literals;

constexpr std::size_t columns = 2, rows = 2;

constexpr std::string_view data =

R"(abc, "def"

"with ""quote inside",6)";

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);

constexpr auto parseTo2DArray(std::string_view data) const

Parse CSV string to 2D std::array, an array of columns.

Definition csv_parser.h:507

Compile-Time Parsing of Integral Matrix Into 1D Vector

The library can be used to parse CSV data at compile-time into a 1D vector, in row-major or column-major order. Additionally, compile-time parsing of integral types is supported since C++23.

Example:

using namespace std::string_view_literals;

constexpr std::size_t columns = 2, rows = 3;

constexpr std::string_view data =

R"(11, -12

21, 4

60, -10)";

{

constexpr auto matrix = parser.parseToArray<rows, columns>(data, Csv::MatrixOrder::RowMajor);

static_assert(matrix[0].getOriginalStringView() == "11"sv);

static_assert(matrix[2].getOriginalStringView() == "21"sv);

}

#if __cplusplus >= 202300L

{

constexpr auto matrix = parser.parseToArray<rows, columns, std::int64_t>(data, Csv::MatrixOrder::ColumnMajor);

static_assert(matrix[0] == 11);

static_assert(matrix[2] == 60);

}

#endif

constexpr auto parseToArray(std::string_view data, MatrixOrder order) const

Parse CSV string to 1D std::array, stored in row-major or column-major order.

Definition csv_parser.h:635

Compile-Time Parsing of Numeric Matrix Using External Library for Floating Point Numbers

A library like fast_float can be used to parse floating point numbers at compile-time. This is done by creating a custom policy that implements the readNumber and create methods. This approach also allows for improving the performance of parsing floating point numbers compared to the standard library.

Example:

#include "fast_float/fast_float.h"

template

static constexpr std::optional readNumber(std::string_view cell)

{

Number parsed_value = 0;

auto [ptr, ec] = fast_float::from_chars(cell.begin(), cell.end(), parsed_value);

if (ec == std::errc() && ptr == (cell.end())) {

}

return std::nullopt;

}

template

{

return readNumber(cell).value_or(std::numeric_limits::quiet_NaN());

}

};

void parse()

{

constexpr std::string_view data =

R"(11.6, -12.3

2e5, -inf

nan, inf)";

constexpr std::size_t columns = 2, rows = 3;

constexpr auto matrix = parser.parseToArray<rows, columns, double>(data, Csv::MatrixOrder::RowMajor);

static_assert(matrix[0] == 11.6);

static_assert(matrix[3] == -std::numeric_limits::infinity());

}

CellTypeHint

Type hint associated with the cell to determine the type of the cell value.

Definition csv_cell.h:32

This policy controls the behavior of the parser.

Definition csv_policies.h:74

static constexpr std::optional< Number > readNumber(std::string_view cell)

Try to read a numeric value from string data.

Definition csv_policies.h:87

static constexpr CellT create(std::string_view cell, CellTypeHint hint)

Create an object of type CellT from cell contents represented as string_view.

Definition csv_policies.h:96

Copyright: Alexander Shaduri ashad.nosp@m.uri@.nosp@m.gmail.nosp@m..com
License: Zlib