-
-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use LEB128 encoding for all integer values in ALiS
- Loading branch information
Showing
4 changed files
with
199 additions
and
271 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
defmodule Asciinema.Leb128 do | ||
def encode(number), do: do_encode(number, <<>>) | ||
|
||
defp do_encode(number, binary) do | ||
low = Bitwise.band(number, 127) | ||
number = Bitwise.bsr(number, 7) | ||
|
||
if number > 0 do | ||
low = Bitwise.bor(low, 128) | ||
do_encode(number, <<binary::binary, low::8>>) | ||
else | ||
<<binary::binary, low::8>> | ||
end | ||
end | ||
|
||
def decode(binary), do: do_decode(binary, 0, 0) | ||
|
||
defp do_decode(<<byte::8>>, number, shift) when byte < 128, | ||
do: {number + Bitwise.bsl(byte, shift), ""} | ||
|
||
defp do_decode(<<byte::8, rest::binary>>, number, shift) when byte < 128, | ||
do: {number + Bitwise.bsl(byte, shift), rest} | ||
|
||
defp do_decode(<<byte::8, rest::binary>>, number, shift) when byte > 127 do | ||
byte = Bitwise.band(byte, 127) | ||
do_decode(rest, number + Bitwise.bsl(byte, shift), shift + 7) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.