This repository was archived by the owner on Aug 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathSystem.Text.RegularExpressions.Regex.js
More file actions
140 lines (116 loc) · 4.44 KB
/
System.Text.RegularExpressions.Regex.js
File metadata and controls
140 lines (116 loc) · 4.44 KB
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
JSIL.ImplementExternals("System.Text.RegularExpressions.Regex", function ($) {
var system = JSIL.GetAssembly("System", true);
var makeRegex = function (pattern, options) {
var tRegexOptions = system.System.Text.RegularExpressions.RegexOptions;
if ((options & tRegexOptions.ECMAScript) === 0) {
JSIL.RuntimeError("Non-ECMAScript regexes are not currently supported.");
}
var flags = "g";
if ((options & tRegexOptions.IgnoreCase) !== 0) {
flags += "i";
}
if ((options & tRegexOptions.Multiline) !== 0) {
flags += "m";
}
return new RegExp(pattern, flags);
};
$.Method({ Static: false, Public: true }, ".ctor",
(new JSIL.MethodSignature(null, [$.String], [])),
function _ctor(pattern) {
this._regex = makeRegex(pattern, System.Text.RegularExpressions.RegexOptions.None);
}
);
$.Method({ Static: false, Public: true }, ".ctor",
(new JSIL.MethodSignature(null, [$.String, system.TypeRef("System.Text.RegularExpressions.RegexOptions")], [])),
function _ctor(pattern, options) {
this._regex = makeRegex(pattern, options);
}
);
$.Method({ Static: false, Public: true }, "Matches",
(new JSIL.MethodSignature(system.TypeRef("System.Text.RegularExpressions.MatchCollection"), [$.String], [])),
function Matches(input) {
var matchObjects = [];
var tMatch = system.System.Text.RegularExpressions.Match.__Type__;
var tGroup = system.System.Text.RegularExpressions.Group.__Type__;
var tGroupCollection = system.System.Text.RegularExpressions.GroupCollection.__Type__;
var current = null;
while ((current = this._regex.exec(input)) !== null) {
var groupObjects = [];
for (var i = 0, l = current.length; i < l; i++) {
var groupObject = JSIL.CreateInstanceOfType(
tGroup, "$internalCtor", [
current[i],
(current[i] !== null) && (current[i].length > 0)
]
);
groupObjects.push(groupObject);
}
var groupCollection = JSIL.CreateInstanceOfType(
tGroupCollection, "$internalCtor", [groupObjects]
);
var matchObject = JSIL.CreateInstanceOfType(
tMatch, "$internalCtor", [
current[0], groupCollection
]
);
matchObjects.push(matchObject);
}
var result = JSIL.CreateInstanceOfType(
System.Text.RegularExpressions.MatchCollection.__Type__,
"$internalCtor", [matchObjects]
);
return result;
}
);
$.Method({ Static: true, Public: true }, "Replace",
(new JSIL.MethodSignature($.String, [
$.String, $.String,
$.String, system.TypeRef("System.Text.RegularExpressions.RegexOptions")
], [])),
function Replace(input, pattern, replacement, options) {
var re = makeRegex(pattern, options);
return input.replace(re, replacement);
}
);
$.Method({ Static: true, Public: true }, "Replace",
(new JSIL.MethodSignature($.String, [
$.String, $.String, $.String], [])),
function Replace(input, pattern, replacement) {
var re = makeRegex(pattern, System.Text.RegularExpressions.RegexOptions.ECMAScript);
return input.replace(re, replacement);
}
);
$.Method({ Static: false, Public: true }, "Replace",
(new JSIL.MethodSignature($.String, [$.String, $.String], [])),
function Replace(input, replacement) {
return input.replace(this._regex, replacement);
}
);
$.Method({ Static: false, Public: true }, "IsMatch",
(new JSIL.MethodSignature($.Boolean, [$.String], [])),
function IsMatch(input) {
var matchCount = 0;
var current = null;
// Have to exec() until done because JS RegExp is stateful for some stupid reason
while ((current = this._regex.exec(input)) !== null) {
matchCount += 1;
}
return (matchCount > 0);
}
);
$.Method({ Static: true, Public: true }, "IsMatch",
(new JSIL.MethodSignature($.Boolean, [$.String, $.String], [])),
function IsMatch(input, pattern) {
var re = makeRegex(pattern, System.Text.RegularExpressions.RegexOptions.ECMAScript);
var matchCount = 0;
var current = null;
while ((current = re.exec(input)) !== null) {
matchCount += 1;
}
return (matchCount > 0);
}
);
});
JSIL.MakeClass($jsilcore.TypeRef("System.Object"), "System.Text.RegularExpressions.Regex", true, [], function ($) {
var $thisType = $.publicInterface;
});