From d5cacd1e5f8333318a1eadedb48a9623b3db5af9 Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Thu, 27 May 2021 09:17:51 +0200 Subject: [PATCH] ast: use string builder for StringInterLiteral.str() (#10207) --- vlib/v/ast/str.v | 22 +++++++++++----------- vlib/v/fmt/fmt.v | 4 +++- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/vlib/v/ast/str.v b/vlib/v/ast/str.v index 5c28689c16..537fb8d7ee 100644 --- a/vlib/v/ast/str.v +++ b/vlib/v/ast/str.v @@ -362,26 +362,26 @@ pub fn (x Expr) str() string { return '__offsetof($x.struct_type, $x.field)' } StringInterLiteral { - mut res := []string{} - res << "'" + mut res := strings.new_builder(50) + res.write_string("'") for i, val in x.vals { - res << val + res.write_string(val) if i >= x.exprs.len { break } - res << '$' + res.write_string('$') fspec_str, needs_braces := x.get_fspec_braces(i) if needs_braces { - res << '{' - res << x.exprs[i].str() - res << fspec_str - res << '}' + res.write_string('{') + res.write_string(x.exprs[i].str()) + res.write_string(fspec_str) + res.write_string('}') } else { - res << x.exprs[i].str() + res.write_string(x.exprs[i].str()) } } - res << "'" - return res.join('') + res.write_string("'") + return res.str() } StringLiteral { return "'$x.val'" diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 02538085cc..c75778bd0a 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -2389,7 +2389,6 @@ pub fn (mut f Fmt) string_literal(node ast.StringLiteral) { } pub fn (mut f Fmt) string_inter_literal(node ast.StringInterLiteral) { - // TODO: this code is very similar to ast.Expr.str() mut quote := "'" for val in node.vals { if val.contains('\\"') { @@ -2407,6 +2406,9 @@ pub fn (mut f Fmt) string_inter_literal(node ast.StringInterLiteral) { quote = '"' } } + // TODO: this code is very similar to ast.Expr.str() + // serkonda7: it can not fully be replaced tho as ´f.expr()´ and `ast.Expr.str()` + // work too different for the various exprs that are interpolated f.write(quote) for i, val in node.vals { f.write(val)