diff --git a/Sources/CSV/Coder/Encoder/AsyncEncoder.swift b/Sources/CSV/Coder/Encoder/AsyncEncoder.swift index aa925fd..34aa2ac 100644 --- a/Sources/CSV/Coder/Encoder/AsyncEncoder.swift +++ b/Sources/CSV/Coder/Encoder/AsyncEncoder.swift @@ -42,14 +42,14 @@ final class AsyncEncoder: Encoder { switch self.container.section { case .header: try object.encode(to: self) - self.onRow(Array(self.container.cells.joined(separator: [self.configuration.delimiter.asciiValue ?? 44]))) + self.onRow(Array(self.container.cells.joined(separator: [self.configuration.cellSeparator]))) self.container.section = .row self.container.rowCount += 1 self.container.cells = [] fallthrough case .row: try object.encode(to: self) - self.onRow(Array(self.container.cells.joined(separator: [self.configuration.delimiter.asciiValue ?? 44]))) + self.onRow(Array(self.container.cells.joined(separator: [self.configuration.cellSeparator]))) self.container.rowCount += 1 self.container.cells = [] } diff --git a/Sources/CSV/Config.swift b/Sources/CSV/Config.swift index 04f0474..2fd1c56 100644 --- a/Sources/CSV/Config.swift +++ b/Sources/CSV/Config.swift @@ -1,19 +1,10 @@ -// -// Config.swift -// CSV -// -// Created by 10.19 on 5/11/19. -// - /// Wraps the Configuration options for Parse/Encode/Decode public struct Config { - public let delimiter: Character - public let inQuotes: Bool - public let enclosingCharacter: Character // Should this be called fieldWrapper? + public let cellSeparator: UInt8 + public let cellDelimiter: UInt8? - public init(delimiter: Character = ",", inQuotes: Bool = true, enclosingCharacter: Character = "\"") { - self.delimiter = delimiter - self.inQuotes = inQuotes - self.enclosingCharacter = enclosingCharacter + public init(cellSeparator: UInt8 = 44, cellDelimiter: UInt8? = nil) { + self.cellSeparator = cellSeparator + self.cellDelimiter = cellDelimiter } } diff --git a/Sources/CSV/Parser.swift b/Sources/CSV/Parser.swift index c0e7074..44d5817 100644 --- a/Sources/CSV/Parser.swift +++ b/Sources/CSV/Parser.swift @@ -115,7 +115,6 @@ public struct Parser { var updateState = false var errors = ErrorList() var slice: (start: Int, end: Int) = (index, index) - let delimiterASCII = configuration.delimiter.asciiValue while index < data.endIndex { let byte = data[index] @@ -143,7 +142,7 @@ public struct Parser { if self.state.position == .headers { updateState = true } fallthrough } - case delimiterASCII: + case configuration.cellSeparator: if self.state.inQuotes { slice.end += 1 } else { diff --git a/Sources/CSV/Seralizer.swift b/Sources/CSV/Seralizer.swift index ca8a35e..eba3178 100644 --- a/Sources/CSV/Seralizer.swift +++ b/Sources/CSV/Seralizer.swift @@ -96,18 +96,16 @@ public struct Serializer { { var errors = ErrorList() guard data.count > 0 else { return errors.result } - - guard let delimiterASCII = configuration.delimiter.asciiValue else { return errors.result } if !self.serializedHeaders { let headers = data.keys.map { title -> [UInt8] in - if self.configuration.inQuotes { - return Array([[34], title.bytes, [34]].joined()) + if let delim = self.configuration.cellDelimiter { + return Array([[delim], title.bytes, [delim]].joined()) }else { return Array(title.bytes) } } - do { try self.onRow(Array(headers.joined(separator: [delimiterASCII]))) } + do { try self.onRow(Array(headers.joined(separator: [configuration.cellSeparator]))) } catch let error { errors.errors.append(error) } self.serializedHeaders = true } @@ -115,13 +113,13 @@ public struct Serializer { guard let first = data.first?.value else { return errors.result } (first.startIndex.. [UInt8] in - if self.configuration.inQuotes { - return Array([[34], column[index].bytes, [34]].joined()) + if let delim = self.configuration.cellDelimiter { + return Array([[delim], column[index].bytes, [delim]].joined()) }else { return Array(column[index].bytes) } } - do { try onRow(Array(cells.joined(separator: [delimiterASCII]))) } + do { try onRow(Array(cells.joined(separator: [configuration.cellSeparator]))) } catch let error { errors.errors.append(error) } }