@@ -21,6 +21,7 @@ package main
21
21
import (
22
22
"bufio"
23
23
"bytes"
24
+ "cmp"
24
25
"encoding/binary"
25
26
"flag"
26
27
"fmt"
@@ -29,7 +30,7 @@ import (
29
30
"net/http"
30
31
"os"
31
32
"regexp"
32
- "sort "
33
+ "slices "
33
34
"strings"
34
35
35
36
"golang.org/x/net/idna"
@@ -62,39 +63,13 @@ var (
62
63
maxLo uint32
63
64
)
64
65
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
-
79
66
const (
80
67
nodeTypeNormal = 0
81
68
nodeTypeException = 1
82
69
nodeTypeParentOnly = 2
83
70
numNodeType = 3
84
71
)
85
72
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
-
98
73
const (
99
74
defaultURL = "https://publicsuffix.org/list/effective_tld_names.dat"
100
75
gitCommitURL = "https://api.github.com/repos/publicsuffix/list/commits?path=public_suffix_list.dat"
@@ -251,7 +226,7 @@ func main1() error {
251
226
for label := range labelsMap {
252
227
labelsList = append (labelsList , label )
253
228
}
254
- sort . Strings (labelsList )
229
+ slices . Sort (labelsList )
255
230
256
231
combinedText = combineText (labelsList )
257
232
if combinedText == "" {
@@ -509,15 +484,13 @@ func (n *node) child(label string) *node {
509
484
icann : true ,
510
485
}
511
486
n .children = append (n .children , c )
512
- sort . Sort ( byLabel ( n .children ) )
487
+ slices . SortFunc ( n .children , byLabel )
513
488
return c
514
489
}
515
490
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
+ }
521
494
522
495
var nextNodesIndex int
523
496
@@ -557,7 +530,7 @@ func assignIndexes(n *node) error {
557
530
n .childrenIndex = len (childrenEncoding )
558
531
lo := uint32 (n .firstChild )
559
532
hi := lo + uint32 (len (n .children ))
560
- maxLo , maxHi = u32max (maxLo , lo ), u32max (maxHi , hi )
533
+ maxLo , maxHi = max (maxLo , lo ), max (maxHi , hi )
561
534
if lo >= 1 << childrenBitsLo {
562
535
return fmt .Errorf ("children lo %d is too large, or childrenBitsLo is too small" , lo )
563
536
}
@@ -586,20 +559,6 @@ func printNodeLabel(w io.Writer, n *node) error {
586
559
return nil
587
560
}
588
561
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
-
603
562
// combineText combines all the strings in labelsList to form one giant string.
604
563
// Overlapping strings will be merged: "arpa" and "parliament" could yield
605
564
// "arparliament".
@@ -616,18 +575,15 @@ func combineText(labelsList []string) string {
616
575
return text
617
576
}
618
577
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
+ }
624
581
625
582
// removeSubstrings returns a copy of its input with any strings removed
626
583
// that are substrings of other provided strings.
627
584
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 )
631
587
632
588
for i , shortString := range ss {
633
589
// For each string, only consider strings higher than it in sort order, i.e.
@@ -641,7 +597,7 @@ func removeSubstrings(input []string) []string {
641
597
}
642
598
643
599
// Remove the empty strings.
644
- sort . Strings (ss )
600
+ slices . Sort (ss )
645
601
for len (ss ) > 0 && ss [0 ] == "" {
646
602
ss = ss [1 :]
647
603
}
0 commit comments