Fix for grouping separators outside of numbers

This commit is contained in:
serso 2016-01-16 19:27:56 +01:00
parent 053f52aa25
commit c0710801cd

View File

@ -22,29 +22,27 @@
package org.solovyev.android.calculator.math; package org.solovyev.android.calculator.math;
import jscl.JsclMathEngine;
import jscl.NumeralBase;
import jscl.math.function.Constants;
import org.solovyev.android.calculator.CalculatorParseException; import org.solovyev.android.calculator.CalculatorParseException;
import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.Locator;
import org.solovyev.common.JPredicate; import org.solovyev.common.JPredicate;
import org.solovyev.common.collections.Collections; import org.solovyev.common.collections.Collections;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import jscl.JsclMathEngine;
import jscl.NumeralBase;
import jscl.math.function.Constants;
public enum MathType { public enum MathType {
numeral_base(50, true, false, MathGroupType.number) { numeral_base(50, true, false, MathGroupType.number) {
private final List<String> tokens = new ArrayList<String>(10); private final List<String> tokens = new ArrayList<>(10);
{ {
for (NumeralBase numeralBase : NumeralBase.values()) { for (NumeralBase numeralBase : NumeralBase.values()) {
tokens.add(numeralBase.getJsclPrefix()); tokens.add(numeralBase.getJsclPrefix());
@ -214,13 +212,6 @@ public enum MathType {
return i; return i;
} }
@Override
public int processFromJscl(@Nonnull StringBuilder result, int i, @Nonnull String match) {
if (match.length() > 0) {
result.append(match.charAt(0));
}
return i;
}
}; };
public static final List<String> groupSymbols = Arrays.asList("()", "[]", "{}"); public static final List<String> groupSymbols = Arrays.asList("()", "[]", "{}");
@ -264,7 +255,7 @@ public enum MathType {
* *
* @param text analyzed text * @param text analyzed text
* @param i index which points to start of substring * @param i index which points to start of substring
* @param hexMode * @param hexMode true if current mode if HEX
* @return math entity type of substring starting from ith index of specified text * @return math entity type of substring starting from ith index of specified text
*/ */
@Nonnull @Nonnull
@ -281,17 +272,29 @@ public enum MathType {
for (MathType mathType : getMathTypesByPriority()) { for (MathType mathType : getMathTypesByPriority()) {
final String s = find(mathType.getTokens(), startsWithFinder); final String s = find(mathType.getTokens(), startsWithFinder);
if (s != null) { if (s == null) {
if (s.length() == 1) { continue;
if (hexMode || JsclMathEngine.getInstance().getNumeralBase() == NumeralBase.hex) { }
final Character ch = s.charAt(0);
if (NumeralBase.hex.getAcceptableCharacters().contains(ch)) { if (s.length() > 1) {
return new Result(MathType.digit, s);
}
}
}
return new Result(mathType, s); return new Result(mathType, s);
} }
if (hexMode || JsclMathEngine.getInstance().getNumeralBase() == NumeralBase.hex) {
final Character ch = s.charAt(0);
if (NumeralBase.hex.getAcceptableCharacters().contains(ch)) {
return new Result(MathType.digit, s);
}
}
if (mathType == MathType.grouping_separator) {
if (i + 1 < text.length() && getType(text, i + 1, hexMode).type == MathType.digit) {
return new Result(mathType, s);
}
continue;
}
return new Result(mathType, s);
} }
return new Result(MathType.text, text.substring(i)); return new Result(MathType.text, text.substring(i));
@ -336,10 +339,6 @@ public enum MathType {
return tokens; return tokens;
} }
private boolean isNeedMultiplicationSignBefore() {
return needMultiplicationSignBefore;
}
private boolean isNeedMultiplicationSignAfter() { private boolean isNeedMultiplicationSignAfter() {
return needMultiplicationSignAfter; return needMultiplicationSignAfter;
} }
@ -362,12 +361,6 @@ public enum MathType {
} }
} }
public int processFromJscl(@Nonnull StringBuilder result, int i, @Nonnull String match) {
final String substitute = getSubstituteFromJscl(match);
result.append(substitute == null ? match : substitute);
return returnI(i, match);
}
@Nullable @Nullable
protected String getSubstituteFromJscl(@Nonnull String match) { protected String getSubstituteFromJscl(@Nonnull String match) {
return null; return null;
@ -386,7 +379,7 @@ public enum MathType {
return c == ')' || c == ']' || c == '}'; return c == ')' || c == ']' || c == '}';
} }
public static enum MathGroupType { public enum MathGroupType {
function, function,
number, number,
operation, operation,
@ -410,30 +403,6 @@ public enum MathType {
public int processToJscl(@Nonnull StringBuilder result, int i) throws CalculatorParseException { public int processToJscl(@Nonnull StringBuilder result, int i) throws CalculatorParseException {
return type.processToJscl(result, i, match); return type.processToJscl(result, i, match);
} }
public int processFromJscl(@Nonnull StringBuilder result, int i) {
return type.processFromJscl(result, i, match);
}
}
private static class EndsWithFinder implements JPredicate<String> {
@Nonnull
private final CharSequence targetString;
private int i;
private EndsWithFinder(@Nonnull CharSequence targetString) {
this.targetString = targetString;
}
@Override
public boolean apply(@Nullable String s) {
return targetString.subSequence(0, i).toString().endsWith(s);
}
public void setI(int i) {
this.i = i;
}
} }
private static class StartsWithFinder implements JPredicate<String> { private static class StartsWithFinder implements JPredicate<String> {