csv support linebreaks

This commit is contained in:
blueloveTH 2023-11-29 11:10:37 +08:00
parent cb51cc95b6
commit 101cf24a36
2 changed files with 19 additions and 2 deletions

View File

@ -16,9 +16,11 @@ void add_module_csv(VM *vm){
if (strncmp(line.data(), "\xEF\xBB\xBF", 3) == 0) line = line.substr(3); if (strncmp(line.data(), "\xEF\xBB\xBF", 3) == 0) line = line.substr(3);
} }
List row; List row;
int j = 0; int j;
bool in_quote = false; bool in_quote = false;
std::string buffer; std::string buffer;
__NEXT_LINE:
j = 0;
while(j < line.size()){ while(j < line.size()){
switch(line[j]){ switch(line[j]){
case '"': case '"':
@ -50,7 +52,14 @@ void add_module_csv(VM *vm){
j++; j++;
} }
if(in_quote){ if(in_quote){
if(i == csvfile.size()-1){
vm->ValueError("unterminated quote"); vm->ValueError("unterminated quote");
}else{
buffer += '\n';
i++;
line = CAST(Str&, csvfile[i]).sv();
goto __NEXT_LINE;
}
} }
row.push_back(VAR(buffer)); row.push_back(VAR(buffer));
ret.push_back(VAR(std::move(row))); ret.push_back(VAR(std::move(row)));

View File

@ -31,6 +31,14 @@ test("""a,b ,c,
1,"22""33",3 1,"22""33",3
""", [['a', 'b ', 'c', ''], ['1', '22"33', '3']]) """, [['a', 'b ', 'c', ''], ['1', '22"33', '3']])
# newline
test('''a,b,c
1,2,"3,
4"
5,"a,""
b",7
''', [['a', 'b', 'c'], ['1', '2', '3,\n 4'], ['5', 'a,"\nb', '7']])
ret = csv.DictReader("""a,b,c ret = csv.DictReader("""a,b,c
1,2,3 1,2,3
"4",5,6 "4",5,6