Skip to content

Commit

Permalink
Merge branch 'swiftcsv:master' into MoveAwayFromResourceHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
Diggory authored Aug 24, 2023
2 parents fbfa9a4 + ed520ff commit 33ec245
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
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 @@ -113,5 +113,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)
}

}

0 comments on commit 33ec245

Please sign in to comment.