aboutsummaryrefslogtreecommitdiffstats
path: root/Source/SPStringAdditions.m
diff options
context:
space:
mode:
authorBibiko <bibiko@eva.mpg.de>2009-07-13 15:24:16 +0000
committerBibiko <bibiko@eva.mpg.de>2009-07-13 15:24:16 +0000
commit27db10fe504117dd8b4a166dcfc94881528c44c3 (patch)
tree1926c3c44974d3a632b90b3bfca7fd9a25e8ce27 /Source/SPStringAdditions.m
parent04e807c2ca899cabe57ee834eab4336b8e3dbb7a (diff)
downloadsequelpro-27db10fe504117dd8b4a166dcfc94881528c44c3.tar.gz
sequelpro-27db10fe504117dd8b4a166dcfc94881528c44c3.tar.bz2
sequelpro-27db10fe504117dd8b4a166dcfc94881528c44c3.zip
• added levenshteinDistanceOfWord: to SPStringAdditions
- will be used mainly for auto-matching column names while csv import • fixed bug for in/decreasing the font size by using the two-finger gesture on a trackpad if a textView has a selection
Diffstat (limited to 'Source/SPStringAdditions.m')
-rw-r--r--Source/SPStringAdditions.m72
1 files changed, 70 insertions, 2 deletions
diff --git a/Source/SPStringAdditions.m b/Source/SPStringAdditions.m
index 114ecf7d..ff7efc88 100644
--- a/Source/SPStringAdditions.m
+++ b/Source/SPStringAdditions.m
@@ -26,6 +26,11 @@
#import "SPStringAdditions.h"
#import "RegexKitLite.h"
+
+@interface NSString (Private)
+- (int)smallestOf:(int)a andOf:(int)b andOf:(int)c;
+@end
+
@implementation NSString (SPStringAdditions)
/*
@@ -272,7 +277,7 @@
#endif
-- (NSString *) stringByRemovingCharactersInSet:(NSCharacterSet*) charSet options:(unsigned) mask
+- (NSString *)stringByRemovingCharactersInSet:(NSCharacterSet*) charSet options:(unsigned) mask
{
NSRange range;
NSMutableString* newString = [NSMutableString string];
@@ -302,9 +307,72 @@
}
-- (NSString *) stringByRemovingCharactersInSet:(NSCharacterSet*) charSet
+- (NSString *)stringByRemovingCharactersInSet:(NSCharacterSet*) charSet
{
return [self stringByRemovingCharactersInSet:charSet options:0];
}
+// calculate the distance between two string case-insensitively
+- (float)levenshteinDistanceWithWord:(NSString *)stringB
+{
+ // normalize strings
+ NSString * stringA = [NSString stringWithString: self];
+ [stringA stringByTrimmingCharactersInSet:
+ [NSCharacterSet whitespaceAndNewlineCharacterSet]];
+ [stringB stringByTrimmingCharactersInSet:
+ [NSCharacterSet whitespaceAndNewlineCharacterSet]];
+ stringA = [stringA lowercaseString];
+ stringB = [stringB lowercaseString];
+
+ int k, i, j, cost, * d, distance;
+
+ int n = [stringA length];
+ int m = [stringB length];
+
+ if( n++ != 0 && m++ != 0 ) {
+
+ d = malloc( sizeof(int) * m * n );
+
+ 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( [stringA characterAtIndex: i-1] == [stringB characterAtIndex: j-1] )
+ cost = 0;
+ else
+ cost = 1;
+
+ d[ j * n + i ] = [self smallestOf: d [ (j - 1) * n + i ] + 1
+ andOf: d[ j * n + i - 1 ] + 1
+ andOf: d[ (j - 1) * n + i -1 ] + cost ];
+ }
+
+ distance = d[ n * m - 1 ];
+
+ free( d );
+
+ return distance;
+ }
+ return 0.0;
+}
+
+// return the minimum of a, b and c
+- (int)smallestOf:(int)a andOf:(int)b andOf:(int)c
+{
+ int min = a;
+ if ( b < min )
+ min = b;
+
+ if( c < min )
+ min = c;
+
+ return min;
+}
+
+
@end