-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathbyte.mbt
348 lines (328 loc) · 9.14 KB
/
byte.mbt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright 2025 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
///|
/// Performs multiplication between two byte values. The result is truncated to
/// fit within the byte range.
///
/// Parameters:
///
/// * `self` : The first byte operand in the multiplication.
/// * `that` : The second byte operand in the multiplication.
///
/// Returns the product of the two bytes, truncated to fit within the byte range
/// (0-255).
///
/// Example:
///
/// ```moonbit
/// test "Byte::op_mul" {
/// let a = b'\x02'
/// let b = b'\x03'
/// inspect!(a * b, content="b'\\x06'") // 2 * 3 = 6
/// let c = b'\xFF'
/// inspect!(c * c, content="b'\\x01'") // 255 * 255 = 65025, truncated to 1
/// }
/// ```
pub fn Byte::op_mul(self : Byte, that : Byte) -> Byte {
(self.to_int() * that.to_int()).to_byte()
}
///|
/// Performs division operation between two bytes by converting them to integers,
/// performing the division, and converting the result back to a byte.
///
/// Parameters:
///
/// * `self` : The dividend byte value.
/// * `that` : The divisor byte value.
///
/// Returns the quotient of the division as a byte.
///
/// Example:
///
/// ```moonbit
/// test "Byte::op_div" {
/// let a = b'\xFF' // 255
/// let b = b'\x03' // 3
/// inspect!(a / b, content="b'\\x55'") // 255 / 3 = 85 (0x55)
/// }
///
/// test "panic Byte::op_div/division_by_zero" {
/// let a = b'\x01'
/// let b = b'\x00'
/// ignore(a / b) // Division by zero
/// }
/// ```
pub fn Byte::op_div(self : Byte, that : Byte) -> Byte {
(self.to_int() / that.to_int()).to_byte()
}
///|
/// Compares two `Byte` values for equality.
///
/// Parameters:
///
/// - `self` : The first `Byte` value to compare.
/// - `that` : The second `Byte` value to compare.
///
/// Returns `true` if the two `Byte` values are equal, otherwise `false`.
pub fn Byte::op_equal(self : Byte, that : Byte) -> Bool {
self.to_int() == that.to_int()
}
///|
/// Adds two `Byte` values together and returns the result as a `Byte`.
///
/// Parameters:
///
/// - `byte1` : The first `Byte` value to be added.
/// - `byte2` : The second `Byte` value to be added.
///
/// Returns the sum of `byte1` and `byte2` as a `Byte`.
pub fn Byte::op_add(self : Byte, that : Byte) -> Byte {
(self.to_int() + that.to_int()).to_byte()
}
///|
/// Subtracts the second byte from the first byte and returns the result as a
/// byte.
///
/// Parameters:
///
/// - `self` : The byte from which the second byte will be subtracted.
/// - `that` : The byte to subtract from the first byte.
///
/// Returns the result of the subtraction as a byte.
pub fn Byte::op_sub(self : Byte, that : Byte) -> Byte {
(self.to_int() - that.to_int()).to_byte()
}
///|
/// Compares two `Byte` values and returns an integer indicating their relative
/// order.
///
/// Parameters:
///
/// - `byte1` : The first `Byte` value to compare.
/// - `byte2` : The second `Byte` value to compare.
///
/// Returns an integer where:
/// - A value less than 0 indicates that `byte1` is less than `byte2`.
/// - A value of 0 indicates that `byte1` is equal to `byte2`.
/// - A value greater than 0 indicates that `byte1` is greater than `byte2`.
pub fn Byte::compare(self : Byte, that : Byte) -> Int {
self.to_int().compare(that.to_int())
}
///|
fn alphabet(self : Int) -> String {
match self {
0 => "0"
1 => "1"
2 => "2"
3 => "3"
4 => "4"
5 => "5"
6 => "6"
7 => "7"
8 => "8"
9 => "9"
10 => "A"
11 => "B"
12 => "C"
13 => "D"
14 => "E"
15 => "F"
_ => abort("impossible")
}
}
///|
/// Converts a `Byte` to its string representation in hexadecimal format.
///
/// Parameters:
///
/// - `byte` : The `Byte` value to be converted.
///
/// Returns a `String` representing the `Byte` in the format `b'\xHH'`, where
/// `HH` is the hexadecimal representation of the byte.
pub fn Byte::to_string(self : Byte) -> String {
let i = self.to_int()
let hi = alphabet(i / 16)
let lo = alphabet(i % 16)
"b'\\x\{hi}\{lo}'"
}
///|
/// Implements the `Hash` trait for `Byte` type by converting the byte to an
/// integer and using it as the hash value.
///
/// Parameters:
///
/// * `self` : The byte value to be hashed.
///
/// Returns an integer representing the hash value of the byte.
///
/// Example:
///
/// ```moonbit
/// test "Byte::hash" {
/// let b = b'\x42'
/// inspect!(Hash::hash(b), content="66") // ASCII value of 'B'
/// }
/// ```
pub impl Hash for Byte with hash(self) { self.to_int() }
///|
/// Implements the `Hash` trait for `Byte` type by providing a `hash_combine`
/// method that combines a byte value with a hasher.
///
/// Parameters:
///
/// * `self` : The byte value to be hashed.
/// * `hasher` : The hasher object that will be used to combine the byte value
/// into its internal state.
///
/// Example:
///
/// ```moonbit
/// test "Byte::hash_combine" {
/// let hasher = Hasher::new()
/// hasher.combine_byte(b'\xFF')
/// inspect!(hasher.finalize(), content="1955036104")
/// }
/// ```
pub impl Hash for Byte with hash_combine(self, hasher) {
hasher.combine_byte(self)
}
///|
/// Returns the default value for a `Byte`, which is `b'\x00'`.
///
/// Parameters:
///
/// - None
///
/// Returns the default `Byte` value, which is `b'\x00'`.
pub fn Byte::default() -> Byte {
b'\x00'
}
///|
/// Performs a bitwise NOT operation on the given `Byte` value.
///
/// Parameters:
///
/// - `value` : The `Byte` value to apply the bitwise NOT operation on.
///
/// Returns the result of the bitwise NOT operation as a `Byte`.
pub fn Byte::lnot(self : Byte) -> Byte {
self.to_int().lnot().to_byte()
}
///|
/// Performs a bitwise AND operation between two `Byte` values.
///
/// Parameters:
///
/// - `byte1` : The first `Byte` value to perform the bitwise AND operation with.
/// - `byte2` : The second `Byte` value to perform the bitwise AND operation
/// with.
///
/// Returns the result of the bitwise AND operation as a `Byte`.
pub fn Byte::land(self : Byte, that : Byte) -> Byte {
(self.to_int() & that.to_int()).to_byte()
}
///|
/// Performs a bitwise OR operation between two `Byte` values.
///
/// Parameters:
///
/// - `self` : The first `Byte` value.
/// - `that` : The second `Byte` value.
///
/// Returns a new `Byte` value resulting from the bitwise OR operation.
pub fn Byte::lor(self : Byte, that : Byte) -> Byte {
(self.to_int() | that.to_int()).to_byte()
}
///|
/// Performs a bitwise XOR operation between two `Byte` values.
///
/// Parameters:
///
/// - `self` : The first `Byte` value.
/// - `that` : The second `Byte` value.
///
/// Returns the result of the bitwise XOR operation as a `Byte`.
pub fn Byte::lxor(self : Byte, that : Byte) -> Byte {
(self.to_int() ^ that.to_int()).to_byte()
}
///|
/// Converts a `Byte` to a `UInt`.
///
/// Parameters:
///
/// - `byte` : The `Byte` value to be converted.
///
/// Returns the `UInt` representation of the `Byte`.
pub fn Byte::to_uint(self : Byte) -> UInt {
self.to_int().reinterpret_as_uint()
}
///|
/// Shifts the bits of the `Byte` value to the left by the specified number of
/// positions.
///
/// Parameters:
///
/// - `byte_value` : The `Byte` value whose bits are to be shifted.
/// - `shift_count` : The number of bit positions to shift the `byte_value` to
/// the left.
///
/// Returns the resulting `Byte` value after the shift operation.
pub fn Byte::op_shl(self : Byte, count : Int) -> Byte {
(self.to_int() << count).to_byte()
}
///|
/// Shifts the bits of the `Byte` value to the right by the specified number of
/// positions.
///
/// Parameters:
///
/// - `byte` : The `Byte` value whose bits are to be shifted.
/// - `count` : The number of bit positions to shift the `byte` value to the
/// right.
///
/// Returns the resulting `Byte` value after the bitwise right shift operation.
pub fn Byte::op_shr(self : Byte, count : Int) -> Byte {
(self.to_uint() >> count).reinterpret_as_int().to_byte()
}
///|
/// positions.
///
/// Parameters:
///
/// - `byte_value` : The `Byte` value whose bits are to be shifted.
/// - `shift_count` : The number of bit positions to shift the `byte_value` to
/// the left.
///
/// Returns the resulting `Byte` value after the bitwise left shift operation.
///
/// @alert deprecated "Use infix operator `<<` instead"
/// @coverage.skip
pub fn Byte::lsl(self : Byte, count : Int) -> Byte {
(self.to_int() << count).to_byte()
}
///|
/// bits.
///
/// Parameters:
///
/// - `value` : The `Byte` value to be shifted.
/// - `count` : The number of bits to shift the `value` to the right.
///
/// Returns the result of the logical shift right operation as a `Byte`.
///
/// @alert deprecated "Use infix operator `>>` instead"
/// @coverage.skip
pub fn Byte::lsr(self : Byte, count : Int) -> Byte {
(self.to_uint() >> count).reinterpret_as_int().to_byte()
}