[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/patchwork/utf8/src/Patchwork/PHP/Shim/ -> Intl.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\PHP\Shim;
  13  
  14  /**
  15   * Partial intl implementation in pure PHP.
  16   *
  17   * Implemented:
  18   * - grapheme_extract  - Extract a sequence of grapheme clusters from a text buffer, which must be encoded in UTF-8
  19   * - grapheme_stripos  - Find position (in grapheme units) of first occurrence of a case-insensitive string
  20   * - grapheme_stristr  - Returns part of haystack string from the first occurrence of case-insensitive needle to the end of haystack
  21   * - grapheme_strlen   - Get string length in grapheme units
  22   * - grapheme_strpos   - Find position (in grapheme units) of first occurrence of a string
  23   * - grapheme_strripos - Find position (in grapheme units) of last occurrence of a case-insensitive string
  24   * - grapheme_strrpos  - Find position (in grapheme units) of last occurrence of a string
  25   * - grapheme_strstr   - Returns part of haystack string from the first occurrence of needle to the end of haystack
  26   * - grapheme_substr   - Return part of a string
  27   *
  28   * @internal
  29   */
  30  class Intl
  31  {
  32      // (CRLF|([ZWNJ-ZWJ]|T+|L*(LV?V+|LV|LVT)T*|L+|[^Control])[Extend]*|[Control])
  33      // This regular expression is a work around for http://bugs.exim.org/1279
  34      const GRAPHEME_CLUSTER_RX = '(?:\r\n|(?:[ -~\x{200C}\x{200D}]|[ᆨ-ᇹ]+|[ᄀ-ᅟ]*(?:[가개갸걔거게겨계고과괘괴교구궈궤귀규그긔기까깨꺄꺠꺼께껴꼐꼬꽈꽤꾀꾜꾸꿔꿰뀌뀨끄끠끼나내냐냬너네녀녜노놔놰뇌뇨누눠눼뉘뉴느늬니다대댜댸더데뎌뎨도돠돼되됴두둬뒈뒤듀드듸디따때땨떄떠떼뗘뗴또똬뙈뙤뚀뚜뚸뛔뛰뜌뜨띄띠라래랴럐러레려례로롸뢔뢰료루뤄뤠뤼류르릐리마매먀먜머메며몌모뫄뫠뫼묘무뭐뭬뮈뮤므믜미바배뱌뱨버베벼볘보봐봬뵈뵤부붜붸뷔뷰브븨비빠빼뺘뺴뻐뻬뼈뼤뽀뽜뽸뾔뾰뿌뿨쀄쀠쀼쁘쁴삐사새샤섀서세셔셰소솨쇄쇠쇼수숴쉐쉬슈스싀시싸쌔쌰썌써쎄쎠쎼쏘쏴쐐쐬쑈쑤쒀쒜쒸쓔쓰씌씨아애야얘어에여예오와왜외요우워웨위유으의이자재쟈쟤저제져졔조좌좨죄죠주줘줴쥐쥬즈즤지짜째쨔쨰쩌쩨쪄쪠쪼쫘쫴쬐쬬쭈쭤쮀쮜쮸쯔쯰찌차채챠챼처체쳐쳬초촤쵀최쵸추춰췌취츄츠츼치카캐캬컈커케켜켸코콰쾌쾨쿄쿠쿼퀘퀴큐크킈키타태탸턔터테텨톄토톼퇘퇴툐투퉈퉤튀튜트틔티파패퍄퍠퍼페펴폐포퐈퐤푀표푸풔풰퓌퓨프픠피하해햐햬허헤혀혜호화홰회효후훠훼휘휴흐희히]?[ᅠ-ᆢ]+|[가-힣])[ᆨ-ᇹ]*|[ᄀ-ᅟ]+|[^\p{Cc}\p{Cf}\p{Zl}\p{Zp}])[\p{Mn}\p{Me}\x{09BE}\x{09D7}\x{0B3E}\x{0B57}\x{0BBE}\x{0BD7}\x{0CC2}\x{0CD5}\x{0CD6}\x{0D3E}\x{0D57}\x{0DCF}\x{0DDF}\x{200C}\x{200D}\x{1D165}\x{1D16E}-\x{1D172}]*|[\p{Cc}\p{Cf}\p{Zl}\p{Zp}])';
  35  
  36      public static function grapheme_extract($s, $size, $type = GRAPHEME_EXTR_COUNT, $start = 0, &$next = 0)
  37      {
  38          if (!is_scalar($s)) {
  39              $hasError = false;
  40              set_error_handler(function () use (&$hasError) {$hasError = true;});
  41              $next = substr($s, $start);
  42              restore_error_handler();
  43              if ($hasError) {
  44                  substr($s, $start);
  45                  $s = '';
  46              } else {
  47                  $s = $next;
  48              }
  49          } else {
  50              $s = substr($s, $start);
  51          }
  52          $size = (int) $size;
  53          $type = (int) $type;
  54          $start = (int) $start;
  55  
  56          if (!isset($s[0]) || 0 > $size || 0 > $start || 0 > $type || 2 < $type) {
  57              return false;
  58          }
  59          if (0 === $size) {
  60              return '';
  61          }
  62  
  63          $next = $start;
  64  
  65          $s = preg_split('/('.GRAPHEME_CLUSTER_RX.')/u', "\r\n".$s, $size + 1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
  66  
  67          if (!isset($s[1])) {
  68              return false;
  69          }
  70  
  71          $i = 1;
  72          $ret = '';
  73  
  74          do {
  75              if (GRAPHEME_EXTR_COUNT === $type) {
  76                  --$size;
  77              } elseif (GRAPHEME_EXTR_MAXBYTES === $type) {
  78                  $size -= strlen($s[$i]);
  79              } else {
  80                  $size -= iconv_strlen($s[$i], 'UTF-8//IGNORE');
  81              }
  82  
  83              if ($size >= 0) {
  84                  $ret .= $s[$i];
  85              }
  86          } while (isset($s[++$i]) && $size > 0);
  87  
  88          $next += strlen($ret);
  89  
  90          return $ret;
  91      }
  92  
  93      public static function grapheme_strlen($s)
  94      {
  95          preg_replace('/'.GRAPHEME_CLUSTER_RX.'/u', '', $s, -1, $len);
  96  
  97          return 0 === $len && '' !== $s ? null : $len;
  98      }
  99  
 100      public static function grapheme_substr($s, $start, $len = 2147483647)
 101      {
 102          preg_match_all('/'.GRAPHEME_CLUSTER_RX.'/u', $s, $s);
 103  
 104          $slen = count($s[0]);
 105          $start = (int) $start;
 106  
 107          if (0 > $start) {
 108              $start += $slen;
 109          }
 110          if (0 > $start) {
 111              return false;
 112          }
 113          if ($start >= $slen) {
 114              return false;
 115          }
 116  
 117          $rem = $slen - $start;
 118  
 119          if (0 > $len) {
 120              $len += $rem;
 121          }
 122          if (0 === $len) {
 123              return '';
 124          }
 125          if (0 > $len) {
 126              return false;
 127          }
 128          if ($len > $rem) {
 129              $len = $rem;
 130          }
 131  
 132          return implode('', array_slice($s[0], $start, $len));
 133      }
 134  
 135      public static function grapheme_substr_workaround62759($s, $start, $len)
 136      {
 137          // Intl based http://bugs.php.net/62759 and 55562 workaround
 138  
 139          if (2147483647 == $len) {
 140              return grapheme_substr($s, $start);
 141          }
 142  
 143          $s .= '';
 144          $slen = grapheme_strlen($s);
 145          $start = (int) $start;
 146  
 147          if (0 > $start) {
 148              $start += $slen;
 149          }
 150          if (0 > $start) {
 151              return false;
 152          }
 153          if ($start >= $slen) {
 154              return false;
 155          }
 156  
 157          $rem = $slen - $start;
 158  
 159          if (0 > $len) {
 160              $len += $rem;
 161          }
 162          if (0 === $len) {
 163              return '';
 164          }
 165          if (0 > $len) {
 166              return false;
 167          }
 168          if ($len > $rem) {
 169              $len = $rem;
 170          }
 171  
 172          return grapheme_substr($s, $start, $len);
 173      }
 174  
 175      public static function grapheme_strpos($s, $needle, $offset = 0)
 176      {
 177          return self::grapheme_position($s, $needle, $offset, 0);
 178      }
 179  
 180      public static function grapheme_stripos($s, $needle, $offset = 0)
 181      {
 182          return self::grapheme_position($s, $needle, $offset, 1);
 183      }
 184  
 185      public static function grapheme_strrpos($s, $needle, $offset = 0)
 186      {
 187          return self::grapheme_position($s, $needle, $offset, 2);
 188      }
 189  
 190      public static function grapheme_strripos($s, $needle, $offset = 0)
 191      {
 192          return self::grapheme_position($s, $needle, $offset, 3);
 193      }
 194  
 195      public static function grapheme_stristr($s, $needle, $beforeNeedle = false)
 196      {
 197          return mb_stristr($s, $needle, $beforeNeedle, 'UTF-8');
 198      }
 199  
 200      public static function grapheme_strstr($s, $needle, $beforeNeedle = false)
 201      {
 202          return mb_strstr($s, $needle, $beforeNeedle, 'UTF-8');
 203      }
 204  
 205      private static function grapheme_position($s, $needle, $offset, $mode)
 206      {
 207          if (!preg_match('/./us', $needle .= '')) {
 208              return false;
 209          }
 210          if (!preg_match('/./us', $s .= '')) {
 211              return false;
 212          }
 213          if ($offset > 0) {
 214              $s = self::grapheme_substr($s, $offset);
 215          } elseif ($offset < 0) {
 216              if (defined('HHVM_VERSION_ID') || PHP_VERSION_ID < 50535 || (50600 <= PHP_VERSION_ID && PHP_VERSION_ID < 50621) || (70000 <= PHP_VERSION_ID && PHP_VERSION_ID < 70006)) {
 217                  $offset = 0;
 218              } else {
 219                  return false;
 220              }
 221          }
 222  
 223          switch ($mode) {
 224              case 0: $needle = iconv_strpos($s, $needle, 0, 'UTF-8'); break;
 225              case 1: $needle = mb_stripos($s, $needle, 0, 'UTF-8'); break;
 226              case 2: $needle = iconv_strrpos($s, $needle, 'UTF-8'); break;
 227              default: $needle = mb_strripos($s, $needle, 0, 'UTF-8'); break;
 228          }
 229  
 230          return $needle ? self::grapheme_strlen(iconv_substr($s, 0, $needle, 'UTF-8')) + $offset : $needle;
 231      }
 232  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1