Improve best match

This commit is contained in:
Evgeny Khramtsov 2019-09-28 11:27:20 +03:00
parent 8f7fa38949
commit c49edaca19
2 changed files with 18 additions and 10 deletions

View File

@ -198,9 +198,11 @@ format_error({mqtt_codec, Reason}) ->
format_error(Reason) ->
yconf:format_error(Reason).
-spec format_module(atom()) -> string().
-spec format_module(atom() | string()) -> string().
format_module(Mod) when is_atom(Mod) ->
format_module(atom_to_list(Mod));
format_module(Mod) ->
case atom_to_list(Mod) of
case Mod of
"Elixir." ++ M -> M;
M -> M
end.

View File

@ -429,19 +429,17 @@ cancel_timer(TRef) when is_reference(TRef) ->
cancel_timer(_) ->
ok.
-spec best_match(atom(), [atom()]) -> atom();
(binary(), [binary()]) -> binary().
-spec best_match(atom() | binary() | string(),
[atom() | binary() | string()]) -> string().
best_match(Pattern, []) ->
Pattern;
best_match(Pattern, Opts) ->
F = if is_atom(Pattern) -> fun atom_to_list/1;
is_binary(Pattern) -> fun binary_to_list/1
end,
String = F(Pattern),
String = to_string(Pattern),
{Ds, _} = lists:mapfoldl(
fun(Opt, Cache) ->
{Distance, Cache1} = ld(String, F(Opt), Cache),
{{Distance, Opt}, Cache1}
SOpt = to_string(Opt),
{Distance, Cache1} = ld(String, SOpt, Cache),
{{Distance, SOpt}, Cache1}
end, #{}, Opts),
element(2, lists:min(Ds)).
@ -670,3 +668,11 @@ ip_to_integer({IP1, IP2, IP3, IP4, IP5, IP6, IP7,
IP8}) ->
IP1 bsl 16 bor IP2 bsl 16 bor IP3 bsl 16 bor IP4 bsl 16
bor IP5 bsl 16 bor IP6 bsl 16 bor IP7 bsl 16 bor IP8.
-spec to_string(atom() | binary() | string()) -> string().
to_string(A) when is_atom(A) ->
atom_to_list(A);
to_string(B) when is_binary(B) ->
binary_to_list(B);
to_string(S) ->
S.