2 个稳定版本
| 1.1.2 | 2024 年 2 月 27 日 |
|---|---|
| 1.0.1 |
|
#54 在 解析工具
83 每月下载次数
用于 2 crates
10KB
125 行
为 &str 添加单个扩展 trait 以取消转义任何反斜杠。支持 no-std。
取消转义旨在支持尽可能多的语言。
以下转义是有效的
\\n->\n\\r->\r\\t->\t\\b->\x08\\f->\x0C\\'->'\\"->"\\\\->\\\\xNN->\xNN\\o->\o\\oo->\oo\\ooo->\ooo\\uXXXX->\u{XXXX}\\u{HEX}->\u{HEX}
由多个连续的 \xNN 转义指定的 UTF-8 转义将无法按预期工作,产生 mojibake。假设转义的数据已经 UTF-8 编码。
use alloc::borrow::Cow;
use descape::UnescapeExt;
let escaped = "Hello,\\nworld!".to_unescaped();
assert_eq!(
escaped,
Ok(Cow::Owned::<'_, str>("Hello,\nworld!".to_string()))
);
let no_escapes = "No escapes here!".to_unescaped();
assert_eq!(
no_escapes,
Ok(Cow::Borrowed("No escapes here!"))
);
// v invalid at index 7
let invalid_escape = "Uh oh! \\xJJ".to_unescaped();
assert_eq!(
invalid_escape,
Err(7)
);