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 Encoder combinators that write numbers into big or little |
16 |
|
%% endian bytes. |
17 |
|
|
18 |
|
-module(narcs_number). |
19 |
|
|
20 |
|
|
21 |
|
-export([f/2]). |
22 |
|
-export([i/2]). |
23 |
|
-export([u/2]). |
24 |
|
-export_type([endian/0]). |
25 |
|
|
26 |
|
|
27 |
|
-type endian() :: big | little. |
28 |
|
|
29 |
|
|
30 |
|
-spec i(endian(), pos_integer()) -> narcs:encoder(integer(), binary()). |
31 |
|
|
32 |
|
%% @doc Write a signed integer into a little or big endian bits. |
33 |
|
|
34 |
:-( |
i(Endianess, Size) -> int(Endianess, signed, Size). |
35 |
|
|
36 |
|
|
37 |
|
-spec u(endian(), pos_integer()) -> narcs:encoder(non_neg_integer(), binary()). |
38 |
|
|
39 |
|
%% @doc Write a unsigned integer into a little or big endian bits. |
40 |
|
|
41 |
17 |
u(Endianess, Size) -> int(Endianess, unsigned, Size). |
42 |
|
|
43 |
|
|
44 |
|
int(little, signed, Size) -> |
45 |
:-( |
fun |
46 |
|
(Input) -> |
47 |
:-( |
<<Input:Size/little-signed>> |
48 |
|
end; |
49 |
|
|
50 |
|
int(little, unsigned, Size) -> |
51 |
:-( |
fun |
52 |
|
(Input) -> |
53 |
:-( |
<<Input:Size/little>> |
54 |
|
end; |
55 |
|
|
56 |
|
int(big, signed, Size) -> |
57 |
:-( |
fun |
58 |
|
(Input) -> |
59 |
:-( |
<<Input:Size/signed>> |
60 |
|
end; |
61 |
|
|
62 |
|
int(big, unsigned, Size) -> |
63 |
17 |
fun |
64 |
|
(Input) -> |
65 |
11 |
<<Input:Size>> |
66 |
|
end. |
67 |
|
|
68 |
|
|
69 |
|
-spec f(endian(), pos_integer()) -> narcs:encoder(float(), binary()). |
70 |
|
|
71 |
|
%% @doc Write a float into a little or big endian bits. |
72 |
|
|
73 |
|
f(little, Size) -> |
74 |
:-( |
fun |
75 |
|
(Input) -> |
76 |
:-( |
<<Input:Size/float-little>> |
77 |
|
end; |
78 |
|
|
79 |
|
f(big, Size) -> |
80 |
:-( |
fun |
81 |
|
(Input) -> |
82 |
:-( |
<<Input:Size/float>> |
83 |
|
end. |