commit 4c36af1d5060ba1d0e14a50c937b4c15399c98e5 Author: Jonathan Flueren Date: Fri Jul 7 11:19:14 2023 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4fffb2f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +/Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..4f60711 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "schmfy" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..17620d3 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,86 @@ +// Schmfies any String +pub fn schmfy(source: String) -> String { + if source.starts_with("schm") { + return source; + } + + // if source is subsite (e.g. news/fsr), schmfy all parts separately + if source.contains('/') { + return source + .split('/') + .map(|s| schmfy(String::from(s))) + .collect::>() + .join("/"); + } + + if source.is_empty() { + return source; + } + + // schmfy first char if word is no longer than 3 + if source.len() <= 3 { + let (prefix, suffix) = source.split_at(1); + let c = prefix.chars().next().unwrap_or('-'); + return schmfy_char(c) + suffix; + } + + // Normal words - replace prefix before first vocal + // with "schm" + let vok_pos = source + .chars() + .position(|c| "aeiouäöü".contains(c)) + .unwrap_or(0); + + let (_, suffix) = source.split_at(vok_pos); + + String::from("schm") + suffix +} + +// Schmfies single char +fn schmfy_char(c: char) -> String { + let mut ret = String::from("schm"); + match c { + 'a' | 'e' | 'i' | 'o' | 'u' | 'ä' | 'ö' | 'ü' => { + ret.push(c); + } + 'b' | 'c' | 'd' | 'g' | 'p' | 't' | 'w' => ret.push('e'), + 'f' | 'l' | 'm' | 'n' | 'r' | 's' => { + ret.push('e'); + ret.push(c) + } + 'h' | 'k' => ret.push('a'), + 'j' => { + ret.push('o'); + ret.push('t') + } + 'q' => ret.push('u'), + 'v' => { + ret.push('a'); + ret.push('u') + } + 'x' => { + ret.push('i'); + ret.push('x') + } + 'y' => ret.push(c), + 'z' => { + ret.push('e'); + ret.push('t') + } + _ => ret.push(c), + } + + ret +} + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let schmfied = schmfy(String::from("test")); + assert_eq!(schmfied, "schmest"); + } +}