Skip to content

Commit

Permalink
Fix % escaping (all cases) for StringMatchesFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
guilliamxavier authored and sebastianbergmann committed Aug 31, 2018
1 parent 0c888fe commit bbfc617
Showing 1 changed file with 15 additions and 29 deletions.
44 changes: 15 additions & 29 deletions src/Framework/Constraint/StringMatchesFormatDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,38 +75,24 @@ protected function additionalFailureDescription($other): string

private function createPatternFromFormat(string $string): string
{
$string = \preg_replace(
$string = \strtr(
\preg_quote($string, '/'),
[
'/(?<!%)%e/',
'/(?<!%)%s/',
'/(?<!%)%S/',
'/(?<!%)%a/',
'/(?<!%)%A/',
'/(?<!%)%w/',
'/(?<!%)%i/',
'/(?<!%)%d/',
'/(?<!%)%x/',
'/(?<!%)%f/',
'/(?<!%)%c/'
],
[
\str_replace('\\', '\\\\', '\\' . \DIRECTORY_SEPARATOR),
'[^\r\n]+',
'[^\r\n]*',
'.+',
'.*',
'\s*',
'[+-]?\d+',
'\d+',
'[0-9a-fA-F]+',
'[+-]?\.?\d+\.?\d*(?:[Ee][+-]?\d+)?',
'.'
],
\preg_quote($string, '/')
'%%' => '%',
'%e' => '\\' . \DIRECTORY_SEPARATOR,
'%s' => '[^\r\n]+',
'%S' => '[^\r\n]*',
'%a' => '.+',
'%A' => '.*',
'%w' => '\s*',
'%i' => '[+-]?\d+',
'%d' => '\d+',
'%x' => '[0-9a-fA-F]+',
'%f' => '[+-]?\.?\d+\.?\d*(?:[Ee][+-]?\d+)?',
'%c' => '.'
]
);

$string = \str_replace('%%', '%', $string);

return '/^' . $string . '$/s';
}

Expand Down

0 comments on commit bbfc617

Please sign in to comment.