Revision: 28966 http://sourceforge.net/p/bibdesk/svn/28966 Author: hofman Date: 2024-10-17 23:25:19 +0000 (Thu, 17 Oct 2024) Log Message: ----------- more efficient way to get the common root of multiple file paths by first getting it for the path components
Modified Paths: -------------- trunk/bibdesk/BibDocument.m trunk/bibdesk/NSString_BDSKExtensions.h trunk/bibdesk/NSString_BDSKExtensions.m Modified: trunk/bibdesk/BibDocument.m =================================================================== --- trunk/bibdesk/BibDocument.m 2024-10-17 23:03:14 UTC (rev 28965) +++ trunk/bibdesk/BibDocument.m 2024-10-17 23:25:19 UTC (rev 28966) @@ -1254,8 +1254,7 @@ } } - for (NSString *filePath in parents) - commonParent = commonParent ? [filePath commonRootPathOfFile:commonParent] : filePath; + commonParent = [NSString commonRootPathOfFiles:[parents allObjects]]; if ([localFiles count] > 1 && [commonParent isEqualToString:NSHomeDirectory()] == NO && [commonParent isEqualToString:[self basePath]] == NO && [[commonParent stringByDeletingLastPathComponent] isEqualToString:@"/"] == NO && [parents containsObject:commonParent]) commonParent = [commonParent stringByDeletingLastPathComponent]; Modified: trunk/bibdesk/NSString_BDSKExtensions.h =================================================================== --- trunk/bibdesk/NSString_BDSKExtensions.h 2024-10-17 23:03:14 UTC (rev 28965) +++ trunk/bibdesk/NSString_BDSKExtensions.h 2024-10-17 23:25:19 UTC (rev 28966) @@ -484,7 +484,7 @@ @property (nonatomic, readonly) NSString *stringByRemovingAliens; @property (class, nonatomic, readonly) NSString *pathSeparator; -- (NSString *)commonRootPathOfFile:(NSString *)filename; ++ (NSString *)commonRootPathOfFiles:(NSArray *)filenames; - (NSString *)relativePathFromPath:(NSString *)basePath; @property (nonatomic, readonly) NSString *stringByNormalizingPath; Modified: trunk/bibdesk/NSString_BDSKExtensions.m =================================================================== --- trunk/bibdesk/NSString_BDSKExtensions.m 2024-10-17 23:03:14 UTC (rev 28965) +++ trunk/bibdesk/NSString_BDSKExtensions.m 2024-10-17 23:25:19 UTC (rev 28966) @@ -1522,14 +1522,13 @@ return [NSOpenStepRootDirectory() substringToIndex:1]; } -- (NSArray *)commonRootPathComponentsOfFilename:(NSString *)filename components:(NSArray **)components otherComponents:(NSArray **)otherComponents { +- (NSArray *)commonRootPathComponentsOfComponents:(NSArray *)fileComponents components:(NSArray **)components otherComponents:(NSArray **)otherComponents { NSArray *array = [self pathComponents]; - NSArray *otherArray = [[filename stringByStandardizingPath] pathComponents]; - NSUInteger i, minLength = MIN([array count], [otherArray count]); + NSUInteger i, minLength = MIN([array count], [fileComponents count]); NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:minLength]; for (i = 0; i < minLength; i++) { - if ([[array objectAtIndex:i] isEqualToString:[otherArray objectAtIndex:i]]) + if ([[array objectAtIndex:i] isEqualToString:[fileComponents objectAtIndex:i]]) [resultArray addObject:[array objectAtIndex:i]]; else break; @@ -1541,21 +1540,31 @@ if (components) *components = [array subarrayWithRange:NSMakeRange(i, [array count] - i)]; if (otherComponents) - *otherComponents = [otherArray subarrayWithRange:NSMakeRange(i, [otherArray count] - i)]; + *otherComponents = [fileComponents subarrayWithRange:NSMakeRange(i, [fileComponents count] - i)]; return resultArray; } -- (NSString *)commonRootPathOfFile:(NSString *)filename { - NSArray *components = [self commonRootPathComponentsOfFilename:filename components:NULL otherComponents:NULL]; - return components ? [NSString pathWithComponents:components] : nil; ++ (NSString *)commonRootPathOfFiles:(NSArray *)filenames { + if ([filenames count] < 2) + return [filenames firstObject]; + + NSArray *components = nil; + for (NSString *filename in filenames) { + if (components) + components = [filename commonRootPathComponentsOfComponents:components components:NULL otherComponents:NULL]; + else + components = [filename pathComponents]; + } + return [NSString pathWithComponents:components]; } - (NSString *)relativePathFromPath:(NSString *)basePath { NSArray *commonRoot, *myUniquePart, *baseUniquePart; NSInteger numberOfStepsUp, i; + NSArray *baseComponents = [[basePath stringByStandardizingPath] pathComponents]; - commonRoot = [[self stringByStandardizingPath] commonRootPathComponentsOfFilename:basePath components:&myUniquePart otherComponents:&baseUniquePart]; + commonRoot = [[self stringByStandardizingPath] commonRootPathComponentsOfComponents:baseComponents components:&myUniquePart otherComponents:&baseUniquePart]; if (commonRoot == nil || [commonRoot count] == 0) return self; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. _______________________________________________ Bibdesk-commit mailing list Bibdesk-commit@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/bibdesk-commit