ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
UtfNormal Class Reference
+ Collaboration diagram for UtfNormal:

Static Public Member Functions

static cleanUp ($string)
 The ultimate convenience function! Clean up invalid UTF-8 sequences, and convert to normal form C, canonical composition. More...
 
static toNFC ($string)
 Convert a UTF-8 string to normal form C, canonical composition. More...
 
static toNFD ($string)
 Convert a UTF-8 string to normal form D, canonical decomposition. More...
 
static toNFKC ($string)
 Convert a UTF-8 string to normal form KC, compatibility composition. More...
 
static toNFKD ($string)
 Convert a UTF-8 string to normal form KD, compatibility decomposition. More...
 
static loadData ()
 Load the basic composition data if necessary. More...
 
static quickIsNFC ($string)
 Returns true if the string is definitely in NFC. More...
 
static quickIsNFCVerify (&$string)
 Returns true if the string is definitely in NFC. More...
 
static NFC ($string)
 
static NFD ($string)
 
static NFKC ($string)
 
static NFKD ($string)
 
static fastDecompose ($string, $map)
 Perform decomposition of a UTF-8 string into either D or KD form (depending on which decomposition map is passed to us). More...
 
static fastCombiningSort ($string)
 Sorts combining characters into canonical order. More...
 
static fastCompose ($string)
 Produces canonically composed sequences, i.e. More...
 
static placebo ($string)
 This is just used for the benchmark, comparing how long it takes to interate through a string without really doing anything of substance. More...
 

Detailed Description

Definition at line 114 of file UtfNormal.php.

Member Function Documentation

◆ cleanUp()

static UtfNormal::cleanUp (   $string)
static

The ultimate convenience function! Clean up invalid UTF-8 sequences, and convert to normal form C, canonical composition.

Fast return for pure ASCII strings; some lesser optimizations for strings containing only known-good characters. Not as fast as toNFC().

Parameters
string$stringa UTF-8 string
Returns
string a clean, shiny, normalized UTF-8 string

Definition at line 127 of file UtfNormal.php.

References NFC(), NORMALIZE_ICU, quickIsNFCVerify(), UNORM_NFC, UTF8_FFFE, and UTF8_FFFF.

Referenced by CleanUpTest\doTestBytes(), CleanUpTest\doTestDoubleBytes(), CleanUpTest\doTestTripleBytes(), CleanUpTest\testAscii(), CleanUpTest\testBomRegression(), CleanUpTest\testChunkRegression(), CleanUpTest\testForbiddenRegression(), CleanUpTest\testHangulRegression(), CleanUpTest\testInterposeRegression(), CleanUpTest\testLatin(), CleanUpTest\testLatinNormal(), CleanUpTest\testNull(), CleanUpTest\testOverlongRegression(), CleanUpTest\testSurrogateRegression(), and CleanUpTest\XtestAllChars().

128  {
129  if (NORMALIZE_ICU) {
130  # We exclude a few chars that ICU would not.
131  $string = preg_replace(
132  '/[\x00-\x08\x0b\x0c\x0e-\x1f]/',
133  UTF8_REPLACEMENT,
134  $string
135  );
136  $string = str_replace(UTF8_FFFE, UTF8_REPLACEMENT, $string);
137  $string = str_replace(UTF8_FFFF, UTF8_REPLACEMENT, $string);
138 
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.
144  return $string;
145  } else {
146  return UtfNormal::NFC($string);
147  }
148  }
const UTF8_FFFE
Definition: UtfNormal.php:80
const UNORM_NFC
Definition: UtfNormal.php:93
const UTF8_FFFF
Definition: UtfNormal.php:81
static NFC($string)
Definition: UtfNormal.php:519
const NORMALIZE_ICU
Definition: UtfNormal.php:98
static quickIsNFCVerify(&$string)
Returns true if the string is definitely in NFC.
Definition: UtfNormal.php:293
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fastCombiningSort()

static UtfNormal::fastCombiningSort (   $string)
static

Sorts combining characters into canonical order.

This is the final step in creating decomposed normal forms D and KD.

Parameters
string$stringa valid, decomposed UTF-8 string. Input is not validated.
Returns
string a UTF-8 string with combining characters sorted in canonical order

Definition at line 640 of file UtfNormal.php.

References Vendor\Package\$c, $i, $n, $out, $utfCombiningClass, and loadData().

Referenced by NFD(), and NFKD().

