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

vfmt: keep UnsafeExpr linebreacks as set by developer (#7558)

This commit is contained in:
Lukas Neubert 2020-12-25 15:52:16 +01:00 committed by GitHub
parent 1605c3b5f8
commit 8cd74df2f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 12 deletions

View File

@ -39,8 +39,10 @@ pub fn parse_rfc2822(s string) ?Time {
unsafe {
tmstr = malloc(s.len * 2)
}
count := unsafe { C.snprintf(charptr(tmstr), (s.len * 2), '%s-%02d-%s %s', fields[3].str,
mm, fields[1].str, fields[4].str) }
count := unsafe {
C.snprintf(charptr(tmstr), (s.len * 2), '%s-%02d-%s %s', fields[3].str, mm, fields[1].str,
fields[4].str)
}
return parse(tos(tmstr, count))
}
@ -66,12 +68,16 @@ fn parse_iso8601_time(s string) ?(int, int, int, int, i64, bool) {
plus_min_z := `a`
offset_hour := 0
offset_minute := 0
mut count := unsafe { C.sscanf(charptr(s.str), '%2d:%2d:%2d.%6d%c%2d:%2d', &hour,
&minute, &second, &microsecond, charptr(&plus_min_z), &offset_hour, &offset_minute) }
mut count := unsafe {
C.sscanf(charptr(s.str), '%2d:%2d:%2d.%6d%c%2d:%2d', &hour, &minute, &second,
&microsecond, charptr(&plus_min_z), &offset_hour, &offset_minute)
}
// Missread microsecond ([Sec Hour Minute].len == 3 < 4)
if count < 4 {
count = unsafe { C.sscanf(charptr(s.str), '%2d:%2d:%2d%c%2d:%2d', &hour, &minute,
&second, charptr(&plus_min_z), &offset_hour, &offset_minute) }
count = unsafe {
C.sscanf(charptr(s.str), '%2d:%2d:%2d%c%2d:%2d', &hour, &minute, &second,
charptr(&plus_min_z), &offset_hour, &offset_minute)
}
count++ // Increment count because skipped microsecond
}
if count < 4 {

View File

@ -1111,8 +1111,22 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
f.write(')')
}
ast.UnsafeExpr {
single_line := node.pos.line_nr >= node.pos.last_line
f.write('unsafe {')
if single_line {
f.write(' ')
} else {
f.writeln('')
f.indent++
f.empty_line = true
}
f.expr(node.expr)
if single_line {
f.write(' ')
} else {
f.writeln('')
f.indent--
}
f.write('}')
}
}

View File

@ -0,0 +1,17 @@
fn main() {
unsafe {
println('hi')
println('hi2')
}
unsafe {
println('qwer')
}
unsafe { 6 }
x := unsafe {
5
}
y := unsafe { 7 }
unsafe {}
unsafe {
}
}

View File

@ -2275,6 +2275,7 @@ fn (mut p Parser) unsafe_stmt() ast.Stmt {
// `unsafe {expr}`
if stmt.expr.is_expr() {
p.next()
pos.last_line = p.prev_tok.line_nr - 1
ue := ast.UnsafeExpr{
expr: stmt.expr
pos: pos
@ -2293,7 +2294,6 @@ fn (mut p Parser) unsafe_stmt() ast.Stmt {
for p.tok.kind != .rcbr {
stmts << p.stmt(false)
}
pos.last_line = p.tok.line_nr
p.next()
return ast.Block{
stmts: stmts

View File

@ -103,7 +103,7 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
}
.key_unsafe {
// unsafe {
pos := p.tok.position()
mut pos := p.tok.position()
p.next()
if p.inside_unsafe {
p.error_with_pos('already inside `unsafe` block', pos)
@ -111,11 +111,13 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
}
p.inside_unsafe = true
p.check(.lcbr)
e := p.expr(0)
p.check(.rcbr)
pos.last_line = p.prev_tok.line_nr - 1
node = ast.UnsafeExpr{
expr: p.expr(0)
expr: e
pos: pos
}
p.check(.rcbr)
p.inside_unsafe = false
}
.key_lock, .key_rlock {