Вход Регистрация
Файл: vendor/symfony/polyfill-intl-normalizer/Normalizer.php
Строк: 166
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace SymfonyPolyfillIntlNormalizer;

/**
 * Normalizer is a PHP fallback implementation of the Normalizer class provided by the intl extension.
 *
 * It has been validated with Unicode 6.3 Normalization Conformance Test.
 * See http://www.unicode.org/reports/tr15/ for detailed info about Unicode normalizations.
 *
 * @author Nicolas Grekas <p@tchwork.com>
 *
 * @internal
 */
class Normalizer
{
    public const 
FORM_D Normalizer::FORM_D;
    public const 
FORM_KD Normalizer::FORM_KD;
    public const 
FORM_C Normalizer::FORM_C;
    public const 
FORM_KC Normalizer::FORM_KC;
    public const 
NFD Normalizer::NFD;
    public const 
NFKD Normalizer::NFKD;
    public const 
NFC Normalizer::NFC;
    public const 
NFKC Normalizer::NFKC;

    private static 
$C;
    private static 
$D;
    private static 
$KD;
    private static 
$cC;
    private static 
$ulenMask = ["xC0" => 2"xD0" => 2"xE0" => 3"xF0" => 4];
    private static 
$ASCII "x20x65x69x61x73x6Ex74x72x6Fx6Cx75x64x5Dx5Bx63x6Dx70x27x0Ax67x7Cx68x76x2Ex66x62x2Cx3Ax3Dx2Dx71x31x30x43x32x2Ax79x78x29x28x4Cx39x41x53x2Fx50x22x45x6Ax4Dx49x6Bx33x3Ex35x54x3Cx44x34x7Dx42x7Bx38x46x77x52x36x37x55x47x4Ex3Bx4Ax7Ax56x23x48x4Fx57x5Fx26x21x4Bx3Fx58x51x25x59x5Cx09x5Ax2Bx7Ex5Ex24x40x60x7Fx00x01x02x03x04x05x06x07x08x0Bx0Cx0Dx0Ex0Fx10x11x12x13x14x15x16x17x18x19x1Ax1Bx1Cx1Dx1Ex1F";

    public static function 
isNormalized(string $sint $form self::FORM_C)
    {
        if (!
in_array($form, [self::NFDself::NFKDself::NFCself::NFKC])) {
            return 
false;
        }
        if (!isset(
$s[strspn($sself::$ASCII)])) {
            return 
true;
        }
        if (
self::NFC == $form && preg_match('//u'$s) && !preg_match('/[^x00-x{2FF}]/u'$s)) {
            return 
true;
        }

        return 
self::normalize($s$form) === $s;
    }

