Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a test to show error described in #126 (also fixes #126) #128

Merged
merged 5 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions SwiftCSV/CSV.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,15 @@ open class CSV<DataView : CSVView> {
/// - rowLimit: Amount of rows to parse (default is `nil`).
/// - Throws: `CSVParseError` when parsing `string` fails.
public init(string: String, delimiter: CSVDelimiter, loadColumns: Bool = true, rowLimit: Int? = nil) throws {
self.text = string
if string.hasPrefix(byteOrderMark) {
let trimmedString = string.dropFirst()
self.text = String(trimmedString)
} else {
self.text = string
}
self.delimiter = delimiter
self.header = try Parser.array(text: string, delimiter: delimiter, rowLimit: 1).first ?? []
self.content = try DataView(header: header, text: text, delimiter: delimiter, loadColumns: loadColumns, rowLimit: rowLimit)
self.header = try Parser.array(text: self.text, delimiter: delimiter, rowLimit: 1).first ?? []
self.content = try DataView(header: header, text: self.text, delimiter: delimiter, loadColumns: loadColumns, rowLimit: rowLimit)
}

/// Load CSV data from a string and guess its delimiter from `CSV.recognizedDelimiters`, falling back to `.comma`.
Expand Down Expand Up @@ -138,11 +143,7 @@ extension CSV {
/// - loadColumns: Whether to populate the columns dictionary (default is `true`)
/// - Throws: `CSVParseError` when parsing the contents of `url` fails, or file loading errors.
public convenience init(url: URL, delimiter: CSVDelimiter, encoding: String.Encoding = .utf8, loadColumns: Bool = true) throws {
var contents = try String(contentsOf: url, encoding: encoding)
if contents.hasPrefix(byteOrderMark) {
contents.removeFirst()
}

let contents = try String(contentsOf: url, encoding: encoding)
try self.init(string: contents, delimiter: delimiter, loadColumns: loadColumns)
}

Expand All @@ -154,11 +155,7 @@ extension CSV {
/// - loadColumns: Whether to populate the columns dictionary (default is `true`)
/// - Throws: `CSVParseError` when parsing the contents of `url` fails, or file loading errors.
public convenience init(url: URL, encoding: String.Encoding = .utf8, loadColumns: Bool = true) throws {
var contents = try String(contentsOf: url, encoding: encoding)
if contents.hasPrefix(byteOrderMark) {
contents.removeFirst()
}

let contents = try String(contentsOf: url, encoding: encoding)
try self.init(string: contents, loadColumns: loadColumns)
}
}
Expand Down
21 changes: 21 additions & 0 deletions SwiftCSVTests/URLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,26 @@ class URLTests: XCTestCase {
XCTAssertEqual(expected[index], row)
}
}

func testBOMInHeadersWhenInitialisingFromString() throws {
var csv: CSV<Named>?
let csvString: String = """
Part Number,Description,Unit Price,Qty
12345,Heizölrückstoßabdämpfung,"€ 100,00",2"
"""
let csvStringWithBOM = "\u{FEFF}" + csvString

// Make a CSV object
do {
// Create from string
csv = try CSV<Named>(string: csvStringWithBOM)
} catch {
XCTFail("Could not convert string literal to CSV instance")
}

// Check that headers match
let correctMatchingHeader = ["Part Number", "Description", "Unit Price", "Qty"]
XCTAssertEqual(csv!.header, correctMatchingHeader)
}

}
Loading