made Charset load its tables JIT
authorgggeek <giunta.gaetano@gmail.com>
Wed, 30 Dec 2020 17:26:28 +0000 (17:26 +0000)
committergggeek <giunta.gaetano@gmail.com>
Wed, 30 Dec 2020 17:26:28 +0000 (17:26 +0000)
src/Helper/Charset.php

index 22604d2..2ea370f 100644 (file)
@@ -16,18 +16,8 @@ class Charset
     ///       IANA ISO-8859-1 does have well-defined 'C1' control codes for those - wikipedia's page on latin-1 says:
     ///       "ISO-8859-1 is the IANA preferred name for this standard when supplemented with the C0 and C1 control codes from ISO/IEC 6429."
     ///       Check what mbstring/iconv do by default with those?
-    /*
-    protected $xml_cp1252_Entities = array('in' => array(), out' => array(
-        '&#x20AC;', '?',        '&#x201A;', '&#x0192;',
-        '&#x201E;', '&#x2026;', '&#x2020;', '&#x2021;',
-        '&#x02C6;', '&#x2030;', '&#x0160;', '&#x2039;',
-        '&#x0152;', '?',        '&#x017D;', '?',
-        '?',        '&#x2018;', '&#x2019;', '&#x201C;',
-        '&#x201D;', '&#x2022;', '&#x2013;', '&#x2014;',
-        '&#x02DC;', '&#x2122;', '&#x0161;', '&#x203A;',
-        '&#x0153;', '?',        '&#x017E;', '&#x0178;'
-    ));
-    */
+    //
+    //protected $xml_cp1252_Entities = array('in' => array(), out' => array());
 
     protected $charset_supersets = array(
         'US-ASCII' => array('ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4',
@@ -55,24 +45,56 @@ class Charset
     }
 
     /**
-     * @todo move the creation of the charset tables to be on-demand. This saves memory and time when latin-1 is not used at all
+     * Force usage as singleton
      */
-    private function __construct()
+    protected function __construct()
     {
-        for ($i = 0; $i < 32; $i++) {
-            $this->xml_iso88591_Entities["in"][] = chr($i);
-            $this->xml_iso88591_Entities["out"][] = "&#{$i};";
-        }
+    }
 
-        for ($i = 160; $i < 256; $i++) {
-            $this->xml_iso88591_Entities["in"][] = chr($i);
-            $this->xml_iso88591_Entities["out"][] = "&#{$i};";
-        }
+    /**
+     * @param string $tableName
+     * @throws \Exception for unsupported $tableName
+     */
+    protected function buildConversionTable($tableName)
+    {
+        switch($tableName) {
+            case 'xml_iso88591_Entities':
+                if (count($this->xml_iso88591_Entities['in'])) {
+                    return;
+                }
+                for ($i = 0; $i < 32; $i++) {
+                    $this->xml_iso88591_Entities["in"][] = chr($i);
+                    $this->xml_iso88591_Entities["out"][] = "&#{$i};";
+                }
 
-        /*for ($i = 128; $i < 160; $i++)
-        {
-            $this->xml_cp1252_Entities['in'][] = chr($i);
-        }*/
+                for ($i = 160; $i < 256; $i++) {
+                    $this->xml_iso88591_Entities["in"][] = chr($i);
+                    $this->xml_iso88591_Entities["out"][] = "&#{$i};";
+                }
+                break;
+            /*case 'xml_cp1252_Entities':
+                if (count($this->xml_cp1252_Entities['in'])) {
+                    return;
+                }
+                for ($i = 128; $i < 160; $i++)
+                {
+                    $this->xml_cp1252_Entities['in'][] = chr($i);
+                }
+                $this->xml_cp1252_Entities['out'] = array(
+                    '&#x20AC;', '?',        '&#x201A;', '&#x0192;',
+                    '&#x201E;', '&#x2026;', '&#x2020;', '&#x2021;',
+                    '&#x02C6;', '&#x2030;', '&#x0160;', '&#x2039;',
+                    '&#x0152;', '?',        '&#x017D;', '?',
+                    '?',        '&#x2018;', '&#x2019;', '&#x201C;',
+                    '&#x201D;', '&#x2022;', '&#x2013;', '&#x2014;',
+                    '&#x02DC;', '&#x2122;', '&#x0161;', '&#x203A;',
+                    '&#x0153;', '?',        '&#x017E;', '&#x0178;'
+                );
+                $this->buildConversionTable('xml_iso88591_Entities');
+                break;*/
+            default:
+                throw new \Exception('Unsupported table: ' . $tableName);
+        }
     }
 
     /**
@@ -105,6 +127,7 @@ class Charset
         switch ($conversion) {
             case 'ISO-8859-1_':
             case 'ISO-8859-1_US-ASCII':
+                $this->buildConversionTable('xml_iso88591_Entities');
                 $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $data);
                 $escapedData = str_replace($this->xml_iso88591_Entities['in'], $this->xml_iso88591_Entities['out'], $escapedData);
                 break;
@@ -195,6 +218,7 @@ class Charset
 
                 // when converting to latin-1, do not be so eager with using entities for characters 160-255
                 if ($conversion == 'UTF-8_ISO-8859-1') {
+                    $this->buildConversionTable('xml_iso88591_Entities');
                     $escapedData = str_replace(array_slice($this->xml_iso88591_Entities['out'], 32), array_slice($this->xml_iso88591_Entities['in'], 32), $escapedData);
                 }
                 break;
@@ -202,17 +226,20 @@ class Charset
             /*
             case 'CP1252_':
             case 'CP1252_US-ASCII':
+                $this->buildConversionTable('xml_cp1252_Entities');
                 $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $data);
                 $escapedData = str_replace($this->xml_iso88591_Entities']['in'], $this->xml_iso88591_Entities['out'], $escapedData);
                 $escapedData = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escapedData);
                 break;
             case 'CP1252_UTF-8':
+                $this->buildConversionTable('xml_cp1252_Entities');
                 $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $data);
-                /// @todo we could use real UTF8 chars here instead of xml entities... (note that utf_8 encode all allone will NOT convert them)
+                /// @todo we could use real UTF8 chars here instead of xml entities... (note that utf_8 encode all alone will NOT convert them)
                 $escapedData = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escapedData);
                 $escapedData = utf8_encode($escapedData);
                 break;
             case 'CP1252_ISO-8859-1':
+                $this->buildConversionTable('xml_cp1252_Entities');
                 $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $data);
                 // we might as well replace all funky chars with a '?' here, but we are kind and leave it to the receiving application layer to decide what to do with these weird entities...
                 $escapedData = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escapedData);