-
Notifications
You must be signed in to change notification settings - Fork 28
/
cpfcnpj.go
101 lines (80 loc) · 1.85 KB
/
cpfcnpj.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package brdoc
import (
"bytes"
"regexp"
"strconv"
"unicode"
)
// Regexp pattern for CPF and CNPJ.
var (
CPFRegexp = regexp.MustCompile(`^\d{3}\.?\d{3}\.?\d{3}-?\d{2}$`)
CNPJRegexp = regexp.MustCompile(`^\d{2}\.?\d{3}\.?\d{3}\/?(:?\d{3}[1-9]|\d{2}[1-9]\d|\d[1-9]\d{2}|[1-9]\d{3})-?\d{2}$`)
)
// IsCPF verifies if the given string is a valid CPF document.
func IsCPF(doc string) bool {
const (
size = 9
pos = 10
)
return isCPFOrCNPJ(doc, CPFRegexp, size, pos)
}
// IsCNPJ verifies if the given string is a valid CNPJ document.
func IsCNPJ(doc string) bool {
const (
size = 12
pos = 5
)
return isCPFOrCNPJ(doc, CNPJRegexp, size, pos)
}
// isCPFOrCNPJ generates the digits for a given CPF or CNPJ and compares it with the original digits.
func isCPFOrCNPJ(doc string, pattern *regexp.Regexp, size int, position int) bool {
if !pattern.MatchString(doc) {
return false
}
cleanNonDigits(&doc)
// Invalidates documents with all digits equal.
if allEq(doc) {
return false
}
d := doc[:size]
digit := calculateDigit(d, position)
d = d + digit
digit = calculateDigit(d, position+1)
return doc == d+digit
}
// cleanNonDigits removes every rune that is not a digit.
func cleanNonDigits(doc *string) {
buf := bytes.NewBufferString("")
for _, r := range *doc {
if unicode.IsDigit(r) {
buf.WriteRune(r)
}
}
*doc = buf.String()
}
// allEq checks if every rune in a given string is equal.
func allEq(doc string) bool {
base := doc[0]
for i := 1; i < len(doc); i++ {
if base != doc[i] {
return false
}
}
return true
}
// calculateDigit calculates the next digit for the given document.
func calculateDigit(doc string, position int) string {
var sum int
for _, r := range doc {
sum += toInt(r) * position
position--
if position < 2 {
position = 9
}
}
sum %= 11
if sum < 2 {
return "0"
}
return strconv.Itoa(11 - sum)
}