-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
theoretical_rot13.rs
43 lines (39 loc) · 1012 Bytes
/
theoretical_rot13.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
// in theory rot-13 only affects the lowercase characters in a cipher
pub fn theoretical_rot13(text: &str) -> String {
let mut pos: u8 = 0;
let mut npos: u8 = 0;
text.to_owned()
.chars()
.map(|mut c| {
if c.is_ascii_lowercase() {
// ((c as u8) + 13) as char
pos = c as u8 - b'a';
npos = (pos + 13) % 26;
c = (npos + b'a') as char;
c
} else {
c
}
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_single_letter() {
assert_eq!("n", theoretical_rot13("a"));
}
#[test]
fn test_bunch_of_letters() {
assert_eq!("nop op", theoretical_rot13("abc bc"));
}
#[test]
fn test_non_ascii() {
assert_eq!("😀ab", theoretical_rot13("😀no"));
}
#[test]
fn test_twice() {
assert_eq!("abcd", theoretical_rot13(&theoretical_rot13("abcd")));
}
}