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 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
94 changes: 93 additions & 1 deletion buffer/buffer.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,105 @@ pub fn T::new(size_hint~ : Int = 0) -> T {
}

///|
/// Write a string into buffer.
/// Write a string into buffer at little endian.
pub fn write_string(self : T, value : String) -> Unit {
self.grow_if_necessary(self.len + value.length() * 2)
self.data.blit_from_string(self.len, value, 0, value.length())
self.len += value.length() * 2
}

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

///|
/// Write an UInt64 into buffer at little endian.
pub fn write_uint64_le(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 at big endian.
pub fn write_int64_be(self : T, value : Int64) -> Unit {
self.write_uint64_be(value.reinterpret_as_uint64())
}

///|
/// Write an Int64 into buffer at little endian.
pub fn write_int64_le(self : T, value : Int64) -> Unit {
self.write_uint64_le(value.reinterpret_as_uint64())
}

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

///|
/// Write an UInt into buffer at little endian.
pub fn write_uint_le(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 at big endian.
pub fn write_int_be(self : T, value : Int) -> Unit {
self.write_uint_be(value.reinterpret_as_uint())
}

///|
/// Write an Int into buffer at little endian.
pub fn write_int_le(self : T, value : Int) -> Unit {
self.write_uint_le(value.reinterpret_as_uint())
}

///|
/// Write a Double into buffer at big endian.
pub fn write_double_be(self : T, value : Double) -> Unit {
self.write_uint64_be(value.reinterpret_as_uint64())
}

///|
/// Write a Double into buffer at little endian.
pub fn write_double_le(self : T, value : Double) -> Unit {
self.write_uint64_le(value.reinterpret_as_uint64())
}

///|
/// Write a Float into buffer at big endian.
pub fn write_float_be(self : T, value : Float) -> Unit {
self.write_uint_be(value.reinterpret_as_uint())
}

///|
/// Write a Float into buffer at little endian.
pub fn write_float_le(self : T, value : Float) -> Unit {
self.write_uint_le(value.reinterpret_as_uint())
}

///|
pub fn write_object(self : T, value : Show) -> Unit {
self.write_string(value.to_string())
Expand Down
12 changes: 12 additions & 0 deletions buffer/buffer.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,22 @@ impl T {
write_byte(Self, Byte) -> Unit
write_bytes(Self, Bytes) -> Unit
write_char(Self, Char) -> Unit
write_double_be(Self, Double) -> Unit
write_double_le(Self, Double) -> Unit
write_float_be(Self, Float) -> Unit
write_float_le(Self, Float) -> Unit
write_int64_be(Self, Int64) -> Unit
write_int64_le(Self, Int64) -> Unit
write_int_be(Self, Int) -> Unit
write_int_le(Self, Int) -> 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_uint64_be(Self, UInt64) -> Unit
write_uint64_le(Self, UInt64) -> Unit
write_uint_be(Self, UInt) -> Unit
write_uint_le(Self, UInt) -> Unit
}
Yoorkin marked this conversation as resolved.
Show resolved Hide resolved
impl Show for T

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

test "write_uint64_le method" {
let buf = @buffer.new(size_hint=16)
buf.write_uint64_le(114)
buf.write_uint64_le(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_uint64_be method" {
let buf = @buffer.new(size_hint=16)
buf.write_uint64_be(114)
buf.write_uint64_be(514)
assert_eq!(
buf.to_bytes(),
b"\x00\x00\x00\x00\x00\x00\x00\x72\x00\x00\x00\x00\x00\x00\x02\x02",
)
}

test "write_int64_le method" {
let buf = @buffer.new(size_hint=16)
buf.write_int64_le(-114)
buf.write_int64_le(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_int64_be method" {
let buf = @buffer.new(size_hint=16)
buf.write_int64_be(-114)
buf.write_int64_be(514)
assert_eq!(
buf.to_bytes(),
b"\xff\xff\xff\xff\xff\xff\xff\x8e\x00\x00\x00\x00\x00\x00\x02\x02",
)
}

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

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

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

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

test "write_double_le method" {
let buf = @buffer.new(size_hint=16)
buf.write_double_le(-114)
buf.write_double_le(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_double_be method" {
let buf = @buffer.new(size_hint=16)
buf.write_double_be(-114)
buf.write_double_be(0.514)
assert_eq!(
buf.to_bytes(),
b"\xc0\x5c\x80\x00\x00\x00\x00\x00\x3f\xe0\x72\xb0\x20\xc4\x9b\xa6",
)
}

test "write_float_le method" {
let buf = @buffer.new(size_hint=8)
buf.write_float_le(-114)
buf.write_float_le(0.514)
assert_eq!(buf.to_bytes(), b"\x00\x00\xe4\xc2\x81\x95\x03\x3f")
}

test "write_float_be method" {
let buf = @buffer.new(size_hint=8)
buf.write_float_be(-114)
buf.write_float_be(0.514)
assert_eq!(buf.to_bytes(), b"\xc2\xe4\x00\x00\x3f\x03\x95\x81")
}
Loading