641  {
643  global $utfCombiningClass;
644  $len = strlen($string);
645  $out = '';
646  $combiners = array();
647  $lastClass = -1;
648  for ($i = 0; $i < $len; $i++) {
649  $c = $string[$i];
650  $n = ord($c);
651  if ($n >= 0x80) {
652  if ($n >= 0xf0) {
653  $c = substr($string, $i, 4);
654  $i += 3;
655  } elseif ($n >= 0xe0) {
656  $c = substr($string, $i, 3);
657  $i += 2;
658  } elseif ($n >= 0xc0) {
659  $c = substr($string, $i, 2);
660  $i++;
661  }
662  if (isset($utfCombiningClass[$c])) {
663  $lastClass = $utfCombiningClass[$c];
664  if (isset($combiners[$lastClass])) {
665  $combiners[$lastClass] .= $c;
666  } else {
667  $combiners[$lastClass] = $c;
668  }
669  continue;
670  }
671  }
672  if ($lastClass) {
673  ksort($combiners);
674  $out .= implode('', $combiners);
675  $combiners = array();
676  }
677  $out .= $c;
678  $lastClass = 0;
679  }
680  if ($lastClass) {
681  ksort($combiners);
682  $out .= implode('', $combiners);
683  }
684  return $out;
685  }
global $utfCombiningClass
Definition: UtfNormal.php:21
static loadData()
Load the basic composition data if necessary.
Definition: UtfNormal.php:234
$n
Definition: RandomTest.php:85
$i
Definition: metadata.php:24
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fastCompose()

static UtfNormal::fastCompose (   $string)
static

Produces canonically composed sequences, i.e.

normal form C or KC.

Parameters
string$stringa valid UTF-8 string in sorted normal form D or KD. Input is not validated.
Returns
string a UTF-8 string with canonical precomposed characters used where possible

Definition at line 695 of file UtfNormal.php.

References Vendor\Package\$c, $i, $n, $out, $utfCanonicalComp, $utfCombiningClass, loadData(), UNICODE_HANGUL_FIRST, UNICODE_HANGUL_TCOUNT, UNICODE_HANGUL_VCOUNT, UTF8_HANGUL_FIRST, UTF8_HANGUL_LAST, UTF8_HANGUL_LBASE, UTF8_HANGUL_LEND, UTF8_HANGUL_TBASE, UTF8_HANGUL_TEND, UTF8_HANGUL_VBASE, and UTF8_HANGUL_VEND.

Referenced by NFC(), and NFKC().

