Module:Sandbox/trappist the monk/strcmp

--local function strcmp (str_a, str_b) local function strcmp (frame) local str_a = frame.args[1]; local str_b = frame.args[2];

str_a = str_a:gsub ('(%-%-templatestyles%-)%x+(%-)', '%100000000%2') str_b = str_b:gsub ('(%-%-templatestyles%-)%x+(%-)', '%100000000%2')

local result = {}; local len_a = str_a:len; local len_b = str_b:len; local t_str_a = {} local t_str_b = {} local limit = len_a;														-- assume that both strings are same length

if len_a == len_b then if str_a == str_b then return 'equal'; end else table.insert (result, 'length mismatch: a: ' .. len_a .. '; b: ' .. len_b); if len_a < len_b then limit = len_a; else limit = len_b; end end

for c in string.gmatch (str_a, '.') do		table.insert (t_str_a, c); end for c in str_b:gmatch ('.') do		table.insert (t_str_b, c); end local i = 1; while i < limit do		if t_str_a[i] == t_str_b[i] then i = i + 1;															-- position of last equal character else break; end end

if len_a ~= len_b then														-- if length mismatch if not t_str_a[i] and t_str_b[i] or 									-- if one but not both are nil t_str_a[i] and not t_str_b[i] then i = i + 1;														-- point to last matching character end end

if limit == i then															-- if we tested number of characters table.insert (result, 'strings eq to position: ' .. i); else table.insert (result, 'strings eq to position: ' .. i .. ': ' .. string.byte (t_str_a[i]) .. '(' .. t_str_a[i] .. ') ≠ ' .. string.byte (t_str_b[i]) .. '(' .. t_str_b[i] .. ')')	end return table.concat (result, ' '); end

return {strcmp = strcmp}