@@ -32,9 +32,14 @@ func RemovePrefixes(items Set[string], sets ...Set[string]) Set[string] {
3232 nonPrefixes := maps .Clone (items )
3333
3434 values := Union (append (sets , items )... ).UnsortedList ()
35- slices .Sort (values )
35+ slices .SortFunc (values , func (a , b string ) int {
36+ aSort := strings .ReplaceAll (a , "[" , ".[" ) + "."
37+ bSort := strings .ReplaceAll (b , "[" , ".[" ) + "."
38+ return strings .Compare (aSort , bSort )
39+ })
40+
3641 for i := 0 ; i < len (values ); i ++ {
37- // If the next value is an extension of the current value, skip
42+ // If the next value is an extension of the current value, remove
3843 // the current value.
3944 if i + 1 < len (values ) && (strings .HasPrefix (values [i + 1 ], values [i ]+ "." ) || strings .HasPrefix (values [i + 1 ], values [i ]+ "[" )) {
4045 nonPrefixes .Delete (values [i ])
@@ -54,12 +59,21 @@ func RemoveExtensions(items Set[string], sets ...Set[string]) Set[string] {
5459 nonExtensions := maps .Clone (items )
5560
5661 values := Union (append (sets , items )... ).UnsortedList ()
57- slices .Sort (values )
62+ slices .SortFunc (values , func (a , b string ) int {
63+ aSort := strings .ReplaceAll (a , "[" , ".[" ) + "."
64+ bSort := strings .ReplaceAll (b , "[" , ".[" ) + "."
65+ return strings .Compare (aSort , bSort )
66+ })
67+
68+ OuterLoop:
5869 for i := 0 ; i < len (values ); i ++ {
59- // If the next value is an extension of the current value, skip
60- // the current value.
61- if i + 1 < len (values ) && (strings .HasPrefix (values [i + 1 ], values [i ]+ "." ) || strings .HasPrefix (values [i + 1 ], values [i ]+ "[" )) {
62- nonExtensions .Delete (values [i + 1 ])
70+ // Remove all following values that are extensions of the current value.
71+ for j := i + 1 ; j < len (values ); j ++ {
72+ if ! strings .HasPrefix (values [j ], values [i ]+ "." ) && ! strings .HasPrefix (values [j ], values [i ]+ "[" ) {
73+ continue OuterLoop
74+ }
75+
76+ nonExtensions .Delete (values [j ])
6377 }
6478 }
6579
0 commit comments