Revision: 3071
Updated Code
at June 1, 2007 09:24 by 0xced
Updated Code
/* * compareVersions(@"10.4", @"10.3") returns NSOrderedDescending (1) * compareVersions(@"10.5", @"10.5.0") returns NSOrderedSame (0) * compareVersions(@"10.4 Build 8L127", @"10.4 Build 8P135") returns NSOrderedAscending (-1) */ NSComparisonResult compareVersions(NSString* leftVersion, NSString* rightVersion) { int i; // Break version into fields (separated by '.') NSMutableArray *leftFields = [[NSMutableArray alloc] initWithArray:[leftVersion componentsSeparatedByString:@"."]]; NSMutableArray *rightFields = [[NSMutableArray alloc] initWithArray:[rightVersion componentsSeparatedByString:@"."]]; // Implict ".0" in case version doesn't have the same number of '.' if ([leftFields count] < [rightFields count]) { while ([leftFields count] != [rightFields count]) { [leftFields addObject:@"0"]; } } else if ([leftFields count] > [rightFields count]) { while ([leftFields count] != [rightFields count]) { [rightFields addObject:@"0"]; } } // Do a numeric comparison on each field for(i = 0; i < [leftFields count]; i++) { NSComparisonResult result = [[leftFields objectAtIndex:i] compare:[rightFields objectAtIndex:i] options:NSNumericSearch]; if (result != NSOrderedSame) { [leftFields release]; [rightFields release]; return result; } } [leftFields release]; [rightFields release]; return NSOrderedSame; }
Revision: 3070
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at June 1, 2007 09:22 by 0xced
Initial Code
/* * compareVersions(@"10.4", @"10.3") returns NSOrderedDescending (1) * compareVersions(@"10.5", @"10.5.0") returns NSOrderedSame (0) * compareVersions(@"10.4 Build 8L127", @"10.4 Build 8P135") returns NSOrderedAscending (-1) */ NSComparisonResult compareVersions(NSString* leftVersion, NSString* rightVersion) { int i; // Break version into fields (separated by '.') NSMutableArray *leftFields = [[NSMutableArray alloc] initWithArray:[leftVersion componentsSeparatedByString:@"."]]; NSMutableArray *rightFields = [[NSMutableArray alloc] initWithArray:[rightVersion componentsSeparatedByString:@"."]]; // Implict ".0" in case version doesn't have the same number of '.' if ([leftFields count] < [rightFields count]) { while ([leftFields count] != [rightFields count]) { [leftFields addObject:@"0"]; } } else if ([leftFields count] > [rightFields count]) { while ([leftFields count] != [rightFields count]) { [rightFields addObject:@"0"]; } } // Do a numeric comparison on each field for(i = 0; i < [leftFields count]; i++) { NSComparisonResult result = [[leftFields objectAtIndex:i] compare:[rightFields objectAtIndex:i] options:NSNumericSearch]; if (result != NSOrderedSame) { [leftFields release]; [rightFields release]; return result; } } [leftFields release]; [rightFields release]; return NSOrderedSame; }
Initial URL
Initial Description
Initial Title
Compare two version strings
Initial Tags
textmate
Initial Language
Objective C