Do not cache "last" value (i.e., size/length) in lowered for-loop
iteration over CharSequences. CharSequences may be mutable (e.g., StringBuilder) and therefore its contents and length can change within the loop.
This commit is contained in:
committed by
max-kammerer
parent
3da3e1cae9
commit
c16b59191b
+16
@@ -0,0 +1,16 @@
|
||||
// KJS_WITH_FULL_RUNTIME
|
||||
// WITH_RUNTIME
|
||||
import kotlin.test.*
|
||||
|
||||
fun box(): String {
|
||||
val sb = StringBuilder("1234")
|
||||
val result = StringBuilder()
|
||||
for (c in sb) {
|
||||
sb.clear()
|
||||
result.append(c)
|
||||
}
|
||||
assertEquals("", sb.toString())
|
||||
assertEquals("1", result.toString())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// KJS_WITH_FULL_RUNTIME
|
||||
// WITH_RUNTIME
|
||||
import kotlin.test.*
|
||||
|
||||
fun box(): String {
|
||||
val sb = StringBuilder("1234")
|
||||
val result = StringBuilder()
|
||||
var ctr = 0
|
||||
for (c in sb) {
|
||||
if (ctr % 2 == 0)
|
||||
sb.append('x')
|
||||
ctr++
|
||||
result.append(c)
|
||||
}
|
||||
assertEquals("1234xxxx", sb.toString())
|
||||
assertEquals("1234xxxx", result.toString())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user