Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add converter between UInt/UInt64/Int/Int64/Float/Double and Bytes. #1341

Merged
merged 5 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions builtin/builtin.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ impl BytesView {
length(Self) -> Int
op_as_view(Self, start~ : Int = .., end? : Int) -> Self
op_get(Self, Int) -> Byte
to_double_be(Self) -> Double
to_double_le(Self) -> Double
to_float_be(Self) -> Float
to_float_le(Self) -> Float
to_int64_be(Self) -> Int64
to_int64_le(Self) -> Int64
to_int_be(Self) -> Int
to_int_le(Self) -> Int
to_uint64_be(Self) -> UInt64
to_uint64_le(Self) -> UInt64
to_uint_be(Self) -> UInt
to_uint_le(Self) -> UInt
unsafe_get(Self, Int) -> Byte
}
impl Show for BytesView
Expand Down
80 changes: 80 additions & 0 deletions builtin/bytesview.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,83 @@ pub fn iter(self : BytesView) -> Iter[Byte] {
}
})
}

///| Converts the 4 bytes long BytesView to a UInt in big-endian byte order.
pub fn to_uint_be(self : BytesView) -> UInt {
(self[0].to_uint() << 24) +
(self[1].to_uint() << 16) +
(self[2].to_uint() << 8) +
self[3].to_uint()
}

///| Converts the 4 bytes long BytesView to a UInt in little-endian byte order.
pub fn to_uint_le(self : BytesView) -> UInt {
self[0].to_uint() +
(self[1].to_uint() << 8) +
(self[2].to_uint() << 16) +
(self[3].to_uint() << 24)
}

///| Converts the 8 bytes long BytesView to a UInt64 in big-endian byte order.
pub fn to_uint64_be(self : BytesView) -> UInt64 {
(self[0].to_uint().to_uint64() << 56) +
(self[1].to_uint().to_uint64() << 48) +
(self[2].to_uint().to_uint64() << 40) +
(self[3].to_uint().to_uint64() << 32) +
(self[4].to_uint().to_uint64() << 24) +
(self[5].to_uint().to_uint64() << 16) +
(self[6].to_uint().to_uint64() << 8) +
self[7].to_uint().to_uint64()
}

///| Converts the 8 bytes long BytesView to a UInt64 in little-endian byte order.
pub fn to_uint64_le(self : BytesView) -> UInt64 {
self[0].to_uint().to_uint64() +
(self[1].to_uint().to_uint64() << 8) +
(self[2].to_uint().to_uint64() << 16) +
(self[3].to_uint().to_uint64() << 24) +
(self[4].to_uint().to_uint64() << 32) +
(self[5].to_uint().to_uint64() << 40) +
(self[6].to_uint().to_uint64() << 48) +
(self[7].to_uint().to_uint64() << 56)
}

///| Converts the 4 bytes long BytesView to a Int in big-endian byte order.
pub fn to_int_be(self : BytesView) -> Int {
self.to_uint_be().reinterpret_as_int()
}

///| Converts the 4 bytes long BytesView to a Int in little-endian byte order.
pub fn to_int_le(self : BytesView) -> Int {
self.to_uint_le().reinterpret_as_int()
}

///| Converts the 8 bytes long BytesView to a Int64 in big-endian byte order.
pub fn to_int64_be(self : BytesView) -> Int64 {
self.to_uint64_be().reinterpret_as_int64()
}

///| Converts the 8 bytes long BytesView to a Int64 in little-endian byte order.
pub fn to_int64_le(self : BytesView) -> Int64 {
self.to_uint64_le().reinterpret_as_int64()
}

///| Converts the 4 bytes long BytesView to a Float in big-endian byte order.
pub fn to_float_be(self : BytesView) -> Float {
self.to_uint_be().reinterpret_as_float()
}

///| Converts the 4 bytes long BytesView to a Float in little-endian byte order.
pub fn to_float_le(self : BytesView) -> Float {
self.to_uint_le().reinterpret_as_float()
}

///| Converts the 8 bytes long BytesView to a Double in big-endian byte order.
pub fn to_double_be(self : BytesView) -> Double {
self.to_uint64_be().reinterpret_as_double()
}

///| Converts the 8 bytes long BytesView to a Double in little-endian byte order.
pub fn to_double_le(self : BytesView) -> Double {
self.to_uint64_le().reinterpret_as_double()
}
10 changes: 10 additions & 0 deletions double/double.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,13 @@ pub fn is_close(
) ||
diff <= absolute_tolerance
}

///| Converts the Double to a Bytes in big-endian byte order.
pub fn to_be_bytes(self : Double) -> Bytes {
self.reinterpret_as_uint64().to_be_bytes()
}

///| Converts the Double to a Bytes in little-endian byte order.
pub fn to_le_bytes(self : Double) -> Bytes {
self.reinterpret_as_uint64().to_le_bytes()
}
2 changes: 2 additions & 0 deletions double/double.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ impl Double {
pow(Double, Double) -> Double
round(Double) -> Double
signum(Double) -> Double
to_be_bytes(Double) -> Bytes
to_le_bytes(Double) -> Bytes
to_string(Double) -> String
trunc(Double) -> Double
}
Expand Down
1 change: 1 addition & 0 deletions double/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/int64",
"moonbitlang/core/uint64",
"moonbitlang/core/double/internal/ryu"
],
"targets": {
Expand Down
10 changes: 10 additions & 0 deletions float/float.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,13 @@ pub impl Hash for Float with hash(self) {
pub impl Hash for Float with hash_combine(self, hasher) {
hasher.combine_float(self)
}

///| Converts the Float to a Bytes in big-endian byte order.
pub fn to_be_bytes(self : Float) -> Bytes {
self.reinterpret_as_uint().to_be_bytes()
}

///| Converts the Float to a Bytes in little-endian byte order.
pub fn to_le_bytes(self : Float) -> Bytes {
self.reinterpret_as_uint().to_le_bytes()
}
2 changes: 2 additions & 0 deletions float/float.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ impl Float {
op_mod(Float, Float) -> Float
pow(Float, Float) -> Float
round(Float) -> Float
to_be_bytes(Float) -> Bytes
to_int(Float) -> Int
to_le_bytes(Float) -> Bytes
trunc(Float) -> Float
}

Expand Down
2 changes: 1 addition & 1 deletion float/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"import": ["moonbitlang/core/builtin", "moonbitlang/core/double"],
"import": ["moonbitlang/core/builtin", "moonbitlang/core/double", "moonbitlang/core/uint"],
"test-import": ["moonbitlang/core/quickcheck"],
"targets": {
"round_js.mbt": ["js"],
Expand Down
10 changes: 10 additions & 0 deletions int/int.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,13 @@ pub fn abs(self : Int) -> Int {
self
}
}

///| Converts the Int to a Bytes in big-endian byte order.
pub fn to_be_bytes(self : Int) -> Bytes {
self.reinterpret_as_uint().to_be_bytes()
}

///| Converts the Int to a Bytes in little-endian byte order.
pub fn to_le_bytes(self : Int) -> Bytes {
self.reinterpret_as_uint().to_le_bytes()
}
2 changes: 2 additions & 0 deletions int/int.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ let min_value : Int

impl Int {
abs(Int) -> Int
to_be_bytes(Int) -> Bytes
to_le_bytes(Int) -> Bytes
}

// Type aliases
Expand Down
2 changes: 1 addition & 1 deletion int/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"import": ["moonbitlang/core/builtin"]
"import": ["moonbitlang/core/builtin", "moonbitlang/core/uint"]
}
10 changes: 10 additions & 0 deletions int64/int64.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,13 @@ pub fn abs(self : Int64) -> Int64 {
self
}
}

///| Converts the Int64 to a Bytes in big-endian byte order.
pub fn to_be_bytes(self : Int64) -> Bytes {
self.reinterpret_as_uint64().to_be_bytes()
}

///| Converts the Int64 to a Bytes in little-endian byte order.
pub fn to_le_bytes(self : Int64) -> Bytes {
self.reinterpret_as_uint64().to_le_bytes()
}
2 changes: 2 additions & 0 deletions int64/int64.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ let min_value : Int64
impl Int64 {
abs(Int64) -> Int64
from_int(Int) -> Int64
to_be_bytes(Int64) -> Bytes
to_le_bytes(Int64) -> Bytes
}

// Type aliases
Expand Down
2 changes: 1 addition & 1 deletion int64/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"import": ["moonbitlang/core/builtin", "moonbitlang/core/bytes", "moonbitlang/core/uint"]
"import": ["moonbitlang/core/builtin", "moonbitlang/core/bytes", "moonbitlang/core/uint", "moonbitlang/core/uint64"]
}
20 changes: 20 additions & 0 deletions uint/uint.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,23 @@ pub fn to_int64(self : UInt) -> Int64 {
pub fn UInt::default() -> UInt {
0
}

///| Converts the UInt to a Bytes in big-endian byte order.
pub fn to_be_bytes(self : UInt) -> Bytes {
[
(self >> 24).to_byte(),
(self >> 16).to_byte(),
(self >> 8).to_byte(),
self.to_byte(),
]
}

///| Converts the UInt to a Bytes in little-endian byte order.
pub fn to_le_bytes(self : UInt) -> Bytes {
[
self.to_byte(),
(self >> 8).to_byte(),
(self >> 16).to_byte(),
(self >> 24).to_byte(),
]
}
2 changes: 2 additions & 0 deletions uint/uint.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ let min_value : UInt

impl UInt {
default() -> UInt
to_be_bytes(UInt) -> Bytes
to_int64(UInt) -> Int64
to_le_bytes(UInt) -> Bytes
}

// Type aliases
Expand Down
28 changes: 28 additions & 0 deletions uint64/uint64.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,31 @@ pub let min_value : UInt64 = 0UL

///|
pub let max_value : UInt64 = 18446744073709551615UL

///| Converts the UInt64 to a Bytes in big-endian byte order.
pub fn to_be_bytes(self : UInt64) -> Bytes {
[
(self >> 56).to_byte(),
(self >> 48).to_byte(),
(self >> 40).to_byte(),
(self >> 32).to_byte(),
(self >> 24).to_byte(),
(self >> 16).to_byte(),
(self >> 8).to_byte(),
self.to_byte(),
]
}

///| Converts the UInt64 to a Bytes in little-endian byte order.
pub fn to_le_bytes(self : UInt64) -> Bytes {
[
self.to_byte(),
(self >> 8).to_byte(),
(self >> 16).to_byte(),
(self >> 24).to_byte(),
(self >> 32).to_byte(),
(self >> 40).to_byte(),
(self >> 48).to_byte(),
(self >> 56).to_byte(),
]
}
6 changes: 6 additions & 0 deletions uint64/uint64.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ let min_value : UInt64

// Types and methods


impl UInt64 {
to_be_bytes(UInt64) -> Bytes
to_le_bytes(UInt64) -> Bytes
}

// Type aliases

// Traits
Expand Down
Loading