Fix composition bug: completed hangul syllable should not be merged with another...
[lhc/web/wiklou.git] / includes / normal / UtfNormal.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
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.
9 #
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.
14 #
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
19
20 /**
21 * Unicode normalization routines for working with UTF-8 strings.
22 * Currently assumes that input strings are valid UTF-8!
23 *
24 * Not as fast as I'd like, but should be usable for most purposes.
25 * UtfNormal::toNFC() will bail early if given ASCII text or text
26 * it can quickly deterimine is already normalized.
27 *
28 * All functions can be called static.
29 *
30 * See description of forms at http://www.unicode.org/reports/tr15/
31 *
32 * @package UtfNormal
33 */
34
35 /** */
36 require_once 'UtfNormalUtil.php';
37
38 global $utfCombiningClass, $utfCanonicalComp, $utfCanonicalDecomp;
39 $utfCombiningClass = NULL;
40 $utfCanonicalComp = NULL;
41 $utfCanonicalDecomp = NULL;
42
43 # Load compatibility decompositions on demand if they are needed.
44 global $utfCompatibilityDecomp;
45 $utfCompatibilityDecomp = NULL;
46
47 define( 'UNICODE_HANGUL_FIRST', 0xac00 );
48 define( 'UNICODE_HANGUL_LAST', 0xd7a3 );
49
50 define( 'UNICODE_HANGUL_LBASE', 0x1100 );
51 define( 'UNICODE_HANGUL_VBASE', 0x1161 );
52 define( 'UNICODE_HANGUL_TBASE', 0x11a7 );
53
54 define( 'UNICODE_HANGUL_LCOUNT', 19 );
55 define( 'UNICODE_HANGUL_VCOUNT', 21 );
56 define( 'UNICODE_HANGUL_TCOUNT', 28 );
57 define( 'UNICODE_HANGUL_NCOUNT', UNICODE_HANGUL_VCOUNT * UNICODE_HANGUL_TCOUNT );
58
59 define( 'UNICODE_HANGUL_LEND', UNICODE_HANGUL_LBASE + UNICODE_HANGUL_LCOUNT - 1 );
60 define( 'UNICODE_HANGUL_VEND', UNICODE_HANGUL_VBASE + UNICODE_HANGUL_VCOUNT - 1 );
61 define( 'UNICODE_HANGUL_TEND', UNICODE_HANGUL_TBASE + UNICODE_HANGUL_TCOUNT - 1 );
62
63 define( 'UNICODE_SURROGATE_FIRST', 0xd800 );
64 define( 'UNICODE_SURROGATE_LAST', 0xdfff );
65 define( 'UNICODE_MAX', 0x10ffff );
66 define( 'UNICODE_REPLACEMENT', 0xfffd );
67
68
69 define( 'UTF8_HANGUL_FIRST', codepointToUtf8( UNICODE_HANGUL_FIRST ) );
70 define( 'UTF8_HANGUL_LAST', codepointToUtf8( UNICODE_HANGUL_LAST ) );
71
72 define( 'UTF8_HANGUL_LBASE', codepointToUtf8( UNICODE_HANGUL_LBASE ) );
73 define( 'UTF8_HANGUL_VBASE', codepointToUtf8( UNICODE_HANGUL_VBASE ) );
74 define( 'UTF8_HANGUL_TBASE', codepointToUtf8( UNICODE_HANGUL_TBASE ) );
75
76 define( 'UTF8_HANGUL_LEND', codepointToUtf8( UNICODE_HANGUL_LEND ) );
77 define( 'UTF8_HANGUL_VEND', codepointToUtf8( UNICODE_HANGUL_VEND ) );
78 define( 'UTF8_HANGUL_TEND', codepointToUtf8( UNICODE_HANGUL_TEND ) );
79
80 define( 'UTF8_SURROGATE_FIRST', codepointToUtf8( UNICODE_SURROGATE_FIRST ) );
81 define( 'UTF8_SURROGATE_LAST', codepointToUtf8( UNICODE_SURROGATE_LAST ) );
82 define( 'UTF8_MAX', codepointToUtf8( UNICODE_MAX ) );
83 define( 'UTF8_REPLACEMENT', codepointToUtf8( UNICODE_REPLACEMENT ) );
84 #define( 'UTF8_REPLACEMENT', '!' );
85
86 define( 'UTF8_OVERLONG_A', "\xc1\xbf" );
87 define( 'UTF8_OVERLONG_B', "\xe0\x9f\xbf" );
88 define( 'UTF8_OVERLONG_C', "\xf0\x8f\xbf\xbf" );
89
90 # These two ranges are illegal
91 define( 'UTF8_FDD0', codepointToUtf8( 0xfdd0 ) );
92 define( 'UTF8_FDEF', codepointToUtf8( 0xfdef ) );
93 define( 'UTF8_FFFE', codepointToUtf8( 0xfffe ) );
94 define( 'UTF8_FFFF', codepointToUtf8( 0xffff ) );
95
96 define( 'UTF8_HEAD', false );
97 define( 'UTF8_TAIL', true );
98
99
100 /**
101 * For using the ICU wrapper
102 */
103 define( 'UNORM_NONE', 1 );
104 define( 'UNORM_NFD', 2 );
105 define( 'UNORM_NFKD', 3 );
106 define( 'UNORM_NFC', 4 );
107 define( 'UNORM_DEFAULT', UNORM_NFC );
108 define( 'UNORM_NFKC', 5 );
109 define( 'UNORM_FCD', 6 );
110
111 define( 'NORMALIZE_ICU', function_exists( 'utf8_normalize' ) );
112
113 /**
114 *
115 * @package MediaWiki
116 */
117 class UtfNormal {
118 /**
119 * The ultimate convenience function! Clean up invalid UTF-8 sequences,
120 * and convert to normal form C, canonical composition.
121 *
122 * Fast return for pure ASCII strings; some lesser optimizations for
123 * strings containing only known-good characters. Not as fast as toNFC().
124 *
125 * @param string $string a UTF-8 string
126 * @return string a clean, shiny, normalized UTF-8 string
127 */
128 function cleanUp( $string ) {
129 if( NORMALIZE_ICU ) {
130 # We exclude a few chars that ICU would not.
131 $string = preg_replace(
132 '/[\x00-\x08\x0b\x0c\x0e-\x1f]/',
133 UTF8_REPLACEMENT,
134 $string );
135 $string = str_replace( UTF8_FFFE, UTF8_REPLACEMENT, $string );
136 $string = str_replace( UTF8_FFFF, UTF8_REPLACEMENT, $string );
137
138 # UnicodeString constructor fails if the string ends with a
139 # head byte. Add a junk char at the end, we'll strip it off.
140 return rtrim( utf8_normalize( $string . "\x01", UNORM_NFC ), "\x01" );
141 } elseif( UtfNormal::quickIsNFCVerify( $string ) ) {
142 # Side effect -- $string has had UTF-8 errors cleaned up.
143 return $string;
144 } else {
145 return UtfNormal::NFC( $string );
146 }
147 }
148
149 /**
150 * Convert a UTF-8 string to normal form C, canonical composition.
151 * Fast return for pure ASCII strings; some lesser optimizations for
152 * strings containing only known-good characters.
153 *
154 * @param string $string a valid UTF-8 string. Input is not validated.
155 * @return string a UTF-8 string in normal form C
156 */
157 function toNFC( $string ) {
158 if( NORMALIZE_ICU )
159 return utf8_normalize( $string, UNORM_NFC );
160 elseif( UtfNormal::quickIsNFC( $string ) )
161 return $string;
162 else
163 return UtfNormal::NFC( $string );
164 }
165
166 /**
167 * Convert a UTF-8 string to normal form D, canonical decomposition.
168 * Fast return for pure ASCII strings.
169 *
170 * @param string $string a valid UTF-8 string. Input is not validated.
171 * @return string a UTF-8 string in normal form D
172 */
173 function toNFD( $string ) {
174 if( NORMALIZE_ICU )
175 return utf8_normalize( $string, UNORM_NFD );
176 elseif( preg_match( '/[\x80-\xff]/', $string ) )
177 return UtfNormal::NFD( $string );
178 else
179 return $string;
180 }
181
182 /**
183 * Convert a UTF-8 string to normal form KC, compatibility composition.
184 * This may cause irreversible information loss, use judiciously.
185 * Fast return for pure ASCII strings.
186 *
187 * @param string $string a valid UTF-8 string. Input is not validated.
188 * @return string a UTF-8 string in normal form KC
189 */
190 function toNFKC( $string ) {
191 if( NORMALIZE_ICU )
192 return utf8_normalize( $string, UNORM_NFKC );
193 elseif( preg_match( '/[\x80-\xff]/', $string ) )
194 return UtfNormal::NFKC( $string );
195 else
196 return $string;
197 }
198
199 /**
200 * Convert a UTF-8 string to normal form KD, compatibility decomposition.
201 * This may cause irreversible information loss, use judiciously.
202 * Fast return for pure ASCII strings.
203 *
204 * @param string $string a valid UTF-8 string. Input is not validated.
205 * @return string a UTF-8 string in normal form KD
206 */
207 function toNFKD( $string ) {
208 if( NORMALIZE_ICU )
209 return utf8_normalize( $string, UNORM_NFKD );
210 elseif( preg_match( '/[\x80-\xff]/', $string ) )
211 return UtfNormal::NFKD( $string );
212 else
213 return $string;
214 }
215
216 /**
217 * Load the basic composition data if necessary
218 * @access private
219 */
220 function loadData() {
221 global $utfCombiningClass, $utfCanonicalComp, $utfCanonicalDecomp;
222 if( !isset( $utfCombiningClass ) ) {
223 require_once( 'UtfNormalData.inc' );
224 }
225 }
226
227 /**
228 * Returns true if the string is _definitely_ in NFC.
229 * Returns false if not or uncertain.
230 * @param string $string a valid UTF-8 string. Input is not validated.
231 * @return bool
232 */
233 function quickIsNFC( $string ) {
234 # ASCII is always valid NFC!
235 # If it's pure ASCII, let it through.
236 if( !preg_match( '/[\x80-\xff]/', $string ) ) return true;
237
238 UtfNormal::loadData();
239 global $utfCheckNFC, $utfCombiningClass;
240 $len = strlen( $string );
241 for( $i = 0; $i < $len; $i++ ) {
242 $c = $string{$i};
243 $n = ord( $c );
244 if( $n < 0x80 ) {
245 continue;
246 } elseif( $n >= 0xf0 ) {
247 $c = substr( $string, $i, 4 );
248 $i += 3;
249 } elseif( $n >= 0xe0 ) {
250 $c = substr( $string, $i, 3 );
251 $i += 2;
252 } elseif( $n >= 0xc0 ) {
253 $c = substr( $string, $i, 2 );
254 $i++;
255 }
256 if( isset( $utfCheckNFC[$c] ) ) {
257 # If it's NO or MAYBE, bail and do the slow check.
258 return false;
259 }
260 if( isset( $utfCombiningClass[$c] ) ) {
261 # Combining character? We might have to do sorting, at least.
262 return false;
263 }
264 }
265 return true;
266 }
267
268 /**
269 * Returns true if the string is _definitely_ in NFC.
270 * Returns false if not or uncertain.
271 * @param string $string a UTF-8 string, altered on output to be valid UTF-8 safe for XML.
272 */
273 function quickIsNFCVerify( &$string ) {
274 # Screen out some characters that eg won't be allowed in XML
275 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', UTF8_REPLACEMENT, $string );
276
277 # ASCII is always valid NFC!
278 # If we're only ever given plain ASCII, we can avoid the overhead
279 # of initializing the decomposition tables by skipping out early.
280 if( !preg_match( '/[\x80-\xff]/', $string ) ) return true;
281
282 static $checkit = null, $tailBytes = null, $utfCheckOrCombining = null;
283 if( !isset( $checkit ) ) {
284 # Load/build some scary lookup tables...
285 UtfNormal::loadData();
286 global $utfCheckNFC, $utfCombiningClass;
287
288 $utfCheckOrCombining = array_merge( $utfCheckNFC, $utfCombiningClass );
289
290 # Head bytes for sequences which we should do further validity checks
291 $checkit = array_flip( array_map( 'chr',
292 array( 0xc0, 0xc1, 0xe0, 0xed, 0xef,
293 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
294 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff ) ) );
295
296 # Each UTF-8 head byte is followed by a certain
297 # number of tail bytes.
298 $tailBytes = array();
299 for( $n = 0; $n < 256; $n++ ) {
300 if( $n < 0xc0 ) {
301 $remaining = 0;
302 } elseif( $n < 0xe0 ) {
303 $remaining = 1;
304 } elseif( $n < 0xf0 ) {
305 $remaining = 2;
306 } elseif( $n < 0xf8 ) {
307 $remaining = 3;
308 } elseif( $n < 0xfc ) {
309 $remaining = 4;
310 } elseif( $n < 0xfe ) {
311 $remaining = 5;
312 } else {
313 $remaining = 0;
314 }
315 $tailBytes[chr($n)] = $remaining;
316 }
317 }
318
319 # Chop the text into pure-ASCII and non-ASCII areas;
320 # large ASCII parts can be handled much more quickly.
321 # Don't chop up Unicode areas for punctuation, though,
322 # that wastes energy.
323 preg_match_all(
324 '/([\x00-\x7f]+|[\x80-\xff][\x00-\x40\x5b-\x5f\x7b-\xff]*)/',
325 $string, $matches );
326
327 $looksNormal = true;
328 $base = 0;
329 $replace = array();
330 foreach( $matches[1] as $str ) {
331 $chunk = strlen( $str );
332
333 if( $str{0} < "\x80" ) {
334 # ASCII chunk: guaranteed to be valid UTF-8
335 # and in normal form C, so skip over it.
336 $base += $chunk;
337 continue;
338 }
339
340 # We'll have to examine the chunk byte by byte to ensure
341 # that it consists of valid UTF-8 sequences, and to see
342 # if any of them might not be normalized.
343 #
344 # Since PHP is not the fastest language on earth, some of
345 # this code is a little ugly with inner loop optimizations.
346
347 $head = '';
348 $len = $chunk + 1; # Counting down is faster. I'm *so* sorry.
349
350 for( $i = -1; --$len; ) {
351 if( $remaining = $tailBytes[$c = $str{++$i}] ) {
352 # UTF-8 head byte!
353 $sequence = $head = $c;
354 do {
355 # Look for the defined number of tail bytes...
356 if( --$len && ( $c = $str{++$i} ) >= "\x80" && $c < "\xc0" ) {
357 # Legal tail bytes are nice.
358 $sequence .= $c;
359 } else {
360 if( 0 == $len ) {
361 # Premature end of string!
362 # Drop a replacement character into output to
363 # represent the invalid UTF-8 sequence.
364 $replace[] = array( UTF8_REPLACEMENT,
365 $base + $i + 1 - strlen( $sequence ),
366 strlen( $sequence ) );
367 break 2;
368 } else {
369 # Illegal tail byte; abandon the sequence.
370 $replace[] = array( UTF8_REPLACEMENT,
371 $base + $i - strlen( $sequence ),
372 strlen( $sequence ) );
373 # Back up and reprocess this byte; it may itself
374 # be a legal ASCII or UTF-8 sequence head.
375 --$i;
376 ++$len;
377 continue 2;
378 }
379 }
380 } while( --$remaining );
381
382 if( isset( $checkit[$head] ) ) {
383 # Do some more detailed validity checks, for
384 # invalid characters and illegal sequences.
385 if( $head == "\xed" ) {
386 # 0xed is relatively frequent in Korean, which
387 # abuts the surrogate area, so we're doing
388 # this check separately to speed things up.
389
390 if( $sequence >= UTF8_SURROGATE_FIRST ) {
391 # Surrogates are legal only in UTF-16 code.
392 # They are totally forbidden here in UTF-8
393 # utopia.
394 $replace[] = array( UTF8_REPLACEMENT,
395 $base + $i + 1 - strlen( $sequence ),
396 strlen( $sequence ) );
397 $head = '';
398 continue;
399 }
400 } else {
401 # Slower, but rarer checks...
402 $n = ord( $head );
403 if(
404 # "Overlong sequences" are those that are syntactically
405 # correct but use more UTF-8 bytes than are necessary to
406 # encode a character. Naïve string comparisons can be
407 # tricked into failing to see a match for an ASCII
408 # character, for instance, which can be a security hole
409 # if blacklist checks are being used.
410 ($n < 0xc2 && $sequence <= UTF8_OVERLONG_A)
411 || ($n == 0xe0 && $sequence <= UTF8_OVERLONG_B)
412 || ($n == 0xf0 && $sequence <= UTF8_OVERLONG_C)
413
414 # U+FFFE and U+FFFF are explicitly forbidden in Unicode.
415 || ($n == 0xef &&
416 ($sequence == UTF8_FFFE)
417 || ($sequence == UTF8_FFFF) )
418
419 # Unicode has been limited to 21 bits; longer
420 # sequences are not allowed.
421 || ($n >= 0xf0 && $sequence > UTF8_MAX) ) {
422
423 $replace[] = array( UTF8_REPLACEMENT,
424 $base + $i + 1 - strlen( $sequence ),
425 strlen( $sequence ) );
426 $head = '';
427 continue;
428 }
429 }
430 }
431
432 if( isset( $utfCheckOrCombining[$sequence] ) ) {
433 # If it's NO or MAYBE, we'll have to rip
434 # the string apart and put it back together.
435 # That's going to be mighty slow.
436 $looksNormal = false;
437 }
438
439 # The sequence is legal!
440 $head = '';
441 } elseif( $c < "\x80" ) {
442 # ASCII byte.
443 $head = '';
444 } elseif( $c < "\xc0" ) {
445 # Illegal tail bytes
446 if( $head == '' ) {
447 # Out of the blue!
448 $replace[] = array( UTF8_REPLACEMENT, $base + $i, 1 );
449 } else {
450 # Don't add if we're continuing a broken sequence;
451 # we already put a replacement character when we looked
452 # at the broken sequence.
453 $replace[] = array( '', $base + $i, 1 );
454 }
455 } else {
456 # Miscellaneous freaks.
457 $replace[] = array( UTF8_REPLACEMENT, $base + $i, 1 );
458 $head = '';
459 }
460 }
461 $base += $chunk;
462 }
463 if( count( $replace ) ) {
464 # There were illegal UTF-8 sequences we need to fix up.
465 $out = '';
466 $last = 0;
467 foreach( $replace as $rep ) {
468 list( $replacement, $start, $length ) = $rep;
469 if( $last < $start ) {
470 $out .= substr( $string, $last, $start - $last );
471 }
472 $out .= $replacement;
473 $last = $start + $length;
474 }
475 if( $last < strlen( $string ) ) {
476 $out .= substr( $string, $last );
477 }
478 $string = $out;
479 }
480 return $looksNormal;
481 }
482
483 # These take a string and run the normalization on them, without
484 # checking for validity or any optimization etc. Input must be
485 # VALID UTF-8!
486 /**
487 * @param string $string
488 * @return string
489 * @access private
490 */
491 function NFC( $string ) {
492 return UtfNormal::fastCompose( UtfNormal::NFD( $string ) );
493 }
494
495 /**
496 * @param string $string
497 * @return string
498 * @access private
499 */
500 function NFD( $string ) {
501 UtfNormal::loadData();
502 global $utfCanonicalDecomp;
503 return UtfNormal::fastCombiningSort(
504 UtfNormal::fastDecompose( $string, $utfCanonicalDecomp ) );
505 }
506
507 /**
508 * @param string $string
509 * @return string
510 * @access private
511 */
512 function NFKC( $string ) {
513 return UtfNormal::fastCompose( UtfNormal::NFKD( $string ) );
514 }
515
516 /**
517 * @param string $string
518 * @return string
519 * @access private
520 */
521 function NFKD( $string ) {
522 global $utfCompatibilityDecomp;
523 if( !isset( $utfCompatibilityDecomp ) ) {
524 require_once( 'UtfNormalDataK.inc' );
525 }
526 return UtfNormal::fastCombiningSort(
527 UtfNormal::fastDecompose( $string, $utfCompatibilityDecomp ) );
528 }
529
530
531 /**
532 * Perform decomposition of a UTF-8 string into either D or KD form
533 * (depending on which decomposition map is passed to us).
534 * Input is assumed to be *valid* UTF-8. Invalid code will break.
535 * @access private
536 * @param string $string Valid UTF-8 string
537 * @param array $map hash of expanded decomposition map
538 * @return string a UTF-8 string decomposed, not yet normalized (needs sorting)
539 */
540 function fastDecompose( $string, &$map ) {
541 UtfNormal::loadData();
542 $len = strlen( $string );
543 $out = '';
544 for( $i = 0; $i < $len; $i++ ) {
545 $c = $string{$i};
546 $n = ord( $c );
547 if( $n < 0x80 ) {
548 # ASCII chars never decompose
549 # THEY ARE IMMORTAL
550 $out .= $c;
551 continue;
552 } elseif( $n >= 0xf0 ) {
553 $c = substr( $string, $i, 4 );
554 $i += 3;
555 } elseif( $n >= 0xe0 ) {
556 $c = substr( $string, $i, 3 );
557 $i += 2;
558 } elseif( $n >= 0xc0 ) {
559 $c = substr( $string, $i, 2 );
560 $i++;
561 }
562 if( isset( $map[$c] ) ) {
563 $out .= $map[$c];
564 continue;
565 } else {
566 if( $c >= UTF8_HANGUL_FIRST && $c <= UTF8_HANGUL_LAST ) {
567 # Decompose a hangul syllable into jamo;
568 # hardcoded for three-byte UTF-8 sequence.
569 # A lookup table would be slightly faster,
570 # but adds a lot of memory & disk needs.
571 #
572 $index = ( (ord( $c{0} ) & 0x0f) << 12
573 | (ord( $c{1} ) & 0x3f) << 6
574 | (ord( $c{2} ) & 0x3f) )
575 - UNICODE_HANGUL_FIRST;
576 $l = IntVal( $index / UNICODE_HANGUL_NCOUNT );
577 $v = IntVal( ($index % UNICODE_HANGUL_NCOUNT) / UNICODE_HANGUL_TCOUNT);
578 $t = $index % UNICODE_HANGUL_TCOUNT;
579 $out .= "\xe1\x84" . chr( 0x80 + $l ) . "\xe1\x85" . chr( 0xa1 + $v );
580 if( $t >= 25 ) {
581 $out .= "\xe1\x87" . chr( 0x80 + $t - 25 );
582 } elseif( $t ) {
583 $out .= "\xe1\x86" . chr( 0xa7 + $t );
584 }
585 continue;
586 }
587 }
588 $out .= $c;
589 }
590 return $out;
591 }
592
593 /**
594 * Sorts combining characters into canonical order. This is the
595 * final step in creating decomposed normal forms D and KD.
596 * @access private
597 * @param string $string a valid, decomposed UTF-8 string. Input is not validated.
598 * @return string a UTF-8 string with combining characters sorted in canonical order
599 */
600 function fastCombiningSort( $string ) {
601 UtfNormal::loadData();
602 global $utfCombiningClass;
603 $len = strlen( $string );
604 $out = '';
605 $combiners = array();
606 $lastClass = -1;
607 for( $i = 0; $i < $len; $i++ ) {
608 $c = $string{$i};
609 $n = ord( $c );
610 if( $n >= 0x80 ) {
611 if( $n >= 0xf0 ) {
612 $c = substr( $string, $i, 4 );
613 $i += 3;
614 } elseif( $n >= 0xe0 ) {
615 $c = substr( $string, $i, 3 );
616 $i += 2;
617 } elseif( $n >= 0xc0 ) {
618 $c = substr( $string, $i, 2 );
619 $i++;
620 }
621 if( isset( $utfCombiningClass[$c] ) ) {
622 $lastClass = $utfCombiningClass[$c];
623 @$combiners[$lastClass] .= $c;
624 continue;
625 }
626 }
627 if( $lastClass ) {
628 ksort( $combiners );
629 $out .= implode( '', $combiners );
630 $combiners = array();
631 }
632 $out .= $c;
633 $lastClass = 0;
634 }
635 if( $lastClass ) {
636 ksort( $combiners );
637 $out .= implode( '', $combiners );
638 }
639 return $out;
640 }
641
642 /**
643 * Produces canonically composed sequences, i.e. normal form C or KC.
644 *
645 * @access private
646 * @param string $string a valid UTF-8 string in sorted normal form D or KD. Input is not validated.
647 * @return string a UTF-8 string with canonical precomposed characters used where possible
648 */
649 function fastCompose( $string ) {
650 UtfNormal::loadData();
651 global $utfCanonicalComp, $utfCombiningClass;
652 $len = strlen( $string );
653 $out = '';
654 $lastClass = -1;
655 $lastHangul = 0;
656 $startChar = '';
657 $combining = '';
658 $x1 = ord(substr(UTF8_HANGUL_VBASE,0,1));
659 $x2 = ord(substr(UTF8_HANGUL_TEND,0,1));
660 for( $i = 0; $i < $len; $i++ ) {
661 $c = $string{$i};
662 $n = ord( $c );
663 if( $n < 0x80 ) {
664 # No combining characters here...
665 $out .= $startChar;
666 $out .= $combining;
667 $startChar = $c;
668 $combining = '';
669 $lastClass = 0;
670 continue;
671 } elseif( $n >= 0xf0 ) {
672 $c = substr( $string, $i, 4 );
673 $i += 3;
674 } elseif( $n >= 0xe0 ) {
675 $c = substr( $string, $i, 3 );
676 $i += 2;
677 } elseif( $n >= 0xc0 ) {
678 $c = substr( $string, $i, 2 );
679 $i++;
680 }
681 $pair = $startChar . $c;
682 if( $n > 0x80 ) {
683 if( isset( $utfCombiningClass[$c] ) ) {
684 # A combining char; see what we can do with it
685 $class = $utfCombiningClass[$c];
686 if( !empty( $startChar ) &&
687 $lastClass < $class &&
688 $class > 0 &&
689 isset( $utfCanonicalComp[$pair] ) ) {
690 $startChar = $utfCanonicalComp[$pair];
691 $class = 0;
692 } else {
693 $combining .= $c;
694 }
695 $lastClass = $class;
696 $lastHangul = 0;
697 continue;
698 }
699 }
700 # New start char
701 if( $lastClass == 0 ) {
702 if( isset( $utfCanonicalComp[$pair] ) ) {
703 $startChar = $utfCanonicalComp[$pair];
704 $lastHangul = 0;
705 continue;
706 }
707 if( $n >= $x1 && $n <= $x2 ) {
708 # WARNING: Hangul code is painfully slow.
709 # I apologize for this ugly, ugly code; however
710 # performance is even more teh suck if we call
711 # out to nice clean functions. Lookup tables are
712 # marginally faster, but require a lot of space.
713 #
714 if( $c >= UTF8_HANGUL_VBASE &&
715 $c <= UTF8_HANGUL_VEND &&
716 $startChar >= UTF8_HANGUL_LBASE &&
717 $startChar <= UTF8_HANGUL_LEND ) {
718 #
719 #$lIndex = utf8ToCodepoint( $startChar ) - UNICODE_HANGUL_LBASE;
720 #$vIndex = utf8ToCodepoint( $c ) - UNICODE_HANGUL_VBASE;
721 $lIndex = ord( $startChar{2} ) - 0x80;
722 $vIndex = ord( $c{2} ) - 0xa1;
723
724 $hangulPoint = UNICODE_HANGUL_FIRST +
725 UNICODE_HANGUL_TCOUNT *
726 (UNICODE_HANGUL_VCOUNT * $lIndex + $vIndex);
727
728 # Hardcode the limited-range UTF-8 conversion:
729 $startChar = chr( $hangulPoint >> 12 & 0x0f | 0xe0 ) .
730 chr( $hangulPoint >> 6 & 0x3f | 0x80 ) .
731 chr( $hangulPoint & 0x3f | 0x80 );
732 $lastHangul = 0;
733 continue;
734 } elseif( $c >= UTF8_HANGUL_TBASE &&
735 $c <= UTF8_HANGUL_TEND &&
736 $startChar >= UTF8_HANGUL_FIRST &&
737 $startChar <= UTF8_HANGUL_LAST &&
738 !$lastHangul ) {
739 # $tIndex = utf8ToCodepoint( $c ) - UNICODE_HANGUL_TBASE;
740 $tIndex = ord( $c{2} ) - 0xa7;
741 if( $tIndex < 0 ) $tIndex = ord( $c{2} ) - 0x80 + (0x11c0 - 0x11a7);
742
743 # Increment the code point by $tIndex, without
744 # the function overhead of decoding and recoding UTF-8
745 #
746 $tail = ord( $startChar{2} ) + $tIndex;
747 if( $tail > 0xbf ) {
748 $tail -= 0x40;
749 $mid = ord( $startChar{1} ) + 1;
750 if( $mid > 0xbf ) {
751 $startChar{0} = chr( ord( $startChar{0} ) + 1 );
752 $mid -= 0x40;
753 }
754 $startChar{1} = chr( $mid );
755 }
756 $startChar{2} = chr( $tail );
757
758 # If there's another jamo char after this, *don't* try to merge it.
759 $lastHangul = 1;
760 continue;
761 }
762 }
763 }
764 $out .= $startChar;
765 $out .= $combining;
766 $startChar = $c;
767 $combining = '';
768 $lastClass = 0;
769 $lastHangul = 0;
770 }
771 $out .= $startChar . $combining;
772 return $out;
773 }
774
775 /**
776 * This is just used for the benchmark, comparing how long it takes to
777 * interate through a string without really doing anything of substance.
778 * @param string $string
779 * @return string
780 */
781 function placebo( $string ) {
782 $len = strlen( $string );
783 $out = '';
784 for( $i = 0; $i < $len; $i++ ) {
785 $out .= $string{$i};
786 }
787 return $out;
788 }
789 }
790
791 ?>