From 79d4bd5125de7ff220fbf8a8a5493d437ed16963 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 18 Sep 2018 19:48:58 -0700 Subject: Got rid of repr() use and replaced with :as_lua() or :as_nomsu() in as many places as possible. --- string2.lua | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'string2.lua') diff --git a/string2.lua b/string2.lua index 5995de2..2cdfefc 100644 --- a/string2.lua +++ b/string2.lua @@ -119,6 +119,24 @@ local string2 = { end return table.concat(lines, "\n") end, + as_lua = function(self) + local escaped = gsub(self, "\\", "\\\\") + escaped = gsub(escaped, "\n", "\\n") + escaped = gsub(escaped, '"', '\\"') + escaped = gsub(escaped, "[^ %g]", function(c) + return format("\\%03d", byte(c, 1)) + end) + return '"' .. escaped .. '"' + end, + as_nomsu = function(self) + local escaped = gsub(self, "\\", "\\\\") + escaped = gsub(escaped, "\n", "\\n") + escaped = gsub(escaped, '"', '\\"') + escaped = gsub(escaped, "[^ %g]", function(c) + return format("\\%03d", byte(c, 1)) + end) + return '"' .. escaped .. '"' + end, as_lua_id = function(str) local orig = str str = gsub(str, "^ *$", "%1 ") -- cgit v1.2.3 From 692fae5416ce1f2702b599ffb27b2e3d2235eba7 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 26 Sep 2018 12:45:08 -0700 Subject: Incremental fixes and more nomnom ports. --- string2.lua | 69 +++++++++++++++++++++++++++---------------------------------- 1 file changed, 31 insertions(+), 38 deletions(-) (limited to 'string2.lua') diff --git a/string2.lua b/string2.lua index 2cdfefc..1f15fed 100644 --- a/string2.lua +++ b/string2.lua @@ -27,34 +27,39 @@ isplit = function(self, sep) }, 0 end local lua_keywords = { - "and", - "break", - "do", - "else", - "elseif", - "end", - "false", - "for", - "function", - "goto", - "if", - "in", - "local", - "nil", - "not", - "or", - "repeat", - "return", - "then", - "true", - "until", - "while" + ["and"] = true, + ["break"] = true, + ["do"] = true, + ["else"] = true, + ["elseif"] = true, + ["end"] = true, + ["false"] = true, + ["for"] = true, + ["function"] = true, + ["goto"] = true, + ["if"] = true, + ["in"] = true, + ["local"] = true, + ["nil"] = true, + ["not"] = true, + ["or"] = true, + ["repeat"] = true, + ["return"] = true, + ["then"] = true, + ["true"] = true, + ["until"] = true, + ["while"] = true } +local is_lua_id +is_lua_id = function(str) + return match(str, "^[_a-zA-Z][_a-zA-Z0-9]*$") and not lua_keywords[str] +end local string2 = { isplit = isplit, uppercase = upper, lowercase = lower, reversed = reverse, + is_lua_id = is_lua_id, capitalized = function(self) return gsub(self, '%l', upper, 1) end, @@ -148,27 +153,15 @@ local string2 = { return format("x%02X", byte(c)) end end) - str = gsub(str, "^_*%d", "_%1") - if match(str, "^_*[abdefgilnortuw][aefhilnoru][acdefiklnoprstu]*$") then - for _index_0 = 1, #lua_keywords do - local kw = lua_keywords[_index_0] - if match(str, ("^_*" .. kw .. "$")) then - str = "_" .. str - end - end + if not (is_lua_id(str:match("^_*(.*)$"))) then + str = "_" .. str end return str end, from_lua_id = function(str) - if match(str, "^_+[abdefgilnortuw][aefhilnoru][acdefiklnoprstu]*$") then - for _index_0 = 1, #lua_keywords do - local kw = lua_keywords[_index_0] - if match(str, ("^_+" .. kw .. "$")) then - str = str:sub(2, -1) - end - end + if not (is_lua_id("^_+(.*)$")) then + str = str:sub(2, -1) end - str = gsub(str, "^_(_*%d.*)", "%1") str = gsub(str, "_", " ") str = gsub(str, "x([0-9A-F][0-9A-F])", function(hex) return char(tonumber(hex, 16)) -- cgit v1.2.3 From b615cb5c8e638cffe77bbe5cb86c9362e2b2fc18 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 3 Oct 2018 16:14:17 -0700 Subject: Fixed up some edge cases with as_lua_id and from_lua_id that were producing bad results. --- string2.lua | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'string2.lua') 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 -- cgit v1.2.3 From 2f68357cb6800e97edd31abfc707d7c7905faa64 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 3 Oct 2018 16:26:24 -0700 Subject: Some incremental progress. --- string2.lua | 1 - 1 file changed, 1 deletion(-) (limited to 'string2.lua') diff --git a/string2.lua b/string2.lua index ae0e49d..1cb89ba 100644 --- a/string2.lua +++ b/string2.lua @@ -189,5 +189,4 @@ for _index_0 = 1, #_list_0 do 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 -- cgit v1.2.3 From be1df7ccd3fb5352ca666129aee93c56b5b27b40 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 3 Oct 2018 16:26:50 -0700 Subject: Recompile --- string2.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'string2.lua') diff --git a/string2.lua b/string2.lua index 1cb89ba..8e13171 100644 --- a/string2.lua +++ b/string2.lua @@ -187,6 +187,6 @@ for _index_0 = 1, #_list_0 do 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)) + assert(roundtrip == test, "Failed lua_id roundtrip: '" .. tostring(test) .. "' -> '" .. tostring(lua_id) .. "' -> '" .. tostring(roundtrip) .. "'") end return string2 -- cgit v1.2.3 From 307dea18815ba4a06a3098edb170d7ad90708815 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 2 Nov 2018 14:38:24 -0700 Subject: Changed stub convention to (foo 1 baz 2) -> foo_1_baz instead of foo_1_baz_2, removed "smext", made some cleanup changes. --- string2.lua | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'string2.lua') diff --git a/string2.lua b/string2.lua index 8e13171..cf86026 100644 --- a/string2.lua +++ b/string2.lua @@ -78,6 +78,9 @@ local string2 = { end return _accum_0 end, + starts_with = function(self, s) + return sub(self, 1, #s) == s + end, lines = function(self) local _accum_0 = { } local _len_0 = 1 @@ -114,10 +117,10 @@ local string2 = { for _index_0 = 1, #_list_0 do local line = _list_0[_index_0] while #line > maxlen do - local chunk = line:sub(1, maxlen) - local split = chunk:find(' ', maxlen - buffer, true) or maxlen - chunk = line:sub(1, split) - line = line:sub(split + 1, -1) + local chunk = sub(line, 1, maxlen) + local split = find(chunk, ' ', maxlen - buffer, true) or maxlen + chunk = sub(line, 1, split) + line = sub(line, split + 1, -1) lines[#lines + 1] = chunk end lines[#lines + 1] = line @@ -151,14 +154,14 @@ local string2 = { return format("x%02X", byte(c)) end end) - if not (is_lua_id(str:match("^_*(.*)$"))) then + if not (is_lua_id(match(str, "^_*(.*)$"))) then str = "_" .. str end return str end, from_lua_id = function(str) - if not (is_lua_id(str:match("^_*(.*)$"))) then - str = str:sub(2, -1) + if not (is_lua_id(match(str, "^_*(.*)$"))) then + str = sub(str, 2, -1) end str = gsub(str, "_", " ") str = gsub(str, "x([0-9A-F][0-9A-F])", function(hex) -- cgit v1.2.3 From dc41f30c73c9686685e3a4183c1213fb4ba55c90 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 2 Nov 2018 15:10:17 -0700 Subject: Tweak --- string2.lua | 3 +++ 1 file changed, 3 insertions(+) (limited to 'string2.lua') diff --git a/string2.lua b/string2.lua index cf86026..09cb5ea 100644 --- a/string2.lua +++ b/string2.lua @@ -81,6 +81,9 @@ local string2 = { starts_with = function(self, s) return sub(self, 1, #s) == s end, + ends_with = function(self, s) + return #self >= #s and sub(self, #self - #s, -1) == s + end, lines = function(self) local _accum_0 = { } local _len_0 = 1 -- cgit v1.2.3