|
1 | 1 | import Foundation |
2 | 2 |
|
| 3 | +internal extension CSVReader { |
| 4 | + /// Select the appropriate encoding depending on the `String` encoding provided by the user and the encoding inferred from the Byte Order Marker. |
| 5 | + /// - parameter provided: The user provided `String` encoding. |
| 6 | + /// - parameter inferred: The `String` encoding inferred from the data Byte Order Marker. |
| 7 | + /// - throws: `CSVError<CSVReader>` exclusively. |
| 8 | + /// - returns: The appropriate `String.Encoding` matching from the provided and inferred values. |
| 9 | + static func selectEncodingFrom(provided: String.Encoding?, inferred: String.Encoding?) throws -> String.Encoding { |
| 10 | + switch (provided, inferred) { |
| 11 | + case (nil, nil): return .utf8 |
| 12 | + case (nil, let rhs?): return rhs |
| 13 | + case (let lhs?, nil): return lhs |
| 14 | + case (let lhs?, let rhs?) where lhs == rhs: return lhs |
| 15 | + case (let lhs?, let rhs?): // Only executes when lhs != rhs |
| 16 | + switch (lhs, rhs) { |
| 17 | + case (.utf16, .utf16LittleEndian), |
| 18 | + (.utf16, .utf16BigEndian), |
| 19 | + (.utf32, .utf32LittleEndian), |
| 20 | + (.utf32, .utf32BigEndian), |
| 21 | + (.unicode, .utf16LittleEndian), |
| 22 | + (.unicode, .utf16BigEndian): return rhs |
| 23 | + default: throw CSVReader.Error.mismatchedEncoding(provided: lhs, inferred: rhs) |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
3 | 29 | internal extension String.Encoding { |
4 | 30 | /// Starts parsing the CSV file to try to figure out its encoding. |
5 | 31 | /// |
@@ -38,32 +64,6 @@ internal extension String.Encoding { |
38 | 64 | } |
39 | 65 | } |
40 | 66 |
|
41 | | -internal extension CSVReader { |
42 | | - /// Select the appropriate encoding depending on the `String` encoding provided by the user and the encoding inferred from the Byte Order Marker. |
43 | | - /// - parameter provided: The user provided `String` encoding. |
44 | | - /// - parameter inferred: The `String` encoding inferred from the data Byte Order Marker. |
45 | | - /// - throws: `CSVError<CSVReader>` exclusively. |
46 | | - /// - returns: The appropriate `String.Encoding` matching from the provided and inferred values. |
47 | | - static func selectEncodingFrom(provided: String.Encoding?, inferred: String.Encoding?) throws -> String.Encoding { |
48 | | - switch (provided, inferred) { |
49 | | - case (nil, nil): return .utf8 |
50 | | - case (nil, let rhs?): return rhs |
51 | | - case (let lhs?, nil): return lhs |
52 | | - case (let lhs?, let rhs?) where lhs == rhs: return lhs |
53 | | - case (let lhs?, let rhs?): // Only executes when lhs != rhs |
54 | | - switch (lhs, rhs) { |
55 | | - case (.utf16, .utf16LittleEndian), |
56 | | - (.utf16, .utf16BigEndian), |
57 | | - (.utf32, .utf32LittleEndian), |
58 | | - (.utf32, .utf32BigEndian), |
59 | | - (.unicode, .utf16LittleEndian), |
60 | | - (.unicode, .utf16BigEndian): return rhs |
61 | | - default: throw CSVReader.Error.mismatchedEncoding(provided: lhs, inferred: rhs) |
62 | | - } |
63 | | - } |
64 | | - } |
65 | | -} |
66 | | - |
67 | 67 | fileprivate extension String.Encoding { |
68 | 68 | /// Creates an encoding if the data return by `dataFetcher` contains BOM bytes. |
69 | 69 | /// - parameter unusedBytes: The input data bytes that have been read, but are not part from the BOM. |
|
0 commit comments