- (NSArray*)extractChapterList
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString* regPattern = @"(\\s)+[第]{0,1}[0-9一二三四五六七八九十百千万]+章[ \t]*(\\S)*";
NSError* error = nil;
NSData* contentsData = [self readData:0 length:_fileSize];
NSString* contentsStr = [[NSString alloc] initWithData:contentsData
encoding:_fileEncode];
NSCharacterSet* whiteSpaceSet = [NSCharacterSet characterSetWithCharactersInString:@" \f\n\r\t\v"];
NSRegularExpression* regExp = [NSRegularExpression regularExpressionWithPattern:regPattern
options:NSRegularExpressionCaseInsensitive
error:&error];
__block int64_t offsetInFile = 0;
__weak typeof(self) _self = self;
[regExp enumerateMatchesInString:contentsStr
options:NSMatchingReportCompletion
range:NSMakeRange(0, contentsStr.length)
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSRange range = [result range];
if (range.length > 0) {
NSString* chapterTitle = [[contentsStr substringWithRange:range] stringByTrimmingCharactersInSet:whiteSpaceSet];
NSUInteger startSearchPos = MAX(0, offsetInFile);
offsetInFile = [_self chapterOffsetInFile:contentsData
searchPosition:startSearchPos
chapterTitle:chapterTitle];
dispatch_sync(dispatch_get_main_queue(), ^{
[_self publishProgress:chapterTitle offsetInFile:offsetInFile];
});
}
}];
});
return nil;
}
// Run in main thread to update UI
- (void)publishProgress:(NSString*)chapterTitle offsetInFile:(uint64_t)offset
{
NSLog(@"loc:%llu title:%@",offset, chapterTitle);
}
- (int64_t)chapterOffsetInFile:(NSData*)fileContents
searchPosition:(NSUInteger)searchPosition
chapterTitle:(NSString*)chapterTitle
{
NSData* titleData = [chapterTitle dataUsingEncoding:_fileEncode];
NSRange chapterRange = [fileContents rangeOfData:titleData
options:kNilOptions
range:NSMakeRange(searchPosition, [fileContents length]-searchPosition)];
if (chapterRange.length > 0) {
NSLog(@"range : (%ld,%ld)", chapterRange.location, chapterRange.length);
return chapterRange.location;
}
return -1;
}
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString* regPattern = @"(\\s)+[第]{0,1}[0-9一二三四五六七八九十百千万]+章[ \t]*(\\S)*";
NSError* error = nil;
NSData* contentsData = [self readData:0 length:_fileSize];
NSString* contentsStr = [[NSString alloc] initWithData:contentsData
encoding:_fileEncode];
NSCharacterSet* whiteSpaceSet = [NSCharacterSet characterSetWithCharactersInString:@" \f\n\r\t\v"];
NSRegularExpression* regExp = [NSRegularExpression regularExpressionWithPattern:regPattern
options:NSRegularExpressionCaseInsensitive
error:&error];
__block int64_t offsetInFile = 0;
__weak typeof(self) _self = self;
[regExp enumerateMatchesInString:contentsStr
options:NSMatchingReportCompletion
range:NSMakeRange(0, contentsStr.length)
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSRange range = [result range];
if (range.length > 0) {
NSString* chapterTitle = [[contentsStr substringWithRange:range] stringByTrimmingCharactersInSet:whiteSpaceSet];
NSUInteger startSearchPos = MAX(0, offsetInFile);
offsetInFile = [_self chapterOffsetInFile:contentsData
searchPosition:startSearchPos
chapterTitle:chapterTitle];
dispatch_sync(dispatch_get_main_queue(), ^{
[_self publishProgress:chapterTitle offsetInFile:offsetInFile];
});
}
}];
});
return nil;
}
// Run in main thread to update UI
- (void)publishProgress:(NSString*)chapterTitle offsetInFile:(uint64_t)offset
{
NSLog(@"loc:%llu title:%@",offset, chapterTitle);
}
- (int64_t)chapterOffsetInFile:(NSData*)fileContents
searchPosition:(NSUInteger)searchPosition
chapterTitle:(NSString*)chapterTitle
{
NSData* titleData = [chapterTitle dataUsingEncoding:_fileEncode];
NSRange chapterRange = [fileContents rangeOfData:titleData
options:kNilOptions
range:NSMakeRange(searchPosition, [fileContents length]-searchPosition)];
if (chapterRange.length > 0) {
NSLog(@"range : (%ld,%ld)", chapterRange.location, chapterRange.length);
return chapterRange.location;
}
return -1;
}