diff --git a/crates/typos-cli/src/file.rs b/crates/typos-cli/src/file.rs index 8c1b8bf..16767e2 100644 --- a/crates/typos-cli/src/file.rs +++ b/crates/typos-cli/src/file.rs @@ -47,14 +47,7 @@ impl FileChecker for Typos { reporter.report(msg.into())?; } else { let mut accum_line_num = AccumulateLineNum::new(); - let mut ignores: Option = None; - for typo in typos::check_bytes(&buffer, policy.tokenizer, policy.dict) { - if ignores - .get_or_insert_with(|| Ignores::new(&buffer, policy.ignore)) - .is_ignored(typo.span()) - { - continue; - } + for typo in check_bytes(&buffer, policy) { let line_num = accum_line_num.line_num(&buffer, typo.byte_offset); let (line, line_offset) = extract_line(&buffer, typo.byte_offset); let msg = report::Typo { @@ -92,14 +85,7 @@ impl FileChecker for FixTypos { } else { let mut fixes = Vec::new(); let mut accum_line_num = AccumulateLineNum::new(); - let mut ignores: Option = None; - for typo in typos::check_bytes(&buffer, policy.tokenizer, policy.dict) { - if ignores - .get_or_insert_with(|| Ignores::new(&buffer, policy.ignore)) - .is_ignored(typo.span()) - { - continue; - } + for typo in check_bytes(&buffer, policy) { if is_fixable(&typo) { fixes.push(typo.into_owned()); } else { @@ -176,14 +162,7 @@ impl FileChecker for DiffTypos { } else { let mut fixes = Vec::new(); let mut accum_line_num = AccumulateLineNum::new(); - let mut ignores: Option = None; - for typo in typos::check_bytes(&buffer, policy.tokenizer, policy.dict) { - if ignores - .get_or_insert_with(|| Ignores::new(&buffer, policy.ignore)) - .is_ignored(typo.span()) - { - continue; - } + for typo in check_bytes(&buffer, policy) { if is_fixable(&typo) { fixes.push(typo.into_owned()); } else { @@ -553,6 +532,19 @@ fn write_file( Ok(()) } +fn check_bytes<'a>( + buffer: &'a [u8], + policy: &'a crate::policy::Policy<'a, 'a, 'a>, +) -> impl Iterator> { + let mut ignores: Option = None; + + typos::check_bytes(buffer, policy.tokenizer, policy.dict).filter(move |typo| { + !ignores + .get_or_insert_with(|| Ignores::new(buffer, policy.ignore)) + .is_ignored(typo.span()) + }) +} + fn report_result( value: Result, reporter: &dyn report::Report,