Initial commit

This commit is contained in:
Jonathan Flueren 2023-07-07 11:19:14 +02:00
commit 4c36af1d50
3 changed files with 96 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
/Cargo.lock

8
Cargo.toml Normal file
View file

@ -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]

86
src/lib.rs Normal file
View file

@ -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::<Vec<String>>()
.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");
}
}