std::code_points
Unicode scalar values. String iterates by grapheme cluster for text
workflows; use String.to_codepoints when code needs to inspect the scalar
values that make up a string. An ASCII codepoint is written as a literal,
'a' or '\n'; every other scalar value comes from Int.to_codepoint.
Import with import std::code_points.
Exports
Section titled “Exports”Types
type Codepoint
Section titled “type Codepoint”type Codepoint Int64
opaque — construction surface is private to its defining module
A Unicode code point / scalar value: 0..=0x10FFFF, excluding UTF-16
surrogate code points. Backed by an Int64, but validated at construction.
The 128 ASCII codepoints have a literal: 'a', '\'', '\u{7F}'.
validated type: Codepoint converts to nothing. A literal where a Codepoint is expected is checked when the program is compiled; Codepoint.try_from(...) checks a value when it runs.
Codepoint.to_int
Section titled “Codepoint.to_int”fn to_int(Codepoint(n)) -> Int
type function on Codepoint
Returns the numeric Unicode scalar value.
assert Codepoint.to_int('A') == 65
Codepoint.is_letter
Section titled “Codepoint.is_letter”fn is_letter(cp: Codepoint) -> Bool
type function on Codepoint
Reports whether cp is a Unicode letter: general category L (Lu, Ll,
Lt, Lm or Lo), as Go’s unicode.IsLetter returns it. Letters from any
script count. Digits, punctuation, emoji and combining marks (category
M, such as U+0301 COMBINING ACUTE ACCENT) do not.
assert Codepoint.is_letter('a')
zhe = Int.to_codepoint(0x0436)?
assert Codepoint.is_letter(zhe)
refute Codepoint.is_letter('7')
Codepoint.is_digit
Section titled “Codepoint.is_digit”fn is_digit(Codepoint(n)) -> Bool
type function on Codepoint
Reports whether cp is an ASCII digit, '0' through '9'. This is
ASCII-only on purpose: a phone number, a checksum or a number to parse
means ASCII digits, so Arabic-Indic ٣ (U+0663) and full-width 1
(U+FF11) are not digits here. There is no function for Unicode decimal
digits (general category Nd).
assert Codepoint.is_digit('7')
three = Int.to_codepoint(0x0663)?
refute Codepoint.is_digit(three)
refute Codepoint.is_digit('x')
Codepoint.to_digit
Section titled “Codepoint.to_digit”fn to_digit(Codepoint(n)) -> Maybe<Int>
type function on Codepoint
Returns the value of an ASCII digit: Some(0) for '0' through
Some(9) for '9', and None for every codepoint Codepoint.is_digit
rejects. ASCII-only on purpose, as is_digit is: Arabic-Indic and
full-width digits return None.
assert Codepoint.to_digit('7') == Some(7)
one = Int.to_codepoint(0xFF11)?
assert Codepoint.to_digit(one) == None
assert Codepoint.to_digit('x') == None
Codepoint.is_whitespace
Section titled “Codepoint.is_whitespace”fn is_whitespace(cp: Codepoint) -> Bool
type function on Codepoint
Reports whether cp has the Unicode White_Space property, as Go’s
unicode.IsSpace returns it: ASCII space, '\t', '\n', '\r',
vertical tab and form feed, U+0085, no-break space U+00A0, the
ideographic space U+3000 and the other Unicode spaces and line
separators. Zero-width space U+200B is not White_Space.
assert Codepoint.is_whitespace('\t')
nbsp = Int.to_codepoint(0x00A0)?
assert Codepoint.is_whitespace(nbsp)
refute Codepoint.is_whitespace('a')
Codepoint.is_upper
Section titled “Codepoint.is_upper”fn is_upper(cp: Codepoint) -> Bool
type function on Codepoint
Reports whether cp is an uppercase letter: general category Lu, as
Go’s unicode.IsUpper returns it. Title-case letters (category Lt,
such as Dž U+01C5) are neither upper nor lower, and neither are
letters without case, such as 中.
assert Codepoint.is_upper('A')
zhe = Int.to_codepoint(0x0416)?
assert Codepoint.is_upper(zhe)
refute Codepoint.is_upper('a')
Codepoint.is_lower
Section titled “Codepoint.is_lower”fn is_lower(cp: Codepoint) -> Bool
type function on Codepoint
Reports whether cp is a lowercase letter: general category Ll, as
Go’s unicode.IsLower returns it. Title-case letters (category Lt,
such as Dž U+01C5) are neither lower nor upper, and neither are
letters without case, such as 中.
assert Codepoint.is_lower('a')
e_acute = Int.to_codepoint(0x00E9)?
assert Codepoint.is_lower(e_acute)
refute Codepoint.is_lower('A')
Codepoint.to_string
Section titled “Codepoint.to_string”impl Display for Codepoint
fn to_string(cp: Codepoint) -> String
impl Display.to_string
Returns a one-codepoint string containing cp.
assert Codepoint.to_string('A') == "A"
Codepoint.inspect
Section titled “Codepoint.inspect”impl Debug for Codepoint
fn inspect(cp: Codepoint) -> String
impl Debug.inspect
Returns the developer-facing form: Codepoint(65) for 'A'.
assert Codepoint.inspect('A') == "Codepoint(65)"
Codepoint.next
Section titled “Codepoint.next”impl Discrete for Codepoint
fn next(cp: Codepoint) -> Maybe<Codepoint>
impl Discrete.next
Returns the next valid Codepoint, skipping the UTF-16 surrogate block. Returns None after the largest Unicode scalar value.
assert Codepoint.next('A') == Some('B')
assert Codepoint.next(Int.to_codepoint(0x10FFFF)?) == None
Codepoint.steps_between
Section titled “Codepoint.steps_between”impl Discrete for Codepoint
fn steps_between(Codepoint(a), Codepoint(b)) -> Maybe<Int>
Counts how many next steps move from the first Codepoint to the
second, skipping invalid surrogate values without walking the range.
assert Codepoint.steps_between('a', 'd') == Some(3)
Codepoint.step_by
Section titled “Codepoint.step_by”impl Steppable for Codepoint
fn step_by(cp: Codepoint, by: Int) -> Maybe<Codepoint>
impl Steppable.step_by
Steps by a count of valid Unicode scalar values, skipping the UTF-16 surrogate block. Returns None if the destination is outside the Codepoint domain.
assert Codepoint.step_by('A', 2) == Some('C')
Codepoint.try_from
Section titled “Codepoint.try_from”impl TryFrom<base(Codepoint)> for Codepoint
fn try_from(value: Int) -> Maybe<Codepoint>
impl TryFrom.try_from
TryFrom.try_from for Codepoint, which its rule supplies: Some of the value when it satisfies Codepoint’s rules, None when it does not.
Codepoint.equal
Section titled “Codepoint.equal”impl Equatable for Codepoint
fn equal(a: Codepoint, b: Codepoint) -> Bool
impl Equatable.equal
Codepoint.hash
Section titled “Codepoint.hash”impl Hashable for Codepoint
fn hash(value: Codepoint) -> Int
impl Hashable.hash
Codepoint.compare
Section titled “Codepoint.compare”impl Comparable for Codepoint
fn compare(a: Codepoint, b: Codepoint) -> Ordering
impl Comparable.compare