Content-Length: 309212 | pFad | http://github.com/esl/base16/commit/94f27b4d1a6395c01ab8f8f60919d6aa39849c10

10 Prepare a new hex package · esl/base16@94f27b4 · GitHub
Skip to content

Commit

Permalink
Prepare a new hex package
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed May 19, 2021
1 parent 08c96ac commit 94f27b4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
Base16 encoding and decoding
============================

[![Hex](http://img.shields.io/hexpm/v/fast_base16.svg)](https://hex.pm/packages/fast_base16)
[![Actions Status](https://github.com/esl/base16/workflows/ci/badge.svg)](https://github.com/esl/base16/actions)
[![codecov](https://codecov.io/gh/esl/base16/branch/main/graph/badge.svg)](https://codecov.io/gh/esl/base16)
[![Hex](http://img.shields.io/hexpm/v/base16.svg)](https://hex.pm/packages/base16)

API
---
## API

Both `encode/1` and `decode/1` functions are of `(binary()) -> binary()` type.

Expand All @@ -19,15 +20,17 @@ B = base16:decode(H).
Please, note:

* `base16:encode/1` returns lower-case letters.
* `base16:decode/1` requires the argument to be a binary of odd-number size, and it parses both
in upper-case or lower-case encoding.
* `base16:decode/1` requires the argument to be a binary of odd-number size, and it parses both in upper-case or lower-case encoding.


Implementation
--------------
## Implementation

Code is implemented using lookup tables, which tremendously speeds-up the algorithm and consumes
constant memory. It's the fastest you can get using pure Erlang code!
Code is implemented using lookup tables, which tremendously speeds-up the algorithm and consumes constant memory. It's the fastest you can get using pure Erlang code!

## NOTE

Note that, lookup tables also means that the code-size is bigger.
If you're running on embedded, where smaller code-sizes are the actual best, you might benefit from using the version 1.0.0, both available on github and hex.


License
Expand Down
3 changes: 1 addition & 2 deletions src/base16.app.src
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{application, base16,
[
{description, "Base16 encoding and decoding"},
{vsn, "1.1.1"},
{vsn, git},
{registered, []},
{applications, [kernel, stdlib]},
{env, []},
{maintainers, ["ESL"]},
{pkg_name, "fast_base16"},
{licenses, ["MIT", "GPL (for tests)"]},
{links, [{"Github", "https://github.com/esl/base16"}]}
]}.
27 changes: 27 additions & 0 deletions test/base16_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ prop_encode_decode() ->
H2 =:= list_to_binary(string:to_lower(binary_to_list(H1)))
end).

prop_compare_algorithms_encode() ->
?FORALL(B, binary(),
slow_encode(B) =:= base16:encode(B)).

prop_compare_algorithms_decode() ->
?FORALL(B, even_len_binary(hex_digit()),
slow_decode(B) =:= base16:decode(B)).

encode_spec_test() ->
proper:check_spec({base16, encode, 1}).

Expand Down Expand Up @@ -69,3 +77,22 @@ even_len_binary(Digit) ->

even_len_list(Elem) ->
?SUCHTHAT(L, list(Elem), length(L) rem 2 =:= 0).


%%--------------------------------------------------------------------
%% Older algorithm
%%--------------------------------------------------------------------
-spec slow_encode(binary()) -> <<_:_*16>>.
slow_encode(Data) ->
<< <<(hex(N div 16)), (hex(N rem 16))>> || <<N>> <= Data >>.

-spec slow_decode(<<_:_*16>>) -> binary().
slow_decode(Base16) when size(Base16) rem 2 =:= 0 ->
<< <<(unhex(H) bsl 4 + unhex(L))>> || <<H,L>> <= Base16 >>.

hex(N) when N < 10 -> N + $0;
hex(N) when N < 16 -> N - 10 + $a.

unhex(D) when $0 =< D andalso D =< $9 -> D - $0;
unhex(D) when $a =< D andalso D =< $f -> 10 + D - $a;
unhex(D) when $A =< D andalso D =< $F -> 10 + D - $A.

0 comments on commit 94f27b4

Please sign in to comment.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/esl/base16/commit/94f27b4d1a6395c01ab8f8f60919d6aa39849c10

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy