Deprecate Char-to-Number conversions in stdlib (JVM and JS)

- Int.toChar was left non-deprecated because the replacement is not intrinsic yet.
- Number.toChar was left non-deprecated because otherwise the deprecation propagates to the override, Int.toChar.

KT-23451
This commit is contained in:
Ilya Gorbunov
2021-04-06 15:36:44 +03:00
parent b976cd812a
commit b64b96eee6
59 changed files with 273 additions and 169 deletions
+12
View File
@@ -41,18 +41,30 @@ constructor(code: UShort) : Comparable<Char> {
public operator fun rangeTo(other: Char): CharRange = CharRange(this, other)
/** Returns the value of this character as a `Byte`. */
@Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code.toByte()"))
@DeprecatedSinceKotlin(warningSince = "1.5")
public fun toByte(): Byte = value.toByte()
/** Returns the value of this character as a `Char`. */
public fun toChar(): Char = this
/** Returns the value of this character as a `Short`. */
@Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code.toShort()"))
@DeprecatedSinceKotlin(warningSince = "1.5")
public fun toShort(): Short = value.toShort()
/** Returns the value of this character as a `Int`. */
@Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code"))
@DeprecatedSinceKotlin(warningSince = "1.5")
public fun toInt(): Int = value
/** Returns the value of this character as a `Long`. */
@Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code.toLong()"))
@DeprecatedSinceKotlin(warningSince = "1.5")
public fun toLong(): Long = value.toLong()
/** Returns the value of this character as a `Float`. */
@Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code.toFloat()"))
@DeprecatedSinceKotlin(warningSince = "1.5")
public fun toFloat(): Float = value.toFloat()
/** Returns the value of this character as a `Double`. */
@Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code.toDouble()"))
@DeprecatedSinceKotlin(warningSince = "1.5")
public fun toDouble(): Double = value.toDouble()
override fun equals(other: Any?): Boolean {
@@ -202,6 +202,8 @@ public class Byte private constructor() : Number(), Comparable<Byte> {
* The least significant 8 bits of the resulting `Char` code are the same as the bits of this `Byte` value,
* whereas the most significant 8 bits are filled with the sign bit of this value.
*/
@Deprecated("Direct conversion to Char is deprecated. Use toInt().toChar() or Char constructor instead.", ReplaceWith("this.toInt().toChar()"))
@DeprecatedSinceKotlin(warningSince = "1.5")
public override fun toChar(): Char
/**
* Converts this [Byte] value to [Short].
@@ -447,6 +449,7 @@ public class Short private constructor() : Number(), Comparable<Short> {
* The resulting `Char` code is equal to this value reinterpreted as an unsigned number,
* i.e. it has the same binary representation as this `Short`.
*/
@Deprecated("Direct conversion to Char is deprecated. Use toInt().toChar() or Char constructor instead.", ReplaceWith("this.toInt().toChar()"))
public override fun toChar(): Char
/** Returns this value. */
public override fun toShort(): Short
@@ -966,6 +969,8 @@ public class Float private constructor() : Number(), Comparable<Float> {
*
* The resulting `Char` value is equal to `this.toInt().toChar()`.
*/
@Deprecated("Direct conversion to Char is deprecated. Use toInt().toChar() or Char constructor instead.", ReplaceWith("this.toInt().toChar()"))
@DeprecatedSinceKotlin(warningSince = "1.5")
public override fun toChar(): Char
/**
* Converts this [Float] value to [Short].
@@ -1210,6 +1215,8 @@ public class Double private constructor() : Number(), Comparable<Double> {
*
* The resulting `Char` value is equal to `this.toInt().toChar()`.
*/
@Deprecated("Direct conversion to Char is deprecated. Use toInt().toChar() or Char constructor instead.", ReplaceWith("this.toInt().toChar()"))
@DeprecatedSinceKotlin(warningSince = "1.5")
public override fun toChar(): Char
/**
* Converts this [Double] value to [Short].
+2
View File
@@ -262,6 +262,8 @@ public class Long internal constructor(
public fun inv(): Long = Long(low.inv(), high.inv())
public override fun toByte(): Byte = low.toByte()
@Deprecated("Direct conversion to Char is deprecated. Use toInt().toChar() or Char constructor instead.", ReplaceWith("this.toInt().toChar()"))
@DeprecatedSinceKotlin(warningSince = "1.5")
public override fun toChar(): Char = low.toChar()
public override fun toShort(): Short = low.toShort()
public override fun toInt(): Int = low
@@ -19,7 +19,7 @@ private object Category {
val toBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
val fromBase64 = IntArray(128)
for (i in toBase64.indices) {
fromBase64[toBase64[i].toInt()] = i
fromBase64[toBase64[i].code] = i
}
// rangeStartDiff.length = 1482
@@ -54,7 +54,7 @@ private fun categoryValueFrom(code: Int, ch: Int): Int {
* Returns the Unicode general category of this character as an Int.
*/
internal fun Char.getCategoryValue(): Int {
val ch = this.toInt()
val ch = this.code
val index = binarySearchRange(Category.decodedRangeStart, ch)
val start = Category.decodedRangeStart[index]
@@ -70,7 +70,7 @@ internal fun decodeVarLenBase64(base64: String, fromBase64: IntArray, resultLeng
var int = 0
var shift = 0
for (char in base64) {
val sixBit = fromBase64[char.toInt()]
val sixBit = fromBase64[char.code]
int = int or ((sixBit and 0x1f) shl shift)
if (sixBit < 0x20) {
result[index++] = int
@@ -40,7 +40,7 @@ internal fun binarySearchRange(array: IntArray, needle: Int): Int {
* Returns `true` if this character is a digit.
*/
internal fun Char.isDigitImpl(): Boolean {
val ch = this.toInt()
val ch = this.code
val index = binarySearchRange(Digit.rangeStart, ch)
val high = Digit.rangeStart[index] + 9
return ch <= high
@@ -20,7 +20,7 @@ private object Letter {
val toBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
val fromBase64 = IntArray(128)
for (i in toBase64.indices) {
fromBase64[toBase64[i].toInt()] = i
fromBase64[toBase64[i].code] = i
}
// rangeStartDiff.length = 356
@@ -72,7 +72,7 @@ internal fun Char.isUpperCaseImpl(): Boolean {
* - `0` otherwise.
*/
private fun Char.getLetterType(): Int {
val ch = this.toInt()
val ch = this.code
val index = binarySearchRange(Letter.decodedRangeStart, ch)
val rangeStart = Letter.decodedRangeStart[index]
@@ -13,7 +13,7 @@ package kotlin.text
// 4 ranges totally
@OptIn(ExperimentalStdlibApi::class)
internal fun Char.titlecaseCharImpl(): Char {
val code = this.toInt()
val code = this.code
// Letters repeating <Lu, Lt, Ll> sequence and code of the Lt is a multiple of 3, e.g. <DŽ, Dž, dž>
if (code in 0x01c4..0x01cc || code in 0x01f1..0x01f3) {
return (3 * ((code + 1) / 3)).toChar()
@@ -15,7 +15,7 @@ package kotlin.text
* Returns `true` if this character is a whitespace.
*/
internal fun Char.isWhitespaceImpl(): Boolean {
val ch = this.toInt()
val ch = this.code
return ch in 0x0009..0x000d
|| ch in 0x001c..0x0020
|| ch == 0x00a0