From 8bf8bd917b5dad5fdcec12a4fa6ade14b0e821b5 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Fri, 22 Apr 2022 12:46:02 -0400 Subject: [PATCH] fixes --- Cargo.toml | 12 ++++++ parsing/build.rs | 1 - parsing/src/suggestions.rs | 86 ++++++++++++++++++++++---------------- push.sh | 2 +- 4 files changed, 64 insertions(+), 37 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dceeb38..11c93ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,18 @@ crate-type = ["cdylib"] [features] threading = ["async-lock", "rayon"] +[profile.release] +debug = false +codegen-units = 1 +opt-level = "z" #optimize for size +lto = true +strip = true + +[profile.dev] +debug = true +opt-level = 0 +lto = true +strip = false [dependencies] parsing = { path = "./parsing" } diff --git a/parsing/build.rs b/parsing/build.rs index f8ea68e..aee249c 100644 --- a/parsing/build.rs +++ b/parsing/build.rs @@ -9,7 +9,6 @@ const SUPPORTED_FUNCTIONS: [&str; 22] = [ ]; fn main() { - println!("cargo:rerun-if-changed=.git/logs/HEAD"); println!("cargo:rerun-if-changed=src/*"); generate_hashmap(); diff --git a/parsing/src/suggestions.rs b/parsing/src/suggestions.rs index 4c2f956..1eb51dc 100644 --- a/parsing/src/suggestions.rs +++ b/parsing/src/suggestions.rs @@ -4,6 +4,7 @@ pub const HINT_EMPTY: Hint = Hint::Single("x^2"); const HINT_CLOSED_PARENS: Hint = Hint::Single(")"); /// Only enacts println if cfg(test) is enabled +#[allow(unused_macros)] macro_rules! test_print { ($($arg:tt)*) => { #[cfg(test)] @@ -11,6 +12,36 @@ macro_rules! test_print { }; } +pub fn split_function(input: &str) -> Vec { + split_function_chars(&input.chars().collect::>()) +} + +fn split_function_chars(chars: &[char]) -> Vec { + assert!(!chars.is_empty()); + + let mut split: Vec = Vec::new(); + + let mut buffer: Vec = Vec::new(); + for c in chars { + buffer.push(*c); + if *c == ')' { + split.push(buffer.iter().collect::()); + buffer.clear(); + continue; + } + + let buffer_string = buffer.iter().collect::(); + + if ((&buffer_string == "log") | (&buffer_string == "log1")) && is_number(&c) { + continue; + } + } + + if !buffer.is_empty() { + split.push(buffer.iter().collect::()); + } + split +} /// Generate a hint based on the input `input`, returns an `Option` pub fn generate_hint<'a>(input: &str) -> &'a Hint<'a> { @@ -32,38 +63,9 @@ pub fn generate_hint<'a>(input: &str) -> &'a Hint<'a> { return &HINT_CLOSED_PARENS; } - // let len = chars.len(); - - - let mut split: Vec = Vec::new(); - - let mut buffer: Vec = Vec::new(); - for c in chars { - buffer.push(c); - if c == ')' { - split.push(buffer.iter().collect::()); - buffer.clear(); - continue; - } - - let buffer_string = buffer.iter().collect::(); - - if ((&buffer_string == "log") | (&buffer_string == "log1")) && is_number(&c) { - continue; - } - } - - if !buffer.is_empty() { - split.push(buffer.iter().collect::()); - } - - test_print!("split: {:?}", split); - - if split.is_empty() { - return COMPLETION_HASHMAP.get(input).unwrap_or(&Hint::None); - } - - COMPLETION_HASHMAP.get(& unsafe {split.last().unwrap_unchecked()}.as_str()).unwrap_or(&Hint::None) + COMPLETION_HASHMAP + .get(&unsafe { split_function_chars(&chars).last().unwrap_unchecked() }.as_str()) + .unwrap_or(&Hint::None) } #[derive(PartialEq)] @@ -113,7 +115,7 @@ mod tests { /// Tests to make sure hints are properly outputed based on input #[test] - fn hint_test() { + fn hints() { let values = HashMap::from([ ("", Hint::Single("x^2")), ("si", Hint::Many(&["n(", "nh(", "gnum("])), @@ -132,7 +134,7 @@ mod tests { } #[test] - fn hint_to_string_test() { + fn hint_to_string() { let values = HashMap::from([ ("x^2", Hint::Single("x^2")), ( @@ -149,7 +151,7 @@ mod tests { } #[test] - fn invalid_function_test() { + fn invalid_function() { SUPPORTED_FUNCTIONS .iter() .map(|func1| { @@ -169,4 +171,18 @@ mod tests { } }); } + + #[test] + fn split_function() { + let values = HashMap::from([ + ("cos(x)", vec!["cos(x)"]), + ("cos(", vec!["cos("]), + ("cos(x)sin(x)", vec!["cos(x)", "sin(x)"]), + ("aaaaaaaaaaa", vec!["aaaaaaaaaaa"]), + ]); + + for (key, value) in values { + assert_eq!(super::split_function(key), value); + } + } } diff --git a/push.sh b/push.sh index fd607ff..8906801 100755 --- a/push.sh +++ b/push.sh @@ -3,7 +3,7 @@ set -e #kill script if error occurs cargo test-all-features -bash build.sh +bash build.sh release echo "rsyncing" #rsync -av --delete --info=progress2 tmp/ rpi-public:/mnt/hdd/http_share/integral-demo/