|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\String\Data; |
| 13 | + |
| 14 | +use Symfony\Component\HttpClient\HttpClient; |
| 15 | +use Symfony\Component\String\Exception\RuntimeException; |
| 16 | +use Symfony\Component\VarExporter\VarExporter; |
| 17 | + |
| 18 | +/** |
| 19 | + * @internal |
| 20 | + */ |
| 21 | +final class WcswidthDataGenerator |
| 22 | +{ |
| 23 | + private $outDir; |
| 24 | + |
| 25 | + private $client; |
| 26 | + |
| 27 | + public function __construct(string $outDir) |
| 28 | + { |
| 29 | + $this->outDir = $outDir; |
| 30 | + |
| 31 | + $this->client = HttpClient::createForBaseUri('https://www.unicode.org/Public/UNIDATA/'); |
| 32 | + } |
| 33 | + |
| 34 | + public function generate(): void |
| 35 | + { |
| 36 | + $this->writeWideWidthData(); |
| 37 | + |
| 38 | + $this->writeZeroWidthData(); |
| 39 | + } |
| 40 | + |
| 41 | + private function writeWideWidthData(): void |
| 42 | + { |
| 43 | + if (!preg_match('/^# EastAsianWidth-(\d+\.\d+\.\d+)\.txt/', $content = $this->client->request('GET', 'EastAsianWidth.txt')->getContent(), $matches)) { |
| 44 | + throw new RuntimeException('The Unicode version could not be determined.'); |
| 45 | + } |
| 46 | + |
| 47 | + $version = $matches[1]; |
| 48 | + |
| 49 | + if (!preg_match_all('/^([A-H\d]{4,})(?:\.\.([A-H\d]{4,}))?;[W|F]/m', $content, $matches, PREG_SET_ORDER)) { |
| 50 | + throw new RuntimeException('The wide width pattern did not match anything.'); |
| 51 | + } |
| 52 | + |
| 53 | + $this->write('wcswidth_table_wide.php', $version, $matches); |
| 54 | + } |
| 55 | + |
| 56 | + private function writeZeroWidthData(): void |
| 57 | + { |
| 58 | + if (!preg_match('/^# DerivedGeneralCategory-(\d+\.\d+\.\d+)\.txt/', $content = $this->client->request('GET', 'extracted/DerivedGeneralCategory.txt')->getContent(), $matches)) { |
| 59 | + throw new RuntimeException('The Unicode version could not be determined.'); |
| 60 | + } |
| 61 | + |
| 62 | + $version = $matches[1]; |
| 63 | + |
| 64 | + if (!preg_match_all('/^([A-H\d]{4,})(?:\.\.([A-H\d]{4,}))? *; (?:Me|Mn)/m', $content, $matches, PREG_SET_ORDER)) { |
| 65 | + throw new RuntimeException('The zero width pattern did not match anything.'); |
| 66 | + } |
| 67 | + |
| 68 | + $this->write('wcswidth_table_zero.php', $version, $matches); |
| 69 | + } |
| 70 | + |
| 71 | + private function write(string $fileName, string $version, array $rawData): void |
| 72 | + { |
| 73 | + $content = $this->getHeader($version).'return '.VarExporter::export($this->format($rawData)).";\n"; |
| 74 | + |
| 75 | + if (false == file_put_contents($this->outDir.'/'.$fileName, $content)) { |
| 76 | + throw new RuntimeException(sprintf('The "%s" file could not be written.', $fileName)); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private function getHeader(string $version): string |
| 81 | + { |
| 82 | + $date = (new \DateTimeImmutable())->format('c'); |
| 83 | + |
| 84 | + return <<<EOT |
| 85 | +<?php |
| 86 | +
|
| 87 | +/* |
| 88 | + * This file is part of the Symfony package. |
| 89 | + * |
| 90 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 91 | + * |
| 92 | + * For the full copyright and license information, please view the LICENSE |
| 93 | + * file that was distributed with this source code. |
| 94 | + */ |
| 95 | +
|
| 96 | +/* |
| 97 | + * This file has been auto-generated by the Symfony String Component for internal use. |
| 98 | + * |
| 99 | + * Unicode version: $version |
| 100 | + * Date: $date |
| 101 | + */ |
| 102 | +
|
| 103 | +
|
| 104 | +EOT; |
| 105 | + } |
| 106 | + |
| 107 | + private function format(array $rawData): array |
| 108 | + { |
| 109 | + $data = array_map(static function (array $row): array { |
| 110 | + $start = $row[1]; |
| 111 | + $end = $row[2] ?? $start; |
| 112 | + |
| 113 | + return [hexdec($start), hexdec($end)]; |
| 114 | + }, $rawData); |
| 115 | + |
| 116 | + usort($data, static function (array $a, array $b) { |
| 117 | + return $a[0] - $b[0]; |
| 118 | + }); |
| 119 | + |
| 120 | + return $data; |
| 121 | + } |
| 122 | +} |
0 commit comments