1///|
2pub extern "c" fn find_char(haystack : 
type @string.StringView

A @string.View represents a view of a String that maintains proper Unicode character boundaries. It allows safe access to a substring while handling multi-byte characters correctly.

@string.View
, needle :
Char
Char
) ->
Int
Int
? = "find_char"
3 4///| 5test "find_char" { 6
(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
(
(haystack : @string.StringView, needle : Char) -> Int?
find_char
("hello", 'h'),
String
content
="Some(0)")
7
(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
(
(haystack : @string.StringView, needle : Char) -> Int?
find_char
("hello", 'e'),
String
content
="Some(1)")
8
(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
(
(haystack : @string.StringView, needle : Char) -> Int?
find_char
("hello", 'o'),
String
content
="Some(4)")
9
(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
(
(haystack : @string.StringView, needle : Char) -> Int?
find_char
("hello", 'x'),
String
content
="None")
10} 11 12///| 13test "find_char/empty" { 14
(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
(
(haystack : @string.StringView, needle : Char) -> Int?
find_char
("", 'a'),
String
content
="None")
15
(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
(
(haystack : @string.StringView, needle : Char) -> Int?
find_char
(" ", ' '),
String
content
="Some(0)")
16} 17 18///| 19test "find_char/unicode" { 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
(
(haystack : @string.StringView, needle : Char) -> Int?
find_char
("你好", '你'),
String
content
="Some(0)")
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
(
(haystack : @string.StringView, needle : Char) -> Int?
find_char
("你好", '好'),
String
content
="Some(1)")
22
(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
(
(haystack : @string.StringView, needle : Char) -> Int?
find_char
("hello世界", '世'),
String
content
="Some(5)")
23
(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
(
(haystack : @string.StringView, needle : Char) -> Int?
find_char
("hello🌍", '🌍')
String
, conte
nt="Some(5)")
24} 25 26///| 27test "find_char/fuzz" { 28 let
Int
iter_num
= 100
29 let
Int
haystack_len
= 500
30 let
@random.Rand
rand
=
type @random.Rand

Rand is a pseudo-random number generator (PRNG) that provides various methods to generate random numbers of different types.

@random.Rand
::
(generator~ : &@random.Source = ..) -> @random.Rand

Create a new random number generator with a given [Gen] source.

new
()
31 for _ in
Int
0
..<
Int
iter_num
{
32 let
() -> Char
gen_char
= fn() {
33 loop
@random.Rand
rand
.
(self : @random.Rand, limit~ : UInt) -> UInt

[uint] returns a non-negative pseudo-random 32-bit integer as a Uint in the range [0, 2^32) or [0, limit) if limit is provided.

Arguments

  • limit - The upper bound (exclusive) of the random number to be generated (Optional). When limit is 0, the range is [0, 2^32).
uint
(
UInt
limit
=0x10FFFF) {
34
UInt
0..=0xD7FF | 0xE000..=0x10FFFF as cp
=>
35 break
UInt
cp
.
(self : UInt) -> Int

reinterpret the unsigned int as signed int For number within the range of 0..=2^31-1, the value is the same. For number within the range of 2^31..=2^32-1, the value is negative

reinterpret_as_int
().
(self : Int) -> Char?
to_char
().
(self : Char?) -> Char

Extract the value in Some.

If the value is None, it throws a panic.

unwrap
()
36 _ => continue
@random.Rand
rand
.
(self : @random.Rand, limit~ : UInt) -> UInt

[uint] returns a non-negative pseudo-random 32-bit integer as a Uint in the range [0, 2^32) or [0, limit) if limit is provided.

Arguments

  • limit - The upper bound (exclusive) of the random number to be generated (Optional). When limit is 0, the range is [0, 2^32).
uint
(
UInt
limit
=0x10FFFF)
37 } 38 } 39 let
Char
needle
=
() -> Char
gen_char
()
40 let
String
haystack_without_needle
=
(Array[Char]) -> String
String::
(chars : Array[Char]) -> String

Convert char array to string.

let s = @string.from_array(['H', 'e', 'l', 'l', 'o'])
assert_eq(s, "Hello")

Do not convert large datas to Array[Char] and build a string with String::from_array.

For efficiency considerations, it's recommended to use Buffer instead.

from_array
(
41
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
::
(Int, (Int) -> Char) -> Array[Char]

Creates a new array of the specified length, where each element is initialized using an index-based initialization function.

Parameters:

  • length : The length of the new array. If length is less than or equal to 0, returns an empty array.
  • initializer : A function that takes an index (starting from 0) and returns a value of type T. This function is called for each index to initialize the corresponding element.

Returns a new array of type Array[T] with the specified length, where each element is initialized using the provided function.

Example:

test "Array::makei" {
  let arr = Array::makei(3, fn(i) { i * 2 })
  inspect(arr, content="[0, 2, 4]")
}
makei
(
Int
haystack_len
, fn(_) {
42 loop
() -> Char
gen_char
() {
43
Char
char
if
Char
char
(self : Char, other : Char) -> Bool

Compares two characters for equality.

Parameters:

  • self : The first character to compare.
  • other : The second character to compare.

Returns true if both characters represent the same Unicode code point, false otherwise.

Example:

test "Char::op_equal" {
  let a = 'A'
  let b = 'A'
  let c = 'B'
  inspect(a == b, content="true")
  inspect(a == c, content="false")
}
==
Char
needle
=> continue
() -> Char
gen_char
()
44
Char
char
=> break
Char
char
45 } 46 }), 47 ) 48
(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
(
(haystack : @string.StringView, needle : Char) -> Int?
find_char
(
String
haystack_without_needle
,
Char
needle
),
String
content
="None")
49 let mut
Int
target
= 0
50 for
Int
i
in
Int
0
..<
Int
haystack_len
{
51 let
Array[Char]
haystack_with_needle
=
String
haystack_without_needle
.
(self : String) -> Array[Char]

Converts the String into an array of Chars.

to_array
()
52
Array[Char]
haystack_with_needle
(Array[Char], Int, Char) -> Unit

Sets the element at the specified index in the array to a new value. The original value at that index is overwritten.

Parameters:

  • array : The array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws an error if index is negative or greater than or equal to the length of the array.

Example:

test "Array::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic Array::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
i] =
Char
needle
53 let
String
haystack_with_needle
=
(Array[Char]) -> String
String::
(chars : Array[Char]) -> String

Convert char array to string.

let s = @string.from_array(['H', 'e', 'l', 'l', 'o'])
assert_eq(s, "Hello")

Do not convert large datas to Array[Char] and build a string with String::from_array.

For efficiency considerations, it's recommended to use Buffer instead.

from_array
(
Array[Char]
haystack_with_needle
)
54
(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
(
55
(haystack : @string.StringView, needle : Char) -> Int?
find_char
(
String
haystack_with_needle
,
Char
needle
),
56
String
content
="Some(\{
Int
target
})",
57 ) 58 match
String
haystack_without_needle
Array[Char]

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

test "Array::op_get" {
  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
}

test "panic Array::op_get/out_of_bounds" {
  let arr = [1, 2, 3]
  ignore(arr[3]) // Index out of bounds
}
.
(self : String) -> Array[Char]

Converts the String into an array of Chars.

to_array
Array[Char]

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

test "Array::op_get" {
  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
}

test "panic Array::op_get/out_of_bounds" {
  let arr = [1, 2, 3]
  ignore(arr[3]) // Index out of bounds
}
()
(Array[Char], Int) -> Char

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

test "Array::op_get" {
  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
}

test "panic Array::op_get/out_of_bounds" {
  let arr = [1, 2, 3]
  ignore(arr[3]) // Index out of bounds
}
[
i].
(self : Char) -> Int

Converts a character to its Unicode code point value as an integer.

Parameters:

  • self : The character to be converted.

Returns an integer representing the Unicode code point value of the character.

Example:

test "Char::to_int" {
  inspect('A'.to_int(), content="65") // ASCII value of 'A'
  inspect('あ'.to_int(), content="12354") // Unicode code point of 'あ'
}
to_int
() {
59 0..=0xFFFF =>
Int
target
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+=
1
60 _ =>
Int
target
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+=
2
61 } 62 } 63 } 64} 65