Skip to content

Instantly share code, notes, and snippets.

@twonds
Created April 9, 2014 16:49
Show Gist options
  • Save twonds/10291023 to your computer and use it in GitHub Desktop.
Save twonds/10291023 to your computer and use it in GitHub Desktop.
-module(same_string).
-export([test/0,
is_re_message_the_same/2,
is_message_the_same/2]).
%% XXX - make sure this works with unicode
non_word() ->
[$\n, $\r, $\t,$!,$",$#,$$,$%,$&,$',$(,
$),$*, $+,$,, $\\, $-,
$., $/, $:,$;,$<,$=,$>,$?,$@,$[,$],$^,
$_,$`,${,$|,$},$~].
is_re_message_the_same(Msg, Msg2) ->
A = re:replace(Msg,"([[:punct:]]+|\\s+)", "", [{return, binary}, global, unicode]),
B = re:replace(Msg2,"([[:punct:]]+|\\s+)", "", [{return, binary}, global, unicode]),
nomatch =/= re:run(A, <<"^", B/binary,"$">>, [caseless, unicode]).
is_message_the_same(Msg1, Msg2) ->
NewMsg1 = replace(Msg1),
NewMsg2 = replace(Msg2),
string:equal(NewMsg1, NewMsg2).
replace(Str) ->
string:to_lower(string:join(string:tokens(Str, non_word() ++" "), "")).
test() ->
TestCount = 100000,
Val1 = timer:tc(fun () ->
lists:foreach(fun(_Word) ->
is_message_the_same("test", "Test")
end,
lists:seq(1, TestCount))
end),
io:format("First test ~p ~n", [Val1]),
Val2 = timer:tc(fun () ->
lists:foreach(fun(_Word) ->
is_re_message_the_same("test", "Test")
end,
lists:seq(1, TestCount))
end),
io:format("Second test ~p ~n", [Val2]),
ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment