Skip to content

Commit 87425af

Browse files
Ayeshmvorisek
andcommitted
[run-tests.php] Combine multiple str_replace calls to a single strtr call
Makes the replacement easier to see, neatly aligned, and only takes one function call. This is safe because none of the combined replacement values contain tokens that would be recursively replaced. This also improves the readability on how the regular expressions in `EXPECTF` matcher is constructed. Co-authored-by: Michael Voříšek <[email protected]>
1 parent 0fcd07a commit 87425af

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

run-tests.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2567,22 +2567,23 @@ function run_test(string $php, $file, array $env): string
25672567
$wanted_re = $temp;
25682568

25692569
// Stick to basics
2570-
$wanted_re = str_replace('%e', '\\' . DIRECTORY_SEPARATOR, $wanted_re);
2571-
$wanted_re = str_replace('%s', '[^\r\n]+', $wanted_re);
2572-
$wanted_re = str_replace('%S', '[^\r\n]*', $wanted_re);
2573-
$wanted_re = str_replace('%a', '.+', $wanted_re);
2574-
$wanted_re = str_replace('%A', '.*', $wanted_re);
2575-
$wanted_re = str_replace('%w', '\s*', $wanted_re);
2576-
$wanted_re = str_replace('%i', '[+-]?\d+', $wanted_re);
2577-
$wanted_re = str_replace('%d', '\d+', $wanted_re);
2578-
$wanted_re = str_replace('%x', '[0-9a-fA-F]+', $wanted_re);
2579-
$wanted_re = str_replace('%f', '[+-]?\.?\d+\.?\d*(?:[Ee][+-]?\d+)?', $wanted_re);
2580-
$wanted_re = str_replace('%c', '.', $wanted_re);
2581-
$wanted_re = str_replace('%0', '\x00', $wanted_re);
2582-
// %f allows two points "-.0.0" but that is the best *simple* expression
2583-
}
2584-
2585-
if (preg_match("/^$wanted_re\$/s", $output)) {
2570+
$wanted_re = strtr($wanted_re, [
2571+
'%e' => preg_quote(DIRECTORY_SEPARATOR, '/'),
2572+
'%s' => '[^\r\n]+',
2573+
'%S' => '[^\r\n]*',
2574+
'%a' => '.+',
2575+
'%A' => '.*',
2576+
'%w' => '\s*',
2577+
'%i' => '[+-]?\d+',
2578+
'%d' => '\d+',
2579+
'%x' => '[0-9a-fA-F]+',
2580+
'%f' => '[+-]?\.?\d+\.?\d*(?:[Ee][+-]?\d+)?', // %f allows two points "-.0.0" but that is the best *simple* expression
2581+
'%c' => '.',
2582+
'%0' => '\x00',
2583+
]);
2584+
}
2585+
2586+
if (preg_match('/^' . $wanted_re . '$/s', $output)) {
25862587
$passed = true;
25872588
}
25882589
} else {

0 commit comments

Comments
 (0)