Skip to content

Instantly share code, notes, and snippets.

@steipete
Created December 1, 2013 19:12
Show Gist options
  • Save steipete/7739473 to your computer and use it in GitHub Desktop.
Save steipete/7739473 to your computer and use it in GitHub Desktop.
Implemented as a category on NSDictionary.
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