Poc
This commit is contained in:
parent
c10f2b9559
commit
f8dca15b90
|
|
@ -0,0 +1,7 @@
|
|||
[[patterns]]
|
||||
regex = "\\p{Pd}"
|
||||
replacement = "-"
|
||||
[[patterns]]
|
||||
regex = "[\u2018\u2019\u201A\u201B\u02BC\u02BB\u00B4\uFF40\uFF07]"
|
||||
replacement = "'"
|
||||
|
||||
49
src/main.rs
49
src/main.rs
|
|
@ -12,7 +12,7 @@ use rayon::prelude::*; // brings `par_iter()` into scope
|
|||
struct Cli {
|
||||
#[arg( value_parser = clap::value_parser!(PathBuf))]
|
||||
/// The path to analyse
|
||||
path: PathBuf,
|
||||
path: Vec<PathBuf>,
|
||||
/// Path to a configuration file (optional)
|
||||
#[arg(short = 'c', long = "config", value_parser = clap::value_parser!(PathBuf))]
|
||||
config: Option<PathBuf>,
|
||||
|
|
@ -30,7 +30,7 @@ struct Pattern {
|
|||
}
|
||||
const DEFAULT_TOML: &str = include_str!("default_config.toml");
|
||||
|
||||
/// Read the user‑supplied file or fall back to the default.
|
||||
/// Read the user-supplied file or fall back to the default.
|
||||
fn load_config(path: &PathBuf) -> Result<Config> {
|
||||
if path.exists() {
|
||||
let data = fs::read_to_string(path)
|
||||
|
|
@ -52,7 +52,7 @@ fn main() -> Result<()> {
|
|||
// usage: ./regex_replacer <directory> <config.toml>
|
||||
let cli = Cli::parse();
|
||||
|
||||
let target_dir = cli.path;
|
||||
let target_dirs = cli.path;
|
||||
let cfg_path = cli.config.unwrap_or("config.toml".into());
|
||||
|
||||
// ---------------------------------------------------------
|
||||
|
|
@ -65,7 +65,7 @@ fn main() -> Result<()> {
|
|||
.with_context(|| format!("Could not read config file {:?}", cfg_path))?;
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// 3. Compile all regexes once (pre‑compute for speed)
|
||||
// 3. Compile all regexes once (pre-compute for speed)
|
||||
// ---------------------------------------------------------
|
||||
let compiled_patterns: Result<Vec<(Regex, String)>> = cfg
|
||||
.patterns
|
||||
|
|
@ -78,16 +78,9 @@ fn main() -> Result<()> {
|
|||
let compiled_patterns = compiled_patterns?;
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// 4. Walk the directory tree – collect all files
|
||||
// 4. Walk the directory tree - collect all files
|
||||
// ---------------------------------------------------------
|
||||
let files: Vec<PathBuf> = WalkDir::new(&target_dir)
|
||||
.into_iter()
|
||||
.filter_entry(is_not_hidden)
|
||||
.filter_map(|e| e.ok()) // skip errors
|
||||
.filter(|e| e.file_type().is_file())
|
||||
.map(|e| e.path().to_path_buf())
|
||||
.collect();
|
||||
|
||||
let files: Vec<PathBuf> = collect_files(&target_dirs);
|
||||
// ---------------------------------------------------------
|
||||
// 5. Process files in parallel
|
||||
// ---------------------------------------------------------
|
||||
|
|
@ -116,11 +109,29 @@ fn process_file(path: &PathBuf, patterns: &[(Regex, String)]) -> Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Return `true` if the path is *not* hidden.
|
||||
/// Returns `true` for every entry that is **not** hidden.
|
||||
fn is_not_hidden(entry: &DirEntry) -> bool {
|
||||
// Skip anything that starts with a dot.
|
||||
!entry
|
||||
.file_name()
|
||||
.to_str()
|
||||
.is_some_and(|s| s.starts_with('.'))
|
||||
// `entry.file_name()` is a `OsStr`; converting it to a string
|
||||
// is cheap because we only need to check the first byte.
|
||||
!entry.file_name().to_string_lossy().starts_with('.')
|
||||
}
|
||||
|
||||
/// Collect all regular files that are reachable from *any* directory
|
||||
/// in `target_dirs` and are not hidden.
|
||||
pub fn collect_files(target_dirs: &[PathBuf]) -> Vec<PathBuf> {
|
||||
target_dirs
|
||||
.iter()
|
||||
.flat_map(|root| {
|
||||
// `WalkDir::new(root)` produces an iterator over *all*
|
||||
// entries below `root`.
|
||||
// We apply the same filters that were used in the original
|
||||
// single‑directory version.
|
||||
WalkDir::new(root)
|
||||
.into_iter()
|
||||
.filter_entry(is_not_hidden) // skip hidden directories
|
||||
.filter_map(Result::ok) // ignore I/O errors
|
||||
.filter(|e| e.file_type().is_file()) // keep only files
|
||||
.map(|e| e.path().to_path_buf())
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,54 +2,54 @@
|
|||
foo-bar
|
||||
bar-foo
|
||||
|
||||
# Unicode dash variants – all are in the Pd category
|
||||
foo–bar # EN DASH (U+2013)
|
||||
foo—bar # EM DASH (U+2014)
|
||||
foo‑bar # HYPHEN‑MINUS (U+2010)
|
||||
foo‒bar # FIGURE DASH (U+2012)
|
||||
foo––bar # Two EN DASHes side‑by‑side
|
||||
foo‑––‒‑—‑––bar # Mix of all
|
||||
# Unicode dash variants - all are in the Pd category
|
||||
foo-bar # EN DASH (U+2013)
|
||||
foo-bar # EM DASH (U+2014)
|
||||
foo-bar # HYPHEN-MINUS (U+2010)
|
||||
foo-bar # FIGURE DASH (U+2012)
|
||||
foo--bar # Two EN DASHes side-by-side
|
||||
foo---------bar # Mix of all
|
||||
|
||||
# Other punctuation that *looks* like a dash but is not Pd
|
||||
foo·bar # BULLET (U+00B7)
|
||||
foo/ bar # SOLIDUS (U+002F)
|
||||
foo— bar # EM DASH followed by a non‑breaking space (U+00A0)
|
||||
foo- bar # EM DASH followed by a non-breaking space (U+00A0)
|
||||
|
||||
# Edge cases
|
||||
foo- –bar # ASCII hyphen, space, en dash, space, bar
|
||||
foo- -bar # ASCII hyphen, space, en dash, space, bar
|
||||
-foo-bar- # Hyphens at the start and end
|
||||
foo––bar– # Mixed dashes with trailing en dash
|
||||
foo--bar- # Mixed dashes with trailing en dash
|
||||
|
||||
|
||||
# Test words surrounded by dashes – word boundaries
|
||||
# Test words surrounded by dashes - word boundaries
|
||||
-foo-
|
||||
foo-
|
||||
-bar
|
||||
--foo--
|
||||
foo––bar
|
||||
foo‑‑bar
|
||||
foo—‑—bar
|
||||
foo--bar
|
||||
foo--bar
|
||||
foo---bar
|
||||
|
||||
# Dashes embedded in words (should NOT be replaced if you keep word‑boundaries)
|
||||
f‑oo-b‑ar # “f‑oo” (hyphen inside a word)
|
||||
fo–ob–ar # “fo–ob” (en dash inside a word)
|
||||
# Dashes embedded in words (should NOT be replaced if you keep word-boundaries)
|
||||
f-oo-b-ar # “f-oo” (hyphen inside a word)
|
||||
fo-ob-ar # “fo-ob” (en dash inside a word)
|
||||
|
||||
# Dashes as punctuation in a sentence
|
||||
"foo – bar" # En dash surrounded by spaces
|
||||
"foo–bar" # No spaces
|
||||
"foo—bar" # EM dash, no spaces
|
||||
"foo‒bar" # FIGURE dash
|
||||
"foo‑bar" # HYPHEN‑MINUS
|
||||
"foo - bar" # En dash surrounded by spaces
|
||||
"foo-bar" # No spaces
|
||||
"foo-bar" # EM dash, no spaces
|
||||
"foo-bar" # FIGURE dash
|
||||
"foo-bar" # HYPHEN-MINUS
|
||||
|
||||
# Mix of Unicode whitespace
|
||||
foo – bar # U+202F NARROW NO‑BREAK SPACE
|
||||
foo\u2005‑bar # U+2005 MEDIUM MATHEMATICAL SPACE
|
||||
foo - bar # U+202F NARROW NO-BREAK SPACE
|
||||
foo\u2005-bar # U+2005 MEDIUM MATHEMATICAL SPACE
|
||||
|
||||
# Multiple dashes with other punctuation
|
||||
foo--bar # Two ASCII hyphens
|
||||
foo——bar # EM DASH + EM DASH
|
||||
foo‑—‒bar # HYPHEN‑MINUS + EM DASH + FIGURE DASH
|
||||
foo--bar # EM DASH + EM DASH
|
||||
foo---bar # HYPHEN-MINUS + EM DASH + FIGURE DASH
|
||||
|
||||
# Apostrophes
|
||||
Here’s a “quote” with a mix of apostrophes: ‘Don’t’ “re‑write” ‘my’ “code”, ʹand ʽtʾe‑sʾtʽ! Also, `like this`, or a full‑width ’, or a low‑9 ‚and a high‑reversed ‛example’. (End of line.)
|
||||
Here's a “quote” with a mix of apostrophes: 'Don't' “re-write” 'my' “code”, ʹand ʽtʾe-sʾtʽ! Also, 'like this', or a full-width ', or a low-9 'and a high-reversed 'example'. (End of line.)
|
||||
"`This line should stay intact`"
|
||||
|
|
|
|||
Loading…
Reference in New Issue