diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 4e7cba6..af854d7 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -30,15 +30,11 @@ - - + - - - - - - - - + + + + + @@ -172,7 +168,8 @@ - + + 1595770727572 @@ -272,7 +269,14 @@ - @@ -302,13 +306,14 @@ - - + - + @@ -317,25 +322,25 @@ - + - + - + - + - + - + - + - + @@ -353,10 +358,14 @@ - + + + + + - + diff --git a/Cargo.lock b/Cargo.lock index 23383b8..a1fe120 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,13 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "aho-corasick" +version = "0.7.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 2.3.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "anyhow" version = "1.0.32" @@ -975,6 +983,22 @@ dependencies = [ "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "regex" +version = "1.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aho-corasick 0.7.13 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.6.18 (registry+https://github.com/rust-lang/crates.io-index)", + "thread_local 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex-syntax" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "rental" version = "0.5.5" @@ -1000,6 +1024,7 @@ version = "0.0.1" dependencies = [ "druid 0.6.0 (git+https://github.com/linebender/druid.git)", "rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 1.3.9 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1194,6 +1219,14 @@ dependencies = [ "syn 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "thread_local" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "time" version = "0.2.21" @@ -1432,6 +1465,7 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] +"checksum aho-corasick 0.7.13 (registry+https://github.com/rust-lang/crates.io-index)" = "043164d8ba5c4c3035fec9bbee8647c0261d788f3474306f93bb65901cae0e86" "checksum anyhow 1.0.32 (registry+https://github.com/rust-lang/crates.io-index)" = "6b602bfe940d21c130f3895acd65221e8a61270debe89d628b9cb4e3ccb8569b" "checksum arrayvec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8" "checksum atk 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "812b4911e210bd51b24596244523c856ca749e6223c50a7fbbba3f89ee37c426" @@ -1532,6 +1566,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum rand_chacha 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" "checksum rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" "checksum rand_hc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +"checksum regex 1.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "9c3780fcf44b193bc4d09f36d2a3c87b251da4a046c87795a0d35f4f927ad8e6" +"checksum regex-syntax 0.6.18 (registry+https://github.com/rust-lang/crates.io-index)" = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8" "checksum rental 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "8545debe98b2b139fb04cad8618b530e9b07c152d99a5de83c860b877d67847f" "checksum rental-impl 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "475e68978dc5b743f2f40d8e0a8fdc83f1c5e78cbf4b8fa5e74e73beebc340de" "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" @@ -1557,6 +1593,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum system-deps 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0f3ecc17269a19353b3558b313bba738b25d82993e30d62a18406a24aba4649b" "checksum thiserror 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)" = "7dfdd070ccd8ccb78f4ad66bf1982dc37f620ef696c6b5028fe2ed83dd3d0d08" "checksum thiserror-impl 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)" = "bd80fc12f73063ac132ac92aceea36734f04a1d93c1240c6944e23a3b8841793" +"checksum thread_local 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14" "checksum time 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)" = "2c2e31fb28e2a9f01f5ed6901b066c1ba2333c04b64dc61254142bafcb3feb2c" "checksum time-macros 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9ae9b6e9f095bc105e183e3cd493d72579be3181ad4004fceb01adbe9eecab2d" "checksum time-macros-impl 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e5c3be1edfad6027c69f5491cf4cb310d1a71ecd6af742788c6ff8bced86b8fa" diff --git a/Cargo.toml b/Cargo.toml index 4c9eea8..c7b8ddc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ edition = "2018" [dependencies] rand = "0.7.3" druid = { git = "https://github.com/linebender/druid.git" } +regex = "1.3.9" [profile.release] opt-level = 3 \ No newline at end of file diff --git a/src/front_end/state/logic/mod.rs b/src/front_end/state/logic/mod.rs index b8c9f36..ae63e14 100644 --- a/src/front_end/state/logic/mod.rs +++ b/src/front_end/state/logic/mod.rs @@ -1,11 +1,10 @@ use std::borrow::Borrow; use rand::Rng; +use regex::Regex; mod dictionary; -//TODO: fix bug that auto decryption works fine without punctuation but doesn't with (for example) full stops at the end of words. - static ALPHABET_LOWER: [char; 26] = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']; static ALPHABET_UPPER: [char; 26] = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', @@ -89,8 +88,26 @@ fn try_decrypt(shift_value: i8, ciphertext: &String, dictionary_words: &[&'stati let words: Vec = possible_plaintext.split(" ").map(|s: &str| String::from(s)).collect(); let mut words_in_dict: i8 = 0; - for word in &words { - if dictionary_words.contains(&&**&word.to_lowercase()) { words_in_dict += 1; } + for mixed_case_word in &words { + let word: String = String::from(mixed_case_word.to_lowercase()); + if dictionary_words.contains(&&**&word) { + words_in_dict += 1; + continue; + } else { + if word.contains("'") { + let tokens: Vec<&str> = word.split("'").collect(); + if dictionary_words.contains(&&*tokens[0]) { + words_in_dict += 1; + continue; + } + } + + let re = Regex::new(r"[^a-z^A-Z]").unwrap(); + let result = String::from(re.replace_all(&*word, "")); + if dictionary_words.contains(&&*result.to_lowercase()) { + words_in_dict += 1; + } + } } let is_passable_text: bool = words_in_dict as f32 / words.len() as f32 >= PASSABLE_PROPORTION_WORDS_IN_DICT;