From 2fa0e793e084800f2274fbb508dd1061f50359d1 Mon Sep 17 00:00:00 2001 From: iska Date: Fri, 31 Oct 2014 18:25:35 +0100 Subject: [PATCH] Replace dictionary with two arrays for Named Entity replacement Currently the entity name is binary-searched and its replacement-value is accessed via found index --- ...5AB53A08-CDD0-43FB-B47F-2EE38B3FE707.plist | 2 +- HTMLKit/HTMLTokenizer.m | 9 +++-- HTMLKit/HTMLTokenizerEntities.h | 6 ++-- HTMLKit/HTMLTokenizerEntities.m | 36 ++++++++----------- 4 files changed, 22 insertions(+), 31 deletions(-) diff --git a/HTMLKit.xcodeproj/xcshareddata/xcbaselines/625A14C219C7829400AD0C32.xcbaseline/5AB53A08-CDD0-43FB-B47F-2EE38B3FE707.plist b/HTMLKit.xcodeproj/xcshareddata/xcbaselines/625A14C219C7829400AD0C32.xcbaseline/5AB53A08-CDD0-43FB-B47F-2EE38B3FE707.plist index 02606c8..d33dc20 100644 --- a/HTMLKit.xcodeproj/xcshareddata/xcbaselines/625A14C219C7829400AD0C32.xcbaseline/5AB53A08-CDD0-43FB-B47F-2EE38B3FE707.plist +++ b/HTMLKit.xcodeproj/xcshareddata/xcbaselines/625A14C219C7829400AD0C32.xcbaseline/5AB53A08-CDD0-43FB-B47F-2EE38B3FE707.plist @@ -11,7 +11,7 @@ com.apple.XCTPerformanceMetric_WallClockTime baselineAverage - 15.94 + 15.47 baselineIntegrationDisplayName Local Baseline diff --git a/HTMLKit/HTMLTokenizer.m b/HTMLKit/HTMLTokenizer.m index c019a44..79b3ac9 100644 --- a/HTMLKit/HTMLTokenizer.m +++ b/HTMLKit/HTMLTokenizer.m @@ -316,11 +316,11 @@ [_inputStreamReader markCurrentLocation]; NSString *entityName = nil; + NSString *entityReplacement = nil; #warning Improve Named Entity Search UTF32Char inputCharacter = [_inputStreamReader consumeNextInputCharacter]; -// NSArray *names = [HTMLTokenizerEntities entityNames]; - NSArray *names = NAMES(); + NSArray *names = [HTMLTokenizerEntities entities]; NSMutableString *name = [NSMutableString stringWithString:StringFromUTF32Char(inputCharacter)]; NSUInteger searchIndex = 0; @@ -337,6 +337,7 @@ if ([[names objectAtIndex:searchIndex] isEqualToString:name]) { entityName = [name copy]; + entityReplacement = [HTMLTokenizerEntities replacementAtIndex:searchIndex]; } if ([name hasSuffix:@";"]) break; @@ -360,8 +361,6 @@ return nil; } - NSString *replacement = [HTMLTokenizerEntities replacementForNamedCharacterEntity:entityName]; - if (inAttribute && [entityName hasSuffix:@";"] == NO) { unichar nextCharacter = [name characterAtIndex:entityName.length]; if (nextCharacter == EQUALS_SIGN || isAlphanumeric(nextCharacter)) { @@ -377,7 +376,7 @@ [self emitParseError:@"Named entity without semicolon"]; } - return replacement; + return entityReplacement; } #pragma mark - States diff --git a/HTMLKit/HTMLTokenizerEntities.h b/HTMLKit/HTMLTokenizerEntities.h index 5c60c70..b4ff9ae 100644 --- a/HTMLKit/HTMLTokenizerEntities.h +++ b/HTMLKit/HTMLTokenizerEntities.h @@ -10,9 +10,7 @@ @interface HTMLTokenizerEntities : NSObject -+ (NSArray *)entityNames; -+ (NSString *)replacementForNamedCharacterEntity:(NSString *)entity; ++ (NSArray *)entities; ++ (NSString *)replacementAtIndex:(NSUInteger)index; @end - -extern NSArray * NAMES(); diff --git a/HTMLKit/HTMLTokenizerEntities.m b/HTMLKit/HTMLTokenizerEntities.m index f9e43dc..4cac6a5 100644 --- a/HTMLKit/HTMLTokenizerEntities.m +++ b/HTMLKit/HTMLTokenizerEntities.m @@ -2241,17 +2241,20 @@ NAMED_CHARACTER_REFERENCE( "zwj;", "\U0000200d" ) \ NAMED_CHARACTER_REFERENCE( "zwnj;", "\U0000200c" ) -static NSDictionary *_entities; +static NSArray *_entities; +static NSArray *_values; -static NSString * x[] = { +static NSString * NamedEntityNames[] = { #define NAMED_CHARACTER_REFERENCE( name, value ) @name, NAMED_CHARACTER_REFERENCES #undef NAMED_CHARACTER_REFERENCE }; -NSArray * NAMES() { - return [[NSArray alloc] initWithObjects:x count:2231]; -} +static NSString * NamedEntityValues[] = { +#define NAMED_CHARACTER_REFERENCE( name, value ) @value, + NAMED_CHARACTER_REFERENCES +#undef NAMED_CHARACTER_REFERENCE +}; @implementation HTMLTokenizerEntities @@ -2259,31 +2262,22 @@ NSArray * NAMES() { { if (self == [HTMLTokenizerEntities class]) { - NSArray * NamedReferencesNames = @[ -#define NAMED_CHARACTER_REFERENCE( name, value ) @(name), - NAMED_CHARACTER_REFERENCES -#undef NAMED_CHARACTER_REFERENCE - ]; + NSUInteger count = sizeof(NamedEntityNames) / sizeof(NamedEntityNames[0]); - NSArray * NamedReferencesValues = @[ -#define NAMED_CHARACTER_REFERENCE( name, value ) @(value), - NAMED_CHARACTER_REFERENCES -#undef NAMED_CHARACTER_REFERENCE - ]; + _entities = [[NSArray alloc] initWithObjects:NamedEntityNames count:count]; + _values = [[NSArray alloc] initWithObjects:NamedEntityValues count:count]; - _entities = [[NSDictionary alloc] initWithObjects:NamedReferencesValues forKeys:NamedReferencesNames]; } } -+ (NSArray *)entityNames ++ (NSArray *)entities { - return [_entities allKeys]; + return _entities; } -+ (NSString *)replacementForNamedCharacterEntity:(NSString *)entity ++ (NSString *)replacementAtIndex:(NSUInteger)index { - NSString *replacement = [_entities objectForKey:entity]; - return replacement; + return [_values objectAtIndex:index]; } @end