diff --git a/MP42/MP42SSAParser.m b/MP42/MP42SSAParser.m index 4433cc7..b94c29d 100644 --- a/MP42/MP42SSAParser.m +++ b/MP42/MP42SSAParser.m @@ -181,13 +181,20 @@ static NSArray * parse(NSString * string, NSUInteger count, BOOL mkv NSScanner *sc = [NSScanner scannerWithString:string]; NSMutableArray *valuesArray = [NSMutableArray array]; + // No field layout (header scan found nothing): there is nothing to split + // into. Without this guard the `count - 1` loop bound below underflows + // and appends empty values until the array traps. + if (count == 0) { + return valuesArray; + } + if (mkvStyle || [sc scanUpToString:@"Dialogue:" intoString:nil] || [sc scanString:@"Dialogue:" intoString:nil]) { [sc scanString:@"Dialogue:" intoString:nil]; NSString *value; for (NSUInteger index = 0; index < count - 1; index += 1) { - if ([sc scanUpToString:@"," intoString:&value]) { + if ([sc scanUpToString:@"," intoString:&value] && value) { [valuesArray addObject:value]; } else { @@ -322,24 +329,30 @@ static NSMutableString *STStandardizeStringNewlines(NSString *str) - (void)parseHeader:(NSString *)header { - NSString *info; - NSString *styles; - NSString *format; - - NSScanner *sc = [NSScanner scannerWithString:header]; - [sc setCharactersToBeSkipped:nil]; - - if ([sc scanUpToString:@"[V4+ Styles]" intoString:&info]) { - _info = [self parseInfo:info]; + // A failed scanUpToString: consumes the whole string here, so the + // section markers are located explicitly: a file without a + // "[V4+ Styles]" section (plain SSA, or no styles at all) used to leave + // the scan position at the end of the document, and the Format: line in + // [Events] was never found — every Dialogue line then parsed to nothing. + NSUInteger stylesLoc = [header rangeOfString:@"[V4+ Styles]"].location; + NSUInteger eventsLoc = [header rangeOfString:@"[Events]"].location; + if (stylesLoc != NSNotFound && (eventsLoc == NSNotFound || stylesLoc > eventsLoc)) { + stylesLoc = NSNotFound; // a styles marker inside/after Events is not the styles header } - if ([sc scanUpToString:@"[Events]" intoString:&styles]) { - _styles = [self parseStyles:styles]; + if (stylesLoc != NSNotFound) { + _info = [self parseInfo:[header substringToIndex:stylesLoc]]; + NSUInteger stylesEnd = (eventsLoc != NSNotFound) ? eventsLoc : header.length; + _styles = [self parseStyles:[header substringWithRange:NSMakeRange(stylesLoc, stylesEnd - stylesLoc)]]; + } else { + NSUInteger stylesEnd = (eventsLoc != NSNotFound) ? eventsLoc : header.length; + _styles = [self parseStyles:[header substringToIndex:stylesEnd]]; } - format = [sc.string substringFromIndex:sc.scanLocation]; - if (format.length) { - _format = [self parseFormat:format]; + if (eventsLoc != NSNotFound) { + _format = [self parseFormat:[header substringFromIndex:eventsLoc]]; + } else if (stylesLoc == NSNotFound) { + _format = [self parseFormat:header]; // no sections at all: last resort } } @@ -399,11 +412,17 @@ static NSMutableString *STStandardizeStringNewlines(NSString *str) [sc scanString:@"Format:" intoString:nil]; NSString *formats; if ([sc scanUpToString:@"\n" intoString:&formats]) { - NSScanner *formatScanner = [NSScanner scannerWithString:formats]; - NSString *formatName; - while ([formatScanner scanUpToString:@"," intoString:&formatName]) { - [formatScanner scanString:@"," intoString:nil]; - [formatsArray addObject:formatName]; + // Split on commas and trim: a scanUpToString:@"," loop silently + // drops the final field (no trailing comma), and SSA Format + // lines separate names with ", " — the leading space made every + // name (" Start", " Text", …) fail its comparison in + // MP42SSALine, so no line ever got text/timestamps. + for (NSString *field in [formats componentsSeparatedByString:@","]) { + NSString *formatName = [field stringByTrimmingCharactersInSet: + [NSCharacterSet whitespaceAndNewlineCharacterSet]]; + if (formatName.length) { + [formatsArray addObject:formatName]; + } } } }