1///|
2/// copy_until copies bytes from a source reader to a destination writer
3/// until a specified byte is encountered. It returns the number of bytes
4/// copied and any error that occurred during the process.
5pub fn 
(dst : &Writer, src : &ByteReader, until : Byte) -> (Int64, IOError?)

copy_until copies bytes from a source reader to a destination writer until a specified byte is encountered. It returns the number of bytes copied and any error that occurred during the process.

copy_until
(
6
&Writer
dst
: &
trait Writer {
  write(Self, Slice[Byte]) -> (Int, IOError?)
}

Writer is the interface that wraps the basic Write method.

Write writes len(p) bytes from p to the underlying data stream. It returns the number of bytes written from p (0 <= n <= len(p)) and any error encountered that caused the write to stop early. Write must return a non-None error if it returns n < len(p). Write must not modify the slice data, even temporarily.

Implementations must not retain p.

Writer
,
7
&ByteReader
src
: &
trait ByteReader {
  read_byte(Self) -> (Byte, IOError?)
}

ByteReader is the interface that wraps the read_byte method.

read_byte reads and returns the next byte from the input or any error encountered. If read_byte returns an error, no input byte was consumed, and the returned byte value is undefined.

read_byte provides an efficient interface for byte-at-time processing. A [Reader] that does not implement ByteReader can be wrapped using bufio.NewReader to add this method.

ByteReader
,
8
Byte
until
:
Byte
Byte
9) -> (
Int64
Int64
,
type! IOError String

An IOError can be tested with equality checks.

IOError
?) {
10 let mut
Int64
total
= 0L
11 for { 12 let (
Byte
b
,
IOError?
err
) =
&ByteReader
src
.
(&ByteReader) -> (Byte, IOError?)
read_byte
()
13 guard
IOError?
err
is
IOError?
None
else { return (
Int64
total
,
IOError?
err
) }
14 let (_,
IOError?
err
) =
&Writer
dst
.
(&Writer, Slice[Byte]) -> (Int, IOError?)
write
(
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
::
(buf : Array[Byte], start~ : Int = .., end? : Int) -> Slice[Byte]
new
([
Byte
b
]))
15
Int64
total
(self : Int64, other : Int64) -> Int64

Adds two 64-bit integers together. Performs overflow checking and wrapping according to two's complement arithmetic.

Parameters:

  • self : The first 64-bit integer operand.
  • other : The second 64-bit integer operand to add to the first.

Returns a new 64-bit integer representing the sum of the two operands.

Example:

test "Int64::op_add" {
  let a = 9223372036854775807L // Int64 maximum value
  let b = 1L
  inspect(a + b, content="-9223372036854775808") // Wraps around to minimum value
  inspect(42L + -42L, content="0")
}
+=
1
16 guard
IOError?
err
is
IOError?
None
else { return (
Int64
total
,
IOError?
err
) }
17 if
Byte
b
(self : Byte, that : Byte) -> Bool

Compares two Byte values for equality.

Parameters:

  • self : The first Byte value to compare.
  • that : The second Byte value to compare.

Returns true if the two Byte values are equal, otherwise false.

==
Byte
until
{
18 return (
Int64
total
,
IOError?
None
)
19 } 20 } 21} 22 23///| 24/// copy_size copies a specified number of bytes from a source reader to a 25/// destination writer. It returns the number of bytes copied and any error 26/// that occurred during the process. 27pub fn
(dst : &Writer, src : &ByteReader, size : Int64) -> (Int64, IOError?)

copy_size copies a specified number of bytes from a source reader to a destination writer. It returns the number of bytes copied and any error that occurred during the process.

copy_size
(
28
&Writer
dst
: &
trait Writer {
  write(Self, Slice[Byte]) -> (Int, IOError?)
}

Writer is the interface that wraps the basic Write method.

Write writes len(p) bytes from p to the underlying data stream. It returns the number of bytes written from p (0 <= n <= len(p)) and any error encountered that caused the write to stop early. Write must return a non-None error if it returns n < len(p). Write must not modify the slice data, even temporarily.

Implementations must not retain p.

Writer
,
29
&ByteReader
src
: &
trait ByteReader {
  read_byte(Self) -> (Byte, IOError?)
}

ByteReader is the interface that wraps the read_byte method.

read_byte reads and returns the next byte from the input or any error encountered. If read_byte returns an error, no input byte was consumed, and the returned byte value is undefined.

read_byte provides an efficient interface for byte-at-time processing. A [Reader] that does not implement ByteReader can be wrapped using bufio.NewReader to add this method.

ByteReader
,
30
Int64
size
:
Int64
Int64
31) -> (
Int64
Int64
,
type! IOError String

An IOError can be tested with equality checks.

IOError
?) {
32 let mut
Int64
total
= 0L
33 for { 34 let (
Byte
b
,
IOError?
err
) =
&ByteReader
src
.
(&ByteReader) -> (Byte, IOError?)
read_byte
()
35 guard
IOError?
err
is
IOError?
None
else { return (
Int64
total
,
IOError?
err
) }
36 let (_,
IOError?
err
) =
&Writer
dst
.
(&Writer, Slice[Byte]) -> (Int, IOError?)
write
(
struct Slice[T] {
  buf: Array[T]
  start: Int
  len: Int
}

A Slice is a slice of an Array.

A separate Slice type was needed because of this issue: https://github.com/moonbitlang/core/issues/1063

This struct is based on MoonBit's "ArrayView" implementation here: https://github.com/moonbitlang/core/blob/main/builtin/arrayview.mbt which has the following copyright:

Copyright 2024 International Digital Economy Academy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slice
::
(buf : Array[Byte], start~ : Int = .., end? : Int) -> Slice[Byte]
new
([
Byte
b
]))
37
Int64
total
(self : Int64, other : Int64) -> Int64

Adds two 64-bit integers together. Performs overflow checking and wrapping according to two's complement arithmetic.

Parameters:

  • self : The first 64-bit integer operand.
  • other : The second 64-bit integer operand to add to the first.

Returns a new 64-bit integer representing the sum of the two operands.

Example:

test "Int64::op_add" {
  let a = 9223372036854775807L // Int64 maximum value
  let b = 1L
  inspect(a + b, content="-9223372036854775808") // Wraps around to minimum value
  inspect(42L + -42L, content="0")
}
+=
1
38 guard
IOError?
err
is
IOError?
None
else { return (
Int64
total
,
IOError?
err
) }
39 if
Int64
total
(self : Int64, other : Int64) -> Bool

Tests if two 64-bit integers are equal.

Parameters:

  • self : The first 64-bit integer to compare.
  • other : The second 64-bit integer to compare.

Returns true if both integers are equal, false otherwise.

Example:

test "Int64::op_equal" {
  let a = 42L
  let b = 42L
  let c = -42L
  inspect(a == b, content="true")
  inspect(a == c, content="false")
}
==
Int64
size
{
40 return (
Int64
total
,
IOError?
None
)
41 } 42 } 43} 44