From 064e35fbc42ee943443fc3b0a5221cb41287f227 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 27 Oct 2022 00:38:08 +0800 Subject: [PATCH] parser: fix $tmpl with single quotes (fix #16154) (#16216) --- vlib/v/parser/tmpl.v | 5 +++- vlib/v/tests/tmpl/template.in | 3 +++ vlib/v/tests/tmpl_with_single_quotes_test.v | 27 +++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/tmpl/template.in create mode 100644 vlib/v/tests/tmpl_with_single_quotes_test.v diff --git a/vlib/v/parser/tmpl.v b/vlib/v/parser/tmpl.v index 64156d3da..58936e847 100644 --- a/vlib/v/parser/tmpl.v +++ b/vlib/v/parser/tmpl.v @@ -80,7 +80,10 @@ fn insert_template_code(fn_name string, tmpl_str_start string, line string) stri round1 := ['\\', '\\\\', r"'", "\\'", r'@', r'$'] round2 := [r'$$', r'\@', r'.$', r'.@'] mut rline := line.replace_each(round1).replace_each(round2) - + comptime_call_str := rline.find_between('\${', '}') + if comptime_call_str.contains("\\'") { + rline = rline.replace(comptime_call_str, comptime_call_str.replace("\\'", r"'")) + } if rline.ends_with('\\') { rline = rline[0..rline.len - 2] + trailing_bs } diff --git a/vlib/v/tests/tmpl/template.in b/vlib/v/tests/tmpl/template.in new file mode 100644 index 000000000..c8771165e --- /dev/null +++ b/vlib/v/tests/tmpl/template.in @@ -0,0 +1,3 @@ +someval: @{s.someval('huh')} + +someotherval: @{s.m['hah']} diff --git a/vlib/v/tests/tmpl_with_single_quotes_test.v b/vlib/v/tests/tmpl_with_single_quotes_test.v new file mode 100644 index 000000000..cb908cfe4 --- /dev/null +++ b/vlib/v/tests/tmpl_with_single_quotes_test.v @@ -0,0 +1,27 @@ +module main + +pub struct SomeThing { +pub: + m map[string]string +} + +fn (s SomeThing) someval(what string) string { + return s.m[what] +} + +fn (s SomeThing) template() string { + return $tmpl('tmpl/template.in') +} + +fn test_tmpl_with_single_quotes() { + mut sm := map[string]string{} + sm['huh'] = 'monkey' + sm['hah'] = 'parrot' + + s := SomeThing{sm} + result := s.template() + println(result) + assert result.trim_space() == 'someval: monkey + +someotherval: parrot' +} -- 2.39.5