fix(cbx): use natural compare that handles numbers larger than int (#2536)
This commit is contained in:
+90
-23
@@ -651,33 +651,100 @@ public class CbxMetadataExtractor implements FileMetadataExtractor {
|
|||||||
return n.startsWith("cover") || "folder".equals(n) || n.startsWith("front");
|
return n.startsWith("cover") || "folder".equals(n) || n.startsWith("front");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the character is an ISO-LATIN-1 digit. This is
|
||||||
|
* different from the `Character.isDigit` which checks multiple
|
||||||
|
* types of digits.
|
||||||
|
*
|
||||||
|
* @param c char to check
|
||||||
|
* @return boolean Whether this is a ISO-LATIN-1 digit
|
||||||
|
*/
|
||||||
|
private boolean isLatinDigit(char c) {
|
||||||
|
return c >= '0' && c <= '9';
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getNextMeaningfulDigit(String target, int start) {
|
||||||
|
for (int i = start; i < target.length(); i++) {
|
||||||
|
char c = target.charAt(i);
|
||||||
|
// If we don't have any more digits we need to bail out.
|
||||||
|
if (!isLatinDigit(c)) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If it's a meaningful number we can stop. (eg, not '0')
|
||||||
|
if (Character.digit(c, 10) > 0) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return target.length() - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getEndOfDigits(String target, int start) {
|
||||||
|
int end = start;
|
||||||
|
while (end < target.length() && isLatinDigit(target.charAt(end))) {
|
||||||
|
end++;
|
||||||
|
}
|
||||||
|
return end;
|
||||||
|
}
|
||||||
|
|
||||||
private int naturalCompare(String a, String b) {
|
private int naturalCompare(String a, String b) {
|
||||||
if (a == null) return b == null ? 0 : -1;
|
if (a == null && b == null ) {
|
||||||
if (b == null) return 1;
|
return 0;
|
||||||
String s1 = a.toLowerCase();
|
} else if (a == null) {
|
||||||
String s2 = b.toLowerCase();
|
return -1;
|
||||||
int i = 0, j = 0, n1 = s1.length(), n2 = s2.length();
|
} else if (b == null) {
|
||||||
while (i < n1 && j < n2) {
|
return 1;
|
||||||
char c1 = s1.charAt(i);
|
}
|
||||||
char c2 = s2.charAt(j);
|
|
||||||
if (Character.isDigit(c1) && Character.isDigit(c2)) {
|
a = a.toLowerCase();
|
||||||
int i1 = i;
|
b = b.toLowerCase();
|
||||||
while (i1 < n1 && Character.isDigit(s1.charAt(i1))) i1++;
|
|
||||||
int j1 = j;
|
int aPointer = 0, bPointer = 0, aLength = a.length(), bLength = b.length();
|
||||||
while (j1 < n2 && Character.isDigit(s2.charAt(j1))) j1++;
|
while (aPointer < aLength && bPointer < bLength) {
|
||||||
String num1 = LEADING_ZEROS_PATTERN.matcher(s1.substring(i, i1)).replaceFirst("");
|
char aChar = a.charAt(aPointer);
|
||||||
String num2 = LEADING_ZEROS_PATTERN.matcher(s2.substring(j, j1)).replaceFirst("");
|
char bChar = b.charAt(bPointer);
|
||||||
int cmp = Integer.compare(num1.isEmpty() ? 0 : Integer.parseInt(num1), num2.isEmpty() ? 0 : Integer.parseInt(num2));
|
if (isLatinDigit(aChar) && isLatinDigit(bChar)) {
|
||||||
if (cmp != 0) return cmp;
|
aPointer = getNextMeaningfulDigit(a, aPointer);
|
||||||
i = i1;
|
int aPointerEnd = getEndOfDigits(a, aPointer);
|
||||||
j = j1;
|
|
||||||
|
bPointer = getNextMeaningfulDigit(b, bPointer);
|
||||||
|
int bPointerEnd = getEndOfDigits(b, bPointer);
|
||||||
|
|
||||||
|
// If they aren't the same length, we can know which number is bigger without
|
||||||
|
// comparing them directly.
|
||||||
|
int lengthCmp = Integer.compare(aPointerEnd - aPointer, bPointerEnd - bPointer);
|
||||||
|
if (lengthCmp != 0) {
|
||||||
|
return lengthCmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
int length = aPointerEnd - aPointer;
|
||||||
|
|
||||||
|
// Since we know they're the same length, and they're comprised of digits
|
||||||
|
// we can operate on both at the same time.
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
int cmp = Integer.compare(
|
||||||
|
Character.digit(a.charAt(aPointer + i), 10),
|
||||||
|
Character.digit(b.charAt(bPointer + i), 10)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (cmp != 0) {
|
||||||
|
return cmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no comparison was made, we push the pointer to the end.
|
||||||
|
aPointer = aPointerEnd;
|
||||||
|
bPointer = bPointerEnd;
|
||||||
|
} else if (aChar != bChar) {
|
||||||
|
return Character.compare(aChar, bChar);
|
||||||
} else {
|
} else {
|
||||||
if (c1 != c2) return Character.compare(c1, c2);
|
aPointer++;
|
||||||
i++;
|
bPointer++;
|
||||||
j++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Integer.compare(n1 - i, n2 - j);
|
|
||||||
|
return Integer.compare(aLength - aPointer, bLength - bPointer);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isComicInfoName(String name) {
|
private static boolean isComicInfoName(String name) {
|
||||||
|
|||||||
+30
@@ -1037,6 +1037,36 @@ class CbxMetadataExtractorTest {
|
|||||||
|
|
||||||
assertThat(cover).isNull();
|
assertThat(cover).isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void extractsCoverWithNaturalNumberComparisons() throws IOException {
|
||||||
|
byte[] expected = createMinimalJpeg(1);
|
||||||
|
|
||||||
|
Path cbzPath = mockArchiveContents(Map.of(
|
||||||
|
"img_000002.jpg", createMinimalJpeg(2),
|
||||||
|
"img_0001.jpg", expected,
|
||||||
|
"img_003.jpg", createMinimalJpeg(3)
|
||||||
|
));
|
||||||
|
|
||||||
|
byte[] actual = extractor.extractCover(cbzPath);
|
||||||
|
|
||||||
|
assertThat(actual).isEqualTo(expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void extractsCoverWithBigIntegerNumbers() throws IOException {
|
||||||
|
byte[] expected = createMinimalJpeg(1);
|
||||||
|
|
||||||
|
Path cbzPath = mockArchiveContents(Map.of(
|
||||||
|
"9789031433940_002.jpg", createMinimalJpeg(3),
|
||||||
|
"9789031433940_000.jpg", expected,
|
||||||
|
"9789031433940_001.jpg", createMinimalJpeg(2)
|
||||||
|
));
|
||||||
|
|
||||||
|
byte[] actual = extractor.extractCover(cbzPath);
|
||||||
|
|
||||||
|
assertThat(actual).isEqualTo(expected);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nested
|
@Nested
|
||||||
|
|||||||
Reference in New Issue
Block a user