-- ============================================ -- UTILITY SCRIPT FOR LUA (5.1+ compatible) -- Author: Generated Assistant -- Description: Reusable functions for string, table, file, and debug tasks -- ============================================

-- 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

-- Trim whitespace from both ends function string_utils.trim(str) return str:match("^%s*(.-)%s*$") end script luar

-- Check if a value exists in a table (linear search) function table_utils.contains(tbl, value) for _, v in pairs(tbl) do if v == value then return true end end return false 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 t2) for k

-- -------------------------------------------- -- 4. DATA VALIDATION -- -------------------------------------------- local validate = {}

-- Check if string starts with a prefix function string_utils.starts_with(str, prefix) return str:sub(1, #prefix) == prefix end content) local f

-- Deep copy a table (handles nested tables) function table_utils.deep_copy(orig) local copy if type(orig) == "table" then copy = {} for k, v in pairs(orig) do copy[table_utils.deep_copy(k)] = table_utils.deep_copy(v) end setmetatable(copy, table_utils.deep_copy(getmetatable(orig))) else copy = orig end return copy end

-- -------------------------------------------- -- 5. DEBUGGING / TIMING -- -------------------------------------------- local debug_utils = {}

-- -------------------------------------------- -- 1. STRING UTILITIES -- -------------------------------------------- local string_utils = {}

-- 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