Function stfu8::encode_u16_pretty

source ·
pub fn encode_u16_pretty(v: &[u16]) -> String
Expand description

Encode UTF-16 as STFU-8, escaping all non-printable or ill-formed UTF-16 characters EXCEPT:

  • \t: tab
  • \n: line feed
  • \r: cariage return

This will allow the encoded text to print “pretilly” while still escaping invalid unicode and other non-printable characters.

Also check out:

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_pretty(ill.as_slice());

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