Utf8 checks, using references and avoid allocating

And add docs
This commit is contained in:
João M. Bezerra 2021-11-10 19:59:36 -03:00
parent f1c0c82323
commit 05d83a3726
2 changed files with 9 additions and 18 deletions

View File

@ -10,14 +10,13 @@ use fs_err as fs;
use walkdir::WalkDir;
use zip::{self, read::ZipFile, ZipArchive};
use self::utf8::get_invalid_utf8_paths;
use crate::{
error::FinalError,
info,
list::FileInArchive,
utils::{
cd_into_same_dir_as, concatenate_os_str_list, dir_is_empty, strip_cur_dir, to_utf, user_wants_to_overwrite,
Bytes,
cd_into_same_dir_as, concatenate_os_str_list, dir_is_empty, get_invalid_utf8_paths, strip_cur_dir, to_utf,
user_wants_to_overwrite, Bytes,
},
QuestionPolicy,
};

View File

@ -16,23 +16,15 @@ pub use question::{
pub use utf8::{get_invalid_utf8_paths, is_invalid_utf8};
mod utf8 {
use std::path::{Path, PathBuf};
use std::{ffi::OsStr, path::PathBuf};
pub fn is_invalid_utf8(path: &Path) -> bool {
#[cfg(unix)]
{
use std::{os::unix::prelude::OsStrExt, str};
let bytes = path.as_os_str().as_bytes();
str::from_utf8(bytes).is_err()
}
#[cfg(not(unix))]
{
path.to_str().is_none()
}
/// Check, without allocating, if os_str can be converted into &str
pub fn is_invalid_utf8(os_str: impl AsRef<OsStr>) -> bool {
os_str.as_ref().to_str().is_none()
}
pub fn get_invalid_utf8_paths(paths: &[PathBuf]) -> Vec<PathBuf> {
paths.iter().filter_map(|path| is_invalid_utf8(&path).then(|| path.clone())).collect()
/// Filter out list of paths that are not utf8 valid
pub fn get_invalid_utf8_paths(paths: &[PathBuf]) -> Vec<&PathBuf> {
paths.iter().filter_map(|path| is_invalid_utf8(path).then(|| path)).collect()
}
}