Skip to content

Commit

Permalink
Improve braces checking (#2593)
Browse files Browse the repository at this point in the history
* Do not count escaped braces for brace calculation

* Add changelog entry
  • Loading branch information
lenhard authored and tobiasdiez committed Feb 28, 2017
1 parent 0a5c8b7 commit bf12dff
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- Sciencedirect/Elsevier fetcher is now able to scrape new HTML structure [#2576](https://github.com/JabRef/jabref/issues/2576)
- Fixed the synchronization logic of keywords and special fields and vice versa [#2580](https://github.com/JabRef/jabref/issues/2580)
- We fixed an issue where the "find unlinked files" functionality threw an error when only one PDF was imported but not assigned to an entry [#2577](https://github.com/JabRef/jabref/issues/2577)

- We fixed issue where escaped braces were incorrectly counted when calculating brace balance in a field [#2561](https://github.com/JabRef/jabref/issues/2561)

### Removed


Expand Down
18 changes: 13 additions & 5 deletions src/main/java/org/jabref/logic/bibtex/LatexFieldFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,19 @@ private static void checkBraces(String text) throws IllegalArgumentException {
int current = -1;

// First we collect all occurrences:
while ((current = text.indexOf('{', current + 1)) != -1) {
left.add(current);
}
while ((current = text.indexOf('}', current + 1)) != -1) {
right.add(current);
for (int i = 0; i < text.length(); i++) {
char item = text.charAt(i);

boolean charBeforeIsEscape = false;
if(i > 0 && text.charAt(i - 1) == '\\') {
charBeforeIsEscape = true;
}

if(!charBeforeIsEscape && item == '{') {
left.add(current);
} else if (!charBeforeIsEscape && item == '{') {
right.add(current);
}
}

// Then we throw an exception if the error criteria are met.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,18 @@ public void removeWhitespaceFromNonMultiLineFields() throws Exception {
assertEquals(expected, title);
assertEquals(expected, any);
}

@Test(expected = IllegalArgumentException.class)
public void reportUnbalancedBracing() {
String unbalanced = "{";

formatter.format(unbalanced, "anyfield");
}

@Test(expected = IllegalArgumentException.class)
public void reportUnbalancedBracingWithEscapedBraces() {
String unbalanced = "{\\}";

formatter.format(unbalanced, "anyfield");
}
}

0 comments on commit bf12dff

Please sign in to comment.