Skip to content
Permalink
Browse files
WIP (Extract index matching)
  • Loading branch information
lexmag committed Apr 11, 2017
1 parent 2eb1a9f commit 6eacf81865bf6a13dc1bf2f546a4e9b129cd619f
Showing with 16 additions and 36 deletions.
  1. +16 −36 lib/msgpax/unpacker.ex
@@ -58,12 +58,7 @@ defmodule Msgpax.Unpacker do
for {formats, value} <- primitives, format <- formats do
defp unpack(<<unquote_splicing(format), rest::bits>>, result, options, outer, index, count) do
result = [unquote(value) | result]
case index + 1 do
^count ->
unpack_continue(rest, options, outer, result, count)
index ->
unpack(rest, result, options, outer, index, count)
end
unpack_continue(rest, result, options, outer, index, count)
end
end

@@ -76,14 +71,7 @@ defmodule Msgpax.Unpacker do
defp unpack(<<unquote_splicing(format), rest::bits>>, result, options, outer, index, count) do
case unquote(quote(do: length)) do
0 ->
result = [[] | result]
case index + 1 do
^count ->
unpack_continue(rest, options, outer, result, count)
index ->
unpack(rest, result, options, outer, index, count)
end

unpack_continue(rest, [[] | result], options, outer, index, count)
length ->
unpack(rest, result, options, [:list, index, count | outer], 0, length)
end
@@ -99,14 +87,7 @@ defmodule Msgpax.Unpacker do
defp unpack(<<unquote_splicing(format), rest::bits>>, result, options, outer, index, count) do
case unquote(quote(do: length)) do
0 ->
result = [%{} | result]
case index + 1 do
^count ->
unpack_continue(rest, options, outer, result, count)
index ->
unpack(rest, result, options, outer, index, count)
end

unpack_continue(rest, [%{} | result], options, outer, index, count)
length ->
unpack(rest, result, options, [:map, index, count | outer], 0, length * 2)
end
@@ -121,13 +102,7 @@ defmodule Msgpax.Unpacker do
for format <- binaries do
defp unpack(<<unquote_splicing(format), rest::bits>>, result, options, outer, index, count) do
value = unpack_binary(unquote(quote(do: content)), options)
result = [value | result]
case index + 1 do
^count ->
unpack_continue(rest, options, outer, result, count)
index ->
unpack(rest, result, options, outer, index, count)
end
unpack_continue(rest, [value | result], options, outer, index, count)
end
end

@@ -144,13 +119,7 @@ defmodule Msgpax.Unpacker do
for format <- extensions do
defp unpack(<<unquote_splicing(format), rest::bits>>, result, options, outer, index, count) do
value = unpack_ext(unquote(quote(do: type)), unquote(quote(do: content)), options)
result = [value | result]
case index + 1 do
^count ->
unpack_continue(rest, options, outer, result, count)
index ->
unpack(rest, result, options, outer, index, count)
end
unpack_continue(rest, [value | result], options, outer, index, count)
end
end

@@ -162,6 +131,17 @@ defmodule Msgpax.Unpacker do
throw :incomplete
end

@compile {:inline, [unpack_continue: 6]}

defp unpack_continue(rest, result, options, outer, index, count) do
case index + 1 do
^count ->
unpack_continue(rest, options, outer, result, count)
index ->
unpack(rest, result, options, outer, index, count)
end
end

defp unpack_continue(<<buffer::bits>>, options, [kind, index, length | outer], result, count) do
result = build_collection(result, count, kind)
case index + 1 do

0 comments on commit 6eacf81

Please sign in to comment.