cgen: fix string interpolation for non-standard integer types

pull/4547/head
Uwe Krüger 2020-04-22 01:48:53 +02:00 committed by GitHub
parent 34fd148f2f
commit 08fac28c52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -2284,8 +2284,18 @@ fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) {
g.write('%g')
} else if sym.kind == .struct_ && !sym.has_method('str') {
g.write('%.*s')
} else if node.expr_types[i] == table.i16_type {
g.write('%"PRId16"')
} else if node.expr_types[i] == table.u16_type {
g.write('%"PRIu16"')
} else if node.expr_types[i] == table.u32_type {
g.write('%"PRIu32"')
} else if node.expr_types[i] == table.i64_type {
g.write('%"PRId64"')
} else if node.expr_types[i] == table.u64_type {
g.write('%"PRIu64"')
} else {
g.write('%d')
g.write('%"PRId32"')
}
}
g.write('", ')

View File

@ -75,3 +75,15 @@ fn test_string_interpolation_string_prefix() {
jsjs := '$js$js'
assert jsjs == 'jsjs'
}
fn test_inttypes_string_interpolation() {
s := i16(-23456)
us := u16(54321)
i := -1622999040
ui := u32(3421958087)
l := i64(-7694555558525237396)
ul := u64(17234006112912956370)
assert '$s $us' == '-23456 54321'
assert '$ui $i' == '3421958087 -1622999040'
assert '$l $ul' == '-7694555558525237396 17234006112912956370'
}