1
1
/*
2
- * Copyright (C) 2023-2024 DiffPlug
2
+ * Copyright (C) 2023-2025 DiffPlug
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -23,16 +23,48 @@ private val STRING_SLASHFIRST =
23
23
val charA = a[i]
24
24
val charB = b[i]
25
25
if (charA != charB) {
26
- return @Comparator ( // treat
27
- if (charA == ' /' ) - 1 // slash as
28
- else if (charB == ' /' ) 1 // the lowest
29
- else charA.compareTo(charB) // character
30
- )
26
+ // Check for slash first as it's special
27
+ if (charA == ' /' ) return @Comparator - 1 // treat slash as the lowest character
28
+ if (charB == ' /' ) return @Comparator 1
29
+
30
+ // Check for embedded numbers
31
+ if (charA.isDigit() && charB.isDigit()) {
32
+ // Extract the complete numbers from both strings
33
+ val numA = extractNumber(a, i)
34
+ val numB = extractNumber(b, i)
35
+
36
+ // Compare the numeric values
37
+ val numCompare = numA.first.compareTo(numB.first)
38
+ if (numCompare != 0 ) return @Comparator numCompare
39
+
40
+ // If the numbers are equal, adjust index to after the numbers
41
+ // and continue comparing the rest of the string
42
+ i =
43
+ maxOf(numA.second, numB.second) -
44
+ 1 // -1 because we increment i at the end of the loop
45
+ } else {
46
+ // Regular character comparison
47
+ return @Comparator charA.compareTo(charB)
48
+ }
31
49
}
32
50
i++
33
51
}
34
52
a.length.compareTo(b.length)
35
53
}
54
+
55
+ /* *
56
+ * Extracts a numeric substring starting at the given index.
57
+ *
58
+ * @return Pair of (numeric value, index after the last digit)
59
+ */
60
+ private fun extractNumber (s : String , startIndex : Int ): Pair <Int , Int > {
61
+ var endIndex = startIndex
62
+ while (endIndex < s.length && s[endIndex].isDigit()) {
63
+ endIndex++
64
+ }
65
+ val number = s.substring(startIndex, endIndex).toInt()
66
+ return Pair (number, endIndex)
67
+ }
36
68
private val PAIR_STRING_SLASHFIRST =
37
69
Comparator <Pair <String , Any >> { a, b -> STRING_SLASHFIRST .compare(a.first, b.first) }
38
70
0 commit comments