2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
20 # Unicode normalization routines for working with UTF-8 strings.
21 # Currently assumes that input strings are valid UTF-8!
23 # Not as fast as I'd like, but should be usable for most purposes.
24 # UtfNormal::toNFC() will bail early if given ASCII text or text
25 # it can quickly deterimine is already normalized.
27 # All functions can be called static.
29 # See description of forms at http://www.unicode.org/reports/tr15/
31 require_once 'UtfNormalUtil.php';
32 require_once 'UtfNormalData.inc';
34 # Load compatibility decompositions on demand if they are needed.
35 global $utfCompatibilityDecomp;
36 $utfCompatibilityDecomp = NULL;
38 define( 'UNICODE_HANGUL_FIRST', 0xac00 );
39 define( 'UNICODE_HANGUL_LAST', 0xd7a3 );
41 define( 'UNICODE_HANGUL_LBASE', 0x1100 );
42 define( 'UNICODE_HANGUL_VBASE', 0x1161 );
43 define( 'UNICODE_HANGUL_TBASE', 0x11a7 );
45 define( 'UNICODE_HANGUL_LCOUNT', 19 );
46 define( 'UNICODE_HANGUL_VCOUNT', 21 );
47 define( 'UNICODE_HANGUL_TCOUNT', 28 );
48 define( 'UNICODE_HANGUL_NCOUNT', UNICODE_HANGUL_VCOUNT
* UNICODE_HANGUL_TCOUNT
);
50 define( 'UNICODE_HANGUL_LEND', UNICODE_HANGUL_LBASE + UNICODE_HANGUL_LCOUNT
- 1 );
51 define( 'UNICODE_HANGUL_VEND', UNICODE_HANGUL_VBASE + UNICODE_HANGUL_VCOUNT
- 1 );
52 define( 'UNICODE_HANGUL_TEND', UNICODE_HANGUL_TBASE + UNICODE_HANGUL_TCOUNT
- 1 );
54 define( 'UTF8_HANGUL_FIRST', codepointToUtf8( UNICODE_HANGUL_FIRST
) );
55 define( 'UTF8_HANGUL_LAST', codepointToUtf8( UNICODE_HANGUL_LAST
) );
57 define( 'UTF8_HANGUL_LBASE', codepointToUtf8( UNICODE_HANGUL_LBASE
) );
58 define( 'UTF8_HANGUL_VBASE', codepointToUtf8( UNICODE_HANGUL_VBASE
) );
59 define( 'UTF8_HANGUL_TBASE', codepointToUtf8( UNICODE_HANGUL_TBASE
) );
61 define( 'UTF8_HANGUL_LEND', codepointToUtf8( UNICODE_HANGUL_LEND
) );
62 define( 'UTF8_HANGUL_VEND', codepointToUtf8( UNICODE_HANGUL_VEND
) );
63 define( 'UTF8_HANGUL_TEND', codepointToUtf8( UNICODE_HANGUL_TEND
) );
66 # These functions try to skip the conversion if it won't be necessary.
67 # An all ASCII string for instance doesn't need conversion.
68 function toNFC( $string ) {
69 if( UtfNormal
::quickIsNFC( $string ) )
72 return UtfNormal
::NFC( $string );
75 function toNFD( $string ) {
76 if( preg_match( '/[\x80-\xff]/', $string ) )
77 return UtfNormal
::NFD( $string );
82 function toNFKC( $string ) {
83 if( preg_match( '/[\x80-\xff]/', $string ) )
84 return UtfNormal
::NFKC( $string );
89 function toNFKD( $string ) {
90 if( preg_match( '/[\x80-\xff]/', $string ) )
91 return UtfNormal
::NFKD( $string );
97 function quickIsNFC( $string ) {
98 # ASCII is always valid NFC!
99 if( !preg_match( '/[\x80-\xff]/', $string ) ) return true;
101 global $utfCheckNFC, $utfCombiningClass;
102 $len = strlen( $string );
103 for( $i = 0; $i < $len; $i++
) {
108 } elseif( $n >= 0xf0 ) {
109 $c = substr( $string, $i, 4 );
111 } elseif( $n >= 0xe0 ) {
112 $c = substr( $string, $i, 3 );
114 } elseif( $n >= 0xc0 ) {
115 $c = substr( $string, $i, 2 );
118 if( isset( $utfCheckNFC[$c] ) ) {
119 # If it's NO or MAYBE, bail and do the slow check.
122 if( isset( $utfCombiningClass[$c] ) ) {
123 # Combining character? We might have to do sorting, at least.
130 # These take a string and run the normalization on them, without
131 # checking for validity or any optimization etc. Input must be
133 function NFC( $string ) {
134 return $out = UtfNormal
::fastCompose( UtfNormal
::NFD( $string ) );
137 function NFD( $string ) {
138 global $utfCanonicalDecomp;
139 return UtfNormal
::fastCombiningSort(
140 UtfNormal
::fastDecompose( $string, $utfCanonicalDecomp ) );
143 function NFKC( $string ) {
144 return UtfNormal
::fastCompose( UtfNormal
::NFKD( $string ) );
147 function NFKD( $string ) {
148 global $utfCompatibilityDecomp;
149 if( !isset( $utfCompatibilityDecomp ) ) {
150 require_once( 'UtfNormalDataK.inc' );
152 return UtfNormal
::fastCombiningSort(
153 UtfNormal
::fastDecompose( $string, $utfCompatibilityDecomp ) );
158 # Perform decomposition of a UTF-8 string into either D or KD form
159 # (depending on which decomposition map is passed to us).
160 # Input is assumed to be *valid* UTF-8. Invalid code will break.
161 function fastDecompose( &$string, &$map ) {
162 $len = strlen( $string );
164 for( $i = 0; $i < $len; $i++
) {
168 # ASCII chars never decompose
172 } elseif( $n >= 0xf0 ) {
173 $c = substr( $string, $i, 4 );
175 } elseif( $n >= 0xe0 ) {
176 $c = substr( $string, $i, 3 );
178 } elseif( $n >= 0xc0 ) {
179 $c = substr( $string, $i, 2 );
182 if( isset( $map[$c] ) ) {
185 if( $c >= UTF8_HANGUL_FIRST
&& $c <= UTF8_HANGUL_LAST
) {
186 $out .= UtfNormal
::decomposeHangul( $c );
195 function decomposeHangul( $c ) {
196 $codepoint = utf8ToCodepoint( $c );
197 $index = $codepoint - UNICODE_HANGUL_FIRST
;
198 $l = IntVal( $index / UNICODE_HANGUL_NCOUNT
);
199 $v = IntVal( ($index % UNICODE_HANGUL_NCOUNT
) / UNICODE_HANGUL_TCOUNT
);
200 $t = $index % UNICODE_HANGUL_TCOUNT
;
201 $out = codepointToUtf8( $l + UNICODE_HANGUL_LBASE
);
202 $out .= codepointToUtf8( $v + UNICODE_HANGUL_VBASE
);
203 if( $t ) $out .= codepointToUtf8( $t + UNICODE_HANGUL_TBASE
);
207 # Sorts combining characters into canonical order. This is the
208 # final step in creating decomposed normal forms D and KD.
209 function fastCombiningSort( $string ) {
210 global $utfCombiningClass;
212 while( $replacedCount > 0 ) {
214 $len = strlen( $string );
218 for( $i = 0; $i < $len; $i++
) {
222 $c = substr( $string, $i, 4 );
224 } elseif( $n >= 0xe0 ) {
225 $c = substr( $string, $i, 3 );
227 } elseif( $n >= 0xc0 ) {
228 $c = substr( $string, $i, 2 );
231 $class = isset( $utfCombiningClass[$c] ) ?
$utfCombiningClass[$c] : 0;
232 if( $lastClass == -1 ) {
236 } elseif( $lastClass > $class && $class > 0 ) {
237 # Swap -- put this one on the stack
252 # Produces canonically composed sequences, i.e. normal form C or KC.
253 # Input must be valid UTF-8 in sorted normal form D or KD.
254 function fastCompose( $string ) {
255 global $utfCanonicalComp, $utfCombiningClass;
256 $len = strlen( $string );
261 for( $i = 0; $i < $len; $i++
) {
265 $c = substr( $string, $i, 4 );
267 } elseif( $n >= 0xe0 ) {
268 $c = substr( $string, $i, 3 );
270 } elseif( $n >= 0xc0 ) {
271 $c = substr( $string, $i, 2 );
274 $class = isset( $utfCombiningClass[$c] ) ?
$utfCombiningClass[$c] : 0;
275 $pair = $startChar . $c;
276 if( empty( $utfCombiningClass[$c] ) ) {
278 if( $lastClass == 0 && isset( $utfCanonicalComp[$pair] ) ) {
279 $startChar = $utfCanonicalComp[$pair];
280 } elseif( $lastClass == 0 &&
281 $c >= UTF8_HANGUL_VBASE
&&
282 $c <= UTF8_HANGUL_VEND
&&
283 $startChar >= UTF8_HANGUL_LBASE
&&
284 $startChar <= UTF8_HANGUL_LEND
) {
285 $lIndex = utf8ToCodepoint( $startChar ) - UNICODE_HANGUL_LBASE
;
286 $vIndex = utf8ToCodepoint( $c ) - UNICODE_HANGUL_VBASE
;
287 $hangulPoint = UNICODE_HANGUL_FIRST +
288 UNICODE_HANGUL_TCOUNT
*
289 (UNICODE_HANGUL_VCOUNT
* $lIndex +
$vIndex);
290 $startChar = codepointToUtf8( $hangulPoint );
291 } elseif( $lastClass == 0 &&
292 $c >= UTF8_HANGUL_TBASE
&&
293 $c <= UTF8_HANGUL_TEND
&&
294 $startChar >= UTF8_HANGUL_FIRST
&&
295 $startChar <= UTF8_HANGUL_LAST
) {
296 $tIndex = utf8ToCodepoint( $c ) - UNICODE_HANGUL_TBASE
;
297 $hangulPoint = utf8ToCodepoint( $startChar ) +
$tIndex;
298 $startChar = codepointToUtf8( $hangulPoint );
306 # A combining char; see what we can do with it
307 if( !empty( $startChar ) &&
308 $lastClass < $class &&
310 isset( $utfCanonicalComp[$pair] ) ) {
311 $startChar = $utfCanonicalComp[$pair];
319 $out .= $startChar . $combining;
323 # This is just used for the benchmark, comparing how long it takes to
324 # interate through a string without really doing anything of substance.
325 function placebo( $string ) {
326 $len = strlen( $string );
328 for( $i = 0; $i < $len; $i++
) {