2424 */
2525class StringInput extends ArgvInput
2626{
27- public const REGEX_STRING = '([^\s]+?)(?:\s|(?<! \\\\ )"|(?<! \\\\ ) \' |$ ) ' ;
27+ public const REGEX_STRING = '([^\s \ \\\]+? ) ' ;
2828 public const REGEX_QUOTED_STRING = '(?:"([^" \\\\]*(?: \\\\.[^" \\\\]*)*)"| \'([^ \'\\\\]*(?: \\\\.[^ \'\\\\]*)*) \') ' ;
2929
3030 /**
@@ -47,14 +47,25 @@ private function tokenize(string $input): array
4747 $ tokens = [];
4848 $ length = \strlen ($ input );
4949 $ cursor = 0 ;
50+ $ token = null ;
5051 while ($ cursor < $ length ) {
52+ if ('\\' === $ input [$ cursor ]) {
53+ $ token .= $ input [++$ cursor ] ?? '' ;
54+ ++$ cursor ;
55+ continue ;
56+ }
57+
5158 if (preg_match ('/\s+/A ' , $ input , $ match , 0 , $ cursor )) {
59+ if (null !== $ token ) {
60+ $ tokens [] = $ token ;
61+ $ token = null ;
62+ }
5263 } 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 )));
5465 } 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 ));
5667 } elseif (preg_match ('/ ' .self ::REGEX_STRING .'/A ' , $ input , $ match , 0 , $ cursor )) {
57- $ tokens [] = stripcslashes ( $ match [1 ]) ;
68+ $ token .= $ match [1 ];
5869 } else {
5970 // should never happen
6071 throw new InvalidArgumentException (sprintf ('Unable to parse input near "... %s ...". ' , substr ($ input , $ cursor , 10 )));
@@ -63,6 +74,10 @@ private function tokenize(string $input): array
6374 $ cursor += \strlen ($ match [0 ]);
6475 }
6576
77+ if (null !== $ token ) {
78+ $ tokens [] = $ token ;
79+ }
80+
6681 return $ tokens ;
6782 }
6883}
0 commit comments