diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2018-10-03 16:14:17 -0700 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2018-10-03 16:14:37 -0700 |
| commit | b615cb5c8e638cffe77bbe5cb86c9362e2b2fc18 (patch) | |
| tree | 724481109f64631dd92b575ed9a262bba46424f1 /string2.moon | |
| parent | 331b22b3a3b61313a65ea6c49e2930b67f7ce82b (diff) | |
Fixed up some edge cases with as_lua_id and from_lua_id that were
producing bad results.
Diffstat (limited to 'string2.moon')
| -rw-r--r-- | string2.moon | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/string2.moon b/string2.moon index 140c586..8d2f3f9 100644 --- a/string2.moon +++ b/string2.moon @@ -67,10 +67,6 @@ string2 = { -- 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). as_lua_id: (str)-> - orig = str - -- Empty strings are not valid lua identifiers, so treat them like " ", - -- and treat " " as " ", etc. to preserve injectivity. - str = gsub str, "^ *$", "%1 " -- Escape 'x' (\x78) when it precedes something that looks like an uppercase hex sequence. -- This way, all Lua IDs can be unambiguously reverse-engineered, but normal usage -- of 'x' won't produce ugly Lua IDs. @@ -88,13 +84,18 @@ string2 = { -- from_lua_id(as_lua_id(str)) == str, but behavior is unspecified for inputs that -- did not come from as_lua_id() from_lua_id: (str)-> - unless is_lua_id("^_+(.*)$") + unless is_lua_id(str\match("^_*(.*)$")) str = str\sub(2,-1) str = gsub(str, "_", " ") str = gsub(str, "x([0-9A-F][0-9A-F])", (hex)-> char(tonumber(hex, 16))) - str = gsub(str, "^ ([ ]*)$", "%1") return str } for k,v in pairs(string) do string2[k] or= v +for test in *{"", "_", " ", "return", "asdf", "one two", "one_two", "Hex2Dec", "He-ec", "\3"} + lua_id = string2.as_lua_id(test) + assert is_lua_id(lua_id), "failed to convert '#{test}' to a valid Lua identifier (got '#{lua_id}')" + roundtrip = string2.from_lua_id(lua_id) + assert roundtrip == test, "Failed lua_id roundtrip: '#{test}' -> #{lua_id} -> #{roundtrip}" + return string2 |
