gen_rare_cp_bitvec.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env php
  2. <?php
  3. if ($argc < 2) {
  4. echo "Usage: php gen_rare_cp_bitvec.php ./common_codepoints.txt\n";
  5. return;
  6. }
  7. $bitvec = array_fill(0, (0xFFFF / 32) + 1, 0xFFFFFFFF);
  8. $input = file_get_contents($argv[1]);
  9. foreach (explode("\n", $input) as $line) {
  10. if (false !== $hashPos = strpos($line, '#')) {
  11. $line = substr($line, 0, $hashPos);
  12. }
  13. $line = trim($line);
  14. if ($line === '') {
  15. continue;
  16. }
  17. $range = explode("\t", $line);
  18. $start = hexdec($range[0]);
  19. $end = hexdec($range[1]);
  20. for ($i = $start; $i <= $end; $i++) {
  21. $bitvec[$i >> 5] &= ~(1 << ($i & 0x1F));
  22. }
  23. }
  24. $result = <<<'HEADER'
  25. /* Machine-generated file; do not edit! See gen_rare_cp_bitvec.php.
  26. *
  27. * The below array has one bit for each Unicode codepoint from U+0000 to U+FFFF.
  28. * The bit is 1 if the codepoint is considered 'rare' for the purpose of
  29. * guessing the text encoding of a string.
  30. *
  31. * Each 'rare' codepoint which appears in a string when it is interpreted
  32. * using a candidate encoding causes the candidate encoding to be treated
  33. * as less likely to be the correct one.
  34. */
  35. static uint32_t rare_codepoint_bitvec[] = {
  36. HEADER;
  37. for ($i = 0; $i < 0xFFFF / 32; $i++) {
  38. if ($i % 8 === 0) {
  39. $result .= "\n";
  40. } else {
  41. $result .= " ";
  42. }
  43. $result .= "0x" . str_pad(dechex($bitvec[$i]), 8, '0', STR_PAD_LEFT) . ",";
  44. }
  45. $result .= "\n};\n";
  46. file_put_contents(__DIR__ . '/rare_cp_bitvec.h', $result);
  47. echo "Done.\n";
  48. ?>