Skip to content

Commit

Permalink
Braces checking followup (#2598)
Browse files Browse the repository at this point in the history
* Fix error in braces checking

* Remove unused imports
  • Loading branch information
lenhard authored and tobiasdiez committed Mar 1, 2017
1 parent bf12dff commit 36ccd64
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/main/java/org/jabref/logic/bibtex/LatexFieldFormatter.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.jabref.logic.bibtex;

import java.util.ArrayList;
import java.util.List;

import org.jabref.logic.util.OS;
import org.jabref.model.entry.InternalBibtexFields;
import org.jabref.model.strings.StringUtil;
Expand Down Expand Up @@ -254,9 +251,8 @@ private void putIn(String s) {
}

private static void checkBraces(String text) throws IllegalArgumentException {
List<Integer> left = new ArrayList<>(5);
List<Integer> right = new ArrayList<>(5);
int current = -1;
int left = 0;
int right = 0;

// First we collect all occurrences:
for (int i = 0; i < text.length(); i++) {
Expand All @@ -268,20 +264,20 @@ private static void checkBraces(String text) throws IllegalArgumentException {
}

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

// Then we throw an exception if the error criteria are met.
if (!right.isEmpty() && left.isEmpty()) {
if (!(right == 0) && (left == 0)) {
throw new IllegalArgumentException("'}' character ends string prematurely.");
}
if (!right.isEmpty() && (right.get(0) < left.get(0))) {
if (!(right == 0) && (right < left)) {
throw new IllegalArgumentException("'}' character ends string prematurely.");
}
if (left.size() != right.size()) {
if (left != right) {
throw new IllegalArgumentException("Braces don't match.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,18 @@ public void reportUnbalancedBracingWithEscapedBraces() {

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

@Test
public void tolerateBalancedBrace() {
String text = "Incorporating evolutionary {Measures into Conservation Prioritization}";

assertEquals("{" + text + "}", formatter.format(text, "anyfield"));
}

@Test
public void tolerateEscapeCharacters() {
String text = "Incorporating {\\O}evolutionary {Measures into Conservation Prioritization}";

assertEquals("{" + text + "}", formatter.format(text, "anyfield"));
}
}

0 comments on commit 36ccd64

Please sign in to comment.