Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions casefolded/natsort.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ func NaturalLess(str1, str2 string) bool {

dig1, dig2 := isDigit(c1), isDigit(c2)
switch {
case dig1 != dig2: // Digits before other characters.
return dig1 // True if LHS is a digit, false if the RHS is one.
case !dig1: // && !dig2, because dig1 == dig2
case !dig1 || !dig2:
// For ASCII it suffices to normalize letters to upper-case,
// because upper-cased ASCII compares lexicographically.
// Note: this does not account for regional special cases
Expand Down Expand Up @@ -132,9 +130,7 @@ hasUnicode:

dig1, dig2 := isDigit(c1), isDigit(c2)
switch {
case dig1 != dig2: // Digits before other characters.
return dig1 // True if LHS is a digit, false if the RHS is one.
case !dig1: // && !dig2, because dig1 == dig2
case !dig1 || !dig2:
idx1 += delta1
idx2 += delta2
// Fast path: identical runes are equal.
Expand Down
3 changes: 3 additions & 0 deletions casefolded/natsort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ func TestNaturalLess(t *testing.T) {
// Kelvin sign followed by numeric comparison
{"\u212alm2", "Klm10", true},
{"Klm01", "\u212alm2", true},
// Special characters
{"!", "1", true},
{"!", "\u212alm2", true},
}
for _, v := range testset {
if got := NaturalLess(v.s1, v.s2); got != v.less {
Expand Down
4 changes: 1 addition & 3 deletions natsort.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ func NaturalLess(str1, str2 string) bool {
c1, c2 := str1[idx1], str2[idx2]
dig1, dig2 := isDigit(c1), isDigit(c2)
switch {
case dig1 != dig2: // Digits before other characters.
return dig1 // True if LHS is a digit, false if the RHS is one.
case !dig1: // && !dig2, because dig1 == dig2
case !dig1 || !dig2:
// UTF-8 compares bytewise-lexicographically, no need to decode
// codepoints.
if c1 != c2 {
Expand Down
2 changes: 2 additions & 0 deletions natsort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func TestNaturalLess(t *testing.T) {
//
{"082", "83", true},
{"9a", "083a", true},
// Special characters
{"!", "1", true},
}
for _, v := range testset {
if got := NaturalLess(v.s1, v.s2); got != v.less {
Expand Down