aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Janda <siffiejoe@gmx.net>2015-06-02 15:14:31 +0200
committerPhilipp Janda <siffiejoe@gmx.net>2015-06-02 15:14:31 +0200
commit823345b8b4de9e2a646dbcd471e05a1c7598d378 (patch)
treeab4f94969bedef33b6ed90747a6c5110486d4832
parentba065189ae67822ac9c69cc6ba633e3e8e123ef6 (diff)
downloadlua-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.
-rw-r--r--compat53/module.lua6
-rwxr-xr-xtests/test.lua2
2 files changed, 4 insertions, 4 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
diff --git a/tests/test.lua b/tests/test.lua
index f8fa894..9ac27d4 100755
--- a/tests/test.lua
+++ b/tests/test.lua
@@ -505,7 +505,7 @@ do
505 return _tostring(v) 505 return _tostring(v)
506 end 506 end
507 end 507 end
508 print("string.format()", string.format("%q", "\"\\\0000\0010\r0\n0\t0\"")) 508 print("string.format()", string.format("%q", "\"\\\0000\0010\002\r\n0\t0\""))
509 print("string.format()", string.format("%12.3fx%%sxx%.6s", 3.1, {})) 509 print("string.format()", string.format("%12.3fx%%sxx%.6s", 3.1, {}))
510 print("string.format()", string.format("%-3f %%%s %%s", 3.1, true)) 510 print("string.format()", string.format("%-3f %%%s %%s", 3.1, true))
511 print("string.format()", string.format("% 3.2g %%d %%%s", 3.1, nil)) 511 print("string.format()", string.format("% 3.2g %%d %%%s", 3.1, nil))