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 strings
pull/4369/head^2
Daniel Däschle 2020-04-12 15:24:23 +02:00 committed by GitHub
parent 4449468ca4
commit 5818956cdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 1 deletions

View File

@ -2363,7 +2363,7 @@ fn (g mut Gen) string_inter_literal(node ast.StringInterLiteral) {
g.write('_STR("') g.write('_STR("')
// Build the string with % // Build the string with %
for i, val in node.vals { 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) g.write(escaped_val)
if i >= node.exprs.len { if i >= node.exprs.len {
continue continue

View File

@ -0,0 +1 @@
%.*sworldhello

View File

@ -0,0 +1,6 @@
fn main() {
test := 'hello'
hello := 'world'
println('%.*s$hello$test')
}

View File

@ -55,3 +55,10 @@ fn test_implicit_str() {
text := '$i' + '42' text := '$i' + '42'
assert text == '4242' assert text == '4242'
} }
fn test_string_interpolation_percent_escaping(){
test := 'hello'
hello := 'world'
x := '%.*s$hello$test |${hello:-30s}|'
assert x == '%.*sworldhello |world |'
}