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
@@ -21,10 +21,16 @@ internal class DigitRangesWriter(private val strategy: RangesWritingStrategy) :
writer.appendLine()
writer.appendLine(binarySearchRange())
writer.appendLine()
writer.appendLine(digitToIntImpl())
writer.appendLine()
writer.appendLine(isDigitImpl())
}
private fun binarySearchRange(): String = """
/**
* 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
@@ -44,17 +50,29 @@ internal class DigitRangesWriter(private val strategy: RangesWritingStrategy) :
}
""".trimIndent()
private fun isDigitImpl(): String {
private fun digitToIntImpl(): String {
val rangeStart = strategy.rangeRef("rangeStart")
return """
/**
* 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($rangeStart, ch)
val diff = ch - $rangeStart[index]
return if (diff < 10) diff else -1
}
""".trimIndent()
}
private fun isDigitImpl(): String {
return """
/**
* Returns `true` if this character is a digit.
*/
internal fun Char.isDigitImpl(): Boolean {
val ch = this.code
val index = binarySearchRange($rangeStart, ch)
val high = $rangeStart[index] + 9
return ch <= high
return digitToIntImpl() >= 0
}
""".trimIndent()
}