-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphone.go
84 lines (79 loc) · 2.2 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
75
76
77
78
79
80
81
82
83
84
package redact
func (this *phoneRedaction) clear() {
this.length = 0
this.start = 0
this.breakLength = 0
this.numericLength = 0
}
func (this *phoneRedaction) match(input []byte) {
var previousBreak byte
for i, val := range input {
if i < len(this.used)-1 && this.used[i] {
this.resetCount(i)
continue
}
if isNumeric(val) {
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(val, previousBreak, i)
previousBreak = input[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, previousBreak byte, i int) {
if !validBreak(input) {
this.resetCount(i)
return
}
if input == ' ' && previousBreak != ')' {
this.resetCount(i)
return
}
if input == '+' {
if this.start != i {
this.resetCount(i)
}
this.start = i + 1
this.length = 1
return
}
this.length++
this.breakLength++
}
func validBreak(input byte) bool {
return input == '-' || input == '(' || input == ')' || input == ' ' || input == '+'
}
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-1 && this.breakLength == MaxPhoneBreakLength-1: // No space
if testMatch[1] == '(' && testMatch[5] == ')' && testMatch[9] == '-' {
this.appendMatch(this.start, this.length)
}
case this.length == MaxPhoneLength_WithBreaks && this.breakLength == MaxPhoneBreakLength: // With space
if testMatch[1] == '(' && testMatch[5] == ')' && testMatch[6] == ' ' && testMatch[10] == '-' {
this.appendMatch(this.start, this.length)
}
}
}