Replaced uses of String(data:encoding:) with custom String(_:), which wraps String(bytes:encoding:)
This commit is contained in:
parent
366d4f235a
commit
bbf86e6917
|
@ -98,14 +98,7 @@ final class _CSVDecoder: Decoder {
|
|||
case .quote: inQuotes = !inQuotes
|
||||
case .comma, .newLine:
|
||||
if inQuotes { currentCell.append(byte); break }
|
||||
guard let title = String(data: Data(currentCell), encoding: stringDecoding) else {
|
||||
throw CoreError(
|
||||
identifier: "dataToString",
|
||||
reason: "Converting byte array to string failed",
|
||||
possibleCauses: ["This could be due to an incorrect string encoding type"]
|
||||
)
|
||||
}
|
||||
columns.append((title, []))
|
||||
try columns.append((String(currentCell), []))
|
||||
|
||||
currentCell = []
|
||||
if byte == .newLine { iterator += 1; break header }
|
||||
|
@ -148,3 +141,12 @@ final class _CSVDecoder: Decoder {
|
|||
return dictionaryResult
|
||||
}
|
||||
}
|
||||
|
||||
extension String {
|
||||
init(_ bytes: Bytes)throws {
|
||||
guard let string = String(bytes: bytes, encoding: .utf8) else {
|
||||
throw CoreError(identifier: "dataToString", reason: "Converting byte array to string using UTF-8 encoding failed")
|
||||
}
|
||||
self = string
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,10 +24,8 @@ final class _CSVKeyedDecoder<K>: KeyedDecodingContainerProtocol where K: CodingK
|
|||
|
||||
func decode(_ type: Bool.Type, forKey key: K) throws -> Bool {
|
||||
guard let cell = row[key.stringValue] else { throw DecodingError.badKey(key, at: self.codingPath + [key]) }
|
||||
guard let value = String(data: Data(cell), encoding: self.stringDecoding) else {
|
||||
throw DecodingError.dataToStringFailed(path: self.codingPath + [key], encoding: self.stringDecoding)
|
||||
}
|
||||
switch value.lowercased() {
|
||||
let value = try String(cell).lowercased()
|
||||
switch value {
|
||||
case "true", "yes", "t", "y", "1": return true
|
||||
case "false", "no", "f", "n", "0": return false
|
||||
default: throw DecodingError.unableToExtract(type: type, at: self.codingPath + [key])
|
||||
|
@ -36,10 +34,7 @@ final class _CSVKeyedDecoder<K>: KeyedDecodingContainerProtocol where K: CodingK
|
|||
|
||||
func decode(_ type: String.Type, forKey key: K) throws -> String {
|
||||
guard let cell = row[key.stringValue] else { throw DecodingError.badKey(key, at: self.codingPath + [key]) }
|
||||
guard let value = String(data: Data(cell), encoding: self.stringDecoding) else {
|
||||
throw DecodingError.dataToStringFailed(path: self.codingPath + [key], encoding: self.stringDecoding)
|
||||
}
|
||||
return value
|
||||
return try String(cell)
|
||||
}
|
||||
|
||||
func decode(_ type: Double.Type, forKey key: K) throws -> Double {
|
||||
|
|
|
@ -17,10 +17,8 @@ final class _CSVSingleValueDecoder: SingleValueDecodingContainer {
|
|||
|
||||
func decode(_ type: Bool.Type) throws -> Bool {
|
||||
guard let cell = self.value else { throw DecodingError.nilValue(type: type, at: self.codingPath) }
|
||||
guard let value = String(data: Data(cell), encoding: self.stringDecoding) else {
|
||||
throw DecodingError.dataToStringFailed(path: self.codingPath, encoding: self.stringDecoding)
|
||||
}
|
||||
switch value.lowercased() {
|
||||
let value = try String(cell).lowercased()
|
||||
switch value {
|
||||
case "true", "yes", "t", "y", "1": return true
|
||||
case "false", "no", "f", "n", "0": return false
|
||||
default: throw DecodingError.unableToExtract(type: type, at: self.codingPath)
|
||||
|
@ -29,10 +27,7 @@ final class _CSVSingleValueDecoder: SingleValueDecodingContainer {
|
|||
|
||||
func decode(_ type: String.Type) throws -> String {
|
||||
guard let cell = self.value else { throw DecodingError.nilValue(type: type, at: self.codingPath) }
|
||||
guard let value = String(data: Data(cell), encoding: self.stringDecoding) else {
|
||||
throw DecodingError.dataToStringFailed(path: self.codingPath, encoding: self.stringDecoding)
|
||||
}
|
||||
return value
|
||||
return try String(cell)
|
||||
}
|
||||
|
||||
func decode(_ type: Double.Type) throws -> Double {
|
||||
|
|
Loading…
Reference in New Issue