From dd2b7b4c24e34d77382ab7213d21546d8f3d93e7 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 21 Dec 2021 20:21:06 +0000 Subject: [PATCH] [Minor] Add unhex utility --- lualib/lua_util.lua | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua index 75d218da3..4b0e50192 100644 --- a/lualib/lua_util.lua +++ b/lualib/lua_util.lua @@ -1473,4 +1473,19 @@ exports.shuffle = function(tbl) return tbl end +-- +local hex_table = {} +for idx = 0, 255 do + hex_table[("%02X"):format(idx)] = string.char(idx) + hex_table[("%02x"):format(idx)] = string.char(idx) +end + +---[[[ +-- @function lua_util.unhex(str) +-- Decode hex encoded string +-- @param {string} str string to decode +-- @return {string} hex decoded string (valid hex pairs are decoded, everything else is printed as is) +--]]] +exports.unhex = function(str) return str:gsub('(..)', hex_table) end + return exports -- 2.39.5