lua-parser icon indicating copy to clipboard operation
lua-parser copied to clipboard

add non-UTF-8 escaping to toLua

Open 7aGiven opened this issue 4 months ago • 0 comments

add a config for toLua for non-UTF-8 escape to hex.

-- lua-parser internal function
function escape(s)
  if checkutf8 and utf8.len(s) == nil then
    local r = ""
    for i=1, #s do
      r = r .. string.format("\\x%02X", string.byte(s, i))
    end
    return r
  else
    return defaultEscape(s)
  end
end
--
toLua()

or allow custom escape

function escape(s, default)
  if utf8.len(s) == nil then
    local r = ""
    for i=1, #s do
      r = r .. string.format("\\x%02X", string.byte(s, i))
    end
    return r
  else
    return default(s)
  end
end
toLua({escape=escape})

or custom function return nil for default

function escape(s)
  if utf8.len(s) == nil then
    local r = ""
    for i=1, #s do
      r = r .. string.format("\\x%02X", string.byte(s, i))
    end
    return r
  else
    return nil
  end
end
toLua({escape=escape})

7aGiven avatar Sep 13 '25 05:09 7aGiven