Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Correct handling of escaped labels.
  • Loading branch information
johnmay committed Jul 18, 2016
1 parent 38b2f34 commit 241f4fe
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Expand Up @@ -77,8 +77,24 @@ private static boolean processAtomLabels(final CharIter iter, final Map<Integer,
return true;
} else {
final int beg = iter.pos - 1;
int rollback = beg;
while (iter.hasNext()) {
if (iter.curr() == ';' || iter.curr() == '$')

// correct step over of escaped label
if (iter.curr() == '&') {
rollback = iter.pos;
if (iter.nextIf('&') && iter.nextIf('#') && iter.nextIfDigit()) {
while (iter.nextIfDigit()){} // more digits
if (!iter.nextIf(';'))
iter.pos = rollback;
} else {
iter.pos = rollback;
}
}

if (iter.curr() == ';')
break;
if (iter.curr() == '$')
break;
iter.next();
}
Expand Down Expand Up @@ -584,6 +600,13 @@ boolean nextIf(char c) {
return true;
}

boolean nextIfDigit() {
if (!hasNext() || !isDigit(str.charAt(pos)))
return false;
pos++;
return true;
}

/**
* If the next sequence of characters matches the prefix the iterator
* is progressed to character following the prefix.
Expand Down
Expand Up @@ -46,6 +46,13 @@ public void atomLabels() {
assertThat(state.atomLabels, hasEntry(8, "A"));
}

@Test
public void escapedAtomLabels() {
CxSmilesState state = new CxSmilesState();
assertThat(CxSmilesParser.processCx("|$R&#39;;;;;;;$|", state), is(not(-1)));
assertThat(state.atomLabels, hasEntry(0, "R'"));
}

@Test
public void atomValues() {
CxSmilesState state = new CxSmilesState();
Expand Down

0 comments on commit 241f4fe

Please sign in to comment.