|
|
|
@ -34,6 +34,27 @@ impl<'a> From<JsStr<'a>> for JsStrDisplayEscaped<'a> {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Display implementation for [`crate::JsString`] that escapes unicode characters.
|
|
|
|
|
#[derive(Debug)] |
|
|
|
|
pub struct JsStrDisplayLossy<'a> { |
|
|
|
|
inner: JsStr<'a>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl fmt::Display for JsStrDisplayLossy<'_> { |
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
|
|
|
// No need to optimize latin1.
|
|
|
|
|
self.inner |
|
|
|
|
.code_points_lossy() |
|
|
|
|
.try_for_each(|c| f.write_char(c)) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<'a> From<JsStr<'a>> for JsStrDisplayLossy<'a> { |
|
|
|
|
fn from(inner: JsStr<'a>) -> Self { |
|
|
|
|
Self { inner } |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
fn latin1() { |
|
|
|
|
// 0xE9 is `é` in ISO-8859-1 (see https://www.ascii-code.com/ISO-8859-1).
|
|
|
|
@ -41,4 +62,31 @@ fn latin1() {
|
|
|
|
|
|
|
|
|
|
let rust_str = format!("{}", JsStrDisplayEscaped { inner: s }); |
|
|
|
|
assert_eq!(rust_str, "Hello é world!"); |
|
|
|
|
|
|
|
|
|
let rust_str = format!("{}", JsStrDisplayLossy { inner: s }); |
|
|
|
|
assert_eq!(rust_str, "Hello é world!"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
fn emoji() { |
|
|
|
|
// 0x1F600 is `😀` (see https://www.fileformat.info/info/unicode/char/1f600/index.htm).
|
|
|
|
|
let s = JsStr::utf16(&[0xD83D, 0xDE00]); |
|
|
|
|
|
|
|
|
|
let rust_str = format!("{}", JsStrDisplayEscaped { inner: s }); |
|
|
|
|
assert_eq!(rust_str, "😀"); |
|
|
|
|
|
|
|
|
|
let rust_str = format!("{}", JsStrDisplayLossy { inner: s }); |
|
|
|
|
assert_eq!(rust_str, "😀"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
fn unpaired_surrogates() { |
|
|
|
|
// 0xD800 is an unpaired surrogate (see https://www.fileformat.info/info/unicode/char/d800/index.htm).
|
|
|
|
|
let s = JsStr::utf16(&[0xD800]); |
|
|
|
|
|
|
|
|
|
let rust_str = format!("{}", JsStrDisplayEscaped { inner: s }); |
|
|
|
|
assert_eq!(rust_str, "\\uD800"); |
|
|
|
|
|
|
|
|
|
let rust_str = format!("{}", JsStrDisplayLossy { inner: s }); |
|
|
|
|
assert_eq!(rust_str, "<EFBFBD>"); |
|
|
|
|
} |
|
|
|
|