Repository navigation
Expand file tree
/
Copy pathgenerate.ts
More file actions
251 lines (232 loc) · 8.75 KB
/
Copy pathgenerate.ts
File metadata and controls
251 lines (232 loc) · 8.75 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import { closeSync, openSync } from "node:fs";
const unicode_version = "18.0.0";
const encoder = new TextEncoder();
function stringEscape(s?: string) {
if (!s) return "";
return Array.from(encoder.encode(s.replace(/\\u([0-9A-F]{4})/g, (s) => String.fromCodePoint(parseInt(s.slice(2), 16)))))
.map((x) => {
if (x === 0x09) return "\\t";
if (x === 0x0a) return "\\n";
if (x === 0x0d) return "\\r";
if (x === 0x20) return " ";
if (x === 0x21) return "!";
if (x === 0x22) return `\\"`;
if (x >= 0x23 && x <= 0x26) return String.fromCodePoint(x);
if (x === 0x27) return "'";
if (x >= 0x28 && x <= 0x5b) return String.fromCodePoint(x);
if (x === 0x5c) return "\\\\";
if (x >= 0x5d && x <= 0x7e) return String.fromCodePoint(x);
return `\\x${x.toString(16).padStart(2, "0")}`;
})
.join("");
}
const E = stringEscape;
{
const source = `https://www.unicode.org/Public/${unicode_version}/idna/IdnaMappingTable.txt`;
const response = await fetch(source);
const data = await response.text();
const lines = data.split("\n").filter((x) => !x.startsWith("#") && x.length > 0);
const lines_clear = lines.map((x) => x.split("#")[0]!);
const cols = lines_clear.map((x) => x.split(";").map((y) => y.trim()));
closeSync(openSync("./table.zig", "w", 0o777));
const f = Bun.file("./table.zig");
const w = f.writer();
w.write(`// This file is part of Unicode IDNA Compatibility Processing\n`);
w.write(`// For documentation, see http://www.unicode.org/reports/tr46/\n`);
w.write(`//\n`);
w.write(`\n`);
w.write(`// Based on the source file: ${source}\n`);
w.write(`//\n`);
w.write(`\n`);
w.write(`pub const Row = struct {\n`);
w.write(` cp: u21,\n`);
w.write(` status: Status,\n`);
w.write(` mapping: []const u21,\n`);
w.write(` status2: Status2008,\n`);
w.write(`};\n`);
w.write(`\n`);
w.write(`pub const RowRange = struct {\n`);
w.write(` from: u21,\n`);
w.write(` to: u21,\n`);
w.write(` status: Status,\n`);
w.write(` mapping: []const u21,\n`);
w.write(` status2: Status2008,\n`);
w.write(`};\n`);
w.write(`\n`);
w.write(`pub const Status = enum {\n`);
w.write(` valid,\n`);
w.write(` ignored,\n`);
w.write(` mapped,\n`);
w.write(` deviation,\n`);
w.write(` disallowed,\n`);
w.write(`};\n`);
w.write(`\n`);
w.write(`pub const Status2008 = enum {\n`);
w.write(` none,\n`);
w.write(` NV8,\n`);
w.write(` XV8,\n`);
w.write(`};\n`);
w.write(`\n`);
w.write(`pub const data = [_]Row{\n`);
{
for (const c of cols.filter((x) => !x[0]!.includes(".."))) {
w.write(` .{ .cp = 0x${c[0]},`);
w.write(` .status = .${c[1]},`);
// prettier-ignore
w.write(` .mapping = &.{${c[2]?.split(" ").filter(x => x.length > 0).map((x) => `0x${x}`).join(",") ?? ""}},`,);
w.write(` .status2 = .${c[3] ?? "none"},`);
w.write(` },\n`);
}
}
w.write(`};\n`);
w.write(`\n`);
w.write(`pub const data_range = [_]RowRange{\n`);
{
for (const c of cols.filter((x) => x[0]!.includes(".."))) {
w.write(` .{ .from = 0x${c[0]!.split("..")[0]}, .to = 0x${c[0]!.split("..")[1]},`);
w.write(` .status = .${c[1]},`);
// prettier-ignore
w.write(` .mapping = &.{${c[2]?.split(" ").filter(x => x.length > 0).map((x) => `0x${x}`).join(",") ?? ""}},`,);
w.write(` .status2 = .${c[3] ?? "none"},`);
w.write(` },\n`);
}
}
w.write(`};\n`);
w.flush();
}
{
const source = `https://www.unicode.org/Public/${unicode_version}/idna/Idna2008.txt`;
const response = await fetch(source);
const data = await response.text();
const lines = data.split("\n").filter((x) => !x.startsWith("#") && x.length > 0);
const lines_clear = lines.map((x) => x.split("#")[0]!);
const cols = lines_clear.map((x) => x.split(";").map((y) => y.trim()));
closeSync(openSync("./2008.zig", "w", 0o777));
const f = Bun.file("./2008.zig");
const w = f.writer();
w.write(`// This file is part of Unicode IDNA Compatibility Processing\n`);
w.write(`// For documentation, see http://www.unicode.org/reports/tr46/\n`);
w.write(`//\n`);
w.write(`\n`);
w.write(`// Based on the source file: ${source}\n`);
w.write(`//\n`);
w.write(`\n`);
w.write(`pub const Prop = struct {\n`);
w.write(` cp: u21,\n`);
w.write(` category: Category,\n`);
w.write(`};\n`);
w.write(`\n`);
w.write(`pub const PropRange = struct {\n`);
w.write(` from: u21,\n`);
w.write(` to: u21,\n`);
w.write(` category: Category,\n`);
w.write(`};\n`);
w.write(`\n`);
w.write(`pub const Category = enum {\n`);
w.write(` pvalid,\n`);
w.write(` contextj,\n`);
w.write(` contexto,\n`);
w.write(` disallowed,\n`);
w.write(` unassigned,\n`);
w.write(`};\n`);
w.write(`\n`);
w.write(`pub const data = [_]Prop{\n`);
{
for (const c of cols.filter((x) => !x[0]!.includes(".."))) {
w.write(` .{ .cp = 0x${c[0]}, .category = .${c[1]!.toLowerCase()} },\n`);
}
}
w.write(`};\n`);
w.write(`\n`);
w.write(`pub const data_range = [_]PropRange{\n`);
{
for (const c of cols.filter((x) => x[0]!.includes(".."))) {
w.write(` .{ .from = 0x${c[0]!.split("..")[0]}, .to = 0x${c[0]!.split("..")[1]}, .category = .${c[1]!.toLowerCase()} },\n`);
}
}
w.write(`};\n`);
w.flush();
}
{
const source = `https://www.unicode.org/Public/${unicode_version}/idna/IdnaTestV2.txt`;
const response = await fetch(source);
const data = await response.text();
const lines = data.split("\n").filter((x) => !x.startsWith("#") && x.length > 0);
const lines_clear = lines.map((x) => x.split("#")[0]!);
const cols = lines_clear.map((x) => x.split(";").map((y) => y.trim()));
closeSync(openSync("./testv2.zig", "w", 0o777));
const f = Bun.file("./testv2.zig");
const w = f.writer();
w.write(`// This file is part of Unicode IDNA Compatibility Processing\n`);
w.write(`// For documentation, see http://www.unicode.org/reports/tr46/\n`);
w.write(`//\n`);
w.write(`\n`);
w.write(`// Based on the source file: ${source}\n`);
w.write(`//\n`);
w.write(`// zig fmt: off\n`);
w.write(`\n`);
w.write(`const std = @import("std");\n`);
w.write(`const idna = @import("unicode-idna");\n`);
w.write(`const expect = @import("expect").expect;\n`);
w.write(`
fn toUnicodePass(
source: []const u8,
expected: []const u8,
) !void {
const allocator = std.testing.allocator;
const result = try idna.ToUnicode(allocator, source, true, true, true, true, false, false);
defer allocator.free(result);
try expect(result).toEqualString(expected);
}
fn toUnicodeFail(
source: []const u8,
) !void {
const allocator = std.testing.allocator;
const result = idna.ToUnicode(allocator, source, true, true, true, true, false, false) catch |err| switch (err) {
error.IDNAFailure => return,
error.OutOfMemory => return error.OutOfMemory,
};
defer allocator.free(result);
return error.ShouldHaveFailed;
}
fn toAsciiPass(
source: []const u8,
expected: []const u8,
Transitional_Processing: bool,
) !void {
const allocator = std.testing.allocator;
const result = try idna.ToASCII(allocator, source, true, true, true, true, Transitional_Processing, true, false);
defer allocator.free(result);
try expect(result).toEqualString(expected);
}
fn toAsciiFail(
source: []const u8,
Transitional_Processing: bool,
) !void {
const allocator = std.testing.allocator;
const result = idna.ToASCII(allocator, source, true, true, true, true, Transitional_Processing, true, false) catch |err| switch (err) {
error.IDNAFailure => return,
error.OutOfMemory => return error.OutOfMemory,
};
defer allocator.free(result);
return error.ShouldHaveFailed;
}
`);
w.write(`\n`);
for (const i of cols) {
const source = i[0];
const toUnicode = i[1] || source;
const toUnicodeStatus = i[2] || "[]";
const toAsciiN = i[3] || toUnicode;
const toAsciiNStatus = i[4] || toUnicodeStatus;
const toAsciiT = i[5] || toAsciiN;
const toAsciiTStatus = i[6] || toAsciiNStatus;
if (toUnicodeStatus === "[]") w.write(`test { try toUnicodePass("${E(source)}", "${E(toUnicode)}"); }\n`);
if (toAsciiNStatus === "[]") w.write(`test { try toAsciiPass("${E(source)}", "${E(toAsciiN)}", false); }\n`);
if (toAsciiTStatus === "[]") w.write(`test { try toAsciiPass("${E(source)}", "${E(toAsciiT)}", true); }\n`);
if (toUnicodeStatus !== "[]" && toUnicodeStatus !== "[X4_2]") w.write(`test { try toUnicodeFail("${E(source)}"); } // ${toUnicodeStatus}\n`);
if (toAsciiNStatus !== "[]") w.write(`test { try toAsciiFail("${E(source)}", false); } // ${toAsciiNStatus}\n`);
if (toAsciiTStatus !== "[]") w.write(`test { try toAsciiFail("${E(source)}", true); } // ${toAsciiTStatus}\n`);
}
w.flush();
}