Home
Random
Recent changes
Special pages
Community portal
Preferences
About Stockhub
Disclaimers
Search
User menu
Talk
Contributions
Create account
Log in
Editing
Module:Sandbox/trappist the monk/strcmp
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
--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 <limit> 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, '<br />'); end return {strcmp = strcmp}
Summary:
Please note that all contributions to Stockhub may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Stockhub:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Template used on this page:
Module:Sandbox/trappist the monk/strcmp/doc
(
edit
)