More restructuring

main
John Zacarias Jekel 1 year ago
parent dc6bb6a0d0
commit c046d15723
  1. 6
      src/anslatortray.rs
  2. 27
      src/ascii.rs
  3. 4
      src/lib.rs
  4. 35
      src/utf8.rs

@ -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());
//eprintln!("\n{}", String::from_utf8(ascii::translate(b"Have a good day!")).unwrap());//TODO add this back
}
fn interactive(args: &Vec<String>) {
@ -155,9 +155,11 @@ fn benchmark_file(args: &Vec<String>) {
let mut total_duration_ascii = std::time::Duration::new(0, 0);
let mut translated_file_contents = Vec::<u8>::new();//TODO set a sane initial size
for _ in 0..iterations {
let start_time = std::time::Instant::now();
let translated_file_contents = ascii::translate(file_contents.as_bytes());
translated_file_contents.truncate(0);
ascii::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

@ -31,24 +31,25 @@ use crate::helpers::{is_vowel_ascii, is_y_ascii, word_is_uppercase_ascii, push_s
/* Functions */
pub fn translate(english: &[u8]) -> Vec::<u8> {
return translate_way(english);
//TODO rename the "ascii" functions to "byte" functions since they work on UTF8 bytestrings too
//TODO be sure to mention that if the strings are not ascii, the non-ascii bytes won't be affected
pub fn translate(english: &[u8], pig_latin_string: &mut Vec::<u8>) {
translate_way(english, pig_latin_string);
}
pub fn translate_way(english: &[u8]) -> Vec::<u8> {
return translate_with_style(english, b"ay", b"way");
//TODO be sure to mention that if the strings are not ascii, the non-ascii bytes won't be affected
pub fn translate_way(english: &[u8], pig_latin_string: &mut Vec::<u8>) {
translate_with_style(english, b"ay", b"way", pig_latin_string);
}
//TODO tests for this function
pub fn translate_with_style(english: &[u8], suffix_lower: &[u8], special_case_suffix_lower: &[u8]) -> Vec::<u8> {
//TODO be sure to mention that if the strings are not ascii, the non-ascii bytes won't be affected
pub fn translate_with_style(english: &[u8], suffix_lower: &[u8], special_case_suffix_lower: &[u8], pig_latin_string: &mut Vec::<u8>) {
if english.is_empty() {
return Vec::<u8>::new();
return;
}
//TODO switch to fully operating on u8 slices/arrays/Vecs internally (converting from a string, then to a string at the end) in anslatortray 0.5.0
let mut pig_latin_string = Vec::<u8>::with_capacity(english.len() * 2);//Plenty of headroom in case the words are very small or the suffixes are long
//Convert the suffix and special_case_suffix we were provided to uppercase for words that are capitalized
let mut suffix_upper = Vec::<u8>::with_capacity(suffix_lower.len());
for letter in suffix_lower.iter() {
@ -95,7 +96,7 @@ pub fn translate_with_style(english: &[u8], suffix_lower: &[u8], special_case_su
translate_word_with_style_reuse_buffers (
word_slice,
suffix_lower, special_case_suffix_lower, &suffix_upper, &special_case_suffix_upper,
&mut pig_latin_string, &mut starting_consonants_buffer
pig_latin_string, &mut starting_consonants_buffer
);
//Bring the slice_start_index to the end since we've finished the word and need it ready for the next one
@ -130,11 +131,9 @@ pub fn translate_with_style(english: &[u8], suffix_lower: &[u8], special_case_su
translate_word_with_style_reuse_buffers (
word_slice,
suffix_lower, special_case_suffix_lower, &suffix_upper, &special_case_suffix_upper,
&mut pig_latin_string, &mut starting_consonants_buffer
pig_latin_string, &mut starting_consonants_buffer
);
}
return pig_latin_string;
}
/*

@ -82,7 +82,7 @@ mod helpers;
mod translate_strings;
mod translate_words;
pub mod ascii;
mod utf8;
pub mod utf8;
pub use translate_strings::{translate, translate_way, translate_yay, translate_hay, translate_ferb, translate_with_style};
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};

@ -7,7 +7,7 @@
/* Imports */
//TODO (include "use" and "mod" here)
use crate::ascii::translate_with_style as translate_with_style_bytes;
/* Constants */
@ -31,4 +31,35 @@
/* Functions */
//TODO
pub fn translate(english: &str) -> String {
return translate_way(english);
}
pub fn translate_way(english: &str) -> String {
return translate_with_style(english, "ay", "way");
}
pub fn translate_yay(english: &str) -> String {
return translate_with_style(english, "ay", "yay");
}
pub fn translate_hay(english: &str) -> String {
return translate_with_style(english, "ay", "hay");
}
pub fn translate_ferb(english: &str) -> String {
return translate_with_style(english, "erb", "ferb");
}
pub fn translate_with_style(english: &str, suffix_lower: &str, special_case_suffix_lower: &str) -> String {
if english.is_empty() {
return String::new();
}
//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);
//This is safe since translate_with_style_bytes does not touch any unicode bytes (it just copies them)
return unsafe { String::from_utf8_unchecked(pig_latin_string_bytes) };
}

Loading…
Cancel
Save