From 502ca17bd3199693b72fddc16ce92dcc42ce9262 Mon Sep 17 00:00:00 2001 From: Kodi Craft Date: Sun, 7 Jul 2024 13:35:21 +0200 Subject: [PATCH] Minor change to data reader --- lib/data.ex | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/lib/data.ex b/lib/data.ex index 08f17af..ac87ee1 100644 --- a/lib/data.ex +++ b/lib/data.ex @@ -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, <>}) do {[value != 0 | acc], rest} end