11/*
2- * Copyright (C) 2023-2024 DiffPlug
2+ * Copyright (C) 2023-2025 DiffPlug
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -23,16 +23,48 @@ private val STRING_SLASHFIRST =
2323 val charA = a[i]
2424 val charB = b[i]
2525 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+ }
3149 }
3250 i++
3351 }
3452 a.length.compareTo(b.length)
3553 }
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+ }
3668private val PAIR_STRING_SLASHFIRST =
3769 Comparator <Pair <String , Any >> { a, b -> STRING_SLASHFIRST .compare(a.first, b.first) }
3870
0 commit comments