use fancy_regex::Regex as FancyRegex;
use regex::Regex;
use std::sync::LazyLock;
pub static SIC: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\s*[(\[]\s*sic\s*!?\s*[)\]]").unwrap());
pub static AGGREGATE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r"(?i)(?:\s+(?:agg\.?|aggregate|species\s+group|species\s+complex|group|complex)|\s*-\s*group|\s*-\s*aggregate)\s*$",
)
.unwrap()
});
pub static IN_PRESS: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"(?i)\s+in\s+press\b\.?").unwrap());
pub static PUBLISHED_PAGE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\s*:\s*(\d+(?:[-\x{2013}]\d+)?)\s*$").unwrap());
pub static TAX_NOTE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r"(?i)\s+,?\s*(auctt?\b\.?(?:[,.]?\s.*)?|sensu(?:\s.*)?|sec\.?(?:\s.*)?|nec\b(?:\s.*)?|nonn?\.?\s+\(?\p{Lu}.*|emend\b\.?\s+\(?\p{Lu}.*|fide\b\.?\s+\(?\p{Lu}.*|according\s+to\s+\p{Lu}.*|excl\.\s+.*|ss\b\.?\s+.*|(?-i:s\.\s*l\.?|s\.\s*str\.?|s\.\s*lat\.?|s\.\s*ampl\.?))$",
)
.unwrap()
});
static CORRIG_BRACKETED: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\s*[(\[]\s*corrig\.?\s*[)\]]").unwrap());
static CORRIG_BARE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(\s)corrig\.?(\s|$)").unwrap());
fn collapse_ws(s: &str) -> String {
s.split_whitespace().collect::<Vec<_>>().join(" ")
}
pub fn strip_corrig_restructured(s: &str) -> String {
let padded = format!(" {s}");
let no_bracket = CORRIG_BRACKETED.replace_all(&padded, "");
let no_bare = CORRIG_BARE.replace_all(&no_bracket, "${1}${2}");
collapse_ws(&no_bare)
}
static CORRIG_FANCY: LazyLock<FancyRegex> = LazyLock::new(|| {
FancyRegex::new(r"\s*[(\[]\s*corrig\.?\s*[)\]]|(?<=\s)corrig\.?(?=\s|$)").unwrap()
});
pub fn strip_corrig_fancy(s: &str) -> String {
let padded = format!(" {s}");
let mut result = String::with_capacity(padded.len());
let mut last = 0usize;
for m in CORRIG_FANCY.find_iter(&padded) {
let m = match m {
Ok(m) => m,
Err(_) => break,
};
result.push_str(&padded[last..m.start()]);
last = m.end();
}
result.push_str(&padded[last..]);
collapse_ws(&result)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sic_removed() {
assert_eq!(
SIC.replace_all("Ameiva plei (sic) Bibron", "").as_ref(),
"Ameiva plei Bibron"
);
}
#[test]
fn aggregate_trimmed() {
assert_eq!(
AGGREGATE
.replace_all("Achillea millefolium agg.", "")
.as_ref(),
"Achillea millefolium"
);
}
#[test]
fn in_press_removed() {
assert_eq!(
IN_PRESS.replace_all("Aus bus Smith in press", "").as_ref(),
"Aus bus Smith"
);
}
#[test]
fn published_page_captured() {
let caps = PUBLISHED_PAGE.captures("Aus bus Smith : 377").unwrap();
assert_eq!(&caps[1], "377");
}
#[test]
fn tax_note_stripped() {
assert_eq!(
TAX_NOTE.replace_all("Aus bus sensu Smith", "").as_ref(),
"Aus bus"
);
assert_eq!(TAX_NOTE.replace_all("Aus bus s.l.", "").as_ref(), "Aus bus");
}
#[test]
fn corrig_bracketed() {
assert_eq!(
strip_corrig_restructured("Aus bus (corrig.) Smith"),
"Aus bus Smith"
);
assert_eq!(
strip_corrig_fancy("Aus bus (corrig.) Smith"),
"Aus bus Smith"
);
}
#[test]
fn corrig_bare_word_between_spaces() {
let collapse = |s: String| s.split_whitespace().collect::<Vec<_>>().join(" ");
assert_eq!(
collapse(strip_corrig_restructured("Aus bus corrig. Smith")),
"Aus bus Smith"
);
assert_eq!(
collapse(strip_corrig_fancy("Aus bus corrig. Smith")),
"Aus bus Smith"
);
}
#[test]
fn corrig_not_matched_mid_word() {
assert_eq!(
strip_corrig_restructured("Aus corrigenda Smith"),
"Aus corrigenda Smith"
);
assert_eq!(
strip_corrig_fancy("Aus corrigenda Smith"),
"Aus corrigenda Smith"
);
}
#[test]
fn case_insensitive_flag_is_active() {
assert_eq!(
AGGREGATE
.replace_all("Achillea millefolium AGG.", "")
.as_ref(),
"Achillea millefolium"
);
assert_eq!(
TAX_NOTE.replace_all("Aus bus SENSU Smith", "").as_ref(),
"Aus bus"
);
}
#[test]
fn tax_note_sl_marker_is_case_sensitive() {
assert!(
!TAX_NOTE.is_match("Aus bus Mill. S.L."),
"uppercase S.L. must not match the sensu-lato marker"
);
assert!(
TAX_NOTE.is_match("Aus bus s.l."),
"lower-case s.l. must match the marker"
);
}
#[test]
fn corrig_strips_leading_and_standalone_marker_like_java() {
assert_eq!(
strip_corrig_restructured("corrig. Peters, 1878"),
"Peters, 1878"
);
assert_eq!(strip_corrig_fancy("corrig. Peters, 1878"), "Peters, 1878");
assert_eq!(strip_corrig_restructured("corrig."), "");
assert_eq!(strip_corrig_fancy("corrig."), "");
}
}