From 65c454bd45d8372ed06d78ee4209bb607aff7cb7 Mon Sep 17 00:00:00 2001 From: zihang Date: Sat, 11 May 2024 18:16:05 +0800 Subject: [PATCH] fix: bytes-buffer --- examples/bytes-buffer/lib/buffer.mbt | 22 +++++++++++----------- examples/bytes-buffer/lib/buffer_test.mbt | 11 +++++++++++ examples/bytes-buffer/main/main.mbt | 11 ----------- examples/bytes-buffer/main/moon.pkg.json | 6 ------ 4 files changed, 22 insertions(+), 28 deletions(-) create mode 100644 examples/bytes-buffer/lib/buffer_test.mbt delete mode 100644 examples/bytes-buffer/main/main.mbt delete mode 100644 examples/bytes-buffer/main/moon.pkg.json diff --git a/examples/bytes-buffer/lib/buffer.mbt b/examples/bytes-buffer/lib/buffer.mbt index 3f4cb3ae..f89d06d5 100644 --- a/examples/bytes-buffer/lib/buffer.mbt +++ b/examples/bytes-buffer/lib/buffer.mbt @@ -1,13 +1,13 @@ -struct Buffer { +struct SBuffer { mut data : Bytes mut len : Int } -pub fn Buffer::new(size : Int) -> Buffer { +pub fn SBuffer::new(size : Int) -> SBuffer { { data: Bytes::make(size, 0), len: 0 } } -pub fn op_get(self : Buffer, i : Int) -> Option[Int] { +pub fn op_get(self : SBuffer, i : Int) -> Option[Int] { if i < self.data.length() { Some(self.data[i]) } else { @@ -15,15 +15,15 @@ pub fn op_get(self : Buffer, i : Int) -> Option[Int] { } } -pub fn capacity(self : Buffer) -> Int { +pub fn capacity(self : SBuffer) -> Int { self.data.length() } -pub fn length(self : Buffer) -> Int { +pub fn length(self : SBuffer) -> Int { self.len } -fn expand_size(self : Buffer) { +fn expand_size(self : SBuffer) -> Unit { let new_capacity = if self.data.length() != 0 { self.data.length() * 2 } else { @@ -38,7 +38,7 @@ fn expand_size(self : Buffer) { self.data = new_data } -pub fn append_int(self : Buffer, value : Int) { +pub fn append_int(self : SBuffer, value : Int) -> Unit { if self.len >= self.data.length() { self.expand_size() } @@ -54,7 +54,7 @@ pub fn append_int(self : Buffer, value : Int) { } } -pub fn truncate(self : Buffer, another: Buffer) { +pub fn truncate(self : SBuffer, another: SBuffer) -> Unit { let mut index = 0 while index < another.len { if self.len >= self.data.length() { @@ -66,7 +66,7 @@ pub fn truncate(self : Buffer, another: Buffer) { } } -pub fn clear(self : Buffer) { +pub fn clear(self : SBuffer) -> Unit { let mut index = 0 while index < self.len { self.data[index] = 0 @@ -75,12 +75,12 @@ pub fn clear(self : Buffer) { self.len = 0 } -pub fn reset(self : Buffer, capacity : Int) { +pub fn reset(self : SBuffer, capacity : Int) -> Unit { self.len = 0 self.data = Bytes::make(capacity, 0) } -pub fn to_bytes(self : Buffer) -> Bytes { +pub fn to_bytes(self : SBuffer) -> Bytes { self.data } diff --git a/examples/bytes-buffer/lib/buffer_test.mbt b/examples/bytes-buffer/lib/buffer_test.mbt new file mode 100644 index 00000000..38440d67 --- /dev/null +++ b/examples/bytes-buffer/lib/buffer_test.mbt @@ -0,0 +1,11 @@ +test { + let buf = SBuffer::new(4) + buf.append_int(256) + buf.append_int(255) + inspect(bytes_to_int(buf.data, 0), content="256")? + inspect(bytes_to_int(buf.data, 4), content="255")? + let buf1 = SBuffer::new(4) + buf1.append_int(257) + buf.truncate(buf1) + inspect(bytes_to_int(buf.data, 8), content="257")? +} diff --git a/examples/bytes-buffer/main/main.mbt b/examples/bytes-buffer/main/main.mbt deleted file mode 100644 index 1fc6c5d6..00000000 --- a/examples/bytes-buffer/main/main.mbt +++ /dev/null @@ -1,11 +0,0 @@ -fn init { - let buf = @lib.Buffer::new(4) - buf.append_int(256) - buf.append_int(255) - println(@lib.bytes_to_int(buf.data, 0)) // 256 - println(@lib.bytes_to_int(buf.data, 4)) // 255 - let buf1 = @lib.Buffer::new(4) - buf1.append_int(257) - buf.truncate(buf1) - println(@lib.bytes_to_int(buf.data, 8)) // 257 -} diff --git a/examples/bytes-buffer/main/moon.pkg.json b/examples/bytes-buffer/main/moon.pkg.json deleted file mode 100644 index 98f16e92..00000000 --- a/examples/bytes-buffer/main/moon.pkg.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "name": "main", - "import": { - "bytes-buffer/lib": "" - } -} \ No newline at end of file