Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SUPWW SPWEED *>w<* (7x speed boost) #2

Merged
merged 29 commits into from
Jan 30, 2022
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
6be7aea
Optimize compile times by converting to lib.rs standard
StratusFearMe21 Jan 30, 2022
29292bd
Speewd *UwU*
StratusFearMe21 Jan 30, 2022
e3459a3
Add Teswts *UWU*
StratusFearMe21 Jan 30, 2022
aa8e84f
OwO fix file stwuctuwe OwO
StratusFearMe21 Jan 30, 2022
6f0bdf9
2x Spweed Bwoost (◎0◎)
StratusFearMe21 Jan 30, 2022
2ad28b4
If statemwent optimization
StratusFearMe21 Jan 30, 2022
71f408e
SUPWW SPWEED >w< *shits aggessively*
StratusFearMe21 Jan 30, 2022
29e9d92
Cwap awgument awtimization UwU
StratusFearMe21 Jan 30, 2022
c14a18a
Fix examples fwldew
StratusFearMe21 Jan 30, 2022
1111bda
Vawious optimizations UwU
StratusFearMe21 Jan 30, 2022
2c7b0bc
Fix bowwing pwobwems
StratusFearMe21 Jan 30, 2022
d43de4f
Bug Fixews ;w;
StratusFearMe21 Jan 30, 2022
d43c0e1
Add back repwaducabiwaty
StratusFearMe21 Jan 30, 2022
dd7e88e
Hmmmm *w*
StratusFearMe21 Jan 30, 2022
de069f9
Fix newwine pwobwems UwU
StratusFearMe21 Jan 30, 2022
7f91d6f
6X SPWWWWEEEEDDD *<>* *<>* *<>*
StratusFearMe21 Jan 30, 2022
651b145
MOWE BUG FIXWES ;;;
StratusFearMe21 Jan 30, 2022
3252426
FIFTEEN TIMES SPEED BOOST HOLY SHIT OMG HOW TF *<>*
StratusFearMe21 Jan 30, 2022
b1e3b57
F-fwix s-stutewing
StratusFearMe21 Jan 30, 2022
1faffcd
Wemove U-Unused depwendency
StratusFearMe21 Jan 30, 2022
e20c823
Wemove U-Unused fiwe
StratusFearMe21 Jan 30, 2022
8d229dc
Update Fowmatting *()*
StratusFearMe21 Jan 30, 2022
668598e
Fix consistewncy issuews UwU
StratusFearMe21 Jan 30, 2022
6940ee0
Bettew ewwow handwing OwO *UwU*
StratusFearMe21 Jan 30, 2022
41f7813
Bettew ewwow handwing OwO *UwU*
StratusFearMe21 Jan 30, 2022
7e46906
Bettew ewwow handwing OwO *UwU*
StratusFearMe21 Jan 30, 2022
746da9b
Bettew ewwow handwing OwO *UwU*
StratusFearMe21 Jan 30, 2022
42e74db
Wemove U-Unused fiwe
StratusFearMe21 Jan 30, 2022
52e6683
Wemove benchmawks
StratusFearMe21 Jan 30, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,29 @@ repository = "https://github.com/sgoudham/uwuifyy"
keywords = ["cli", "uwu", "owo", "uwuify", "anime"]
categories = ["command-line-utilities"]
exclude = [
"examples/the-complete-works-of-william-shakespeare.txt",
"examples/tiktok_app_reviews.csv",
"examples/tokyo-2020-olympics-tweets.csv",
"examples/uwu/**"
"examples/the-complete-works-of-william-shakespeare.txt",
"examples/tiktok_app_reviews.csv",
"examples/tokyo-2020-olympics-tweets.csv",
"examples/uwu/**",
]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
name = "uwuifyy"

[dependencies]
clap = { version = "3.0.10", features = ["derive"] }
clap = "3.0.13"
rand = "0.8.4"
rand_seeder = "0.2.2"
rand_pcg = "0.3.1"
indicatif = "0.16.2"
linkify = "0.8.0"
linkify = "0.8.0"
rand_xoshiro = "0.6.0"
memmap = "0.7.0"

[profile.release]
sgoudham marked this conversation as resolved.
Show resolved Hide resolved
lto = "fat"
codegen-units = 1
panic = "abort"

