aboutsummaryrefslogtreecommitdiff
path: root/string2.lua
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2018-10-03 16:14:17 -0700
committerBruce Hill <bruce@bruce-hill.com>2018-10-03 16:14:37 -0700
commitb615cb5c8e638cffe77bbe5cb86c9362e2b2fc18 (patch)
tree724481109f64631dd92b575ed9a262bba46424f1 /string2.lua
parent331b22b3a3b61313a65ea6c49e2930b67f7ce82b (diff)
Fixed up some edge cases with as_lua_id and from_lua_id that were
producing bad results.
Diffstat (limited to 'string2.lua')
-rw-r--r--string2.lua25
1 files changed, 21 insertions, 4 deletions
diff --git a/string2.lua b/string2.lua
index 1f15fed..ae0e49d 100644
--- a/string2.lua
+++ b/string2.lua
@@ -143,8 +143,6 @@ local string2 = {
return '"' .. escaped .. '"'
end,
as_lua_id = function(str)
- local orig = str
- str = gsub(str, "^ *$", "%1 ")
str = gsub(str, "x([0-9A-F][0-9A-F])", "x78%1")
str = gsub(str, "%W", function(c)
if c == ' ' then
@@ -159,18 +157,37 @@ local string2 = {
return str
end,
from_lua_id = function(str)
- if not (is_lua_id("^_+(.*)$")) then
+ if not (is_lua_id(str:match("^_*(.*)$"))) then
str = str:sub(2, -1)
end
str = gsub(str, "_", " ")
str = gsub(str, "x([0-9A-F][0-9A-F])", function(hex)
return char(tonumber(hex, 16))
end)
- str = gsub(str, "^ ([ ]*)$", "%1")
return str
end
}
for k, v in pairs(string) do
string2[k] = string2[k] or v
end
+local _list_0 = {
+ "",
+ "_",
+ " ",
+ "return",
+ "asdf",
+ "one two",
+ "one_two",
+ "Hex2Dec",
+ "He-ec",
+ "\3"
+}
+for _index_0 = 1, #_list_0 do
+ local test = _list_0[_index_0]
+ local lua_id = string2.as_lua_id(test)
+ assert(is_lua_id(lua_id), "failed to convert '" .. tostring(test) .. "' to a valid Lua identifier (got '" .. tostring(lua_id) .. "')")
+ local roundtrip = string2.from_lua_id(lua_id)
+ assert(roundtrip == test, "Failed lua_id roundtrip: '" .. tostring(test) .. "' -> " .. tostring(lua_id) .. " -> " .. tostring(roundtrip))
+end
+assert(string2.as_lua_id('') == '_')
return string2