cgen: fix array/fixed_array_rune_str (#7881)

pull/7900/head
yuyi 2021-01-06 00:16:55 +08:00 committed by GitHub
parent c137a79666
commit 10e7045bee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 14 deletions

View File

@ -214,6 +214,8 @@ fn (mut g Gen) gen_str_for_array(info table.Array, styp string, str_fn_name stri
}
} else if sym.kind in [.f32, .f64] {
g.auto_str_funcs.writeln('\t\tstring x = _STR("%g", 1, it);')
} else if sym.kind == .rune {
g.auto_str_funcs.writeln('\t\tstring x = _STR("`%.*s\\000`", 2, ${elem_str_fn_name}(it));')
} else {
// There is a custom .str() method, so use it.
// NB: we need to take account of whether the user has defined
@ -280,6 +282,8 @@ fn (mut g Gen) gen_str_for_array_fixed(info table.ArrayFixed, styp string, str_f
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, _STR("%g", 1, a[i]));')
} else if sym.kind == .string {
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, _STR("\'%.*s\\000\'", 2, a[i]));')
} else if sym.kind == .rune {
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, _STR("`%.*s\\000`", 2, ${elem_str_fn_name}(a[i])));')
} else {
if (str_method_expects_ptr && is_elem_ptr) || (!str_method_expects_ptr && !is_elem_ptr) {
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, ${elem_str_fn_name}(a[i]));')
@ -328,9 +332,7 @@ fn (mut g Gen) gen_str_for_map(info table.Map, styp string, str_fn_name string)
if key_sym.kind == .string {
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, _STR("\'%.*s\\000\'", 2, key));')
} else if key_sym.kind == .rune {
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, tos3("`"));')
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, ${key_str_fn_name}(key));')
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, tos3("`"));')
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, _STR("`%.*s\\000`", 2, ${key_str_fn_name}(key)));')
} else {
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, ${key_str_fn_name}(key));')
}
@ -346,9 +348,7 @@ fn (mut g Gen) gen_str_for_map(info table.Map, styp string, str_fn_name string)
} else if val_sym.kind in [.f32, .f64] {
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, _STR("%g", 1, it));')
} else if val_sym.kind == .rune {
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, tos3("`"));')
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, ${elem_str_fn_name}(it));')
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, tos3("`"));')
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, _STR("`%.*s\\000`", 2, ${elem_str_fn_name}(it)));')
} else {
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, ${elem_str_fn_name}(it));')
}

View File

@ -42,10 +42,10 @@ fn test_array_of_ints() {
assert '$c2' == '[11, 22, 33]'
}
fn test_array_of_bytes() {
fn test_array_of_runes() {
aa := [`a`, `b`, `c`]
assert aa.str() == '[a, b, c]'
assert '$aa' == '[a, b, c]'
assert aa.str() == '[`a`, `b`, `c`]'
assert '$aa' == '[`a`, `b`, `c`]'
}
fn test_array_of_strings() {
@ -128,10 +128,10 @@ fn test_fixed_array_of_ints() {
assert '$c2' == '[11, 22, 33]'
}
fn test_fixed_array_of_bytes() {
fn test_fixed_array_of_runes() {
aa := [`a`, `b`, `c`]!!
assert aa.str() == '[a, b, c]'
assert '$aa' == '[a, b, c]'
assert aa.str() == '[`a`, `b`, `c`]'
assert '$aa' == '[`a`, `b`, `c`]'
}
fn test_fixed_array_of_strings() {

View File

@ -67,9 +67,9 @@ fn test_array_of_ints_interpolation() {
assert '$c2' == '[11, 22, 33]'
}
fn test_array_of_bytes_interpolation() {
fn test_array_of_runes_interpolation() {
aa := [`a`, `b`, `c`]
assert '$aa' == '[a, b, c]'
assert '$aa' == '[`a`, `b`, `c`]'
}
fn test_array_of_strings_interpolation() {