696  {
699  $len = strlen($string);
700  $out = '';
701  $lastClass = -1;
702  $lastHangul = 0;
703  $startChar = '';
704  $combining = '';
705  $x1 = ord(substr(UTF8_HANGUL_VBASE, 0, 1));
706  $x2 = ord(substr(UTF8_HANGUL_TEND, 0, 1));
707  for ($i = 0; $i < $len; $i++) {
708  $c = $string[$i];
709  $n = ord($c);
710  if ($n < 0x80) {
711  # No combining characters here...
712  $out .= $startChar;
713  $out .= $combining;
714  $startChar = $c;
715  $combining = '';
716  $lastClass = 0;
717  continue;
718  } elseif ($n >= 0xf0) {
719  $c = substr($string, $i, 4);
720  $i += 3;
721  } elseif ($n >= 0xe0) {
722  $c = substr($string, $i, 3);
723  $i += 2;
724  } elseif ($n >= 0xc0) {
725  $c = substr($string, $i, 2);
726  $i++;
727  }
728  $pair = $startChar . $c;
729  if ($n > 0x80) {
730  if (isset($utfCombiningClass[$c])) {
731  # A combining char; see what we can do with it
732  $class = $utfCombiningClass[$c];
733  if (!empty($startChar) &&
734  $lastClass < $class &&
735  $class > 0 &&
736  isset($utfCanonicalComp[$pair])) {
737  $startChar = $utfCanonicalComp[$pair];
738  $class = 0;
739  } else {
740  $combining .= $c;
741  }
742  $lastClass = $class;
743  $lastHangul = 0;
744  continue;
745  }
746  }
747  # New start char
748  if ($lastClass == 0) {
749  if (isset($utfCanonicalComp[$pair])) {
750  $startChar = $utfCanonicalComp[$pair];
751  $lastHangul = 0;
752  continue;
753  }
754  if ($n >= $x1 && $n <= $x2) {
755  # WARNING: Hangul code is painfully slow.
756  # I apologize for this ugly, ugly code; however
757  # performance is even more teh suck if we call
758  # out to nice clean functions. Lookup tables are
759  # marginally faster, but require a lot of space.
760  #
761  if ($c >= UTF8_HANGUL_VBASE &&
762  $c <= UTF8_HANGUL_VEND &&
763  $startChar >= UTF8_HANGUL_LBASE &&
764  $startChar <= UTF8_HANGUL_LEND) {
765  #
766  #$lIndex = utf8ToCodepoint( $startChar ) - UNICODE_HANGUL_LBASE;
767  #$vIndex = utf8ToCodepoint( $c ) - UNICODE_HANGUL_VBASE;
768  $lIndex = ord($startChar[2]) - 0x80;
769  $vIndex = ord($c[2]) - 0xa1;
770 
771  $hangulPoint = UNICODE_HANGUL_FIRST +
773  (UNICODE_HANGUL_VCOUNT * $lIndex + $vIndex);
774 
775  # Hardcode the limited-range UTF-8 conversion:
776  $startChar = chr($hangulPoint >> 12 & 0x0f | 0xe0) .
777  chr($hangulPoint >> 6 & 0x3f | 0x80) .
778  chr($hangulPoint & 0x3f | 0x80);
779  $lastHangul = 0;
780  continue;
781  } elseif ($c >= UTF8_HANGUL_TBASE &&
782  $c <= UTF8_HANGUL_TEND &&
783  $startChar >= UTF8_HANGUL_FIRST &&
784  $startChar <= UTF8_HANGUL_LAST &&
785  !$lastHangul) {
786  # $tIndex = utf8ToCodepoint( $c ) - UNICODE_HANGUL_TBASE;
787  $tIndex = ord($c[2]) - 0xa7;
788  if ($tIndex < 0) {
789  $tIndex = ord($c[2]) - 0x80 + (0x11c0 - 0x11a7);
790  }
791 
792  # Increment the code point by $tIndex, without
793  # the function overhead of decoding and recoding UTF-8
794  #
795  $tail = ord($startChar[2]) + $tIndex;
796  if ($tail > 0xbf) {
797  $tail -= 0x40;
798  $mid = ord($startChar[1]) + 1;
799  if ($mid > 0xbf) {
800  $startChar[0] = chr(ord($startChar[0]) + 1);
801  $mid -= 0x40;
802  }
803  $startChar[1] = chr($mid);
804  }
805  $startChar[2] = chr($tail);
806 
807  # If there's another jamo char after this, *don't* try to merge it.
808  $lastHangul = 1;
809  continue;
810  }
811  }
812  }
813  $out .= $startChar;
814  $out .= $combining;
815  $startChar = $c;
816  $combining = '';
817  $lastClass = 0;
818  $lastHangul = 0;
819  }
820  $out .= $startChar . $combining;
821  return $out;
822  }
const UTF8_HANGUL_LEND
Definition: UtfNormal.php:61
const UTF8_HANGUL_FIRST
Definition: UtfNormal.php:54
const UNICODE_HANGUL_FIRST
Definition: UtfNormal.php:32
global $utfCombiningClass
Definition: UtfNormal.php:21
const UNICODE_HANGUL_VCOUNT
Definition: UtfNormal.php:40
const UNICODE_HANGUL_TCOUNT
Definition: UtfNormal.php:41
const UTF8_HANGUL_VBASE
Definition: UtfNormal.php:58
const UTF8_HANGUL_LAST
Definition: UtfNormal.php:55
const UTF8_HANGUL_LBASE
Definition: UtfNormal.php:57
const UTF8_HANGUL_TBASE
Definition: UtfNormal.php:59
static loadData()
Load the basic composition data if necessary.
Definition: UtfNormal.php:234
const UTF8_HANGUL_TEND
Definition: UtfNormal.php:63
$n
Definition: RandomTest.php:85
global $utfCanonicalComp
Definition: UtfNormal.php:21
const UTF8_HANGUL_VEND
Definition: UtfNormal.php:62
$i
Definition: metadata.php:24
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fastDecompose()

static UtfNormal::fastDecompose (   $string,
  $map 
)
static

Perform decomposition of a UTF-8 string into either D or KD form (depending on which decomposition map is passed to us).

Input is assumed to be valid UTF-8. Invalid code will break.

Parameters
string$stringValid UTF-8 string
array$maphash of expanded decomposition map
Returns
string a UTF-8 string decomposed, not yet normalized (needs sorting)

