{"id":16164959,"url":"https://github.com/ashaduri/csv-parser","last_synced_at":"2025-03-16T10:30:42.646Z","repository":{"id":75742795,"uuid":"335952553","full_name":"ashaduri/csv-parser","owner":"ashaduri","description":"Compile-time and runtime CSV parser written in C++17","archived":false,"fork":false,"pushed_at":"2024-05-23T18:22:05.000Z","size":906,"stargazers_count":31,"open_issues_count":0,"forks_count":5,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-27T07:30:56.039Z","etag":null,"topics":["cplusplus","cplusplus-17","cpp","cpp-library","cpp17","csv","parser"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"zlib","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ashaduri.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2021-02-04T12:50:12.000Z","updated_at":"2025-01-28T10:28:37.000Z","dependencies_parsed_at":"2023-12-22T10:43:15.321Z","dependency_job_id":"7f59cc80-b28f-4af0-bf5f-47021152101d","html_url":"https://github.com/ashaduri/csv-parser","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashaduri%2Fcsv-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashaduri%2Fcsv-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashaduri%2Fcsv-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashaduri%2Fcsv-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ashaduri","download_url":"https://codeload.github.com/ashaduri/csv-parser/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243809886,"owners_count":20351407,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cplusplus","cplusplus-17","cpp","cpp-library","cpp17","csv","parser"],"created_at":"2024-10-10T02:48:37.683Z","updated_at":"2025-03-16T10:30:42.327Z","avatar_url":"https://github.com/ashaduri.png","language":"C++","readme":"# Csv::Parser (csv-parser)\n***Compile-time and runtime CSV parser written in C++17***\n\n![GitHub](https://img.shields.io/github/license/ashaduri/csv-parser)\n![Language](https://img.shields.io/badge/language-ISO%20C++17-blue)\n\n\n## Features\n- Header-only.\n- Requires only standard C++ (C++17).\n- Supports reading CSV data from `std::string_view` during compilation (using `constexpr`).\n- Fully supports [RFC 4180](https://www.ietf.org/rfc/rfc4180.txt), including quoted values, escaped quotes, and newlines in field values.\n- Liberal in terms of accepting not-quite-standard CSV files, but detects errors when needed.\n- Supports Excel CSV variations.\n- Supports reading data as different types (string, double, empty field) (runtime only).\n- Extensively tested using [Catch2](https://github.com/catchorg/Catch2).\n\n## API Reference\n\nAutomatically generated [API reference](https://ashaduri.github.io/csv-parser/) describes the public API in detail.\n\n## Usage Examples\n\n### Runtime Parsing into 2D std::vector\n\n#### Example:\n\n```cpp\n#include \"csv_parser.h\"\n\n// ...\n\nusing namespace std::string_view_literals;\n\n// Data to parse\nstd::string_view data = \"abc,def\\n5,6\"sv;\n\n// Let \"cell_refs\" be a vector of columns.\n// After parsing, each element will contain Csv::CellReference object. If the cell data type\n// is Csv::CellType::String, Csv::CellReference object will reference a part of the original data.\n// Other Cell* types, as well as floating point and integral types can also be used here.\nstd::vector\u003cstd::vector\u003cCsv::CellReference\u003e\u003e cell_refs;\n\nCsv::Parser parser;\n\ntry {\n    // This throws Csv::ParseError on error.\n    parser.parseTo2DVector(data, cell_refs);\n}\ncatch(Csv::ParseError\u0026 ex) {\n    std::cerr \u003c\u003c \"CSV parse error: \" \u003c\u003c ex.what() \u003c\u003c std::endl;\n    return EXIT_FAILURE;\n}\n\nassert(cell_refs.size() == 2);\nassert(cell_refs[0].size() == 2);\nassert(cell_refs[1].size() == 2);\n\nassert(cell_refs[0][0].getType() == Csv::CellType::String);\nassert(cell_refs[1][0].getType() == Csv::CellType::String);\nassert(cell_refs[0][1].getType() == Csv::CellType::Double);\nassert(cell_refs[1][1].getType() == Csv::CellType::Double);\n\nstd::cout \u003c\u003c \"Column 0, row 0: \" \u003c\u003c cell_refs[0][0].getCleanString().value() \u003c\u003c std::endl;  // abc\nstd::cout \u003c\u003c \"Column 1, row 0: \" \u003c\u003c cell_refs[1][0].getCleanString().value() \u003c\u003c std::endl;  // def\nstd::cout \u003c\u003c \"Column 0, row 1: \" \u003c\u003c cell_refs[0][1].getDouble().value() \u003c\u003c std::endl;  // 5\nstd::cout \u003c\u003c \"Column 1, row 1: \" \u003c\u003c cell_refs[1][1].getDouble().value() \u003c\u003c std::endl;  // 6\n```\n\n### Runtime Parsing of Numeric Matrix Into 1D Vector With Row-Major Order \n\n#### Example:\n\n```cpp\n#include \"csv_parser.h\"\n\n// ...\n\nusing namespace std::string_view_literals;\n\n// Data to parse\nstd::string_view data = \"11,12,13\\n21,22,23\"sv;\n\n// Let matrix_data be a flat matrix of doubles in row-major order.\n// Other floating point and integral types, as well as Cell* types can also be used here.\nstd::vector\u003cdouble\u003e matrix_data;\n\nCsv::Parser parser;\nCsv::MatrixInformation info;\n\ntry {\n    // This throws Csv::ParseError on error.\n    info = parser.parseToVectorRowMajor(data, matrix_data);\n}\ncatch(Csv::ParseError\u0026 ex) {\n    std::cerr \u003c\u003c \"CSV parse error: \" \u003c\u003c ex.what() \u003c\u003c std::endl;\n    return EXIT_FAILURE;\n}\n\nassert(matrix_data.size() == 3 * 2);\nassert(info.getColumns() == 3);\nassert(info.getRows() == 2);\n\nstd::cout \u003c\u003c \"Row 0, column 0: \" \u003c\u003c matrix_data[0] \u003c\u003c std::endl;  // 11\nstd::cout \u003c\u003c \"Row 0, column 1: \" \u003c\u003c matrix_data[1] \u003c\u003c std::endl;  // 12\nstd::cout \u003c\u003c \"Row 0, column 2: \" \u003c\u003c matrix_data[2] \u003c\u003c std::endl;  // 13\n\n// matrixIndex(row, column) can be used to avoid accidental mistakes\nstd::cout \u003c\u003c \"Row 1, column 0: \" \u003c\u003c matrix_data[info.matrixIndex(1, 0)] \u003c\u003c std::endl;  // 21\nstd::cout \u003c\u003c \"Row 1, column 1: \" \u003c\u003c matrix_data[info.matrixIndex(1, 1)] \u003c\u003c std::endl;  // 22\nstd::cout \u003c\u003c \"Row 1, column 2: \" \u003c\u003c matrix_data[info.matrixIndex(1, 2)] \u003c\u003c std::endl;  // 23\n\nreturn EXIT_SUCCESS;\n```\n\n### Compile-Time Parsing\n\nCurrently, parsing at compile-time has some restrictions:\n- Only string_views are supported for output (no doubles).\n- To collapse consecutive double-quotes in strings, a compile-time-allocated buffer has to be used.\n\nOne (possibly useful) consequence of compile-time parsing is that a parse error also causes a compilation error. \n\n#### Example:\n```cpp\n#include \"csv_parser.h\"\n\n// ...\n\nusing namespace std::string_view_literals;\n\nconstexpr std::string_view data =\nR\"(abc, \"def\"\n\"with \"\"quote inside\",6)\";\n\nconstexpr std::size_t columns = 2, rows = 2;\n\nconstexpr Csv::Parser parser;\n\n// parse into std::array\u003cstd::array\u003cCellStringReference, rows\u003e, columns\u003e\nconstexpr auto matrix = parser.parseTo2DArray\u003crows, columns\u003e(data);\n\n// Verify the data at compile time.\n// Note that consecutive double-quotes are not collapsed when using\n// getOriginalStringView(). To collapse them, use the getCleanStringBuffer()\n// approach below.\nstatic_assert(matrix[0][0].getOriginalStringView() == \"abc\"sv);\nstatic_assert(matrix[1][0].getOriginalStringView() == \"def\"sv);\nstatic_assert(matrix[1][1].getOriginalStringView() == \"6\"sv);\n\n// To support consecutive double-quote collapsing at compile-time, allocate a compile-time\n// buffer to place the clean string inside. The buffer size has to be at least that\n// of an uncollapsed string value.\n// If the buffer is too small, the code will simply not compile.\nconstexpr auto buffer_size = matrix[0][1].getRequiredBufferSize();  // uncollapsed size\nconstexpr auto buffer = matrix[0][1].getCleanStringBuffer\u003cbuffer_size\u003e();\nstatic_assert(buffer.getStringView() == R\"(with \"quote inside)\"sv);\n```\n\n\n## Copyright\n\nCopyright: Alexander Shaduri \u003cashaduri@gmail.com\u003e   \nLicense: Zlib\n","funding_links":[],"categories":["CSV"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fashaduri%2Fcsv-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fashaduri%2Fcsv-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fashaduri%2Fcsv-parser/lists"}