parser: fix inter literal format error

pull/4350/head
yuyi 2020-04-11 23:25:39 +08:00 committed by GitHub
parent df825506fd
commit ea960b2ce2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 16 deletions

View File

@ -397,11 +397,6 @@ fn test_to_num() {
f := '71.5 hasdf'
// QTODO
//assert f.f32() == 71.5
b := 1.52345
mut a := '${b:.03f}'
assert a == '1.523'
num := 7
a = '${num:03d}'
vals := ['9']
assert vals[0].int() == 9
big := '93993993939322'
@ -409,6 +404,27 @@ fn test_to_num() {
assert big.i64() == 93993993939322
}
fn test_inter_format_string() {
float_num := 1.52345
float_num_string := '-${float_num:.03f}-'
assert float_num_string == '-1.523-'
int_num := 7
int_num_string := '-${int_num:03d}-'
assert int_num_string == '-007-'
ch := `a`
ch_string := '-${ch:c}-'
assert ch_string == '-a-'
hex_n := 192
hex_n_string := '-${hex_n:x}-'
assert hex_n_string == '-c0-'
oct_n := 192
oct_n_string := '-${oct_n:o}-'
assert oct_n_string == '-300-'
str := 'abc'
str_string := '-${str:s}-'
assert str_string == '-abc-'
}
fn test_hash() {
s := '10000'
assert s.hash() == 46730161
@ -687,4 +703,3 @@ fn test_split_into_lines() {
assert line == line_content
}
}

View File

@ -1276,16 +1276,17 @@ fn (p mut Parser) string_expr() ast.Expr {
if p.tok.kind == .colon {
efmt << ':'
p.next()
}
// ${num:-2d}
if p.tok.kind == .minus {
efmt << '-'
p.next()
}
// ${num:2d}
if p.tok.kind == .number {
efmt << p.tok.lit
p.next()
// ${num:-2d}
if p.tok.kind == .minus {
efmt << '-'
p.next()
}
// ${num:2d}
if p.tok.kind == .number {
efmt << p.tok.lit
p.next()
}
if p.tok.lit.len == 1 {
efmt << p.tok.lit
p.next()