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:
@@ -695,35 +695,32 @@ fn (p mut Parser) fn_call_args(f *Fn) *Fn {
|
|||||||
T := p.table.find_type(typ)
|
T := p.table.find_type(typ)
|
||||||
fmt := p.typ_to_fmt(typ)
|
fmt := p.typ_to_fmt(typ)
|
||||||
if fmt != '' {
|
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
|
continue
|
||||||
}
|
}
|
||||||
if T.parent == 'int' {
|
if typ.ends_with('*') {
|
||||||
p.cgen.set_placeholder(ph, 'int_str(')
|
|
||||||
}
|
|
||||||
else if typ.ends_with('*') {
|
|
||||||
p.cgen.set_placeholder(ph, 'ptr_str(')
|
p.cgen.set_placeholder(ph, 'ptr_str(')
|
||||||
|
p.gen(')')
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
else {
|
// Make sure this type has a `str()` method
|
||||||
// Make sure this type has a `str()` method
|
if !T.has_method('str') {
|
||||||
if !T.has_method('str') {
|
if T.fields.len > 0 {
|
||||||
if T.fields.len > 0 {
|
mut index := p.cgen.cur_line.len - 1
|
||||||
mut index := p.cgen.cur_line.len - 1
|
for index > 0 && p.cgen.cur_line[index] != ` ` { index-- }
|
||||||
for index > 0 && p.cgen.cur_line[index] != ` ` { index-- }
|
name := p.cgen.cur_line.right(index + 1)
|
||||||
name := p.cgen.cur_line.right(index + 1)
|
if name == '}' {
|
||||||
if name == '}' {
|
p.error('`$typ` needs to have method `str() string` to be printable')
|
||||||
p.error('`$typ` needs to have method `str() string` to be printable')
|
|
||||||
}
|
|
||||||
p.cgen.cur_line = p.cgen.cur_line.left(index)
|
|
||||||
p.create_type_string(T, name)
|
|
||||||
p.cgen.cur_line.replace(typ, '')
|
|
||||||
p.next()
|
|
||||||
return p.fn_call_args(f)
|
|
||||||
}
|
}
|
||||||
p.error('`$typ` needs to have method `str() string` to be printable')
|
p.cgen.cur_line = p.cgen.cur_line.left(index)
|
||||||
|
p.create_type_string(T, name)
|
||||||
|
p.cgen.cur_line.replace(typ, '')
|
||||||
|
p.next()
|
||||||
|
return p.fn_call_args(f)
|
||||||
}
|
}
|
||||||
p.cgen.set_placeholder(ph, '${typ}_str(')
|
p.error('`$typ` needs to have method `str() string` to be printable')
|
||||||
}
|
}
|
||||||
|
p.cgen.set_placeholder(ph, '${typ}_str(')
|
||||||
p.gen(')')
|
p.gen(')')
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@@ -2127,20 +2127,15 @@ fn format_str(str string) string {
|
|||||||
|
|
||||||
fn (p mut Parser) typ_to_fmt(typ string) string {
|
fn (p mut Parser) typ_to_fmt(typ string) string {
|
||||||
t := p.table.find_type(typ)
|
t := p.table.find_type(typ)
|
||||||
if t.parent == 'int' {
|
if t.is_enum {
|
||||||
return '%d'
|
return '%d'
|
||||||
}
|
}
|
||||||
switch typ {
|
switch typ {
|
||||||
case 'string': return '%.*s'
|
case 'string': return '%.*s'
|
||||||
case 'ustring': return '%.*s'
|
case 'ustring': return '%.*s'
|
||||||
case 'byte': return '%d'
|
case 'byte', 'int', 'char', 'byte', 'bool', 'u32', 'i32', 'i16', 'u16', 'i8', 'u8': return '%d'
|
||||||
case 'int': return '%d'
|
|
||||||
case 'char': return '%d'
|
|
||||||
case 'byte': return '%d'
|
|
||||||
case 'bool': return '%d'
|
|
||||||
case 'u32': return '%d'
|
|
||||||
case 'f64', 'f32': return '%f'
|
case 'f64', 'f32': return '%f'
|
||||||
case 'i64': return '%lld'
|
case 'i64', 'u64': return '%lld'
|
||||||
case 'byte*', 'byteptr': return '%s'
|
case 'byte*', 'byteptr': return '%s'
|
||||||
// case 'array_string': return '%s'
|
// case 'array_string': return '%s'
|
||||||
// case 'array_int': return '%s'
|
// case 'array_int': return '%s'
|
||||||
@@ -2213,7 +2208,7 @@ fn (p mut Parser) string_expr() {
|
|||||||
p.next()
|
p.next()
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
f := p.typ_to_fmt(typ)
|
f := p.typ_to_fmt(typ)
|
||||||
if f == '' {
|
if f == '' {
|
||||||
p.error('unhandled sprintf format "$typ" ')
|
p.error('unhandled sprintf format "$typ" ')
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user