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]
name = "snakify"
version = "1.1.0"
version = "1.1.1"
edition = "2021"
authors = ["mxhagen"]

View File

@ -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();