The pass-by-reference on the string on fastCompose() really slows things down sometim...
[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 * @return bool
259 */
260 function quickIsNFCVerify( &$string ) {
261 # ASCII is always valid NFC!
262 if( !preg_match( '/[\x80-\xff]/', $string ) ) return true;
263
264 UtfNormal::loadData();
265 global $utfCheckNFC, $utfCombiningClass;
266 $len = strlen( $string );
267 $out = '';
268 $state = UTF8_HEAD;
269 $looksNormal = true;
270
271 $rep = false;
272 $head = 0;
273 for( $i = 0; $i < $len; $i++ ) {
274 $c = $string{$i};
275 $n = ord( $c );
276 if( $state == UTF8_TAIL ) {
277 if( $n >= 0x80 && $n < 0xc0 ) {
278 $sequence .= $c;
279 if( --$remaining == 0 ) {
280 if( $head < 0xc2 || $head == 0xed || $head == 0xe0 || $head > 0xee ) {
281 if( ( $sequence >= UTF8_SURROGATE_FIRST
282 && $sequence <= UTF8_SURROGATE_LAST)
283 || ($head == 0xc0 && $sequence <= UTF8_OVERLONG_A)
284 || ($head == 0xc1 && $sequence <= UTF8_OVERLONG_A)
285 || ($head == 0xe0 && $sequence <= UTF8_OVERLONG_B)
286 || ($head == 0xef &&
287 ($sequence >= UTF8_FDD0 && $sequence <= UTF8_FDEF)
288 || ($sequence == UTF8_FFFE)
289 || ($sequence == UTF8_FFFF) )
290 || ($head == 0xf0 && $sequence <= UTF8_OVERLONG_C)
291 || ($sequence > UTF8_MAX) ) {
292 $out .= UTF8_REPLACEMENT;
293 $state = UTF8_HEAD;
294 continue;
295 }
296 }
297 if( isset( $utfCheckNFC[$sequence] ) ||
298 isset( $utfCombiningClass[$sequence] ) ) {
299 # If it's NO or MAYBE, we'll have to do the slow check.
300 $looksNormal = false;
301 }
302 $out .= $sequence;
303 $state = UTF8_HEAD;
304 $head = 0;
305 }
306 continue;
307 }
308 # Not a valid tail byte! DIscard the char we've been building.
309 #printf ("Invalid '%x' in tail with %d remaining bytes\n", $n, $remaining );
310 $state = UTF8_HEAD;
311 $out .= UTF8_REPLACEMENT;
312 }
313 if( $n < 0x20 ) {
314 if( $n < 0x09 ) {
315 $out .= UTF8_REPLACEMENT;
316 } elseif( $n == 0x0a ) {
317 $out .= $c;
318 } elseif( $n < 0x0d ) {
319 $out .= UTF8_REPLACEMENT;
320 } elseif( $n == 0x0d ) {
321 # Strip \r silently
322 } else {
323 $out .= UTF8_REPLACEMENT;
324 }
325 } elseif( $n < 0x80 ) {
326 # Friendly ASCII chars.
327 $out .= $c;
328 } elseif( $n < 0xc0 ) {
329 # illegal tail bytes or head byte of overlong sequence
330 if( $head == 0 ) $out .= UTF8_REPLACEMENT;
331 } elseif( $n < 0xe0 ) {
332 $state = UTF8_TAIL;
333 $remaining = 1;
334 $sequence = $c;
335 $head = $n;
336 } elseif( $n < 0xf0 ) {
337 $state = UTF8_TAIL;
338 $remaining = 2;
339 $sequence = $c;
340 $head = $n;
341 } elseif( $n < 0xf8 ) {
342 $state = UTF8_TAIL;
343 $remaining = 3;
344 $sequence = $c;
345 $head = $n;
346 } elseif( $n < 0xfc ) {
347 $state = UTF8_TAIL;
348 $remaining = 4;
349 $sequence = $c;
350 $head = $n;
351 } elseif( $n < 0xfe ) {
352 $state = UTF8_TAIL;
353 $remaining = 5;
354 $sequence = $c;
355 $head = $n;
356 } else {
357 $out .= UTF8_REPLACEMENT;
358 }
359 }
360 if( $state == UTF8_TAIL ) {
361 $out .= UTF8_REPLACEMENT;
362 }
363 $string = $out;
364 return $looksNormal;
365 }
366
367 # These take a string and run the normalization on them, without
368 # checking for validity or any optimization etc. Input must be
369 # VALID UTF-8!
370 /**
371 * @param string $string
372 * @return string
373 * @access private
374 */
375 function NFC( $string ) {
376 return UtfNormal::fastCompose( UtfNormal::NFD( $string ) );
377 }
378
379 /**
380 * @param string $string
381 * @return string
382 * @access private
383 */
384 function NFD( $string ) {
385 UtfNormal::loadData();
386 global $utfCanonicalDecomp;
387 return UtfNormal::fastCombiningSort(
388 UtfNormal::fastDecompose( $string, $utfCanonicalDecomp ) );
389 }
390
391 /**
392 * @param string $string
393 * @return string
394 * @access private
395 */
396 function NFKC( $string ) {
397 return UtfNormal::fastCompose( UtfNormal::NFKD( $string ) );
398 }
399
400 /**
401 * @param string $string
402 * @return string
403 * @access private
404 */
405 function NFKD( $string ) {
406 global $utfCompatibilityDecomp;
407 if( !isset( $utfCompatibilityDecomp ) ) {
408 require_once( 'UtfNormalDataK.inc' );
409 }
410 return UtfNormal::fastCombiningSort(
411 UtfNormal::fastDecompose( $string, $utfCompatibilityDecomp ) );
412 }
413
414
415 /**
416 * Perform decomposition of a UTF-8 string into either D or KD form
417 * (depending on which decomposition map is passed to us).
418 * Input is assumed to be *valid* UTF-8. Invalid code will break.
419 * @access private
420 * @param string $string Valid UTF-8 string
421 * @param array $map hash of expanded decomposition map
422 * @return string a UTF-8 string decomposed, not yet normalized (needs sorting)
423 */
424 function fastDecompose( $string, &$map ) {
425 UtfNormal::loadData();
426 $len = strlen( $string );
427 $out = '';
428 for( $i = 0; $i < $len; $i++ ) {
429 $c = $string{$i};
430 $n = ord( $c );
431 if( $n < 0x80 ) {
432 # ASCII chars never decompose
433 # THEY ARE IMMORTAL
434 $out .= $c;
435 continue;
436 } elseif( $n >= 0xf0 ) {
437 $c = substr( $string, $i, 4 );
438 $i += 3;
439 } elseif( $n >= 0xe0 ) {
440 $c = substr( $string, $i, 3 );
441 $i += 2;
442 } elseif( $n >= 0xc0 ) {
443 $c = substr( $string, $i, 2 );
444 $i++;
445 }
446 if( isset( $map[$c] ) ) {
447 $out .= $map[$c];
448 continue;
449 } else {
450 if( $c >= UTF8_HANGUL_FIRST && $c <= UTF8_HANGUL_LAST ) {
451 # Decompose a hangul syllable into jamo;
452 # hardcoded for three-byte UTF-8 sequence.
453 # A lookup table would be slightly faster,
454 # but adds a lot of memory & disk needs.
455 #
456 $index = ( (ord( $c{0} ) & 0x0f) << 12
457 | (ord( $c{1} ) & 0x3f) << 6
458 | (ord( $c{2} ) & 0x3f) )
459 - UNICODE_HANGUL_FIRST;
460 $l = IntVal( $index / UNICODE_HANGUL_NCOUNT );
461 $v = IntVal( ($index % UNICODE_HANGUL_NCOUNT) / UNICODE_HANGUL_TCOUNT);
462 $t = $index % UNICODE_HANGUL_TCOUNT;
463 $out .= "\xe1\x84" . chr( 0x80 + $l ) . "\xe1\x85" . chr( 0xa1 + $v );
464 if( $t >= 25 ) {
465 $out .= "\xe1\x87" . chr( 0x80 + $t - 25 );
466 } elseif( $t ) {
467 $out .= "\xe1\x86" . chr( 0xa7 + $t );
468 }
469 continue;
470 }
471 }
472 $out .= $c;
473 }
474 return $out;
475 }
476
477 /**
478 * Sorts combining characters into canonical order. This is the
479 * final step in creating decomposed normal forms D and KD.
480 * @access private
481 * @param string $string a valid, decomposed UTF-8 string. Input is not validated.
482 * @return string a UTF-8 string with combining characters sorted in canonical order
483 */
484 function fastCombiningSort( $string ) {
485 UtfNormal::loadData();
486 global $utfCombiningClass;
487 $len = strlen( $string );
488 $out = '';
489 $combiners = array();
490 $lastClass = -1;
491 for( $i = 0; $i < $len; $i++ ) {
492 $c = $string{$i};
493 $n = ord( $c );
494 if( $n >= 0x80 ) {
495 if( $n >= 0xf0 ) {
496 $c = substr( $string, $i, 4 );
497 $i += 3;
498 } elseif( $n >= 0xe0 ) {
499 $c = substr( $string, $i, 3 );
500 $i += 2;
501 } elseif( $n >= 0xc0 ) {
502 $c = substr( $string, $i, 2 );
503 $i++;
504 }
505 if( isset( $utfCombiningClass[$c] ) ) {
506 $lastClass = $utfCombiningClass[$c];
507 @$combiners[$lastClass] .= $c;
508 continue;
509 }
510 }
511 if( $lastClass ) {
512 ksort( $combiners );
513 $out .= implode( '', $combiners );
514 $combiners = array();
515 }
516 $out .= $c;
517 $lastClass = 0;
518 }
519 if( $lastClass ) {
520 ksort( $combiners );
521 $out .= implode( '', $combiners );
522 }
523 return $out;
524 }
525
526 /**
527 * Produces canonically composed sequences, i.e. normal form C or KC.
528 *
529 * @access private
530 * @param string $string a valid UTF-8 string in sorted normal form D or KD. Input is not validated.
531 * @return string a UTF-8 string with canonical precomposed characters used where possible
532 */
533 function fastCompose( $string ) {
534 UtfNormal::loadData();
535 global $utfCanonicalComp, $utfCombiningClass;
536 $len = strlen( $string );
537 $out = '';
538 $lastClass = -1;
539 $startChar = '';
540 $combining = '';
541 $x1 = ord(substr(UTF8_HANGUL_VBASE,0,1));
542 $x2 = ord(substr(UTF8_HANGUL_TEND,0,1));
543 for( $i = 0; $i < $len; $i++ ) {
544 $c = $string{$i};
545 $n = ord( $c );
546 if( $n < 0x80 ) {
547 # No combining characters here...
548 $out .= $startChar;
549 $out .= $combining;
550 $startChar = $c;
551 $combining = '';
552 $lastClass = 0;
553 continue;
554 } elseif( $n >= 0xf0 ) {
555 $c = substr( $string, $i, 4 );
556 $i += 3;
557 } elseif( $n >= 0xe0 ) {
558 $c = substr( $string, $i, 3 );
559 $i += 2;
560 } elseif( $n >= 0xc0 ) {
561 $c = substr( $string, $i, 2 );
562 $i++;
563 }
564 $pair = $startChar . $c;
565 if( $n > 0x80 ) {
566 if( isset( $utfCombiningClass[$c] ) ) {
567 # A combining char; see what we can do with it
568 $class = $utfCombiningClass[$c];
569 if( !empty( $startChar ) &&
570 $lastClass < $class &&
571 $class > 0 &&
572 isset( $utfCanonicalComp[$pair] ) ) {
573 $startChar = $utfCanonicalComp[$pair];
574 $class = 0;
575 } else {
576 $combining .= $c;
577 }
578 $lastClass = $class;
579 continue;
580 }
581 }
582 # New start char
583 if( $lastClass == 0 ) {
584 if( isset( $utfCanonicalComp[$pair] ) ) {
585 $startChar = $utfCanonicalComp[$pair];
586 continue;
587 }
588 if( $n >= $x1 && $n <= $x2 ) {
589 # WARNING: Hangul code is painfully slow.
590 # I apologize for this ugly, ugly code; however
591 # performance is even more teh suck if we call
592 # out to nice clean functions. Lookup tables are
593 # marginally faster, but require a lot of space.
594 #
595 if( $c >= UTF8_HANGUL_VBASE &&
596 $c <= UTF8_HANGUL_VEND &&
597 $startChar >= UTF8_HANGUL_LBASE &&
598 $startChar <= UTF8_HANGUL_LEND ) {
599 #
600 #$lIndex = utf8ToCodepoint( $startChar ) - UNICODE_HANGUL_LBASE;
601 #$vIndex = utf8ToCodepoint( $c ) - UNICODE_HANGUL_VBASE;
602 $lIndex = ord( $startChar{2} ) - 0x80;
603 $vIndex = ord( $c{2} ) - 0xa1;
604
605 $hangulPoint = UNICODE_HANGUL_FIRST +
606 UNICODE_HANGUL_TCOUNT *
607 (UNICODE_HANGUL_VCOUNT * $lIndex + $vIndex);
608
609 # Hardcode the limited-range UTF-8 conversion:
610 $startChar = chr( $hangulPoint >> 12 & 0x0f | 0xe0 ) .
611 chr( $hangulPoint >> 6 & 0x3f | 0x80 ) .
612 chr( $hangulPoint & 0x3f | 0x80 );
613 continue;
614 } elseif( $c >= UTF8_HANGUL_TBASE &&
615 $c <= UTF8_HANGUL_TEND &&
616 $startChar >= UTF8_HANGUL_FIRST &&
617 $startChar <= UTF8_HANGUL_LAST ) {
618 # $tIndex = utf8ToCodepoint( $c ) - UNICODE_HANGUL_TBASE;
619 $tIndex = ord( $c{2} ) - 0xa7;
620 if( $tIndex < 0 ) $tIndex = ord( $c{2} ) - 0x80 + (0x11c0 - 0x11a7);
621
622 # Increment the code point by $tIndex, without
623 # the function overhead of decoding and recoding UTF-8
624 #
625 $tail = ord( $startChar{2} ) + $tIndex;
626 if( $tail > 0xbf ) {
627 $tail -= 0x40;
628 $mid = ord( $startChar{1} ) + 1;
629 if( $mid > 0xbf ) {
630 $startChar{0} = chr( ord( $startChar{0} ) + 1 );
631 $mid -= 0x40;
632 }
633 $startChar{1} = chr( $mid );
634 }
635 $startChar{2} = chr( $tail );
636 continue;
637 }
638 }
639 }
640 $out .= $startChar;
641 $out .= $combining;
642 $startChar = $c;
643 $combining = '';
644 $lastClass = 0;
645 }
646 $out .= $startChar . $combining;
647 return $out;
648 }
649
650 /**
651 * This is just used for the benchmark, comparing how long it takes to
652 * interate through a string without really doing anything of substance.
653 * @param string $string
654 * @return string
655 */
656 function placebo( $string ) {
657 $len = strlen( $string );
658 $out = '';
659 for( $i = 0; $i < $len; $i++ ) {
660 $out .= $string{$i};
661 }
662 return $out;
663 }
664 }
665
666 ?>