grouping separator preference

This commit is contained in:
serso 2011-10-23 16:32:36 +04:00
parent 0afb2cd4d8
commit 4577e1e90c
5 changed files with 33 additions and 16 deletions

View File

@ -139,7 +139,11 @@ public class TextHighlighter implements TextProcessor<TextHighlighter.Result> {
private int processHighlightedText(@NotNull StringBuilder result, int i, @NotNull String functionName, @NotNull String tag) {
result.append("<").append(tag).append(">").append(functionName).append("</").append(tag).append(">");
if (functionName.length() > 1) {
return i + functionName.length() - 1;
} else {
return i;
}
}
private int processBracketGroup(@NotNull StringBuilder result, @NotNull String s, int i, int numberOfOpenings, int maxNumberOfGroups) {

View File

@ -36,6 +36,18 @@ public enum MathType {
}
},
grouping_separator(250, false, false, "'", " ") {
@Override
public int processToJscl(@NotNull StringBuilder result, int i, @NotNull String match) {
return i;
}
@Override
public int processFromJscl(@NotNull StringBuilder result, int i, @NotNull String match) {
return i;
}
},
power_10(300, true, false, "E") {
@Override
protected String getSubstituteToJscl(@NotNull String match) {

View File

@ -22,9 +22,6 @@ public class FromJsclSimplifyTextProcessor implements TextProcessor<String> {
final NumberBuilder numberBuilder = new NumberBuilder();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if ( Character.isWhitespace(ch) ) {
continue;
}
MathType.Result mathTypeResult = MathType.getType(s, i);

View File

@ -30,13 +30,14 @@ public class NumberBuilder {
number = null;
final MathType.Result possibleResult;
if (mathTypeResult.getMathType() == MathType.digit || mathTypeResult.getMathType() == MathType.dot || mathTypeResult.getMathType() == MathType.power_10) {
if (CollectionsUtils.contains(mathTypeResult.getMathType(), MathType.digit, MathType.dot, MathType.grouping_separator, MathType.power_10)) {
if (numberBuilder == null) {
numberBuilder = new StringBuilder();
}
numberBuilder.append(mathTypeResult.getMatch());
possibleResult = replaceSystemVars(sb, number, numberOffset);
possibleResult = null;
} else {
possibleResult = process(sb, numberOffset);
}
@ -47,9 +48,16 @@ public class NumberBuilder {
@Nullable
public MathType.Result process(@NotNull StringBuilder sb, @Nullable MutableObject<Integer> numberOffset) {
int numberOfGroupingSeparators = 0;
if (numberBuilder != null) {
try {
number = numberBuilder.toString();
for (String groupingSeparator : MathType.grouping_separator.getTokens()) {
String newNumber = number.replace(groupingSeparator, "");
numberOfGroupingSeparators += number.length() - newNumber.length();
number = newNumber;
}
Double.valueOf(number);
} catch (NumberFormatException e) {
number = null;
@ -60,11 +68,11 @@ public class NumberBuilder {
number = null;
}
return replaceSystemVars(sb, number, numberOffset);
return replaceSystemVars(sb, number, numberOfGroupingSeparators, numberOffset);
}
@Nullable
private MathType.Result replaceSystemVars(StringBuilder sb, String number, @Nullable MutableObject<Integer> numberOffset) {
private MathType.Result replaceSystemVars(StringBuilder sb, String number, int numberOfGroupingSeparators, @Nullable MutableObject<Integer> numberOffset) {
MathType.Result result = null;
if (number != null) {
@ -77,14 +85,14 @@ public class NumberBuilder {
});
if (var != null) {
sb.delete(sb.length() - number.length(), sb.length());
sb.delete(sb.length() - number.length() - numberOfGroupingSeparators, sb.length());
sb.append(var.getName());
result = new MathType.Result(MathType.constant, var.getName());
} else {
sb.delete(sb.length() - number.length(), sb.length());
sb.delete(sb.length() - number.length() - numberOfGroupingSeparators, sb.length());
final String formattedNumber = CalculatorEngine.instance.format(Double.valueOf(number));
if ( numberOffset != null ) {
numberOffset.setObject(formattedNumber.length() - number.length());
numberOffset.setObject(formattedNumber.length() - number.length() - numberOfGroupingSeparators);
}
sb.append(formattedNumber);
}

View File

@ -33,10 +33,6 @@ class ToJsclTextProcessor implements TextProcessor<PreparedExpression> {
for (int i = 0; i < s.length(); i++) {
startsWithFinder.setI(i);
if ( Character.isWhitespace(s.charAt(i))) {
continue;
}
mathTypeBefore = mathTypeResult == null ? null : mathTypeResult.getMathType();
mathTypeResult = MathType.getType(s, i);
@ -123,7 +119,7 @@ class ToJsclTextProcessor implements TextProcessor<PreparedExpression> {
final MathType mathType = MathType.getType(s, result).getMathType();
if (CollectionsUtils.contains(mathType, MathType.digit, MathType.dot)) {
if (CollectionsUtils.contains(mathType, MathType.digit, MathType.dot, MathType.grouping_separator)) {
// continue
} else if (mathType == MathType.close_group_symbol) {
numberOfOpenGroups++;