diff options
author | Philipp Janda <siffiejoe@gmx.net> | 2015-06-02 15:14:31 +0200 |
---|---|---|
committer | Philipp Janda <siffiejoe@gmx.net> | 2015-06-02 15:14:31 +0200 |
commit | 823345b8b4de9e2a646dbcd471e05a1c7598d378 (patch) | |
tree | ab4f94969bedef33b6ed90747a6c5110486d4832 /compat53 | |
parent | ba065189ae67822ac9c69cc6ba633e3e8e123ef6 (diff) | |
download | lua-compat-5.3-823345b8b4de9e2a646dbcd471e05a1c7598d378.tar.gz lua-compat-5.3-823345b8b4de9e2a646dbcd471e05a1c7598d378.tar.bz2 lua-compat-5.3-823345b8b4de9e2a646dbcd471e05a1c7598d378.zip |
Compress decimal escapes for string.format("%q").
Lua 5.2+ escape all control characters using decimal escape
sequences. The previous code did the same but always used three
decimals (possibly zero-padded) no matter the following character.
The new version only uses all three decimals when another decimal
character is following.
Diffstat (limited to 'compat53')
-rw-r--r-- | compat53/module.lua | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/compat53/module.lua b/compat53/module.lua index f278f13..56c9932 100644 --- a/compat53/module.lua +++ b/compat53/module.lua | |||
@@ -671,8 +671,8 @@ if lua_version < "5.3" then | |||
671 | ["\""] = "\\\"" | 671 | ["\""] = "\\\"" |
672 | } | 672 | } |
673 | 673 | ||
674 | local function addquoted(c) | 674 | local function addquoted(c, d) |
675 | return addqt[c] or string_format("\\%03d", c:byte()) | 675 | return (addqt[c] or string_format(d~="" and "\\%03d" or "\\%d", c:byte()))..d |
676 | end | 676 | end |
677 | 677 | ||
678 | function M.string.format(fmt, ...) | 678 | function M.string.format(fmt, ...) |
@@ -684,7 +684,7 @@ if lua_version < "5.3" then | |||
684 | if kind == "s" then | 684 | if kind == "s" then |
685 | args[i] = _G.tostring(args[i]) | 685 | args[i] = _G.tostring(args[i]) |
686 | elseif kind == "q" then | 686 | elseif kind == "q" then |
687 | args[i] = '"'..string_gsub(args[i], "[%z%c\\\"\n]", addquoted)..'"' | 687 | args[i] = '"'..string_gsub(args[i], "([%z%c\\\"\n])(%d?)", addquoted)..'"' |
688 | return lead.."%"..mods.."s" | 688 | return lead.."%"..mods.."s" |
689 | end | 689 | end |
690 | end | 690 | end |