Skip to content

Commit 84e5cfa

Browse files
committed
Improve buffer calls
1 parent 52b0d6b commit 84e5cfa

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

packages/guides-restructured-text/src/RestructuredText/Parser/Buffer.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,29 @@ public function clear(): void
103103

104104
public function trimLines(): void
105105
{
106-
array_walk($this->lines, static function (&$value): void {
107-
$value = trim($value);
108-
});
106+
foreach ($this->lines as $i => $line) {
107+
$this->lines[$i] = trim($line);
108+
}
109109
}
110110

111111
private function unIndent(): void
112112
{
113+
if ($this->unindentStrategy === UnindentStrategy::NONE) {
114+
return;
115+
}
116+
113117
$indentation = $this->detectIndentation();
114-
array_walk($this->lines, static function (&$value) use ($indentation): void {
115-
if (strlen($value) < $indentation) {
116-
return;
118+
if ($indentation === 0) {
119+
return;
120+
}
121+
122+
foreach ($this->lines as $i => $line) {
123+
if (strlen($line) < $indentation) {
124+
continue;
117125
}
118126

119-
$value = substr($value, $indentation);
120-
});
127+
$this->lines[$i] = substr($line, $indentation);
128+
}
121129
}
122130

123131
private function detectIndentation(): int
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Guides\RestructuredText;
6+
7+
use PhpBench\Attributes\Iterations;
8+
use PhpBench\Attributes\Revs;
9+
use phpDocumentor\Guides\RestructuredText\Parser\Buffer;
10+
11+
final class BufferBench
12+
{
13+
private Buffer $buffer;
14+
15+
public function __construct()
16+
{
17+
$this->buffer = new Buffer();
18+
$this->buffer->push(' This is a line with leading spaces. ');
19+
$this->buffer->push(' This is another line.');
20+
$this->buffer->push(' Yet another line with spaces. ');
21+
$this->buffer->push(' Final line.');
22+
}
23+
24+
25+
#[Revs([1000, 10_000])]
26+
#[Iterations(5)]
27+
public function benchGetLines(): void
28+
{
29+
$this->buffer->getLines();
30+
}
31+
}

0 commit comments

Comments
 (0)