Function stfu8::decode_u16

source ·
pub fn decode_u16(s: &str) -> Result<Vec<u16>, DecodeError>
Expand description

Decode a UTF-8 string containing encoded STFU-8 into a Vec<u16>.

Can decode the output of these functions:

Examples


let mut ill: Vec<u16> = "fooÿ\nbar"
    .encode_utf16()
    .collect();

// Make it ill formed UTF-16
ill.push(0xD800);       // surrogate pair lead
ill.push(b' ' as u16);  // NOT a trail
ill.push(0xDEED);       // Trail... with no lead
ill.push(b' ' as u16);
ill.push(0xDABA);       // lead... but end of str
let encoded = stfu8::encode_u16(ill.as_slice());

// Note that 0xFF is the valid character "ÿ"
// and the ill-formed characters are escaped.
assert_eq!(
    encoded,
    r"fooÿ\nbar\u00D800 \u00DEED \u00DABA"
);

assert_eq!(ill, stfu8::decode_u16(&encoded).unwrap());