Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add @autoreleasepool around NSScanner functions to release objects pa…

…ssed by reference with ARC (e.g. "scanCharactersFromSet:intoString:"). See https://developer.apple.com/library/mac/#releasenotes/ObjectiveC/RN-TransitioningToARC/_index.html
  • Loading branch information...
commit f266dfaa3d261f40d54b9f96151404365d3fbae0 1 parent 23809e2
@larsxschneider larsxschneider authored schwa committed
Showing with 163 additions and 158 deletions.
  1. +163 −158 Source/CSimpleHTMLParser.m
View
321 Source/CSimpleHTMLParser.m
@@ -81,180 +81,185 @@ - (NSString *)stringForEntity:(NSString *)inEntity
- (BOOL)parseString:(NSString *)inString error:(NSError **)outError
{
- NSMutableCharacterSet *theCharacterSet = [self.whitespaceCharacterSet mutableCopy];
- [theCharacterSet addCharactersInString:@"<&"];
- [theCharacterSet invert];
-
- NSScanner *theScanner = [[NSScanner alloc] initWithString:inString];
- theScanner.charactersToBeSkipped = NULL;
-
- NSMutableArray *theTagStack = [NSMutableArray array];
-
- __block NSMutableString *theString = [NSMutableString string];
-
- BOOL theLastCharacterWasWhitespace = NO;
-
- while ([theScanner isAtEnd] == NO)
+ @autoreleasepool
{
- NSString *theRun = NULL;
+ NSMutableCharacterSet *theCharacterSet = [self.whitespaceCharacterSet mutableCopy];
+ [theCharacterSet addCharactersInString:@"<&"];
+ [theCharacterSet invert];
- NSString *theTagName = NULL;
- NSDictionary *theAttributes = NULL;
+ NSScanner *theScanner = [[NSScanner alloc] initWithString:inString];
+ theScanner.charactersToBeSkipped = NULL;
- if ([theScanner scanCloseTag:&theTagName] == YES)
- {
- CSimpleHTMLTag *theTag = [[CSimpleHTMLTag alloc] init];
- theTag.name = theTagName;
-
- if (theString.length > 0)
- {
- theLastCharacterWasWhitespace = [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[theString characterAtIndex:theString.length - 1]];
- self.textHandler(theString, theTagStack);
- }
- theString = [NSMutableString string];
+ NSMutableArray *theTagStack = [NSMutableArray array];
+ NSMutableString *theString = [NSMutableString string];
- self.closeTagHandler(theTag, theTagStack);
+ BOOL theLastCharacterWasWhitespace = NO;
- NSUInteger theIndex = [theTagStack indexOfObjectWithOptions:NSEnumerationReverse passingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) { return([[obj name] isEqualToString:theTagName]); }];
- if (theIndex == NSNotFound)
- {
- if (outError)
+ while ([theScanner isAtEnd] == NO)
+ {
+
+ @autoreleasepool {
+
+ NSString *theRun = NULL;
+ NSString *theTagName = NULL;
+ NSDictionary *theAttributes = NULL;
+
+ if ([theScanner scanCloseTag:&theTagName] == YES)
{
- NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
- @"Stack underflow", NSLocalizedDescriptionKey,
- NULL];
- *outError = [NSError errorWithDomain:kSimpleHTMLParserErrorDomain code:kSimpleHTMLParserErrorCode_StackUnderflow userInfo:theUserInfo];
+ CSimpleHTMLTag *theTag = [[CSimpleHTMLTag alloc] init];
+ theTag.name = theTagName;
+
+ if (theString.length > 0)
+ {
+ theLastCharacterWasWhitespace = [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[theString characterAtIndex:theString.length - 1]];
+ self.textHandler(theString, theTagStack);
+ }
+ theString = [NSMutableString string];
+
+ self.closeTagHandler(theTag, theTagStack);
+
+ NSUInteger theIndex = [theTagStack indexOfObjectWithOptions:NSEnumerationReverse passingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) { return([[obj name] isEqualToString:theTagName]); }];
+ if (theIndex == NSNotFound)
+ {
+ if (outError)
+ {
+ NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
+ @"Stack underflow", NSLocalizedDescriptionKey,
+ NULL];
+ *outError = [NSError errorWithDomain:kSimpleHTMLParserErrorDomain code:kSimpleHTMLParserErrorCode_StackUnderflow userInfo:theUserInfo];
+ }
+ return(NO);
+ }
+
+ [theTagStack removeObjectsInRange:(NSRange){ .location = theIndex, .length = theTagStack.count - theIndex }];
}
- return(NO);
- }
-
- [theTagStack removeObjectsInRange:(NSRange){ .location = theIndex, .length = theTagStack.count - theIndex }];
- }
- else if ([theScanner scanOpenTag:&theTagName attributes:&theAttributes] == YES)
- {
- CSimpleHTMLTag *theTag = [[CSimpleHTMLTag alloc] init];
- theTag.name = theTagName;
- theTag.attributes = theAttributes;
-
- if (theString.length > 0)
- {
- theLastCharacterWasWhitespace = NO;
- self.textHandler(theString, theTagStack);
- theString = [NSMutableString string];
- }
-
- if ([theTagName isEqualToString:@"br"])
- {
- theLastCharacterWasWhitespace = YES;
- self.textHandler(@"\n", theTagStack);
- theString = [NSMutableString string];
- }
- else
- {
- self.openTagHandler(theTag, theTagStack);
-
- [theTagStack addObject:theTag];
- }
- }
- else if ([theScanner scanStandaloneTag:&theTagName attributes:&theAttributes] == YES)
- {
- CSimpleHTMLTag *theTag = [[CSimpleHTMLTag alloc] init];
- theTag.name = theTagName;
- theTag.attributes = theAttributes;
-
- if (theString.length > 0)
- {
- theLastCharacterWasWhitespace = NO;
- self.textHandler(theString, theTagStack);
- theString = [NSMutableString string];
- }
-
- if ([theTagName isEqualToString:@"br"])
- {
- theLastCharacterWasWhitespace = YES;
- self.textHandler(@"\n", theTagStack);
- theString = [NSMutableString string];
- }
- else
- {
- self.openTagHandler(theTag, theTagStack);
- self.closeTagHandler(theTag, theTagStack);
- }
- }
- else if ([theScanner scanString:@"&" intoString:NULL] == YES)
- {
- NSString *theEntity = NULL;
- if ([theScanner scanUpToString:@";" intoString:&theEntity] == NO)
- {
- if (outError)
+ else if ([theScanner scanOpenTag:&theTagName attributes:&theAttributes] == YES)
{
- NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
- @"& not followed by ;", NSLocalizedDescriptionKey,
- NULL];
- *outError = [NSError errorWithDomain:kSimpleHTMLParserErrorDomain code:kSimpleHTMLParserErrorCode_MalformedEntity userInfo:theUserInfo];
+ CSimpleHTMLTag *theTag = [[CSimpleHTMLTag alloc] init];
+ theTag.name = theTagName;
+ theTag.attributes = theAttributes;
+
+ if (theString.length > 0)
+ {
+ theLastCharacterWasWhitespace = NO;
+ self.textHandler(theString, theTagStack);
+ theString = [NSMutableString string];
+ }
+
+ if ([theTagName isEqualToString:@"br"])
+ {
+ theLastCharacterWasWhitespace = YES;
+ self.textHandler(@"\n", theTagStack);
+ theString = [NSMutableString string];
+ }
+ else
+ {
+ self.openTagHandler(theTag, theTagStack);
+
+ [theTagStack addObject:theTag];
+ }
}
- return(NO);
- }
- if ([theScanner scanString:@";" intoString:NULL] == NO)
- {
- if (outError)
+ else if ([theScanner scanStandaloneTag:&theTagName attributes:&theAttributes] == YES)
{
- NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
- @"& not followed by ;", NSLocalizedDescriptionKey,
- NULL];
- *outError = [NSError errorWithDomain:kSimpleHTMLParserErrorDomain code:kSimpleHTMLParserErrorCode_MalformedEntity userInfo:theUserInfo];
+ CSimpleHTMLTag *theTag = [[CSimpleHTMLTag alloc] init];
+ theTag.name = theTagName;
+ theTag.attributes = theAttributes;
+
+ if (theString.length > 0)
+ {
+ theLastCharacterWasWhitespace = NO;
+ self.textHandler(theString, theTagStack);
+ theString = [NSMutableString string];
+ }
+
+ if ([theTagName isEqualToString:@"br"])
+ {
+ theLastCharacterWasWhitespace = YES;
+ self.textHandler(@"\n", theTagStack);
+ theString = [NSMutableString string];
+ }
+ else
+ {
+ self.openTagHandler(theTag, theTagStack);
+ self.closeTagHandler(theTag, theTagStack);
+ }
+ }
+ else if ([theScanner scanString:@"&" intoString:NULL] == YES)
+ {
+ NSString *theEntity = NULL;
+ if ([theScanner scanUpToString:@";" intoString:&theEntity] == NO)
+ {
+ if (outError)
+ {
+ NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
+ @"& not followed by ;", NSLocalizedDescriptionKey,
+ NULL];
+ *outError = [NSError errorWithDomain:kSimpleHTMLParserErrorDomain code:kSimpleHTMLParserErrorCode_MalformedEntity userInfo:theUserInfo];
+ }
+ return(NO);
+ }
+ if ([theScanner scanString:@";" intoString:NULL] == NO)
+ {
+ if (outError)
+ {
+ NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
+ @"& not followed by ;", NSLocalizedDescriptionKey,
+ NULL];
+ *outError = [NSError errorWithDomain:kSimpleHTMLParserErrorDomain code:kSimpleHTMLParserErrorCode_MalformedEntity userInfo:theUserInfo];
+ }
+ return(NO);
+ }
+
+ if (theString.length > 0)
+ {
+ theLastCharacterWasWhitespace = [self.whitespaceCharacterSet characterIsMember:[theString characterAtIndex:theString.length - 1]];
+ self.textHandler(theString, theTagStack);
+ theString = [NSMutableString string];
+ }
+
+ NSString *theEntityString = [self stringForEntity:theEntity];
+ if (theEntityString.length > 0)
+ {
+ self.textHandler(theEntityString, theTagStack);
+ theLastCharacterWasWhitespace = NO;
+ }
+ }
+ else if ([theScanner scanCharactersFromSet:self.whitespaceCharacterSet intoString:NULL])
+ {
+ if (theLastCharacterWasWhitespace == NO)
+ {
+ [theString appendString:@" "];
+ theLastCharacterWasWhitespace = YES;
+ }
+ }
+ else if ([theScanner scanCharactersFromSet:theCharacterSet intoString:&theRun])
+ {
+ [theString appendString:theRun];
+ theLastCharacterWasWhitespace = [self.whitespaceCharacterSet characterIsMember:[theString characterAtIndex:theString.length - 1]];
+ }
+ else
+ {
+ if (outError)
+ {
+ NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
+ @"Unknown error occured!", NSLocalizedDescriptionKey,
+ [NSNumber numberWithInt:theScanner.scanLocation], @"character",
+ inString, @"markup",
+ NULL];
+ *outError = [NSError errorWithDomain:kSimpleHTMLParserErrorDomain code:kSimpleHTMLParserErrorCode_UnknownError userInfo:theUserInfo];
+ }
+ return(NO);
}
- return(NO);
- }
-
- if (theString.length > 0)
- {
- theLastCharacterWasWhitespace = [self.whitespaceCharacterSet characterIsMember:[theString characterAtIndex:theString.length - 1]];
- self.textHandler(theString, theTagStack);
- theString = [NSMutableString string];
- }
-
- NSString *theEntityString = [self stringForEntity:theEntity];
- if (theEntityString.length > 0)
- {
- self.textHandler(theEntityString, theTagStack);
- theLastCharacterWasWhitespace = NO;
- }
- }
- else if ([theScanner scanCharactersFromSet:self.whitespaceCharacterSet intoString:NULL])
- {
- if (theLastCharacterWasWhitespace == NO)
- {
- [theString appendString:@" "];
- theLastCharacterWasWhitespace = YES;
}
}
- else if ([theScanner scanCharactersFromSet:theCharacterSet intoString:&theRun])
+
+ if (theString.length > 0)
{
- [theString appendString:theRun];
theLastCharacterWasWhitespace = [self.whitespaceCharacterSet characterIsMember:[theString characterAtIndex:theString.length - 1]];
+ self.textHandler(theString, theTagStack);
}
- else
- {
- if (outError)
- {
- NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
- @"Unknown error occured!", NSLocalizedDescriptionKey,
- [NSNumber numberWithInt:theScanner.scanLocation], @"character",
- inString, @"markup",
- NULL];
- *outError = [NSError errorWithDomain:kSimpleHTMLParserErrorDomain code:kSimpleHTMLParserErrorCode_UnknownError userInfo:theUserInfo];
- }
- return(NO);
- }
- }
-
- if (theString.length > 0)
- {
- theLastCharacterWasWhitespace = [self.whitespaceCharacterSet characterIsMember:[theString characterAtIndex:theString.length - 1]];
- self.textHandler(theString, theTagStack);
- }
-
+ }
+
return(YES);
}
Please sign in to comment.
Something went wrong with that request. Please try again.