@@ -126,16 +126,34 @@ remove_space_after_unary_pm_nested <- function(pd) {
126126# ' '")
127127# ' @importFrom purrr map map_chr
128128# ' @param pd_flat A flat parse table.
129+ # ' @importFrom rlang is_empty
129130# ' @keywords internal
130131fix_quotes <- function (pd_flat ) {
131- str_const <- pd_flat $ token == " STR_CONST"
132- str_const_change <- grepl(" ^'([^\" ]*)'$" , pd_flat $ text [str_const ])
133- pd_flat $ text [str_const ][str_const_change ] <-
134- map(pd_flat $ text [str_const ][str_const_change ], parse_text ) %> %
135- map_chr(~ paste0(" \" " , .x , " \" " ))
132+ str_const <- which(pd_flat $ token == " STR_CONST" )
133+ if (is_empty(str_const )) {
134+ return (pd_flat )
135+ }
136+
137+ pd_flat $ text [str_const ] <- map(pd_flat $ text [str_const ], fix_quotes_one )
136138 pd_flat
137139}
138140
141+ # ' @importFrom rlang is_empty
142+ fix_quotes_one <- function (x ) {
143+ rx <- " ^'([^\" ]*)'$"
144+ i <- grep(rx , x )
145+ if (is_empty(i )) {
146+ return (x )
147+ }
148+
149+ # replace outer single quotes
150+ xi <- gsub(rx , ' "\\ 1"' , x [i ])
151+
152+ # Replace inner escaped quotes (\') by ' and keep all other instances of \., including \\
153+ x [i ] <- gsub(" \\\\ (')|(\\\\ [^'])" , " \\ 1\\ 2" , xi )
154+ x
155+ }
156+
139157remove_space_before_opening_paren <- function (pd_flat ) {
140158 paren_after <- pd_flat $ token == " '('"
141159 if (! any(paren_after )) {
0 commit comments