Vince's CSV Parser
Loading...
Searching...
No Matches
guessing.cpp
1#include "driver.hpp"
2#include "../string_view_stream.hpp"
3#include "stream.hpp"
4
5#include <unordered_map>
6#include <vector>
7
8namespace csv {
9 namespace internals {
10 namespace parser {
11 GuessScore calculate_score(csv::string_view head, const CSVFormat& format) {
12 // Frequency counter of row length
13 std::unordered_map<size_t, size_t> row_tally = { { 0, 0 } };
14
15 // Map row lengths to row num where they first occurred
16 std::unordered_map<size_t, size_t> row_when = { { 0, 0 } };
17
18 // Parse the CSV using the low-level constructor that takes pre-built flag
19 // tables — bypasses format resolution entirely and avoids recursion back
20 // into guess_format.
21 std::vector<CSVRow> rows;
22
23 const char delimiter = resolved_delimiter(format);
24 const auto parse_flags = format.is_quoting_enabled()
25 ? make_parse_flags(delimiter, format.get_quote_char())
26 : make_parse_flags(delimiter);
27 const auto ws_flags = make_ws_flags(format.get_trim_chars());
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);
31 parser.end_feed();
32
33 for (size_t i = 0; i < rows.size(); i++) {
34 auto& row = rows[i];
35
36 // Ignore zero-length rows
37 if (row.size() > 0) {
38 if (row_tally.find(row.size()) != row_tally.end()) {
39 row_tally[row.size()]++;
40 }
41 else {
42 row_tally[row.size()] = 1;
43 row_when[row.size()] = i;
44 }
45 }
46 }
47
48 double final_score = 0;
49 size_t header_row = 0;
50 size_t mode_row_length = 0;
51
52 // Choose the row-length group with the largest width * frequency score.
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) {
58 final_score = score;
59 mode_row_length = row_size;
60 header_row = row_when[row_size];
61 }
62 }
63
64 // Prefer the first row as header when it is at least as wide as the winning group.
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) {
67 header_row = 0;
68 }
69
70 return { header_row, mode_row_length, final_score };
71 }
72
73 CSVGuessResult guess_format(csv::string_view head, const std::vector<char>& delims, CSVError& error) {
74 error.clear();
75 if (delims.empty()) {
76 fail_static(error, CSVErrorCode::InvalidFormat, "At least one delimiter is required.");
77 return {',', 0, 0};
78 }
86 // The parser core does not handle BOMs, so strip one here (once, not
87 // per candidate). This also rejects UTF-16/UTF-32 before guessing
88 // scores mis-decoded bytes.
89 bool utf8_bom = false;
90 const size_t bom_skip = get_bom_skip(head, utf8_bom, error);
91 if (error) return {',', 0, 0};
92 head = head.substr(bom_skip);
93
94 CSVFormat format;
95 size_t max_score = 0;
96 size_t header = 0;
97 size_t n_cols = 0;
98 char current_delim = delims[0];
99
100 for (char cand_delim : delims) {
101 if (!format.try_delimiter(cand_delim, error)) return {',', 0, 0};
102 auto result = calculate_score(head, format);
103
104 if ((size_t)result.score > max_score) {
105 max_score = (size_t)result.score;
107 header = result.header;
108 n_cols = result.mode_row_length;
109 }
110 }
111
112 return { current_delim, (int)header, n_cols };
113 }
114 }
115 }
116}
Stores information about how to parse a CSV file.
bool try_delimiter(char delim, CSVError &error)
Set parsing characters, returning false and preserving the format on invalid input.
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,...
Definition core.hpp:40
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,...
Definition core.hpp:86
char resolved_delimiter(const CSVFormat &format) noexcept
Read a delimiter after configuration has been validated and resolved to one candidate.
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.
Definition guessing.cpp:73
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.
Definition common.hpp:182
A diagnostic whose text remains valid for the lifetime of the error.
Definition csv_error.hpp:94
Stores the inferred format of a CSV file.