-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphone.go
74 lines (72 loc) · 1.76 KB
/
phone.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
package redact
func (this *phoneRedaction) clear() {
this.length = 0
this.start = 0
this.breakLength = 0
this.numericLength = 0
}
func (this *phoneRedaction) match(input []byte) {
for i := 0; i < len(input)-1; i++ {
if i < len(this.used)-1 && this.used[i] {
this.resetCount(i)
continue
}
if isNumeric(input[i]) {
this.numericLength++
this.length++
switch {
case this.length < MaxPhoneLength_WithBreaks && this.numericLength != MinPhoneLength_WithNoBreaks:
continue
case this.length >= MinPhoneLength_WithNoBreaks && this.length <= MaxPhoneLength_WithBreaks && this.breakLength <= MaxPhoneBreakLength:
this.validateMatch(input[this.start:i])
}
if i < len(input)-1 {
this.resetCount(i)
}
continue
}
if i < len(input)-1 {
this.validateBreaks(input[i], i)
}
}
}
func (this *phoneRedaction) resetCount(i int) {
this.start = i + 1
this.length = 0
this.breakLength = 0
this.numericLength = 0
}
func (this *phoneRedaction) validateBreaks(input byte, i int) {
switch input {
case '-':
this.length++
this.breakLength++
case '(':
this.length++
this.breakLength++
case ')':
this.length++
this.breakLength++
case '+':
if this.start != i {
this.resetCount(i)
break
}
this.start = i + 1
this.length = 1
default:
this.resetCount(i)
}
}
func (this *phoneRedaction) validateMatch(testMatch []byte) {
switch {
case this.length == MinPhoneLength_WithBreaks && this.breakLength == MinPhoneBreakLength:
if testMatch[3] == '-' && testMatch[7] == '-' {
this.appendMatch(this.start, this.length)
}
case this.length == MaxPhoneLength_WithBreaks && this.breakLength == MaxPhoneBreakLength:
if testMatch[1] == '(' && testMatch[5] == ')' && testMatch[9] == '-' {
this.appendMatch(this.start, this.length)
}
}
}