git @ Cat's Eye Technologies Decoy / master src / decoy / mangler.lua
master

Tree @master (Download .tar.gz)

mangler.lua @masterraw · history · blame

--
-- decoy_mangler.lua
--

-- SPDX-FileCopyrightText: Copyright (c) 2023-2024 Chris Pressey, Cat's Eye Technologies.
-- This work is distributed under a 2-clause BSD license. For more information, see:
-- SPDX-License-Identifier: LicenseRef-BSD-2-Clause-X-Decoy

string = require "string"


local Mangler = {}

function Mangler:mangle(s)
    --
    -- replace all non-alphanumeric symbols with "_xx"
    -- where xx is a hex code; and leave all alphanumeric alone
    --
    s = s:gsub("[^%w]", function(c)
        return "_" .. string.format("%x", string.byte(c))
    end)
    --
    -- map out any reserved words
    s = s:gsub("true", "t_r_u_e_")
    s = s:gsub("false", "f_a_l_s_e_")
    return s
end


Mangler.new = function()
    local self = {
    }
    setmetatable(self, {__index = Mangler})
    return self
end

return Mangler