converts Config variables from Character -> UInt8

- converts delimiter -> cellSeparator
- converts enclosingCharacter -> cellDelimiter
- removes inQuotes
- converts Char to UInt8
This commit is contained in:
Justin 2019-05-11 13:24:45 -06:00
parent 0c4531ddd9
commit a011800eb2
4 changed files with 14 additions and 26 deletions

View File

@ -42,14 +42,14 @@ final class AsyncEncoder: Encoder {
switch self.container.section { switch self.container.section {
case .header: case .header:
try object.encode(to: self) 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.section = .row
self.container.rowCount += 1 self.container.rowCount += 1
self.container.cells = [] self.container.cells = []
fallthrough fallthrough
case .row: case .row:
try object.encode(to: self) 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.rowCount += 1
self.container.cells = [] self.container.cells = []
} }

View File

@ -1,19 +1,10 @@
//
// Config.swift
// CSV
//
// Created by 10.19 on 5/11/19.
//
/// Wraps the Configuration options for Parse/Encode/Decode /// Wraps the Configuration options for Parse/Encode/Decode
public struct Config { public struct Config {
public let delimiter: Character public let cellSeparator: UInt8
public let inQuotes: Bool public let cellDelimiter: UInt8?
public let enclosingCharacter: Character // Should this be called fieldWrapper?
public init(delimiter: Character = ",", inQuotes: Bool = true, enclosingCharacter: Character = "\"") { public init(cellSeparator: UInt8 = 44, cellDelimiter: UInt8? = nil) {
self.delimiter = delimiter self.cellSeparator = cellSeparator
self.inQuotes = inQuotes self.cellDelimiter = cellDelimiter
self.enclosingCharacter = enclosingCharacter
} }
} }

View File

@ -115,7 +115,6 @@ public struct Parser {
var updateState = false var updateState = false
var errors = ErrorList() var errors = ErrorList()
var slice: (start: Int, end: Int) = (index, index) var slice: (start: Int, end: Int) = (index, index)
let delimiterASCII = configuration.delimiter.asciiValue
while index < data.endIndex { while index < data.endIndex {
let byte = data[index] let byte = data[index]
@ -143,7 +142,7 @@ public struct Parser {
if self.state.position == .headers { updateState = true } if self.state.position == .headers { updateState = true }
fallthrough fallthrough
} }
case delimiterASCII: case configuration.cellSeparator:
if self.state.inQuotes { if self.state.inQuotes {
slice.end += 1 slice.end += 1
} else { } else {

View File

@ -96,18 +96,16 @@ public struct Serializer {
{ {
var errors = ErrorList() var errors = ErrorList()
guard data.count > 0 else { return errors.result } guard data.count > 0 else { return errors.result }
guard let delimiterASCII = configuration.delimiter.asciiValue else { return errors.result }
if !self.serializedHeaders { if !self.serializedHeaders {
let headers = data.keys.map { title -> [UInt8] in let headers = data.keys.map { title -> [UInt8] in
if self.configuration.inQuotes { if let delim = self.configuration.cellDelimiter {
return Array([[34], title.bytes, [34]].joined()) return Array([[delim], title.bytes, [delim]].joined())
}else { }else {
return Array(title.bytes) 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) } catch let error { errors.errors.append(error) }
self.serializedHeaders = true self.serializedHeaders = true
} }
@ -115,13 +113,13 @@ public struct Serializer {
guard let first = data.first?.value else { return errors.result } guard let first = data.first?.value else { return errors.result }
(first.startIndex..<first.endIndex).forEach { index in (first.startIndex..<first.endIndex).forEach { index in
let cells = data.values.map { column -> [UInt8] in let cells = data.values.map { column -> [UInt8] in
if self.configuration.inQuotes { if let delim = self.configuration.cellDelimiter {
return Array([[34], column[index].bytes, [34]].joined()) return Array([[delim], column[index].bytes, [delim]].joined())
}else { }else {
return Array(column[index].bytes) 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) } catch let error { errors.errors.append(error) }
} }