diff --git a/src/ciphers/xor.rs b/src/ciphers/xor.rs index 0f4233af48b..fe97f315957 100644 --- a/src/ciphers/xor.rs +++ b/src/ciphers/xor.rs @@ -1,5 +1,9 @@ -pub fn xor(text: &str, key: u8) -> String { - text.chars().map(|c| ((c as u8) ^ key) as char).collect() +pub fn xor_bytes(text: &[u8], key: u8) -> Vec { + text.iter().map(|c| c ^ key).collect() +} + +pub fn xor(text: &str, key: u8) -> Vec { + xor_bytes(text.as_bytes(), key) } #[cfg(test)] @@ -10,13 +14,37 @@ mod tests { fn test_simple() { let test_string = "test string"; let ciphered_text = xor(test_string, 32); - assert_eq!(test_string, xor(&ciphered_text, 32)); + assert_eq!(test_string.as_bytes(), xor_bytes(&ciphered_text, 32)); } #[test] fn test_every_alphabet_with_space() { let test_string = "The quick brown fox jumps over the lazy dog"; let ciphered_text = xor(test_string, 64); - assert_eq!(test_string, xor(&ciphered_text, 64)); + assert_eq!(test_string.as_bytes(), xor_bytes(&ciphered_text, 64)); + } + + #[test] + fn test_multi_byte() { + let test_string = "日本語"; + let key = 42; + let ciphered_text = xor(test_string, key); + assert_eq!(test_string.as_bytes(), xor_bytes(&ciphered_text, key)); + } + + #[test] + fn test_zero_byte() { + let test_string = "The quick brown fox jumps over the lazy dog"; + let key = ' ' as u8; + let ciphered_text = xor(test_string, key); + assert_eq!(test_string.as_bytes(), xor_bytes(&ciphered_text, key)); + } + + #[test] + fn test_invalid_byte() { + let test_string = "The quick brown fox jumps over the lazy dog"; + let key = !0 as u8; + let ciphered_text = xor(test_string, key); + assert_eq!(test_string.as_bytes(), xor_bytes(&ciphered_text, key)); } } diff --git a/src/graph/depth_first_search_tic_tac_toe.rs b/src/graph/depth_first_search_tic_tac_toe.rs index d4a464be2ad..a5e942c35b3 100644 --- a/src/graph/depth_first_search_tic_tac_toe.rs +++ b/src/graph/depth_first_search_tic_tac_toe.rs @@ -36,7 +36,7 @@ struct Position { y: u8, } -#[derive(Copy, Clone, PartialEq, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, Debug)] pub enum Players { Blank, PlayerX, diff --git a/src/searching/interpolation_search.rs b/src/searching/interpolation_search.rs index 5bb1840b966..4ecb3229892 100644 --- a/src/searching/interpolation_search.rs +++ b/src/searching/interpolation_search.rs @@ -11,7 +11,7 @@ pub fn interpolation_search(nums: &[i32], item: &i32) -> Result return Ok(offset), std::cmp::Ordering::Less => low = offset + 1, std::cmp::Ordering::Greater => high = offset - 1,