博学而笃志 切问而近思 仁在其中
详情
分析PHP的similar_text函数
作者:Aliot     发布时间:2017-07-02     评论:0     阅读:0

分析PHP的similar_text函数

时间:2010-01-20

//比较字串,返回两个字串第一个相同字符的区域
static void php_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;
  }
 }
}
}
//递归函数,比较txt1和txt2的相同字符数量
static int php_similar_char(const char *txt1, int len1, const char *txt2, int len2)
{
int sum;
int pos1, pos2, max;
php_similar_str(txt1, len1, txt2, len2, &pos1, &pos2, &max);
if ((sum = max)) {//这样写有什么意义?????!!!!!
 //递归上次不同部分的前部
 if (pos1 && pos2) {
  sum += php_similar_char(txt1, pos1, txt2, pos2);
 }
 //递归上次不同部分的后部
 if ((pos1 + max < len1) && (pos2 + max < len2)) {
  sum += php_similar_char(txt1 + pos1 + max, len1 - pos1 - max,
   txt2 + pos2 + max, len2 - pos2 - max);
 }
}
//返回本次比较后的相同字符数量
return sum;
}
//PHP函数本身,一堆宏,做了些串转换和返回值换算,主功能由上面两个函数做了。
PHP_FUNCTION(similar_text)
{
zval **t1, **t2, **percent;
int ac = ZEND_NUM_ARGS();
int sim;
if (ac < 2 || ac > 3 || zend_get_parameters_ex(ac, &t1, &t2, &percent) == FAILURE) {
 WRONG_PARAM_COUNT;
}
convert_to_string_ex(t1);
convert_to_string_ex(t2);
if (ac > 2) {
 convert_to_double_ex(percent);
}
if (Z_STRLEN_PP(t1) + Z_STRLEN_PP(t2) == 0) {
 if (ac > 2) {
  Z_DVAL_PP(percent) = 0;
 }
 RETURN_LONG(0);
}
sim = php_similar_char(Z_STRVAL_PP(t1), Z_STRLEN_PP(t1), Z_STRVAL_PP(t2), Z_STRLEN_PP(t2));
if (ac > 2) {
 Z_DVAL_PP(percent) = sim * 200.0 / (Z_STRLEN_PP(t1) + Z_STRLEN_PP(t2));
}
RETURN_LONG(sim);
}


相关文章
loading......
最新动态
所有评论

loading......

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册