Add ascii only option

pull/5/head
Isaac Mills 3 years ago
parent 5b8c40ada1
commit b6dcad1878
No known key found for this signature in database
GPG Key ID: B67D7410F33A0F61

@ -1,5 +1,4 @@
pub const FACES_SIZE: usize = 106; pub const FACES_SIZE: usize = 106;
pub const FACES: [&[u8]; FACES_SIZE] = [ pub const FACES: [&[u8]; FACES_SIZE] = [
b"OwO", b"OwO",
b"UwU", b"UwU",
@ -201,6 +200,55 @@ pub const FACES: [&[u8]; FACES_SIZE] = [
kaomoji_ru::special::POINTING, kaomoji_ru::special::POINTING,
]; ];
pub const ASCII_SIZE: usize = ascii_len(&FACES);
pub const ASCII: [&[u8]; ASCII_SIZE] = ascii_array(&FACES);
pub const fn ascii_len(array: &[&[u8]]) -> usize {
let mut result = 0;
let len = array.len();
let mut head = 0;
while head != len {
let mut ascii_head = 0;
let ascii_len = array[head].len();
let mut ascii_res = 0;
while ascii_head != ascii_len {
if array[head][ascii_head].is_ascii() {
ascii_res += 1;
}
ascii_head += 1;
}
if ascii_res == array[head].len() {
result += 1;
}
head += 1;
}
result
}
pub const fn ascii_array(array: &'static [&'static [u8]]) -> [&'static [u8]; ASCII_SIZE] {
let mut ascii_array: [&'static [u8]; ASCII_SIZE] = [&[]; ASCII_SIZE];
let mut result_head = 0;
let len = array.len();
let mut head = 0;
while head != len {
let mut ascii_head = 0;
let ascii_len = array[head].len();
let mut ascii_res = 0;
while ascii_head != ascii_len {
if array[head][ascii_head].is_ascii() {
ascii_res += 1;
}
ascii_head += 1;
}
if ascii_res == array[head].len() {
ascii_array[result_head] = array[head];
result_head += 1;
}
head += 1;
}
ascii_array
}
pub const ACTIONS_SIZE: usize = 17; pub const ACTIONS_SIZE: usize = 17;
pub const ACTIONS: [&[u8]; ACTIONS_SIZE] = [ pub const ACTIONS: [&[u8]; ACTIONS_SIZE] = [
b"*notices bulge* ", b"*notices bulge* ",

@ -5,14 +5,15 @@ use std::io::{BufWriter, Error, ErrorKind, Write};
use std::path::Path; use std::path::Path;
use std::str::from_utf8_unchecked; use std::str::from_utf8_unchecked;
use ahash::RandomState;
use linkify::{LinkFinder, LinkKind}; use linkify::{LinkFinder, LinkKind};
use memmap::Mmap; use memmap::Mmap;
use constants::ACTIONS;
use constants::ACTIONS_SIZE; use constants::ACTIONS_SIZE;
use constants::FACES; use constants::FACES;
use constants::FACES_SIZE; use constants::FACES_SIZE;
use constants::{ACTIONS, ASCII, ASCII_SIZE};
use rand::SeedableRng;
use rand_xoshiro::Xoshiro256Plus;
mod constants; mod constants;
@ -30,14 +31,6 @@ macro_rules! progress_bar {
}}; }};
} }
macro_rules! new_seeder {
($word:expr, $random:expr) => {
<rand_xoshiro::Xoshiro256Plus as rand::SeedableRng>::seed_from_u64(
<[u8] as ahash::CallHasher>::get_hash($word, $random),
)
};
}
macro_rules! random_float { macro_rules! random_float {
($seeder:expr) => { ($seeder:expr) => {
rand::Rng::gen_range($seeder, 0.0..1.0) rand::Rng::gen_range($seeder, 0.0..1.0)
@ -59,7 +52,8 @@ pub struct UwUify<'a> {
faces: f64, faces: f64,
actions: f64, actions: f64,
stutters: f64, stutters: f64,
random: RandomState, random: Xoshiro256Plus,
ascii: bool,
is_runtime: bool, is_runtime: bool,
linkify: LinkFinder, linkify: LinkFinder,
} }
@ -74,8 +68,9 @@ impl<'a> Default for UwUify<'a> {
faces: 0.05, faces: 0.05,
actions: 0.125, actions: 0.125,
stutters: 0.225, stutters: 0.225,
random: RandomState::with_seeds(69, 420, 28, 95), random: Xoshiro256Plus::seed_from_u64(69420),
is_runtime: false, is_runtime: false,
ascii: false,
linkify: LinkFinder::new(), linkify: LinkFinder::new(),
} }
} }
@ -90,6 +85,7 @@ impl<'a> UwUify<'a> {
faces: Option<&'a str>, faces: Option<&'a str>,
actions: Option<&'a str>, actions: Option<&'a str>,
stutters: Option<&'a str>, stutters: Option<&'a str>,
ascii: bool,
random: bool, random: bool,
is_runtime: bool, is_runtime: bool,
) -> UwUify<'a> { ) -> UwUify<'a> {
@ -101,13 +97,14 @@ impl<'a> UwUify<'a> {
text: text.unwrap_or_default(), text: text.unwrap_or_default(),
input: infile.unwrap_or_default(), input: infile.unwrap_or_default(),
output: outfile.unwrap_or_default(), output: outfile.unwrap_or_default(),
ascii,
is_runtime, is_runtime,
linkify, linkify,
..Default::default() ..Default::default()
}; };
if random { if random {
uwuify.random = RandomState::new(); uwuify.random = Xoshiro256Plus::from_entropy();
} }
if let Some(words) = words { if let Some(words) = words {
@ -126,7 +123,7 @@ impl<'a> UwUify<'a> {
uwuify uwuify
} }
pub fn uwuify(&self) -> Result<(), Error> { pub fn uwuify(&mut self) -> Result<(), Error> {
// Handle Text // Handle Text
if !self.text.is_empty() { if !self.text.is_empty() {
// Handle Text Output // Handle Text Output
@ -167,20 +164,19 @@ impl<'a> UwUify<'a> {
Ok(()) Ok(())
} }
pub fn uwuify_sentence<T: Write>(&self, text: &str, out: &mut T) -> Result<(), Error> { pub fn uwuify_sentence<T: Write>(&mut self, text: &str, out: &mut T) -> Result<(), Error> {
text.lines().try_for_each(|line| { text.lines().try_for_each(|line| {
line.split_whitespace() line.split_whitespace()
.map(|word_str| word_str.as_bytes()) .map(|word_str| word_str.as_bytes())
.try_for_each(|word| { .try_for_each(|word| {
let mut seeder = new_seeder!(word, &self.random); let random_value = random_float!(&mut self.random);
let random_value = random_float!(&mut seeder);
if !self.is_runtime { if !self.is_runtime {
if random_value <= self.faces { if random_value <= self.faces {
out.write_all(FACES[random_int!(&mut seeder, 0..FACES_SIZE)])?; out.write_all(FACES[random_int!(&mut self.random, 0..FACES_SIZE)])?;
out.write_all(b" ")?; out.write_all(b" ")?;
} else if random_value <= self.actions { } else if random_value <= self.actions {
out.write_all(ACTIONS[random_int!(&mut seeder, 0..ACTIONS_SIZE)])?; out.write_all(ACTIONS[random_int!(&mut self.random, 0..ACTIONS_SIZE)])?;
} else if random_value <= self.stutters { } else if random_value <= self.stutters {
match word[0] { match word[0] {
b'L' | b'R' => out.write_all(b"W"), b'L' | b'R' => out.write_all(b"W"),
@ -191,11 +187,15 @@ impl<'a> UwUify<'a> {
} }
} else { } else {
if random_value <= self.faces { if random_value <= self.faces {
out.write_all(FACES[random_int!(&mut seeder, 0..FACES_SIZE)])?; if self.ascii {
out.write_all(ASCII[random_int!(&mut self.random, 0..ASCII_SIZE)])?;
} else {
out.write_all(FACES[random_int!(&mut self.random, 0..FACES_SIZE)])?;
}
out.write_all(b" ")?; out.write_all(b" ")?;
} }
if random_value <= self.actions { if random_value <= self.actions {
out.write_all(ACTIONS[random_int!(&mut seeder, 0..ACTIONS_SIZE)])?; out.write_all(ACTIONS[random_int!(&mut self.random, 0..ACTIONS_SIZE)])?;
} }
if random_value <= self.stutters { if random_value <= self.stutters {
match word[0] { match word[0] {
@ -242,7 +242,7 @@ mod tests {
#[cfg(feature = "bench")] #[cfg(feature = "bench")]
#[bench] #[bench]
fn uwu_bench(b: &mut test::Bencher) { fn uwu_bench(b: &mut test::Bencher) {
let uwuify = super::UwUify::new( let mut uwuify = super::UwUify::new(
Some(include_str!("test.txt")), Some(include_str!("test.txt")),
None, None,
None, None,

@ -25,6 +25,12 @@ macro_rules! app {
.required_unless_present_all(["infile", "outfile"]) .required_unless_present_all(["infile", "outfile"])
.display_order(1), .display_order(1),
) )
.arg(
Arg::new("ascii-only")
.help("The output file will not use UTF-8 charecters")
.long("ascii-only")
.display_order(2),
)
.arg( .arg(
Arg::new("infile") Arg::new("infile")
.help("The file to uwu'ify") .help("The file to uwu'ify")
@ -34,7 +40,7 @@ macro_rules! app {
.requires("outfile") .requires("outfile")
.value_name("FILE") .value_name("FILE")
.value_hint(clap::ValueHint::FilePath) .value_hint(clap::ValueHint::FilePath)
.display_order(2), .display_order(3),
) )
.arg( .arg(
Arg::new("outfile") Arg::new("outfile")
@ -43,7 +49,7 @@ macro_rules! app {
.long("outfile") .long("outfile")
.value_name("FILE") .value_name("FILE")
.value_hint(clap::ValueHint::FilePath) .value_hint(clap::ValueHint::FilePath)
.display_order(3), .display_order(4),
) )
.arg( .arg(
Arg::new("words") Arg::new("words")
@ -53,7 +59,7 @@ macro_rules! app {
.value_name("VALUE") .value_name("VALUE")
.default_value("1") .default_value("1")
.validator(is_between_zero_and_one) .validator(is_between_zero_and_one)
.display_order(4), .display_order(5),
) )
.arg( .arg(
Arg::new("faces") Arg::new("faces")
@ -63,7 +69,7 @@ macro_rules! app {
.value_name("VALUE") .value_name("VALUE")
.default_value("0.05") .default_value("0.05")
.validator(is_between_zero_and_one) .validator(is_between_zero_and_one)
.display_order(5), .display_order(6),
) )
.arg( .arg(
Arg::new("actions") Arg::new("actions")
@ -73,7 +79,7 @@ macro_rules! app {
.value_name("VALUE") .value_name("VALUE")
.default_value("0.125") .default_value("0.125")
.validator(is_between_zero_and_one) .validator(is_between_zero_and_one)
.display_order(6), .display_order(7),
) )
.arg( .arg(
Arg::new("stutters") Arg::new("stutters")
@ -83,14 +89,14 @@ macro_rules! app {
.value_name("VALUE") .value_name("VALUE")
.default_value("0.225") .default_value("0.225")
.validator(is_between_zero_and_one) .validator(is_between_zero_and_one)
.display_order(7), .display_order(8),
) )
.arg( .arg(
Arg::new("random") Arg::new("random")
.help("Flag to enable/disable random uwu'ifying") .help("Flag to enable/disable random uwu'ifying")
.short('r') .short('r')
.long("random") .long("random")
.display_order(8), .display_order(9),
) )
}; };
} }
@ -102,8 +108,8 @@ macro_rules! clap_panic {
} }
macro_rules! is_runtime { macro_rules! is_runtime {
($faces:expr, $actions:expr, $stutters:expr) => { ($faces:expr, $actions:expr, $ascii:expr, $stutters:expr) => {
$faces > 0 || $actions > 0 || $stutters > 0 $faces > 0 || $actions > 0 || $ascii > 0 || $stutters > 0
}; };
} }
@ -119,10 +125,12 @@ fn main() {
matches.value_of("faces"), matches.value_of("faces"),
matches.value_of("actions"), matches.value_of("actions"),
matches.value_of("stutters"), matches.value_of("stutters"),
matches.is_present("ascii-only"),
matches.is_present("random"), matches.is_present("random"),
is_runtime!( is_runtime!(
matches.occurrences_of("faces"), matches.occurrences_of("faces"),
matches.occurrences_of("actions"), matches.occurrences_of("actions"),
matches.occurrences_of("ascii-only"),
matches.occurrences_of("stutters") matches.occurrences_of("stutters")
), ),
) )

Loading…
Cancel
Save