Revision: 14927
          http://sourceforge.net/p/skim-app/code/14927
Author:   hofman
Date:     2025-02-28 15:32:57 +0000 (Fri, 28 Feb 2025)
Log Message:
-----------
Optimize getting number of characters, get other sub text values at most once

Modified Paths:
--------------
    trunk/PDFSelection_SKExtensions.m

Modified: trunk/PDFSelection_SKExtensions.m
===================================================================
--- trunk/PDFSelection_SKExtensions.m   2025-02-23 16:10:38 UTC (rev 14926)
+++ trunk/PDFSelection_SKExtensions.m   2025-02-28 15:32:57 UTC (rev 14927)
@@ -303,10 +303,18 @@
                 // now get the ranges, which can be any kind of specifier
                 NSInteger startIndex, endIndex, i, count = -2, *indices;
                 NSPointerArray *tmpRanges = [[NSPointerArray alloc] 
initForRangePointers];
+                NSArray *subTextStorages = nil;
                 
                 if ([specifier isKindOfClass:[NSPropertySpecifier class]]) {
                     // this should be the full range of characters, words, or 
paragraphs
-                    NSRange range = NSMakeRange(0, [[textStorage 
valueForKey:key] count]);
+                    NSRange range = NSMakeRange(0, 0);
+                    if ([key isEqualToString:@"character"]) {
+                        range.length = [textStorage length];
+                    } else {
+                        if (subTextStorages == nil)
+                            subTextStorages = [textStorage valueForKey:key];
+                        range.length = [subTextStorages count];
+                    }
                     if (range.length)
                         [tmpRanges addPointer:&range];
                 } else if ([specifier isKindOfClass:[NSRangeSpecifier class]]) 
{
@@ -327,7 +335,13 @@
                             indices = [endSpec 
indicesOfObjectsByEvaluatingWithContainer:textStorage count:&count];
                             endIndex = count > 0 ? indices[count - 1] : -1;
                         } else {
-                            endIndex = [[textStorage valueForKey:key] count] - 
1;
+                            if ([key isEqualToString:@"character"]) {
+                                endIndex = [textStorage length] - 1;
+                            } else {
+                                if (subTextStorages == nil)
+                                    subTextStorages = [textStorage 
valueForKey:key];
+                                endIndex = [subTextStorages count] - 1;
+                            }
                         }
                         if (startIndex >= 0 && endIndex >= 0) {
                             NSRange range = NSMakeRange(MIN(startIndex, 
endIndex), MAX(startIndex, endIndex) + 1 - MIN(startIndex, endIndex));
@@ -339,7 +353,13 @@
                     indices = [specifier 
indicesOfObjectsByEvaluatingWithContainer:textStorage count:&count];
                     NSRange range = NSMakeRange(0, 0);
                     if (count == -1) {
-                        range.length = [[textStorage valueForKey:key] count];
+                        if ([key isEqualToString:@"character"]) {
+                            range.length = [textStorage length];
+                        } else {
+                            if (subTextStorages == nil)
+                                subTextStorages = [textStorage 
valueForKey:key];
+                            range.length = [subTextStorages count];
+                        }
                     } else if (count > 0) {
                         for (i = 0; i < count; i++) {
                             NSUInteger idx = indices[i];
@@ -377,12 +397,13 @@
                     }
                 } else {
                     // translate from subtext ranges to character ranges
-                    NSArray *subTextStorages = [textStorage valueForKey:key];
+                    if (subTextStorages == nil)
+                        subTextStorages = [textStorage valueForKey:key];
                     if ([subTextStorages count]) {
                         NSString *string = nil;
                         NSArray *substrings = nil;
                         // The private subclass NSSubTextStorage has a -range 
method
-                        BOOL knowsRange = [[subTextStorages objectAtIndex:0] 
respondsToSelector:@selector(range)];
+                        BOOL knowsRange = [[subTextStorages firstObject] 
respondsToSelector:@selector(range)];
                         if (knowsRange == NO) {
                             // if we can't get the range directly, we try to 
search a substring
                             string = [textStorage string];

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Skim-app-commit mailing list
Skim-app-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to