Provide optimized code generation for for-in-withIndex for CharSequences

#KT-5177 In Progress
This commit is contained in:
Dmitry Petrov
2018-01-19 17:29:43 +03:00
parent 9c9e507172
commit 2399a39414
22 changed files with 610 additions and 6 deletions
@@ -0,0 +1,16 @@
// IGNORE_BACKEND: JS, NATIVE
// WITH_RUNTIME
fun box(): String {
val s = StringBuilder()
val xs = StringBuilder("abcd")
for ((index, x) in xs.withIndex()) {
s.append("$index:$x;")
xs.setLength(0)
}
val ss = s.toString()
return if (ss == "0:a;") "OK" else "fail: '$ss'"
}
@@ -0,0 +1,14 @@
// WITH_RUNTIME
val cs: CharSequence = "abcd"
fun box(): String {
val s = StringBuilder()
for ((index, x) in cs.withIndex()) {
s.append("$index:$x;")
}
val ss = s.toString()
return if (ss == "0:a;1:b;2:c;3:d;") "OK" else "fail: '$ss'"
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
fun box(): String {
for ((index, x) in "".withIndex()) {
return "Loop over empty String should not be executed"
}
return "OK"
}
@@ -0,0 +1,12 @@
// WITH_RUNTIME
fun box(): String {
val s = StringBuilder()
for ((index, x) in "abcd".withIndex()) {
s.append("$index:$x;")
}
val ss = s.toString()
return if (ss == "0:a;1:b;2:c;3:d;") "OK" else "fail: '$ss'"
}
@@ -0,0 +1,14 @@
// WITH_RUNTIME
val xs = "abcd"
fun box(): String {
val s = StringBuilder()
for ((i, _) in xs.withIndex()) {
s.append("$i;")
}
val ss = s.toString()
return if (ss == "0;1;2;3;") "OK" else "fail: '$ss'"
}
@@ -0,0 +1,14 @@
// WITH_RUNTIME
val xs = "abcd"
fun box(): String {
val s = StringBuilder()
for ((_, x) in xs.withIndex()) {
s.append("$x;")
}
val ss = s.toString()
return if (ss == "a;b;c;d;") "OK" else "fail: '$ss'"
}
@@ -0,0 +1,17 @@
// WITH_RUNTIME
val xs = "abcd"
fun useAny(x: Any) {}
fun box(): String {
val s = StringBuilder()
for ((index: Any, x) in xs.withIndex()) {
useAny(index)
s.append("$index:$x;")
}
val ss = s.toString()
return if (ss == "0:a;1:b;2:c;3:d;") "OK" else "fail: '$ss'"
}