1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix map_get: use zero value if the key was not found

This commit is contained in:
Alexander Medvednikov 2020-03-23 20:02:09 +01:00
parent 80676cf44f
commit 2e29e09b1b
2 changed files with 27 additions and 0 deletions

View File

@ -345,6 +345,25 @@ fn (m map) get2(key string) voidptr {
return voidptr(0) return voidptr(0)
} }
fn (m map) get3(key string, zero voidptr) voidptr {
mut index,mut meta := m.key_to_index(key)
index,meta = meta_less(m.metas, index, meta)
for meta == m.metas[index] {
kv_index := m.metas[index + 1]
if key == m.key_values.data[kv_index].key {
out := malloc(m.value_bytes)
C.memcpy(out, m.key_values.data[kv_index].value, m.value_bytes)
return out
}
index += 2
meta += probe_inc
}
out := malloc(m.value_bytes)
C.memcpy(out, zero, m.value_bytes)
return out
//return voidptr(0)
}
fn (m map) exists(key string) bool { fn (m map) exists(key string) bool {
if m.value_bytes == 0 { if m.value_bytes == 0 {
return false return false

View File

@ -1430,11 +1430,19 @@ fn (g mut Gen) index_expr(node ast.IndexExpr) {
g.write(', &($elem_type_str[]) { ') g.write(', &($elem_type_str[]) { ')
} }
else { else {
/*
g.write('(*($elem_type_str*)map_get2(') g.write('(*($elem_type_str*)map_get2(')
g.expr(node.left) g.expr(node.left)
g.write(', ') g.write(', ')
g.expr(node.index) g.expr(node.index)
g.write('))') g.write('))')
*/
zero := g.type_default(info.value_type)
g.write('(*($elem_type_str*)map_get3(')
g.expr(node.left)
g.write(', ')
g.expr(node.index)
g.write(', &($elem_type_str[]){ $zero }))')
} }
} }
else if sym.kind == .string && !table.type_is_ptr(node.container_type) { else if sym.kind == .string && !table.type_is_ptr(node.container_type) {