Function core::slice::ascii::is_ascii

source ·
const fn is_ascii(s: &[u8]) -> bool
Expand description

Optimized ASCII test that will use usize-at-a-time operations instead of byte-at-a-time operations (when possible).

The algorithm we use here is pretty simple. If s is too short, we just check each byte and be done with it. Otherwise:

  • Read the first word with an unaligned load.
  • Align the pointer, read subsequent words until end with aligned loads.
  • Read the last usize from s with an unaligned load.

If any of these loads produces something for which contains_nonascii (above) returns true, then we know the answer is false.