-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathpatchRegexpConvertor.php
executable file
·204 lines (165 loc) · 3.95 KB
/
patchRegexpConvertor.php
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/php
<?php
use s9e\TextFormatter\Configurator\Helpers\RegexpBuilder;
include __DIR__ . '/../vendor/autoload.php';
$filepath = __DIR__ . '/../src/Configurator/JavaScript/RegexpConvertor.php';
function generateRange($start, $end)
{
$str = pcreChr($start);
if ($end > $start)
{
if ($end > $start + 1)
{
$str .= '-';
}
$str .= pcreChr($end);
}
return $str;
}
function pcreChr($cp)
{
if ($cp >= 32 && $cp <= 126)
{
return preg_quote(chr($cp));
}
return '\\u' . sprintf('%04X', $cp);
}
function wget($url)
{
return file_get_contents(
'compress.zlib://' . $url,
false,
stream_context_create(['http' => ['header' => 'Accept-Encoding: gzip']])
);
}
if (!file_exists('/tmp/props.txt'))
{
file_put_contents(
'/tmp/props.txt',
wget('http://unicode.org/Public/UNIDATA/PropList.txt') . "\n" . wget('http://unicode.org/Public/UNIDATA/DerivedCoreProperties.txt')
);
}
$supportedProperties = array_flip([
'L&', 'Ll', 'Lm', 'Lo', 'Lt', 'Lu',
'Nd', 'Nl', 'No',
'Pc', 'Pd', 'Pe', 'Pf', 'Pi', 'Po', 'Ps',
'Sc', 'Sk', 'Sm', 'So',
'Zl', 'Zp', 'Zs'
]);
$lines = file('/tmp/props.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$ranges = [];
foreach ($lines as $line)
{
if ($line[0] === '#'
|| ($line[4] !== ' ' && $line[4] !== '.'))
{
// Ignore comments and stuff outside of the BMP
continue;
}
$propName = substr($line, strpos($line, '# ') + 2, 2);
if (!isset($supportedProperties[$propName]))
{
continue;
}
$range = substr($line, 0, 4) . substr($line, 6 * ($line[4] === '.'), 4);
$ranges['p' . $propName][$range] = false;
$ranges['p' . $propName[0]][$range] = false;
}
unset($ranges['pL&']);
// Sort ranges
foreach ($ranges as &$propRanges)
{
ksort($propRanges, SORT_STRING);
}
unset($propRanges);
// Create anti-ranges for \P properties
foreach ($ranges as $propName => $propRanges)
{
$nextCp = 0;
$tmp = [];
foreach (array_keys($propRanges) as $range)
{
$startCp = hexdec(substr($range, 0, 4));
if ($startCp - 1 > $nextCp)
{
$tmp[sprintf('%04X%04X', $nextCp, $startCp - 1)] = false;
}
$nextCp = max($nextCp, 1 + hexdec(substr($range, 4)));
}
if ($nextCp <= 0xFFFF)
{
$tmp[sprintf('%04X%04X', $nextCp, 0xFFFF)] = false;
}
$propName[0] = 'P';
$ranges[$propName] = $tmp;
}
$props = [];
foreach ($ranges as $propName => $propRanges)
{
$str = '';
unset($bufStart, $bufEnd);
foreach ($propRanges as $range => $void)
{
$curStart = hexdec(substr($range, 0, 4));
$curEnd = hexdec(substr($range, 4));
if (!isset($bufStart))
{
$bufStart = $curStart;
$bufEnd = $curEnd;
continue;
}
if ($curStart <= $bufEnd + 1)
{
// extend the buffered range
$bufEnd = max($bufEnd, $curEnd);
}
else
{
// dump the buffered range and unset it
$str .= generateRange($bufStart, $bufEnd);
$bufStart = $curStart;
$bufEnd = $curEnd;
}
}
if (isset($bufStart))
{
$str .= generateRange($bufStart, $bufEnd);
}
$props[$propName] = $str;
}
ksort($props);
$php = trim(var_export($props, true), "ary (\r\n),");
$php = str_replace("\r\n", "\n", $php);
$php = str_replace(' ', "\t\t", $php);
$php = " = [\n\t\t" . $php . "\n\t]";
$file = file_get_contents($filepath);
$file = preg_replace(
'#(protected static \\$unicodeProps(?!Regexp))(.*?)\\n\\t[\\])];#s',
'$1;',
$file
);
$file = str_replace(
'protected static $unicodeProps;',
'protected static $unicodeProps' . $php . ';',
$file
);
$propNames = [];
foreach (array_keys($props) as $propName)
{
$propNames[] = $propName;
$propNames[] = preg_replace('#(.)(.+)#', '$1{$2}', $propName);
$propNames[] = preg_replace('#(.)(.+)#', '$1{^$2}', $propName);
}
$regexp = '((?<!\\\\)((?:\\\\\\\\)*+)\\\\(' . RegexpBuilder::fromList($propNames) . '))';
$file = preg_replace(
'#(protected static \\$unicodePropsRegexp)[^;]++;#s',
'$1;',
$file
);
$file = str_replace(
'protected static $unicodePropsRegexp;',
'protected static $unicodePropsRegexp = ' . var_export($regexp, true) . ';',
$file
);
file_put_contents($filepath, $file);
echo "Done.\n";