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 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
21 * Unicode normalization routines for working with UTF-8 strings.
22 * Currently assumes that input strings are valid UTF-8!
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.
28 * All functions can be called static.
30 * See description of forms at http://www.unicode.org/reports/tr15/
36 require_once dirname(__FILE__
).'/UtfNormalUtil.php';
38 global $utfCombiningClass, $utfCanonicalComp, $utfCanonicalDecomp;
39 $utfCombiningClass = NULL;
40 $utfCanonicalComp = NULL;
41 $utfCanonicalDecomp = NULL;
43 # Load compatibility decompositions on demand if they are needed.
44 global $utfCompatibilityDecomp;
45 $utfCompatibilityDecomp = NULL;
47 define( 'UNICODE_HANGUL_FIRST', 0xac00 );
48 define( 'UNICODE_HANGUL_LAST', 0xd7a3 );
50 define( 'UNICODE_HANGUL_LBASE', 0x1100 );
51 define( 'UNICODE_HANGUL_VBASE', 0x1161 );
52 define( 'UNICODE_HANGUL_TBASE', 0x11a7 );
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
);
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 );
63 define( 'UNICODE_SURROGATE_FIRST', 0xd800 );
64 define( 'UNICODE_SURROGATE_LAST', 0xdfff );
65 define( 'UNICODE_MAX', 0x10ffff );
66 define( 'UNICODE_REPLACEMENT', 0xfffd );
69 define( 'UTF8_HANGUL_FIRST', "\xea\xb0\x80" /*codepointToUtf8( UNICODE_HANGUL_FIRST )*/ );
70 define( 'UTF8_HANGUL_LAST', "\xed\x9e\xa3" /*codepointToUtf8( UNICODE_HANGUL_LAST )*/ );
72 define( 'UTF8_HANGUL_LBASE', "\xe1\x84\x80" /*codepointToUtf8( UNICODE_HANGUL_LBASE )*/ );
73 define( 'UTF8_HANGUL_VBASE', "\xe1\x85\xa1" /*codepointToUtf8( UNICODE_HANGUL_VBASE )*/ );
74 define( 'UTF8_HANGUL_TBASE', "\xe1\x86\xa7" /*codepointToUtf8( UNICODE_HANGUL_TBASE )*/ );
76 define( 'UTF8_HANGUL_LEND', "\xe1\x84\x92" /*codepointToUtf8( UNICODE_HANGUL_LEND )*/ );
77 define( 'UTF8_HANGUL_VEND', "\xe1\x85\xb5" /*codepointToUtf8( UNICODE_HANGUL_VEND )*/ );
78 define( 'UTF8_HANGUL_TEND', "\xe1\x87\x82" /*codepointToUtf8( UNICODE_HANGUL_TEND )*/ );
80 define( 'UTF8_SURROGATE_FIRST', "\xed\xa0\x80" /*codepointToUtf8( UNICODE_SURROGATE_FIRST )*/ );
81 define( 'UTF8_SURROGATE_LAST', "\xed\xbf\xbf" /*codepointToUtf8( UNICODE_SURROGATE_LAST )*/ );
82 define( 'UTF8_MAX', "\xf4\x8f\xbf\xbf" /*codepointToUtf8( UNICODE_MAX )*/ );
83 define( 'UTF8_REPLACEMENT', "\xef\xbf\xbd" /*codepointToUtf8( UNICODE_REPLACEMENT )*/ );
84 #define( 'UTF8_REPLACEMENT', '!' );
86 define( 'UTF8_OVERLONG_A', "\xc1\xbf" );
87 define( 'UTF8_OVERLONG_B', "\xe0\x9f\xbf" );
88 define( 'UTF8_OVERLONG_C', "\xf0\x8f\xbf\xbf" );
90 # These two ranges are illegal
91 define( 'UTF8_FDD0', "\xef\xb7\x90" /*codepointToUtf8( 0xfdd0 )*/ );
92 define( 'UTF8_FDEF', "\xef\xb7\xaf" /*codepointToUtf8( 0xfdef )*/ );
93 define( 'UTF8_FFFE', "\xef\xbf\xbe" /*codepointToUtf8( 0xfffe )*/ );
94 define( 'UTF8_FFFF', "\xef\xbf\xbf" /*codepointToUtf8( 0xffff )*/ );
96 define( 'UTF8_HEAD', false );
97 define( 'UTF8_TAIL', true );
101 * For using the ICU wrapper
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 );
111 define( 'NORMALIZE_ICU', function_exists( 'utf8_normalize' ) );
119 * The ultimate convenience function! Clean up invalid UTF-8 sequences,
120 * and convert to normal form C, canonical composition.
122 * Fast return for pure ASCII strings; some lesser optimizations for
123 * strings containing only known-good characters. Not as fast as toNFC().
125 * @param string $string a UTF-8 string
126 * @return string a clean, shiny, normalized UTF-8 string
129 static function cleanUp( $string ) {
130 if( NORMALIZE_ICU
) {
131 # We exclude a few chars that ICU would not.
132 $string = preg_replace(
133 '/[\x00-\x08\x0b\x0c\x0e-\x1f]/',
136 $string = str_replace( UTF8_FFFE
, UTF8_REPLACEMENT
, $string );
137 $string = str_replace( UTF8_FFFF
, UTF8_REPLACEMENT
, $string );
139 # UnicodeString constructor fails if the string ends with a
140 # head byte. Add a junk char at the end, we'll strip it off.
141 return rtrim( utf8_normalize( $string . "\x01", UNORM_NFC
), "\x01" );
142 } elseif( UtfNormal
::quickIsNFCVerify( $string ) ) {
143 # Side effect -- $string has had UTF-8 errors cleaned up.
146 return UtfNormal
::NFC( $string );
151 * Convert a UTF-8 string to normal form C, canonical composition.
152 * Fast return for pure ASCII strings; some lesser optimizations for
153 * strings containing only known-good characters.
155 * @param string $string a valid UTF-8 string. Input is not validated.
156 * @return string a UTF-8 string in normal form C
159 static function toNFC( $string ) {
161 return utf8_normalize( $string, UNORM_NFC
);
162 elseif( UtfNormal
::quickIsNFC( $string ) )
165 return UtfNormal
::NFC( $string );
169 * Convert a UTF-8 string to normal form D, canonical decomposition.
170 * Fast return for pure ASCII strings.
172 * @param string $string a valid UTF-8 string. Input is not validated.
173 * @return string a UTF-8 string in normal form D
176 static function toNFD( $string ) {
178 return utf8_normalize( $string, UNORM_NFD
);
179 elseif( preg_match( '/[\x80-\xff]/', $string ) )
180 return UtfNormal
::NFD( $string );
186 * Convert a UTF-8 string to normal form KC, compatibility composition.
187 * This may cause irreversible information loss, use judiciously.
188 * Fast return for pure ASCII strings.
190 * @param string $string a valid UTF-8 string. Input is not validated.
191 * @return string a UTF-8 string in normal form KC
194 static function toNFKC( $string ) {
196 return utf8_normalize( $string, UNORM_NFKC
);
197 elseif( preg_match( '/[\x80-\xff]/', $string ) )
198 return UtfNormal
::NFKC( $string );
204 * Convert a UTF-8 string to normal form KD, compatibility decomposition.
205 * This may cause irreversible information loss, use judiciously.
206 * Fast return for pure ASCII strings.
208 * @param string $string a valid UTF-8 string. Input is not validated.
209 * @return string a UTF-8 string in normal form KD
212 static function toNFKD( $string ) {
214 return utf8_normalize( $string, UNORM_NFKD
);
215 elseif( preg_match( '/[\x80-\xff]/', $string ) )
216 return UtfNormal
::NFKD( $string );
222 * Load the basic composition data if necessary
226 static function loadData() {
227 global $utfCombiningClass;
228 if( !isset( $utfCombiningClass ) ) {
229 require_once( 'UtfNormalData.inc' );
234 * Returns true if the string is _definitely_ in NFC.
235 * Returns false if not or uncertain.
236 * @param string $string a valid UTF-8 string. Input is not validated.
240 static function quickIsNFC( $string ) {
241 # ASCII is always valid NFC!
242 # If it's pure ASCII, let it through.
243 if( !preg_match( '/[\x80-\xff]/', $string ) ) return true;
245 UtfNormal
::loadData();
246 global $utfCheckNFC, $utfCombiningClass;
247 $len = strlen( $string );
248 for( $i = 0; $i < $len; $i++
) {
253 } elseif( $n >= 0xf0 ) {
254 $c = substr( $string, $i, 4 );
256 } elseif( $n >= 0xe0 ) {
257 $c = substr( $string, $i, 3 );
259 } elseif( $n >= 0xc0 ) {
260 $c = substr( $string, $i, 2 );
263 if( isset( $utfCheckNFC[$c] ) ) {
264 # If it's NO or MAYBE, bail and do the slow check.
267 if( isset( $utfCombiningClass[$c] ) ) {
268 # Combining character? We might have to do sorting, at least.
276 * Returns true if the string is _definitely_ in NFC.
277 * Returns false if not or uncertain.
278 * @param string $string a UTF-8 string, altered on output to be valid UTF-8 safe for XML.
281 static function quickIsNFCVerify( &$string ) {
282 # Screen out some characters that eg won't be allowed in XML
283 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', UTF8_REPLACEMENT
, $string );
285 # ASCII is always valid NFC!
286 # If we're only ever given plain ASCII, we can avoid the overhead
287 # of initializing the decomposition tables by skipping out early.
288 if( !preg_match( '/[\x80-\xff]/', $string ) ) return true;
290 static $checkit = null, $tailBytes = null, $utfCheckOrCombining = null;
291 if( !isset( $checkit ) ) {
292 # Load/build some scary lookup tables...
293 UtfNormal
::loadData();
294 global $utfCheckNFC, $utfCombiningClass;
296 $utfCheckOrCombining = array_merge( $utfCheckNFC, $utfCombiningClass );
298 # Head bytes for sequences which we should do further validity checks
299 $checkit = array_flip( array_map( 'chr',
300 array( 0xc0, 0xc1, 0xe0, 0xed, 0xef,
301 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
302 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff ) ) );
304 # Each UTF-8 head byte is followed by a certain
305 # number of tail bytes.
306 $tailBytes = array();
307 for( $n = 0; $n < 256; $n++
) {
310 } elseif( $n < 0xe0 ) {
312 } elseif( $n < 0xf0 ) {
314 } elseif( $n < 0xf8 ) {
316 } elseif( $n < 0xfc ) {
318 } elseif( $n < 0xfe ) {
323 $tailBytes[chr($n)] = $remaining;
327 # Chop the text into pure-ASCII and non-ASCII areas;
328 # large ASCII parts can be handled much more quickly.
329 # Don't chop up Unicode areas for punctuation, though,
330 # that wastes energy.
333 '/([\x00-\x7f]+|[\x80-\xff][\x00-\x40\x5b-\x5f\x7b-\xff]*)/',
339 foreach( $matches[1] as $str ) {
340 $chunk = strlen( $str );
342 if( $str{0} < "\x80" ) {
343 # ASCII chunk: guaranteed to be valid UTF-8
344 # and in normal form C, so skip over it.
349 # We'll have to examine the chunk byte by byte to ensure
350 # that it consists of valid UTF-8 sequences, and to see
351 # if any of them might not be normalized.
353 # Since PHP is not the fastest language on earth, some of
354 # this code is a little ugly with inner loop optimizations.
357 $len = $chunk +
1; # Counting down is faster. I'm *so* sorry.
359 for( $i = -1; --$len; ) {
360 if( $remaining = $tailBytes[$c = $str{++
$i}] ) {
362 $sequence = $head = $c;
364 # Look for the defined number of tail bytes...
365 if( --$len && ( $c = $str{++
$i} ) >= "\x80" && $c < "\xc0" ) {
366 # Legal tail bytes are nice.
370 # Premature end of string!
371 # Drop a replacement character into output to
372 # represent the invalid UTF-8 sequence.
373 $replace[] = array( UTF8_REPLACEMENT
,
374 $base +
$i +
1 - strlen( $sequence ),
375 strlen( $sequence ) );
378 # Illegal tail byte; abandon the sequence.
379 $replace[] = array( UTF8_REPLACEMENT
,
380 $base +
$i - strlen( $sequence ),
381 strlen( $sequence ) );
382 # Back up and reprocess this byte; it may itself
383 # be a legal ASCII or UTF-8 sequence head.
389 } while( --$remaining );
391 if( isset( $checkit[$head] ) ) {
392 # Do some more detailed validity checks, for
393 # invalid characters and illegal sequences.
394 if( $head == "\xed" ) {
395 # 0xed is relatively frequent in Korean, which
396 # abuts the surrogate area, so we're doing
397 # this check separately to speed things up.
399 if( $sequence >= UTF8_SURROGATE_FIRST
) {
400 # Surrogates are legal only in UTF-16 code.
401 # They are totally forbidden here in UTF-8
403 $replace[] = array( UTF8_REPLACEMENT
,
404 $base +
$i +
1 - strlen( $sequence ),
405 strlen( $sequence ) );
410 # Slower, but rarer checks...
413 # "Overlong sequences" are those that are syntactically
414 # correct but use more UTF-8 bytes than are necessary to
415 # encode a character. Naïve string comparisons can be
416 # tricked into failing to see a match for an ASCII
417 # character, for instance, which can be a security hole
418 # if blacklist checks are being used.
419 ($n < 0xc2 && $sequence <= UTF8_OVERLONG_A
)
420 ||
($n == 0xe0 && $sequence <= UTF8_OVERLONG_B
)
421 ||
($n == 0xf0 && $sequence <= UTF8_OVERLONG_C
)
423 # U+FFFE and U+FFFF are explicitly forbidden in Unicode.
425 ($sequence == UTF8_FFFE
)
426 ||
($sequence == UTF8_FFFF
) )
428 # Unicode has been limited to 21 bits; longer
429 # sequences are not allowed.
430 ||
($n >= 0xf0 && $sequence > UTF8_MAX
) ) {
432 $replace[] = array( UTF8_REPLACEMENT
,
433 $base +
$i +
1 - strlen( $sequence ),
434 strlen( $sequence ) );
441 if( isset( $utfCheckOrCombining[$sequence] ) ) {
442 # If it's NO or MAYBE, we'll have to rip
443 # the string apart and put it back together.
444 # That's going to be mighty slow.
445 $looksNormal = false;
448 # The sequence is legal!
450 } elseif( $c < "\x80" ) {
453 } elseif( $c < "\xc0" ) {
457 $replace[] = array( UTF8_REPLACEMENT
, $base +
$i, 1 );
459 # Don't add if we're continuing a broken sequence;
460 # we already put a replacement character when we looked
461 # at the broken sequence.
462 $replace[] = array( '', $base +
$i, 1 );
465 # Miscellaneous freaks.
466 $replace[] = array( UTF8_REPLACEMENT
, $base +
$i, 1 );
472 if( count( $replace ) ) {
473 # There were illegal UTF-8 sequences we need to fix up.
476 foreach( $replace as $rep ) {
477 list( $replacement, $start, $length ) = $rep;
478 if( $last < $start ) {
479 $out .= substr( $string, $last, $start - $last );
481 $out .= $replacement;
482 $last = $start +
$length;
484 if( $last < strlen( $string ) ) {
485 $out .= substr( $string, $last );
492 # These take a string and run the normalization on them, without
493 # checking for validity or any optimization etc. Input must be
496 * @param string $string
501 static function NFC( $string ) {
502 return UtfNormal
::fastCompose( UtfNormal
::NFD( $string ) );
506 * @param string $string
511 static function NFD( $string ) {
512 UtfNormal
::loadData();
513 global $utfCanonicalDecomp;
514 return UtfNormal
::fastCombiningSort(
515 UtfNormal
::fastDecompose( $string, $utfCanonicalDecomp ) );
519 * @param string $string
524 static function NFKC( $string ) {
525 return UtfNormal
::fastCompose( UtfNormal
::NFKD( $string ) );
529 * @param string $string
534 static function NFKD( $string ) {
535 global $utfCompatibilityDecomp;
536 if( !isset( $utfCompatibilityDecomp ) ) {
537 require_once( 'UtfNormalDataK.inc' );
539 return UtfNormal
::fastCombiningSort(
540 UtfNormal
::fastDecompose( $string, $utfCompatibilityDecomp ) );
545 * Perform decomposition of a UTF-8 string into either D or KD form
546 * (depending on which decomposition map is passed to us).
547 * Input is assumed to be *valid* UTF-8. Invalid code will break.
549 * @param string $string Valid UTF-8 string
550 * @param array $map hash of expanded decomposition map
551 * @return string a UTF-8 string decomposed, not yet normalized (needs sorting)
554 static function fastDecompose( $string, $map ) {
555 UtfNormal
::loadData();
556 $len = strlen( $string );
558 for( $i = 0; $i < $len; $i++
) {
562 # ASCII chars never decompose
566 } elseif( $n >= 0xf0 ) {
567 $c = substr( $string, $i, 4 );
569 } elseif( $n >= 0xe0 ) {
570 $c = substr( $string, $i, 3 );
572 } elseif( $n >= 0xc0 ) {
573 $c = substr( $string, $i, 2 );
576 if( isset( $map[$c] ) ) {
580 if( $c >= UTF8_HANGUL_FIRST
&& $c <= UTF8_HANGUL_LAST
) {
581 # Decompose a hangul syllable into jamo;
582 # hardcoded for three-byte UTF-8 sequence.
583 # A lookup table would be slightly faster,
584 # but adds a lot of memory & disk needs.
586 $index = ( (ord( $c{0} ) & 0x0f) << 12
587 |
(ord( $c{1} ) & 0x3f) << 6
588 |
(ord( $c{2} ) & 0x3f) )
589 - UNICODE_HANGUL_FIRST
;
590 $l = intval( $index / UNICODE_HANGUL_NCOUNT
);
591 $v = intval( ($index % UNICODE_HANGUL_NCOUNT
) / UNICODE_HANGUL_TCOUNT
);
592 $t = $index % UNICODE_HANGUL_TCOUNT
;
593 $out .= "\xe1\x84" . chr( 0x80 +
$l ) . "\xe1\x85" . chr( 0xa1 +
$v );
595 $out .= "\xe1\x87" . chr( 0x80 +
$t - 25 );
597 $out .= "\xe1\x86" . chr( 0xa7 +
$t );
608 * Sorts combining characters into canonical order. This is the
609 * final step in creating decomposed normal forms D and KD.
611 * @param string $string a valid, decomposed UTF-8 string. Input is not validated.
612 * @return string a UTF-8 string with combining characters sorted in canonical order
615 static function fastCombiningSort( $string ) {
616 UtfNormal
::loadData();
617 global $utfCombiningClass;
618 $len = strlen( $string );
620 $combiners = array();
622 for( $i = 0; $i < $len; $i++
) {
627 $c = substr( $string, $i, 4 );
629 } elseif( $n >= 0xe0 ) {
630 $c = substr( $string, $i, 3 );
632 } elseif( $n >= 0xc0 ) {
633 $c = substr( $string, $i, 2 );
636 if( isset( $utfCombiningClass[$c] ) ) {
637 $lastClass = $utfCombiningClass[$c];
638 @$combiners[$lastClass] .= $c;
644 $out .= implode( '', $combiners );
645 $combiners = array();
652 $out .= implode( '', $combiners );
658 * Produces canonically composed sequences, i.e. normal form C or KC.
661 * @param string $string a valid UTF-8 string in sorted normal form D or KD. Input is not validated.
662 * @return string a UTF-8 string with canonical precomposed characters used where possible
665 static function fastCompose( $string ) {
666 UtfNormal
::loadData();
667 global $utfCanonicalComp, $utfCombiningClass;
668 $len = strlen( $string );
674 $x1 = ord(substr(UTF8_HANGUL_VBASE
,0,1));
675 $x2 = ord(substr(UTF8_HANGUL_TEND
,0,1));
676 for( $i = 0; $i < $len; $i++
) {
680 # No combining characters here...
687 } elseif( $n >= 0xf0 ) {
688 $c = substr( $string, $i, 4 );
690 } elseif( $n >= 0xe0 ) {
691 $c = substr( $string, $i, 3 );
693 } elseif( $n >= 0xc0 ) {
694 $c = substr( $string, $i, 2 );
697 $pair = $startChar . $c;
699 if( isset( $utfCombiningClass[$c] ) ) {
700 # A combining char; see what we can do with it
701 $class = $utfCombiningClass[$c];
702 if( !empty( $startChar ) &&
703 $lastClass < $class &&
705 isset( $utfCanonicalComp[$pair] ) ) {
706 $startChar = $utfCanonicalComp[$pair];
717 if( $lastClass == 0 ) {
718 if( isset( $utfCanonicalComp[$pair] ) ) {
719 $startChar = $utfCanonicalComp[$pair];
723 if( $n >= $x1 && $n <= $x2 ) {
724 # WARNING: Hangul code is painfully slow.
725 # I apologize for this ugly, ugly code; however
726 # performance is even more teh suck if we call
727 # out to nice clean functions. Lookup tables are
728 # marginally faster, but require a lot of space.
730 if( $c >= UTF8_HANGUL_VBASE
&&
731 $c <= UTF8_HANGUL_VEND
&&
732 $startChar >= UTF8_HANGUL_LBASE
&&
733 $startChar <= UTF8_HANGUL_LEND
) {
735 #$lIndex = utf8ToCodepoint( $startChar ) - UNICODE_HANGUL_LBASE;
736 #$vIndex = utf8ToCodepoint( $c ) - UNICODE_HANGUL_VBASE;
737 $lIndex = ord( $startChar{2} ) - 0x80;
738 $vIndex = ord( $c{2} ) - 0xa1;
740 $hangulPoint = UNICODE_HANGUL_FIRST +
741 UNICODE_HANGUL_TCOUNT
*
742 (UNICODE_HANGUL_VCOUNT
* $lIndex +
$vIndex);
744 # Hardcode the limited-range UTF-8 conversion:
745 $startChar = chr( $hangulPoint >> 12 & 0x0f |
0xe0 ) .
746 chr( $hangulPoint >> 6 & 0x3f |
0x80 ) .
747 chr( $hangulPoint & 0x3f |
0x80 );
750 } elseif( $c >= UTF8_HANGUL_TBASE
&&
751 $c <= UTF8_HANGUL_TEND
&&
752 $startChar >= UTF8_HANGUL_FIRST
&&
753 $startChar <= UTF8_HANGUL_LAST
&&
755 # $tIndex = utf8ToCodepoint( $c ) - UNICODE_HANGUL_TBASE;
756 $tIndex = ord( $c{2} ) - 0xa7;
757 if( $tIndex < 0 ) $tIndex = ord( $c{2} ) - 0x80 +
(0x11c0 - 0x11a7);
759 # Increment the code point by $tIndex, without
760 # the function overhead of decoding and recoding UTF-8
762 $tail = ord( $startChar{2} ) +
$tIndex;
765 $mid = ord( $startChar{1} ) +
1;
767 $startChar{0} = chr( ord( $startChar{0} ) +
1 );
770 $startChar{1} = chr( $mid );
772 $startChar{2} = chr( $tail );
774 # If there's another jamo char after this, *don't* try to merge it.
787 $out .= $startChar . $combining;
792 * This is just used for the benchmark, comparing how long it takes to
793 * interate through a string without really doing anything of substance.
794 * @param string $string
798 static function placebo( $string ) {
799 $len = strlen( $string );
801 for( $i = 0; $i < $len; $i++
) {