cgen: fix gen_str_for_array_fixed error

pull/4576/head
yuyi 2020-04-24 18:36:45 +08:00 committed by GitHub
parent 288db055ec
commit afa9d42ff0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 15 deletions

View File

@ -3044,6 +3044,8 @@ fn (mut g Gen) gen_str_for_array_fixed(info table.ArrayFixed, styp, str_fn_name
g.auto_str_funcs.writeln('\t\t\tstrings__Builder_write(&sb, ${field_styp}_str(a[i],0));') g.auto_str_funcs.writeln('\t\t\tstrings__Builder_write(&sb, ${field_styp}_str(a[i],0));')
} else if sym.kind in [.f32, .f64] { } else if sym.kind in [.f32, .f64] {
g.auto_str_funcs.writeln('\t\t\tstrings__Builder_write(&sb, _STR("%g", a[i]));') g.auto_str_funcs.writeln('\t\t\tstrings__Builder_write(&sb, _STR("%g", a[i]));')
} else if sym.kind == .string {
g.auto_str_funcs.writeln('\t\t\tstrings__Builder_write(&sb, _STR("\\"%.*s\\"", a[i].len, a[i].str));')
} else { } else {
g.auto_str_funcs.writeln('\t\t\tstrings__Builder_write(&sb, ${field_styp}_str(a[i]));') g.auto_str_funcs.writeln('\t\t\tstrings__Builder_write(&sb, ${field_styp}_str(a[i]));')
} }

View File

@ -1,17 +1,48 @@
fn test_fixed_array_to_string_conversion() { fn test_fixed_array_to_string() {
a := ['1', '2', '3', '4']!! mut a1 := [3]string
assert a.str() == '["1", "2", "3", "4"]' a1[0] = '1'
b := [1, 2, 3, 4]!! a1[1] = '2'
assert b.str() == '[1, 2, 3, 4]' a1[2] = '3'
} assert '$a1' == '["1", "2", "3"]'
fn test_interpolation_fixed_array_to_string() { mut a2 := [2]string
a := ['1', '2', '3']!! a2[0] = 'a'
assert '$a' == '["1", "2", "3"]' a2[1] = 'b'
b := ['a', 'b']!! assert '$a2' == '["a", "b"]'
assert '$b' == '["a", "b"]'
c := [1, 2, 3]!! mut c1 := [3]int
assert '$c' == '[1, 2, 3]' c1[0] = 1
d := [1.1, 2.2, 3.3]!! c1[1] = 2
assert '$d' == '[1.1, 2.2, 3.3]' c1[2] = 3
assert '$c1' == '[1, 2, 3]'
mut c2 := [3]i16
c2[0] = 1
c2[1] = 2
c2[2] = 3
assert '$c2' == '[1, 2, 3]'
mut c3 := [3]i64
c3[0] = 1
c3[1] = 2
c3[2] = 3
assert '$c3' == '[1, 2, 3]'
mut c4 := [3]u64
c4[0] = 1
c4[1] = 2
c4[2] = 3
assert '$c4' == '[1, 2, 3]'
mut d1 := [3]f64
d1[0] = 1.1
d1[1] = 2.2
d1[2] = 3.3
assert '$d1' == '[1.1, 2.2, 3.3]'
mut d2 := [3]f32
d2[0] = 1.1
d2[1] = 2.2
d2[2] = 3.3
assert '$d2' == '[1.1, 2.2, 3.3]'
} }