You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
3.0 KiB
JavaScript
120 lines
3.0 KiB
JavaScript
5 years ago
|
// Generated by CoffeeScript 1.12.4
|
||
|
var Pattern;
|
||
|
|
||
|
Pattern = (function() {
|
||
|
Pattern.prototype.regex = null;
|
||
|
|
||
|
Pattern.prototype.rawRegex = null;
|
||
|
|
||
|
Pattern.prototype.cleanedRegex = null;
|
||
|
|
||
|
Pattern.prototype.mapping = null;
|
||
|
|
||
|
function Pattern(rawRegex, modifiers) {
|
||
|
var _char, capturingBracketNumber, cleanedRegex, i, len, mapping, name, part, subChar;
|
||
|
if (modifiers == null) {
|
||
|
modifiers = '';
|
||
|
}
|
||
|
cleanedRegex = '';
|
||
|
len = rawRegex.length;
|
||
|
mapping = null;
|
||
|
capturingBracketNumber = 0;
|
||
|
i = 0;
|
||
|
while (i < len) {
|
||
|
_char = rawRegex.charAt(i);
|
||
|
if (_char === '\\') {
|
||
|
cleanedRegex += rawRegex.slice(i, +(i + 1) + 1 || 9e9);
|
||
|
i++;
|
||
|
} else if (_char === '(') {
|
||
|
if (i < len - 2) {
|
||
|
part = rawRegex.slice(i, +(i + 2) + 1 || 9e9);
|
||
|
if (part === '(?:') {
|
||
|
i += 2;
|
||
|
cleanedRegex += part;
|
||
|
} else if (part === '(?<') {
|
||
|
capturingBracketNumber++;
|
||
|
i += 2;
|
||
|
name = '';
|
||
|
while (i + 1 < len) {
|
||
|
subChar = rawRegex.charAt(i + 1);
|
||
|
if (subChar === '>') {
|
||
|
cleanedRegex += '(';
|
||
|
i++;
|
||
|
if (name.length > 0) {
|
||
|
if (mapping == null) {
|
||
|
mapping = {};
|
||
|
}
|
||
|
mapping[name] = capturingBracketNumber;
|
||
|
}
|
||
|
break;
|
||
|
} else {
|
||
|
name += subChar;
|
||
|
}
|
||
|
i++;
|
||
|
}
|
||
|
} else {
|
||
|
cleanedRegex += _char;
|
||
|
capturingBracketNumber++;
|
||
|
}
|
||
|
} else {
|
||
|
cleanedRegex += _char;
|
||
|
}
|
||
|
} else {
|
||
|
cleanedRegex += _char;
|
||
|
}
|
||
|
i++;
|
||
|
}
|
||
|
this.rawRegex = rawRegex;
|
||
|
this.cleanedRegex = cleanedRegex;
|
||
|
this.regex = new RegExp(this.cleanedRegex, 'g' + modifiers.replace('g', ''));
|
||
|
this.mapping = mapping;
|
||
|
}
|
||
|
|
||
|
Pattern.prototype.exec = function(str) {
|
||
|
var index, matches, name, ref;
|
||
|
this.regex.lastIndex = 0;
|
||
|
matches = this.regex.exec(str);
|
||
|
if (matches == null) {
|
||
|
return null;
|
||
|
}
|
||
|
if (this.mapping != null) {
|
||
|
ref = this.mapping;
|
||
|
for (name in ref) {
|
||
|
index = ref[name];
|
||
|
matches[name] = matches[index];
|
||
|
}
|
||
|
}
|
||
|
return matches;
|
||
|
};
|
||
|
|
||
|
Pattern.prototype.test = function(str) {
|
||
|
this.regex.lastIndex = 0;
|
||
|
return this.regex.test(str);
|
||
|
};
|
||
|
|
||
|
Pattern.prototype.replace = function(str, replacement) {
|
||
|
this.regex.lastIndex = 0;
|
||
|
return str.replace(this.regex, replacement);
|
||
|
};
|
||
|
|
||
|
Pattern.prototype.replaceAll = function(str, replacement, limit) {
|
||
|
var count;
|
||
|
if (limit == null) {
|
||
|
limit = 0;
|
||
|
}
|
||
|
this.regex.lastIndex = 0;
|
||
|
count = 0;
|
||
|
while (this.regex.test(str) && (limit === 0 || count < limit)) {
|
||
|
this.regex.lastIndex = 0;
|
||
|
str = str.replace(this.regex, replacement);
|
||
|
count++;
|
||
|
}
|
||
|
return [str, count];
|
||
|
};
|
||
|
|
||
|
return Pattern;
|
||
|
|
||
|
})();
|
||
|
|
||
|
module.exports = Pattern;
|