pub(crate) fn kind(code: &str) -> Option<&str> {
code.split_whitespace().next()
}
pub(crate) fn links(code: &str) -> bool {
let Some(kind) = kind(code) else {
return false;
};
let is = |name: &str| kind.eq_ignore_ascii_case(name);
is("HYPERLINK")
|| ((is("REF") || is("PAGEREF")) && words_after_kind(code).any(|word| REF_LINK.is(word)))
}
pub(super) fn after_kind(code: &str) -> &str {
let code = code.trim_start();
code.find(char::is_whitespace).map_or("", |at| &code[at..])
}
pub(super) fn words_after_kind(code: &str) -> impl Iterator<Item = &str> {
code.split_whitespace().skip(1)
}
pub(super) fn word_argument(word: &str) -> &str {
word.trim_matches('"')
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct QuotedArgument<'a> {
pub(super) text: &'a str,
pub(super) unclosed: bool,
pub(super) rest: &'a str,
}
pub(super) fn quoted_argument(code: &str) -> QuotedArgument<'_> {
let code = code.trim_start();
if let Some(inner) = code.strip_prefix('"') {
return match inner.split_once('"') {
Some((text, rest)) => QuotedArgument {
text,
unclosed: false,
rest,
},
None => QuotedArgument {
text: inner,
unclosed: true,
rest: "",
},
};
}
let (text, rest) = code.split_once(char::is_whitespace).unwrap_or((code, ""));
QuotedArgument {
text,
unclosed: false,
rest,
}
}
pub(super) fn escaped_argument(code: &str) -> String {
let code = code.trim_start();
let mut chars = code.chars();
let quoted = code.starts_with('"');
if quoted {
chars.next();
}
let mut out = String::new();
while let Some(ch) = chars.next() {
match ch {
'\\' => out.extend(chars.next()),
'"' if quoted => break,
ch if ch.is_whitespace() && !quoted => break,
ch => out.push(ch),
}
}
out
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum SwitchCase {
Exact,
AnyCase,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct Switch {
name: &'static str,
case: SwitchCase,
}
impl Switch {
pub(super) fn is(self, word: &str) -> bool {
match self.case {
SwitchCase::Exact => word == self.name,
SwitchCase::AnyCase => word.eq_ignore_ascii_case(self.name),
}
}
pub(super) fn after_first(self, code: &str) -> Option<&str> {
code.split_once(self.name).map(|(_, rest)| rest)
}
}
pub(super) const FORMAT: Switch = Switch {
name: "\\*",
case: SwitchCase::Exact,
};
pub(super) const PICTURE: Switch = Switch {
name: "\\@",
case: SwitchCase::Exact,
};
pub(super) const REF_PATTERN: Switch = Switch {
name: "\\n",
case: SwitchCase::Exact,
};
pub(super) const REF_RELATIVE: Switch = Switch {
name: "\\r",
case: SwitchCase::Exact,
};
pub(super) const REF_FULL: Switch = Switch {
name: "\\w",
case: SwitchCase::Exact,
};
pub(super) const REF_LINK: Switch = Switch {
name: "\\h",
case: SwitchCase::Exact,
};
pub(super) const PAGE_POSITION: Switch = Switch {
name: "\\p",
case: SwitchCase::AnyCase,
};
pub(super) const STYLE_LAST: Switch = Switch {
name: "\\l",
case: SwitchCase::AnyCase,
};
pub(super) const STYLE_NUMBER: Switch = Switch {
name: "\\n",
case: SwitchCase::AnyCase,
};
pub(super) const FORM_DROP_DOWN: &str = "FORMDROPDOWN";
pub(super) const FORM_TEXT: &str = "FORMTEXT";
pub(super) const ADVANCE_RIGHT: Switch = Switch {
name: "\\r",
case: SwitchCase::AnyCase,
};
pub(super) const ADVANCE_LEFT: Switch = Switch {
name: "\\l",
case: SwitchCase::AnyCase,
};
pub(super) const MERGE_FORMAT: &str = "MERGEFORMAT";
pub(super) const CHAR_FORMAT: &str = "CHARFORMAT";
pub(super) fn is_format(word: &str, names: &[&str]) -> bool {
names.iter().any(|name| word.eq_ignore_ascii_case(name))
}
#[cfg(test)]
mod tests {
use super::{
FORMAT, PAGE_POSITION, PICTURE, QuotedArgument, REF_FULL, REF_LINK, REF_PATTERN,
REF_RELATIVE, STYLE_LAST, after_kind, escaped_argument, kind, quoted_argument,
word_argument, words_after_kind,
};
#[test]
fn the_three_readings_of_an_argument_differ_on_quotes_spaces_and_backslashes() {
let cases = [
(r#" "a b" \n"#, "a", ("a b", false, r" \n"), "a b"),
(
r#" "a \"b\" c""#,
"a",
(r"a \", false, r#"b\" c""#),
r#"a "b" c"#,
),
(r#" "a b"#, "a", ("a b", true, ""), "a b"),
(r#" "a"b c"#, r#"a"b"#, ("a", false, "b c"), "a"),
(r" a\ b", r"a\", (r"a\", false, "b"), "a b"),
(" plain rest", "plain", ("plain", false, "rest"), "plain"),
];
for (code, word, (text, unclosed, rest), escaped) in cases {
assert_eq!(
words_after_kind(&format!("KIND{code}"))
.next()
.map(word_argument),
Some(word),
"{code}"
);
assert_eq!(
quoted_argument(code),
QuotedArgument {
text,
unclosed,
rest
},
"{code}"
);
assert_eq!(escaped_argument(code), escaped, "{code}");
}
}
#[test]
fn reads_the_kind_and_the_code_after_it() {
assert_eq!(kind(" toc \\o \"1-3\""), Some("toc"));
assert_eq!(kind(" "), None);
assert_eq!(after_kind(" STYLEREF Heading"), " Heading");
assert_eq!(after_kind(" PAGE"), "");
}
#[test]
fn reads_each_switch_in_its_case() {
let cases = [
(REF_PATTERN, r"\n", true),
(REF_PATTERN, r"\N", false),
(REF_RELATIVE, r"\R", false),
(REF_FULL, r"\W", false),
(REF_LINK, r"\H", false),
(FORMAT, r"\*", true),
(FORMAT, r"\**", false),
(PAGE_POSITION, r"\P", true),
(PAGE_POSITION, r"\p", true),
(STYLE_LAST, r"\L", true),
(STYLE_LAST, r"\l", true),
(STYLE_LAST, r"\n", false),
];
for (switch, word, is) in cases {
assert_eq!(switch.is(word), is, "{switch:?} {word}");
}
}
#[test]
fn finds_the_picture_switch_anywhere_also_inside_a_quoted_argument() {
assert_eq!(
PICTURE.after_first(r#"DOCPROPERTY "x \@ y""#),
Some(r#" y""#)
);
assert_eq!(PICTURE.after_first(r#"DATE\@"yyyy""#), Some(r#""yyyy""#));
assert_eq!(PICTURE.after_first("DATE"), None);
}
}