Skip to content

Commit 8ebcc27

Browse files
authored
fix: Make xor output Vec<u8> (#354)
1 parent 14ede43 commit 8ebcc27

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

src/ciphers/xor.rs

+32-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
pub fn xor(text: &str, key: u8) -> String {
2-
text.chars().map(|c| ((c as u8) ^ key) as char).collect()
1+
pub fn xor_bytes(text: &[u8], key: u8) -> Vec<u8> {
2+
text.iter().map(|c| c ^ key).collect()
3+
}
4+
5+
pub fn xor(text: &str, key: u8) -> Vec<u8> {
6+
xor_bytes(text.as_bytes(), key)
37
}
48

59
#[cfg(test)]
@@ -10,13 +14,37 @@ mod tests {
1014
fn test_simple() {
1115
let test_string = "test string";
1216
let ciphered_text = xor(test_string, 32);
13-
assert_eq!(test_string, xor(&ciphered_text, 32));
17+
assert_eq!(test_string.as_bytes(), xor_bytes(&ciphered_text, 32));
1418
}
1519

1620
#[test]
1721
fn test_every_alphabet_with_space() {
1822
let test_string = "The quick brown fox jumps over the lazy dog";
1923
let ciphered_text = xor(test_string, 64);
20-
assert_eq!(test_string, xor(&ciphered_text, 64));
24+
assert_eq!(test_string.as_bytes(), xor_bytes(&ciphered_text, 64));
25+
}
26+
27+
#[test]
28+
fn test_multi_byte() {
29+
let test_string = "日本語";
30+
let key = 42;
31+
let ciphered_text = xor(test_string, key);
32+
assert_eq!(test_string.as_bytes(), xor_bytes(&ciphered_text, key));
33+
}
34+
35+
#[test]
36+
fn test_zero_byte() {
37+
let test_string = "The quick brown fox jumps over the lazy dog";
38+
let key = ' ' as u8;
39+
let ciphered_text = xor(test_string, key);
40+
assert_eq!(test_string.as_bytes(), xor_bytes(&ciphered_text, key));
41+
}
42+
43+
#[test]
44+
fn test_invalid_byte() {
45+
let test_string = "The quick brown fox jumps over the lazy dog";
46+
let key = !0 as u8;
47+
let ciphered_text = xor(test_string, key);
48+
assert_eq!(test_string.as_bytes(), xor_bytes(&ciphered_text, key));
2149
}
2250
}

src/graph/depth_first_search_tic_tac_toe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct Position {
3636
y: u8,
3737
}
3838

39-
#[derive(Copy, Clone, PartialEq, Debug)]
39+
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
4040
pub enum Players {
4141
Blank,
4242
PlayerX,

src/searching/interpolation_search.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn interpolation_search<Ordering>(nums: &[i32], item: &i32) -> Result<usize,
1111
}
1212
let offset: usize = low
1313
+ (((high - low) / (nums[high] - nums[low]) as usize) * (*item - nums[low]) as usize);
14-
match nums[offset].cmp(&*item) {
14+
match nums[offset].cmp(item) {
1515
std::cmp::Ordering::Equal => return Ok(offset),
1616
std::cmp::Ordering::Less => low = offset + 1,
1717
std::cmp::Ordering::Greater => high = offset - 1,

0 commit comments

Comments
 (0)