Skip to content

Commit ca0ccf5

Browse files
committed
Test case and fix misaligned (when at end of line) MDL int parsing.
1 parent e539781 commit ca0ccf5

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

storage/ctab/src/main/java/org/openscience/cdk/io/MDLV2000Reader.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,8 @@ private static int readMolfileInt(final String line, final int index) {
15191519
default:
15201520
return 0;
15211521
}
1522+
if (index+1 == line.length())
1523+
return sign * result;
15221524
switch ((c = line.charAt(index + 1))) {
15231525
case ' ':
15241526
if (result > 0) return sign * result;
@@ -1542,14 +1544,12 @@ private static int readMolfileInt(final String line, final int index) {
15421544
default:
15431545
return sign * result;
15441546
}
1547+
if (index+2 == line.length())
1548+
return sign * result;
15451549
switch ((c = line.charAt(index + 2))) {
15461550
case ' ':
15471551
if (result > 0) return sign * result;
15481552
break;
1549-
case '-':
1550-
if (result > 0) return sign * result;
1551-
sign = -1;
1552-
break;
15531553
case '0':
15541554
case '1':
15551555
case '2':

storage/ctab/src/test/java/org/openscience/cdk/io/MDLV2000ReaderTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* */
2525
package org.openscience.cdk.io;
2626

27+
import org.hamcrest.CoreMatchers;
2728
import org.junit.Assert;
2829
import org.junit.BeforeClass;
2930
import org.junit.Test;
@@ -40,6 +41,8 @@
4041
import org.openscience.cdk.interfaces.IBond;
4142
import org.openscience.cdk.interfaces.IBond.Order;
4243
import org.openscience.cdk.interfaces.IChemFile;
44+
import org.openscience.cdk.interfaces.IChemObject;
45+
import org.openscience.cdk.interfaces.IChemObjectBuilder;
4346
import org.openscience.cdk.interfaces.IPseudoAtom;
4447
import org.openscience.cdk.interfaces.IStereoElement;
4548
import org.openscience.cdk.interfaces.ITetrahedralChirality;
@@ -51,6 +54,7 @@
5154
import org.openscience.cdk.sgroup.SgroupBracket;
5255
import org.openscience.cdk.sgroup.SgroupKey;
5356
import org.openscience.cdk.sgroup.SgroupType;
57+
import org.openscience.cdk.silent.SilentChemObjectBuilder;
5458
import org.openscience.cdk.tools.ILoggingTool;
5559
import org.openscience.cdk.tools.LoggingToolFactory;
5660
import org.openscience.cdk.tools.manipulator.AtomContainerManipulator;
@@ -1790,4 +1794,23 @@ public void testBadAtomCoordinateFormat() throws Exception {
17901794
final org.openscience.cdk.silent.AtomContainer atomContainer = mdlv2000Reader.read(new org.openscience.cdk.silent.AtomContainer());
17911795
Assert.assertEquals(17, atomContainer.getAtomCount());
17921796
}
1797+
1798+
@Test public void test() throws Exception {
1799+
String input = "\n" +
1800+
"Structure query\n" +
1801+
"\n" +
1802+
" 1 0 0 0 0 0 0 0 0 0999 V2000\n" +
1803+
" 2430.7100 2427.0000 0.0000 C 0 0 0 0 0 0\n" +
1804+
"A 1\n" +
1805+
"Blah\n" +
1806+
"M END";
1807+
IChemObjectBuilder bldr = SilentChemObjectBuilder.getInstance();
1808+
try (MDLV2000Reader mdlr = new MDLV2000Reader(new StringReader(input))) {
1809+
IAtomContainer mol = mdlr.read(bldr.newAtomContainer());
1810+
assertThat(mol.getAtom(0),
1811+
instanceOf(IPseudoAtom.class));
1812+
assertThat(((IPseudoAtom)mol.getAtom(0)).getLabel(),
1813+
is("Blah"));
1814+
}
1815+
}
17931816
}

0 commit comments

Comments
 (0)