Escaped quotes not accounted for
carn1x opened this issue · 9 comments
I have the following snippet from some LUA code:
UnitName = '<LOC ope3001_name>Arnold\'s Black Box',
The ' appears to throw off the entire operation as the single quote still ends the string. I've hacked line 117 to the following to solve this for my purposes, however I'm not confident in my own knowledge of the entirety of slpp to know if this is best practice or buggy:
FROM
if self.ch == end:
TO
if self.ch == end and s[-1] != '\\\':
The last snipped broke empty strings, so I've revised it to:
if self.ch == end and (not len(s) or s[-1] != '\\'):
Thanks. Fixed the main problem, but due to python's string mechanics, it will not work with python-escaped string literals, only raw strings.
In "__encode", escaped characters are not taken into account.
Also, for an unknown reason, multi-lines string are getting more newlines than expected at "__encode".
Here a quick'n dirty fix that works for me so far:
if isinstance(obj, str):
if self.newline in obj:
obj = "{}{}".format(self.newline.join([s for s in obj.splitlines() if s]), self.newline)
s += '"%s"' % obj.replace('"', '\\"')
Can you provide some general examples of it? I'll add it to tests.
This (stripped) table:
["actions"] =
{
...
[2] = "a_do_script_file(\"1.container.lua\");a_do_script_file(\"1.gather_spawnable_units.lua\");a_do_script_file(\"1.spawn_random_group.lua\");a_do_script_file(\"1.spawner.lua\");a_do_script_file(\"1.count_blue_units_in_zone.lua\");a_do_script_file(\"1.outcome.lua\");a_do_script_file(\"1.count_alive_red_units.lua\");a_do_script_file(\"2.main.lua\"); mission.trig.func[2]=nil;",
...
[23] = "a_do_script(\"e.addKa()\");a_clear_flag(501);",
[24] = "a_do_script(\"e.addMi()\");a_clear_flag(502);",
[25] = "a_do_script(\"e.destroy_all_spawned()\");a_clear_flag(503);",
}, -- end of ["actions"]
will decode fine, but when encoded, the backslashes will not be added in front of the quotes.
Also this multi-lines string:
["descriptionText"] = "L'escadron est stationné à Mineralnye.\
\
20km au sud se trouve la ville de Pyatigorsk, sur le point d'être prise d'assaut par une compagnie aéroportée.\
",
will decode fine, but when encoded, an extra blank line would be added after each backslash, resulting in:
["descriptionText"] = "L'escadron est stationné à Mineralnye.\
\
20km au sud se trouve la ville de Pyatigorsk, sur le point d'être prise d'assaut par une compagnie aéroportée.\
",
, which will not compile in lua. I'm not sure of the cause, maybe it's related to UNIX/Windows newline conversion, but the trick I used works for both binary and text file reading / writing.
You have newline escape (\n) at the end of your string, guessed as multiline. If you really mean this (did not lost some spaces symbols at the end of line in this example), It must be treated as single line, and not as multiline. I'll have thought of how it must be treated correctly. If you have some proposals, I'll consider it gladly.
I cannot reproduce your issue. You mentioned about non-unix newline symbols, do you use it? It really can be root of the problem.
Yes, I just tested in a pure UNIX environment, and it behaves absolutely fine, so the problem does not come from SLPP, please excuse me for wasting your time.
On another hand, can you confirm the issue concerning the quotes not being escaped by "__encode()", or is it just another one of my derps ? =)
I fixed it in previous commit, thanks.