[features]
bench = []
124,456 changes: 124,456 additions & 0 deletions examples/uwu/william-shakespeare.txt

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
pub const FACES_SIZE: usize = 14;
pub const FACES: [&[u8]; FACES_SIZE] = [
sgoudham marked this conversation as resolved.
Show resolved Hide resolved
b"OwO",
b"UwU",
b">w<",
b"^w^",
b"^-^",
b":3",
b"x3",
b"xDD",
b";;w;;",
b">_<",
b">_>",
b"^.^",
b":33",
b"uWu",
];

pub const ACTIONS_SIZE: usize = 17;
pub const ACTIONS: [&[u8]; ACTIONS_SIZE] = [
b"*notices bulge*",
b"*cries*",
b"*hugs tightly*",
b"*screams*",
b"*looks away*",
b"*blushes*",
b"*sweats*",
b"*cuddles you*",
b"*moans*",
b"*giggles shyly*",
b"*looks at you*",
b"*twerks*",
b"*sighs*",
b"*leans over*",
b"*pokes you*",
b"*teleports behind you*",
b"*shuffles closer*",
];
234 changes: 234 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
#![cfg_attr(test, feature(test))]
sgoudham marked this conversation as resolved.
Show resolved Hide resolved

use indicatif::{ProgressBar, ProgressStyle};
use linkify::{LinkFinder, LinkKind};
use rand::{Rng, RngCore, SeedableRng};
use rand_xoshiro::{Xoshiro256Plus, Xoshiro256PlusPlus};
use std::fs::File;
use std::io::{BufWriter, Error, Write};
use std::path::Path;

use constants::ACTIONS;
use constants::ACTIONS_SIZE;
use constants::FACES;
use constants::FACES_SIZE;

mod constants;

macro_rules! progress_bar {
() => {{
let progress_bar = ProgressBar::new_spinner();
progress_bar.set_style(
ProgressStyle::default_spinner().template("{spinner:.green} [{elapsed_precise}] {msg}"),
);
progress_bar.enable_steady_tick(100);

progress_bar
}};
}

#[derive(Debug)]
pub struct UwUify<'a> {
sgoudham marked this conversation as resolved.
Show resolved Hide resolved
text: &'a str,
input: &'a str,
output: &'a str,
words: f64,
faces: f64,
actions: f64,
stutters: f64,
floating_rng: Xoshiro256Plus,
int_rng: Xoshiro256PlusPlus,
linkify: LinkFinder,
}

impl<'a> Default for UwUify<'a> {
fn default() -> Self {
Self {
text: "",
input: "",
output: "",
words: 1.0,
faces: 0.05,
actions: 0.125,
stutters: 0.225,
floating_rng: Xoshiro256Plus::seed_from_u64(69),
int_rng: Xoshiro256PlusPlus::seed_from_u64(420),
linkify: LinkFinder::new(),
}
}
}

impl<'a> UwUify<'a> {
pub fn new(
text: Option<&'a str>,
infile: Option<&'a str>,
outfile: Option<&'a str>,
words: Option<&'a str>,
faces: Option<&'a str>,
actions: Option<&'a str>,
stutters: Option<&'a str>,
random: bool,
) -> UwUify<'a> {
// I dislike this

let mut linkify = LinkFinder::new();
linkify.kinds(&[LinkKind::Email, LinkKind::Url]);
linkify.url_must_have_scheme(false);
let mut uwuify = UwUify {
text: text.unwrap_or_default(),
input: infile.unwrap_or_default(),
output: outfile.unwrap_or_default(),
linkify,
..Default::default()
};

if random {
uwuify.floating_rng = Xoshiro256Plus::seed_from_u64(rand::rngs::OsRng.next_u64());
uwuify.int_rng = Xoshiro256PlusPlus::seed_from_u64(rand::rngs::OsRng.next_u64());
}

if let Some(words) = words {
uwuify.words = words.parse::<f64>().unwrap();
}
if let Some(faces) = faces {
uwuify.faces = faces.parse::<f64>().unwrap();
}
if let Some(actions) = actions {
uwuify.actions = actions.parse::<f64>().unwrap();
}
if let Some(stutters) = stutters {
uwuify.stutters = stutters.parse::<f64>().unwrap();
}
uwuify
}

