|
1 | 1 | /// Separators scalars/strings. |
2 | 2 | public enum Delimiter { |
3 | | - /// The CSV pair of delimiters (field & row delimiters). |
4 | | - public typealias Pair = (field: Self.Field, row: Self.Row) |
| 3 | + /// The CSV pair of delimiters (field & row delimiters). |
| 4 | + public typealias Pair = (field: Self.Field, row: Self.Row) |
5 | 5 | } |
6 | 6 |
|
7 | 7 | extension Delimiter { |
8 | | - /// The delimiter between fields/values. |
| 8 | + /// The delimiter between fields/values. |
| 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 `,`. |
9 | 13 | /// |
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] |
16 | | - |
17 | | - public init(nilLiteral: ()) { |
18 | | - self.scalars = Array() |
19 | | - } |
20 | | - |
21 | | - public init(unicodeScalarLiteral value: Unicode.Scalar) { |
22 | | - self.scalars = [value] |
23 | | - } |
24 | | - |
25 | | - public init(stringLiteral value: String) { |
26 | | - precondition(!value.isEmpty) |
27 | | - self.scalars = Array(value.unicodeScalars) |
28 | | - } |
29 | | - |
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) |
35 | | - } |
36 | | - |
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)) |
45 | | - } |
| 14 | + /// If it's empty, the field delimiter is unknown. |
| 15 | + let scalars: [Unicode.Scalar] |
| 16 | + |
| 17 | + public init(nilLiteral: ()) { |
| 18 | + self.scalars = Array() |
| 19 | + } |
| 20 | + |
| 21 | + public init(unicodeScalarLiteral value: Unicode.Scalar) { |
| 22 | + self.scalars = [value] |
| 23 | + } |
| 24 | + |
| 25 | + public init(stringLiteral value: String) { |
| 26 | + precondition(!value.isEmpty) |
| 27 | + self.scalars = Array(value.unicodeScalars) |
| 28 | + } |
| 29 | + |
| 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) |
46 | 35 | } |
| 36 | + |
| 37 | + /// Boolean indicating if the exact unicode scalar composition for the field delimiter is known or unknown. |
| 38 | + 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)) |
| 45 | + } |
| 46 | + } |
47 | 47 | } |
48 | 48 |
|
49 | 49 | extension Delimiter { |
50 | | - /// The delimiter between rows. |
| 50 | + /// The delimiter between rows. |
| 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 | + let scalars: Set<[Unicode.Scalar]> |
| 57 | + |
| 58 | + public init(nilLiteral: ()) { |
| 59 | + self.scalars = Set() |
| 60 | + } |
| 61 | + |
| 62 | + public init(unicodeScalarLiteral value: Unicode.Scalar) { |
| 63 | + var delimiters = Set<[Unicode.Scalar]>(minimumCapacity: 1) |
| 64 | + delimiters.insert([value]) |
| 65 | + self.scalars = delimiters |
| 66 | + } |
| 67 | + |
| 68 | + public init(stringLiteral value: String) { |
| 69 | + precondition(!value.isEmpty) |
| 70 | + |
| 71 | + var delimiters = Set<[Unicode.Scalar]>(minimumCapacity: 1) |
| 72 | + delimiters.insert(Array(value.unicodeScalars)) |
| 73 | + self.scalars = delimiters |
| 74 | + } |
| 75 | + |
| 76 | + /// Creates one or more possible row delimiters. |
| 77 | + /// - parameter delimiters:The exact composition of the row delimiters. If any of the `delimiters` is empty, the initializer fails returning `nil`. |
| 78 | + public init?<S:StringProtocol>(_ delimiters: S...) { |
| 79 | + let scalars: [[Unicode.Scalar]] = delimiters.compactMap { |
| 80 | + guard !$0.isEmpty else { return nil } |
| 81 | + return Array($0.unicodeScalars) |
| 82 | + } |
| 83 | + guard !scalars.isEmpty else { return nil } |
| 84 | + self.scalars = Set(scalars) |
| 85 | + } |
| 86 | + |
| 87 | + /// Specifies two row delimiters: CR (Carriage Return) LF (Line Feed) `\r\n` and s single line feed `\n`. |
51 | 88 | /// |
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 | | - } |
64 | | - |
65 | | - public init(nilLiteral: ()) { |
66 | | - self.scalars = Set() |
67 | | - } |
68 | | - |
69 | | - public init(unicodeScalarLiteral value: Unicode.Scalar) { |
70 | | - var delimiters = Set<[Unicode.Scalar]>(minimumCapacity: 1) |
71 | | - delimiters.insert([value]) |
72 | | - self.scalars = delimiters |
73 | | - } |
74 | | - |
75 | | - public init(stringLiteral value: String) { |
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) |
92 | | - } |
93 | | - |
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 |
97 | | - } |
98 | | - |
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 | | - } |
| 89 | + /// 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. |
| 90 | + public static var standard: Self { |
| 91 | + self.init("\n", "\r\n")! |
109 | 92 | } |
| 93 | + |
| 94 | + /// Boolean indicating if the exact unicode scalar composition for the row delimiter is known or unknown. |
| 95 | + var isKnown: Bool { |
| 96 | + !self.scalars.isEmpty |
| 97 | + } |
| 98 | + |
| 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 | 110 | } |
111 | 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 } |
131 | | - } |
| 112 | +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 } |
132 | 131 | } |
| 132 | + } |
133 | 133 | } |
0 commit comments