Expect backslashes to be escaped in dupe: text

This commit is contained in:
RumovZ 2021-01-13 11:54:49 +01:00 committed by Damien Elmes
parent f4cfccfc0a
commit d9bb20ac24
2 changed files with 17 additions and 2 deletions

View file

@ -399,7 +399,7 @@ fn parse_dupe(val: &str) -> ParseResult<SearchNode> {
let text = it.next().ok_or(ParseError {})?;
Ok(SearchNode::Duplicates {
note_type_id: mid,
text: unescape_quotes(text),
text: unescape_quotes_and_backslashes(text),
})
}
@ -478,6 +478,15 @@ fn unescape_quotes(s: &str) -> Cow<str> {
}
}
/// For non-globs like dupe text without any assumption about the content
fn unescape_quotes_and_backslashes(s: &str) -> Cow<str> {
if s.contains('"') || s.contains('\\') {
s.replace(r#"\""#, "\"").replace(r"\\", r"\").into()
} else {
s.into()
}
}
/// Unescape chars with special meaning to the parser.
fn unescape(txt: &str) -> ParseResult<Cow<str>> {
if is_invalid_escape(txt) {

View file

@ -119,7 +119,7 @@ fn write_search_node(node: &SearchNode) -> String {
NoteType(s) => quote(&format!("note:{}", s)),
Rated { days, ease } => write_rated(days, ease),
Tag(s) => quote(&format!("tag:{}", s)),
Duplicates { note_type_id, text } => quote(&format!("dupe:{},{}", note_type_id, text)),
Duplicates { note_type_id, text } => write_dupe(note_type_id, text),
State(k) => write_state(k),
Flag(u) => format!("\"flag:{}\"", u),
NoteIDs(s) => format!("\"nid:{}\"", s),
@ -163,6 +163,12 @@ fn write_rated(days: &u32, ease: &EaseKind) -> String {
}
}
/// Escape double quotes and backslashes: \"
fn write_dupe(note_type_id: &NoteTypeIDType, text: &str) -> String {
let esc = text.replace(r"\", r"\\").replace('"', r#"\""#);
format!("\"dupe:{},{}\"", note_type_id, esc)
}
fn write_state(kind: &StateKind) -> String {
use StateKind::*;
format!(