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
This commit is contained in:
iska
2014-10-31 18:25:54 +01:00
parent c04d4a35f3
commit 2fa0e793e0
4 changed files with 22 additions and 31 deletions
@@ -11,7 +11,7 @@
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
<dict>
<key>baselineAverage</key>
<real>15.94</real>
<real>15.47</real>
<key>baselineIntegrationDisplayName</key>
<string>Local Baseline</string>
</dict>
+4 -5
View File
@@ -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
+2 -4
View File
@@ -10,9 +10,7 @@
@interface HTMLTokenizerEntities : NSObject
+ (NSArray *)entityNames;
+ (NSString *)replacementForNamedCharacterEntity:(NSString *)entity;
+ (NSArray *)entities;
+ (NSString *)replacementAtIndex:(NSUInteger)index;
@end
extern NSArray * NAMES();
+15 -21
View File
@@ -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