mirror of
https://github.com/HackPlan/MTDates.git
synced 2026-04-28 19:55:43 +08:00
Remove levenshteinDistanceWithString...
Looks like it was causing crashes like this; > xctest(8240,0x7fff77890310) malloc: *** error for object 0x100823c08: > incorrect checksum for freed object - object was probably modified > after being freed. > *** set a breakpoint in malloc_error_break to debug I didn't get what actually was wrong there so as possible solution I'm proposing to remove it. Probably it was added because on different OS (OS X and iOS) date formatter may return different values. But right now the project configured that way that it runs tests only in OS X. And I expect it returns the same values all the time. Fixes #34
This commit is contained in:
@@ -878,9 +878,7 @@
|
||||
{
|
||||
NSDate *date = [_formatter dateFromString:@"07/11/1986 11:00pm"];
|
||||
[NSDate mt_setFormatterDateStyle:NSDateFormatterLongStyle];
|
||||
|
||||
NSString *s = [date mt_stringValue];
|
||||
XCTAssertTrue([self levenshteinDistanceWithString:s fromString:@"July 11, 1986 11:00 PM"] < 4);
|
||||
XCTAssertEqualObjects([date mt_stringValue], @"July 11, 1986 at 11:00 PM");
|
||||
|
||||
[NSDate mt_setFormatterTimeStyle:NSDateFormatterNoStyle];
|
||||
XCTAssertEqualObjects([date mt_stringValue], @"July 11, 1986");
|
||||
@@ -890,9 +888,7 @@
|
||||
{
|
||||
NSDate *date = [_formatter dateFromString:@"07/11/1986 11:00pm"];
|
||||
[NSDate mt_setFormatterTimeStyle:NSDateFormatterMediumStyle];
|
||||
|
||||
NSString *s = [date mt_stringValue];
|
||||
XCTAssertTrue([self levenshteinDistanceWithString:s fromString:@"July 11, 1986 11:00:00 PM"] < 4);
|
||||
XCTAssertEqualObjects([date mt_stringValue], @"July 11, 1986 at 11:00:00 PM");
|
||||
|
||||
[NSDate mt_setFormatterDateStyle:NSDateFormatterNoStyle];
|
||||
XCTAssertEqualObjects([date mt_stringValue], @"11:00:00 PM");
|
||||
@@ -902,7 +898,7 @@
|
||||
{
|
||||
NSDate *date = [_formatter dateFromString:@"07/11/1986 11:00pm"];
|
||||
NSString *s = [date mt_stringValueWithDateStyle:NSDateFormatterFullStyle timeStyle:NSDateFormatterShortStyle];
|
||||
XCTAssertTrue([self levenshteinDistanceWithString:s fromString:@"Friday, July 11, 1986 11:00 PM"] < 4);
|
||||
XCTAssertEqualObjects(s, @"Friday, July 11, 1986 at 11:00 PM");
|
||||
}
|
||||
|
||||
- (void)test_stringFromDateWithHourAndMinuteFormat
|
||||
@@ -1198,7 +1194,7 @@
|
||||
|
||||
XCTAssertEqualObjects([NSDate mt_dateFromString:string usingFormat:MTDatesFormatDefault], date);
|
||||
NSString *formatted = [date mt_stringFromDateWithFormat:MTDatesFormatDefault localized:YES];
|
||||
XCTAssertTrue([self levenshteinDistanceWithString:formatted fromString:formatted] < 2);
|
||||
XCTAssertEqualObjects(formatted, string);
|
||||
}
|
||||
|
||||
- (void)test_MTDatesFormatShortDate
|
||||
@@ -1335,58 +1331,4 @@
|
||||
[NSDate mt_setCalendarIdentifier:NSGregorianCalendar];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#pragma mark - Private
|
||||
|
||||
// calculate the distance between two string treating them eash as a single word
|
||||
// credit: https://github.com/pigoz/imal/blob/master/NSString+Levenshtein.m
|
||||
|
||||
int minimum(int a,int b,int c)
|
||||
{
|
||||
NSInteger min=a;
|
||||
if(b<min)
|
||||
min=b;
|
||||
if(c<min)
|
||||
min=c;
|
||||
return min;
|
||||
}
|
||||
|
||||
|
||||
- (int)levenshteinDistanceWithString:(NSString *)string fromString:(NSString *)string2
|
||||
{
|
||||
NSInteger *d; // distance vector
|
||||
NSInteger i,j,k; // indexes
|
||||
NSInteger cost, distance;
|
||||
|
||||
NSUInteger n = [string2 length];
|
||||
NSUInteger m = [string length];
|
||||
|
||||
if( n!=0 && m!=0 ){
|
||||
|
||||
d = malloc( sizeof(int) * (++n) * (++m) );
|
||||
|
||||
for( k=0 ; k<n ; k++ )
|
||||
d[k] = k;
|
||||
for( k=0 ; k<m ; k++ )
|
||||
d[k*n] = k;
|
||||
|
||||
for( i=1; i<n ; i++ ) {
|
||||
for( j=1 ;j<m ; j++ ) {
|
||||
if( [string2 characterAtIndex:i-1] == [string characterAtIndex:j-1])
|
||||
cost = 0;
|
||||
else
|
||||
cost = 1;
|
||||
d[j*n+i]=minimum(d[(j-1)*n+i]+1,d[j*n+i-1]+1,d[(j-1)*n+i-1]+cost);
|
||||
}
|
||||
}
|
||||
distance = d[n*m-1];
|
||||
free(d);
|
||||
return distance;
|
||||
}
|
||||
|
||||
return -1; // error
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user