@@ -9,6 +9,8 @@ extension CSVReader {
99 public var headerStrategy : Strategy . Header
1010 /// Trims the given characters at the beginning and end of each row, and between fields.
1111 public var trimStrategry : CharacterSet
12+ /// The strategy for escaping quoted fields.
13+ public var escapingStrategy : Strategy . Escaping
1214 /// The encoding used to identify the underlying data or `nil` if you want the CSV reader to try to figure it out.
1315 ///
1416 /// If no encoding is provided and the input data doesn't contain a Byte Order Marker (BOM), UTF8 is presumed.
@@ -24,6 +26,7 @@ extension CSVReader {
2426 self . delimiters = ( field: " , " , row: " \n " )
2527 self . headerStrategy = . none
2628 self . trimStrategry = . init( )
29+ self . escapingStrategy = . doubleQuote
2730 self . encoding = nil
2831 self . presample = false
2932 }
@@ -38,7 +41,7 @@ extension CSVReader {
3841 /// The characters set to be trimmed at the beginning and ending of each field.
3942 let trimCharacters : CharacterSet
4043 /// The unicode scalar used as encapsulator and escaping character (when printed two times).
41- let escapingScalar : Unicode . Scalar = " \" "
44+ let escapingScalar : Unicode . Scalar ?
4245
4346 /// Creates the inmutable reader settings from the user provided configuration values.
4447 /// - parameter configuration: The configuration values provided by the API user.
@@ -61,6 +64,12 @@ extension CSVReader {
6164 }
6265 // 2. Set the trim characters set.
6366 self . trimCharacters = configuration. trimStrategry
67+ // 3. Set the escaping scalar.
68+ self . escapingScalar = configuration. escapingStrategy. scalar
69+ // 4. Ensure trim character set does not include escaping scalar
70+ if let escapingScalar = escapingScalar, trimCharacters. contains ( escapingScalar) {
71+ throw Error . invalidTrimCharacter ( escapingScalar: escapingScalar, trimCharacters: trimCharacters)
72+ }
6473 }
6574 }
6675}
@@ -74,4 +83,11 @@ fileprivate extension CSVReader.Error {
7483 help: " Set different delimiters for field and rows. " ,
7584 userInfo: [ " Delimiter " : delimiter] )
7685 }
86+
87+ static func invalidTrimCharacter( escapingScalar: Unicode . Scalar , trimCharacters: CharacterSet ) -> CSVError < CSVReader > {
88+ . init( . invalidConfiguration,
89+ reason: " The trim characters set can not include the escaping scalar. " ,
90+ help: " Remove the escaping scalar from the trim characters set. " ,
91+ userInfo: [ " Escaping scalar " : escapingScalar, " Trim characters " : trimCharacters] )
92+ }
7793}
0 commit comments