You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/string-extensions.md
+17Lines changed: 17 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -106,3 +106,20 @@ print(string.random(10)) -- e.g. "aZ7qT19BcP"
106
106
107
107
print(string.random(5, "abc")) -- e.g. "abacb"
108
108
```
109
+
110
+
### `string.levenshtein(first, second)`
111
+
112
+
Returns the [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance) between two strings - the minimum number of edits (insertions, deletions, or subtitutions) required to transform the `first` string into the `second` string.
113
+
114
+
This is useful for fuzzy matching, typo tolerance, and somewhat measuring similarity between two strings.
115
+
116
+
> [!NOTE]
117
+
> Empty strings return the length of the other string as distance.
The function internally builds a distance matrix that tracks how much "effort" it takes to align each character of `first` with each character of `second`. Every mismatch adds a penalty (`+1`), unless the characters match (in which case it is `+0`). The bottom-right cell of the matrix (table) holds the final answer, which is the total number of edits needed.
-- yes, I know that 'injecting' my own custom functions into default libraries isn't entirely ideal
13
+
-- but its part of actually making the string library more useful
14
+
-- also for that built-in feel
15
+
12
16
localgmatch=string.gmatch
13
17
localfind=string.find
14
18
localsub=string.sub
@@ -19,17 +23,6 @@ local concat = table.concat
19
23
20
24
localrandom=math.random
21
25
22
-
-- yes, I know that 'injecting' my own custom functions into default libraries isn't entirely ideal
23
-
-- but its part of actually making the string library more useful
24
-
-- also for that built-in feel
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
26
-- Returns `true` if `str` starts with `prefix`.
34
27
--
35
28
-- ```lua
@@ -222,5 +215,50 @@ function string.random(length, charset)
222
215
returnr
223
216
end
224
217
218
+
-- Returns the [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance) between two strings - the minimum number of edits (insertions, deletions, or subtitutions) required to transform the `first` string into the `second` string.
219
+
--
220
+
-- This is useful for fuzzy matching, typo tolerance, and somewhat measuring similarity between two strings.
221
+
--
222
+
-- > **NOTE!!!**
223
+
-- > Empty strings return the length of the other string as distance.
-- The function internally builds a distance matrix that tracks how much "effort" it takes to align each character of `first` with each character of `second`. Every mismatch adds a penalty (`+1`), unless the characters match (in which case it is `+0`). The bottom-right cell of the matrix (table) holds the final answer, which is the total number of edits needed.
232
+
---@paramfirststring
233
+
---@paramsecondstring
234
+
---@returninteger distance Levenshtein distance between `first` and `second`
0 commit comments