Skip to content

Instantly share code, notes, and snippets.

@rozap
Created August 27, 2015 04:05
Show Gist options
  • Save rozap/7179ba0eccace90d3940 to your computer and use it in GitHub Desktop.
Save rozap/7179ba0eccace90d3940 to your computer and use it in GitHub Desktop.
def(at(<<var!(head) :: binary - size(2), rest :: binary>>, ptr, meta) when ptr >= 8 and ptr <= 9) do
new_posn = ptr + 2
case({:meta, {:header_length, head}}) do
{:halt, _} = halt ->
halt
{:meta, {key, val}} ->
{rest, new_posn, Dict.put(meta, key, val), []}
result ->
{rest, new_posn, meta, [result]}
end
end
def(at(<<var!(head) :: binary - size(2), rest :: binary>>, ptr, meta) when ptr >= 10 and ptr <= 11) do
new_posn = ptr + 2
case({:meta, {:record_length, head}}) do
{:halt, _} = halt ->
halt
{:meta, {key, val}} ->
{rest, new_posn, Dict.put(meta, key, val), []}
result ->
{rest, new_posn, meta, [result]}
end
end
@rozap
Copy link
Author

rozap commented Aug 27, 2015

lib/shapex/dbf.ex:9: warning: this clause cannot match because a previous clause at line 9 always matches
lib/shapex/dbf.ex:10: warning: this clause cannot match because of different types/sizes
lib/shapex/dbf.ex:10: warning: this clause cannot match because a previous clause at line 10 always matches

@rozap
Copy link
Author

rozap commented Aug 27, 2015

  defp build_byte_reader(from_byte, to_byte, type, endianness, body) do
    byte_size = (to_byte - from_byte) + 1
    units_to_match = to_match_unit(byte_size, type)

    matcher = build_byte_matcher(type, units_to_match, endianness)

    # Macro.to_string(matcher) |> IO.puts

    wat = quote do
      def at(unquote(matcher), ptr, meta) when ptr >= unquote(from_byte) and ptr <= unquote(to_byte) do
        new_posn = ptr + unquote(byte_size)
        case unquote(body[:do]) do
          {:halt, _} = halt -> halt
          {:meta, {key, val}} -> {rest, new_posn, Dict.put(meta, key, val), []}
          result -> {rest, new_posn, meta, [result]}
        end
      end
    end

    Macro.to_string(wat) |> IO.puts

    wat
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment