unicode/core

sourceunicode/core.sx

REPLACEMENT_CHARACTER

pub val REPLACEMENT_CHARACTER = 0xfffd

The Unicode replacement character, U+FFFD.

MAX_CODEPOINT

pub val MAX_CODEPOINT = 0x10ffff

The largest valid Unicode code point, U+10FFFF.

HIGH_SURROGATE_START

pub val HIGH_SURROGATE_START = 0xd800

The first high-surrogate code unit.

LOW_SURROGATE_END

pub val LOW_SURROGATE_END = 0xdfff

The last low-surrogate code unit.

make_decoded

private
fn make_decoded(codepoint: u32, size: i64) u64

decoded_codepoint

pub fn decoded_codepoint(decoded: u64) u32

Extract the code point from a value returned by utf8_decode or utf16_decode.

decoded_size

pub fn decoded_size(decoded: u64) i64

Extract the number of UTF-8 bytes or UTF-16 code units consumed from a value returned by utf8_decode or utf16_decode.

UnicodeError

pub enum UnicodeError = { InvalidUtf8, Truncated, InvalidCodepoint, InvalidUtf16, BufferTooSmall, InvalidIndex, OutOfMemory }

Errors produced by the UTF-8 and UTF-16 routines.

Variants

  • InvalidUtf8
  • Truncated
  • InvalidCodepoint
  • InvalidUtf16
  • BufferTooSmall
  • InvalidIndex
  • OutOfMemory

Category

pub enum Category = { UppercaseLetter, LowercaseLetter, TitlecaseLetter, ModifierLetter, OtherLetter, NonspacingMark, SpacingMark, EnclosingMark, DecimalNumber, LetterNumber, OtherNumber, ConnectorPunctuation, DashPunctuation, OpenPunctuation, ClosePunctuation, InitialPunctuation, FinalPunctuation, OtherPunctuation, MathSymbol, CurrencySymbol, ModifierSymbol, OtherSymbol, SpaceSeparator, LineSeparator, ParagraphSeparator, Control, Format, Surrogate, PrivateUse, Unassigned }

The Unicode general category of a code point.

Variants

  • UppercaseLetter
  • LowercaseLetter
  • TitlecaseLetter
  • ModifierLetter
  • OtherLetter
  • NonspacingMark
  • SpacingMark
  • EnclosingMark
  • DecimalNumber
  • LetterNumber
  • OtherNumber
  • ConnectorPunctuation
  • DashPunctuation
  • OpenPunctuation
  • ClosePunctuation
  • InitialPunctuation
  • FinalPunctuation
  • OtherPunctuation
  • MathSymbol
  • CurrencySymbol
  • ModifierSymbol
  • OtherSymbol
  • SpaceSeparator
  • LineSeparator
  • ParagraphSeparator
  • Control
  • Format
  • Surrogate
  • PrivateUse
  • Unassigned

is_continuation

private
fn is_continuation(byte: u32) bool

is_valid_codepoint

pub fn is_valid_codepoint(cp: u32) bool

Return true when cp is a Unicode scalar value.

Surrogate code points and values above U+10FFFF are not scalar values.

is_surrogate

pub fn is_surrogate(cp: u32) bool

Return true when cp is in the UTF-16 surrogate range.

is_ascii

pub fn is_ascii(cp: u32) bool

Return true when cp is an ASCII code point (U+0000..=U+007F).

is_ascii_digit

pub fn is_ascii_digit(cp: u32) bool

Return true when cp is an ASCII decimal digit.

is_ascii_alpha

pub fn is_ascii_alpha(cp: u32) bool

Return true when cp is an ASCII letter.

is_ascii_alphanumeric

pub fn is_ascii_alphanumeric(cp: u32) bool

Return true when cp is an ASCII letter or digit.

is_ascii_hex_digit

pub fn is_ascii_hex_digit(cp: u32) bool

Return true when cp is an ASCII hexadecimal digit.

is_ascii_whitespace

pub fn is_ascii_whitespace(cp: u32) bool

Return true when cp is an ASCII whitespace character.

is_ascii_control

pub fn is_ascii_control(cp: u32) bool

Return true when cp is an ASCII control character.

category_code

private
fn category_code(cp: u32) u8

category

pub fn category(cp: u32) Category

Return the Unicode general category of cp.

Unassigned code points and invalid scalar values both report Category.Unassigned.

is_letter

pub fn is_letter(cp: u32) bool

Return true when cp is a letter (categories Lu, Ll, Lt, Lm, Lo).

is_alphabetic

pub fn is_alphabetic(cp: u32) bool

Alias for is_letter.

is_uppercase

pub fn is_uppercase(cp: u32) bool

Return true when cp is an uppercase letter.

is_lowercase

pub fn is_lowercase(cp: u32) bool

Return true when cp is a lowercase letter.

is_digit

pub fn is_digit(cp: u32) bool

Return true when cp is a decimal digit (category Nd).

is_decimal

pub fn is_decimal(cp: u32) bool

Alias for is_digit.

is_numeric

pub fn is_numeric(cp: u32) bool

Return true when cp is any kind of numeric character (Nd, Nl, No).

is_alphanumeric

pub fn is_alphanumeric(cp: u32) bool

Return true when cp is a letter or a number.

is_mark

pub fn is_mark(cp: u32) bool

Return true when cp is a combining mark (Mn, Mc, Me).

is_punctuation

pub fn is_punctuation(cp: u32) bool

Return true when cp is a punctuation character (category P*).

is_symbol

pub fn is_symbol(cp: u32) bool

Return true when cp is a symbol (category S*).

is_separator

pub fn is_separator(cp: u32) bool

Return true when cp is a separator (category Z*).

is_control

pub fn is_control(cp: u32) bool

Return true when cp is a control character (category Cc).

is_format

pub fn is_format(cp: u32) bool

Return true when cp is a format character (category Cf).

is_private_use

pub fn is_private_use(cp: u32) bool

Return true when cp is a private-use character (category Co).

is_whitespace

pub fn is_whitespace(cp: u32) bool

Return true when cp has the Unicode White_Space property.

is_space

pub fn is_space(cp: u32) bool

Alias for is_whitespace.

find_upper

private
fn find_upper(cp: u32) u32

find_lower

private
fn find_lower(cp: u32) u32

find_title

private
fn find_title(cp: u32) u32

find_fold

private
fn find_fold(cp: u32) u32

to_upper

pub fn to_upper(cp: u32) u32

Return the simple uppercase mapping of cp, or cp when it has none.

to_uppercase

pub fn to_uppercase(cp: u32) u32

Alias for to_upper.

to_lower

pub fn to_lower(cp: u32) u32

Return the simple lowercase mapping of cp, or cp when it has none.

to_lowercase

pub fn to_lowercase(cp: u32) u32

Alias for to_lower.

to_title

pub fn to_title(cp: u32) u32

Return the simple titlecase mapping of cp, or cp when it has none.

to_titlecase

pub fn to_titlecase(cp: u32) u32

Alias for to_title.

to_casefold

pub fn to_casefold(cp: u32) u32

Return the simple case-folded mapping of cp.

This is the single-code-point subset of Unicode case folding. A few characters have multi-code-point folds (for example U+00DF); this function leaves those unchanged.

utf8_decode

pub fn utf8_decode(text: ref u8, len: i64, offset: i64) result[u64, UnicodeError]

Decode one UTF-8 code point at offset.

len is the number of readable bytes at text. On success the result packs the code point and the number of bytes consumed; use decoded_codepoint and decoded_size to inspect it.

utf8_decode_cstr

pub fn utf8_decode_cstr(text: ref u8, offset: i64) result[u64, UnicodeError]

Decode one UTF-8 code point from a NUL-terminated string at offset.

utf8_next

pub fn utf8_next(text: ref u8, len: i64, offset: i64) result[u64, UnicodeError]

Decode the next UTF-8 code point starting at offset.

This is an alias for utf8_decode that reads naturally in loops:

val first = unicode.utf8_next(text, len, 0)

utf8_validate

pub fn utf8_validate(text: ref u8, len: i64) bool

Return true when every byte in text[0..len] is a valid UTF-8 sequence.

