Revision: 14943 http://sourceforge.net/p/skim-app/code/14943 Author: hofman Date: 2025-03-05 10:41:09 +0000 (Wed, 05 Mar 2025) Log Message: ----------- reintroduce macro to lazily get subTextStorages when needed
Modified Paths: -------------- trunk/PDFSelection_SKExtensions.m Modified: trunk/PDFSelection_SKExtensions.m =================================================================== --- trunk/PDFSelection_SKExtensions.m 2025-03-05 10:10:55 UTC (rev 14942) +++ trunk/PDFSelection_SKExtensions.m 2025-03-05 10:41:09 UTC (rev 14943) @@ -268,6 +268,9 @@ #define RICH_TEXT_CLASSNAME @"rich text" #define CHARACTERS_KEY @"characters" +// macro to create subTextStorages lazily only when needed +#define subTextStorages_count ([key isEqualToString:CHARACTERS_KEY] ? [textStorage length] : [(subTextStorages ?: (subTextStorages = [textStorage valueForKey:key])) count]) + static NSArray *characterRangesAndContainersForSpecifier(NSScriptObjectSpecifier *specifier, BOOL continuous) { if ([specifier isKindOfClass:[NSScriptObjectSpecifier class]] == NO) return nil; @@ -307,13 +310,7 @@ if ([specifier isKindOfClass:[NSPropertySpecifier class]]) { // this should be the full range of characters, words, or paragraphs - NSRange range = NSMakeRange(0, 0); - if ([key isEqualToString:CHARACTERS_KEY]) { - range.length = [textStorage length]; - } else { - subTextStorages = [textStorage valueForKey:key]; - range.length = [subTextStorages count]; - } + NSRange range = NSMakeRange(0, subTextStorages_count); if (range.length) [tmpRanges addPointer:&range]; } else if ([specifier isKindOfClass:[NSRangeSpecifier class]]) { @@ -324,14 +321,8 @@ if (startSpec || endSpec) { if ([startSpec isKindOfClass:[NSIndexSpecifier class]]) { startIndex = [(NSIndexSpecifier *)startSpec index]; - if (startIndex < 0) { - if ([key isEqualToString:CHARACTERS_KEY]) { - startIndex += [textStorage length]; - } else { - subTextStorages = [textStorage valueForKey:key]; - startIndex += [subTextStorages count]; - } - } + if (startIndex < 0) + startIndex += subTextStorages_count; } else if (startSpec) { count = -2; indices = [startSpec indicesOfObjectsByEvaluatingWithContainer:textStorage count:&count]; @@ -341,23 +332,14 @@ } if ([endSpec isKindOfClass:[NSIndexSpecifier class]]) { endIndex = [(NSIndexSpecifier *)endSpec index]; - if (endIndex < 0) { - if ([key isEqualToString:CHARACTERS_KEY]) { - endIndex += [textStorage length]; - } else { - subTextStorages = [textStorage valueForKey:key]; - endIndex += [subTextStorages count]; - } - } + if (endIndex < 0) + endIndex += subTextStorages_count; } else if (endSpec) { count = -2; indices = [endSpec indicesOfObjectsByEvaluatingWithContainer:textStorage count:&count]; endIndex = count > 0 ? indices[count - 1] : -1; - } else if ([key isEqualToString:CHARACTERS_KEY]) { - endIndex = [textStorage length] - 1; } else { - subTextStorages = [textStorage valueForKey:key]; - endIndex = [subTextStorages count] - 1; + endIndex = subTextStorages_count - 1; } if (startIndex >= 0 && endIndex >= 0) { NSRange range = NSMakeRange(MIN(startIndex, endIndex), MAX(startIndex, endIndex) + 1 - MIN(startIndex, endIndex)); @@ -366,27 +348,16 @@ } } else if ([specifier isKindOfClass:[NSIndexSpecifier class]]) { NSInteger idx = [(NSIndexSpecifier *)specifier index]; - if (idx < 0) { - if ([key isEqualToString:CHARACTERS_KEY]) { - idx += [textStorage length]; - } else { - subTextStorages = [textStorage valueForKey:key]; - idx += [subTextStorages count]; - } - } + if (idx < 0) + idx += subTextStorages_count; NSRange range = NSMakeRange(idx, 1); [tmpRanges addPointer:&range]; } else { - // this handles other objectSpecifiers (index, middel, random, relative, whose). It can contain several ranges, e.g. for aan NSWhoseSpecifier + // this handles other objectSpecifiers (middel, random, relative, whose). It can contain several ranges, e.g. for aan NSWhoseSpecifier indices = [specifier indicesOfObjectsByEvaluatingWithContainer:textStorage count:&count]; NSRange range = NSMakeRange(0, 0); if (count == -1) { - if ([key isEqualToString:CHARACTERS_KEY]) { - range.length = [textStorage length]; - } else { - subTextStorages = [textStorage valueForKey:key]; - range.length = [subTextStorages count]; - } + range.length = subTextStorages_count; } else if (count > 0) { for (i = 0; i < count; i++) { NSUInteger idx = indices[i]; @@ -422,57 +393,54 @@ } } } - } else { + } else if (subTextStorages_count) { // translate from subtext ranges to character ranges - 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 firstObject] respondsToSelector:@selector(range)]; - if (knowsRange == NO) { - // if we can't get the range directly, we try to search a substring - string = [textStorage string]; - substrings = [subTextStorages valueForKey:@"string"]; - } - for (i = 0; i < count; i++) { - NSRange range = [tmpRanges rangeAtIndex:i]; - startIndex = MIN(range.location, [subTextStorages count] - 1); - endIndex = MIN(NSMaxRange(range) - 1, [subTextStorages count] - 1); - if (endIndex == startIndex) endIndex = -1; - if (continuous) { + // subTextStorages should be assigned at this point + NSString *string = nil; + NSArray *substrings = nil; + // The private subclass NSSubTextStorage has a -range method + 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]; + substrings = [subTextStorages valueForKey:@"string"]; + } + for (i = 0; i < count; i++) { + NSRange range = [tmpRanges rangeAtIndex:i]; + startIndex = MIN(range.location, [subTextStorages count] - 1); + endIndex = MIN(NSMaxRange(range) - 1, [subTextStorages count] - 1); + if (endIndex == startIndex) endIndex = -1; + if (continuous) { + if (knowsRange) + range = [[subTextStorages objectAtIndex:startIndex] range]; + else + range = rangeOfSubstringOfStringAtIndex(string, substrings, startIndex); + if (range.location == NSNotFound) + continue; + startIndex = range.location; + if (endIndex >= 0) { if (knowsRange) - range = [[subTextStorages objectAtIndex:startIndex] range]; + range = [[subTextStorages objectAtIndex:endIndex] range]; else - range = rangeOfSubstringOfStringAtIndex(string, substrings, startIndex); + range = rangeOfSubstringOfStringAtIndex(string, substrings, endIndex); if (range.location == NSNotFound) continue; - startIndex = range.location; - if (endIndex >= 0) { - if (knowsRange) - range = [[subTextStorages objectAtIndex:endIndex] range]; - else - range = rangeOfSubstringOfStringAtIndex(string, substrings, endIndex); - if (range.location == NSNotFound) - continue; - } - endIndex = NSMaxRange(range) - 1; - range = NSMakeRange(textRange.location + startIndex, endIndex + 1 - startIndex); + } + endIndex = NSMaxRange(range) - 1; + range = NSMakeRange(textRange.location + startIndex, endIndex + 1 - startIndex); + [ranges addPointer:&range]; + } else { + if (endIndex == -1) endIndex = startIndex; + NSInteger j; + for (j = startIndex; j <= endIndex; j++) { + if (knowsRange) + range = [[subTextStorages objectAtIndex:j] range]; + else + range = rangeOfSubstringOfStringAtIndex(string, substrings, j); + if (range.location == NSNotFound) + continue; + range.location += textRange.location; [ranges addPointer:&range]; - } else { - if (endIndex == -1) endIndex = startIndex; - NSInteger j; - for (j = startIndex; j <= endIndex; j++) { - if (knowsRange) - range = [[subTextStorages objectAtIndex:j] range]; - else - range = rangeOfSubstringOfStringAtIndex(string, substrings, j); - if (range.location == NSNotFound) - continue; - range.location += textRange.location; - [ranges addPointer:&range]; - } } } } 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