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

cgen: fix wrong when string attributes with quotes (fix #15194) (#16020)

This commit is contained in:
shove 2022-10-11 12:48:26 +08:00 committed by GitHub
parent eebc82d83a
commit 34d115d883
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 0 deletions

View File

@ -202,6 +202,9 @@ fn cgen_attrs(attrs []ast.Attr) []string {
if attr.arg.len > 0 {
s += ': $attr.arg'
}
if attr.kind == .string {
s = escape_quotes(s)
}
res << '_SLIT("$s")'
}
return res

View File

@ -0,0 +1,7 @@
FunctionData{
name: 'sample_method'
attrs: ['option: "1"']
args: []
return_type: 1
typ: 0
}

View File

@ -0,0 +1,11 @@
struct Dummy {}
['option: "1"']
fn (d Dummy) sample_method() {
}
fn main() {
$for method in Dummy.methods {
println(method)
}
}

View File

@ -83,3 +83,15 @@ fn (mut g Gen) fn_var_signature(return_type ast.Type, arg_types []ast.Type, var_
sig += ')'
return sig
}
// escape quotes for string
fn escape_quotes(val string) string {
bs := '\\'
unescaped_val := val.replace('$bs$bs', '\x01').replace_each([
"$bs'",
"'",
'$bs"',
'"',
])
return unescaped_val.replace_each(['\x01', '$bs$bs', "'", "$bs'", '"', '$bs"'])
}