2#include "../string_view_stream.hpp"
5#include <unordered_map>
13 std::unordered_map<size_t, size_t> row_tally = { { 0, 0 } };
16 std::unordered_map<size_t, size_t> row_when = { { 0, 0 } };
21 std::vector<CSVRow> rows;
24 const auto parse_flags = format.is_quoting_enabled()
28 auto head_owner = std::make_shared<std::string>(std::string(head));
29 CSVParserCore<std::vector<CSVRow>> parser(parse_flags, ws_flags);
30 parser.parse_chunk(*head_owner, head_owner, rows);
33 for (
size_t i = 0; i < rows.size(); i++) {
38 if (row_tally.find(row.size()) != row_tally.end()) {
39 row_tally[row.size()]++;
42 row_tally[row.size()] = 1;
43 row_when[row.size()] = i;
48 double final_score = 0;
49 size_t header_row = 0;
50 size_t mode_row_length = 0;
53 for (
auto& pair : row_tally) {
54 const size_t row_size = pair.first;
55 const size_t row_count = pair.second;
56 const double score = (double)(row_size * row_count);
57 if (score > final_score) {
59 mode_row_length = row_size;
60 header_row = row_when[row_size];
65 size_t first_row_length = rows.size() > 0 ? rows[0].size() : 0;
66 if (first_row_length >= mode_row_length && first_row_length > 0) {
70 return { header_row, mode_row_length, final_score };
89 bool utf8_bom =
false;
90 const size_t bom_skip = get_bom_skip(
head, utf8_bom, error);
91 if (error)
return {
',', 0, 0};
108 n_cols =
result.mode_row_length;
Borrow a row and error state for synchronous, single-pass field access.
CSV_CONST CONSTEXPR_17 ParseFlagMap make_parse_flags(char delimiter)
Create a vector v where each index i corresponds to the ASCII number for a character and,...
CSV_CONST CONSTEXPR_17 WhitespaceMap make_ws_flags(const char *ws_chars, size_t n_chars)
Create a vector v where each index i corresponds to the ASCII number for a character c and,...
Contains CSV parser source-adapter declarations and format helpers.
CSVGuessResult guess_format(csv::string_view head, const std::vector< char > &delims, CSVError &error)
Guess the delimiter used by a delimiter-separated values file.
The all encompassing namespace.
@ InvalidFormat
Conflicting parsing characters or an unresolved delimiter query.
std::string_view string_view
The string_view class used by this library.
A diagnostic whose text remains valid for the lifetime of the error.
Stores the inferred format of a CSV file.