local s=utf8.len local m=utf8.codes local function h(h,f)if h==f then return 0 end local S=s(h)local I=s(f)if S==0 then return I elseif I==0 then return S end local b={}for s=0,S,1 do b[s]={[0]=s}end for s=0,I,1 do b[0][s]=s end local H=1 local a for s,h in m(h)do local S=1 local I for s,m in m(f)do local f=h==m and 0 or 1 a=H-1 I=S-1 b[H][S]=math.min(b[a][S]+1,b[H][I]+1,b[a][I]+f)S=S+1 end H=H+1 end return b[S][I]end local function f(s,m)return s.Distance<m.Distance end local function S(s,m,S)local I=table.create(#m)for m,f in ipairs(m)do I[m]={String=f,Distance=h(s,f)}end table.sort(I,f)return I end return S

local utf8_len = utf8.len -- I'm not even sure if this is faster localized, thanks Luau and your massive inconsistency.
local utf8_codes = utf8.codes

local function Levenshtein(String1, String2)
	if String1 == String2 then
		return 0
	end

	local Length1 = utf8_len(String1)
	local Length2 = utf8_len(String2)

	if Length1 == 0 then
		return Length2
	elseif Length2 == 0 then
		return Length1
	end

	local Matrix = {} -- Would love to use table.create for this, but it starts at 0.
	for Index = 0, Length1 do
		Matrix[Index] = {[0] = Index}
	end

	for Index = 0, Length2 do
		Matrix[0][Index] = Index
	end

	local Index = 1
	local IndexSub1

	for _, Code1 in utf8_codes(String1) do
		local Jndex = 1
		local JndexSub1

		for _, Code2 in utf8_codes(String2) do
			local Cost = Code1 == Code2 and 0 or 1
			IndexSub1 = Index - 1
			JndexSub1 = Jndex - 1

			Matrix[Index][Jndex] = math.min(Matrix[IndexSub1][Jndex] + 1, Matrix[Index][JndexSub1] + 1, Matrix[IndexSub1][JndexSub1] + Cost)
			Jndex = Jndex + 1
		end

		Index = Index + 1
	end

	return Matrix[Length1][Length2]
end

local function SortResults(A, B)
	return A.Distance < B.Distance
end

local function FuzzySearch(String, ArrayOfStrings, MaxResults)
	local Results = table.create(#ArrayOfStrings)

	for Index, Value in ipairs(ArrayOfStrings) do
		Results[Index] = {
			String = Value;
			Distance = Levenshtein(String, Value);
		}
	end

	table.sort(Results, SortResults)
	return Results
end

return FuzzySearch
