Skip to content

Commit 2f20926

Browse files
committed
add write_bytesview API
1 parent ce9fcdf commit 2f20926

File tree

6 files changed

+95
-0
lines changed

6 files changed

+95
-0
lines changed

buffer/buffer.mbt

+30
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,36 @@ pub fn write_bytes(self : T, value : Bytes) -> Unit {
558558
self.len += val_len
559559
}
560560
561+
///|
562+
/// Writes a sequence of bytes from a BytesView into the buffer.
563+
///
564+
/// Parameters:
565+
///
566+
/// * `buffer` : The buffer to write to.
567+
/// * `value` : The BytesView containing the bytes to write.
568+
///
569+
/// Example:
570+
///
571+
/// ```moonbit
572+
/// test "write_bytesview" {
573+
/// let buf = @buffer.new()
574+
/// let view = b"Test"[1:3]
575+
/// buf.write_bytesview(view)
576+
/// inspect!(
577+
/// buf.contents(),
578+
/// content=
579+
/// #|b"\x65\x73"
580+
/// ,
581+
/// )
582+
/// }
583+
/// ```
584+
pub fn write_bytesview(self : T, value : BytesView) -> Unit {
585+
let val_len = value.length()
586+
self.grow_if_necessary(self.len + val_len)
587+
self.data.blit_from_bytesview(self.len, value)
588+
self.len += val_len
589+
}
590+
561591
///|
562592
/// Writes a portion of a string into the buffer in UTF-16LE encoding.
563593
///

buffer/buffer.mbti

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ impl T {
1616
to_unchecked_string(Self) -> String //deprecated
1717
write_byte(Self, Byte) -> Unit
1818
write_bytes(Self, Bytes) -> Unit
19+
write_bytesview(Self, BytesView) -> Unit
1920
write_char(Self, Char) -> Unit
2021
write_double_be(Self, Double) -> Unit
2122
write_double_le(Self, Double) -> Unit

buffer/buffer_test.mbt

+11
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,14 @@ test "write_iter" {
310310
,
311311
)
312312
}
313+
314+
test "write_bytesview" {
315+
let buf = @buffer.new(size_hint=4)
316+
buf.write_bytesview(b"Test"[1:3])
317+
inspect!(
318+
buf.contents(),
319+
content=
320+
#|b"\x65\x73"
321+
,
322+
)
323+
}

builtin/builtin.mbti

+1
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ impl Result {
694694

695695
impl FixedArray {
696696
blit_from_bytes(Self[Byte], Int, Bytes, Int, Int) -> Unit
697+
blit_from_bytesview(Self[Byte], Int, BytesView) -> Unit
697698
blit_from_string(Self[Byte], Int, String, Int, Int) -> Unit
698699
blit_to[A](Self[A], Self[A], len~ : Int, src_offset~ : Int = .., dst_offset~ : Int = ..) -> Unit
699700
compare[T : Compare](Self[T], Self[T]) -> Int

builtin/bytes.mbt

+33
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,39 @@ pub fn FixedArray::blit_from_bytes(
221221
}
222222
}
223223
224+
///|
225+
/// Copy bytes from a BytesView into a fixed array of bytes.
226+
///
227+
/// Parameters:
228+
///
229+
/// * `self` : The destination fixed array of bytes.
230+
/// * `bytes_offset` : The starting position in the destination array where bytes will be copied.
231+
/// * `src` : The source BytesView to copy from.
232+
///
233+
/// Throws a panic if:
234+
/// * `bytes_offset` is negative
235+
/// * The destination array is too small to hold all bytes from the source BytesView
236+
///
237+
/// Example:
238+
///
239+
/// ```moonbit
240+
/// let arr = FixedArray::make(4, b'\x00')
241+
/// let view = b"\x01\x02\x03"[1:]
242+
/// arr.blit_from_bytesview(1, view)
243+
/// inspect!(arr, content="[b'\\x00', b'\\x02', b'\\x03', b'\\x00']")
244+
/// ```
245+
pub fn FixedArray::blit_from_bytesview(
246+
self : FixedArray[Byte],
247+
bytes_offset : Int,
248+
src : BytesView
249+
) -> Unit {
250+
let src_len = src.length()
251+
guard bytes_offset >= 0 && bytes_offset + src_len - 1 < self.length()
252+
for i = 0, j = bytes_offset; i < src_len; i = i + 1, j = j + 1 {
253+
self[j] = src[i]
254+
}
255+
}
256+
224257
///|
225258
/// Creates a new byte sequence by copying all bytes from the input sequence.
226259
///

bytes/bytes_test.mbt

+19
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,22 @@ test "Bytes::from_iter with multiple elements" {
147147
let bytes = Bytes::from_iter(iter)
148148
inspect!(bytes, content="b\"\\x61\\x62\\x63\"")
149149
}
150+
151+
test "Fixed::blit_from_bytesview" {
152+
let arr = FixedArray::make(4, b'\x00')
153+
let view = b"\x01\x02\x03"[1:]
154+
arr.blit_from_bytesview(1, view)
155+
inspect!(arr, content="[b'\\x00', b'\\x02', b'\\x03', b'\\x00']")
156+
}
157+
158+
test "panic Fixed::blit_from_bytesview 1" {
159+
let arr = FixedArray::make(2, b'\x00')
160+
let view = b"\x01\x02\x03"[1:]
161+
ignore(arr.blit_from_bytesview(1, view))
162+
}
163+
164+
test "panic Fixed::blit_from_bytesview 2" {
165+
let arr = FixedArray::make(2, b'\x00')
166+
let view = b"\x01\x02\x03"[1:]
167+
ignore(arr.blit_from_bytesview(-1, view))
168+
}

0 commit comments

Comments
 (0)