CVS update: php3/functions

From: Date: Mon, 04 Jan 1999 03:43:00 +0000
Subject: CVS update: php3/functions
Groups: php.dev 
Request: Send a blank email to php-dev+get-3233@lists.php.net to get a copy of this message
Date: Sunday January 3, 1999 @ 22:42 Author: sas Update of /repository/php3/functions In directory asf:/u/temp/cvs-serv27479/functions Modified Files: basic_functions.c string.c php3_string.h Log Message: add similar_text() function Index: php3/functions/basic_functions.c diff -c php3/functions/basic_functions.c:1.246 php3/functions/basic_functions.c:1.247 *** php3/functions/basic_functions.c:1.246 Sun Jan 3 12:32:01 1999 --- php3/functions/basic_functions.c Sun Jan 3 22:42:57 1999 *************** *** 28,34 **** +----------------------------------------------------------------------+ */ ! /* $Id: basic_functions.c,v 1.246 1999/01/03 17:32:01 rasmus Exp $ */ #ifdef THREAD_SAFE #include "tls.h" #endif --- 28,34 ---- +----------------------------------------------------------------------+ */ ! /* $Id: basic_functions.c,v 1.247 1999/01/04 03:42:57 sas Exp $ */ #ifdef THREAD_SAFE #include "tls.h" #endif *************** *** 192,197 **** --- 192,198 ---- {"strtr", php3_strtr, NULL}, {"sprintf", php3_user_sprintf, NULL}, {"printf", php3_user_printf, NULL}, + {"similar_text", php3_similar_text, NULL}, {"setlocale", php3_setlocale, NULL}, {"exec", php3_exec, second_and_third_args_force_ref}, Index: php3/functions/string.c diff -c php3/functions/string.c:1.160 php3/functions/string.c:1.161 *** php3/functions/string.c:1.160 Fri Jan 1 12:59:18 1999 --- php3/functions/string.c Sun Jan 3 22:42:57 1999 *************** *** 30,36 **** */ ! /* $Id: string.c,v 1.160 1999/01/01 17:59:18 zeev Exp $ */ #ifdef THREAD_SAFE #include "tls.h" #endif --- 30,36 ---- */ ! /* $Id: string.c,v 1.161 1999/01/04 03:42:57 sas Exp $ */ #ifdef THREAD_SAFE #include "tls.h" #endif *************** *** 1048,1054 **** --- 1048,1120 ---- } /* }}} */ + static void _php3_similar_str(const char *txt1, int len1, const char *txt2, + int len2, int *pos1, int *pos2, int *max) + { + char *p, *q; + char *end1 = (char *) txt1 + len1; + char *end2 = (char *) txt2 + len2; + int l; + + *max = 0; + for (p = (char *) txt1; p < end1; p++) { + for (q = (char *) txt2; q < end2; q++) { + for (l = 0; (p + l < end1) && (q + l < end2) && (p[l] == q[l]); + l++); + if (l > *max) { + *max = l; + *pos1 = p - txt1; + *pos2 = q - txt2; + } + } + } + } + static int _php3_similar_char(const char *txt1, int len1, + const char *txt2, int len2) + { + int sum; + int pos1, pos2, max; + + _php3_similar_str(txt1, len1, txt2, len2, &pos1, &pos2, &max); + if ((sum = max)) { + if (pos1 && pos2) + sum += _php3_similar_char(txt1, pos1, txt2, pos2); + if ((pos1 + max < len1) && (pos2 + max < len2)) + sum += _php3_similar_char(txt1 + pos1 + max, len1 - pos1 - max, + txt2 + pos2 + max, len2 - pos2 -max); + } + return sum; + } + + /* {{{ proto int similar_text(string str1, string str2 [, double percent]) + Calculates the similarity between two strings */ + void php3_similar_text(INTERNAL_FUNCTION_PARAMETERS) + { + pval *t1, *t2, *percent; + int ac = ARG_COUNT(ht); + int sim; + + if (ac < 2 || ac > 3 || + getParameters(ht, ac, &t1, &t2, &percent) == FAILURE) { + WRONG_PARAM_COUNT; + } + + convert_to_string(t1); + convert_to_string(t2); + + sim = _php3_similar_char(t1->value.str.val, t1->value.str.len, + t2->value.str.val, t2->value.str.len); + + if (ac > 2) { + convert_to_double(percent); + percent->value.dval = sim * 200.0 / (t1->value.str.len + t2->value.str.len); + } + + RETURN_LONG(sim); + } + /* }}} */ + /* be careful, this edits the string in-place */ PHPAPI void _php3_stripslashes(char *string, int *len) { Index: php3/functions/php3_string.h diff -c php3/functions/php3_string.h:1.28 php3/functions/php3_string.h:1.29 *** php3/functions/php3_string.h:1.28 Fri Jan 1 12:59:16 1999 --- php3/functions/php3_string.h Sun Jan 3 22:42:57 1999 *************** *** 29,35 **** */ ! /* $Id: php3_string.h,v 1.28 1999/01/01 17:59:16 zeev Exp $ */ #ifndef _PHPSTRING_H #define _PHPSTRING_H --- 29,35 ---- */ ! /* $Id: php3_string.h,v 1.29 1999/01/04 03:42:57 sas Exp $ */ #ifndef _PHPSTRING_H #define _PHPSTRING_H *************** *** 47,52 **** --- 47,53 ---- extern void php3_chop(INTERNAL_FUNCTION_PARAMETERS); extern void php3_trim(INTERNAL_FUNCTION_PARAMETERS); extern void php3_ltrim(INTERNAL_FUNCTION_PARAMETERS); + extern void php3_similar_text(INTERNAL_FUNCTION_PARAMETERS); extern void soundex(INTERNAL_FUNCTION_PARAMETERS); extern void php3_explode(INTERNAL_FUNCTION_PARAMETERS); -- PHP Development Mailing List http://www.php.net/ To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net For help: php-dev-help@lists.php.net

« previous php.dev (#3233) next »