refactor: rename functions to please elvis

This commit is contained in:
Zaiming (Stone) Shi 2022-04-29 10:52:08 +02:00
parent edc6a6ab38
commit 6b44d96c0d
4 changed files with 16 additions and 17 deletions

View File

@ -145,10 +145,10 @@ npid() ->
NPid.
to_hexstr(I) when byte_size(I) =:= 16 ->
emqx_misc:bin2hexstr_A_F(I).
emqx_misc:bin_to_hexstr(I, upper).
from_hexstr(S) when byte_size(S) =:= 32 ->
emqx_misc:hexstr2bin(S).
emqx_misc:hexstr_to_bin(S).
to_base62(<<I:128>>) ->
emqx_base62:encode(I).

View File

@ -53,9 +53,8 @@
]).
-export([
bin2hexstr_A_F/1,
bin2hexstr_a_f/1,
hexstr2bin/1
bin_to_hexstr/2,
hexstr_to_bin/1
]).
-export([clamp/3]).
@ -304,20 +303,18 @@ index_of(E, I, [E | _]) ->
index_of(E, I, [_ | L]) ->
index_of(E, I + 1, L).
-spec bin2hexstr_A_F(binary()) -> binary().
bin2hexstr_A_F(B) when is_binary(B) ->
<<<<(int2hexchar(H, upper)), (int2hexchar(L, upper))>> || <<H:4, L:4>> <= B>>.
-spec bin2hexstr_a_f(binary()) -> binary().
bin2hexstr_a_f(B) when is_binary(B) ->
-spec bin_to_hexstr(binary(), lower | upper) -> binary().
bin_to_hexstr(B, upper) when is_binary(B) ->
<<<<(int2hexchar(H, upper)), (int2hexchar(L, upper))>> || <<H:4, L:4>> <= B>>;
bin_to_hexstr(B, lower) when is_binary(B) ->
<<<<(int2hexchar(H, lower)), (int2hexchar(L, lower))>> || <<H:4, L:4>> <= B>>.
int2hexchar(I, _) when I >= 0 andalso I < 10 -> I + $0;
int2hexchar(I, upper) -> I - 10 + $A;
int2hexchar(I, lower) -> I - 10 + $a.
-spec hexstr2bin(binary()) -> binary().
hexstr2bin(B) when is_binary(B) ->
-spec hexstr_to_bin(binary()) -> binary().
hexstr_to_bin(B) when is_binary(B) ->
<<<<(hexchar2int(H) * 16 + hexchar2int(L))>> || <<H:8, L:8>> <= B>>.
hexchar2int(I) when I >= $0 andalso I =< $9 -> I - $0;

View File

@ -190,7 +190,7 @@ handler_id(Name, Type) ->
do_handler_id(Name, Type)
catch
_:_ ->
Hash = emqx_misc:bin2hexstr_a_f(crypto:hash(md5, Name)),
Hash = emqx_misc:bin_to_hexstr(crypto:hash(md5, Name), lower),
do_handler_id(Hash, Type)
end.

View File

@ -20,6 +20,8 @@
-include_lib("emqx/include/emqx.hrl").
-include_lib("emqx/include/logger.hrl").
-elvis([{elvis_style, god_modules, disable}]).
%% IoT Funcs
-export([
msgid/0,
@ -617,10 +619,10 @@ map(Data) ->
emqx_plugin_libs_rule:map(Data).
bin2hexstr(Bin) when is_binary(Bin) ->
emqx_misc:bin2hexstr_A_F(Bin).
emqx_misc:bin_to_hexstr(Bin, upper).
hexstr2bin(Str) when is_binary(Str) ->
emqx_misc:hexstr2bin(Str).
emqx_misc:hexstr_to_bin(Str).
%%------------------------------------------------------------------------------
%% NULL Funcs
@ -885,7 +887,7 @@ sha256(S) when is_binary(S) ->
hash(sha256, S).
hash(Type, Data) ->
emqx_misc:bin2hexstr_a_f(crypto:hash(Type, Data)).
emqx_misc:bin_to_hexstr(crypto:hash(Type, Data), lower).
%%------------------------------------------------------------------------------
%% Data encode and decode Funcs