Definition at line 578 of file UtfNormal.php.

References Vendor\Package\$c, $i, $index, $n, $out, loadData(), UNICODE_HANGUL_FIRST, UNICODE_HANGUL_NCOUNT, UNICODE_HANGUL_TCOUNT, UTF8_HANGUL_FIRST, and UTF8_HANGUL_LAST.

Referenced by NFD(), and NFKD().

579  {
581  $len = strlen($string);
582  $out = '';
583  for ($i = 0; $i < $len; $i++) {
584  $c = $string[$i];
585  $n = ord($c);
586  if ($n < 0x80) {
587  # ASCII chars never decompose
588  # THEY ARE IMMORTAL
589  $out .= $c;
590  continue;
591  } elseif ($n >= 0xf0) {
592  $c = substr($string, $i, 4);
593  $i += 3;
594  } elseif ($n >= 0xe0) {
595  $c = substr($string, $i, 3);
596  $i += 2;
597  } elseif ($n >= 0xc0) {
598  $c = substr($string, $i, 2);
599  $i++;
600  }
601  if (isset($map[$c])) {
602  $out .= $map[$c];
603  continue;
604  } else {
605  if ($c >= UTF8_HANGUL_FIRST && $c <= UTF8_HANGUL_LAST) {
606  # Decompose a hangul syllable into jamo;
607  # hardcoded for three-byte UTF-8 sequence.
608  # A lookup table would be slightly faster,
609  # but adds a lot of memory & disk needs.
610  #
611  $index = ((ord($c[0]) & 0x0f) << 12
612  | (ord($c[1]) & 0x3f) << 6
613  | (ord($c[2]) & 0x3f))
615  $l = intval($index / UNICODE_HANGUL_NCOUNT);
618  $out .= "\xe1\x84" . chr(0x80 + $l) . "\xe1\x85" . chr(0xa1 + $v);
619  if ($t >= 25) {
620  $out .= "\xe1\x87" . chr(0x80 + $t - 25);
621  } elseif ($t) {
622  $out .= "\xe1\x86" . chr(0xa7 + $t);
623  }
624  continue;
625  }
626  }
627  $out .= $c;
628  }
629  return $out;
630  }
const UTF8_HANGUL_FIRST
Definition: UtfNormal.php:54
const UNICODE_HANGUL_FIRST
Definition: UtfNormal.php:32
const UNICODE_HANGUL_TCOUNT
Definition: UtfNormal.php:41
const UTF8_HANGUL_LAST
Definition: UtfNormal.php:55
$index
Definition: metadata.php:128
static loadData()
Load the basic composition data if necessary.
Definition: UtfNormal.php:234
$n
Definition: RandomTest.php:85
const UNICODE_HANGUL_NCOUNT
Definition: UtfNormal.php:42
$i
Definition: metadata.php:24
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ loadData()

static UtfNormal::loadData ( )
static

Load the basic composition data if necessary.

Definition at line 234 of file UtfNormal.php.

References $utfCombiningClass.

Referenced by fastCombiningSort(), fastCompose(), fastDecompose(), NFD(), quickIsNFC(), and quickIsNFCVerify().

235  {
236  global $utfCombiningClass;
237  if (!isset($utfCombiningClass)) {
238  require_once(dirname(__FILE__) . '/UtfNormalData.inc');
239  }
240  }
global $utfCombiningClass
Definition: UtfNormal.php:21
+ Here is the caller graph for this function:

◆ NFC()

static UtfNormal::NFC (   $string)
static
Parameters
string$string
Returns
string

Definition at line 519 of file UtfNormal.php.

References fastCompose(), and NFD().

Referenced by cleanUp(), CleanUpTest\doTestDoubleBytes(), CleanUpTest\doTestTripleBytes(), toNFC(), and CleanUpTest\XtestAllChars().

520  {
521  return UtfNormal::fastCompose(UtfNormal::NFD($string));
522  }
static NFD($string)
Definition: UtfNormal.php:530
static fastCompose($string)
Produces canonically composed sequences, i.e.
Definition: UtfNormal.php:695
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ NFD()

static UtfNormal::NFD (   $string)
static
Parameters
string$string
Returns
string

Definition at line 530 of file UtfNormal.php.

References $utfCanonicalDecomp, fastCombiningSort(), fastDecompose(), and loadData().

Referenced by NFC(), and toNFD().

