[ Index ]

PHP Cross Reference of phpBB-3.3.0-deutsch

title

Body

[close]

/vendor/patchwork/utf8/src/Patchwork/Utf8/ -> BestFit.php (source)

   1  <?php
   2  
   3  /*
   4   * Copyright (C) 2016 Nicolas Grekas - p@tchwork.com
   5   *
   6   * This library is free software; you can redistribute it and/or modify it
   7   * under the terms of the (at your option):
   8   * Apache License v2.0 (http://apache.org/licenses/LICENSE-2.0.txt), or
   9   * GNU General Public License v2.0 (http://gnu.org/licenses/gpl-2.0.txt).
  10   */
  11  
  12  namespace Patchwork\Utf8;
  13  
  14  /**
  15   * UTF-8 to Code Page conversion using best fit mappings
  16   * See http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WindowsBestFit/.
  17   */
  18  class BestFit
  19  {
  20      public static function fit($cp, $s, $placeholder = '?')
  21      {
  22          if (!$len = strlen($s)) {
  23              return 0 === $len ? '' : false;
  24          }
  25  
  26          static $map = array();
  27          static $ulen_mask = array("\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4);
  28  
  29          $s .= '';
  30          $cp = (string) (int) $cp;
  31          $result = '9' === $cp[0] ? $s.$s : $s;
  32  
  33          if ('932' === $cp && 2 === func_num_args()) {
  34              $placeholder = "\x81\x45"; // Katakana Middle Dot in CP932
  35          }
  36  
  37          if (!isset($map[$cp])) {
  38              $i = static::getData('to.bestfit'.$cp);
  39              if (false === $i) {
  40                  return false;
  41              }
  42              $map[$cp] = $i;
  43          }
  44  
  45          $i = $j = 0;
  46          $cp = $map[$cp];
  47  
  48          while ($i < $len) {
  49              if ($s[$i] < "\x80") {
  50                  $uchr = $s[$i++];
  51              } else {
  52                  $ulen = $ulen_mask[$s[$i] & "\xF0"];
  53                  $uchr = substr($s, $i, $ulen);
  54                  $i += $ulen;
  55              }
  56  
  57              if (isset($cp[$uchr])) {
  58                  $uchr = $cp[$uchr];
  59              } else {
  60                  $uchr = $placeholder;
  61              }
  62  
  63              isset($uchr[0]) and $result[$j++] = $uchr[0];
  64              isset($uchr[1]) and $result[$j++] = $uchr[1];
  65          }
  66  
  67          return substr($result, 0, $j);
  68      }
  69  
  70      protected static function getData($file)
  71      {
  72          $file = __DIR__.'/data/'.$file.'.ser';
  73          if (file_exists($file)) {
  74              return unserialize(file_get_contents($file));
  75          } else {
  76              return false;
  77          }
  78      }
  79  }


Generated: Tue Apr 7 19:44:41 2020 Cross-referenced by PHPXref 0.7.1