From 9472a7b969a3eb553876706f375e05904ebd8d4b Mon Sep 17 00:00:00 2001 From: TheOnePath Date: Sun, 8 Jan 2023 22:29:33 +0000 Subject: [PATCH] Added utils.lua File is not used extensively at current development; however, usage in the future is to be considered. Provide utility functions to aid built-in functions. Greater usage to be made in future development. --- utils.lua | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 utils.lua diff --git a/utils.lua b/utils.lua new file mode 100644 index 0000000..98573fc --- /dev/null +++ b/utils.lua @@ -0,0 +1,40 @@ +local M = {} + +M.iter_table_dump = function ( arr ) + for k, v in pairs(arr) do + print("Table:", k, v) + end +end + +local escape_chars = function ( text ) + return string.gsub(text, "[%(|%)|\\|%[|%]|%-|%{%}|%?|%+|%*|%^|%$|%.]", { + ["\\"] = "\\\\", + ["-"] = "\\-", + ["("] = "\\(", + [")"] = "\\)", + ["["] = "\\[", + ["]"] = "\\]", + ["{"] = "\\{", + ["}"] = "\\}", + ["?"] = "\\?", + ["+"] = "\\+", + ["*"] = "\\*", + ["^"] = "\\^", + ["$"] = "\\$", + ["."] = "\\.", + }) +end + +-- :@dev: clean the text once it has been fetched from buffer +M.clean_buf = function ( text ) + if not type(text) == "string" or text == nil then + print("[DEBUG - clean_buf] Given input is not of type string.") + return + end + + return escape_chars( text ) + :match( "^%s*(.-)%s*$" ) +end + + +return M