v.gen.c: free `indents` in autogenerated .str() methods
parent
d820f2da6f
commit
f9c4365dc7
|
@ -17,6 +17,9 @@ pub fn new_builder(initial_size int) Builder {
|
||||||
// write_ptr writes `len` bytes provided byteptr to the accumulated buffer
|
// write_ptr writes `len` bytes provided byteptr to the accumulated buffer
|
||||||
[unsafe]
|
[unsafe]
|
||||||
pub fn (mut b Builder) write_ptr(ptr &byte, len int) {
|
pub fn (mut b Builder) write_ptr(ptr &byte, len int) {
|
||||||
|
if len == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
unsafe { b.push_many(ptr, len) }
|
unsafe { b.push_many(ptr, len) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,6 +30,9 @@ pub fn (mut b Builder) write_b(data byte) {
|
||||||
|
|
||||||
// write implements the Writer interface
|
// write implements the Writer interface
|
||||||
pub fn (mut b Builder) write(data []byte) ?int {
|
pub fn (mut b Builder) write(data []byte) ?int {
|
||||||
|
if data.len == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
b << data
|
b << data
|
||||||
return data.len
|
return data.len
|
||||||
}
|
}
|
||||||
|
@ -85,7 +91,9 @@ pub fn (mut b Builder) writeln(s string) {
|
||||||
// for c in s {
|
// for c in s {
|
||||||
// b.buf << c
|
// b.buf << c
|
||||||
// }
|
// }
|
||||||
|
if s.len > 0 {
|
||||||
unsafe { b.push_many(s.str, s.len) }
|
unsafe { b.push_many(s.str, s.len) }
|
||||||
|
}
|
||||||
// b.buf << []byte(s) // TODO
|
// b.buf << []byte(s) // TODO
|
||||||
b << byte(`\n`)
|
b << byte(`\n`)
|
||||||
}
|
}
|
||||||
|
|
|
@ -231,17 +231,16 @@ fn (mut g Gen) gen_str_for_alias(info ast.Alias, styp string, str_fn_name string
|
||||||
g.auto_str_funcs.writeln('static string ${str_fn_name}($styp it) { return indent_${str_fn_name}(it, 0); }')
|
g.auto_str_funcs.writeln('static string ${str_fn_name}($styp it) { return indent_${str_fn_name}(it, 0); }')
|
||||||
g.type_definitions.writeln('static string indent_${str_fn_name}($styp it, int indent_count); // auto')
|
g.type_definitions.writeln('static string indent_${str_fn_name}($styp it, int indent_count); // auto')
|
||||||
g.auto_str_funcs.writeln('static string indent_${str_fn_name}($styp it, int indent_count) {')
|
g.auto_str_funcs.writeln('static string indent_${str_fn_name}($styp it, int indent_count) {')
|
||||||
g.auto_str_funcs.writeln('\tstring indents = _SLIT("");')
|
g.auto_str_funcs.writeln('\tstring indents = string_repeat(_SLIT(" "), indent_count);')
|
||||||
g.auto_str_funcs.writeln('\tfor (int i = 0; i < indent_count; ++i) {')
|
g.auto_str_funcs.writeln('\tstring tmp_ds = ${parent_str_fn_name}(it);')
|
||||||
g.auto_str_funcs.writeln('\t\tindents = string__plus(indents, _SLIT(" "));')
|
g.auto_str_funcs.writeln('\tstring res = str_intp(3, _MOV((StrIntpData[]){
|
||||||
g.auto_str_funcs.writeln('\t}')
|
|
||||||
|
|
||||||
g.auto_str_funcs.writeln('\treturn str_intp(3, _MOV((StrIntpData[]){
|
|
||||||
{_SLIT0, $c.si_s_code, {.d_s = indents }},
|
{_SLIT0, $c.si_s_code, {.d_s = indents }},
|
||||||
{_SLIT("${clean_type_v_type_name}("), $c.si_s_code, {.d_s = ${parent_str_fn_name}(it) }},
|
{_SLIT("${clean_type_v_type_name}("), $c.si_s_code, {.d_s = tmp_ds }},
|
||||||
{_SLIT(")"), 0, {.d_c = 0 }}
|
{_SLIT(")"), 0, {.d_c = 0 }}
|
||||||
}));\n')
|
}));')
|
||||||
|
g.auto_str_funcs.writeln('\tstring_free(&indents);')
|
||||||
|
g.auto_str_funcs.writeln('\tstring_free(&tmp_ds);')
|
||||||
|
g.auto_str_funcs.writeln('\treturn res;')
|
||||||
g.auto_str_funcs.writeln('}')
|
g.auto_str_funcs.writeln('}')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -81,15 +81,16 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, str_fn_name stri
|
||||||
}
|
}
|
||||||
clean_struct_v_type_name = util.strip_main_name(clean_struct_v_type_name)
|
clean_struct_v_type_name = util.strip_main_name(clean_struct_v_type_name)
|
||||||
// generate ident / indent length = 4 spaces
|
// generate ident / indent length = 4 spaces
|
||||||
g.auto_str_funcs.writeln('\tstring indents = _SLIT("");')
|
|
||||||
g.auto_str_funcs.writeln('\tfor (int i = 0; i < indent_count; ++i) {')
|
|
||||||
g.auto_str_funcs.writeln('\t\tindents = string__plus(indents, _SLIT(" "));')
|
|
||||||
g.auto_str_funcs.writeln('\t}')
|
|
||||||
if info.fields.len == 0 {
|
if info.fields.len == 0 {
|
||||||
g.auto_str_funcs.write_string('\treturn _SLIT("$clean_struct_v_type_name{}");')
|
g.auto_str_funcs.writeln('\treturn _SLIT("$clean_struct_v_type_name{}");')
|
||||||
} else {
|
g.auto_str_funcs.writeln('}')
|
||||||
g.auto_str_funcs.write_string('\treturn str_intp( ${info.fields.len * 4 + 3}, _MOV((StrIntpData[]){\n')
|
g.auto_str_funcs.writeln('')
|
||||||
g.auto_str_funcs.write_string('\t\t{_SLIT("$clean_struct_v_type_name{\\n"), 0, {.d_c=0}},\n')
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
g.auto_str_funcs.writeln('\tstring indents = string_repeat(_SLIT(" "), indent_count);')
|
||||||
|
g.auto_str_funcs.writeln('\tstring res = str_intp( ${info.fields.len * 4 + 3}, _MOV((StrIntpData[]){')
|
||||||
|
g.auto_str_funcs.writeln('\t\t{_SLIT("$clean_struct_v_type_name{\\n"), 0, {.d_c=0}},')
|
||||||
|
|
||||||
for i, field in info.fields {
|
for i, field in info.fields {
|
||||||
mut ptr_amp := if field.typ.is_ptr() { '&' } else { '' }
|
mut ptr_amp := if field.typ.is_ptr() { '&' } else { '' }
|
||||||
|
@ -134,13 +135,11 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, str_fn_name stri
|
||||||
|
|
||||||
// manage reference types can be "nil"
|
// manage reference types can be "nil"
|
||||||
if field.typ.is_ptr() && !(field.typ in ast.charptr_types
|
if field.typ.is_ptr() && !(field.typ in ast.charptr_types
|
||||||
|| field.typ in ast.byteptr_types
|
|| field.typ in ast.byteptr_types || field.typ == ast.voidptr_type_idx) {
|
||||||
|| field.typ == ast.voidptr_type_idx) {
|
|
||||||
g.auto_str_funcs.write_string('isnil(it.${c_name(field.name)})')
|
g.auto_str_funcs.write_string('isnil(it.${c_name(field.name)})')
|
||||||
g.auto_str_funcs.write_string(' ? _SLIT("nil") : ')
|
g.auto_str_funcs.write_string(' ? _SLIT("nil") : ')
|
||||||
// struct, floats and ints have a special case through the _str function
|
// struct, floats and ints have a special case through the _str function
|
||||||
if sym.kind != .struct_ && !field.typ.is_int_valptr()
|
if sym.kind != .struct_ && !field.typ.is_int_valptr() && !field.typ.is_float_valptr() {
|
||||||
&& !field.typ.is_float_valptr() {
|
|
||||||
g.auto_str_funcs.write_string('*')
|
g.auto_str_funcs.write_string('*')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -156,11 +155,12 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, str_fn_name stri
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
g.auto_str_funcs.write_string('}}, {_SLIT("$quote_str"), 0, {.d_c=0}},\n')
|
g.auto_str_funcs.writeln('}}, {_SLIT("$quote_str"), 0, {.d_c=0}},')
|
||||||
}
|
|
||||||
g.auto_str_funcs.write_string('\t\t{_SLIT("\\n"), $si_s_code, {.d_s=indents}}, {_SLIT("}"), 0, {.d_c=0}},\n')
|
|
||||||
g.auto_str_funcs.write_string('\t}));\n')
|
|
||||||
}
|
}
|
||||||
|
g.auto_str_funcs.writeln('\t\t{_SLIT("\\n"), $si_s_code, {.d_s=indents}}, {_SLIT("}"), 0, {.d_c=0}},')
|
||||||
|
g.auto_str_funcs.writeln('\t}));')
|
||||||
|
g.auto_str_funcs.writeln('\tstring_free(&indents);')
|
||||||
|
g.auto_str_funcs.writeln('\treturn res;')
|
||||||
g.auto_str_funcs.writeln('}')
|
g.auto_str_funcs.writeln('}')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue