gen: minor optimization in `type_to_fmt()` (#7028)

pull/7037/head
yuyi 2020-11-30 15:35:55 +08:00 committed by GitHub
parent f7cc3d3718
commit 39b46e95a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 8 deletions

View File

@ -5563,22 +5563,16 @@ fn (mut g Gen) gen_str_default(sym table.TypeSymbol, styp string, str_fn_name st
fn (g &Gen) type_to_fmt(typ table.Type) string {
sym := g.table.get_type_symbol(typ)
if (typ.is_int() || typ.is_float()) && typ.is_ptr() {
if typ.is_ptr() && (typ.is_int() || typ.is_float()) {
return '%.*s\\000'
} else if sym.kind in [.struct_, .array, .array_fixed, .map] {
} else if sym.kind in [.struct_, .array, .array_fixed, .map, .bool, .enum_, .sum_type] {
return '%.*s\\000'
} else if sym.kind == .string {
return "\'%.*s\\000\'"
} else if sym.kind == .bool {
return '%.*s\\000'
} else if sym.kind == .enum_ {
return '%.*s\\000'
} else if sym.kind in [.f32, .f64] {
return '%g\\000' // g removes trailing zeros unlike %f
} else if sym.kind == .u64 {
return '%lld\\000'
} else if sym.kind == .sum_type {
return '%.*s\\000'
}
return '%d\\000'
}