swiftcsv / SwiftCSV

CSV parser for Swift
MIT License
947 stars 190 forks source link

Strip BOM from start of CSV (or exclude it from Headers) #126

Closed Diggory closed 1 year ago

Diggory commented 1 year ago

Hello,

It seems that macOS Excel puts a ByteOrderMarker at the start of exported CSV files. This is then copied into the first Header object.

e.g. I wanted to check that the user had imported a CSV file with the expected columns:

let correctMatchingHeader = ["Owner", "Major", "Minor", "UUID"]
guard csv.header == correctMatchingHeader else {
    log.error("CSV Headers aren't as expected: \(csv.header) vs \(correctMatchingHeader)")
    throw HBHTTPError(.badRequest)
}

gave me the error:

2023-04-09 12:44:10 🧵8744294 [🔴 ERROR] GateControl/Beacon.swift#L.265 uploadCSV(request:) CSV Headers aren't as expected: ["Owner", "Major", "Minor", "UUID"] vs ["Owner", "Major", "Minor", "UUID"]

They appear the same, but copying the error to TextMate shows that there's an invisible character in the first header (Owner), so the arrays don't match.

CSV Headers aren't as expected: ["<U+FEFF>Owner", "Major", "Minor", "UUID"] vs ["Owner", "Major", "Minor", "UUID"]

This is a BOM character at the start of the CSV file. See more here

Would it be possible to remove the character when building the 'headers'?

Thanks.

DivineDominion commented 1 year ago

Hmm we addressed this in #103 and #104 I thought.

@Diggory Could you add a failing test to the SwiftCSV test suite that reproduces the problem you describe? If you have a fix handy, even better, but the failing test would be a good start.