converts Config variables from Character -> UInt8
- converts delimiter -> cellSeparator - converts enclosingCharacter -> cellDelimiter - removes inQuotes - converts Char to UInt8
This commit is contained in:
parent
0c4531ddd9
commit
a011800eb2
|
@ -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 = []
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -97,17 +97,15 @@ 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..<first.endIndex).forEach { index in
|
||||
let cells = data.values.map { column -> [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) }
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue