Silk

silk/char

Checked construction, integer inspection, equality, and ordering for Unicode scalar char values.

When to use

Prefer comparison operators in direct expressions. Import these functions when comparison must be passed by name or when the Unicode scalar ordering should be explicit at a call site.

Details

Ordering compares scalar values, not locale collation, normalization, grapheme clusters, or UTF-8 byte sequences. These functions allocate no storage and add no Effect channels.

Gotchas

Integer construction rejects 0xD800 through 0xDFFF and values above 0x10FFFF. It returns None; it does not truncate or trap.

Examples

Validate a Unicode scalar integer

import silk.char as Char

import silk.option as Option

pub fn main() -> i32 {
  let scalar = Char.fromU32(0x2603)
  let value = move scalar
    |> Option.unwrapOr<char>('?')
  if Char.toU32(value) != 0x2603 {
    return 1
  }
  let surrogate = Char.fromU32(0xD800)
  let replacement = move surrogate
    |> Option.unwrapOr<char>('?')
  if replacement != '?' {
    return 2
  }
  return 42
}

Import as char with import silk.char.

Public declarations: 8.

fromU32

pub fn fromU32(value: u32) -> Option<char>

Converts an integer to a Unicode scalar. Returns None for 0xD800 through 0xDFFF and values above 0x10FFFF.

toU32

pub fn toU32(value: char) -> u32

Returns the exact integer value of an already valid Unicode scalar.

equals

pub fn equals(left: char, right: char) -> bool

Returns true when left and right are the same Unicode scalar value.

notEquals

pub fn notEquals(left: char, right: char) -> bool

Returns true when left and right are different Unicode scalar values.

lessThan

pub fn lessThan(left: char, right: char) -> bool

Returns true when the Unicode scalar value of left is less than right.

lessOrEqual

pub fn lessOrEqual(left: char, right: char) -> bool

Returns true when the Unicode scalar value of left is less than or equal to right.

greaterThan

pub fn greaterThan(left: char, right: char) -> bool

Returns true when the Unicode scalar value of left is greater than right.

greaterOrEqual

pub fn greaterOrEqual(left: char, right: char) -> bool

Returns true when the Unicode scalar value of left is greater than or equal to right.

On this page