Skip to content

Instantly share code, notes, and snippets.

@Wevah
Last active October 8, 2015 17:38
Show Gist options
  • Save Wevah/3365918 to your computer and use it in GitHub Desktop.
Save Wevah/3365918 to your computer and use it in GitHub Desktop.
Pipe Parse
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
@autoreleasepool {
NSString *str = @"foo|bar|baz\\|quux";
BOOL lastWasBackslash = NO;
NSMutableString *sub = [NSMutableString string];
NSMutableArray *matches = [NSMutableArray array];
for (NSUInteger idx; idx < [str length]; ++idx) {
unichar c = [str characterAtIndex:idx];
NSLog(@"%C", c);
if (lastWasBackslash) {
[sub appendFormat:@"%C", c];
lastWasBackslash = NO;
continue;
}
if (c == '\\') {
lastWasBackslash = YES;
continue;
}
if (c == '|') {
NSLog(@"pipe");
[matches addObject:sub];
sub = [NSMutableString string];
} else {
[sub appendFormat:@"%C", c];
}
}
[matches addObject:sub];
NSLog(@"%@", matches);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment