Skip to content

Commit d21aca7

Browse files
authored
Merge pull request #12 from esl/updates
Updates
2 parents 398e31a + 65e077a commit d21aca7

File tree

4 files changed

+175
-44
lines changed

4 files changed

+175
-44
lines changed

rebar.config

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
]}]}.
1010

1111
{deps, [
12-
{stringprep, "1.0.29"}
12+
{stringprep, "1.0.31"}
1313
]}.
1414

1515
{project_plugins, [
@@ -22,7 +22,7 @@
2222
{cover_enabled, true},
2323
{cover_export_enabled, true},
2424
{deps, [{proper, "1.4.0"}]},
25-
{plugins, [{rebar3_codecov, "0.6.0"}]}
25+
{plugins, [{rebar3_codecov, "0.7.0"}]}
2626
]},
2727
{prod, [
2828
{erl_opts, [deterministic]}

rebar.lock

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{"1.2.0",
2-
[{<<"p1_utils">>,{pkg,<<"p1_utils">>,<<"1.0.23">>},1},
3-
{<<"stringprep">>,{pkg,<<"stringprep">>,<<"1.0.27">>},0}]}.
2+
[{<<"p1_utils">>,{pkg,<<"p1_utils">>,<<"1.0.26">>},1},
3+
{<<"stringprep">>,{pkg,<<"stringprep">>,<<"1.0.31">>},0}]}.
44
[
55
{pkg_hash,[
6-
{<<"p1_utils">>, <<"7F94466ADA69BD982EA7BB80FBCA18E7053E7D0B82C9D9E37621FA508587069B">>},
7-
{<<"stringprep">>, <<"02808C7024BC6285CA6A8A67E7ADDFC16F35DDA55551A582C5181D8EA960E890">>}]},
6+
{<<"p1_utils">>, <<"67B0C4AC9FA3BA3EF563B31AA111B0A004439A37FAC85E027F1C3617E1C7EC6C">>},
7+
{<<"stringprep">>, <<"FA1688C156DD271722AA18C423A4163E710D2F4F475AD0BC220910DF669B53AF">>}]},
88
{pkg_hash_ext,[
9-
{<<"p1_utils">>, <<"47F21618694EEEE5006AF1C88731AD86B757161E7823C29B6F73921B571C8502">>},
10-
{<<"stringprep">>, <<"A5967B1144CA8002A58A03D16DD109FBD0BCDB82616CEAD2F983944314AF6A00">>}]}
9+
{<<"p1_utils">>, <<"D0379E8C1156B98BD64F8129C1DE022FCCA4F2FDB7486CE73BF0ED2C3376B04C">>},
10+
{<<"stringprep">>, <<"E9699C88E8DB16B3A41F0E45AC6874A4DA81A6E4854A77D76EDE6D09B08E3530">>}]}
1111
].

src/jid.erl

+113-34
Original file line numberDiff line numberDiff line change
@@ -120,40 +120,6 @@ from_binary(J) when is_binary(J), byte_size(J) < ?XMPP_JID_SIZE_LIMIT ->
120120
from_binary(_) ->
121121
error.
122122

