php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
gen_rare_cp_bitvec.php
Go to the documentation of this file.
1#!/usr/bin/env php
2<?php
3
4if ($argc < 2) {
5 echo "Usage: php gen_rare_cp_bitvec.php ./common_codepoints.txt\n";
6 return;
7}
8
9$bitvec = array_fill(0, (0xFFFF / 32) + 1, 0xFFFFFFFF);
10
12foreach (explode("\n", $input) as $line) {
13 if (false !== $hashPos = strpos($line, '#')) {
14 $line = substr($line, 0, $hashPos);
15 }
16
17 $line = trim($line);
18 if ($line === '') {
19 continue;
20 }
21
22 $range = explode("\t", $line);
23 $start = hexdec($range[0]);
24 $end = hexdec($range[1]);
25
26 for ($i = $start; $i <= $end; $i++) {
27 $bitvec[$i >> 5] &= ~(1 << ($i & 0x1F));
28 }
29}
30
31$result = <<<'HEADER'
32/* Machine-generated file; do not edit! See gen_rare_cp_bitvec.php.
33 *
34 * The below array has one bit for each Unicode codepoint from U+0000 to U+FFFF.
35 * The bit is 1 if the codepoint is considered 'rare' for the purpose of
36 * guessing the text encoding of a string.
37 *
38 * Each 'rare' codepoint which appears in a string when it is interpreted
39 * using a candidate encoding causes the candidate encoding to be treated
40 * as less likely to be the correct one.
41 */
42
43static const uint32_t rare_codepoint_bitvec[] = {
44HEADER;
45
46for ($i = 0; $i < 0xFFFF / 32; $i++) {
47 if ($i % 8 === 0) {
48 $result .= "\n";
49 } else {
50 $result .= " ";
51 }
52
53 $result .= "0x" . str_pad(dechex($bitvec[$i]), 8, '0', STR_PAD_LEFT) . ",";
54}
55
56$result .= "\n};\n";
57
58file_put_contents(__DIR__ . '/rare_cp_bitvec.h', $result);
59
60echo "Done.\n";
61?>
array_fill(int $start_index, int $count, mixed $value)
hexdec(string $hex_string)
trim(string $string, string $characters=" \n\r\t\v\0")
file_get_contents(string $filename, bool $use_include_path=false, $context=null, int $offset=0, ?int $length=null)
explode(string $separator, string $string, int $limit=PHP_INT_MAX)
strpos(string $haystack, string $needle, int $offset=0)
dechex(int $num)
file_put_contents(string $filename, mixed $data, int $flags=0, $context=null)
str_pad(string $string, int $length, string $pad_string=" ", int $pad_type=STR_PAD_RIGHT)
const STR_PAD_LEFT
substr(string $string, int $offset, ?int $length=null)
foreach(explode("\n", $input) as $line) $result
if($argc< 2) $bitvec