use super::check::UNKNOWN_ITEM;
pub(super) fn printed_item(name: &str, args: &[&str]) -> String {
let words = match name {
"echo" => &args[args.iter().take_while(|a| is_echo_switch(a)).count()..],
_ => args,
};
match words {
[] => "sc_item".to_string(),
[word] if is_plain(word, name == "printf") => (*word).to_string(),
_ => UNKNOWN_ITEM.to_string(),
}
}
fn is_echo_switch(arg: &str) -> bool {
arg.strip_prefix('-')
.is_some_and(|rest| !rest.is_empty() && rest.chars().all(|c| matches!(c, 'n' | 'e' | 'E')))
}
fn is_plain(word: &str, printf: bool) -> bool {
!word.is_empty()
&& !word.chars().any(|c| c.is_whitespace() || matches!(c, '\'' | '"' | '\\') || (printf && c == '%'))
&& !(printf && word.starts_with('-'))
}
#[cfg(test)]
#[path = "printed_tests.rs"]
mod tests;