summaryrefslogtreecommitdiff
path: root/javascript/class.JavaScriptPacker.php
diff options
context:
space:
mode:
Diffstat (limited to 'javascript/class.JavaScriptPacker.php')
-rwxr-xr-x[-rw-r--r--]javascript/class.JavaScriptPacker.php452
1 files changed, 224 insertions, 228 deletions
diff --git a/javascript/class.JavaScriptPacker.php b/javascript/class.JavaScriptPacker.php
index 959b817..635d1f5 100644..100755
--- a/javascript/class.JavaScriptPacker.php
+++ b/javascript/class.JavaScriptPacker.php
@@ -63,63 +63,62 @@
* # Be careful with the 'High ASCII' Level encoding if you use
* UTF-8 in your files...
*/
-
+
/*
* modified by Mark Fabrizio Jr. to work with php 4
*/
-
class JavaScriptPacker {
- var $IGNORE = '$1';
+ var $IGNORE = "$1";
// validate parameters
- var $_script = '';
+ var $_script = "";
var $_encoding = 62;
var $_fastDecode = true;
var $_specialChars = false;
-
- var $LITERAL_ENCODING = array(
- 'None' => 0,
- 'Numeric' => 10,
- 'Normal' => 62,
- 'High ASCII' => 95
- );
-
+
+ var $LITERAL_ENCODING = [
+ "None" => 0,
+ "Numeric" => 10,
+ "Normal" => 62,
+ "High ASCII" => 95,
+ ];
+
function JavaScriptPacker($_script, $_encoding = 62, $_fastDecode = true, $_specialChars = false)
{
$this->_script = $_script . "\n";
if (array_key_exists($_encoding, $this->LITERAL_ENCODING))
$_encoding = $this->LITERAL_ENCODING[$_encoding];
$this->_encoding = min((int)$_encoding, 95);
- $this->_fastDecode = $_fastDecode;
+ $this->_fastDecode = $_fastDecode;
$this->_specialChars = $_specialChars;
}
-
+
function pack() {
- $this->_addParser('_basicCompression');
+ $this->_addParser("_basicCompression");
if ($this->_specialChars)
- $this->_addParser('_encodeSpecialChars');
+ $this->_addParser("_encodeSpecialChars");
if ($this->_encoding)
- $this->_addParser('_encodeKeywords');
-
+ $this->_addParser("_encodeKeywords");
+
// go!
return $this->_pack($this->_script);
}
-
+
// apply all parsing routines
function _pack($script) {
for ($i = 0; isset($this->_parsers[$i]); $i++) {
- $script = call_user_func(array(&$this,$this->_parsers[$i]), $script);
+ $script = call_user_func([&$this,$this->_parsers[$i]], $script);
}
return $script;
}
-
+
// keep a list of parsing functions, they'll be executed all at once
- var $_parsers = array();
+ var $_parsers = [];
function _addParser($parser) {
$this->_parsers[] = $parser;
}
-
+
// zero encoding - just removal of white space and comments
function _basicCompression($script) {
$parser = new ParseMaster();
@@ -129,49 +128,49 @@ class JavaScriptPacker {
$parser->add('/\'[^\'\\n\\r]*\'/',$this->IGNORE);
$parser->add('/"[^"\\n\\r]*"/', $this->IGNORE);
// remove comments
- $parser->add('/\\/\\/[^\\n\\r]*[\\n\\r]/', ' ');
- $parser->add('/\\/\\*[^*]*\\*+([^\\/][^*]*\\*+)*\\//', ' ');
+ $parser->add('/\\/\\/[^\\n\\r]*[\\n\\r]/', " ");
+ $parser->add('/\\/\\*[^*]*\\*+([^\\/][^*]*\\*+)*\\//', " ");
// protect regular expressions
- $parser->add('/\\s+(\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/g?i?)/', '$2'); // IGNORE
+ $parser->add('/\\s+(\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/g?i?)/', "$2"); // IGNORE
$parser->add('/[^\\w\\x24\\/\'"*)\\?:]\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/g?i?/', $this->IGNORE);
// remove: ;;; doSomething();
if ($this->_specialChars) $parser->add('/;;;[^\\n\\r]+[\\n\\r]/');
// remove redundant semi-colons
$parser->add('/\\(;;\\)/', $this->IGNORE); // protect for (;;) loops
- $parser->add('/;+\\s*([};])/', '$2');
+ $parser->add('/;+\\s*([};])/', "$2");
// apply the above
$script = $parser->exec($script);
// remove white-space
- $parser->add('/(\\b|\\x24)\\s+(\\b|\\x24)/', '$2 $3');
- $parser->add('/([+\\-])\\s+([+\\-])/', '$2 $3');
- $parser->add('/\\s+/', '');
+ $parser->add('/(\\b|\\x24)\\s+(\\b|\\x24)/', "$2 $3");
+ $parser->add('/([+\\-])\\s+([+\\-])/', "$2 $3");
+ $parser->add('/\\s+/', "");
// done
return $parser->exec($script);
}
-
+
function _encodeSpecialChars($script) {
$parser = new ParseMaster();
// replace: $name -> n, $$name -> na
$parser->add('/((\\x24+)([a-zA-Z$_]+))(\\d*)/',
- array('fn' => '_replace_name')
+ ["fn" => "_replace_name"],
);
// replace: _name -> _0, double-underscore (__name) is ignored
$regexp = '/\\b_[A-Za-z\\d]\\w*/';
// build the word list
- $keywords = $this->_analyze($script, $regexp, '_encodePrivate');
+ $keywords = $this->_analyze($script, $regexp, "_encodePrivate");
// quick ref
- $encoded = $keywords['encoded'];
-
+ $encoded = $keywords["encoded"];
+
$parser->add($regexp,
- array(
- 'fn' => '_replace_encoded',
- 'data' => $encoded
- )
+ [
+ "fn" => "_replace_encoded",
+ "data" => $encoded,
+ ],
);
return $parser->exec($script);
}
-
+
function _encodeKeywords($script) {
// escape high-ascii values already in the script (i.e. in strings)
if ($this->_encoding > 62)
@@ -183,51 +182,51 @@ class JavaScriptPacker {
$regexp = ($this->_encoding > 62) ? '/\\w\\w+/' : '/\\w+/';
// build the word list
$keywords = $this->_analyze($script, $regexp, $encode);
- $encoded = $keywords['encoded'];
-
+ $encoded = $keywords["encoded"];
+
// encode
$parser->add($regexp,
- array(
- 'fn' => '_replace_encoded',
- 'data' => $encoded
- )
+ [
+ "fn" => "_replace_encoded",
+ "data" => $encoded,
+ ],
);
if (empty($script)) return $script;
- else {
+
//$res = $parser->exec($script);
//$res = $this->_bootStrap($res, $keywords);
//return $res;
return $this->_bootStrap($parser->exec($script), $keywords);
- }
+
}
-
+
function _analyze($script, $regexp, $encode) {
// analyse
// retreive all words in the script
- $all = array();
+ $all = [];
preg_match_all($regexp, $script, $all);
- $_sorted = array(); // list of words sorted by frequency
- $_encoded = array(); // dictionary of word->encoding
- $_protected = array(); // instances of "protected" words
+ $_sorted = []; // list of words sorted by frequency
+ $_encoded = []; // dictionary of word->encoding
+ $_protected = []; // instances of "protected" words
$all = $all[0]; // simulate the javascript comportement of global match
if (!empty($all)) {
- $unsorted = array(); // same list, not sorted
- $protected = array(); // "protected" words (dictionary of word->"word")
- $value = array(); // dictionary of charCode->encoding (eg. 256->ff)
- $this->_count = array(); // word->count
+ $unsorted = []; // same list, not sorted
+ $protected = []; // "protected" words (dictionary of word->"word")
+ $value = []; // dictionary of charCode->encoding (eg. 256->ff)
+ $this->_count = []; // word->count
$i = count($all); $j = 0; //$word = null;
// count the occurrences - used for sorting later
do {
--$i;
- $word = '$' . $all[$i];
+ $word = "$" . $all[$i];
if (!isset($this->_count[$word])) {
$this->_count[$word] = 0;
$unsorted[$j] = $word;
// make a dictionary of all of the protected words in this script
// these are words that might be mistaken for encoding
//if (is_string($encode) && method_exists($this, $encode))
- $values[$j] = call_user_func(array(&$this, $encode), $j);
- $protected['$' . $values[$j]] = $j++;
+ $values[$j] = call_user_func([&$this, $encode], $j);
+ $protected["$" . $values[$j]] = $j++;
}
// increment the word counter
$this->_count[$word]++;
@@ -247,7 +246,7 @@ class JavaScriptPacker {
$this->_count[$word] = 0;
}
} while ($i);
-
+
// sort the words by frequency
// Note: the javascript and php version of sort can be different :
// in php manual, usort :
@@ -257,8 +256,8 @@ class JavaScriptPacker {
// but equivalent.
// the ECMAscript standard does not guarantee this behaviour,
// and thus not all browsers (e.g. Mozilla versions dating back to at
- // least 2003) respect this.
- usort($unsorted, array(&$this, '_sortWords'));
+ // least 2003) respect this.
+ usort($unsorted, [&$this, "_sortWords"]);
$j = 0;
// because there are "protected" words in the list
// we must add the sorted words around them
@@ -268,17 +267,17 @@ class JavaScriptPacker {
$_encoded[$_sorted[$i]] = $values[$i];
} while (++$i < count($unsorted));
}
- return array(
- 'sorted' => $_sorted,
- 'encoded' => $_encoded,
- 'protected' => $_protected);
+ return [
+ "sorted" => $_sorted,
+ "encoded" => $_encoded,
+ "protected" => $_protected, ];
}
-
- var $_count = array();
+
+ var $_count = [];
function _sortWords($match1, $match2) {
return $this->_count[$match2] - $this->_count[$match1];
}
-
+
// build the boot function used for loading and decoding
function _bootStrap($packed, $keywords) {
$ENCODE = $this->_safeRegExp('$encode\\($count\\)');
@@ -287,30 +286,30 @@ class JavaScriptPacker {
$packed = "'" . $this->_escape($packed) . "'";
// $ascii: base for encoding
- $ascii = min(count($keywords['sorted']), $this->_encoding);
+ $ascii = min(count($keywords["sorted"]), $this->_encoding);
if ($ascii == 0) $ascii = 1;
// $count: number of words contained in the script
- $count = count($keywords['sorted']);
+ $count = count($keywords["sorted"]);
// $keywords: list of words contained in the script
- foreach ($keywords['protected'] as $i=>$value) {
- $keywords['sorted'][$i] = '';
+ foreach ($keywords["protected"] as $i=>$value) {
+ $keywords["sorted"][$i] = "";
}
// convert from a string to an array
- ksort($keywords['sorted']);
- $keywords = "'" . implode('|',$keywords['sorted']) . "'.split('|')";
+ ksort($keywords["sorted"]);
+ $keywords = "'" . implode("|",$keywords["sorted"]) . "'.split('|')";
- $encode = ($this->_encoding > 62) ? '_encode95' : $this->_getEncoder($ascii);
+ $encode = ($this->_encoding > 62) ? "_encode95" : $this->_getEncoder($ascii);
$encode = $this->_getJSFunction($encode);
- $encode = preg_replace('/_encoding/','$ascii', $encode);
- $encode = preg_replace('/arguments\\.callee/','$encode', $encode);
- $inline = '\\$count' . ($ascii > 10 ? '.toString(\\$ascii)' : '');
+ $encode = preg_replace("/_encoding/","$ascii", $encode);
+ $encode = preg_replace('/arguments\\.callee/',"$encode", $encode);
+ $inline = '\\$count' . ($ascii > 10 ? '.toString(\\$ascii)' : "");
// $decode: code snippet to speed up decoding
if ($this->_fastDecode) {
// create the decoder
- $decode = $this->_getJSFunction('_decodeBody');
+ $decode = $this->_getJSFunction("_decodeBody");
if ($this->_encoding > 62)
$decode = preg_replace('/\\\\w/', '[\\xa1-\\xff]', $decode);
// perform the encoding inline for lower ascii values
@@ -319,25 +318,25 @@ class JavaScriptPacker {
// special case: when $count==0 there are no keywords. I want to keep
// the basic shape of the unpacking funcion so i'll frig the code...
if ($count == 0)
- $decode = preg_replace($this->_safeRegExp('($count)\\s*=\\s*1'), '$1=0', $decode, 1);
+ $decode = preg_replace($this->_safeRegExp('($count)\\s*=\\s*1'), "$1=0", $decode, 1);
}
// boot function
- $unpack = $this->_getJSFunction('_unpack');
+ $unpack = $this->_getJSFunction("_unpack");
if ($this->_fastDecode) {
// insert the decoder
$this->buffer = $decode;
- $unpack = preg_replace_callback('/\\{/', array(&$this, '_insertFastDecode'), $unpack, 1);
+ $unpack = preg_replace_callback('/\\{/', [&$this, "_insertFastDecode"], $unpack, 1);
}
$unpack = preg_replace('/"/', "'", $unpack);
if ($this->_encoding > 62) { // high-ascii
// get rid of the word-boundaries for regexp matches
- $unpack = preg_replace('/\'\\\\\\\\b\'\s*\\+|\\+\s*\'\\\\\\\\b\'/', '', $unpack);
+ $unpack = preg_replace('/\'\\\\\\\\b\'\s*\\+|\\+\s*\'\\\\\\\\b\'/', "", $unpack);
}
if ($ascii > 36 || $this->_encoding > 62 || $this->_fastDecode) {
// insert the encode function
$this->buffer = $encode;
- $unpack = preg_replace_callback('/\\{/', array(&$this, '_insertFastEncode'), $unpack, 1);
+ $unpack = preg_replace_callback('/\\{/', [&$this, "_insertFastEncode"], $unpack, 1);
} else {
// perform the encoding inline
$unpack = preg_replace($ENCODE, $inline, $unpack);
@@ -345,105 +344,104 @@ class JavaScriptPacker {
// pack the boot function too
$unpackPacker = new JavaScriptPacker($unpack, 0, false, true);
$unpack = $unpackPacker->pack();
-
+
// arguments
- $params = array($packed, $ascii, $count, $keywords);
+ $params = [$packed, $ascii, $count, $keywords];
if ($this->_fastDecode) {
$params[] = 0;
- $params[] = '{}';
+ $params[] = "{}";
}
- $params = implode(',', $params);
-
+ $params = implode(",", $params);
+
// the whole thing
- return 'eval(' . $unpack . '(' . $params . "));\n";
+ return "eval(" . $unpack . "(" . $params . "));\n";
}
-
+
var $buffer;
function _insertFastDecode($match) {
- return '{' . $this->buffer . ';';
+ return "{" . $this->buffer . ";";
}
function _insertFastEncode($match) {
- return '{$encode=' . $this->buffer . ';';
+ return "{$encode=" . $this->buffer . ";";
}
-
+
// mmm.. ..which one do i need ??
function _getEncoder($ascii) {
return $ascii > 10 ? $ascii > 36 ? $ascii > 62 ?
- '_encode95' : '_encode62' : '_encode36' : '_encode10';
+ "_encode95" : "_encode62" : "_encode36" : "_encode10";
}
-
+
// zero encoding
// characters: 0123456789
function _encode10($charCode) {
return $charCode;
}
-
+
// inherent base36 support
// characters: 0123456789abcdefghijklmnopqrstuvwxyz
function _encode36($charCode) {
return base_convert($charCode, 10, 36);
}
-
+
// hitch a ride on base36 and add the upper case alpha characters
// characters: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
function _encode62($charCode) {
- $res = '';
+ $res = "";
if ($charCode >= $this->_encoding) {
$res = $this->_encode62((int)($charCode / $this->_encoding));
}
$charCode = $charCode % $this->_encoding;
-
+
if ($charCode > 35)
return $res . chr($charCode + 29);
- else
+
return $res . base_convert($charCode, 10, 36);
}
-
+
// use high-ascii values
// characters: ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿À�ÂÃÄÅÆÇÈÉÊËÌ�Î��ÑÒÓÔÕÖרÙÚÛÜ�Þßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ
function _encode95($charCode) {
- $res = '';
+ $res = "";
if ($charCode >= $this->_encoding)
$res = $this->_encode95($charCode / $this->_encoding);
-
+
return $res . chr(($charCode % $this->_encoding) + 161);
}
-
+
function _safeRegExp($string) {
- return '/'.preg_replace('/\$/', '\\\$', $string).'/';
+ return "/".preg_replace('/\$/', '\\\$', $string)."/";
}
-
+
function _encodePrivate($charCode) {
return "_" . $charCode;
}
-
+
// protect characters used by the parser
function _escape($script) {
return preg_replace('/([\\\\\'])/', '\\\$1', $script);
}
-
+
// protect high-ascii characters already in the script
function _escape95($script) {
return preg_replace_callback(
'/[\\xa1-\\xff]/',
- array(&$this, '_escape95Bis'),
- $script
+ [&$this, "_escape95Bis"],
+ $script,
);
}
function _escape95Bis($match) {
return '\x'.((string)dechex(ord($match)));
}
-
-
+
function _getJSFunction($aName) {
- $func = 'JSFUNCTION'.$aName;
+ $func = "JSFUNCTION".$aName;
if (isset($this->$func)){
return $this->$func;
}
- else
- return '';
+
+ return "";
}
-
+
// JavaScript Functions used.
// Note : In Dean's version, these functions are converted
// with 'String(aFunctionName);'.
@@ -451,7 +449,7 @@ class JavaScriptPacker {
// 'while (aBool) anAction();' is converted to
// 'while (aBool) { anAction(); }'.
// The JavaScript functions below are corrected.
-
+
// unpacking function - this is the boot strap function
// data extracted from this packing routine is passed to
// this function when decoded in the target
@@ -466,13 +464,13 @@ class JavaScriptPacker {
}';
/*
'function($packed, $ascii, $count, $keywords, $encode, $decode) {
- while ($count--)
- if ($keywords[$count])
- $packed = $packed.replace(new RegExp(\'\\\\b\' + $encode($count) + \'\\\\b\', \'g\'), $keywords[$count]);
- return $packed;
+ while ($count--)
+ if ($keywords[$count])
+ $packed = $packed.replace(new RegExp(\'\\\\b\' + $encode($count) + \'\\\\b\', \'g\'), $keywords[$count]);
+ return $packed;
}';
*/
-
+
// code-snippet inserted into the unpacker to speed up decoding
var $JSFUNCTION_decodeBody = ' if (!\'\'.replace(/^/, String)) {
// decode all the values we need
@@ -490,55 +488,54 @@ class JavaScriptPacker {
//};
/*
' if (!\'\'.replace(/^/, String)) {
- // decode all the values we need
- while ($count--) $decode[$encode($count)] = $keywords[$count] || $encode($count);
- // global replacement function
- $keywords = [function ($encoded) {return $decode[$encoded]}];
- // generic match
- $encode = function () {return\'\\\\w+\'};
- // reset the loop counter - we are now doing a global replace
- $count = 1;
- }';
+ // decode all the values we need
+ while ($count--) $decode[$encode($count)] = $keywords[$count] || $encode($count);
+ // global replacement function
+ $keywords = [function ($encoded) {return $decode[$encoded]}];
+ // generic match
+ $encode = function () {return\'\\\\w+\'};
+ // reset the loop counter - we are now doing a global replace
+ $count = 1;
+ }';
*/
-
+
// zero encoding
// characters: 0123456789
- var $JSFUNCTION_encode10 = 'function($charCode) {
+ var $JSFUNCTION_encode10 = "function($charCode) {
return $charCode;
-}';//;';
-
+}";//;';
+
// inherent base36 support
// characters: 0123456789abcdefghijklmnopqrstuvwxyz
- var $JSFUNCTION_encode36 = 'function($charCode) {
+ var $JSFUNCTION_encode36 = "function($charCode) {
return $charCode.toString(36);
-}';//;';
-
+}";//;';
+
// hitch a ride on base36 and add the upper case alpha characters
// characters: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
var $JSFUNCTION_encode62 = 'function($charCode) {
return ($charCode < _encoding ? \'\' : arguments.callee(parseInt($charCode / _encoding))) +
(($charCode = $charCode % _encoding) > 35 ? String.fromCharCode($charCode + 29) : $charCode.toString(36));
}';
-
+
// use high-ascii values
// characters: ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿À�ÂÃÄÅÆÇÈÉÊËÌ�Î��ÑÒÓÔÕÖרÙÚÛÜ�Þßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ
var $JSFUNCTION_encode95 = 'function($charCode) {
return ($charCode < _encoding ? \'\' : arguments.callee($charCode / _encoding)) +
String.fromCharCode($charCode % _encoding + 161);
-}';
-
-}
+}';
+}
class ParseMaster {
var $ignoreCase = false;
- var $escapeChar = '';
-
+ var $escapeChar = "";
+
// constants
var $EXPRESSION = 0;
var $REPLACEMENT = 1;
var $LENGTH = 2;
-
+
// used to determine nesting levels
var $GROUPS = '/\\(/';//g
var $SUB_REPLACE = '/\\$\\d/';
@@ -547,12 +544,12 @@ class ParseMaster {
var $ESCAPE = '/\\\./';//g
var $QUOTE = '/\'/';
var $DELETED = '/\\x01[^\\x01]*\\x01/';//g
-
- function add($expression, $replacement = '') {
+
+ function add($expression, $replacement = "") {
// count the number of sub-expressions
// - add one because each pattern is itself a sub-expression
$length = 1 + preg_match_all($this->GROUPS, $this->_internalEscape((string)$expression), $out);
-
+
// treat only strings $replacement
if (is_string($replacement)) {
// does the pattern deal with sub-expressions?
@@ -564,169 +561,168 @@ class ParseMaster {
} else { // a complicated lookup (e.g. "Hello $2 $1")
// build a function to do the lookup
$quote = preg_match($this->QUOTE, $this->_internalEscape($replacement))
- ? '"' : "'";
- $replacement = array(
- 'fn' => '_backReferences',
- 'data' => array(
- 'replacement' => $replacement,
- 'length' => $length,
- 'quote' => $quote
- )
- );
+ ? '"' : "'";
+ $replacement = [
+ "fn" => "_backReferences",
+ "data" => [
+ "replacement" => $replacement,
+ "length" => $length,
+ "quote" => $quote,
+ ],
+ ];
}
}
}
// pass the modified arguments
if (!empty($expression)) $this->_add($expression, $replacement, $length);
- else $this->_add('/^$/', $replacement, $length);
+ else $this->_add("/^$/", $replacement, $length);
}
-
+
function exec($string) {
// execute the global replacement
- $this->_escaped = array();
-
+ $this->_escaped = [];
+
// simulate the _patterns.toSTring of Dean
- $regexp = '/';
+ $regexp = "/";
foreach ($this->_patterns as $reg) {
- $regexp .= '(' . substr($reg[$this->EXPRESSION], 1, -1) . ')|';
+ $regexp .= "(" . substr($reg[$this->EXPRESSION], 1, -1) . ")|";
}
- $regexp = substr($regexp, 0, -1) . '/';
- $regexp .= ($this->ignoreCase) ? 'i' : '';
-
+ $regexp = substr($regexp, 0, -1) . "/";
+ $regexp .= ($this->ignoreCase) ? "i" : "";
+
$string = $this->_escape($string, $this->escapeChar);
$string = preg_replace_callback(
$regexp,
- array(
+ [
&$this,
- '_replacement'
- ),
- $string
+ "_replacement",
+ ],
+ $string,
);
$string = $this->_unescape($string, $this->escapeChar);
-
- return preg_replace($this->DELETED, '', $string);
+
+ return preg_replace($this->DELETED, "", $string);
}
-
+
function reset() {
// clear the patterns collection so that this object may be re-used
- $this->_patterns = array();
+ $this->_patterns = [];
}
// private
- var $_escaped = array(); // escaped characters
- var $_patterns = array(); // patterns stored by index
-
+ var $_escaped = []; // escaped characters
+ var $_patterns = []; // patterns stored by index
+
// create and add a new pattern to the patterns collection
function _add() {
$arguments = func_get_args();
$this->_patterns[] = $arguments;
}
-
+
// this is the global replace function (it's quite complicated)
function _replacement($arguments) {
- if (empty($arguments)) return '';
-
+ if (empty($arguments)) return "";
+
$i = 1; $j = 0;
// loop through the patterns
while (isset($this->_patterns[$j])) {
$pattern = $this->_patterns[$j++];
// do we have a result?
- if (isset($arguments[$i]) && ($arguments[$i] != '')) {
+ if (isset($arguments[$i]) && ($arguments[$i] != "")) {
$replacement = $pattern[$this->REPLACEMENT];
-
- if (is_array($replacement) && isset($replacement['fn'])) {
-
- if (isset($replacement['data'])) $this->buffer = $replacement['data'];
- return call_user_func(array(&$this, $replacement['fn']), $arguments, $i);
-
+
+ if (is_array($replacement) && isset($replacement["fn"])) {
+
+ if (isset($replacement["data"])) $this->buffer = $replacement["data"];
+ return call_user_func([&$this, $replacement["fn"]], $arguments, $i);
+
} elseif (is_int($replacement)) {
return $arguments[$replacement + $i];
-
+
}
- $delete = ($this->escapeChar == '' ||
- strpos($arguments[$i], $this->escapeChar) === false)
- ? '' : "\x01" . $arguments[$i] . "\x01";
+ $delete = ($this->escapeChar == "" ||
+ strpos($arguments[$i], $this->escapeChar) === false)
+ ? "" : "\x01" . $arguments[$i] . "\x01";
return $delete . $replacement;
-
+
// skip over references to sub-expressions
- } else {
- $i += $pattern[$this->LENGTH];
}
+ $i += $pattern[$this->LENGTH];
+
}
}
-
+
function _backReferences($match, $offset) {
- $replacement = $this->buffer['replacement'];
- $quote = $this->buffer['quote'];
- $i = $this->buffer['length'];
+ $replacement = $this->buffer["replacement"];
+ $quote = $this->buffer["quote"];
+ $i = $this->buffer["length"];
while ($i) {
- $replacement = str_replace('$'.$i--, $match[$offset + $i], $replacement);
+ $replacement = str_replace("$".$i--, $match[$offset + $i], $replacement);
}
return $replacement;
}
-
+
function _replace_name($match, $offset){
$length = strlen($match[$offset + 2]);
$start = $length - max($length - strlen($match[$offset + 3]), 0);
return substr($match[$offset + 1], $start, $length) . $match[$offset + 4];
}
-
+
function _replace_encoded($match, $offset) {
return $this->buffer[$match[$offset]];
}
-
-
+
// php : we cannot pass additional data to preg_replace_callback,
// and we cannot use &$this in create_function, so let's go to lower level
var $buffer;
-
+
// encode escaped characters
function _escape($string, $escapeChar) {
if ($escapeChar) {
$this->buffer = $escapeChar;
return preg_replace_callback(
- '/\\' . $escapeChar . '(.)' .'/',
- array(&$this, '_escapeBis'),
- $string
+ '/\\' . $escapeChar . "(.)" ."/",
+ [&$this, "_escapeBis"],
+ $string,
);
-
- } else {
- return $string;
+
}
+ return $string;
+
}
function _escapeBis($match) {
$this->_escaped[] = $match[1];
return $this->buffer;
}
-
+
// decode escaped characters
function _unescape($string, $escapeChar) {
if ($escapeChar) {
- $regexp = '/'.'\\'.$escapeChar.'/';
- $this->buffer = array('escapeChar'=> $escapeChar, 'i' => 0);
+ $regexp = "/".'\\'.$escapeChar."/";
+ $this->buffer = ["escapeChar"=> $escapeChar, "i" => 0];
return preg_replace_callback
(
$regexp,
- array(&$this, '_unescapeBis'),
- $string
+ [&$this, "_unescapeBis"],
+ $string,
);
-
- } else {
- return $string;
+
}
+ return $string;
+
}
function _unescapeBis() {
- if (!empty($this->_escaped[$this->buffer['i']])) {
- $temp = $this->_escaped[$this->buffer['i']];
+ if (!empty($this->_escaped[$this->buffer["i"]])) {
+ $temp = $this->_escaped[$this->buffer["i"]];
} else {
- $temp = '';
+ $temp = "";
}
- $this->buffer['i']++;
- return $this->buffer['escapeChar'] . $temp;
+ $this->buffer["i"]++;
+ return $this->buffer["escapeChar"] . $temp;
}
-
+
function _internalEscape($string) {
- return preg_replace($this->ESCAPE, '', $string);
+ return preg_replace($this->ESCAPE, "", $string);
}
}
?>