[][src]Function binjs_io::escaped_wtf8::escape

pub fn escape(bytes: Vec<u8>) -> Vec<u8>

If the given bytes is WTF-8 which contains lone surrogate, escape the lone surrogate with \x7F + XXXX (4 hex digits) and return the byte array. If not, return the given bytes.

Failures

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

Examples

Lone lead surrogate is escaped.

let input: Vec<u8>    = vec!(0xED, 0xA0, 0xBE);
let expected: Vec<u8> = vec!(0x7F, b'D', b'8', b'3', b'E');
assert_eq!(binjs_io::escaped_wtf8::escape(input), expected);

Lone trail surrogate is escaped.

let input: Vec<u8>    = vec!(0xED, 0xB6, 0x9D);
let expected: Vec<u8> = vec!(0x7F, b'D', b'D', b'9', b'D');
assert_eq!(binjs_io::escaped_wtf8::escape(input), expected);

Escape character is escaped

let input: Vec<u8>    = vec!(0x7F);
let expected: Vec<u8> = vec!(0x7F, b'0', b'0', b'7', b'F');
assert_eq!(binjs_io::escaped_wtf8::escape(input), expected);

Anything else are not converted.

let input: Vec<u8> = vec!(b'A', b'\n', 0xE3, 0x81, 0x82, 0xED, 0x83, 0xBF);
let expected: Vec<u8> = input.clone();
assert_eq!(binjs_io::escaped_wtf8::escape(input), expected);

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

// Input is clamped
let input: Vec<u8>  = vec!(0xED, 0xA0);
binjs_io::escaped_wtf8::escape(input);