Current section
Files
Jump to
Current section
Files
src/lzf_gleam@internal@back_ref.erl
-module(lzf_gleam@internal@back_ref).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([to_string/1, to_bit_array/1, resolve/3]).
-export_type([back_ref/0]).
-type back_ref() :: {back_ref, integer(), integer()}.
-spec to_string(back_ref()) -> binary().
to_string(Ref) ->
<<<<<<"len: "/utf8, (gleam@int:to_string(erlang:element(2, Ref)))/binary>>/binary,
", offset: "/utf8>>/binary,
(gleam@int:to_string(erlang:element(3, Ref)))/binary>>.
-spec to_bit_array(back_ref()) -> bitstring().
to_bit_array(Ref) ->
<<1:1, (erlang:element(2, Ref)):3, (erlang:element(3, Ref)):12>>.
-spec resolve(back_ref(), bitstring(), integer()) -> {ok, binary()} |
{error, nil}.
resolve(Ref, Data, Current_pos) ->
case gleam_stdlib:bit_array_slice(
Data,
Current_pos - erlang:element(3, Ref),
erlang:element(2, Ref)
) of
{ok, Slice} ->
gleam@bit_array:to_string(Slice);
{error, nil} ->
{error, nil}
end.