allow to skip a BOM mark character when parsing a CSV file

This commit is contained in:
tomasz.sowa committed 2026-11-05 07:54:36 +01:00
1 parent 98f10b310d
commit 67640b3341
2 files changed
+22 -3

No files matched your search

+15 -2
View File
@@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2021, Tomasz Sowa
* Copyright (c) 2021-2026, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -43,7 +43,8 @@ namespace pt
CSVParser::CSVParser()
{
input_as_utf8 = true;
input_as_utf8 = true;
skip_bom_char = false;
}
@@ -139,6 +140,13 @@ CSVParser::Status CSVParser::parse(const std::wstring & str, Space & out_space)
}
bool CSVParser::skip_bom(bool skip_bom_char)
{
bool old_value = this->skip_bom_char;
this->skip_bom_char = skip_bom_char;
return old_value;
}
void CSVParser::parse()
{
@@ -148,6 +156,11 @@ void CSVParser::parse()
space->set_empty_table();
read_char(); // put first character to lastc
if( skip_bom_char && lastc == 0xFEFF )
{
read_char(); // skip the BOM mark character
}
if( lastc == -1 )
{
// an empty file/string, in such a case we return such a space struct (if would be serialized to json): [[]]
+7 -1
View File
@@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2021, Tomasz Sowa
* Copyright (c) 2021-2026, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -73,6 +73,7 @@ public:
Status parse(const wchar_t * str, Space & out_space);
Status parse(const std::wstring & str, Space & out_space);
bool skip_bom(bool skip_bom_char);
protected:
@@ -83,6 +84,11 @@ protected:
*/
Status status;
/*
* if true then skip the BOM mark character (if exists)
* default: false;
*/
bool skip_bom_char;
Space * space;