From facb23f0a53f93bb11cda533519ab7a05e11c918 Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Mon, 13 May 2024 21:17:13 +0200 Subject: [PATCH] Properly fix decimal separator detection logic --- .../card_locker/LoyaltyCardViewActivity.java | 2 +- .../main/java/protect/card_locker/Utils.java | 27 ++++++++++++++----- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java index f1abb7a97..92d5cd7ab 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java @@ -441,7 +441,7 @@ public class LoyaltyCardViewActivity extends CatimaAppCompatActivity implements layout.addView(currentTextview); final TextInputEditText input = new TextInputEditText(this); - input.setInputType(InputType.TYPE_CLASS_NUMBER); + input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); input.setKeyListener(DigitsKeyListener.getInstance("0123456789,.")); input.setHint(R.string.updateBalanceHint); diff --git a/app/src/main/java/protect/card_locker/Utils.java b/app/src/main/java/protect/card_locker/Utils.java index 1fcc80265..7402468b9 100644 --- a/app/src/main/java/protect/card_locker/Utils.java +++ b/app/src/main/java/protect/card_locker/Utils.java @@ -461,16 +461,31 @@ public class Utils { if (numberFormat instanceof DecimalFormat) { // If the string contains both thousand separators and decimals separators, fail hard DecimalFormatSymbols decimalFormatSymbols = ((DecimalFormat) numberFormat).getDecimalFormatSymbols(); - char groupingSeparator = decimalFormatSymbols.getGroupingSeparator(); char decimalSeparator = decimalFormatSymbols.getDecimalSeparator(); - // Replace thousand separator with decimal separators - value = value.replace(groupingSeparator, decimalSeparator); + // Translate all non-digits to decimal separators, failing if we find more than 1. + // We loop over the codepoints to make sure eastern arabic numerals are not mistakenly + // treated as a separator. + boolean separatorFound = false; + StringBuilder translatedValue = new StringBuilder(); + for (int i = 0; i < value.length();) { + int character = value.codePointAt(i); - // if we have more than one separator, fail hard - if (value.indexOf(decimalSeparator) != value.lastIndexOf(decimalSeparator)) { - throw new ParseException("Contains multiple separators", value.indexOf(decimalSeparator)); + if (Character.isDigit(character)) { + translatedValue.append(value.charAt(i)); + } else { + if (separatorFound) { + throw new ParseException("Contains multiple separators", i); + } + + separatorFound = true; + translatedValue.append(decimalSeparator); + } + + i += Character.charCount(character); } + + value = translatedValue.toString(); } }