Skip to content

Instantly share code, notes, and snippets.

@sgl0v
Forked from steipete/CaseInsensitiveDictionary.m
Last active August 29, 2015 14:08
Show Gist options
  • Save sgl0v/84a80467f2a88cc11b08 to your computer and use it in GitHub Desktop.
Save sgl0v/84a80467f2a88cc11b08 to your computer and use it in GitHub Desktop.
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