Skip to content

Commit ef686db

Browse files
committed
Strategies restructuring
1 parent 5f7e96e commit ef686db

19 files changed

+544
-458
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
</p>
44

55
<p align="center">
6-
<img src="docs/Assets/Badges/Swift.svg" alt="Swift 5.x">
7-
<img src="docs/Assets/Badges/Apple.svg" alt="macOS 10.10+ - iOS 8+ - tvOS 9+ - watchOS 2+">
8-
<img src="docs/Assets/Badges/Ubuntu.svg" alt="Ubuntu 18.04">
6+
<a href="https://swift.org/about/#swiftorg-and-open-source"><img src="docs/Assets/Badges/Swift.svg" alt="Swift 5.x"></a>
7+
<a href="https://www.apple.com/macos"><img src="docs/Assets/Badges/Apple.svg" alt="macOS 10.10+ - iOS 8+ - tvOS 9+ - watchOS 2+"></a>
8+
<a href="https://ubuntu.com"><img src="docs/Assets/Badges/Ubuntu.svg" alt="Ubuntu 18.04"></a>
99
<a href="http://doge.mit-license.org"><img src="docs/Assets/Badges/License.svg" alt="MIT License"></a>
1010
</p>
1111

sources/Active/Reader/ReaderConfiguration.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import Foundation
33
extension CSVReader {
44
/// Configuration for how to read CSV data.
55
public struct Configuration {
6+
/// The encoding used to identify the underlying data or `nil` if you want the CSV reader to try to figure it out.
7+
///
8+
/// If no encoding is provided and the input data doesn't contain a Byte Order Marker (BOM), UTF8 is presumed.
9+
public var encoding: String.Encoding?
610
/// The field and row delimiters.
711
public var delimiters: Delimiter.Pair
812
/// The strategy to allow/disable escaped fields and how.
@@ -11,10 +15,6 @@ extension CSVReader {
1115
public var headerStrategy: Strategy.Header
1216
/// Trims the given characters at the beginning and end of each row, and between fields.
1317
public var trimStrategry: CharacterSet
14-
/// The encoding used to identify the underlying data or `nil` if you want the CSV reader to try to figure it out.
15-
///
16-
/// If no encoding is provided and the input data doesn't contain a Byte Order Marker (BOM), UTF8 is presumed.
17-
public var encoding: String.Encoding?
1818
/// Boolean indicating whether the data/file/string should be completely parsed at reader's initialization.
1919
///
2020
/// Setting this property to `true` samples the data/file/string at initialization time. This process returns some interesting data such as blob/file size, full-file encoding validation, etc.
@@ -23,11 +23,11 @@ extension CSVReader {
2323

2424
/// Designated initializer setting the default values.
2525
public init() {
26+
self.encoding = nil
2627
self.delimiters = (field: ",", row: "\n")
2728
self.escapingStrategy = .doubleQuote
2829
self.headerStrategy = .none
2930
self.trimStrategry = .init()
30-
self.encoding = nil
3131
self.presample = false
3232
}
3333
}

sources/Active/Reader/ReaderEncodings.swift

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
import Foundation
22

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+
329
internal extension String.Encoding {
430
/// Starts parsing the CSV file to try to figure out its encoding.
531
///
@@ -38,32 +64,6 @@ internal extension String.Encoding {
3864
}
3965
}
4066

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-
6767
fileprivate extension String.Encoding {
6868
/// Creates an encoding if the data return by `dataFetcher` contains BOM bytes.
6969
/// - parameter unusedBytes: The input data bytes that have been read, but are not part from the BOM.

sources/Active/Reader/ReaderInference.swift

Lines changed: 0 additions & 101 deletions
This file was deleted.

0 commit comments

Comments
 (0)