1 |
|
2 | import { decode, pair } from '../swu/swu-parse';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | const range = (min, max) => {
|
21 | if (min > max) return '';
|
22 | let pattern = '';
|
23 | let cnt, code, str;
|
24 | let re = [];
|
25 |
|
26 | min = pair(min);
|
27 | max = pair(max);
|
28 | if (min.length != 2 && max.length != 2) return '';
|
29 |
|
30 | if (min[0] == max[0]) {
|
31 | if (min[1] == max[1]) {
|
32 | pattern = '\\u' + min[0] + '\\u' + min[1];
|
33 | re.push(pattern);
|
34 | } else {
|
35 | pattern = '\\u' + min[0] + '[\\u' + min[1] + '-\\u' + max[1] + ']';
|
36 | re.push(pattern);
|
37 | }
|
38 | } else {
|
39 | if (min[1] == "DFFF") {
|
40 | pattern = '\\u' + min[0] + '\\uDFFF';
|
41 | } else {
|
42 | pattern = '\\u' + min[0] + '[\\u' + min[1] + '-\\uDFFF]';
|
43 | }
|
44 | re.push(pattern);
|
45 |
|
46 |
|
47 | let diff = (parseInt(max[0], 16)) - (parseInt(min[0], 16));
|
48 | if (diff == 2) {
|
49 | pattern = '\\u' + (parseInt(min[0], 16) + 1).toString(16).toUpperCase();
|
50 | pattern += '[\\uDC00-\\uDFFF]';
|
51 | re.push(pattern);
|
52 | }
|
53 | if (diff > 2) {
|
54 | pattern = '[';
|
55 | pattern += '\\u' + (parseInt(min[0], 16) + 1).toString(16).toUpperCase();
|
56 | pattern += '-\\u' + (parseInt(max[0], 16) - 1).toString(16).toUpperCase();
|
57 | pattern += '][\\uDC00-\\uDFFF]';
|
58 | re.push(pattern);
|
59 | }
|
60 |
|
61 |
|
62 | if (max[1] == "DC00") {
|
63 | pattern = '\\u' + max[0] + '\\uDC00';
|
64 | } else {
|
65 | pattern = '\\u' + max[0] + '[\\uDC00-\\u' + max[1] + ']';
|
66 | }
|
67 | re.push(pattern);
|
68 |
|
69 | }
|
70 | cnt = re.length;
|
71 | if (cnt == 1) {
|
72 | pattern = re[0];
|
73 | } else {
|
74 | pattern = re.join(')|(');
|
75 | pattern = '((' + pattern + '))';
|
76 | }
|
77 | return decode(pattern);
|
78 | }
|
79 |
|
80 | export { range } |
\ | No newline at end of file |