Created
November 13, 2019 04:01
-
-
Save liyanage/c7c762a397db21cbed29758da03fd848 to your computer and use it in GitHub Desktop.
Enumerate lines in a large file with memory mapping through NSData
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
typedef void(^LineEnumerator)(NSData *lineData, BOOL *stop); | |
void enumerateLinesInFileAtURL(NSURL *url, LineEnumerator lineEnumerator) { | |
NSError *error = nil; | |
NSData *data = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedAlways error:&error]; | |
if (!data) { | |
NSLog(@"Unable to open: %@", error); | |
return; | |
} | |
NSData *separatorData = [@"\n" dataUsingEncoding:NSUTF8StringEncoding]; | |
BOOL shouldStop = NO; | |
NSRange searchRange = NSMakeRange(0, data.length); | |
while (!shouldStop && searchRange.length) { | |
NSRange separatorRange = [data rangeOfData:separatorData options:0 range:searchRange]; | |
if (separatorRange.location == NSNotFound) { | |
// Imaginary zero-length separator at the end of the input | |
separatorRange = NSMakeRange(NSMaxRange(searchRange), 0); | |
} | |
NSRange lineRange = NSMakeRange(searchRange.location, separatorRange.location - searchRange.location); | |
NSData *lineData = [data subdataWithRange:lineRange]; | |
lineEnumerator(lineData, &shouldStop); | |
NSInteger advance = lineRange.length + separatorRange.length; | |
searchRange.location += advance; | |
searchRange.length -= advance; | |
} | |
} | |
int main(int argc, const char * argv[]) { | |
@autoreleasepool { | |
NSURL *url = [NSURL fileURLWithFileSystemRepresentation:argv[1] isDirectory:NO relativeToURL:nil]; | |
enumerateLinesInFileAtURL(url, ^(NSData *lineData, BOOL *stop) { | |
NSString *lineString = [[NSString alloc] initWithData:lineData encoding:NSUTF8StringEncoding]; | |
NSLog(@"line: %@", lineString); | |
}); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment