Minor change to data reader
All checks were successful
Build & Test / nix-build (push) Successful in 1m6s

This commit is contained in:
Kodi Craft 2024-07-07 13:35:21 +02:00
parent b0a85aaefc
commit 502ca17bd3
Signed by: kodi
GPG Key ID: 69D9EED60B242822

View File

@ -64,29 +64,19 @@ defmodule Amethyst.Minecraft.Read do
These functions allow you to chain them into eachother, at the end they will produce a list of all the
values they have read.
You may use the helper function Amethyst.Minecraft.Read.start/1 to start the chain and Amethyst.Minecraft.Read.stop/1
to get the data out of the chain. If you pass the option `reverse: true` to stop/1, the list will be reversed, this may
provide better performance if you do not mind the inconvenience of the reversed list.
You may use the helper function Amethyst.Minecraft.Read.start/1 to start the chain with a binary buffer.
The return value of the chain is a tuple containing the list of values and the remaining binary buffer, note
that the values are ordered from last to first, so in the opposite order of the chain.
iex> alias Amethyst.Minecraft.Read
iex> [_, _, _] = Read.start(<<1, 999::16, 64>>) |> Read.bool() |> Read.short() |> Read.byte() |> Read.stop()
[true, 999, 64]
iex> {[_, _, _], ""} = Read.start(<<1, 999::16, 64>>) |> Read.bool() |> Read.short() |> Read.byte()
{[64, 999, true], ""}
"""
def start(binary) do
{[], binary}
end
def stop({acc, rest}, opts \\ []) do
if Keyword.get(opts, :force_empty, false) and bit_size(rest) != 0 do
raise ArgumentError, "Expected no more data, but there is still #{bit_size(rest)} bits left"
end
case Keyword.get(opts, :reverse, false) do
true -> acc
false -> Enum.reverse(acc)
end
end
def bool({acc, <<value, rest::bitstring>>}) do
{[value != 0 | acc], rest}
end