tobogganSe connecter

packages

The Louise packages, each released on its own

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

170 lignes · 4206 octetsBrut · Historique
1/// The binary wire forms (PostgreSQL's binary format, per type): big-
2/// endian integers, IEEE floats, one-byte booleans, raw bytes, 16-byte
3/// UUIDs, and `timestamptz` as microseconds since 2000-01-01 UTC. Pure
4/// Louise, over `std/int`'s bit operations and `std/float`'s bits (gabla108/louise#143).
5
6import std/bytes
7import std/float
8import std/int
9import std/list
10
11// ---- encoding (to Bytes) -------------------------------------------------------
12
13/// `n` as 2 big-endian bytes (its low 16 bits, two's complement).
14pub fn be16 :: n: Int -> Bytes
15{
16 let n = int.low_bits n 16
17 bytes.from_list [byte_at n 1, byte_at n 0]
18}
19
20/// `n` as 4 big-endian bytes (its low 32 bits).
21pub fn be32 :: n: Int -> Bytes
22{
23 let n = int.low_bits n 32
24 bytes.from_list [byte_at n 3, byte_at n 2, byte_at n 1, byte_at n 0]
25}
26
27/// `n` as 8 big-endian bytes (its low 64 bits).
28pub fn be64 :: n: Int -> Bytes
29{
30 let n = int.low_bits n 64
31 bytes.from_list
32 [ byte_at n 7
33 , byte_at n 6
34 , byte_at n 5
35 , byte_at n 4
36 , byte_at n 3
37 , byte_at n 2
38 , byte_at n 1
39 , byte_at n 0
40 ]
41}
42
43/// The byte of `n` at position `i` (0 is least significant).
44fn byte_at :: n: Int i: Int -> Int
45{
46 int.and (int.shift_right n (i * 8)) 255
47}
48
49/// A `Float` as 4 big-endian IEEE 754 bytes.
50pub fn f32 :: x: Float -> Bytes
51{
52 be32 (float.to_bits32 x)
53}
54
55/// A `Float` as 8 big-endian IEEE 754 bytes.
56pub fn f64 :: x: Float -> Bytes
57{
58 be64 (float.to_bits x)
59}
60
61// ---- decoding (from Bytes, at an offset) ---------------------------------------
62
63/// The unsigned 16-bit integer at `at`.
64pub fn u16_at :: b: Bytes at: Int -> Int
65{
66 int.shift_left (get b at) 8 + get b (at + 1)
67}
68
69/// The signed 16-bit integer at `at`.
70pub fn i16_at :: b: Bytes at: Int -> Int
71{
72 signed (u16_at b at) 16
73}
74
75/// The unsigned 32-bit integer at `at`.
76pub fn u32_at :: b: Bytes at: Int -> Int
77{
78 int.shift_left (get b at) 24 + int.shift_left (get b (at + 1)) 16
79 + int.shift_left (get b (at + 2)) 8
80 + get b (at + 3)
81}
82
83/// The signed 32-bit integer at `at`.
84pub fn i32_at :: b: Bytes at: Int -> Int
85{
86 signed (u32_at b at) 32
87}
88
89/// The signed 64-bit integer at `at` (a bigint above 2^62, ADR 0014).
90pub fn i64_at :: b: Bytes at: Int -> Int
91{
92 let n =
93 list.fold
94 [ get b at
95 , get b (at + 1)
96 , get b (at + 2)
97 , get b (at + 3)
98 , get b (at + 4)
99 , get b (at + 5)
100 , get b (at + 6)
101 , get b (at + 7)
102 ]
103 0
104 fn acc byte { int.shift_left acc 8 + byte }
105 signed n 64
106}
107
108/// `n` (unsigned, `bits` wide) as signed: two's complement.
109fn signed :: n: Int bits: Int -> Int
110{
111 let sign = int.shift_left 1 (bits - 1)
112 if int.and n sign == 0
113 {
114 n
115 }
116 else
117 {
118 n - int.shift_left sign 1
119 }
120}
121
122/// The byte at `i` (bounds-checked).
123fn get :: b: Bytes i: Int -> Int
124{
125 match bytes.get b i
126 { Some(x) -> x
127 , None -> panic "binary: a byte out of bounds (a bug in the codec)"
128 }
129}
130
131/// The 32-bit IEEE 754 float at `at`.
132pub fn f32_at :: b: Bytes at: Int -> Float
133{
134 float.from_bits32 (u32_at b at)
135}
136
137/// The 64-bit IEEE 754 float at `at`.
138pub fn f64_at :: b: Bytes at: Int -> Float
139{
140 float.from_bits (i64_at b at)
141}
142
143// ---- tests ------------------------------------------------------------------------
144
145import std/test.{assert_equal}
146
147@test
148fn integers_big_endian_and_signed ::
149{
150 assert_equal (bytes.to_list (be16 258)) [1, 2]
151 assert_equal (bytes.to_list (be32 16909060)) [1, 2, 3, 4]
152 assert_equal (bytes.to_list (be64 42)) [0, 0, 0, 0, 0, 0, 0, 42]
153 // Negative numbers are two's complement: -1 is all ones.
154 assert_equal (bytes.to_list (be16 (-1))) [255, 255]
155 assert_equal (i16_at (be16 (-2)) 0) (-2)
156 assert_equal (i32_at (be32 (-2147483648)) 0) (-2147483648)
157 assert_equal (i64_at (be64 (-1)) 0) (-1)
158 assert_equal (i32_at (be32 16909060) 0) 16909060
159 assert_equal (i64_at (be64 946684800000000) 0) 946684800000000
160}
161
162@test
163fn floats_big_endian ::
164{
165 // 1.5 as IEEE 754 f64: 0x3FF8000000000000; as f32: 0x3FC00000.
166 assert_equal (bytes.to_list (f64 1.5)) [63, 248, 0, 0, 0, 0, 0, 0]
167 assert_equal (bytes.to_list (f32 1.5)) [63, 192, 0, 0]
168 assert_equal (f64_at (f64 1.5) 0) 1.5
169 assert_equal (f32_at (f32 (-0.5)) 0) (-0.5)
170}