File: generateCaseFoldingArray.php

package info (click to toggle)
php-algo26-idna-convert 4.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 684 kB
  • sloc: php: 5,821; makefile: 18; xml: 12
file content (54 lines) | stat: -rw-r--r-- 1,243 bytes parent folder | download
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
<?php

$inputFile = __DIR__ . '/CaseFolding.txt';
$outputFile = __DIR__ . '/../src/NamePrep/CaseFoldingData.php';

$map = [];

// Datei zeilenweise lesen
foreach (file($inputFile) as $line) {
    // Kommentare und leere Zeilen überspringen
    if (empty($line) || $line[0] === '#') {
        continue;
    }

    [$code, $status, $mapping] = array_map('trim', explode(';', $line));

    // ignore mapping irrelevant for IDNA2008
    if (!in_array($status, ['C', 'F'], true)) {
        continue;
    }
    // For IDNA2008, ß must not be case-folded to "ss"
    if (strtoupper($code) === '00DF') {
        continue;
    }

    $from = hexdec($code);
    $to = array_map('hexdec', explode(' ', $mapping));

    $map[$from] = $to;
}

$output = '<?php

declare(strict_types=1);

namespace Algo26\IdnaConvert\NamePrep;

/**
 * @codeCoverageIgnore character maps
 */
class CaseFoldingData implements CaseFoldingDataInterface
{
    public array $foldingMap = [
';
foreach ($map as $from => $to) {
    $fromHex = sprintf('0x%X', $from);
    $toHex = array_map(fn($v) => sprintf('0x%X', $v), $to);

    $output .= '        ' . $fromHex . ' => [' . join(', ', $toHex) .'],' . PHP_EOL;
}
$output .= '    ];
}
';
file_put_contents($outputFile, $output);