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:
@@ -68,7 +68,7 @@ class Arrays {
|
||||
fun associateArrayOfPrimitives() {
|
||||
val charCodes = intArrayOf(72, 69, 76, 76, 79)
|
||||
|
||||
val byCharCode = charCodes.associate { it to it.toChar() }
|
||||
val byCharCode = charCodes.associate { it to Char(it) }
|
||||
|
||||
// 76=L only occurs once because only the last pair with the same key gets added
|
||||
assertPrints(byCharCode, "{72=H, 69=E, 76=L, 79=O}")
|
||||
@@ -79,7 +79,7 @@ class Arrays {
|
||||
fun associateArrayOfPrimitivesBy() {
|
||||
val charCodes = intArrayOf(72, 69, 76, 76, 79)
|
||||
|
||||
val byChar = charCodes.associateBy { it.toChar() }
|
||||
val byChar = charCodes.associateBy { Char(it) }
|
||||
|
||||
// L=76 only occurs once because only the last pair with the same key gets added
|
||||
assertPrints(byChar, "{H=72, E=69, L=76, O=79}")
|
||||
@@ -89,7 +89,7 @@ class Arrays {
|
||||
fun associateArrayOfPrimitivesByWithValueTransform() {
|
||||
val charCodes = intArrayOf(65, 65, 66, 67, 68, 69)
|
||||
|
||||
val byUpperCase = charCodes.associateBy({ it.toChar() }, { (it + 32).toChar() })
|
||||
val byUpperCase = charCodes.associateBy({ Char(it) }, { Char(it + 32) })
|
||||
|
||||
// A=a only occurs once because only the last pair with the same key gets added
|
||||
assertPrints(byUpperCase, "{A=a, B=b, C=c, D=d, E=e}")
|
||||
@@ -101,7 +101,7 @@ class Arrays {
|
||||
val byChar = mutableMapOf<Char, Int>()
|
||||
|
||||
assertTrue(byChar.isEmpty())
|
||||
charCodes.associateByTo(byChar) { it.toChar() }
|
||||
charCodes.associateByTo(byChar) { Char(it) }
|
||||
|
||||
assertTrue(byChar.isNotEmpty())
|
||||
// L=76 only occurs once because only the last pair with the same key gets added
|
||||
@@ -113,7 +113,7 @@ class Arrays {
|
||||
val charCodes = intArrayOf(65, 65, 66, 67, 68, 69)
|
||||
|
||||
val byUpperCase = mutableMapOf<Char, Char>()
|
||||
charCodes.associateByTo(byUpperCase, { it.toChar() }, { (it + 32).toChar() } )
|
||||
charCodes.associateByTo(byUpperCase, { Char(it) }, { Char(it + 32) })
|
||||
|
||||
// A=a only occurs once because only the last pair with the same key gets added
|
||||
assertPrints(byUpperCase, "{A=a, B=b, C=c, D=d, E=e}")
|
||||
@@ -124,7 +124,7 @@ class Arrays {
|
||||
val charCodes = intArrayOf(72, 69, 76, 76, 79)
|
||||
|
||||
val byChar = mutableMapOf<Int, Char>()
|
||||
charCodes.associateTo(byChar) { it to it.toChar() }
|
||||
charCodes.associateTo(byChar) { it to Char(it) }
|
||||
|
||||
// 76=L only occurs once because only the last pair with the same key gets added
|
||||
assertPrints(byChar, "{72=H, 69=E, 76=L, 79=O}")
|
||||
|
||||
@@ -35,7 +35,7 @@ class Chars {
|
||||
val chars = listOf('\u0000', '\u000E', '\u0009', '1', 'a')
|
||||
val (isoControls, notIsoControls) = chars.partition { it.isISOControl() }
|
||||
// some ISO-control char codes
|
||||
assertPrints(isoControls.map(Char::toInt), "[0, 14, 9]")
|
||||
assertPrints(isoControls.map(Char::code), "[0, 14, 9]")
|
||||
// non-ISO-control chars
|
||||
assertPrints(notIsoControls, "[1, a]")
|
||||
}
|
||||
@@ -61,7 +61,7 @@ class Chars {
|
||||
val chars = listOf(' ', '\t', '\n', '1', 'a', '\u00A0')
|
||||
val (whitespaces, notWhitespaces) = chars.partition { it.isWhitespace() }
|
||||
// whitespace char codes
|
||||
assertPrints(whitespaces.map(Char::toInt), "[32, 9, 10, 160]")
|
||||
assertPrints(whitespaces.map(Char::code), "[32, 9, 10, 160]")
|
||||
// non-whitespace chars
|
||||
assertPrints(notWhitespaces, "[1, a]")
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ class Strings {
|
||||
fun associate() {
|
||||
val string = "bonne journée"
|
||||
// associate each character with its code
|
||||
val result = string.associate { char -> char to char.toInt() }
|
||||
val result = string.associate { char -> char to char.code }
|
||||
// notice each letter occurs only once
|
||||
assertPrints(result, "{b=98, o=111, n=110, e=101, =32, j=106, u=117, r=114, é=233}")
|
||||
}
|
||||
@@ -135,7 +135,7 @@ class Strings {
|
||||
fun associateBy() {
|
||||
val string = "bonne journée"
|
||||
// associate each character by its code
|
||||
val result = string.associateBy { char -> char.toInt() }
|
||||
val result = string.associateBy { char -> char.code }
|
||||
// notice each char code occurs only once
|
||||
assertPrints(result, "{98=b, 111=o, 110=n, 101=e, 32= , 106=j, 117=u, 114=r, 233=é}")
|
||||
}
|
||||
@@ -144,7 +144,7 @@ class Strings {
|
||||
fun associateByWithValueTransform() {
|
||||
val string = "bonne journée"
|
||||
// associate each character by the code of its upper case equivalent and transform the character to upper case
|
||||
val result = string.associateBy({ char -> char.toUpperCase().toInt() }, { char -> char.toUpperCase() })
|
||||
val result = string.associateBy({ char -> char.uppercaseChar().code }, { char -> char.uppercaseChar() })
|
||||
// notice each char code occurs only once
|
||||
assertPrints(result, "{66=B, 79=O, 78=N, 69=E, 32= , 74=J, 85=U, 82=R, 201=É}")
|
||||
}
|
||||
@@ -154,7 +154,7 @@ class Strings {
|
||||
val string = "bonne journée"
|
||||
// associate each character by its code
|
||||
val result = mutableMapOf<Int, Char>()
|
||||
string.associateByTo(result) { char -> char.toInt() }
|
||||
string.associateByTo(result) { char -> char.code }
|
||||
// notice each char code occurs only once
|
||||
assertPrints(result, "{98=b, 111=o, 110=n, 101=e, 32= , 106=j, 117=u, 114=r, 233=é}")
|
||||
}
|
||||
@@ -164,7 +164,7 @@ class Strings {
|
||||
val string = "bonne journée"
|
||||
// associate each character by the code of its upper case equivalent and transform the character to upper case
|
||||
val result = mutableMapOf<Int, Char>()
|
||||
string.associateByTo(result, { char -> char.toUpperCase().toInt() }, { char -> char.toUpperCase() })
|
||||
string.associateByTo(result, { char -> char.uppercaseChar().code }, { char -> char.uppercaseChar() })
|
||||
// notice each char code occurs only once
|
||||
assertPrints(result, "{66=B, 79=O, 78=N, 69=E, 32= , 74=J, 85=U, 82=R, 201=É}")
|
||||
}
|
||||
@@ -174,7 +174,7 @@ class Strings {
|
||||
val string = "bonne journée"
|
||||
// associate each character with its code
|
||||
val result = mutableMapOf<Char, Int>()
|
||||
string.associateTo(result) { char -> char to char.toInt() }
|
||||
string.associateTo(result) { char -> char to char.code }
|
||||
// notice each letter occurs only once
|
||||
assertPrints(result, "{b=98, o=111, n=110, e=101, =32, j=106, u=117, r=114, é=233}")
|
||||
}
|
||||
@@ -183,7 +183,7 @@ class Strings {
|
||||
fun associateWith() {
|
||||
val string = "bonne journée"
|
||||
// associate each character with its code
|
||||
val result = string.associateWith { char -> char.toInt() }
|
||||
val result = string.associateWith { char -> char.code }
|
||||
// notice each letter occurs only once
|
||||
assertPrints(result, "{b=98, o=111, n=110, e=101, =32, j=106, u=117, r=114, é=233}")
|
||||
}
|
||||
@@ -193,7 +193,7 @@ class Strings {
|
||||
val string = "bonne journée"
|
||||
// associate each character with its code
|
||||
val result = mutableMapOf<Char, Int>()
|
||||
string.associateWithTo(result) { char -> char.toInt() }
|
||||
string.associateWithTo(result) { char -> char.code }
|
||||
// notice each letter occurs only once
|
||||
assertPrints(result, "{b=98, o=111, n=110, e=101, =32, j=106, u=117, r=114, é=233}")
|
||||
}
|
||||
@@ -414,7 +414,7 @@ class Strings {
|
||||
@Sample
|
||||
fun map() {
|
||||
val string = "kotlin"
|
||||
assertPrints(string.map { it.toUpperCase() }, "[K, O, T, L, I, N]")
|
||||
assertPrints(string.map { it.uppercaseChar() }, "[K, O, T, L, I, N]")
|
||||
}
|
||||
|
||||
@Sample
|
||||
|
||||
Reference in New Issue
Block a user