From 88bd07834aac4e8bc9bc2fbe481caea85a57afec Mon Sep 17 00:00:00 2001 From: to0oner <36884138+to0oner@users.noreply.github.com> Date: Tue, 27 Feb 2018 12:35:01 +0100 Subject: [PATCH 1/4] PHP stripos() replaced through strpos() for better performance in TokenizerBase::next_pos() --- gan_tokenizer.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/gan_tokenizer.php b/gan_tokenizer.php index 1b6a98b..50dc0c5 100644 --- a/gan_tokenizer.php +++ b/gan_tokenizer.php @@ -57,6 +57,12 @@ class TokenizerBase { * @access private */ var $doc = ''; + + /** + * The document as string in lowercase for faster search in next_pos() + * @var string + */ + var $doc_lower = ''; /** * The size of the document (length of string) @@ -184,6 +190,7 @@ function __construct($doc = '', $pos = 0) { */ function setDoc($doc, $pos = 0) { $this->doc = $doc; + $this->doc_lower = strtolower($doc); $this->size = strlen($doc); $this->setPos($pos); } @@ -489,7 +496,10 @@ function next_search($characters, $callback = true) { */ function next_pos($needle, $callback = true) { $this->token_start = $this->pos; - if (($this->pos < $this->size) && (($p = stripos($this->doc, $needle, $this->pos + 1)) !== false)) { + + $needle_lower = strtolower($needle); + + if (($this->pos < $this->size) && (($p = strpos($this->doc_lower, $needle_lower, $this->pos + 1)) !== false)) { $len = $p - $this->pos - 1; if ($len > 0) { From acf3f8be9e6e3b637e5d677570d97f169a3536c7 Mon Sep 17 00:00:00 2001 From: to0oner <36884138+to0oner@users.noreply.github.com> Date: Tue, 27 Feb 2018 13:02:16 +0100 Subject: [PATCH 2/4] Added Braces for If Statements --- pQuery.php | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/pQuery.php b/pQuery.php index 7d451e8..0f28d00 100644 --- a/pQuery.php +++ b/pQuery.php @@ -48,12 +48,14 @@ public function append($content) { } public function attr($name, $value = null) { - if (empty($this->nodes) && $value === null) + if (empty($this->nodes) && $value === null){ return ''; + } foreach ($this->nodes as $node) { - if ($value === null) + if ($value === null){ return $node->attr($name); + } $node->attr($name, $value); } return $this; @@ -100,19 +102,22 @@ public function getIterator() { public function hasClass($classname) { foreach ($this->nodes as $node) { - if ($node->hasClass($classname)) + if ($node->hasClass($classname)){ return true; + } } return false; } public function html($value = null) { - if (empty($this->nodes) && $value === null) + if (empty($this->nodes) && $value === null){ return ''; + } foreach ($this->nodes as $node) { - if ($value === null) + if ($value === null){ return $node->html(); + } $node->html($value); } return $this; @@ -173,12 +178,14 @@ public function prepend($content = null) { } public function prop($name, $value = null) { - if (empty($this->nodes) && $value === null) + if (empty($this->nodes) && $value === null){ return ''; + } foreach ($this->nodes as $node) { - if ($value === null) + if ($value === null){ return $node->prop($name); + } $node->prop($name, $value); } return $this; @@ -188,8 +195,9 @@ public function remove($selector = null) { foreach ($this->nodes as $node) { $node->remove($selector); } - if ($selector === null) + if ($selector === null){ $this->nodes = array(); + } return $this; } @@ -217,20 +225,23 @@ public function replaceWith($content) { public function tagName($value = null) { foreach ($this->nodes as $node) { - if ($value === null) + if ($value === null){ return $node->tagName(); + } $node->tagName($value); } return $this; } public function text($value = null) { - if (empty($this->nodes) && $value === null) + if (empty($this->nodes) && $value === null){ return ''; + } foreach ($this->nodes as $node) { - if ($value === null) + if ($value === null){ return $node->text(); + } $node->text($value); } return $this; @@ -252,12 +263,14 @@ public function unwrap() { } public function val($value = null) { - if (empty($this->nodes) && $value === null) + if (empty($this->nodes) && $value === null){ return ''; + } foreach ($this->nodes as $node) { - if ($value === null) + if ($value === null){ return $node->val(); + } $node->val($value); } return $this; From 914f0bcfe6bb77a4fe00ea71389ede54021e7a0c Mon Sep 17 00:00:00 2001 From: to0oner <36884138+to0oner@users.noreply.github.com> Date: Tue, 27 Feb 2018 18:08:27 +0100 Subject: [PATCH 3/4] composer.json declares php has to be version >=5.3.0, so some unnecessary checks are removed --- ganon.php | 43 +------------------------------------------ 1 file changed, 1 insertion(+), 42 deletions(-) diff --git a/ganon.php b/ganon.php index 1f80ebd..f4ce74b 100644 --- a/ganon.php +++ b/ganon.php @@ -30,14 +30,7 @@ function str_get_dom($str, $return_root = true) { * @return Html5Parser|DomNode */ function file_get_dom($file, $return_root = true, $use_include_path = false, $context = null) { - if (version_compare(PHP_VERSION, '5.0.0', '>=')) - $f = file_get_contents($file, $use_include_path, $context); - else { - if ($context !== null) - trigger_error('Context parameter not supported in this PHP version'); - $f = file_get_contents($file, $use_include_path); - } - + $f = file_get_contents($file, $use_include_path, $context); return (($f === false) ? false : str_get_dom($f, $return_root)); } @@ -52,40 +45,6 @@ function dom_format(&$root, $options = array()) { return $formatter->format($root); } -if (version_compare(PHP_VERSION, '5.0.0', '<')) { - /** - * PHP alternative to str_split, for backwards compatibility - * @param string $string - * @return string - */ - function str_split($string) { - $res = array(); - $size = strlen($string); - for ($i = 0; $i < $size; $i++) { - $res[] = $string[$i]; - } - - return $res; - } -} - -if (version_compare(PHP_VERSION, '5.2.0', '<')) { - /** - * PHP alternative to array_fill_keys, for backwards compatibility - * @param array $keys - * @param mixed $value - * @return array - */ - function array_fill_keys($keys, $value) { - $res = array(); - foreach($keys as $k) { - $res[$k] = $value; - } - - return $res; - } -} - #!! <- Ignore when converting to single file if (!defined('GANON_NO_INCLUDES')) { define('GANON_NO_INCLUDES', true); From 2d1ac01dabdbbf45c4ff270dcd5b470741274545 Mon Sep 17 00:00:00 2001 From: to0oner <36884138+to0oner@users.noreply.github.com> Date: Tue, 27 Feb 2018 18:31:09 +0100 Subject: [PATCH 4/4] small addition for composer usage --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 305415e..78fe895 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,10 @@ To get started using pQuery do the following. The following example parses an html string and does some manipulation on it. ```php + +// composer autoload +require "vendor/autoload.php"; + $html = '
Hello
Cruel