utf8_count

pub fn utf8_count(text: ref u8, len: i64) i64

Count the Unicode code points in valid UTF-8 text.

Returns -1 when the input is not valid UTF-8.

utf8_codepoint_at

pub fn utf8_codepoint_at(text: ref u8, len: i64, index: i64) result[u64, UnicodeError]

Return the code point at Unicode index index as a u64.

index counts code points, not bytes. An out-of-range index yields UnicodeError.InvalidIndex. The value is widened to u64 so the result is portable across all compiler backends.

utf8_byte_index

pub fn utf8_byte_index(text: ref u8, len: i64, index: i64) result[i64, UnicodeError]

Return the byte offset of Unicode index index.

The returned offset may equal len, which denotes the end of the text.

utf8_encoded_len

pub fn utf8_encoded_len(cp: u32) i64

Return the number of bytes needed to encode cp as UTF-8.

Returns 0 when cp is not a valid scalar value.

utf8_encode

pub fn utf8_encode(cp: u32, out: ref u8) i64

Encode cp as UTF-8 into out and return the number of bytes written.

Returns 0 for an invalid scalar value. The caller must provide at least utf8_encoded_len(cp) writable bytes. The result is not NUL-terminated.

utf8_encode_result

pub fn utf8_encode_result(cp: u32, out: ref u8, cap: i64) result[i64, UnicodeError]

Encode cp as UTF-8 into a bounded buffer.

utf8_encoded_size

pub fn utf8_encoded_size(codepoints: ref u32, count: i64) i64

Return the number of bytes needed to encode count code points as UTF-8.

Returns -1 if any code point is invalid.

utf8_encode_codepoints

pub fn utf8_encode_codepoints(codepoints: ref u32, count: i64, out: ref u8, cap: i64) result[i64, UnicodeError]

Encode count code points as UTF-8 into out.

Returns the number of bytes written, or BufferTooSmall when cap is not large enough. The output is not NUL-terminated.

utf8_from_codepoints

pub fn utf8_from_codepoints[A: allocators.Allocator](ac: A, codepoints: ref u32, count: i64) result[str.String, UnicodeError]

Encode count code points into an allocator-owned String.

The returned string is owned by ac; release it with the same allocator's deallocation operation (or by freeing the enclosing arena).

utf16_encoded_len

pub fn utf16_encoded_len(cp: u32) i64

Return the number of UTF-16 code units needed to encode cp.

Returns 0 when cp is not a valid scalar value.

utf16_encode

pub fn utf16_encode(cp: u32, out: ref u16) i64

Encode cp as UTF-16 into out and return the number of code units written.

Returns 0 for an invalid scalar value.

utf16_decode

pub fn utf16_decode(units: ref u16, len: i64, index: i64) result[u64, UnicodeError]

Decode one UTF-16 code point at unit index index.

utf16_validate

pub fn utf16_validate(units: ref u16, len: i64) bool

Return true when units[0..len] is valid UTF-16.

utf16_count

pub fn utf16_count(units: ref u16, len: i64) i64

Count the Unicode code points in valid UTF-16 text.

Returns -1 when the input is not valid UTF-16.

utf8_to_codepoints

pub fn utf8_to_codepoints[A: allocators.GeneralAllocator](ac: A, text: ref u8, len: i64) result[list[u64], UnicodeError]

Decode valid UTF-8 into an allocator-owned list of code points.

utf16_to_codepoints

pub fn utf16_to_codepoints[A: allocators.GeneralAllocator](ac: A, units: ref u16, len: i64) result[list[u64], UnicodeError]

Decode valid UTF-16 into an allocator-owned list of code points.

utf8_validate_string

pub fn utf8_validate_string(text: str.String) bool

Validate a String as UTF-8.

utf8_count_string

pub fn utf8_count_string(text: str.String) i64

Count code points in a String.

Returns -1 when the string is not valid UTF-8.

utf8_to_codepoints_string

pub fn utf8_to_codepoints_string[A: allocators.GeneralAllocator](ac: A, text: str.String) result[list[u64], UnicodeError]

Decode a String into an allocator-owned list of code points.