Bunch.Binary (Bunch v1.6.3)

View Source

A bunch of helpers for manipulating binaries.

Summary

Functions

Chunks given binary into parts of given size.

Chunks given binary into parts of given size.

Returns a 2-tuple, where the first element is the result of take_int_part(binary, i), and the second is the rest of binary.

Cuts off the smallest possible chunk from the end of binary, so that the size of returned binary is an integer multiple of i.

Functions

chunk_every(binary, chunk_size)

@spec chunk_every(binary(), pos_integer()) :: [binary()]

Chunks given binary into parts of given size.

Remaining part is cut off.

Examples

iex> <<1, 2, 3, 4, 5, 6>> |> Bunch.Binary.chunk_every(2)
[<<1, 2>>, <<3, 4>>, <<5, 6>>]
iex> <<1, 2, 3, 4, 5, 6, 7>> |> Bunch.Binary.chunk_every(2)
[<<1, 2>>, <<3, 4>>, <<5, 6>>]

chunk_every_rem(binary, chunk_size)

@spec chunk_every_rem(binary(), chunk_size :: pos_integer()) ::
  {[binary()], remainder :: binary()}

Chunks given binary into parts of given size.

Returns list of chunks and remainder.

Examples

iex> <<1, 2, 3, 4, 5, 6>> |> Bunch.Binary.chunk_every_rem(2)
{[<<1, 2>>, <<3, 4>>, <<5, 6>>], <<>>}
iex> <<1, 2, 3, 4, 5, 6, 7>> |> Bunch.Binary.chunk_every_rem(2)
{[<<1, 2>>, <<3, 4>>, <<5, 6>>], <<7>>}

split_int_part(binary, i)

@spec split_int_part(binary(), pos_integer()) :: {binary(), binary()}

Returns a 2-tuple, where the first element is the result of take_int_part(binary, i), and the second is the rest of binary.

Examples

iex> import Bunch.Binary
iex> split_int_part(<<1,2,3,4,5,6,7,8>>, 3)
{<<1,2,3,4,5,6>>, <<7,8>>}
iex> split_int_part(<<1,2,3,4,5,6,7,8>>, 4)
{<<1,2,3,4,5,6,7,8>>, <<>>}

take_int_part(binary, i)

@spec take_int_part(binary(), pos_integer()) :: binary()

Cuts off the smallest possible chunk from the end of binary, so that the size of returned binary is an integer multiple of i.

Examples

iex> import Bunch.Binary
iex> take_int_part(<<1,2,3,4,5,6,7,8>>, 3)
<<1,2,3,4,5,6>>
iex> take_int_part(<<1,2,3,4,5,6,7,8>>, 4)
<<1,2,3,4,5,6,7,8>>