--- postgresql-8.1.9/contrib/fuzzystrmatch/fuzzystrmatch.c	2007-08-24 16:44:37.006805991 +0800
+++ postgresql-8.1.9/contrib/fuzzystrmatch/fuzzystrmatch.c	2007-08-24 16:48:07.518802385 +0800
@@ -41,8 +41,15 @@
  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  *
  */
-
+#include "postgres.h"
+#include "mb/pg_wchar.h"
 #include "fuzzystrmatch.h"
+#ifdef HAVE_WCHAR_H
+#include <wchar.h>
+#endif
+#ifdef HAVE_WCTYPE_H
+#include <wctype.h>
+#endif
 
 /*
  * Calculates Levenshtein Distance between two strings.
@@ -53,9 +60,8 @@
 Datum
 levenshtein(PG_FUNCTION_ARGS)
 {
-	char	   *str_s;
-	char	   *str_s0;
-	char	   *str_t;
+	char	   *str_s_c;
+	char	   *str_t_c;
 	int			cols = 0;
 	int			rows = 0;
 	int		   *u_cells;
@@ -65,16 +71,27 @@
 	int			j;
 
 	/*
+     	 * support the multibyte string
+	 */
+        pg_wchar *str_s;
+	pg_wchar *str_s0;
+	pg_wchar *str_t;
+
+	/*
 	 * Fetch the arguments. str_s is referred to as the "source" cols = length
 	 * of source + 1 to allow for the initialization column str_t is referred
 	 * to as the "target", rows = length of target + 1 rows = length of target
 	 * + 1 to allow for the initialization row
 	 */
-	str_s = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(PG_GETARG_TEXT_P(0))));
-	str_t = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(PG_GETARG_TEXT_P(1))));
+	str_s_c = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(PG_GETARG_TEXT_P(0))));
+	str_t_c = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(PG_GETARG_TEXT_P(1))));
+	str_s = (pg_wchar *)palloc((strlen(str_s_c) + 1) * sizeof(pg_wchar));
+	str_t = (pg_wchar *)palloc((strlen(str_t_c) + 1) * sizeof(pg_wchar));
+	pg_mb2wchar_with_len(str_s_c, str_s, strlen(str_s_c));
+	pg_mb2wchar_with_len(str_t_c, str_t, strlen(str_t_c));
 
-	cols = strlen(str_s) + 1;
-	rows = strlen(str_t) + 1;
+	cols = wcslen(str_s) + 1;
+	rows = wcslen(str_t) + 1;
 
 	/*
 	 * Restrict the length of the strings being compared to something
