55 *
66 * Joe Conway <mail@joeconway.com>
77 *
8- * $PostgreSQL: pgsql/contrib/fuzzystrmatch/fuzzystrmatch.c,v 1.30 2009/06/11 14:48:51 momjian Exp $
8+ * $PostgreSQL: pgsql/contrib/fuzzystrmatch/fuzzystrmatch.c,v 1.31 2009/12/10 01:54:17 rhaas Exp $
99 * Copyright (c) 2001-2009, PostgreSQL Global Development Group
1010 * ALL RIGHTS RESERVED;
1111 *
@@ -207,13 +207,13 @@ levenshtein_internal(const char *s, const char *t,
207207n = strlen (t );
208208
209209/*
210- *If either m or n is 0, the answer is the other value. This makes sense
211- *since it would take that many insertions to build a matching string
210+ *We can transform an empty s into t with n insertions, or a non-empty t
211+ *into an empty s with m deletions.
212212 */
213213if (!m )
214- return n ;
214+ return n * ins_c ;
215215if (!n )
216- return m ;
216+ return m * del_c ;
217217
218218/*
219219 * For security concerns, restrict excessive CPU+RAM usage. (This
@@ -241,7 +241,7 @@ levenshtein_internal(const char *s, const char *t,
241241
242242/* Initialize the "previous" row to 0..cols */
243243for (i = 0 ;i < m ;i ++ )
244- prev [i ]= i ;
244+ prev [i ]= i * del_c ;
245245
246246/* Loop through rows of the notional array */
247247for (y = t ,j = 1 ;j < n ;y ++ ,j ++ )
@@ -252,7 +252,7 @@ levenshtein_internal(const char *s, const char *t,
252252 * First cell must increment sequentially, as we're on the j'th row of
253253 * the (m+1)x(n+1) array.
254254 */
255- curr [0 ]= j ;
255+ curr [0 ]= j * ins_c ;
256256
257257for (x = s ,i = 1 ;i < m ;x ++ ,i ++ )
258258{