grouping separator preference

This commit is contained in:
serso 2011-10-23 16:32:36 +04:00
parent c32ec5cd47
commit d9ce5da2fc
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) { 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(">"); result.append("<").append(tag).append(">").append(functionName).append("</").append(tag).append(">");
return i + functionName.length() - 1; 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) { 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") { power_10(300, true, false, "E") {
@Override @Override
protected String getSubstituteToJscl(@NotNull String match) { protected String getSubstituteToJscl(@NotNull String match) {

View File

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

View File

@ -30,13 +30,14 @@ public class NumberBuilder {
number = null; number = null;
final MathType.Result possibleResult; 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) { if (numberBuilder == null) {
numberBuilder = new StringBuilder(); numberBuilder = new StringBuilder();
} }
numberBuilder.append(mathTypeResult.getMatch()); numberBuilder.append(mathTypeResult.getMatch());
possibleResult = replaceSystemVars(sb, number, numberOffset); possibleResult = null;
} else { } else {
possibleResult = process(sb, numberOffset); possibleResult = process(sb, numberOffset);
} }
@ -47,9 +48,16 @@ public class NumberBuilder {
@Nullable @Nullable
public MathType.Result process(@NotNull StringBuilder sb, @Nullable MutableObject<Integer> numberOffset) { public MathType.Result process(@NotNull StringBuilder sb, @Nullable MutableObject<Integer> numberOffset) {
int numberOfGroupingSeparators = 0;
if (numberBuilder != null) { if (numberBuilder != null) {
try { try {
number = numberBuilder.toString(); 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); Double.valueOf(number);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
number = null; number = null;
@ -60,11 +68,11 @@ public class NumberBuilder {
number = null; number = null;
} }
return replaceSystemVars(sb, number, numberOffset); return replaceSystemVars(sb, number, numberOfGroupingSeparators, numberOffset);
} }
@Nullable @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; MathType.Result result = null;
if (number != null) { if (number != null) {
@ -77,14 +85,14 @@ public class NumberBuilder {
}); });
if (var != null) { if (var != null) {
sb.delete(sb.length() - number.length(), sb.length()); sb.delete(sb.length() - number.length() - numberOfGroupingSeparators, sb.length());
sb.append(var.getName()); sb.append(var.getName());
result = new MathType.Result(MathType.constant, var.getName()); result = new MathType.Result(MathType.constant, var.getName());
} else { } 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)); final String formattedNumber = CalculatorEngine.instance.format(Double.valueOf(number));
if ( numberOffset != null ) { if ( numberOffset != null ) {
numberOffset.setObject(formattedNumber.length() - number.length()); numberOffset.setObject(formattedNumber.length() - number.length() - numberOfGroupingSeparators);
} }
sb.append(formattedNumber); sb.append(formattedNumber);
} }

View File

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