24
24
*/
25
25
class StringInput extends ArgvInput
26
26
{
27
- public const REGEX_STRING = '([^\s]+?)(?:\s|(?<! \\\\ )"|(?<! \\\\ ) \' |$ ) ' ;
27
+ public const REGEX_STRING = '([^\s \ \\\]+? ) ' ;
28
28
public const REGEX_QUOTED_STRING = '(?:"([^" \\\\]*(?: \\\\.[^" \\\\]*)*)"| \'([^ \'\\\\]*(?: \\\\.[^ \'\\\\]*)*) \') ' ;
29
29
30
30
/**
@@ -47,14 +47,25 @@ private function tokenize(string $input): array
47
47
$ tokens = [];
48
48
$ length = \strlen ($ input );
49
49
$ cursor = 0 ;
50
+ $ token = null ;
50
51
while ($ cursor < $ length ) {
52
+ if ('\\' === $ input [$ cursor ]) {
53
+ $ token .= $ input [++$ cursor ] ?? '' ;
54
+ ++$ cursor ;
55
+ continue ;
56
+ }
57
+
51
58
if (preg_match ('/\s+/A ' , $ input , $ match , 0 , $ cursor )) {
59
+ if (null !== $ token ) {
60
+ $ tokens [] = $ token ;
61
+ $ token = null ;
62
+ }
52
63
} elseif (preg_match ('/([^=" \'\s]+?)(=?)( ' .self ::REGEX_QUOTED_STRING .'+)/A ' , $ input , $ match , 0 , $ cursor )) {
53
- $ tokens [] = $ match [1 ].$ match [2 ].stripcslashes (str_replace (['" \'' , '\'" ' , '\'\'' , '"" ' ], '' , substr ($ match [3 ], 1 , -1 )));
64
+ $ token . = $ match [1 ].$ match [2 ].stripcslashes (str_replace (['" \'' , '\'" ' , '\'\'' , '"" ' ], '' , substr ($ match [3 ], 1 , -1 )));
54
65
} elseif (preg_match ('/ ' .self ::REGEX_QUOTED_STRING .'/A ' , $ input , $ match , 0 , $ cursor )) {
55
- $ tokens [] = stripcslashes (substr ($ match [0 ], 1 , -1 ));
66
+ $ token . = stripcslashes (substr ($ match [0 ], 1 , -1 ));
56
67
} elseif (preg_match ('/ ' .self ::REGEX_STRING .'/A ' , $ input , $ match , 0 , $ cursor )) {
57
- $ tokens [] = stripcslashes ( $ match [1 ]) ;
68
+ $ token .= $ match [1 ];
58
69
} else {
59
70
// should never happen
60
71
throw new InvalidArgumentException (sprintf ('Unable to parse input near "... %s ...". ' , substr ($ input , $ cursor , 10 )));
@@ -63,6 +74,10 @@ private function tokenize(string $input): array
63
74
$ cursor += \strlen ($ match [0 ]);
64
75
}
65
76
77
+ if (null !== $ token ) {
78
+ $ tokens [] = $ token ;
79
+ }
80
+
66
81
return $ tokens ;
67
82
}
68
83
}
0 commit comments