<?php
# converts a string to NFC or NFD and returns the result
# n is one of nfd or nfc
# str is the string to be converted
mb_internal_encoding("UTF-8");
include('../n11n.php');
// TESTING
function cp2utf8 ($hexcp) {
$outputString = '';
$numbers = explode(' ', $hexcp); //print 'count numbers '.count($numbers);
for ($i=0; $i<count($numbers); $i++) {
$n = hexdec($numbers[$i]);
if ($n <= 0x7F) {
$outputString .= chr($n);
}
else if ($n <= 0x7FF) {
$outputString .= chr(0xC0 | (($n>>6) & 0x1F)).chr(0x80 | ($n & 0x3F));
}
else if ($n <= 0xFFFF) {
$outputString .= chr(0xE0 | (($n>>12) & 0x0F)).chr(0x80 | (($n>>6) & 0x3F)).chr(0x80 | ($n & 0x3F));
}
else if ($n <= 0x10FFFF) {
$outputString .= chr(0xF0 | (($n>>18) & 0x07)).chr(0x80 | (($n>>12) & 0x3F)).chr(0x80 | (($n>>6) & 0x3F)).chr(0x80 | ($n & 0x3F));
}
else {
$outputString .= 'Error=> ' + $n +'!';
}
}
return $outputString;
}
$testsuite = fopen('../tests/NormalizationTest.txt','r');
$counter = 0;
$testarray = array();
while(!feof($testsuite)) {
$line = fgets($testsuite);
if (substr($line,0,1) != '#' and substr($line,0,1) != '@' and strlen($line) > 8) {
$items = explode(";", $line);
$testarray[$counter] = array(cp2utf8($items[0]), cp2utf8($items[1]), cp2utf8($items[2]), cp2utf8($items[3]), $counter );
$counter++;
}
}
fclose($testsuite);
$total = count($testarray)*4;
print $total.' tests in total.<br />';
//print $testarray[0][2];
$nfdcounter = 0;
$nfccounter = 0;
for ($i=0; $i<count($testarray); $i++) {
$nfdPassed = true;
$normOutput = nfd($testarray[$i][0]);
if ($testarray[$i][2] != $normOutput) {
print 'NFD test '.$testarray[$i][4].' failed. ['.$testarray[$i][0].
'>'.$normOutput.'!'.$testarray[$i][2].'] (1st test)<br/>';
$nfdcounter++;
$nfdPassed = false;
}
$normOutput = nfd($testarray[$i][1]);
if ($testarray[$i][2] != $normOutput) {
print 'NFD test '.$testarray[$i][4].' failed. ['.$testarray[$i][1].
'>'.$normOutput.'!'.$testarray[$i][2].'] (2nd test)<br/>';
$nfdcounter++;
$nfdPassed = false;
}
if ($nfdPassed) {
$normOutput = nfc($testarray[$i][0]);
if ($testarray[$i][1] != $normOutput) {
print 'NFC test '.$testarray[$i][4].' failed. ['.$testarray[$i][0].
'>'.$normOutput.'!'.$testarray[$i][1].'] (1st test)<br/>';
$nfccounter++;
}
}
if ($nfdPassed) {
$normOutput = nfc($testarray[$i][2]);
if ($testarray[$i][1] != $normOutput) {
print 'NFC test '.$testarray[$i][4].' failed. ['.$testarray[$i][2].
'>'.$normOutput.'!'.$testarray[$i][1].'] (2nd test)<br/>';
$nfccounter++;
}
}
}
print $nfdcounter.' NFD tests failed.';
print $nfccounter.' NFC tests failed.';
?>