Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RunLength.php
Go to the documentation of this file.
1 <?php
24 #require_once 'Zend/Pdf/Filter/Interface.php';
25 
34 {
43  public static function encode($data, $params = null)
44  {
45  $output = '';
46 
47  $chainStartOffset = 0;
48  $offset = 0;
49 
50  while ($offset < strlen($data)) {
51  // Do not encode 2 char chains since they produce 2 char run sequence,
52  // but it takes more time to decode such output (because of processing additional run)
53  if (($repeatedCharChainLength = strspn($data, $data[$offset], $offset + 1, 127) + 1) > 2) {
54  if ($chainStartOffset != $offset) {
55  // Drop down previouse (non-repeatable chars) run
56  $output .= chr($offset - $chainStartOffset - 1)
57  . substr($data, $chainStartOffset, $offset - $chainStartOffset);
58  }
59 
60  $output .= chr(257 - $repeatedCharChainLength) . $data[$offset];
61 
62  $offset += $repeatedCharChainLength;
63  $chainStartOffset = $offset;
64  } else {
65  $offset++;
66 
67  if ($offset - $chainStartOffset == 128) {
68  // Maximum run length is reached
69  // Drop down non-repeatable chars run
70  $output .= "\x7F" . substr($data, $chainStartOffset, 128);
71 
72  $chainStartOffset = $offset;
73  }
74  }
75  }
76 
77  if ($chainStartOffset != $offset) {
78  // Drop down non-repeatable chars run
79  $output .= chr($offset - $chainStartOffset - 1) . substr($data, $chainStartOffset, $offset - $chainStartOffset);
80  }
81 
82  $output .= "\x80";
83 
84  return $output;
85  }
86 
95  public static function decode($data, $params = null)
96  {
97  $dataLength = strlen($data);
98  $output = '';
99  $offset = 0;
100 
101  while($offset < $dataLength) {
102  $length = ord($data[$offset]);
103 
104  $offset++;
105 
106  if ($length == 128) {
107  // EOD byte
108  break;
109  } else if ($length < 128) {
110  $length++;
111 
112  $output .= substr($data, $offset, $length);
113 
114  $offset += $length;
115  } else if ($length > 128) {
116  $output .= str_repeat($data[$offset], 257 - $length);
117 
118  $offset++;
119  }
120  }
121 
122  return $output;
123  }
124 }
125 
static encode($data, $params=null)
Definition: RunLength.php:43
static decode($data, $params=null)
Definition: RunLength.php:95
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18