Skip to content

Commit 6a8c094

Browse files
committed
Remove string length limit from levenshtein()
As noted on https://bugs.php.net/bug.php?id=80073, I don't think having this limitation makes sense. The similar_text() function has much worse asymptotic complexity than levenshtein() and does not enforce such a limitation. levenshtein() does have fairly high memory requirements, but they are a fixed factor of the string length (and subject to memory limit).
1 parent 240d061 commit 6a8c094

File tree

2 files changed

+6
-18
lines changed

2 files changed

+6
-18
lines changed

ext/standard/levenshtein.c

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
#include "php.h"
1818
#include "php_string.h"
1919

20-
#define LEVENSHTEIN_MAX_LENGTH 255
21-
2220
/* {{{ reference_levdist
2321
* reference implementation, only optimized for memory usage, not speed */
2422
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 )
@@ -75,24 +73,12 @@ PHP_FUNCTION(levenshtein)
7573
zend_long cost_ins = 1;
7674
zend_long cost_rep = 1;
7775
zend_long cost_del = 1;
78-
zend_long distance = 0;
7976

8077
if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|lll", &string1, &string2, &cost_ins, &cost_rep, &cost_del) == FAILURE) {
8178
RETURN_THROWS();
8279
}
8380

84-
if (ZSTR_LEN(string1) > LEVENSHTEIN_MAX_LENGTH) {
85-
zend_argument_value_error(1, "must be less than %d characters", LEVENSHTEIN_MAX_LENGTH + 1);
86-
RETURN_THROWS();
87-
}
88-
89-
if (ZSTR_LEN(string2) > LEVENSHTEIN_MAX_LENGTH) {
90-
zend_argument_value_error(2, "must be less than %d characters", LEVENSHTEIN_MAX_LENGTH + 1);
91-
RETURN_THROWS();
92-
}
93-
94-
distance = reference_levdist(string1, string2, cost_ins, cost_rep, cost_del);
9581

96-
RETURN_LONG(distance);
82+
RETURN_LONG(reference_levdist(string1, string2, cost_ins, cost_rep, cost_del));
9783
}
9884
/* }}} */

ext/standard/tests/strings/levenshtein_error_conditions.phpt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
--TEST--
2-
levenshtein() error conditions
2+
levenshtein() former error conditions
33
--FILE--
44
<?php
55

6+
// levenshtein no longer has a maximum string length limit.
7+
68
echo '--- String 1 ---' . \PHP_EOL;
79
var_dump(levenshtein('AbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrtsuvwxyzAbcdefghijklmnopqrtsuvwxyzAbcdefghijklmnopqrtsuvwxyzAbcdefghijklmnopqrtsuvwxyzAbcdefghijklmnopqrtsuvwxyzAbcdefghijklmnopqrtsu', 'A'));
810
try {
@@ -25,7 +27,7 @@ try {
2527
--EXPECT--
2628
--- String 1 ---
2729
int(254)
28-
levenshtein(): Argument #1 ($string1) must be less than 256 characters
30+
int(255)
2931
--- String 2 ---
3032
int(254)
31-
levenshtein(): Argument #2 ($string2) must be less than 256 characters
33+
int(255)

0 commit comments

Comments
 (0)