Updated on 2026-08-14

This commit is contained in:
Tangem 2024-04-17 15:49:31 +05:00
parent bf9307f71f
commit ec1b9093ec
2 changed files with 17 additions and 6 deletions

View file

@ -12,6 +12,7 @@ import java.util.Locale
private const val TEXT_CHUNK_THOUSAND = 3
private const val POINT_SEPARATOR = '.'
private const val COMMA_SEPARATOR = ','
private const val SCIENTIFIC_NOTATION = 'e'
const val DECIMAL_SEPARATOR_LIMIT = 1
@Composable
@ -162,7 +163,17 @@ fun BigDecimal.parseBigDecimal(decimals: Int, roundingMode: RoundingMode = Round
fun String.parseBigDecimalOrNull() = runCatching {
// Filtering value containing more than one either grouping or decimal separator.
// We assume there will be only decimal separator, otherwise parsing will fail.
if (count { !it.isDigit() } > DECIMAL_SEPARATOR_LIMIT) return null
// Step 1. Exclude formatted (100,000.0) except scientific notation (100.000e10)
val excludeFormatted = this.count {
!it.isDigit() && !it.equals(SCIENTIFIC_NOTATION, ignoreCase = true)
} > DECIMAL_SEPARATOR_LIMIT
// Step 2. Exclude wrong scientific notation (100e100e100)
val excludeWrongScientific = this.count {
it.equals(SCIENTIFIC_NOTATION, ignoreCase = true)
} > DECIMAL_SEPARATOR_LIMIT
if (excludeFormatted || excludeWrongScientific) return null
// An attempt to parse value with POINT decimal separator
val parsed = this.toBigDecimalOrNull()

View file

@ -152,11 +152,6 @@ internal class DefaultQrScanningEventsRepositoryTest {
QrResult(address = address2),
cryptoCurrency,
)
positiveCase(
"$garbage$schema2:$address2$function?$addressParam=$addressParamValue",
QrResult(address = address2),
cryptoCurrency,
)
positiveCase(
"$garbage$schema2:$address2?$someParam=$someParamValue&$valueParam=$someAmountParamValue",
QrResult(address = address2),
@ -177,6 +172,11 @@ internal class DefaultQrScanningEventsRepositoryTest {
QrResult(address = address2, amount = BigDecimal("0.000000023")),
cryptoCurrency,
)
negativeCase(
"$garbage$schema2:$address2$function?$addressParam=$addressParamValue",
QrResult(address = address2),
cryptoCurrency,
)
}
@Test