aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEvgeny Khramtsov <ekhramtsov@process-one.net>2019-09-28 11:27:20 +0300
committerEvgeny Khramtsov <ekhramtsov@process-one.net>2019-09-28 11:27:20 +0300
commitc49edaca1964cf525db1c4dac9f6aacc1bc39014 (patch)
treeb03cfc539d6a2f2db3f42ca500aea6c1e1076f91 /src
parentSupport OAUTH client authentication (diff)
Improve best match
Diffstat (limited to 'src')
-rw-r--r--src/econf.erl6
-rw-r--r--src/misc.erl22
2 files changed, 18 insertions, 10 deletions
diff --git a/src/econf.erl b/src/econf.erl
index 372a8563d..fdb807588 100644
--- a/src/econf.erl
+++ b/src/econf.erl
@@ -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.
diff --git a/src/misc.erl b/src/misc.erl
index 9d9335fa2..e29f73fc1 100644
--- a/src/misc.erl
+++ b/src/misc.erl
@@ -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.