summaryrefslogtreecommitdiff
path: root/contrib/fuzzystrmatch/fuzzystrmatch.c
diff options
context:
space:
mode:
authorBruce Momjian <bruce@momjian.us>2001-08-07 18:16:01 +0000
committerBruce Momjian <bruce@momjian.us>2001-08-07 18:16:01 +0000
commitcdd02cdf00f020292cdcc8dafa5475e0149c34a6 (patch)
tree317c5c67dbecac152c6c67766ac86d3f5c8e9536 /contrib/fuzzystrmatch/fuzzystrmatch.c
parentfb5b85a8f2663883f1e3287680dbe0db54e1b617 (diff)
Sorry - I should have gotten to this sooner. Here's a patch which you should
be able to apply against what you just committed. It rolls soundex into fuzzystrmatch. Remove soundex/metaphone and merge into fuzzystrmatch. Joe Conway
Diffstat (limited to 'contrib/fuzzystrmatch/fuzzystrmatch.c')
-rw-r--r--contrib/fuzzystrmatch/fuzzystrmatch.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/contrib/fuzzystrmatch/fuzzystrmatch.c b/contrib/fuzzystrmatch/fuzzystrmatch.c
index 80e9e69cfad..4a2d3f932e7 100644
--- a/contrib/fuzzystrmatch/fuzzystrmatch.c
+++ b/contrib/fuzzystrmatch/fuzzystrmatch.c
@@ -629,3 +629,71 @@ int _metaphone (
return(META_SUCCESS);
} /* END metaphone */
+
+
+/*
+ * SQL function: soundex(text) returns text
+ */
+PG_FUNCTION_INFO_V1(soundex);
+
+Datum
+soundex(PG_FUNCTION_ARGS)
+{
+ char outstr[SOUNDEX_LEN + 1];
+ char *arg;
+
+ arg = _textout(PG_GETARG_TEXT_P(0));
+
+ _soundex(arg, outstr);
+
+ PG_RETURN_TEXT_P(_textin(outstr));
+}
+
+static void
+_soundex(const char *instr, char *outstr)
+{
+ int count;
+
+ AssertArg(instr);
+ AssertArg(outstr);
+
+ outstr[SOUNDEX_LEN] = '\0';
+
+ /* Skip leading non-alphabetic characters */
+ while (!isalpha((unsigned char) instr[0]) && instr[0])
+ ++instr;
+
+ /* No string left */
+ if (!instr[0])
+ {
+ outstr[0] = (char) 0;
+ return;
+ }
+
+ /* Take the first letter as is */
+ *outstr++ = (char) toupper((unsigned char) *instr++);
+
+ count = 1;
+ while (*instr && count < SOUNDEX_LEN)
+ {
+ if (isalpha((unsigned char) *instr) &&
+ soundex_code(*instr) != soundex_code(*(instr - 1)))
+ {
+ *outstr = soundex_code(instr[0]);
+ if (*outstr != '0')
+ {
+ ++outstr;
+ ++count;
+ }
+ }
+ ++instr;
+ }
+
+ /* Fill with 0's */
+ while (count < SOUNDEX_LEN)
+ {
+ *outstr = '0';
+ ++outstr;
+ ++count;
+ }
+}