123-
binary_to_jid1(_, <<$@, _J/binary>>, 0) ->
124-
error;
125-
binary_to_jid1(Jid, <<$@, J/binary>>, N) ->
126-
binary_to_jid2(Jid, J, N, 0);
127-
binary_to_jid1(_, <<$/, _J/binary>>, 0) ->
128-
error;
129-
binary_to_jid1(Jid, <<$/, _/binary>>, N) ->
130-
{<<>>,
131-
erlang:binary_part(Jid, {0, N}),
132-
erlang:binary_part(Jid, {N + 1, byte_size(Jid) - N - 1})};
133-
binary_to_jid1(Jid, <<_C, J/binary>>, N) ->
134-
binary_to_jid1(Jid, J, N + 1);
135-
binary_to_jid1(_, <<>>, 0) ->
136-
error;
137-
binary_to_jid1(J, <<>>, _) ->
138-
{<<>>, J, <<>>}.
139-
140-
binary_to_jid2(_, <<$@, _J/binary>>, _N, _S) ->
141-
error;
142-
binary_to_jid2(_, <<$/, _J/binary>>, _N, 0) ->
143-
error;
144-
binary_to_jid2(Jid, <<$/, _/binary>>, N, S) ->
145-
{erlang:binary_part(Jid, {0, N}),
146-
erlang:binary_part(Jid, {N + 1, S}),
147-
erlang:binary_part(Jid, {N + S + 2, byte_size(Jid) - N - S - 2})};
148-
binary_to_jid2(Jid, <<_C, J/binary>>, N, S) ->
149-
binary_to_jid2(Jid, J, N, S + 1);
150-
binary_to_jid2(_, <<>>, _N, 0) ->
151-
error;
152-
binary_to_jid2(Jid, <<>>, N, S) ->
153-
{erlang:binary_part(Jid, {0, N}),
154-
erlang:binary_part(Jid, {N + 1, S}),
155-
<<>>}.
156-
157123
%% @doc Parses a binary and returns a jid record or an error, but without normalisation of the parts
158124
-spec from_binary_noprep(binary()) -> jid() | error.
159125
from_binary_noprep(J) when is_binary(J), byte_size(J) < ?XMPP_JID_SIZE_LIMIT ->
@@ -338,3 +304,116 @@ binary_to_bare(JID) when is_binary(JID) ->
338304
-spec str_tolower(iodata()) -> binary() | error.
339305
str_tolower(Val) when is_binary(Val); is_list(Val) ->
340306
stringprep:tolower(Val).
307+
308+
%%%%% Helpers
309+
binary_to_jid1(_, <<>>, 0) ->
310+
error;
311+
binary_to_jid1(J, <<>>, _) ->
312+
{<<>>, J, <<>>};
313+
binary_to_jid1(_, <<$@, _J/binary>>, 0) ->
314+
error;
315+
binary_to_jid1(Jid, <<$@, J/binary>>, N) ->
316+
binary_to_jid2(Jid, J, N, 0);
317+
binary_to_jid1(Jid, <<_, $@, J/binary>>, N) ->
318+
binary_to_jid2(Jid, J, N + 1, 0);
319+
binary_to_jid1(Jid, <<_, _, $@, J/binary>>, N) ->
320+
binary_to_jid2(Jid, J, N + 2, 0);
321+
binary_to_jid1(Jid, <<_, _, _, $@, J/binary>>, N) ->
322+
binary_to_jid2(Jid, J, N + 3, 0);
323+
binary_to_jid1(Jid, <<_, _, _, _, $@, J/binary>>, N) ->
324+
binary_to_jid2(Jid, J, N + 4, 0);
325+
binary_to_jid1(Jid, <<_, _, _, _, _, $@, J/binary>>, N) ->
326+
binary_to_jid2(Jid, J, N + 5, 0);
327+
binary_to_jid1(Jid, <<_, _, _, _, _, _, $@, J/binary>>, N) ->
328+
binary_to_jid2(Jid, J, N + 6, 0);
329+
binary_to_jid1(Jid, <<_, _, _, _, _, _, _, $@, J/binary>>, N) ->
330+
binary_to_jid2(Jid, J, N + 7, 0);
331+
binary_to_jid1(Jid, <<_, _, _, _, _, _, _, _, $@, J/binary>>, N) ->
332+
binary_to_jid2(Jid, J, N + 8, 0);
333+
binary_to_jid1(_, <<$/, _J/binary>>, 0) ->
334+
error;
335+
binary_to_jid1(Jid, <<$/, _/binary>>, N) ->
336+
{<<>>,
337+
erlang:binary_part(Jid, {0, N}),
338+
erlang:binary_part(Jid, {N + 1, byte_size(Jid) - N - 1})};
339+
binary_to_jid1(Jid, <<_, $/, _/binary>>, N) ->
340+
{<<>>,
341+
erlang:binary_part(Jid, {0, N + 1}),
342+
erlang:binary_part(Jid, {N + 2, byte_size(Jid) - N - 2})};
343+
binary_to_jid1(Jid, <<_, _, $/, _/binary>>, N) ->
344+
{<<>>,
345+
erlang:binary_part(Jid, {0, N + 2}),
346+
erlang:binary_part(Jid, {N + 3, byte_size(Jid) - N - 3})};
347+
binary_to_jid1(Jid, <<_, _, _, $/, _/binary>>, N) ->
348+
{<<>>,
349+
erlang:binary_part(Jid, {0, N + 3}),
350+
erlang:binary_part(Jid, {N + 4, byte_size(Jid) - N - 4})};
351+
binary_to_jid1(Jid, <<_, _, _, _, $/, _/binary>>, N) ->
352+
{<<>>,
353+
erlang:binary_part(Jid, {0, N + 4}),
354+
erlang:binary_part(Jid, {N + 5, byte_size(Jid) - N - 5})};
355+
binary_to_jid1(Jid, <<_, _, _, _, _, $/, _/binary>>, N) ->
356+
{<<>>,
357+
erlang:binary_part(Jid, {0, N + 5}),
358+
erlang:binary_part(Jid, {N + 6, byte_size(Jid) - N - 6})};
359+
binary_to_jid1(Jid, <<_, _, _, _, _, _, $/, _/binary>>, N) ->
360+
{<<>>,
361+
erlang:binary_part(Jid, {0, N + 6}),
362+
erlang:binary_part(Jid, {N + 7, byte_size(Jid) - N - 7})};
363+
binary_to_jid1(Jid, <<_, _, _, _, _, _, _, $/, _/binary>>, N) ->
364+
{<<>>,
365+
erlang:binary_part(Jid, {0, N + 7}),
366+
erlang:binary_part(Jid, {N + 8, byte_size(Jid) - N - 8})};
367+
binary_to_jid1(Jid, <<_, _, _, _, _, _, _, _, $/, _/binary>>, N) ->
368+
{<<>>,
369+
erlang:binary_part(Jid, {0, N + 8}),
370+
erlang:binary_part(Jid, {N + 9, byte_size(Jid) - N - 9})};
371+
binary_to_jid1(Jid, <<_, _, _, _, _, _, _, _, J/binary>>, N) ->
372+
binary_to_jid1(Jid, J, N + 8);
373+
binary_to_jid1(Jid, <<_C, J/binary>>, N) ->
374+
binary_to_jid1(Jid, J, N + 1).
375+
376+
binary_to_jid2(_, <<>>, _N, 0) ->
377+
error;
378+
binary_to_jid2(Jid, <<>>, N, S) ->
379+
{erlang:binary_part(Jid, {0, N}),
380+
erlang:binary_part(Jid, {N + 1, S}),
381+
<<>>};
382+
binary_to_jid2(_, <<$/, _J/binary>>, _N, 0) ->
383+
error;
384+
binary_to_jid2(Jid, <<$/, _/binary>>, N, S) ->
385+
{erlang:binary_part(Jid, {0, N}),
386+
erlang:binary_part(Jid, {N + 1, S}),
387+
erlang:binary_part(Jid, {N + S + 2, byte_size(Jid) - N - S - 2})};
388+
binary_to_jid2(Jid, <<_, "/", _/binary>>, N, S) ->
389+
{erlang:binary_part(Jid, {0, N}),
390+
erlang:binary_part(Jid, {N + 1, S + 1}),
391+
erlang:binary_part(Jid, {N + S + 3, byte_size(Jid) - N - S - 3})};
392+
binary_to_jid2(Jid, <<_, _, "/", _/binary>>, N, S) ->
393+
{erlang:binary_part(Jid, {0, N}),
394+
erlang:binary_part(Jid, {N + 1, S + 2}),
395+
erlang:binary_part(Jid, {N + S + 4, byte_size(Jid) - N - S - 4})};
396+
binary_to_jid2(Jid, <<_, _, _, "/", _/binary>>, N, S) ->
397+
{erlang:binary_part(Jid, {0, N}),
398+
erlang:binary_part(Jid, {N + 1, S + 3}),
399+
erlang:binary_part(Jid, {N + S + 5, byte_size(Jid) - N - S - 5})};
400+
binary_to_jid2(Jid, <<_, _, _, _, "/", _/binary>>, N, S) ->
401+
{erlang:binary_part(Jid, {0, N}),
402+
erlang:binary_part(Jid, {N + 1, S + 4}),
403+
erlang:binary_part(Jid, {N + S + 6, byte_size(Jid) - N - S - 6})};
404+
binary_to_jid2(Jid, <<_, _, _, _, _, "/", _/binary>>, N, S) ->
405+
{erlang:binary_part(Jid, {0, N}),
406+
erlang:binary_part(Jid, {N + 1, S + 5}),
407+
erlang:binary_part(Jid, {N + S + 7, byte_size(Jid) - N - S - 7})};
408+
binary_to_jid2(Jid, <<_, _, _, _, _, _, "/", _/binary>>, N, S) ->
409+
{erlang:binary_part(Jid, {0, N}),
410+
erlang:binary_part(Jid, {N + 1, S + 6}),
411+
erlang:binary_part(Jid, {N + S + 8, byte_size(Jid) - N - S - 8})};
412+
binary_to_jid2(Jid, <<_, _, _, _, _, _, _, "/", _/binary>>, N, S) ->
413+
{erlang:binary_part(Jid, {0, N}),
414+
erlang:binary_part(Jid, {N + 1, S + 7}),
415+
erlang:binary_part(Jid, {N + S + 9, byte_size(Jid) - N - S - 9})};
416+
binary_to_jid2(Jid, <<_, _, _, _, _, _, _, _, J/binary>>, N, S) ->
417+
binary_to_jid2(Jid, J, N, S + 8);
418+
binary_to_jid2(Jid, <<_C, J/binary>>, N, S) ->
419+
binary_to_jid2(Jid, J, N, S + 1).

