aboutsummaryrefslogtreecommitdiff
path: root/string2.moon
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2018-09-18 19:48:58 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2018-09-18 19:49:29 -0700
commit79d4bd5125de7ff220fbf8a8a5493d437ed16963 (patch)
treefa09e1a5ccb384373b50e27067a7cc3a25669550 /string2.moon
parentd11f9bc5d3971a68dd78cdaca0b046a40a329000 (diff)
Got rid of repr() use and replaced with :as_lua() or :as_nomsu() in as
many places as possible.
Diffstat (limited to 'string2.moon')
-rw-r--r--string2.moon14
1 files changed, 14 insertions, 0 deletions
diff --git a/string2.moon b/string2.moon
index 2259272..662cd0d 100644
--- a/string2.moon
+++ b/string2.moon
@@ -45,6 +45,20 @@ string2 = {
lines[#lines+1] = line
return table.concat(lines, "\n")
+ as_lua: =>
+ escaped = gsub(@, "\\", "\\\\")
+ escaped = gsub(escaped, "\n", "\\n")
+ escaped = gsub(escaped, '"', '\\"')
+ escaped = gsub(escaped, "[^ %g]", (c)-> format("\\%03d", byte(c, 1)))
+ return '"'..escaped..'"'
+
+ as_nomsu: =>
+ escaped = gsub(@, "\\", "\\\\")
+ escaped = gsub(escaped, "\n", "\\n")
+ escaped = gsub(escaped, '"', '\\"')
+ escaped = gsub(escaped, "[^ %g]", (c)-> format("\\%03d", byte(c, 1)))
+ return '"'..escaped..'"'
+
-- Convert an arbitrary text into a valid Lua identifier. This function is injective,
-- but not idempotent. In logic terms: (x != y) => (as_lua_id(x) != as_lua_id(y)),
-- but not (as_lua_id(a) == b) => (as_lua_id(b) == b).