Skip to content

Commit

Permalink
Fix NumberFormatException in BracketedPattern (#7600)
Browse files Browse the repository at this point in the history
* Fix NumberFormatException in BracketedPattern

* Add entry in CHANGELOG.md

* Add test case for `[lastpage]`

* Fix checkstyle
  • Loading branch information
k3KAW8Pnf7mkmdSMPHz27 authored Mar 31, 2021
1 parent d6b7e20 commit bb011c9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We fixed an issue in which a linked online file consisting of a web page was saved as an invalid pdf file upon being downloaded. The user is now notified when downloading a linked file results in an HTML file. [#7452](https://github.com/JabRef/jabref/issues/7452)
- We fixed an issue where opening BibTex file (doubleclick) from Folder with spaces not working. [#6487](https://github.com/JabRef/jabref/issues/6487)
- We fixed an issue with saving large `.bib` files [#7265](https://github.com/JabRef/jabref/issues/7265)
- We fixed an issue with very large page numbers [#7590](https://github.com/JabRef/jabref/issues/7590)

### Removed

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jabref.logic.citationkeypattern;

import java.math.BigInteger;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -56,6 +57,10 @@ public class BracketedPattern {
*/
private static final int MAX_ALPHA_AUTHORS = 4;

/**
* Matches everything that is not a unicode decimal digit.
*/
private static final Pattern NOT_DECIMAL_DIGIT = Pattern.compile("\\P{Nd}");
/**
* Matches everything that is not an uppercase ASCII letter. The intended use is to remove all lowercase letters
*/
Expand Down Expand Up @@ -1019,19 +1024,12 @@ public static String firstPage(String pages) {
// FIXME: incorrectly exracts the first page when pages are
// specified with ellipse, e.g. "213-6", which should stand
// for "213-216". S.G.
final String[] splitPages = pages.split("\\D+");
int result = Integer.MAX_VALUE;
for (String n : splitPages) {
if (n.matches("\\d+")) {
result = Math.min(Integer.parseInt(n), result);
}
}

if (result == Integer.MAX_VALUE) {
return "";
} else {
return String.valueOf(result);
}
return NOT_DECIMAL_DIGIT.splitAsStream(pages)
.filter(Predicate.not(String::isBlank))
.map(BigInteger::new)
.min(BigInteger::compareTo)
.map(BigInteger::toString)
.orElse("");
}

/**
Expand All @@ -1057,19 +1055,12 @@ public static String pagePrefix(String pages) {
* @throws NullPointerException if pages is null.
*/
public static String lastPage(String pages) {
final String[] splitPages = pages.split("\\D+");
int result = Integer.MIN_VALUE;
for (String n : splitPages) {
if (n.matches("\\d+")) {
result = Math.max(Integer.parseInt(n), result);
}
}

if (result == Integer.MIN_VALUE) {
return "";
} else {
return String.valueOf(result);
}
return NOT_DECIMAL_DIGIT.splitAsStream(pages)
.filter(Predicate.not(String::isBlank))
.map(BigInteger::new)
.max(BigInteger::compareTo)
.map(BigInteger::toString)
.orElse("");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,22 @@ void expandBracketsLastNameWithChineseCharacters() {
assertEquals("杨秀群", BracketedPattern.expandBrackets("[auth]", null, bibEntry, null));
}

@Test
void expandBracketsUnmodifiedStringFromLongFirstPageNumber() {
BibEntry bibEntry = new BibEntry()
.withField(StandardField.PAGES, "2325967120921344");

assertEquals("2325967120921344", BracketedPattern.expandBrackets("[firstpage]", null, bibEntry, null));
}

@Test
void expandBracketsUnmodifiedStringFromLongLastPageNumber() {
BibEntry bibEntry = new BibEntry()
.withField(StandardField.PAGES, "2325967120921344");

assertEquals("2325967120921344", BracketedPattern.expandBrackets("[lastpage]", null, bibEntry, null));
}

@Test
void expandBracketsWithTestCasesFromRegExpBasedFileFinder() {
BibEntry entry = new BibEntry(StandardEntryType.Article).withCitationKey("HipKro03");
Expand Down

0 comments on commit bb011c9

Please sign in to comment.