Even more cleanup

main
John Zacarias Jekel 1 year ago
parent c046d15723
commit 197fd51228
  1. 20
      src/anslatortray.rs
  2. 2
      src/byte_string.rs
  3. 12
      src/lib.rs
  4. 6
      src/string.rs

@ -8,8 +8,8 @@
/* Imports */
use anslatortray::translate;
use anslatortray::ascii;
use anslatortray::string;
use anslatortray::byte_string;
/* Constants */
@ -71,7 +71,7 @@ fn help() {
eprintln!("--translate-args Translates all remaining arguments provided and outputs them to stdout");
eprintln!("--stdin-to-stdout Translates input from stdin directly to stdout");
//eprintln!("\n{}", String::from_utf8(ascii::translate(b"Have a good day!")).unwrap());//TODO add this back
eprintln!("\n{}", string::translate("Have a good day!"));
}
fn interactive(args: &Vec<String>) {
@ -89,7 +89,7 @@ fn interactive(args: &Vec<String>) {
loop {
eprint!("anslatortray> ");
stdin.read_line(&mut line_buffer).unwrap();
eprintln!("{}", translate(&line_buffer));
eprintln!("{}", string::translate(&line_buffer));
line_buffer.truncate(0);
}
}
@ -104,13 +104,14 @@ fn file(args: &Vec<String>) {
}
//TODO error handling
//TODO switch to using byte_string for efficiency
let input_file = &args[0];
let output_file = &args[1];
let file_contents = std::fs::read_to_string(input_file).unwrap();
let start_time = std::time::Instant::now();
let translated_file_contents = translate(&file_contents);
let translated_file_contents = string::translate(&file_contents);
//let translated_file_contents = ascii::translate(file_contents.as_bytes());//TESTING
let time_to_translate = start_time.elapsed();
std::fs::write(output_file, &translated_file_contents).unwrap();
@ -128,6 +129,7 @@ fn benchmark_file(args: &Vec<String>) {
}
//TODO error handling
//TODO just benchmark byte_string from now on
let input_file = &args[0];
let iterations = args[1].parse::<u128>().unwrap();//TODO error handling
@ -138,7 +140,7 @@ fn benchmark_file(args: &Vec<String>) {
for _ in 0..iterations {
let start_time = std::time::Instant::now();
let translated_file_contents = translate(&file_contents);
let translated_file_contents = string::translate(&file_contents);
let time_to_translate = start_time.elapsed();
total_duration_utf8 += time_to_translate;
@ -159,7 +161,7 @@ fn benchmark_file(args: &Vec<String>) {
for _ in 0..iterations {
let start_time = std::time::Instant::now();
translated_file_contents.truncate(0);
ascii::translate(file_contents.as_bytes(), &mut translated_file_contents);
byte_string::translate(file_contents.as_bytes(), &mut translated_file_contents);
let time_to_translate = start_time.elapsed();
total_duration_ascii += time_to_translate;
std::fs::write("/dev/null", &translated_file_contents).unwrap();//TODO avoid needing unix
@ -176,7 +178,7 @@ fn translate_args(args: &Vec<String>) {
//Translate the arguments and print them out for the user
for string in args {
print!("{} ", translate(&string));
print!("{} ", string::translate(&string));
}
println!();
}
@ -196,7 +198,7 @@ fn stdin_to_stdout(args: &Vec<String>) {
while let Ok(bytes_read) = stdin.read_to_string(&mut buffer) {
if bytes_read == 0 { return; }
write!(stdout, "{}", translate(&buffer)).unwrap();//TODO do this more efficiently (avoid format string)
write!(stdout, "{}", string::translate(&buffer)).unwrap();//TODO do this more efficiently (avoid format string)
buffer.truncate(0);//TODO is this needed here?
}
}

@ -148,7 +148,7 @@ pub(super) fn translate_word_with_style_reuse_buffers_better_perhaps <
}
*/
pub(super) fn translate_word_with_style_reuse_buffers (
fn translate_word_with_style_reuse_buffers (
english_word: &[u8],//Assumes this word is not empty
suffix_lower: &[u8], special_case_suffix_lower: &[u8], suffix_upper: &[u8], special_case_suffix_upper: &[u8],
buffer_to_append_to: &mut Vec<u8>, starting_consonants: &mut Vec<u8>

@ -79,10 +79,10 @@
/* Imports */
mod helpers;
mod translate_strings;
mod translate_words;
pub mod ascii;
pub mod utf8;
//mod translate_strings;
//mod translate_words;
pub mod byte_string;
pub mod string;
pub use utf8::{translate, translate_way, translate_yay, translate_hay, translate_ferb, translate_with_style};
pub use translate_strings::{translate_ascii, translate_way_ascii, translate_yay_ascii, translate_hay_ascii, translate_ferb_ascii, translate_with_style_ascii};
pub use string::translate;//For convinence; for other functions use either the string or byte_string modules
//pub use translate_strings::{translate_ascii, translate_way_ascii, translate_yay_ascii, translate_hay_ascii, translate_ferb_ascii, translate_with_style_ascii};

@ -7,7 +7,7 @@
/* Imports */
use crate::ascii::translate_with_style as translate_with_style_bytes;
use crate::byte_string::translate_with_style as translate_byte_string_with_style;
/* Constants */
@ -58,8 +58,8 @@ pub fn translate_with_style(english: &str, suffix_lower: &str, special_case_suff
//Convert the string slices to byte slices and translate those (only ASCII letters are affected, non-letters or UTF-8 are preserved)
let mut pig_latin_string_bytes = Vec::<u8>::with_capacity(english.len() * 2);//Plenty of headroom in case the words are very small or the suffixes are long
translate_with_style_bytes(english.as_bytes(), suffix_lower.as_bytes(), special_case_suffix_lower.as_bytes(), &mut pig_latin_string_bytes);
translate_byte_string_with_style(english.as_bytes(), suffix_lower.as_bytes(), special_case_suffix_lower.as_bytes(), &mut pig_latin_string_bytes);
//This is safe since translate_with_style_bytes does not touch any unicode bytes (it just copies them)
//This is safe since translate_byte_string_with_style does not touch any unicode bytes (it just copies them)
return unsafe { String::from_utf8_unchecked(pig_latin_string_bytes) };
}
Loading…
Cancel
Save