15
15
16
16
@implementation CSVDocument
17
17
18
- @synthesize separator, rows, columnKeys, autoDetectSeparator;
19
18
20
-
21
- - (id ) init
19
+ - (id )init
22
20
{
23
- self = [super init ];
24
- if (nil != self) {
21
+ if ((self = [super init ])) {
25
22
self.separator = @" ," ;
26
23
}
27
24
28
25
return self;
29
26
}
30
27
31
- + (CSVDocument *) csvDoc
32
- {
33
- return [[[CSVDocument alloc ] init ] autorelease ];
34
- }
35
-
36
- - (void ) dealloc
37
- {
38
- self.separator = nil ;
39
- self.rows = nil ;
40
- self.columnKeys = nil ;
41
-
42
- [super dealloc ];
43
- }
44
- #pragma mark -
45
-
46
28
47
29
48
30
#pragma mark Parsing from String
49
- - (NSUInteger ) numRowsFromCSVString : (NSString *)string error : (NSError **)error
31
+ /* *
32
+ * Parse the given string into CSV rows.
33
+ */
34
+ - (NSUInteger )numRowsFromCSVString : (NSString *)string error : (NSError **)error
50
35
{
51
36
return [self numRowsFromCSVString: string maxRows: 0 error: error];
52
37
}
53
38
54
- - (NSUInteger ) numRowsFromCSVString : (NSString *)string maxRows : (NSUInteger )maxRows error : (NSError **)error
39
+ /* *
40
+ * Parse the given string into CSV rows, up to a given number of rows if "maxRows" is greater than 0.
41
+ */
42
+ - (NSUInteger )numRowsFromCSVString : (NSString *)string maxRows : (NSUInteger )maxRows error : (NSError **)error
55
43
{
56
44
NSUInteger numRows = 0 ;
57
45
@@ -61,26 +49,26 @@ - (NSUInteger) numRowsFromCSVString:(NSString *)string maxRows:(NSUInteger)maxRo
61
49
NSMutableArray *thisColumnKeys = [NSMutableArray array ];
62
50
63
51
// Check whether the file uses ";" or TAB as separator by comparing relative occurrences in the first AUTODETECT_NUM_FIRST_CHARS chars
64
- if (autoDetectSeparator ) {
52
+ if (_autoDetectSeparator ) {
65
53
self.separator = @" ," ;
66
54
67
55
NSUInteger testStringLength = ([string length ] > AUTODETECT_NUM_FIRST_CHARS) ? AUTODETECT_NUM_FIRST_CHARS : [string length ];
68
56
NSString *testString = [string substringToIndex: testStringLength];
69
- NSArray *possSeparators = [ NSArray arrayWithObjects: @" ;" , @" " , @" |" , nil ];
57
+ NSArray *possSeparators = @[ @" ;" , @" " , @" |" ];
70
58
71
59
for (NSString *s in possSeparators) {
72
- if ([[testString componentsSeparatedByString: s] count ] > [[testString componentsSeparatedByString: separator ] count ]) {
60
+ if ([[testString componentsSeparatedByString: s] count ] > [[testString componentsSeparatedByString: _separator ] count ]) {
73
61
self.separator = s;
74
62
}
75
63
}
76
64
}
77
65
78
66
// Get newline character set
79
- NSMutableCharacterSet *newlineCharacterSet = ( id ) [NSMutableCharacterSet whitespaceAndNewlineCharacterSet ];
67
+ NSMutableCharacterSet *newlineCharacterSet = [NSMutableCharacterSet whitespaceAndNewlineCharacterSet ];
80
68
[newlineCharacterSet formIntersectionWithCharacterSet: [[NSCharacterSet whitespaceCharacterSet ] invertedSet ]];
81
69
82
70
// Characters where the parser should stop
83
- NSMutableCharacterSet *importantCharactersSet = ( id ) [NSMutableCharacterSet characterSetWithCharactersInString: [NSString stringWithFormat: @" %@ \" " , separator ]];
71
+ NSMutableCharacterSet *importantCharactersSet = [NSMutableCharacterSet characterSetWithCharactersInString: [NSString stringWithFormat: @" %@ \" " , _separator ]];
84
72
[importantCharactersSet formUnionWithCharacterSet: newlineCharacterSet];
85
73
86
74
@@ -89,14 +77,14 @@ - (NSUInteger) numRowsFromCSVString:(NSString *)string maxRows:(NSUInteger)maxRo
89
77
BOOL insideQuotes = NO ; // needed to determine whether we're inside doublequotes
90
78
BOOL finishedRow = NO ; // used for the inner while loop
91
79
BOOL isNewColumn = NO ;
92
- BOOL skipWhitespace = (NSNotFound == [separator rangeOfCharacterFromSet: [NSCharacterSet whitespaceCharacterSet ]].location );
80
+ BOOL skipWhitespace = (NSNotFound == [_separator rangeOfCharacterFromSet: [NSCharacterSet whitespaceCharacterSet ]].location );
93
81
NSMutableDictionary *columns = nil ;
94
82
NSMutableString *currentCellString = [NSMutableString string ];
95
83
NSUInteger colIndex = 0 ;
96
84
97
85
NSScanner *scanner = [NSScanner scannerWithString: string];
98
86
[scanner setCharactersToBeSkipped: nil ];
99
- while (![scanner isAtEnd ]) {
87
+ while (![scanner isAtEnd ]) {
100
88
101
89
// we'll end up here after every row
102
90
insideQuotes = NO ;
@@ -106,11 +94,11 @@ - (NSUInteger) numRowsFromCSVString:(NSString *)string maxRows:(NSUInteger)maxRo
106
94
colIndex = 0 ;
107
95
108
96
// Scan row up to the next terminator
109
- while (!finishedRow) {
97
+ while (!finishedRow) {
110
98
NSString *tempString;
111
99
NSString *colKey;
112
100
if ([thisColumnKeys count ] > colIndex) {
113
- colKey = [ thisColumnKeys objectAtIndex: colIndex];
101
+ colKey = thisColumnKeys[ colIndex];
114
102
isNewColumn = NO ;
115
103
}
116
104
else {
@@ -126,12 +114,12 @@ - (NSUInteger) numRowsFromCSVString:(NSString *)string maxRows:(NSUInteger)maxRo
126
114
127
115
128
116
// found the separator
129
- if ([scanner scanString: separator intoString: NULL ]) {
117
+ if ([scanner scanString: _separator intoString: NULL ]) {
130
118
if (insideQuotes) { // Separator character inside double quotes
131
- [currentCellString appendString: separator ];
119
+ [currentCellString appendString: _separator ];
132
120
}
133
121
else { // This is a column separating comma
134
- [ columns setObject: [[ currentCellString copy ] autorelease ] forKey: colKey ];
122
+ columns[colKey] = [ currentCellString copy ];
135
123
if (isNewColumn) {
136
124
[thisColumnKeys addObject: colKey];
137
125
}
@@ -163,7 +151,7 @@ - (NSUInteger) numRowsFromCSVString:(NSString *)string maxRows:(NSUInteger)maxRo
163
151
[currentCellString appendString: tempString];
164
152
}
165
153
else { // End of row
166
- [ columns setObject: [[ currentCellString copy ] autorelease ] forKey: colKey ];
154
+ columns[colKey] = [ currentCellString copy ];
167
155
if (isNewColumn) {
168
156
[thisColumnKeys addObject: colKey];
169
157
}
@@ -175,7 +163,7 @@ - (NSUInteger) numRowsFromCSVString:(NSString *)string maxRows:(NSUInteger)maxRo
175
163
176
164
// found the end
177
165
else if ([scanner isAtEnd ]) {
178
- [ columns setObject: [[ currentCellString copy ] autorelease ] forKey: colKey ];
166
+ columns[colKey] = [ currentCellString copy ];
179
167
if (isNewColumn) {
180
168
[thisColumnKeys addObject: colKey];
181
169
}
@@ -187,7 +175,7 @@ - (NSUInteger) numRowsFromCSVString:(NSString *)string maxRows:(NSUInteger)maxRo
187
175
188
176
// one row scanned - add to the lines array
189
177
if ([columns count ] > 0 ) {
190
- CSVRowObject *newRow = [CSVRowObject rowFromDict : columns];
178
+ CSVRowObject *newRow = [CSVRowObject newWithDictionary : columns];
191
179
[thisRows addObject: newRow];
192
180
}
193
181
@@ -204,21 +192,20 @@ - (NSUInteger) numRowsFromCSVString:(NSString *)string maxRows:(NSUInteger)maxRo
204
192
205
193
// empty string
206
194
else if (nil != error) {
207
- NSDictionary *errorDict = [ NSDictionary dictionaryWithObject: @" Cannot parse an empty string" forKey: @" userInfo " ] ;
195
+ NSDictionary *errorDict = @{ @" userInfo " : @" Cannot parse an empty string" } ;
208
196
*error = [NSError errorWithDomain: NSCocoaErrorDomain code: 1 userInfo: errorDict];
209
197
}
210
198
211
199
return numRows;
212
200
}
213
- #pragma mark -
214
201
215
202
216
203
217
- #pragma mark Document Properties
218
- - (BOOL ) isFirstColumn : (NSString *)columnKey
204
+ #pragma mark - Document Properties
205
+ - (BOOL )isFirstColumn : (NSString *)columnKey
219
206
{
220
- if ((nil != columnKeys ) && ([columnKeys count ] > 0 )) {
221
- return [columnKey isEqualToString: [columnKeys objectAtIndex: 0 ]];
207
+ if ((nil != _columnKeys ) && ([_columnKeys count ] > 0 )) {
208
+ return [columnKey isEqualToString: _columnKeys[ 0 ]];
222
209
}
223
210
224
211
return NO ;
0 commit comments