Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
melhorias nos comentários .
  • Loading branch information
dennersousa committed Feb 12, 2024
1 parent d0d81bb commit 0411799
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions validate/cpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,52 +26,52 @@ func IsValidCPFHandler(c *gin.Context) {
c.JSON(http.StatusOK, result)
}

// IsValidCPF checks if a CPF (Brazilian ID number) is valid.
// The input should be a string containing only numeric digits.
// IsValidCPF verifica se um CPF (Cadastro de Pessoa Física brasileiro) é válido.
// A entrada deve ser uma string contendo apenas dígitos numéricos.
func IsValidCPF(cpf string) bool {
// Remove non-numeric characters from CPF
// Remove caracteres não numéricos do CPF
cpf = strings.ReplaceAll(cpf, ".", "")
cpf = strings.ReplaceAll(cpf, "-", "")

// Check if the CPF contains only numeric digits
// Verifique se o CPF contém apenas dígitos numéricos
if _, err := strconv.Atoi(cpf); err != nil {
return false
}

// Check for known invalid CPFs
// Verifique CPFs inválidos conhecidos
if strings.Count(cpf, string(cpf[0])) == 11 {
return false
}

// Extract digits from CPF
// Extraia os dígitos do CPF
digits := make([]int, 11)
for i := 0; i < 11; i++ {
if i < len(cpf) {
digit, err := strconv.Atoi(string(cpf[i]))
if err != nil {
// Handle error as needed
// Trate o erro conforme necessário
}
digits[i] = digit
} else {
// Handle the case where the string is too short
// Trate o caso em que a string é muito curta
return false
}
}

// Calculate the first verification digit
// Calcule o primeiro dígito de verificação
sum1 := digits[0]*10 + digits[1]*9 + digits[2]*8 + digits[3]*7 + digits[4]*6 + digits[5]*5 + digits[6]*4 + digits[7]*3 + digits[8]*2
remainder1 := (sum1 * 10) % 11
if remainder1 == 10 {
remainder1 = 0
}

// Calculate the second verification digit
// Calcule o segundo dígito de verificação
sum2 := digits[0]*11 + digits[1]*10 + digits[2]*9 + digits[3]*8 + digits[4]*7 + digits[5]*6 + digits[6]*5 + digits[7]*4 + digits[8]*3 + digits[9]*2
remainder2 := (sum2 * 10) % 11
if remainder2 == 10 {
remainder2 = 0
}

// Check verification digits
// Verifique os dígitos de verificação
return remainder1 == digits[9] && remainder2 == digits[10]
}

0 comments on commit 0411799

Please sign in to comment.