cgen: skip deleted map entries in the auto generated .str() method

pull/6808/head
Delyan Angelov 2020-11-13 13:37:11 +02:00
parent a3404a38c6
commit 7feb1742d3
2 changed files with 15 additions and 0 deletions

View File

@ -339,3 +339,17 @@ fn test_mut_map_with_relation_op_in_fn() {
assert 'seven' in m
assert 'eight' in m
}
fn test_map_str_after_delete() {
mut m := {
'first': 1,
'second': 2,
'third': 3,
}
osm := '$m'
m.delete('second')
nsm := '$m'
println('m: $m')
assert osm == "{'first': 1, 'second': 2, 'third': 3}"
assert nsm == "{'first': 1, 'third': 3}"
}

View File

@ -256,6 +256,7 @@ fn (mut g Gen) gen_str_for_map(info table.Map, styp string, str_fn_name string)
g.auto_str_funcs.writeln('\tstrings__Builder sb = strings__new_builder(m.key_values.len*10);')
g.auto_str_funcs.writeln('\tstrings__Builder_write(&sb, tos_lit("{"));')
g.auto_str_funcs.writeln('\tfor (unsigned int i = 0; i < m.key_values.len; ++i) {')
g.auto_str_funcs.writeln('\t\tif (m.key_values.keys[i].str == 0) { continue; }')
g.auto_str_funcs.writeln('\t\tstring key = (*(string*)DenseArray_get(m.key_values, i));')
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, _STR("\'%.*s\\000\'", 2, key));')
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, tos_lit(": "));')