Revision: 13875
http://sourceforge.net/p/skim-app/code/13875
Author: hofman
Date: 2023-12-10 22:41:28 +0000 (Sun, 10 Dec 2023)
Log Message:
-----------
convenience class factory method for case insensitive string pointer functions
Modified Paths:
--------------
trunk/NSPointerFunctions_SKExtensions.h
trunk/NSPointerFunctions_SKExtensions.m
trunk/SKPDFSynchronizer.m
Modified: trunk/NSPointerFunctions_SKExtensions.h
===================================================================
--- trunk/NSPointerFunctions_SKExtensions.h 2023-12-10 17:36:12 UTC (rev
13874)
+++ trunk/NSPointerFunctions_SKExtensions.h 2023-12-10 22:41:28 UTC (rev
13875)
@@ -45,7 +45,7 @@
@property (class, nonatomic, readonly) NSPointerFunctions
*rectPointerFunctions;
@property (class, nonatomic, readonly) NSPointerFunctions
*rangePointerFunctions;
@property (class, nonatomic, readonly) NSPointerFunctions
*strongPointerFunctions;
-@property (class, nonatomic, readonly) NSPointerFunctions
*weakPointerFunctions;
@property (class, nonatomic, readonly) NSPointerFunctions
*integerPointerFunctions;
+@property (class, nonatomic, readonly) NSPointerFunctions
*caseInsensitiveStringPointerFunctions;
@end
Modified: trunk/NSPointerFunctions_SKExtensions.m
===================================================================
--- trunk/NSPointerFunctions_SKExtensions.m 2023-12-10 17:36:12 UTC (rev
13874)
+++ trunk/NSPointerFunctions_SKExtensions.m 2023-12-10 22:41:28 UTC (rev
13875)
@@ -50,6 +50,36 @@
static NSString *rangeDescriptionFunction(const void *item) { return [NSString
stringWithFormat:@"(%lu, %lu)", (unsigned long)(((NSRange *)item)->location),
(unsigned long)(((NSRange *)item)->length)]; }
+static BOOL caseInsensitiveStringEqual(const void *item1, const void *item2,
NSUInteger (*size)(const void *item)) {
+ return CFStringCompare(item1, item2, kCFCompareCaseInsensitive |
kCFCompareNonliteral) == kCFCompareEqualTo;
+}
+
+#define STACK_BUFFER_SIZE 256
+
+static NSUInteger caseInsensitiveStringHash(const void *item, NSUInteger
(*size)(const void *item)) {
+ if(item == NULL) return 0;
+
+ NSUInteger hash = 0;
+ CFAllocatorRef allocator = CFGetAllocator(item);
+ CFIndex len = CFStringGetLength(item);
+
+ // use a generous length, in case the lowercase changes the number of
characters
+ UniChar *buffer, stackBuffer[STACK_BUFFER_SIZE];
+ if (len + 10 >= STACK_BUFFER_SIZE)
+ buffer = (UniChar *)CFAllocatorAllocate(allocator, (len + 10) *
sizeof(UniChar), 0);
+ else
+ buffer = stackBuffer;
+ CFStringGetCharacters(item, CFRangeMake(0, len), buffer);
+
+ // If we create the string with external characters,
CFStringGetCharactersPtr is guaranteed to succeed; since we're going to call
CFStringGetCharacters anyway in fastHash if CFStringGetCharactsPtr fails, let's
do it now when we lowercase the string
+ CFMutableStringRef mutableString =
CFStringCreateMutableWithExternalCharactersNoCopy(allocator, buffer, len, len +
10, (buffer != stackBuffer ? allocator : kCFAllocatorNull));
+ CFStringLowercase(mutableString, NULL);
+ hash = [(id)mutableString hash];
+ // if we used the allocator, this should free the buffer for us
+ CFRelease(mutableString);
+ return hash;
+}
+
@implementation NSPointerFunctions (SKExtensions)
+ (NSPointerFunctions *)structPointerFunctionsWithSizeFunction:(NSUInteger
(*)(const void *))sizeFunction descriptionFunction:(NSString *(*)(const void
*))descriptionFunction {
@@ -71,16 +101,19 @@
return [self structPointerFunctionsWithSizeFunction:rangeSizeFunction
descriptionFunction:rangeDescriptionFunction];
}
++ (NSPointerFunctions *)integerPointerFunctions {
+ return [self pointerFunctionsWithOptions:NSPointerFunctionsOpaqueMemory |
NSPointerFunctionsIntegerPersonality];
+}
+
+ (NSPointerFunctions *)strongPointerFunctions {
return [self pointerFunctionsWithOptions:NSPointerFunctionsStrongMemory |
NSPointerFunctionsObjectPersonality];
}
-+ (NSPointerFunctions *)weakPointerFunctions {
- return [self pointerFunctionsWithOptions:NSPointerFunctionsWeakMemory |
NSPointerFunctionsObjectPersonality];
++ (NSPointerFunctions *)caseInsensitiveStringPointerFunctions {
+ NSPointerFunctions *pointerFunctions = [self
pointerFunctionsWithOptions:NSPointerFunctionsWeakMemory |
NSPointerFunctionsObjectPersonality];;
+ [pointerFunctions setIsEqualFunction:&caseInsensitiveStringEqual];
+ [pointerFunctions setHashFunction:&caseInsensitiveStringHash];
+ return pointerFunctions;
}
-+ (NSPointerFunctions *)integerPointerFunctions {
- return [self pointerFunctionsWithOptions:NSPointerFunctionsOpaqueMemory |
NSPointerFunctionsIntegerPersonality];
-}
-
@end
Modified: trunk/SKPDFSynchronizer.m
===================================================================
--- trunk/SKPDFSynchronizer.m 2023-12-10 17:36:12 UTC (rev 13874)
+++ trunk/SKPDFSynchronizer.m 2023-12-10 22:41:28 UTC (rev 13875)
@@ -52,9 +52,6 @@
#define SKPDFSynchronizerPdfsyncExtension @"pdfsync"
static NSArray *SKPDFSynchronizerTexExtensions = nil;
-static BOOL caseInsensitiveStringEqual(const void *item1, const void *item2,
NSUInteger (*size)(const void *item));
-static NSUInteger caseInsensitiveStringHash(const void *item, NSUInteger
(*size)(const void *item));
-
#pragma mark -
@implementation SKPDFSynchronizer
@@ -222,11 +219,7 @@
if (lines) {
[lines removeAllObjects];
} else {
- NSPointerFunctions *keyPointerFunctions = [NSPointerFunctions
strongPointerFunctions];
- [keyPointerFunctions setIsEqualFunction:&caseInsensitiveStringEqual];
- [keyPointerFunctions setHashFunction:&caseInsensitiveStringHash];
- NSPointerFunctions *valuePointerFunctions = [NSPointerFunctions
strongPointerFunctions];
- lines = [[NSMapTable alloc]
initWithKeyPointerFunctions:keyPointerFunctions
valuePointerFunctions:valuePointerFunctions capacity:0];
+ lines = [[NSMapTable alloc]
initWithKeyPointerFunctions:[NSPointerFunctions
caseInsensitiveStringPointerFunctions]
valuePointerFunctions:[NSPointerFunctions strongPointerFunctions] capacity:0];
}
[self setSyncFileName:theFileName];
@@ -474,9 +467,7 @@
if (filenames) {
[filenames removeAllObjects];
} else {
- NSPointerFunctions *keyPointerFunctions = [NSPointerFunctions
strongPointerFunctions];
- [keyPointerFunctions
setIsEqualFunction:&caseInsensitiveStringEqual];
- [keyPointerFunctions setHashFunction:&caseInsensitiveStringHash];
+ NSPointerFunctions *keyPointerFunctions = [NSPointerFunctions
caseInsensitiveStringPointerFunctions];
NSPointerFunctions *valuePointerFunctions = [NSPointerFunctions
pointerFunctionsWithOptions:NSPointerFunctionsMallocMemory |
NSPointerFunctionsCStringPersonality | NSPointerFunctionsCopyIn];
filenames = [[NSMapTable alloc]
initWithKeyPointerFunctions:keyPointerFunctions
valuePointerFunctions:valuePointerFunctions capacity:0];
}
@@ -631,35 +622,3 @@
}
@end
-
-#pragma mark -
-
-#define STACK_BUFFER_SIZE 256
-
-static BOOL caseInsensitiveStringEqual(const void *item1, const void *item2,
NSUInteger (*size)(const void *item)) {
- return CFStringCompare(item1, item2, kCFCompareCaseInsensitive |
kCFCompareNonliteral) == kCFCompareEqualTo;
-}
-
-static NSUInteger caseInsensitiveStringHash(const void *item, NSUInteger
(*size)(const void *item)) {
- if(item == NULL) return 0;
-
- NSUInteger hash = 0;
- CFAllocatorRef allocator = CFGetAllocator(item);
- CFIndex len = CFStringGetLength(item);
-
- // use a generous length, in case the lowercase changes the number of
characters
- UniChar *buffer, stackBuffer[STACK_BUFFER_SIZE];
- if (len + 10 >= STACK_BUFFER_SIZE)
- buffer = (UniChar *)CFAllocatorAllocate(allocator, (len + 10) *
sizeof(UniChar), 0);
- else
- buffer = stackBuffer;
- CFStringGetCharacters(item, CFRangeMake(0, len), buffer);
-
- // If we create the string with external characters,
CFStringGetCharactersPtr is guaranteed to succeed; since we're going to call
CFStringGetCharacters anyway in fastHash if CFStringGetCharactsPtr fails, let's
do it now when we lowercase the string
- CFMutableStringRef mutableString =
CFStringCreateMutableWithExternalCharactersNoCopy(allocator, buffer, len, len +
10, (buffer != stackBuffer ? allocator : kCFAllocatorNull));
- CFStringLowercase(mutableString, NULL);
- hash = [(id)mutableString hash];
- // if we used the allocator, this should free the buffer for us
- CFRelease(mutableString);
- return hash;
-}
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit