From d6ac36f057cad01ad83bcdf4857a1c4c2bcea1de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Thu, 25 May 2023 16:55:11 +0300 Subject: [PATCH] fix(cli): Make `.in` stripping work with non-UTF-8 filenames --- crates/typos-cli/src/file_type.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/crates/typos-cli/src/file_type.rs b/crates/typos-cli/src/file_type.rs index c5787b6..dc6828c 100644 --- a/crates/typos-cli/src/file_type.rs +++ b/crates/typos-cli/src/file_type.rs @@ -1,4 +1,5 @@ use std::collections::BTreeMap; +use std::path::Path; use kstring::KString; @@ -107,14 +108,23 @@ impl Types { } pub fn file_matched(&self, path: &std::path::Path) -> Option<&str> { - let mut file_name = path.file_name()?.to_str()?; + let mut mpath = Path::new(path); let mut matches = self.matches.get_or_default().borrow_mut(); loop { - self.set.matches_into(file_name, &mut *matches); - if !matches.is_empty() || !file_name.ends_with(".in") { + self.set.matches_into(mpath.file_name()?, &mut *matches); + if !matches.is_empty() { break; } - file_name = file_name.strip_suffix(".in")?; + match mpath.extension() { + None => break, + Some(ext) => { + if ext == "in" { + mpath = Path::new(mpath.file_stem()?); + continue; + } + } + } + break; } matches .last()