Skip to content

Commit

Permalink
Merge pull request #48 from strawbberrys/patch-1
Browse files Browse the repository at this point in the history
use string patterns and table
  • Loading branch information
Upbolt authored Nov 12, 2021
2 parents 5fd0a70 + 8a8ebcc commit 24af1cd
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions methods/string.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,24 @@ local function toString(value)
end
end

local gsubCharacters = {
["\""] = "\\\"",
["\\"] = "\\\\",
["\0"] = "\\0",
["\n"] = "\\n",
["\t"] = "\\t",
["\f"] = "\\f",
["\r"] = "\\r",
["\v"] = "\\v",
["\a"] = "\\a",
["\b"] = "\\b"
}

local function dataToString(data)
local dataType = type(data)

if dataType == "string" then
return '"' .. data:gsub('"', '\\"')
:gsub('\\', "\\\\")
:gsub('\0', '\\0')
:gsub('\n', "\\n")
:gsub("\t", "\\t")
:gsub('\f', '\\f')
:gsub('\r', '\\r')
:gsub('\v', '\\v')
:gsub('\a', '\\a')
:gsub('\b', '\\b') .. '"'
return '"' .. data:gsub("[%c%z\\\"]", gsubCharacters) .. '"'
elseif dataType == "table" then
return tableToString(data)
elseif dataType == "userdata" then
Expand Down

0 comments on commit 24af1cd

Please sign in to comment.