Skip to content

Commit 8f6024c

Browse files
committed
Fixed 2 bugs related to quotes
Bug 1: Parsing failed when quotes were used and a delimiter was not a comma (due to using hardcoded comma in one condition...). Bug 2: Parsing failed when four quote characters were within quoted string (for the purpose of including two quote characters within the parsed string itself).
1 parent 8719cf9 commit 8f6024c

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

CSV_Parser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ char * CSV_Parser::parseStringValue(const char * s, int * chars_occupied) {
175175
bool ending_quote_found = false;
176176
while (char *next_quote = strchr(s, quote_char)) {
177177
if (*(next_quote+1) == quote_char) {
178-
s += 2;
178+
s = next_quote+2;
179179
len--;
180180
continue;
181181
}
@@ -184,7 +184,7 @@ char * CSV_Parser::parseStringValue(const char * s, int * chars_occupied) {
184184
*chars_occupied += next_quote - base;
185185
len += next_quote - base;
186186

187-
if ((*(next_quote+1) == ','))
187+
if ((*(next_quote+1) == delimiter))
188188
*chars_occupied += 1;
189189
else
190190
*chars_occupied += strspn(next_quote + 1, "\r\n");

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=CSV Parser
2-
version=1.0.0
2+
version=1.0.1
33
author=Michal Borowski <michalmonday17@gmail.com>
44
maintainer=Michal Borowski <michalmonday17@gmail.com>
55
sentence=CSV Parser for Arduino.

tests/quotes/quotes.ino

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <CSV_Parser.h>
2+
3+
void setup() {
4+
Serial.begin(115200);
5+
delay(5000);
6+
7+
// two quote characters next to each other include one quote character into the parsed string itself
8+
static char buff[] = "'Ajatempel (UTC)';'Kuupev (Eesti aeg)';'NPS Eesti'\n"
9+
"'1590883200';'31.05.2020''''03:00';'1.96'\n"
10+
"'1590966000';'01.06.2020 02:00';'0.16'";
11+
CSV_Parser cp(buff, "Lsf", true, ';', '\'');
12+
cp.parseLeftover();
13+
cp.print();
14+
long *timestamps = (long *)cp[0];
15+
char ** dates = (char**)cp[1];
16+
float * prices = (float*)cp[2];
17+
18+
for (int i = 0; i < cp.getRowsCount(); i++) {
19+
Serial.print(timestamps[i]); Serial.print(" - ");
20+
Serial.print(dates[i]); Serial.print(" - ");
21+
Serial.print(prices[i]); Serial.print(" - ");
22+
Serial.println("");
23+
}
24+
25+
}
26+
27+
void loop() {
28+
}

0 commit comments

Comments
 (0)