]> granicus.if.org Git - ejabberd/commitdiff
Improve best match
authorEvgeny Khramtsov <ekhramtsov@process-one.net>
Sat, 28 Sep 2019 08:27:20 +0000 (11:27 +0300)
committerEvgeny Khramtsov <ekhramtsov@process-one.net>
Sat, 28 Sep 2019 08:27:20 +0000 (11:27 +0300)
src/econf.erl
src/misc.erl

index 372a8563d0381255f28d7f4bdd240b9cb88a97d5..fdb80758823ef3cc2aa41eeda0783f16fd02c803 100644 (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.
index 9d9335fa28bec6321f90e64a7b94a571343ba14b..e29f73fc1380379a36d5d336b0a139ac3e8e6729 100644 (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.