From 12007e197832263b9113554bdfea1a8258f179bc Mon Sep 17 00:00:00 2001 From: Tadas Vilkeliskis Date: Thu, 11 Jun 2026 10:00:42 -0400 Subject: Fix multiple parsing bugs and add RFC 4180 quote support - Change nread to size_t to prevent overflow. - Fix state machine to handle consecutive newlines appropriately. - Add csv_parser_finish to emit pending trailing fields at EOF. - Fix nread boundary count on callback errors. - Add support for quoted fields, including escaped quotes. - Update and expand test suite to cover new cases and quote parsing. --- src/csv_parser.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src/csv_parser.h') diff --git a/src/csv_parser.h b/src/csv_parser.h index 8c78e8b..267c768 100644 --- a/src/csv_parser.h +++ b/src/csv_parser.h @@ -35,6 +35,8 @@ typedef enum csv_parser_state_t { csvps_line_start = 0, csvps_field_start, csvps_field_value, + csvps_field_quoted_value, + csvps_field_quoted_quote, csvps_field_end, csvps_line_end_begin, csvps_line_end, @@ -48,7 +50,7 @@ typedef struct csv_parser_t { int row; /* csv column */ int col; - uint32_t nread; + size_t nread; /* user data */ void *data; } csv_parser_t; @@ -88,6 +90,12 @@ size_t csv_parser_execute(csv_parser_t *parser, const char *data, size_t data_len); +/** + * Flush any pending state. This is useful when the file ends + * without a trailing newline, to flush the final empty field. + * Returns 0 on success, anything else on error. + */ +int csv_parser_finish(csv_parser_t *parser, const csv_parser_settings_t *settings); #ifdef __cplusplus } -- cgit v1.2.3