[][src]Function binjs_io::escaped_wtf8::unescape

pub fn unescape(bytes: &[u8]) -> Cow<[u8]>

If the given bytes contains any escaped lone surropgate, unescape all escaped lone surrogate and returns the byte array. If not, return None.

Failures

If the input is not well-formed escaped WTF-8, this function causes a panic.

Examples

Lone lead surrogate is unescaped.

use std::ops::Deref;
let input: [u8; 5]    = [0x7F, b'D', b'8', b'3', b'E'];
let expected: [u8; 3] = [0xED, 0xA0, 0xBE];
let output = binjs_io::escaped_wtf8::unescape(&input);
let actual: &[u8] = output.deref();
assert_eq!(actual, expected);

Lone trail surrogate is unescaped.

use std::ops::Deref;
let input: [u8; 5]    = [0x7F, b'D', b'D', b'9', b'D'];
let expected: [u8; 3] = [0xED, 0xB6, 0x9D];
let output = binjs_io::escaped_wtf8::unescape(&input);
let actual: &[u8] = output.deref();
assert_eq!(actual, expected);

Escape character is unescaped.

use std::ops::Deref;
let input: [u8; 5]    = [0x7F, b'0', b'0', b'7', b'F'];
let expected: [u8; 1] = [0x7F];
let output = binjs_io::escaped_wtf8::unescape(&input);
let actual: &[u8] = output.deref();
assert_eq!(actual, expected);

Anything else are not converted.

use std::ops::Deref;
let input: [u8; 8] = [b'A', b'\n', 0xE3, 0x81, 0x82, 0xED, 0x83, 0xBF];
let expected: [u8; 8] = input.clone();
let output = binjs_io::escaped_wtf8::unescape(&input);
let actual: &[u8] = output.deref();
assert_eq!(actual, expected);

If the input is ill-formed, causes a panic.

// Input is clamped
let input: [u8; 4]    = [0x7F, b'0', b'0', b'7'];
binjs_io::escaped_wtf8::unescape(&input);
// Codeunit is not in the surrogate pair range.
let input: [u8; 5]    = [0x7F, b'3', b'0', b'4', b'2'];
binjs_io::escaped_wtf8::unescape(&input);