cgen: % escape in string interpolated literals
* cgen: % escape in c call to _STR * tests: add test for string % escape * Add a test for the % escaping when interpolating inside stringspull/4369/head^2
parent
4449468ca4
commit
5818956cdc
|
@ -2363,7 +2363,7 @@ fn (g mut Gen) string_inter_literal(node ast.StringInterLiteral) {
|
|||
g.write('_STR("')
|
||||
// Build the string with %
|
||||
for i, val in node.vals {
|
||||
escaped_val := val.replace_each(['"', '\\"', '\r\n', '\\n', '\n', '\\n'])
|
||||
escaped_val := val.replace_each(['"', '\\"', '\r\n', '\\n', '\n', '\\n', '%', '%%'])
|
||||
g.write(escaped_val)
|
||||
if i >= node.exprs.len {
|
||||
continue
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
%.*sworldhello
|
|
@ -0,0 +1,6 @@
|
|||
|
||||
fn main() {
|
||||
test := 'hello'
|
||||
hello := 'world'
|
||||
println('%.*s$hello$test')
|
||||
}
|
|
@ -55,3 +55,10 @@ fn test_implicit_str() {
|
|||
text := '$i' + '42'
|
||||
assert text == '4242'
|
||||
}
|
||||
|
||||
fn test_string_interpolation_percent_escaping(){
|
||||
test := 'hello'
|
||||
hello := 'world'
|
||||
x := '%.*s$hello$test |${hello:-30s}|'
|
||||
assert x == '%.*sworldhello |world |'
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue