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,23 @@
// 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 withIndex
// 0 iterator
// 0 hasNext
// 0 next
// 0 component1
// 0 component2
// 1 length
// 1 charAt
@@ -0,0 +1,17 @@
// WITH_RUNTIME
fun box(): String {
for ((index, x) in "".withIndex()) {
return "Loop over empty String should not be executed"
}
return "OK"
}
// 0 withIndex
// 0 iterator
// 0 hasNext
// 0 next
// 0 component1
// 0 component2
// 1 length
// 1 charAt
@@ -0,0 +1,21 @@
// 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 withIndex
// 0 iterator
// 0 hasNext
// 0 next
// 0 component1
// 0 component2
// 1 length
// 1 charAt
@@ -0,0 +1,23 @@
// 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 withIndex
// 0 iterator
// 0 hasNext
// 0 next
// 0 component1
// 0 component2
// 1 length
// 0 charAt
@@ -0,0 +1,23 @@
// 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 withIndex
// 0 iterator
// 0 hasNext
// 0 next
// 0 component1
// 0 component2
// 1 length
// 1 charAt
@@ -0,0 +1,26 @@
// 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'"
}
// 0 withIndex
// 0 iterator
// 0 hasNext
// 0 next
// 0 component1
// 0 component2
// 1 length
// 1 charAt