Skip to content

Commit b37b84f

Browse files
committed
Optimize partial indentation and raw output
1 parent f04f5f8 commit b37b84f

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

src/Runtime.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,15 @@ public static function escapeExpression(mixed $var): string
316316

317317
/**
318318
* Get string representation for output
319+
*
320+
* This function has been optimized for JIT via a microbenchmark with common inputs.
319321
*/
320322
public static function raw(mixed $value): string
321323
{
324+
if (is_string($value)) {
325+
return $value;
326+
}
327+
322328
if ($value === true) {
323329
return 'true';
324330
}
@@ -437,16 +443,11 @@ public static function invokePartial(RuntimeContext $cx, ?string $name, mixed $c
437443
}
438444

439445
if ($indent !== '') {
440-
$lines = explode("\n", $result);
441-
$lastIdx = count($lines) - 1;
442-
foreach ($lines as $i => &$line) {
443-
if ($line === '' && $i === $lastIdx) {
444-
break;
445-
}
446-
$line = $indent . $line;
446+
if (str_ends_with($result, "\n")) {
447+
$result = $indent . str_replace("\n", "\n" . $indent, substr($result, 0, -1)) . "\n";
448+
} elseif ($result !== '') {
449+
$result = $indent . str_replace("\n", "\n" . $indent, $result);
447450
}
448-
unset($line);
449-
$result = implode("\n", $lines);
450451
}
451452

452453
return $result;

0 commit comments

Comments
 (0)