-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchar-env.ts
247 lines (200 loc) · 6.54 KB
/
char-env.ts
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
import { CharRange, CharSet } from "../char-set";
import { Char } from "../char-types";
import { CharCaseFolding, getCharCaseFolding } from "./char-case-folding";
import { Flags, UnicodeSetsFlags } from "./flags";
import { Maximum } from "./maximum";
import { UnicodeCaseFolding, UnicodeCaseVarying } from "./unicode";
import { UTF16CaseFolding, UTF16CaseVarying } from "./utf16-case-folding";
interface CharEnvBase {
readonly maxCharacter: Char;
readonly all: CharSet;
readonly empty: CharSet;
readonly lineTerminator: CharSet;
readonly nonLineTerminator: CharSet;
readonly space: CharSet;
readonly nonSpace: CharSet;
readonly digit: CharSet;
readonly nonDigit: CharSet;
readonly word: CharSet;
readonly nonWord: CharSet;
}
export interface CharEnvIgnoreCase {
readonly ignoreCase: true;
readonly caseFolding: Readonly<Record<number, readonly Char[] | undefined>>;
readonly caseVarying: CharSet;
readonly withCaseVaryingCharacters: (cs: CharSet) => CharSet;
}
export interface CharEnvNonIgnoreCase {
readonly ignoreCase: false;
}
export interface CharEnvUnicode {
readonly unicode: true;
readonly charCaseFolding: CharCaseFolding;
}
export interface CharEnvNonUnicode {
readonly unicode: false;
}
export type CharEnv = CharEnvBase & (CharEnvUnicode | CharEnvNonUnicode) & (CharEnvIgnoreCase | CharEnvNonIgnoreCase);
const DIGIT: readonly CharRange[] = [
{ min: 0x30, max: 0x39 }, // 0-9
];
const SPACE: readonly CharRange[] = [
{ min: 0x09, max: 0x0d }, // \t \n \v \f \r
{ min: 0x20, max: 0x20 }, // space
{ min: 0xa0, max: 0xa0 }, // non-breaking space
{ min: 0x1680, max: 0x1680 },
{ min: 0x2000, max: 0x200a },
{ min: 0x2028, max: 0x2029 },
{ min: 0x202f, max: 0x202f },
{ min: 0x205f, max: 0x205f },
{ min: 0x3000, max: 0x3000 },
{ min: 0xfeff, max: 0xfeff },
];
const WORD: readonly CharRange[] = [
{ min: 0x30, max: 0x39 }, // 0-9
{ min: 0x41, max: 0x5a }, // A-Z
{ min: 0x5f, max: 0x5f }, // _
{ min: 0x61, max: 0x7a }, // a-z
];
const LINE_TERMINATOR: readonly CharRange[] = [
{ min: 0x0a, max: 0x0a }, // \n
{ min: 0x0d, max: 0x0d }, // \r
{ min: 0x2028, max: 0x2029 },
];
const lineTerminatorUTF16 = CharSet.empty(Maximum.UTF16).union(LINE_TERMINATOR);
const nonLineTerminatorUTF16 = lineTerminatorUTF16.negate();
const lineTerminatorUnicode = CharSet.empty(Maximum.UNICODE).union(LINE_TERMINATOR);
const nonLineTerminatorUnicode = lineTerminatorUnicode.negate();
const spaceUTF16 = CharSet.empty(Maximum.UTF16).union(SPACE);
const nonSpaceUTF16 = spaceUTF16.negate();
const spaceUnicode = CharSet.empty(Maximum.UNICODE).union(SPACE);
const nonSpaceUnicode = spaceUnicode.negate();
const digitUTF16 = CharSet.empty(Maximum.UTF16).union(DIGIT);
const nonDigitUTF16 = digitUTF16.negate();
const digitUnicode = CharSet.empty(Maximum.UNICODE).union(DIGIT);
const nonDigitUnicode = digitUnicode.negate();
const wordUTF16 = CharSet.empty(Maximum.UTF16).union(WORD);
const nonWordUTF16 = wordUTF16.negate();
const wordUnicode = CharSet.empty(Maximum.UNICODE).union(WORD);
const nonWordUnicode = wordUnicode.negate();
const wordIgnoreCaseUnicode = withCaseVaryingCharacters(
CharSet.empty(Maximum.UNICODE).union(WORD),
UnicodeCaseFolding,
UnicodeCaseVarying
);
const nonWordIgnoreCaseUnicode = wordIgnoreCaseUnicode.negate();
const CHAR_ENV: CharEnv = {
// base
maxCharacter: Maximum.UTF16,
all: CharSet.all(Maximum.UTF16),
empty: CharSet.empty(Maximum.UTF16),
lineTerminator: lineTerminatorUTF16,
nonLineTerminator: nonLineTerminatorUTF16,
space: spaceUTF16,
nonSpace: nonSpaceUTF16,
digit: digitUTF16,
nonDigit: nonDigitUTF16,
word: wordUTF16,
nonWord: nonWordUTF16,
// ignore case
ignoreCase: false,
// unicode
unicode: false,
};
const CHAR_ENV_I: CharEnv = {
// base
maxCharacter: Maximum.UTF16,
all: CharSet.all(Maximum.UTF16),
empty: CharSet.empty(Maximum.UTF16),
lineTerminator: lineTerminatorUTF16,
nonLineTerminator: nonLineTerminatorUTF16,
space: spaceUTF16,
nonSpace: nonSpaceUTF16,
digit: digitUTF16,
nonDigit: nonDigitUTF16,
word: wordUTF16,
nonWord: nonWordUTF16,
// ignore case
ignoreCase: true,
caseFolding: UTF16CaseFolding,
caseVarying: UTF16CaseVarying,
withCaseVaryingCharacters: cs => withCaseVaryingCharacters(cs, UTF16CaseFolding, UTF16CaseVarying),
// unicode
unicode: false,
};
const CHAR_ENV_U: CharEnv = {
// base
maxCharacter: Maximum.UNICODE,
all: CharSet.all(Maximum.UNICODE),
empty: CharSet.empty(Maximum.UNICODE),
lineTerminator: lineTerminatorUnicode,
nonLineTerminator: nonLineTerminatorUnicode,
space: spaceUnicode,
nonSpace: nonSpaceUnicode,
digit: digitUnicode,
nonDigit: nonDigitUnicode,
word: wordUnicode,
nonWord: nonWordUnicode,
charCaseFolding: getCharCaseFolding(true, false),
// ignore case
ignoreCase: false,
// unicode
unicode: true,
};
const CHAR_ENV_IU: CharEnv = {
// base
maxCharacter: Maximum.UNICODE,
all: CharSet.all(Maximum.UNICODE),
empty: CharSet.empty(Maximum.UNICODE),
lineTerminator: lineTerminatorUnicode,
nonLineTerminator: nonLineTerminatorUnicode,
space: spaceUnicode,
nonSpace: nonSpaceUnicode,
digit: digitUnicode,
nonDigit: nonDigitUnicode,
word: wordIgnoreCaseUnicode,
nonWord: nonWordIgnoreCaseUnicode,
charCaseFolding: getCharCaseFolding(true, true),
// ignore case
ignoreCase: true,
caseFolding: UnicodeCaseFolding,
caseVarying: UnicodeCaseVarying,
withCaseVaryingCharacters: cs => withCaseVaryingCharacters(cs, UnicodeCaseFolding, UnicodeCaseVarying),
// unicode
unicode: true,
};
function withCaseVaryingCharacters(
cs: CharSet,
caseFolding: Readonly<Record<number, readonly Char[] | undefined>>,
caseVarying: CharSet
): CharSet {
if (cs.isSupersetOf(caseVarying)) {
// this set already includes all case varying characters
return cs;
}
const actualCaseVarying = cs.intersect(caseVarying);
if (actualCaseVarying.isEmpty) {
return cs;
}
const caseVariationSet = new Set<Char>();
for (const { min, max } of actualCaseVarying.ranges) {
for (let i = min; i <= max; i++) {
const fold = caseFolding[i]!;
for (let j = 0, l = fold.length; j < l; j++) {
caseVariationSet.add(fold[j]);
}
}
}
const caseVariationArray = [...caseVariationSet];
caseVariationArray.sort((a, b) => a - b);
return cs.union(CharSet.fromCharacters(cs.maximum, caseVariationArray));
}
export function getCharEnv(flags: Readonly<UnicodeSetsFlags>): CharEnv & CharEnvUnicode;
export function getCharEnv(flags: Readonly<Flags>): CharEnv;
export function getCharEnv(flags: Readonly<Flags>): CharEnv {
if (flags.unicode || flags.unicodeSets) {
return flags.ignoreCase ? CHAR_ENV_IU : CHAR_ENV_U;
} else {
return flags.ignoreCase ? CHAR_ENV_I : CHAR_ENV;
}
}