Add CharSequence.subSequence

This is done primarily for JVM interoperability, otherwise it's impossible to
inherit from CharSequence there. On JS subSequence at the moment just invokes
substring.

 #KT-5956 Fixed
This commit is contained in:
Alexander Udalov
2014-11-27 16:02:41 +03:00
parent 4d95bcfc7e
commit 8dae1b62dd
14 changed files with 64 additions and 21 deletions
+2
View File
@@ -296,6 +296,7 @@ public final class CharRange : kotlin.Range<kotlin.Char>, kotlin.Progression<kot
public trait CharSequence {
public abstract fun charAt(/*0*/ index: kotlin.Int): kotlin.Char
public abstract fun length(): kotlin.Int
public abstract fun subSequence(/*0*/ start: kotlin.Int, /*1*/ end: kotlin.Int): kotlin.CharSequence
}
public trait Cloneable {
@@ -1433,6 +1434,7 @@ public final class String : kotlin.Comparable<kotlin.String>, kotlin.CharSequenc
public final fun get(/*0*/ index: kotlin.Int): kotlin.Char
public open override /*1*/ fun length(): kotlin.Int
public final fun plus(/*0*/ other: kotlin.Any?): kotlin.String
public open override /*1*/ fun subSequence(/*0*/ start: kotlin.Int, /*1*/ end: kotlin.Int): kotlin.CharSequence
}
public open class Throwable {
@@ -0,0 +1,15 @@
// KT-5956 java.lang.AbstractMethodError: test.Thing.subSequence(II)Ljava/lang/CharSequence
class Thing(val delegate: CharSequence) : CharSequence {
override fun charAt(index: Int): Char {
throw UnsupportedOperationException()
}
override fun length(): Int = 0
override fun subSequence(start: Int, end: Int) = delegate.subSequence(start, end)
}
fun box(): String {
val txt = Thing("hello there")
val s = txt.subSequence(0, 1)
return if ("$s" == "h") "OK" else "Fail: $s"
}
@@ -4508,7 +4508,7 @@ package java {
public open fun reverse(): java.lang.AbstractStringBuilder!
public open fun setCharAt(/*0*/ p0: kotlin.Int, /*1*/ p1: kotlin.Char): kotlin.Unit
public open fun setLength(/*0*/ p0: kotlin.Int): kotlin.Unit
public open fun subSequence(/*0*/ p0: kotlin.Int, /*1*/ p1: kotlin.Int): kotlin.CharSequence!
public open override /*1*/ fun subSequence(/*0*/ start: kotlin.Int, /*1*/ end: kotlin.Int): kotlin.CharSequence!
public open fun substring(/*0*/ p0: kotlin.Int): kotlin.String!
public open fun substring(/*0*/ p0: kotlin.Int, /*1*/ p1: kotlin.Int): kotlin.String!
public abstract override /*2*/ fun toString(): kotlin.String!
@@ -8017,7 +8017,7 @@ package java {
public open fun split(/*0*/ regex: kotlin.String!, /*1*/ limit: kotlin.Int): kotlin.Array<(out) kotlin.String!>!
public open fun startsWith(/*0*/ p0: kotlin.String!): kotlin.Boolean
public open fun startsWith(/*0*/ p0: kotlin.String!, /*1*/ p1: kotlin.Int): kotlin.Boolean
public open fun subSequence(/*0*/ p0: kotlin.Int, /*1*/ p1: kotlin.Int): kotlin.CharSequence!
public open override /*1*/ fun subSequence(/*0*/ start: kotlin.Int, /*1*/ end: kotlin.Int): kotlin.CharSequence!
public open fun substring(/*0*/ p0: kotlin.Int): kotlin.String!
public open fun substring(/*0*/ p0: kotlin.Int, /*1*/ p1: kotlin.Int): kotlin.String!
public open fun toCharArray(): kotlin.CharArray!
@@ -8120,7 +8120,7 @@ package java {
public open override /*1*/ fun reverse(): java.lang.StringBuffer!
public open override /*1*/ fun setCharAt(/*0*/ p0: kotlin.Int, /*1*/ p1: kotlin.Char): kotlin.Unit
public open override /*1*/ fun setLength(/*0*/ p0: kotlin.Int): kotlin.Unit
public open override /*1*/ fun subSequence(/*0*/ p0: kotlin.Int, /*1*/ p1: kotlin.Int): kotlin.CharSequence!
public open override /*2*/ fun subSequence(/*0*/ start: kotlin.Int, /*1*/ end: kotlin.Int): kotlin.CharSequence!
public open override /*1*/ fun substring(/*0*/ p0: kotlin.Int): kotlin.String!
public open override /*1*/ fun substring(/*0*/ p0: kotlin.Int, /*1*/ p1: kotlin.Int): kotlin.String!
public open override /*3*/ fun toString(): kotlin.String!
@@ -8191,7 +8191,7 @@ package java {
public open override /*1*/ fun reverse(): java.lang.StringBuilder!
public open override /*1*/ /*fake_override*/ fun setCharAt(/*0*/ p0: kotlin.Int, /*1*/ p1: kotlin.Char): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun setLength(/*0*/ p0: kotlin.Int): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun subSequence(/*0*/ p0: kotlin.Int, /*1*/ p1: kotlin.Int): kotlin.CharSequence!
public open override /*2*/ /*fake_override*/ fun subSequence(/*0*/ start: kotlin.Int, /*1*/ end: kotlin.Int): kotlin.CharSequence
public open override /*1*/ /*fake_override*/ fun substring(/*0*/ p0: kotlin.Int): kotlin.String!
public open override /*1*/ /*fake_override*/ fun substring(/*0*/ p0: kotlin.Int, /*1*/ p1: kotlin.Int): kotlin.String!
public open override /*3*/ fun toString(): kotlin.String!
@@ -18,5 +18,6 @@ internal final class Sub : kotlin.CharSequence {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public abstract override /*1*/ /*fake_override*/ fun length(): kotlin.Int
public abstract override /*1*/ /*fake_override*/ fun subSequence(/*0*/ start: kotlin.Int, /*1*/ end: kotlin.Int): kotlin.CharSequence
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -6,7 +6,7 @@ public open class C : kotlin.CharSequence {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
java.lang.Override() public open override /*1*/ fun length(): kotlin.Int
java.lang.Override() public open fun subSequence(/*0*/ start: kotlin.Int, /*1*/ end: kotlin.Int): kotlin.CharSequence!
java.lang.Override() public open override /*1*/ fun subSequence(/*0*/ start: kotlin.Int, /*1*/ end: kotlin.Int): kotlin.CharSequence!
java.lang.Override() public open override /*1*/ fun toString(): kotlin.String!
}
@@ -4513,7 +4513,7 @@ package java {
public open fun reverse(): java.lang.AbstractStringBuilder!
public open fun setCharAt(/*0*/ p0: kotlin.Int, /*1*/ p1: kotlin.Char): kotlin.Unit
public open fun setLength(/*0*/ p0: kotlin.Int): kotlin.Unit
public open fun subSequence(/*0*/ p0: kotlin.Int, /*1*/ p1: kotlin.Int): kotlin.CharSequence!
public open override /*1*/ fun subSequence(/*0*/ start: kotlin.Int, /*1*/ end: kotlin.Int): kotlin.CharSequence!
public open fun substring(/*0*/ p0: kotlin.Int): kotlin.String!
public open fun substring(/*0*/ p0: kotlin.Int, /*1*/ p1: kotlin.Int): kotlin.String!
public abstract override /*2*/ fun toString(): kotlin.String!
@@ -8022,7 +8022,7 @@ package java {
public open fun split(/*0*/ regex: kotlin.String!, /*1*/ limit: kotlin.Int): kotlin.Array<(out) kotlin.String!>!
public open fun startsWith(/*0*/ p0: kotlin.String!): kotlin.Boolean
public open fun startsWith(/*0*/ p0: kotlin.String!, /*1*/ p1: kotlin.Int): kotlin.Boolean
public open fun subSequence(/*0*/ p0: kotlin.Int, /*1*/ p1: kotlin.Int): kotlin.CharSequence!
public open override /*1*/ fun subSequence(/*0*/ start: kotlin.Int, /*1*/ end: kotlin.Int): kotlin.CharSequence!
public open fun substring(/*0*/ p0: kotlin.Int): kotlin.String!
public open fun substring(/*0*/ p0: kotlin.Int, /*1*/ p1: kotlin.Int): kotlin.String!
public open fun toCharArray(): kotlin.CharArray!
@@ -8125,7 +8125,7 @@ package java {
public open override /*1*/ fun reverse(): java.lang.StringBuffer!
public open override /*1*/ fun setCharAt(/*0*/ p0: kotlin.Int, /*1*/ p1: kotlin.Char): kotlin.Unit
public open override /*1*/ fun setLength(/*0*/ p0: kotlin.Int): kotlin.Unit
public open override /*1*/ fun subSequence(/*0*/ p0: kotlin.Int, /*1*/ p1: kotlin.Int): kotlin.CharSequence!
public open override /*2*/ fun subSequence(/*0*/ start: kotlin.Int, /*1*/ end: kotlin.Int): kotlin.CharSequence!
public open override /*1*/ fun substring(/*0*/ p0: kotlin.Int): kotlin.String!
public open override /*1*/ fun substring(/*0*/ p0: kotlin.Int, /*1*/ p1: kotlin.Int): kotlin.String!
public open override /*3*/ fun toString(): kotlin.String!
@@ -8196,7 +8196,7 @@ package java {
public open override /*1*/ fun reverse(): java.lang.StringBuilder!
public open override /*1*/ /*fake_override*/ fun setCharAt(/*0*/ p0: kotlin.Int, /*1*/ p1: kotlin.Char): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun setLength(/*0*/ p0: kotlin.Int): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun subSequence(/*0*/ p0: kotlin.Int, /*1*/ p1: kotlin.Int): kotlin.CharSequence!
public open override /*2*/ /*fake_override*/ fun subSequence(/*0*/ start: kotlin.Int, /*1*/ end: kotlin.Int): kotlin.CharSequence
public open override /*1*/ /*fake_override*/ fun substring(/*0*/ p0: kotlin.Int): kotlin.String!
public open override /*1*/ /*fake_override*/ fun substring(/*0*/ p0: kotlin.Int, /*1*/ p1: kotlin.Int): kotlin.String!
public open override /*3*/ fun toString(): kotlin.String!
@@ -6298,6 +6298,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("kt5956.kt")
public void testKt5956() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/strings/kt5956.kt");
doTest(fileName);
}
@TestMetadata("kt881.kt")
public void testKt881() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/strings/kt881.kt");
@@ -20,4 +20,6 @@ public trait CharSequence {
public fun length(): Int
public fun charAt(index: Int): Char
public fun subSequence(start: Int, end: Int): CharSequence
}
+2
View File
@@ -25,5 +25,7 @@ public class String : Comparable<String>, CharSequence {
public override fun charAt(index: Int): Char
public override fun subSequence(start: Int, end: Int): CharSequence
public override fun compareTo(other: String): Int
}
@@ -22,13 +22,12 @@ import com.intellij.openapi.util.io.FileUtil
import org.jetbrains.jet.utils.Printer
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns
import org.jetbrains.jet.lang.descriptors.ClassDescriptor
import java.util.Collections
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor
import org.jetbrains.jet.lang.types.TypeUtils
import org.jetbrains.jet.lang.types.JetType
val DEST_FILE: File = File("compiler/frontend/src/org/jetbrains/jet/lang/evaluate/OperationsMapGenerated.kt")
private val EXCLUDED_FUNCTIONS = listOf("rangeTo", "hashCode", "inc", "dec")
private val EXCLUDED_FUNCTIONS = listOf("rangeTo", "hashCode", "inc", "dec", "subSequence")
fun main(args: Array<String>) {
GeneratorsFileUtil.writeFileIfContentChanged(DEST_FILE, generate())
@@ -56,7 +55,7 @@ fun generate(): String {
for (descriptor in allPrimitiveTypes + builtIns.getString()) {
[suppress("UNCHECKED_CAST")]
val functions = descriptor.getMemberScope(Collections.emptyList()).getDescriptors()
val functions = descriptor.getMemberScope(listOf()).getDescriptors()
.filter { it is FunctionDescriptor && !EXCLUDED_FUNCTIONS.contains(it.getName().asString()) } as List<FunctionDescriptor>
for (function in functions) {
@@ -82,7 +81,7 @@ fun generate(): String {
val (funcName, parameters) = unaryOperationsMapIterator.next()
p.println(
"unaryOperation(",
parameters.map { it.asSrting() }.makeString(", "),
parameters.map { it.asString() }.joinToString(", "),
", ",
"\"$funcName\"",
", { a -> a.${funcName}() }, ",
@@ -105,7 +104,7 @@ fun generate(): String {
val (funcName, parameters) = binaryOperationsMapIterator.next()
p.println(
"binaryOperation(",
parameters.map { it.asSrting() }.makeString(", "),
parameters.map { it.asString() }.joinToString(", "),
", ",
"\"$funcName\"",
", { a, b -> a.${funcName}(b) }, ",
@@ -168,4 +167,4 @@ private fun FunctionDescriptor.getParametersTypes(): List<JetType> {
return list
}
private fun JetType.asSrting(): String = getConstructor().getDeclarationDescriptor()!!.getName().asString().toUpperCase()
private fun JetType.asString(): String = getConstructor().getDeclarationDescriptor()!!.getName().asString().toUpperCase()
@@ -90,4 +90,8 @@ public final class StringTest extends AbstractExpressionTest {
public void testNullableTypeInStringTemplate() throws Exception {
checkFooBoxIsOk();
}
public void testSubSequence() throws Exception {
checkFooBoxIsOk();
}
}
@@ -30,6 +30,7 @@ public final class StringOperationFIF extends CompositeFIF {
// This intrinsic is needed because charAt is a public function taking one parameter and thus its name would be mangled otherwise
add(pattern("kotlin", "CharSequence", "charAt").checkOverridden(), new BuiltInFunctionIntrinsic("charAt"));
add(pattern("kotlin", "CharSequence", "length").checkOverridden(), LENGTH_PROPERTY_INTRINSIC);
add(pattern("kotlin", "CharSequence", "subSequence").checkOverridden(), new BuiltInFunctionIntrinsic("substring"));
add(pattern("kotlin.js", "isEmpty").isExtensionOf("kotlin.CharSequence"), IS_EMPTY_INTRINSIC);
}
}
@@ -0,0 +1,15 @@
package foo
fun box(): String {
val kotlin: String = "kotlin"
if (kotlin.subSequence(0, kotlin.length()) != kotlin) return "Fail 0"
val kot: CharSequence = kotlin.subSequence(0, 3)
if (kot.toString() != "kot") return "Fail 1: $kot"
val tlin = (kotlin : CharSequence).subSequence(2, 6)
if (tlin.toString() != "tlin") return "Fail 2: $tlin"
return "OK"
}
@@ -115,14 +115,10 @@ public fun String.regionMatches(toffset: Int, other: String, ooffset: Int, len:
public fun String.replace(target: CharSequence, replacement: CharSequence): String = (this as java.lang.String).replace(target, replacement)
public fun String.subSequence(beginIndex: Int, endIndex: Int): CharSequence = (this as java.lang.String).subSequence(beginIndex, endIndex)
public fun String.toLowerCase(locale: java.util.Locale): String = (this as java.lang.String).toLowerCase(locale)
public fun String.toUpperCase(locale: java.util.Locale): String = (this as java.lang.String).toUpperCase(locale)
public fun CharSequence.subSequence(start: Int, end: Int): CharSequence? = (this as java.lang.CharSequence).subSequence(start, end)
public val CharSequence.size: Int
get() = this.length
@@ -138,7 +134,7 @@ public fun String.toDouble(): Double = java.lang.Double.parseDouble(this)
public fun String.toCharList(): List<Char> = toCharArray().toList()
public fun CharSequence.get(start: Int, end: Int): CharSequence? = subSequence(start, end)
public fun CharSequence.get(start: Int, end: Int): CharSequence = subSequence(start, end)
public fun String.toByteArray(charset: String): ByteArray = (this as java.lang.String).getBytes(charset)
public fun String.toByteArray(charset: Charset = Charsets.UTF_8): ByteArray = (this as java.lang.String).getBytes(charset)
@@ -156,7 +152,7 @@ public fun String.getBytes(charset: String): ByteArray = (this as java.lang.Stri
* Returns a subsequence specified by given range.
*/
public fun CharSequence.slice(range: IntRange): CharSequence {
return subSequence(range.start, range.end + 1)!! // inclusive
return subSequence(range.start, range.end + 1) // inclusive
}
/**