///|
priv struct OutputStream {
bytes : Array[Byte]
offset : Int32
}
///|
fn OutputStream::new() -> OutputStream {
{ bytes: [], offset: 0, }
}
///|
fn OutputStream::write_byte(self : OutputStream, b : Byte) -> OutputStream {
self.bytes.push(b)
{ bytes: self.bytes, offset: self.offset + 1, }
}
///|
fn OutputStream::write_int(self : OutputStream, b : Int32) -> OutputStream {
self.write_byte(b.to_byte())
}
///|
fn OutputStream::write_ints(
self : OutputStream,
bytes : Array[Int32],
) -> OutputStream {
let mut cur = self
for b in bytes {
cur = cur.write_int(b)
}
cur
}
///|
fn OutputStream::write_bytes(
self : OutputStream,
bytes : Bytes,
) -> OutputStream {
let mut cur = self
for byte in bytes.to_array() {
cur = cur.write_byte(byte)
}
cur
}
///|
fn OutputStream::to_bytes(self : OutputStream) -> Bytes {
Bytes::from_array(self.bytes)
}
///|
priv struct InputStream {
bytes : Array[Byte]
offset : Int32
}
///|
fn InputStream::from_bytes(
bytes : Bytes,
start_offset~ : Int32 = 0,
) -> InputStream {
{ bytes: bytes.to_array(), offset: start_offset, }
}
///|
fn InputStream::read_int(
self : InputStream,
) -> Result[(Int32, InputStream), String] {
match self.bytes.get(self.offset) {
Some(b) => Ok((b.to_int(), { bytes: self.bytes, offset: self.offset + 1, }))
None => Err("unexpected end of input")
}
}
///|
fn InputStream::peek_int(self : InputStream) -> Result[Int32, String] {
match self.bytes.get(self.offset) {
Some(b) => Ok(b.to_int())
None => Err("unexpected end of input")
}
}
///|
fn InputStream::slice_bytes(
self : InputStream,
start_offset : Int32,
end_offset : Int32,
) -> Bytes {
let out : Array[Byte] = []
for i in start_offset..