[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/s9e/regexp-builder/src/Output/ -> PrintableAscii.php (source)

   1  <?php declare(strict_types=1);
   2  
   3  /**
   4  * @package   s9e\RegexpBuilder
   5  * @copyright Copyright (c) 2016-2022 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\RegexpBuilder\Output;
   9  
  10  use function chr, sprintf;
  11  
  12  abstract class PrintableAscii extends BaseImplementation
  13  {
  14      /**
  15      * @var string 'x' for lowercase hexadecimal symbols, 'X' for uppercase
  16      */
  17      protected $hexCase;
  18  
  19      /**
  20      * {@inheritdoc}
  21      */
  22  	public function __construct(array $options = [])
  23      {
  24          $this->hexCase = (isset($options['case']) && $options['case'] === 'lower') ? 'x' : 'X';
  25      }
  26  
  27      /**
  28      * Escape given ASCII codepoint
  29      *
  30      * @param  integer $cp
  31      * @return string
  32      */
  33  	protected function escapeAscii(int $cp): string
  34      {
  35          return '\\x' . sprintf('%02' . $this->hexCase, $cp);
  36      }
  37  
  38      /**
  39      * Escape given control code
  40      *
  41      * @param  integer $cp
  42      * @return string
  43      */
  44  	protected function escapeControlCode(int $cp): string
  45      {
  46          $table = [9 => '\\t', 10 => '\\n', 13 => '\\r'];
  47  
  48          return $table[$cp] ?? $this->escapeAscii($cp);
  49      }
  50  
  51      /**
  52      * Output the representation of a unicode character
  53      *
  54      * @param  integer $cp Unicode codepoint
  55      * @return string
  56      */
  57      abstract protected function escapeUnicode(int $cp): string;
  58  
  59      /**
  60      * {@inheritdoc}
  61      */
  62  	protected function outputValidValue(int $value): string
  63      {
  64          if ($value < 32)
  65          {
  66              return $this->escapeControlCode($value);
  67          }
  68  
  69          if ($value < 127)
  70          {
  71              return chr($value);
  72          }
  73  
  74          return ($value > 255) ? $this->escapeUnicode($value) : $this->escapeAscii($value);
  75      }
  76  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1