aboutsummaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
Diffstat (limited to 'ext')
-rw-r--r--ext/misc/csv.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/ext/misc/csv.c b/ext/misc/csv.c
index f887784ab..bcafdc526 100644
--- a/ext/misc/csv.c
+++ b/ext/misc/csv.c
@@ -77,6 +77,7 @@ struct CsvReader {
int n; /* Number of bytes in z */
int nAlloc; /* Space allocated for z[] */
int nLine; /* Current line number */
+ int bNotFirst; /* True if prior text has been seen */
char cTerm; /* Character that terminated the most recent field */
size_t iIn; /* Next unread character in the input buffer */
size_t nIn; /* Number of characters in the input buffer */
@@ -91,6 +92,7 @@ static void csv_reader_init(CsvReader *p){
p->n = 0;
p->nAlloc = 0;
p->nLine = 0;
+ p->bNotFirst = 0;
p->nIn = 0;
p->zIn = 0;
p->zErr[0] = 0;
@@ -251,6 +253,21 @@ static char *csv_read_one_field(CsvReader *p){
pc = c;
}
}else{
+ /* If this is the first field being parsed and it begins with the
+ ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
+ if( (c&0xff)==0xef && p->bNotFirst==0 ){
+ csv_append(p, c);
+ c = csv_getc(p);
+ if( (c&0xff)==0xbb ){
+ csv_append(p, c);
+ c = csv_getc(p);
+ if( (c&0xff)==0xbf ){
+ p->bNotFirst = 1;
+ p->n = 0;
+ return csv_read_one_field(p);
+ }
+ }
+ }
while( c>',' || (c!=EOF && c!=',' && c!='\n') ){
if( csv_append(p, (char)c) ) return 0;
c = csv_getc(p);
@@ -262,6 +279,7 @@ static char *csv_read_one_field(CsvReader *p){
p->cTerm = (char)c;
}
if( p->z ) p->z[p->n] = 0;
+ p->bNotFirst = 1;
return p->z;
}