lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Tue, Aug 27, 2019 at 1:55 AM Scott Morgan <> wrote:
What's the best way to filter out non-specified chars from an ASCII
string? (so no need to worry about UTF8, etc.)

There are ways to do this without having to escape chars in regular expressions, and get fairly decent performance.

'make_filter' returns a table/object that does the filtering. The table is used as a cache to store per-character results. The function assigned to __index
has to be evaluated only once for each character value, so it's never called more than 256 times. The actual substitution work is done by a single call to 'string.gsub' with the table as its third parameter.

local function make_filter(valid_chars)
        return setmetatable({}, {
                __index = function(t, c)
                        local r = string.find(valid_chars, c, 1, true) and c or ""
                        t[c] = r
                        return r

                __call = function(t, s)
                        return (string.gsub(s, '.', t))

local filter = make_filter("abc[]")