cgen: honor `[direct_array_access]` for `a_string[idx]` too

pull/10020/head
Delyan Angelov 2021-05-05 23:43:46 +03:00
parent fc801fd563
commit ab39011fb9
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
1 changed files with 13 additions and 5 deletions

View File

@ -18,11 +18,19 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
} else if sym.kind == .map {
g.index_of_map(node, sym)
} else if sym.kind == .string && !node.left_type.is_ptr() {
g.write('string_at(')
g.expr(node.left)
g.write(', ')
g.expr(node.index)
g.write(')')
is_direct_array_access := g.fn_decl != 0 && g.fn_decl.is_direct_arr
if is_direct_array_access {
g.expr(node.left)
g.write('.str[ ')
g.expr(node.index)
g.write(']')
} else {
g.write('string_at(')
g.expr(node.left)
g.write(', ')
g.expr(node.index)
g.write(')')
}
} else {
g.expr(node.left)
g.write('[')