'use strict';
const path = require('path');
const Markdoc = require(path.resolve(process.argv[2]));
const md = new (new Markdoc.Tokenizer().parser.constructor)({ linkify: true });
const re = md.linkify.re;
if (process.argv[3] === '--table') {
const sticky = (src) => new RegExp('(?:' + src + ')', 'iy');
const sepRe = sticky(re.get_text_separators().source);
const zccRe = sticky(re.src_ZCc);
const zpccRe = sticky(re.src_ZPCc);
const plRe = sticky(re.get_pseudo_letter().source);
const at = (r, s, i) => { r.lastIndex = i; const m = r.exec(s); return m ? m[0].length : 0; };
const LOW = String.fromCharCode(0xdc00);
const pats = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:/-._@' +
String.fromCharCode(0x440, 0x444, 0x420, 0x424);
const patRe = [...pats].map((p) => [p, new RegExp('^' + p.replace(/[.?*+^$[\]\\(){}|/-]/g, '\\$&') + '$', 'i')]);
const runs = (rows, first) => {
const out = [];
rows.forEach((row, i) => {
const key = JSON.stringify(row);
const last = out[out.length - 1];
if (last && last.key === key) last.end = first + i;
else out.push({ start: first + i, end: first + i, key, row });
});
return out.map((r) => [r.start, r.end, ...r.row]);
};
const bmp = [];
for (let c = 0; c <= 0xffff; c++) {
const u = String.fromCharCode(c);
const eq = patRe.filter(([, r]) => r.test(u)).map(([p]) => p).join('') || '-';
bmp.push([at(sepRe, u, 0), at(zccRe, u, 0), at(zpccRe, u, 0), at(plRe, u, 0),
at(plRe, u + 'a', 0), at(plRe, u + LOW, 0), at(plRe, 'a' + u, 1), at(plRe, 'a' + u + LOW, 1), eq]);
}
const astral = [];
for (let cp = 0x10000; cp <= 0x10ffff; cp++) {
const s = String.fromCodePoint(cp);
astral.push([at(zpccRe, s, 0), at(plRe, s, 0), at(plRe, s + 'a', 0), at(plRe, 'a' + s, 1)]);
}
const lines = ['{"bmp": ['];
const b = runs(bmp, 0);
b.forEach((r, i) => lines.push(' ' + JSON.stringify(r) + (i + 1 < b.length ? ',' : '')));
lines.push('], "astral": [');
const a = runs(astral, 0x10000);
a.forEach((r, i) => lines.push(' ' + JSON.stringify(r) + (i + 1 < a.length ? ',' : '')));
lines.push(']}');
process.stdout.write(lines.join('\n') + '\n');
process.exit(0);
}
const all = [];
for (let c = 0; c < 0x10000; c++) all.push(String.fromCharCode(c));
function setOf(src, flags) {
const r = new RegExp('^(?:' + src + ')$', flags);
const out = [];
for (let c = 0; c < 0x10000; c++) if (r.test(all[c])) out.push(c);
return out;
}
const same = (a, b) => a.length === b.length && a.every((x, i) => x === b[i]);
let bad = 0;
function check(name, src) {
const a = setOf(src, ''), b = setOf(src, 'i');
if (!same(a, b)) { bad++; console.log('DIFF', name, 'plain', a.length, 'i', b.length, b.filter(x => !a.includes(x)).map(x => x.toString(16)).slice(0, 20)); }
}
check('ZPCc', re.src_ZPCc);
check('ZCc', re.src_ZCc);
check('sep', '[><|]');
check('Any-bmp', '[\\0-\\uD7FF\\uE000-\\uFFFF]');
check('hex', '[0-9A-Fa-f]');
check('path-class', '[a-zA-Z0-9%/&]');
check('mail', "[-!#$%&'*+/=?^_`{|}~a-zA-Z0-9]");
check('digit', '\\d');
check('punct', '[/?#\\\\:%@&=_~*"\'.,;!?\\[\\](){}$+<=>^`|\\uff5c-]');
const letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZрфРФ';
for (const l of letters) {
const b = setOf(l.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&'), 'i');
const want = [l.toLowerCase().charCodeAt(0), l.toUpperCase().charCodeAt(0)].sort((x, y) => x - y);
if (!same(b, [...new Set(want)])) { bad++; console.log('LETTER', l, b.map(x => x.toString(16))); }
}
const xn = setOf('[a-z0-9\\-]', 'i');
const xnWant = setOf('[a-zA-Z0-9\\-]', '');
if (!same(xn, xnWant)) { bad++; console.log('xn class differs'); }
console.log(bad ? `${bad} differences` : 'ok: every class is closed under V8 non-unicode case folding; letters match only their ASCII/Cyrillic pair');
for (const [c, l] of [['ſ', 's'], ['ı', 'i'], ['İ', 'i'], ['K', 'k'], ['h', 'h'], ['ẞ', 's']]) {
console.log(JSON.stringify(c), 'matches', l, 'with i:', new RegExp(l, 'i').test(c));
}