Skip to content

fix: Make xor output Vec<u8> #354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions src/ciphers/xor.rs
Original file line number Diff line number Diff line change
@@ -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<u8> {
text.iter().map(|c| c ^ key).collect()
}

pub fn xor(text: &str, key: u8) -> Vec<u8> {
xor_bytes(text.as_bytes(), key)
}

#[cfg(test)]
Expand All @@ -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));
}
}
2 changes: 1 addition & 1 deletion src/graph/depth_first_search_tic_tac_toe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct Position {
y: u8,
}

#[derive(Copy, Clone, PartialEq, Debug)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Players {
Blank,
PlayerX,
Expand Down
2 changes: 1 addition & 1 deletion src/searching/interpolation_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn interpolation_search<Ordering>(nums: &[i32], item: &i32) -> Result<usize,
}
let offset: usize = low
+ (((high - low) / (nums[high] - nums[low]) as usize) * (*item - nums[low]) as usize);
match nums[offset].cmp(&*item) {
match nums[offset].cmp(item) {
std::cmp::Ordering::Equal => return Ok(offset),
std::cmp::Ordering::Less => low = offset + 1,
std::cmp::Ordering::Greater => high = offset - 1,
Expand Down