From 2f6885f273a105504ed8e1bcd91802e386b8fe2d Mon Sep 17 00:00:00 2001 From: dmiller Date: Thu, 8 Sep 2022 18:14:52 +0000 Subject: [PATCH] Memoize lpeg patterns for keywords in script spec/rules --- nse_main.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/nse_main.lua b/nse_main.lua index f06edc1bb..9fc6b7b22 100644 --- a/nse_main.lua +++ b/nse_main.lua @@ -278,9 +278,14 @@ local function host_copy(t) end -- Return a pattern which matches a "keyword" literal, case insensitive. +local memo_K = {} local function K (a) - local insensitize = Cf((P(1) / function (a) return S(lower(a)..upper(a)) end)^1, function (a, b) return a * b end); - return assert(insensitize:match(a)) * #(V "space" + S"()," + P(-1)); -- "keyword" token + local kw = memo_K[a] + if not kw then + kw = U.caseless(a) * #(V "space" + S"()," + P(-1)) + memo_K[a] = kw + end + return kw end local REQUIRE_ERROR = {};