Skip to main content

Crate rfc9839

Crate rfc9839 

Source
Expand description

§RFC 9839 Unicode Subset Validators

This crate provides fast, zero-allocation checks to validate whether individual characters, strings, or raw byte slices conform to the subsets defined in RFC 9839:

  • Unicode Scalars — all code points except the UTF-16 surrogate range. In Rust, all char values are scalars by construction, but functions are included for completeness and defensive validation of &str / byte data.
  • XML Characters — the “Char” production from XML: {TAB, LF, CR} ∪ [0x20–0xD7FF] ∪ [0xE000–0xFFFD] ∪ [0x10000–0x10FFFF]. Excludes surrogates, C0 controls except TAB/LF/CR, and U+FFFE/U+FFFF.
  • Unicode Assignables — all scalar values that are not legacy controls, surrogates, or standardized noncharacters, including currently unassigned code points.

§Features

  • String-level APIs (is_*) are available at the crate root and in str. XML/Assignable validation uses an ASCII fast-path where validation is required: scan raw bytes first, and only fall back to chars() after the first non-ASCII byte.
  • Byte-level APIs are available in bytes for validating raw UTF-8 input. Scalar validation only needs UTF-8 validation; XML/Assignable checks decode the tail once and return false on invalid UTF-8.
  • Character-level APIs are available in chars and implemented as const fn with simple range tests.
  • Backwards-compatible aliases such as is_xml_chars_bytes and is_xml_char remain available at the crate root.
  • Zero allocations, no heap lookups, no tables.

§Examples

use rfc9839::{bytes, chars};

// Scalars (always true for safe Rust strings)
assert!(rfc9839::is_unicode_scalar("hello 🌍"));

// XML Characters
assert!(rfc9839::is_xml_chars("ok\tline\n"));
assert!(!rfc9839::is_xml_chars("\u{0000}")); // NUL is disallowed

// Byte and character APIs live in modules.
assert!(bytes::is_unicode_assignable("emoji 👍".as_bytes()));
assert!(!chars::is_unicode_assignable('\u{007F}')); // DEL is excluded

§Performance

All string/byte checks run in O(n). ASCII data is validated in a tight loop; non-ASCII triggers a one-time chars() traversal or UTF-8 decode. These functions are designed for high-throughput pipelines, parsers, and validators.

Modules§

bytes
Byte-level validators for UTF-8 encoded data.
chars
Character-level validators.
str
String-level validators for &str.

Functions§

is_unicode_assignable
Returns true if all characters in s are Unicode Assignables.
is_unicode_assignable_bytes
Returns true if bytes are all valid Unicode Assignables.
is_unicode_assignable_char
Returns true if c is a Unicode Assignable character per RFC 9839.
is_unicode_scalar
Returns true if all code points in s are Unicode scalar values.
is_unicode_scalar_bytes
Returns true if bytes are valid Unicode scalar values.
is_unicode_scalar_char
Returns true if c is a Unicode scalar value per RFC 9839.
is_xml_char
Returns true if c is an XML Character as defined in RFC 9839.
is_xml_chars
Returns true if all characters in s are XML Characters.
is_xml_chars_bytes
Returns true if bytes are all valid XML Characters.