Skip to content

Commit 7d4dd2c

Browse files
committed
Added valid empty space after parenthesis for phone; updated tests
1 parent 68d9394 commit 7d4dd2c

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

phone.go

+27-17
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ func (this *phoneRedaction) clear() {
77
this.numericLength = 0
88
}
99
func (this *phoneRedaction) match(input []byte) {
10+
var previousBreak byte
1011
for i := 0; i < len(input)-1; i++ {
1112
if i < len(this.used)-1 && this.used[i] {
1213
this.resetCount(i)
@@ -27,7 +28,8 @@ func (this *phoneRedaction) match(input []byte) {
2728
continue
2829
}
2930
if i < len(input)-1 {
30-
this.validateBreaks(input[i], i)
31+
this.validateBreaks(input[i], previousBreak, i)
32+
previousBreak = input[i]
3133
}
3234
}
3335
}
@@ -38,37 +40,45 @@ func (this *phoneRedaction) resetCount(i int) {
3840
this.breakLength = 0
3941
this.numericLength = 0
4042
}
41-
func (this *phoneRedaction) validateBreaks(input byte, i int) {
42-
switch input {
43-
case '-':
44-
this.length++
45-
this.breakLength++
46-
case '(':
47-
this.length++
48-
this.breakLength++
49-
case ')':
50-
this.length++
51-
this.breakLength++
52-
case '+':
43+
func (this *phoneRedaction) validateBreaks(input, previousBreak byte, i int) {
44+
if !validBreak(input) {
45+
this.resetCount(i)
46+
return
47+
}
48+
if input == ' ' && previousBreak != ')' {
49+
this.resetCount(i)
50+
return
51+
}
52+
if input == '+' {
5353
if this.start != i {
5454
this.resetCount(i)
55-
break
5655
}
5756
this.start = i + 1
5857
this.length = 1
59-
default:
60-
this.resetCount(i)
58+
return
6159
}
60+
61+
this.length++
62+
this.breakLength++
6263
}
64+
func validBreak(input byte) bool {
65+
return input == '-' || input == '(' || input == ')' || input == ' ' || input == '+'
66+
}
67+
6368
func (this *phoneRedaction) validateMatch(testMatch []byte) {
6469
switch {
6570
case this.length == MinPhoneLength_WithBreaks && this.breakLength == MinPhoneBreakLength:
6671
if testMatch[3] == '-' && testMatch[7] == '-' {
6772
this.appendMatch(this.start, this.length)
6873
}
69-
case this.length == MaxPhoneLength_WithBreaks && this.breakLength == MaxPhoneBreakLength:
74+
75+
case this.length == MaxPhoneLength_WithBreaks-1 && this.breakLength == MaxPhoneBreakLength-1: // No space
7076
if testMatch[1] == '(' && testMatch[5] == ')' && testMatch[9] == '-' {
7177
this.appendMatch(this.start, this.length)
7278
}
79+
case this.length == MaxPhoneLength_WithBreaks && this.breakLength == MaxPhoneBreakLength: // With space
80+
if testMatch[1] == '(' && testMatch[5] == ')' && testMatch[6] == ' ' && testMatch[10] == '-' {
81+
this.appendMatch(this.start, this.length)
82+
}
7383
}
7484
}

redact_structs.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ func isNumeric(value byte) bool {
5858
const (
5959
MaxEmailLength = 254
6060

61-
MaxPhoneLength_WithBreaks = 14
61+
MaxPhoneLength_WithBreaks = 15
6262
MinPhoneLength_WithBreaks = 12
6363
MinPhoneLength_WithNoBreaks = 10
64-
MaxPhoneBreakLength = 3
64+
MaxPhoneBreakLength = 4
6565
MinPhoneBreakLength = 2
6666

6767
MaxSSNLength_WithBreaks = 11

redactor_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ func TestRedactPhone_Valid_Redaction(t *testing.T) {
160160
"+1(801)111-1111 taco",
161161
"+************** taco",
162162
)
163+
assertRedaction(t, redaction,
164+
"+1(801) 111-1111 taco",
165+
"+*************** taco",
166+
)
163167
assertRedaction(t, redaction,
164168
"801-111-1111 and (801) 111-1111 +1(801)111-1111 taco",
165169
"************ and (801) 111-1111 +************** taco",
@@ -168,7 +172,6 @@ func TestRedactPhone_Valid_Redaction(t *testing.T) {
168172
"Blah 801-111-1111 and (801) 111-1111 +1(801)111-1111 taco",
169173
"Blah ************ and (801) 111-1111 +************** taco",
170174
)
171-
172175
}
173176
func TestRedactPhone_Invalid_NoRedaction(t *testing.T) {
174177
t.Parallel()

0 commit comments

Comments
 (0)