1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix str() for arrays

This commit is contained in:
yuyi 2020-04-14 01:18:22 +08:00 committed by GitHub
parent a6100be8df
commit fb4cfa72cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 15 deletions

View File

@ -2405,18 +2405,11 @@ fn (g mut Gen) string_inter_literal(node ast.StringInterLiteral) {
verror('only V strings can be formatted with a ${sfmt} format')
}
g.write('%' + sfmt[1..])
} else if node.expr_types[i] in [table.string_type, table.bool_type] || sym.kind ==
.enum_ {
} else if node.expr_types[i] in [table.string_type, table.bool_type, table.f32_type,
table.f64_type] || sym.kind in [.enum_, .array, .array_fixed] {
g.write('%.*s')
} else {
match node.exprs[i] {
ast.EnumVal {
g.write('%.*s')
}
else {
g.write('%d')
}
}
g.write('%d')
}
}
g.write('", ')
@ -2428,21 +2421,25 @@ fn (g mut Gen) string_inter_literal(node ast.StringInterLiteral) {
if fspec == `s` && node.expr_types[i] == table.string_type {
g.expr(expr)
g.write('.str')
} else {
}
else {
g.expr(expr)
}
} else if node.expr_types[i] == table.string_type {
}
else if node.expr_types[i] == table.string_type {
// `name.str, name.len,`
g.expr(expr)
g.write('.len, ')
g.expr(expr)
g.write('.str')
} else if node.expr_types[i] == table.bool_type {
}
else if node.expr_types[i] == table.bool_type {
g.expr(expr)
g.write(' ? 4 : 5, ')
g.expr(expr)
g.write(' ? "true" : "false"')
} else {
}
else {
sym := g.table.get_type_symbol(node.expr_types[i])
if sym.kind == .enum_ {
is_var := match node.exprs[i] {
@ -2475,7 +2472,19 @@ fn (g mut Gen) string_inter_literal(node ast.StringInterLiteral) {
g.enum_expr(expr)
g.write('"')
}
} else {
}
else if node.expr_types[i] in [table.f32_type, table.f64_type, table.array_type,
table.map_type] || sym.kind in [.array, .array_fixed] {
styp := g.typ(node.expr_types[i])
g.write('${styp}_str(')
g.expr(expr)
g.write(')')
g.write('.len, ')
g.write('${styp}_str(')
g.expr(expr)
g.write(').str')
}
else {
g.expr(expr)
}
}

View File

@ -3,3 +3,10 @@ fn test_array_to_string_conversion() {
arr := ['1', '2', '3', '4']
assert arr.str() == expected
}
fn test_interpolation_array_to_string() {
a := ['1', '2', '3']
assert '$a' == '["1", "2", "3"]'
b := ['a', 'b']
assert '$b' == '["a", "b"]'
}