Skip to content

Commit

Permalink
fix: bytes-buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-jerry-ye committed May 11, 2024
1 parent 8652505 commit 65c454b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 28 deletions.
22 changes: 11 additions & 11 deletions examples/bytes-buffer/lib/buffer.mbt
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
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 {
None
}
}

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 {
Expand All @@ -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()
}
Expand All @@ -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() {
Expand All @@ -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
Expand All @@ -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
}

Expand Down
11 changes: 11 additions & 0 deletions examples/bytes-buffer/lib/buffer_test.mbt
Original file line number Diff line number Diff line change
@@ -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")?
}
11 changes: 0 additions & 11 deletions examples/bytes-buffer/main/main.mbt

This file was deleted.

6 changes: 0 additions & 6 deletions examples/bytes-buffer/main/moon.pkg.json

This file was deleted.

0 comments on commit 65c454b

Please sign in to comment.