Skip to content

Commit 48e82f9

Browse files
committed
Pull request: 2479 simpl
Updates #2479. Squashed commit of the following: commit 0fdb0d0 Author: Ainar Garipov <[email protected]> Date: Mon Mar 29 19:22:56 2021 +0300 dnsfilter: add a comment commit d5d6538 Merge: 6a09acc e710ce1 Author: Ainar Garipov <[email protected]> Date: Mon Mar 29 19:19:39 2021 +0300 Merge branch 'master' into 2479-simpl commit 6a09acc Author: jvoisin <[email protected]> Date: Tue Dec 22 16:43:47 2020 +0100 Generalise a construct to simplify a function
1 parent e710ce1 commit 48e82f9

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

internal/dnsfilter/rewrites.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,27 @@ func findRewrites(a []RewriteEntry, host string) []RewriteEntry {
122122

123123
sort.Sort(rr)
124124

125-
isWC := isWildcard(rr[0].Domain)
126-
if !isWC {
127-
for i, r := range rr {
128-
if isWildcard(r.Domain) {
129-
rr = rr[:i]
130-
break
131-
}
125+
for i, r := range rr {
126+
if isWildcard(r.Domain) {
127+
// Don't use rr[:0], because we need to return at least
128+
// one item here.
129+
rr = rr[:max(1, i)]
130+
131+
break
132132
}
133-
} else {
134-
rr = rr[:1]
135133
}
136134

137135
return rr
138136
}
139137

138+
func max(a, b int) int {
139+
if a > b {
140+
return a
141+
}
142+
143+
return b
144+
}
145+
140146
func rewriteArrayDup(a []RewriteEntry) []RewriteEntry {
141147
a2 := make([]RewriteEntry, len(a))
142148
copy(a2, a)

0 commit comments

Comments
 (0)