Skip to content

Commit 71ddf62

Browse files
committed
Handle CSV files with BOM
1 parent b77d5bc commit 71ddf62

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

  • rustyms/src/identification

rustyms/src/identification/csv.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl CsvLine {
100100
) -> Result<(&'a str, &'a Range<usize>), BoxedError<'a, BasicKind>> {
101101
self.fields
102102
.iter()
103-
.find(|f| *f.0 == *name)
103+
.find(|f| f.0.eq_ignore_ascii_case(name))
104104
.map(|f| (&self.line[f.1.clone()], &f.1))
105105
.ok_or_else(|| {
106106
BoxedError::new(
@@ -221,6 +221,7 @@ pub fn parse_csv_raw<T: std::io::Read>(
221221
if let Some(sep) = lines
222222
.peek()
223223
.and_then(|(_, l)| l.as_ref().ok())
224+
.map(|l| l.trim_start_matches("\u{feff}"))
224225
.and_then(|l| l.strip_prefix("sep="))
225226
{
226227
skip = true;
@@ -250,22 +251,25 @@ pub fn parse_csv_raw<T: std::io::Read>(
250251
Context::default(),
251252
)
252253
})?;
253-
let header_line = column_headers.as_ref().map_err(|err| {
254-
BoxedError::new(
255-
BasicKind::Error,
256-
"Could not read header line",
257-
err.to_string(),
258-
Context::default(),
259-
)
260-
})?;
261-
let first_line = csv_separate(&header_line, separator)
254+
let header_line = column_headers
255+
.as_ref()
256+
.map_err(|err| {
257+
BoxedError::new(
258+
BasicKind::Error,
259+
"Could not read header line",
260+
err.to_string(),
261+
Context::default(),
262+
)
263+
})?
264+
.trim_start_matches("\u{feff}");
265+
let first_line = csv_separate(header_line, separator)
262266
.map_err(BoxedError::to_owned)?
263267
.into_iter()
264268
.map(|r| Arc::new(header_line[r].to_lowercase()))
265269
.collect_vec();
266270
let provided_header = header.into_iter().map(Arc::new).collect();
267271
if first_line == provided_header {
268-
drop(lines.next()) // Ignore the first line if the first line is identical to the provided header
272+
drop(lines.next()); // Ignore the first line if the first line is identical to the provided header
269273
}
270274
provided_header
271275
} else {
@@ -285,7 +289,9 @@ pub fn parse_csv_raw<T: std::io::Read>(
285289
Context::none(),
286290
)
287291
})?;
288-
csv_separate(&header_line, separator)
292+
let header_line = header_line.trim_start_matches("\u{feff}");
293+
294+
csv_separate(header_line, separator)
289295
.map_err(BoxedError::to_owned)?
290296
.into_iter()
291297
.map(|r| Arc::new(header_line[r].to_lowercase()))

0 commit comments

Comments
 (0)