JS: char boxing

This commit is contained in:
Anton Bannykh
2016-12-27 16:11:14 +03:00
parent 55eeb74c08
commit 77aa685496
33 changed files with 438 additions and 59 deletions
+23
View File
@@ -70,4 +70,27 @@ internal fun newThrowable(message: String?, cause: Throwable?): Throwable {
throwable.cause = cause
throwable.name = "Throwable"
return throwable
}
public class BoxedChar(val c: Char) : Comparable<Char> {
override fun equals(other: Any?): Boolean {
return other is BoxedChar && c == other.c
}
override fun hashCode(): Int {
return c.toInt()
}
override fun toString(): String {
return c.toString()
}
override fun compareTo(other: Char): Int {
return c - other
}
@JsName("valueOf")
public fun valueOf(): Int {
return js("this.c")
}
}
+2 -2
View File
@@ -20,10 +20,10 @@ package kotlin.text
public fun Char.isWhitespace(): Boolean = toString().matches("[\\s\\xA0]")
@kotlin.internal.InlineOnly
public inline fun Char.toLowerCase(): Char = asDynamic().toLowerCase()
public inline fun Char.toLowerCase(): Char = js("String.fromCharCode")(this).toLowerCase().charCodeAt(0)
@kotlin.internal.InlineOnly
public inline fun Char.toUpperCase(): Char = asDynamic().toUpperCase()
public inline fun Char.toUpperCase(): Char = js("String.fromCharCode")(this).toUpperCase().charCodeAt(0)
/**
* Returns `true` if this character is a Unicode high-surrogate code unit (also known as leading-surrogate code unit).
@@ -12944,7 +12944,7 @@ public inline fun BooleanArray.asList(): List<Boolean> {
* Returns a [List] that wraps the original array.
*/
public inline fun CharArray.asList(): List<Char> {
return this.unsafeCast<Array<Char>>().asList()
return this.toTypedArray().asList()
}
/**
@@ -13072,7 +13072,7 @@ public fun BooleanArray.copyOf(newSize: Int): BooleanArray {
* Returns new array which is a copy of the original array, resized to the given [newSize].
*/
public fun CharArray.copyOf(newSize: Int): CharArray {
return arrayCopyResize(this, newSize, '\u0000')
return arrayCopyResize(this, newSize, 0)
}
/**
@@ -13494,7 +13494,7 @@ public fun BooleanArray.toTypedArray(): Array<Boolean> {
* Returns a *typed* object array containing all of the elements of this primitive array.
*/
public fun CharArray.toTypedArray(): Array<Char> {
return copyOf().unsafeCast<Array<Char>>()
return Array<Char>(size, { i -> this[i] })
}
/**
+13 -2
View File
@@ -23,7 +23,7 @@ Kotlin.toByte = function (a) {
};
Kotlin.toChar = function (a) {
return String.fromCharCode((((a | 0) % 65536) & 0xFFFF) << 16 >>> 16);
return a & 0xFFFF;
};
Kotlin.numberToLong = function (a) {
@@ -48,4 +48,15 @@ Kotlin.numberToDouble = function (a) {
Kotlin.numberToChar = function (a) {
return Kotlin.toChar(Kotlin.numberToInt(a));
};
};
Kotlin.toBoxedChar = function (a) {
if (a == null) return a;
if (a instanceof Kotlin.BoxedChar) return a;
return new Kotlin.BoxedChar(a);
};
Kotlin.unboxChar = function(a) {
if (a == null) return a;
return Kotlin.toChar(a);
};
+2 -2
View File
@@ -34,11 +34,11 @@ Kotlin.primitiveCompareTo = function (a, b) {
};
Kotlin.charInc = function (value) {
return String.fromCharCode(value.charCodeAt(0)+1);
return Kotlin.toChar(value+1);
};
Kotlin.charDec = function (value) {
return String.fromCharCode(value.charCodeAt(0)-1);
return Kotlin.toChar(value-1);
};
Kotlin.imul = Math.imul || imul;
+1 -1
View File
@@ -128,7 +128,7 @@ Kotlin.isNumber = function (a) {
};
Kotlin.isChar = function (value) {
return (typeof value) == "string" && value.length == 1;
return value instanceof Kotlin.BoxedChar
};
Kotlin.isComparable = function (value) {