-- -------------------------------------------- -- 5. DEBUGGING / TIMING -- -------------------------------------------- local debug_utils = {}
-- Log with timestamp function debug_utils.log(message, level) level = level or "INFO" local timestamp = os.date("%Y-%m-%d %H:%M:%S") print(string.format("[%s] [%s] %s", timestamp, level, message)) end
-- Check if string starts with a prefix function string_utils.starts_with(str, prefix) return str:sub(1, #prefix) == prefix end
-- Trim whitespace from both ends function string_utils.trim(str) return str:match("^%s*(.-)%s*$") end script luar
-- Write string to file (overwrites) function file_utils.write_file(filename, content) local f, err = io.open(filename, "w") if not f then return false, err end f:write(content) f:close() return true end
-- Check if value is a number within range function validate.is_in_range(val, min, max) return type(val) == "number" and val >= min and val <= max end
-- Append string to file function file_utils.append_file(filename, content) local f, err = io.open(filename, "a") if not f then return false, err end f:write(content) f:close() return true end -- -------------------------------------------- -- 5
-- Merge table t2 into t1 (overwrites t1 keys) function table_utils.merge(t1, t2) for k, v in pairs(t2) do t1[k] = v end return t1 end
-- Check if table is non-empty function validate.non_empty_table(tbl) return type(tbl) == "table" and next(tbl) ~= nil end
-- Check if string matches a pattern (regex-lite) function validate.matches_pattern(str, pattern) return string.match(str, pattern) ~= nil end prefix) return str:sub(1
local my_table = {a=1, b={c=2}} local copy = table_utils.deep_copy(my_table)
-- Simple execution timer (prints elapsed time) function debug_utils.time_function(func, ...) local start = os.clock() local results = {func(...)} local elapsed = os.clock() - start print(string.format("Execution time: %.4f seconds", elapsed)) return table.unpack(results) end
-- -------------------------------------------- -- 6. EXAMPLE USAGE (commented out) -- -------------------------------------------- --[[ local my_string = " hello world " print(string_utils.trim(my_string)) --> "hello world" local parts = string_utils.split("a,b,c", ",") --> {"a","b","c"}