1///| Base64 character set used for Base64 VLQ encoding.
2/// Standard Base64 alphabet: A-Z, a-z, 0-9, +, /
3const String
Base64 character set used for Base64 VLQ encoding.
Standard Base64 alphabet: A-Z, a-z, 0-9, +, /
BASE64_CHARS : String
String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
4
5///| Error types for VLQ operations.
6///
7/// - `Overflow`: The decoded number exceeds the maximum supported value (0x01FFFFFF)
8/// - `IncompleteSequence`: The VLQ sequence is incomplete (missing continuation bytes)
9/// - `InvalidBase64Character`: An invalid character was encountered during Base64 VLQ decoding
10pub(all) suberror VlqError {
11 VlqError
Overflow
12 VlqError
IncompleteSequence
13 (Char) -> VlqError
InvalidBase64Character(Char
Char)
14} derive(trait Eq {
op_equal(Self, Self) -> Bool
}
Trait for types whose elements can test for equality
Eq, trait Show {
output(Self, &Logger) -> Unit
to_string(Self) -> String
}
Trait for types that can be converted to String
Show)
15
16///| Encodes an array of unsigned integers into Variable Length Quantity (VLQ) byte format.
17///
18pub fn (numbers : Array[UInt]) -> Array[Byte]
Encodes an array of unsigned integers into Variable Length Quantity (VLQ) byte format.
to_vlq(Array[UInt]
numbers : type Array[T]
An Array is a collection of values that supports random access and can
grow in size.
Array[UInt
UInt]) -> type Array[T]
An Array is a collection of values that supports random access and can
grow in size.
Array[Byte
Byte] {
19 let Array[Byte]
bytes : type Array[T]
An Array is a collection of values that supports random access and can
grow in size.
Array[Byte
Byte] = []
20 for UInt
number in Array[UInt]
numbers {
21 let mut UInt
value = UInt
number
22 let Array[Byte]
encoded_bytes = []
23 while true {
24 let Byte
seven_bits = (UInt
value (UInt, UInt) -> UInt
& 0x7F).(self : UInt) -> Byte
Converts an unsigned 32-bit integer to a byte by taking its least significant
8 bits. Any bits beyond the first 8 bits are truncated.
Parameters:
self : The unsigned 32-bit integer to be converted. Only the least
significant 8 bits will be used.
Returns a byte containing the least significant 8 bits of the input integer.
Example:
let n = 258U // In binary: 100000010
inspect(n.to_byte(), content="b'\\x02'") // Only keeps 00000010
let big = 4294967295U // Maximum value of UInt
inspect(big.to_byte(), content="b'\\xFF'") // Only keeps 11111111
to_byte()
25 Array[Byte]
encoded_bytes.(self : Array[Byte], value : Byte) -> Unit
Adds an element to the end of the array.
If the array is at capacity, it will be reallocated.
Example
let v = []
v.push(3)
push(Byte
seven_bits)
26 UInt
value = UInt
value (self : UInt, shift : Int) -> UInt
Performs a logical right shift operation on an unsigned 32-bit integer. The
operation shifts all bits to the right by a specified number of positions,
filling the leftmost positions with zeros.
Parameters:
self : The unsigned 32-bit integer to be shifted.
shift : The number of positions to shift right. If this value is
negative, the behavior is undefined. Values larger than 31 are masked with & 31.
Returns a new unsigned 32-bit integer containing the result of the right
shift operation.
Example:
let x = 0xFF000000U
inspect(x >> 8, content="16711680") // 0x00FF0000
inspect(x >> 24, content="255") // 0x000000FF
let x = 0xFF000000U
inspect(x >> 32, content="4278190080") // Same as x >> 0 due to masking
>> 7
27 if UInt
value (self : UInt, other : UInt) -> Bool
Compares two unsigned 32-bit integers for equality.
Parameters:
self : The first unsigned integer operand.
other : The second unsigned integer operand to compare with.
Returns true if both integers have the same value, false otherwise.
Example:
let a = 42U
let b = 42U
let c = 24U
inspect(a == b, content="true")
inspect(a == c, content="false")
== 0 {
28 break
29 }
30 }
31 Array[Byte]
encoded_bytes.(self : Array[Byte]) -> Unit
Reverses the order of elements in an array in place, modifying the original
array.
Parameters:
self : The array to be reversed.
Example:
let arr = [1, 2, 3, 4, 5]
arr.rev_inplace()
inspect(arr, content="[5, 4, 3, 2, 1]")
let arr : Array[Int] = []
arr.rev_inplace()
inspect(arr, content="[]")
rev_inplace()
32 let Int
len = Array[Byte]
encoded_bytes.(self : Array[Byte]) -> Int
Returns the number of elements in the array.
Parameters:
array : The array whose length is to be determined.
Returns the number of elements in the array as an integer.
Example:
let arr = [1, 2, 3]
inspect(arr.length(), content="3")
let empty : Array[Int] = []
inspect(empty.length(), content="0")
length()
33 if Int
len (self_ : Int, other : Int) -> Bool
> 1 {
34 Array[Byte]
encoded_bytes[0:Int
len (self : Int, other : Int) -> Int
Performs subtraction between two 32-bit integers, following standard two's
complement arithmetic rules. When the result overflows or underflows, it
wraps around within the 32-bit integer range.
Parameters:
self : The minuend (the number being subtracted from).
other : The subtrahend (the number to subtract).
Returns the difference between self and other.
Example:
let a = 42
let b = 10
inspect(a - b, content="32")
let max = 2147483647 // Int maximum value
inspect(max - -1, content="-2147483648") // Overflow case
- 1].(self : ArrayView[Byte], f : (Byte) -> Byte) -> Unit
Maps a function over the elements of the array view in place.
Example
let v = [3, 4, 5]
v[1:].map_inplace(fn (x) {x + 1})
assert_eq(v, [3, 5, 6])
map_inplace(Byte
x => Byte
x (self : Byte, that : Byte) -> 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.
| 0x80)
35 }
36 Array[Byte]
encoded_bytes.(self : Array[Byte], f : (Byte) -> Unit) -> Unit
Iterates through each element of the array in order, applying the given
function to each element.
Parameters:
array : The array to iterate over.
function : A function that takes a single element of type T as input
and returns Unit. This function is applied to each element of the array in
order.
Example:
let arr = [1, 2, 3]
let mut sum = 0
arr.each(fn(x) { sum = sum + x })
inspect(sum, content="6")
each(Array[Byte]
bytes.(self : Array[Byte], value : Byte) -> Unit
Adds an element to the end of the array.
If the array is at capacity, it will be reallocated.
Example
let v = []
v.push(3)
push(_))
37 }
38 Array[Byte]
bytes
39}
40
41///| Decodes Variable Length Quantity (VLQ) bytes back into unsigned integers.
42///
43/// # Errors:
44/// - Overflow: If a number exceeds 0x01FFFFFF (28 bits)
45/// - IncompleteSequence: If the sequence ends with a continuation byte
46pub fn (bytes : Array[Byte]) -> Array[UInt] raise VlqError
Decodes Variable Length Quantity (VLQ) bytes back into unsigned integers.
Errors:
- Overflow: If a number exceeds 0x01FFFFFF (28 bits)
- IncompleteSequence: If the sequence ends with a continuation byte
from_vlq(Array[Byte]
bytes : type Array[T]
An Array is a collection of values that supports random access and can
grow in size.
Array[Byte
Byte]) -> type Array[T]
An Array is a collection of values that supports random access and can
grow in size.
Array[UInt
UInt] raise type! VlqError {
Overflow
IncompleteSequence
InvalidBase64Character(Char)
}
Error types for VLQ operations.
Overflow: The decoded number exceeds the maximum supported value (0x01FFFFFF)
IncompleteSequence: The VLQ sequence is incomplete (missing continuation bytes)
InvalidBase64Character: An invalid character was encountered during Base64 VLQ decoding
VlqError {
47 let Array[UInt]
numbers : type Array[T]
An Array is a collection of values that supports random access and can
grow in size.
Array[UInt
UInt] = []
48 let mut UInt
current_number : UInt
UInt = 0
49 let mut Bool
in_sequence = false
50 for Byte
byte in Array[Byte]
bytes {
51 Bool
in_sequence = true
52 guard UInt
current_number (self_ : UInt, other : UInt) -> Bool
<= 0x01FFFFFF else { raise VlqError
Overflow }
53 let UInt
seven_bits = (Byte
byte (self : Byte, that : Byte) -> 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.
& 0x7F).(self : Byte) -> UInt
Converts a Byte to a UInt.
Parameters:
byte : The Byte value to be converted.
Returns the UInt representation of the Byte.
to_uint()
54 UInt
current_number = (UInt
current_number (self : UInt, shift : Int) -> UInt
Performs a left shift operation on an unsigned 32-bit integer. Each bit in
the integer is shifted left by the specified number of positions, and zeros
are filled in from the right.
Parameters:
self : The unsigned 32-bit integer to be shifted.
shift : The number of positions to shift. Only the least significant 5
bits are used, effectively making the shift count always between 0 and 31.
Returns a new unsigned 32-bit integer that is the result of shifting self
left by shift positions.
Example:
let x = 1U
inspect(x << 3, content="8") // Binary: 1 -> 1000
let y = 0xFFFFFFFFU
inspect(y << 16, content="4294901760") // All bits after position 16 are discarded
<< 7) (UInt, UInt) -> UInt
| UInt
seven_bits
55 if (Byte
byte (self : Byte, that : Byte) -> 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.
& 0x80) (self : Byte, that : Byte) -> Bool
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.
== 0 {
56 Array[UInt]
numbers.(self : Array[UInt], value : UInt) -> Unit
Adds an element to the end of the array.
If the array is at capacity, it will be reallocated.
Example
let v = []
v.push(3)
push(UInt
current_number)
57 UInt
current_number = 0
58 Bool
in_sequence = false
59 }
60 }
61 guard (x : Bool) -> Bool
Performs logical negation on a boolean value.
Parameters:
value : The boolean value to negate.
Returns the logical NOT of the input value: true if the input is false,
and false if the input is true.
Example:
inspect(not(true), content="false")
inspect(not(false), content="true")
not(Bool
in_sequence) else { raise VlqError
IncompleteSequence }
62 Array[UInt]
numbers
63}
64
65///| Creates a mapping from Base64 characters to their corresponding values (0-63).
66/// Used for efficient Base64 VLQ decoding.
67fn () -> Map[Char, Int]
Creates a mapping from Base64 characters to their corresponding values (0-63).
Used for efficient Base64 VLQ decoding.
get_base64_map() -> type Map[K, V]
Mutable linked hash map that maintains the order of insertion, not thread safe.
Example
let map = { 3: "three", 8 : "eight", 1 : "one"}
assert_eq(map.get(2), None)
assert_eq(map.get(3), Some("three"))
map.set(3, "updated")
assert_eq(map.get(3), Some("updated"))
Map[Char
Char, Int
Int] {
68 let Map[Char, Int]
map = type Map[K, V]
Mutable linked hash map that maintains the order of insertion, not thread safe.
Example
let map = { 3: "three", 8 : "eight", 1 : "one"}
assert_eq(map.get(2), None)
assert_eq(map.get(3), Some("three"))
map.set(3, "updated")
assert_eq(map.get(3), Some("updated"))
Map::(capacity~ : Int = ..) -> Map[Char, Int]
Create a hash map.
The capacity of the map will be the smallest power of 2 that is
greater than or equal to the provided [capacity].
new()
69 String
Base64 character set used for Base64 VLQ encoding.
Standard Base64 alphabet: A-Z, a-z, 0-9, +, /
BASE64_CHARS.(self : String) -> Iter[Char]
Returns an iterator over the Unicode characters in the string.
Note: This iterator yields Unicode characters, not Utf16 code units.
As a result, the count of characters returned by iter().count() may not be equal to the length of the string returned by length().
let s = "Hello, World!🤣";
assert_eq(s.iter().count(), 14); // Unicode characters
assert_eq(s.length(), 15); // Utf16 code units
iter().(self : Iter[Char], f : (Int, Char) -> Unit) -> Unit
Iterates over each element in the iterator, applying the function f to each element with index.
Type Parameters
T: The type of the elements in the iterator.
Arguments
self: The iterator to consume.
f: A function that takes an index of type Int and an element of type T and returns Unit. This function is applied to each element of the iterator.
TODO: Add intrinsic
eachi((Int
i, Char
c) => Map[Char, Int]
map(Map[Char, Int], Char, Int) -> Unit
[c] = Int
i)
70 Map[Char, Int]
map
71}
72
73///| Encodes a single signed integer to Base64 VLQ format.
74///
75/// Converts the number to sign-magnitude format, then encodes using 5-bit groups
76/// with continuation bits, and finally converts to Base64 characters.
77fn (number : Int) -> String
Encodes a single signed integer to Base64 VLQ format.
Converts the number to sign-magnitude format, then encodes using 5-bit groups
with continuation bits, and finally converts to Base64 characters.
encode_single_to_base64_vlq(Int
number : Int
Int) -> String
String {
78 let mut Int
vlq = if Int
number (self_ : Int, other : Int) -> Bool
< 0 { ((self : Int) -> Int
Performs arithmetic negation on an integer value, returning its additive
inverse.
Parameters:
self : The integer value to negate.
Returns the negation of the input value. For all inputs except
Int::min_value(), returns the value with opposite sign. When the input is
Int::min_value(), returns Int::min_value() due to two's complement
representation.
Example:
inspect(-42, content="-42")
inspect(42, content="42")
inspect(--2147483647, content="2147483647") // negating near min value
-Int
number (self : Int, other : Int) -> Int
Performs a left shift operation on a 32-bit integer. Shifts each bit in the
integer to the left by the specified number of positions, filling the
rightmost positions with zeros.
Parameters:
self : The integer value to be shifted.
shift : The number of positions to shift. Must be a non-negative value
less than 32. Values outside this range will be masked with & 31.
Returns a new integer with bits shifted left by the specified number of
positions. For each position shifted, the rightmost bit is filled with 0, and
the leftmost bit is discarded.
Example:
let x = 1
inspect(x << 3, content="8") // Binary: 1 -> 1000
let y = -4
inspect(y << 2, content="-16") // Binary: 100 -> 10000
<< 1) (Int, Int) -> Int
| 1 } else { Int
number (self : Int, other : Int) -> Int
Performs a left shift operation on a 32-bit integer. Shifts each bit in the
integer to the left by the specified number of positions, filling the
rightmost positions with zeros.
Parameters:
self : The integer value to be shifted.
shift : The number of positions to shift. Must be a non-negative value
less than 32. Values outside this range will be masked with & 31.
Returns a new integer with bits shifted left by the specified number of
positions. For each position shifted, the rightmost bit is filled with 0, and
the leftmost bit is discarded.
Example:
let x = 1
inspect(x << 3, content="8") // Binary: 1 -> 1000
let y = -4
inspect(y << 2, content="-16") // Binary: 100 -> 10000
<< 1 }
79 guard Int
vlq (x : Int, y : Int) -> Bool
!= 0 else { "A" }
80 let mut String
encoded = ""
81 while Int
vlq (self_ : Int, other : Int) -> Bool
> 0 {
82 let mut Int
digit = Int
vlq (Int, Int) -> Int
& 0b11111
83 Int
vlq = Int
vlq (self : Int, other : Int) -> Int
Performs an arithmetic right shift operation on an integer value. Shifts the
bits of the first operand to the right by the number of positions specified
by the second operand. The sign bit is preserved and copied to the leftmost
positions.
Parameters:
self : The integer value to be shifted.
shift : The number of positions to shift the bits to the right. Must be
non-negative.
Returns an integer representing the result of the arithmetic right shift
operation.
Example:
let n = -16
inspect(n >> 2, content="-4") // Sign bit is preserved during shift
let p = 16
inspect(p >> 2, content="4") // Regular right shift for positive numbers
>> 5
84 if Int
vlq (self_ : Int, other : Int) -> Bool
> 0 {
85 Int
digit = Int
digit (Int, Int) -> Int
| 0b100000
86 }
87 String
encoded = String
encoded (self : String, other : String) -> String
Concatenates two strings, creating a new string that contains all characters
from the first string followed by all characters from the second string.
Parameters:
self : The first string to concatenate.
other : The second string to concatenate.
Returns a new string containing the concatenation of both input strings.
Example:
let hello = "Hello"
let world = " World!"
inspect(hello + world, content="Hello World!")
inspect("" + "abc", content="abc") // concatenating with empty string
+ String
Base64 character set used for Base64 VLQ encoding.
Standard Base64 alphabet: A-Z, a-z, 0-9, +, /
BASE64_CHARS.(self : String, offset : Int) -> Char
Returns the Unicode character at the given offset. Note this is not the n-th character.
This has O(1) complexity.
char_at(Int
digit).(self : Char) -> String
Convert Char to String
to_string()
88 }
89 String
encoded
90}
91
92///| Encodes an array of signed integers into Base64 VLQ format.
93///
94/// # Example
95/// ```moonbit
96/// let numbers = [1, -1, 16]
97/// let encoded = to_base64_vlq(numbers)
98/// inspect(encoded, content="CDgB")
99/// ```
100///
101pub fn (numbers : Array[Int]) -> String
Encodes an array of signed integers into Base64 VLQ format.
Example
let numbers = [1, -1, 16]
let encoded = to_base64_vlq(numbers)
inspect(encoded, content="CDgB")
to_base64_vlq(Array[Int]
numbers : type Array[T]
An Array is a collection of values that supports random access and can
grow in size.
Array[Int
Int]) -> String
String {
102 let mut String
result = ""
103 Array[Int]
numbers.(self : Array[Int], f : (Int) -> Unit) -> Unit
Iterates through each element of the array in order, applying the given
function to each element.
Parameters:
array : The array to iterate over.
function : A function that takes a single element of type T as input
and returns Unit. This function is applied to each element of the array in
order.
Example:
let arr = [1, 2, 3]
let mut sum = 0
arr.each(fn(x) { sum = sum + x })
inspect(sum, content="6")
each(Int
n => String
result = String
result (self : String, other : String) -> String
Concatenates two strings, creating a new string that contains all characters
from the first string followed by all characters from the second string.
Parameters:
self : The first string to concatenate.
other : The second string to concatenate.
Returns a new string containing the concatenation of both input strings.
Example:
let hello = "Hello"
let world = " World!"
inspect(hello + world, content="Hello World!")
inspect("" + "abc", content="abc") // concatenating with empty string
+ (number : Int) -> String
Encodes a single signed integer to Base64 VLQ format.
Converts the number to sign-magnitude format, then encodes using 5-bit groups
with continuation bits, and finally converts to Base64 characters.
encode_single_to_base64_vlq(Int
n))
104 String
result
105}
106
107///| Decodes a Base64 VLQ string back into signed integers.
108///
109/// # Example
110/// ```moonbit
111/// let encoded = "CDgB"
112/// let decoded = try? from_base64_vlq(encoded)
113/// inspect(decoded, content="Ok([1, -1, 16])")
114/// ```
115///
116/// # Errors
117/// - `IncompleteSequence`: If the string ends with a continuation character
118/// - `InvalidBase64Character`: If an invalid Base64 character is encountered
119pub fn (s : String) -> Array[Int] raise VlqError
Decodes a Base64 VLQ string back into signed integers.
Example
let encoded = "CDgB"
let decoded = try? from_base64_vlq(encoded)
inspect(decoded, content="Ok([1, -1, 16])")
Errors
IncompleteSequence: If the string ends with a continuation character
InvalidBase64Character: If an invalid Base64 character is encountered
from_base64_vlq(String
s : String
String) -> type Array[T]
An Array is a collection of values that supports random access and can
grow in size.
Array[Int
Int] raise type! VlqError {
Overflow
IncompleteSequence
InvalidBase64Character(Char)
}
Error types for VLQ operations.
Overflow: The decoded number exceeds the maximum supported value (0x01FFFFFF)
IncompleteSequence: The VLQ sequence is incomplete (missing continuation bytes)
InvalidBase64Character: An invalid character was encountered during Base64 VLQ decoding
VlqError {
120 let Array[Int]
numbers = []
121 let mut Int
i = 0
122 let Int
len = String
s.(self : String) -> Int
Returns the number of UTF-16 code units in the string. Note that this is not
necessarily equal to the number of Unicode characters (code points) in the
string, as some characters may be represented by multiple UTF-16 code units.
Parameters:
string : The string whose length is to be determined.
Returns the number of UTF-16 code units in the string.
Example:
inspect("hello".length(), content="5")
inspect("🤣".length(), content="2") // Emoji uses two UTF-16 code units
inspect("".length(), content="0") // Empty string
length()
123 while Int
i (self_ : Int, other : Int) -> Bool
< Int
len {
124 let mut Int
vlq = 0
125 let mut Int
shift = 0
126 let mut Bool
continuation = true
127 while Bool
continuation {
128 guard Int
i (self_ : Int, other : Int) -> Bool
< Int
len else { raise VlqError
IncompleteSequence }
129 let Char
char = String
s.(self : String, offset : Int) -> Char
Returns the Unicode character at the given offset. Note this is not the n-th character.
This has O(1) complexity.
char_at(Int
i)
130 Int
i = Int
i (self : Int, other : Int) -> Int
Adds two 32-bit signed integers. Performs two's complement arithmetic, which
means the operation will wrap around if the result exceeds the range of a
32-bit integer.
Parameters:
self : The first integer operand.
other : The second integer operand.
Returns a new integer that is the sum of the two operands. If the
mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to
2,147,483,647), the result wraps around according to two's complement rules.
Example:
inspect(42 + 1, content="43")
inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+ 1
131 match () -> Map[Char, Int]
Creates a mapping from Base64 characters to their corresponding values (0-63).
Used for efficient Base64 VLQ decoding.
get_base64_map().(self : Map[Char, Int], key : Char) -> Int?
Get the value associated with a key.
get(Char
char) {
132 Int?
None => raise (Char) -> VlqError
InvalidBase64Character(Char
char)
133 (Int) -> Int?
Some(Int
digit) => {
134 let Int
value_part = Int
digit (Int, Int) -> Int
& 0b11111
135 Int
vlq = Int
vlq (Int, Int) -> Int
| (Int
value_part (self : Int, other : Int) -> Int
Performs a left shift operation on a 32-bit integer. Shifts each bit in the
integer to the left by the specified number of positions, filling the
rightmost positions with zeros.
Parameters:
self : The integer value to be shifted.
shift : The number of positions to shift. Must be a non-negative value
less than 32. Values outside this range will be masked with & 31.
Returns a new integer with bits shifted left by the specified number of
positions. For each position shifted, the rightmost bit is filled with 0, and
the leftmost bit is discarded.
Example:
let x = 1
inspect(x << 3, content="8") // Binary: 1 -> 1000
let y = -4
inspect(y << 2, content="-16") // Binary: 100 -> 10000
<< Int
shift)
136 Int
shift = Int
shift (self : Int, other : Int) -> Int
Adds two 32-bit signed integers. Performs two's complement arithmetic, which
means the operation will wrap around if the result exceeds the range of a
32-bit integer.
Parameters:
self : The first integer operand.
other : The second integer operand.
Returns a new integer that is the sum of the two operands. If the
mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to
2,147,483,647), the result wraps around according to two's complement rules.
Example:
inspect(42 + 1, content="43")
inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+ 5
137 if (Int
digit (Int, Int) -> Int
& 0b100000) (self : Int, other : Int) -> Bool
Compares two integers for equality.
Parameters:
self : The first integer to compare.
other : The second integer to compare.
Returns true if both integers have the same value, false otherwise.
Example:
inspect(42 == 42, content="true")
inspect(42 == -42, content="false")
== 0 {
138 Bool
continuation = false
139 }
140 }
141 }
142 }
143 let Bool
is_negative = (Int
vlq (Int, Int) -> Int
& 1) (self : Int, other : Int) -> Bool
Compares two integers for equality.
Parameters:
self : The first integer to compare.
other : The second integer to compare.
Returns true if both integers have the same value, false otherwise.
Example:
inspect(42 == 42, content="true")
inspect(42 == -42, content="false")
== 1
144 Int
vlq = Int
vlq (self : Int, other : Int) -> Int
Performs an arithmetic right shift operation on an integer value. Shifts the
bits of the first operand to the right by the number of positions specified
by the second operand. The sign bit is preserved and copied to the leftmost
positions.
Parameters:
self : The integer value to be shifted.
shift : The number of positions to shift the bits to the right. Must be
non-negative.
Returns an integer representing the result of the arithmetic right shift
operation.
Example:
let n = -16
inspect(n >> 2, content="-4") // Sign bit is preserved during shift
let p = 16
inspect(p >> 2, content="4") // Regular right shift for positive numbers
>> 1
145 let Int
number = if Bool
is_negative { (self : Int) -> Int
Performs arithmetic negation on an integer value, returning its additive
inverse.
Parameters:
self : The integer value to negate.
Returns the negation of the input value. For all inputs except
Int::min_value(), returns the value with opposite sign. When the input is
Int::min_value(), returns Int::min_value() due to two's complement
representation.
Example:
inspect(-42, content="-42")
inspect(42, content="42")
inspect(--2147483647, content="2147483647") // negating near min value
-Int
vlq } else { Int
vlq }
146 Array[Int]
numbers.(self : Array[Int], value : Int) -> Unit
Adds an element to the end of the array.
If the array is at capacity, it will be reallocated.
Example
let v = []
v.push(3)
push(Int
number)
147 }
148 Array[Int]
numbers
149}
150