|
2 | 2 | public enum Delimiter { |
3 | 3 | /// The CSV pair of delimiters (field & row delimiters). |
4 | 4 | public typealias Pair = (field: Self.Field, row: Self.Row) |
5 | | - /// The CSV pair of delimiter in string format. |
6 | | - internal typealias RawPair = (field: [Unicode.Scalar], row: [Unicode.Scalar]) |
7 | 5 | } |
8 | 6 |
|
9 | 7 | extension Delimiter { |
10 | 8 | /// The delimiter between fields/values. |
11 | | - public struct Field: ExpressibleByNilLiteral, ExpressibleByStringLiteral, RawRepresentable { |
12 | | - public let rawValue: String.UnicodeScalarView |
| 9 | + /// |
| 10 | + /// If the delimiter is initialized with `nil`, it implies the field delimiter is unknown and the system should try to figure it out. |
| 11 | + public struct Field: ExpressibleByNilLiteral, ExpressibleByStringLiteral, CustomStringConvertible { |
| 12 | + /// The accepted field delimiter. Usually a comma `,`. |
| 13 | + /// |
| 14 | + /// If it's empty, the field delimiter is unknown. |
| 15 | + internal let scalars: [Unicode.Scalar] |
13 | 16 |
|
14 | 17 | public init(nilLiteral: ()) { |
15 | | - self.rawValue = .init() |
| 18 | + self.scalars = Array() |
16 | 19 | } |
17 | 20 |
|
18 | 21 | public init(unicodeScalarLiteral value: Unicode.Scalar) { |
19 | | - self.rawValue = .init(repeating: value, count: 1) |
| 22 | + self.scalars = [value] |
20 | 23 | } |
21 | 24 |
|
22 | 25 | public init(stringLiteral value: String) { |
23 | | - self.rawValue = value.unicodeScalars |
| 26 | + precondition(!value.isEmpty) |
| 27 | + self.scalars = Array(value.unicodeScalars) |
24 | 28 | } |
25 | 29 |
|
26 | | - public init?(rawValue: String.UnicodeScalarView) { |
27 | | - self.rawValue = rawValue |
| 30 | + /// The field delimiter is represented by the given `String`-like type. |
| 31 | + /// - parameter delimiter: The exact composition of the field delimiter. If empty, the initializer fails returning `nil`. |
| 32 | + public init?<S:StringProtocol>(_ delimiter: S) { |
| 33 | + guard !delimiter.isEmpty else { return nil } |
| 34 | + self.scalars = Array(delimiter.unicodeScalars) |
28 | 35 | } |
29 | 36 |
|
30 | | - public init<S:StringProtocol>(_ value: S) { |
31 | | - self.rawValue = String.UnicodeScalarView(value.unicodeScalars) |
| 37 | + /// Boolean indicating if the exact unicode scalar composition for the field delimiter is known or unknown. |
| 38 | + internal var isKnown: Bool { |
| 39 | + !self.scalars.isEmpty |
| 40 | + } |
| 41 | + |
| 42 | + /// Returns the `String` representation of the field delimiter. |
| 43 | + public var description: String { |
| 44 | + String(String.UnicodeScalarView(self.scalars)) |
32 | 45 | } |
33 | 46 | } |
34 | 47 | } |
35 | 48 |
|
36 | 49 | extension Delimiter { |
37 | 50 | /// The delimiter between rows. |
38 | | - public struct Row: ExpressibleByNilLiteral, ExpressibleByStringLiteral, RawRepresentable { |
39 | | - public let rawValue: String.UnicodeScalarView |
| 51 | + /// |
| 52 | + /// If the delimiter is initialized with `nil`, it implies the row delimiter is unknown and the system should try to figure it out. |
| 53 | + public struct Row: ExpressibleByStringLiteral, ExpressibleByNilLiteral, CustomStringConvertible { |
| 54 | + /// All the accepted row delimiters. Usually, it is only one. |
| 55 | + /// - invariant: The elements of the set (i.e. the arrays) always contain at least one element. |
| 56 | + internal let scalars: Set<[Unicode.Scalar]> |
| 57 | + |
| 58 | + /// Specifies two row delimiters: CR (Carriage Return) LF (Line Feed) `\r\n` and s single line feed `\n`. |
| 59 | + /// |
| 60 | + /// This delimiter is intended to be used with CSVs where the end of the row may be marked with a CRLF sometimes and other times with LF. |
| 61 | + public static var standard: Self { |
| 62 | + self.init("\n", "\r\n")! |
| 63 | + } |
40 | 64 |
|
41 | 65 | public init(nilLiteral: ()) { |
42 | | - self.rawValue = .init() |
| 66 | + self.scalars = Set() |
43 | 67 | } |
44 | 68 |
|
45 | 69 | public init(unicodeScalarLiteral value: Unicode.Scalar) { |
46 | | - self.rawValue = .init(repeating: value, count: 1) |
| 70 | + var delimiters = Set<[Unicode.Scalar]>(minimumCapacity: 1) |
| 71 | + delimiters.insert([value]) |
| 72 | + self.scalars = delimiters |
47 | 73 | } |
48 | 74 |
|
49 | 75 | public init(stringLiteral value: String) { |
50 | | - self.rawValue = value.unicodeScalars |
| 76 | + precondition(!value.isEmpty) |
| 77 | + |
| 78 | + var delimiters = Set<[Unicode.Scalar]>(minimumCapacity: 1) |
| 79 | + delimiters.insert(Array(value.unicodeScalars)) |
| 80 | + self.scalars = delimiters |
| 81 | + } |
| 82 | + |
| 83 | + /// Creates one or more possible row delimiters. |
| 84 | + /// - parameter delimiters:The exact composition of the row delimiters. If any of the `delimiters` is empty, the initializer fails returning `nil`. |
| 85 | + public init?<S:StringProtocol>(_ delimiters: S...) { |
| 86 | + let scalars: [[Unicode.Scalar]] = delimiters.compactMap { |
| 87 | + guard !$0.isEmpty else { return nil } |
| 88 | + return Array($0.unicodeScalars) |
| 89 | + } |
| 90 | + guard !scalars.isEmpty else { return nil } |
| 91 | + self.scalars = Set(scalars) |
51 | 92 | } |
52 | 93 |
|
53 | | - public init?(rawValue: String.UnicodeScalarView) { |
54 | | - self.rawValue = rawValue |
| 94 | + /// Boolean indicating if the exact unicode scalar composition for the row delimiter is known or unknown. |
| 95 | + internal var isKnown: Bool { |
| 96 | + !self.scalars.isEmpty |
55 | 97 | } |
56 | 98 |
|
57 | | - public init<S:StringProtocol>(_ value: S) { |
58 | | - self.rawValue = String.UnicodeScalarView(value.unicodeScalars) |
| 99 | + /// Returns the `String` representation of the row delimiter. |
| 100 | + /// |
| 101 | + /// If more than one row has been provided, the `String` with less number of characters and less value (i.e. less Integer value) is selected. |
| 102 | + public var description: String { |
| 103 | + String(String.UnicodeScalarView(self.scalars.min { |
| 104 | + guard $0.count == $1.count else { return $0.count < $1.count } |
| 105 | + for (lhs, rhs) in zip($0, $1) where lhs != rhs { return lhs < rhs } |
| 106 | + return true |
| 107 | + }!)) |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +internal extension Delimiter { |
| 113 | + /// Contains the exact composition of a CSV field and row delimiter. |
| 114 | + struct Scalars { |
| 115 | + /// The exact composition of unicode scalars indetifying a field delimiter. |
| 116 | + /// - invariant: The array always contains at least one element. |
| 117 | + let field: [Unicode.Scalar] |
| 118 | + /// All possile row delimiters specifying its exact compositon of unicode scalars. |
| 119 | + /// - invariant: The set always contains at least one element and all set elements always contain at least on scalar. |
| 120 | + let row: Set<[Unicode.Scalar]> |
| 121 | + |
| 122 | + /// Designated initializer checking that the delimiters aren't empty and the field delimiter is not included in the row delimiter. |
| 123 | + /// - parameter field: The exact composition of the field delimiter. If empty, `nil` is returned. |
| 124 | + /// - parameter row: The exact composition of all possible row delimiters. If it is empty or any of its elements is an empty array, `nil` is returned. |
| 125 | + init?(field: [Unicode.Scalar], row: Set<[Unicode.Scalar]>) { |
| 126 | + guard !field.isEmpty else { return nil } |
| 127 | + self.field = field |
| 128 | + guard !row.isEmpty, row.allSatisfy({ !$0.isEmpty }) else { return nil } |
| 129 | + self.row = row |
| 130 | + guard self.row.allSatisfy({ $0 != self.field }) else { return nil } |
59 | 131 | } |
60 | 132 | } |
61 | 133 | } |
0 commit comments