[Console] Fix StringInput parsing to accept newlines and tabs

This commit is contained in:
Jordi Boggiano 2013-04-12 17:10:07 +02:00
parent 8642b676b3
commit 93b1369bb5
2 changed files with 4 additions and 4 deletions

View File

@ -24,7 +24,7 @@ namespace Symfony\Component\Console\Input;
*/
class StringInput extends ArgvInput
{
const REGEX_STRING = '([^ ]+?)(?: |(?<!\\\\)"|(?<!\\\\)\'|$)';
const REGEX_STRING = '([^\s]+?)(?:\s|(?<!\\\\)"|(?<!\\\\)\'|$)';
const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')';
/**
@ -59,14 +59,12 @@ class StringInput extends ArgvInput
*/
private function tokenize($input)
{
$input = preg_replace('/(\r\n|\r|\n|\t)/', ' ', $input);
$tokens = array();
$length = strlen($input);
$cursor = 0;
while ($cursor < $length) {
if (preg_match('/\s+/A', $input, $match, null, $cursor)) {
} elseif (preg_match('/([^="\' ]+?)(=?)('.self::REGEX_QUOTED_STRING.'+)/A', $input, $match, null, $cursor)) {
} elseif (preg_match('/([^="\'\s]+?)(=?)('.self::REGEX_QUOTED_STRING.'+)/A', $input, $match, null, $cursor)) {
$tokens[] = $match[1].$match[2].stripcslashes(str_replace(array('"\'', '\'"', '\'\'', '""'), '', substr($match[3], 1, strlen($match[3]) - 2)));
} elseif (preg_match('/'.self::REGEX_QUOTED_STRING.'/A', $input, $match, null, $cursor)) {
$tokens[] = stripcslashes(substr($match[0], 1, strlen($match[0]) - 2));

View File

@ -53,6 +53,8 @@ class StringInputTest extends \PHPUnit_Framework_TestCase
array(' foo bar ', array('foo', 'bar'), '->tokenize() ignores whitespaces between arguments'),
array('"quoted"', array('quoted'), '->tokenize() parses quoted arguments'),
array("'quoted'", array('quoted'), '->tokenize() parses quoted arguments'),
array("'a\rb\nc\td'", array("a\rb\nc\td"), '->tokenize() parses whitespace chars in strings'),
array("'a'\r'b'\n'c'\t'd'", array('a','b','c','d'), '->tokenize() parses whitespace chars between args as spaces'),
array('\"quoted\"', array('"quoted"'), '->tokenize() parses escaped-quoted arguments'),
array("\'quoted\'", array('\'quoted\''), '->tokenize() parses escaped-quoted arguments'),
array('-a', array('-a'), '->tokenize() parses short options'),