forked from benekastah/empcre
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
132 lines (115 loc) · 3.83 KB
/
index.js
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
(function (root, factory) {
var native;
if (!native) {
if (typeof module === 'object' && module.exports) {
native = require('./dist/native.js');
} else {
native = root.__native__PCRE;
}
}
if (!native) {
throw new Error("PCRE native library is missing.");
}
if (typeof module === 'object' && module.exports) {
module.exports = factory(native);
} else {
root.PCRE = factory(native);
}
}(this, function (Module) {
var method = function (cls, clsName, ptrProp, methodName, ret, args) {
var fn = Module.cwrap(clsName + methodName, ret, args);
cls.prototype[methodName] = function () {
var ptr = this[ptrProp];
var args = [ptr && ptr.call ? ptr.call(this) : ptr];
args.push.apply(args, arguments);
return fn.apply(null, args);
};
};
var REMatch_destroy = Module.cwrap('REMatch_destroy', 'null', ['number']);
var REMatch_get_length = Module.cwrap('REMatch_get_length', 'number', ['number']);
var REMatch_get_match_at = Module.cwrap('REMatch_get_match_at', 'string', ['number', 'number']);
function RE(pattern, flags) {
this.pattern = pattern;
this.flags = flags || '';
this.buildRe();
}
RE.method = function (methodName, ret, args) {
method(this, 'RE', '_re', methodName, ret, args);
};
RE._new = Module.cwrap('RE_new', 'number', ['string', 'string']);
RE.method('_destroy', 'null', ['number']);
RE.method('_new', 'number', ['string', 'string']);
RE.method('_match', 'number', ['number', 'string', 'number']);
RE.method('_hasFlag', 'number', ['number', 'string']);
RE.method('_get_name_length', 'number', ['number']);
RE.method('_get_name_id_at', 'number', ['number', 'number']);
RE.method('_get_name_at', 'string', ['number', 'number']);
RE.prototype.toString = function () {
return '/' + this.pattern + '/' + this.flags;
};
RE.prototype.buildRe = function () {
if (!this._re) {
//console.warn('Allocating memory for ' + this);
this._re = RE._new(this.pattern, this.flags);
this.ignoreCase = this._hasFlag('ignoreCase');
this.multiline = this._hasFlag('multiline');
this.dotall = this._hasFlag('dotall');
this.jsCompat = this._hasFlag('jsCompat');
this.extended = this._hasFlag('jsCompat');
this.names = {};
let nameCount = this._get_name_length();
for(var i = 0; i < nameCount; i++) {
this.names[this._get_name_at(i)] = this._get_name_id_at(i)
}
}
// Try to manage memory automatically
var that = this;
clearTimeout(this._destroyTimeout);
this._destroyTimeout = setTimeout(function () {
that.destroy();
}, 5000);
return this._re;
};
RE.prototype.destroy = function () {
//console.warn('Cleaning up memory for ' + this);
this._destroy();
this._re = null;
};
RE.prototype.match = function (input, startOffset) {
this.buildRe();
var matchPtr = this._match(input, startOffset || 0);
var matches = {
length: 0,
input: input
};
for (var i = 0, len = REMatch_get_length(matchPtr); i < len; i++) {
Array.prototype.push.call(matches, REMatch_get_match_at(matchPtr, i));
}
for (var prop in this.names) {
if (this.names.hasOwnProperty(prop)) {
if (matches[this.names[prop]]) {
matches[prop] = matches[this.names[prop]];
}
}
}
REMatch_destroy(matchPtr);
return matches.length ? matches : null;
};
RE.prototype.executor = function (input) {
function REExecutor() {
this.input = input;
this.offset = 0;
var match = this.match;
this.match = function () {
var result = match.call(this, this.input, this.offset);
if (result) {
result.offset += result[0].length;
}
return result;
};
};
REExecutor.prototype = this;
return new REExecutor;
};
return RE;
}));