Check whether a period at `pos` in `text` is part of a known abbreviation. Requires a word boundary before the abbreviation — the abbreviation must be preceded by a space, start of string, or punctuation. This prevents "Jest." from matching "est." or "arest." from matching "est.".
(text: &str, dot_pos: usize)
| 182 | /// be preceded by a space, start of string, or punctuation. This prevents |
| 183 | /// "Jest." from matching "est." or "arest." from matching "est.". |
| 184 | pub(crate) fn is_abbreviation(text: &str, dot_pos: usize) -> bool { |
| 185 | let before = &text[..=dot_pos]; |
| 186 | for abbr in ABBREVIATIONS { |
| 187 | if before.ends_with(abbr) { |
| 188 | // Check word boundary: the character before the abbreviation |
| 189 | // must be whitespace, start-of-string, or non-alphabetic. |
| 190 | let abbr_start = before.len() - abbr.len(); |
| 191 | if abbr_start == 0 { |
| 192 | // Abbreviation is at the very start of the text |
| 193 | return true; |
| 194 | } |
| 195 | let preceding = before.as_bytes()[abbr_start - 1]; |
| 196 | if !preceding.is_ascii_alphabetic() { |
| 197 | // Preceded by space, punctuation, etc. — real abbreviation |
| 198 | return true; |
| 199 | } |
| 200 | // Otherwise it's a suffix match (e.g., "Jest." matching "est.") |
| 201 | // — not an abbreviation, keep looking |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // Also catch single-letter abbreviations like "e." in "e.g." |
| 206 | // by checking if the character before the dot is a single letter |
| 207 | // preceded by another dot (pattern: letter-dot-letter-dot) |
| 208 | if dot_pos >= 2 { |
| 209 | let bytes = text.as_bytes(); |
| 210 | if bytes[dot_pos - 1].is_ascii_alphabetic() && bytes[dot_pos - 2] == b'.' { |
| 211 | return true; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | false |
| 216 | } |
| 217 | |
| 218 | /// Extract the first sentence from a single paragraph of text. |
| 219 | /// |
no test coverage detected