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

println: optimize and remove memory leaks

This commit is contained in:
Alexander Medvednikov
2019-07-03 23:53:48 +02:00
parent 5d4d3b838b
commit 1e32a4cec4
3 changed files with 37 additions and 18 deletions

View File

@@ -2149,7 +2149,6 @@ fn (p mut Parser) typ_to_fmt(typ string) string {
if typ.ends_with('*') {
return '%p'
}
p.error('unhandled sprintf format "$typ" ')
}
return ''
}
@@ -2214,17 +2213,22 @@ fn (p mut Parser) string_expr() {
p.next()
}
else {
format += p.typ_to_fmt(typ)
f := p.typ_to_fmt(typ)
if f == '' {
p.error('unhandled sprintf format "$typ" ')
}
format += f
}
}
// println("hello %d", num) optimization.
if p.cgen.nogen {
return
}
// Don't allocate a new string, just print it. TODO HACK PRINT OPT
// println: don't allocate a new string, just print it.
cur_line := p.cgen.cur_line.trim_space()
if cur_line.contains('println(') && p.tok != PLUS && !p.pref.is_prod && !cur_line.contains('string_add') {
p.cgen.cur_line = cur_line.replace('println(', 'printf(')
if cur_line.contains('println (') && p.tok != PLUS &&
!cur_line.contains('string_add') && !cur_line.contains('eprintln') {
p.cgen.cur_line = cur_line.replace('println (', 'printf(')
p.gen('$format\\n$args')
return
}