@@ -224,10 +224,11 @@ private function lexValue()
224
224
throw $ this ->createFormatException ('Missing quote to end the value ' );
225
225
}
226
226
++$ this ->cursor ;
227
- $ value = str_replace (array ('\\\\' , '\\ " ' , '\r ' , '\n ' ), array ('\\' , '" ' , "\r" , "\n" ), $ value );
227
+ $ value = str_replace (array ('\\" ' , '\r ' , '\n ' ), array ('" ' , "\r" , "\n" ), $ value );
228
228
$ resolvedValue = $ value ;
229
229
$ resolvedValue = $ this ->resolveVariables ($ resolvedValue );
230
230
$ resolvedValue = $ this ->resolveCommands ($ resolvedValue );
231
+ $ resolvedValue = str_replace ('\\\\' , '\\' , $ resolvedValue );
231
232
$ v .= $ resolvedValue ;
232
233
} else {
233
234
$ value = '' ;
@@ -250,6 +251,7 @@ private function lexValue()
250
251
$ resolvedValue = $ value ;
251
252
$ resolvedValue = $ this ->resolveVariables ($ resolvedValue );
252
253
$ resolvedValue = $ this ->resolveCommands ($ resolvedValue );
254
+ $ resolvedValue = str_replace ('\\\\' , '\\' , $ resolvedValue );
253
255
254
256
if ($ resolvedValue === $ value && preg_match ('/\s+/ ' , $ value )) {
255
257
throw $ this ->createFormatException ('A value containing spaces must be surrounded by quotes ' );
@@ -350,17 +352,23 @@ private function resolveVariables($value)
350
352
}
351
353
352
354
$ regex = '/
353
- ( \\\\)? # escaped with a backslash?
355
+ (?<! \\\\)
356
+ ((?: \\\\)*) # escaped with a backslash?
354
357
\$
355
358
(?!\() # no opening parenthesis
356
359
(\{)? # optional brace
357
- ( ' .self ::VARNAME_REGEX .') # var name
360
+ ( ' .self ::VARNAME_REGEX .')? # var name
358
361
(\})? # optional closing brace
359
362
/x ' ;
360
363
361
364
$ value = preg_replace_callback ($ regex , function ($ matches ) {
362
- if ('\\' === $ matches [1 ]) {
363
- return substr ($ matches [0 ], 1 );
365
+ if (strlen ($ matches [1 ]) % 2 === 1 ) {
366
+ return substr ($ matches [1 ], 0 , - 1 ).substr ($ matches [0 ], strlen ($ matches [1 ]));
367
+ }
368
+
369
+ // $ not followed by variable name
370
+ if (!isset ($ matches [3 ])) {
371
+ return $ matches [0 ];
364
372
}
365
373
366
374
if ('{ ' === $ matches [2 ] && !isset ($ matches [4 ])) {
@@ -382,11 +390,10 @@ private function resolveVariables($value)
382
390
$ value .= '} ' ;
383
391
}
384
392
385
- return $ value ;
393
+ return $ matches [ 1 ]. $ value ;
386
394
}, $ value );
387
395
388
- // unescape $
389
- return str_replace ('\\$ ' , '$ ' , $ value );
396
+ return $ value ;
390
397
}
391
398
392
399
private function moveCursor ($ text )
0 commit comments