Skip to content

Commit

Permalink
Handle and refactor binary pattern expressions for unique expressions…
Browse files Browse the repository at this point in the history
… of integer and size variants. (#16)
  • Loading branch information
LivInAbsurdism committed Oct 12, 2023
1 parent 094fef7 commit 2c36f88
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion lib/credo_binary_patterns/check/consistency/pattern.ex
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,34 @@ defmodule CredoBinaryPatterns.Check.Consistency.Pattern do
end

# Proper format of the pattern as a string, used to suggest corrections
defp stringify(value, %{
type: :integer,
sign: :unsigned,
size: {:size, :size},
unit: {:unit, unit_value},
endian: nil
}),
do: "<<" <> value <> "::integer-size(size)-unit(#{unit_value})>>"

defp stringify(value, %{
type: :integer,
sign: :signed,
size: {:size, size},
unit: {:unit, unit_value},
endian: nil
}),
do: "<<" <> value <> "::signed-size(#{size})-unit(#{unit_value})" <> ">>"

defp stringify(value, %{
type: nil,
sign: :signed,
size: {:size, byte_size},
unit: {:unit, unit_value}
})
when byte_size in [:byte_size, :size],
do: "<<" <> value <> "::signed-size(#{byte_size})-unit(#{unit_value})" <> ">>"

defp stringify(value, pattern_info) do
# Ordering depends on the type, if byte/binary, size comes first
order =
if pattern_info.type in [:bytes, :bits] do
[:endian, :sign, :size, :unit, :type]
Expand Down

0 comments on commit 2c36f88

Please sign in to comment.