Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
fix(core): out of bounds errors
Elijah Potter4167300e41b3
diff --git a/harper-core/src/parsers/markdown.rs b/harper-core/src/parsers/markdown.rsindex ea21e9c4e..16e40cb18 100644--- a/harper-core/src/parsers/markdown.rs+++ b/harper-core/src/parsers/markdown.rs@@ -37,7 +37,7 @@ impl Markdown {/// Remove hidden Wikilink target text.///- /// As in, the stuff to the left of the pipe operator:+ /// As in the stuff to the left of the pipe operator:////// ```markdown/// [[Target text|Display Text]]@@ -46,6 +46,10 @@ impl Markdown {let mut to_remove = VecDeque::new();for pipe_idx in tokens.iter_pipe_indices() {+ if pipe_idx < 2 {+ continue;+ }+// Locate preceding `[[`let mut cursor = pipe_idx - 2;let mut open_bracket = None;@@ -351,6 +355,35 @@ mod tests {))}+ #[test]+ fn just_pipe() {+ let source = r"|";++ let tokens = Markdown::default().parse_str(source);++ let token_kinds = tokens.iter().map(|t| t.kind).collect::<Vec<_>>();++ dbg!(&token_kinds);++ assert!(matches!(+ token_kinds.as_slice(),+ &[TokenKind::Punctuation(Punctuation::Pipe)]+ ))+ }++ #[test]+ fn empty_wikilink_text() {+ let source = r"[[|]]";++ let tokens = Markdown::default().parse_str(source);++ let token_kinds = tokens.iter().map(|t| t.kind).collect::<Vec<_>>();++ dbg!(&token_kinds);++ assert!(matches!(token_kinds.as_slice(), &[]))+ }+#[test]fn improper_wikilink_text() {let source = r"this is shown|this is also shown]]";diff --git a/harper-core/src/title_case.rs b/harper-core/src/title_case.rsindex 3dd262698..c215343eb 100644--- a/harper-core/src/title_case.rs+++ b/harper-core/src/title_case.rs@@ -1,5 +1,6 @@use crate::Lrc;use crate::Token;+use crate::TokenKind;use hashbrown::HashSet;use lazy_static::lazy_static;@@ -33,33 +34,8 @@ pub fn make_title_case(toks: &[Token], source: &[char], dict: &impl Dictionary)let mut word_likes = toks.iter_word_likes().enumerate().peekable();let mut output = toks.span().unwrap().get_content(source).to_vec();- // Only specific conjunctions are not capitalized.- lazy_static! {- static ref SPECIAL_CONJUNCTIONS: HashSet<Vec<char>> = ["and", "but", "for", "or", "nor"]- .iter()- .map(|v| v.chars().collect())- .collect();- }-while let Some((index, word)) = word_likes.next() {- if !word.kind.is_word() {- continue;- }-- let chars = word.span.get_content(source);- let chars_lower = chars.to_lower();-- let metadata = word- .kind- .as_word()- .unwrap()- .or(&dict.get_word_metadata(&chars_lower));-- let is_short_preposition = metadata.preposition && word.span.len() <= 4;-- let should_capitalize = (!is_short_preposition- && !metadata.article- && !SPECIAL_CONJUNCTIONS.contains(chars_lower.as_slice()))+ let should_capitalize = should_capitalize_token(&word, source, dict)|| index == 0|| word_likes.peek().is_none();@@ -82,10 +58,39 @@ pub fn make_title_case(toks: &[Token], source: &[char], dict: &impl Dictionary)output}+/// Determines whether a token should be capitalized.+/// Is not responsible for capitalization requirements that are dependent on token position.+fn should_capitalize_token(tok: &Token, source: &[char], dict: &impl Dictionary) -> bool {+ match tok.kind {+ TokenKind::Word(mut metadata) => {+ // Only specific conjunctions are not capitalized.+ lazy_static! {+ static ref SPECIAL_CONJUNCTIONS: HashSet<Vec<char>> =+ ["and", "but", "for", "or", "nor"]+ .iter()+ .map(|v| v.chars().collect())+ .collect();+ }++ let chars = tok.span.get_content(source);+ let chars_lower = chars.to_lower();++ metadata = metadata.or(&dict.get_word_metadata(&chars_lower));++ let is_short_preposition = metadata.preposition && tok.span.len() <= 4;++ !is_short_preposition+ && !metadata.article+ && !SPECIAL_CONJUNCTIONS.contains(chars_lower.as_slice())+ }+ _ => true,+ }+}+#[cfg(test)]mod tests {- use quickcheck::{Arbitrary, TestResult};+ use quickcheck::TestResult;use quickcheck_macros::quickcheck;use super::make_title_case_str;@@ -131,49 +136,28 @@ mod tests {)}- #[derive(Debug, Clone)]- struct Word(String);-- impl Arbitrary for Word {- fn arbitrary(g: &mut quickcheck::Gen) -> Self {- let mut s = String::new();-- for _ in 0..g.size() {- let c = loop {- let gen = char::arbitrary(g);-- if gen.is_ascii_alphanumeric() {- break gen;- }- };-- s.push(c);- }-- Self(s)- }+ #[test]+ fn pipe_does_not_cause_crash() {+ assert_eq!(+ make_title_case_str("|", &Markdown::default(), &FstDictionary::curated()),+ "|"+ )}- #[derive(Debug, Clone)]- struct Sentence(String);-- /// Builds a sentence out of words from the curated [`FullDictionary`].- impl Arbitrary for Sentence {- fn arbitrary(g: &mut quickcheck::Gen) -> Self {- let mut s = String::new();-- let Word(first_word) = Word::arbitrary(g);- s.push_str(&first_word);-- for _ in 0..g.size() {- let Word(word) = Word::arbitrary(g);-- s.push(' ');- s.push_str(&word);- }+ #[test]+ fn a_paragraph_does_not_cause_crash() {+ assert_eq!(+ make_title_case_str("A\n", &Markdown::default(), &FstDictionary::curated()),+ "A"+ )+ }- Self(s)- }+ #[test]+ fn tab_a_becomes_upcase() {+ assert_eq!(+ make_title_case_str("\ta", &PlainEnglish, &FstDictionary::curated()),+ "\tA"+ )}#[quickcheck]@@ -195,8 +179,6 @@ mod tests {.chars().collect();- dbg!(&title_case);-TestResult::from_bool(title_case[prefix.chars().count() + 1] == 'a')}@@ -223,15 +205,15 @@ mod tests {}#[quickcheck]- fn first_word_is_upcase(sentence: Sentence) -> TestResult {+ fn first_word_is_upcase(text: String) -> TestResult {let title_case: Vec<_> =- make_title_case_str(&sentence.0, &Markdown::default(), &FstDictionary::curated())+ make_title_case_str(&text, &PlainEnglish, &FstDictionary::curated()).chars().collect();if let Some(first) = title_case.first() {- if first.is_alphabetic() {- TestResult::from_bool(first.is_uppercase())+ if first.is_ascii_alphabetic() {+ TestResult::from_bool(first.is_ascii_uppercase())} else {TestResult::discard()}diff --git a/harper-core/src/token.rs b/harper-core/src/token.rsindex 06ab11ce9..f33c4a2b4 100644--- a/harper-core/src/token.rs+++ b/harper-core/src/token.rs@@ -156,7 +156,16 @@ impl TokenStringExt for [Token] {}fn span(&self) -> Option<Span> {- Some(Span::new(self.first()?.span.start, self.last()?.span.end))+ let min_max = self+ .iter()+ .flat_map(|v| [v.span.start, v.span.end].into_iter())+ .minmax();++ match min_max {+ itertools::MinMaxResult::NoElements => None,+ itertools::MinMaxResult::OneElement(min) => Some(Span::new(min, min)),+ itertools::MinMaxResult::MinMax(min, max) => Some(Span::new(min, max)),+ }}fn iter_linking_verb_indices(&self) -> impl Iterator<Item = usize> + '_ {diff --git a/justfile b/justfileindex 54f16bfc9..20f056b4b 100644--- a/justfile+++ b/justfile@@ -270,12 +270,12 @@ bump-versions:lazygit# Enter an infinite loop of testing until a bug is found.-fuzz mod:+fuzz:#!/usr/bin/bashwhile truedo- QUICKCHECK_TESTS=1000 cargo test {{mod}}+ QUICKCHECK_TESTS=100000 cargo testif [[ x$? != x0 ]] ; thenexit $?fi