Skip to content

Commit

Permalink
Merge pull request #33 from twotoasters/master
Browse files Browse the repository at this point in the history
Add separate option for preserving or stripping leading and trailing whitespace
  • Loading branch information
davedelong committed May 6, 2013
2 parents 01c5b60 + 3bb01c6 commit 59e4d66
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ build
**/*.perspective*
**/*.pbxuser
**/*.mode*
*.xcodeproj/xcworkspace/xcuserdata/**
*.xcodeproj/xcworkspace/xcuserdata/**
.DS_Store
xcuserdata
4 changes: 3 additions & 1 deletion CHCSVParser/CHCSVParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ typedef NSInteger CHCSVErrorCode;
@property (assign) BOOL recognizesBackslashesAsEscapes; // default is NO
@property (assign) BOOL sanitizesFields; // default is NO
@property (assign) BOOL recognizesComments; // default is NO
@property (assign) BOOL stripsLeadingAndTrailingWhitespace; // default is NO

@property (readonly) NSUInteger totalBytesRead;

Expand Down Expand Up @@ -91,7 +92,8 @@ typedef NSInteger CHCSVErrorCode;
typedef NS_OPTIONS(NSUInteger, CHCSVParserOptions) {
CHCSVParserOptionsRecognizesBackslashesAsEscapes = 1 << 0,
CHCSVParserOptionsSanitizesFields = 1 << 1,
CHCSVParserOptionsRecognizesComments = 1 << 2
CHCSVParserOptionsRecognizesComments = 1 << 2,
CHCSVParserOptionsStripsLeadingAndTrailingWhitespace = 1 << 3
};

@interface NSArray (CHCSVAdditions)
Expand Down
23 changes: 18 additions & 5 deletions CHCSVParser/CHCSVParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ - (id)initWithInputStream:(NSInputStream *)stream usedEncoding:(NSStringEncoding
_recognizesBackslashesAsEscapes = NO;
_sanitizesFields = NO;
_sanitizedField = [[NSMutableString alloc] init];
_stripsLeadingAndTrailingWhitespace = NO;

NSMutableCharacterSet *m = [[NSCharacterSet newlineCharacterSet] mutableCopy];
NSString *invalid = [NSString stringWithFormat:@"%c%C", DOUBLE_QUOTE, _delimiter];
Expand Down Expand Up @@ -347,18 +348,26 @@ - (BOOL)_parseField {

BOOL parsedField = NO;
[self _beginField];
// consume leading whitespace
[self _parseFieldWhitespace];
if (_stripsLeadingAndTrailingWhitespace) {
// consume leading whitespace
[self _parseFieldWhitespace];
}

if ([self _peekCharacter] == DOUBLE_QUOTE) {
parsedField = [self _parseEscapedField];
} else {
parsedField = [self _parseUnescapedField];
if (_stripsLeadingAndTrailingWhitespace) {
NSString *trimmedString = [_sanitizedField stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[_sanitizedField setString:trimmedString];
}
}

if (parsedField) {
// consume trailing whitespace
[self _parseFieldWhitespace];
if (_stripsLeadingAndTrailingWhitespace) {
// consume trailing whitespace
[self _parseFieldWhitespace];
}
[self _endField];
}
return parsedField;
Expand Down Expand Up @@ -490,9 +499,11 @@ - (void)_endField {

if (_sanitizesFields) {
field = CHCSV_AUTORELEASE([_sanitizedField copy]);
field = [field stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
} else {
field = [_string substringWithRange:_fieldRange];
if (_stripsLeadingAndTrailingWhitespace) {
field = [field stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
}

if ([_delegate respondsToSelector:@selector(parser:didReadField:atIndex:)]) {
Expand Down Expand Up @@ -731,6 +742,7 @@ + (instancetype)arrayWithContentsOfCSVFile:(NSString *)csvFilePath options:(CHCS
[parser setRecognizesBackslashesAsEscapes:!!(options & CHCSVParserOptionsRecognizesBackslashesAsEscapes)];
[parser setSanitizesFields:!!(options & CHCSVParserOptionsSanitizesFields)];
[parser setRecognizesComments:!!(options & CHCSVParserOptionsRecognizesComments)];
[parser setStripsLeadingAndTrailingWhitespace:!!(options & CHCSVParserOptionsStripsLeadingAndTrailingWhitespace)];

[parser parse];
CHCSV_RELEASE(parser);
Expand Down Expand Up @@ -773,6 +785,7 @@ - (NSArray *)CSVComponentsWithOptions:(CHCSVParserOptions)options {
[parser setRecognizesBackslashesAsEscapes:!!(options & CHCSVParserOptionsRecognizesBackslashesAsEscapes)];
[parser setSanitizesFields:!!(options & CHCSVParserOptionsSanitizesFields)];
[parser setRecognizesComments:!!(options & CHCSVParserOptionsRecognizesComments)];
[parser setStripsLeadingAndTrailingWhitespace:!!(options & CHCSVParserOptionsStripsLeadingAndTrailingWhitespace)];

[parser parse];
CHCSV_RELEASE(parser);
Expand Down
2 changes: 1 addition & 1 deletion UnitTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ - (NSArray *) expectedFields {
- (void) testCSV {
NSString *file = [[NSBundle bundleForClass:[self class]] pathForResource:@"Test" ofType:@"csv"];

NSArray *fields = [NSArray arrayWithContentsOfCSVFile:file];
NSArray *fields = [NSArray arrayWithContentsOfCSVFile:file options:CHCSVParserOptionsRecognizesBackslashesAsEscapes];
NSLog(@"read: %@", fields);

NSArray *expectedFields = [self expectedFields];
Expand Down

0 comments on commit 59e4d66

Please sign in to comment.