Skip to content

add @buffer.write_bytesview and FixedArray::blit_from_bytesview API #1573

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

Merged
merged 1 commit into from
Jan 24, 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
30 changes: 30 additions & 0 deletions buffer/buffer.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,36 @@ pub fn write_bytes(self : T, value : Bytes) -> Unit {
self.len += val_len
}

///|
/// Writes a sequence of bytes from a BytesView into the buffer.
///
/// Parameters:
///
/// * `buffer` : The buffer to write to.
/// * `value` : The BytesView containing the bytes to write.
///
/// Example:
///
/// ```moonbit
/// test "write_bytesview" {
/// let buf = @buffer.new()
/// let view = b"Test"[1:3]
/// buf.write_bytesview(view)
/// inspect!(
/// buf.contents(),
/// content=
/// #|b"\x65\x73"
/// ,
/// )
/// }
/// ```
pub fn write_bytesview(self : T, value : BytesView) -> Unit {
let val_len = value.length()
self.grow_if_necessary(self.len + val_len)
self.data.blit_from_bytesview(self.len, value)
self.len += val_len
}

///|
/// Writes a portion of a string into the buffer in UTF-16LE encoding.
///
Expand Down
1 change: 1 addition & 0 deletions buffer/buffer.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ impl T {
to_unchecked_string(Self) -> String //deprecated
write_byte(Self, Byte) -> Unit
write_bytes(Self, Bytes) -> Unit
write_bytesview(Self, BytesView) -> Unit
write_char(Self, Char) -> Unit
write_double_be(Self, Double) -> Unit
write_double_le(Self, Double) -> Unit
Expand Down
11 changes: 11 additions & 0 deletions buffer/buffer_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,14 @@ test "write_iter" {
,
)
}

test "write_bytesview" {
let buf = @buffer.new(size_hint=4)
buf.write_bytesview(b"Test"[1:3])
inspect!(
buf.contents(),
content=
#|b"\x65\x73"
,
)
}
1 change: 1 addition & 0 deletions builtin/builtin.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ impl Result {

impl FixedArray {
blit_from_bytes(Self[Byte], Int, Bytes, Int, Int) -> Unit
blit_from_bytesview(Self[Byte], Int, BytesView) -> Unit
blit_from_string(Self[Byte], Int, String, Int, Int) -> Unit
blit_to[A](Self[A], Self[A], len~ : Int, src_offset~ : Int = .., dst_offset~ : Int = ..) -> Unit
compare[T : Compare](Self[T], Self[T]) -> Int
Expand Down
33 changes: 33 additions & 0 deletions builtin/bytes.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,39 @@ pub fn FixedArray::blit_from_bytes(
}
}

///|
/// Copy bytes from a BytesView into a fixed array of bytes.
///
/// Parameters:
///
/// * `self` : The destination fixed array of bytes.
/// * `bytes_offset` : The starting position in the destination array where bytes will be copied.
/// * `src` : The source BytesView to copy from.
///
/// Throws a panic if:
/// * `bytes_offset` is negative
/// * The destination array is too small to hold all bytes from the source BytesView
///
/// Example:
///
/// ```moonbit
/// let arr = FixedArray::make(4, b'\x00')
/// let view = b"\x01\x02\x03"[1:]
/// arr.blit_from_bytesview(1, view)
/// inspect!(arr, content="[b'\\x00', b'\\x02', b'\\x03', b'\\x00']")
/// ```
pub fn FixedArray::blit_from_bytesview(
self : FixedArray[Byte],
bytes_offset : Int,
src : BytesView
) -> Unit {
let src_len = src.length()
guard bytes_offset >= 0 && bytes_offset + src_len - 1 < self.length()
for i = 0, j = bytes_offset; i < src_len; i = i + 1, j = j + 1 {
self[j] = src[i]
}
}

///|
/// Creates a new byte sequence by copying all bytes from the input sequence.
///
Expand Down
19 changes: 19 additions & 0 deletions bytes/bytes_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,22 @@ test "Bytes::from_iter with multiple elements" {
let bytes = Bytes::from_iter(iter)
inspect!(bytes, content="b\"\\x61\\x62\\x63\"")
}

test "Fixed::blit_from_bytesview" {
let arr = FixedArray::make(4, b'\x00')
let view = b"\x01\x02\x03"[1:]
arr.blit_from_bytesview(1, view)
inspect!(arr, content="[b'\\x00', b'\\x02', b'\\x03', b'\\x00']")
}

test "panic Fixed::blit_from_bytesview 1" {
let arr = FixedArray::make(2, b'\x00')
let view = b"\x01\x02\x03"[1:]
ignore(arr.blit_from_bytesview(1, view))
}

test "panic Fixed::blit_from_bytesview 2" {
let arr = FixedArray::make(2, b'\x00')
let view = b"\x01\x02\x03"[1:]
ignore(arr.blit_from_bytesview(-1, view))
}
Loading