From c29c4137a11f2ab56f43dee26059db2f5ba675ed Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 23 Oct 2011 16:32:36 +0400 Subject: [PATCH] grouping separator preference --- .../android/calculator/TextHighlighter.java | 6 ++++- .../android/calculator/math/MathType.java | 12 ++++++++++ .../model/FromJsclSimplifyTextProcessor.java | 3 --- .../calculator/model/NumberBuilder.java | 22 +++++++++++++------ .../calculator/model/ToJsclTextProcessor.java | 6 +---- 5 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/solovyev/android/calculator/TextHighlighter.java b/src/main/java/org/solovyev/android/calculator/TextHighlighter.java index f99d7182..1dd1c6f2 100644 --- a/src/main/java/org/solovyev/android/calculator/TextHighlighter.java +++ b/src/main/java/org/solovyev/android/calculator/TextHighlighter.java @@ -139,7 +139,11 @@ public class TextHighlighter implements TextProcessor { private int processHighlightedText(@NotNull StringBuilder result, int i, @NotNull String functionName, @NotNull String tag) { result.append("<").append(tag).append(">").append(functionName).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) { diff --git a/src/main/java/org/solovyev/android/calculator/math/MathType.java b/src/main/java/org/solovyev/android/calculator/math/MathType.java index 3437982a..43e41157 100644 --- a/src/main/java/org/solovyev/android/calculator/math/MathType.java +++ b/src/main/java/org/solovyev/android/calculator/math/MathType.java @@ -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) { diff --git a/src/main/java/org/solovyev/android/calculator/model/FromJsclSimplifyTextProcessor.java b/src/main/java/org/solovyev/android/calculator/model/FromJsclSimplifyTextProcessor.java index 5daf6e38..93077fc4 100644 --- a/src/main/java/org/solovyev/android/calculator/model/FromJsclSimplifyTextProcessor.java +++ b/src/main/java/org/solovyev/android/calculator/model/FromJsclSimplifyTextProcessor.java @@ -22,9 +22,6 @@ public class FromJsclSimplifyTextProcessor implements TextProcessor { 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); diff --git a/src/main/java/org/solovyev/android/calculator/model/NumberBuilder.java b/src/main/java/org/solovyev/android/calculator/model/NumberBuilder.java index 24e7d82c..eaa298fa 100644 --- a/src/main/java/org/solovyev/android/calculator/model/NumberBuilder.java +++ b/src/main/java/org/solovyev/android/calculator/model/NumberBuilder.java @@ -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 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 numberOffset) { + private MathType.Result replaceSystemVars(StringBuilder sb, String number, int numberOfGroupingSeparators, @Nullable MutableObject 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); } diff --git a/src/main/java/org/solovyev/android/calculator/model/ToJsclTextProcessor.java b/src/main/java/org/solovyev/android/calculator/model/ToJsclTextProcessor.java index 1dda86d1..b9c9df67 100644 --- a/src/main/java/org/solovyev/android/calculator/model/ToJsclTextProcessor.java +++ b/src/main/java/org/solovyev/android/calculator/model/ToJsclTextProcessor.java @@ -33,10 +33,6 @@ class ToJsclTextProcessor implements TextProcessor { 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 { 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++;