pub fn is_negated_glob(pattern: &str) -> bool {
let mut chars = pattern.chars();
let first_char = chars.next();
let second_char = chars.next();
first_char == Some('!') && second_char != Some('(')
}
pub fn is_absolute_pattern(pattern: &str) -> bool {
let pattern = if is_negated_glob(pattern) { &pattern[1..] } else { pattern };
pattern.starts_with('/') || is_windows_absolute_pattern(pattern)
}
fn is_windows_absolute_pattern(pattern: &str) -> bool {
let mut chars = pattern.chars();
let next_char = chars.next();
if next_char.is_none() || !next_char.unwrap().is_ascii_alphabetic() {
return false;
}
let mut next_char = chars.next();
while next_char.is_some() && next_char.unwrap().is_ascii_alphabetic() {
next_char = chars.next();
}
if next_char != Some(':') {
return false;
}
let next_char = chars.next();
matches!(next_char, Some('/'))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_get_if_absolute_pattern() {
assert_eq!(is_absolute_pattern("test.ts"), false);
assert_eq!(is_absolute_pattern("!test.ts"), false);
assert_eq!(is_absolute_pattern("/test.ts"), true);
assert_eq!(is_absolute_pattern("!/test.ts"), true);
assert_eq!(is_absolute_pattern("D:/test.ts"), true);
assert_eq!(is_absolute_pattern("!D:/test.ts"), true);
}
}