Skip to content

Commit d508e5a

Browse files
authored
chore(stdlib)!: Update Buffer/Bytes to use new integer types (#1704)
* chore(stdlib)!: Update Buffer/Bytes to use new integer types * format literals.re * account for octal, comment wording change * format code
1 parent 84e6e54 commit d508e5a

File tree

11 files changed

+1322
-271
lines changed

11 files changed

+1322
-271
lines changed

compiler/src/utils/literals.re

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,59 @@ let int8_max = 127l;
7171
let int8_min = (-128l);
7272

7373
let conv_int8 = s => {
74+
let non_decimal =
75+
String.length(s) > 2
76+
&& List.mem(String.sub(s, 0, 2), ["0x", "0b", "0o"]);
7477
switch (Int32.of_string_opt(s)) {
7578
| None => None
7679
| Some(n) =>
77-
if (n < int8_min || n > int8_max) {
80+
let (&) = Int32.logand;
81+
let (>>) = Int32.shift_right;
82+
let (<<) = Int32.shift_left;
83+
if (n > int8_max) {
84+
// Trick to allow setting the sign bit in representations like 0xFF
85+
if (non_decimal && (n & 0xFFFFFF80l) >> 7 == 1l) {
86+
Some(n << 24 >> 24);
87+
} else {
88+
None;
89+
};
90+
} else if (n < int8_min) {
7891
None;
92+
} else if (non_decimal && n < 0l) {
93+
None; // Reject something like 0xFFFFFFFFs
7994
} else {
8095
Some(n);
81-
}
96+
};
8297
};
8398
};
8499

85100
let int16_max = 32767l;
86101
let int16_min = (-32768l);
87102

88103
let conv_int16 = s => {
104+
let non_decimal =
105+
String.length(s) > 2
106+
&& List.mem(String.sub(s, 0, 2), ["0x", "0b", "0o"]);
89107
switch (Int32.of_string_opt(s)) {
90108
| None => None
91109
| Some(n) =>
92-
if (n < int16_min || n > int16_max) {
110+
let (&) = Int32.logand;
111+
let (>>) = Int32.shift_right;
112+
let (<<) = Int32.shift_left;
113+
if (n > int16_max) {
114+
// Trick to allow setting the sign bit in representations like 0xFFFF
115+
if (non_decimal && (n & 0xFFFF8000l) >> 15 == 1l) {
116+
Some(n << 16 >> 16);
117+
} else {
118+
None;
119+
};
120+
} else if (n < int16_min) {
93121
None;
122+
} else if (non_decimal && n < 0l) {
123+
None; // Reject something like 0xFFFFFFFFS
94124
} else {
95125
Some(n);
96-
}
126+
};
97127
};
98128
};
99129

compiler/test/stdlib/buffer.test.gr

Lines changed: 87 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ assert Buffer.length(Buffer.make(1024)) == 0
1313
// its contents should equal other buffers with equivalent bytes
1414
assert Buffer.toBytes(Buffer.make(0)) == Buffer.toBytes(Buffer.make(0))
1515
let a = Buffer.make(0)
16-
Buffer.addInt8(1l, a)
16+
Buffer.addInt8(1s, a)
1717
let b = Buffer.make(0)
18-
Buffer.addInt8(1l, b)
18+
Buffer.addInt8(1s, b)
1919
assert Buffer.toBytes(a) == Buffer.toBytes(b)
2020

2121
// it should add/get int8
@@ -24,10 +24,24 @@ let size = 8 * byteSize
2424
let buf = Buffer.make(size)
2525
for (let mut i = 0; i < size / byteSize; i += 1) {
2626
// it should add an int8
27-
let n = 42l
27+
let n = 42s
2828
Buffer.addInt8(n, buf)
2929
// it should have value n at position i
30-
assert Buffer.getInt8S(i * byteSize, buf) == n
30+
assert Buffer.getInt8(i * byteSize, buf) == n
31+
// it should have length equal to i + 1
32+
assert Buffer.length(buf) == (i + 1) * byteSize
33+
}
34+
35+
// it should add/get uint8
36+
let byteSize = 1
37+
let size = 8 * byteSize
38+
let buf = Buffer.make(size)
39+
for (let mut i = 0; i < size / byteSize; i += 1) {
40+
// it should add an uint8
41+
let n = 42us
42+
Buffer.addUint8(n, buf)
43+
// it should have value n at position i
44+
assert Buffer.getUint8(i * byteSize, buf) == n
3145
// it should have length equal to i + 1
3246
assert Buffer.length(buf) == (i + 1) * byteSize
3347
}
@@ -38,10 +52,24 @@ let size = 8 * byteSize
3852
let buf = Buffer.make(size)
3953
for (let mut i = 0; i < size / byteSize; i += 1) {
4054
// it should add an int16
41-
let n = 42l
55+
let n = 42S
4256
Buffer.addInt16(n, buf)
4357
// it should have value n at position i
44-
assert Buffer.getInt16S(i * byteSize, buf) == n
58+
assert Buffer.getInt16(i * byteSize, buf) == n
59+
// it should have length equal to i + 1
60+
assert Buffer.length(buf) == (i + 1) * byteSize
61+
}
62+
63+
// it should add/get int16
64+
let byteSize = 2
65+
let size = 8 * byteSize
66+
let buf = Buffer.make(size)
67+
for (let mut i = 0; i < size / byteSize; i += 1) {
68+
// it should add an int16
69+
let n = 42uS
70+
Buffer.addUint16(n, buf)
71+
// it should have value n at position i
72+
assert Buffer.getUint16(i * byteSize, buf) == n
4573
// it should have length equal to i + 1
4674
assert Buffer.length(buf) == (i + 1) * byteSize
4775
}
@@ -60,6 +88,20 @@ for (let mut i = 0; i < size / byteSize; i += 1) {
6088
assert Buffer.length(buf) == (i + 1) * byteSize
6189
}
6290

91+
// it should add/get uint32
92+
let byteSize = 4
93+
let size = 8 * byteSize
94+
let buf = Buffer.make(size)
95+
for (let mut i = 0; i < size / byteSize; i += 1) {
96+
// it should add an uint32
97+
let n = 42ul
98+
Buffer.addUint32(n, buf)
99+
// it should have value n at position i
100+
assert Buffer.getUint32(i * byteSize, buf) == n
101+
// it should have length equal to i + 1
102+
assert Buffer.length(buf) == (i + 1) * byteSize
103+
}
104+
63105
// it should add/get float32
64106
let byteSize = 4
65107
let size = 8 * byteSize
@@ -88,6 +130,20 @@ for (let mut i = 0; i < size / byteSize; i += 1) {
88130
assert Buffer.length(buf) == (i + 1) * byteSize
89131
}
90132

133+
// it should add/get uint64
134+
let byteSize = 8
135+
let size = 8 * byteSize
136+
let buf = Buffer.make(size)
137+
for (let mut i = 0; i < size / byteSize; i += 1) {
138+
// it should add an uint64
139+
let n = 42uL
140+
Buffer.addUint64(n, buf)
141+
// it should have value n at position i
142+
assert Buffer.getUint64(i * byteSize, buf) == n
143+
// it should have length equal to i + 1
144+
assert Buffer.length(buf) == (i + 1) * byteSize
145+
}
146+
91147
// it should add/get float64
92148
let byteSize = 8
93149
let size = 8 * byteSize
@@ -104,48 +160,48 @@ for (let mut i = 0; i < size / byteSize; i += 1) {
104160

105161
// it should clear the buffer
106162
let buf = Buffer.make(8)
107-
Buffer.addInt8(0l, buf)
163+
Buffer.addInt8(0s, buf)
108164
assert Buffer.length(buf) == 1
109165
Buffer.clear(buf)
110166
assert Buffer.length(buf) == 0
111167

112168
// it should reset the buffer
113169
let buf = Buffer.make(0)
114-
Buffer.addInt8(0l, buf)
170+
Buffer.addInt8(0s, buf)
115171
assert Buffer.length(buf) == 1
116172
Buffer.reset(buf)
117173
assert Buffer.length(buf) == 0
118174

119175
// it should truncate the buffer
120176
let buf = Buffer.make(0)
121-
Buffer.addInt8(1l, buf)
122-
Buffer.addInt8(2l, buf)
123-
Buffer.addInt8(3l, buf)
177+
Buffer.addInt8(1s, buf)
178+
Buffer.addInt8(2s, buf)
179+
Buffer.addInt8(3s, buf)
124180
assert Buffer.length(buf) == 3
125181
Buffer.truncate(1, buf)
126182
assert Buffer.length(buf) == 1
127-
assert Buffer.getInt8S(0, buf) == 1l
183+
assert Buffer.getInt8(0, buf) == 1s
128184
let bytes = Buffer.toBytes(buf)
129185
assert Bytes.length(bytes) == 1
130186

131187
// it should convert a buffer to bytes
132188
let buf = Buffer.make(0)
133-
Buffer.addInt8(0l, buf)
134-
Buffer.addInt8(1l, buf)
135-
Buffer.addInt8(2l, buf)
189+
Buffer.addInt8(0s, buf)
190+
Buffer.addInt8(1s, buf)
191+
Buffer.addInt8(2s, buf)
136192
assert Buffer.length(buf) == 3
137193
let bytes = Buffer.toBytes(buf)
138194
assert Bytes.length(bytes) == 3
139-
assert Bytes.getInt8U(0, bytes) == 0l
140-
assert Bytes.getInt8U(1, bytes) == 1l
141-
assert Bytes.getInt8U(2, bytes) == 2l
195+
assert Bytes.getUint8(0, bytes) == 0us
196+
assert Bytes.getUint8(1, bytes) == 1us
197+
assert Bytes.getUint8(2, bytes) == 2us
142198
assert Bytes.length(Buffer.toBytes(Buffer.make(1024))) == 0
143199

144200
// it should convert a buffer to a bytes slice
145201
let buf = Buffer.make(0)
146-
Buffer.addInt8(0l, buf)
147-
Buffer.addInt8(1l, buf)
148-
Buffer.addInt8(2l, buf)
202+
Buffer.addInt8(0s, buf)
203+
Buffer.addInt8(1s, buf)
204+
Buffer.addInt8(2s, buf)
149205
assert Buffer.length(buf) == 3
150206
let bytes = Buffer.toBytesSlice(0, 0, buf)
151207
assert Bytes.length(bytes) == 0
@@ -155,26 +211,26 @@ let bytes = Buffer.toBytesSlice(2, 0, buf)
155211
assert Bytes.length(bytes) == 0
156212
let bytes = Buffer.toBytesSlice(0, 1, buf)
157213
assert Bytes.length(bytes) == 1
158-
assert Bytes.getInt8U(0, bytes) == 0l
214+
assert Bytes.getUint8(0, bytes) == 0us
159215
let bytes = Buffer.toBytesSlice(1, 1, buf)
160216
assert Bytes.length(bytes) == 1
161-
assert Bytes.getInt8U(0, bytes) == 1l
217+
assert Bytes.getUint8(0, bytes) == 1us
162218
let bytes = Buffer.toBytesSlice(2, 1, buf)
163219
assert Bytes.length(bytes) == 1
164-
assert Bytes.getInt8U(0, bytes) == 2l
220+
assert Bytes.getUint8(0, bytes) == 2us
165221
let bytes = Buffer.toBytesSlice(0, 2, buf)
166222
assert Bytes.length(bytes) == 2
167-
assert Bytes.getInt8U(0, bytes) == 0l
168-
assert Bytes.getInt8U(1, bytes) == 1l
223+
assert Bytes.getUint8(0, bytes) == 0us
224+
assert Bytes.getUint8(1, bytes) == 1us
169225
let bytes = Buffer.toBytesSlice(1, 2, buf)
170226
assert Bytes.length(bytes) == 2
171-
assert Bytes.getInt8U(0, bytes) == 1l
172-
assert Bytes.getInt8U(1, bytes) == 2l
227+
assert Bytes.getUint8(0, bytes) == 1us
228+
assert Bytes.getUint8(1, bytes) == 2us
173229
let bytes = Buffer.toBytesSlice(0, 3, buf)
174230
assert Bytes.length(bytes) == 3
175-
assert Bytes.getInt8U(0, bytes) == 0l
176-
assert Bytes.getInt8U(1, bytes) == 1l
177-
assert Bytes.getInt8U(2, bytes) == 2l
231+
assert Bytes.getUint8(0, bytes) == 0us
232+
assert Bytes.getUint8(1, bytes) == 1us
233+
assert Bytes.getUint8(2, bytes) == 2us
178234

179235
// addBytes / toBytes
180236
let str = "Let's get this 🍞"

0 commit comments

Comments
 (0)