-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathcaesar.rs
118 lines (106 loc) · 4.11 KB
/
caesar.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const ERROR_MESSAGE: &str = "Rotation must be in the range [0, 25]";
const ALPHABET_LENGTH: u8 = b'z' - b'a' + 1;
/// Encrypts a given text using the Caesar cipher technique.
///
/// In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code,
/// or Caesar shift, is one of the simplest and most widely known encryption techniques.
/// It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter
/// some fixed number of positions down the alphabet.
///
/// # Arguments
///
/// * `text` - The text to be encrypted.
/// * `rotation` - The number of rotations (shift) to be applied. It should be within the range [0, 25].
///
/// # Returns
///
/// Returns a `Result` containing the encrypted string if successful, or an error message if the rotation
/// is out of the valid range.
///
/// # Errors
///
/// Returns an error if the rotation value is out of the valid range [0, 25]
pub fn caesar(text: &str, rotation: isize) -> Result<String, &'static str> {
if !(0..ALPHABET_LENGTH as isize).contains(&rotation) {
return Err(ERROR_MESSAGE);
}
let result = text
.chars()
.map(|c| {
if c.is_ascii_alphabetic() {
shift_char(c, rotation)
} else {
c
}
})
.collect();
Ok(result)
}
/// Shifts a single ASCII alphabetic character by a specified number of positions in the alphabet.
///
/// # Arguments
///
/// * `c` - The ASCII alphabetic character to be shifted.
/// * `rotation` - The number of positions to shift the character. Should be within the range [0, 25].
///
/// # Returns
///
/// Returns the shifted ASCII alphabetic character.
fn shift_char(c: char, rotation: isize) -> char {
let first = if c.is_ascii_lowercase() { b'a' } else { b'A' };
let rotation = rotation as u8; // Safe cast as rotation is within [0, 25]
(((c as u8 - first) + rotation) % ALPHABET_LENGTH + first) as char
}
#[cfg(test)]
mod tests {
use super::*;
macro_rules! test_caesar_happy_path {
($($name:ident: $test_case:expr,)*) => {
$(
#[test]
fn $name() {
let (text, rotation, expected) = $test_case;
assert_eq!(caesar(&text, rotation).unwrap(), expected);
let backward_rotation = if rotation == 0 { 0 } else { ALPHABET_LENGTH as isize - rotation };
assert_eq!(caesar(&expected, backward_rotation).unwrap(), text);
}
)*
};
}
macro_rules! test_caesar_error_cases {
($($name:ident: $test_case:expr,)*) => {
$(
#[test]
fn $name() {
let (text, rotation) = $test_case;
assert_eq!(caesar(&text, rotation), Err(ERROR_MESSAGE));
}
)*
};
}
#[test]
fn alphabet_length_should_be_26() {
assert_eq!(ALPHABET_LENGTH, 26);
}
test_caesar_happy_path! {
empty_text: ("", 13, ""),
rot_13: ("rust", 13, "ehfg"),
unicode: ("attack at dawn 攻", 5, "fyyfhp fy ifbs 攻"),
rotation_within_alphabet_range: ("Hello, World!", 3, "Khoor, Zruog!"),
no_rotation: ("Hello, World!", 0, "Hello, World!"),
rotation_at_alphabet_end: ("Hello, World!", 25, "Gdkkn, Vnqkc!"),
longer: ("The quick brown fox jumps over the lazy dog.", 5, "Ymj vznhp gwtbs ktc ozrux tajw ymj qfed itl."),
non_alphabetic_characters: ("12345!@#$%", 3, "12345!@#$%"),
uppercase_letters: ("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1, "BCDEFGHIJKLMNOPQRSTUVWXYZA"),
mixed_case: ("HeLlO WoRlD", 7, "OlSsV DvYsK"),
with_whitespace: ("Hello, World!", 13, "Uryyb, Jbeyq!"),
with_special_characters: ("Hello!@#$%^&*()_+World", 4, "Lipps!@#$%^&*()_+Asvph"),
with_numbers: ("Abcd1234XYZ", 10, "Klmn1234HIJ"),
}
test_caesar_error_cases! {
negative_rotation: ("Hello, World!", -5),
empty_input_negative_rotation: ("", -1),
empty_input_large_rotation: ("", 27),
large_rotation: ("Large rotation", 139),
}
}