-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from ionspin/152-invalid-string-parsed
Check for base, and check if character belongs to base. Fixes #152
- Loading branch information
Showing
6 changed files
with
31 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,10 @@ package com.ionspin.kotlin.bignum.integer.util | |
* [email protected] | ||
* on 18-Mar-2019 | ||
*/ | ||
fun Char.toDigit(): Int { | ||
return when (this) { | ||
fun Char.toDigit(base: Int = 10): Int { | ||
// Check if number belongs to est used in base | ||
|
||
val digit = when (this) { | ||
in '0'..'9' -> (this - 48).toInt() | ||
in 'a'..'z' -> this - 'a' + 10 | ||
in 'A'..'Z' -> this - 'A' + 10 | ||
|
@@ -32,4 +34,8 @@ fun Char.toDigit(): Int { | |
'.' -> throw NumberFormatException("Invalid digit for radix $this (Possibly a decimal value, which is not supported by BigInteger parser") | ||
else -> throw NumberFormatException("Invalid digit for radix $this") | ||
} | ||
if (digit < 0 || digit >= base) { | ||
throw NumberFormatException("$this is not a valid digit for number system with base $base") | ||
} | ||
return digit | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters