Support all digit chars on JS and Native #KT-46002

This commit is contained in:
Abduqodiri Qurbonzoda
2021-04-12 19:26:17 +03:00
parent 8481f4a9d2
commit 54994a289f
9 changed files with 112 additions and 73 deletions
@@ -18,6 +18,10 @@ private object Digit {
)
}
/**
* Returns the index of the largest element in [array] smaller or equal to the specified [needle],
* or -1 if [needle] is smaller than the smallest element in [array].
*/
internal fun binarySearchRange(array: IntArray, needle: Int): Int {
var bottom = 0
var top = array.size - 1
@@ -36,12 +40,20 @@ internal fun binarySearchRange(array: IntArray, needle: Int): Int {
return middle - (if (needle < value) 1 else 0)
}
/**
* Returns an integer from 0..9 indicating the digit this character represents,
* or -1 if this character is not a digit.
*/
internal fun Char.digitToIntImpl(): Int {
val ch = this.code
val index = binarySearchRange(Digit.rangeStart, ch)
val diff = ch - Digit.rangeStart[index]
return if (diff < 10) diff else -1
}
/**
* Returns `true` if this character is a digit.
*/
internal fun Char.isDigitImpl(): Boolean {
val ch = this.code
val index = binarySearchRange(Digit.rangeStart, ch)
val high = Digit.rangeStart[index] + 9
return ch <= high
return digitToIntImpl() >= 0
}
@@ -152,5 +152,8 @@ internal actual fun digitOf(char: Char, radix: Int): Int = when {
char >= '0' && char <= '9' -> char - '0'
char >= 'A' && char <= 'Z' -> char - 'A' + 10
char >= 'a' && char <= 'z' -> char - 'a' + 10
else -> -1
char < '\u0080' -> -1
char >= '\uFF21' && char <= '\uFF3A' -> char - '\uFF21' + 10 // full-width latin capital letter
char >= '\uFF41' && char <= '\uFF5A' -> char - '\uFF41' + 10 // full-width latin small letter
else -> char.digitToIntImpl()
}.let { if (it >= radix) -1 else it }