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 more write_X method and some bytes conversions to buffer #1307

Merged
merged 14 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
46 changes: 46 additions & 0 deletions buffer/buffer.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,52 @@ pub fn write_string(self : T, value : String) -> Unit {
self.len += value.length() * 2
}

///|
/// Write an UInt64 into buffer.
pub fn write_uint64(self : T, value : UInt64) -> Unit {
self.write_byte(value.to_byte())
self.write_byte((value >> 8).to_byte())
self.write_byte((value >> 16).to_byte())
self.write_byte((value >> 24).to_byte())
self.write_byte((value >> 32).to_byte())
self.write_byte((value >> 40).to_byte())
self.write_byte((value >> 48).to_byte())
self.write_byte((value >> 56).to_byte())
}

///|
/// Write an Int64 into buffer.
pub fn write_int64(self : T, value : Int64) -> Unit {
self.write_uint64(value.reinterpret_as_uint64())
}

///|
/// Write an UInt into buffer.
pub fn write_uint(self : T, value : UInt) -> Unit {
self.write_byte(value.to_byte())
self.write_byte((value >> 8).to_byte())
self.write_byte((value >> 16).to_byte())
self.write_byte((value >> 24).to_byte())
}

///|
/// Write an Int into buffer.
pub fn write_int(self : T, value : Int) -> Unit {
self.write_uint(value.reinterpret_as_uint())
}

///|
/// Write a Double into buffer.
pub fn write_double(self : T, value : Double) -> Unit {
self.write_uint64(value.reinterpret_as_uint64())
}

///|
/// Write a Float into buffer.
pub fn write_float(self : T, value : Float) -> Unit {
self.write_uint(value.reinterpret_as_uint())
}

///|
pub fn write_object(self : T, value : Show) -> Unit {
self.write_string(value.to_string())
Expand Down
6 changes: 6 additions & 0 deletions buffer/buffer.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ impl T {
write_byte(Self, Byte) -> Unit
write_bytes(Self, Bytes) -> Unit
write_char(Self, Char) -> Unit
write_double(Self, Double) -> Unit
write_float(Self, Float) -> Unit
write_int(Self, Int) -> Unit
write_int64(Self, Int64) -> Unit
write_object(Self, Show) -> Unit
write_string(Self, String) -> Unit
write_sub_string(Self, String, Int, Int) -> Unit //deprecated
write_substring(Self, String, Int, Int) -> Unit
write_uint(Self, UInt) -> Unit
write_uint64(Self, UInt64) -> Unit
}
Yoorkin marked this conversation as resolved.
Show resolved Hide resolved
impl Show for T

Expand Down
51 changes: 51 additions & 0 deletions buffer/buffer_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,54 @@ test "write_bytes" {
buf.write_bytes(b"5\x006\x007\x008\x00")
inspect!(buf, content="12345678")
}

test "write_uint64 method" {
let buf = @buffer.new(size_hint=16)
buf.write_uint64(114)
buf.write_uint64(514)
assert_eq!(
buf.to_bytes(),
b"\x72\x00\x00\x00\x00\x00\x00\x00\x02\x02\x00\x00\x00\x00\x00\x00",
)
}

test "write_int64 method" {
let buf = @buffer.new(size_hint=16)
buf.write_int64(-114)
buf.write_int64(514)
assert_eq!(
buf.to_bytes(),
b"\x8e\xff\xff\xff\xff\xff\xff\xff\x02\x02\x00\x00\x00\x00\x00\x00",
)
}

test "write_uint method" {
let buf = @buffer.new(size_hint=8)
buf.write_uint(114)
buf.write_uint(514)
assert_eq!(buf.to_bytes(), b"\x72\x00\x00\x00\x02\x02\x00\x00")
}

test "write_int method" {
let buf = @buffer.new(size_hint=8)
buf.write_int(-114)
buf.write_int(514)
assert_eq!(buf.to_bytes(), b"\x8e\xff\xff\xff\x02\x02\x00\x00")
}

test "write_double method" {
let buf = @buffer.new(size_hint=16)
buf.write_double(-114)
buf.write_double(0.514)
assert_eq!(
buf.to_bytes(),
b"\x00\x00\x00\x00\x00\x80\x5c\xc0\xa6\x9b\xc4\x20\xb0\x72\xe0\x3f",
)
}

test "write_float method" {
let buf = @buffer.new(size_hint=8)
buf.write_float(-114)
buf.write_float(0.514)
assert_eq!(buf.to_bytes(), b"\x00\x00\xe4\xc2\x81\x95\x03\x3f")
}
8 changes: 7 additions & 1 deletion buffer/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/coverage",
"moonbitlang/core/bytes"
"moonbitlang/core/bytes",
"moonbitlang/core/uint64",
"moonbitlang/core/uint",
"moonbitlang/core/int64",
"moonbitlang/core/int",
"moonbitlang/core/double",
"moonbitlang/core/float"
Yoorkin marked this conversation as resolved.
Show resolved Hide resolved
]
}
11 changes: 11 additions & 0 deletions double/double.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,14 @@ pub fn is_close(
) ||
diff <= absolute_tolerance
}

