forked from core-plot/core-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSString+ParseCSV.m
More file actions
57 lines (52 loc) · 2.23 KB
/
Copy pathNSString+ParseCSV.m
File metadata and controls
57 lines (52 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#import "NSString+ParseCSV.h"
@implementation NSString(ParseCSV)
-(NSArray *)arrayByParsingCSVLine
{
BOOL isRemoveWhitespace = YES;
NSMutableArray *theArray = [NSMutableArray array];
NSArray *theFields = [self componentsSeparatedByString:@","];
NSCharacterSet *quotedCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"\""];
BOOL inField = NO;
NSMutableString *theConcatenatedField = [NSMutableString string];
unsigned int i;
for ( i = 0; i < [theFields count]; i++ ) {
NSString *theField = theFields[i];
switch ( inField ) {
case NO:
if ( ([theField hasPrefix:@"\""] == YES) && ([theField hasSuffix:@"\""] == NO) ) {
inField = YES;
[theConcatenatedField appendString:theField];
[theConcatenatedField appendString:@","];
}
else {
if ( isRemoveWhitespace ) {
[theArray addObject:[theField stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
else {
[theArray addObject:theField];
}
}
break;
case YES:
[theConcatenatedField appendString:theField];
if ( [theField hasSuffix:@"\""] == YES ) {
NSString *field = [theConcatenatedField stringByTrimmingCharactersInSet:quotedCharacterSet];
if ( isRemoveWhitespace ) {
[theArray addObject:[field stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
else {
[theArray addObject:field];
}
[theConcatenatedField setString:@""];
inField = NO;
}
else {
[theConcatenatedField appendString:@","];
}
break;
}
}
return theArray;
// TODO: Check this for potential memory leaks, not sure that the array is autoreleased
}
@end