From ee57a92daabdd65dc2530e64f81c0d726158cc37 Mon Sep 17 00:00:00 2001 From: n1474335 Date: Thu, 28 Sep 2017 19:24:28 +0000 Subject: [PATCH] Improved register reference regex --- src/core/FlowControl.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/core/FlowControl.js b/src/core/FlowControl.js index aa4082e5..4a94ffdf 100755 --- a/src/core/FlowControl.js +++ b/src/core/FlowControl.js @@ -127,14 +127,13 @@ const FlowControl = { */ function replaceRegister(str) { // Replace references to registers ($Rn) with contents of registers - str = str ? str.replace(/((?:^|[^\\])(?:\\.|[^\\])*?)\$R(\d{1,2})/g, (match, pre, regNum) => { + return str.replace(/(\\*)\$R(\d{1,2})/g, (match, slashes, regNum) => { const index = parseInt(regNum, 10) + 1; - return (index <= state.numRegisters || index >= state.numRegisters + registers.length) ? - match : pre + registers[index - state.numRegisters]; - }) : str; - - // Unescape remaining register references - return str ? str.replace(/\\\$R(\d{1,2})/, "$R$1") : str; + if (index <= state.numRegisters || index >= state.numRegisters + registers.length) + return match; + if (slashes.length % 2 !== 0) return match.slice(1); // Remove escape + return slashes + registers[index - state.numRegisters]; + }); } // Step through all subsequent ops and replace registers in args with extracted content