20
20
package modfile
21
21
22
22
import (
23
+ "cmp"
23
24
"errors"
24
25
"fmt"
25
26
"path/filepath"
26
- "sort "
27
+ "slices "
27
28
"strconv"
28
29
"strings"
29
30
"unicode"
@@ -1633,15 +1634,13 @@ func (f *File) SortBlocks() {
1633
1634
if ! ok {
1634
1635
continue
1635
1636
}
1636
- less := lineLess
1637
+ less := compareLine
1637
1638
if block .Token [0 ] == "exclude" && useSemanticSortForExclude {
1638
- less = lineExcludeLess
1639
+ less = compareLineExclude
1639
1640
} else if block .Token [0 ] == "retract" {
1640
- less = lineRetractLess
1641
+ less = compareLineRetract
1641
1642
}
1642
- sort .SliceStable (block .Line , func (i , j int ) bool {
1643
- return less (block .Line [i ], block .Line [j ])
1644
- })
1643
+ slices .SortStableFunc (block .Line , less )
1645
1644
}
1646
1645
}
1647
1646
@@ -1746,39 +1745,38 @@ func removeDups(syntax *FileSyntax, exclude *[]*Exclude, replace *[]*Replace, to
1746
1745
syntax .Stmt = stmts
1747
1746
}
1748
1747
1749
- // lineLess returns whether li should be sorted before lj. It sorts
1750
- // lexicographically without assigning any special meaning to tokens.
1751
- func lineLess (li , lj * Line ) bool {
1748
+ // compareLine compares li and lj. It sorts lexicographically without assigning
1749
+ // any special meaning to tokens.
1750
+ func compareLine (li , lj * Line ) int {
1752
1751
for k := 0 ; k < len (li .Token ) && k < len (lj .Token ); k ++ {
1753
1752
if li .Token [k ] != lj .Token [k ] {
1754
- return li .Token [k ] < lj .Token [k ]
1753
+ return cmp . Compare ( li .Token [k ], lj .Token [k ])
1755
1754
}
1756
1755
}
1757
- return len (li .Token ) < len (lj .Token )
1756
+ return cmp . Compare ( len (li .Token ), len (lj .Token ) )
1758
1757
}
1759
1758
1760
- // lineExcludeLess reports whether li should be sorted before lj for lines in
1761
- // an "exclude" block.
1762
- func lineExcludeLess (li , lj * Line ) bool {
1759
+ // compareLineExclude compares li and lj for lines in an "exclude" block.
1760
+ func compareLineExclude (li , lj * Line ) int {
1763
1761
if len (li .Token ) != 2 || len (lj .Token ) != 2 {
1764
1762
// Not a known exclude specification.
1765
1763
// Fall back to sorting lexicographically.
1766
- return lineLess (li , lj )
1764
+ return compareLine (li , lj )
1767
1765
}
1768
1766
// An exclude specification has two tokens: ModulePath and Version.
1769
1767
// Compare module path by string order and version by semver rules.
1770
1768
if pi , pj := li .Token [0 ], lj .Token [0 ]; pi != pj {
1771
- return pi < pj
1769
+ return cmp . Compare ( pi , pj )
1772
1770
}
1773
- return semver .Compare (li .Token [1 ], lj .Token [1 ]) < 0
1771
+ return semver .Compare (li .Token [1 ], lj .Token [1 ])
1774
1772
}
1775
1773
1776
- // lineRetractLess returns whether li should be sorted before lj for lines in
1777
- // a "retract" block. It treats each line as a version interval. Single versions
1778
- // are compared as if they were intervals with the same low and high version.
1774
+ // compareLineRetract compares li and lj for lines in a "retract" block.
1775
+ // It treats each line as a version interval. Single versions are compared as
1776
+ // if they were intervals with the same low and high version.
1779
1777
// Intervals are sorted in descending order, first by low version, then by
1780
- // high version, using semver.Compare.
1781
- func lineRetractLess (li , lj * Line ) bool {
1778
+ // high version, using [ semver.Compare] .
1779
+ func compareLineRetract (li , lj * Line ) int {
1782
1780
interval := func (l * Line ) VersionInterval {
1783
1781
if len (l .Token ) == 1 {
1784
1782
return VersionInterval {Low : l .Token [0 ], High : l .Token [0 ]}
@@ -1792,9 +1790,9 @@ func lineRetractLess(li, lj *Line) bool {
1792
1790
vii := interval (li )
1793
1791
vij := interval (lj )
1794
1792
if cmp := semver .Compare (vii .Low , vij .Low ); cmp != 0 {
1795
- return cmp > 0
1793
+ return - cmp
1796
1794
}
1797
- return semver .Compare (vii .High , vij .High ) > 0
1795
+ return - semver .Compare (vii .High , vij .High )
1798
1796
}
1799
1797
1800
1798
// checkCanonicalVersion returns a non-nil error if vers is not a canonical
0 commit comments