forked from Zodiac1978/tl-normalizer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBaseNormalizer.php
More file actions
638 lines (534 loc) · 24.7 KB
/
BaseNormalizer.php
File metadata and controls
638 lines (534 loc) · 24.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
// gitlost removed namespace stuff, renamed to UNFC_Normalizer (UNFC_BaseNormalizer) to avoid conflicts.
// gitlost use unfc_is_valid_utf8() and generated regex alternatives, added full isNormalized() check.
// gitlost made PHP 7.3 compat by implementing NFKC_CF and getRawDecomposition().
// namespace Symfony\Polyfill\Intl\Normalizer; // gitlost
/**
* UNFC_BaseNormalizer is the parent and main implementation of the UNFC_Normalizer polyfill class.
*
* It has been validated with Unicode 12.1.0 Normalization Conformance Test. // gitlost
* See http://www.unicode.org/reports/tr15/ for detailed info about Unicode normalizations.
*
* @author Nicolas Grekas <p@tchwork.com>
*
* @internal
*/
// gitlost begin
// To test UTF-8 validity, use PCRE UTF-8 mode if available and RFC 3629 compliant, or htmlspecialchars() if RFC 3629 compliant, or as a last resort one of the following regexs.
// NOTE: The more natural regexs checking positively for validity unfortunately blow up on large (~20K) strings due to unnecessary recursion in PCRE < 8.13.
define('UNFC_REGEX_IS_INVALID_UTF8', // Using (*SKIP) verbs doubles the speed, but verbs only available for PCRE >= 7.3.
'/
(?> [\xc2-\xdf] (?: [^\x80-\xbf] | .(*SKIP)[\x80-\xbf] | \z ) )
| (?> [\xe0-\xef] (?: [^\x80-\xbf] | (?<= \xe0 ) [\x80-\x9f] | (?<= \xed ) [\xa0-\xbf] | .(*SKIP) (?: [^\x80-\xbf] | .(*SKIP)[\x80-\xbf] | \z ) | \z ) )
| (?> [\xf0-\xf4] (?: [^\x80-\xbf] | (?<= \xf0 ) [\x80-\x8f] | (?<= \xf4 ) [\x90-\xbf] | .(*SKIP) (?: [^\x80-\xbf] | .(*SKIP) (?: [^\x80-\xbf] | .(*SKIP)[\x80-\xbf] | \z ) | \z ) | \z ) )
| [\x80-\xc1\xf5-\xff] (?<= [\x00-\x7f]. | \A. )
| [\xc0\xc1\xf5-\xff]
/sx'
);
define('UNFC_REGEX_IS_INVALID_UTF8_NOVERBS',
'/
(?> [\xc2-\xdf] (?: [^\x80-\xbf] | .[\x80-\xbf] | \z ) )
| (?> [\xe0-\xef] (?: [^\x80-\xbf] | (?<= \xe0 ) [\x80-\x9f] | (?<= \xed ) [\xa0-\xbf] | . (?: [^\x80-\xbf] | .[\x80-\xbf] | \z ) | \z ) )
| (?> [\xf0-\xf4] (?: [^\x80-\xbf] | (?<= \xf0 ) [\x80-\x8f] | (?<= \xf4 ) [\x90-\xbf] | . (?: [^\x80-\xbf] | . (?: [^\x80-\xbf] | .[\x80-\xbf] | \z ) | \z ) | \z ) )
| [\x80-\xc1\xf5-\xff] (?<= [\x00-\x7f]. | \A. )
| [\xc0\xc1\xf5-\xff]
/sx'
);
// PCRE UTF-8 mode was not RFC 3629 compliant until PCRE 7.3, and then there was a compliance regression for PCRE 8.32 due to an over-enthusiastic interpretation of noncharacters.
// See https://www.ietf.org/rfc/rfc3629.txt
// See http://vcs.pcre.org/pcre/code/tags/pcre-8.32/pcre_valid_utf8.c?r1=1032&r2=1098 for the regression.
// See http://www.unicode.org/versions/corrigendum9.html for the clarification.
$_unfc_pcre_version = substr(PCRE_VERSION, 0, strspn(PCRE_VERSION, '0123456789.')); // Remove any trailing date stuff.
// If PCRE UTF-8 mode is not RFC 3629 compliant or is unavailable...
if (version_compare($_unfc_pcre_version, '7.3', '<') || version_compare($_unfc_pcre_version, '8.32', '=') || false === @preg_match('//u', '')) {
// If before htmlspecialchars() RFC 3629 compliance...
if (version_compare(PHP_VERSION, '5.3.4', '<')) {
// If verbs unavailable...
if (version_compare($_unfc_pcre_version, '7.3', '<')) {
// Typically PHP 5.2.4 only (with or without PCRE UTF-8 mode).
function unfc_is_valid_utf8($str) {
return 0 === preg_match(UNFC_REGEX_IS_INVALID_UTF8_NOVERBS, $str); // Very slow for PHP < 7.
}
} else {
// Typically when PCRE UTF-8 mode unavailable and PHP < 5.3.4, ie 5.2.5 to 5.2.17 (last), and 5.3.0 to 5.3.3.
function unfc_is_valid_utf8($str) {
return 0 === preg_match(UNFC_REGEX_IS_INVALID_UTF8, $str); // Very slow for PHP < 7.
}
}
} else {
// Typically when PCRE UTF-8 mode unavailable and PHP >= 5.3.4; or when built against PCRE 8.32, ie PHP 5.3.24 to 5.3.29 (last), 5.4.14 to 5.4.40, and 5.5.0 to 5.5.9.
function unfc_is_valid_utf8($str) {
// See https://core.trac.wordpress.org/ticket/29717#comment:11
return '' === $str || '' !== htmlspecialchars($str, ENT_NOQUOTES, 'UTF-8'); // Fast but not as fast as PCRE UTF-8 mode.
}
}
} else {
// Typically all PHPs with PCRE UTF-8 mode available except 5.2.4 and those built against PCRE 8.32.
function unfc_is_valid_utf8($str) {
return 1 === preg_match('//u', $str); // Fastest. Original Normalizer validity check.
}
}
unset($_unfc_pcre_version);
if (!defined('UNFC_REGEX_ALTS_NFC_NOES')) {
require dirname(__FILE__) . '/unfc_regex_alts.php';
}
// (Possibly) unstable code point(s) (or end of string) preceded by a stable code point (or start of string). See http://unicode.org/reports/tr15/#Stable_Code_Points
define('UNFC_REGEX_NFC_SUBNORMALIZE', '/(?:\A|[\x00-\x7f]|(?:[\xc2-\xdf]|(?:[\xe0-\xef]|[\xf0-\xf4].).).)(?:(?:' . UNFC_REGEX_ALTS_NFC_NOES_MAYBES_REORDERS . ')++|\z)/');
// gitlost end
class UNFC_BaseNormalizer // gitlost
{
const NFD = UNFC_Normalizer::FORM_D;
const NFKD = UNFC_Normalizer::FORM_KD;
const NFC = UNFC_Normalizer::FORM_C;
const NFKC = UNFC_Normalizer::FORM_KC;
const NFKC_CF = UNFC_Normalizer::FORM_KC_CF;
private static $C;
private static $D;
private static $KD;
private static $cC;
private static $kcCF; // gitlost Array loaded from "kcCaseFolding.php" for NFKC_CF.
private static $RD; // gitlost Array loaded from "rawDecomposition.php".
private static $ulenMask = array(0 => 1, 0x10 => 1, 0x20 => 1, 0x30 => 1, 0x40 => 1, 0x50 => 1, 0x60 => 1, 0x70 => 1, 0xC0 => 2, 0xD0 => 2, 0xE0 => 3, 0xF0 => 4); // gitlost Use for ASCII as well.
private static $ASCII = "\x20\x65\x69\x61\x73\x6E\x74\x72\x6F\x6C\x75\x64\x5D\x5B\x63\x6D\x70\x27\x0A\x67\x7C\x68\x76\x2E\x66\x62\x2C\x3A\x3D\x2D\x71\x31\x30\x43\x32\x2A\x79\x78\x29\x28\x4C\x39\x41\x53\x2F\x50\x22\x45\x6A\x4D\x49\x6B\x33\x3E\x35\x54\x3C\x44\x34\x7D\x42\x7B\x38\x46\x77\x52\x36\x37\x55\x47\x4E\x3B\x4A\x7A\x56\x23\x48\x4F\x57\x5F\x26\x21\x4B\x3F\x58\x51\x25\x59\x5C\x09\x5A\x2B\x7E\x5E\x24\x40\x60\x7F\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F";
protected static $s = null, $form = null, $normalize = null; // gitlost Cache various info discovered in isNormalized().
protected static $mb_overload_string = null; // gitlost Set if mbstring extension loaded with string function overload set.
protected static $forms = array(self::NFD => array(), self::NFKD => array('K' => true), self::NFC => array('C' => true), self::NFKC => array('C' => true, 'K' => true), self::NFKC_CF => array('C' => true, 'K' => true, 'kcCF' => true));
public static function isNormalized($s, $form = self::NFC)
{
// gitlost begin
if (!is_string($s) && null !== $s && !is_scalar($s) && !(is_object($s) && method_exists($s, '__toString'))) {
trigger_error('UNFC_Normalizer::'.__FUNCTION__.'() expects parameter 1 to be string, '.self::getArgType($s).' given', PHP_VERSION_ID >= 80000 ? E_USER_ERROR : E_USER_WARNING);
return false;
}
if (!is_int($form) && (!is_numeric($form) || PHP_INT_MAX <= $form || ~PHP_INT_MAX >= $form)) {
trigger_error('UNFC_Normalizer::'.__FUNCTION__.'() expects parameter 2 to be int, '.self::getArgType($form).' given', PHP_VERSION_ID >= 80000 ? E_USER_ERROR : E_USER_WARNING);
return false;
}
$form = (int) $form;
if (!isset(self::$forms[$form])) {
return false;
}
$s = (string) $s;
if (self::NFKC_CF !== $form && !isset($s[strspn($s, self::$ASCII)])) {
return true;
}
if (self::NFC === $form) {
if (!unfc_is_valid_utf8($s)) {
return false;
}
if (0 === preg_match('/[\xcc-\xf0]/', $s) || 0 === preg_match(UNFC_REGEX_NFC_NOES_MAYBES_REORDERS, $s)) { // If contains no characters that could possibly need normalizing...
return true;
}
if (null === self::$D) {
self::$D = self::getData('canonicalDecomposition');
self::$cC = self::getData('combiningClass');
}
if (null === self::$C) {
self::$C = self::getData('canonicalComposition');
}
if (null === self::$mb_overload_string) {
self::$mb_overload_string = defined('MB_OVERLOAD_STRING') && (ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING);
}
if (self::$mb_overload_string) {
$mbEncoding = mb_internal_encoding();
mb_internal_encoding('8bit');
}
// Using this method is faster where percentage of normalization candidates < 30%, and significantly faster for < 5%. It's slower where percentage > 40%.
$normalize = preg_replace_callback(UNFC_REGEX_NFC_SUBNORMALIZE, 'UNFC_BaseNormalizer::subnormalize', $s);
if (self::$mb_overload_string) {
mb_internal_encoding($mbEncoding);
}
} else {
$normalize = self::normalize($s, $form); // Give true answer by doing full normalize check.
}
$result = ($s === $normalize);
if ($result) {
self::$s = self::$form = self::$normalize = null; // Clear cache.
} else {
// Note assuming use of "if !isNormalized() normalize()" pattern.
self::$s = $s; self::$form = $form; self::$normalize = $normalize; // Cache for immediate use in normalize().
}
return $result;
// gitlost end
}
public static function normalize($s, $form = self::NFC)
{
// gitlost begin
if (!is_string($s) && null !== $s && !is_scalar($s) && !(is_object($s) && method_exists($s, '__toString'))) {
trigger_error('UNFC_Normalizer::'.__FUNCTION__.'() expects parameter 1 to be string, '.self::getArgType($s).' given', PHP_VERSION_ID >= 80000 ? E_USER_ERROR : E_USER_WARNING);
return false;
}
if (!is_int($form) && (!is_numeric($form) || PHP_INT_MAX <= ($form += 0) || ~PHP_INT_MAX >= $form)) {
trigger_error('UNFC_Normalizer::'.__FUNCTION__.'() expects parameter 2 to be int, '.self::getArgType($form).' given', PHP_VERSION_ID >= 80000 ? E_USER_ERROR : E_USER_WARNING);
return false;
}
$s = (string) $s;
$form = (int) $form;
if (null !== self::$normalize) {
if ($s === self::$s && $form === self::$form) { // Use cache if available.
$result = self::$normalize;
self::$s = self::$form = self::$normalize = null; // Clear cache (try to keep memory usage to a min).
return $result;
}
self::$s = self::$form = self::$normalize = null; // Clear cache.
}
if (UNFC_Normalizer::NONE === $form) {
return unfc_is_valid_utf8($s) ? $s : false; // Note must still check validity.
}
if (!isset(self::$forms[$form])) {
return false;
}
if ('' === $s) {
return '';
}
if (!unfc_is_valid_utf8($s)) {
return false;
}
$C = $K = $kcCF = false;
extract(self::$forms[$form]);
// gitlost end
if ($K && null === self::$KD) {
self::$KD = self::getData('compatibilityDecomposition');
}
if (null === self::$D) {
self::$D = self::getData('canonicalDecomposition');
self::$cC = self::getData('combiningClass');
}
if (null === self::$mb_overload_string) { // gitlost
self::$mb_overload_string = defined('MB_OVERLOAD_STRING') && (ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING); // gitlost
} // gitlost
if (self::$mb_overload_string) { // gitlost
$mbEncoding = mb_internal_encoding(); // gitlost
mb_internal_encoding('8bit');
}
// gitlost begin
if ($kcCF) {
if (null === self::$kcCF) {
self::$kcCF = self::getData('kcCaseFolding');
}
$s = self::kcCasefold($s);
if ('' === $s) {
return '';
}
}
// gitlost end
$r = self::decompose($s, $K);
if ($C) {
if (null === self::$C) {
self::$C = self::getData('canonicalComposition');
}
$r = self::recompose($r);
}
if (self::$mb_overload_string) { // gitlost
mb_internal_encoding($mbEncoding);
}
return $r;
}
// gitlost begin
public static function getRawDecomposition($s, $form = self::NFC)
{
if (!is_string($s) && null !== $s && !is_scalar($s) && !(is_object($s) && method_exists($s, '__toString'))) {
trigger_error('UNFC_Normalizer::'.__FUNCTION__.'() expects parameter 1 to be string, '.self::getArgType($s).' given', PHP_VERSION_ID >= 80000 ? E_USER_ERROR : E_USER_WARNING);
return null; // Note differs from isNormalized() and normalize().
}
if (!is_int($form) && (!is_numeric($form) || PHP_INT_MAX <= ($form += 0) || ~PHP_INT_MAX >= $form)) {
trigger_error('UNFC_Normalizer::'.__FUNCTION__.'() expects parameter 2 to be int, '.self::getArgType($form).' given', PHP_VERSION_ID >= 80000 ? E_USER_ERROR : E_USER_WARNING);
return null; // Note differs from isNormalized() and normalize().
}
$s = (string) $s;
if ('' === $s || !unfc_is_valid_utf8($s) || isset($s[self::$ulenMask[ord($s[0]) & 0xF0]])) { // Single UTF-8 char only.
return null;
}
$form = (int) $form;
if (!isset(self::$forms[$form])) {
return ''; // Undocumented and probably unintended but matches PHP behaviour as of 7.3.5.
}
if ($s < "\xEA\xB0\x80" || "\xED\x9E\xA3" < $s) { // If not Hangul.
if (self::NFKC_CF === $form) {
if (null === self::$kcCF) {
self::$kcCF = self::getData('kcCaseFolding');
}
if (isset(self::$kcCF[$s])) {
return self::$kcCF[$s];
}
if ("\xf3\xa0\x80\x80" <= $s && $s <= "\xf3\xa0\xbf\xbf") { // U+E0000..E0FFF go to zero-length string, left out of mapping array to lessen size.
return '';
}
}
if (null === self::$RD) {
self::$RD = self::getData('rawDecomposition');
}
if (!isset(self::$RD[$s])) {
return null;
}
if (is_array(self::$RD[$s])) { // Canonical.
return self::$RD[$s][0];
}
if (self::NFC === $form || self::NFD === $form) {
return null;
}
return self::$RD[$s]; // Compatibility.
}
// Hangul chars
$uchr = unpack('C*', $s);
$j = (($uchr[1] - 224) << 12) + (($uchr[2] - 128) << 6) + $uchr[3] - 0xAC80;
if ($c2 = $j % 28) { // A Hangul LVT syllable has a raw decomposition of an LV syllable + T.
$LV = 0xAC00 + $j - $c2;
$uchr = chr(0xE0 | $LV >> 12).chr(0x80 | $LV >> 6 & 0x3F).chr(0x80 | $LV & 0x3F)
.($c2 < 25
? ("\xE1\x86".chr(0xA7 + $c2))
: ("\xE1\x87".chr(0x67 + $c2)));
} else {
$uchr = "\xE1\x84".chr(0x80 + (int) ($j / 588))
."\xE1\x85".chr(0xA1 + (int) (($j % 588) / 28));
}
return $uchr;
}
// gitlost end
private static function recompose($s)
{
$compMap = self::$C;
$combClass = self::$cC;
$ulenMask = self::$ulenMask;
$result = $tail = '';
$i = $ulenMask[ord($s[0]) & 0xF0];
$len = strlen($s);
$lastUchr = substr($s, 0, $i);
$lastUcls = isset($combClass[$lastUchr]) ? 256 : 0;
while ($i < $len) {
// gitlost Don't bother treating ASCII specially. Note this is a subnormalize() biased change.
$ulen = $ulenMask[ord($s[$i]) & 0xF0];
$uchr = substr($s, $i, $ulen);
if ($lastUcls || $lastUchr < "\xE1\x84\x80" || "\xE1\x84\x92" < $lastUchr // gitlost Seems to be slightly faster to check $lastUcls first, at least in subnormalize() case.
|| $uchr < "\xE1\x85\xA1" || "\xE1\x85\xB5" < $uchr
) {
// Table lookup and combining chars composition
$ucls = isset($combClass[$uchr]) ? $combClass[$uchr] : 0;
if (isset($compMap[$lastUchr.$uchr]) && (!$lastUcls || $lastUcls < $ucls)) {
$lastUchr = $compMap[$lastUchr.$uchr];
} elseif ($lastUcls = $ucls) {
$tail .= $uchr;
} else {
$result .= $lastUchr.$tail; // gitlost Slightly faster than testing for $tail.
$tail = ''; // gitlost
$lastUchr = $uchr;
}
} else {
// Hangul chars
$L = ord($lastUchr[2]) - 0x80;
$V = ord($uchr[2]) - 0xA1;
$T = 0;
$uchr = substr($s, $i + $ulen, 3);
if ("\xE1\x86\xA7" <= $uchr && $uchr <= "\xE1\x87\x82") {
$T = ord($uchr[2]) - 0xA7;
0 > $T && $T += 0x40;
$ulen += 3;
}
$L = 0xAC00 + ($L * 21 + $V) * 28 + $T;
$lastUchr = chr(0xE0 | $L >> 12).chr(0x80 | $L >> 6 & 0x3F).chr(0x80 | $L & 0x3F);
}
$i += $ulen;
}
return $result.$lastUchr.$tail;
}
private static function decompose($s, $c)
{
$result = '';
$ASCII = self::$ASCII;
$decompMap = self::$D;
$combClass = self::$cC;
$ulenMask = self::$ulenMask;
if ($c) {
$compatMap = self::$KD;
}
$c = array();
$i = 0;
$len = strlen($s);
while ($i < $len) {
if ($s[$i] < "\x80") {
// ASCII chars
if ($c) {
ksort($c);
$result .= implode('', $c);
$c = array();
}
$j = 1 + strspn($s, $ASCII, $i + 1);
$result .= substr($s, $i, $j);
$i += $j;
continue;
}
$ulen = $ulenMask[ord($s[$i]) & 0xF0];
$uchr = substr($s, $i, $ulen);
$i += $ulen;
if ($uchr < "\xEA\xB0\x80" || "\xED\x9E\xA3" < $uchr) {
// Table lookup
if ($uchr !== $j = isset($compatMap[$uchr]) ? $compatMap[$uchr] : (isset($decompMap[$uchr]) ? $decompMap[$uchr] : $uchr)) {
$uchr = $j;
$j = strlen($uchr);
$ulen = $ulenMask[ord($uchr[0]) & 0xF0]; // gitlost Use $ulenMask for ASCII as well.
if ($ulen != $j) {
// Put trailing chars in $s
$j -= $ulen;
$i -= $j;
if (0 > $i) {
$s = str_repeat(' ', -$i).$s;
$len -= $i;
$i = 0;
}
while ($j--) {
$s[$i + $j] = $uchr[$ulen + $j];
}
$uchr = substr($uchr, 0, $ulen);
}
}
if (isset($combClass[$uchr])) {
// Combining chars, for sorting
if (!isset($c[$combClass[$uchr]])) {
$c[$combClass[$uchr]] = '';
}
$c[$combClass[$uchr]] .= $uchr;
continue;
}
} else {
// Hangul chars
$uchr = unpack('C*', $uchr);
$j = (($uchr[1] - 224) << 12) + (($uchr[2] - 128) << 6) + $uchr[3] - 0xAC80;
$uchr = "\xE1\x84".chr(0x80 + (int) ($j / 588))
."\xE1\x85".chr(0xA1 + (int) (($j % 588) / 28));
if ($j %= 28) {
$uchr .= $j < 25
? ("\xE1\x86".chr(0xA7 + $j))
: ("\xE1\x87".chr(0x67 + $j));
}
}
if ($c) {
ksort($c);
$result .= implode('', $c);
$c = array();
}
$result .= $uchr;
}
if ($c) {
ksort($c);
$result .= implode('', $c);
}
return $result;
}
// gitlost begin Optimized for subnormalize().
private static function subdecompose($s)
{
$result = '';
$decompMap = self::$D;
$combClass = self::$cC;
$ulenMask = self::$ulenMask;
$c = array();
$i = 0;
$len = strlen($s);
while ($i < $len) {
$ulen = $ulenMask[ord($s[$i]) & 0xF0];
$uchr = substr($s, $i, $ulen);
$i += $ulen;
if ($uchr < "\xEA\xB0\x80" || "\xED\x9E\xA3" < $uchr) {
// Table lookup
if ($uchr !== $j = isset($decompMap[$uchr]) ? $decompMap[$uchr] : $uchr) {
$uchr = $j;
$j = strlen($uchr);
$ulen = $ulenMask[ord($uchr[0]) & 0xF0];
if ($ulen != $j) {
// Put trailing chars in $s
$j -= $ulen;
$i -= $j;
if (0 > $i) {
$s = str_repeat(' ', -$i).$s;
$len -= $i;
$i = 0;
}
while ($j--) {
$s[$i + $j] = $uchr[$ulen + $j];
}
$uchr = substr($uchr, 0, $ulen);
}
}
if (isset($combClass[$uchr])) {
// Combining chars, for sorting
if (!isset($c[$combClass[$uchr]])) {
$c[$combClass[$uchr]] = $uchr;
} else {
$c[$combClass[$uchr]] .= $uchr;
}
continue;
}
} else {
// Hangul chars
$uchr = unpack('C*', $uchr);
$j = (($uchr[1] - 224) << 12) + (($uchr[2] - 128) << 6) + $uchr[3] - 0xAC80;
$uchr = "\xE1\x84".chr(0x80 + (int) ($j / 588))
."\xE1\x85".chr(0xA1 + (int) (($j % 588) / 28));
if ($j %= 28) {
$uchr .= $j < 25
? ("\xE1\x86".chr(0xA7 + $j))
: ("\xE1\x87".chr(0x67 + $j));
}
}
if ($c) {
ksort($c);
$result .= implode('', $c);
$c = array();
}
$result .= $uchr;
}
if ($c) {
ksort($c);
$result .= implode('', $c);
}
return $result;
}
// gitlost Callback from preg_replace_callback().
protected static function subnormalize($matches)
{
return self::recompose(self::subdecompose($matches[0]));
}
private static function kcCasefold($s)
{
$result = '';
$kcCaseFold = self::$kcCF;
$ulenMask = self::$ulenMask;
$i = 0;
$len = strlen($s);
while ($i < $len) {
$ulen = $ulenMask[ord($s[$i]) & 0xF0];
$uchr = substr($s, $i, $ulen);
$i += $ulen;
if (isset($kcCaseFold[$uchr])) {
$result .= $kcCaseFold[$uchr];
} elseif ($uchr < "\xf3\xa0\x80\x80" || "\xf3\xa0\xbf\xbf" < $uchr) { // U+E0000..E0FFF go to zero-length string, left out of mapping array to lessen size.
$result .= $uchr;
}
}
return $result;
}
private static function getArgType($v)
{
// 'boolean' & 'integer' were used until PHP 7.3. 'double' was used until PHP 7. Returning PHP >= 7.3 names.
return str_replace(array('boolean', 'integer', 'double', 'NULL', ' (closed)', ' type'), array('bool', 'int', 'float', 'null', '', ''), gettype($v));
}
// gitlost end
private static function getData($file)
{
return require dirname(__FILE__).'/Resources/unidata/'.$file.'.php'; // gitlost (__DIR__ is 5.3.0)
/* gitlost
if (file_exists($file = __DIR__.'/Resources/unidata/'.$file.'.php')) {
return require $file;
}
return false;
gitlost */
}
}