diff --git a/CHANGELOG.md b/CHANGELOG.md index 1894b78..8854d6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ This project follows [Semantic Versioning](https://semver.org/). - `with_locale("en")` tries `en`, then `en_US`, then `en_GB` - macOS `with_locale` stores the installed language tag that matched, and accepts a bare language when only a regional tag is installed - Linux tokenization uses Hunspell `WORDCHARS`/`ICONV`, keeps curly apostrophes in words, trims edge quotes, and skips URLs and emails +- `is_correct` requires a single whitespace-delimited token with a letter or digit; empty and punctuation-only inputs are false ## [0.4.0] - 2026-08-25 diff --git a/src/lib.rs b/src/lib.rs index b79d27c..7b22862 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -218,8 +218,28 @@ impl Checker { self.0.check(text).map(SpellingError) } - /// Returns true if `word` is spelled correctly. + /// Returns true if `word` is a single spelled-correctly token. + /// + /// The input is trimmed. It must be exactly one whitespace-delimited token + /// that contains at least one letter or digit. Otherwise this returns + /// `false` (empty input, `"!!!"`, `"hello world"`, etc.). pub fn is_correct(&self, word: &str) -> bool { + let word = word.trim(); + if word.is_empty() { + return false; + } + + let mut tokens = word.split_whitespace(); + let Some(token) = tokens.next() else { + return false; + }; + if tokens.next().is_some() { + return false; + } + if !token.chars().any(|c| c.is_alphanumeric()) { + return false; + } + self.check(word).next().is_none() } @@ -540,6 +560,11 @@ mod tests { let checker = Checker::with_locale("en_US").unwrap(); assert!(checker.is_correct("believe")); assert!(!checker.is_correct("beleeve")); + assert!(!checker.is_correct("")); + assert!(!checker.is_correct(" ")); + assert!(!checker.is_correct("!!!")); + assert!(!checker.is_correct("hello world")); + assert!(checker.is_correct(" believe ")); } #[test]