Skip to content

Commit 395867e

Browse files
author
Julien Cretel
committed
publicsuffix: spruce up code generating the table
Rely on functions from the slices package where convenient. Drop custom max functions in favor of max builtin. Remove unused non-exported functions.
1 parent b4c8655 commit 395867e

File tree

1 file changed

+14
-58
lines changed

1 file changed

+14
-58
lines changed

Diff for: publicsuffix/gen.go

+14-58
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package main
2121
import (
2222
"bufio"
2323
"bytes"
24+
"cmp"
2425
"encoding/binary"
2526
"flag"
2627
"fmt"
@@ -29,7 +30,7 @@ import (
2930
"net/http"
3031
"os"
3132
"regexp"
32-
"sort"
33+
"slices"
3334
"strings"
3435

3536
"golang.org/x/net/idna"
@@ -62,39 +63,13 @@ var (
6263
maxLo uint32
6364
)
6465

65-
func max(a, b int) int {
66-
if a < b {
67-
return b
68-
}
69-
return a
70-
}
71-
72-
func u32max(a, b uint32) uint32 {
73-
if a < b {
74-
return b
75-
}
76-
return a
77-
}
78-
7966
const (
8067
nodeTypeNormal = 0
8168
nodeTypeException = 1
8269
nodeTypeParentOnly = 2
8370
numNodeType = 3
8471
)
8572

86-
func nodeTypeStr(n int) string {
87-
switch n {
88-
case nodeTypeNormal:
89-
return "+"
90-
case nodeTypeException:
91-
return "!"
92-
case nodeTypeParentOnly:
93-
return "o"
94-
}
95-
panic("unreachable")
96-
}
97-
9873
const (
9974
defaultURL = "https://publicsuffix.org/list/effective_tld_names.dat"
10075
gitCommitURL = "https://api.github.com/repos/publicsuffix/list/commits?path=public_suffix_list.dat"
@@ -251,7 +226,7 @@ func main1() error {
251226
for label := range labelsMap {
252227
labelsList = append(labelsList, label)
253228
}
254-
sort.Strings(labelsList)
229+
slices.Sort(labelsList)
255230

256231
combinedText = combineText(labelsList)
257232
if combinedText == "" {
@@ -509,15 +484,13 @@ func (n *node) child(label string) *node {
509484
icann: true,
510485
}
511486
n.children = append(n.children, c)
512-
sort.Sort(byLabel(n.children))
487+
slices.SortFunc(n.children, byLabel)
513488
return c
514489
}
515490

516-
type byLabel []*node
517-
518-
func (b byLabel) Len() int { return len(b) }
519-
func (b byLabel) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
520-
func (b byLabel) Less(i, j int) bool { return b[i].label < b[j].label }
491+
func byLabel(a, b *node) int {
492+
return strings.Compare(a.label, b.label)
493+
}
521494

522495
var nextNodesIndex int
523496

@@ -557,7 +530,7 @@ func assignIndexes(n *node) error {
557530
n.childrenIndex = len(childrenEncoding)
558531
lo := uint32(n.firstChild)
559532
hi := lo + uint32(len(n.children))
560-
maxLo, maxHi = u32max(maxLo, lo), u32max(maxHi, hi)
533+
maxLo, maxHi = max(maxLo, lo), max(maxHi, hi)
561534
if lo >= 1<<childrenBitsLo {
562535
return fmt.Errorf("children lo %d is too large, or childrenBitsLo is too small", lo)
563536
}
@@ -586,20 +559,6 @@ func printNodeLabel(w io.Writer, n *node) error {
586559
return nil
587560
}
588561

589-
func icannStr(icann bool) string {
590-
if icann {
591-
return "I"
592-
}
593-
return " "
594-
}
595-
596-
func wildcardStr(wildcard bool) string {
597-
if wildcard {
598-
return "*"
599-
}
600-
return " "
601-
}
602-
603562
// combineText combines all the strings in labelsList to form one giant string.
604563
// Overlapping strings will be merged: "arpa" and "parliament" could yield
605564
// "arparliament".
@@ -616,18 +575,15 @@ func combineText(labelsList []string) string {
616575
return text
617576
}
618577

619-
type byLength []string
620-
621-
func (s byLength) Len() int { return len(s) }
622-
func (s byLength) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
623-
func (s byLength) Less(i, j int) bool { return len(s[i]) < len(s[j]) }
578+
func byLength(a, b string) int {
579+
return cmp.Compare(len(a), len(b))
580+
}
624581

625582
// removeSubstrings returns a copy of its input with any strings removed
626583
// that are substrings of other provided strings.
627584
func removeSubstrings(input []string) []string {
628-
// Make a copy of input.
629-
ss := append(make([]string, 0, len(input)), input...)
630-
sort.Sort(byLength(ss))
585+
ss := slices.Clone(input)
586+
slices.SortFunc(ss, byLength)
631587

632588
for i, shortString := range ss {
633589
// For each string, only consider strings higher than it in sort order, i.e.
@@ -641,7 +597,7 @@ func removeSubstrings(input []string) []string {
641597
}
642598

643599
// Remove the empty strings.
644-
sort.Strings(ss)
600+
slices.Sort(ss)
645601
for len(ss) > 0 && ss[0] == "" {
646602
ss = ss[1:]
647603
}

0 commit comments

Comments
 (0)