From 48e82f9ab5c337f9579737d6408047485100a53d Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Mon, 29 Mar 2021 19:35:35 +0300 Subject: [PATCH] Pull request: 2479 simpl Updates #2479. Squashed commit of the following: commit 0fdb0d041d0bd0d9af64513cf82397456a30e2f2 Author: Ainar Garipov Date: Mon Mar 29 19:22:56 2021 +0300 dnsfilter: add a comment commit d5d6538b8b5133d7c1e9b242a8ac802448d40893 Merge: 6a09acc2 e710ce11 Author: Ainar Garipov Date: Mon Mar 29 19:19:39 2021 +0300 Merge branch 'master' into 2479-simpl commit 6a09acc2626900fc52873fb0bba3c5978ae319a0 Author: jvoisin Date: Tue Dec 22 16:43:47 2020 +0100 Generalise a construct to simplify a function --- internal/dnsfilter/rewrites.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/internal/dnsfilter/rewrites.go b/internal/dnsfilter/rewrites.go index 146be60a..5aae925a 100644 --- a/internal/dnsfilter/rewrites.go +++ b/internal/dnsfilter/rewrites.go @@ -122,21 +122,27 @@ func findRewrites(a []RewriteEntry, host string) []RewriteEntry { sort.Sort(rr) - isWC := isWildcard(rr[0].Domain) - if !isWC { - for i, r := range rr { - if isWildcard(r.Domain) { - rr = rr[:i] - break - } + for i, r := range rr { + if isWildcard(r.Domain) { + // Don't use rr[:0], because we need to return at least + // one item here. + rr = rr[:max(1, i)] + + break } - } else { - rr = rr[:1] } return rr } +func max(a, b int) int { + if a > b { + return a + } + + return b +} + func rewriteArrayDup(a []RewriteEntry) []RewriteEntry { a2 := make([]RewriteEntry, len(a)) copy(a2, a)