From f878fbc0a098427844c048ac0d5de2462f9d345f Mon Sep 17 00:00:00 2001 From: mxhagen Date: Sun, 3 Nov 2024 18:14:47 +0100 Subject: [PATCH] fix sep reduction, trim leading/trailing seps fix separator reduction to single underscore and trim leading as well as trailing separators from output --- Cargo.toml | 2 +- src/main.rs | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b5d8473..3eab91d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "snakify" -version = "1.1.0" +version = "1.1.1" edition = "2021" authors = ["mxhagen"] diff --git a/src/main.rs b/src/main.rs index 47128d2..49f2e7f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,6 +42,7 @@ fn main() { let mut arg_warned = false; let mut output = String::new(); + let mut last = '_'; for arg in std::env::args().skip(1 + arg_start) { if !arg_warned && matches!( @@ -55,11 +56,14 @@ fn main() { arg_warned = true; } - let last_was_separator = output.bytes().last().map(|x| x == b'_').unwrap_or(true); for c in arg.chars() { match c { - ' ' | '-' | '_' if last_was_separator => {} - ' ' | '-' | '_' => output.push('_'), + ' ' | '-' | '_' if last == '_' => {}, + ' ' | '-' | '_' => { + output.push('_'); + last = '_'; + continue; + }, 'a'..='z' | 'A'..='Z' | '0'..='9' => output.push(c.to_ascii_lowercase()), _ if flag == Some(Force) => output.push(c), @@ -69,8 +73,13 @@ fn main() { std::process::exit(1); } } + last = c; + } + + if last != '_' { + output.push('_'); + last = '_'; } - output.push('_') } output.pop();