diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ff72e2d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/composer.lock +/vendor diff --git a/.travis.yml b/.travis.yml index 624ba26..c6cb335 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,12 +7,15 @@ php: - 5.4 - 5.5 - 5.6 - - nightly + - 7.0 - hhvm +install: + - composer update + script: - phpunit --coverage-text - - if [[ "$TRAVIS_PHP_VERSION" != "nightly" ]] && [[ "$TRAVIS_PHP_VERSION" != "hhvm" ]]; then phpenv config-rm xdebug.ini; fi; + - if [[ "$TRAVIS_PHP_VERSION" != "hhvm" ]]; then phpenv config-rm xdebug.ini; fi; - phpunit --group unicode branches: diff --git a/CHANGELOG.md b/CHANGELOG.md index f287447..f72adf6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +## v1.3.0 (2015-12-15) + +- add shim for mb_convert_variables +- marked all shims as @internal +- test on appveyor +- a few fixes in iconv and mbstring shims +- cleanup refacto for preparing v2 based on symfony-polyfill + ## v1.2.5 (2015-10-14) - handle the third argument of mb_convert_encoding() being an array diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..aba4afd --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,39 @@ +build: false +platform: x86 +clone_folder: c:\projects\utf8 + +cache: + - c:\php -> appveyor.yml + +branches: + only: + - master + +init: + - SET PATH=c:\php;%PATH% + - SET COMPOSER_NO_INTERACTION=1 + - SET PHP=1 + +install: + - IF EXIST c:\php (SET PHP=0) ELSE (mkdir c:\php) + - cd c:\php + - IF %PHP%==1 appveyor DownloadFile http://windows.php.net/downloads/releases/archives/php-5.5.9-nts-Win32-VC11-x86.zip + - IF %PHP%==1 7z x php-5.5.9-nts-Win32-VC11-x86.zip -y >nul + - IF %PHP%==1 del /Q *.zip + - IF %PHP%==1 appveyor DownloadFile https://phar.phpunit.de/phpunit-4.8.15.phar + - IF %PHP%==1 echo @php %%~dp0composer.phar %%* > composer.bat + - IF %PHP%==1 echo @php %%~dp0phpunit-4.8.15.phar %%* > phpunit.bat + - IF %PHP%==1 copy /Y php.ini-development php.ini + - IF %PHP%==1 echo max_execution_time=1200 >> php.ini + - IF %PHP%==1 echo date.timezone="UTC" >> php.ini + - IF %PHP%==1 echo extension_dir=ext >> php.ini + - IF %PHP%==1 echo extension=php_openssl.dll >> php.ini + - IF %PHP%==1 echo extension=php_intl.dll >> php.ini + - IF %PHP%==1 echo extension=php_mbstring.dll >> php.ini + - appveyor DownloadFile https://getcomposer.org/composer.phar + - cd c:\projects\utf8 + - IF %APPVEYOR_REPO_BRANCH%==master (SET COMPOSER_ROOT_VERSION=dev-master) ELSE (SET COMPOSER_ROOT_VERSION=%APPVEYOR_REPO_BRANCH%.x-dev) + - composer update --prefer-source --no-progress --ansi + +test_script: + - phpunit diff --git a/composer.json b/composer.json index 4b35276..6e0e64b 100644 --- a/composer.json +++ b/composer.json @@ -25,9 +25,12 @@ "psr-4": {"Patchwork\\": "src/Patchwork/"}, "classmap": ["src/Normalizer.php"] }, + "autoload-dev": { + "files": ["tests/bootstrap.php"] + }, "extra": { "branch-alias": { - "dev-master": "1.2-dev" + "dev-master": "1.3-dev" } } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index b73ebd8..fa08f1c 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,14 +1,18 @@ - + + + - ./tests/Patchwork/ - ./tests/php-utf8/ - ./tests/unicode/ + ./tests/ @@ -17,5 +21,4 @@ unicode - diff --git a/src/Normalizer.php b/src/Normalizer.php index f9b716c..7c14662 100644 --- a/src/Normalizer.php +++ b/src/Normalizer.php @@ -1,7 +1,7 @@ 'utf-8', 'ascii' => 'us-ascii', 'tis-620' => 'iso-8859-11', @@ -114,15 +116,15 @@ class Iconv 'iso885915' => 'iso-8859-15', 'iso885916' => 'iso-8859-16', ); - protected static $translit_map = array(); - protected static $convert_map = array(); - protected static $error_handler; - protected static $last_error; + private static $translitMap = array(); + private static $convertMap = array(); + private static $errorHandler; + private static $lastError; - protected static $ulen_mask = array("\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4); - protected static $is_valid_utf8; + private static $ulenMask = array("\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4); + private static $isValidUtf8; - public static function iconv($in_charset, $out_charset, $str) + public static function iconv($inCharset, $outCharset, $str) { if ('' === $str .= '') { return ''; @@ -130,81 +132,97 @@ public static function iconv($in_charset, $out_charset, $str) // Prepare for //IGNORE and //TRANSLIT - $TRANSLIT = $IGNORE = ''; + $translit = $ignore = ''; - $out_charset = strtolower($out_charset); - $in_charset = strtolower($in_charset); + $outCharset = strtolower($outCharset); + $inCharset = strtolower($inCharset); - '' === $out_charset and $out_charset = 'iso-8859-1'; - '' === $in_charset and $in_charset = 'iso-8859-1'; + if ('' === $outCharset) { + $outCharset = 'iso-8859-1'; + } + if ('' === $inCharset) { + $inCharset = 'iso-8859-1'; + } - if ('//translit' === substr($out_charset, -10)) { - $TRANSLIT = '//TRANSLIT'; - $out_charset = substr($out_charset, 0, -10); + if ('//translit' === substr($outCharset, -10)) { + $translit = '//TRANSLIT'; + $outCharset = substr($outCharset, 0, -10); } - if ('//ignore' === substr($out_charset, -8)) { - $IGNORE = '//IGNORE'; - $out_charset = substr($out_charset, 0, -8); + if ('//ignore' === substr($outCharset, -8)) { + $ignore = '//IGNORE'; + $outCharset = substr($outCharset, 0, -8); } - '//translit' === substr($in_charset, -10) and $in_charset = substr($in_charset, 0, -10); - '//ignore' === substr($in_charset, -8) and $in_charset = substr($in_charset, 0, -8); + if ('//translit' === substr($inCharset, -10)) { + $inCharset = substr($inCharset, 0, -10); + } + if ('//ignore' === substr($inCharset, -8)) { + $inCharset = substr($inCharset, 0, -8); + } - isset(self::$alias[ $in_charset]) and $in_charset = self::$alias[ $in_charset]; - isset(self::$alias[$out_charset]) and $out_charset = self::$alias[$out_charset]; + if (isset(self::$alias[ $inCharset])) { + $inCharset = self::$alias[ $inCharset]; + } + if (isset(self::$alias[$outCharset])) { + $outCharset = self::$alias[$outCharset]; + } // Load charset maps - if (('utf-8' !== $in_charset && !static::loadMap('from.', $in_charset, $in_map)) - || ('utf-8' !== $out_charset && !static::loadMap('to.', $out_charset, $out_map))) { - user_error(sprintf(self::ERROR_WRONG_CHARSET, $in_charset, $out_charset)); + if (('utf-8' !== $inCharset && !self::loadMap('from.', $inCharset, $inMap)) + || ('utf-8' !== $outCharset && !self::loadMap('to.', $outCharset, $outMap))) { + trigger_error(sprintf(self::ERROR_WRONG_CHARSET, $inCharset, $outCharset)); return false; } - if ('utf-8' !== $in_charset) { + if ('utf-8' !== $inCharset) { // Convert input to UTF-8 $result = ''; - if (self::map_to_utf8($result, $in_map, $str, $IGNORE)) { + if (self::mapToUtf8($result, $inMap, $str, $ignore)) { $str = $result; } else { $str = false; } - self::$is_valid_utf8 = true; + self::$isValidUtf8 = true; } else { - self::$is_valid_utf8 = preg_match('//u', $str); + self::$isValidUtf8 = preg_match('//u', $str); - if (!self::$is_valid_utf8 && !$IGNORE) { - user_error(self::ERROR_ILLEGAL_CHARACTER); + if (!self::$isValidUtf8 && !$ignore) { + trigger_error(self::ERROR_ILLEGAL_CHARACTER); return false; } - if ('utf-8' === $out_charset) { + if ('utf-8' === $outCharset) { // UTF-8 validation - $str = self::utf8_to_utf8($str, $IGNORE); + $str = self::utf8ToUtf8($str, $ignore); } } - if ('utf-8' !== $out_charset && false !== $str) { + if ('utf-8' !== $outCharset && false !== $str) { // Convert output to UTF-8 $result = ''; - if (self::map_from_utf8($result, $out_map, $str, $IGNORE, $TRANSLIT)) { + if (self::mapFromUtf8($result, $outMap, $str, $ignore, $translit)) { return $result; - } else { - return false; } - } else { - return $str; + + return false; } + + return $str; } - public static function iconv_mime_decode_headers($str, $mode = 0, $charset = INF) + public static function iconv_mime_decode_headers($str, $mode = 0, $charset = null) { - INF === $charset and $charset = self::$internal_encoding; + if (null === $charset) { + $charset = self::$internalEncoding; + } - false !== strpos($str, "\r") and $str = strtr(str_replace("\r\n", "\n", $str), "\r", "\n"); + if (false !== strpos($str, "\r")) { + $str = strtr(str_replace("\r\n", "\n", $str), "\r", "\n"); + } $str = explode("\n\n", $str, 2); $headers = array(); @@ -219,7 +237,9 @@ public static function iconv_mime_decode_headers($str, $mode = 0, $charset = INF if (2 === count($str)) { if (isset($headers[$str[0]])) { - is_array($headers[$str[0]]) or $headers[$str[0]] = array($headers[$str[0]]); + if (!is_array($headers[$str[0]])) { + $headers[$str[0]] = array($headers[$str[0]]); + } $headers[$str[0]][] = ltrim($str[1]); } else { $headers[$str[0]] = ltrim($str[1]); @@ -230,14 +250,18 @@ public static function iconv_mime_decode_headers($str, $mode = 0, $charset = INF return $headers; } - public static function iconv_mime_decode($str, $mode = 0, $charset = INF) + public static function iconv_mime_decode($str, $mode = 0, $charset = null) { - INF === $charset and $charset = self::$internal_encoding; + if (null === $charset) { + $charset = self::$internalEncoding; + } if (ICONV_MIME_DECODE_CONTINUE_ON_ERROR & $mode) { $charset .= '//IGNORE'; } - false !== strpos($str, "\r") and $str = strtr(str_replace("\r\n", "\n", $str), "\r", "\n"); + if (false !== strpos($str, "\r")) { + $str = strtr(str_replace("\r\n", "\n", $str), "\r", "\n"); + } $str = preg_split('/\n(?![ \t])/', rtrim($str), 2); $str = preg_replace('/[ \t]*\n[ \t]+/', ' ', rtrim($str[0])); $str = preg_split('/=\?([^?]+)\?([bqBQ])\?(.*?)\?=/', $str, -1, PREG_SPLIT_DELIM_CAPTURE); @@ -255,7 +279,7 @@ public static function iconv_mime_decode($str, $mode = 0, $charset = INF) if ((ICONV_MIME_DECODE_CONTINUE_ON_ERROR & $mode) && 'utf-8' !== $c && !isset(self::$alias[$c]) - && !static::loadMap('from.', $c, $d)) { + && !self::loadMap('from.', $c, $d)) { $d = false; } elseif ('B' === strtoupper($str[$i + 1])) { $d = base64_decode($str[$i + 2]); @@ -264,7 +288,13 @@ public static function iconv_mime_decode($str, $mode = 0, $charset = INF) } if (false !== $d) { - $result .= self::iconv($c, $charset, $d); + if ('' !== $d) { + if ('' === $d = self::iconv($c, $charset, $d)) { + $str[$i + 3] = substr($str[$i + 3], 1); + } else { + $result .= $d; + } + } $d = self::iconv('utf-8', $charset, $str[$i + 3]); if ('' !== trim($d)) { $result .= $d; @@ -285,24 +315,24 @@ public static function iconv_mime_decode($str, $mode = 0, $charset = INF) public static function iconv_get_encoding($type = 'all') { switch ($type) { - case 'input_encoding' : return self::$input_encoding; - case 'output_encoding' : return self::$output_encoding; - case 'internal_encoding': return self::$internal_encoding; + case 'input_encoding': return self::$inputEncoding; + case 'output_encoding': return self::$outputEncoding; + case 'internal_encoding': return self::$internalEncoding; } return array( - 'input_encoding' => self::$input_encoding, - 'output_encoding' => self::$output_encoding, - 'internal_encoding' => self::$internal_encoding, + 'input_encoding' => self::$inputEncoding, + 'output_encoding' => self::$outputEncoding, + 'internal_encoding' => self::$internalEncoding, ); } public static function iconv_set_encoding($type, $charset) { switch ($type) { - case 'input_encoding' : self::$input_encoding = $charset; break; - case 'output_encoding' : self::$output_encoding = $charset; break; - case 'internal_encoding': self::$internal_encoding = $charset; break; + case 'input_encoding': self::$inputEncoding = $charset; break; + case 'output_encoding': self::$outputEncoding = $charset; break; + case 'internal_encoding': self::$internalEncoding = $charset; break; default: return false; } @@ -310,39 +340,43 @@ public static function iconv_set_encoding($type, $charset) return true; } - public static function iconv_mime_encode($field_name, $field_value, $pref = INF) + public static function iconv_mime_encode($fieldName, $fieldValue, $pref = null) { - is_array($pref) or $pref = array(); + if (!is_array($pref)) { + $pref = array(); + } $pref += array( - 'scheme' => 'B', - 'input-charset' => self::$internal_encoding, - 'output-charset' => self::$internal_encoding, - 'line-length' => 76, + 'scheme' => 'B', + 'input-charset' => self::$internalEncoding, + 'output-charset' => self::$internalEncoding, + 'line-length' => 76, 'line-break-chars' => "\r\n", ); - preg_match('/[\x80-\xFF]/', $field_name) and $field_name = ''; + if (preg_match('/[\x80-\xFF]/', $fieldName)) { + $fieldName = ''; + } $scheme = strtoupper(substr($pref['scheme'], 0, 1)); - $in = strtolower($pref['input-charset']); + $in = strtolower($pref['input-charset']); $out = strtolower($pref['output-charset']); - if ('utf-8' !== $in and false === $field_value = self::iconv($in, 'utf-8', $field_value)) { + if ('utf-8' !== $in && false === $fieldValue = self::iconv($in, 'utf-8', $fieldValue)) { return false; } - preg_match_all('/./us', $field_value, $chars); + preg_match_all('/./us', $fieldValue, $chars); $chars = isset($chars[0]) ? $chars[0] : array(); - $line_break = (int) $pref['line-length']; - $line_start = "=?{$pref['output-charset']}?{$scheme}?"; - $line_length = strlen($field_name) + 2 + strlen($line_start) + 2; - $line_offset = strlen($line_start) + 3; - $line_data = ''; + $lineBreak = (int) $pref['line-length']; + $lineStart = "=?{$pref['output-charset']}?{$scheme}?"; + $lineLength = strlen($fieldName) + 2 + strlen($lineStart) + 2; + $lineOffset = strlen($lineStart) + 3; + $lineData = ''; - $field_value = array(); + $fieldValue = array(); $Q = 'Q' === $scheme; @@ -354,39 +388,45 @@ public static function iconv_mime_encode($field_name, $field_value, $pref = INF) $o = $Q ? $c = preg_replace_callback( '/[=_\?\x00-\x1F\x80-\xFF]/', - array(__CLASS__, 'qp_byte_callback'), + array(__CLASS__, 'qpByteCallback'), $c ) - : base64_encode($line_data.$c); + : base64_encode($lineData.$c); - if (isset($o[$line_break - $line_length])) { - $Q or $line_data = base64_encode($line_data); - $field_value[] = $line_start.$line_data.'?='; - $line_length = $line_offset; - $line_data = ''; + if (isset($o[$lineBreak - $lineLength])) { + if (!$Q) { + $lineData = base64_encode($lineData); + } + $fieldValue[] = $lineStart.$lineData.'?='; + $lineLength = $lineOffset; + $lineData = ''; } - $line_data .= $c; - $Q && $line_length += strlen($c); + $lineData .= $c; + $Q && $lineLength += strlen($c); } - if ('' !== $line_data) { - $Q or $line_data = base64_encode($line_data); - $field_value[] = $line_start.$line_data.'?='; + if ('' !== $lineData) { + if (!$Q) { + $lineData = base64_encode($lineData); + } + $fieldValue[] = $lineStart.$lineData.'?='; } - return $field_name.': '.implode($pref['line-break-chars'].' ', $field_value); + return $fieldName.': '.implode($pref['line-break-chars'].' ', $fieldValue); } public static function ob_iconv_handler($buffer, $mode) { - return self::iconv(self::$internal_encoding, self::$output_encoding, $buffer); + return self::iconv(self::$internalEncoding, self::$outputEncoding, $buffer); } - public static function iconv_strlen($s, $encoding = INF) + public static function iconv_strlen($s, $encoding = null) { - static $hasXml; - isset($hasXml) or $hasXml = extension_loaded('xml'); + static $hasXml = null; + if (null === $hasXml) { + $hasXml = extension_loaded('xml'); + } if ($hasXml) { return self::strlen1($s, $encoding); @@ -395,24 +435,28 @@ public static function iconv_strlen($s, $encoding = INF) return self::strlen2($s, $encoding); } - public static function strlen1($s, $encoding = INF) + public static function strlen1($s, $encoding = null) { - INF === $encoding and $encoding = self::$internal_encoding; - if (0 !== strncasecmp($encoding, 'utf-8', 5) && false === $s = self::iconv($encoding, 'utf-8', $s)) { + if (null === $encoding) { + $encoding = self::$internalEncoding; + } + if (0 !== stripos($encoding, 'utf-8') && false === $s = self::iconv($encoding, 'utf-8', $s)) { return false; } return strlen(utf8_decode($s)); } - public static function strlen2($s, $encoding = INF) + public static function strlen2($s, $encoding = null) { - INF === $encoding and $encoding = self::$internal_encoding; - if (0 !== strncasecmp($encoding, 'utf-8', 5) && false === $s = self::iconv($encoding, 'utf-8', $s)) { + if (null === $encoding) { + $encoding = self::$internalEncoding; + } + if (0 !== stripos($encoding, 'utf-8') && false === $s = self::iconv($encoding, 'utf-8', $s)) { return false; } - $ulen_mask = self::$ulen_mask; + $ulenMask = self::$ulenMask; $i = 0; $j = 0; @@ -420,18 +464,20 @@ public static function strlen2($s, $encoding = INF) while ($i < $len) { $u = $s[$i] & "\xF0"; - $i += isset($ulen_mask[$u]) ? $ulen_mask[$u] : 1; + $i += isset($ulenMask[$u]) ? $ulenMask[$u] : 1; ++$j; } return $j; } - public static function iconv_strpos($haystack, $needle, $offset = 0, $encoding = INF) + public static function iconv_strpos($haystack, $needle, $offset = 0, $encoding = null) { - INF === $encoding and $encoding = self::$internal_encoding; + if (null === $encoding) { + $encoding = self::$internalEncoding; + } - if (0 !== strncasecmp($encoding, 'utf-8', 5)) { + if (0 !== stripos($encoding, 'utf-8')) { if (false === $haystack = self::iconv($encoding, 'utf-8', $haystack)) { return false; } @@ -448,11 +494,13 @@ public static function iconv_strpos($haystack, $needle, $offset = 0, $encoding = return false === $pos ? false : ($offset + ($pos ? self::iconv_strlen(substr($haystack, 0, $pos), 'utf-8') : 0)); } - public static function iconv_strrpos($haystack, $needle, $encoding = INF) + public static function iconv_strrpos($haystack, $needle, $encoding = null) { - INF === $encoding and $encoding = self::$internal_encoding; + if (null === $encoding) { + $encoding = self::$internalEncoding; + } - if (0 !== strncasecmp($encoding, 'utf-8', 5)) { + if (0 !== stripos($encoding, 'utf-8')) { if (false === $haystack = self::iconv($encoding, 'utf-8', $haystack)) { return false; } @@ -466,11 +514,13 @@ public static function iconv_strrpos($haystack, $needle, $encoding = INF) return false === $pos ? false : self::iconv_strlen($pos ? substr($haystack, 0, $pos) : $haystack, 'utf-8'); } - public static function iconv_substr($s, $start, $length = 2147483647, $encoding = INF) + public static function iconv_substr($s, $start, $length = 2147483647, $encoding = null) { - INF === $encoding and $encoding = self::$internal_encoding; - if (0 === strncasecmp($encoding, 'utf-8', 5)) { - $encoding = INF; + if (null === $encoding) { + $encoding = self::$internalEncoding; + } + if (0 !== stripos($encoding, 'utf-8')) { + $encoding = null; } elseif (false === $s = self::iconv($encoding, 'utf-8', $s)) { return false; } @@ -505,40 +555,40 @@ public static function iconv_substr($s, $start, $length = 2147483647, $encoding $length = $rx; } - $rx = '/^'.($start ? self::preg_offset($start) : '').'('.self::preg_offset($length).')/u'; + $rx = '/^'.($start ? self::pregOffset($start) : '').'('.self::pregOffset($length).')/u'; $s = preg_match($rx, $s, $s) ? $s[1] : ''; - if (INF === $encoding) { + if (null === $encoding) { return $s; - } else { - return self::iconv('utf-8', $encoding, $s); } + + return self::iconv('utf-8', $encoding, $s); } - protected static function loadMap($type, $charset, &$map) + private static function loadMap($type, $charset, &$map) { - if (!isset(self::$convert_map[$type.$charset])) { - if (false === $map = static::getData($type.$charset)) { - if ('to.' === $type && static::loadMap('from.', $charset, $map)) { + if (!isset(self::$convertMap[$type.$charset])) { + if (false === $map = self::getData($type.$charset)) { + if ('to.' === $type && self::loadMap('from.', $charset, $map)) { $map = array_flip($map); } else { return false; } } - self::$convert_map[$type.$charset] = $map; + self::$convertMap[$type.$charset] = $map; } else { - $map = self::$convert_map[$type.$charset]; + $map = self::$convertMap[$type.$charset]; } return true; } - protected static function utf8_to_utf8($str, $IGNORE) + private static function utf8ToUtf8($str, $ignore) { - $ulen_mask = self::$ulen_mask; - $valid = self::$is_valid_utf8; + $ulenMask = self::$ulenMask; + $valid = self::$isValidUtf8; $u = $str; $i = $j = 0; @@ -549,16 +599,16 @@ protected static function utf8_to_utf8($str, $IGNORE) $u[$j++] = $str[$i++]; } else { $ulen = $str[$i] & "\xF0"; - $ulen = isset($ulen_mask[$ulen]) ? $ulen_mask[$ulen] : 1; + $ulen = isset($ulenMask[$ulen]) ? $ulenMask[$ulen] : 1; $uchr = substr($str, $i, $ulen); if (1 === $ulen || !($valid || preg_match('/^.$/us', $uchr))) { - if ($IGNORE) { + if ($ignore) { ++$i; continue; } - user_error(self::ERROR_ILLEGAL_CHARACTER); + trigger_error(self::ERROR_ILLEGAL_CHARACTER); return false; } else { @@ -576,7 +626,7 @@ protected static function utf8_to_utf8($str, $IGNORE) return substr($u, 0, $j); } - protected static function map_to_utf8(&$result, $map, $str, $IGNORE) + private static function mapToUtf8(&$result, $map, $str, $ignore) { $len = strlen($str); for ($i = 0; $i < $len; ++$i) { @@ -584,8 +634,8 @@ protected static function map_to_utf8(&$result, $map, $str, $IGNORE) $result .= $map[$str[$i].$str[++$i]]; } elseif (isset($map[$str[$i]])) { $result .= $map[$str[$i]]; - } elseif (!$IGNORE) { - user_error(self::ERROR_ILLEGAL_CHARACTER); + } elseif (!$ignore) { + trigger_error(self::ERROR_ILLEGAL_CHARACTER); return false; } @@ -594,13 +644,13 @@ protected static function map_to_utf8(&$result, $map, $str, $IGNORE) return true; } - protected static function map_from_utf8(&$result, $map, $str, $IGNORE, $TRANSLIT) + private static function mapFromUtf8(&$result, $map, $str, $ignore, $translit) { - $ulen_mask = self::$ulen_mask; - $valid = self::$is_valid_utf8; + $ulenMask = self::$ulenMask; + $valid = self::$isValidUtf8; - if ($TRANSLIT) { - self::$translit_map or self::$translit_map = static::getData('translit'); + if ($translit && !self::$translitMap) { + self::$translitMap = self::getData('translit'); } $i = 0; @@ -611,10 +661,10 @@ protected static function map_from_utf8(&$result, $map, $str, $IGNORE, $TRANSLIT $uchr = $str[$i++]; } else { $ulen = $str[$i] & "\xF0"; - $ulen = isset($ulen_mask[$ulen]) ? $ulen_mask[$ulen] : 1; + $ulen = isset($ulenMask[$ulen]) ? $ulenMask[$ulen] : 1; $uchr = substr($str, $i, $ulen); - if ($IGNORE && (1 === $ulen || !($valid || preg_match('/^.$/us', $uchr)))) { + if ($ignore && (1 === $ulen || !($valid || preg_match('/^.$/us', $uchr)))) { ++$i; continue; } else { @@ -624,15 +674,15 @@ protected static function map_from_utf8(&$result, $map, $str, $IGNORE, $TRANSLIT if (isset($map[$uchr])) { $result .= $map[$uchr]; - } elseif ($TRANSLIT) { - if (isset(self::$translit_map[$uchr])) { - $uchr = self::$translit_map[$uchr]; + } elseif ($translit) { + if (isset(self::$translitMap[$uchr])) { + $uchr = self::$translitMap[$uchr]; } elseif ($uchr >= "\xC3\x80") { $uchr = \Normalizer::normalize($uchr, \Normalizer::NFD); if ($uchr[0] < "\x80") { $uchr = $uchr[0]; - } elseif ($IGNORE) { + } elseif ($ignore) { continue; } else { return false; @@ -642,7 +692,7 @@ protected static function map_from_utf8(&$result, $map, $str, $IGNORE, $TRANSLIT $str = $uchr.substr($str, $i); $len = strlen($str); $i = 0; - } elseif (!$IGNORE) { + } elseif (!$ignore) { return false; } } @@ -650,12 +700,12 @@ protected static function map_from_utf8(&$result, $map, $str, $IGNORE, $TRANSLIT return true; } - protected static function qp_byte_callback($m) + private static function qpByteCallback($m) { return '='.strtoupper(dechex(ord($m[0]))); } - protected static function preg_offset($offset) + private static function pregOffset($offset) { $rx = array(); $offset = (int) $offset; @@ -668,13 +718,12 @@ protected static function preg_offset($offset) return implode('', $rx).'.{'.$offset.'}'; } - protected static function getData($file) + private static function getData($file) { - $file = __DIR__.'/charset/'.$file.'.ser'; - if (file_exists($file)) { + if (file_exists($file = __DIR__.'/charset/'.$file.'.ser')) { return unserialize(file_get_contents($file)); - } else { - return false; } + + return false; } } diff --git a/src/Patchwork/PHP/Shim/Intl.php b/src/Patchwork/PHP/Shim/Intl.php index 88be63d..7da4f21 100644 --- a/src/Patchwork/PHP/Shim/Intl.php +++ b/src/Patchwork/PHP/Shim/Intl.php @@ -1,7 +1,7 @@ $size || 0 > $start || 0 > $type || 2 < $type) { @@ -174,28 +176,33 @@ public static function grapheme_strpos($s, $needle, $offset = 0) { return self::grapheme_position($s, $needle, $offset, 0); } + public static function grapheme_stripos($s, $needle, $offset = 0) { return self::grapheme_position($s, $needle, $offset, 1); } + public static function grapheme_strrpos($s, $needle, $offset = 0) { return self::grapheme_position($s, $needle, $offset, 2); } + public static function grapheme_strripos($s, $needle, $offset = 0) { return self::grapheme_position($s, $needle, $offset, 3); } - public static function grapheme_stristr($s, $needle, $before_needle = false) + + public static function grapheme_stristr($s, $needle, $beforeNeedle = false) { - return mb_stristr($s, $needle, $before_needle, 'UTF-8'); + return mb_stristr($s, $needle, $beforeNeedle, 'UTF-8'); } - public static function grapheme_strstr($s, $needle, $before_needle = false) + + public static function grapheme_strstr($s, $needle, $beforeNeedle = false) { - return mb_strstr($s, $needle, $before_needle, 'UTF-8'); + return mb_strstr($s, $needle, $beforeNeedle, 'UTF-8'); } - protected static function grapheme_position($s, $needle, $offset, $mode) + private static function grapheme_position($s, $needle, $offset, $mode) { if (!preg_match('/./us', $needle .= '')) { return false; diff --git a/src/Patchwork/PHP/Shim/Mbstring.php b/src/Patchwork/PHP/Shim/Mbstring.php index e6baca0..4850cf4 100644 --- a/src/Patchwork/PHP/Shim/Mbstring.php +++ b/src/Patchwork/PHP/Shim/Mbstring.php @@ -1,7 +1,7 @@ 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4); + static $ulenMask = array("\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4); $i = 0; $len = strlen($s); while ($i < $len) { - $ulen = $s[$i] < "\x80" ? 1 : $ulen_mask[$s[$i] & "\xF0"]; + $ulen = $s[$i] < "\x80" ? 1 : $ulenMask[$s[$i] & "\xF0"]; $uchr = substr($s, $i, $ulen); $i += $ulen; @@ -179,23 +196,23 @@ public static function mb_convert_case($s, $mode, $encoding = INF) } } - if (INF === $encoding) { + if (null === $encoding) { return $s; - } else { - return iconv('UTF-8', $encoding, $s); } + + return iconv('UTF-8', $encoding, $s); } - public static function mb_internal_encoding($encoding = INF) + public static function mb_internal_encoding($encoding = null) { - if (INF === $encoding) { - return self::$internal_encoding; - } else { - $encoding = strtoupper($encoding); + if (null === $encoding) { + return self::$internalEncoding; } - if ('UTF-8' === $encoding || 'UTF8' === $encoding || false !== @iconv($encoding, $encoding, ' ')) { - self::$internal_encoding = 'UTF8' === $encoding ? 'UTF-8' : $encoding; + $encoding = self::getEncoding($encoding); + + if ('UTF-8' === $encoding || false !== @iconv($encoding, $encoding, ' ')) { + self::$internalEncoding = $encoding; return true; } @@ -203,9 +220,9 @@ public static function mb_internal_encoding($encoding = INF) return false; } - public static function mb_language($lang = INF) + public static function mb_language($lang = null) { - if (INF === $lang) { + if (null === $lang) { return self::$language; } @@ -227,39 +244,39 @@ public static function mb_list_encodings() public static function mb_encoding_aliases($encoding) { - switch (strtolower($encoding)) { - case 'utf8': - case 'utf-8': + switch (strtoupper($encoding)) { + case 'UTF8': + case 'UTF-8': return array('utf8'); } return false; } - public static function mb_check_encoding($var = INF, $encoding = INF) + public static function mb_check_encoding($var = null, $encoding = null) { - if (INF === $encoding) { - if (INF === $var) { + if (null === $encoding) { + if (null === $var) { return false; } - $encoding = self::$internal_encoding; + $encoding = self::$internalEncoding; } - return false !== mb_detect_encoding($var, array($encoding), true); + return self::mb_detect_encoding($var, array($encoding)) || false !== @iconv($encoding, $encoding, $var); } - public static function mb_detect_encoding($str, $encoding_list = INF, $strict = false) + public static function mb_detect_encoding($str, $encodingList = null, $strict = false) { - if (INF === $encoding_list) { - $encoding_list = self::$encoding_list; + if (null === $encodingList) { + $encodingList = self::$encodingList; } else { - if (!is_array($encoding_list)) { - $encoding_list = array_map('trim', explode(',', $encoding_list)); + if (!is_array($encodingList)) { + $encodingList = array_map('trim', explode(',', $encodingList)); } - $encoding_list = array_map('strtoupper', $encoding_list); + $encodingList = array_map('strtoupper', $encodingList); } - foreach ($encoding_list as $enc) { + foreach ($encodingList as $enc) { switch ($enc) { case 'ASCII': if (!preg_match('/[\x80-\xFF]/', $str)) { @@ -270,7 +287,7 @@ public static function mb_detect_encoding($str, $encoding_list = INF, $strict = case 'UTF8': case 'UTF-8': if (preg_match('//u', $str)) { - return $enc; + return 'UTF-8'; } break; @@ -284,18 +301,18 @@ public static function mb_detect_encoding($str, $encoding_list = INF, $strict = return false; } - public static function mb_detect_order($encoding_list = INF) + public static function mb_detect_order($encodingList = null) { - if (INF === $encoding_list) { - return self::$encoding_list; + if (null === $encodingList) { + return self::$encodingList; } - if (!is_array($encoding_list)) { - $encoding_list = array_map('trim', explode(',', $encoding_list)); + if (!is_array($encodingList)) { + $encodingList = array_map('trim', explode(',', $encodingList)); } - $encoding_list = array_map('strtoupper', $encoding_list); + $encodingList = array_map('strtoupper', $encodingList); - foreach ($encoding_list as $enc) { + foreach ($encodingList as $enc) { switch ($enc) { default: if (strncmp($enc, 'ISO-8859-', 9)) { @@ -307,33 +324,34 @@ public static function mb_detect_order($encoding_list = INF) } } - self::$encoding_list = $encoding_list; + self::$encodingList = $encodingList; return true; } - public static function mb_strlen($s, $encoding = INF) + public static function mb_strlen($s, $encoding = null) { - INF === $encoding and $encoding = self::$internal_encoding; + $encoding = self::getEncoding($encoding); - return iconv_strlen($s, $encoding.'//IGNORE'); + return iconv_strlen($s, $encoding); } - public static function mb_strpos($haystack, $needle, $offset = 0, $encoding = INF) + public static function mb_strpos($haystack, $needle, $offset = 0, $encoding = null) { - INF === $encoding and $encoding = self::$internal_encoding; + $encoding = self::getEncoding($encoding); + if ('' === $needle .= '') { - user_error(__METHOD__.': Empty delimiter', E_USER_WARNING); + trigger_error(__METHOD__.': Empty delimiter', E_USER_WARNING); return false; - } else { - return iconv_strpos($haystack, $needle, $offset, $encoding.'//IGNORE'); } + + return iconv_strpos($haystack, $needle, $offset, $encoding); } - public static function mb_strrpos($haystack, $needle, $offset = 0, $encoding = INF) + public static function mb_strrpos($haystack, $needle, $offset = 0, $encoding = null) { - INF === $encoding and $encoding = self::$internal_encoding; + $encoding = self::getEncoding($encoding); if ($offset != (int) $offset) { $offset = 0; @@ -346,32 +364,36 @@ public static function mb_strrpos($haystack, $needle, $offset = 0, $encoding = I } } - $pos = iconv_strrpos($haystack, $needle, $encoding.'//IGNORE'); + $pos = iconv_strrpos($haystack, $needle, $encoding); return false !== $pos ? $offset + $pos : false; } - public static function mb_strtolower($s, $encoding = INF) + public static function mb_strtolower($s, $encoding = null) { return self::mb_convert_case($s, MB_CASE_LOWER, $encoding); } - public static function mb_strtoupper($s, $encoding = INF) + public static function mb_strtoupper($s, $encoding = null) { return self::mb_convert_case($s, MB_CASE_UPPER, $encoding); } - public static function mb_substitute_character($c = INF) + public static function mb_substitute_character($c = null) { - return INF !== $c ? false : 'none'; + if (0 === strcasecmp($c, 'none')) { + return true; + } + + return null !== $c ? false : 'none'; } - public static function mb_substr($s, $start, $length = null, $encoding = INF) + public static function mb_substr($s, $start, $length = null, $encoding = null) { - INF === $encoding and $encoding = self::$internal_encoding; + $encoding = self::getEncoding($encoding); if ($start < 0) { - $start = iconv_strlen($s, $encoding.'//IGNORE') + $start; + $start = iconv_strlen($s, $encoding) + $start; if ($start < 0) { $start = 0; } @@ -380,41 +402,40 @@ public static function mb_substr($s, $start, $length = null, $encoding = INF) if (null === $length) { $length = 2147483647; } elseif ($length < 0) { - $length = iconv_strlen($s, $encoding.'//IGNORE') + $length - $start; + $length = iconv_strlen($s, $encoding) + $length - $start; if ($length < 0) { return ''; } } - return iconv_substr($s, $start, $length, $encoding.'//IGNORE').''; + return iconv_substr($s, $start, $length, $encoding).''; } - public static function mb_stripos($haystack, $needle, $offset = 0, $encoding = INF) + public static function mb_stripos($haystack, $needle, $offset = 0, $encoding = null) { - INF === $encoding and $encoding = self::$internal_encoding; $haystack = self::mb_convert_case($haystack, self::MB_CASE_FOLD, $encoding); $needle = self::mb_convert_case($needle, self::MB_CASE_FOLD, $encoding); return self::mb_strpos($haystack, $needle, $offset, $encoding); } - public static function mb_stristr($haystack, $needle, $part = false, $encoding = INF) + public static function mb_stristr($haystack, $needle, $part = false, $encoding = null) { $pos = self::mb_stripos($haystack, $needle, 0, $encoding); return self::getSubpart($pos, $part, $haystack, $encoding); } - public static function mb_strrchr($haystack, $needle, $part = false, $encoding = INF) + public static function mb_strrchr($haystack, $needle, $part = false, $encoding = null) { - INF === $encoding and $encoding = self::$internal_encoding; + $encoding = self::getEncoding($encoding); $needle = self::mb_substr($needle, 0, 1, $encoding); $pos = iconv_strrpos($haystack, $needle, $encoding); return self::getSubpart($pos, $part, $haystack, $encoding); } - public static function mb_strrichr($haystack, $needle, $part = false, $encoding = INF) + public static function mb_strrichr($haystack, $needle, $part = false, $encoding = null) { $needle = self::mb_substr($needle, 0, 1, $encoding); $pos = self::mb_strripos($haystack, $needle, $encoding); @@ -422,16 +443,15 @@ public static function mb_strrichr($haystack, $needle, $part = false, $encoding return self::getSubpart($pos, $part, $haystack, $encoding); } - public static function mb_strripos($haystack, $needle, $offset = 0, $encoding = INF) + public static function mb_strripos($haystack, $needle, $offset = 0, $encoding = null) { - INF === $encoding and $encoding = self::$internal_encoding; $haystack = self::mb_convert_case($haystack, self::MB_CASE_FOLD, $encoding); $needle = self::mb_convert_case($needle, self::MB_CASE_FOLD, $encoding); return self::mb_strrpos($haystack, $needle, $offset, $encoding); } - public static function mb_strstr($haystack, $needle, $part = false, $encoding = INF) + public static function mb_strstr($haystack, $needle, $part = false, $encoding = null) { $pos = strpos($haystack, $needle); if (false === $pos) { @@ -439,15 +459,15 @@ public static function mb_strstr($haystack, $needle, $part = false, $encoding = } if ($part) { return substr($haystack, 0, $pos); - } else { - return substr($haystack, $pos); } + + return substr($haystack, $pos); } public static function mb_get_info($type = 'all') { $info = array( - 'internal_encoding' => self::$internal_encoding, + 'internal_encoding' => self::$internalEncoding, 'http_output' => 'pass', 'http_output_conv_mimetypes' => '^(text/|application/xhtml\+xml)', 'func_overload' => 0, @@ -458,18 +478,19 @@ public static function mb_get_info($type = 'all') 'illegal_chars' => 0, 'encoding_translation' => 'Off', 'language' => self::$language, - 'detect_order' => self::$encoding_list, + 'detect_order' => self::$encodingList, 'substitute_character' => 'none', 'strict_detection' => 'Off', ); if ('all' === $type) { return $info; - } elseif (isset($info[$type])) { + } + if (isset($info[$type])) { return $info[$type]; - } else { - return false; } + + return false; } public static function mb_http_input($type = '') @@ -477,27 +498,25 @@ public static function mb_http_input($type = '') return false; } - public static function mb_http_output($encoding = INF) + public static function mb_http_output($encoding = null) { - return INF !== $encoding ? 'pass' === $encoding : 'pass'; + return null !== $encoding ? 'pass' === $encoding : 'pass'; } - public static function mb_strwidth($s, $encoding = INF) + public static function mb_strwidth($s, $encoding = null) { - $encoding = INF === $encoding ? self::$internal_encoding : strtoupper($encoding); + $encoding = self::getEncoding($encoding); - if ('UTF-8' !== $encoding && 'UTF8' !== $encoding) { - $s = iconv($encoding, 'UTF-8//IGNORE', $s); + if ('UTF-8' !== $encoding) { + $s = iconv($encoding, 'UTF-8', $s); } - $s = preg_replace('/[\x00-\x19]/', '', $s); - - preg_replace('/[\x{0020}-\x{1FFF}\x{FF61}-\x{FF9F}]/u', '', $s, -1, $narrow); + $s = preg_replace('/[\x{1100}-\x{115F}\x{2329}\x{232A}\x{2E80}-\x{303E}\x{3040}-\x{A4CF}\x{AC00}-\x{D7A3}\x{F900}-\x{FAFF}\x{FE10}-\x{FE19}\x{FE30}-\x{FE6F}\x{FF00}-\x{FF60}\x{FFE0}-\x{FFE6}\x{20000}-\x{2FFFD}\x{30000}-\x{3FFFD}]/u', '', $s, -1, $wide); - return (iconv_strlen($s, 'UTF-8') << 1) - $narrow; + return ($wide << 1) + iconv_strlen($s, 'UTF-8'); } - public static function mb_substr_count($haystack, $needle, $encoding = INF) + public static function mb_substr_count($haystack, $needle, $encoding = null) { return substr_count($haystack, $needle); } @@ -507,21 +526,19 @@ public static function mb_output_handler($contents, $status) return $contents; } - protected static function getSubpart($pos, $part, $haystack, $encoding) + private static function getSubpart($pos, $part, $haystack, $encoding) { - INF === $encoding and $encoding = self::$internal_encoding; - if (false === $pos) { return false; } if ($part) { return self::mb_substr($haystack, 0, $pos, $encoding); - } else { - return self::mb_substr($haystack, $pos, null, $encoding); } + + return self::mb_substr($haystack, $pos, null, $encoding); } - protected static function html_encoding_callback($m) + private static function html_encoding_callback($m) { $i = 1; $entities = ''; @@ -535,7 +552,7 @@ protected static function html_encoding_callback($m) if (0xF0 <= $m[$i]) { $c = (($m[$i++] - 0xF0) << 18) + (($m[$i++] - 0x80) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80; } elseif (0xE0 <= $m[$i]) { - $c = (($m[$i++] - 0xE0) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80; + $c = (($m[$i++] - 0xE0) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80; } else { $c = (($m[$i++] - 0xC0) << 6) + $m[$i++] - 0x80; } @@ -546,23 +563,40 @@ protected static function html_encoding_callback($m) return $entities; } - protected static function title_case_lower($s) + private static function title_case_lower($s) { return self::mb_convert_case($s[0], MB_CASE_LOWER, 'UTF-8'); } - protected static function title_case_upper($s) + private static function title_case_upper($s) { return self::mb_convert_case($s[0], MB_CASE_UPPER, 'UTF-8'); } - protected static function getData($file) + private static function getData($file) { - $file = __DIR__.'/unidata/'.$file.'.ser'; - if (file_exists($file)) { + if (file_exists($file = __DIR__.'/unidata/'.$file.'.ser')) { return unserialize(file_get_contents($file)); - } else { - return false; } + + return false; + } + + private static function getEncoding($encoding) + { + if (null === $encoding) { + return self::$internalEncoding; + } + + $encoding = strtoupper($encoding); + + if ('8BIT' === $encoding || 'BINARY' === $encoding) { + return 'CP850'; + } + if ('UTF8' === $encoding) { + return 'UTF-8'; + } + + return $encoding; } } diff --git a/src/Patchwork/PHP/Shim/Normalizer.php b/src/Patchwork/PHP/Shim/Normalizer.php index eac95fb..a5774e2 100644 --- a/src/Patchwork/PHP/Shim/Normalizer.php +++ b/src/Patchwork/PHP/Shim/Normalizer.php @@ -1,7 +1,7 @@ 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4); - protected static $ASCII = "\x20\x65\x69\x61\x73\x6E\x74\x72\x6F\x6C\x75\x64\x5D\x5B\x63\x6D\x70\x27\x0A\x67\x7C\x68\x76\x2E\x66\x62\x2C\x3A\x3D\x2D\x71\x31\x30\x43\x32\x2A\x79\x78\x29\x28\x4C\x39\x41\x53\x2F\x50\x22\x45\x6A\x4D\x49\x6B\x33\x3E\x35\x54\x3C\x44\x34\x7D\x42\x7B\x38\x46\x77\x52\x36\x37\x55\x47\x4E\x3B\x4A\x7A\x56\x23\x48\x4F\x57\x5F\x26\x21\x4B\x3F\x58\x51\x25\x59\x5C\x09\x5A\x2B\x7E\x5E\x24\x40\x60\x7F\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"; + private static $C; + private static $D; + private static $KD; + private static $cC; + private static $ulenMask = array("\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4); + private static $ASCII = "\x20\x65\x69\x61\x73\x6E\x74\x72\x6F\x6C\x75\x64\x5D\x5B\x63\x6D\x70\x27\x0A\x67\x7C\x68\x76\x2E\x66\x62\x2C\x3A\x3D\x2D\x71\x31\x30\x43\x32\x2A\x79\x78\x29\x28\x4C\x39\x41\x53\x2F\x50\x22\x45\x6A\x4D\x49\x6B\x33\x3E\x35\x54\x3C\x44\x34\x7D\x42\x7B\x38\x46\x77\x52\x36\x37\x55\x47\x4E\x3B\x4A\x7A\x56\x23\x48\x4F\x57\x5F\x26\x21\x4B\x3F\x58\x51\x25\x59\x5C\x09\x5A\x2B\x7E\x5E\x24\x40\x60\x7F\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"; public static function isNormalized($s, $form = self::NFC) { @@ -56,10 +58,10 @@ public static function normalize($s, $form = self::NFC) switch ($form) { case self::NONE: return $s; - case self::NFC: $C = true; $K = false; break; - case self::NFD: $C = false; $K = false; break; - case self::NFKC: $C = true; $K = true; break; - case self::NFKD: $C = false; $K = true; break; + case self::NFC: $C = true; $K = false; break; + case self::NFD: $C = false; $K = false; break; + case self::NFKC: $C = true; $K = true; break; + case self::NFKD: $C = false; $K = true; break; default: return false; } @@ -67,116 +69,117 @@ public static function normalize($s, $form = self::NFC) return ''; } - if ($K && empty(self::$KD)) { - self::$KD = static::getData('compatibilityDecomposition'); + if ($K && null === self::$KD) { + self::$KD = self::getData('compatibilityDecomposition'); } - if (empty(self::$D)) { - self::$D = static::getData('canonicalDecomposition'); - self::$cC = static::getData('combiningClass'); + if (null === self::$D) { + self::$D = self::getData('canonicalDecomposition'); + self::$cC = self::getData('combiningClass'); } if ($C) { - if (empty(self::$C)) { - self::$C = static::getData('canonicalComposition'); + if (null === self::$C) { + self::$C = self::getData('canonicalComposition'); } return self::recompose(self::decompose($s, $K)); - } else { - return self::decompose($s, $K); } + + return self::decompose($s, $K); } - protected static function recompose($s) + private static function recompose($s) { $ASCII = self::$ASCII; $compMap = self::$C; $combClass = self::$cC; - $ulen_mask = self::$ulen_mask; + $ulenMask = self::$ulenMask; $result = $tail = ''; - $i = $s[0] < "\x80" ? 1 : $ulen_mask[$s[0] & "\xF0"]; + $i = $s[0] < "\x80" ? 1 : $ulenMask[$s[0] & "\xF0"]; $len = strlen($s); - $last_uchr = substr($s, 0, $i); - $last_ucls = isset($combClass[$last_uchr]) ? 256 : 0; + $lastUchr = substr($s, 0, $i); + $lastUcls = isset($combClass[$lastUchr]) ? 256 : 0; while ($i < $len) { if ($s[$i] < "\x80") { // ASCII chars if ($tail) { - $last_uchr .= $tail; + $lastUchr .= $tail; $tail = ''; } if ($j = strspn($s, $ASCII, $i + 1)) { - $last_uchr .= substr($s, $i, $j); + $lastUchr .= substr($s, $i, $j); $i += $j; } - $result .= $last_uchr; - $last_uchr = $s[$i]; + $result .= $lastUchr; + $lastUchr = $s[$i]; ++$i; - } else { - $ulen = $ulen_mask[$s[$i] & "\xF0"]; - $uchr = substr($s, $i, $ulen); - - if ($last_uchr < "\xE1\x84\x80" || "\xE1\x84\x92" < $last_uchr - || $uchr < "\xE1\x85\xA1" || "\xE1\x85\xB5" < $uchr - || $last_ucls) { - // Table lookup and combining chars composition - - $ucls = isset($combClass[$uchr]) ? $combClass[$uchr] : 0; - - if (isset($compMap[$last_uchr.$uchr]) && (!$last_ucls || $last_ucls < $ucls)) { - $last_uchr = $compMap[$last_uchr.$uchr]; - } elseif ($last_ucls = $ucls) { - $tail .= $uchr; - } else { - if ($tail) { - $last_uchr .= $tail; - $tail = ''; - } + continue; + } - $result .= $last_uchr; - $last_uchr = $uchr; - } - } else { - // Hangul chars + $ulen = $ulenMask[$s[$i] & "\xF0"]; + $uchr = substr($s, $i, $ulen); - $L = ord($last_uchr[2]) - 0x80; - $V = ord($uchr[2]) - 0xA1; - $T = 0; + if ($lastUchr < "\xE1\x84\x80" || "\xE1\x84\x92" < $lastUchr + || $uchr < "\xE1\x85\xA1" || "\xE1\x85\xB5" < $uchr + || $lastUcls) { + // Table lookup and combining chars composition - $uchr = substr($s, $i + $ulen, 3); + $ucls = isset($combClass[$uchr]) ? $combClass[$uchr] : 0; - if ("\xE1\x86\xA7" <= $uchr && $uchr <= "\xE1\x87\x82") { - $T = ord($uchr[2]) - 0xA7; - 0 > $T && $T += 0x40; - $ulen += 3; + if (isset($compMap[$lastUchr.$uchr]) && (!$lastUcls || $lastUcls < $ucls)) { + $lastUchr = $compMap[$lastUchr.$uchr]; + } elseif ($lastUcls = $ucls) { + $tail .= $uchr; + } else { + if ($tail) { + $lastUchr .= $tail; + $tail = ''; } - $L = 0xAC00 + ($L * 21 + $V) * 28 + $T; - $last_uchr = chr(0xE0 | $L >> 12).chr(0x80 | $L >> 6 & 0x3F).chr(0x80 | $L & 0x3F); + $result .= $lastUchr; + $lastUchr = $uchr; + } + } else { + // Hangul chars + + $L = ord($lastUchr[2]) - 0x80; + $V = ord($uchr[2]) - 0xA1; + $T = 0; + + $uchr = substr($s, $i + $ulen, 3); + + if ("\xE1\x86\xA7" <= $uchr && $uchr <= "\xE1\x87\x82") { + $T = ord($uchr[2]) - 0xA7; + 0 > $T && $T += 0x40; + $ulen += 3; } - $i += $ulen; + $L = 0xAC00 + ($L * 21 + $V) * 28 + $T; + $lastUchr = chr(0xE0 | $L >> 12).chr(0x80 | $L >> 6 & 0x3F).chr(0x80 | $L & 0x3F); } + + $i += $ulen; } - return $result.$last_uchr.$tail; + return $result.$lastUchr.$tail; } - protected static function decompose($s, $c) + private static function decompose($s, $c) { $result = ''; $ASCII = self::$ASCII; $decompMap = self::$D; $combClass = self::$cC; - $ulen_mask = self::$ulen_mask; + $ulenMask = self::$ulenMask; if ($c) { $compatMap = self::$KD; } @@ -198,72 +201,74 @@ protected static function decompose($s, $c) $j = 1 + strspn($s, $ASCII, $i + 1); $result .= substr($s, $i, $j); $i += $j; - } else { - $ulen = $ulen_mask[$s[$i] & "\xF0"]; - $uchr = substr($s, $i, $ulen); - $i += $ulen; - - if (isset($combClass[$uchr])) { - // Combining chars, for sorting + continue; + } - isset($c[$combClass[$uchr]]) or $c[$combClass[$uchr]] = ''; - $c[$combClass[$uchr]] .= isset($compatMap[$uchr]) ? $compatMap[$uchr] : (isset($decompMap[$uchr]) ? $decompMap[$uchr] : $uchr); - } else { - if ($c) { - ksort($c); - $result .= implode('', $c); - $c = array(); - } + $ulen = $ulenMask[$s[$i] & "\xF0"]; + $uchr = substr($s, $i, $ulen); + $i += $ulen; - if ($uchr < "\xEA\xB0\x80" || "\xED\x9E\xA3" < $uchr) { - // Table lookup + if (isset($combClass[$uchr])) { + // Combining chars, for sorting - $j = isset($compatMap[$uchr]) ? $compatMap[$uchr] : (isset($decompMap[$uchr]) ? $decompMap[$uchr] : $uchr); + if (!isset($c[$combClass[$uchr]])) { + $c[$combClass[$uchr]] = ''; + } + $c[$combClass[$uchr]] .= isset($compatMap[$uchr]) ? $compatMap[$uchr] : (isset($decompMap[$uchr]) ? $decompMap[$uchr] : $uchr); + continue; + } + if ($c) { + ksort($c); + $result .= implode('', $c); + $c = array(); + } + if ($uchr < "\xEA\xB0\x80" || "\xED\x9E\xA3" < $uchr) { + // Table lookup - if ($uchr != $j) { - $uchr = $j; + $j = isset($compatMap[$uchr]) ? $compatMap[$uchr] : (isset($decompMap[$uchr]) ? $decompMap[$uchr] : $uchr); - $j = strlen($uchr); - $ulen = $uchr[0] < "\x80" ? 1 : $ulen_mask[$uchr[0] & "\xF0"]; + if ($uchr != $j) { + $uchr = $j; - if ($ulen != $j) { - // Put trailing chars in $s + $j = strlen($uchr); + $ulen = $uchr[0] < "\x80" ? 1 : $ulenMask[$uchr[0] & "\xF0"]; - $j -= $ulen; - $i -= $j; + if ($ulen != $j) { + // Put trailing chars in $s - if (0 > $i) { - $s = str_repeat(' ', -$i).$s; - $len -= $i; - $i = 0; - } + $j -= $ulen; + $i -= $j; - while ($j--) { - $s[$i + $j] = $uchr[$ulen + $j]; - } + if (0 > $i) { + $s = str_repeat(' ', -$i).$s; + $len -= $i; + $i = 0; + } - $uchr = substr($uchr, 0, $ulen); - } + while ($j--) { + $s[$i + $j] = $uchr[$ulen + $j]; } - } else { - // Hangul chars - $uchr = unpack('C*', $uchr); - $j = (($uchr[1] - 224) << 12) + (($uchr[2] - 128) << 6) + $uchr[3] - 0xAC80; + $uchr = substr($uchr, 0, $ulen); + } + } + } else { + // Hangul chars - $uchr = "\xE1\x84".chr(0x80 + (int) ($j / 588)) - ."\xE1\x85".chr(0xA1 + (int) (($j % 588) / 28)); + $uchr = unpack('C*', $uchr); + $j = (($uchr[1] - 224) << 12) + (($uchr[2] - 128) << 6) + $uchr[3] - 0xAC80; - if ($j %= 28) { - $uchr .= $j < 25 - ? ("\xE1\x86".chr(0xA7 + $j)) - : ("\xE1\x87".chr(0x67 + $j)); - } - } + $uchr = "\xE1\x84".chr(0x80 + (int) ($j / 588)) + ."\xE1\x85".chr(0xA1 + (int) (($j % 588) / 28)); - $result .= $uchr; + if ($j %= 28) { + $uchr .= $j < 25 + ? ("\xE1\x86".chr(0xA7 + $j)) + : ("\xE1\x87".chr(0x67 + $j)); } } + + $result .= $uchr; } if ($c) { @@ -274,13 +279,12 @@ protected static function decompose($s, $c) return $result; } - protected static function getData($file) + private static function getData($file) { - $file = __DIR__.'/unidata/'.$file.'.ser'; - if (file_exists($file)) { + if (file_exists($file = __DIR__.'/unidata/'.$file.'.ser')) { return unserialize(file_get_contents($file)); - } else { - return false; } + + return false; } } diff --git a/src/Patchwork/PHP/Shim/Xml.php b/src/Patchwork/PHP/Shim/Xml.php index 84bb2d4..bb028fa 100644 --- a/src/Patchwork/PHP/Shim/Xml.php +++ b/src/Patchwork/PHP/Shim/Xml.php @@ -1,7 +1,7 @@ = '8.32' ? '\X' : s\Intl::GRAPHEME_CLUSTER_RX); - if (!extension_loaded('intl')) { + if (!function_exists('grapheme_strlen')) { extension_loaded('iconv') or static::initIconv(); extension_loaded('mbstring') or static::initMbstring(); diff --git a/src/Patchwork/Utf8/Bootup/iconv.php b/src/Patchwork/Utf8/Bootup/iconv.php index d490cc9..0730dda 100644 --- a/src/Patchwork/Utf8/Bootup/iconv.php +++ b/src/Patchwork/Utf8/Bootup/iconv.php @@ -1,7 +1,7 @@ = 50610 || (PHP_VERSION_ID >= 50526 && PHP_VERSION_ID < 50600) || '\\' === DIRECTORY_SEPARATOR)) { - $this->assertSame(PHP_VERSION_ID >= 50400 ? false : 'n', @iconv('UTF-8', 'ISO-8859-1', 'nœud')); - $this->assertSame('nud', iconv('UTF-8', 'ISO-8859-1//IGNORE', 'nœud')); - } elseif (PHP_VERSION_ID >= 50400) { - $this->assertSame(false, @iconv('UTF-8', 'ISO-8859-1', 'nœud')); - $this->assertSame(false, @iconv('UTF-8', 'ISO-8859-1//IGNORE', 'nœud')); - } else { - $this->assertSame('n', @iconv('UTF-8', 'ISO-8859-1', 'nœud')); - $this->assertSame('nud', @iconv('UTF-8', 'ISO-8859-1//IGNORE', 'nœud')); + if (defined('HHVM_VERSION') ? HHVM_VERSION_ID >= 30901 : (PHP_VERSION_ID >= 50610)) { + $this->assertFalse(@p::iconv('UTF-8', 'ISO-8859-1', 'nœud')); + $this->assertSame('nud', p::iconv('UTF-8', 'ISO-8859-1//IGNORE', 'nœud')); } - // The recent Windows behavior is the most useful - $this->assertFalse(p::iconv('UTF-8', 'ISO-8859-1', 'nœud')); - $this->assertSame('nud', p::iconv('UTF-8', 'ISO-8859-1//IGNORE', 'nœud')); - $this->assertSame(utf8_decode('déjà'), p::iconv('CP1252', 'ISO-8859-1', utf8_decode('déjà'))); - $this->assertSame('déjà', p::iconv('UTF-8', 'utf8', 'déjà')); $this->assertSame('deja noeud', p::iconv('UTF-8', 'US-ASCII//TRANSLIT', 'déjà nœud')); $this->assertSame('4', p::iconv('UTF-8', 'UTF-8', 4)); @@ -96,16 +85,15 @@ public function testIconvMimeEncode() /** * @covers Patchwork\PHP\Shim\Iconv::iconv_mime_decode - * @expectedException PHPUnit_Framework_Error_Notice */ public function testIconvMimeDecode() { $this->assertSame('Legal encoded-word: * .', p::iconv_mime_decode('Legal encoded-word: =?utf-8?B?Kg==?= .')); $this->assertSame('Legal encoded-word: * .', p::iconv_mime_decode('Legal encoded-word: =?utf-8?Q?*?= .')); - $this->assertSame('Illegal encoded-word: .', p::iconv_mime_decode('Illegal encoded-word: =?utf-8?Q?'.chr(0xA1).'?= .', ICONV_MIME_DECODE_CONTINUE_ON_ERROR)); - - p::iconv_mime_decode('Illegal encoded-word: =?utf-8?Q?'.chr(0xA1).'?= .'); - $this->assertFalse(true, 'An illegal encoded-word should trigger a notice'); + if (!defined('HHVM_VERSION') && '\\' !== DIRECTORY_SEPARATOR) { + $this->assertSame('Illegal encoded-word: .', p::iconv_mime_decode('Illegal encoded-word: =?utf-8?Q??= .', ICONV_MIME_DECODE_CONTINUE_ON_ERROR)); + $this->assertSame('Illegal encoded-word: .', p::iconv_mime_decode('Illegal encoded-word: =?utf-8?Q?'.chr(0xA1).'?= .', ICONV_MIME_DECODE_CONTINUE_ON_ERROR)); + } } /** diff --git a/tests/Patchwork/Tests/PHP/Shim/IntlTest.php b/tests/PHP/Shim/IntlTest.php similarity index 100% rename from tests/Patchwork/Tests/PHP/Shim/IntlTest.php rename to tests/PHP/Shim/IntlTest.php diff --git a/tests/Patchwork/Tests/PHP/Shim/MbstringTest.php b/tests/PHP/Shim/MbstringTest.php similarity index 98% rename from tests/Patchwork/Tests/PHP/Shim/MbstringTest.php rename to tests/PHP/Shim/MbstringTest.php index 81186df..fe3cfbe 100644 --- a/tests/Patchwork/Tests/PHP/Shim/MbstringTest.php +++ b/tests/PHP/Shim/MbstringTest.php @@ -211,7 +211,7 @@ public function testmb_check_encoding() public function testmb_detect_encoding() { $this->assertSame('ASCII', p::mb_detect_encoding('abc')); - $this->assertSame('UTF8', p::mb_detect_encoding('abc', 'UTF8, ASCII')); + $this->assertSame('UTF-8', p::mb_detect_encoding('abc', 'UTF8, ASCII')); $this->assertSame('ISO-8859-1', p::mb_detect_encoding("\x9D", array('UTF-8', 'ASCII', 'ISO-8859-1'))); } @@ -250,7 +250,7 @@ public function testmb_encoding_aliases() */ public function testmb_strwidth() { - $this->assertSame(2, p::mb_strwidth("\0実")); + $this->assertSame(3, p::mb_strwidth("\000実", 'UTF-8')); $this->assertSame(4, p::mb_strwidth('déjà')); $this->assertSame(4, p::mb_strwidth(utf8_decode('déjà'), 'CP1252')); } diff --git a/tests/unicode/NormalizationTest.63.txt b/tests/PHP/Shim/NormalizationTest.63.txt similarity index 100% rename from tests/unicode/NormalizationTest.63.txt rename to tests/PHP/Shim/NormalizationTest.63.txt diff --git a/tests/unicode/NormalizationTest.70.txt b/tests/PHP/Shim/NormalizationTest.70.txt similarity index 100% rename from tests/unicode/NormalizationTest.70.txt rename to tests/PHP/Shim/NormalizationTest.70.txt diff --git a/tests/PHP/Shim/NormalizerTest.php b/tests/PHP/Shim/NormalizerTest.php new file mode 100644 index 0000000..268e5d0 --- /dev/null +++ b/tests/PHP/Shim/NormalizerTest.php @@ -0,0 +1,119 @@ + + */ +class NormalizerTest extends \PHPUnit_Framework_TestCase +{ + public $unicodeVersion = 70; + + public function testConstants() + { + $rpn = new \ReflectionClass('Patchwork\PHP\Shim\Normalizer'); + $rin = new \ReflectionClass('Normalizer'); + + $rpn = $rpn->getConstants(); + $rin = $rin->getConstants(); + + ksort($rpn); + ksort($rin); + + $this->assertSame($rin, $rpn); + } + + /** + * @covers Patchwork\PHP\Shim\Normalizer::isNormalized + */ + public function testIsNormalized() + { + $c = 'déjà'; + $d = in::normalize($c, pn::NFD); + + $this->assertTrue(pn::isNormalized('')); + $this->assertTrue(pn::isNormalized('abc')); + $this->assertTrue(pn::isNormalized($c)); + $this->assertTrue(pn::isNormalized($c, pn::NFC)); + $this->assertFalse(pn::isNormalized($d, pn::NFD)); // The current implementation defensively says false + $this->assertFalse(pn::isNormalized($c, pn::NFD)); + $this->assertFalse(pn::isNormalized($d, pn::NFC)); + $this->assertFalse(pn::isNormalized("\xFF")); + } + + /** + * @covers Patchwork\PHP\Shim\Normalizer::normalize + */ + public function testNormalize() + { + $c = in::normalize('déjà', pn::NFC).in::normalize('훈쇼™', pn::NFD); + $this->assertSame($c, pn::normalize($c, pn::NONE)); + $this->assertSame($c, in::normalize($c, pn::NONE)); + + $c = 'déjà 훈쇼™'; + $d = in::normalize($c, pn::NFD); + $kc = in::normalize($c, pn::NFKC); + $kd = in::normalize($c, pn::NFKD); + + $this->assertSame('', pn::normalize('')); + $this->assertSame($c, pn::normalize($d)); + $this->assertSame($c, pn::normalize($d, pn::NFC)); + $this->assertSame($d, pn::normalize($c, pn::NFD)); + $this->assertSame($kc, pn::normalize($d, pn::NFKC)); + $this->assertSame($kd, pn::normalize($c, pn::NFKD)); + + $this->assertFalse(pn::normalize($c, -1)); + $this->assertFalse(pn::normalize("\xFF")); + } + + /** + * @covers Patchwork\PHP\Shim\Normalizer::normalize + * @group unicode + */ + public function testNormalizeConformance() + { + $t = file(__DIR__.'/NormalizationTest.'.$this->unicodeVersion.'.txt'); + $c = array(); + + foreach ($t as $s) { + $t = explode('#', $s); + $t = explode(';', $t[0]); + + if (6 === count($t)) { + foreach ($t as $k => $s) { + $t = explode(' ', $s); + $t = array_map('hexdec', $t); + $t = array_map('Patchwork\Utf8::chr', $t); + $c[$k] = implode('', $t); + } + + $this->assertSame($c[1], pn::normalize($c[0], pn::NFC)); + $this->assertSame($c[1], pn::normalize($c[1], pn::NFC)); + $this->assertSame($c[1], pn::normalize($c[2], pn::NFC)); + $this->assertSame($c[3], pn::normalize($c[3], pn::NFC)); + $this->assertSame($c[3], pn::normalize($c[4], pn::NFC)); + + $this->assertSame($c[2], pn::normalize($c[0], pn::NFD)); + $this->assertSame($c[2], pn::normalize($c[1], pn::NFD)); + $this->assertSame($c[2], pn::normalize($c[2], pn::NFD)); + $this->assertSame($c[4], pn::normalize($c[3], pn::NFD)); + $this->assertSame($c[4], pn::normalize($c[4], pn::NFD)); + + $this->assertSame($c[3], pn::normalize($c[0], pn::NFKC)); + $this->assertSame($c[3], pn::normalize($c[1], pn::NFKC)); + $this->assertSame($c[3], pn::normalize($c[2], pn::NFKC)); + $this->assertSame($c[3], pn::normalize($c[3], pn::NFKC)); + $this->assertSame($c[3], pn::normalize($c[4], pn::NFKC)); + + $this->assertSame($c[4], pn::normalize($c[0], pn::NFKD)); + $this->assertSame($c[4], pn::normalize($c[1], pn::NFKD)); + $this->assertSame($c[4], pn::normalize($c[2], pn::NFKD)); + $this->assertSame($c[4], pn::normalize($c[3], pn::NFKD)); + $this->assertSame($c[4], pn::normalize($c[4], pn::NFKD)); + } + } + } +} diff --git a/tests/Patchwork/Tests/PHP/Shim/XmlTest.php b/tests/PHP/Shim/XmlTest.php similarity index 100% rename from tests/Patchwork/Tests/PHP/Shim/XmlTest.php rename to tests/PHP/Shim/XmlTest.php diff --git a/tests/Patchwork/Tests/PHP/Shim/NormalizerTest.php b/tests/Patchwork/Tests/PHP/Shim/NormalizerTest.php deleted file mode 100644 index 3a166b0..0000000 --- a/tests/Patchwork/Tests/PHP/Shim/NormalizerTest.php +++ /dev/null @@ -1,69 +0,0 @@ - - */ -class NormalizerTest extends \PHPUnit_Framework_TestCase -{ - public function testConstants() - { - $rpn = new \ReflectionClass('Patchwork\PHP\Shim\Normalizer'); - $rin = new \ReflectionClass('Normalizer'); - - $rpn = $rpn->getConstants(); - $rin = $rin->getConstants(); - - ksort($rpn); - ksort($rin); - - $this->assertSame($rin, $rpn); - } - - /** - * @covers Patchwork\PHP\Shim\Normalizer::isNormalized - */ - public function testIsNormalized() - { - $c = 'déjà'; - $d = in::normalize($c, pn::NFD); - - $this->assertTrue(pn::isNormalized('')); - $this->assertTrue(pn::isNormalized('abc')); - $this->assertTrue(pn::isNormalized($c)); - $this->assertTrue(pn::isNormalized($c, pn::NFC)); - $this->assertFalse(pn::isNormalized($d, pn::NFD)); // The current implementation defensively says false - $this->assertFalse(pn::isNormalized($c, pn::NFD)); - $this->assertFalse(pn::isNormalized($d, pn::NFC)); - $this->assertFalse(pn::isNormalized("\xFF")); - } - - /** - * @covers Patchwork\PHP\Shim\Normalizer::normalize - */ - public function testNormalize() - { - $c = in::normalize('déjà', pn::NFC).in::normalize('훈쇼™', pn::NFD); - $this->assertSame($c, pn::normalize($c, pn::NONE)); - $this->assertSame($c, in::normalize($c, pn::NONE)); - - $c = 'déjà 훈쇼™'; - $d = in::normalize($c, pn::NFD); - $kc = in::normalize($c, pn::NFKC); - $kd = in::normalize($c, pn::NFKD); - - $this->assertSame('', pn::normalize('')); - $this->assertSame($c, pn::normalize($d)); - $this->assertSame($c, pn::normalize($d, pn::NFC)); - $this->assertSame($d, pn::normalize($c, pn::NFD)); - $this->assertSame($kc, pn::normalize($d, pn::NFKC)); - $this->assertSame($kd, pn::normalize($c, pn::NFKD)); - - $this->assertFalse(pn::normalize($c, -1)); - $this->assertFalse(pn::normalize("\xFF")); - } -} diff --git a/tests/Patchwork/Tests/TurkishUtf8Test.php b/tests/TurkishUtf8Test.php similarity index 100% rename from tests/Patchwork/Tests/TurkishUtf8Test.php rename to tests/TurkishUtf8Test.php diff --git a/tests/Patchwork/Tests/Utf8/BestFitTest.php b/tests/Utf8/BestFitTest.php similarity index 100% rename from tests/Patchwork/Tests/Utf8/BestFitTest.php rename to tests/Utf8/BestFitTest.php diff --git a/tests/Patchwork/Tests/Utf8/BootupTest.php b/tests/Utf8/BootupTest.php similarity index 100% rename from tests/Patchwork/Tests/Utf8/BootupTest.php rename to tests/Utf8/BootupTest.php diff --git a/tests/Patchwork/Tests/Utf8/HhvmTest.php b/tests/Utf8/HhvmTest.php similarity index 100% rename from tests/Patchwork/Tests/Utf8/HhvmTest.php rename to tests/Utf8/HhvmTest.php diff --git a/tests/php-utf8/Utf8CompliantTest.php b/tests/Utf8/Utf8CompliantTest.php similarity index 96% rename from tests/php-utf8/Utf8CompliantTest.php rename to tests/Utf8/Utf8CompliantTest.php index d8071b2..c738dcd 100644 --- a/tests/php-utf8/Utf8CompliantTest.php +++ b/tests/Utf8/Utf8CompliantTest.php @@ -1,12 +1,14 @@ */ -class Utf8CompliantTest extends PHPUnit_Framework_TestCase +class Utf8CompliantTest extends \PHPUnit_Framework_TestCase { public function test_valid_utf8() { diff --git a/tests/php-utf8/Utf8IsValidTest.php b/tests/Utf8/Utf8IsValidTest.php similarity index 96% rename from tests/php-utf8/Utf8IsValidTest.php rename to tests/Utf8/Utf8IsValidTest.php index b40d580..7bf608f 100644 --- a/tests/php-utf8/Utf8IsValidTest.php +++ b/tests/Utf8/Utf8IsValidTest.php @@ -1,12 +1,14 @@ */ -class Utf8IsValidTest extends PHPUnit_Framework_TestCase +class Utf8IsValidTest extends \PHPUnit_Framework_TestCase { public function test_valid_utf8() { diff --git a/tests/php-utf8/Utf8LcfirstTest.php b/tests/Utf8/Utf8LcfirstTest.php similarity index 91% rename from tests/php-utf8/Utf8LcfirstTest.php rename to tests/Utf8/Utf8LcfirstTest.php index a9c7fec..2bc0d73 100644 --- a/tests/php-utf8/Utf8LcfirstTest.php +++ b/tests/Utf8/Utf8LcfirstTest.php @@ -1,12 +1,14 @@ */ -class Utf8LcfirstTest extends PHPUnit_Framework_TestCase +class Utf8LcfirstTest extends \PHPUnit_Framework_TestCase { public function test_lcfirst() { diff --git a/tests/php-utf8/Utf8LtrimTest.php b/tests/Utf8/Utf8LtrimTest.php similarity index 94% rename from tests/php-utf8/Utf8LtrimTest.php rename to tests/Utf8/Utf8LtrimTest.php index d555da8..b7860ea 100644 --- a/tests/php-utf8/Utf8LtrimTest.php +++ b/tests/Utf8/Utf8LtrimTest.php @@ -1,12 +1,14 @@ */ -class Utf8LtrimTest extends PHPUnit_Framework_TestCase +class Utf8LtrimTest extends \PHPUnit_Framework_TestCase { public function test_trim() { diff --git a/tests/php-utf8/Utf8OrdTest.php b/tests/Utf8/Utf8OrdTest.php similarity index 89% rename from tests/php-utf8/Utf8OrdTest.php rename to tests/Utf8/Utf8OrdTest.php index 3d49fdb..e335038 100644 --- a/tests/php-utf8/Utf8OrdTest.php +++ b/tests/Utf8/Utf8OrdTest.php @@ -1,12 +1,14 @@ */ -class Utf8OrdTest extends PHPUnit_Framework_TestCase +class Utf8OrdTest extends \PHPUnit_Framework_TestCase { public function test_empty_str() { diff --git a/tests/php-utf8/Utf8RtrimTest.php b/tests/Utf8/Utf8RtrimTest.php similarity index 92% rename from tests/php-utf8/Utf8RtrimTest.php rename to tests/Utf8/Utf8RtrimTest.php index e7734a3..32fe3d5 100644 --- a/tests/php-utf8/Utf8RtrimTest.php +++ b/tests/Utf8/Utf8RtrimTest.php @@ -1,12 +1,14 @@ */ -class Utf8RtrimTest extends PHPUnit_Framework_TestCase +class Utf8RtrimTest extends \PHPUnit_Framework_TestCase { public function test_trim() { diff --git a/tests/php-utf8/Utf8StrIreplaceTest.php b/tests/Utf8/Utf8StrIreplaceTest.php similarity index 97% rename from tests/php-utf8/Utf8StrIreplaceTest.php rename to tests/Utf8/Utf8StrIreplaceTest.php index 06c7ca4..08557d8 100644 --- a/tests/php-utf8/Utf8StrIreplaceTest.php +++ b/tests/Utf8/Utf8StrIreplaceTest.php @@ -1,12 +1,14 @@ */ -class Utf8StrIreplaceTest extends PHPUnit_Framework_TestCase +class Utf8StrIreplaceTest extends \PHPUnit_Framework_TestCase { public function test_replace() { diff --git a/tests/php-utf8/Utf8StrPadTest.php b/tests/Utf8/Utf8StrPadTest.php similarity index 92% rename from tests/php-utf8/Utf8StrPadTest.php rename to tests/Utf8/Utf8StrPadTest.php index 3cff11e..2fc365e 100644 --- a/tests/php-utf8/Utf8StrPadTest.php +++ b/tests/Utf8/Utf8StrPadTest.php @@ -1,12 +1,14 @@ */ -class Utf8StrPadTest extends PHPUnit_Framework_TestCase +class Utf8StrPadTest extends \PHPUnit_Framework_TestCase { public function test_str_pad() { diff --git a/tests/php-utf8/Utf8StrSplitTest.php b/tests/Utf8/Utf8StrSplitTest.php similarity index 94% rename from tests/php-utf8/Utf8StrSplitTest.php rename to tests/Utf8/Utf8StrSplitTest.php index 7e98303..c104390 100644 --- a/tests/php-utf8/Utf8StrSplitTest.php +++ b/tests/Utf8/Utf8StrSplitTest.php @@ -1,12 +1,14 @@ */ -class Utf8StrSplitTest extends PHPUnit_Framework_TestCase +class Utf8StrSplitTest extends \PHPUnit_Framework_TestCase { public function test_split_one_char() { diff --git a/tests/php-utf8/Utf8StrWordwrapTest.php b/tests/Utf8/Utf8StrWordwrapTest.php similarity index 91% rename from tests/php-utf8/Utf8StrWordwrapTest.php rename to tests/Utf8/Utf8StrWordwrapTest.php index 185f3d3..4d2deeb 100644 --- a/tests/php-utf8/Utf8StrWordwrapTest.php +++ b/tests/Utf8/Utf8StrWordwrapTest.php @@ -1,12 +1,14 @@ */ -class Utf8StrWordwrapTest extends PHPUnit_Framework_TestCase +class Utf8StrWordwrapTest extends \PHPUnit_Framework_TestCase { public function test_no_args() { diff --git a/tests/php-utf8/Utf8StrcasecmpTest.php b/tests/Utf8/Utf8StrcasecmpTest.php similarity index 93% rename from tests/php-utf8/Utf8StrcasecmpTest.php rename to tests/Utf8/Utf8StrcasecmpTest.php index 6ec63e6..7c65f21 100644 --- a/tests/php-utf8/Utf8StrcasecmpTest.php +++ b/tests/Utf8/Utf8StrcasecmpTest.php @@ -1,12 +1,14 @@ */ -class Utf8StrcasecmpTest extends PHPUnit_Framework_TestCase +class Utf8StrcasecmpTest extends \PHPUnit_Framework_TestCase { public function test_compare_equal() { diff --git a/tests/php-utf8/Utf8StrcspnTest.php b/tests/Utf8/Utf8StrcspnTest.php similarity index 92% rename from tests/php-utf8/Utf8StrcspnTest.php rename to tests/Utf8/Utf8StrcspnTest.php index 3628c3e..1ee0d34 100644 --- a/tests/php-utf8/Utf8StrcspnTest.php +++ b/tests/Utf8/Utf8StrcspnTest.php @@ -1,12 +1,14 @@ */ -class Utf8StrcspnTest extends PHPUnit_Framework_TestCase +class Utf8StrcspnTest extends \PHPUnit_Framework_TestCase { public function test_no_match_single_byte_search() { diff --git a/tests/php-utf8/Utf8StristrTest.php b/tests/Utf8/Utf8StristrTest.php similarity index 93% rename from tests/php-utf8/Utf8StristrTest.php rename to tests/Utf8/Utf8StristrTest.php index debfd46..ce17cdc 100644 --- a/tests/php-utf8/Utf8StristrTest.php +++ b/tests/Utf8/Utf8StristrTest.php @@ -1,12 +1,14 @@ */ -class Utf8StristrTest extends PHPUnit_Framework_TestCase +class Utf8StristrTest extends \PHPUnit_Framework_TestCase { public function test_substr() { diff --git a/tests/php-utf8/Utf8StrlenTest.php b/tests/Utf8/Utf8StrlenTest.php similarity index 87% rename from tests/php-utf8/Utf8StrlenTest.php rename to tests/Utf8/Utf8StrlenTest.php index 90f9afe..3cf981b 100644 --- a/tests/php-utf8/Utf8StrlenTest.php +++ b/tests/Utf8/Utf8StrlenTest.php @@ -1,12 +1,14 @@ */ -class Utf8StrlenTest extends PHPUnit_Framework_TestCase +class Utf8StrlenTest extends \PHPUnit_Framework_TestCase { public function test_utf8() { diff --git a/tests/php-utf8/Utf8StrposTest.php b/tests/Utf8/Utf8StrposTest.php similarity index 93% rename from tests/php-utf8/Utf8StrposTest.php rename to tests/Utf8/Utf8StrposTest.php index 2de1b19..2f27b32 100644 --- a/tests/php-utf8/Utf8StrposTest.php +++ b/tests/Utf8/Utf8StrposTest.php @@ -1,12 +1,14 @@ */ -class Utf8StrposTest extends PHPUnit_Framework_TestCase +class Utf8StrposTest extends \PHPUnit_Framework_TestCase { public function test_utf8() { diff --git a/tests/php-utf8/Utf8StrrevTest.php b/tests/Utf8/Utf8StrrevTest.php similarity index 87% rename from tests/php-utf8/Utf8StrrevTest.php rename to tests/Utf8/Utf8StrrevTest.php index d9fceef..a9f248d 100644 --- a/tests/php-utf8/Utf8StrrevTest.php +++ b/tests/Utf8/Utf8StrrevTest.php @@ -1,12 +1,14 @@ */ -class Utf8StrrevTest extends PHPUnit_Framework_TestCase +class Utf8StrrevTest extends \PHPUnit_Framework_TestCase { public function test_reverse() { diff --git a/tests/php-utf8/Utf8StrrposTest.php b/tests/Utf8/Utf8StrrposTest.php similarity index 93% rename from tests/php-utf8/Utf8StrrposTest.php rename to tests/Utf8/Utf8StrrposTest.php index 20c418c..7e70f95 100644 --- a/tests/php-utf8/Utf8StrrposTest.php +++ b/tests/Utf8/Utf8StrrposTest.php @@ -1,12 +1,14 @@ */ -class Utf8StrrposTest extends PHPUnit_Framework_TestCase +class Utf8StrrposTest extends \PHPUnit_Framework_TestCase { public function test_utf8() { diff --git a/tests/php-utf8/Utf8StrspnTest.php b/tests/Utf8/Utf8StrspnTest.php similarity index 92% rename from tests/php-utf8/Utf8StrspnTest.php rename to tests/Utf8/Utf8StrspnTest.php index 5684614..10cffcc 100644 --- a/tests/php-utf8/Utf8StrspnTest.php +++ b/tests/Utf8/Utf8StrspnTest.php @@ -1,12 +1,14 @@ */ -class Utf8StrspnTest extends PHPUnit_Framework_TestCase +class Utf8StrspnTest extends \PHPUnit_Framework_TestCase { public function test_match() { diff --git a/tests/php-utf8/Utf8SubstrReplaceTest.php b/tests/Utf8/Utf8SubstrReplaceTest.php similarity index 93% rename from tests/php-utf8/Utf8SubstrReplaceTest.php rename to tests/Utf8/Utf8SubstrReplaceTest.php index 38bf23a..9008239 100644 --- a/tests/php-utf8/Utf8SubstrReplaceTest.php +++ b/tests/Utf8/Utf8SubstrReplaceTest.php @@ -1,12 +1,14 @@ */ -class Utf8SubstrReplaceTest extends PHPUnit_Framework_TestCase +class Utf8SubstrReplaceTest extends \PHPUnit_Framework_TestCase { public function test_replace_start() { diff --git a/tests/php-utf8/Utf8SubstrTest.php b/tests/Utf8/Utf8SubstrTest.php similarity index 96% rename from tests/php-utf8/Utf8SubstrTest.php rename to tests/Utf8/Utf8SubstrTest.php index b5fb128..7fc8bc0 100644 --- a/tests/php-utf8/Utf8SubstrTest.php +++ b/tests/Utf8/Utf8SubstrTest.php @@ -1,12 +1,14 @@ */ -class Utf8SubstrTest extends PHPUnit_Framework_TestCase +class Utf8SubstrTest extends \PHPUnit_Framework_TestCase { public function test_utf8() { diff --git a/tests/php-utf8/Utf8ToAsciiTest.php b/tests/Utf8/Utf8ToAsciiTest.php similarity index 90% rename from tests/php-utf8/Utf8ToAsciiTest.php rename to tests/Utf8/Utf8ToAsciiTest.php index 03a133e..9e4ba52 100644 --- a/tests/php-utf8/Utf8ToAsciiTest.php +++ b/tests/Utf8/Utf8ToAsciiTest.php @@ -1,12 +1,14 @@ */ -class Utf8ToAsciiTest extends PHPUnit_Framework_TestCase +class Utf8ToAsciiTest extends \PHPUnit_Framework_TestCase { public function test_utf8() { diff --git a/tests/php-utf8/Utf8TrimTest.php b/tests/Utf8/Utf8TrimTest.php similarity index 88% rename from tests/php-utf8/Utf8TrimTest.php rename to tests/Utf8/Utf8TrimTest.php index c730775..83c9cc7 100644 --- a/tests/php-utf8/Utf8TrimTest.php +++ b/tests/Utf8/Utf8TrimTest.php @@ -1,12 +1,14 @@ */ -class Utf8TrimTest extends PHPUnit_Framework_TestCase +class Utf8TrimTest extends \PHPUnit_Framework_TestCase { public function test_trim() { diff --git a/tests/php-utf8/Utf8UcfirstTest.php b/tests/Utf8/Utf8UcfirstTest.php similarity index 92% rename from tests/php-utf8/Utf8UcfirstTest.php rename to tests/Utf8/Utf8UcfirstTest.php index 5027127..ffb88d1 100644 --- a/tests/php-utf8/Utf8UcfirstTest.php +++ b/tests/Utf8/Utf8UcfirstTest.php @@ -1,12 +1,14 @@ */ -class Utf8UcfirstTest extends PHPUnit_Framework_TestCase +class Utf8UcfirstTest extends \PHPUnit_Framework_TestCase { public function test_ucfirst() { diff --git a/tests/php-utf8/Utf8UcwordsTest.php b/tests/Utf8/Utf8UcwordsTest.php similarity index 93% rename from tests/php-utf8/Utf8UcwordsTest.php rename to tests/Utf8/Utf8UcwordsTest.php index ced3891..3d5bf8b 100644 --- a/tests/php-utf8/Utf8UcwordsTest.php +++ b/tests/Utf8/Utf8UcwordsTest.php @@ -1,12 +1,14 @@ */ -class Utf8UcwordsTest extends PHPUnit_Framework_TestCase +class Utf8UcwordsTest extends \PHPUnit_Framework_TestCase { public function test_ucword() { diff --git a/tests/Patchwork/Tests/Utf8/WindowsStreamWrapperTest.php b/tests/Utf8/WindowsStreamWrapperTest.php similarity index 98% rename from tests/Patchwork/Tests/Utf8/WindowsStreamWrapperTest.php rename to tests/Utf8/WindowsStreamWrapperTest.php index 43eba93..38e1abd 100644 --- a/tests/Patchwork/Tests/Utf8/WindowsStreamWrapperTest.php +++ b/tests/Utf8/WindowsStreamWrapperTest.php @@ -74,6 +74,8 @@ public function testDir() 'TurkishUtf8Test.php', 'Utf8', 'Utf8Test.php', + 'bootstrap.php', + 'compile-unicode-data.php', 'µ€', ); $d = array(); diff --git a/tests/Patchwork/Tests/Utf8Test.php b/tests/Utf8Test.php similarity index 100% rename from tests/Patchwork/Tests/Utf8Test.php rename to tests/Utf8Test.php diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 01f3cfe..e844478 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,18 +1,3 @@ - */ -class NormalizationTest extends \PHPUnit_Framework_TestCase -{ - public $unicodeVersion = 70; - - /** - * @covers Patchwork\PHP\Shim\Normalizer::normalize - */ - public function testNormalize() - { - $t = file(__DIR__.'/NormalizationTest.'.$this->unicodeVersion.'.txt'); - $c = array(); - - foreach ($t as $s) { - $t = explode('#', $s); - $t = explode(';', $t[0]); - - if (6 === count($t)) { - foreach ($t as $k => $s) { - $t = explode(' ', $s); - $t = array_map('hexdec', $t); - $t = array_map('Patchwork\Utf8::chr', $t); - $c[$k] = implode('', $t); - } - - $this->assertSame($c[1], n::normalize($c[0], n::NFC)); - $this->assertSame($c[1], n::normalize($c[1], n::NFC)); - $this->assertSame($c[1], n::normalize($c[2], n::NFC)); - $this->assertSame($c[3], n::normalize($c[3], n::NFC)); - $this->assertSame($c[3], n::normalize($c[4], n::NFC)); - - $this->assertSame($c[2], n::normalize($c[0], n::NFD)); - $this->assertSame($c[2], n::normalize($c[1], n::NFD)); - $this->assertSame($c[2], n::normalize($c[2], n::NFD)); - $this->assertSame($c[4], n::normalize($c[3], n::NFD)); - $this->assertSame($c[4], n::normalize($c[4], n::NFD)); - - $this->assertSame($c[3], n::normalize($c[0], n::NFKC)); - $this->assertSame($c[3], n::normalize($c[1], n::NFKC)); - $this->assertSame($c[3], n::normalize($c[2], n::NFKC)); - $this->assertSame($c[3], n::normalize($c[3], n::NFKC)); - $this->assertSame($c[3], n::normalize($c[4], n::NFKC)); - - $this->assertSame($c[4], n::normalize($c[0], n::NFKD)); - $this->assertSame($c[4], n::normalize($c[1], n::NFKD)); - $this->assertSame($c[4], n::normalize($c[2], n::NFKD)); - $this->assertSame($c[4], n::normalize($c[3], n::NFKD)); - $this->assertSame($c[4], n::normalize($c[4], n::NFKD)); - } - } - } -}