Compare commits
No commits in common. "9ca389c90aedcda80a6a5fc6ec8d77befbd98968" and "4be4032055dea2a1205f19efc5e0685f0afe739d" have entirely different histories.
9ca389c90a
...
4be4032055
3 changed files with 33 additions and 212 deletions
|
@ -1,11 +1,11 @@
|
||||||
[package]
|
[package]
|
||||||
name = "schmfy"
|
name = "schmfy"
|
||||||
version = "0.2.0"
|
version = "0.1.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
description = "Schmfication library"
|
description = "Schmfication library"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
repository = "https://git.flueren.eu/JonOfUs/Schmfy"
|
|
||||||
documentation = "https://docs.rs/schmfy"
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
19
README.md
19
README.md
|
@ -1,20 +1,5 @@
|
||||||
# Schmfy library
|
# Schmfy library
|
||||||
This library is able to schmfy any text.
|
This library is able to schmfy single words.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
Example usage:
|
Just add this crate to your project and you will be able to use the `schmfy()`-function to turn any word into its 'schmord'.
|
||||||
```rust
|
|
||||||
use schmfy::schmfy;
|
|
||||||
|
|
||||||
fn improve_text(text: &str) {
|
|
||||||
let improved_text = schmfy(text);
|
|
||||||
println!("Old and boring: {}", text);
|
|
||||||
println!("Improved and great: {}", text);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Capabilities
|
|
||||||
The schmfication capabilities are able to preserve text case and any non-alphabetical characters.
|
|
||||||
If a non-alphabetical character is between two alphabetical strings, both of the alphabetical strings will be interpreted as completely separate words.
|
|
||||||
|
|
||||||
For example, the HTML code `<span>Entry<br></span>` will be converted into `<schman>Schmentry<schmer></schman>`.
|
|
220
src/lib.rs
220
src/lib.rs
|
@ -1,124 +1,36 @@
|
||||||
//! schmfy - a library to schmfy everything
|
// Schmfies any String
|
||||||
|
pub fn schmfy(source: String) -> String {
|
||||||
#[derive(PartialEq, Copy, Clone)]
|
if source.starts_with("schm") {
|
||||||
enum CaseType {
|
return source;
|
||||||
/// the whole word is in lowercase [default]
|
|
||||||
Lcase,
|
|
||||||
/// the whole word is in uppercase
|
|
||||||
Ucase,
|
|
||||||
/// the first letter us uppercase, the rest is lowercase
|
|
||||||
FstUcase,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the case type of a str
|
|
||||||
fn get_case(txt: &str) -> CaseType {
|
|
||||||
let mut cnt_lcase: usize = 0;
|
|
||||||
let mut cnt_ucase: usize = 0;
|
|
||||||
|
|
||||||
let alph = txt
|
|
||||||
.chars()
|
|
||||||
.filter(|c| c.is_alphabetic())
|
|
||||||
.collect::<Vec<char>>();
|
|
||||||
|
|
||||||
alph.iter().for_each(|c| {
|
|
||||||
if c.is_uppercase() {
|
|
||||||
cnt_ucase += 1;
|
|
||||||
}
|
|
||||||
if c.is_lowercase() {
|
|
||||||
cnt_lcase += 1;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if alph.len() == 0 {
|
|
||||||
return CaseType::Lcase; // default
|
|
||||||
} else if cnt_lcase > 0 && cnt_ucase == 0 {
|
|
||||||
return CaseType::Lcase;
|
|
||||||
} else if cnt_lcase == 0 && cnt_ucase > 0 {
|
|
||||||
return CaseType::Ucase;
|
|
||||||
} else if alph[0].is_uppercase() && alph[1].is_lowercase() {
|
|
||||||
// atleast 2 entries
|
|
||||||
return CaseType::FstUcase;
|
|
||||||
}
|
|
||||||
|
|
||||||
CaseType::Lcase
|
|
||||||
}
|
|
||||||
|
|
||||||
fn restore_case(txt: String, case: CaseType) -> String {
|
|
||||||
match case {
|
|
||||||
CaseType::FstUcase => txt
|
|
||||||
.to_lowercase()
|
|
||||||
.chars()
|
|
||||||
.enumerate()
|
|
||||||
.map(|(pos, c)| {
|
|
||||||
if pos == 0 {
|
|
||||||
c.to_ascii_uppercase()
|
|
||||||
} else {
|
|
||||||
c.to_ascii_lowercase()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect::<String>(),
|
|
||||||
CaseType::Lcase => txt.to_lowercase(),
|
|
||||||
CaseType::Ucase => txt.to_uppercase(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Schmfies any str, preserving case and everything non-alphabetical
|
|
||||||
pub fn schmfy(source: &str) -> String {
|
|
||||||
// instantly return if input is non-alphabetic single char
|
|
||||||
if source.len() == 1 && !source.chars().next().unwrap().is_alphabetic() {
|
|
||||||
return String::from(source);
|
|
||||||
}
|
|
||||||
|
|
||||||
let case = get_case(source);
|
|
||||||
|
|
||||||
// already schmfied
|
|
||||||
if source.to_lowercase().starts_with("schm") {
|
|
||||||
return String::from(source);
|
|
||||||
}
|
|
||||||
|
|
||||||
// empty
|
|
||||||
if source.len() == 0 {
|
if source.len() == 0 {
|
||||||
return String::from(source);
|
return source;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Schmfy each substring separately
|
// can't be empty
|
||||||
let mut current_substr: Vec<char> = vec![];
|
|
||||||
let mut substrings: Vec<String> = vec![];
|
|
||||||
source.chars().for_each(|c| {
|
|
||||||
if c.is_alphabetic() {
|
|
||||||
current_substr.push(c)
|
|
||||||
} else {
|
|
||||||
if current_substr.len() > 0 {
|
|
||||||
substrings.push(current_substr.iter().collect::<String>());
|
|
||||||
current_substr.clear();
|
|
||||||
}
|
|
||||||
substrings.push(c.to_string())
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if current_substr.len() > 0 {
|
|
||||||
substrings.push(current_substr.iter().collect::<String>());
|
|
||||||
}
|
|
||||||
|
|
||||||
if substrings.len() > 1 {
|
|
||||||
return substrings
|
|
||||||
.iter()
|
|
||||||
.map(|txt| schmfy(txt))
|
|
||||||
.collect::<Vec<String>>()
|
|
||||||
.join("");
|
|
||||||
}
|
|
||||||
|
|
||||||
// substrings now has to contain exactly one element
|
|
||||||
let source = substrings[0].to_lowercase();
|
|
||||||
|
|
||||||
if !source.chars().next().unwrap().is_alphabetic() {
|
if !source.chars().next().unwrap().is_alphabetic() {
|
||||||
return String::from(source);
|
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
|
// schmfy first char if word is no longer than 3
|
||||||
if source.len() <= 3 && case != CaseType::FstUcase {
|
if source.len() <= 3 {
|
||||||
let (prefix, suffix) = source.split_at(1);
|
let (prefix, suffix) = source.split_at(1);
|
||||||
let c = prefix.chars().next().unwrap_or('-');
|
let c = prefix.chars().next().unwrap_or('-');
|
||||||
return restore_case(schmfy_char(c) + suffix, case);
|
return schmfy_char(c) + suffix;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Normal words - replace prefix before first vocal
|
// Normal words - replace prefix before first vocal
|
||||||
|
@ -130,10 +42,10 @@ pub fn schmfy(source: &str) -> String {
|
||||||
|
|
||||||
let (_, suffix) = source.split_at(vok_pos);
|
let (_, suffix) = source.split_at(vok_pos);
|
||||||
|
|
||||||
restore_case(String::from("schm") + suffix, case)
|
String::from("schm") + suffix
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Schmfies single char
|
// Schmfies single char
|
||||||
fn schmfy_char(c: char) -> String {
|
fn schmfy_char(c: char) -> String {
|
||||||
let mut ret = String::from("schm");
|
let mut ret = String::from("schm");
|
||||||
match c {
|
match c {
|
||||||
|
@ -170,90 +82,14 @@ fn schmfy_char(c: char) -> String {
|
||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn schmfy_plaintext_tests() {
|
fn it_works() {
|
||||||
assert_eq!(schmfy("test"), "schmest");
|
let schmfied = schmfy(String::from("test"));
|
||||||
assert_eq!(schmfy("Hello"), "Schmello");
|
assert_eq!(schmfied, "schmest");
|
||||||
assert_eq!(schmfy("HELLO"), "SCHMELLO");
|
|
||||||
assert_eq!(schmfy("hello"), "schmello");
|
|
||||||
assert_eq!(schmfy("Bar"), "Schmar");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn schmfy_mixtext_tests() {
|
|
||||||
assert_eq!(schmfy(">Test"), ">Schmest");
|
|
||||||
assert_eq!(schmfy(">tesT"), ">schmest");
|
|
||||||
assert_eq!(schmfy("One&Two"), "Schmone&Schmo");
|
|
||||||
assert_eq!(
|
|
||||||
schmfy("<span>Entry<br></span>"),
|
|
||||||
"<schman>Schmentry<schmer></schman>"
|
|
||||||
);
|
|
||||||
assert_eq!(schmfy("foo/bar/baz"), "schmefoo/schmear/schmeaz");
|
|
||||||
assert_eq!(
|
|
||||||
schmfy("long/Longer/LONGESTTT"),
|
|
||||||
"schmong/Schmonger/SCHMONGESTTT"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn schmfy_sentences_tests() {
|
|
||||||
assert_eq!(
|
|
||||||
schmfy("Today I am VERY tired."),
|
|
||||||
"Schmoday SCHMI schmam SCHMERY schmired."
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
schmfy("Lorem ipsum dolor sit amet, consetetur sadipscing elitr"),
|
|
||||||
"Schmorem schmipsum schmolor schmesit schmamet, schmonsetetur schmadipscing schmelitr"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn schmfy_code_tests() {
|
|
||||||
assert_eq!(
|
|
||||||
schmfy(
|
|
||||||
"#include <stdio.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
while(1)
|
|
||||||
fork();
|
|
||||||
return 0;
|
|
||||||
}"
|
|
||||||
),
|
|
||||||
"#schminclude <schmio.schma>
|
|
||||||
#schminclude <schmesys/schmes.schma>
|
|
||||||
|
|
||||||
schmint schmain()
|
|
||||||
{
|
|
||||||
schmile(1)
|
|
||||||
schmork();
|
|
||||||
schmeturn 0;
|
|
||||||
}"
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
schmfy(
|
|
||||||
"
|
|
||||||
```
|
|
||||||
This is a Markdown codebox
|
|
||||||
```
|
|
||||||
| This | is |
|
|
||||||
|---|---|
|
|
||||||
| a | Markdown |
|
|
||||||
| table | ! |"),
|
|
||||||
"
|
|
||||||
```
|
|
||||||
Schmis schmis schma Schmarkdown schmodebox
|
|
||||||
```
|
|
||||||
| Schmis | schmis |
|
|
||||||
|---|---|
|
|
||||||
| schma | Schmarkdown |
|
|
||||||
| schmable | ! |"
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue