aboutsummaryrefslogtreecommitdiffstats
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
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
-rw-r--r--Source/SPStringAdditions.h6
-rw-r--r--Source/SPStringAdditions.m72
-rw-r--r--Source/SPTextViewAdditions.m3
3 files changed, 75 insertions, 6 deletions
diff --git a/Source/SPStringAdditions.h b/Source/SPStringAdditions.h
index f6a49105..47863370 100644
--- a/Source/SPStringAdditions.h
+++ b/Source/SPStringAdditions.h
@@ -59,8 +59,10 @@ static inline NSData* NSStringDataUsingLossyEncoding(NSString* self, int encodin
- (NSArray *)lineRangesForRange:(NSRange)aRange;
- (NSString *)createViewSyntaxPrettifier;
-- (NSString *) stringByRemovingCharactersInSet:(NSCharacterSet*) charSet options:(unsigned) mask;
-- (NSString *) stringByRemovingCharactersInSet:(NSCharacterSet*) charSet;
+- (NSString *)stringByRemovingCharactersInSet:(NSCharacterSet*) charSet options:(unsigned) mask;
+- (NSString *)stringByRemovingCharactersInSet:(NSCharacterSet*) charSet;
+
+- (float)levenshteinDistanceWithWord:(NSString *)stringB;
#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5
- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)set;
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
diff --git a/Source/SPTextViewAdditions.m b/Source/SPTextViewAdditions.m
index f9c9a1de..2dcf0614 100644
--- a/Source/SPTextViewAdditions.m
+++ b/Source/SPTextViewAdditions.m
@@ -416,7 +416,7 @@
#pragma mark multi-touch trackpad support
/*
- * Trackpad two-finger zooming gesture in/decreases the font size
+ * Trackpad two-finger zooming gesture for in/decreasing the font size
*/
- (void) magnifyWithEvent:(NSEvent *)anEvent
{
@@ -430,7 +430,6 @@
else if([anEvent deltaZ]<-5.0)
[self makeTextSizeSmaller];
- [self insertText:@""];
}