Optimization on cleanUp(): roughly 1/3 speed boost on ascii-dominant but not ascii...
[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( UtfNormal::quickIsNFCVerify( $string ) )
130 return $string;
131 else
132 return UtfNormal::NFC( $string );
133 }
134
135 /**
136 * Convert a UTF-8 string to normal form C, canonical composition.
137 * Fast return for pure ASCII strings; some lesser optimizations for
138 * strings containing only known-good characters.
139 *
140 * @param string $string a valid UTF-8 string. Input is not validated.
141 * @return string a UTF-8 string in normal form C
142 */
143 function toNFC( $string ) {
144 if( NORMALIZE_ICU )
145 return utf8_normalize( $string, UNORM_NFC );
146 elseif( UtfNormal::quickIsNFC( $string ) )
147 return $string;
148 else
149 return UtfNormal::NFC( $string );
150 }
151
152 /**
153 * Convert a UTF-8 string to normal form D, canonical decomposition.
154 * Fast return for pure ASCII strings.
155 *
156 * @param string $string a valid UTF-8 string. Input is not validated.
157 * @return string a UTF-8 string in normal form D
158 */
159 function toNFD( $string ) {
160 if( NORMALIZE_ICU )
161 return utf8_normalize( $string, UNORM_NFD );
162 elseif( preg_match( '/[\x80-\xff]/', $string ) )
163 return UtfNormal::NFD( $string );
164 else
165 return $string;
166 }
167
168 /**
169 * Convert a UTF-8 string to normal form KC, compatibility composition.
170 * This may cause irreversible information loss, use judiciously.
171 * Fast return for pure ASCII strings.
172 *
173 * @param string $string a valid UTF-8 string. Input is not validated.
174 * @return string a UTF-8 string in normal form KC
175 */
176 function toNFKC( $string ) {
177 if( NORMALIZE_ICU )
178 return utf8_normalize( $string, UNORM_NFKC );
179 elseif( preg_match( '/[\x80-\xff]/', $string ) )
180 return UtfNormal::NFKC( $string );
181 else
182 return $string;
183 }
184
185 /**
186 * Convert a UTF-8 string to normal form KD, compatibility decomposition.
187 * This may cause irreversible information loss, use judiciously.
188 * Fast return for pure ASCII strings.
189 *
190 * @param string $string a valid UTF-8 string. Input is not validated.
191 * @return string a UTF-8 string in normal form KD
192 */
193 function toNFKD( $string ) {
194 if( NORMALIZE_ICU )
195 return utf8_normalize( $string, UNORM_NFKD );
196 elseif( preg_match( '/[\x80-\xff]/', $string ) )
197 return UtfNormal::NFKD( $string );
198 else
199 return $string;
200 }
201
202 /**
203 * Load the basic composition data if necessary
204 * @access private
205 */
206 function loadData() {
207 global $utfCombiningClass, $utfCanonicalComp, $utfCanonicalDecomp;
208 if( !isset( $utfCombiningClass ) ) {
209 require_once( 'UtfNormalData.inc' );
210 }
211 }
212
213 /**
214 * Returns true if the string is _definitely_ in NFC.
215 * Returns false if not or uncertain.
216 * @param string $string a valid UTF-8 string. Input is not validated.
217 * @return bool
218 */
219 function quickIsNFC( $string ) {
220 # ASCII is always valid NFC!
221 # If it's pure ASCII, let it through.
222 if( !preg_match( '/[\x80-\xff]/', $string ) ) return true;
223
224 UtfNormal::loadData();
225 global $utfCheckNFC, $utfCombiningClass;
226 $len = strlen( $string );
227 for( $i = 0; $i < $len; $i++ ) {
228 $c = $string{$i};
229 $n = ord( $c );
230 if( $n < 0x80 ) {
231 continue;
232 } elseif( $n >= 0xf0 ) {
233 $c = substr( $string, $i, 4 );
234 $i += 3;
235 } elseif( $n >= 0xe0 ) {
236 $c = substr( $string, $i, 3 );
237 $i += 2;
238 } elseif( $n >= 0xc0 ) {
239 $c = substr( $string, $i, 2 );
240 $i++;
241 }
242 if( isset( $utfCheckNFC[$c] ) ) {
243 # If it's NO or MAYBE, bail and do the slow check.
244 return false;
245 }
246 if( isset( $utfCombiningClass[$c] ) ) {
247 # Combining character? We might have to do sorting, at least.
248 return false;
249 }
250 }
251 return true;
252 }
253
254 /**
255 * Returns true if the string is _definitely_ in NFC.
256 * Returns false if not or uncertain.
257 * @param string $string a UTF-8 string, altered on output to be valid UTF-8 safe for XML.
258 */
259 function quickIsNFCVerify( &$string ) {
260 # Screen out some characters that eg won't be allowed in XML
261 preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', UTF8_REPLACEMENT, $string );
262
263 # ASCII is always valid NFC!
264 if( !preg_match( '/[\x80-\xff]/', $string ) ) return true;
265
266 UtfNormal::loadData();
267 global $utfCheckNFC, $utfCombiningClass;
268
269 static $checkit = null, $tailBytes = null;
270 if( !isset( $checkit ) ) {
271 # Head bytes for sequences which we should do further validity checks
272 $checkit = array_flip(
273 array( 0xc0, 0xc1, 0xe0, 0xed, 0xef,
274 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
275 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff ) );
276
277 $tailBytes = array();
278 for( $n = 0xc0; $n < 0xfe; $n++ ) {
279 if( $n < 0xe0 ) {
280 $remaining = 1;
281 } elseif( $n < 0xf0 ) {
282 $remaining = 2;
283 } elseif( $n < 0xf8 ) {
284 $remaining = 3;
285 } elseif( $n < 0xfc ) {
286 $remaining = 4;
287 } elseif( $n < 0xfe ) {
288 $remaining = 5;
289 }
290 $tailBytes[$n] = $remaining;
291 }
292 }
293
294 $len = strlen( $string );
295 $out = '';
296 $tail = false;
297 $looksNormal = true;
298 $head = 0;
299
300 for( $i = 0; $i < $len; $i++ ) {
301 $c = $string{$i};
302 $n = ord( $c );
303 if( $tail ) {
304 if( $n >= 0x80 && $n < 0xc0 ) {
305 $sequence .= $c;
306 if( --$remaining ) {
307 # Keep adding bytes...
308 continue;
309 }
310
311 if( isset( $checkit[$head] ) ) {
312 # Do some more detailed validity checks, for
313 # invalid characters and illegal sequences.
314 if( ( $head == 0xed && $sequence >= UTF8_SURROGATE_FIRST
315 && $sequence <= UTF8_SURROGATE_LAST)
316 || ($head < 0xc2 && $sequence <= UTF8_OVERLONG_A)
317 || ($head == 0xe0 && $sequence <= UTF8_OVERLONG_B)
318 || ($head == 0xef &&
319 ($sequence >= UTF8_FDD0 && $sequence <= UTF8_FDEF)
320 || ($sequence == UTF8_FFFE)
321 || ($sequence == UTF8_FFFF) )
322 || ($head == 0xf0 && $sequence <= UTF8_OVERLONG_C)
323 || ($head >= 0xf0 && $sequence > UTF8_MAX) ) {
324 $out .= UTF8_REPLACEMENT;
325 $tail = false;
326 continue;
327 }
328 }
329
330 if( isset( $utfCheckNFC[$sequence] ) ||
331 isset( $utfCombiningClass[$sequence] ) ) {
332 # If it's NO or MAYBE, we'll have to do the slow check.
333 $looksNormal = false;
334 }
335
336 # The sequence is legal!
337 $out .= $sequence;
338 $tail = false;
339 $head = 0;
340 continue;
341 }
342 # Not a valid tail byte! DIscard the char we've been building.
343 #printf ("Invalid '%x' in tail with %d remaining bytes\n", $n, $remaining );
344 $tail = false;
345 $out .= UTF8_REPLACEMENT;
346 }
347 if( $n < 0x80 ) {
348 # Friendly ASCII chars.
349 # We can speed things up a bit for latin-based scripts
350 # where they tend to come in groups:
351 $out .= $c;
352 $i++;
353 while( $i < $len && ( $c = $string{$i} ) < "\x80" ) {
354 $out .= $c;
355 $i++;
356 }
357 $i--;
358 } elseif( $n < 0xc0 ) {
359 # illegal tail bytes or head byte of overlong sequence
360 if( $head == 0 ) {
361 # Don't add if we're continuing a too-long sequence
362 $out .= UTF8_REPLACEMENT;
363 }
364 } elseif( $n < 0xfe ) {
365 $tail = true;
366 $remaining = $tailBytes[$n];
367 $sequence = $c;
368 $head = $n;
369 } else {
370 $out .= UTF8_REPLACEMENT;
371 }
372 }
373 if( $tail ) {
374 $out .= UTF8_REPLACEMENT;
375 }
376 $string = $out;
377 return $looksNormal;
378 }
379
380 # These take a string and run the normalization on them, without
381 # checking for validity or any optimization etc. Input must be
382 # VALID UTF-8!
383 /**
384 * @param string $string
385 * @return string
386 * @access private
387 */
388 function NFC( $string ) {
389 return UtfNormal::fastCompose( UtfNormal::NFD( $string ) );
390 }
391
392 /**
393 * @param string $string
394 * @return string
395 * @access private
396 */
397 function NFD( $string ) {
398 UtfNormal::loadData();
399 global $utfCanonicalDecomp;
400 return UtfNormal::fastCombiningSort(
401 UtfNormal::fastDecompose( $string, $utfCanonicalDecomp ) );
402 }
403
404 /**
405 * @param string $string
406 * @return string
407 * @access private
408 */
409 function NFKC( $string ) {
410 return UtfNormal::fastCompose( UtfNormal::NFKD( $string ) );
411 }
412
413 /**
414 * @param string $string
415 * @return string
416 * @access private
417 */
418 function NFKD( $string ) {
419 global $utfCompatibilityDecomp;
420 if( !isset( $utfCompatibilityDecomp ) ) {
421 require_once( 'UtfNormalDataK.inc' );
422 }
423 return UtfNormal::fastCombiningSort(
424 UtfNormal::fastDecompose( $string, $utfCompatibilityDecomp ) );
425 }
426
427
428 /**
429 * Perform decomposition of a UTF-8 string into either D or KD form
430 * (depending on which decomposition map is passed to us).
431 * Input is assumed to be *valid* UTF-8. Invalid code will break.
432 * @access private
433 * @param string $string Valid UTF-8 string
434 * @param array $map hash of expanded decomposition map
435 * @return string a UTF-8 string decomposed, not yet normalized (needs sorting)
436 */
437 function fastDecompose( $string, &$map ) {
438 UtfNormal::loadData();
439 $len = strlen( $string );
440 $out = '';
441 for( $i = 0; $i < $len; $i++ ) {
442 $c = $string{$i};
443 $n = ord( $c );
444 if( $n < 0x80 ) {
445 # ASCII chars never decompose
446 # THEY ARE IMMORTAL
447 $out .= $c;
448 continue;
449 } elseif( $n >= 0xf0 ) {
450 $c = substr( $string, $i, 4 );
451 $i += 3;
452 } elseif( $n >= 0xe0 ) {
453 $c = substr( $string, $i, 3 );
454 $i += 2;
455 } elseif( $n >= 0xc0 ) {
456 $c = substr( $string, $i, 2 );
457 $i++;
458 }
459 if( isset( $map[$c] ) ) {
460 $out .= $map[$c];
461 continue;
462 } else {
463 if( $c >= UTF8_HANGUL_FIRST && $c <= UTF8_HANGUL_LAST ) {
464 # Decompose a hangul syllable into jamo;
465 # hardcoded for three-byte UTF-8 sequence.
466 # A lookup table would be slightly faster,
467 # but adds a lot of memory & disk needs.
468 #
469 $index = ( (ord( $c{0} ) & 0x0f) << 12
470 | (ord( $c{1} ) & 0x3f) << 6
471 | (ord( $c{2} ) & 0x3f) )
472 - UNICODE_HANGUL_FIRST;
473 $l = IntVal( $index / UNICODE_HANGUL_NCOUNT );
474 $v = IntVal( ($index % UNICODE_HANGUL_NCOUNT) / UNICODE_HANGUL_TCOUNT);
475 $t = $index % UNICODE_HANGUL_TCOUNT;
476 $out .= "\xe1\x84" . chr( 0x80 + $l ) . "\xe1\x85" . chr( 0xa1 + $v );
477 if( $t >= 25 ) {
478 $out .= "\xe1\x87" . chr( 0x80 + $t - 25 );
479 } elseif( $t ) {
480 $out .= "\xe1\x86" . chr( 0xa7 + $t );
481 }
482 continue;
483 }
484 }
485 $out .= $c;
486 }
487 return $out;
488 }
489
490 /**
491 * Sorts combining characters into canonical order. This is the
492 * final step in creating decomposed normal forms D and KD.
493 * @access private
494 * @param string $string a valid, decomposed UTF-8 string. Input is not validated.
495 * @return string a UTF-8 string with combining characters sorted in canonical order
496 */
497 function fastCombiningSort( $string ) {
498 UtfNormal::loadData();
499 global $utfCombiningClass;
500 $len = strlen( $string );
501 $out = '';
502 $combiners = array();
503 $lastClass = -1;
504 for( $i = 0; $i < $len; $i++ ) {
505 $c = $string{$i};
506 $n = ord( $c );
507 if( $n >= 0x80 ) {
508 if( $n >= 0xf0 ) {
509 $c = substr( $string, $i, 4 );
510 $i += 3;
511 } elseif( $n >= 0xe0 ) {
512 $c = substr( $string, $i, 3 );
513 $i += 2;
514 } elseif( $n >= 0xc0 ) {
515 $c = substr( $string, $i, 2 );
516 $i++;
517 }
518 if( isset( $utfCombiningClass[$c] ) ) {
519 $lastClass = $utfCombiningClass[$c];
520 @$combiners[$lastClass] .= $c;
521 continue;
522 }
523 }
524 if( $lastClass ) {
525 ksort( $combiners );
526 $out .= implode( '', $combiners );
527 $combiners = array();
528 }
529 $out .= $c;
530 $lastClass = 0;
531 }
532 if( $lastClass ) {
533 ksort( $combiners );
534 $out .= implode( '', $combiners );
535 }
536 return $out;
537 }
538
539 /**
540 * Produces canonically composed sequences, i.e. normal form C or KC.
541 *
542 * @access private
543 * @param string $string a valid UTF-8 string in sorted normal form D or KD. Input is not validated.
544 * @return string a UTF-8 string with canonical precomposed characters used where possible
545 */
546 function fastCompose( $string ) {
547 UtfNormal::loadData();
548 global $utfCanonicalComp, $utfCombiningClass;
549 $len = strlen( $string );
550 $out = '';
551 $lastClass = -1;
552 $startChar = '';
553 $combining = '';
554 $x1 = ord(substr(UTF8_HANGUL_VBASE,0,1));
555 $x2 = ord(substr(UTF8_HANGUL_TEND,0,1));
556 for( $i = 0; $i < $len; $i++ ) {
557 $c = $string{$i};
558 $n = ord( $c );
559 if( $n < 0x80 ) {
560 # No combining characters here...
561 $out .= $startChar;
562 $out .= $combining;
563 $startChar = $c;
564 $combining = '';
565 $lastClass = 0;
566 continue;
567 } elseif( $n >= 0xf0 ) {
568 $c = substr( $string, $i, 4 );
569 $i += 3;
570 } elseif( $n >= 0xe0 ) {
571 $c = substr( $string, $i, 3 );
572 $i += 2;
573 } elseif( $n >= 0xc0 ) {
574 $c = substr( $string, $i, 2 );
575 $i++;
576 }
577 $pair = $startChar . $c;
578 if( $n > 0x80 ) {
579 if( isset( $utfCombiningClass[$c] ) ) {
580 # A combining char; see what we can do with it
581 $class = $utfCombiningClass[$c];
582 if( !empty( $startChar ) &&
583 $lastClass < $class &&
584 $class > 0 &&
585 isset( $utfCanonicalComp[$pair] ) ) {
586 $startChar = $utfCanonicalComp[$pair];
587 $class = 0;
588 } else {
589 $combining .= $c;
590 }
591 $lastClass = $class;
592 continue;
593 }
594 }
595 # New start char
596 if( $lastClass == 0 ) {
597 if( isset( $utfCanonicalComp[$pair] ) ) {
598 $startChar = $utfCanonicalComp[$pair];
599 continue;
600 }
601 if( $n >= $x1 && $n <= $x2 ) {
602 # WARNING: Hangul code is painfully slow.
603 # I apologize for this ugly, ugly code; however
604 # performance is even more teh suck if we call
605 # out to nice clean functions. Lookup tables are
606 # marginally faster, but require a lot of space.
607 #
608 if( $c >= UTF8_HANGUL_VBASE &&
609 $c <= UTF8_HANGUL_VEND &&
610 $startChar >= UTF8_HANGUL_LBASE &&
611 $startChar <= UTF8_HANGUL_LEND ) {
612 #
613 #$lIndex = utf8ToCodepoint( $startChar ) - UNICODE_HANGUL_LBASE;
614 #$vIndex = utf8ToCodepoint( $c ) - UNICODE_HANGUL_VBASE;
615 $lIndex = ord( $startChar{2} ) - 0x80;
616 $vIndex = ord( $c{2} ) - 0xa1;
617
618 $hangulPoint = UNICODE_HANGUL_FIRST +
619 UNICODE_HANGUL_TCOUNT *
620 (UNICODE_HANGUL_VCOUNT * $lIndex + $vIndex);
621
622 # Hardcode the limited-range UTF-8 conversion:
623 $startChar = chr( $hangulPoint >> 12 & 0x0f | 0xe0 ) .
624 chr( $hangulPoint >> 6 & 0x3f | 0x80 ) .
625 chr( $hangulPoint & 0x3f | 0x80 );
626 continue;
627 } elseif( $c >= UTF8_HANGUL_TBASE &&
628 $c <= UTF8_HANGUL_TEND &&
629 $startChar >= UTF8_HANGUL_FIRST &&
630 $startChar <= UTF8_HANGUL_LAST ) {
631 # $tIndex = utf8ToCodepoint( $c ) - UNICODE_HANGUL_TBASE;
632 $tIndex = ord( $c{2} ) - 0xa7;
633 if( $tIndex < 0 ) $tIndex = ord( $c{2} ) - 0x80 + (0x11c0 - 0x11a7);
634
635 # Increment the code point by $tIndex, without
636 # the function overhead of decoding and recoding UTF-8
637 #
638 $tail = ord( $startChar{2} ) + $tIndex;
639 if( $tail > 0xbf ) {
640 $tail -= 0x40;
641 $mid = ord( $startChar{1} ) + 1;
642 if( $mid > 0xbf ) {
643 $startChar{0} = chr( ord( $startChar{0} ) + 1 );
644 $mid -= 0x40;
645 }
646 $startChar{1} = chr( $mid );
647 }
648 $startChar{2} = chr( $tail );
649 continue;
650 }
651 }
652 }
653 $out .= $startChar;
654 $out .= $combining;
655 $startChar = $c;
656 $combining = '';
657 $lastClass = 0;
658 }
659 $out .= $startChar . $combining;
660 return $out;
661 }
662
663 /**
664 * This is just used for the benchmark, comparing how long it takes to
665 * interate through a string without really doing anything of substance.
666 * @param string $string
667 * @return string
668 */
669 function placebo( $string ) {
670 $len = strlen( $string );
671 $out = '';
672 for( $i = 0; $i < $len; $i++ ) {
673 $out .= $string{$i};
674 }
675 return $out;
676 }
677 }
678
679 ?>