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

println: fix a bug with u64 etc and newlines

This commit is contained in:
Alexander Medvednikov 2019-07-04 00:44:57 +02:00
parent 1e32a4cec4
commit 7fdd94fcbb
2 changed files with 23 additions and 31 deletions

View File

@ -695,16 +695,14 @@ fn (p mut Parser) fn_call_args(f *Fn) *Fn {
T := p.table.find_type(typ)
fmt := p.typ_to_fmt(typ)
if fmt != '' {
p.cgen.cur_line = p.cgen.cur_line.replace('println (', '/*opt*/printf ("' + fmt + '", ')
p.cgen.cur_line = p.cgen.cur_line.replace('println (', '/*opt*/printf ("' + fmt + '\\n", ')
continue
}
if T.parent == 'int' {
p.cgen.set_placeholder(ph, 'int_str(')
}
else if typ.ends_with('*') {
if typ.ends_with('*') {
p.cgen.set_placeholder(ph, 'ptr_str(')
p.gen(')')
continue
}
else {
// Make sure this type has a `str()` method
if !T.has_method('str') {
if T.fields.len > 0 {
@ -723,7 +721,6 @@ fn (p mut Parser) fn_call_args(f *Fn) *Fn {
p.error('`$typ` needs to have method `str() string` to be printable')
}
p.cgen.set_placeholder(ph, '${typ}_str(')
}
p.gen(')')
continue
}

View File

@ -2127,20 +2127,15 @@ fn format_str(str string) string {
fn (p mut Parser) typ_to_fmt(typ string) string {
t := p.table.find_type(typ)
if t.parent == 'int' {
if t.is_enum {
return '%d'
}
switch typ {
case 'string': return '%.*s'
case 'ustring': return '%.*s'
case 'byte': return '%d'
case 'int': return '%d'
case 'char': return '%d'
case 'byte': return '%d'
case 'bool': return '%d'
case 'u32': return '%d'
case 'byte', 'int', 'char', 'byte', 'bool', 'u32', 'i32', 'i16', 'u16', 'i8', 'u8': return '%d'
case 'f64', 'f32': return '%f'
case 'i64': return '%lld'
case 'i64', 'u64': return '%lld'
case 'byte*', 'byteptr': return '%s'
// case 'array_string': return '%s'
// case 'array_int': return '%s'