From ea960b2ce25f9b034d2c16352a38090ae7eb51b4 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 11 Apr 2020 23:25:39 +0800 Subject: [PATCH] parser: fix inter literal format error --- vlib/builtin/string_test.v | 27 +++++++++++++++++++++------ vlib/v/parser/parser.v | 21 +++++++++++---------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/vlib/builtin/string_test.v b/vlib/builtin/string_test.v index 206e692564..eff7536830 100644 --- a/vlib/builtin/string_test.v +++ b/vlib/builtin/string_test.v @@ -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 } } - diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 4840ba6e4c..75dc3480e6 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -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()