More thorough workaround for Char.code and Char(Int) crashing (#6419)

This commit is contained in:
SomeTroglodyte 2022-03-25 15:27:52 +01:00 committed by GitHub
parent 0b50715fb3
commit f9b34fd40d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -42,7 +42,7 @@ data class KeyCharAndCode(val char: Char, val code: Int) {
return when {
char == Char.MIN_VALUE -> fixedKeysToString(code)
this == ESC -> "ESC"
char < ' ' -> "Ctrl-" + Char(char.code+64)
char < ' ' -> "Ctrl-" + (char.toCode() + 64).makeChar()
else -> "\"$char\""
}
}
@ -63,16 +63,18 @@ data class KeyCharAndCode(val char: Char, val code: Int) {
/** Guaranteed to be ignored by [KeyPressDispatcher.set] and never to be generated for an actual event, used as fallback to ensure no action is taken */
val UNKNOWN = KeyCharAndCode(Input.Keys.UNKNOWN)
// Kludges because we got crashes: java.lang.NoSuchMethodError: 'int kotlin.CharCodeKt.access$getCode$p(char)'
private fun Char.toCode() =
try { code } catch (ex: Throwable) { null }
?: try { @Suppress("DEPRECATION") toInt() } catch (ex: Throwable) { null }
?: 0
private fun Int.makeChar() =
try { Char(this) } catch (ex: Throwable) { null }
?: try { toChar() } catch (ex: Throwable) { null }
?: Char.MIN_VALUE
/** mini-factory for control codes - case insensitive */
fun ctrl(letter: Char) = KeyCharAndCode(Char(
try {
letter.code and 31
} catch (ex: NoSuchMethodError) {
// Seems to happen on some Ubuntu systems
@Suppress("DEPRECATION")
letter.toInt() and 31
}
), 0)
fun ctrl(letter: Char) = KeyCharAndCode((letter.toCode() and 31).makeChar(),0)
/** mini-factory for control codes from keyCodes */
fun ctrlFromCode(keyCode: Int): KeyCharAndCode {