cvs: /php3/functions soundex.c

From: Date: Mon, 26 Jul 1999 22:08:38 +0000
Subject: cvs: /php3/functions soundex.c
Groups: php.dev 
Request: Send a blank email to php-dev+get-9200@lists.php.net to get a copy of this message
verdy_p Mon Jul 26 18:08:38 1999 EDT Modified files: /php3/functions soundex.c Log: Merged loops into one to avoid alteration of source string and minimize the count of loops performed on long strings Index: php3/functions/soundex.c diff -u php3/functions/soundex.c:1.31 php3/functions/soundex.c:1.32 --- php3/functions/soundex.c:1.31 Thu Apr 22 09:19:50 1999 +++ php3/functions/soundex.c Mon Jul 26 18:08:38 1999 @@ -26,7 +26,7 @@ | Author: Bjørn Borud <borud@fast.no> | +----------------------------------------------------------------------+ */ -/* $Id: soundex.c,v 1.31 1999/04/22 13:19:50 ssb Exp $ */ +/* $Id: soundex.c,v 1.32 1999/07/26 22:08:38 verdy_p Exp $ */ #include "php.h" #include "internal_functions.h" @@ -40,15 +40,12 @@ Calculate the soundex key of a string */ void soundex(INTERNAL_FUNCTION_PARAMETERS) { - char l, u; - char *somestring; - int i, j, n; - pval *arg; + char last, code; + char *somestring; + int i, small, len; + pval *arg; + char soundex[4 + 1]; - /* pad with '0' and terminate with 0 ;-) */ - char soundex[5] = - {'0', '0', '0', '0', 0}; - static char soundex_table[26] = {0, /* A */ '1', /* B */ @@ -76,6 +73,7 @@ '2', /* X */ 0, /* Y */ '2'}; /* Z */ + if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg) == FAILURE) { WRONG_PARAM_COUNT; } @@ -83,48 +81,44 @@ if (arg->value.str.len==0) { RETURN_FALSE; } - somestring = arg->value.str.val; + len = arg->value.str.len; - n = arg->value.str.len; - - /* convert chars to upper case and strip non-letter chars */ - j = 0; - for (i = 0; i < n; i++) { - u = toupper(somestring[i]); - if ((u > 64) && (u < 91)) { - somestring[j] = u; - j++; - } - } - - /* null-terminate string */ - somestring[j] = 0; - - n = strlen(somestring); - - /* prefix soundex string with first valid char */ - soundex[0] = somestring[0]; - - /* remember first char */ - l = soundex_table[((somestring[0]) - 65)]; - - j = 1; - /* build soundex string */ - for (i = 1; i < n && j < 4; i++) { - u = soundex_table[((somestring[i]) - 65)]; - - if (u != l) { - if (u != 0) { - soundex[(int) j++] = u; + for (i = 0, small = 0; i < len && small < 4; i++) { + /* convert chars to upper case and strip non-letter chars */ + /* BUG: should also map here accented letters used in non */ + /* English words or names (also found in English text!): */ + /* esstsett, thorn, n-tilde, c-cedilla, s-caron, ... */ + code = toupper(somestring[i]); + if (code >= 'A' && code <= 'Z') { + if (small == 0) { + /* remember first valid char */ + soundex[small++] = code; + last = soundex_table[code - 'A']; + } + else { + /* ignore sequences of consonants with same soundex */ + /* code in trail, and vowels unless they separate */ + /* consonant letters */ + code = soundex_table[code - 'A']; + if (code != last) { + if (code != 0) { + soundex[small++] = code; + } + last = code; + } } - l = u; } } + /* pad with '0' and terminate with 0 ;-) */ + while (small < 4) { + soundex[small++] = '0'; + } + soundex[small] = '\0'; - return_value->value.str.val = estrndup(soundex, 4); - return_value->value.str.len = strlen(soundex); + return_value->value.str.val = estrndup(soundex, small); + return_value->value.str.len = small; return_value->type = IS_STRING; } /* }}} */

« previous php.dev (#9200) next »