Skip to content

Commit ff4d361

Browse files
authored
Modified breakExtractor to allow more formulas
Modified breakExtractor to allow formulas with more elements after the parenthesis multiplier, and formulas with more than one parenthesis, like (BiO)2(CO3)
1 parent 4a69a00 commit ff4d361

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

tool/formula/src/main/java/org/openscience/cdk/tools/manipulator/MolecularFormulaManipulator.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,25 +1232,40 @@ public static String simplifyMolecularFormula(String formula) {
12321232
* @return Formula with the correction
12331233
*/
12341234
private static String breakExtractor(String formula) {
1235+
boolean started = false;
12351236
boolean finalBreak = false;
12361237
String recentformula = "";
1237-
String multiple = "0";
1238+
String multiple = "";
1239+
String finalformula = "";
12381240
for (int f = 0; f < formula.length(); f++) {
12391241
char thisChar = formula.charAt(f);
1240-
if (thisChar == '(') {
1241-
// start
1242-
} else if (thisChar == ')') {
1243-
// final
1244-
finalBreak = true;
1245-
} else if (!finalBreak) {
1246-
recentformula += thisChar;
1247-
} else {
1248-
multiple += thisChar;
1242+
if (!started) {
1243+
if (thisChar == '(') {
1244+
// start
1245+
started = true;
1246+
}else {
1247+
finalformula += thisChar;
1248+
}
1249+
}else {
1250+
if (thisChar == ')') {
1251+
// final
1252+
finalBreak = true;
1253+
} else if (!finalBreak) {
1254+
recentformula += thisChar;
1255+
} else if ( isDigit(thisChar) ){
1256+
multiple += thisChar;
1257+
} else {
1258+
finalformula += formula.substring(f, formula.length());
1259+
break;
1260+
}
12491261
}
12501262
}
1251-
1252-
String finalformula = muliplier(recentformula, Integer.valueOf(multiple));
1253-
return finalformula;
1263+
finalformula += muliplier(recentformula, multiple.isEmpty() ? 1:Integer.valueOf(multiple));
1264+
1265+
if (finalformula.contains("("))
1266+
return breakExtractor(finalformula);
1267+
else
1268+
return finalformula;
12541269
}
12551270

12561271
/**

0 commit comments

Comments
 (0)