diff options
| author | ruki <[email protected]> | 2023-02-01 23:19:19 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2023-02-01 23:19:19 +0800 |
| commit | 8d09077f31b043b1d94494f42f6ae2a3109535ea (patch) | |
| tree | a3167de651aa3a52976e7748d72eaca45a14171a /xmake/core/base/string.lua | |
| parent | 8658eae245dafc3b539c6b4280455773caa0ef8a (diff) | |
add probable value
Diffstat (limited to 'xmake/core/base/string.lua')
| -rw-r--r-- | xmake/core/base/string.lua | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/xmake/core/base/string.lua b/xmake/core/base/string.lua index 43b0096b4..625678c6c 100644 --- a/xmake/core/base/string.lua +++ b/xmake/core/base/string.lua @@ -379,5 +379,42 @@ function string:wcswidth(idx) return width end +-- compute the Levenshtein distance between two strings +function string:levenshtein(str2) + local str1 = self + local len1 = #str1 + local len2 = #str2 + local matrix = {} + local cost = 0 + + if len1 == 0 then + return len2 + elseif len2 == 0 then + return len1 + elseif str1 == str2 then + return 0 + end + + for i = 0, len1, 1 do + matrix[i] = {} + matrix[i][0] = i + end + for j = 0, len2, 1 do + matrix[0][j] = j + end + + for i = 1, len1, 1 do + for j = 1, len2, 1 do + if (str1:byte(i) == str2:byte(j)) then + cost = 0 + else + cost = 1 + end + matrix[i][j] = math.min(matrix[i-1][j] + 1, matrix[i][j-1] + 1, matrix[i-1][j-1] + cost) + end + end + return matrix[len1][len2] +end + -- return module: string return string |