531  {
533  global $utfCanonicalDecomp;
535  UtfNormal::fastDecompose($string, $utfCanonicalDecomp)
536  );
537  }
global $utfCanonicalDecomp
Definition: UtfNormal.php:21
static loadData()
Load the basic composition data if necessary.
Definition: UtfNormal.php:234
static fastCombiningSort($string)
Sorts combining characters into canonical order.
Definition: UtfNormal.php:640
static fastDecompose($string, $map)
Perform decomposition of a UTF-8 string into either D or KD form (depending on which decomposition ma...
Definition: UtfNormal.php:578
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ NFKC()

static UtfNormal::NFKC (   $string)
static
Parameters
string$string
Returns
string

Definition at line 545 of file UtfNormal.php.

References fastCompose(), and NFKD().

Referenced by toNFKC().

546  {
547  return UtfNormal::fastCompose(UtfNormal::NFKD($string));
548  }
static fastCompose($string)
Produces canonically composed sequences, i.e.
Definition: UtfNormal.php:695
static NFKD($string)
Definition: UtfNormal.php:556
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ NFKD()

static UtfNormal::NFKD (   $string)
static
Parameters
string$string
Returns
string

Definition at line 556 of file UtfNormal.php.

References $utfCompatibilityDecomp, fastCombiningSort(), and fastDecompose().

Referenced by NFKC(), and toNFKD().

557  {
559  if (!isset($utfCompatibilityDecomp)) {
560  require_once('UtfNormalDataK.inc');
561  }
563  UtfNormal::fastDecompose($string, $utfCompatibilityDecomp)
564  );
565  }
static fastCombiningSort($string)
Sorts combining characters into canonical order.
Definition: UtfNormal.php:640
static fastDecompose($string, $map)
Perform decomposition of a UTF-8 string into either D or KD form (depending on which decomposition ma...
Definition: UtfNormal.php:578
global $utfCompatibilityDecomp
Definition: UtfNormal.php:29
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ placebo()

static UtfNormal::placebo (   $string)
static

This is just used for the benchmark, comparing how long it takes to interate through a string without really doing anything of substance.

Parameters
string$string
Returns
string

Definition at line 831 of file UtfNormal.php.

References $i, and $out.

832  {
833  $len = strlen($string);
834  $out = '';
835  for ($i = 0; $i < $len; $i++) {
836  $out .= $string[$i];
837  }
838  return $out;
839  }
$i
Definition: metadata.php:24

◆ quickIsNFC()

static UtfNormal::quickIsNFC (   $string)
static

Returns true if the string is definitely in NFC.

Returns false if not or uncertain.

Parameters
string$stringa valid UTF-8 string. Input is not validated.
Returns
bool

Definition at line 249 of file UtfNormal.php.

References Vendor\Package\$c, $i, $n, $utfCombiningClass, and loadData().

Referenced by toNFC().

250  {
251  # ASCII is always valid NFC!
252  # If it's pure ASCII, let it through.
253  if (!preg_match('/[\x80-\xff]/', $string)) {
254  return true;
255  }
256 
258  global $utfCheckNFC, $utfCombiningClass;
259  $len = strlen($string);
260  for ($i = 0; $i < $len; $i++) {
261  $c = $string[$i];
262  $n = ord($c);
263  if ($n < 0x80) {
264  continue;
265  } elseif ($n >= 0xf0) {
266  $c = substr($string, $i, 4);
267  $i += 3;
268  } elseif ($n >= 0xe0) {
269  $c = substr($string, $i, 3);
270  $i += 2;
271  } elseif ($n >= 0xc0) {
272  $c = substr($string, $i, 2);
273  $i++;
274  }
275  if (isset($utfCheckNFC[$c])) {
276  # If it's NO or MAYBE, bail and do the slow check.
277  return false;
278  }
279  if (isset($utfCombiningClass[$c])) {
280  # Combining character? We might have to do sorting, at least.
281  return false;
282  }
283  }
284  return true;
285  }
global $utfCombiningClass
Definition: UtfNormal.php:21
static loadData()
Load the basic composition data if necessary.
Definition: UtfNormal.php:234
$n
Definition: RandomTest.php:85
$i
Definition: metadata.php:24
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ quickIsNFCVerify()

static UtfNormal::quickIsNFCVerify ( $string)
static

Returns true if the string is definitely in NFC.

Returns false if not or uncertain.

Parameters
string$stringa UTF-8 string, altered on output to be valid UTF-8 safe for XML.

Definition at line 293 of file UtfNormal.php.

References $base, Vendor\Package\$c, $i, $n, $out, $utfCombiningClass, down(), ILIAS\GlobalScreen\has(), loadData(), more(), UTF8_FFFE, UTF8_FFFF, UTF8_MAX, UTF8_OVERLONG_A, UTF8_OVERLONG_B, UTF8_OVERLONG_C, and UTF8_SURROGATE_FIRST.

Referenced by cleanUp().

294  {
295  # Screen out some characters that eg won't be allowed in XML
296  $string = preg_replace('/[\x00-\x08\x0b\x0c\x0e-\x1f]/', UTF8_REPLACEMENT, $string);
297 
298  # ASCII is always valid NFC!
299  # If we're only ever given plain ASCII, we can avoid the overhead
300  # of initializing the decomposition tables by skipping out early.
301  if (!preg_match('/[\x80-\xff]/', $string)) {
302  return true;
303  }
304 
305  static $checkit = null, $tailBytes = null, $utfCheckOrCombining = null;
306  if (!isset($checkit)) {
307  # Load/build some scary lookup tables...
309  global $utfCheckNFC, $utfCombiningClass;
310 
311  $utfCheckOrCombining = array_merge($utfCheckNFC, $utfCombiningClass);
312 
313  # Head bytes for sequences which we should do further validity checks
314  $checkit = array_flip(array_map(
315  'chr',
316  array( 0xc0, 0xc1, 0xe0, 0xed, 0xef,
317  0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
318  0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff )
319  ));
320 
321  # Each UTF-8 head byte is followed by a certain
322  # number of tail bytes.
323  $tailBytes = array();
324  for ($n = 0; $n < 256; $n++) {
325  if ($n < 0xc0) {
326  $remaining = 0;
327  } elseif ($n < 0xe0) {
328  $remaining = 1;
329  } elseif ($n < 0xf0) {
330  $remaining = 2;
331  } elseif ($n < 0xf8) {
332  $remaining = 3;
333  } elseif ($n < 0xfc) {
334  $remaining = 4;
335  } elseif ($n < 0xfe) {
336  $remaining = 5;
337  } else {
338  $remaining = 0;
339  }
340  $tailBytes[chr($n)] = $remaining;
341  }
342  }
343 
344  # Chop the text into pure-ASCII and non-ASCII areas;
345  # large ASCII parts can be handled much more quickly.
346  # Don't chop up Unicode areas for punctuation, though,
347  # that wastes energy.
348  $matches = array();
349  preg_match_all(
350  '/([\x00-\x7f]+|[\x80-\xff][\x00-\x40\x5b-\x5f\x7b-\xff]*)/',
351  $string,
352  $matches
353  );
354 
355  $looksNormal = true;
356  $base = 0;
357  $replace = array();
358  foreach ($matches[1] as $str) {
359  $chunk = strlen($str);
360 
361  if ($str[0] < "\x80") {
362  # ASCII chunk: guaranteed to be valid UTF-8
363  # and in normal form C, so skip over it.
364  $base += $chunk;
365  continue;
366  }
367 
368  # We'll have to examine the chunk byte by byte to ensure
369  # that it consists of valid UTF-8 sequences, and to see
370  # if any of them might not be normalized.
371  #
372  # Since PHP is not the fastest language on earth, some of
373  # this code is a little ugly with inner loop optimizations.
374 
375  $head = '';
376  $len = $chunk + 1; # Counting down is faster. I'm *so* sorry.
377 
378  for ($i = -1; --$len;) {
379  if ($remaining = $tailBytes[$c = $str[++$i]]) {
380  # UTF-8 head byte!
381  $sequence = $head = $c;
382  do {
383  # Look for the defined number of tail bytes...
384  if (--$len && ($c = $str[++$i]) >= "\x80" && $c < "\xc0") {
385  # Legal tail bytes are nice.
386  $sequence .= $c;
387  } else {
388  if (0 == $len) {
389  # Premature end of string!
390  # Drop a replacement character into output to
391  # represent the invalid UTF-8 sequence.
392  $replace[] = array( UTF8_REPLACEMENT,
393  $base + $i + 1 - strlen($sequence),
394  strlen($sequence) );
395  break 2;
396  } else {
397  # Illegal tail byte; abandon the sequence.
398  $replace[] = array( UTF8_REPLACEMENT,
399  $base + $i - strlen($sequence),
400  strlen($sequence) );
401  # Back up and reprocess this byte; it may itself
402  # be a legal ASCII or UTF-8 sequence head.
403  --$i;
404  ++$len;
405  continue 2;
406  }
407  }
408  } while (--$remaining);
409 
410  if (isset($checkit[$head])) {
411  # Do some more detailed validity checks, for
412  # invalid characters and illegal sequences.
413  if ($head == "\xed") {
414  # 0xed is relatively frequent in Korean, which
415  # abuts the surrogate area, so we're doing
416  # this check separately to speed things up.
417 
418  if ($sequence >= UTF8_SURROGATE_FIRST) {
419  # Surrogates are legal only in UTF-16 code.
420  # They are totally forbidden here in UTF-8
421  # utopia.
422  $replace[] = array( UTF8_REPLACEMENT,
423  $base + $i + 1 - strlen($sequence),
424  strlen($sequence) );
425  $head = '';
426  continue;
427  }
428  } else {
429  # Slower, but rarer checks...
430  $n = ord($head);
431  if (
432  # "Overlong sequences" are those that are syntactically
433  # correct but use more UTF-8 bytes than are necessary to
434  # encode a character. Naïve string comparisons can be
435  # tricked into failing to see a match for an ASCII
436  # character, for instance, which can be a security hole
437  # if blacklist checks are being used.
438  ($n < 0xc2 && $sequence <= UTF8_OVERLONG_A)
439  || ($n == 0xe0 && $sequence <= UTF8_OVERLONG_B)
440  || ($n == 0xf0 && $sequence <= UTF8_OVERLONG_C)
441 
442  # U+FFFE and U+FFFF are explicitly forbidden in Unicode.
443  || ($n == 0xef &&
444  ($sequence == UTF8_FFFE)
445  || ($sequence == UTF8_FFFF))
446 
447  # Unicode has been limited to 21 bits; longer
448  # sequences are not allowed.
449  || ($n >= 0xf0 && $sequence > UTF8_MAX)) {
450  $replace[] = array( UTF8_REPLACEMENT,
451  $base + $i + 1 - strlen($sequence),
452  strlen($sequence) );
453  $head = '';
454  continue;
455  }
456  }
457  }
458 
459  if (isset($utfCheckOrCombining[$sequence])) {
460  # If it's NO or MAYBE, we'll have to rip
461  # the string apart and put it back together.
462  # That's going to be mighty slow.
463  $looksNormal = false;
464  }
465 
466  # The sequence is legal!
467  $head = '';
468  } elseif ($c < "\x80") {
469  # ASCII byte.
470  $head = '';
471  } elseif ($c < "\xc0") {
472  # Illegal tail bytes
473  if ($head == '') {
474  # Out of the blue!
475  $replace[] = array( UTF8_REPLACEMENT, $base + $i, 1 );
476  } else {
477  # Don't add if we're continuing a broken sequence;
478  # we already put a replacement character when we looked
479  # at the broken sequence.
480  $replace[] = array( '', $base + $i, 1 );
481  }
482  } else {
483  # Miscellaneous freaks.
484  $replace[] = array( UTF8_REPLACEMENT, $base + $i, 1 );
485  $head = '';
486  }
487  }
488  $base += $chunk;
489  }
490  if (count($replace)) {
491  # There were illegal UTF-8 sequences we need to fix up.
492  $out = '';
493  $last = 0;
494  foreach ($replace as $rep) {
495  list($replacement, $start, $length) = $rep;
496  if ($last < $start) {
497  $out .= substr($string, $last, $start - $last);
498  }
499  $out .= $replacement;
500  $last = $start + $length;
501  }
502  if ($last < strlen($string)) {
503  $out .= substr($string, $last);
504  }
505  $string = $out;
506  }
507  return $looksNormal;
508  }
const UTF8_FFFE
Definition: UtfNormal.php:80
const UTF8_OVERLONG_C
Definition: UtfNormal.php:75
global $utfCombiningClass
Definition: UtfNormal.php:21
const UTF8_OVERLONG_B
Definition: UtfNormal.php:74
const UTF8_FFFF
Definition: UtfNormal.php:81
const UTF8_SURROGATE_FIRST
Definition: UtfNormal.php:65
more()
Definition: more.php:2
down()
Definition: down.php:2
has(string $class_name)
$base
Definition: index.php:4
const UTF8_OVERLONG_A(!defined('UTF8_REPLACEMENT'))
Definition: UtfNormal.php:73
static loadData()
Load the basic composition data if necessary.
Definition: UtfNormal.php:234
$n
Definition: RandomTest.php:85
const UTF8_MAX
Definition: UtfNormal.php:67
$i
Definition: metadata.php:24
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ toNFC()

static UtfNormal::toNFC (   $string)
static

Convert a UTF-8 string to normal form C, canonical composition.

Fast return for pure ASCII strings; some lesser optimizations for strings containing only known-good characters.

Parameters
string$stringa valid UTF-8 string. Input is not validated.
Returns
string a UTF-8 string in normal form C

Definition at line 159 of file UtfNormal.php.

References NFC(), NORMALIZE_ICU, quickIsNFC(), and UNORM_NFC.

Referenced by ilWebDAVUtil\davDeslashify(), ilTree\getNodePathForTitlePath(), and ilStr\normalizeUtf8String().

160  {
161  if (NORMALIZE_ICU) {
162  return utf8_normalize($string, UNORM_NFC);
163  } elseif (UtfNormal::quickIsNFC($string)) {
164  return $string;
165  } else {
166  return UtfNormal::NFC($string);
167  }
168  }
const UNORM_NFC
Definition: UtfNormal.php:93
static NFC($string)
Definition: UtfNormal.php:519
const NORMALIZE_ICU
Definition: UtfNormal.php:98
static quickIsNFC($string)
Returns true if the string is definitely in NFC.
Definition: UtfNormal.php:249
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ toNFD()

static UtfNormal::toNFD (   $string)
static

Convert a UTF-8 string to normal form D, canonical decomposition.

Fast return for pure ASCII strings.

Parameters
string$stringa valid UTF-8 string. Input is not validated.
Returns
string a UTF-8 string in normal form D

Definition at line 178 of file UtfNormal.php.

References NFD(), NORMALIZE_ICU, and UNORM_NFD.

179  {
180  if (NORMALIZE_ICU) {
181  return utf8_normalize($string, UNORM_NFD);
182  } elseif (preg_match('/[\x80-\xff]/', $string)) {
183  return UtfNormal::NFD($string);
184  } else {
185  return $string;
186  }
187  }
static NFD($string)
Definition: UtfNormal.php:530
const UNORM_NFD
Definition: UtfNormal.php:91
const NORMALIZE_ICU
Definition: UtfNormal.php:98
+ Here is the call graph for this function:

◆ toNFKC()

static UtfNormal::toNFKC (   $string)
static

Convert a UTF-8 string to normal form KC, compatibility composition.

This may cause irreversible information loss, use judiciously. Fast return for pure ASCII strings.

Parameters
string$stringa valid UTF-8 string. Input is not validated.
Returns
string a UTF-8 string in normal form KC

Definition at line 198 of file UtfNormal.php.

References NFKC(), NORMALIZE_ICU, and UNORM_NFKC.

199  {
200  if (NORMALIZE_ICU) {
201  return utf8_normalize($string, UNORM_NFKC);
202  } elseif (preg_match('/[\x80-\xff]/', $string)) {
203  return UtfNormal::NFKC($string);
204  } else {
205  return $string;
206  }
207  }
static NFKC($string)
Definition: UtfNormal.php:545
const UNORM_NFKC
Definition: UtfNormal.php:95
const NORMALIZE_ICU
Definition: UtfNormal.php:98
+ Here is the call graph for this function:

◆ toNFKD()

static UtfNormal::toNFKD (   $string)
static

Convert a UTF-8 string to normal form KD, compatibility decomposition.

This may cause irreversible information loss, use judiciously. Fast return for pure ASCII strings.

Parameters
string$stringa valid UTF-8 string. Input is not validated.
Returns
string a UTF-8 string in normal form KD

Definition at line 218 of file UtfNormal.php.

References NFKD(), NORMALIZE_ICU, and UNORM_NFKD.

219  {
220  if (NORMALIZE_ICU) {
221  return utf8_normalize($string, UNORM_NFKD);
222  } elseif (preg_match('/[\x80-\xff]/', $string)) {
223  return UtfNormal::NFKD($string);
224  } else {
225  return $string;
226  }
227  }
const NORMALIZE_ICU
Definition: UtfNormal.php:98
static NFKD($string)
Definition: UtfNormal.php:556
const UNORM_NFKD
Definition: UtfNormal.php:92
+ Here is the call graph for this function:

The documentation for this class was generated from the following file: