-
-
Save ytlvy/cd02ed4bb9b752bbbd18c2cf855953d7 to your computer and use it in GitHub Desktop.
Implemented as a category on NSDictionary.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static Boolean PSPDFCaseInsensitiveEqualCallback(const void *a, const void *b) { | |
id objA = (__bridge id)a, objB = (__bridge id)b; | |
Boolean ret = FALSE; | |
if ([objA isKindOfClass:NSString.class] && [objB isKindOfClass:NSString.class]) { | |
ret = ([objA compare:objB options:NSCaseInsensitiveSearch|NSLiteralSearch] == NSOrderedSame); | |
}else { | |
ret = [objA isEqual:objB]; | |
} | |
return ret; | |
} | |
static CFHashCode PSPDFCaseInsensitiveHashCallback(const void *value) { | |
id idValue = (__bridge id)value; | |
CFHashCode ret = 0; | |
if ([idValue isKindOfClass:NSString.class]) { | |
ret = [[idValue lowercaseString] hash]; | |
}else { | |
ret = [(NSObject *)idValue hash]; | |
} | |
return ret; | |
} | |
- (NSDictionary *)pspdf_caseInsensitiveDictionary { | |
CFDictionaryKeyCallBacks keyCallbacks = kCFCopyStringDictionaryKeyCallBacks; | |
keyCallbacks.equal = PSPDFCaseInsensitiveEqualCallback; | |
keyCallbacks.hash = PSPDFCaseInsensitiveHashCallback; | |
void *keys = NULL, *values = NULL; | |
CFIndex count = CFDictionaryGetCount((CFDictionaryRef)self); | |
if (count) { | |
keys = malloc(count * sizeof(void *)); | |
values = malloc(count * sizeof(void *)); | |
if (!keys || !values) { | |
free(keys), free(values); | |
return self; | |
} | |
CFDictionaryGetKeysAndValues((CFDictionaryRef)self, keys, values); | |
} | |
CFDictionaryRef caseInsensitiveDict = CFDictionaryCreate(kCFAllocatorDefault, keys, values, count, &keyCallbacks, &kCFTypeDictionaryValueCallBacks); | |
free(keys); | |
free(values); | |
return CFBridgingRelease(caseInsensitiveDict); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment