cgen: minor optimization to `for ch in "StringLiteral"` (#9240)

pull/9249/head
StunxFS 2021-03-11 09:01:53 -04:00 committed by GitHub
parent a547e889af
commit 8de6511056
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 2 deletions

View File

@ -1509,13 +1509,18 @@ fn (mut g Gen) for_in_stmt(node ast.ForInStmt) {
} }
} }
} else if node.kind == .string { } else if node.kind == .string {
cond := if node.cond is ast.StringLiteral || node.cond is ast.StringInterLiteral {
ast.Expr(g.new_ctemp_var_then_gen(node.cond, table.string_type))
} else {
node.cond
}
i := if node.key_var in ['', '_'] { g.new_tmp_var() } else { node.key_var } i := if node.key_var in ['', '_'] { g.new_tmp_var() } else { node.key_var }
g.write('for (int $i = 0; $i < ') g.write('for (int $i = 0; $i < ')
g.expr(node.cond) g.expr(cond)
g.writeln('.len; ++$i) {') g.writeln('.len; ++$i) {')
if node.val_var != '_' { if node.val_var != '_' {
g.write('\tbyte ${c_name(node.val_var)} = ') g.write('\tbyte ${c_name(node.val_var)} = ')
g.expr(node.cond) g.expr(cond)
g.writeln('.str[$i];') g.writeln('.str[$i];')
} }
} else if node.kind == .struct_ { } else if node.kind == .struct_ {