rust_br/check/
cpf.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
pub fn cpf(cpf: &str) -> bool {
    let cpf: Vec<u8> = cpf
        .chars()
        .filter_map(|c| c.to_digit(10))
        .map(|d| d as u8)
        .collect();

    if cpf.len() != 11 || cpf.iter().all(|&d| d == cpf[0]) {
        return false;
    }

    let mut sum = 0;
    for i in 0..9 {
        sum += (cpf[i] as usize) * (10 - i);
    }
    let rest = (sum * 10) % 11;
    let first_digit = if rest == 10 { 0 } else { rest as u8 };

    sum = 0;
    for i in 0..10 {
        sum += (cpf[i] as usize) * (11 - i);
    }
    let rest = (sum * 10) % 11;
    let second_digit = if rest == 10 { 0 } else { rest as u8 };

    first_digit == cpf[9] && second_digit == cpf[10]
}