2004-08-29 10:30:23 +00:00
|
|
|
<?php
|
|
|
|
|
# Copyright (C) 2004 Brion Vibber <brion@pobox.com>
|
|
|
|
|
# http://www.mediawiki.org/
|
|
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
# http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
|
2004-09-03 23:00:01 +00:00
|
|
|
/**
|
|
|
|
|
* Unicode normalization routines for working with UTF-8 strings.
|
|
|
|
|
* Currently assumes that input strings are valid UTF-8!
|
|
|
|
|
*
|
|
|
|
|
* Not as fast as I'd like, but should be usable for most purposes.
|
|
|
|
|
* UtfNormal::toNFC() will bail early if given ASCII text or text
|
|
|
|
|
* it can quickly deterimine is already normalized.
|
|
|
|
|
*
|
|
|
|
|
* All functions can be called static.
|
|
|
|
|
*
|
|
|
|
|
* See description of forms at http://www.unicode.org/reports/tr15/
|
|
|
|
|
*
|
2004-10-28 02:56:13 +00:00
|
|
|
* @package UtfNormal
|
2004-09-03 23:00:01 +00:00
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
|
2004-09-03 23:00:01 +00:00
|
|
|
/** */
|
2004-08-29 10:30:23 +00:00
|
|
|
require_once 'UtfNormalUtil.php';
|
2004-10-09 08:08:26 +00:00
|
|
|
|
|
|
|
|
global $utfCombiningClass, $utfCanonicalComp, $utfCanonicalDecomp;
|
|
|
|
|
$utfCombiningClass = NULL;
|
|
|
|
|
$utfCanonicalComp = NULL;
|
|
|
|
|
$utfCanonicalDecomp = NULL;
|
2004-08-29 10:30:23 +00:00
|
|
|
|
2004-09-02 07:39:06 +00:00
|
|
|
# Load compatibility decompositions on demand if they are needed.
|
|
|
|
|
global $utfCompatibilityDecomp;
|
|
|
|
|
$utfCompatibilityDecomp = NULL;
|
|
|
|
|
|
2004-08-29 10:30:23 +00:00
|
|
|
define( 'UNICODE_HANGUL_FIRST', 0xac00 );
|
|
|
|
|
define( 'UNICODE_HANGUL_LAST', 0xd7a3 );
|
|
|
|
|
|
|
|
|
|
define( 'UNICODE_HANGUL_LBASE', 0x1100 );
|
|
|
|
|
define( 'UNICODE_HANGUL_VBASE', 0x1161 );
|
|
|
|
|
define( 'UNICODE_HANGUL_TBASE', 0x11a7 );
|
|
|
|
|
|
|
|
|
|
define( 'UNICODE_HANGUL_LCOUNT', 19 );
|
|
|
|
|
define( 'UNICODE_HANGUL_VCOUNT', 21 );
|
|
|
|
|
define( 'UNICODE_HANGUL_TCOUNT', 28 );
|
|
|
|
|
define( 'UNICODE_HANGUL_NCOUNT', UNICODE_HANGUL_VCOUNT * UNICODE_HANGUL_TCOUNT );
|
|
|
|
|
|
|
|
|
|
define( 'UNICODE_HANGUL_LEND', UNICODE_HANGUL_LBASE + UNICODE_HANGUL_LCOUNT - 1 );
|
|
|
|
|
define( 'UNICODE_HANGUL_VEND', UNICODE_HANGUL_VBASE + UNICODE_HANGUL_VCOUNT - 1 );
|
|
|
|
|
define( 'UNICODE_HANGUL_TEND', UNICODE_HANGUL_TBASE + UNICODE_HANGUL_TCOUNT - 1 );
|
|
|
|
|
|
2004-09-03 05:39:30 +00:00
|
|
|
define( 'UNICODE_SURROGATE_FIRST', 0xd800 );
|
|
|
|
|
define( 'UNICODE_SURROGATE_LAST', 0xdfff );
|
|
|
|
|
define( 'UNICODE_MAX', 0x10ffff );
|
|
|
|
|
define( 'UNICODE_REPLACEMENT', 0xfffd );
|
|
|
|
|
|
|
|
|
|
|
2004-08-29 10:30:23 +00:00
|
|
|
define( 'UTF8_HANGUL_FIRST', codepointToUtf8( UNICODE_HANGUL_FIRST ) );
|
|
|
|
|
define( 'UTF8_HANGUL_LAST', codepointToUtf8( UNICODE_HANGUL_LAST ) );
|
|
|
|
|
|
|
|
|
|
define( 'UTF8_HANGUL_LBASE', codepointToUtf8( UNICODE_HANGUL_LBASE ) );
|
|
|
|
|
define( 'UTF8_HANGUL_VBASE', codepointToUtf8( UNICODE_HANGUL_VBASE ) );
|
|
|
|
|
define( 'UTF8_HANGUL_TBASE', codepointToUtf8( UNICODE_HANGUL_TBASE ) );
|
|
|
|
|
|
|
|
|
|
define( 'UTF8_HANGUL_LEND', codepointToUtf8( UNICODE_HANGUL_LEND ) );
|
|
|
|
|
define( 'UTF8_HANGUL_VEND', codepointToUtf8( UNICODE_HANGUL_VEND ) );
|
|
|
|
|
define( 'UTF8_HANGUL_TEND', codepointToUtf8( UNICODE_HANGUL_TEND ) );
|
|
|
|
|
|
2004-09-03 05:39:30 +00:00
|
|
|
define( 'UTF8_SURROGATE_FIRST', codepointToUtf8( UNICODE_SURROGATE_FIRST ) );
|
|
|
|
|
define( 'UTF8_SURROGATE_LAST', codepointToUtf8( UNICODE_SURROGATE_LAST ) );
|
|
|
|
|
define( 'UTF8_MAX', codepointToUtf8( UNICODE_MAX ) );
|
|
|
|
|
define( 'UTF8_REPLACEMENT', codepointToUtf8( UNICODE_REPLACEMENT ) );
|
|
|
|
|
#define( 'UTF8_REPLACEMENT', '!' );
|
|
|
|
|
|
|
|
|
|
define( 'UTF8_OVERLONG_A', "\xc1\xbf" );
|
|
|
|
|
define( 'UTF8_OVERLONG_B', "\xe0\x9f\xbf" );
|
|
|
|
|
define( 'UTF8_OVERLONG_C', "\xf0\x8f\xbf\xbf" );
|
|
|
|
|
|
|
|
|
|
# These two ranges are illegal
|
|
|
|
|
define( 'UTF8_FDD0', codepointToUtf8( 0xfdd0 ) );
|
|
|
|
|
define( 'UTF8_FDEF', codepointToUtf8( 0xfdef ) );
|
|
|
|
|
define( 'UTF8_FFFE', codepointToUtf8( 0xfffe ) );
|
|
|
|
|
define( 'UTF8_FFFF', codepointToUtf8( 0xffff ) );
|
|
|
|
|
|
|
|
|
|
define( 'UTF8_HEAD', false );
|
|
|
|
|
define( 'UTF8_TAIL', true );
|
|
|
|
|
|
2004-10-07 05:59:10 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* For using the ICU wrapper
|
|
|
|
|
*/
|
|
|
|
|
define( 'UNORM_NONE', 1 );
|
|
|
|
|
define( 'UNORM_NFD', 2 );
|
|
|
|
|
define( 'UNORM_NFKD', 3 );
|
|
|
|
|
define( 'UNORM_NFC', 4 );
|
|
|
|
|
define( 'UNORM_DEFAULT', UNORM_NFC );
|
|
|
|
|
define( 'UNORM_NFKC', 5 );
|
|
|
|
|
define( 'UNORM_FCD', 6 );
|
|
|
|
|
|
|
|
|
|
define( 'NORMALIZE_ICU', function_exists( 'utf8_normalize' ) );
|
|
|
|
|
|
2004-09-03 23:00:01 +00:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @package MediaWiki
|
|
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
class UtfNormal {
|
2004-09-04 09:35:01 +00:00
|
|
|
/**
|
|
|
|
|
* The ultimate convenience function! Clean up invalid UTF-8 sequences,
|
|
|
|
|
* and convert to normal form C, canonical composition.
|
|
|
|
|
*
|
|
|
|
|
* Fast return for pure ASCII strings; some lesser optimizations for
|
|
|
|
|
* strings containing only known-good characters. Not as fast as toNFC().
|
|
|
|
|
*
|
|
|
|
|
* @param string $string a UTF-8 string
|
|
|
|
|
* @return string a clean, shiny, normalized UTF-8 string
|
|
|
|
|
*/
|
2004-09-03 05:39:30 +00:00
|
|
|
function cleanUp( $string ) {
|
|
|
|
|
if( UtfNormal::quickIsNFCVerify( $string ) )
|
|
|
|
|
return $string;
|
|
|
|
|
else
|
|
|
|
|
return UtfNormal::NFC( $string );
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-04 09:35:01 +00:00
|
|
|
/**
|
|
|
|
|
* Convert a UTF-8 string to normal form C, canonical composition.
|
|
|
|
|
* Fast return for pure ASCII strings; some lesser optimizations for
|
|
|
|
|
* strings containing only known-good characters.
|
|
|
|
|
*
|
|
|
|
|
* @param string $string a valid UTF-8 string. Input is not validated.
|
|
|
|
|
* @return string a UTF-8 string in normal form C
|
|
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
function toNFC( $string ) {
|
2004-10-07 05:59:10 +00:00
|
|
|
if( NORMALIZE_ICU )
|
|
|
|
|
return utf8_normalize( $string, UNORM_NFC );
|
|
|
|
|
elseif( UtfNormal::quickIsNFC( $string ) )
|
2004-08-29 10:30:23 +00:00
|
|
|
return $string;
|
|
|
|
|
else
|
|
|
|
|
return UtfNormal::NFC( $string );
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-04 09:35:01 +00:00
|
|
|
/**
|
|
|
|
|
* Convert a UTF-8 string to normal form D, canonical decomposition.
|
|
|
|
|
* Fast return for pure ASCII strings.
|
|
|
|
|
*
|
|
|
|
|
* @param string $string a valid UTF-8 string. Input is not validated.
|
|
|
|
|
* @return string a UTF-8 string in normal form D
|
|
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
function toNFD( $string ) {
|
2004-10-07 05:59:10 +00:00
|
|
|
if( NORMALIZE_ICU )
|
|
|
|
|
return utf8_normalize( $string, UNORM_NFD );
|
|
|
|
|
elseif( preg_match( '/[\x80-\xff]/', $string ) )
|
2004-08-29 10:30:23 +00:00
|
|
|
return UtfNormal::NFD( $string );
|
|
|
|
|
else
|
|
|
|
|
return $string;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-04 09:35:01 +00:00
|
|
|
/**
|
|
|
|
|
* Convert a UTF-8 string to normal form KC, compatibility composition.
|
|
|
|
|
* This may cause irreversible information loss, use judiciously.
|
|
|
|
|
* Fast return for pure ASCII strings.
|
|
|
|
|
*
|
|
|
|
|
* @param string $string a valid UTF-8 string. Input is not validated.
|
|
|
|
|
* @return string a UTF-8 string in normal form KC
|
|
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
function toNFKC( $string ) {
|
2004-10-07 05:59:10 +00:00
|
|
|
if( NORMALIZE_ICU )
|
|
|
|
|
return utf8_normalize( $string, UNORM_NFKC );
|
|
|
|
|
elseif( preg_match( '/[\x80-\xff]/', $string ) )
|
2004-08-29 10:30:23 +00:00
|
|
|
return UtfNormal::NFKC( $string );
|
|
|
|
|
else
|
|
|
|
|
return $string;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-04 09:35:01 +00:00
|
|
|
/**
|
|
|
|
|
* Convert a UTF-8 string to normal form KD, compatibility decomposition.
|
|
|
|
|
* This may cause irreversible information loss, use judiciously.
|
|
|
|
|
* Fast return for pure ASCII strings.
|
|
|
|
|
*
|
|
|
|
|
* @param string $string a valid UTF-8 string. Input is not validated.
|
|
|
|
|
* @return string a UTF-8 string in normal form KD
|
|
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
function toNFKD( $string ) {
|
2004-10-07 05:59:10 +00:00
|
|
|
if( NORMALIZE_ICU )
|
|
|
|
|
return utf8_normalize( $string, UNORM_NFKD );
|
|
|
|
|
elseif( preg_match( '/[\x80-\xff]/', $string ) )
|
2004-08-29 10:30:23 +00:00
|
|
|
return UtfNormal::NFKD( $string );
|
|
|
|
|
else
|
|
|
|
|
return $string;
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-09 08:08:26 +00:00
|
|
|
/**
|
|
|
|
|
* Load the basic composition data if necessary
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
|
|
|
|
function loadData() {
|
|
|
|
|
global $utfCombiningClass, $utfCanonicalComp, $utfCanonicalDecomp;
|
|
|
|
|
if( !isset( $utfCombiningClass ) ) {
|
|
|
|
|
require_once( 'UtfNormalData.inc' );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-04 09:35:01 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true if the string is _definitely_ in NFC.
|
|
|
|
|
* Returns false if not or uncertain.
|
|
|
|
|
* @param string $string a valid UTF-8 string. Input is not validated.
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
function quickIsNFC( $string ) {
|
|
|
|
|
# ASCII is always valid NFC!
|
2004-09-04 09:35:01 +00:00
|
|
|
# If it's pure ASCII, let it through.
|
|
|
|
|
if( !preg_match( '/[\x80-\xff]/', $string ) ) return true;
|
2004-08-29 10:30:23 +00:00
|
|
|
|
2004-10-09 08:08:26 +00:00
|
|
|
UtfNormal::loadData();
|
2004-08-29 10:30:23 +00:00
|
|
|
global $utfCheckNFC, $utfCombiningClass;
|
|
|
|
|
$len = strlen( $string );
|
|
|
|
|
for( $i = 0; $i < $len; $i++ ) {
|
|
|
|
|
$c = $string{$i};
|
|
|
|
|
$n = ord( $c );
|
|
|
|
|
if( $n < 0x80 ) {
|
|
|
|
|
continue;
|
|
|
|
|
} elseif( $n >= 0xf0 ) {
|
|
|
|
|
$c = substr( $string, $i, 4 );
|
|
|
|
|
$i += 3;
|
|
|
|
|
} elseif( $n >= 0xe0 ) {
|
|
|
|
|
$c = substr( $string, $i, 3 );
|
|
|
|
|
$i += 2;
|
|
|
|
|
} elseif( $n >= 0xc0 ) {
|
|
|
|
|
$c = substr( $string, $i, 2 );
|
|
|
|
|
$i++;
|
|
|
|
|
}
|
|
|
|
|
if( isset( $utfCheckNFC[$c] ) ) {
|
|
|
|
|
# If it's NO or MAYBE, bail and do the slow check.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if( isset( $utfCombiningClass[$c] ) ) {
|
|
|
|
|
# Combining character? We might have to do sorting, at least.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2004-09-03 05:39:30 +00:00
|
|
|
|
2004-09-04 09:35:01 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true if the string is _definitely_ in NFC.
|
|
|
|
|
* Returns false if not or uncertain.
|
|
|
|
|
* @param string $string a UTF-8 string, altered on output to be valid UTF-8 safe for XML.
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2004-09-03 05:39:30 +00:00
|
|
|
function quickIsNFCVerify( &$string ) {
|
|
|
|
|
# ASCII is always valid NFC!
|
|
|
|
|
if( !preg_match( '/[\x80-\xff]/', $string ) ) return true;
|
|
|
|
|
|
2004-10-09 08:08:26 +00:00
|
|
|
UtfNormal::loadData();
|
2004-09-03 05:39:30 +00:00
|
|
|
global $utfCheckNFC, $utfCombiningClass;
|
|
|
|
|
$len = strlen( $string );
|
|
|
|
|
$out = '';
|
|
|
|
|
$state = UTF8_HEAD;
|
|
|
|
|
$looksNormal = true;
|
|
|
|
|
|
|
|
|
|
$rep = false;
|
|
|
|
|
$head = 0;
|
|
|
|
|
for( $i = 0; $i < $len; $i++ ) {
|
|
|
|
|
$c = $string{$i};
|
|
|
|
|
$n = ord( $c );
|
|
|
|
|
if( $state == UTF8_TAIL ) {
|
|
|
|
|
if( $n >= 0x80 && $n < 0xc0 ) {
|
|
|
|
|
$sequence .= $c;
|
|
|
|
|
if( --$remaining == 0 ) {
|
2004-10-30 06:02:30 +00:00
|
|
|
if( $head < 0xc2 || $head == 0xed || $head == 0xe0 || $head > 0xee ) {
|
|
|
|
|
if( ( $sequence >= UTF8_SURROGATE_FIRST
|
|
|
|
|
&& $sequence <= UTF8_SURROGATE_LAST)
|
|
|
|
|
|| ($head == 0xc0 && $sequence <= UTF8_OVERLONG_A)
|
|
|
|
|
|| ($head == 0xc1 && $sequence <= UTF8_OVERLONG_A)
|
|
|
|
|
|| ($head == 0xe0 && $sequence <= UTF8_OVERLONG_B)
|
|
|
|
|
|| ($head == 0xef &&
|
|
|
|
|
($sequence >= UTF8_FDD0 && $sequence <= UTF8_FDEF)
|
|
|
|
|
|| ($sequence == UTF8_FFFE)
|
|
|
|
|
|| ($sequence == UTF8_FFFF) )
|
|
|
|
|
|| ($head == 0xf0 && $sequence <= UTF8_OVERLONG_C)
|
|
|
|
|
|| ($sequence > UTF8_MAX) ) {
|
|
|
|
|
$out .= UTF8_REPLACEMENT;
|
|
|
|
|
$state = UTF8_HEAD;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2004-09-03 05:39:30 +00:00
|
|
|
}
|
|
|
|
|
if( isset( $utfCheckNFC[$sequence] ) ||
|
|
|
|
|
isset( $utfCombiningClass[$sequence] ) ) {
|
|
|
|
|
# If it's NO or MAYBE, we'll have to do the slow check.
|
|
|
|
|
$looksNormal = false;
|
|
|
|
|
}
|
|
|
|
|
$out .= $sequence;
|
|
|
|
|
$state = UTF8_HEAD;
|
|
|
|
|
$head = 0;
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
# Not a valid tail byte! DIscard the char we've been building.
|
|
|
|
|
#printf ("Invalid '%x' in tail with %d remaining bytes\n", $n, $remaining );
|
|
|
|
|
$state = UTF8_HEAD;
|
|
|
|
|
$out .= UTF8_REPLACEMENT;
|
|
|
|
|
}
|
2004-10-30 06:02:30 +00:00
|
|
|
if( $n < 0x20 ) {
|
|
|
|
|
if( $n < 0x09 ) {
|
|
|
|
|
$out .= UTF8_REPLACEMENT;
|
|
|
|
|
} elseif( $n == 0x0a ) {
|
|
|
|
|
$out .= $c;
|
|
|
|
|
} elseif( $n < 0x0d ) {
|
|
|
|
|
$out .= UTF8_REPLACEMENT;
|
|
|
|
|
} elseif( $n == 0x0d ) {
|
|
|
|
|
# Strip \r silently
|
|
|
|
|
} else {
|
|
|
|
|
$out .= UTF8_REPLACEMENT;
|
|
|
|
|
}
|
2004-09-03 05:39:30 +00:00
|
|
|
} elseif( $n < 0x80 ) {
|
2004-10-30 06:02:30 +00:00
|
|
|
# Friendly ASCII chars.
|
2004-09-03 05:39:30 +00:00
|
|
|
$out .= $c;
|
|
|
|
|
} elseif( $n < 0xc0 ) {
|
|
|
|
|
# illegal tail bytes or head byte of overlong sequence
|
|
|
|
|
if( $head == 0 ) $out .= UTF8_REPLACEMENT;
|
|
|
|
|
} elseif( $n < 0xe0 ) {
|
|
|
|
|
$state = UTF8_TAIL;
|
|
|
|
|
$remaining = 1;
|
|
|
|
|
$sequence = $c;
|
|
|
|
|
$head = $n;
|
|
|
|
|
} elseif( $n < 0xf0 ) {
|
|
|
|
|
$state = UTF8_TAIL;
|
|
|
|
|
$remaining = 2;
|
|
|
|
|
$sequence = $c;
|
|
|
|
|
$head = $n;
|
|
|
|
|
} elseif( $n < 0xf8 ) {
|
|
|
|
|
$state = UTF8_TAIL;
|
|
|
|
|
$remaining = 3;
|
|
|
|
|
$sequence = $c;
|
|
|
|
|
$head = $n;
|
|
|
|
|
} elseif( $n < 0xfc ) {
|
|
|
|
|
$state = UTF8_TAIL;
|
|
|
|
|
$remaining = 4;
|
|
|
|
|
$sequence = $c;
|
|
|
|
|
$head = $n;
|
|
|
|
|
} elseif( $n < 0xfe ) {
|
|
|
|
|
$state = UTF8_TAIL;
|
|
|
|
|
$remaining = 5;
|
|
|
|
|
$sequence = $c;
|
|
|
|
|
$head = $n;
|
|
|
|
|
} else {
|
|
|
|
|
$out .= UTF8_REPLACEMENT;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if( $state == UTF8_TAIL ) {
|
|
|
|
|
$out .= UTF8_REPLACEMENT;
|
|
|
|
|
}
|
|
|
|
|
$string = $out;
|
|
|
|
|
return $looksNormal;
|
|
|
|
|
}
|
2004-08-29 10:30:23 +00:00
|
|
|
|
|
|
|
|
# These take a string and run the normalization on them, without
|
|
|
|
|
# checking for validity or any optimization etc. Input must be
|
|
|
|
|
# VALID UTF-8!
|
2004-09-27 02:59:24 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $string
|
|
|
|
|
* @return string
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
function NFC( $string ) {
|
2004-10-30 06:02:30 +00:00
|
|
|
return UtfNormal::fastCompose( UtfNormal::NFD( $string ) );
|
2004-08-29 10:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-27 02:59:24 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $string
|
|
|
|
|
* @return string
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
function NFD( $string ) {
|
2004-10-09 08:08:26 +00:00
|
|
|
UtfNormal::loadData();
|
2004-08-29 10:30:23 +00:00
|
|
|
global $utfCanonicalDecomp;
|
|
|
|
|
return UtfNormal::fastCombiningSort(
|
|
|
|
|
UtfNormal::fastDecompose( $string, $utfCanonicalDecomp ) );
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-27 02:59:24 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $string
|
|
|
|
|
* @return string
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
function NFKC( $string ) {
|
|
|
|
|
return UtfNormal::fastCompose( UtfNormal::NFKD( $string ) );
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-27 02:59:24 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $string
|
|
|
|
|
* @return string
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
function NFKD( $string ) {
|
|
|
|
|
global $utfCompatibilityDecomp;
|
2004-09-02 07:39:06 +00:00
|
|
|
if( !isset( $utfCompatibilityDecomp ) ) {
|
|
|
|
|
require_once( 'UtfNormalDataK.inc' );
|
|
|
|
|
}
|
2004-08-29 10:30:23 +00:00
|
|
|
return UtfNormal::fastCombiningSort(
|
|
|
|
|
UtfNormal::fastDecompose( $string, $utfCompatibilityDecomp ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-09-04 09:35:01 +00:00
|
|
|
/**
|
|
|
|
|
* Perform decomposition of a UTF-8 string into either D or KD form
|
|
|
|
|
* (depending on which decomposition map is passed to us).
|
|
|
|
|
* Input is assumed to be *valid* UTF-8. Invalid code will break.
|
2004-09-27 02:59:24 +00:00
|
|
|
* @access private
|
2004-09-04 09:35:01 +00:00
|
|
|
* @param string &$string Valid UTF-8 string
|
|
|
|
|
* @param array &$map hash of expanded decomposition map
|
|
|
|
|
* @return string a UTF-8 string decomposed, not yet normalized (needs sorting)
|
|
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
function fastDecompose( &$string, &$map ) {
|
2004-10-09 08:08:26 +00:00
|
|
|
UtfNormal::loadData();
|
2004-08-29 10:30:23 +00:00
|
|
|
$len = strlen( $string );
|
|
|
|
|
$out = '';
|
|
|
|
|
for( $i = 0; $i < $len; $i++ ) {
|
|
|
|
|
$c = $string{$i};
|
|
|
|
|
$n = ord( $c );
|
|
|
|
|
if( $n < 0x80 ) {
|
|
|
|
|
# ASCII chars never decompose
|
|
|
|
|
# THEY ARE IMMORTAL
|
|
|
|
|
$out .= $c;
|
|
|
|
|
continue;
|
|
|
|
|
} elseif( $n >= 0xf0 ) {
|
|
|
|
|
$c = substr( $string, $i, 4 );
|
|
|
|
|
$i += 3;
|
|
|
|
|
} elseif( $n >= 0xe0 ) {
|
|
|
|
|
$c = substr( $string, $i, 3 );
|
|
|
|
|
$i += 2;
|
|
|
|
|
} elseif( $n >= 0xc0 ) {
|
|
|
|
|
$c = substr( $string, $i, 2 );
|
|
|
|
|
$i++;
|
|
|
|
|
}
|
|
|
|
|
if( isset( $map[$c] ) ) {
|
|
|
|
|
$out .= $map[$c];
|
|
|
|
|
} else {
|
|
|
|
|
if( $c >= UTF8_HANGUL_FIRST && $c <= UTF8_HANGUL_LAST ) {
|
|
|
|
|
$out .= UtfNormal::decomposeHangul( $c );
|
|
|
|
|
} else {
|
|
|
|
|
$out .= $c;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $out;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-04 09:35:01 +00:00
|
|
|
/**
|
|
|
|
|
* Decompose a Hangul syllable character into its constituent jamo.
|
2004-09-27 02:59:24 +00:00
|
|
|
* @access private
|
2004-09-04 09:35:01 +00:00
|
|
|
* @param int $c Unicode code point of the character
|
|
|
|
|
* @return string a UTF-8 string containing a sequence of jamo
|
|
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
function decomposeHangul( $c ) {
|
|
|
|
|
$codepoint = utf8ToCodepoint( $c );
|
|
|
|
|
$index = $codepoint - UNICODE_HANGUL_FIRST;
|
|
|
|
|
$l = IntVal( $index / UNICODE_HANGUL_NCOUNT );
|
|
|
|
|
$v = IntVal( ($index % UNICODE_HANGUL_NCOUNT) / UNICODE_HANGUL_TCOUNT);
|
|
|
|
|
$t = $index % UNICODE_HANGUL_TCOUNT;
|
|
|
|
|
$out = codepointToUtf8( $l + UNICODE_HANGUL_LBASE );
|
|
|
|
|
$out .= codepointToUtf8( $v + UNICODE_HANGUL_VBASE );
|
|
|
|
|
if( $t ) $out .= codepointToUtf8( $t + UNICODE_HANGUL_TBASE );
|
|
|
|
|
return $out;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-04 09:35:01 +00:00
|
|
|
/**
|
|
|
|
|
* Sorts combining characters into canonical order. This is the
|
|
|
|
|
* final step in creating decomposed normal forms D and KD.
|
2004-09-27 02:59:24 +00:00
|
|
|
* @access private
|
2004-09-04 09:35:01 +00:00
|
|
|
* @param string $string a valid, decomposed UTF-8 string. Input is not validated.
|
|
|
|
|
* @return string a UTF-8 string with combining characters sorted in canonical order
|
|
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
function fastCombiningSort( $string ) {
|
2004-10-09 08:08:26 +00:00
|
|
|
UtfNormal::loadData();
|
2004-08-29 10:30:23 +00:00
|
|
|
global $utfCombiningClass;
|
|
|
|
|
$replacedCount = 1;
|
|
|
|
|
while( $replacedCount > 0 ) {
|
|
|
|
|
$replacedCount = 0;
|
|
|
|
|
$len = strlen( $string );
|
|
|
|
|
$out = '';
|
|
|
|
|
$lastClass = -1;
|
|
|
|
|
$lastChar = '';
|
|
|
|
|
for( $i = 0; $i < $len; $i++ ) {
|
|
|
|
|
$c = $string{$i};
|
|
|
|
|
$n = ord( $c );
|
2004-10-30 06:02:30 +00:00
|
|
|
if( $n < 0x80 ) {
|
|
|
|
|
# No combining characters in ASCII.
|
|
|
|
|
$out .= $lastChar;
|
|
|
|
|
$lastChar = $c;
|
|
|
|
|
$lastClass = 0;
|
|
|
|
|
continue;
|
|
|
|
|
} elseif( $n >= 0xf0 ) {
|
2004-08-29 10:30:23 +00:00
|
|
|
$c = substr( $string, $i, 4 );
|
|
|
|
|
$i += 3;
|
|
|
|
|
} elseif( $n >= 0xe0 ) {
|
|
|
|
|
$c = substr( $string, $i, 3 );
|
|
|
|
|
$i += 2;
|
|
|
|
|
} elseif( $n >= 0xc0 ) {
|
|
|
|
|
$c = substr( $string, $i, 2 );
|
|
|
|
|
$i++;
|
|
|
|
|
}
|
2004-10-30 06:02:30 +00:00
|
|
|
$class = 0;
|
2004-08-29 10:30:23 +00:00
|
|
|
if( $lastClass == -1 ) {
|
|
|
|
|
# First one
|
|
|
|
|
$lastChar = $c;
|
2004-10-30 06:02:30 +00:00
|
|
|
$class = isset( $utfCombiningClass[$c] ) ? $utfCombiningClass[$c] : 0;
|
2004-08-29 10:30:23 +00:00
|
|
|
$lastClass = $class;
|
2004-10-30 06:02:30 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if( isset( $utfCombiningClass[$c] ) ) {
|
|
|
|
|
$class = $utfCombiningClass[$c];
|
|
|
|
|
if( $lastClass > $class ) {
|
|
|
|
|
# Swap -- put this one on the stack
|
|
|
|
|
$out .= $c;
|
|
|
|
|
$replacedCount++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2004-08-29 10:30:23 +00:00
|
|
|
}
|
2004-10-30 06:02:30 +00:00
|
|
|
$out .= $lastChar;
|
|
|
|
|
$lastChar = $c;
|
|
|
|
|
$lastClass = $class;
|
2004-08-29 10:30:23 +00:00
|
|
|
}
|
|
|
|
|
$out .= $lastChar;
|
|
|
|
|
$string = $out;
|
|
|
|
|
}
|
|
|
|
|
return $string;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-04 09:35:01 +00:00
|
|
|
/**
|
|
|
|
|
* Produces canonically composed sequences, i.e. normal form C or KC.
|
|
|
|
|
*
|
2004-09-27 02:59:24 +00:00
|
|
|
* @access private
|
2004-09-04 09:35:01 +00:00
|
|
|
* @param string $string a valid UTF-8 string in sorted normal form D or KD. Input is not validated.
|
|
|
|
|
* @return string a UTF-8 string with canonical precomposed characters used where possible
|
|
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
function fastCompose( $string ) {
|
2004-10-09 08:08:26 +00:00
|
|
|
UtfNormal::loadData();
|
2004-08-29 10:30:23 +00:00
|
|
|
global $utfCanonicalComp, $utfCombiningClass;
|
|
|
|
|
$len = strlen( $string );
|
|
|
|
|
$out = '';
|
|
|
|
|
$lastClass = -1;
|
|
|
|
|
$startChar = '';
|
|
|
|
|
$combining = '';
|
2004-10-30 06:02:30 +00:00
|
|
|
$x1 = ord(substr(UTF8_HANGUL_VBASE,0,1));
|
|
|
|
|
$x2 = ord(substr(UTF8_HANGUL_TEND,0,1));
|
2004-08-29 10:30:23 +00:00
|
|
|
for( $i = 0; $i < $len; $i++ ) {
|
|
|
|
|
$c = $string{$i};
|
|
|
|
|
$n = ord( $c );
|
2004-10-30 06:02:30 +00:00
|
|
|
if( $n < 0x80 ) {
|
|
|
|
|
# No combining characters here...
|
|
|
|
|
$out .= $startChar;
|
|
|
|
|
$out .= $combining;
|
|
|
|
|
$startChar = $c;
|
|
|
|
|
$combining = '';
|
|
|
|
|
$lastClass = 0;
|
|
|
|
|
continue;
|
|
|
|
|
} elseif( $n >= 0xf0 ) {
|
2004-08-29 10:30:23 +00:00
|
|
|
$c = substr( $string, $i, 4 );
|
|
|
|
|
$i += 3;
|
|
|
|
|
} elseif( $n >= 0xe0 ) {
|
|
|
|
|
$c = substr( $string, $i, 3 );
|
|
|
|
|
$i += 2;
|
|
|
|
|
} elseif( $n >= 0xc0 ) {
|
|
|
|
|
$c = substr( $string, $i, 2 );
|
|
|
|
|
$i++;
|
|
|
|
|
}
|
|
|
|
|
$pair = $startChar . $c;
|
2004-10-30 06:02:30 +00:00
|
|
|
if( $n > 0x80 ) {
|
|
|
|
|
if( isset( $utfCombiningClass[$c] ) ) {
|
|
|
|
|
# A combining char; see what we can do with it
|
|
|
|
|
$class = $utfCombiningClass[$c];
|
|
|
|
|
if( !empty( $startChar ) &&
|
|
|
|
|
$lastClass < $class &&
|
|
|
|
|
$class > 0 &&
|
|
|
|
|
isset( $utfCanonicalComp[$pair] ) ) {
|
|
|
|
|
$startChar = $utfCanonicalComp[$pair];
|
|
|
|
|
$class = 0;
|
|
|
|
|
} else {
|
|
|
|
|
$combining .= $c;
|
|
|
|
|
}
|
|
|
|
|
$lastClass = $class;
|
|
|
|
|
continue;
|
2004-08-29 10:30:23 +00:00
|
|
|
}
|
2004-10-30 06:02:30 +00:00
|
|
|
}
|
|
|
|
|
# New start char
|
|
|
|
|
if( $lastClass == 0 ) {
|
|
|
|
|
if( isset( $utfCanonicalComp[$pair] ) ) {
|
2004-08-29 10:30:23 +00:00
|
|
|
$startChar = $utfCanonicalComp[$pair];
|
2004-10-30 06:02:30 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if( $n >= $x1 && $n <= $x2 ) {
|
|
|
|
|
# WARNING: Hangul code is painfully slow.
|
|
|
|
|
if( $c >= UTF8_HANGUL_VBASE &&
|
|
|
|
|
$c <= UTF8_HANGUL_VEND &&
|
|
|
|
|
$startChar >= UTF8_HANGUL_LBASE &&
|
|
|
|
|
$startChar <= UTF8_HANGUL_LEND ) {
|
|
|
|
|
#
|
|
|
|
|
$lIndex = utf8ToCodepoint( $startChar ) - UNICODE_HANGUL_LBASE;
|
|
|
|
|
$vIndex = utf8ToCodepoint( $c ) - UNICODE_HANGUL_VBASE;
|
|
|
|
|
$hangulPoint = UNICODE_HANGUL_FIRST +
|
|
|
|
|
UNICODE_HANGUL_TCOUNT *
|
|
|
|
|
(UNICODE_HANGUL_VCOUNT * $lIndex + $vIndex);
|
|
|
|
|
$startChar = codepointToUtf8( $hangulPoint );
|
|
|
|
|
continue;
|
|
|
|
|
} elseif( $c >= UTF8_HANGUL_TBASE &&
|
|
|
|
|
$c <= UTF8_HANGUL_TEND &&
|
|
|
|
|
$startChar >= UTF8_HANGUL_FIRST &&
|
|
|
|
|
$startChar <= UTF8_HANGUL_LAST ) {
|
|
|
|
|
$tIndex = utf8ToCodepoint( $c ) - UNICODE_HANGUL_TBASE;
|
|
|
|
|
$hangulPoint = utf8ToCodepoint( $startChar ) + $tIndex;
|
|
|
|
|
$startChar = codepointToUtf8( $hangulPoint );
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2004-08-29 10:30:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
2004-10-30 06:02:30 +00:00
|
|
|
$out .= $startChar;
|
|
|
|
|
$out .= $combining;
|
|
|
|
|
$startChar = $c;
|
|
|
|
|
$combining = '';
|
|
|
|
|
$lastClass = 0;
|
2004-08-29 10:30:23 +00:00
|
|
|
}
|
|
|
|
|
$out .= $startChar . $combining;
|
|
|
|
|
return $out;
|
|
|
|
|
}
|
2004-10-30 06:02:30 +00:00
|
|
|
|
2004-09-04 09:35:01 +00:00
|
|
|
/**
|
|
|
|
|
* This is just used for the benchmark, comparing how long it takes to
|
|
|
|
|
* interate through a string without really doing anything of substance.
|
|
|
|
|
* @param string $string
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
function placebo( $string ) {
|
|
|
|
|
$len = strlen( $string );
|
|
|
|
|
$out = '';
|
|
|
|
|
for( $i = 0; $i < $len; $i++ ) {
|
|
|
|
|
$out .= $string{$i};
|
|
|
|
|
}
|
|
|
|
|
return $out;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-03 23:00:01 +00:00
|
|
|
?>
|