diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 6bbb493a85..83f9f87357 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -46,7 +46,7 @@ pub struct string { pub: str byteptr // points to a C style 0 terminated string of bytes. len int // the length of the .str field, excluding the ending 0 byte. It is always equal to strlen(.str). -mut: +mut: is_lit int } // mut: @@ -157,7 +157,7 @@ pub fn cstring_to_vstring(cstr byteptr) string { pub fn (s string) replace_once(rep, with string) string { index := s.index(rep) or { - return s + return s.clone() } return s.substr(0, index) + with + s.substr(index + rep.len, s.len) } diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 87fb8bd2a9..8a5d77910f 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -676,6 +676,15 @@ pub fn (mut f Fmt) prefix_expr_cast_expr(fexpr ast.Expr) { pub fn (f &Fmt) type_to_str(t table.Type) string { mut res := f.table.type_to_str(t) + map_prefix := 'map[string]' + cur_mod := f.cur_mod + '.' + has_map_prefix := res.starts_with(map_prefix) + if has_map_prefix { + res = res.replace(map_prefix, '') + } + no_symbols := res.trim_left('&[]') + should_shorten := no_symbols.starts_with(cur_mod) + // for res.ends_with('_ptr') { // type_ptr => &type res = res[0..res.len - 4] @@ -686,12 +695,18 @@ pub fn (f &Fmt) type_to_str(t table.Type) string { prefix := '[]fixed_' res = res[prefix.len..] last_underscore_idx := res.last_index('_') or { - return '[]' + res.replace(f.cur_mod + '.', '') + return '[]' + if should_shorten { res.replace_once(cur_mod, '') } else { res } } limit := res[last_underscore_idx + 1..] res = '[' + limit + ']' + res[..last_underscore_idx] } - return res.replace(f.cur_mod + '.', '') + if should_shorten { + res = res.replace_once(cur_mod, '') + } + if has_map_prefix { + res = map_prefix + res + } + return res } pub fn (mut f Fmt) expr(node ast.Expr) { diff --git a/vlib/v/fmt/tests/proto_module_importing_vproto_keep.vv b/vlib/v/fmt/tests/proto_module_importing_vproto_keep.vv new file mode 100644 index 0000000000..2d9d842335 --- /dev/null +++ b/vlib/v/fmt/tests/proto_module_importing_vproto_keep.vv @@ -0,0 +1,23 @@ +module proto + +import vproto + +struct Xyz { + x int +} + +struct Abcde { + f1 vproto.Xyz + f2 &vproto.Xyz + f3 []vproto.Xyz + f4 []&vproto.Xyz + f5 map[string]vproto.Xyz + f6 map[string]&vproto.Xyz + // + p1 Xyz + p2 &Xyz + p3 []Xyz + p4 []&Xyz + p5 map[string]Xyz + p6 map[string]&Xyz +}