mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: optimize auto_str_methods of [str:skip] fields (#15227)
This commit is contained in:
parent
0b0c496eff
commit
03b7c76b38
@ -843,25 +843,23 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, str_fn_name stri
|
|||||||
fn_builder.writeln('\treturn res;')
|
fn_builder.writeln('\treturn res;')
|
||||||
fn_builder.writeln('}')
|
fn_builder.writeln('}')
|
||||||
}
|
}
|
||||||
// find skip fields
|
// find `[str: skip]` fields
|
||||||
mut skip_field_count := 0
|
mut field_skips := []int{}
|
||||||
for field in info.fields {
|
for i, field in info.fields {
|
||||||
if attr := field.attrs.find_first('str') {
|
if attr := field.attrs.find_first('str') {
|
||||||
if attr.arg == 'skip' {
|
if attr.arg == 'skip' {
|
||||||
skip_field_count++
|
field_skips << i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn_body.writeln('\tstring res = str_intp( ${(info.fields.len - skip_field_count) * 4 + 3}, _MOV((StrIntpData[]){')
|
fn_body.writeln('\tstring res = str_intp( ${(info.fields.len - field_skips.len) * 4 + 3}, _MOV((StrIntpData[]){')
|
||||||
fn_body.writeln('\t\t{_SLIT("$clean_struct_v_type_name{\\n"), 0, {.d_c=0}},')
|
fn_body.writeln('\t\t{_SLIT("$clean_struct_v_type_name{\\n"), 0, {.d_c=0}},')
|
||||||
|
mut is_first := true
|
||||||
for i, field in info.fields {
|
for i, field in info.fields {
|
||||||
// Skip `str:skip` fields
|
// Skip `str:skip` fields
|
||||||
if attr := field.attrs.find_first('str') {
|
if i in field_skips {
|
||||||
if attr.arg == 'skip' {
|
continue
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ftyp_noshared := if field.typ.has_flag(.shared_f) {
|
ftyp_noshared := if field.typ.has_flag(.shared_f) {
|
||||||
field.typ.deref().clear_flag(.shared_f)
|
field.typ.deref().clear_flag(.shared_f)
|
||||||
} else {
|
} else {
|
||||||
@ -885,9 +883,10 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, str_fn_name stri
|
|||||||
prefix = 'C'
|
prefix = 'C'
|
||||||
}
|
}
|
||||||
|
|
||||||
// first fields doesn't need \n
|
if is_first {
|
||||||
if i == 0 {
|
// first field doesn't need \n
|
||||||
fn_body.write_string('\t\t{_SLIT0, $c.si_s_code, {.d_s=indents}}, {_SLIT(" $field.name: $ptr_amp$prefix"), 0, {.d_c=0}}, ')
|
fn_body.write_string('\t\t{_SLIT0, $c.si_s_code, {.d_s=indents}}, {_SLIT(" $field.name: $ptr_amp$prefix"), 0, {.d_c=0}}, ')
|
||||||
|
is_first = false
|
||||||
} else {
|
} else {
|
||||||
fn_body.write_string('\t\t{_SLIT("\\n"), $c.si_s_code, {.d_s=indents}}, {_SLIT(" $field.name: $ptr_amp$prefix"), 0, {.d_c=0}}, ')
|
fn_body.write_string('\t\t{_SLIT("\\n"), $c.si_s_code, {.d_s=indents}}, {_SLIT(" $field.name: $ptr_amp$prefix"), 0, {.d_c=0}}, ')
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
struct Foo {
|
struct Foo {
|
||||||
name string
|
|
||||||
age int [str: skip]
|
age int [str: skip]
|
||||||
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
x := Foo{'Peter', 25}
|
x := Foo{25, 'Peter'}
|
||||||
println(x)
|
println(x)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user