pub fn uwuify(&mut self) -> Result<(), Error> {
// Handle Text
if !self.text.is_empty() {
// Handle Text Output
if !self.output.is_empty() {
if Path::new(&self.output).exists() {
return Err(Error::new(
std::io::ErrorKind::AlreadyExists,
format!("File '{}' already exists, aborting operation", &self.output),
));
}

let mut uwu_out_file = BufWriter::new(File::create(&self.output)?);
let uwu_progress_bar = progress_bar!();
self.uwuify_sentence(self.text, &mut uwu_out_file)?;

uwu_progress_bar.finish_with_message("UwU'ifying Complete!");
Ok(())
} else {
#[cfg(not(test))]
sgoudham marked this conversation as resolved.
Show resolved Hide resolved
let stdout = std::io::stdout();
#[cfg(not(test))]
let mut out = BufWriter::new(stdout.lock());
#[cfg(test)]
let mut out = std::io::sink();
self.uwuify_sentence(self.text, &mut out)?;
#[cfg(not(test))]
out.write_all(b"\n")?;
Ok(())
}
} else {
// Handle File I/O
if Path::new(&self.output).exists() {
return Err(Error::new(
std::io::ErrorKind::AlreadyExists,
format!("File '{}' already exists, aborting operation", &self.output),
));
}

let uwu_progress_bar = progress_bar!();
self.uwuify_sentence(
unsafe {
std::str::from_utf8_unchecked(
sgoudham marked this conversation as resolved.
Show resolved Hide resolved
memmap::Mmap::map(&File::open(&self.input)?)?.as_ref(),
sgoudham marked this conversation as resolved.
Show resolved Hide resolved
)
},
&mut BufWriter::new(File::create(&self.output)?),
)?;
uwu_progress_bar.finish_with_message("UwU'ifying Complete!");
Ok(())
}
}

pub fn uwuify_sentence<T: Write>(
&mut self,
text: &str,
out: &mut T,
) -> Result<(), std::io::Error> {
text.lines().try_for_each(|line| {
line.split_whitespace()
.map(|f| f.as_bytes())
.try_for_each(|word| {
let random_value = self.floating_rng.gen_range(0.0..1.0);

if random_value <= self.faces {
out.write_all(FACES[self.int_rng.gen_range(0..FACES_SIZE)])?;
out.write_all(b" ")?;
} else if random_value <= self.actions {
out.write_all(ACTIONS[self.int_rng.gen_range(0..ACTIONS_SIZE)])?;
out.write_all(b" ")?;
} else if random_value <= self.stutters {
(0..self.int_rng.gen_range(1..2))
.into_iter()
.try_for_each(|_| {
match word[0] {
b'L' | b'R' => out.write_all(b"W"),
b'l' | b'r' => out.write_all(b"w"),
b => out.write_all(&[b]),
}?;
out.write_all(b"-")
})?;
}

if self
.linkify
.links(unsafe { std::str::from_utf8_unchecked(word) })
.count()
> 0
sgoudham marked this conversation as resolved.
Show resolved Hide resolved
|| random_value > self.words
{
out.write_all(word)?;
} else {
(0..word.len()).try_for_each(|index| match word[index] {
b'L' | b'R' => out.write_all(b"W"),
b'l' | b'r' => out.write_all(b"w"),
b'E' | b'e' | b'A' | b'I' | b'O' | b'U' | b'a' | b'i' | b'o' | b'u' => {
match word.get(index - 1).unwrap_or(&word[0]) {
b'N' | b'n' => out.write_all(&[b'y', word[index]]),
_ => out.write_all(&[word[index]]),
}
}
b => out.write_all(&[b]),
})?;
}
out.write_all(b" ")
})?;
out.write_all(b"\n")
})
}
}

#[cfg(test)]
sgoudham marked this conversation as resolved.
Show resolved Hide resolved
mod tests {
extern crate test;
use linkify::{LinkFinder, LinkKind};

#[bench]
fn uwu_bench(b: &mut test::Bencher) {
let mut linkify = LinkFinder::new();
linkify.kinds(&[LinkKind::Email, LinkKind::Url]);
linkify.url_must_have_scheme(false);
let mut uwuify = super::UwUify {
text: include_str!("test.txt"),
linkify,
..Default::default()
};

b.iter(|| uwuify.uwuify());
}
}
Loading