1///|
2/// `bytes2str` decodes a UTF-8 `Bytes` to a UTF-16 `String`.
3///
4/// Examples:
5/// ```
6/// inspect!(bytes2str("+/ Hello, δΈη! π"), content="+/ Hello, δΈη! π")
7/// ```
8pub fn (b : Bytes) -> String
bytes2str decodes a UTF-8 Bytes to a UTF-16 String.
Examples:
inspect!(bytes2str("+/ Hello, δΈη! π"), content="+/ Hello, δΈη! π")
bytes2str(Bytes
b : Bytes
Bytes) -> String
String {
9 let Array[Char]
chars = (Bytes
b.(self : Bytes) -> Iter[Byte]
Creates an iterator over the bytes in the sequence.
Parameters:
bytes : A byte sequence to iterate over.
Returns an iterator that yields each byte in the sequence in order.
Example:
test "Bytes::iter" {
let bytes = Bytes::from_array([b'h', b'i'])
let mut sum = 0
bytes.iter().each(fn(b) { sum = sum + b.to_int() })
inspect(sum, content="209") // ASCII values: 'h'(104) + 'i'(105) = 209
}
iter() |> (bytes : Iter[Byte]) -> Iter[Char]
@decoder.utf8()).(self : Iter[Char]) -> Array[Char]
Collects the elements of the iterator into an array.
collect()
10 let @buffer.T
buf = (size_hint~ : Int) -> @buffer.T
Creates a new extensible buffer with specified initial capacity. If the
initial capacity is less than 1, the buffer will be initialized with capacity
1.
Parameters:
size_hint : Initial capacity of the buffer in bytes. Defaults to 0.
Returns a new buffer of type T.
Example:
test "new" {
let buf = @buffer.new(size_hint=10)
inspect(buf.length(), content="0")
buf.write_string("test")
inspect(buf.length(), content="8")
}
@buffer.new(Int
size_hint=Array[Char]
chars.(self : Array[Char]) -> Int
Returns the number of elements in the array.
Parameters:
array : The array whose length is to be determined.
Returns the number of elements in the array as an integer.
Example:
test "Array::length" {
let arr = [1, 2, 3]
inspect(arr.length(), content="3")
let empty : Array[Int] = []
inspect(empty.length(), content="0")
}
length())
11 for Char
ch in Array[Char]
chars {
12 @buffer.T
buf.(self : @buffer.T, value : Char) -> Unit
Writes a UTF-16LE encoded character into the buffer. Automatically grows the
buffer if necessary.
Parameters:
buffer : The buffer to write to.
char : The character to be written.
Example:
test "write_char" {
let buf = @buffer.new()
buf.write_char('A')
inspect(buf.contents(), content=
#|b"\x41\x00"
)
}
write_char(Char
ch)
13 }
14 @buffer.T
buf.(self : @buffer.T) -> Bytes
Returns a copy of the buffer's content as a sequence of bytes.
Parameters:
buffer : The buffer to read from.
Returns a Bytes object containing all bytes written to the buffer.
Example:
test "contents" {
let buf = @buffer.new()
buf.write_string("Test")
inspect(
buf.contents(),
content=
#|b"\x54\x00\x65\x00\x73\x00\x74\x00"
,
)
}
contents().(self : Bytes, offset~ : Int = .., length~ : Int = ..) -> String
Return an unchecked string, containing the subsequence of self that starts at
offset and has length length. Both offset and length
are indexed by byte.
Note this function does not validate the encoding of the byte sequence,
it simply copy the bytes into a new String.
to_unchecked_string()
15}
16
17///|
18test "bytes2str works on simple case" {
19 let String
got = (b : Bytes) -> String
bytes2str decodes a UTF-8 Bytes to a UTF-16 String.
Examples:
inspect!(bytes2str("+/ Hello, δΈη! π"), content="+/ Hello, δΈη! π")
bytes2str(b"simple case")
20 (obj : &Show, content~ : String, loc~ : SourceLoc = _, args_loc~ : ArgsLoc = _) -> Unit!InspectError
Tests if the string representation of an object matches the expected content.
Used primarily in test cases to verify the correctness of Show
implementations and program outputs.
Parameters:
object : The object to be inspected. Must implement the Show trait.
content : The expected string representation of the object. Defaults to
an empty string.
location : Source code location information for error reporting.
Automatically provided by the compiler.
arguments_location : Location information for function arguments in
source code. Automatically provided by the compiler.
Throws an InspectError if the actual string representation of the object
does not match the expected content. The error message includes detailed
information about the mismatch, including source location and both expected
and actual values.
Example:
test "inspect/basic" {
inspect(42, content="42")
inspect("hello", content="hello")
inspect([1, 2, 3], content="[1, 2, 3]")
}
inspect!(String
got, String
content="simple case")
21 (obj : &Show, content~ : String, loc~ : SourceLoc = _, args_loc~ : ArgsLoc = _) -> Unit!InspectError
Tests if the string representation of an object matches the expected content.
Used primarily in test cases to verify the correctness of Show
implementations and program outputs.
Parameters:
object : The object to be inspected. Must implement the Show trait.
content : The expected string representation of the object. Defaults to
an empty string.
location : Source code location information for error reporting.
Automatically provided by the compiler.
arguments_location : Location information for function arguments in
source code. Automatically provided by the compiler.
Throws an InspectError if the actual string representation of the object
does not match the expected content. The error message includes detailed
information about the mismatch, including source location and both expected
and actual values.
Example:
test "inspect/basic" {
inspect(42, content="42")
inspect("hello", content="hello")
inspect([1, 2, 3], content="[1, 2, 3]")
}
inspect!(
22 (b : Bytes) -> String
bytes2str decodes a UTF-8 Bytes to a UTF-16 String.
Examples:
inspect!(bytes2str("+/ Hello, δΈη! π"), content="+/ Hello, δΈη! π")
bytes2str("+/ Hello, δΈη! π"),
23 String
content="+/ Hello, δΈη! π",
24 )
25}
26
27///|
28/// `array2str` decodes a UTF-8 `ArrayView[Byte]` to a UTF-16 `String`.
29/// This is a more convenient version of `bytes2str` for when you have an `ArrayView[Byte]`.
30/// If you have an `Array[Byte]`, you can use `array2str(arr[:])`.
31///
32/// Examples:
33/// ```
34/// let arr = b"+/ Hello, δΈη! π".to_array()
35/// inspect!(array2str(arr[:]), content="+/ Hello, δΈη! π")
36/// ```
37pub fn (b : ArrayView[Byte]) -> String
array2str decodes a UTF-8 ArrayView[Byte] to a UTF-16 String.
This is a more convenient version of bytes2str for when you have an ArrayView[Byte].
If you have an Array[Byte], you can use array2str(arr[:]).
Examples:
let arr = b"+/ Hello, δΈη! π".to_array()
inspect!(array2str(arr[:]), content="+/ Hello, δΈη! π")
array2str(ArrayView[Byte]
b : type ArrayView[T]
An ArrayView represents a view into a section of an array without copying the data.
Example
let arr = [1, 2, 3, 4, 5]
let view = arr[1:4] // Creates a view of elements at indices 1,2,3
assert_eq(view[0], 2)
assert_eq(view.length(), 3)
ArrayView[Byte
Byte]) -> String
String {
38 let Array[Char]
chars = (ArrayView[Byte]
b.(self : ArrayView[Byte]) -> Iter[Byte]
Returns an iterator that yields each element of the array view in sequence
from start to end.
Parameters:
array_view : The array view to iterate over.
Returns an iterator that yields elements of type A from the array view.
Example:
test "View::iter" {
let arr = [1, 2, 3]
let view = arr[1:]
let mut sum = 0
view.iter().each(fn(x) { sum = sum + x })
inspect(sum, content="5")
}
iter() |> (bytes : Iter[Byte]) -> Iter[Char]
@decoder.utf8()).(self : Iter[Char]) -> Array[Char]
Collects the elements of the iterator into an array.
collect()
39 let @buffer.T
buf = (size_hint~ : Int) -> @buffer.T
Creates a new extensible buffer with specified initial capacity. If the
initial capacity is less than 1, the buffer will be initialized with capacity
1.
Parameters:
size_hint : Initial capacity of the buffer in bytes. Defaults to 0.
Returns a new buffer of type T.
Example:
test "new" {
let buf = @buffer.new(size_hint=10)
inspect(buf.length(), content="0")
buf.write_string("test")
inspect(buf.length(), content="8")
}
@buffer.new(Int
size_hint=Array[Char]
chars.(self : Array[Char]) -> Int
Returns the number of elements in the array.
Parameters:
array : The array whose length is to be determined.
Returns the number of elements in the array as an integer.
Example:
test "Array::length" {
let arr = [1, 2, 3]
inspect(arr.length(), content="3")
let empty : Array[Int] = []
inspect(empty.length(), content="0")
}
length())
40 for Char
ch in Array[Char]
chars {
41 @buffer.T
buf.(self : @buffer.T, value : Char) -> Unit
Writes a UTF-16LE encoded character into the buffer. Automatically grows the
buffer if necessary.
Parameters:
buffer : The buffer to write to.
char : The character to be written.
Example:
test "write_char" {
let buf = @buffer.new()
buf.write_char('A')
inspect(buf.contents(), content=
#|b"\x41\x00"
)
}
write_char(Char
ch)
42 }
43 @buffer.T
buf.(self : @buffer.T) -> Bytes
Returns a copy of the buffer's content as a sequence of bytes.
Parameters:
buffer : The buffer to read from.
Returns a Bytes object containing all bytes written to the buffer.
Example:
test "contents" {
let buf = @buffer.new()
buf.write_string("Test")
inspect(
buf.contents(),
content=
#|b"\x54\x00\x65\x00\x73\x00\x74\x00"
,
)
}
contents().(self : Bytes, offset~ : Int = .., length~ : Int = ..) -> String
Return an unchecked string, containing the subsequence of self that starts at
offset and has length length. Both offset and length
are indexed by byte.
Note this function does not validate the encoding of the byte sequence,
it simply copy the bytes into a new String.
to_unchecked_string()
44}
45
46///|
47test "array2str works on simple case" {
48 let Array[Byte]
inp = b"simple case".(self : Bytes) -> Array[Byte]
Converts a bytes sequence into an array of bytes.
Parameters:
bytes : A sequence of bytes to be converted into an array.
Returns an array containing the same bytes as the input sequence.
Example:
test "Bytes::to_array" {
let bytes = b"hello"
let arr = bytes.to_array()
inspect(arr, content="[b'\\x68', b'\\x65', b'\\x6C', b'\\x6C', b'\\x6F']")
}
to_array()
49 let String
got = (b : ArrayView[Byte]) -> String
array2str decodes a UTF-8 ArrayView[Byte] to a UTF-16 String.
This is a more convenient version of bytes2str for when you have an ArrayView[Byte].
If you have an Array[Byte], you can use array2str(arr[:]).
Examples:
let arr = b"+/ Hello, δΈη! π".to_array()
inspect!(array2str(arr[:]), content="+/ Hello, δΈη! π")
array2str(Array[Byte]
inp[:])
50 (obj : &Show, content~ : String, loc~ : SourceLoc = _, args_loc~ : ArgsLoc = _) -> Unit!InspectError
Tests if the string representation of an object matches the expected content.
Used primarily in test cases to verify the correctness of Show
implementations and program outputs.
Parameters:
object : The object to be inspected. Must implement the Show trait.
content : The expected string representation of the object. Defaults to
an empty string.
location : Source code location information for error reporting.
Automatically provided by the compiler.
arguments_location : Location information for function arguments in
source code. Automatically provided by the compiler.
Throws an InspectError if the actual string representation of the object
does not match the expected content. The error message includes detailed
information about the mismatch, including source location and both expected
and actual values.
Example:
test "inspect/basic" {
inspect(42, content="42")
inspect("hello", content="hello")
inspect([1, 2, 3], content="[1, 2, 3]")
}
inspect!(String
got, String
content="simple case")
51 let Array[Byte]
arr = b"+/ Hello, δΈη! π"(self : Bytes) -> Array[Byte]
Converts a bytes sequence into an array of bytes.
Parameters:
bytes : A sequence of bytes to be converted into an array.
Returns an array containing the same bytes as the input sequence.
Example:
test "Bytes::to_array" {
let bytes = b"hello"
let arr = bytes.to_array()
inspect(arr, content="[b'\\x68', b'\\x65', b'\\x6C', b'\\x6C', b'\\x6F']")
}
.to_array()
52 (obj : &Show, content~ : String, loc~ : SourceLoc = _, args_loc~ : ArgsLoc = _) -> Unit!InspectError
Tests if the string representation of an object matches the expected content.
Used primarily in test cases to verify the correctness of Show
implementations and program outputs.
Parameters:
object : The object to be inspected. Must implement the Show trait.
content : The expected string representation of the object. Defaults to
an empty string.
location : Source code location information for error reporting.
Automatically provided by the compiler.
arguments_location : Location information for function arguments in
source code. Automatically provided by the compiler.
Throws an InspectError if the actual string representation of the object
does not match the expected content. The error message includes detailed
information about the mismatch, including source location and both expected
and actual values.
Example:
test "inspect/basic" {
inspect(42, content="42")
inspect("hello", content="hello")
inspect([1, 2, 3], content="[1, 2, 3]")
}
inspect!((b : ArrayView[Byte]) -> String
array2str decodes a UTF-8 ArrayView[Byte] to a UTF-16 String.
This is a more convenient version of bytes2str for when you have an ArrayView[Byte].
If you have an Array[Byte], you can use array2str(arr[:]).
Examples:
let arr = b"+/ Hello, δΈη! π".to_array()
inspect!(array2str(arr[:]), content="+/ Hello, δΈη! π")
array2str(Array[Byte]
arr[:]), String
content="+/ Hello, δΈη! π")
53}
54