From 67640b334133e2304a8d2818657adb89baaee814 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Thu, 5 Nov 2026 07:54:36 +0100 Subject: [PATCH] allow to skip a BOM mark character when parsing a CSV file --- src/csv/csvparser.cpp | 17 +++++++++++++++-- src/csv/csvparser.h | 8 +++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/csv/csvparser.cpp b/src/csv/csvparser.cpp index df7ceb3..a1a7d16 100644 --- a/src/csv/csvparser.cpp +++ b/src/csv/csvparser.cpp @@ -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): [[]] diff --git a/src/csv/csvparser.h b/src/csv/csvparser.h index 176042c..5f991a1 100644 --- a/src/csv/csvparser.h +++ b/src/csv/csvparser.h @@ -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;