dataDir = $dataDir ?: __DIR__ . '/../../data'; $this->distDir = $distDir ?: __DIR__ . '/../../dist'; } /** {@inheritdoc} */ protected function configure() { $this->setName( 'generate-equivset' ); $this->setDescription( 'Generate the JSON, PHP, and plain text versions of the equivset in `./dist`' ); } /** {@inheritdoc} */ public function execute( InputInterface $input, OutputInterface $output ) { // phpcs:ignore Generic.PHP.NoSilencedErrors $fp = @fopen( $this->dataDir . '/equivset.in', 'rb' ); if ( $fp === false ) { throw new RuntimeException( "Unable to open equivset.in" ); } $lineNum = 0; $setsByChar = []; $sets = []; $exitStatus = 0; $lastChar = null; // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition while ( ( $line = fgets( $fp ) ) !== false ) { $lineNum++; $line = trim( $line ); # Filter comments if ( !$line || $line[0] == '#' ) { continue; } # Process line if ( !preg_match( '/^(?P[0-9A-F]+) +(?P.) +=> +' . '(?:(?P[0-9A-F]+) +(?P.)|(?Pinvisible)|(?Pspace))$/u', $line, $m, PREG_UNMATCHED_AS_NULL ) ) { $output->writeln( "Error: invalid entry at line $lineNum: $line" ); $exitStatus = 1; continue; } $error = false; if ( mb_chr( hexdec( $m['hexleft'] ) ) !== $m['charleft'] ) { $actual = strtoupper( dechex( mb_ord( $m['charleft'] ) ) ); $output->writeln( "Error: left number ({$m['hexleft']}) does not match left " . "character ($actual) at line $lineNum: $line" ); $error = true; } if ( isset( $m['invisible'] ) ) { $m['charright'] = ''; } elseif ( isset( $m['space'] ) ) { $m['charright'] = ' '; } elseif ( mb_chr( hexdec( $m['hexright'] ) ) !== $m['charright'] ) { $actual = strtoupper( dechex( mb_ord( $m['charright'] ) ) ); $output->writeln( "Error: right number ({$m['hexright']}) does not match right " . "character ($actual) at line $lineNum: $line" ); $error = true; } if ( $m['charleft'] === $m['charright'] ) { $output->writeln( "Error: {$m['hexright']} maps to itself" ); $error = true; } if ( isset( $setsByChar[$m['charleft']] ) ) { $output->writeln( "Error: Duplicate character ({$m['charleft']}) " . "at line $lineNum: $line" ); $error = true; } if ( $lastChar !== null && $m['charleft'] < $lastChar ) { $output->writeln( "Error: Characters not in order based on hex-value ({$m['charleft']}) " . "at line $lineNum: $line" ); $error = true; } else { $lastChar = $m['charleft']; } if ( $error ) { $exitStatus = 1; continue; } # Find the set for the right character, add a new one if necessary $setName = $setsByChar[$m['charright']] ?? $m['charright']; // Circle detected, one edge in every circle is redundant and can just be ignored if ( $setName === $m['charleft'] ) { continue; } $sets[$setName] ??= [ $setName ]; // When a mapping between two chars exists before one of them gets the final set, a merge is needed if ( isset( $sets[$m['charleft']] ) ) { foreach ( $sets[$m['charleft']] as $char ) { $setsByChar[$char] = $setName; $sets[$setName][] = $char; } unset( $sets[$m['charleft']] ); } else { $setsByChar[$m['charleft']] = $setName; $sets[$setName][] = $m['charleft']; } } $header = [ 'This file is generated by `bin/console generate-equivset`', 'It contains a map of characters, encoded in UTF-8, such that running', 'strtr() on a string with this map will cause confusable characters to', 'be reduced to a canonical representation.', ]; // JSON $data = json_encode( [ '_readme' => implode( ' ', $header ) ] + $setsByChar, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE ); $data = preg_replace( '/^ +/m', "\t", $data ); file_put_contents( $this->distDir . '/equivset.json', $data ); // PHP file file_put_contents( $this->distDir . '/equivset.php', self::generatePHP( $setsByChar, $header ) ); // Text File. uksort( $sets, [ self::class, 'compareCodePoints' ] ); touch( $this->distDir . '/equivset.txt' ); $textFile = fopen( $this->distDir . '/equivset.txt', 'w' ); foreach ( $sets as $members ) { $setName = array_shift( $members ); usort( $members, [ self::class, 'compareCodePoints' ] ); fwrite( $textFile, $setName . ' ' . implode( ' ', $members ) . "\n" ); } fclose( $textFile ); if ( $exitStatus > 0 ) { $output->writeln( 'Finished with errors' ); } else { $output->writeln( 'Finished' ); } return $exitStatus; } private static function compareCodePoints( string $a, string $b ): int { if ( $a === '' ) { return -1; } elseif ( $b === '' ) { return 1; } return mb_ord( $a ) - mb_ord( $b ); } /** * @param string[] $data * @param string[] $header * @return string */ private static function generatePHP( array $data, array $header ): string { $s = " $value ) { $s .= "\t" . var_export( (string)$key, true ) . ' => ' . var_export( $value, true ) . ",\n"; } $s .= "];\n"; return $s; } }