]> granicus.if.org Git - ejabberd/commitdiff
Rename ejabberd_config:similar_option/2 -> misc:best_match/2
authorEvgeny Khramtsov <ekhramtsov@process-one.net>
Tue, 30 Apr 2019 06:36:38 +0000 (09:36 +0300)
committerEvgeny Khramtsov <ekhramtsov@process-one.net>
Tue, 30 Apr 2019 06:36:38 +0000 (09:36 +0300)
src/ejabberd_config.erl
src/gen_mod.erl
src/misc.erl

index 40d157a86b609fbbdf00fcfdecf3c3da8e5e0ea8..f4abf8d5509611732edff337e23859cc5e15c46f 100644 (file)
@@ -39,7 +39,7 @@
         default_queue_type/1, queue_dir/0, fsm_limit_opts/1,
         use_cache/1, cache_size/1, cache_missed/1, cache_life_time/1,
         codec_options/1, get_plain_terms_file/2, negotiation_timeout/0,
-        similar_option/2, get_modules/0]).
+        get_modules/0]).
 
 -export([start/2]).
 
@@ -1102,7 +1102,7 @@ validate_opts(#state{opts = Opts} = State, ModOpts) ->
                                _ ->
                                    KnownOpts = dict:fetch_keys(ModOpts),
                                    ?ERROR_MSG("Unknown option '~s', did you mean '~s'?",
-                                              [Opt, similar_option(Opt, KnownOpts)]),
+                                              [Opt, misc:best_match(Opt, KnownOpts)]),
                                    erlang:error(unknown_option)
                            end
                    end, Opts),
@@ -1126,34 +1126,6 @@ is_file_readable(Path) ->
            false
     end.
 
--spec similar_option(atom(), [atom()]) -> atom().
-similar_option(Pattern, [_|_] = Opts) ->
-    String = atom_to_list(Pattern),
-    {Ds, _} = lists:mapfoldl(
-               fun(Opt, Cache) ->
-                       {Distance, Cache1} = ld(String, atom_to_list(Opt), Cache),
-                       {{Distance, Opt}, Cache1}
-               end, #{}, Opts),
-    element(2, lists:min(Ds)).
-
-%% Levenshtein distance
--spec ld(string(), string(), map()) -> {non_neg_integer(), map()}.
-ld([] = S, T, Cache) ->
-    {length(T), maps:put({S, T}, length(T), Cache)};
-ld(S, [] = T, Cache) ->
-    {length(S), maps:put({S, T}, length(S), Cache)};
-ld([X|S], [X|T], Cache) ->
-    ld(S, T, Cache);
-ld([_|ST] = S, [_|TT] = T, Cache) ->
-    try {maps:get({S, T}, Cache), Cache}
-    catch _:{badkey, _} ->
-            {L1, C1} = ld(S, TT, Cache),
-            {L2, C2} = ld(ST, T, C1),
-            {L3, C3} = ld(ST, TT, C2),
-            L = 1 + lists:min([L1, L2, L3]),
-            {L, maps:put({S, T}, L, C3)}
-    end.
-
 get_version() ->
     case application:get_env(ejabberd, custom_vsn) of
        {ok, Vsn0} when is_list(Vsn0) ->
index eb433de54a5930be37b0ee248f0fee692ee6d335..b972285f5719dae3ec18a698def4f3aa7d504c72 100644 (file)
@@ -585,7 +585,7 @@ validate_opts(Host, Module, Opts0) ->
                                   " did you mean '~s'?"
                                   " Available options are: ~s",
                                   [Opt, Module,
-                                   ejabberd_config:similar_option(Opt, KnownOpts),
+                                   misc:best_match(Opt, KnownOpts),
                                    misc:join_atoms(KnownOpts, <<", ">>)]),
            module_error(ErrTxt)
     end.
@@ -746,7 +746,7 @@ format_module_error(Module, Fun, Arity, Opts, Class, Reason, St) ->
                          "exists inside either ~s or ~s "
                          "directory",
                          [Fun, Module,
-                          ejabberd_config:similar_option(
+                          misc:best_match(
                             Module, ejabberd_config:get_modules()),
                           Module,
                           filename:dirname(code:which(?MODULE)),
index 73d05ff24fa112583336d44156bf24e5a3d17cc0..4f683a431216698e6daf9b0fe91c5b6bd68d873d 100644 (file)
@@ -39,7 +39,7 @@
         css_dir/0, img_dir/0, js_dir/0, msgs_dir/0, sql_dir/0, lua_dir/0,
         read_css/1, read_img/1, read_js/1, read_lua/1, try_url/1,
         intersection/2, format_val/1, cancel_timer/1, unique_timestamp/0,
-        is_mucsub_message/1]).
+        is_mucsub_message/1, best_match/2]).
 
 %% Deprecated functions
 -export([decode_base64/1, encode_base64/1]).
@@ -425,6 +425,18 @@ cancel_timer(TRef) when is_reference(TRef) ->
 cancel_timer(_) ->
     ok.
 
+-spec best_match(atom(), [atom()]) -> atom().
+best_match(Pattern, []) ->
+    Pattern;
+best_match(Pattern, Opts) ->
+    String = atom_to_list(Pattern),
+    {Ds, _} = lists:mapfoldl(
+               fun(Opt, Cache) ->
+                       {Distance, Cache1} = ld(String, atom_to_list(Opt), Cache),
+                       {{Distance, Opt}, Cache1}
+               end, #{}, Opts),
+    element(2, lists:min(Ds)).
+
 %%%===================================================================
 %%% Internal functions
 %%%===================================================================
@@ -485,3 +497,21 @@ get_dir(Type) ->
 unique_timestamp() ->
     {MS, S, _} = erlang:timestamp(),
     {MS, S, erlang:unique_integer([positive, monotonic]) rem 1000000}.
+
+%% Levenshtein distance
+-spec ld(string(), string(), map()) -> {non_neg_integer(), map()}.
+ld([] = S, T, Cache) ->
+    {length(T), maps:put({S, T}, length(T), Cache)};
+ld(S, [] = T, Cache) ->
+    {length(S), maps:put({S, T}, length(S), Cache)};
+ld([X|S], [X|T], Cache) ->
+    ld(S, T, Cache);
+ld([_|ST] = S, [_|TT] = T, Cache) ->
+    try {maps:get({S, T}, Cache), Cache}
+    catch _:{badkey, _} ->
+            {L1, C1} = ld(S, TT, Cache),
+            {L2, C2} = ld(ST, T, C1),
+            {L3, C3} = ld(ST, TT, C2),
+            L = 1 + lists:min([L1, L2, L3]),
+            {L, maps:put({S, T}, L, C3)}
+    end.