PageRenderTime 27ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/patchwork/utf8/src/Patchwork/PHP/Shim/Intl.php

https://gitlab.com/xolotsoft/pumasruiz
PHP | 221 lines | 164 code | 32 blank | 25 comment | 37 complexity | 72d9d8c5ed13a99f518b5f9dd1510b55 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright (C) 2013 Nicolas Grekas - p@tchwork.com
  4. *
  5. * This library is free software; you can redistribute it and/or modify it
  6. * under the terms of the (at your option):
  7. * Apache License v2.0 (http://apache.org/licenses/LICENSE-2.0.txt), or
  8. * GNU General Public License v2.0 (http://gnu.org/licenses/gpl-2.0.txt).
  9. */
  10. namespace Patchwork\PHP\Shim;
  11. /**
  12. * Partial intl implementation in pure PHP.
  13. *
  14. * Implemented:
  15. * - grapheme_extract - Extract a sequence of grapheme clusters from a text buffer, which must be encoded in UTF-8
  16. * - grapheme_stripos - Find position (in grapheme units) of first occurrence of a case-insensitive string
  17. * - grapheme_stristr - Returns part of haystack string from the first occurrence of case-insensitive needle to the end of haystack
  18. * - grapheme_strlen - Get string length in grapheme units
  19. * - grapheme_strpos - Find position (in grapheme units) of first occurrence of a string
  20. * - grapheme_strripos - Find position (in grapheme units) of last occurrence of a case-insensitive string
  21. * - grapheme_strrpos - Find position (in grapheme units) of last occurrence of a string
  22. * - grapheme_strstr - Returns part of haystack string from the first occurrence of needle to the end of haystack
  23. * - grapheme_substr - Return part of a string
  24. */
  25. class Intl
  26. {
  27. // (CRLF|([ZWNJ-ZWJ]|T+|L*(LV?V+|LV|LVT)T*|L+|[^Control])[Extend]*|[Control])
  28. // This regular expression is a work around for http://bugs.exim.org/1279
  29. 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}])';
  30. public static function grapheme_extract($s, $size, $type = GRAPHEME_EXTR_COUNT, $start = 0, &$next = 0)
  31. {
  32. if (!is_scalar($s)) {
  33. $hasError = false;
  34. set_error_handler(function () use (&$hasError) {$hasError = true;});
  35. $next = substr($s, $start);
  36. restore_error_handler();
  37. if ($hasError) {
  38. substr($s, $start);
  39. $s = '';
  40. } else {
  41. $s = $next;
  42. }
  43. } else {
  44. $s = substr($s, $start);
  45. }
  46. $size = (int) $size;
  47. $type = (int) $type;
  48. $start = (int) $start;
  49. if (!isset($s[0]) || 0 > $size || 0 > $start || 0 > $type || 2 < $type) {
  50. return false;
  51. }
  52. if (0 === $size) {
  53. return '';
  54. }
  55. $next = $start;
  56. $s = preg_split('/('.GRAPHEME_CLUSTER_RX.')/u', "\r\n".$s, $size + 1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
  57. if (!isset($s[1])) {
  58. return false;
  59. }
  60. $i = 1;
  61. $ret = '';
  62. do {
  63. if (GRAPHEME_EXTR_COUNT === $type) {
  64. --$size;
  65. } elseif (GRAPHEME_EXTR_MAXBYTES === $type) {
  66. $size -= strlen($s[$i]);
  67. } else {
  68. $size -= iconv_strlen($s[$i], 'UTF-8//IGNORE');
  69. }
  70. if ($size >= 0) {
  71. $ret .= $s[$i];
  72. }
  73. } while (isset($s[++$i]) && $size > 0);
  74. $next += strlen($ret);
  75. return $ret;
  76. }
  77. public static function grapheme_strlen($s)
  78. {
  79. preg_replace('/'.GRAPHEME_CLUSTER_RX.'/u', '', $s, -1, $len);
  80. return 0 === $len && '' !== $s ? null : $len;
  81. }
  82. public static function grapheme_substr($s, $start, $len = 2147483647)
  83. {
  84. preg_match_all('/'.GRAPHEME_CLUSTER_RX.'/u', $s, $s);
  85. $slen = count($s[0]);
  86. $start = (int) $start;
  87. if (0 > $start) {
  88. $start += $slen;
  89. }
  90. if (0 > $start) {
  91. return false;
  92. }
  93. if ($start >= $slen) {
  94. return false;
  95. }
  96. $rem = $slen - $start;
  97. if (0 > $len) {
  98. $len += $rem;
  99. }
  100. if (0 === $len) {
  101. return '';
  102. }
  103. if (0 > $len) {
  104. return false;
  105. }
  106. if ($len > $rem) {
  107. $len = $rem;
  108. }
  109. return implode('', array_slice($s[0], $start, $len));
  110. }
  111. public static function grapheme_substr_workaround62759($s, $start, $len)
  112. {
  113. // Intl based http://bugs.php.net/62759 and 55562 workaround
  114. if (2147483647 == $len) {
  115. return grapheme_substr($s, $start);
  116. }
  117. $s .= '';
  118. $slen = grapheme_strlen($s);
  119. $start = (int) $start;
  120. if (0 > $start) {
  121. $start += $slen;
  122. }
  123. if (0 > $start) {
  124. return false;
  125. }
  126. if ($start >= $slen) {
  127. return false;
  128. }
  129. $rem = $slen - $start;
  130. if (0 > $len) {
  131. $len += $rem;
  132. }
  133. if (0 === $len) {
  134. return '';
  135. }
  136. if (0 > $len) {
  137. return false;
  138. }
  139. if ($len > $rem) {
  140. $len = $rem;
  141. }
  142. return grapheme_substr($s, $start, $len);
  143. }
  144. public static function grapheme_strpos($s, $needle, $offset = 0)
  145. {
  146. return self::grapheme_position($s, $needle, $offset, 0);
  147. }
  148. public static function grapheme_stripos($s, $needle, $offset = 0)
  149. {
  150. return self::grapheme_position($s, $needle, $offset, 1);
  151. }
  152. public static function grapheme_strrpos($s, $needle, $offset = 0)
  153. {
  154. return self::grapheme_position($s, $needle, $offset, 2);
  155. }
  156. public static function grapheme_strripos($s, $needle, $offset = 0)
  157. {
  158. return self::grapheme_position($s, $needle, $offset, 3);
  159. }
  160. public static function grapheme_stristr($s, $needle, $before_needle = false)
  161. {
  162. return mb_stristr($s, $needle, $before_needle, 'UTF-8');
  163. }
  164. public static function grapheme_strstr($s, $needle, $before_needle = false)
  165. {
  166. return mb_strstr($s, $needle, $before_needle, 'UTF-8');
  167. }
  168. protected static function grapheme_position($s, $needle, $offset, $mode)
  169. {
  170. if (!preg_match('/./us', $needle .= '')) {
  171. return false;
  172. }
  173. if (!preg_match('/./us', $s .= '')) {
  174. return false;
  175. }
  176. if ($offset > 0) {
  177. $s = self::grapheme_substr($s, $offset);
  178. } elseif ($offset < 0) {
  179. $offset = 0;
  180. }
  181. switch ($mode) {
  182. case 0: $needle = iconv_strpos($s, $needle, 0, 'UTF-8'); break;
  183. case 1: $needle = mb_stripos($s, $needle, 0, 'UTF-8'); break;
  184. case 2: $needle = iconv_strrpos($s, $needle, 'UTF-8'); break;
  185. default: $needle = mb_strripos($s, $needle, 0, 'UTF-8'); break;
  186. }
  187. return $needle ? self::grapheme_strlen(iconv_substr($s, 0, $needle, 'UTF-8')) + $offset : $needle;
  188. }
  189. }