[][src]Function binjs_io::escaped_wtf8::from_unicode_escape

pub fn from_unicode_escape(s: String) -> String

If the given bytes contains any unicode escape \uXXXX and that is lone surrogate, convert it to escaped lone surropgate.

Failures

If the input is not well-formed JSON, this function causes a panic.

Examples

Escape character is escaped.

let input    = "\x7F".to_string();
let expected = "\x7F007F".to_string();
assert_eq!(binjs_io::escaped_wtf8::from_unicode_escape(input), expected);

Lone lead surrogate is escaped.

let input    = "\\uD83E".to_string();
let expected = "\x7FD83E".to_string();
assert_eq!(binjs_io::escaped_wtf8::from_unicode_escape(input), expected);

Lone trail surrogate is escaped.

let input    = "\\uDD9D".to_string();
let expected = "\x7FDD9D".to_string();
assert_eq!(binjs_io::escaped_wtf8::from_unicode_escape(input), expected);

If the input is escaped surrogate pair, does nothing.

let input = "\\uD83E\\uDD9D".to_string();
let expected = input.clone();
assert_eq!(binjs_io::escaped_wtf8::from_unicode_escape(input), expected);

If the leading backslash is also escaped, does nothing.

let input = "\\\\uD83E".to_string();
let expected = input.clone();
assert_eq!(binjs_io::escaped_wtf8::from_unicode_escape(input), expected);

Anything else are not converted.

let input = "\t\\u3042\\r\\n".to_string();
let expected = input.clone();
assert_eq!(binjs_io::escaped_wtf8::from_unicode_escape(input), expected);

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

// Not enough character after \\u.
let input = "\\u000".to_string();
binjs_io::escaped_wtf8::from_unicode_escape(input);
// Invalid character in the codeunit
let input  = "\\u012X".to_string();
binjs_io::escaped_wtf8::from_unicode_escape(input);