test/jid_SUITE.erl

+54-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ groups() ->
5050
make_empty_domain,
5151
make_bare_empty_domain,
5252
make_noprep_empty_domain,
53-
getters_equal_struct_lookup
53+
getters_equal_struct_lookup,
54+
equivalent_good,
55+
equivalent_bad
5456
]}
5557
].
5658

@@ -61,7 +63,6 @@ init_per_suite(C) ->
6163
end_per_suite(C) ->
6264
C.
6365

64-
6566
empty_server_fails(_C) ->
6667
?assertEqual(error, jid:from_binary(<<"$@/">>)).
6768

@@ -312,6 +313,14 @@ getters_equal_struct_lookup(_) ->
312313
?assertEqual(jid:lserver(US), Jid#jid.lserver),
313314
?assertEqual(jid:lresource(US), <<>>).
314315

316+
equivalent_good(_C) ->
317+
Prop = ?FORALL(Jid, jid_gen:jid(), from_binary(Jid) =:= jid:from_binary(Jid)),
318+
run_property(Prop, 500, 1, 100).
319+
320+
equivalent_bad(_C) ->
321+
Prop = ?FORALL(Jid, jid_gen:invalid_jid(), from_binary(Jid) =:= jid:from_binary(Jid)),
322+
run_property(Prop, 500, 1, 100).
323+
315324
%% HELPERS
316325
run_property(Prop, NumTest, StartSize, StopSize) ->
317326
Res = proper:quickcheck(Prop, [verbose, long_result,
@@ -320,3 +329,46 @@ run_property(Prop, NumTest, StartSize, StopSize) ->
320329
{max_size, StopSize}]),
321330
ct:pal("Result of the property is ~p~n", [Res]),
322331
?assert(Res).
332+
333+
%% Original code
334+
from_binary(J) when is_binary(J), byte_size(J) < 3100 ->
335+
case binary_to_jid1(J, J, 0) of
336+
{U, H, R} -> jid:make(U, H, R);
337+
error -> error
338+
end;
339+
from_binary(_) ->
340+
error.
341+
342+
binary_to_jid1(_, <<$@, _J/binary>>, 0) ->
343+
error;
344+
binary_to_jid1(Jid, <<$@, J/binary>>, N) ->
345+
binary_to_jid2(Jid, J, N, 0);
346+
binary_to_jid1(_, <<$/, _J/binary>>, 0) ->
347+
error;
348+
binary_to_jid1(Jid, <<$/, _/binary>>, N) ->
349+
{<<>>,
350+
erlang:binary_part(Jid, {0, N}),
351+
erlang:binary_part(Jid, {N + 1, byte_size(Jid) - N - 1})};
352+
binary_to_jid1(Jid, <<_C, J/binary>>, N) ->
353+
binary_to_jid1(Jid, J, N + 1);
354+
binary_to_jid1(_, <<>>, 0) ->
355+
error;
356+
binary_to_jid1(J, <<>>, _) ->
357+
{<<>>, J, <<>>}.
358+
359+
binary_to_jid2(_, <<$@, _J/binary>>, _N, _S) ->
360+
error;
361+
binary_to_jid2(_, <<$/, _J/binary>>, _N, 0) ->
362+
error;
363+
binary_to_jid2(Jid, <<$/, _/binary>>, N, S) ->
364+
{erlang:binary_part(Jid, {0, N}),
365+
erlang:binary_part(Jid, {N + 1, S}),
366+
erlang:binary_part(Jid, {N + S + 2, byte_size(Jid) - N - S - 2})};
367+
binary_to_jid2(Jid, <<_C, J/binary>>, N, S) ->
368+
binary_to_jid2(Jid, J, N, S + 1);
369+
binary_to_jid2(_, <<>>, _N, 0) ->
370+
error;
371+
binary_to_jid2(Jid, <<>>, N, S) ->
372+
{erlang:binary_part(Jid, {0, N}),
373+
erlang:binary_part(Jid, {N + 1, S}),
374+
<<>>}.

0 commit comments

Comments
 (0)