///| Convert a Double into a 8 bytes long Bytes
///
/// # Example
///
/// ```
/// let double_bytes = 114.514 |> Double::to_bytes
/// ```
pub fn to_bytes(self : Double) -> Bytes {
self |> Double::reinterpret_as_uint64 |> UInt64::to_bytes
}
1 change: 1 addition & 0 deletions double/double.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ impl Double {
pow(Double, Double) -> Double
round(Double) -> Double
signum(Double) -> Double
to_bytes(Double) -> Bytes
Yoorkin marked this conversation as resolved.
Show resolved Hide resolved
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/coverage",
"moonbitlang/core/double/internal/ryu"
],
Expand Down
11 changes: 11 additions & 0 deletions float/float.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,14 @@ pub fn Float::to_int(self : Float) -> Int {
self.unchecked_to_int()
}
}

///| Convert a Float into a 4 bytes long Bytes
///
/// # Example
///
/// ```
/// let float_bytes = 114.514 |> Float::to_bytes
/// ```
pub fn to_bytes(self : Float) -> Bytes {
self |> Float::reinterpret_as_uint |> UInt::to_bytes
}
1 change: 1 addition & 0 deletions float/float.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ impl Float {
op_mod(Float, Float) -> Float
pow(Float, Float) -> Float
round(Float) -> Float
to_bytes(Float) -> Bytes
to_int(Float) -> Int
trunc(Float) -> Float
}
Expand Down
3 changes: 2 additions & 1 deletion float/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/coverage",
"moonbitlang/core/double"
"moonbitlang/core/double",
"moonbitlang/core/uint"
],
"test-import": ["moonbitlang/core/quickcheck"],
"targets": {
Expand Down
11 changes: 11 additions & 0 deletions int/int.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,14 @@ pub fn abs(self : Int) -> Int {
self
}
}

///| Convert an Int into a 4 bytes long Bytes.
///
/// # Example
///
/// ```
/// let int_bytes = 114514 |> Int::to_bytes
/// ```
pub fn to_bytes(self : Int) -> Bytes {
self |> Int::reinterpret_as_uint |> UInt::to_bytes
}
1 change: 1 addition & 0 deletions int/int.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ let min_value : Int

impl Int {
abs(Int) -> Int
to_bytes(Int) -> Bytes
}

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

///| Convert an Int64 into a 8 bytes long Bytes.
///
/// # Example
///
/// ```
/// let int64_bytes = 114514 |> Int64::to_bytes
/// ```
pub fn to_bytes(self : Int64) -> Bytes {
self |> Int64::reinterpret_as_uint64 |> UInt64::to_bytes
}
1 change: 1 addition & 0 deletions int64/int64.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ let min_value : Int64
impl Int64 {
abs(Int64) -> Int64
from_int(Int) -> Int64
to_bytes(Int64) -> Bytes
}

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

///| Convert an UInt into a 4 bytes long Bytes.
///
/// # Example
///
/// ```
/// let uint_bytes = 114514 |> UInt::to_bytes
/// ```
pub fn to_bytes(self : UInt) -> Bytes {
let byte1 = self.to_byte()
let byte2 = (self >> 8).to_byte()
let byte3 = (self >> 16).to_byte()
let byte4 = (self >> 24).to_byte()
[byte1, byte2, byte3, byte4] |> Bytes::from_fixedarray
}
1 change: 1 addition & 0 deletions uint/uint.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ let min_value : UInt

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

Expand Down
3 changes: 2 additions & 1 deletion uint64/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/coverage"
"moonbitlang/core/coverage",
"moonbitlang/core/bytes"
]
}
20 changes: 20 additions & 0 deletions uint64/uint64.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,23 @@ pub let min_value : UInt64 = 0UL

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

///| Convert an UInt64 into a 8 bytes long Bytes.
///
/// # Example
///
/// ```
/// let uint64_bytes = 1145141919810 |> UInt64::to_bytes
/// ```
pub fn to_bytes(self : UInt64) -> Bytes {
let byte1 = self.to_byte()
let byte2 = (self >> 8).to_byte()
let byte3 = (self >> 16).to_byte()
let byte4 = (self >> 24).to_byte()
let byte5 = (self >> 32).to_byte()
let byte6 = (self >> 40).to_byte()
let byte7 = (self >> 48).to_byte()
let byte8 = (self >> 56).to_byte()
[byte1, byte2, byte3, byte4, byte5, byte6, byte7, byte8]
|> Bytes::from_fixedarray
}
5 changes: 5 additions & 0 deletions uint64/uint64.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ let min_value : UInt64

// Types and methods


impl UInt64 {
to_bytes(UInt64) -> Bytes
}

// Type aliases

// Traits
Expand Down
Loading