Skip to content

Commit

Permalink
Merge pull request #10 from esl/remove_nif
Browse files Browse the repository at this point in the history
Remove nif
  • Loading branch information
DenysGonchar authored Jun 1, 2024
2 parents 3d70277 + 80c1dcc commit 14c5242
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 262 deletions.
15 changes: 6 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ jobs:
name: OTP ${{matrix.otp_vsn}}
strategy:
matrix:
otp_vsn: ['26.2', '25.3', '24.3']
rebar_vsn: ['3.22.0']
otp_vsn: ['27', '26', '25']
rebar_vsn: ['3.23.0']
runs-on: 'ubuntu-22.04'
steps:
- uses: actions/checkout@v4
Expand All @@ -26,16 +26,13 @@ jobs:
- run: rebar3 as test compile
- run: rebar3 ct
- run: rebar3 ex_doc
if: ${{ matrix.otp == '26.2' }}
if: ${{ matrix.otp == '27' }}
- run: rebar3 dialyzer
if: ${{ matrix.otp == '26.2' }}
if: ${{ matrix.otp == '27' }}
- run: rebar3 as test codecov analyze
if: ${{ matrix.otp == '26.2' }}
- run: gcov -o c_src mongoose_jid
if: ${{ matrix.otp == '26.2' }}
if: ${{ matrix.otp == '27' }}
- run: rebar3 as test codecov analyze
- uses: codecov/codecov-action@v3
- uses: codecov/codecov-action@v4
with:
name: Upload coverage reports to Codecov
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
73 changes: 0 additions & 73 deletions c_src/mongoose_jid.c

This file was deleted.

31 changes: 2 additions & 29 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,19 @@
]}.

{project_plugins, [
pc,
rebar3_hex,
rebar3_ex_doc
]}.

{artifacts, [
"priv/jid.so"
]}.

{port_specs, [
{
".*",
"priv/jid.so",
["c_src/*.c"],
[{env, [{"CFLAGS", "$CFLAGS -std=c99 -O3 -Wall -Wextra -fPIC"}]}]
}
]}.

{provider_hooks, [
{post, [
{compile, {pc, compile}},
{clean, {pc, clean}}
]}
]}.

{profiles, [
{test, [
{cover_enabled, true},
{cover_export_enabled, true},
{deps, [{proper, "1.4.0"}]},
{plugins, [{rebar3_codecov, "0.6.0"}]},
{port_env,
[{"CFLAGS", "$CFLAGS -std=c99 -O2 -g -Wall -Wextra -fPIC --coverage"},
{"LDFLAGS", "$LDFLAGS --coverage"}
]}
{plugins, [{rebar3_codecov, "0.6.0"}]}
]},
{prod, [
{erl_opts, [deterministic]},
{port_env,
[{"CFLAGS", "$CFLAGS -std=c99 -O3 -Wall -Wextra -fPIC"}]}
{erl_opts, [deterministic]}
]}
]}.

Expand Down
63 changes: 36 additions & 27 deletions src/jid.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
-module(jid).
-on_load(load/0).

-export([make/3, make/1, make_bare/2, make_noprep/3, make_noprep/1]).
-export([from_binary/1, from_binary_noprep/1]).
Expand Down Expand Up @@ -109,31 +108,58 @@ are_bare_equal(_, _) ->
%% @doc Parses a binary and returns a jid record or an error
-spec from_binary(binary()) -> jid() | error.
from_binary(J) when is_binary(J), byte_size(J) < ?XMPP_JID_SIZE_LIMIT ->
case from_binary_nif(J) of
case binary_to_jid1(J, J, 0) of
{U, H, R} -> make(U, H, R);
error -> error
end;
from_binary(_) ->
error.

binary_to_jid1(_, <<$@, _J/binary>>, 0) ->
error;
binary_to_jid1(Jid, <<$@, J/binary>>, N) ->
binary_to_jid2(Jid, J, N, 0);
binary_to_jid1(_, <<$/, _J/binary>>, 0) ->
error;
binary_to_jid1(Jid, <<$/, _/binary>>, N) ->
{<<>>,
erlang:binary_part(Jid, {0, N}),
erlang:binary_part(Jid, {N + 1, byte_size(Jid) - N - 1})};
binary_to_jid1(Jid, <<_C, J/binary>>, N) ->
binary_to_jid1(Jid, J, N + 1);
binary_to_jid1(_, <<>>, 0) ->
error;
binary_to_jid1(J, <<>>, _) ->
{<<>>, J, <<>>}.

binary_to_jid2(_, <<$@, _J/binary>>, _N, _S) ->
error;
binary_to_jid2(_, <<$/, _J/binary>>, _N, 0) ->
error;
binary_to_jid2(Jid, <<$/, _/binary>>, N, S) ->
{erlang:binary_part(Jid, {0, N}),
erlang:binary_part(Jid, {N + 1, S}),
erlang:binary_part(Jid, {N + S + 2, byte_size(Jid) - N - S - 2})};
binary_to_jid2(Jid, <<_C, J/binary>>, N, S) ->
binary_to_jid2(Jid, J, N, S + 1);
binary_to_jid2(_, <<>>, _N, 0) ->
error;
binary_to_jid2(Jid, <<>>, N, S) ->
{erlang:binary_part(Jid, {0, N}),
erlang:binary_part(Jid, {N + 1, S}),
<<>>}.

%% @doc Parses a binary and returns a jid record or an error, but without normalisation of the parts
-spec from_binary_noprep(binary()) -> jid() | error.
from_binary_noprep(J) when is_binary(J), byte_size(J) < ?XMPP_JID_SIZE_LIMIT ->
case from_binary_nif(J) of
case binary_to_jid1(J, J, 0) of
{U, S, R} ->
#jid{luser = U, lserver = S, lresource = R};
error -> error
end;
from_binary_noprep(_) ->
error.

%% Original Erlang equivalent can be found in test/jid_SUITE.erl,
%% together with `proper` generators to check for equivalence
%% @private
-spec from_binary_nif(binary()) -> simple_jid() | error.
from_binary_nif(_) ->
erlang:nif_error(not_loaded).

%% @doc Takes a representation of a jid, and outputs such jid as a literal binary
-spec to_binary(simple_jid() | simple_bare_jid() | jid() | literal_jid()) -> binary().
to_binary({<<>>, Server, <<>>}) ->
Expand Down Expand Up @@ -291,20 +317,3 @@ binary_to_bare(JID) when is_binary(JID) ->
-spec str_tolower(iodata()) -> binary() | error.
str_tolower(Val) when is_binary(Val); is_list(Val) ->
stringprep:tolower(Val).

%%%===================================================================
%%% Load NIF
%%%===================================================================

-dialyzer({nowarn_function, [load/0]}).
-spec load() -> ok | {error, term()}.
load() ->
PrivDir = case code:priv_dir(?MODULE) of
{error, _} ->
EbinDir = filename:dirname(code:which(?MODULE)),
AppPath = filename:dirname(EbinDir),
filename:join(AppPath, "priv");
Path ->
Path
end,
erlang:load_nif(filename:join(PrivDir, ?MODULE_STRING), none).
Loading

0 comments on commit 14c5242

Please sign in to comment.