Projekt

Obecné

Profil

Stáhnout (1.76 KB) Statistiky
| Větev: | Revize:
1
"use strict";
2

    
3
// Which Unicode version should be used?
4
const version = "9.0.0";
5

    
6
const start = require("unicode-" + version + "/Binary_Property/ID_Start/code-points.js")
7
    .filter(function(ch) { return ch > 0x7f; });
8
let last = -1;
9
const cont = [0x200c, 0x200d].concat(
10
  require("unicode-" + version + "/Binary_Property/ID_Continue/code-points.js")
11
    .filter(function(ch) {
12
      return ch > 0x7f && search(start, ch, last + 1) == -1;
13
    })
14
  );
15

    
16
function search(arr, ch, starting) {
17
  for (let i = starting; arr[i] <= ch && i < arr.length; last = i++)
18
    if (arr[i] === ch)
19
      return i;
20
  return -1;
21
}
22

    
23
function pad(str, width) {
24
  while (str.length < width) str = "0" + str;
25
  return str;
26
}
27

    
28
function esc(code) {
29
  const hex = code.toString(16);
30
  if (hex.length <= 2) return "\\x" + pad(hex, 2);
31
  else return "\\u" + pad(hex, 4);
32
}
33

    
34
function generate(chars) {
35
  const astral = [];
36
  let re = "";
37
  for (let i = 0, at = 0x10000; i < chars.length; i++) {
38
    const from = chars[i];
39
    let to = from;
40
    while (i < chars.length - 1 && chars[i + 1] == to + 1) {
41
      i++;
42
      to++;
43
    }
44
    if (to <= 0xffff) {
45
      if (from == to) re += esc(from);
46
      else if (from + 1 == to) re += esc(from) + esc(to);
47
      else re += esc(from) + "-" + esc(to);
48
    } else {
49
      astral.push(from - at, to - from);
50
      at = to;
51
    }
52
  }
53
  return { nonASCII: re, astral: astral };
54
}
55

    
56
const startData = generate(start);
57
const contData = generate(cont);
58

    
59
console.log("let nonASCIIidentifierStartChars = \"" + startData.nonASCII + "\";");
60
console.log("let nonASCIIidentifierChars = \"" + contData.nonASCII + "\";");
61
console.log("const astralIdentifierStartCodes = " + JSON.stringify(startData.astral) + ";");
62
console.log("const astralIdentifierCodes = " + JSON.stringify(contData.astral) + ";");
(2-2/2)