Skip to content

Commit

Permalink
Resolve some problems with token handling and abbreviations.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmay committed Mar 26, 2017
1 parent 55c12fe commit 2e8e586
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
Expand Up @@ -542,7 +542,8 @@ public int compare(String o1, String o2) {
if (nbrSymbol.equals(prev)) {
count++;
} else {
appendGroup(sb, prev, count, count == 0 || countUpper(prev) > 1);
boolean useParen = count == 0 || countUpper(prev) > 1 || (prev != null && nbrSymbol.startsWith(prev));
appendGroup(sb, prev, count, useParen);
prev = nbrSymbol;
count = 1;
}
Expand Down
Expand Up @@ -273,8 +273,11 @@ static void reverse(List<String> tokens) {
String token = tokens.get(i);
if (token.equals("(")) {
tokens.set(i, ")");
tokens.add(i+1, numbers.pop());
i++;
String num = numbers.pop();
if (!num.isEmpty()) {
tokens.add(i + 1, num);
i++;
}
}
else if (token.equals(")")) {
tokens.set(i, "(");
Expand Down Expand Up @@ -358,7 +361,7 @@ static void reduce(List<FormattedText> texts, int from, int to) {
* @return the token is a charge label (+2, -, +, -2)
*/
private static boolean isChargeToken(String token) {
return norm(token.charAt(0)) == '-' || token.charAt(0) == '+';
return token.length() > 0 && (norm(token.charAt(0)) == '-' || token.charAt(0) == '+');
}

/**
Expand Down
Expand Up @@ -133,6 +133,15 @@ public void reversingBrackets() {
assertThat(Joiner.on("").join(tokens), is("H2C(OH2CH2C)N"));
}

@Test
public void reversingFormatPOOHOEt() {
List<String> tokens = new ArrayList<>();
assertTrue(AbbreviationLabel.parse("PO(OH)OEt", tokens));
AbbreviationLabel.reverse(tokens);
AbbreviationLabel.format(tokens);
assertThat(Joiner.on("").join(tokens), is("EtO(HO)OP"));
}

@Test
public void reversingBracketsWithNumbers() {
List<String> tokens = new ArrayList<>();
Expand Down

0 comments on commit 2e8e586

Please sign in to comment.