Skip to content

Commit 55aad68

Browse files
author
José Valim
committed
Splice nested binaries with default types and size
1 parent 3b4954b commit 55aad68

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

lib/elixir/src/elixir_literal.erl

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,16 @@ build_bitstr_each(Fun, [H|T], Meta, S, Acc) ->
9494

9595
build_bitstr_each(Fun, T, Meta, S, Acc, H, Size, Types) ->
9696
{ Expr, NS } = Fun(H, S),
97-
case (is_default_or_utf(Types) andalso Expr) of
98-
{ bin, _,[{ bin_element, 0, { string, 0, String }, default, default }] } ->
97+
98+
AllowString = types_allow_string(Types),
99+
AllowSplice = types_allow_splice(Types),
100+
AllowAny = (AllowString orelse AllowSplice) andalso (Size == default),
101+
102+
case AllowAny andalso Expr of
103+
{ bin, _, [{ bin_element, 0, { string, 0, String }, default, default }] } when AllowString ->
99104
build_bitstr_each(Fun, T, Meta, NS, [{ bin_element, ?line(Meta), { string, 0, String }, Size, Types }|Acc]);
105+
{ bin, _, Elements } when AllowSplice ->
106+
build_bitstr_each(Fun, T, Meta, NS, lists:reverse(Elements) ++ Acc);
100107
{ cons, _, _, _ } = Cons ->
101108
build_bitstr_each(Fun, T, Meta, NS, rehash_cons(Cons, Size, Types, []) ++ Acc);
102109
{ nil, _ } ->
@@ -109,10 +116,15 @@ rehash_cons({ nil, _ }, _Size, _Types, Acc) -> Acc;
109116
rehash_cons({ cons, Line, Left, Right }, Size, Types, Acc) ->
110117
rehash_cons(Right, Size, Types, [{ bin_element, Line, Left, Size, Types }|Acc]).
111118

112-
is_default_or_utf(default) -> true;
113-
is_default_or_utf([UTF|_]) when UTF == utf8; UTF == utf16; UTF == utf32 -> true;
114-
is_default_or_utf([_|T]) -> is_default_or_utf(T);
115-
is_default_or_utf([]) -> false.
119+
types_allow_string([End|T]) when End == little; End == big -> types_allow_string(T);
120+
types_allow_string([UTF|T]) when UTF == utf8; UTF == utf16; UTF == utf32 -> types_allow_string(T);
121+
types_allow_string([]) -> true;
122+
types_allow_string(_) -> false.
123+
124+
types_allow_splice(default) -> true;
125+
types_allow_splice([binary]) -> true;
126+
types_allow_splice([bitstring]) -> true;
127+
types_allow_splice(_) -> false.
116128

117129
%% Extra bitstring specifiers
118130

lib/elixir/test/elixir/kernel/binary_test.exs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,19 @@ bar
9696
assert <<_a, _b :: size(s)>> = "foo"
9797
end
9898

99+
test :pattern_match_with_splice do
100+
assert << 1, <<2, 3, 4>>, 5 >> = <<1, 2, 3, 4, 5>>
101+
end
102+
99103
test :partial_application do
100104
assert (<< &1, 2 >>).(1) == << 1, 2 >>
101105
assert (<< &1, &2 >>).(1, 2) == << 1, 2 >>
102106
assert (<< &2, &1 >>).(2, 1) == << 1, 2 >>
103107
end
104108

105-
test :bitsyntax_with_utf do
109+
test :literal do
110+
assert <<106, 111, 115, 101>> == << "jose" :: binary >>
111+
106112
assert <<106, 111, 115, 101>> == << "jose" :: utf8 >>
107113
assert <<106, 111, 115, 101>> == << 'jose' :: utf8 >>
108114

0 commit comments

Comments
 (0)