From 05d83a3726d718f19b645c8f1b5de67ea9be8ef8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Wed, 10 Nov 2021 19:59:36 -0300 Subject: [PATCH] Utf8 checks, using references and avoid allocating And add docs --- src/archive/zip.rs | 5 ++--- src/utils/mod.rs | 22 +++++++--------------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/src/archive/zip.rs b/src/archive/zip.rs index 0cd6031..6a46b31 100644 --- a/src/archive/zip.rs +++ b/src/archive/zip.rs @@ -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, }; diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 3d0001b..7e948b7 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -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) -> bool { + os_str.as_ref().to_str().is_none() } - pub fn get_invalid_utf8_paths(paths: &[PathBuf]) -> Vec { - 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() } } -- 2.11.4.GIT