Algod/ciphers/
rot13.rs

1pub fn rot13(text: &str) -> String {
2    let to_enc = text.to_uppercase();
3    to_enc
4        .chars()
5        .map(|c| match c {
6            'A'..='M' => ((c as u8) + 13) as char,
7            'N'..='Z' => ((c as u8) - 13) as char,
8            _ => c,
9        })
10        .collect()
11}
12
13#[cfg(test)]
14mod test {
15    use super::*;
16
17    #[test]
18    fn test_single_letter() {
19        assert_eq!("N", rot13("A"));
20    }
21
22    #[test]
23    fn test_bunch_of_letters() {
24        assert_eq!("NOP", rot13("ABC"));
25    }
26
27    #[test]
28    fn test_non_ascii() {
29        assert_eq!("😀NO", rot13("😀AB"));
30    }
31
32    #[test]
33    fn test_twice() {
34        assert_eq!("ABCD", rot13(&rot13("ABCD")));
35    }
36}