for (const name in exp.rules) {
const el = `\${${name}}`;
obj[el] = exp[el];
obj.rules[name] = exp.rules[name];
}
return obj;
};
const singleReplaceFunction = function singleReplaceFunction(p, ostr, func) {
const result = p.thisThis.exec(ostr);
if (result === null) {
return ostr;
}
const rstr = func(result, lastObj(p.thisThis));
const ret = p.thisThis.leftContext.concat(rstr, p.thisThis.rightContext);
return ret;
};
const globalReplaceFunction = function globalReplaceFunction(p, ostr, func) {
const exp = p.thisThis;
let retstr = ostr.slice(0);
const TRUE = true;
while (TRUE) {
const result = exp.exec(retstr);
if (result === null) {
break;
}
const newrstr = func(result, lastObj(exp));
retstr = exp.leftContext.concat(newrstr, exp.rightContext);
exp.lastIndex = exp.leftContext.length + newrstr.length;
if (result[0].length === 0) {
exp.lastIndex += 1;
}
}
return retstr;
};
const singleReplaceString = function singleReplaceString(p, ostr, rstr) {
const exp = p.thisThis;
const result = exp.exec(ostr);
if (result === null) {
return ostr;
}
const ritems = parseReplacementString(p, rstr);
const rep = generateReplacementString(p, rstr, ritems);
const ret = exp.leftContext.concat(rep, exp.rightContext);
return ret;
};
const globalReplaceString = function globalReplaceString(p, ostr, rstr) {
const exp = p.thisThis;
let retstr = ostr.slice(0);
let ritems = null;
const TRUE = true;
while (TRUE) {
const result = exp.exec(retstr);
if (result == null) {
break;
}
if (ritems === null) {
ritems = parseReplacementString(p, rstr);
}
const newrstr = generateReplacementString(p, rstr, ritems);
retstr = exp.leftContext.concat(newrstr, exp.rightContext);
exp.lastIndex = exp.leftContext.length + newrstr.length;
if (result[0].length === 0) {
exp.lastIndex += 1;
}
}
return retstr;
};
exports.replaceString = function replaceString(p, str, replacement) {
if (p.thisThis.global || p.thisThis.sticky) {
return globalReplaceString(p, str, replacement);
}
return singleReplaceString(p, str, replacement);
};
exports.replaceFunction = function replaceFunction(p, str, func) {
if (p.thisThis.global || p.thisThis.sticky) {
return globalReplaceFunction(p, str, func);
}
return singleReplaceFunction(p, str, func);
};