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
/// Checks if a char is allowed in a PDB file.
/// The char has to be ASCII graphic or a space.
/// Returns `true` if the char is valid.
pub const fn check_char(c: char) -> bool {
    (c as u32) < 127 && c as u32 > 31
}

/// Checks a string using `check_char`.
/// Returns `true` if the text is valid.
pub fn valid_text(text: impl AsRef<str>) -> bool {
    text.as_ref().chars().all(check_char)
}

/// Checks a string using `check_char`.
/// Returns `true` if the text is valid.
pub fn valid_identifier(text: impl AsRef<str>) -> bool {
    text.as_ref().chars().all(check_char)
}

/// Creates a valid identifier from the given string slice.
/// Also turns the identifier to uppercase.
pub fn prepare_identifier(text: impl AsRef<str>) -> Option<String> {
    let text = text.as_ref();
    (valid_identifier(text) && !text.trim().is_empty()).then(|| text.trim().to_ascii_uppercase())
}

const ALPHABET: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

/// Converts a number into a base26 with only the alphabet as possible chars
#[allow(clippy::unwrap_used)]
pub fn number_to_base26(mut num: usize) -> String {
    let mut output = vec![ALPHABET.chars().nth(num % 26).unwrap()];
    num /= 26;
    while num != 0 {
        output.push(ALPHABET.chars().nth(num % 26).unwrap());
        num /= 26;
    }
    output.iter().rev().collect::<String>()
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn correct_examples() {
        assert!(check_char('a'));
        assert!(check_char('9'));
        assert!(check_char('*'));
        assert!(check_char('@'));
        assert!(check_char('O'));
        assert!(valid_text("ResidueName"));
        assert!(valid_text("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`-=[]\\;',./~!@#$%^&*()_+{}|:\"<>? "));
    }
    #[test]
    fn incorrect_examples() {
        assert!(!check_char('̊'));
        assert!(!check_char('∞'));
        assert!(!check_char('👍'));
        assert!(!check_char('ÿ'));
        assert!(!check_char('\u{0}'));
        assert!(!valid_text("ResidueName∞"));
        assert!(!valid_text("Escape\u{0}"));
    }
    #[test]
    fn number_to_base26_test() {
        assert_eq!(number_to_base26(26), "BA");
        assert_eq!(number_to_base26(0), "A");
        assert_eq!(number_to_base26(234), "JA");
        assert_eq!(number_to_base26(25), "Z");
        assert_eq!(number_to_base26(457), "RP");
        assert_eq!(number_to_base26(15250), "WOO");
        assert_eq!(number_to_base26(396514), "WOOO");
    }
}