    public static function 
normalize(string $sint $form self::FORM_C)
    {
        if (!
preg_match('//u'$s)) {
            return 
false;
        }

        switch (
$form) {
            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:
                if (
defined('Normalizer::NONE') && Normalizer::NONE == $form) {
                    return 
$s;
                }

                if (
80000 PHP_VERSION_ID) {
                    return 
false;
                }

                throw new 
ValueError('normalizer_normalize(): Argument #2 ($form) must be a a valid normalization form');
        }

        if (
'' === $s) {
            return 
'';
        }

        if (
$K && null === self::$KD) {
            
self::$KD self::getData('compatibilityDecomposition');
        }

        if (
null === self::$D) {
            
self::$D self::getData('canonicalDecomposition');
            
self::$cC self::getData('combiningClass');
        }

        if (
null !== $mbEncoding = (/* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) ? mb_internal_encoding() : null) {
            
mb_internal_encoding('8bit');
        }

        
$r self::decompose($s$K);

        if (
$C) {
            if (
null === self::$C) {
                
self::$C self::getData('canonicalComposition');
            }

            
$r self::recompose($r);
        }
        if (
null !== $mbEncoding) {
            
mb_internal_encoding($mbEncoding);
        }

        return 
$r;
    }

    private static function 
recompose($s)
    {
        
$ASCII self::$ASCII;
        
$compMap self::$C;
        
$combClass self::$cC;
        
$ulenMask self::$ulenMask;

        
$result $tail '';

        
$i $s[0] < "x80" $ulenMask[$s[0] & "xF0"];
        
$len strlen($s);

        
$lastUchr substr($s0$i);
        
$lastUcls = isset($combClass[$lastUchr]) ? 256 0;

        while (
$i $len) {
            if (
$s[$i] < "x80") {
                
// ASCII chars

                
if ($tail) {
                    
$lastUchr .= $tail;
                    
$tail '';
                }

                if (
$j strspn($s$ASCII$i 1)) {
                    
$lastUchr .= substr($s$i$j);
                    
$i += $j;
                }

                
$result .= $lastUchr;
                
$lastUchr $s[$i];
                
$lastUcls 0;
                ++
$i;
                continue;
            }

            
$ulen $ulenMask[$s[$i] & "xF0"];
            
$uchr substr($s$i$ulen);

            if (
$lastUchr "xE1x84x80" || "xE1x84x92" $lastUchr
                
|| $uchr "xE1x85xA1" || "xE1x85xB5" $uchr
                
|| $lastUcls) {
                
// Table lookup and combining chars composition

                
$ucls $combClass[$uchr] ?? 0;

                if (isset(
$compMap[$lastUchr.$uchr]) && (!$lastUcls || $lastUcls $ucls)) {
                    
$lastUchr $compMap[$lastUchr.$uchr];
                } elseif (
$lastUcls $ucls) {
                    
$tail .= $uchr;
                } else {
                    if (
$tail) {
                        
$lastUchr .= $tail;
                        
$tail '';
                    }

                    
$result .= $lastUchr;
                    
$lastUchr $uchr;
                }
            } else {
                
// Hangul chars

                
$L ord($lastUchr[2]) - 0x80;
                
$V ord($uchr[2]) - 0xA1;
                
$T 0;

                
$uchr substr($s$i $ulen3);

                if (
"xE1x86xA7" <= $uchr && $uchr <= "xE1x87x82") {
                    
$T ord($uchr[2]) - 0xA7;
                    
$T && $T += 0x40;
                    
$ulen += 3;
                }

                
$L 0xAC00 + ($L 21 $V) * 28 $T;
                
$lastUchr chr(0xE0 $L >> 12).chr(0x80 $L >> 0x3F).chr(0x80 $L 0x3F);
            }

            
$i += $ulen;
        }

        return 
$result.$lastUchr.$tail;
    }

    private static function 
decompose($s$c)
    {
        
$result '';

        
$ASCII self::$ASCII;
        
$decompMap self::$D;
        
$combClass self::$cC;
        
$ulenMask self::$ulenMask;
        if (
$c) {
            
$compatMap self::$KD;
        }

        
$c = [];
        
$i 0;
        
$len strlen($s);

        while (
$i $len) {
            if (
$s[$i] < "x80") {
                
// ASCII chars

                
if ($c) {
                    
ksort($c);
                    
$result .= implode(''$c);
                    
$c = [];
                }

                
$j strspn($s$ASCII$i 1);
                
$result .= substr($s$i$j);
                
$i += $j;
                continue;
            }

            
$ulen $ulenMask[$s[$i] & "xF0"];
            
$uchr substr($s$i$ulen);
            
$i += $ulen;

            if (
$uchr "xEAxB0x80" || "xEDx9ExA3" $uchr) {
                
// Table lookup

                
if ($uchr !== $j $compatMap[$uchr] ?? ($decompMap[$uchr] ?? $uchr)) {
                    
$uchr $j;

                    
$j strlen($uchr);
                    
$ulen $uchr[0] < "x80" $ulenMask[$uchr[0] & "xF0"];

                    if (
$ulen != $j) {
                        
// Put trailing chars in $s

                        
$j -= $ulen;
                        
$i -= $j;

                        if (
$i) {
                            
$s str_repeat(' ', -$i).$s;
                            
$len -= $i;
                            
$i 0;
                        }

                        while (
$j--) {
                            
$s[$i $j] = $uchr[$ulen $j];
                        }

                        
$uchr substr($uchr0$ulen);
                    }
                }
                if (isset(
$combClass[$uchr])) {
                    
// Combining chars, for sorting

                    
if (!isset($c[$combClass[$uchr]])) {
                        
$c[$combClass[$uchr]] = '';
                    }
                    
$c[$combClass[$uchr]] .= $uchr;
                    continue;
                }
            } else {
                
// Hangul chars

                
$uchr unpack('C*'$uchr);
                
$j = (($uchr[1] - 224) << 12) + (($uchr[2] - 128) << 6) + $uchr[3] - 0xAC80;

                
$uchr "xE1x84".chr(0x80 + (int) ($j 588))
                       .
"xE1x85".chr(0xA1 + (int) (($j 588) / 28));

                if (
$j %= 28) {
                    
$uchr .= $j 25
                        
? ("xE1x86".chr(0xA7 $j))
                        : (
"xE1x87".chr(0x67 $j));
                }
            }
            if (
$c) {
                
ksort($c);
                
$result .= implode(''$c);
                
$c = [];
            }

            
$result .= $uchr;
        }

        if (
$c) {
            
ksort($c);
            
$result .= implode(''$c);
        }

        return 
$result;
    }

    private static function 
getData($file)
    {
        if (
file_exists($file __DIR__.'/Resources/unidata/'.$file.'.php')) {
            return require 
$file;
        }

        return 
false;
    }
}
Онлайн: 1
Реклама