[JS IR BE] Covert Char into inline class

This commit is contained in:
Svyatoslav Kuzmich
2019-01-24 18:45:50 +03:00
parent a5f537adc5
commit 2818d3767e
9 changed files with 90 additions and 39 deletions
+1 -1
View File
@@ -32,7 +32,7 @@ internal fun booleanArray(size: Int): BooleanArray = withType("BooleanArray", fi
internal fun booleanArrayOf(arr: Array<Boolean>): BooleanArray = withType("BooleanArray", arr.asDynamic().slice()).unsafeCast<BooleanArray>()
internal fun charArray(size: Int): CharArray = withType("CharArray", fillArrayVal(Array<Char>(size), '\u0000')).unsafeCast<CharArray>()
internal fun charArray(size: Int): CharArray = withType("CharArray", fillArrayVal(Array<Int>(size), 0)).unsafeCast<CharArray>()
internal fun charArrayOf(arr: Array<Char>): CharArray = withType("CharArray", arr.asDynamic().slice()).unsafeCast<CharArray>()
+7 -12
View File
@@ -9,10 +9,9 @@ package kotlin
* Represents a 16-bit Unicode character.
* On the JVM, non-nullable values of this type are represented as values of the primitive type `char`.
*/
public class Char(value: Int) : Comparable<Char> {
private val value = value and 0xFFFF
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
public inline class Char internal constructor (val value: Int) : Comparable<Char> {
/**
* Compares this value with the specified value for order.
* Returns zero if this value is equal to the specified other value, a negative number if it's less than other,
@@ -21,17 +20,17 @@ public class Char(value: Int) : Comparable<Char> {
public override fun compareTo(other: Char): Int = value - other.value
/** Adds the other Int value to this value resulting a Char. */
public operator fun plus(other: Int): Char = Char(value + other)
public operator fun plus(other: Int): Char = (value + other).toChar()
/** Subtracts the other Char value from this value resulting an Int. */
public operator fun minus(other: Char): Int = value - other.value
/** Subtracts the other Int value from this value resulting a Char. */
public operator fun minus(other: Int): Char = Char(value - other)
public operator fun minus(other: Int): Char = (value - other).toChar()
/** Increments this value. */
public operator fun inc(): Char = Char(value + 1)
public operator fun inc(): Char = (value + 1).toChar()
/** Decrements this value. */
public operator fun dec(): Char = Char(value - 1)
public operator fun dec(): Char = (value - 1).toChar()
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Char): CharRange = CharRange(this, other)
@@ -51,10 +50,6 @@ public class Char(value: Int) : Comparable<Char> {
/** Returns the value of this character as a `Double`. */
public fun toDouble(): Double = value.toDouble()
override fun equals(other: Any?): Boolean = other is Char && value == other.value
override fun hashCode(): Int = value
override fun toString(): String {
val value = value
return js("String.fromCharCode(value)").unsafeCast<String>()
@@ -27,4 +27,4 @@ fun doubleToInt(a: dynamic) = js("""
return a | 0;
""").unsafeCast<Int>()
fun numberToChar(a: dynamic) = Char(numberToInt(a))
fun numberToChar(a: dynamic) = Char(numberToInt(a) and 0xFFFF)