_site/cover/msmp_integer_fixed.COVER.html

1 %% Copyright (c) 2023 Peter Morgan <peter.james.morgan@gmail.com>
2 %%
3 %% Licensed under the Apache License, Version 2.0 (the "License");
4 %% you may not use this file except in compliance with the License.
5 %% You may obtain a copy of the License at
6 %%
7 %% http://www.apache.org/licenses/LICENSE-2.0
8 %%
9 %% Unless required by applicable law or agreed to in writing, software
10 %% distributed under the License is distributed on an "AS IS" BASIS,
11 %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 %% See the License for the specific language governing permissions and
13 %% limitations under the License.
14
15 %% @doc An unsigned integer represented by a fixed number of little
16 %% endian bytes.
17
18 -module(msmp_integer_fixed).
19
20
21 -feature(maybe_expr, enable).
22
23
24 -export([decode/1]).
25 -export([encode/1]).
26
27
28 %% @doc Decode a fixed size little endian unsigned integer.
29 -spec decode(pos_integer() | scran_number:uparser()) -> scran:parser(binary(), non_neg_integer()).
30
31 decode(Bytes) when is_integer(Bytes) ->
32 2515 scran_number:u(little, Bytes * 8);
33
34 decode(ByteLengthParser) ->
35 5 fun
36 (Input) ->
37 5 maybe
38 5 {Remaining, Bytes} ?= ByteLengthParser(Input),
39 5 (decode(Bytes))(Remaining)
40 end
41 end.
42
43
44 %% @doc Encode a fixed size little endian unsigned integer.
45 -spec encode(pos_integer()) -> narcs:encoder(non_neg_integer(), binary()).
46
47 encode(Bytes) ->
48 69 narcs_number:u(little, Bytes * 8).
Line Hits Source