xref: /PHP-8.4/ext/standard/levenshtein.c (revision 25a51461)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Author: Hartmut Holzgraefe <hholzgra@php.net>                        |
14    +----------------------------------------------------------------------+
15  */
16 
17 #include "php.h"
18 
19 /* {{{ reference_levdist
20  * reference implementation, only optimized for memory usage, not speed */
reference_levdist(const zend_string * string1,const zend_string * string2,zend_long cost_ins,zend_long cost_rep,zend_long cost_del)21 static zend_long reference_levdist(const zend_string *string1, const zend_string *string2, zend_long cost_ins, zend_long cost_rep, zend_long cost_del )
22 {
23 	zend_long *p1, *p2, *tmp;
24 	zend_long c0, c1, c2;
25 	size_t i1, i2;
26 
27 	if (ZSTR_LEN(string1) == 0) {
28 		return ZSTR_LEN(string2) * cost_ins;
29 	}
30 	if (ZSTR_LEN(string2) == 0) {
31 		return ZSTR_LEN(string1) * cost_del;
32 	}
33 
34 	/* When all costs are equal, levenshtein fulfills the requirements of a metric, which means
35 	 * that the distance is symmetric. If string1 is shorter than string 2 we can save memory (and CPU time)
36 	 * by having shorter rows (p1 & p2). */
37 	if (ZSTR_LEN(string1) < ZSTR_LEN(string2) && cost_ins == cost_rep && cost_rep == cost_del) {
38 		const zend_string *tmp = string1;
39 		string1 = string2;
40 		string2 = tmp;
41 	}
42 
43 	p1 = safe_emalloc((ZSTR_LEN(string2) + 1), sizeof(zend_long), 0);
44 	p2 = safe_emalloc((ZSTR_LEN(string2) + 1), sizeof(zend_long), 0);
45 
46 	for (i2 = 0; i2 <= ZSTR_LEN(string2); i2++) {
47 		p1[i2] = i2 * cost_ins;
48 	}
49 	for (i1 = 0; i1 < ZSTR_LEN(string1) ; i1++) {
50 		p2[0] = p1[0] + cost_del;
51 
52 		for (i2 = 0; i2 < ZSTR_LEN(string2); i2++) {
53 			c0 = p1[i2] + ((ZSTR_VAL(string1)[i1] == ZSTR_VAL(string2)[i2]) ? 0 : cost_rep);
54 			c1 = p1[i2 + 1] + cost_del;
55 			if (c1 < c0) {
56 				c0 = c1;
57 			}
58 			c2 = p2[i2] + cost_ins;
59 			if (c2 < c0) {
60 				c0 = c2;
61 			}
62 			p2[i2 + 1] = c0;
63 		}
64 		tmp = p1;
65 		p1 = p2;
66 		p2 = tmp;
67 	}
68 	c0 = p1[ZSTR_LEN(string2)];
69 
70 	efree(p1);
71 	efree(p2);
72 
73 	return c0;
74 }
75 /* }}} */
76 
77 /* {{{ Calculate Levenshtein distance between two strings */
PHP_FUNCTION(levenshtein)78 PHP_FUNCTION(levenshtein)
79 {
80 	zend_string *string1, *string2;
81 	zend_long cost_ins = 1;
82 	zend_long cost_rep = 1;
83 	zend_long cost_del = 1;
84 
85 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|lll", &string1, &string2, &cost_ins, &cost_rep, &cost_del) == FAILURE) {
86 		RETURN_THROWS();
87 	}
88 
89 
90 	RETURN_LONG(reference_levdist(string1, string2, cost_ins, cost_rep, cost_del));
91 }
92 /* }}} */
93