Skip to content

Instantly share code, notes, and snippets.

@TaizWeb
Last active April 27, 2020 02:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TaizWeb/8489b72d474aeb8dbd432b10ae372cc0 to your computer and use it in GitHub Desktop.
Save TaizWeb/8489b72d474aeb8dbd432b10ae372cc0 to your computer and use it in GitHub Desktop.
A function to fix escape sequences in lua strings. Useful for preparing JSON from an API for conversion.
function escapeFix(data)
local newData = ""
for i=1,string.len(data) do
local char = string.sub(data, i, i)
local nextChar = string.sub(data, i+1, i+1)
local prevChar = string.sub(data, i-1, i-1)
if (char == '"' and (prevChar ~= "{" and nextChar ~= ":" and prevChar ~= ":" and nextChar ~= "," and prevChar ~= "," and nextChar ~= "}")) then
newData = newData .. "\\" -- Append another, non-escaped, '\'
end
newData = newData .. string.sub(data, i, i)
end
newData = string.gsub(newData, "\n", "\\n") -- Strip out newlines as well
return newData
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment