fix sep reduction, trim leading/trailing seps

fix separator reduction to single underscore and trim leading
as well as trailing separators from output
This commit is contained in:
mxhagen 2024-11-03 18:14:47 +01:00
parent 81cf35c69e
commit f878fbc0a0
2 changed files with 14 additions and 5 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "snakify" name = "snakify"
version = "1.1.0" version = "1.1.1"
edition = "2021" edition = "2021"
authors = ["mxhagen"] authors = ["mxhagen"]

View File

@ -42,6 +42,7 @@ fn main() {
let mut arg_warned = false; let mut arg_warned = false;
let mut output = String::new(); let mut output = String::new();
let mut last = '_';
for arg in std::env::args().skip(1 + arg_start) { for arg in std::env::args().skip(1 + arg_start) {
if !arg_warned if !arg_warned
&& matches!( && matches!(
@ -55,11 +56,14 @@ fn main() {
arg_warned = true; arg_warned = true;
} }
let last_was_separator = output.bytes().last().map(|x| x == b'_').unwrap_or(true);
for c in arg.chars() { for c in arg.chars() {
match c { match c {
' ' | '-' | '_' if last_was_separator => {} ' ' | '-' | '_' if last == '_' => {},
' ' | '-' | '_' => output.push('_'), ' ' | '-' | '_' => {
output.push('_');
last = '_';
continue;
},
'a'..='z' | 'A'..='Z' | '0'..='9' => output.push(c.to_ascii_lowercase()), 'a'..='z' | 'A'..='Z' | '0'..='9' => output.push(c.to_ascii_lowercase()),
_ if flag == Some(Force) => output.push(c), _ if flag == Some(Force) => output.push(c),
@ -69,8 +73,13 @@ fn main() {
std::process::exit(1); std::process::exit(1);
} }
} }
last = c;
}
if last != '_' {
output.push('_');
last = '_';
} }
output.push('_')
} }
output.pop(); output.pop();