silk.text.char.
Conversion between char and integers, and named comparison functions for char.
When to use#
Use fromU32 to make a char from an integer code point, and toU32 to get the code
point. Use the comparison operators in ordinary expressions. Use the named comparison functions
when a comparison must be a function value.
Details#
A char is one Unicode scalar value: a code point from 0 to 0x10FFFF that is not a
surrogate. Comparisons use the numeric code point. They do not use locale rules, normalization,
grapheme clusters, or UTF-8 bytes. These functions do not allocate and have no effects.
Gotchas#
fromU32 returns None for the surrogates 0xD800 to 0xDFFF and for values more than
0x10FFFF. It does not truncate and does not trap.
Examples#
Validate a Unicode scalar integer#
import silk.text.char
import silk.core.option { Option }
pub fn main() -> i32 {
let scalar = char.fromU32(0x2603)
let value = move scalar |> Option<char>.unwrapOr('?')
if char.toU32(value) != 0x2603 {
return 1
}
let surrogate = char.fromU32(0xD800)
let replacement = move surrogate |> Option<char>.unwrapOr('?')
if replacement != '?' {
return 1
}
return 0
}Import as char with import silk.text.char.
Public declarations: 8.
fromU32#
pub fn fromU32(value: u32) -> Option<char>Returns the char with code point value, or None if value is not a Unicode scalar value.
When to use#
Use fromU32 to check a code point from untrusted input, such as a decoded escape sequence.
Details#
The result is None for the surrogates 0xD800 to 0xDFFF and for values more than
0x10FFFF. For each other value, the result is present.
toU32#
pub fn toU32(value: char) -> u32Returns the code point of value. The result is at most 0x10FFFF and is never a surrogate.
equals#
pub fn equals(left: char, right: char) -> boolReturns true if left and right have the same code point. This function has the same
result as left == right.
notEquals#
pub fn notEquals(left: char, right: char) -> boolReturns true if left and right have different code points. This function has the same
result as left != right.
lessThan#
pub fn lessThan(left: char, right: char) -> boolReturns true if the code point of left is less than the code point of right.
lessOrEqual#
pub fn lessOrEqual(left: char, right: char) -> boolReturns true if the code point of left is less than or equal to the code point of right.
greaterThan#
pub fn greaterThan(left: char, right: char) -> boolReturns true if the code point of left is more than the code point of right.
greaterOrEqual#
pub fn greaterOrEqual(left: char, right: char) -> boolReturns true if the code point of left is more than or equal to the code point of right.
Category: Text.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2.