wtf8_rs/
lib.rs

1#![no_std]
2#![warn(clippy::all)]
3
4//! # wtf8-rs
5//!
6//! Implementation of [the WTF-8 encoding](https://simonsapin.github.io/wtf-8/).
7
8// Much of the code from this library has been copied from std sys_commmon,
9// which itself copied from @SimonSapin's repo.
10extern crate alloc;
11
12pub mod codepoint;
13pub mod wtf8;
14pub mod wtf8buf;
15
16#[doc(inline)]
17pub use codepoint::CodePoint;
18
19#[doc(inline)]
20pub use wtf8::{Wtf8, Wtf8Chunk};
21
22#[doc(inline)]
23pub use wtf8buf::Wtf8Buf;
24
25#[inline]
26fn decode_surrogate(second_byte: u8, third_byte: u8) -> u16 {
27    // The first byte is assumed to be 0xED
28    0xD800 | (second_byte as u16 & 0x3F) << 6 | third_byte as u16 & 0x3F
29}
30
31#[inline]
32fn decode_surrogate_pair(lead: u16, trail: u16) -> char {
33    let code_point = 0x1_0000 + ((((lead - 0xD800) as u32) << 10) | (trail - 0xDC00) as u32);
34    // Safety: this can not be greater than 10FFFF, by construction.
35    unsafe { char::from_u32_unchecked(code_point) }
36}