This repository has been archived by the owner on Dec 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
RegularExpressions.ts
342 lines (284 loc) · 7.51 KB
/
RegularExpressions.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*!
* @author electricessence / https://github.com/electricessence/
* Named groups based on: http://trentrichardson.com/2011/08/02/javascript-regexp-match-named-captures/
* Licensing: MIT https://github.com/electricessence/TypeScript.NET/blob/master/LICENSE.md
*/
// NOTE: Avoid real (types/interfaces only = ok) dependencies so this class can be used cleanly.
import {IMap} from "../../IMap";
import {Primitive} from "../Primitive";
import {SelectorWithIndex} from "../FunctionTypes";
import __extendsImport from "../../extends";
// noinspection JSUnusedLocalSymbols
const __extends = __extendsImport;
const EMPTY:string = "";
const _I = 'i', _G = 'g', _M = 'm', _U = 'u', _W = 'w', _Y = 'y';
/**
* https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regexoptions%28v=vs.110%29.aspx
*/
export module RegexOptions
{
export type Global = 'g';
export type IgnoreCase = 'i';
export type MultiLine = 'm';
export type Unicode = 'u';
export type Sticky = 'y';
export type IgnorePatternWhitespace = "w";
export type Literal =
Global
| IgnoreCase
| MultiLine
| Unicode
| Sticky
| IgnorePatternWhitespace;
/**
* Specifies case-insensitive matching. For more information, see the "Case-Insensitive Matching " section in the Regular Expression Options topic.
*/
export const IGNORE_CASE:IgnoreCase = _I;
export const I:IgnoreCase = _I;
/**
* Specifies global matching instead of single.
*/
export const GLOBAL:Global = _G;
export const G:Global = _G;
/**
* treat beginning and end characters (^ and $) as working over multiple lines (i.e., match the beginning or end of each line (delimited by \n or \r), not only the very beginning or end of the whole input string)
*/
export const MULTI_LINE:MultiLine = _M;
export const M:MultiLine = _M;
/**
* treat pattern as a sequence of unicode code points
*/
export const UNICODE:Unicode = _U;
export const U:Unicode = _U;
/**
* matches only from the index indicated by the lastIndex property of this regular expression in the target string (and does not attempt to match from any later indexes).
*/
export const STICKY:Sticky = _Y;
export const Y:Sticky = _Y;
/**
* Modifies the pattern to ignore standard whitespace characters.
*/
export const IGNORE_PATTERN_WHITESPACE:IgnorePatternWhitespace = _W;
export const W:IgnorePatternWhitespace = _W;
}
export class Regex
{
private readonly _re:RegExp;
private readonly _keys:string[] | undefined;
constructor(
pattern:string | RegExp,
options?:RegexOptions.Literal | RegexOptions.Literal[],
...extra:RegexOptions.Literal[])
{
if(!pattern) throw new Error("'pattern' cannot be null or empty.");
let patternString:string,
flags:string
= (options && ((options) instanceof (Array)
? options
: [options]).concat(extra) || extra)
.join(EMPTY)
.toLowerCase();
if(pattern instanceof RegExp)
{
let p = <RegExp>pattern;
if(p.ignoreCase && flags.indexOf(_I)=== -1)
flags += _I;
if(p.multiline && flags.indexOf(_M)=== -1)
flags += _M;
patternString = p.source;
}
else
{
patternString = pattern;
}
const ignoreWhiteSpace = flags.indexOf(_W)!= -1;
// For the majority of expected behavior, we need to eliminate global and whitespace ignore.
flags = flags.replace(/[gw]/g, EMPTY);
// find the keys inside the pattern, and place in mapping array {0:'key1', 1:'key2', ...}
const keys:string[] = [];
{
let k = patternString.match(/(?!\(\?<)(\w+)(?=>)/g);
if(k)
{
for(let i = 0, len = k.length; i<len; i++)
{
keys[i + 1] = k[i];
}
// remove keys from regexp leaving standard regexp
patternString = patternString.replace(/\?<\w+>/g, EMPTY);
this._keys = keys;
}
if(ignoreWhiteSpace)
patternString = patternString.replace(/\s+/g, "\\s*");
this._re = new RegExp(patternString, flags);
}
Object.freeze(this);
}
match(input:string, startIndex:number = 0):Match
{
const _ = this;
let r:RegExpExecArray | null;
if(!input
|| startIndex>=input.length
|| !(r = this._re.exec(input.substring(startIndex))))
return Match.Empty;
if(!(startIndex>0)) startIndex = 0;
const first = startIndex + r.index;
let loc = first;
const groups:Group[] = [],
groupMap:IMap<Group> = {};
for(let i = 0, len = r.length; i<len; ++i)
{
let text = r[i];
let g = EmptyGroup;
if(text!=null)
{
// Empty string might mean \b match or similar.
g = new Group(text, loc);
g.freeze();
}
if(i && _._keys && i<_._keys.length) groupMap[_._keys[i]] = g;
groups.push(g);
if(i!==0) loc += text.length;
}
const m = new Match(r[0], first, groups, groupMap);
m.freeze();
return m;
}
matches(input:string):Match[] //Readonly<Match[]>
{
const matches:Match[] = [];
let m:Match, p = 0;
const end = input && input.length || 0;
while(p<end && (m = this.match(input, p)) && m.success)
{
matches.push(m);
p = m.index + m.length;
}
Object.freeze(matches);
return matches;
}
replace(
input:string,
replacement:Primitive,
count?:number):string;
replace(
input:string,
evaluator:SelectorWithIndex<Match, Primitive>,
count?:number):string;
replace(
input:string,
r:any,
count:number = Infinity):string
{
if(!input || r==null || !(count>0)) return input;
const result:string[] = [];
let p = 0;
const end = input.length, isEvaluator = typeof r=="function";
let m:Match, i:number = 0;
while(i<count && p<end && (m = this.match(input, p)) && m.success)
{
let {index, length} = m;
if(p!==index) result.push(input.substring(p, index));
result.push(isEvaluator ? r(m, i++) : r);
p = index + length;
}
if(p<end) result.push(input.substring(p));
return result.join(EMPTY);
}
isMatch(input:string):boolean
{
return this._re.test(input);
}
static isMatch(
input:string,
pattern:string,
options?:RegexOptions.Literal[]):boolean
{
const r = new Regex(pattern, options);
return r.isMatch(input);
}
static replace(
input:string,
pattern:string,
replacement:string,
options?:RegexOptions.Literal[]):string;
static replace(
input:string,
pattern:string,
evaluator:SelectorWithIndex<Match, Primitive>,
options?:RegexOptions.Literal[]):string;
static replace(
input:string,
pattern:string,
e:any,
options?:RegexOptions.Literal[]):string
{
const r = new Regex(pattern, options);
return r.replace(input, e);
}
}
export class Capture
{
get length():number
{
const v = this.value;
return v && v.length || 0;
}
constructor(
public readonly value:string = EMPTY,
public readonly index:number = -1)
{
}
freeze():void
{
Object.freeze(this);
}
}
export class Group
extends Capture
{
get success():boolean
{
return this.index!= -1;
}
constructor(
value:string = EMPTY,
index:number = -1)
{
super(value, index);
}
static get Empty():Group
{
return EmptyGroup;
}
}
const EmptyGroup = new Group();
EmptyGroup.freeze();
export class Match
extends Group
{
constructor(
value:string = EMPTY,
index:number = -1,
public readonly groups:Group[] = [],
public readonly namedGroups:IMap<Group> = {})
{
super(value, index);
}
freeze():void
{
if(!this.groups) throw new Error("'groups' cannot be null.");
if(!this.namedGroups) throw new Error("'groupMap' cannot be null.");
Object.freeze(this.groups);
Object.freeze(this.namedGroups);
super.freeze();
}
static get Empty():Match
{
return EmptyMatch;
}
}
const EmptyMatch = new Match();
EmptyMatch.freeze();
export default Regex;