Skip to content

Commit 9af727a

Browse files
authored
Merge pull request #4 from rodion-k/master
Performance optimizations
2 parents 2267295 + ae354b1 commit 9af727a

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

src/Phug/Reader.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ class Reader
122122
*/
123123
private $nextConsumeLength;
124124

125+
private $inputLength;
126+
125127
/**
126128
* Creates a new reader instance.
127129
*
@@ -136,6 +138,7 @@ public function __construct($input, $encoding = null)
136138
$this->input = substr($this->input, 3);
137139
}
138140
$this->encoding = $encoding ?: $this->defaultEncoding;
141+
$this->inputLength = mb_strlen($this->input, $this->encoding);
139142

140143
$this->position = 0;
141144
$this->line = 1;
@@ -236,6 +239,7 @@ public function getOffset()
236239
public function normalize()
237240
{
238241
$this->input = str_replace(str_split($this->badCharacters), '', $this->input);
242+
$this->inputLength = mb_strlen($this->input, $this->encoding);
239243

240244
return $this;
241245
}
@@ -247,7 +251,7 @@ public function normalize()
247251
*/
248252
public function getLength()
249253
{
250-
return mb_strlen($this->input, $this->encoding);
254+
return $this->inputLength;
251255
}
252256

253257
/**
@@ -257,7 +261,7 @@ public function getLength()
257261
*/
258262
public function hasLength()
259263
{
260-
return $this->getLength() > 0;
264+
return $this->inputLength !== 0;
261265
}
262266

263267
/**
@@ -294,7 +298,7 @@ public function peek($length = null, $start = null)
294298
}
295299

296300
$this->lastPeekResult = mb_substr($this->input, $start, $length, $this->encoding);
297-
$this->nextConsumeLength = $start + mb_strlen($this->lastPeekResult, $this->encoding);
301+
$this->nextConsumeLength = $start + $length;
298302

299303
return $this->lastPeekResult;
300304
}
@@ -408,7 +412,8 @@ public function consume($length = null)
408412
}
409413

410414
$consumedPart = mb_substr($this->input, 0, $length, $this->encoding);
411-
$this->input = mb_substr($this->input, $length, mb_strlen($this->input) - $length, $this->encoding);
415+
$this->input = substr($this->input, strlen($consumedPart));
416+
$this->inputLength -= $length;
412417
$this->position += $length;
413418
$this->offset += $length;
414419

0 commit comments

Comments
 (0)