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
//! # vs_rot13
//! `vs_rot13` 是一个ROT13转码工具

/// 将给定的字符串进行ROT13转码
///
/// # Examples
///
/// ```
/// use vs_rot13::rot13;
///
/// assert_eq!("uryyb,ebg13!", rot13("hello,rot13!"));
/// assert_eq!("Test", rot13(&rot13("Test")));
/// ```
pub fn rot13(text: &str) -> String {
    text.chars()
        .map(|c| match c {
            'A'..='M' | 'a'..='m' => ((c as u8) + 13) as char,
            'N'..='Z' | 'n'..='z' => ((c as u8) - 13) as char,
            _ => c,
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_rot13() {
        assert_eq!("uryyb,ebg13!", rot13("hello,rot13!"));
        assert_eq!("Test", rot13(&rot13("Test")));
    }
}