Skip to content

Commit

Permalink
v.scanner: fix string interpolation when quote is directly after '}' (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Casper64 committed Jul 24, 2023
1 parent b29f3ca commit fab9157
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 7 additions & 2 deletions vlib/v/scanner/scanner.v
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ pub mut:
is_print_rel_paths_on_error bool
quote u8 // which quote is used to denote current string: ' or "
inter_quote u8
nr_lines int // total number of lines in the source file that were scanned
just_closed_inter bool // if is_enclosed_inter was set to false on the previous character: `}`
nr_lines int // total number of lines in the source file that were scanned
is_vh bool // Keep newlines
is_fmt bool // Used for v fmt.
comments_mode CommentsMode
Expand Down Expand Up @@ -836,6 +837,7 @@ fn (mut s Scanner) text_scan() token.Token {
return s.new_token(.string, '', 1)
}
s.is_enclosed_inter = false
s.just_closed_inter = true
ident_string := s.ident_string()
return s.new_token(.string, ident_string, ident_string.len + 2) // + two quotes
} else {
Expand Down Expand Up @@ -1149,13 +1151,16 @@ fn (mut s Scanner) ident_string() string {
is_quote := q == scanner.single_quote || q == scanner.double_quote
is_raw := is_quote && s.pos > 0 && s.text[s.pos - 1] == `r` && !s.is_inside_string
is_cstr := is_quote && s.pos > 0 && s.text[s.pos - 1] == `c` && !s.is_inside_string
if is_quote {
// don't interpret quote as "start of string" quote when a string interpolation has
// just ended on the previous character meaning it's not the start of a new string
if is_quote && !s.just_closed_inter {
if s.is_inside_string || s.is_enclosed_inter || s.is_inter_start {
s.inter_quote = q
} else {
s.quote = q
}
}
s.just_closed_inter = false
mut n_cr_chars := 0
mut start := s.pos
start_char := s.text[start]
Expand Down
2 changes: 2 additions & 0 deletions vlib/v/tests/string_interpolation_string_args_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ fn test_interpolation_string_args() {

assert '1_${show_more_info('aaa', '111')} 2_${show_more_info('bbb', '222')}' == '1_aaa111 2_bbb222'
assert '1_${show_more_info('aaa', '111')} 2_${show_more_info('bbb', '222')}' == '1_aaa111 2_bbb222'

assert '"${show_info('abc')}"' == '"abc"'
}

0 comments on commit fab9157

Please sign in to comment.