25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-24 17:29:28 +01:00

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) -> format_error(Reason) ->
yconf: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) -> format_module(Mod) ->
case atom_to_list(Mod) of case Mod of
"Elixir." ++ M -> M; "Elixir." ++ M -> M;
M -> M M -> M
end. end.

View File

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