diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2018-11-02 15:17:48 -0700 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2018-11-02 15:17:49 -0700 |
| commit | 0f17c5eb9ac4660f2f969bd1e67af42713e45eac (patch) | |
| tree | 279ca7da2de0efe2f363684f3c84a540635f11a8 /string2.lua | |
| parent | acd9c2acd4688f2301b091daad910c04e402bd6a (diff) | |
| parent | dc41f30c73c9686685e3a4183c1213fb4ba55c90 (diff) | |
Merge branch 'master' into working
Diffstat (limited to 'string2.lua')
| -rw-r--r-- | string2.lua | 123 |
1 files changed, 78 insertions, 45 deletions
diff --git a/string2.lua b/string2.lua index 5995de2..09cb5ea 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, @@ -73,6 +78,12 @@ local string2 = { end return _accum_0 end, + 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 @@ -109,19 +120,35 @@ 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 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 ") str = gsub(str, "x([0-9A-F][0-9A-F])", "x78%1") str = gsub(str, "%W", function(c) if c == ' ' then @@ -130,36 +157,42 @@ 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(match(str, "^_*(.*)$"))) 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(match(str, "^_*(.*)$"))) then + str = sub(str, 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)) 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 return string2 |
