/
create_category_function.js
executable file
·197 lines (182 loc) · 5.92 KB
/
create_category_function.js
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
#!/usr/bin/nodejs
// Copyright (C) 2012-2016 Jacob R. Lifshay
// This file is part of Voxels.
//
// Voxels is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Voxels is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Voxels; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
// MA 02110-1301, USA.
//
//
var fs = require("fs");
if(process.argv.length != 3 || process.argv[2] == '' || process.argv[2].substr(0, 1) == '-')
{
console.log("usage: ./create_category_function.js <unicode category>");
console.log("generates a C++ function that recognizes the Unicode category <unicode category>.");
process.exit(1);
}
var generateCategory = process.argv[2];
function writeProgress(progress)
{
if(progress == null)
console.log("done. ");
else
process.stdout.write("loading... " + (' ' + Math.floor(progress).toString()).substr(-2) + "%\r");
}
function loadUnicodeData()
{
var unicodeData = fs.readFileSync("UCD/UnicodeData.txt", {encoding:"utf-8"}).split('\n');
var characterCategory = [];
console.assert(unicodeData.length > 0);
if(unicodeData[unicodeData.length - 1] === '')
{
unicodeData.pop();
}
for(var i = 0; i < unicodeData.length; i++)
{
var line = unicodeData[i].split(';');
var category = line[2];
console.assert(line.length == 15);
if(/^<[^>]+, First>$/.test(line[1]))
{
i++;
console.assert(i < unicodeData.length);
var line2 = unicodeData[i].split(';');
console.assert(line2.length == 15);
console.assert(/^<[^>]+, Last>$/.test(line2[1]));
var start = +('0x' + line[0]);
var end = +('0x' + line2[0]);
for(var j = start; j <= end; j++)
{
characterCategory[j] = category;
}
}
else
{
characterCategory[+('0x' + line[0])] = category;
}
}
return characterCategory;
}
var characterCategory = loadUnicodeData();
function Match(start, end, includeEven, includeOdd)
{
this.start = start >>> 0;
this.end = end >>> 0;
this.includeEven = !!includeEven;
this.includeOdd = !!includeOdd;
}
Match.getHex = function(v)
{
v >>>= 0;
var s = '0x' + v.toString(16).toUpperCase() + 'U';
if(v > 0xFFFF)
s += 'L';
return s;
}
Match.prototype =
{
isSingle: function()
{
return this.start == this.end;
},
includes: function(codePoint)
{
codePoint >>>= 0;
return codePoint >= this.start && codePoint <= this.end && (codePoint & 1 ? this.includeEven : this.includeOdd);
},
toString: function(varName)
{
if(!varName)
varName = 'codePoint'
if(this.start == this.end)
{
return varName + ' == ' + Match.getHex(this.start);
}
if(this.includeEven && this.includeOdd)
{
if(this.start == this.end - 1)
{
return varName + ' == ' + Match.getHex(this.start) + ' || ' + varName + ' == ' + Match.getHex(this.end);
}
return '(' + varName + ' >= ' + Match.getHex(this.start) + ' && ' + varName + ' <= ' + Match.getHex(this.end) + ')';
}
if(this.includeEven)
{
return '(' + varName + ' >= ' + Match.getHex(this.start) + ' && ' + varName + ' <= ' + Match.getHex(this.end) + ' && ' + varName + ' % 2 == 0)';
}
return '(' + varName + ' >= ' + Match.getHex(this.start) + ' && ' + varName + ' <= ' + Match.getHex(this.end) + ' && ' + varName + ' % 2 != 0)';
},
};
var matches = [];
for(var codePoint = 0, lastMatched = false; codePoint < characterCategory.length; codePoint++)
{
if(characterCategory[codePoint] === generateCategory)
{
if(!lastMatched)
{
matches.push(new Match(codePoint, codePoint, true, true));
}
else
{
matches[matches.length - 1].end = codePoint;
}
lastMatched = true;
}
else
{
lastMatched = false;
}
}
var oldMatches = matches;
matches = [];
for(var i = 0; i < oldMatches.length; i++)
{
var match = oldMatches[i];
if(matches.length === 0 || match.end != match.start)
{
matches.push(match);
continue;
}
var lastMatch = matches[matches.length - 1];
var canCombine = match.start == lastMatch.end + 2;
if(canCombine)
{
if(match.start & 1)
canCombine = lastMatch.includeOdd && (lastMatch.start == lastMatch.end || !lastMatch.includeEven);
else
canCombine = lastMatch.includeEven && (lastMatch.start == lastMatch.end || !lastMatch.includeOdd);
}
if(canCombine)
{
lastMatch.end = match.start;
if(match.start & 1)
lastMatch.includeEven = false;
else
lastMatch.includeOdd = false;
}
else
{
matches.push(match);
}
}
var code = matches.join(' || ');
console.log('constexpr bool category' + generateCategory + '(std::uint32_t codePoint) noexcept\n{\n return ' + code.replace(/ \|\| /g, ' ||\n ') + ';\n}\n');
var testFn = new Function('codePoint', 'codePoint >>>= 0; return ' + code.replace(/(0x[A-F0-9]+)UL?/g, '$1'));
for(var codePoint = 0; codePoint < characterCategory.length; codePoint++)
{
if(testFn(codePoint) !== (characterCategory[codePoint] === generateCategory))
{
throw new Error('mismatch at ' + Match.getHex(codePoint));
}
}