31 define(
'NORMALIZE_ICU', function_exists(
'utf8_normalize' ) );
32 define(
'NORMALIZE_INTL', function_exists(
'normalizer_normalize' ) );
64 # Load compatibility decompositions on demand if they are needed.
83 # UnicodeString constructor fails if the string ends with a
84 # head byte. Add a junk char at the end, we'll strip it off.
85 return rtrim( utf8_normalize( $string .
"\x01", self::UNORM_NFC ),
"\x01" );
88 $norm = normalizer_normalize( $string, Normalizer::FORM_C );
89 if( $norm ===
null || $norm ===
false ) {
90 # normalizer_normalize will either return false or null
91 # (depending on which doc you read) if invalid utf8 string.
92 # quickIsNFCVerify cleans up invalid sequences.
95 # if that's true, the string is actually already normal.
98 # Now we are valid but non-normal
99 return normalizer_normalize( $string, Normalizer::FORM_C );
105 # Side effect -- $string has had UTF-8 errors cleaned up.
122 return normalizer_normalize( $string, Normalizer::FORM_C );
124 return utf8_normalize( $string, self::UNORM_NFC );
140 return normalizer_normalize( $string, Normalizer::FORM_D );
142 return utf8_normalize( $string, self::UNORM_NFD );
143 elseif( preg_match(
'/[\x80-\xff]/', $string ) )
159 return normalizer_normalize( $string, Normalizer::FORM_KC );
161 return utf8_normalize( $string, self::UNORM_NFKC );
162 elseif( preg_match(
'/[\x80-\xff]/', $string ) )
178 return normalizer_normalize( $string, Normalizer::FORM_KD );
180 return utf8_normalize( $string, self::UNORM_NFKD );
181 elseif( preg_match(
'/[\x80-\xff]/', $string ) )
192 if( !isset( self::$utfCombiningClass ) ) {
193 require_once __DIR__ .
'/UtfNormalData.inc';
204 # ASCII is always valid NFC!
205 # If it's pure ASCII, let it through.
206 if( !preg_match(
'/[\x80-\xff]/', $string ) )
return true;
209 $len = strlen( $string );
210 for( $i = 0; $i < $len; $i++ ) {
215 } elseif(
$n >= 0xf0 ) {
216 $c = substr( $string, $i, 4 );
218 } elseif(
$n >= 0xe0 ) {
219 $c = substr( $string, $i, 3 );
221 } elseif(
$n >= 0xc0 ) {
222 $c = substr( $string, $i, 2 );
225 if( isset( self::$utfCheckNFC[$c] ) ) {
226 # If it's NO or MAYBE, bail and do the slow check.
229 if( isset( self::$utfCombiningClass[$c] ) ) {
230 # Combining character? We might have to do sorting, at least.
244 # Screen out some characters that eg won't be allowed in XML
245 $string = preg_replace(
'/[\x00-\x08\x0b\x0c\x0e-\x1f]/',
UTF8_REPLACEMENT, $string );
247 # ASCII is always valid NFC!
248 # If we're only ever given plain ASCII, we can avoid the overhead
249 # of initializing the decomposition tables by skipping out early.
250 if( !preg_match(
'/[\x80-\xff]/', $string ) )
return true;
252 static $checkit =
null, $tailBytes =
null, $utfCheckOrCombining =
null;
253 if( !isset( $checkit ) ) {
254 # Load/build some scary lookup tables...
257 $utfCheckOrCombining = array_merge( self::$utfCheckNFC, self::$utfCombiningClass );
259 # Head bytes for sequences which we should do further validity checks
260 $checkit = array_flip( array_map(
'chr',
261 array( 0xc0, 0xc1, 0xe0, 0xed, 0xef,
262 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
263 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff ) ) );
265 # Each UTF-8 head byte is followed by a certain
266 # number of tail bytes.
267 $tailBytes =
array();
268 for(
$n = 0;
$n < 256;
$n++ ) {
271 } elseif(
$n < 0xe0 ) {
273 } elseif(
$n < 0xf0 ) {
275 } elseif(
$n < 0xf8 ) {
277 } elseif(
$n < 0xfc ) {
279 } elseif(
$n < 0xfe ) {
284 $tailBytes[chr(
$n)] = $remaining;
288 # Chop the text into pure-ASCII and non-ASCII areas;
289 # large ASCII parts can be handled much more quickly.
290 # Don't chop up Unicode areas for punctuation, though,
291 # that wastes energy.
294 '/([\x00-\x7f]+|[\x80-\xff][\x00-\x40\x5b-\x5f\x7b-\xff]*)/',
301 $chunk = strlen( $str );
303 if( $str[0] <
"\x80" ) {
304 # ASCII chunk: guaranteed to be valid UTF-8
305 # and in normal form C, so skip over it.
310 # We'll have to examine the chunk byte by byte to ensure
311 # that it consists of valid UTF-8 sequences, and to see
312 # if any of them might not be normalized.
314 # Since PHP is not the fastest language on earth, some of
315 # this code is a little ugly with inner loop optimizations.
318 $len = $chunk + 1; # Counting down
is faster. I
'm *so* sorry.
320 for( $i = -1; --$len; ) {
321 $remaining = $tailBytes[$c = $str[++$i]];
324 $sequence = $head = $c;
326 # Look for the defined number of tail bytes...
327 if( --$len && ( $c = $str[++$i] ) >= "\x80" && $c < "\xc0" ) {
328 # Legal tail bytes are nice.
332 # Premature end of string!
333 # Drop a replacement character into output to
334 # represent the invalid UTF-8 sequence.
335 $replace[] = array( UTF8_REPLACEMENT,
336 $base + $i + 1 - strlen( $sequence ),
337 strlen( $sequence ) );
340 # Illegal tail byte; abandon the sequence.
341 $replace[] = array( UTF8_REPLACEMENT,
342 $base + $i - strlen( $sequence ),
343 strlen( $sequence ) );
344 # Back up and reprocess this byte; it may itself
345 # be a legal ASCII or UTF-8 sequence head.
351 } while( --$remaining );
353 if( isset( $checkit[$head] ) ) {
354 # Do some more detailed validity checks, for
355 # invalid characters and illegal sequences.
356 if( $head == "\xed" ) {
357 # 0xed is relatively frequent in Korean, which
358 # abuts the surrogate area, so we're doing
359 # this check separately to speed things up.
362 # Surrogates are legal only in UTF-16 code.
363 # They are totally forbidden here in UTF-8
366 $base + $i + 1 - strlen( $sequence ),
367 strlen( $sequence ) );
372 # Slower, but rarer checks...
375 #
"Overlong sequences" are those
that are syntactically
376 # correct but use more UTF-8 bytes than
are necessary to
377 # encode a
character. Naïve
string comparisons can be
378 # tricked into failing to
see a match
for an ASCII
379 #
character,
for instance, which can be a security hole
380 #
if blacklist checks
are being
used.
385 # U+FFFE and U+FFFF
are explicitly forbidden
in Unicode.
390 # Unicode has been limited to 21 bits; longer
391 # sequences
are not allowed.
395 $base + $i + 1 - strlen( $sequence ),
396 strlen( $sequence ) );
403 if( isset( $utfCheckOrCombining[$sequence] ) ) {
404 # If it's NO or MAYBE, we'll have to rip
405 # the string apart and put it back together.
406 # That's going to be mighty slow.
407 $looksNormal =
false;
410 # The sequence is legal!
412 } elseif( $c <
"\x80" ) {
415 } elseif( $c <
"\xc0" ) {
421 # Don't add if we're continuing a broken sequence;
422 # we already put a replacement character when we looked
423 # at the broken sequence.
424 $replace[] =
array(
'', $base + $i, 1 );
427 # Miscellaneous freaks.
434 if( count( $replace ) ) {
435 # There were illegal UTF-8 sequences we need to fix up.
438 foreach( $replace
as $rep ) {
439 list( $replacement, $start, $length ) = $rep;
440 if(
$last < $start ) {
443 $out .= $replacement;
444 $last = $start + $length;
446 if(
$last < strlen( $string ) ) {
454 # These take a string and run the normalization on them, without
455 # checking for validity or any optimization etc. Input must be
462 static function NFC( $string ) {
471 static function NFD( $string ) {
483 static function NFKC( $string ) {
492 static function NFKD( $string ) {
493 if( !isset( self::$utfCompatibilityDecomp ) ) {
494 require_once
'UtfNormalDataK.inc';
496 return self::fastCombiningSort(
497 self::fastDecompose( $string, self::$utfCompatibilityDecomp ) );
512 $len = strlen( $string );
514 for( $i = 0; $i < $len; $i++ ) {
518 # ASCII chars never decompose
522 } elseif(
$n >= 0xf0 ) {
523 $c = substr( $string, $i, 4 );
525 } elseif(
$n >= 0xe0 ) {
526 $c = substr( $string, $i, 3 );
528 } elseif(
$n >= 0xc0 ) {
529 $c = substr( $string, $i, 2 );
532 if( isset( $map[$c] ) ) {
537 # Decompose a hangul syllable into jamo;
538 # hardcoded for three-byte UTF-8 sequence.
539 # A lookup table would be slightly faster,
540 # but adds a lot of memory & disk needs.
542 $index = ( (ord( $c[0] ) & 0x0f) << 12
543 | (ord( $c[1] ) & 0x3f) << 6
544 | (ord( $c[2] ) & 0x3f) )
549 $out .=
"\xe1\x84" . chr( 0x80 + $l ) .
"\xe1\x85" . chr( 0xa1 + $v );
551 $out .=
"\xe1\x87" . chr( 0x80 +
$t - 25 );
553 $out .=
"\xe1\x86" . chr( 0xa7 +
$t );
572 $len = strlen( $string );
574 $combiners =
array();
576 for( $i = 0; $i < $len; $i++ ) {
581 $c = substr( $string, $i, 4 );
583 } elseif(
$n >= 0xe0 ) {
584 $c = substr( $string, $i, 3 );
586 } elseif(
$n >= 0xc0 ) {
587 $c = substr( $string, $i, 2 );
590 if( isset( self::$utfCombiningClass[$c] ) ) {
591 $lastClass = self::$utfCombiningClass[$c];
592 if( isset( $combiners[$lastClass] ) ) {
593 $combiners[$lastClass] .= $c;
595 $combiners[$lastClass] = $c;
602 $out .= implode(
'', $combiners );
603 $combiners =
array();
610 $out .= implode(
'', $combiners );
624 $len = strlen( $string );
632 for( $i = 0; $i < $len; $i++ ) {
636 # No combining characters here...
643 } elseif(
$n >= 0xf0 ) {
644 $c = substr( $string, $i, 4 );
646 } elseif(
$n >= 0xe0 ) {
647 $c = substr( $string, $i, 3 );
649 } elseif(
$n >= 0xc0 ) {
650 $c = substr( $string, $i, 2 );
653 $pair = $startChar . $c;
655 if( isset( self::$utfCombiningClass[$c] ) ) {
656 # A combining char; see what we can do with it
657 $class = self::$utfCombiningClass[$c];
658 if( !empty( $startChar ) &&
659 $lastClass < $class &&
661 isset( self::$utfCanonicalComp[$pair] ) ) {
662 $startChar = self::$utfCanonicalComp[$pair];
673 if( $lastClass == 0 ) {
674 if( isset( self::$utfCanonicalComp[$pair] ) ) {
675 $startChar = self::$utfCanonicalComp[$pair];
679 if(
$n >= $x1 &&
$n <= $x2 ) {
680 # WARNING: Hangul code is painfully slow.
681 # I apologize for this ugly, ugly code; however
682 # performance is even more teh suck if we call
683 # out to nice clean functions. Lookup tables are
684 # marginally faster, but require a lot of space.
691 #$lIndex = utf8ToCodepoint( $startChar ) - UNICODE_HANGUL_LBASE;
692 #$vIndex = utf8ToCodepoint( $c ) - UNICODE_HANGUL_VBASE;
693 $lIndex = ord( $startChar[2] ) - 0x80;
694 $vIndex = ord( $c[2] ) - 0xa1;
700 # Hardcode the limited-range UTF-8 conversion:
701 $startChar = chr( $hangulPoint >> 12 & 0x0f | 0xe0 ) .
702 chr( $hangulPoint >> 6 & 0x3f | 0x80 ) .
703 chr( $hangulPoint & 0x3f | 0x80 );
711 # $tIndex = utf8ToCodepoint( $c ) - UNICODE_HANGUL_TBASE;
712 $tIndex = ord( $c[2] ) - 0xa7;
713 if( $tIndex < 0 ) $tIndex = ord( $c[2] ) - 0x80 + (0x11c0 - 0x11a7);
715 # Increment the code point by $tIndex, without
716 # the function overhead of decoding and recoding UTF-8
718 $tail = ord( $startChar[2] ) + $tIndex;
721 $mid = ord( $startChar[1] ) + 1;
723 $startChar[0] = chr( ord( $startChar[0] ) + 1 );
726 $startChar[1] = chr( $mid );
728 $startChar[2] = chr( $tail );
730 # If there's another jamo char after this, *don't* try to merge it.
743 $out .= $startChar . $combining;
754 $len = strlen( $string );
756 for( $i = 0; $i < $len; $i++ ) {
769 $string = preg_replace(
770 '/[\x00-\x08\x0b\x0c\x0e-\x1f]/',