protected function shortenBools($content)
{
$content = preg_replace('/\\btrue\\b(?!:)/', '!0', $content);
$content = preg_replace('/\\bfalse\\b(?!:)/', '!1', $content);
// for(;;) is exactly the same as while(true)
$content = preg_replace('/\\bwhile\\(!0\\){/', 'for(;;){', $content);
// now make sure we didn't turn any do ... while(true) into do ... for(;;)
preg_match_all('/\\bdo\\b/', $content, $dos, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
// go backward to make sure positional offsets aren't altered when $content changes
$dos = array_reverse($dos);
foreach ($dos as $do) {
$offsetDo = $do[0][1];
// find all `while` (now `for`) following `do`: one of those must be
// associated with the `do` and be turned back into `while`
preg_match_all('/\\bfor\\(;;\\)/', $content, $whiles, PREG_OFFSET_CAPTURE | PREG_SET_ORDER, $offsetDo);
foreach ($whiles as $while) {
$offsetWhile = $while[0][1];
$open = substr_count($content, '{', $offsetDo, $offsetWhile - $offsetDo);
$close = substr_count($content, '}', $offsetDo, $offsetWhile - $offsetDo);
if ($open === $close) {
// only restore `while` if amount of `{` and `}` are the same;
// otherwise, that `for` isn't associated with this `do`
$content = substr_replace($content, 'while(!0)', $offsetWhile, strlen('for(;;)'));
break;
}
}
}
return $content;
}