php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
levenshtein.c
Go to the documentation of this file.
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 */
21static 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 */
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) {
87 }
88
89
90 RETURN_LONG(reference_levdist(string1, string2, cost_ins, cost_rep, cost_del));
91}
92/* }}} */
levenshtein(string $string1, string $string2, int $insertion_cost=1, int $replacement_cost=1, int $deletion_cost=1)
#define PHP_FUNCTION
Definition php.h:364
ZEND_API zend_result zend_parse_parameters(uint32_t num_args, const char *type_spec,...)
Definition zend_API.c:1300
#define ZEND_NUM_ARGS()
Definition zend_API.h:530
#define RETURN_LONG(l)
Definition zend_API.h:1037
#define RETURN_THROWS()
Definition zend_API.h:1060
#define efree(ptr)
Definition zend_alloc.h:155
#define safe_emalloc(nmemb, size, offset)
Definition zend_alloc.h:154
int32_t zend_long
Definition zend_long.h:42
struct _zend_string zend_string
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
#define ZSTR_LEN(zstr)
Definition zend_string.h:69
@ FAILURE
Definition zend_types.h:61