Blame view

sources/3rdparty/Patchwork/PHP/Shim/Xml.php 1.48 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  <?php // vi: set fenc=utf-8 ts=4 sw=4 et:
  /*
   * Copyright (C) 2013 Nicolas Grekas - p@tchwork.com
   *
   * This library is free software; you can redistribute it and/or modify it
   * under the terms of the (at your option):
   * Apache License v2.0 (http://apache.org/licenses/LICENSE-2.0.txt), or
   * GNU General Public License v2.0 (http://gnu.org/licenses/gpl-2.0.txt).
   */
  
  namespace Patchwork\PHP\Shim;
  
  /**
   * utf8_encode/decode
   */
  class Xml
  {
      static function utf8_encode($s)
      {
          $len = strlen($s);
          $e = $s . $s;
  
          for ($i = 0, $j = 0; $i < $len; ++$i, ++$j) switch (true)
          {
          case $s[$i] < "\x80": $e[$j] = $s[$i]; break;
          case $s[$i] < "\xC0": $e[$j] = "\xC2"; $e[++$j] = $s[$i]; break;
          default:              $e[$j] = "\xC3"; $e[++$j] = chr(ord($s[$i]) - 64); break;
          }
  
          return substr($e, 0, $j);
      }
  
      static function utf8_decode($s)
      {
          $len = strlen($s);
  
          for ($i = 0, $j = 0; $i < $len; ++$i, ++$j)
          {
              switch ($s[$i] & "\xF0")
              {
              case "\xC0":
              case "\xD0":
                  $c = (ord($s[$i] & "\x1F") << 6) | ord($s[++$i] & "\x3F");
                  $s[$j] = $c < 256 ? chr($c) : '?';
                  break;
  
              case "\xF0": ++$i;
              case "\xE0":
                  $s[$j] = '?';
                  $i += 2;
                  break;
  
              default:
                  $s[$j] = $s[$i];
              }
          }
  
          return substr($s, 0, $j);
      }
  }