1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-25 15:00:47 +02:00

faster! faster! must go faster!

This commit is contained in:
Ryan Grove
2007-05-04 21:00:01 +00:00
parent aa536c53d8
commit c46733c51d

View File

@@ -40,16 +40,19 @@
* @author Ryan Grove <ryan@wonko.com> * @author Ryan Grove <ryan@wonko.com>
* @copyright 2002 Douglas Crockford <douglas@crockford.com> (jsmin.c) * @copyright 2002 Douglas Crockford <douglas@crockford.com> (jsmin.c)
* @copyright 2007 Ryan Grove <ryan@wonko.com> * @copyright 2007 Ryan Grove <ryan@wonko.com>
* @version 1.0.0 (2007-05-03) * @version 1.0.0 (?)
* @link http://code.google.com/p/minify/ * @link http://code.google.com/p/minify/
*/ */
class JSMin { class JSMin {
const ORD_LF = 10; const ORD_LF = 10;
const ORD_SPACE = 32; const ORD_SPACE = 32;
protected $a = ''; protected $a = '';
protected $b = ''; protected $b = '';
protected $input = array(); protected $input = '';
protected $inputIndex = 0;
protected $inputLength = 0;
protected $lookAhead = null; protected $lookAhead = null;
protected $output = array(); protected $output = array();
@@ -63,7 +66,8 @@ class JSMin {
// -- Public Instance Methods ------------------------------------------------ // -- Public Instance Methods ------------------------------------------------
public function __construct($input) { public function __construct($input) {
$this->input = str_split($input); $this->input = $input;
$this->inputLength = strlen($input);
} }
// -- Protected Instance Methods --------------------------------------------- // -- Protected Instance Methods ---------------------------------------------
@@ -89,7 +93,7 @@ class JSMin {
throw new JSMinException('Unterminated string literal.'); throw new JSMinException('Unterminated string literal.');
} }
if ($this->a === "\\") { if ($this->a === '\\') {
$this->output[] = $this->a; $this->output[] = $this->a;
$this->a = $this->get(); $this->a = $this->get();
} }
@@ -113,7 +117,7 @@ class JSMin {
if ($this->a === '/') { if ($this->a === '/') {
break; break;
} }
elseif ($this->a === "\\") { elseif ($this->a === '\\') {
$this->output[] = $this->a; $this->output[] = $this->a;
$this->a = $this->get(); $this->a = $this->get();
} }
@@ -134,14 +138,10 @@ class JSMin {
$c = $this->lookAhead; $c = $this->lookAhead;
$this->lookAhead = null; $this->lookAhead = null;
if (is_null($c)) { if ($c === null) {
// Believe it or not, the following weirdness is MUCH faster than using if ($this->inputIndex < $this->inputLength) {
// array_shift(). $c = $this->input[$this->inputIndex];
$i = key($this->input); $this->inputIndex += 1;
if (!is_null($i)) {
$c = $this->input[$i];
unset($this->input[$i]);
} }
else { else {
$c = null; $c = null;
@@ -152,7 +152,7 @@ class JSMin {
return "\n"; return "\n";
} }
if (is_null($c) || $c === "\n" || ord($c) >= self::ORD_SPACE) { if ($c === null || $c === "\n" || ord($c) >= self::ORD_SPACE) {
return $c; return $c;
} }
@@ -160,14 +160,14 @@ class JSMin {
} }
protected function isAlphaNum($c) { protected function isAlphaNum($c) {
return ord($c) > 126 || $c === "\\" || preg_match('/^[\w\$]$/', $c) === 1; return ord($c) > 126 || $c === '\\' || preg_match('/^[\w\$]$/', $c) === 1;
} }
protected function jsmin() { protected function jsmin() {
$this->a = "\n"; $this->a = "\n";
$this->action(3); $this->action(3);
while (!is_null($this->a)) { while ($this->a !== null) {
switch ($this->a) { switch ($this->a) {
case ' ': case ' ':
if ($this->isAlphaNum($this->b)) { if ($this->isAlphaNum($this->b)) {