From f7ed8f6d7bf0d70ee5f9ec1301a6493aeaf7fe8d Mon Sep 17 00:00:00 2001 From: Ben Gubler Date: Sat, 13 May 2023 14:56:27 -0600 Subject: [PATCH] fix RegExp leading to slow parsing w/out tags --- deno_dist/parse.ts | 10 +++++----- src/parse.ts | 9 +++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/deno_dist/parse.ts b/deno_dist/parse.ts index 451fac0..83119ed 100644 --- a/deno_dist/parse.ts +++ b/deno_dist/parse.ts @@ -105,8 +105,7 @@ export default function parse( }, ""); const parseOpenReg = new RegExp( - "([^]*?)" + escapeRegExp(config.tags[0]) + "(-|_)?\\s*(" + prefixes + - ")?\\s*", + escapeRegExp(config.tags[0]) + "(-|_)?\\s*(" + prefixes + ")?\\s*", "g", ); @@ -119,11 +118,12 @@ export default function parse( let m; while ((m = parseOpenReg.exec(str))) { + const precedingString = str.slice(lastIndex, m.index); + lastIndex = m[0].length + m.index; - const precedingString = m[1]; - const wsLeft = m[2]; - const prefix = m[3] || ""; // by default either ~, =, or empty + const wsLeft = m[1]; + const prefix = m[2] || ""; // by default either ~, =, or empty pushString(precedingString, wsLeft); diff --git a/src/parse.ts b/src/parse.ts index 7a650b3..9cdbb85 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -98,7 +98,7 @@ export default function parse(str: string, config: EtaConfig): Array ""); const parseOpenReg = new RegExp( - "([^]*?)" + escapeRegExp(config.tags[0]) + "(-|_)?\\s*(" + prefixes + ")?\\s*", + escapeRegExp(config.tags[0]) + "(-|_)?\\s*(" + prefixes + ")?\\s*", "g" ); @@ -111,11 +111,12 @@ export default function parse(str: string, config: EtaConfig): Array let m; while ((m = parseOpenReg.exec(str))) { + const precedingString = str.slice(lastIndex, m.index); + lastIndex = m[0].length + m.index; - const precedingString = m[1]; - const wsLeft = m[2]; - const prefix = m[3] || ""; // by default either ~, =, or empty + const wsLeft = m[1]; + const prefix = m[2] || ""; // by default either ~, =, or empty pushString(precedingString, wsLeft);