Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 170 lines (151 sloc) 5.517 kB
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
1 //
63011aa Beware of falling bricks! Huge refactoring commit #1: cleansing Spark…
andym authored
2 // SUStandardVersionComparator.m
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
3 // Sparkle
4 //
63011aa Beware of falling bricks! Huge refactoring commit #1: cleansing Spark…
andym authored
5 // Created by Andy Matuschak on 12/21/07.
49e3d81 Major refactoring, yay.
andym authored
6 // Copyright 2007 Andy Matuschak. All rights reserved.
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
7 //
8
43a0a7d @uliwitness Merge of changes from SVN repository:
uliwitness authored
9 #import "SUUpdater.h"
10
11 #import "SUAppcast.h"
12 #import "SUAppcastItem.h"
13 #import "SUVersionComparisonProtocol.h"
63011aa Beware of falling bricks! Huge refactoring commit #1: cleansing Spark…
andym authored
14 #import "SUStandardVersionComparator.h"
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
15
63011aa Beware of falling bricks! Huge refactoring commit #1: cleansing Spark…
andym authored
16 @implementation SUStandardVersionComparator
5e05339 #76 Support for Plug-ins
catlan authored
17
63011aa Beware of falling bricks! Huge refactoring commit #1: cleansing Spark…
andym authored
18 + (SUStandardVersionComparator *)defaultComparator
5e05339 #76 Support for Plug-ins
catlan authored
19 {
63011aa Beware of falling bricks! Huge refactoring commit #1: cleansing Spark…
andym authored
20 static SUStandardVersionComparator *defaultComparator = nil;
21 if (defaultComparator == nil)
22 defaultComparator = [[SUStandardVersionComparator alloc] init];
23 return defaultComparator;
5e05339 #76 Support for Plug-ins
catlan authored
24 }
25
63011aa Beware of falling bricks! Huge refactoring commit #1: cleansing Spark…
andym authored
26 typedef enum {
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
27 kNumberType,
28 kStringType,
43a0a7d @uliwitness Merge of changes from SVN repository:
uliwitness authored
29 kSeparatorType,
63011aa Beware of falling bricks! Huge refactoring commit #1: cleansing Spark…
andym authored
30 } SUCharacterType;
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
31
63011aa Beware of falling bricks! Huge refactoring commit #1: cleansing Spark…
andym authored
32 - (SUCharacterType)typeOfCharacter:(NSString *)character
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
33 {
34 if ([character isEqualToString:@"."]) {
43a0a7d @uliwitness Merge of changes from SVN repository:
uliwitness authored
35 return kSeparatorType;
d5495b5 Fixing #30: making version comparison smarter about determining what …
andym authored
36 } else if ([[NSCharacterSet decimalDigitCharacterSet] characterIsMember:[character characterAtIndex:0]]) {
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
37 return kNumberType;
43a0a7d @uliwitness Merge of changes from SVN repository:
uliwitness authored
38 } else if ([[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember:[character characterAtIndex:0]]) {
39 return kSeparatorType;
40 } else if ([[NSCharacterSet punctuationCharacterSet] characterIsMember:[character characterAtIndex:0]]) {
41 return kSeparatorType;
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
42 } else {
43 return kStringType;
44 }
45 }
46
63011aa Beware of falling bricks! Huge refactoring commit #1: cleansing Spark…
andym authored
47 - (NSArray *)splitVersionString:(NSString *)version
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
48 {
49 NSString *character;
50 NSMutableString *s;
d7774c0 performed a code review, specifically:
Sean McBride authored
51 NSUInteger i, n;
52 SUCharacterType oldType, newType;
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
53 NSMutableArray *parts = [NSMutableArray array];
54 if ([version length] == 0) {
55 // Nothing to do here
56 return parts;
57 }
58 s = [[[version substringToIndex:1] mutableCopy] autorelease];
63011aa Beware of falling bricks! Huge refactoring commit #1: cleansing Spark…
andym authored
59 oldType = [self typeOfCharacter:s];
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
60 n = [version length] - 1;
61 for (i = 1; i <= n; ++i) {
62 character = [version substringWithRange:NSMakeRange(i, 1)];
63011aa Beware of falling bricks! Huge refactoring commit #1: cleansing Spark…
andym authored
63 newType = [self typeOfCharacter:character];
c0ca224 @uliwitness Revert Patrick's version number change.
uliwitness authored
64 if (oldType != newType || oldType == kSeparatorType) {
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
65 // We've reached a new segment
b21b308 all but a few places use the typical [[[foo alloc] init] autorelease]…
Sean McBride authored
66 NSString *aPart = [[[NSString alloc] initWithString:s] autorelease];
c0ca224 @uliwitness Revert Patrick's version number change.
uliwitness authored
67 [parts addObject:aPart];
68 [s setString:character];
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
69 } else {
70 // Add character to string and continue
71 [s appendString:character];
72 }
73 oldType = newType;
74 }
75
76 // Add the last part onto the array
77 [parts addObject:[NSString stringWithString:s]];
78 return parts;
79 }
80
63011aa Beware of falling bricks! Huge refactoring commit #1: cleansing Spark…
andym authored
81 - (NSComparisonResult)compareVersion:(NSString *)versionA toVersion:(NSString *)versionB;
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
82 {
63011aa Beware of falling bricks! Huge refactoring commit #1: cleansing Spark…
andym authored
83 NSArray *partsA = [self splitVersionString:versionA];
84 NSArray *partsB = [self splitVersionString:versionB];
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
85
86 NSString *partA, *partB;
d7774c0 performed a code review, specifically:
Sean McBride authored
87 NSUInteger i, n;
88 int intA, intB;
89 SUCharacterType typeA, typeB;
90
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
91 n = MIN([partsA count], [partsB count]);
92 for (i = 0; i < n; ++i) {
93 partA = [partsA objectAtIndex:i];
94 partB = [partsB objectAtIndex:i];
95
63011aa Beware of falling bricks! Huge refactoring commit #1: cleansing Spark…
andym authored
96 typeA = [self typeOfCharacter:partA];
97 typeB = [self typeOfCharacter:partB];
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
98
99 // Compare types
100 if (typeA == typeB) {
101 // Same type; we can compare
102 if (typeA == kNumberType) {
103 intA = [partA intValue];
104 intB = [partB intValue];
105 if (intA > intB) {
106 return NSOrderedDescending;
63011aa Beware of falling bricks! Huge refactoring commit #1: cleansing Spark…
andym authored
107 } else if (intA < intB) {
108 return NSOrderedAscending;
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
109 }
110 } else if (typeA == kStringType) {
db44390 Finally added unit tests.
andym authored
111 NSComparisonResult result = [partA compare:partB];
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
112 if (result != NSOrderedSame) {
113 return result;
114 }
115 }
116 } else {
117 // Not the same type? Now we have to do some validity checking
118 if (typeA != kStringType && typeB == kStringType) {
119 // typeA wins
63011aa Beware of falling bricks! Huge refactoring commit #1: cleansing Spark…
andym authored
120 return NSOrderedDescending;
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
121 } else if (typeA == kStringType && typeB != kStringType) {
122 // typeB wins
63011aa Beware of falling bricks! Huge refactoring commit #1: cleansing Spark…
andym authored
123 return NSOrderedAscending;
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
124 } else {
125 // One is a number and the other is a period. The period is invalid
126 if (typeA == kNumberType) {
127 return NSOrderedDescending;
63011aa Beware of falling bricks! Huge refactoring commit #1: cleansing Spark…
andym authored
128 } else {
129 return NSOrderedAscending;
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
130 }
131 }
132 }
133 }
134 // The versions are equal up to the point where they both still have parts
135 // Lets check to see if one is larger than the other
136 if ([partsA count] != [partsB count]) {
137 // Yep. Lets get the next part of the larger
63011aa Beware of falling bricks! Huge refactoring commit #1: cleansing Spark…
andym authored
138 // n holds the index of the part we want.
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
139 NSString *missingPart;
ef89e42 Fixed a potential buffer overrun, made sure to assign to self in -ini…
andym authored
140 SUCharacterType missingType;
141 NSComparisonResult shorterResult, largerResult;
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
142
143 if ([partsA count] > [partsB count]) {
144 missingPart = [partsA objectAtIndex:n];
145 shorterResult = NSOrderedAscending;
146 largerResult = NSOrderedDescending;
63011aa Beware of falling bricks! Huge refactoring commit #1: cleansing Spark…
andym authored
147 } else {
148 missingPart = [partsB objectAtIndex:n];
149 shorterResult = NSOrderedDescending;
150 largerResult = NSOrderedAscending;
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
151 }
152
63011aa Beware of falling bricks! Huge refactoring commit #1: cleansing Spark…
andym authored
153 missingType = [self typeOfCharacter:missingPart];
9fa3da5 Holy restructuring, batman! Watch out for falling folders.
andym authored
154 // Check the type
155 if (missingType == kStringType) {
156 // It's a string. Shorter version wins
157 return shorterResult;
158 } else {
159 // It's a number/period. Larger version wins
160 return largerResult;
161 }
162 }
163
164 // The 2 strings are identical
165 return NSOrderedSame;
166 }
63011aa Beware of falling bricks! Huge refactoring commit #1: cleansing Spark…
andym authored
167
168
87f2893 Fixed warnings for missing newlines at the end of the file.
andym authored
169 @end
Something went wrong with that request. Please try again.