Don't forgot to actually _make_ the replacements for illegal chars. :P
[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 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', UTF8_REPLACEMENT, $string );
262
263 # ASCII is always valid NFC!
264 # If we're only ever given plain ASCII, we can avoid the overhead
265 # of initializing the decomposition tables by skipping out early.
266 if( !preg_match( '/[\x80-\xff]/', $string ) ) return true;
267
268 UtfNormal::loadData();
269 global $utfCheckNFC, $utfCombiningClass;
270
271 static $checkit = null, $tailBytes = null, $utfCheckOrCombining = null;
272 if( !isset( $checkit ) ) {
273 $utfCheckOrCombining = array_merge( $utfCheckNFC, $utfCombiningClass );
274
275 # Head bytes for sequences which we should do further validity checks
276 $checkit = array_flip( array_map( 'chr',
277 array( 0xc0, 0xc1, 0xe0, 0xed, 0xef,
278 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
279 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff ) ) );
280
281 $tailBytes = array();
282 for( $n = 0; $n < 256; $n++ ) {
283 if( $n < 0xc0 ) {
284 $remaining = 0;
285 } elseif( $n < 0xe0 ) {
286 $remaining = 1;
287 } elseif( $n < 0xf0 ) {
288 $remaining = 2;
289 } elseif( $n < 0xf8 ) {
290 $remaining = 3;
291 } elseif( $n < 0xfc ) {
292 $remaining = 4;
293 } elseif( $n < 0xfe ) {
294 $remaining = 5;
295 } else {
296 $remaining = 0;
297 }
298 $tailBytes[chr($n)] = $remaining;
299 }
300 }
301
302 # Chop the text into pure-ASCII and non-ASCII areas;
303 # large ASCII parts can be handled much more quickly.
304 # Don't chop up Unicode areas for punctuation, though,
305 # that wastes energy.
306 preg_match_all(
307 '/([\x00-\x7f]+|[\x80-\xff][\x00-\x40\x5b-\x5f\x7b-\xff]*)/',
308 $string, $matches );
309
310 ob_start();
311 $looksNormal = true;
312 foreach( $matches[1] as $str ) {
313 if( $str{0} < "\x80" ) {
314 # ASCII chunk: guaranteed to be valid UTF-8
315 # and in normal form C, so output it quick.
316 echo $str;
317 continue;
318 }
319
320 # We'll have to examine the chunk byte by byte to ensure
321 # that it consists of valid UTF-8 sequences, and to see
322 # if any of them might not be normalized.
323 #
324 # Since PHP is not the fastest language on earth, some of
325 # this code is a little ugly with inner loop optimizations.
326
327 $len = strlen( $str ) + 1;
328 $tail = 0;
329 $head = '';
330 for( $i = 0; --$len; ++$i ) {
331 if( $tail ) {
332 if( ( $c = $str{$i} ) >= "\x80" && $c < "\xc0" ) {
333 $sequence .= $c;
334 if( --$remaining ) {
335 # Keep adding bytes...
336 continue;
337 }
338
339 # We have come to the end of the sequence...
340 $tail = 0;
341
342 if( isset( $checkit[$head] ) ) {
343 # Do some more detailed validity checks, for
344 # invalid characters and illegal sequences.
345 if( $head == "\xed" ) {
346 # 0xed is relatively frequent in Korean, which
347 # abuts the surrogate area, so we're doing
348 # this check separately.
349 if( $sequence >= UTF8_SURROGATE_FIRST ) {
350 echo UTF8_REPLACEMENT;
351 continue;
352 }
353 } else {
354 $n = ord( $head );
355 if( ($n < 0xc2 && $sequence <= UTF8_OVERLONG_A)
356 || ($n == 0xe0 && $sequence <= UTF8_OVERLONG_B)
357 || ($n == 0xef &&
358 ($sequence >= UTF8_FDD0 && $sequence <= UTF8_FDEF)
359 || ($sequence == UTF8_FFFE)
360 || ($sequence == UTF8_FFFF) )
361 || ($n == 0xf0 && $sequence <= UTF8_OVERLONG_C)
362 || ($n >= 0xf0 && $sequence > UTF8_MAX) ) {
363 echo UTF8_REPLACEMENT;
364 continue;
365 }
366 }
367 }
368
369 if( isset( $utfCheckOrCombining[$sequence] ) ) {
370 # If it's NO or MAYBE, we'll have to rip
371 # the string apart and put it back together.
372 # That's going to be mighty slow.
373 $looksNormal = false;
374 }
375
376 # The sequence is legal!
377 echo $sequence;
378 $head = '';
379 continue;
380 }
381 # Not a valid tail byte! DIscard the char we've been building.
382 $tail = false;
383 echo UTF8_REPLACEMENT;
384 }
385 if( $remaining = $tailBytes[$c = $str{$i}] ) {
386 $tail = 1;
387 $sequence = $head = $c;
388 } elseif( $c < "\x80" ) {
389 echo $c;
390 } elseif( $c < "\xc0" ) {
391 # illegal tail bytes or head byte of overlong sequence
392 if( $head == '' ) {
393 # Don't add if we're continuing a too-long sequence
394 echo UTF8_REPLACEMENT;
395 }
396 } else {
397 echo UTF8_REPLACEMENT;
398 }
399 }
400 if( $tail ) {
401 # We ended the chunk in the middle of a sequence;
402 # that's so not cool.
403 echo UTF8_REPLACEMENT;
404 }
405 }
406 $string = ob_get_contents();
407 ob_end_clean();
408 return $looksNormal;
409 }
410
411 # These take a string and run the normalization on them, without
412 # checking for validity or any optimization etc. Input must be
413 # VALID UTF-8!
414 /**
415 * @param string $string
416 * @return string
417 * @access private
418 */
419 function NFC( $string ) {
420 return UtfNormal::fastCompose( UtfNormal::NFD( $string ) );
421 }
422
423 /**
424 * @param string $string
425 * @return string
426 * @access private
427 */
428 function NFD( $string ) {
429 UtfNormal::loadData();
430 global $utfCanonicalDecomp;
431 return UtfNormal::fastCombiningSort(
432 UtfNormal::fastDecompose( $string, $utfCanonicalDecomp ) );
433 }
434
435 /**
436 * @param string $string
437 * @return string
438 * @access private
439 */
440 function NFKC( $string ) {
441 return UtfNormal::fastCompose( UtfNormal::NFKD( $string ) );
442 }
443
444 /**
445 * @param string $string
446 * @return string
447 * @access private
448 */
449 function NFKD( $string ) {
450 global $utfCompatibilityDecomp;
451 if( !isset( $utfCompatibilityDecomp ) ) {
452 require_once( 'UtfNormalDataK.inc' );
453 }
454 return UtfNormal::fastCombiningSort(
455 UtfNormal::fastDecompose( $string, $utfCompatibilityDecomp ) );
456 }
457
458
459 /**
460 * Perform decomposition of a UTF-8 string into either D or KD form
461 * (depending on which decomposition map is passed to us).
462 * Input is assumed to be *valid* UTF-8. Invalid code will break.
463 * @access private
464 * @param string $string Valid UTF-8 string
465 * @param array $map hash of expanded decomposition map
466 * @return string a UTF-8 string decomposed, not yet normalized (needs sorting)
467 */
468 function fastDecompose( $string, &$map ) {
469 UtfNormal::loadData();
470 $len = strlen( $string );
471 $out = '';
472 for( $i = 0; $i < $len; $i++ ) {
473 $c = $string{$i};
474 $n = ord( $c );
475 if( $n < 0x80 ) {
476 # ASCII chars never decompose
477 # THEY ARE IMMORTAL
478 $out .= $c;
479 continue;
480 } elseif( $n >= 0xf0 ) {
481 $c = substr( $string, $i, 4 );
482 $i += 3;
483 } elseif( $n >= 0xe0 ) {
484 $c = substr( $string, $i, 3 );
485 $i += 2;
486 } elseif( $n >= 0xc0 ) {
487 $c = substr( $string, $i, 2 );
488 $i++;
489 }
490 if( isset( $map[$c] ) ) {
491 $out .= $map[$c];
492 continue;
493 } else {
494 if( $c >= UTF8_HANGUL_FIRST && $c <= UTF8_HANGUL_LAST ) {
495 # Decompose a hangul syllable into jamo;
496 # hardcoded for three-byte UTF-8 sequence.
497 # A lookup table would be slightly faster,
498 # but adds a lot of memory & disk needs.
499 #
500 $index = ( (ord( $c{0} ) & 0x0f) << 12
501 | (ord( $c{1} ) & 0x3f) << 6
502 | (ord( $c{2} ) & 0x3f) )
503 - UNICODE_HANGUL_FIRST;
504 $l = IntVal( $index / UNICODE_HANGUL_NCOUNT );
505 $v = IntVal( ($index % UNICODE_HANGUL_NCOUNT) / UNICODE_HANGUL_TCOUNT);
506 $t = $index % UNICODE_HANGUL_TCOUNT;
507 $out .= "\xe1\x84" . chr( 0x80 + $l ) . "\xe1\x85" . chr( 0xa1 + $v );
508 if( $t >= 25 ) {
509 $out .= "\xe1\x87" . chr( 0x80 + $t - 25 );
510 } elseif( $t ) {
511 $out .= "\xe1\x86" . chr( 0xa7 + $t );
512 }
513 continue;
514 }
515 }
516 $out .= $c;
517 }
518 return $out;
519 }
520
521 /**
522 * Sorts combining characters into canonical order. This is the
523 * final step in creating decomposed normal forms D and KD.
524 * @access private
525 * @param string $string a valid, decomposed UTF-8 string. Input is not validated.
526 * @return string a UTF-8 string with combining characters sorted in canonical order
527 */
528 function fastCombiningSort( $string ) {
529 UtfNormal::loadData();
530 global $utfCombiningClass;
531 $len = strlen( $string );
532 $out = '';
533 $combiners = array();
534 $lastClass = -1;
535 for( $i = 0; $i < $len; $i++ ) {
536 $c = $string{$i};
537 $n = ord( $c );
538 if( $n >= 0x80 ) {
539 if( $n >= 0xf0 ) {
540 $c = substr( $string, $i, 4 );
541 $i += 3;
542 } elseif( $n >= 0xe0 ) {
543 $c = substr( $string, $i, 3 );
544 $i += 2;
545 } elseif( $n >= 0xc0 ) {
546 $c = substr( $string, $i, 2 );
547 $i++;
548 }
549 if( isset( $utfCombiningClass[$c] ) ) {
550 $lastClass = $utfCombiningClass[$c];
551 @$combiners[$lastClass] .= $c;
552 continue;
553 }
554 }
555 if( $lastClass ) {
556 ksort( $combiners );
557 $out .= implode( '', $combiners );
558 $combiners = array();
559 }
560 $out .= $c;
561 $lastClass = 0;
562 }
563 if( $lastClass ) {
564 ksort( $combiners );
565 $out .= implode( '', $combiners );
566 }
567 return $out;
568 }
569
570 /**
571 * Produces canonically composed sequences, i.e. normal form C or KC.
572 *
573 * @access private
574 * @param string $string a valid UTF-8 string in sorted normal form D or KD. Input is not validated.
575 * @return string a UTF-8 string with canonical precomposed characters used where possible
576 */
577 function fastCompose( $string ) {
578 UtfNormal::loadData();
579 global $utfCanonicalComp, $utfCombiningClass;
580 $len = strlen( $string );
581 $out = '';
582 $lastClass = -1;
583 $startChar = '';
584 $combining = '';
585 $x1 = ord(substr(UTF8_HANGUL_VBASE,0,1));
586 $x2 = ord(substr(UTF8_HANGUL_TEND,0,1));
587 for( $i = 0; $i < $len; $i++ ) {
588 $c = $string{$i};
589 $n = ord( $c );
590 if( $n < 0x80 ) {
591 # No combining characters here...
592 $out .= $startChar;
593 $out .= $combining;
594 $startChar = $c;
595 $combining = '';
596 $lastClass = 0;
597 continue;
598 } elseif( $n >= 0xf0 ) {
599 $c = substr( $string, $i, 4 );
600 $i += 3;
601 } elseif( $n >= 0xe0 ) {
602 $c = substr( $string, $i, 3 );
603 $i += 2;
604 } elseif( $n >= 0xc0 ) {
605 $c = substr( $string, $i, 2 );
606 $i++;
607 }
608 $pair = $startChar . $c;
609 if( $n > 0x80 ) {
610 if( isset( $utfCombiningClass[$c] ) ) {
611 # A combining char; see what we can do with it
612 $class = $utfCombiningClass[$c];
613 if( !empty( $startChar ) &&
614 $lastClass < $class &&
615 $class > 0 &&
616 isset( $utfCanonicalComp[$pair] ) ) {
617 $startChar = $utfCanonicalComp[$pair];
618 $class = 0;
619 } else {
620 $combining .= $c;
621 }
622 $lastClass = $class;
623 continue;
624 }
625 }
626 # New start char
627 if( $lastClass == 0 ) {
628 if( isset( $utfCanonicalComp[$pair] ) ) {
629 $startChar = $utfCanonicalComp[$pair];
630 continue;
631 }
632 if( $n >= $x1 && $n <= $x2 ) {
633 # WARNING: Hangul code is painfully slow.
634 # I apologize for this ugly, ugly code; however
635 # performance is even more teh suck if we call
636 # out to nice clean functions. Lookup tables are
637 # marginally faster, but require a lot of space.
638 #
639 if( $c >= UTF8_HANGUL_VBASE &&
640 $c <= UTF8_HANGUL_VEND &&
641 $startChar >= UTF8_HANGUL_LBASE &&
642 $startChar <= UTF8_HANGUL_LEND ) {
643 #
644 #$lIndex = utf8ToCodepoint( $startChar ) - UNICODE_HANGUL_LBASE;
645 #$vIndex = utf8ToCodepoint( $c ) - UNICODE_HANGUL_VBASE;
646 $lIndex = ord( $startChar{2} ) - 0x80;
647 $vIndex = ord( $c{2} ) - 0xa1;
648
649 $hangulPoint = UNICODE_HANGUL_FIRST +
650 UNICODE_HANGUL_TCOUNT *
651 (UNICODE_HANGUL_VCOUNT * $lIndex + $vIndex);
652
653 # Hardcode the limited-range UTF-8 conversion:
654 $startChar = chr( $hangulPoint >> 12 & 0x0f | 0xe0 ) .
655 chr( $hangulPoint >> 6 & 0x3f | 0x80 ) .
656 chr( $hangulPoint & 0x3f | 0x80 );
657 continue;
658 } elseif( $c >= UTF8_HANGUL_TBASE &&
659 $c <= UTF8_HANGUL_TEND &&
660 $startChar >= UTF8_HANGUL_FIRST &&
661 $startChar <= UTF8_HANGUL_LAST ) {
662 # $tIndex = utf8ToCodepoint( $c ) - UNICODE_HANGUL_TBASE;
663 $tIndex = ord( $c{2} ) - 0xa7;
664 if( $tIndex < 0 ) $tIndex = ord( $c{2} ) - 0x80 + (0x11c0 - 0x11a7);
665
666 # Increment the code point by $tIndex, without
667 # the function overhead of decoding and recoding UTF-8
668 #
669 $tail = ord( $startChar{2} ) + $tIndex;
670 if( $tail > 0xbf ) {
671 $tail -= 0x40;
672 $mid = ord( $startChar{1} ) + 1;
673 if( $mid > 0xbf ) {
674 $startChar{0} = chr( ord( $startChar{0} ) + 1 );
675 $mid -= 0x40;
676 }
677 $startChar{1} = chr( $mid );
678 }
679 $startChar{2} = chr( $tail );
680 continue;
681 }
682 }
683 }
684 $out .= $startChar;
685 $out .= $combining;
686 $startChar = $c;
687 $combining = '';
688 $lastClass = 0;
689 }
690 $out .= $startChar . $combining;
691 return $out;
692 }
693
694 /**
695 * This is just used for the benchmark, comparing how long it takes to
696 * interate through a string without really doing anything of substance.
697 * @param string $string
698 * @return string
699 */
700 function placebo( $string ) {
701 $len = strlen( $string );
702 $out = '';
703 for( $i = 0; $i < $len; $i++ ) {
704 $out .= $string{$i};
705 }
706 return $out;
707 }
708 }
709
710 ?>